The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Robert Xiao <brx@cs.cmu.edu>
To: "LKML <linux-kernel@vger.kernel.org>; Mikulas Patocka
	<mikulas@twibright.com>; Ilya Dryomov" <idryomov@gmail.com>
Cc: Robert Xiao <brx@cs.cmu.edu>
Subject: [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems
Date: Fri, 10 Jul 2015 23:56:52 -0700	[thread overview]
Message-ID: <20150711065652.18270.87751@bawang.local> (raw)

On LP64 systems, reading a sysctl file containing an INT_MIN (-2147483648)
could incorrectly show -18446744071562067968 due to an incorrect conversion
in do_proc_dointvec_conv. This patch fixes the edge case by converting to
unsigned int first to avoid sign extending INT_MIN to unsigned long.

Test:

root:/proc/sys/kernel# echo -2147483648 0 0 0 > printk
root:/proc/sys/kernel# cat printk

Without patch, produces -18446744071562067968 0 0 0.
With patch, should produce -2147483648 0 0 0.

Signed-off-by: Robert Xiao <brx@cs.cmu.edu>
---
 kernel/sysctl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 19b62b5..464df36 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -1995,10 +1995,10 @@ static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
 		int val = *valp;
 		if (val < 0) {
 			*negp = true;
-			*lvalp = (unsigned long)-val;
+			*lvalp = (unsigned int)-val;
 		} else {
 			*negp = false;
-			*lvalp = (unsigned long)val;
+			*lvalp = (unsigned int)val;
 		}
 	}
 	return 0;
-- 
2.2.2

             reply	other threads:[~2015-07-11  7:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-11  6:56 Robert Xiao [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-07-11  7:34 [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems Robert Xiao
2015-07-11  7:35 Robert Xiao
2015-07-11 17:15 ` Ilya Dryomov

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=20150711065652.18270.87751@bawang.local \
    --to=brx@cs.cmu.edu \
    --cc=idryomov@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox