kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
Subsystems (2)
PATCH 1/1 kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
The sysctl knob allows users with SYS_ADMIN capability to taint the kernel with any arbitrary value, but this might produce an invalid flags bitset being committed to tainted_mask.
This patch introduces a simple way for proc_taint() to ignore any eventual invalid bit coming from the user input before committing those bits to the kernel tainted_mask.
--- include/linux/kernel.h | 2 ++ kernel/sysctl.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/kernel.h +2 -0
@@ -597,6 +597,8 @@ extern enum system_states {#define TAINT_RANDSTRUCT 17#define TAINT_FLAGS_COUNT 18 +#define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) +struct taint_flag {char c_true;/* character printed when tainted */ char c_false;/* character printed when not tainted */
kernel/sysctl.c +13 -1
@@ -2623,11 +2623,23 @@ static int proc_taint(struct ctl_table *table, int write,return err;if (write) { +int i; + +/* +* Ignore user input that would cause the loop below +* to commit arbitrary and out of valid range TAINT flags. +*/ +if (tmptaint >TAINT_FLAGS_MAX ) { + tmptaint &=TAINT_FLAGS_MAX ; +pr_warn_once ("%s: out-of-range taint input ignored." +" tainted_mask adjusted to 0x%lx\n" , + __func__, tmptaint); + } +/* * Poor man's atomic or. Not worth adding a primitive * to everyone's atomic.h for this */ -int i;for (i =0 ; i <BITS_PER_LONG && tmptaint >> i; i++) {if ((tmptaint >> i) &1 )add_taint (i,LOCKDEP_STILL_OK );
Signature · 1 line
2.25.4
Quoted 1 line · On Tue, 12 May 2020 13:46:53 -0400 Rafael Aquini <[email protected]> wrote:
The sysctl knob
/proc/sys/kernel/tainted, yes?
Quoted 38 lines
allows users with
SYS_ADMINcapability to
taint the kernel with any arbitrary value, but this might produce an invalid flags bitset being committed totainted_mask.This patch introduces a simple way for
proc_taint()to ignore any eventual invalid bit coming from the user input before committing those bits to the kerneltainted_mask....
include/linux/kernel.h +2
-0@@ -597,6 +597,8 @@ extern enum system_states {#define TAINT_RANDSTRUCT 17#define TAINT_FLAGS_COUNT 18 +#define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) +struct taint_flag {char c_true;/* character printed when tainted */ char c_false;/* character printed when not tainted */ kernel/sysctl.c +9
-0@@ -2623,11 +2623,23 @@ static int proc_taint(struct ctl_table *table, int write,return err;if (write) { +int i; + +/* +* Ignore user input that would cause the loop below +* to commit arbitrary and out of valid range TAINT flags. +*/ +if (tmptaint >TAINT_FLAGS_MAX ) { + tmptaint &=TAINT_FLAGS_MAX ; + pr_warn_once("%s: out-of-range taint input ignored."
kernel/sysctl.c static int proc_taint(struct ctl_table *table, int write, · line 2643
Quoted 10 lines
kernel/sysctl.c +4
-1+" tainted_mask adjusted to 0x%lx\n" , + __func__, tmptaint); + } +/* * Poor man's atomic or. Not worth adding a primitive * to everyone's atomic.h for this */ -int i;for (i =0 ; i <BITS_PER_LONG && tmptaint >> i; i++) {
Could simply replace BITS_PER_LONG with TAINT_FLAGS_COUNT here?
(That "&& tmptaint >> i" seems a rather silly optimization?)
kernel/sysctl.c static int proc_taint(struct ctl_table *table, int write, · line 2645
Quoted 2 lines
kernel/sysctl.c +0
-0if ((tmptaint >> i) &1 )add_taint (i,LOCKDEP_STILL_OK );
In fact the whole thing could be simplified down to
for (i = 1; i <= TAINT_FLAGS_COUNT; i <<= 1) if (i & tmptaint) add_taint(...)
and silently drop out-of-range bits?
Quoted 62 lines · Andrew Morton
Quoted 1 line · On Tue, 12 May 2020 13:46:53 -0400 Rafael Aquini <[email protected]> wrote:
The sysctl knob
/proc/sys/kernel/tainted, yes?Quoted 38 lines
allows users with
SYS_ADMINcapability to
taint the kernel with any arbitrary value, but this might produce an invalid flags bitset being committed totainted_mask.This patch introduces a simple way for
proc_taint()to ignore any eventual invalid bit coming from the user input before committing those bits to the kerneltainted_mask....
include/linux/kernel.h +2
-0@@ -597,6 +597,8 @@ extern enum system_states {#define TAINT_RANDSTRUCT 17#define TAINT_FLAGS_COUNT 18 +#define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) +struct taint_flag {char c_true;/* character printed when tainted */ char c_false;/* character printed when not tainted */ kernel/sysctl.c +9
-0@@ -2623,11 +2623,23 @@ static int proc_taint(struct ctl_table *table, int write,return err;if (write) { +int i; + +/* +* Ignore user input that would cause the loop below +* to commit arbitrary and out of valid range TAINT flags. +*/ +if (tmptaint >TAINT_FLAGS_MAX ) { + tmptaint &=TAINT_FLAGS_MAX ; + pr_warn_once("%s: out-of-range taint input ignored."kernel/sysctl.c static int proc_taint(struct ctl_table *table, int write, · line 2643
Quoted 10 lines
kernel/sysctl.c +4
-1+" tainted_mask adjusted to 0x%lx\n" , + __func__, tmptaint); + } +/* * Poor man's atomic or. Not worth adding a primitive * to everyone's atomic.h for this */ -int i;for (i =0 ; i <BITS_PER_LONG && tmptaint >> i; i++) {Could simply replace
BITS_PER_LONGwithTAINT_FLAGS_COUNThere?(That "&& tmptaint >> i" seems a rather silly optimization?)
kernel/sysctl.c static int proc_taint(struct ctl_table *table, int write, · line 2645
Quoted 2 lines
kernel/sysctl.c +0
-0if ((tmptaint >> i) &1 )add_taint (i,LOCKDEP_STILL_OK );
Quoted 8 lines
In fact the whole thing could be simplified down to
for (i = 1; i <= TAINT_FLAGS_COUNT; i <<= 1) if (i & tmptaint) add_taint(...)and silently drop out-of-range bits?
Sure!
-- Rafael
Quoted 9 lines · Rafael Aquini
The sysctl knob allows users with
SYS_ADMINcapability to taint the kernel with any arbitrary value, but this might produce an invalid flags bitset being committed totainted_mask.This patch introduces a simple way for
proc_taint()to ignore any eventual invalid bit coming from the user input before committing those bits to the kerneltainted_mask.Signed-off-by Rafael Aquini <[email protected]>
Luis