misc: amd-sbi: Fix use-after-free on driver unbind
PATCH 1/1 misc: amd-sbi: Fix use-after-free on driver unbind
From: Prathima L K <[email protected]>
sbrmi_data was allocated with devm_kzalloc() and freed at unbind while an open /dev/sbrmi-* fd could still reference it, leading to a use-after-free on ioctl after removal.
Refcount sbrmi_data with a kref: take a reference in open(), drop it in release(), and free the structure only when the last fd closes. Mark the device 'detached' under lock when the misc device is deregistered so all ioctl/xfer paths return -ENODEV. Move misc deregistration to a devm action and drop the now-unneeded i2c/i3c ".remove" callbacks.
--- drivers/misc/amd-sbi/rmi-core.c | 54 +++++++++++++++++++++++++++++++++ drivers/misc/amd-sbi/rmi-core.h | 6 ++++ drivers/misc/amd-sbi/rmi-i2c.c | 54 +++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 23 deletions(-)
drivers/misc/amd-sbi/rmi-core.c +54 -0
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regmap.h> +#include <linux/slab.h> #include "rmi-core.h" /* Mask for Status Register bit[1] */
@@ -193,6 +194,10 @@ static int rmi_cpuid_read(struct sbrmi_data *data,u16 thread;mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock; + }/* cache the rev value to identify if protocol is supported or not */ if (!data->rev) { ret =sbrmi_get_rev (data);
@@ -307,6 +312,10 @@ static int rmi_mca_msr_read(struct sbrmi_data *data,u16 thread;mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock; + }/* cache the rev value to identify if protocol is supported or not */ if (!data->rev) { ret =sbrmi_get_rev (data);
@@ -384,6 +393,10 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,u8 byte;mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock; + } msg->fw_ret_code =0 ;
@@ -475,6 +488,10 @@ static int apml_rmi_reg_xfer(struct sbrmi_data *data,return -EFAULT ;mutex_lock (&data->lock); +if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + }if (msg.rflag) { ret =regmap_read (data->regmap, msg.reg_addr, &data_read);if (!ret)
@@ -548,6 +565,41 @@ static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __usreturn ret; } +void sbrmi_data_release (struct kref *kref) +{ + struct sbrmi_data *data = container_of(kref, struct sbrmi_data, kref); + +mutex_destroy (&data->lock); +kfree (data); +} + +static int sbrmi_open (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); + +mutex_lock (&data->lock); +if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + } +mutex_unlock (&data->lock); + +kref_get (&data->kref); + +return 0 ; +} + +static int sbrmi_release (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); +kref_put (&data->kref, sbrmi_data_release); +return 0 ; +} +static long sbrmi_ioctl (struct file *fp,unsigned int cmd,unsigned long arg) {void __user *argp = (void __user *)arg;
@@ -570,6 +622,8 @@ static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)static const struct file_operations sbrmi_fops = { .owner =THIS_MODULE , + .open = sbrmi_open, + .release = sbrmi_release, .unlocked_ioctl = sbrmi_ioctl, .compat_ioctl = compat_ptr_ioctl, };
drivers/misc/amd-sbi/rmi-core.h +6 -0
@@ -9,6 +9,7 @@#include <linux/miscdevice.h> #include <linux/mutex.h> #include <linux/i2c.h> +#include <linux/kref.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <uapi/misc/amd-apml.h>
@@ -56,9 +57,13 @@ struct sbrmi_data {struct regmap *regmap;/* Mutex locking */ struct mutex lock; +/* Reference count; keeps @sbrmi_data alive while misc fds are open */ +struct kref kref;u32 pwr_limit_max;u8 dev_static_addr;u8 rev; +/* Set on driver unbind; open/ioctl return -ENODEV afterward */ +bool detached; };int rmi_mailbox_xfer (struct sbrmi_data *data,struct apml_mbox_msg *msg);
@@ -71,4 +76,5 @@ static inline int create_hwmon_sensor_device(struct device *dev, struct sbrmi_da }#endif int create_misc_rmi_device (struct sbrmi_data *data,struct device *dev); +void sbrmi_data_release (struct kref *kref);#endif /*_SBRMI_CORE_H_*/
drivers/misc/amd-sbi/rmi-i2c.c +31 -23
106 lines not on this page · raw message
Signature · 1 line
2.34.1
Greg KH
maintainerQuoted 6 lines · Akshay Gupta
From: Prathima L K <[email protected]>
sbrmi_datawas allocated withdevm_kzalloc()and freed at unbind while an open/dev/sbrmi-*fd could still reference it, leading to a use-after-free on ioctl after removal.
Quoted 7 lines
Refcount
sbrmi_datawith a kref: take a reference inopen(), drop it inrelease(), and free the structure only when the last fd closes. Mark the device 'detached' under lock when the misc device is deregistered so all ioctl/xfer paths return-ENODEV. Move misc deregistration to a devm action and drop the now-unneeded i2c/i3c ".remove" callbacks.Fixes 35ac2034db72bbbc73609aab5f05ff6e0d38fdd0 ("misc: amd-sbi: Add support for AMD_SBI IOCTL")
The documentation shows how to properly format this line, please fix it up, and don't put a blank line between it and these:
Quoted 2 lines
Reviewed-by Akshay Gupta <[email protected]>Signed-off-by Akshay Gupta <[email protected]>
signed-off-by implies review.
Quoted 1 line
Signed-off-by Prathima L K <[email protected]>
Wait, this is out of order, yours should be below this, right?
Quoted 19 lines
--- drivers/misc/amd-sbi/rmi-core.c | 54 +++++++++++++++++++++++++++++++++ drivers/misc/amd-sbi/rmi-core.h | 6 ++++ drivers/misc/amd-sbi/rmi-i2c.c | 54 +++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 23 deletions(-)drivers/misc/amd-sbi/rmi-core.c +1
-0@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regmap.h> +#include <linux/slab.h> #include "rmi-core.h" /* Mask for Status Register bit[1] */
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 199
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c +3
-0@@ -193,6 +194,10 @@ static int rmi_cpuid_read(struct sbrmi_data *data,u16 thread;mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock;
Why isn't a guard being used here instead? Then you can do a simple "return -ENODEV", right?
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 582
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+static int sbrmi_open (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); + +mutex_lock (&data->lock);
Again, please use a guard.
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 589
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + } +mutex_unlock (&data->lock); + +kref_get (&data->kref);
Are you attempting to use a kref without a lock? That way lies madness and is sure to be wrong.
Are you attempting to keep the data "alive" somehow? Are you sure this is correct?
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 599
Quoted 10 lines
drivers/misc/amd-sbi/rmi-core.c +10
-0+ +return 0 ; +} + +static int sbrmi_release (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); +kref_put (&data->kref, sbrmi_data_release);
If open is the only thing that can get the reference to the kref, and this is the thing that drops it, why is the kref needed at all?
Quoted 28 lines
drivers/misc/amd-sbi/rmi-core.c +5
-0+return 0 ; +} +static long sbrmi_ioctl (struct file *fp,unsigned int cmd,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -570,6 +622,8 @@ static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)static const struct file_operations sbrmi_fops = { .owner =THIS_MODULE , + .open = sbrmi_open, + .release = sbrmi_release, .unlocked_ioctl = sbrmi_ioctl, .compat_ioctl = compat_ptr_ioctl, };drivers/misc/amd-sbi/rmi-core.h +1
-0@@ -9,6 +9,7 @@#include <linux/miscdevice.h> #include <linux/mutex.h> #include <linux/i2c.h> +#include <linux/kref.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <uapi/misc/amd-apml.h>
drivers/misc/amd-sbi/rmi-core.h struct sbrmi_data { · line 66
Quoted 10 lines
drivers/misc/amd-sbi/rmi-core.h +4
-0@@ -56,9 +57,13 @@ struct sbrmi_data {struct regmap *regmap;/* Mutex locking */ struct mutex lock; +/* Reference count; keeps @sbrmi_data alive while misc fds are open */ +struct kref kref;u32 pwr_limit_max;u8 dev_static_addr;u8 rev; +/* Set on driver unbind; open/ioctl return -ENODEV afterward */ +bool detached;
How can open/ioctl even run if the driver is unloaded? You aren't using the cdev api properly I think...
thanks,
greg k-h
Quoted 16 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 12 lines · Akshay Gupta
From: Prathima L K <[email protected]>
sbrmi_datawas allocated withdevm_kzalloc()and freed at unbind while an open/dev/sbrmi-*fd could still reference it, leading to a use-after-free on ioctl after removal.Refcount
sbrmi_datawith a kref: take a reference inopen(), drop it inrelease(), and free the structure only when the last fd closes. Mark the device 'detached' under lock when the misc device is deregistered so all ioctl/xfer paths return-ENODEV. Move misc deregistration to a devm action and drop the now-unneeded i2c/i3c ".remove" callbacks.
Quoted 3 lines
Quoted 1 line
Fixes 35ac2034db72bbbc73609aab5f05ff6e0d38fdd0 ("misc: amd-sbi: Add support for AMD_SBI IOCTL")The documentation shows how to properly format this line, please fix it up, and don't put a blank line between it and these:
Hi Greg,
Thank you for the feedback.
Will update the commit message in proper format.
Quoted 8 lines
Quoted 3 lines
Reviewed-by Akshay Gupta <[email protected]>Signed-off-by Akshay Gupta <[email protected]>signed-off-by implies review.
Quoted 1 line
Signed-off-by Prathima L K <[email protected]>Wait, this is out of order, yours should be below this, right?
I will drop my Reviewed-by, and fix the order
Quoted 22 lines
Quoted 22 lines
--- drivers/misc/amd-sbi/rmi-core.c | 54 +++++++++++++++++++++++++++++++++ drivers/misc/amd-sbi/rmi-core.h | 6 ++++ drivers/misc/amd-sbi/rmi-i2c.c | 54 +++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 23 deletions(-)drivers/misc/amd-sbi/rmi-core.c +1
-0@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regmap.h> +#include <linux/slab.h> #include "rmi-core.h" /* Mask for Status Register bit[1] */ @@ -193,6 +194,10 @@ static int rmi_cpuid_read(struct sbrmi_data *data,u16 thread;
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 199
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +3
-0mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock;Why isn't a guard being used here instead? Then you can do a simple "return
-ENODEV", right?
The existing code in rmi-core.c take data->lock with mutex_lock() and release through mutex_unlock() The new code added as part of this fix patch, follows this so patch stays a lifetime fix.
I can submit separate patch converting the mutex_lock/mutex_unlock() to guard(mutex) after the fix patch is merged. Let me know your opinion if you suggest to submit the guard_mutex() patch before the fix patch.
Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 582
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+static int sbrmi_open (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); + +mutex_lock (&data->lock);Again, please use a guard.
same as above.
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 589
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + } +mutex_unlock (&data->lock); + +kref_get (&data->kref);
Quoted 5 lines
Are you attempting to use a kref without a lock? That way lies madness and is sure to be wrong.
Are you attempting to keep the data "alive" somehow? Are you sure this is correct?
Yes, the kref is there to keep sbrmi_data alive. kref_get() in open() does not race with the final put. misc_open() calls ->open() while holding misc_mtx, and misc_deregister() takes that same mutex. The probe reference is dropped by a devm action that runs only after misc_deregister() has returned, so the object cannot reach zero while open() runs.kref_get() and kref_put() are atomic. I will add a comment at the kref_get() saying this.
Quoted 4 lines
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +4
-0+ +return 0 ; +} +
Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 599
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c +6
-0+static int sbrmi_release (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); +kref_put (&data->kref, sbrmi_data_release);If open is the only thing that can get the reference to the kref, and this is the thing that drops it, why is the kref needed at all?
open () is not the only holder, kref_init() is the reference owned by the driver binding and a devm action drops it on unbind. each open() takes another reference and drops it in release(). sbrmi_data is freed only when both are gone, which is what closes the window where devm frees the structure while an ioctl is still using it.
Quoted 34 lines
Quoted 34 lines
drivers/misc/amd-sbi/rmi-core.c +5
-0+return 0 ; +} +static long sbrmi_ioctl (struct file *fp,unsigned int cmd,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -570,6 +622,8 @@ static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)static const struct file_operations sbrmi_fops = { .owner =THIS_MODULE , + .open = sbrmi_open, + .release = sbrmi_release, .unlocked_ioctl = sbrmi_ioctl, .compat_ioctl = compat_ptr_ioctl, };drivers/misc/amd-sbi/rmi-core.h +2
-0@@ -9,6 +9,7 @@#include <linux/miscdevice.h> #include <linux/mutex.h> #include <linux/i2c.h> +#include <linux/kref.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <uapi/misc/amd-apml.h> @@ -56,9 +57,13 @@ struct sbrmi_data {struct regmap *regmap;/* Mutex locking */ struct mutex lock; +/* Reference count; keeps @sbrmi_data alive while misc fds are open */
Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.h struct sbrmi_data { · line 66
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.h +3
-0+struct kref kref;u32 pwr_limit_max;u8 dev_static_addr;u8 rev; +/* Set on driver unbind; open/ioctl return -ENODEV afterward */ +bool detached;How can open/ioctl even run if the driver is unloaded? You aren't using the cdev api properly I think...
This fix addresses driver unbind, including unbind through sysfs; it is not limited to module unload. misc_deregister() removes the miscdevice from the misc-device list and destroys its device node. Although it prevents new opens, it does not retain a reference to the driver-private sbrmi_data for files that are already open. Such files retain sbrmi_fops and private_data, so a later ioctl can still enter the driver.
Without the kref, unbind cleanup could free sbrmi_data while an open file still refers to its embedded miscdevice, causing a use-after-free on a subsequent ioctl. With the kref, unbind marks the device detached, deregisters the miscdevice, and drops the probe reference. Existing open files retain their references, and the object is freed only after the final close. Subsequent ioctl operations on those lingering descriptors observe detached and return -ENODEV.
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerQuoted 116 lines · Akshay
Quoted 16 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 12 lines · Akshay Gupta
From: Prathima L K <[email protected]>
sbrmi_datawas allocated withdevm_kzalloc()and freed at unbind while an open/dev/sbrmi-*fd could still reference it, leading to a use-after-free on ioctl after removal.Refcount
sbrmi_datawith a kref: take a reference inopen(), drop it inrelease(), and free the structure only when the last fd closes. Mark the device 'detached' under lock when the misc device is deregistered so all ioctl/xfer paths return-ENODEV. Move misc deregistration to a devm action and drop the now-unneeded i2c/i3c ".remove" callbacks.Quoted 3 lines
Quoted 1 line
Fixes 35ac2034db72bbbc73609aab5f05ff6e0d38fdd0 ("misc: amd-sbi: Add support for AMD_SBI IOCTL")The documentation shows how to properly format this line, please fix it up, and don't put a blank line between it and these:
Hi Greg,
Thank you for the feedback.
Will update the commit message in proper format.
Quoted 8 lines
Quoted 3 lines
Reviewed-by Akshay Gupta <[email protected]>Signed-off-by Akshay Gupta <[email protected]>signed-off-by implies review.
Quoted 1 line
Signed-off-by Prathima L K <[email protected]>Wait, this is out of order, yours should be below this, right?
I will drop my Reviewed-by, and fix the order
Quoted 22 lines
Quoted 22 lines
--- drivers/misc/amd-sbi/rmi-core.c | 54 +++++++++++++++++++++++++++++++++ drivers/misc/amd-sbi/rmi-core.h | 6 ++++ drivers/misc/amd-sbi/rmi-i2c.c | 54 +++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 23 deletions(-)drivers/misc/amd-sbi/rmi-core.c +1
-0@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regmap.h> +#include <linux/slab.h> #include "rmi-core.h" /* Mask for Status Register bit[1] */ @@ -193,6 +194,10 @@ static int rmi_cpuid_read(struct sbrmi_data *data,u16 thread;Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 199
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +3
-0mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock;Why isn't a guard being used here instead? Then you can do a simple "return
-ENODEV", right?The existing code in
rmi-core.ctakedata->lockwithmutex_lock()and release throughmutex_unlock()
The new code added as part of this fix patch, follows this so patch stays a lifetime fix.
I can submit separate patch converting the mutex_lock/mutex_unlock() to guard(mutex) after the fix patch is merged.
Let me know your opinion if you suggest to submit theguard_mutex()patch before the fix patch.Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 582
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+static int sbrmi_open (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); + +mutex_lock (&data->lock);Again, please use a guard.
same as above.
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 589
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + } +mutex_unlock (&data->lock); + +kref_get (&data->kref);Quoted 5 lines
Are you attempting to use a kref without a lock? That way lies madness and is sure to be wrong.
Are you attempting to keep the data "alive" somehow? Are you sure this is correct?
Yes, the kref is there to keep
sbrmi_dataalive.kref_get()inopen()does not race with the final put.misc_open()calls ->open() while holdingmisc_mtx, andmisc_deregister()takes that same mutex. The probe reference is dropped by a devm action that runs only aftermisc_deregister()has returned, so the object cannot reach zero whileopen()runs.kref_get()andkref_put()are atomic. I will add a comment at thekref_get()saying this.Quoted 4 lines
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +4
-0+ +return 0 ; +} +Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 599
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c +6
-0+static int sbrmi_release (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); +kref_put (&data->kref, sbrmi_data_release);If open is the only thing that can get the reference to the kref, and this is the thing that drops it, why is the kref needed at all?
Quoted 5 lines
open () is not the only holder,
kref_init()is the reference owned by the driver binding and a devm action drops it on unbind. eachopen()takes another reference and drops it inrelease().sbrmi_datais freed only when both are gone, which is what closes the window where devm frees the structure while an ioctl is still using it.
That's not good, this should be attached to the file descriptor. Don't go allocating things at bind as you will mess with the lifetime rules here very quickly, and it gets very messy. Rely on the rules that the vfs layer gives us and it will be much much simpler.
Quoted 43 lines
Quoted 34 lines
Quoted 34 lines
drivers/misc/amd-sbi/rmi-core.c +5
-0+return 0 ; +} +static long sbrmi_ioctl (struct file *fp,unsigned int cmd,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -570,6 +622,8 @@ static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)static const struct file_operations sbrmi_fops = { .owner =THIS_MODULE , + .open = sbrmi_open, + .release = sbrmi_release, .unlocked_ioctl = sbrmi_ioctl, .compat_ioctl = compat_ptr_ioctl, };drivers/misc/amd-sbi/rmi-core.h +2
-0@@ -9,6 +9,7 @@#include <linux/miscdevice.h> #include <linux/mutex.h> #include <linux/i2c.h> +#include <linux/kref.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <uapi/misc/amd-apml.h> @@ -56,9 +57,13 @@ struct sbrmi_data {struct regmap *regmap;/* Mutex locking */ struct mutex lock; +/* Reference count; keeps @sbrmi_data alive while misc fds are open */ Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.h struct sbrmi_data { · line 66
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.h +3
-0+struct kref kref;u32 pwr_limit_max;u8 dev_static_addr;u8 rev; +/* Set on driver unbind; open/ioctl return -ENODEV afterward */ +bool detached;How can open/ioctl even run if the driver is unloaded? You aren't using the cdev api properly I think...
Quoted 6 lines
This fix addresses driver unbind, including unbind through sysfs; it is not limited to module unload.
misc_deregister()removes the miscdevice from the misc-device list and destroys its device node. Although it prevents new opens, it does not retain a reference to the driver-privatesbrmi_datafor files that are already open. Such files retainsbrmi_fopsandprivate_data, so a later ioctl can still enter the driver.
unbind through sysfs is for developers and if things break, you get to keep the pieces when it happens (see the discussions on lkml about the recent taint flag added when this happens.)
So it's rare, but still, let's make this simpler by just moving the resource management to open/release instead of probe/remove.
thanks,
greg k-h
Quoted 176 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 111 lines · Akshay
Quoted 16 lines · Greg KH
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 12 lines · Akshay Gupta
From: Prathima L K <[email protected]>
sbrmi_datawas allocated withdevm_kzalloc()and freed at unbind while an open/dev/sbrmi-*fd could still reference it, leading to a use-after-free on ioctl after removal.Refcount
sbrmi_datawith a kref: take a reference inopen(), drop it inrelease(), and free the structure only when the last fd closes. Mark the device 'detached' under lock when the misc device is deregistered so all ioctl/xfer paths return-ENODEV. Move misc deregistration to a devm action and drop the now-unneeded i2c/i3c ".remove" callbacks.Quoted 3 lines
Quoted 1 line
Fixes 35ac2034db72bbbc73609aab5f05ff6e0d38fdd0 ("misc: amd-sbi: Add support for AMD_SBI IOCTL")The documentation shows how to properly format this line, please fix it up, and don't put a blank line between it and these:
Hi Greg,
Thank you for the feedback.
Will update the commit message in proper format.
Quoted 7 lines
Quoted 2 lines
Reviewed-by Akshay Gupta <[email protected]>Signed-off-by Akshay Gupta <[email protected]>signed-off-by implies review.
Quoted 1 line
Signed-off-by Prathima L K <[email protected]>Wait, this is out of order, yours should be below this, right?
I will drop my Reviewed-by, and fix the order
Quoted 21 lines
Quoted 21 lines
--- drivers/misc/amd-sbi/rmi-core.c | 54 +++++++++++++++++++++++++++++++++ drivers/misc/amd-sbi/rmi-core.h | 6 ++++ drivers/misc/amd-sbi/rmi-i2c.c | 54 +++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 23 deletions(-)drivers/misc/amd-sbi/rmi-core.c +1
-0@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regmap.h> +#include <linux/slab.h> #include "rmi-core.h" /* Mask for Status Register bit[1] */ @@ -193,6 +194,10 @@ static int rmi_cpuid_read(struct sbrmi_data *data,u16 thread;Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c static int rmi_cpuid_read(struct sbrmi_data *data, · line 199
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +3
-0mutex_lock (&data->lock); +if (data->detached) { + ret = -ENODEV ; + goto exit_unlock;Why isn't a guard being used here instead? Then you can do a simple "return
-ENODEV", right?The existing code in
rmi-core.ctakedata->lockwithmutex_lock()and release throughmutex_unlock()
The new code added as part of this fix patch, follows this so patch stays a lifetime fix.
I can submit separate patch converting the mutex_lock/mutex_unlock() to guard(mutex) after the fix patch is merged.
Let me know your opinion if you suggest to submit theguard_mutex()patch before the fix patch.Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 582
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+static int sbrmi_open (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); + +mutex_lock (&data->lock);Again, please use a guard.
same as above.
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 589
Quoted 7 lines
drivers/misc/amd-sbi/rmi-core.c +7
-0+if (data->detached) { +mutex_unlock (&data->lock); +return -ENODEV ; + } +mutex_unlock (&data->lock); + +kref_get (&data->kref);Quoted 5 lines
Are you attempting to use a kref without a lock? That way lies madness and is sure to be wrong.
Are you attempting to keep the data "alive" somehow? Are you sure this is correct?
Yes, the kref is there to keep
sbrmi_dataalive.kref_get()inopen()does not race with the final put.misc_open()calls ->open() while holdingmisc_mtx, andmisc_deregister()takes that same mutex. The probe reference is dropped by a devm action that runs only aftermisc_deregister()has returned, so the object cannot reach zero whileopen()runs.kref_get()andkref_put()are atomic. I will add a comment at thekref_get()saying this.Quoted 4 lines
Quoted 4 lines
drivers/misc/amd-sbi/rmi-core.c +4
-0+ +return 0 ; +} +Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.c static int apml_mcamsr_xfer(struct sbrmi_data *data, struct apml_mcamsr_msg __us · line 599
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.c +6
-0+static int sbrmi_release (struct inode *inode,struct file *fp) +{ +struct sbrmi_data *data; + + data = container_of(fp->private_data, struct sbrmi_data, sbrmi_misc_dev); +kref_put (&data->kref, sbrmi_data_release);If open is the only thing that can get the reference to the kref, and this is the thing that drops it, why is the kref needed at all?
Quoted 5 lines
open () is not the only holder,
kref_init()is the reference owned by the driver binding and a devm action drops it on unbind. eachopen()takes another reference and drops it inrelease().sbrmi_datais freed only when both are gone, which is what closes the window where devm frees the structure while an ioctl is still using it.That's not good, this should be attached to the file descriptor. Don't go allocating things at bind as you will mess with the lifetime rules here very quickly, and it gets very messy. Rely on the rules that the vfs layer gives us and it will be much much simpler.
Quoted 41 lines
Quoted 33 lines
Quoted 33 lines
drivers/misc/amd-sbi/rmi-core.c +5
-0+return 0 ; +} +static long sbrmi_ioctl (struct file *fp,unsigned int cmd,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -570,6 +622,8 @@ static long sbrmi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)static const struct file_operations sbrmi_fops = { .owner =THIS_MODULE , + .open = sbrmi_open, + .release = sbrmi_release, .unlocked_ioctl = sbrmi_ioctl, .compat_ioctl = compat_ptr_ioctl, };drivers/misc/amd-sbi/rmi-core.h +2
-0@@ -9,6 +9,7 @@#include <linux/miscdevice.h> #include <linux/mutex.h> #include <linux/i2c.h> +#include <linux/kref.h> #include <linux/platform_device.h> #include <linux/regmap.h> #include <uapi/misc/amd-apml.h> @@ -56,9 +57,13 @@ struct sbrmi_data {struct regmap *regmap;/* Mutex locking */ struct mutex lock; +/* Reference count; keeps @sbrmi_data alive while misc fds are open */ Quoted 8 lines
drivers/misc/amd-sbi/rmi-core.h struct sbrmi_data { · line 66
Quoted 6 lines
drivers/misc/amd-sbi/rmi-core.h +3
-0+struct kref kref;u32 pwr_limit_max;u8 dev_static_addr;u8 rev; +/* Set on driver unbind; open/ioctl return -ENODEV afterward */ +bool detached;How can open/ioctl even run if the driver is unloaded? You aren't using the cdev api properly I think...
Quoted 6 lines
This fix addresses driver unbind, including unbind through sysfs; it is not limited to module unload.
misc_deregister()removes the miscdevice from the misc-device list and destroys its device node. Although it prevents new opens, it does not retain a reference to the driver-privatesbrmi_datafor files that are already open. Such files retainsbrmi_fopsandprivate_data, so a later ioctl can still enter the driver.unbind through sysfs is for developers and if things break, you get to keep the pieces when it happens (see the discussions on lkml about the recent taint flag added when this happens.)
Quoted 6 lines
So it's rare, but still, let's make this simpler by just moving the resource management to open/release instead of probe/remove.
thanks,
greg k-h
Hi Greg,
Thanks for the review. I will drop the probe-owned kref.
v2 will move misc to cdev API so the open file holds the reference. sbrmi_data will be freed from the device release callback when the last file is released, rather than from remove.
thanks,
Akshay