Skip to contentSkip to conversation

dnotify: Fix ENOMEM handling

PATCH In review Jan Kara 3 participants 12 messages Oct 31, 2017

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author maintainer
#0 lore raw

cover dnotify: Fix ENOMEM handling

Hello,

in response to syzkaller reported I plan to push following two fixes to my tree. Review is welcome.

Honza

author maintainer
#1 lore raw

PATCH 1/2 dnotify: Handle errors from fsnotify_add_mark_locked() in fcntl_dirnotify()

fsnotify_add_mark_locked() can fail (e.g. because of ENOMEM since commit 9dd813c15b2c "fsnotify: Move mark list head from object into dedicated structure"). Handle this error properly in fcntl_dirnotify() as otherwise we just hit BUG_ON(dn_mark->dn) in dnotify_free_mark().

Reported-by syzkaller
Fixes 9dd813c15b2c101168808d4f5941a29985758973
---
 fs/notify/dnotify/dnotify.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/notify/dnotify/dnotify.c +4 -1
index cba328315929..a50183bd0ab9 100644
@@ -319,7 +319,9 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
 		dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
 		spin_lock(&fsn_mark->lock);
 	} else {
-		fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+		error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+		if (error)
+			goto out_err;
 		spin_lock(&new_fsn_mark->lock);
 		fsn_mark = new_fsn_mark;
 		dn_mark = new_dn_mark;
@@ -345,6 +347,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
 		 */
 		if (dn_mark == new_dn_mark)
 			destroy = 1;
+		error = 0;
 		goto out;
 	}
 
Signature · 1 line
2.12.3
#2 lore raw

Re: [PATCH 1/2] dnotify: Handle errors from fsnotify_add_mark_locked() in fcntl_dirnotify()

Quoted 3 lines · Jan Kara

fsnotify_add_mark_locked() can fail (e.g. because of ENOMEM since commit 9dd813c15b2c "fsnotify: Move mark list head from object into dedicated structure").

Since we have the Fixes: tag, I rather have the regression commit only in Fixes:
tag (with the subject) and this description can start with:
"fsnotify_add_mark_locked() can fail, but we do not check its return value."

Quoted 5 lines

Handle this error properly in fcntl_dirnotify() as otherwise we just hit BUG_ON(dn_mark->dn) in dnotify_free_mark().

Reported-by syzkaller
Fixes 9dd813c15b2c101168808d4f5941a29985758973

I think for some backport maintainers the "cc: stable # v4.12" tags matters.

Quoted 10 lines
---
 fs/notify/dnotify/dnotify.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/notify/dnotify/dnotify.c +0 -0
index cba328315929..a50183bd0ab9 100644

fs/notify/dnotify/dnotify.c int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) · line 324

Quoted 7 lines
fs/notify/dnotify/dnotify.c +3 -1
@@ -319,7 +319,9 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
                dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
                spin_lock(&fsn_mark->lock);
        } else {
-               fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               if (error)
+                       goto out_err;

out_err is not unlocking dnotify_group->mark_mutex, and probably need to put fsn_mark as well?

Quoted 14 lines
fs/notify/dnotify/dnotify.c +1 -1
                spin_lock(&new_fsn_mark->lock);
                fsn_mark = new_fsn_mark;
                dn_mark = new_dn_mark;
@@ -345,6 +347,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
                 */
                if (dn_mark == new_dn_mark)
                        destroy = 1;
+               error = 0;
                goto out;
        }
 
--

2.12.3

author maintainer
#3 lore raw

Re: [PATCH 1/2] dnotify: Handle errors from fsnotify_add_mark_locked() in fcntl_dirnotify()

Quoted 8 lines · Amir Goldstein
Quoted 3 lines · Jan Kara

fsnotify_add_mark_locked() can fail (e.g. because of ENOMEM since commit 9dd813c15b2c "fsnotify: Move mark list head from object into dedicated structure").

Since we have the Fixes: tag, I rather have the regression commit only in Fixes:
tag (with the subject) and this description can start with:
"fsnotify_add_mark_locked() can fail, but we do not check its return value."

Good suggestion, I'll update the changelog.

Quoted 8 lines
Quoted 5 lines

Handle this error properly in fcntl_dirnotify() as otherwise we just hit BUG_ON(dn_mark->dn) in dnotify_free_mark().

Reported-by syzkaller
Fixes 9dd813c15b2c101168808d4f5941a29985758973

I think for some backport maintainers the "cc: stable # v4.12" tags matters.

Yeah, but frankly I don't think you can trigger this unless you are using fault injection since GFP_KERNEL slab allocations never fail currently (the kernel will rather retry allocation indefinitely). That's why I didn't CC stable as I don't think real users can ever see this and for stable inclusion the bug needs to be a real treat to users...

Quoted 6 lines
Quoted 6 lines
---
 fs/notify/dnotify/dnotify.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 15 lines

fs/notify/dnotify/dnotify.c int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) · line 324

Quoted 12 lines
fs/notify/dnotify/dnotify.c +3 -1
index cba328315929..a50183bd0ab9 100644
@@ -319,7 +319,9 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
                dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
                spin_lock(&fsn_mark->lock);
        } else {
-               fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               if (error)
+                       goto out_err;

out_err is not unlocking dnotify_group->mark_mutex, and probably need to put fsn_mark as well?

Argh, good point about mark_mutex (I wonder how come this didn't deadlock when I've tested it). You don't need to put fsn_mark - that is guaranteed to be NULL here.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#4 lore raw

Re: [PATCH 1/2] dnotify: Handle errors from fsnotify_add_mark_locked() in fcntl_dirnotify()

Quoted 23 lines · Jan Kara
Quoted 6 lines · Amir Goldstein
Quoted 5 lines · Jan Kara
---
 fs/notify/dnotify/dnotify.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 15 lines

fs/notify/dnotify/dnotify.c int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) · line 324

Quoted 12 lines
fs/notify/dnotify/dnotify.c +3 -1
index cba328315929..a50183bd0ab9 100644
@@ -319,7 +319,9 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
                dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
                spin_lock(&fsn_mark->lock);
        } else {
-               fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               if (error)
+                       goto out_err;

out_err is not unlocking dnotify_group->mark_mutex, and probably need to put fsn_mark as well?

Quoted 3 lines

Argh, good point about mark_mutex (I wonder how come this didn't deadlock when I've tested it). You don't need to put fsn_mark - that is guaranteed to be NULL here.

Attached is a new version of the patch.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Quoted 1 line

From 54abcb76cd57877794e2007ac944d8437baf49c1 Mon Sep 17 00:00:00 2001

From: Jan Kara <[email protected]>
Date: Tue, 31 Oct 2017 09:53:28 +0100
Subject: [PATCH 1/2] dnotify: Handle errors from fsnotify_add_mark_locked() in
fcntl_dirnotify()

fsnotify_add_mark_locked() can fail but we do not check its return value. This didn't matter before commit 9dd813c15b2c "fsnotify: Move mark list head from object into dedicated structure" as none of possible failures could happen for dnotify but after that commit -ENOMEM can be returned. Handle this error properly in fcntl_dirnotify() as otherwise we just hit BUG_ON(dn_mark->dn) in dnotify_free_mark().

Reported-by syzkaller
Fixes 9dd813c15b2c101168808d4f5941a29985758973
---
 fs/notify/dnotify/dnotify.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/notify/dnotify/dnotify.c +6 -1
index cba328315929..63a1ca4b9dee 100644
@@ -319,7 +319,11 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
 		dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
 		spin_lock(&fsn_mark->lock);
 	} else {
-		fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+		error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+		if (error) {
+			mutex_unlock(&dnotify_group->mark_mutex);
+			goto out_err;
+		}
 		spin_lock(&new_fsn_mark->lock);
 		fsn_mark = new_fsn_mark;
 		dn_mark = new_dn_mark;
@@ -345,6 +349,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
 		 */
 		if (dn_mark == new_dn_mark)
 			destroy = 1;
+		error = 0;
 		goto out;
 	}
 
Signature · 1 line
2.12.3
reviewer Reviewed-by
#5 lore raw

Re: [PATCH 1/2] dnotify: Handle errors from fsnotify_add_mark_locked() in fcntl_dirnotify()

Quoted 24 lines · Jan Kara
Quoted 23 lines · Jan Kara
Quoted 21 lines · Amir Goldstein
Quoted 10 lines · Jan Kara
---
 fs/notify/dnotify/dnotify.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/notify/dnotify/dnotify.c +0 -0
index cba328315929..a50183bd0ab9 100644

fs/notify/dnotify/dnotify.c int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg) · line 324

Quoted 7 lines
fs/notify/dnotify/dnotify.c +3 -1
@@ -319,7 +319,9 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
                dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
                spin_lock(&fsn_mark->lock);
        } else {
-               fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               error = fsnotify_add_mark_locked(new_fsn_mark, inode, NULL, 0);
+               if (error)
+                       goto out_err;

out_err is not unlocking dnotify_group->mark_mutex, and probably need to put fsn_mark as well?

Quoted 5 lines
Quoted 3 lines

Argh, good point about mark_mutex (I wonder how come this didn't deadlock when I've tested it). You don't need to put fsn_mark - that is guaranteed to be NULL here.

Attached is a new version of the patch.

author maintainer
#6 lore raw

PATCH 2/2 fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

In reply to Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

---
 fs/notify/mark.c | 2 ++
 1 file changed, 2 insertions(+)
fs/notify/mark.c +2 -0
index 9991f8826734..47a827975b58 100644
@@ -599,9 +599,11 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
 
 	return ret;
 err:
+	spin_lock(&mark->lock);
 	mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
 			 FSNOTIFY_MARK_FLAG_ATTACHED);
 	list_del_init(&mark->g_list);
+	spin_unlock(&mark->lock);
 	atomic_dec(&group->num_marks);
 
 	fsnotify_put_mark(mark);
Signature · 1 line
2.12.3
reviewer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

Quoted 7 lines · Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

IMO, even though this does not fix a concrete bug, if it's worth fixing in upstream, it's worth fixing in stable.
A future stable fix may either make this into a concrete bug or just be harder to apply.

So I suggest to add the Fixes: and Cc: stable tags.

Greg,

Do you agree with this reasoning?

Quoted 23 lines
---
 fs/notify/mark.c | 2 ++
 1 file changed, 2 insertions(+)
fs/notify/mark.c +2 -1
index 9991f8826734..47a827975b58 100644
@@ -599,9 +599,11 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
 
        return ret;
 err:
+       spin_lock(&mark->lock);
        mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
                         FSNOTIFY_MARK_FLAG_ATTACHED);
        list_del_init(&mark->g_list);
+       spin_unlock(&mark->lock);
        atomic_dec(&group->num_marks);
 
        fsnotify_put_mark(mark);
--

2.12.3

#8 lore raw

Re: [PATCH 2/2] fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

Quoted 16 lines · Amir Goldstein
Quoted 7 lines · Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

IMO, even though this does not fix a concrete bug, if it's worth fixing in upstream, it's worth fixing in stable.
A future stable fix may either make this into a concrete bug or just be harder to apply.

Quoted 5 lines

So I suggest to add the Fixes: and Cc: stable tags.

Greg,

Do you agree with this reasoning?

If it doesn't fix an actual bug, how does that fit with the stable kernel rules?

#9 lore raw

Re: [PATCH 2/2] fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

Quoted 17 lines · Greg KH
Quoted 16 lines · Amir Goldstein
Quoted 7 lines · Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

IMO, even though this does not fix a concrete bug, if it's worth fixing in upstream, it's worth fixing in stable.
A future stable fix may either make this into a concrete bug or just be harder to apply.

Quoted 9 lines
Quoted 5 lines

So I suggest to add the Fixes: and Cc: stable tags.

Greg,

Do you agree with this reasoning?

If it doesn't fix an actual bug, how does that fit with the stable kernel rules?

So this is the case of incorrect code w.r.t locking rules that either does not hit a bug because of an indirect protection (as Jan wrote in commit) or we did not find how to hit a bug.

Not sure how you want to call this, but if you think it doesn't belong for stable we won't send it. That's why I called for your opinion.

Thanks,
Amir.

#10 lore raw

Re: [PATCH 2/2] fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

Quoted 28 lines · Amir Goldstein
Quoted 17 lines · Greg KH
Quoted 16 lines · Amir Goldstein
Quoted 7 lines · Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

IMO, even though this does not fix a concrete bug, if it's worth fixing in upstream, it's worth fixing in stable.
A future stable fix may either make this into a concrete bug or just be harder to apply.

Quoted 9 lines
Quoted 5 lines

So I suggest to add the Fixes: and Cc: stable tags.

Greg,

Do you agree with this reasoning?

If it doesn't fix an actual bug, how does that fit with the stable kernel rules?

Quoted 6 lines

So this is the case of incorrect code w.r.t locking rules that either does not hit a bug because of an indirect protection (as Jan wrote in commit) or we did not find how to hit a bug.

Not sure how you want to call this, but if you think it doesn't belong for stable we won't send it. That's why I called for your opinion.

How about we get the opinion of the developer and maintainer of the subsystem, I will defer to them...

author maintainer
#11 lore raw

Re: [PATCH 2/2] fsnotify: Protect bail out path of fsnotify_add_mark_locked() properly

In reply to Amir Goldstein
Quoted 16 lines · Amir Goldstein
Quoted 7 lines · Jan Kara

When fsnotify_add_mark_locked() fails it cleans up the mark it was adding. Since the mark is already visible in group's list, we should protect update of mark->flags with mark->lock. I'm not aware of any real issues this could cause (since we also hold group->mark_mutex) but better be safe and obey locking rules properly.

IMO, even though this does not fix a concrete bug, if it's worth fixing in upstream, it's worth fixing in stable.
A future stable fix may either make this into a concrete bug or just be harder to apply.

Quoted 5 lines

So I suggest to add the Fixes: and Cc: stable tags.

Greg,

Do you agree with this reasoning?

Similarly as with patch 1, I don't think real users can hit this (and even if they could, I doubt it would have any negative impact). So bothering stable with this is just a waste of resources... It falls under the "theoretical race condition" cathegory which is explicitely forbidden from stable.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR