mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] apparmor: don't try to replace stale label in ptrace access check
@ 2018-09-13 16:12 Jann Horn
  2018-09-13 16:23 ` John Johansen
  0 siblings, 1 reply; 2+ messages in thread
From: Jann Horn @ 2018-09-13 16:12 UTC (permalink / raw)
  To: John Johansen, apparmor, jannh; +Cc: linux-kernel, Kees Cook

As a comment above begin_current_label_crit_section() explains,
begin_current_label_crit_section() must run in sleepable context because
when label_is_stale() is true, aa_replace_current_label() runs, which uses
prepare_creds(), which can sleep.
Until now, the ptrace access check (which runs with a task lock held)
violated this rule.

Also add a might_sleep() assertion to begin_current_label_crit_section(),
because asserts are less likely to be ignored than comments.

Signed-off-by: Jann Horn <jannh@google.com>
---
I have tested that just the might_sleep() yells a lot, and adding the
rest of the patch stops the yelling.

 security/apparmor/include/cred.h | 2 ++
 security/apparmor/lsm.c          | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/security/apparmor/include/cred.h b/security/apparmor/include/cred.h
index e287b7d0d4be..265ae6641a06 100644
--- a/security/apparmor/include/cred.h
+++ b/security/apparmor/include/cred.h
@@ -151,6 +151,8 @@ static inline struct aa_label *begin_current_label_crit_section(void)
 {
 	struct aa_label *label = aa_current_raw_label();
 
+	might_sleep();
+
 	if (label_is_stale(label)) {
 		label = aa_get_newest_label(label);
 		if (aa_replace_current_label(label) == 0)
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index 8b8b70620bbe..e3f40c20b9b4 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -114,13 +114,13 @@ static int apparmor_ptrace_access_check(struct task_struct *child,
 	struct aa_label *tracer, *tracee;
 	int error;
 
-	tracer = begin_current_label_crit_section();
+	tracer = __begin_current_label_crit_section();
 	tracee = aa_get_task_label(child);
 	error = aa_may_ptrace(tracer, tracee,
 			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
 						  : AA_PTRACE_TRACE);
 	aa_put_label(tracee);
-	end_current_label_crit_section(tracer);
+	__end_current_label_crit_section(tracer);
 
 	return error;
 }
-- 
2.19.0.rc2.392.g5ba43deb5a-goog


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

* Re: [PATCH] apparmor: don't try to replace stale label in ptrace access check
  2018-09-13 16:12 [PATCH] apparmor: don't try to replace stale label in ptrace access check Jann Horn
@ 2018-09-13 16:23 ` John Johansen
  0 siblings, 0 replies; 2+ messages in thread
From: John Johansen @ 2018-09-13 16:23 UTC (permalink / raw)
  To: Jann Horn, apparmor; +Cc: linux-kernel, Kees Cook

On 09/13/2018 09:12 AM, Jann Horn wrote:
> As a comment above begin_current_label_crit_section() explains,
> begin_current_label_crit_section() must run in sleepable context because
> when label_is_stale() is true, aa_replace_current_label() runs, which uses
> prepare_creds(), which can sleep.
> Until now, the ptrace access check (which runs with a task lock held)
> violated this rule.
> 
> Also add a might_sleep() assertion to begin_current_label_crit_section(),
> because asserts are less likely to be ignored than comments.
> 
> Signed-off-by: Jann Horn <jannh@google.com>

oh ouch, thanks Jann nice catch. I've pulled it into apparmor-next

Acked-by: John Johansen <john.johansen@canonical.com>


> ---
> I have tested that just the might_sleep() yells a lot, and adding the
> rest of the patch stops the yelling.
> 
>  security/apparmor/include/cred.h | 2 ++
>  security/apparmor/lsm.c          | 4 ++--
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/security/apparmor/include/cred.h b/security/apparmor/include/cred.h
> index e287b7d0d4be..265ae6641a06 100644
> --- a/security/apparmor/include/cred.h
> +++ b/security/apparmor/include/cred.h
> @@ -151,6 +151,8 @@ static inline struct aa_label *begin_current_label_crit_section(void)
>  {
>  	struct aa_label *label = aa_current_raw_label();
>  
> +	might_sleep();
> +
>  	if (label_is_stale(label)) {
>  		label = aa_get_newest_label(label);
>  		if (aa_replace_current_label(label) == 0)
> diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
> index 8b8b70620bbe..e3f40c20b9b4 100644
> --- a/security/apparmor/lsm.c
> +++ b/security/apparmor/lsm.c
> @@ -114,13 +114,13 @@ static int apparmor_ptrace_access_check(struct task_struct *child,
>  	struct aa_label *tracer, *tracee;
>  	int error;
>  
> -	tracer = begin_current_label_crit_section();
> +	tracer = __begin_current_label_crit_section();
>  	tracee = aa_get_task_label(child);
>  	error = aa_may_ptrace(tracer, tracee,
>  			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
>  						  : AA_PTRACE_TRACE);
>  	aa_put_label(tracee);
> -	end_current_label_crit_section(tracer);
> +	__end_current_label_crit_section(tracer);
>  
>  	return error;
>  }
> 


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

end of thread, other threads:[~2018-09-13 17:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-13 16:12 [PATCH] apparmor: don't try to replace stale label in ptrace access check Jann Horn
2018-09-13 16:23 ` John Johansen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®