[PATCH 2/2] sysctl: handle overflow for file-max

From: Christian Brauner
Date: Sun Oct 14 2018 - 09:25:53 EST


Currently, when writing

echo 18446744073709551616 > /proc/sys/fs/file-max

/proc/sys/fs/file-max will overflow and be set to 0. That quickly
crashes the system. Let's detect the overflow and set to ULONG_MAX in
this case effectively capping the value.

Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Signed-off-by: Christian Brauner <christian@xxxxxxxxxx>
---
kernel/sysctl.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index a9409375380c..a3e4321b8ffa 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -127,6 +127,7 @@ static int __maybe_unused one = 1;
static int __maybe_unused two = 2;
static int __maybe_unused four = 4;
static unsigned long one_ul = 1;
+static unsigned long ulong_max = ULONG_MAX;
static int one_hundred = 100;
static int one_thousand = 1000;
#ifdef CONFIG_PRINTK
@@ -1696,6 +1697,7 @@ static struct ctl_table fs_table[] = {
.maxlen = sizeof(files_stat.max_files),
.mode = 0644,
.proc_handler = proc_doulongvec_minmax,
+ .extra2 = &ulong_max,
},
{
.procname = "nr_open",
@@ -2789,17 +2791,20 @@ static int __do_proc_doulongvec_minmax(void *data, struct ctl_table *table, int
unsigned long val;

if (write) {
- bool neg;
+ bool neg, overflow;

left -= proc_skip_spaces(&p);

err = proc_get_long(&p, &left, &val, &neg,
proc_wspace_sep,
- sizeof(proc_wspace_sep), NULL);
+ sizeof(proc_wspace_sep), NULL,
+ &overflow);
if (err)
break;
if (neg)
continue;
+ if (overflow && max)
+ val = *max;
val = convmul * val / convdiv;
if ((min && val < *min) || (max && val > *max))
continue;
--
2.17.1