* [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems
@ 2015-07-11 6:56 Robert Xiao
0 siblings, 0 replies; 4+ messages in thread
From: Robert Xiao @ 2015-07-11 6:56 UTC (permalink / raw)
To: LKML <linux-kernel@vger.kernel.org>; Mikulas Patocka <mikulas@twibright.com>; Ilya Dryomov
Cc: Robert Xiao
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems
@ 2015-07-11 7:34 Robert Xiao
0 siblings, 0 replies; 4+ messages in thread
From: Robert Xiao @ 2015-07-11 7:34 UTC (permalink / raw)
To: LKML <linux-kernel@vger.kernel.org>; Mikulas Patocka <mikulas@twibright.com>; Ilya Dryomov <idryomov@gmail.com>; Robert Xiao <brx@cs.cmu.edu>; Robert Xiao
Cc: Robert Xiao
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems
@ 2015-07-11 7:35 Robert Xiao
2015-07-11 17:15 ` Ilya Dryomov
0 siblings, 1 reply; 4+ messages in thread
From: Robert Xiao @ 2015-07-11 7:35 UTC (permalink / raw)
To: LKML <linux-kernel@vger.kernel.org>; Mikulas Patocka <mikulas@twibright.com>; Ilya Dryomov <idryomov@gmail.com>; Robert Xiao <brx@cs.cmu.edu>; Robert Xiao
Cc: Robert Xiao
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems
2015-07-11 7:35 Robert Xiao
@ 2015-07-11 17:15 ` Ilya Dryomov
0 siblings, 0 replies; 4+ messages in thread
From: Ilya Dryomov @ 2015-07-11 17:15 UTC (permalink / raw)
To: Robert Xiao
Cc: LKML, Mikulas Patocka, Robert Xiao, Andrew Morton,
Eric W. Biederman, Kees Cook
On Sat, Jul 11, 2015 at 10:35 AM, Robert Xiao <brx@cs.cmu.edu> wrote:
> 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;
I don't know why am I CC'ed on this - CC'ing Andrew along with Eric and
Kees who seem to have worked directly on sysctl.c not too long ago.
That said, I took a look at this and I think this patch is wrong.
Casting to unsigned int instead of unsigned long *after* the negation
is bogus, because we have
if (val < 0)
...
*lvalp = (unsigned long)-val;
and the compiler is free to assume -val to be positive and use the
sign-extend instruction. On gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
that I have here the cast to unsigned int works only with -O1, with -O2
it goes to town and uses cltq which sign-extends:
neg %eax
movb $0x1,(%rdi)
cltq
IMO the right way to do this would be to first cast to unsigned long
and then negate - that way we will first sign-extend and then negate an
unsigned, which is well defined. Also, this needs to be done not just
for do_proc_dointvec_conv(), but for do_proc_dointvec_minmax_conv() and
jiffies functions as well (although it's probably virtually impossible
to set val to exactly INT_MIN through jiffies write branches).
Speaking of write branches, only do_proc_dointvec_conv() does check
it's input properly, so that's something to look at.
Thanks,
Ilya
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-07-11 17:15 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-11 6:56 [PATCH] sysctl: Fix conversion of INT_MIN for LP64 systems Robert Xiao
-- strict thread matches above, loose matches on Subject: below --
2015-07-11 7:34 Robert Xiao
2015-07-11 7:35 Robert Xiao
2015-07-11 17:15 ` Ilya Dryomov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox