Skip to contentSkip to conversation

inotify: race use after free/double free in inotify inode marks

PATCH In review Eric Paris 2 participants 3 messages May 12, 2010 – May 13, 2010

Subsystems (2)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 inotify: race use after free/double free in inotify inode marks

From: root <[email protected]>

There is a race in the inotify add/rm watch code. A task can find and remove a mark which doesn't have all of it's references. This can result in a use after free/double free situation.

Task A Task B
------------ -----------
inotify_new_watch()
allocate a mark (refcnt == 1)
add it to the idr
inotify_rm_watch()
inotify_remove_from_idr()
fsnotify_put_mark()
refcnt hits 0, free
take reference because we are on idr
[at this point it is a use after free]
[time goes on]
refcnt may hit 0 again, double free

The fix is to take the reference BEFORE the object can be found in the idr.

---
 fs/notify/inotify/inotify_user.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
fs/notify/inotify/inotify_user.c +6 -3
index 40da732..e46ca68 100644
@@ -546,21 +546,24 @@ retry:
 	if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL)))
 		goto out_err;
 
+	/* we are putting the mark on the idr, take a reference */
+	fsnotify_get_mark(&tmp_ientry->fsn_entry);
+
 	spin_lock(&group->inotify_data.idr_lock);
 	ret = idr_get_new_above(&group->inotify_data.idr, &tmp_ientry->fsn_entry,
 				group->inotify_data.last_wd+1,
 				&tmp_ientry->wd);
 	spin_unlock(&group->inotify_data.idr_lock);
 	if (ret) {
+		/* we didn't get on the idr, drop the idr reference */
+		fsnotify_put_mark(&tmp_ientry->fsn_entry);
+
 		/* idr was out of memory allocate and try again */
 		if (ret == -EAGAIN)
 			goto retry;
 		goto out_err;
 	}
 
-	/* we put the mark on the idr, take a reference */
-	fsnotify_get_mark(&tmp_ientry->fsn_entry);
-
 	/* we are on the idr, now get on the inode */
 	ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode);
 	if (ret) {
Signature · 1 line
1.6.5.3
#1 lore raw
Quoted 1 line · On Wed, 12 May 2010 12:08:00 -0400 Eric Paris <[email protected]> wrote:

From: root <[email protected]>

hm.

Quoted 18 lines

There is a race in the inotify add/rm watch code. A task can find and remove a mark which doesn't have all of it's references. This can result in a use after free/double free situation.

Task A Task B
------------ -----------
inotify_new_watch()
allocate a mark (refcnt == 1)
add it to the idr
inotify_rm_watch()
inotify_remove_from_idr()
fsnotify_put_mark()
refcnt hits 0, free
take reference because we are on idr
[at this point it is a use after free]
[time goes on]
refcnt may hit 0 again, double free

Quoted 4 lines

The fix is to take the reference BEFORE the object can be found in the idr.

The changelog has no "Cc: <[email protected]>". I suspect it should have that?

author
#2 lore raw
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Wed, 12 May 2010 12:08:00 -0400 Eric Paris <[email protected]> wrote:

From: root <[email protected]>

hm.

Well poop. That's what I get for using git on a random test box. I moved it to my real tree with git-format-patch > git-am. I'll fix the author.

Quoted 18 lines
Quoted 18 lines

There is a race in the inotify add/rm watch code. A task can find and remove a mark which doesn't have all of it's references. This can result in a use after free/double free situation.

Task A Task B
------------ -----------
inotify_new_watch()
allocate a mark (refcnt == 1)
add it to the idr
inotify_rm_watch()
inotify_remove_from_idr()
fsnotify_put_mark()
refcnt hits 0, free
take reference because we are on idr
[at this point it is a use after free]
[time goes on]
refcnt may hit 0 again, double free

Quoted 7 lines
Quoted 4 lines

The fix is to take the reference BEFORE the object can be found in the idr.

The changelog has no "Cc: <[email protected]>". I suspect it should have that?

Will fix before I ask Linus to pull....

-Eric