Skip to contentSkip to conversation

sysctl: Fix conversion of INT_MIN for LP64 systems

PATCH New Robert Xiao 1 participant 1 message Jul 11, 2015 Proc sysctl

lore
Share & tools
lore
#0 lore raw

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
index 19b62b5..464df36 100644
@@ -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