Skip to content

Commit 16e5c1f

Browse files
author
Al Viro
committed
convert a bunch of open-coded instances of memdup_user_nul()
A _lot_ of ->write() instances were open-coding it; some are converted to memdup_user_nul(), a lot more remain... Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 7e935c7 commit 16e5c1f

File tree

12 files changed

+71
-197
lines changed

12 files changed

+71
-197
lines changed

arch/xtensa/platforms/iss/simdisk.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,19 @@ static ssize_t proc_read_simdisk(struct file *file, char __user *buf,
227227
static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
228228
size_t count, loff_t *ppos)
229229
{
230-
char *tmp = kmalloc(count + 1, GFP_KERNEL);
230+
char *tmp = memdup_user_nul(buf, count);
231231
struct simdisk *dev = PDE_DATA(file_inode(file));
232232
int err;
233233

234-
if (tmp == NULL)
235-
return -ENOMEM;
236-
if (copy_from_user(tmp, buf, count)) {
237-
err = -EFAULT;
238-
goto out_free;
239-
}
234+
if (IS_ERR(tmp))
235+
return PTR_ERR(tmp);
240236

241237
err = simdisk_detach(dev);
242238
if (err != 0)
243239
goto out_free;
244240

245241
if (count > 0 && tmp[count - 1] == '\n')
246242
tmp[count - 1] = 0;
247-
else
248-
tmp[count] = 0;
249243

250244
if (tmp[0])
251245
err = simdisk_attach(dev, tmp);

drivers/net/wireless/ath/wil6210/debugfs.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,10 @@ static ssize_t wil_write_file_rxon(struct file *file, const char __user *buf,
580580
long channel;
581581
bool on;
582582

583-
char *kbuf = kmalloc(len + 1, GFP_KERNEL);
584-
585-
if (!kbuf)
586-
return -ENOMEM;
587-
if (copy_from_user(kbuf, buf, len)) {
588-
kfree(kbuf);
589-
return -EIO;
590-
}
583+
char *kbuf = memdup_user_nul(buf, len);
591584

592-
kbuf[len] = '\0';
585+
if (IS_ERR(kbuf))
586+
return PTR_ERR(kbuf);
593587
rc = kstrtol(kbuf, 0, &channel);
594588
kfree(kbuf);
595589
if (rc)

drivers/s390/char/vmcp.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,9 @@ vmcp_write(struct file *file, const char __user *buff, size_t count,
8888

8989
if (count > 240)
9090
return -EINVAL;
91-
cmd = kmalloc(count + 1, GFP_KERNEL);
92-
if (!cmd)
93-
return -ENOMEM;
94-
if (copy_from_user(cmd, buff, count)) {
95-
kfree(cmd);
96-
return -EFAULT;
97-
}
98-
cmd[count] = '\0';
91+
cmd = memdup_user_nul(buff, count);
92+
if (IS_ERR(cmd))
93+
return PTR_ERR(cmd);
9994
session = file->private_data;
10095
if (mutex_lock_interruptible(&session->mutex)) {
10196
kfree(cmd);

drivers/sbus/char/openprom.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,9 @@ static int copyin_string(char __user *user, size_t len, char **ptr)
390390
if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
391391
return -EINVAL;
392392

393-
tmp = kmalloc(len + 1, GFP_KERNEL);
394-
if (!tmp)
395-
return -ENOMEM;
396-
397-
if (copy_from_user(tmp, user, len)) {
398-
kfree(tmp);
399-
return -EFAULT;
400-
}
401-
402-
tmp[len] = '\0';
393+
tmp = memdup_user_nul(user, len);
394+
if (IS_ERR(tmp))
395+
return PTR_ERR(tmp);
403396

404397
*ptr = tmp;
405398

fs/afs/proc.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,9 @@ static ssize_t afs_proc_cells_write(struct file *file, const char __user *buf,
230230
if (size <= 1 || size >= PAGE_SIZE)
231231
return -EINVAL;
232232

233-
kbuf = kmalloc(size + 1, GFP_KERNEL);
234-
if (!kbuf)
235-
return -ENOMEM;
236-
237-
ret = -EFAULT;
238-
if (copy_from_user(kbuf, buf, size) != 0)
239-
goto done;
240-
kbuf[size] = 0;
233+
kbuf = memdup_user_nul(buf, size);
234+
if (IS_ERR(kbuf))
235+
return PTR_ERR(kbuf);
241236

242237
/* trim to first NL */
243238
name = memchr(kbuf, '\n', size);
@@ -315,15 +310,9 @@ static ssize_t afs_proc_rootcell_write(struct file *file,
315310
if (size <= 1 || size >= PAGE_SIZE)
316311
return -EINVAL;
317312

318-
ret = -ENOMEM;
319-
kbuf = kmalloc(size + 1, GFP_KERNEL);
320-
if (!kbuf)
321-
goto nomem;
322-
323-
ret = -EFAULT;
324-
if (copy_from_user(kbuf, buf, size) != 0)
325-
goto infault;
326-
kbuf[size] = 0;
313+
kbuf = memdup_user_nul(buf, size);
314+
if (IS_ERR(kbuf))
315+
return PTR_ERR(kbuf);
327316

328317
/* trim to first NL */
329318
s = memchr(kbuf, '\n', size);
@@ -337,9 +326,7 @@ static ssize_t afs_proc_rootcell_write(struct file *file,
337326
if (ret >= 0)
338327
ret = size; /* consume everything, always */
339328

340-
infault:
341329
kfree(kbuf);
342-
nomem:
343330
_leave(" = %d", ret);
344331
return ret;
345332
}

fs/cachefiles/daemon.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,9 @@ static ssize_t cachefiles_daemon_write(struct file *file,
226226
return -EOPNOTSUPP;
227227

228228
/* drag the command string into the kernel so we can parse it */
229-
data = kmalloc(datalen + 1, GFP_KERNEL);
230-
if (!data)
231-
return -ENOMEM;
232-
233-
ret = -EFAULT;
234-
if (copy_from_user(data, _data, datalen) != 0)
235-
goto error;
236-
237-
data[datalen] = '\0';
229+
data = memdup_user_nul(_data, datalen);
230+
if (IS_ERR(data))
231+
return PTR_ERR(data);
238232

239233
ret = -EINVAL;
240234
if (memchr(data, '\0', datalen))

fs/dlm/user.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,9 @@ static ssize_t device_write(struct file *file, const char __user *buf,
515515
if (count > sizeof(struct dlm_write_request) + DLM_RESNAME_MAXLEN)
516516
return -EINVAL;
517517

518-
kbuf = kzalloc(count + 1, GFP_NOFS);
519-
if (!kbuf)
520-
return -ENOMEM;
521-
522-
if (copy_from_user(kbuf, buf, count)) {
523-
error = -EFAULT;
524-
goto out_free;
525-
}
518+
kbuf = memdup_user_nul(buf, count);
519+
if (!IS_ERR(kbuf))
520+
return PTR_ERR(kbuf);
526521

527522
if (check_version(kbuf)) {
528523
error = -EBADE;

kernel/trace/blktrace.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,10 @@ static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
349349
if (count >= BLK_TN_MAX_MSG)
350350
return -EINVAL;
351351

352-
msg = kmalloc(count + 1, GFP_KERNEL);
353-
if (msg == NULL)
354-
return -ENOMEM;
355-
356-
if (copy_from_user(msg, buffer, count)) {
357-
kfree(msg);
358-
return -EFAULT;
359-
}
352+
msg = memdup_user_nul(buffer, count);
353+
if (IS_ERR(msg))
354+
return PTR_ERR(msg);
360355

361-
msg[count] = '\0';
362356
bt = filp->private_data;
363357
__trace_note_message(bt, "%s", msg);
364358
kfree(msg);

lib/dynamic_debug.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,9 @@ static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
657657
pr_warn("expected <%d bytes into control\n", USER_BUF_PAGE);
658658
return -E2BIG;
659659
}
660-
tmpbuf = kmalloc(len + 1, GFP_KERNEL);
661-
if (!tmpbuf)
662-
return -ENOMEM;
663-
if (copy_from_user(tmpbuf, ubuf, len)) {
664-
kfree(tmpbuf);
665-
return -EFAULT;
666-
}
667-
tmpbuf[len] = '\0';
660+
tmpbuf = memdup_user_nul(ubuf, len);
661+
if (IS_ERR(tmpbuf))
662+
return PTR_ERR(tmpbuf);
668663
vpr_info("read %d bytes from userspace\n", (int)len);
669664

670665
ret = ddebug_exec_queries(tmpbuf, NULL);

net/rxrpc/ar-key.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -896,15 +896,9 @@ int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
896896
if (optlen <= 0 || optlen > PAGE_SIZE - 1)
897897
return -EINVAL;
898898

899-
description = kmalloc(optlen + 1, GFP_KERNEL);
900-
if (!description)
901-
return -ENOMEM;
902-
903-
if (copy_from_user(description, optval, optlen)) {
904-
kfree(description);
905-
return -EFAULT;
906-
}
907-
description[optlen] = 0;
899+
description = memdup_user_nul(optval, optlen);
900+
if (IS_ERR(description))
901+
return PTR_ERR(description);
908902

909903
key = request_key(&key_type_rxrpc, description, NULL);
910904
if (IS_ERR(key)) {
@@ -933,15 +927,9 @@ int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
933927
if (optlen <= 0 || optlen > PAGE_SIZE - 1)
934928
return -EINVAL;
935929

936-
description = kmalloc(optlen + 1, GFP_KERNEL);
937-
if (!description)
938-
return -ENOMEM;
939-
940-
if (copy_from_user(description, optval, optlen)) {
941-
kfree(description);
942-
return -EFAULT;
943-
}
944-
description[optlen] = 0;
930+
description = memdup_user_nul(optval, optlen);
931+
if (IS_ERR(description))
932+
return PTR_ERR(description);
945933

946934
key = request_key(&key_type_keyring, description, NULL);
947935
if (IS_ERR(key)) {

0 commit comments

Comments
 (0)