1 From 913050b91eb94f194392dd797b1ff3779f606ac0 Mon Sep 17 00:00:00 2001
2 From: Robert Richter <robert.richter@amd.com>
3 Date: Mon, 19 Dec 2011 16:38:30 +0100
4 Subject: oprofile: Fix uninitialized memory access when writing to writing to oprofilefs
6 From: Robert Richter <robert.richter@amd.com>
8 commit 913050b91eb94f194392dd797b1ff3779f606ac0 upstream.
10 If oprofilefs_ulong_from_user() is called with count equals
11 zero, *val remains unchanged. Depending on the implementation it
12 might be uninitialized.
14 Change oprofilefs_ulong_from_user()'s interface to return count
15 on success. Thus, we are able to return early if count equals
16 zero which avoids using *val uninitialized. Fixing all users of
17 oprofilefs_ulong_ from_user().
19 This follows write syscall implementation when count is zero:
20 "If count is zero ... [and if] no errors are detected, 0 will be
21 returned without causing any other effect." (man 2 write)
23 Reported-By: Mike Waychison <mikew@google.com>
24 Signed-off-by: Robert Richter <robert.richter@amd.com>
25 Cc: Andrew Morton <akpm@linux-foundation.org>
26 Cc: oprofile-list <oprofile-list@lists.sourceforge.net>
27 Link: http://lkml.kernel.org/r/20111219153830.GH16765@erda.amd.com
28 Signed-off-by: Ingo Molnar <mingo@elte.hu>
29 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
32 arch/s390/oprofile/init.c | 2 +-
33 drivers/oprofile/oprofile_files.c | 7 ++++---
34 drivers/oprofile/oprofilefs.c | 11 +++++++++--
35 3 files changed, 14 insertions(+), 6 deletions(-)
37 --- a/arch/s390/oprofile/init.c
38 +++ b/arch/s390/oprofile/init.c
39 @@ -90,7 +90,7 @@ static ssize_t hwsampler_write(struct fi
42 retval = oprofilefs_ulong_from_user(&val, buf, count);
48 --- a/drivers/oprofile/oprofile_files.c
49 +++ b/drivers/oprofile/oprofile_files.c
50 @@ -45,7 +45,7 @@ static ssize_t timeout_write(struct file
53 retval = oprofilefs_ulong_from_user(&val, buf, count);
58 retval = oprofile_set_timeout(val);
59 @@ -84,7 +84,7 @@ static ssize_t depth_write(struct file *
62 retval = oprofilefs_ulong_from_user(&val, buf, count);
67 retval = oprofile_set_ulong(&oprofile_backtrace_depth, val);
68 @@ -141,9 +141,10 @@ static ssize_t enable_write(struct file
71 retval = oprofilefs_ulong_from_user(&val, buf, count);
78 retval = oprofile_start();
80 --- a/drivers/oprofile/oprofilefs.c
81 +++ b/drivers/oprofile/oprofilefs.c
82 @@ -60,6 +60,13 @@ ssize_t oprofilefs_ulong_to_user(unsigne
87 + * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
88 + * unchanged and might be uninitialized. This follows write syscall
89 + * implementation when count is zero: "If count is zero ... [and if]
90 + * no errors are detected, 0 will be returned without causing any
91 + * other effect." (man 2 write)
93 int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
95 char tmpbuf[TMPBUFSIZE];
96 @@ -79,7 +86,7 @@ int oprofilefs_ulong_from_user(unsigned
97 spin_lock_irqsave(&oprofilefs_lock, flags);
98 *val = simple_strtoul(tmpbuf, NULL, 0);
99 spin_unlock_irqrestore(&oprofilefs_lock, flags);
105 @@ -99,7 +106,7 @@ static ssize_t ulong_write_file(struct f
108 retval = oprofilefs_ulong_from_user(&value, buf, count);
113 retval = oprofile_set_ulong(file->private_data, value);