diff options
author | Robert Richter <robert.richter@amd.com> | 2011-12-19 10:38:30 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2011-12-19 11:18:43 -0500 |
commit | 913050b91eb94f194392dd797b1ff3779f606ac0 (patch) | |
tree | bcb861e17dc40875ef0f694641392b7177f04852 /drivers/oprofile | |
parent | 497f16f21a04060098c0da6ed522fbcafb90c0db (diff) |
oprofile: Fix uninitialized memory access when writing to writing to oprofilefs
If oprofilefs_ulong_from_user() is called with count equals
zero, *val remains unchanged. Depending on the implementation it
might be uninitialized.
Change oprofilefs_ulong_from_user()'s interface to return count
on success. Thus, we are able to return early if count equals
zero which avoids using *val uninitialized. Fixing all users of
oprofilefs_ulong_ from_user().
This follows write syscall implementation when count is zero:
"If count is zero ... [and if] no errors are detected, 0 will be
returned without causing any other effect." (man 2 write)
Reported-By: Mike Waychison <mikew@google.com>
Signed-off-by: Robert Richter <robert.richter@amd.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: <stable@vger.kernel.org>
Cc: oprofile-list <oprofile-list@lists.sourceforge.net>
Link: http://lkml.kernel.org/r/20111219153830.GH16765@erda.amd.com
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'drivers/oprofile')
-rw-r--r-- | drivers/oprofile/oprofile_files.c | 7 | ||||
-rw-r--r-- | drivers/oprofile/oprofilefs.c | 11 |
2 files changed, 13 insertions, 5 deletions
diff --git a/drivers/oprofile/oprofile_files.c b/drivers/oprofile/oprofile_files.c index 89f63456646f..84a208dbed93 100644 --- a/drivers/oprofile/oprofile_files.c +++ b/drivers/oprofile/oprofile_files.c | |||
@@ -45,7 +45,7 @@ static ssize_t timeout_write(struct file *file, char const __user *buf, | |||
45 | return -EINVAL; | 45 | return -EINVAL; |
46 | 46 | ||
47 | retval = oprofilefs_ulong_from_user(&val, buf, count); | 47 | retval = oprofilefs_ulong_from_user(&val, buf, count); |
48 | if (retval) | 48 | if (retval <= 0) |
49 | return retval; | 49 | return retval; |
50 | 50 | ||
51 | retval = oprofile_set_timeout(val); | 51 | retval = oprofile_set_timeout(val); |
@@ -84,7 +84,7 @@ static ssize_t depth_write(struct file *file, char const __user *buf, size_t cou | |||
84 | return -EINVAL; | 84 | return -EINVAL; |
85 | 85 | ||
86 | retval = oprofilefs_ulong_from_user(&val, buf, count); | 86 | retval = oprofilefs_ulong_from_user(&val, buf, count); |
87 | if (retval) | 87 | if (retval <= 0) |
88 | return retval; | 88 | return retval; |
89 | 89 | ||
90 | retval = oprofile_set_ulong(&oprofile_backtrace_depth, val); | 90 | retval = oprofile_set_ulong(&oprofile_backtrace_depth, val); |
@@ -141,9 +141,10 @@ static ssize_t enable_write(struct file *file, char const __user *buf, size_t co | |||
141 | return -EINVAL; | 141 | return -EINVAL; |
142 | 142 | ||
143 | retval = oprofilefs_ulong_from_user(&val, buf, count); | 143 | retval = oprofilefs_ulong_from_user(&val, buf, count); |
144 | if (retval) | 144 | if (retval <= 0) |
145 | return retval; | 145 | return retval; |
146 | 146 | ||
147 | retval = 0; | ||
147 | if (val) | 148 | if (val) |
148 | retval = oprofile_start(); | 149 | retval = oprofile_start(); |
149 | else | 150 | else |
diff --git a/drivers/oprofile/oprofilefs.c b/drivers/oprofile/oprofilefs.c index d0de6cc2d7a5..2f0aa0f700e6 100644 --- a/drivers/oprofile/oprofilefs.c +++ b/drivers/oprofile/oprofilefs.c | |||
@@ -60,6 +60,13 @@ ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t cou | |||
60 | } | 60 | } |
61 | 61 | ||
62 | 62 | ||
63 | /* | ||
64 | * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains | ||
65 | * unchanged and might be uninitialized. This follows write syscall | ||
66 | * implementation when count is zero: "If count is zero ... [and if] | ||
67 | * no errors are detected, 0 will be returned without causing any | ||
68 | * other effect." (man 2 write) | ||
69 | */ | ||
63 | int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count) | 70 | int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count) |
64 | { | 71 | { |
65 | char tmpbuf[TMPBUFSIZE]; | 72 | char tmpbuf[TMPBUFSIZE]; |
@@ -79,7 +86,7 @@ int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_ | |||
79 | raw_spin_lock_irqsave(&oprofilefs_lock, flags); | 86 | raw_spin_lock_irqsave(&oprofilefs_lock, flags); |
80 | *val = simple_strtoul(tmpbuf, NULL, 0); | 87 | *val = simple_strtoul(tmpbuf, NULL, 0); |
81 | raw_spin_unlock_irqrestore(&oprofilefs_lock, flags); | 88 | raw_spin_unlock_irqrestore(&oprofilefs_lock, flags); |
82 | return 0; | 89 | return count; |
83 | } | 90 | } |
84 | 91 | ||
85 | 92 | ||
@@ -99,7 +106,7 @@ static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_ | |||
99 | return -EINVAL; | 106 | return -EINVAL; |
100 | 107 | ||
101 | retval = oprofilefs_ulong_from_user(&value, buf, count); | 108 | retval = oprofilefs_ulong_from_user(&value, buf, count); |
102 | if (retval) | 109 | if (retval <= 0) |
103 | return retval; | 110 | return retval; |
104 | 111 | ||
105 | retval = oprofile_set_ulong(file->private_data, value); | 112 | retval = oprofile_set_ulong(file->private_data, value); |