Skip to contentSkip to conversation

sysctl: add bound to panic_timeout to prevent overflow

PATCH In review Changming Liu 3 participants 4 messages Jul 10, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysctl: add bound to panic_timeout to prevent overflow

Function panic() in kernel/panic.c will use panic_timeout multiplying 1000 as a loop boundery. So this multiplication can overflow when panic_timeout is greater than (INT_MAX/1000). And this results in a zero-delay panic, instead of a huge timeout as the user intends.

Fix this by adding bound check to make it no bigger than (INT_MAX/1000).

---
 kernel/sysctl.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/sysctl.c +5 -1
index db1ce7a..e60cf04 100644
@@ -137,6 +137,9 @@ static int minolduid;
 static int ngroups_max = NGROUPS_MAX;
 static const int cap_last_cap = CAP_LAST_CAP;
 
+/* this is needed for setting boundery for panic_timeout to prevent it from overflow*/
+static int panic_time_max = INT_MAX / 1000;
+
 /*
  * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
  * and hung_task_check_interval_secs
@@ -1857,7 +1860,8 @@ static struct ctl_table kern_table[] = {
 		.data		= &panic_timeout,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra2		= &panic_time_max,
 	},
 #ifdef CONFIG_COREDUMP
 	{
Signature · 1 line
2.7.4
#1 lore raw
Quoted 2 lines · Changming Liu

Function panic() in kernel/panic.c will use panic_timeout multiplying 1000 as a loop boundery. So this multiplication

boundary.

Quoted 17 lines

can overflow when panic_timeout is greater than (INT_MAX/1000). And this results in a zero-delay panic, instead of a huge timeout as the user intends.

Fix this by adding bound check to make it no bigger than (INT_MAX/1000).

---
 kernel/sysctl.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/sysctl.c +0 -0
index db1ce7a..e60cf04 100644

kernel/sysctl.c static int minolduid; · line 140

Quoted 4 lines
kernel/sysctl.c +1 -0
@@ -137,6 +137,9 @@ static int minolduid;
 static int ngroups_max = NGROUPS_MAX;
 static const int cap_last_cap = CAP_LAST_CAP;
 
+/* this is needed for setting boundery for panic_timeout to prevent it from overflow*/

boundary (or max value) overflow */

Quoted 7 lines
kernel/sysctl.c +2 -0
+static int panic_time_max = INT_MAX / 1000;
+
 /*
  * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
  * and hung_task_check_interval_secs

kernel/sysctl.c static struct ctl_table kern_table[] = { · line 1867

Quoted 9 lines
kernel/sysctl.c +2 -1
@@ -1857,7 +1860,8 @@ static struct ctl_table kern_table[] = {
 		.data		= &panic_timeout,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra2		= &panic_time_max,
 	},
 #ifdef CONFIG_COREDUMP
 	{

thanks.

Signature · 1 line
~Randy
#2 lore raw
Quoted 5 lines · Randy Dunlap
Quoted 1 line

+/* this is needed for setting boundery for panic_timeout to prevent it from overflow*/

boundary (or max value) overflow */

Quoted 1 line

+static int panic_time_max = INT_MAX / 1000;

Or just simplify the comment.

/* Prevent overflow in panic() */

Or perhaps better, fix panic() to not overflow.

- for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP) {
+ for (i = 0; i / 1000 < panic_timeout; i += PANIC_TIMER_STEP) {

you probably also want to change i to be a long long or the loop may never terminate.

#3 lore raw
Quoted 5 lines · Randy Dunlap
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+/* this is needed for setting boundery for panic_timeout to prevent
+it from overflow*/

boundary (or max value)

overflow */

Review

Quoted 10 lines
Quoted 2 lines
Quoted 2 lines

+static int panic_time_max = INT_MAX / 1000;

Or just simplify the comment.

/* Prevent overflow in panic() */

Or perhaps better, fix panic() to not overflow.

(quoted diff) +0 -1
-		for (i = 0; i < panic_timeout * 1000; i += PANIC_TIMER_STEP)

{

Review

Quoted 1 line
(quoted diff) +1 -0
+		for (i = 0; i / 1000 < panic_timeout; i += PANIC_TIMER_STEP)

{

Quoted 3 lines

you probably also want to change i to be a long long or the loop may never terminate.

Thanks for the feedback, I too agree this should be better than modifying the sysctl, considering how localized and neat this change is. It's also more readable. Setting a bound in sysctl.c which is dependent on the constant value in panic.c is not a very good idea.

I agree changing i from long to long long is necessary.

I'll submit a v2 patch enforcing this shortly.

Cheers,
Changming