Skip to contentSkip to conversation

drivers/comedi: copy userspace array safely

PATCH In review Philipp Stanner 3 participants 4 messages Nov 2, 2023 – Nov 3, 2023 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/comedi: copy userspace array safely

comedi_fops.c utilizes memdup_user() to copy a userspace array. This does not check for an overflow.

Use the new wrapper memdup_array_user() to copy the array more safely.

---
 drivers/comedi/comedi_fops.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/comedi/comedi_fops.c +2 -2
index 1548dea15df1..1b481731df96 100644
@@ -1714,8 +1714,8 @@ static int __comedi_get_user_chanlist(struct comedi_device *dev,
 
 	lockdep_assert_held(&dev->mutex);
 	cmd->chanlist = NULL;
-	chanlist = memdup_user(user_chanlist,
-			       cmd->chanlist_len * sizeof(unsigned int));
+	chanlist = memdup_array_user(user_chanlist,
+				     cmd->chanlist_len, sizeof(unsigned int));
 	if (IS_ERR(chanlist))
 		return PTR_ERR(chanlist);
 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 2 lines · Philipp Stanner

comedi_fops.c utilizes memdup_user() to copy a userspace array. This does not check for an overflow.

Is there potential for an overflow today?

Quoted 2 lines

Use the new wrapper memdup_array_user() to copy the array more safely.

How about saying something like:
"Use the new function memdup_array_user() in case things change in the future which would prevent overflows if something were to change in the size of the structures".

Or something to the affect of "all is good today, but make it easy to be correct in the future as well".

thanks,

greg k-h

#2 lore raw
Quoted 5 lines · Greg Kroah-Hartman
Quoted 2 lines · Philipp Stanner

comedi_fops.c utilizes memdup_user() to copy a userspace array. This does not check for an overflow.

Is there potential for an overflow today?

None that I'm aware of, no. This is more about establishing the new function as the standard for array-copying, thereby improving readability and maybe robustness in case of future changes.

Quoted 11 lines
Quoted 3 lines

Use the new wrapper memdup_array_user() to copy the array more safely.

How about saying something like:
        "Use the new function memdup_array_user() in case things change in the future which would prevent overflows if something were to change in the size of the structures".

Or something to the affect of "all is good today, but make it easy to be correct in the future as well".

Yes, good idea. I'll send a better wording

P.

Quoted 5 lines

thanks,

greg k-h

maintainer Reviewed-by
#3 lore raw
Quoted 10 lines · Philipp Stanner
Quoted 5 lines · Greg Kroah-Hartman
Quoted 2 lines · Philipp Stanner

comedi_fops.c utilizes memdup_user() to copy a userspace array. This does not check for an overflow.

Is there potential for an overflow today?

None that I'm aware of, no. This is more about establishing the new function as the standard for array-copying, thereby improving readability and maybe robustness in case of future changes.

I agree there is no potential for overflow. The chanlist_len in the command is bound checked against the len_chanlist in the comedi subdevice in __comedi_get_user_cmd(), and the len_chanlist value is set by driver code with no user input. So it should be fine barring some rogue comedi driver.

Quoted 11 lines
Quoted 9 lines
Quoted 1 line

Use the new wrapper memdup_array_user() to copy the array more safely.

How about saying something like:
        "Use the new function memdup_array_user() in case things change in the future which would prevent overflows if something were to change in the size of the structures".

Or something to the affect of "all is good today, but make it easy to be correct in the future as well".

Yes, good idea. I'll send a better wording

Feel free to add my reviewed by line:

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-