scsi: scsi_transport_sas: fix error handling in sas_rphy_add()
Subsystems (2)
PATCH v2 1/1 scsi: scsi_transport_sas: fix error handling in sas_rphy_add()
In sas_rphy_add(), if transport_add_device() fails, the device is not added, the return value is not checked, it won't goto error path, when removing rphy in normal remove path, it causes null-ptr-deref, because transport_remove_device() is called to remove the device that was not added.
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000108 pc : device_del+0x54/0x3d0 lr : device_del+0x37c/0x3d0 Call trace: device_del+0x54/0x3d0 attribute_container_class_device_del+0x28/0x38 transport_remove_classdev+0x6c/0x80 attribute_container_device_trigger+0x108/0x110 transport_remove_device+0x28/0x38 sas_rphy_remove+0x50/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_rphy_remove+0x38/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_remove_host+0x20/0x38 [scsi_transport_sas] scsih_remove+0xd8/0x420 [mpt3sas]
Fix this by checking and handling return value of transport_add_device() in sas_rphy_add().
---
v1 -> v2:
Update commit message.
--- drivers/scsi/scsi_transport_sas.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/scsi_transport_sas.c +5 -1
@@ -1526,7 +1526,11 @@ int sas_rphy_add(struct sas_rphy *rphy) error =device_add (&rphy->dev);if (error)return error; -transport_add_device (&rphy->dev); + error =transport_add_device (&rphy->dev); +if (error) { +device_del (&rphy->dev); +return error; + }transport_configure_device (&rphy->dev);if (sas_bsg_initialize (shost, rphy))printk ("fail to a bsg device %s\n" ,dev_name (&rphy->dev));
Signature · 1 line
2.25.1
Quoted 59 lines · Yang Yingliang
In
sas_rphy_add(), iftransport_add_device()fails, the device is not added, the return value is not checked, it won't goto error path, when removing rphy in normal remove path, it causes null-ptr-deref, becausetransport_remove_device()is called to remove the device that was not added.Unable to handle kernel NULL pointer dereference at virtual address 0000000000000108 pc : device_del+0x54/0x3d0 lr : device_del+0x37c/0x3d0 Call trace: device_del+0x54/0x3d0 attribute_container_class_device_del+0x28/0x38 transport_remove_classdev+0x6c/0x80 attribute_container_device_trigger+0x108/0x110 transport_remove_device+0x28/0x38 sas_rphy_remove+0x50/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_rphy_remove+0x38/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_remove_host+0x20/0x38 [scsi_transport_sas] scsih_remove+0xd8/0x420 [mpt3sas]Fix this by checking and handling return value of
transport_add_device()
insas_rphy_add().Fixes c7ebbbce366c ("[SCSI] SAS transport class")Signed-off-by Yang Yingliang <[email protected]>---
v1 -> v2:
Update commit message.
---
drivers/scsi/scsi_transport_sas.c | 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)drivers/scsi/scsi_transport_sas.c +5
-1@@ -1526,7 +1526,11 @@ int sas_rphy_add(struct sas_rphy *rphy) error = device_add(&rphy->dev); if (error) return error; - transport_add_device(&rphy->dev); + error = transport_add_device(&rphy->dev); + if (error) { + device_del(&rphy->dev); + return error; + } transport_configure_device(&rphy->dev); if (sas_bsg_initialize(shost, rphy))
Quoted 2 lines
printk("fail to a bsg device %s\n", dev_name(&rphy-
Quoted 1 line
dev));
There is a slight problem with doing this in that if transport_device_add() ever fails it's likely because memory pressure caused the allocation of the internal_container to fail. What that means is that the visible sysfs attributes don't get added, but otherwise the rphy is fully functional as far as the driver sees it, so this condition doesn't have to be a fatal error which kills the device.
There are two ways of handling this:
1. The above to move the condition from an ignored to a fatal error. It's so rare that we almost never see it in practice and if it ever happened, the machine is so low on memory that something else is bound to fail an allocation and kill the device anyway, so treating it as non-fatal likely serves no purpose.
2. Simply to make the assumption that transport_remove_device() is idempotent true by adding a flag in the internal_class to signify removal is required. This would preserve current behaviour and have the bonus that it only requires a single patch, not one patch per transport class object that has this problem.
I'd probably prefer 2. since it's way less work, but others might have different opinions.
James
+Cc: Greg
Hi Greg,
Quoted 82 lines · James Bottomley
Quoted 60 lines · Yang Yingliang
In
sas_rphy_add(), iftransport_add_device()fails, the device is not added, the return value is not checked, it won't goto error path, when removing rphy in normal remove path, it causes null-ptr-deref, becausetransport_remove_device()is called to remove the device that was not added.Unable to handle kernel NULL pointer dereference at virtual address 0000000000000108 pc : device_del+0x54/0x3d0 lr : device_del+0x37c/0x3d0 Call trace: device_del+0x54/0x3d0 attribute_container_class_device_del+0x28/0x38 transport_remove_classdev+0x6c/0x80 attribute_container_device_trigger+0x108/0x110 transport_remove_device+0x28/0x38 sas_rphy_remove+0x50/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_rphy_remove+0x38/0x78 [scsi_transport_sas] sas_port_delete+0x30/0x148 [scsi_transport_sas] do_sas_phy_delete+0x78/0x80 [scsi_transport_sas] device_for_each_child+0x68/0xb0 sas_remove_children+0x30/0x50 [scsi_transport_sas] sas_remove_host+0x20/0x38 [scsi_transport_sas] scsih_remove+0xd8/0x420 [mpt3sas]Fix this by checking and handling return value of
transport_add_device()
insas_rphy_add().Fixes c7ebbbce366c ("[SCSI] SAS transport class")Signed-off-by Yang Yingliang <[email protected]>---
v1 -> v2:
Update commit message.--- drivers/scsi/scsi_transport_sas.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/scsi/scsi_transport_sas.c +5
-1@@ -1526,7 +1526,11 @@ int sas_rphy_add(struct sas_rphy *rphy) error = device_add(&rphy->dev); if (error) return error; - transport_add_device(&rphy->dev); + error = transport_add_device(&rphy->dev); + if (error) { + device_del(&rphy->dev); + return error; + } transport_configure_device(&rphy->dev); if (sas_bsg_initialize(shost, rphy)) printk("fail to a bsg device %s\n", dev_name(&rphy-Quoted 1 line
Quoted 1 line
dev));
There is a slight problem with doing this in that if
transport_device_add()ever fails it's likely because memory pressure caused the allocation of theinternal_containerto fail. What that means is that the visible sysfs attributes don't get added, but otherwise the rphy is fully functional as far as the driver sees it, so this condition doesn't have to be a fatal error which kills the device.There are two ways of handling this:
1. The above to move the condition from an ignored to a fatal error. It's so rare that we almost never see it in practice and if it ever happened, the machine is so low on memory that something else is bound to fail an allocation and kill the device anyway, so treating it as non-fatal likely serves no purpose.
2. Simply to make the assumption thattransport_remove_device()is idempotent true by adding a flag in theinternal_classto signify removal is required. This would preserve current behaviour and have the bonus that it only requires a single patch, not one patch per transport class object that has this problem.
Quoted 2 lines
I'd probably prefer 2. since it's way less work, but others might have different opinions.
Current some callers ignore the return value of transport_add_device(), if it fails, it will cause null-ptr-deref in transport_remove_device().
James suggested that add some check in transport_remove_device(), so all can be fix in one patch.
Do you have any suggestion for this ?
Thanks,
Yang
Quoted 4 lines
James
.
Quoted 23 lines · Yang Yingliang
Quoted 21 lines
Quoted 1 line
Quoted 1 line
);
There is a slight problem with doing this in that if
transport_device_add()ever fails it's likely because memory pressure caused the allocation of theinternal_containerto fail. What that means is that the visible sysfs attributes don't get added, but otherwise the rphy is fully functional as far as the driver sees it, so this condition doesn't have to be a fatal error which kills the device.There are two ways of handling this:
1. The above to move the condition from an ignored to a fatal error. It's so rare that we almost never see it in practice and if it ever happened, the machine is so low on memory that something else is bound to fail an allocation and kill the device anyway, so treating it as non-fatal likely serves no purpose.
2. Simply to make the assumption thattransport_remove_device()is idempotent true by adding a flag in theinternal_classto signify removal is required. This would preserve current behaviour and have the bonus that it only requires a single patch, not one patch per transport class object that has this problem.Quoted 2 lines
I'd probably prefer 2. since it's way less work, but others might have different opinions.
Quoted 7 lines
Current some callers ignore the return value of
transport_add_device(), if it fails, it will cause null-ptr-deref intransport_remove_device().James suggested that add some check in
transport_remove_device(), so all can be fix in one patch.Do you have any suggestion for this ?
Personally I prefer 1. However did you develop a prototype patch for how 2. would look? And how many changes are still required for 1.?
Thanks,
John
Quoted 27 lines · John Garry
Quoted 26 lines · Yang Yingliang
Quoted 21 lines
Quoted 1 line
Quoted 1 line
);
There is a slight problem with doing this in that if
transport_device_add()ever fails it's likely because memory pressure caused the allocation of theinternal_containerto fail. What that means is that the visible sysfs attributes don't get added, but otherwise the rphy is fully functional as far as the driver sees it, so this condition doesn't have to be a fatal error which kills the device.There are two ways of handling this:
1. The above to move the condition from an ignored to a fatal error. It's so rare that we almost never see it in practice and if it ever happened, the machine is so low on memory that something else is bound to fail an allocation and kill the device anyway, so treating it as non-fatal likely serves no purpose.
2. Simply to make the assumption thattransport_remove_device()is idempotent true by adding a flag in theinternal_classto signify removal is required. This would preserve current behaviour and have the bonus that it only requires a single patch, not one patch per transport class object that has this problem.Quoted 2 lines
I'd probably prefer 2. since it's way less work, but others might have different opinions.
Current some callers ignore the return value of
transport_add_device(), if it fails, it will cause null-ptr-deref intransport_remove_device().
Quoted 6 lines
Quoted 4 lines
James suggested that add some check in
transport_remove_device(), so all can be fix in one patch.Do you have any suggestion for this ?
Personally I prefer 1. However did you develop a prototype patch for how 2. would look? And how many changes are still required for 1.?
For 1, in total, there are 8 places need be checked in drivers/scsi/scsi_transport_sas.c, 2 places in drivers/scsi/scsi_sysfs.c, 3 places
in drivers/scsi/scsi_transport_fc.c, 2 places in drivers/scsi/scsi_transport_srp.c, 1 place
For 2, I think we can use device_is_registered() to check if add operation is successful, may be like this (not test yet):
drivers/base/transport_class.c +0 -0
@@ -227,9 +227,11 @@ static int transport_remove_classdev(struct attribute_container *cont,
tclass->remove(tcont, dev, classdev);
if (tclass->remove != anon_transport_dummy_function) { - if (tcont->statistics)
- sysfs_remove_group(&classdev->kobj, tcont->statistics); - attribute_container_class_device_del(classdev);
+ if (device_is_registered(classdev)) {
+ if (tcont->statistics)
+ sysfs_remove_group(&classdev->kobj, tcont->statistics); + attribute_container_class_device_del(classdev); + }
}
return 0;
Thanks,
Yang
Quoted 4 lines
Thanks,
John
.
Quoted 6 lines · Yang Yingliang
Quoted 1 line
Personally I prefer 1. However did you develop a prototype patch for how 2. would look? And how many changes are still required for 1.?
For 1, in total, there are 8 places need be checked in
drivers/scsi/scsi_transport_sas.c, 2 places indrivers/scsi/scsi_sysfs.c, 3 places
indrivers/scsi/scsi_transport_fc.c, 2 places indrivers/scsi/scsi_transport_srp.c, 1 place
and in linux-next there are 4x places which do already check the return code...
Not sure what's best to do. I'll leave it to James' wisdom.
However, we do seem to have a common pattern:
error = device_add(dev);
if (error)
return error;
transport_add_device(dev);transport_configure_device(dev);
Could we make an "add" (which does as above pattern) and "remove" helper? It might simplify things such that we not only fixing the possible crash but also reducing code.
Thanks,
John
Quoted 23 lines
For 2, I think we can use
device_is_registered()to check if add operation is successful, may be like this (not test yet):drivers/base/transport_class.c +5
-3@@ -227,9 +227,11 @@ static int transport_remove_classdev(structattribute_container *cont, tclass->remove(tcont, dev, classdev); if (tclass->remove != anon_transport_dummy_function) { - if (tcont->statistics) - sysfs_remove_group(&classdev->kobj, tcont->statistics); - attribute_container_class_device_del(classdev); + if (device_is_registered(classdev)) { + if (tcont->statistics) + sysfs_remove_group(&classdev->kobj, tcont->statistics); + attribute_container_class_device_del(classdev); + } }return 0;