staging: pi433: fix race condition in pi433_ioctl
PATCH v2 1/1 staging: pi433: fix race condition in pi433_ioctl
In the PI433_IOC_WR_TX_CFG case in pi433_ioctl, instance->tx_cfg is modified via
copy_from_user(&instance->tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
without any kind of synchronization. In the case where two threads would execute this same command concurrently the tx_cfg field might enter in an inconsistent state.
Additionally: if ioctl(PI433_IOC_WR_TX_CFG) and write() execute concurrently the tx config might be modified while it is being copied to the fifo, resulting in potential data corruption.
Fix: Get instance->tx_cfg_lock before modifying tx config in the PI433_IOC_WR_TX_CFG case in pi433_ioctl.
Also, do not copy data directly from user space to instance->tx_cfg. Instead use a temporary buffer allowing future checks for correctness of copied data.
---
Changes in v2:
- Use device->tx_fifo_lock instead of introducing a new lock in instance.
- Do not copy data directly from user space to instance->tx_cfg, instead use a temporary buffer allowing future checks for correctness of copied data.
--- drivers/staging/pi433/pi433_if.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/staging/pi433/pi433_if.c +9 -2
@@ -880,6 +880,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)int retval =0 ;struct pi433_instance *instance;struct pi433_device *device; +struct pi433_tx_cfg tx_cfg_buffer;void __user *argp = (void __user *)arg;/* Check type and command number */
@@ -902,9 +903,15 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return -EFAULT ;break ;case PI433_IOC_WR_TX_CFG : -if (copy_from_user (&instance->tx_cfg, argp, -sizeof (struct pi433_tx_cfg ))) +/* do not modify tx config while it is being copied to fifo */ +mutex_lock (&device->tx_fifo_lock); +if (copy_from_user (&tx_cfg_buffer, argp, +sizeof (struct pi433_tx_cfg ))) { +mutex_unlock (&device->tx_fifo_lock);return -EFAULT ; + } +memcpy (&instance->tx_cfg, &tx_cfg_buffer,sizeof (struct pi433_tx_cfg )); +mutex_unlock (&device->tx_fifo_lock);break ;case PI433_IOC_RD_RX_CFG : if (copy_to_user(argp, &device->rx_cfg,
Signature · 1 line
2.17.1
Quoted 49 lines · Hugo Lefeuvre
In the
PI433_IOC_WR_TX_CFGcase inpi433_ioctl,instance->tx_cfgis modified via
copy_from_user(&instance->tx_cfg, argp, sizeof(struct pi433_tx_cfg)))without any kind of synchronization. In the case where two threads would execute this same command concurrently the
tx_cfgfield might enter in an inconsistent state.Additionally: if
ioctl(PI433_IOC_WR_TX_CFG)andwrite()execute concurrently the tx config might be modified while it is being copied to the fifo, resulting in potential data corruption.Fix: Get
instance->tx_cfg_lockbefore modifying tx config in thePI433_IOC_WR_TX_CFGcase inpi433_ioctl.Also, do not copy data directly from user space to
instance->tx_cfg. Instead use a temporary buffer allowing future checks for correctness of copied data.Signed-off-by Hugo Lefeuvre <[email protected]>---
Changes in v2:
- Usedevice->tx_fifo_lockinstead of introducing a new lock in instance.
- Do not copy data directly from user space toinstance->tx_cfg, instead use a temporary buffer allowing future checks for correctness of copied data.--- drivers/staging/pi433/pi433_if.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/staging/pi433/pi433_if.c +1
-1@@ -880,6 +880,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)int retval =0 ;struct pi433_instance *instance;struct pi433_device *device; +struct pi433_tx_cfg tx_cfg_buffer;void __user *argp = (void __user *)arg;/* Check type and command number */ @@ -902,9 +903,15 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return -EFAULT ;break ;case PI433_IOC_WR_TX_CFG : - if (copy_from_user(&instance->tx_cfg, argp,
drivers/staging/pi433/pi433_if.c pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 914
Quoted 10 lines
drivers/staging/pi433/pi433_if.c +8
-1- sizeof(struct pi433_tx_cfg))) +/* do not modify tx config while it is being copied to fifo */ +mutex_lock (&device->tx_fifo_lock); +if (copy_from_user (&tx_cfg_buffer, argp, +sizeof (struct pi433_tx_cfg ))) { +mutex_unlock (&device->tx_fifo_lock);return -EFAULT ; + } +memcpy (&instance->tx_cfg, &tx_cfg_buffer,sizeof (struct pi433_tx_cfg )); +mutex_unlock (&device->tx_fifo_lock);
The lock is only needed around the memcpy() and that makes the code a bit simpler as well.
regards,
dan carpenter
Quoted 16 lines · Hugo Lefeuvre
drivers/staging/pi433/pi433_if.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/staging/pi433/pi433_if.c +1
-0@@ -880,6 +880,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)int retval =0 ;struct pi433_instance *instance;struct pi433_device *device; +struct pi433_tx_cfg tx_cfg_buffer;void __user *argp = (void __user *)arg;/* Check type and command number */
drivers/staging/pi433/pi433_if.c pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 906
Quoted 6 lines
drivers/staging/pi433/pi433_if.c +1
-2@@ -902,9 +903,15 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return -EFAULT ;break ;case PI433_IOC_WR_TX_CFG : -if (copy_from_user (&instance->tx_cfg, argp, -sizeof (struct pi433_tx_cfg ))) +/* do not modify tx config while it is being copied to fifo */
There is no need for this comment, since it's obvious. Also if you use simpler names then the copy fits on one line:
if (copy_from_user(&tx_cfg, argp, sizeof(tx_cfg)) {drivers/staging/pi433/pi433_if.c pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 909
Quoted 3 lines
drivers/staging/pi433/pi433_if.c +3
-0+mutex_lock (&device->tx_fifo_lock); +if (copy_from_user (&tx_cfg_buffer, argp, +sizeof (struct pi433_tx_cfg ))) {
Sorry for the duplicate review, but it got sent to both my inboxes... :P
regards,
dan carpenter
Hi Dan,
Quoted 11 lines
There is no need for this comment, since it's obvious. Also if you use simpler names then the copy fits on one line:
if (copy_from_user(&tx_cfg, argp, sizeof(tx_cfg)) {drivers/staging/pi433/pi433_if.c pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) · line 909
Quoted 3 lines
drivers/staging/pi433/pi433_if.c +3
-0+mutex_lock (&device->tx_fifo_lock); +if (copy_from_user (&tx_cfg_buffer, argp, +sizeof (struct pi433_tx_cfg ))) {Sorry for the duplicate review, but it got sent to both my inboxes... :P
Thanks for your review ! Patch updated.
Please tell me if you don't to be CC-ed anymore. :)
regards,
Hugo
Signature · 9 lines
Hugo Lefeuvre (hle) | www.owl.eu.com 4096/ 9C4F C8BF A4B0 8FC5 48EB 56B8 1962 765B B9A8 BACA -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: not available URL: <http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20180613/c911b35c/attachment.sig>