Skip to content

Commit eae1fb3

Browse files
ebiggersgregkh
authored andcommitted
fs/namespace.c: WARN if mnt_count has become negative
[ Upstream commit edf7ddb ] 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 1b0b9cc ("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. Suggested-by: Miklos Szeredi <miklos@szeredi.hu> Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b1e155c commit eae1fb3

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎fs/namespace.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ static inline void mnt_add_count(struct mount *mnt, int n)
156156
/*
157157
* vfsmount lock must be held for write
158158
*/
159-
unsigned int mnt_get_count(struct mount *mnt)
159+
int mnt_get_count(struct mount *mnt)
160160
{
161161
#ifdef CONFIG_SMP
162-
unsigned int count = 0;
162+
int count = 0;
163163
int cpu;
164164

165165
for_each_possible_cpu(cpu) {
@@ -1139,6 +1139,7 @@ static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
11391139
static void mntput_no_expire(struct mount *mnt)
11401140
{
11411141
LIST_HEAD(list);
1142+
int count;
11421143

11431144
rcu_read_lock();
11441145
if (likely(READ_ONCE(mnt->mnt_ns))) {
@@ -1162,7 +1163,9 @@ static void mntput_no_expire(struct mount *mnt)
11621163
*/
11631164
smp_mb();
11641165
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);
11661169
rcu_read_unlock();
11671170
unlock_mount_hash();
11681171
return;

‎fs/pnode.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int propagate_mount_busy(struct mount *, int);
4444
void propagate_mount_unlock(struct mount *);
4545
void mnt_release_group_id(struct mount *);
4646
int get_dominating_id(struct mount *mnt, const struct path *root);
47-
unsigned int mnt_get_count(struct mount *mnt);
47+
int mnt_get_count(struct mount *mnt);
4848
void mnt_set_mountpoint(struct mount *, struct mountpoint *,
4949
struct mount *);
5050
void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp,

0 commit comments

Comments
 (0)