History

fs/namespace.c: WARN if mnt_count has become negative

Missing calls to mntget() (or equivalently, too many calls to mntput()) are hard to detect because mntput() delays freeing mounts using task_work_add(), then again using call_rcu(). As a result, mnt_count can often be decremented to -1 without getting a KASAN use-after-free report. Such cases are still bugs though, and they point to real use-after-frees being possible. For an example of this, see the bug fixed by commit 1b0b9cc8d379 ("vfs: fsmount: add missing mntget()"), discussed at https://lkml.kernel.org/linux-fsdevel/20190605135401.GB30925@xxxxxxxxxxxxxxxxxxxxxxxxx/T/#u. This bug *should* have been trivial to find. But actually, it wasn't found until syzkaller happened to use fchdir() to manipulate the reference count just right for the bug to be noticeable. Address this by making mntput_no_expire() issue a WARN if mnt_count has become negative.

Eric Biggers committed 6 years ago

Commit edf7ddb1 parent b6505452 files · +7 −4Browse files

Unified · Split

2 files
  1. fs/namespace.c +6 −3
  2. fs/pnode.h +1 −1
fs/namespace.c+6−3
@@ -156,10 +156,10 @@ static inline void mnt_add_count(struct mount *mnt, int n) · 155 unmodified lines
156/*156/*
157 * vfsmount lock must be held for write157 * vfsmount lock must be held for write
158 */158 */
159unsigned int mnt_get_count(struct mount *mnt)159int mnt_get_count(struct mount *mnt)
160{160{
161#ifdef CONFIG_SMP161#ifdef CONFIG_SMP
162 unsigned int count = 0;162 int count = 0;
163 int cpu;163 int cpu;
164 164
165 for_each_possible_cpu(cpu) {165 for_each_possible_cpu(cpu) {
@@ -1139,6 +1139,7 @@ static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); · 973 unmodified lines
1139static void mntput_no_expire(struct mount *mnt)1139static void mntput_no_expire(struct mount *mnt)
1140{1140{
1141 LIST_HEAD(list);1141 LIST_HEAD(list);
1142 int count;
1142 1143
1143 rcu_read_lock();1144 rcu_read_lock();
1144 if (likely(READ_ONCE(mnt->mnt_ns))) {1145 if (likely(READ_ONCE(mnt->mnt_ns))) {
@@ -1162,7 +1163,9 @@ static void mntput_no_expire(struct mount *mnt) · 17 unmodified lines
1162 */1163 */
1163 smp_mb();1164 smp_mb();
1164 mnt_add_count(mnt, -1);1165 mnt_add_count(mnt, -1);
1165 if (mnt_get_count(mnt)) {1166 count = mnt_get_count(mnt);
1167 if (count != 0) {
1168 WARN_ON(count < 0);
1166 rcu_read_unlock();1169 rcu_read_unlock();
1167 unlock_mount_hash();1170 unlock_mount_hash();
1168 return;1171 return;
fs/pnode.h+1−1
@@ -44,7 +44,7 @@ int propagate_mount_busy(struct mount *, int); · 43 unmodified lines
44void propagate_mount_unlock(struct mount *);44void propagate_mount_unlock(struct mount *);
45void mnt_release_group_id(struct mount *);45void mnt_release_group_id(struct mount *);
46int get_dominating_id(struct mount *mnt, const struct path *root);46int get_dominating_id(struct mount *mnt, const struct path *root);
47unsigned int mnt_get_count(struct mount *mnt);47int mnt_get_count(struct mount *mnt);
48void mnt_set_mountpoint(struct mount *, struct mountpoint *,48void mnt_set_mountpoint(struct mount *, struct mountpoint *,
49 struct mount *);49 struct mount *);
50void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp,50void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t