All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Richter <robert.richter@amd.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: LKML <linux-kernel@vger.kernel.org>,
	oprofile-list <oprofile-list@lists.sourceforge.net>
Subject: [PATCH v2] oprofile: Fix uninitialized memory access when writing to
Date: Mon, 19 Dec 2011 16:38:30 +0100	[thread overview]
Message-ID: <20111219153830.GH16765@erda.amd.com> (raw)
In-Reply-To: <20111219141703.GG16765@erda.amd.com>

On 19.12.11 15:17:03, Robert Richter wrote:
> On 19.12.11 15:00:49, Ingo Molnar wrote:
> 
> > +	if (!count)
> > +		return 0;
> > +
> >  	retval = oprofilefs_ulong_from_user(&value, buf, count);
> >  	if (retval)
> >  		return retval;
> > 
> > See the ugly and fragile pattern?
> > 
> > This should *really* be solved via the 
> > oprofilefs_ulong_from_user() helper function, not by sprinkling 
> > the !count checks in half a dozen places ...
> 
> Hmm, I thought there was no way to leave the code path with count == 0
> and retval. But thinking about it it would be possible with returning
> count or errors < 0. Will improve the patch.

Ingo, see my updated version below. I am fine with appling it directly
on tip/perf/urgent.

Thanks,

-Robert

--


>From f07214bc05560f63da8404d31aa83034a16c6229 Mon Sep 17 00:00:00 2001
From: Robert Richter <robert.richter@amd.com>
Date: Fri, 16 Dec 2011 15:45:31 +0100
Subject: [PATCH] oprofile: Fix uninitialized memory access when 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>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: stable@vger.kernel.org
Signed-off-by: Robert Richter <robert.richter@amd.com>
---
 arch/s390/oprofile/init.c         |    2 +-
 drivers/oprofile/oprofile_files.c |    7 ++++---
 drivers/oprofile/oprofilefs.c     |   11 +++++++++--
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/s390/oprofile/init.c b/arch/s390/oprofile/init.c
index 6efc18b..bd58b72 100644
--- a/arch/s390/oprofile/init.c
+++ b/arch/s390/oprofile/init.c
@@ -88,7 +88,7 @@ static ssize_t hwsampler_write(struct file *file, char const __user *buf,
 		return -EINVAL;
 
 	retval = oprofilefs_ulong_from_user(&val, buf, count);
-	if (retval)
+	if (retval <= 0)
 		return retval;
 
 	if (oprofile_started)
diff --git a/drivers/oprofile/oprofile_files.c b/drivers/oprofile/oprofile_files.c
index 89f6345..84a208d 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,
 		return -EINVAL;
 
 	retval = oprofilefs_ulong_from_user(&val, buf, count);
-	if (retval)
+	if (retval <= 0)
 		return retval;
 
 	retval = oprofile_set_timeout(val);
@@ -84,7 +84,7 @@ static ssize_t depth_write(struct file *file, char const __user *buf, size_t cou
 		return -EINVAL;
 
 	retval = oprofilefs_ulong_from_user(&val, buf, count);
-	if (retval)
+	if (retval <= 0)
 		return retval;
 
 	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
 		return -EINVAL;
 
 	retval = oprofilefs_ulong_from_user(&val, buf, count);
-	if (retval)
+	if (retval <= 0)
 		return retval;
 
+	retval = 0;
 	if (val)
 		retval = oprofile_start();
 	else
diff --git a/drivers/oprofile/oprofilefs.c b/drivers/oprofile/oprofilefs.c
index d0de6cc..2f0aa0f 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
 }
 
 
+/*
+ * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
+ * unchanged and might be uninitialized. 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)
+ */
 int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
 {
 	char tmpbuf[TMPBUFSIZE];
@@ -79,7 +86,7 @@ int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_
 	raw_spin_lock_irqsave(&oprofilefs_lock, flags);
 	*val = simple_strtoul(tmpbuf, NULL, 0);
 	raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
-	return 0;
+	return count;
 }
 
 
@@ -99,7 +106,7 @@ static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_
 		return -EINVAL;
 
 	retval = oprofilefs_ulong_from_user(&value, buf, count);
-	if (retval)
+	if (retval <= 0)
 		return retval;
 
 	retval = oprofile_set_ulong(file->private_data, value);
-- 
1.7.7




-- 
Advanced Micro Devices, Inc.
Operating System Research Center


  reply	other threads:[~2011-12-19 15:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-19 13:46 [GIT PULL] oprofile fixes for 3.2 Robert Richter
2011-12-19 13:46 ` [PATCH] oprofile: Fix uninitialized memory access when writing to oprofilefs Robert Richter
2011-12-19 14:00 ` [GIT PULL] oprofile fixes for 3.2 Ingo Molnar
2011-12-19 14:17   ` Robert Richter
2011-12-19 15:38     ` Robert Richter [this message]
2011-12-19 16:19       ` [PATCH v2] oprofile: Fix uninitialized memory access when writing to Ingo Molnar
2011-12-20  8:57       ` [tip:perf/urgent] oprofile: Fix uninitialized memory access when writing to writing to oprofilefs tip-bot for Robert Richter
2011-12-22 15:15 ` [PATCH] oprofile, arm/sh: Fix oprofile_arch_exit() linkage issue Robert Richter
2011-12-23 13:19   ` [tip:perf/urgent] " tip-bot for Vladimir Zapolskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111219153830.GH16765@erda.amd.com \
    --to=robert.richter@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oprofile-list@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.