Commit 774636e1 authored by Alexey Dobriyan's avatar Alexey Dobriyan Committed by Linus Torvalds

proc: convert to kstrto*()/kstrto*_from_user()

Convert from manual allocation/copy_from_user/...  to kstrto*() family
which were designed for exactly that.

One case can not be converted to kstrto*_from_user() to make code even
more simpler because of whitespace stripping, oh well...
Signed-off-by: default avatarAlexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 2d2e4715
...@@ -1230,10 +1230,9 @@ static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, ...@@ -1230,10 +1230,9 @@ static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct inode * inode = file_inode(file); struct inode * inode = file_inode(file);
char *page, *tmp;
ssize_t length;
uid_t loginuid; uid_t loginuid;
kuid_t kloginuid; kuid_t kloginuid;
int rv;
rcu_read_lock(); rcu_read_lock();
if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
...@@ -1242,46 +1241,28 @@ static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, ...@@ -1242,46 +1241,28 @@ static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
} }
rcu_read_unlock(); rcu_read_unlock();
if (count >= PAGE_SIZE)
count = PAGE_SIZE - 1;
if (*ppos != 0) { if (*ppos != 0) {
/* No partial writes. */ /* No partial writes. */
return -EINVAL; return -EINVAL;
} }
page = (char*)__get_free_page(GFP_TEMPORARY);
if (!page)
return -ENOMEM;
length = -EFAULT;
if (copy_from_user(page, buf, count))
goto out_free_page;
page[count] = '\0'; rv = kstrtou32_from_user(buf, count, 10, &loginuid);
loginuid = simple_strtoul(page, &tmp, 10); if (rv < 0)
if (tmp == page) { return rv;
length = -EINVAL;
goto out_free_page;
}
/* is userspace tring to explicitly UNSET the loginuid? */ /* is userspace tring to explicitly UNSET the loginuid? */
if (loginuid == AUDIT_UID_UNSET) { if (loginuid == AUDIT_UID_UNSET) {
kloginuid = INVALID_UID; kloginuid = INVALID_UID;
} else { } else {
kloginuid = make_kuid(file->f_cred->user_ns, loginuid); kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
if (!uid_valid(kloginuid)) { if (!uid_valid(kloginuid))
length = -EINVAL; return -EINVAL;
goto out_free_page;
}
} }
length = audit_set_loginuid(kloginuid); rv = audit_set_loginuid(kloginuid);
if (likely(length == 0)) if (rv < 0)
length = count; return rv;
return count;
out_free_page:
free_page((unsigned long) page);
return length;
} }
static const struct file_operations proc_loginuid_operations = { static const struct file_operations proc_loginuid_operations = {
...@@ -1335,8 +1316,9 @@ static ssize_t proc_fault_inject_write(struct file * file, ...@@ -1335,8 +1316,9 @@ static ssize_t proc_fault_inject_write(struct file * file,
const char __user * buf, size_t count, loff_t *ppos) const char __user * buf, size_t count, loff_t *ppos)
{ {
struct task_struct *task; struct task_struct *task;
char buffer[PROC_NUMBUF], *end; char buffer[PROC_NUMBUF];
int make_it_fail; int make_it_fail;
int rv;
if (!capable(CAP_SYS_RESOURCE)) if (!capable(CAP_SYS_RESOURCE))
return -EPERM; return -EPERM;
...@@ -1345,9 +1327,9 @@ static ssize_t proc_fault_inject_write(struct file * file, ...@@ -1345,9 +1327,9 @@ static ssize_t proc_fault_inject_write(struct file * file,
count = sizeof(buffer) - 1; count = sizeof(buffer) - 1;
if (copy_from_user(buffer, buf, count)) if (copy_from_user(buffer, buf, count))
return -EFAULT; return -EFAULT;
make_it_fail = simple_strtol(strstrip(buffer), &end, 0); rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
if (*end) if (rv < 0)
return -EINVAL; return rv;
if (make_it_fail < 0 || make_it_fail > 1) if (make_it_fail < 0 || make_it_fail > 1)
return -EINVAL; return -EINVAL;
...@@ -2488,32 +2470,20 @@ static ssize_t proc_coredump_filter_write(struct file *file, ...@@ -2488,32 +2470,20 @@ static ssize_t proc_coredump_filter_write(struct file *file,
{ {
struct task_struct *task; struct task_struct *task;
struct mm_struct *mm; struct mm_struct *mm;
char buffer[PROC_NUMBUF], *end;
unsigned int val; unsigned int val;
int ret; int ret;
int i; int i;
unsigned long mask; unsigned long mask;
ret = -EFAULT; ret = kstrtouint_from_user(buf, count, 0, &val);
memset(buffer, 0, sizeof(buffer)); if (ret < 0)
if (count > sizeof(buffer) - 1) return ret;
count = sizeof(buffer) - 1;
if (copy_from_user(buffer, buf, count))
goto out_no_task;
ret = -EINVAL;
val = (unsigned int)simple_strtoul(buffer, &end, 0);
if (*end == '\n')
end++;
if (end - buffer == 0)
goto out_no_task;
ret = -ESRCH; ret = -ESRCH;
task = get_proc_task(file_inode(file)); task = get_proc_task(file_inode(file));
if (!task) if (!task)
goto out_no_task; goto out_no_task;
ret = end - buffer;
mm = get_task_mm(task); mm = get_task_mm(task);
if (!mm) if (!mm)
goto out_no_mm; goto out_no_mm;
...@@ -2529,7 +2499,9 @@ static ssize_t proc_coredump_filter_write(struct file *file, ...@@ -2529,7 +2499,9 @@ static ssize_t proc_coredump_filter_write(struct file *file,
out_no_mm: out_no_mm:
put_task_struct(task); put_task_struct(task);
out_no_task: out_no_task:
return ret; if (ret < 0)
return ret;
return count;
} }
static const struct file_operations proc_coredump_filter_operations = { static const struct file_operations proc_coredump_filter_operations = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment