sysctl: Fix conversion of INT_MIN for LP64 systems
PATCH 1/1 sysctl: Fix conversion of INT_MIN for LP64 systems
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.
--- kernel/sysctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sysctl.c +2 -2
@@ -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 ;
Signature · 1 line
2.2.2
Quoted 25 lines · Robert Xiao
On LP64 systems, reading a sysctl file containing an
INT_MIN(-2147483648) could incorrectly show -18446744071562067968 due to an incorrect conversion indo_proc_dointvec_conv. This patch fixes the edge case by converting to unsigned int first to avoid sign extendingINT_MINto unsigned long.Test:
root:/proc/sys/kernel# echo -2147483648 0 0 0 > printk root:/proc/sys/kernel# cat printkWithout patch, produces -18446744071562067968 0 0 0. With patch, should produce -2147483648 0 0 0.
Signed-off-by Robert Xiao <[email protected]>--- kernel/sysctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/sysctl.c +0
-0@@ -1995,10 +1995,10 @@ static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,int val = *valp;if (val <0 ) {
kernel/sysctl.c static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp, · line 2004
Quoted 10 lines
kernel/sysctl.c +2
-2*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)
cltqIMO 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
PATCH 1/1 sysctl: fix int -> unsigned long assignments in INT_MIN case
The following
if (val < 0)
*lvalp = (unsigned long)-val;is incorrect because the compiler is free to assume -val to be positive and use a sign-extend instruction for extending the bit pattern. This is a problem if val == INT_MIN:
# echo -2147483648 >/proc/sys/dev/scsi/logging_level
# cat /proc/sys/dev/scsi/logging_level
-18446744071562067968Cast to unsigned long before negation - that way we first sign-extend and then negate an unsigned, which is well defined. With this:
# cat /proc/sys/dev/scsi/logging_level
-2147483648--- kernel/sysctl.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/sysctl.c +5 -5
@@ -1995,7 +1995,7 @@ 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 long )val; }else { *negp = false; *lvalp = (unsigned long )val;
@@ -2201,7 +2201,7 @@ static int do_proc_dointvec_minmax_conv(bool *negp, unsigned long *lvalp,int val = *valp;if (val <0 ) { *negp = true; - *lvalp = (unsigned long )-val; + *lvalp = -(unsigned long )val; }else { *negp = false; *lvalp = (unsigned long )val;
@@ -2436,7 +2436,7 @@ static int do_proc_dointvec_jiffies_conv(bool *negp, unsigned long *lvalp,unsigned long lval;if (val <0 ) { *negp = true; - lval = (unsigned long )-val; + lval = -(unsigned long )val; }else { *negp = false; lval = (unsigned long )val;
@@ -2459,7 +2459,7 @@ static int do_proc_dointvec_userhz_jiffies_conv(bool *negp, unsigned long *lvalpunsigned long lval;if (val <0 ) { *negp = true; - lval = (unsigned long )-val; + lval = -(unsigned long )val; }else { *negp = false; lval = (unsigned long )val;
@@ -2484,7 +2484,7 @@ static int do_proc_dointvec_ms_jiffies_conv(bool *negp, unsigned long *lvalp,unsigned long lval;if (val <0 ) { *negp = true; - lval = (unsigned long )-val; + lval = -(unsigned long )val; }else { *negp = false; lval = (unsigned long )val;
Signature · 1 line
1.9.3