Skip to contentSkip to conversation

fs/proc/task_mmu.c: fix clear_refs_write() input sanity check

PATCH In review Vincent Li 2 participants 3 messages Aug 12, 2009

Subsystems (2)
lore
Share & tools
lore

Vincent Li

author
#0 lore raw

PATCH 1/1 fs/proc/task_mmu.c: fix clear_refs_write() input sanity check

Andrew Morton pointed out similar string hacking and obfuscated check for zero-length input at the end of the function, David Rientjes suggested to use strict_strtol to replace simple_strtol, this patch cover above suggestions, add removing of leading and trailing whitespace from user input. It does not change function behavious.

This patch is rebased on mmotm-2009-08-04-14-22.

---
 fs/proc/task_mmu.c |   18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)
fs/proc/task_mmu.c +8 -10
index 2079969..026b532 100644
@@ -494,18 +494,17 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 	char buffer[PROC_NUMBUF], *end;
 	struct mm_struct *mm;
 	struct vm_area_struct *vma;
-	int type;
+	long res;
 
 	memset(buffer, 0, sizeof(buffer));
 	if (count > sizeof(buffer) - 1)
 		count = sizeof(buffer) - 1;
 	if (copy_from_user(buffer, buf, count))
 		return -EFAULT;
-	type = simple_strtol(buffer, &end, 0);
-	if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
+	if (strict_strtol(strstrip(buffer), 10, &res)
+		return -EINVAL;
+	if (res < CLEAR_REFS_ALL || res > CLEAR_REFS_MAPPED)
 		return -EINVAL;
-	if (*end == '\n')
-		end++;
 	task = get_proc_task(file->f_path.dentry->d_inode);
 	if (!task)
 		return -ESRCH;
@@ -529,9 +528,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 			 * Writing 3 to /proc/pid/clear_refs only affects file
 			 * mapped pages.
 			 */
-			if (type == CLEAR_REFS_ANON && vma->vm_file)
+			if (res == CLEAR_REFS_ANON && vma->vm_file)
 				continue;
-			if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
+			if (res == CLEAR_REFS_MAPPED && !vma->vm_file)
 				continue;
 			walk_page_range(vma->vm_start, vma->vm_end,
 					&clear_refs_walk);
@@ -541,9 +540,8 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 		mmput(mm);
 	}
 	put_task_struct(task);
-	if (end - buffer == 0)
-		return -EIO;
-	return end - buffer;
+
+	return count;
 }
 
 const struct file_operations proc_clear_refs_operations = {
Signature · 1 line
1.6.0.4
#1 lore raw

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 498

Quoted 11 lines · Vincent Li
fs/proc/task_mmu.c +1 -1
index 2079969..026b532 100644
@@ -494,18 +494,17 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 	char buffer[PROC_NUMBUF], *end;
 	struct mm_struct *mm;
 	struct vm_area_struct *vma;
-	int type;
+	long res;
 

What does res' mean? Why can't it just be left as type'?

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 505

Quoted 9 lines
fs/proc/task_mmu.c +2 -2
 	memset(buffer, 0, sizeof(buffer));
 	if (count > sizeof(buffer) - 1)
 		count = sizeof(buffer) - 1;
 	if (copy_from_user(buffer, buf, count))
 		return -EFAULT;
-	type = simple_strtol(buffer, &end, 0);
-	if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
+	if (strict_strtol(strstrip(buffer), 10, &res)
+		return -EINVAL;

Please test your patch submissions (which would require that the above actually compiles).

#2 lore raw
Quoted 15 lines · David Rientjes

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 498

Quoted 11 lines · Vincent Li
fs/proc/task_mmu.c +1 -1
index 2079969..026b532 100644
@@ -494,18 +494,17 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 	char buffer[PROC_NUMBUF], *end;
 	struct mm_struct *mm;
 	struct vm_area_struct *vma;
-	int type;
+	long res;
 

What does res' mean? Why can't it just be left as type'?

res mean the result parsed from userspace input, I can keep it as name 'type'.

Quoted 14 lines

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 505

Quoted 10 lines
fs/proc/task_mmu.c +2 -2
 	memset(buffer, 0, sizeof(buffer));
 	if (count > sizeof(buffer) - 1)
 		count = sizeof(buffer) - 1;
 	if (copy_from_user(buffer, buf, count))
 		return -EFAULT;
-	type = simple_strtol(buffer, &end, 0);
-	if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
+	if (strict_strtol(strstrip(buffer), 10, &res)
+		return -EINVAL;

Please test your patch submissions (which would require that the above actually compiles).

I did get compiling error and warnings, I will resend the patch with varible name 'type' left and no compiling errors.

Vincent Li
Biomedical Research Center
University of British Columbia