All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] audit: fix use-after-free in audit_add_watch
@ 2018-06-18 13:27 Ronny Chevalier
  2018-06-18 18:35 ` Richard Guy Briggs
  0 siblings, 1 reply; 4+ messages in thread
From: Ronny Chevalier @ 2018-06-18 13:27 UTC (permalink / raw)
  To: linux-audit; +Cc: ronny.chevalier

audit_add_watch stores locally krule->watch without taking a reference
on watch. Then, it calls audit_add_to_parent, and uses the watch stored
locally.

Unfortunately, it is possible that audit_add_to_parent updates
krule->watch.
When it happens, it also drops a reference of watch which
could free the watch.

How to reproduce (with KASAN enabled):

    auditctl -w /etc/passwd -F success=0 -k test_passwd
    auditctl -w /etc/passwd -F success=1 -k test_passwd2

The second call to auditctl triggers the use-after-free, because
audit_to_parent updates krule->watch to use a previous existing watch
and drops the reference to the newly created watch.

To fix the issue, we grab a reference of watch and we release it at the
end of the function.

Signed-off-by: Ronny Chevalier <ronny.chevalier@hp.com>
---
 kernel/audit_watch.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/audit_watch.c b/kernel/audit_watch.c
index f1ba88994508..4bb8d7718fbc 100644
--- a/kernel/audit_watch.c
+++ b/kernel/audit_watch.c
@@ -421,6 +421,14 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
 
 	mutex_unlock(&audit_filter_mutex);
 
+	/*
+	 * When we will be calling audit_add_to_parent, krule->watch might have
+	 * been updated and watch might have been freed.
+	 * So we need to keep a reference of watch.
+	 */
+
+	audit_get_watch(watch);
+
 	/* Avoid calling path_lookup under audit_filter_mutex. */
 	ret = audit_get_nd(watch, &parent_path);
 
@@ -446,6 +454,7 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
 	*list = &audit_inode_hash[h];
 error:
 	path_put(&parent_path);
+	audit_put_watch(watch);
 	return ret;
 }
 
-- 
2.17.1

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

* Re: [PATCH] audit: fix use-after-free in audit_add_watch
  2018-06-18 13:27 [PATCH] audit: fix use-after-free in audit_add_watch Ronny Chevalier
@ 2018-06-18 18:35 ` Richard Guy Briggs
  2018-06-18 18:54   ` Ronny Chevalier
  2018-06-28 16:29   ` Paul Moore
  0 siblings, 2 replies; 4+ messages in thread
From: Richard Guy Briggs @ 2018-06-18 18:35 UTC (permalink / raw)
  To: Ronny Chevalier; +Cc: linux-audit

On 2018-06-18 15:27, Ronny Chevalier wrote:
> audit_add_watch stores locally krule->watch without taking a reference
> on watch. Then, it calls audit_add_to_parent, and uses the watch stored
> locally.
> 
> Unfortunately, it is possible that audit_add_to_parent updates
> krule->watch.
> When it happens, it also drops a reference of watch which
> could free the watch.
> 
> How to reproduce (with KASAN enabled):
> 
>     auditctl -w /etc/passwd -F success=0 -k test_passwd
>     auditctl -w /etc/passwd -F success=1 -k test_passwd2
> 
> The second call to auditctl triggers the use-after-free, because
> audit_to_parent updates krule->watch to use a previous existing watch
> and drops the reference to the newly created watch.
> 
> To fix the issue, we grab a reference of watch and we release it at the
> end of the function.

Nice catch.

This doesn't quite look right though.  What if audit_get_nd() fails?
How about we put that audit_get_watch(watch) right before
audit_find_parent()?

> Signed-off-by: Ronny Chevalier <ronny.chevalier@hp.com>
> ---
>  kernel/audit_watch.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/kernel/audit_watch.c b/kernel/audit_watch.c
> index f1ba88994508..4bb8d7718fbc 100644
> --- a/kernel/audit_watch.c
> +++ b/kernel/audit_watch.c
> @@ -421,6 +421,14 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
>  
>  	mutex_unlock(&audit_filter_mutex);
>  
> +	/*
> +	 * When we will be calling audit_add_to_parent, krule->watch might have
> +	 * been updated and watch might have been freed.
> +	 * So we need to keep a reference of watch.
> +	 */
> +
> +	audit_get_watch(watch);
> +
>  	/* Avoid calling path_lookup under audit_filter_mutex. */
>  	ret = audit_get_nd(watch, &parent_path);
>  
> @@ -446,6 +454,7 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
>  	*list = &audit_inode_hash[h];
>  error:
>  	path_put(&parent_path);
> +	audit_put_watch(watch);
>  	return ret;
>  }
>  

- RGB

--
Richard Guy Briggs <rgb@redhat.com>
Sr. S/W Engineer, Kernel Security, Base Operating Systems
Remote, Ottawa, Red Hat Canada
IRC: rgb, SunRaycer
Voice: +1.647.777.2635, Internal: (81) 32635

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

* Re: [PATCH] audit: fix use-after-free in audit_add_watch
  2018-06-18 18:35 ` Richard Guy Briggs
@ 2018-06-18 18:54   ` Ronny Chevalier
  2018-06-28 16:29   ` Paul Moore
  1 sibling, 0 replies; 4+ messages in thread
From: Ronny Chevalier @ 2018-06-18 18:54 UTC (permalink / raw)
  To: Richard Guy Briggs; +Cc: linux-audit

On 18/06/2018 20:35, Richard Guy Briggs wrote:
> 
> Nice catch.
> 
> This doesn't quite look right though.  What if audit_get_nd() fails?

Oh, yes, obviously. I don't know why I thought there was a goto instead of a return there.

> How about we put that audit_get_watch(watch) right before
> audit_find_parent()?
> 

Sure, I will send a V2.

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

* Re: [PATCH] audit: fix use-after-free in audit_add_watch
  2018-06-18 18:35 ` Richard Guy Briggs
  2018-06-18 18:54   ` Ronny Chevalier
@ 2018-06-28 16:29   ` Paul Moore
  1 sibling, 0 replies; 4+ messages in thread
From: Paul Moore @ 2018-06-28 16:29 UTC (permalink / raw)
  To: rgb, ronny.chevalier; +Cc: linux-audit

On Mon, Jun 18, 2018 at 2:37 PM Richard Guy Briggs <rgb@redhat.com> wrote:
>
> On 2018-06-18 15:27, Ronny Chevalier wrote:
> > audit_add_watch stores locally krule->watch without taking a reference
> > on watch. Then, it calls audit_add_to_parent, and uses the watch stored
> > locally.
> >
> > Unfortunately, it is possible that audit_add_to_parent updates
> > krule->watch.
> > When it happens, it also drops a reference of watch which
> > could free the watch.
> >
> > How to reproduce (with KASAN enabled):
> >
> >     auditctl -w /etc/passwd -F success=0 -k test_passwd
> >     auditctl -w /etc/passwd -F success=1 -k test_passwd2
> >
> > The second call to auditctl triggers the use-after-free, because
> > audit_to_parent updates krule->watch to use a previous existing watch
> > and drops the reference to the newly created watch.
> >
> > To fix the issue, we grab a reference of watch and we release it at the
> > end of the function.
>
> Nice catch.
>
> This doesn't quite look right though.  What if audit_get_nd() fails?
> How about we put that audit_get_watch(watch) right before
> audit_find_parent()?

Since we end up using the watch in audit_get_nd() shouldn't we bump
the refcnt in the watch before we call audit_get_nd()?  Further, I
think we need to bump the refcnt before we drop audit_filter_mutex so
we don't end up in a race with audit_find_parent(), yes?

> > Signed-off-by: Ronny Chevalier <ronny.chevalier@hp.com>
> > ---
> >  kernel/audit_watch.c | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> >
> > diff --git a/kernel/audit_watch.c b/kernel/audit_watch.c
> > index f1ba88994508..4bb8d7718fbc 100644
> > --- a/kernel/audit_watch.c
> > +++ b/kernel/audit_watch.c
> > @@ -421,6 +421,14 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
> >
> >       mutex_unlock(&audit_filter_mutex);
> >
> > +     /*
> > +      * When we will be calling audit_add_to_parent, krule->watch might have
> > +      * been updated and watch might have been freed.
> > +      * So we need to keep a reference of watch.
> > +      */
> > +
> > +     audit_get_watch(watch);
> > +
> >       /* Avoid calling path_lookup under audit_filter_mutex. */
> >       ret = audit_get_nd(watch, &parent_path);
> >
> > @@ -446,6 +454,7 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
> >       *list = &audit_inode_hash[h];
> >  error:
> >       path_put(&parent_path);
> > +     audit_put_watch(watch);
> >       return ret;
> >  }
> >
>
> - RGB
>
> --
> Richard Guy Briggs <rgb@redhat.com>
> Sr. S/W Engineer, Kernel Security, Base Operating Systems
> Remote, Ottawa, Red Hat Canada
> IRC: rgb, SunRaycer
> Voice: +1.647.777.2635, Internal: (81) 32635
>
> --
> Linux-audit mailing list
> Linux-audit@redhat.com
> https://www.redhat.com/mailman/listinfo/linux-audit



-- 
paul moore
www.paul-moore.com

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

end of thread, other threads:[~2018-06-28 16:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-18 13:27 [PATCH] audit: fix use-after-free in audit_add_watch Ronny Chevalier
2018-06-18 18:35 ` Richard Guy Briggs
2018-06-18 18:54   ` Ronny Chevalier
2018-06-28 16:29   ` Paul Moore

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.