pinctrl: Duplicate user memory in one go in pinmux_select()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Sun, 4 Jun 2023 13:12:14 +0000 (16:12 +0300)
committerLinus Walleij <linus.walleij@linaro.org>
Fri, 9 Jun 2023 07:20:56 +0000 (09:20 +0200)
Current code is suboptimal in three ways:
1) it explicitly terminates the string which is not needed;
2) it might provoke additional faults, because asked lenght might be
   bigger than the real one;
3) it consumes more than needed lines in the source.

Instead of using kmalloc() + strncpy_from_user() + terminating, just
utilize memdup_user_nul().

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20230604131215.78847-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/pinmux.c

index 0213826..2d2f3bd 100644 (file)
@@ -692,14 +692,9 @@ static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
        if (len > PINMUX_SELECT_MAX)
                return -ENOMEM;
 
-       buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
-       if (!buf)
-               return -ENOMEM;
-
-       ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
-       if (ret < 0)
-               goto exit_free_buf;
-       buf[len-1] = '\0';
+       buf = memdup_user_nul(user_buf, len);
+       if (IS_ERR(buf))
+               return PTR_ERR(buf);
 
        /* remove leading and trailing spaces of input buffer */
        gname = strstrip(buf);