linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
@ 2025-02-19 16:14 Oleg Nesterov
  2025-02-19 19:33 ` Kees Cook
  2025-03-04 23:53 ` Kees Cook
  0 siblings, 2 replies; 6+ messages in thread
From: Oleg Nesterov @ 2025-02-19 16:14 UTC (permalink / raw)
  To: Kees Cook, Paul Moore, James Morris, Serge E. Hallyn
  Cc: linux-security-module, linux-kernel

current->group_leader is stable, no need to take rcu_read_lock() and do
get/put_task_struct().

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 security/yama/yama_lsm.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index 1971710620c1..3d064dd4e03f 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -222,7 +222,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 			   unsigned long arg4, unsigned long arg5)
 {
 	int rc = -ENOSYS;
-	struct task_struct *myself = current;
+	struct task_struct *myself;
 
 	switch (option) {
 	case PR_SET_PTRACER:
@@ -232,11 +232,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 		 * leader checking is handled later when walking the ancestry
 		 * at the time of PTRACE_ATTACH check.
 		 */
-		rcu_read_lock();
-		if (!thread_group_leader(myself))
-			myself = rcu_dereference(myself->group_leader);
-		get_task_struct(myself);
-		rcu_read_unlock();
+		myself = current->group_leader;
 
 		if (arg2 == 0) {
 			yama_ptracer_del(NULL, myself);
@@ -255,7 +251,6 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 			}
 		}
 
-		put_task_struct(myself);
 		break;
 	}
 
-- 
2.25.1.362.g51ebf55



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

* Re: [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
  2025-02-19 16:14 [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl() Oleg Nesterov
@ 2025-02-19 19:33 ` Kees Cook
  2025-02-19 20:17   ` Oleg Nesterov
  2025-03-04 23:53 ` Kees Cook
  1 sibling, 1 reply; 6+ messages in thread
From: Kees Cook @ 2025-02-19 19:33 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Paul Moore, James Morris, Serge E. Hallyn, linux-security-module,
	linux-kernel

On Wed, Feb 19, 2025 at 05:14:17PM +0100, Oleg Nesterov wrote:
> current->group_leader is stable, no need to take rcu_read_lock() and do
> get/put_task_struct().

Can you explain why this is true? In trying to figure this out again,
it seems that the only way current->group_leader can vanish is if
the entire process vanishes (fork or thread exec), in which case the
"current" in this prctl can't be happening; this appears to be locked
behind tsk->sighand->siglock ?

-Kees

> 
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---
>  security/yama/yama_lsm.c | 9 ++-------
>  1 file changed, 2 insertions(+), 7 deletions(-)
> 
> diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> index 1971710620c1..3d064dd4e03f 100644
> --- a/security/yama/yama_lsm.c
> +++ b/security/yama/yama_lsm.c
> @@ -222,7 +222,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
>  			   unsigned long arg4, unsigned long arg5)
>  {
>  	int rc = -ENOSYS;
> -	struct task_struct *myself = current;
> +	struct task_struct *myself;
>  
>  	switch (option) {
>  	case PR_SET_PTRACER:
> @@ -232,11 +232,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
>  		 * leader checking is handled later when walking the ancestry
>  		 * at the time of PTRACE_ATTACH check.
>  		 */
> -		rcu_read_lock();
> -		if (!thread_group_leader(myself))
> -			myself = rcu_dereference(myself->group_leader);
> -		get_task_struct(myself);
> -		rcu_read_unlock();
> +		myself = current->group_leader;
>  
>  		if (arg2 == 0) {
>  			yama_ptracer_del(NULL, myself);
> @@ -255,7 +251,6 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
>  			}
>  		}
>  
> -		put_task_struct(myself);
>  		break;
>  	}
>  
> -- 
> 2.25.1.362.g51ebf55
> 
> 

-- 
Kees Cook

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

* Re: [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
  2025-02-19 19:33 ` Kees Cook
@ 2025-02-19 20:17   ` Oleg Nesterov
  2025-02-19 21:00     ` Oleg Nesterov
  0 siblings, 1 reply; 6+ messages in thread
From: Oleg Nesterov @ 2025-02-19 20:17 UTC (permalink / raw)
  To: Kees Cook
  Cc: Paul Moore, James Morris, Serge E. Hallyn, linux-security-module,
	linux-kernel

On 02/19, Kees Cook wrote:
>
> On Wed, Feb 19, 2025 at 05:14:17PM +0100, Oleg Nesterov wrote:
> > current->group_leader is stable, no need to take rcu_read_lock() and do
> > get/put_task_struct().
>
> Can you explain why this is true? In trying to figure this out again,
> it seems that the only way current->group_leader can vanish is if
> the entire process vanishes (fork or thread exec), in which case the
> "current" in this prctl can't be happening; this appears to be locked
> behind tsk->sighand->siglock ?

Well, almost, but this has nothing to do with tsk->sighand->siglock...

task->group_leader can only be changed by thread exec, when a non leader
thread does exec, see de_thread(). But de_thread() can't succeed and change
->group_leader until all other threads exit, see the "Kill all other threads
in the thread group" code in de_thread(). The "current" task can't exit, so
current->group_leader is stable.

Note also that we already have a lot of current->group_leader users which
don't use rcu/get_task_struct.

That said, we have a lot of buggy users of tsk->group_leader when
same_thread_group(tsk, current) != true ;) For example, sys_prlimit64().
And note that rcu_read_lock/get_task_struct can't help in this case.
I am going to send some fixes.

Oleg.

> 
> -Kees
> 
> > 
> > Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> > ---
> >  security/yama/yama_lsm.c | 9 ++-------
> >  1 file changed, 2 insertions(+), 7 deletions(-)
> > 
> > diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> > index 1971710620c1..3d064dd4e03f 100644
> > --- a/security/yama/yama_lsm.c
> > +++ b/security/yama/yama_lsm.c
> > @@ -222,7 +222,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> >  			   unsigned long arg4, unsigned long arg5)
> >  {
> >  	int rc = -ENOSYS;
> > -	struct task_struct *myself = current;
> > +	struct task_struct *myself;
> >  
> >  	switch (option) {
> >  	case PR_SET_PTRACER:
> > @@ -232,11 +232,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> >  		 * leader checking is handled later when walking the ancestry
> >  		 * at the time of PTRACE_ATTACH check.
> >  		 */
> > -		rcu_read_lock();
> > -		if (!thread_group_leader(myself))
> > -			myself = rcu_dereference(myself->group_leader);
> > -		get_task_struct(myself);
> > -		rcu_read_unlock();
> > +		myself = current->group_leader;
> >  
> >  		if (arg2 == 0) {
> >  			yama_ptracer_del(NULL, myself);
> > @@ -255,7 +251,6 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> >  			}
> >  		}
> >  
> > -		put_task_struct(myself);
> >  		break;
> >  	}
> >  
> > -- 
> > 2.25.1.362.g51ebf55
> > 
> > 
> 
> -- 
> Kees Cook
> 


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

* Re: [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
  2025-02-19 20:17   ` Oleg Nesterov
@ 2025-02-19 21:00     ` Oleg Nesterov
  2025-02-19 21:42       ` Oleg Nesterov
  0 siblings, 1 reply; 6+ messages in thread
From: Oleg Nesterov @ 2025-02-19 21:00 UTC (permalink / raw)
  To: Kees Cook
  Cc: Paul Moore, James Morris, Serge E. Hallyn, linux-security-module,
	linux-kernel

Forgot to say...

with or without this patch the usage of ptrace_relation->tracer/tracee
doesn't look right (safe) to me... but probably I missed something and
this is another story. Currently I am trying to audit the usage of
task->group_leader.

On 02/19, Oleg Nesterov wrote:
>
> On 02/19, Kees Cook wrote:
> >
> > On Wed, Feb 19, 2025 at 05:14:17PM +0100, Oleg Nesterov wrote:
> > > current->group_leader is stable, no need to take rcu_read_lock() and do
> > > get/put_task_struct().
> >
> > Can you explain why this is true? In trying to figure this out again,
> > it seems that the only way current->group_leader can vanish is if
> > the entire process vanishes (fork or thread exec), in which case the
> > "current" in this prctl can't be happening; this appears to be locked
> > behind tsk->sighand->siglock ?
> 
> Well, almost, but this has nothing to do with tsk->sighand->siglock...
> 
> task->group_leader can only be changed by thread exec, when a non leader
> thread does exec, see de_thread(). But de_thread() can't succeed and change
> ->group_leader until all other threads exit, see the "Kill all other threads
> in the thread group" code in de_thread(). The "current" task can't exit, so
> current->group_leader is stable.
> 
> Note also that we already have a lot of current->group_leader users which
> don't use rcu/get_task_struct.
> 
> That said, we have a lot of buggy users of tsk->group_leader when
> same_thread_group(tsk, current) != true ;) For example, sys_prlimit64().
> And note that rcu_read_lock/get_task_struct can't help in this case.
> I am going to send some fixes.
> 
> Oleg.
> 
> > 
> > -Kees
> > 
> > > 
> > > Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> > > ---
> > >  security/yama/yama_lsm.c | 9 ++-------
> > >  1 file changed, 2 insertions(+), 7 deletions(-)
> > > 
> > > diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> > > index 1971710620c1..3d064dd4e03f 100644
> > > --- a/security/yama/yama_lsm.c
> > > +++ b/security/yama/yama_lsm.c
> > > @@ -222,7 +222,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> > >  			   unsigned long arg4, unsigned long arg5)
> > >  {
> > >  	int rc = -ENOSYS;
> > > -	struct task_struct *myself = current;
> > > +	struct task_struct *myself;
> > >  
> > >  	switch (option) {
> > >  	case PR_SET_PTRACER:
> > > @@ -232,11 +232,7 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> > >  		 * leader checking is handled later when walking the ancestry
> > >  		 * at the time of PTRACE_ATTACH check.
> > >  		 */
> > > -		rcu_read_lock();
> > > -		if (!thread_group_leader(myself))
> > > -			myself = rcu_dereference(myself->group_leader);
> > > -		get_task_struct(myself);
> > > -		rcu_read_unlock();
> > > +		myself = current->group_leader;
> > >  
> > >  		if (arg2 == 0) {
> > >  			yama_ptracer_del(NULL, myself);
> > > @@ -255,7 +251,6 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> > >  			}
> > >  		}
> > >  
> > > -		put_task_struct(myself);
> > >  		break;
> > >  	}
> > >  
> > > -- 
> > > 2.25.1.362.g51ebf55
> > > 
> > > 
> > 
> > -- 
> > Kees Cook
> > 


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

* Re: [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
  2025-02-19 21:00     ` Oleg Nesterov
@ 2025-02-19 21:42       ` Oleg Nesterov
  0 siblings, 0 replies; 6+ messages in thread
From: Oleg Nesterov @ 2025-02-19 21:42 UTC (permalink / raw)
  To: Kees Cook
  Cc: Paul Moore, James Morris, Serge E. Hallyn, linux-security-module,
	linux-kernel

Damn, sorry for the spam ;)

On 02/19, Oleg Nesterov wrote:
>
> Forgot to say...
>
> with or without this patch the usage of ptrace_relation->tracer/tracee
> doesn't look right (safe) to me... but probably I missed something
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Yes I did. I didn't realize that put_task_struct(tracer/tracee) calls
security_task_free() -> yama_task_free().

Sorry fo the noise.

Oleg.


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

* Re: [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
  2025-02-19 16:14 [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl() Oleg Nesterov
  2025-02-19 19:33 ` Kees Cook
@ 2025-03-04 23:53 ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2025-03-04 23:53 UTC (permalink / raw)
  To: Paul Moore, James Morris, Serge E. Hallyn, Oleg Nesterov
  Cc: Kees Cook, linux-security-module, linux-kernel

On Wed, 19 Feb 2025 17:14:17 +0100, Oleg Nesterov wrote:
> current->group_leader is stable, no need to take rcu_read_lock() and do
> get/put_task_struct().
> 
> 

Applied to for-next/hardening, thanks!

[1/1] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl()
      https://git.kernel.org/kees/c/c6822ed5d037

Take care,

-- 
Kees Cook


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

end of thread, other threads:[~2025-03-04 23:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-19 16:14 [PATCH] yama: don't abuse rcu_read_lock/get_task_struct in yama_task_prctl() Oleg Nesterov
2025-02-19 19:33 ` Kees Cook
2025-02-19 20:17   ` Oleg Nesterov
2025-02-19 21:00     ` Oleg Nesterov
2025-02-19 21:42       ` Oleg Nesterov
2025-03-04 23:53 ` Kees Cook

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).