linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
@ 2020-05-12 17:46 Rafael Aquini
  2020-05-12 20:53 ` Andrew Morton
  2020-05-12 22:31 ` Luis Chamberlain
  0 siblings, 2 replies; 4+ messages in thread
From: Rafael Aquini @ 2020-05-12 17:46 UTC (permalink / raw)
  To: linux-kernel, linux-fsdevel, mcgrof; +Cc: keescook, akpm, yzaikin, tytso

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.

Signed-off-by: Rafael Aquini <aquini@redhat.com>
---
 include/linux/kernel.h |  2 ++
 kernel/sysctl.c        | 14 +++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 9b7a8d74a9d6..e8c22a9bbc95 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -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 */
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 8a176d8727a3..fb2d693fc08c 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -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);
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
  2020-05-12 17:46 [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted Rafael Aquini
@ 2020-05-12 20:53 ` Andrew Morton
  2020-05-12 21:13   ` Rafael Aquini
  2020-05-12 22:31 ` Luis Chamberlain
  1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2020-05-12 20:53 UTC (permalink / raw)
  To: Rafael Aquini
  Cc: linux-kernel, linux-fsdevel, mcgrof, keescook, yzaikin, tytso

On Tue, 12 May 2020 13:46:53 -0400 Rafael Aquini <aquini@redhat.com> wrote:

> The sysctl knob

/proc/sys/kernel/tainted, yes?

> 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.
> 
> ...
>
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -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 */
> diff --git a/kernel/sysctl.c b/kernel/sysctl.c
> index 8a176d8727a3..fb2d693fc08c 100644
> --- a/kernel/sysctl.c
> +++ b/kernel/sysctl.c
> @@ -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++) {

Could simply replace BITS_PER_LONG with TAINT_FLAGS_COUNT here?

(That "&& tmptaint >> i" seems a rather silly optimization?)

>  			if ((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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
  2020-05-12 20:53 ` Andrew Morton
@ 2020-05-12 21:13   ` Rafael Aquini
  0 siblings, 0 replies; 4+ messages in thread
From: Rafael Aquini @ 2020-05-12 21:13 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-kernel, linux-fsdevel, mcgrof, keescook, yzaikin, tytso

On Tue, May 12, 2020 at 01:53:26PM -0700, Andrew Morton wrote:
> On Tue, 12 May 2020 13:46:53 -0400 Rafael Aquini <aquini@redhat.com> wrote:
> 
> > The sysctl knob
> 
> /proc/sys/kernel/tainted, yes?
> 
> > 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.
> > 
> > ...
> >
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -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 */
> > diff --git a/kernel/sysctl.c b/kernel/sysctl.c
> > index 8a176d8727a3..fb2d693fc08c 100644
> > --- a/kernel/sysctl.c
> > +++ b/kernel/sysctl.c
> > @@ -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++) {
> 
> Could simply replace BITS_PER_LONG with TAINT_FLAGS_COUNT here?
> 
> (That "&& tmptaint >> i" seems a rather silly optimization?)
> 
> >  			if ((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?
>

Sure!

-- Rafael


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted
  2020-05-12 17:46 [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted Rafael Aquini
  2020-05-12 20:53 ` Andrew Morton
@ 2020-05-12 22:31 ` Luis Chamberlain
  1 sibling, 0 replies; 4+ messages in thread
From: Luis Chamberlain @ 2020-05-12 22:31 UTC (permalink / raw)
  To: Rafael Aquini; +Cc: linux-kernel, linux-fsdevel, keescook, akpm, yzaikin, tytso

On Tue, May 12, 2020 at 01:46:53PM -0400, Rafael Aquini wrote:
> 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.
> 
> Signed-off-by: Rafael Aquini <aquini@redhat.com>

Acked-by: Luis Chamberlain <mcgrof@kernel.org>

  Luis

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-12 22:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-12 17:46 [PATCH] kernel: sysctl: ignore out-of-range taint bits introduced via kernel.tainted Rafael Aquini
2020-05-12 20:53 ` Andrew Morton
2020-05-12 21:13   ` Rafael Aquini
2020-05-12 22:31 ` Luis Chamberlain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).