Skip to contentSkip to conversation

audit: Fix two bugs with deleting audit watches

PATCH In review Jan Kara 4 participants 8 messages Aug 15, 2017 – Aug 18, 2017

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover audit: Fix two bugs with deleting audit watches

Hello,

Tony pointed me to a long-standing use after free issue in audit watch subsystem. The first patch should fix this. When analyzing and fixing that issue, I've noticed that audit watch does not received FS_UNMOUNT event although from the code it seems it expects to receive it - patch two fixes that. The patches survive some basic testing but especially for the second patch I'd like someone with more experience with audit subsystem to have a look whether the patch makes sense.

Honza

author
#1 lore raw

PATCH 1/2 audit: Fix use after free in audit_remove_watch_rule()

audit_remove_watch_rule() drops watch's reference to parent but then continues to work with it. That is not safe as parent can get freed once we drop our reference. The following is a trivial reproducer:

mount -o loop image /mnt
touch /mnt/file
auditctl -w /mnt/file -p wax
umount /mnt
auditctl -D
<crash in fsnotify_destroy_mark()>

Grab our own reference in audit_remove_watch_rule() earlier to make sure mark does not get freed under us.

---
 kernel/audit_watch.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/audit_watch.c +7 -5
index 62d686d96581..ed748ee40029 100644
@@ -457,13 +457,15 @@ void audit_remove_watch_rule(struct audit_krule *krule)
 	list_del(&krule->rlist);
 
 	if (list_empty(&watch->rules)) {
+		/*
+		 * audit_remove_watch() drops our reference to 'parent' which
+		 * can get freed. Grab our own reference to be safe.
+		 */
+		audit_get_parent(parent);
 		audit_remove_watch(watch);
-
-		if (list_empty(&parent->watches)) {
-			audit_get_parent(parent);
+		if (list_empty(&parent->watches))
 			fsnotify_destroy_mark(&parent->mark, audit_watch_group);
-			audit_put_parent(parent);
-		}
+		audit_put_parent(parent);
 	}
 }
 
Signature · 1 line
2.12.3
Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] audit: Fix use after free in audit_remove_watch_rule()

Quoted 11 lines · Jan Kara

audit_remove_watch_rule() drops watch's reference to parent but then continues to work with it. That is not safe as parent can get freed once we drop our reference. The following is a trivial reproducer:

mount -o loop image /mnt
touch /mnt/file
auditctl -w /mnt/file -p wax
umount /mnt
auditctl -D
<crash in fsnotify_destroy_mark()>

Quoted 7 lines

Grab our own reference in audit_remove_watch_rule() earlier to make sure mark does not get freed under us.

---

Fix tested and verified against v3.0 and mainline

maintainer
#3 lore raw

Re: [PATCH 1/2] audit: Fix use after free in audit_remove_watch_rule()

In reply to Jan Kara
Quoted 14 lines · Jan Kara

audit_remove_watch_rule() drops watch's reference to parent but then continues to work with it. That is not safe as parent can get freed once we drop our reference. The following is a trivial reproducer:

mount -o loop image /mnt
touch /mnt/file
auditctl -w /mnt/file -p wax
umount /mnt
auditctl -D
<crash in fsnotify_destroy_mark()>

Grab our own reference in audit_remove_watch_rule() earlier to make sure mark does not get freed under us.

Quoted 6 lines
---
 kernel/audit_watch.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

This looks good to me, thanks for the fix. I'm going to merge this into the audit/stable-4.13 branch and assuming it passes the audit tests I'll send it up to Linus later this week.

Quoted 27 lines
kernel/audit_watch.c +7 -6
index 62d686d96581..ed748ee40029 100644
@@ -457,13 +457,15 @@ void audit_remove_watch_rule(struct audit_krule *krule)
        list_del(&krule->rlist);
 
        if (list_empty(&watch->rules)) {
+               /*
+                * audit_remove_watch() drops our reference to 'parent' which
+                * can get freed. Grab our own reference to be safe.
+                */
+               audit_get_parent(parent);
                audit_remove_watch(watch);
-
-               if (list_empty(&parent->watches)) {
-                       audit_get_parent(parent);
+               if (list_empty(&parent->watches))
                        fsnotify_destroy_mark(&parent->mark, audit_watch_group);
-                       audit_put_parent(parent);
-               }
+               audit_put_parent(parent);
        }
 }
 
--

2.12.3

Signature · 2 lines
paul moore
www.paul-moore.com
Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] audit: Fix use after free in audit_remove_watch_rule()

In reply to Jan Kara
Quoted 11 lines · Jan Kara

audit_remove_watch_rule() drops watch's reference to parent but then continues to work with it. That is not safe as parent can get freed once we drop our reference. The following is a trivial reproducer:

mount -o loop image /mnt
touch /mnt/file
auditctl -w /mnt/file -p wax
umount /mnt
auditctl -D
<crash in fsnotify_destroy_mark()>

Quoted 6 lines

Grab our own reference in audit_remove_watch_rule() earlier to make sure mark does not get freed under us.

Quoted 36 lines
---
 kernel/audit_watch.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/audit_watch.c +7 -5
index 62d686d96581..ed748ee40029 100644
@@ -457,13 +457,15 @@ void audit_remove_watch_rule(struct audit_krule *krule)
 	list_del(&krule->rlist);
 
 	if (list_empty(&watch->rules)) {
+		/*
+		 * audit_remove_watch() drops our reference to 'parent' which
+		 * can get freed. Grab our own reference to be safe.
+		 */
+		audit_get_parent(parent);
 		audit_remove_watch(watch);
-
-		if (list_empty(&parent->watches)) {
-			audit_get_parent(parent);
+		if (list_empty(&parent->watches))
 			fsnotify_destroy_mark(&parent->mark, audit_watch_group);
-			audit_put_parent(parent);
-		}
+		audit_put_parent(parent);
 	}
 }
 
Signature · 6 lines
2.12.3

--
Linux-audit mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/linux-audit

- RGB

author
#5 lore raw

PATCH 2/2 audit: Receive unmount event

In reply to Jan Kara

Although audit_watch_handle_event() can handle FS_UNMOUNT event, it is not part of AUDIT_FS_WATCH mask and thus such event never gets to audit_watch_handle_event(). Thus fsnotify marks are deleted by fsnotify subsystem on unmount without audit being notified about that which leads to a strange state of existing audit rules with dead fsnotify marks.

Add FS_UNMOUNT to the mask of events to be received so that audit can clean up its state accordingly.

---
 kernel/audit_watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/audit_watch.c +1 -1
index ed748ee40029..9eb8b3511636 100644
@@ -66,7 +66,7 @@ static struct fsnotify_group *audit_watch_group;
 
 /* fsnotify events we care about. */
 #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
-			FS_MOVE_SELF | FS_EVENT_ON_CHILD)
+			FS_MOVE_SELF | FS_EVENT_ON_CHILD | FS_UNMOUNT)
 
 static void audit_free_parent(struct audit_parent *parent)
 {
Signature · 1 line
2.12.3
maintainer
#6 lore raw

Re: [PATCH 2/2] audit: Receive unmount event

Quoted 6 lines · Jan Kara

Although audit_watch_handle_event() can handle FS_UNMOUNT event, it is not part of AUDIT_FS_WATCH mask and thus such event never gets to audit_watch_handle_event(). Thus fsnotify marks are deleted by fsnotify subsystem on unmount without audit being notified about that which leads to a strange state of existing audit rules with dead fsnotify marks.

Quoted 7 lines

Add FS_UNMOUNT to the mask of events to be received so that audit can clean up its state accordingly.

---
 kernel/audit_watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

It's funny how the rest of the audit code handles the FS_UNMOUNT event, but it isn't in the mask. It looks like it was lost in the inotify to fanotify conversion. Since I'm likely sending your other patch up to Linus later this week, and I think this is a reasonable bug-fix, I'm going to include this in the audit/stable-4.13 branch.

Quoted 15 lines
kernel/audit_watch.c +1 -2
index ed748ee40029..9eb8b3511636 100644
@@ -66,7 +66,7 @@ static struct fsnotify_group *audit_watch_group;
 
 /* fsnotify events we care about. */
 #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
-                       FS_MOVE_SELF | FS_EVENT_ON_CHILD)
+                       FS_MOVE_SELF | FS_EVENT_ON_CHILD | FS_UNMOUNT)
 
 static void audit_free_parent(struct audit_parent *parent)
 {
--

2.12.3

Signature · 2 lines
paul moore
www.paul-moore.com
Reviewed-by
#7 lore raw

Re: [PATCH 2/2] audit: Receive unmount event

Quoted 11 lines · Paul Moore
Quoted 10 lines · Jan Kara

Although audit_watch_handle_event() can handle FS_UNMOUNT event, it is not part of AUDIT_FS_WATCH mask and thus such event never gets to audit_watch_handle_event(). Thus fsnotify marks are deleted by fsnotify subsystem on unmount without audit being notified about that which leads to a strange state of existing audit rules with dead fsnotify marks.

Add FS_UNMOUNT to the mask of events to be received so that audit can clean up its state accordingly.

Quoted 21 lines
Quoted 3 lines
---
 kernel/audit_watch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

It's funny how the rest of the audit code handles the FS_UNMOUNT event, but it isn't in the mask. It looks like it was lost in the inotify to fanotify conversion. Since I'm likely sending your other patch up to Linus later this week, and I think this is a reasonable bug-fix, I'm going to include this in the audit/stable-4.13 branch.

Quoted 11 lines
kernel/audit_watch.c +1 -1
index ed748ee40029..9eb8b3511636 100644
@@ -66,7 +66,7 @@ static struct fsnotify_group *audit_watch_group;
 
 /* fsnotify events we care about. */
 #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
-                       FS_MOVE_SELF | FS_EVENT_ON_CHILD)
+                       FS_MOVE_SELF | FS_EVENT_ON_CHILD | FS_UNMOUNT)
 
Quoted 13 lines
Quoted 4 lines
kernel/audit_watch.c +0 -1
 static void audit_free_parent(struct audit_parent *parent)
 {
--

2.12.3

Signature · 7 lines
paul moore
www.paul-moore.com

--
Linux-audit mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/linux-audit

- RGB

--
Richard Guy Briggs <[email protected]>
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