Skip to content

Commit f188ac1

Browse files
warthog618Linus Walleij
authored andcommitted
gpiolib: cdev: switch from kstrdup() to kstrndup()
Use kstrndup() to copy line labels from the userspace provided char array, rather than ensuring the char array contains a null terminator and using kstrdup(). Note that the length provided to kstrndup() still assumes that the char array does contain a null terminator, so the maximum string length is one less than the array. This is consistent with the previous behaviour. Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Kent Gibson <warthog618@gmail.com> Link: https://lore.kernel.org/r/20201005070246.20927-1-warthog618@gmail.com Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 8c270fb commit f188ac1

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

drivers/gpio/gpiolib-cdev.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
307307
lh->gdev = gdev;
308308
get_device(&gdev->dev);
309309

310-
/* Make sure this is terminated */
311-
handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
312-
if (strlen(handlereq.consumer_label)) {
313-
lh->label = kstrdup(handlereq.consumer_label,
314-
GFP_KERNEL);
310+
if (handlereq.consumer_label[0] != '\0') {
311+
/* label is only initialized if consumer_label is set */
312+
lh->label = kstrndup(handlereq.consumer_label,
313+
sizeof(handlereq.consumer_label) - 1,
314+
GFP_KERNEL);
315315
if (!lh->label) {
316316
ret = -ENOMEM;
317317
goto out_free_lh;
@@ -1322,11 +1322,10 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
13221322
INIT_DELAYED_WORK(&lr->lines[i].work, debounce_work_func);
13231323
}
13241324

1325-
/* Make sure this is terminated */
1326-
ulr.consumer[sizeof(ulr.consumer)-1] = '\0';
1327-
if (strlen(ulr.consumer)) {
1325+
if (ulr.consumer[0] != '\0') {
13281326
/* label is only initialized if consumer is set */
1329-
lr->label = kstrdup(ulr.consumer, GFP_KERNEL);
1327+
lr->label = kstrndup(ulr.consumer, sizeof(ulr.consumer) - 1,
1328+
GFP_KERNEL);
13301329
if (!lr->label) {
13311330
ret = -ENOMEM;
13321331
goto out_free_linereq;
@@ -1711,11 +1710,11 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
17111710
le->gdev = gdev;
17121711
get_device(&gdev->dev);
17131712

1714-
/* Make sure this is terminated */
1715-
eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1716-
if (strlen(eventreq.consumer_label)) {
1717-
le->label = kstrdup(eventreq.consumer_label,
1718-
GFP_KERNEL);
1713+
if (eventreq.consumer_label[0] != '\0') {
1714+
/* label is only initialized if consumer_label is set */
1715+
le->label = kstrndup(eventreq.consumer_label,
1716+
sizeof(eventreq.consumer_label) - 1,
1717+
GFP_KERNEL);
17191718
if (!le->label) {
17201719
ret = -ENOMEM;
17211720
goto out_free_le;

0 commit comments

Comments
 (0)