Skip to contentSkip to conversation

fsnotify: don't put user context if it was never assigned

PATCH In review Sasha Levin 4 participants 7 messages Jul 29, 2014 – Sep 12, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsnotify: don't put user context if it was never assigned

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

---
 fs/notify/inotify/inotify_fsnotify.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 43ab1e1..9c8187e 100644
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	/* ideally the idr is empty and we won't hit the BUG in the callback */
 	idr_for_each(&group->inotify_data.idr, idr_callback, group);
 	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
Signature · 1 line
1.7.10.4
#1 lore raw

Ping? This is a NULL ptr deref that userspace can trigger.

Quoted 27 lines · Sasha Levin

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

---
 fs/notify/inotify/inotify_fsnotify.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 43ab1e1..9c8187e 100644
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	/* ideally the idr is empty and we won't hit the BUG in the callback */
 	idr_for_each(&group->inotify_data.idr, idr_callback, group);
 	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
#2 lore raw

Ping?

Quoted 31 lines · Sasha Levin

Ping? This is a NULL ptr deref that userspace can trigger.

Quoted 27 lines · Sasha Levin

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

---
 fs/notify/inotify/inotify_fsnotify.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 43ab1e1..9c8187e 100644
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	/* ideally the idr is empty and we won't hit the BUG in the callback */
 	idr_for_each(&group->inotify_data.idr, idr_callback, group);
 	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
Reviewed-by on v1 1/1
#3 lore raw
In reply to Sasha Levin

Hello Sasha,

I have CCed Jan, because he has been the only one working on this file in the last 18 months.

A failure path in which group->inotify_data.user is not yet assigned starts here:

static struct fsnotify_group *inotify_new_group(unsigned int max_events) {
...

	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
	if (unlikely(!oevent)) {
		fsnotify_destroy_group(group);
		return ERR_PTR(-ENOMEM);
	}

So your check
if (group->inotify_data.user)
is reasonable.

3.17.0-rc3 compiles with the patch applied.

@Andrew
Please, add the patch to the mm-tree.

Best regards

Heinrich Schuchardt

Quoted 27 lines · Sasha Levin

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

---
  fs/notify/inotify/inotify_fsnotify.c |    6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 43ab1e1..9c8187e 100644
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
  	/* ideally the idr is empty and we won't hit the BUG in the callback */
  	idr_for_each(&group->inotify_data.idr, idr_callback, group);
  	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
  }
 
  static void inotify_free_event(struct fsnotify_event *fsn_event)
maintainer Reviewed-by
#4 lore raw
Quoted 21 lines · Heinrich Schuchardt

Hello Sasha,

I have CCed Jan, because he has been the only one working on this file in the last 18 months.

A failure path in which group->inotify_data.user is not yet assigned starts here:

static struct fsnotify_group *inotify_new_group(unsigned int max_events) {
...

	oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
	if (unlikely(!oevent)) {
		fsnotify_destroy_group(group);
		return ERR_PTR(-ENOMEM);
	}

So your check
if (group->inotify_data.user)
is reasonable.

Quoted 6 lines

3.17.0-rc3 compiles with the patch applied.

@Andrew
Please, add the patch to the mm-tree.

Yeah, the patch looks good. Thanks for CC. You can add:

Honza

Quoted 29 lines · Sasha Levin
Quoted 27 lines

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

---
 fs/notify/inotify/inotify_fsnotify.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 43ab1e1..9c8187e 100644
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	/* ideally the idr is empty and we won't hit the BUG in the callback */
 	idr_for_each(&group->inotify_data.idr, idr_callback, group);
 	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
In reply to Sasha Levin
Quoted 3 lines · On Tue, 29 Jul 2014 09:25:14 -0400 Sasha Levin <[email protected]> wrote:

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

Are you able to identify "some failure paths"? I spent some time grepping, but it's a pain.

Please try to include such info in changelogs because reviewers (ie, me) might want to review those callers to decide whether the bug lies elsewhere.

Quoted 15 lines
fs/notify/inotify/inotify_fsnotify.c +4 -2
@@ -165,8 +165,10 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	/* ideally the idr is empty and we won't hit the BUG in the callback */
 	idr_for_each(&group->inotify_data.idr, idr_callback, group);
 	idr_destroy(&group->inotify_data.idr);
-	atomic_dec(&group->inotify_data.user->inotify_devs);
-	free_uid(group->inotify_data.user);
+	if (group->inotify_data.user) {
+		atomic_dec(&group->inotify_data.user->inotify_devs);
+		free_uid(group->inotify_data.user);
+	}
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
#6 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Tue, 29 Jul 2014 09:25:14 -0400 Sasha Levin <[email protected]> wrote:
Quoted 3 lines

On some failure paths we may attempt to free user context even if it wasn't assigned yet. This will cause a NULL ptr deref and a kernel BUG.

Quoted 7 lines

Are you able to identify "some failure paths"? I spent some time grepping, but it's a pain.

Please try to include such info in changelogs because reviewers (ie, me) might want to review those callers to decide whether the bug lies elsewhere.

Sorry about that.

The path I was looking at is in inotify_new_group():

        oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL);
        if (unlikely(!oevent)) {
                fsnotify_destroy_group(group);
                return ERR_PTR(-ENOMEM);
        }

fsnotify_destroy_group() would get called here, but group->inotify_data.user is only getting assigned later:

	group->inotify_data.user = get_current_user();

Thanks,
Sasha