Skip to content

Commit c50fd34

Browse files
ebiedermgregkh
authored andcommitted
mnt: Add a per mount namespace limit on the number of mounts
commit d29216842a85c7970c536108e093963f02714498 upstream. CAI Qian <caiqian@redhat.com> pointed out that the semantics of shared subtrees make it possible to create an exponentially increasing number of mounts in a mount namespace. mkdir /tmp/1 /tmp/2 mount --make-rshared / for i in $(seq 1 20) ; do mount --bind /tmp/1 /tmp/2 ; done Will create create 2^20 or 1048576 mounts, which is a practical problem as some people have managed to hit this by accident. As such CVE-2016-6213 was assigned. Ian Kent <raven@themaw.net> described the situation for autofs users as follows: > The number of mounts for direct mount maps is usually not very large because of > the way they are implemented, large direct mount maps can have performance > problems. There can be anywhere from a few (likely case a few hundred) to less > than 10000, plus mounts that have been triggered and not yet expired. > > Indirect mounts have one autofs mount at the root plus the number of mounts that > have been triggered and not yet expired. > > The number of autofs indirect map entries can range from a few to the common > case of several thousand and in rare cases up to between 30000 and 50000. I've > not heard of people with maps larger than 50000 entries. > > The larger the number of map entries the greater the possibility for a large > number of active mounts so it's not hard to expect cases of a 1000 or somewhat > more active mounts. So I am setting the default number of mounts allowed per mount namespace at 100,000. This is more than enough for any use case I know of, but small enough to quickly stop an exponential increase in mounts. Which should be perfect to catch misconfigurations and malfunctioning programs. For anyone who needs a higher limit this can be changed by writing to the new /proc/sys/fs/mount-max sysctl. Tested-by: CAI Qian <caiqian@redhat.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> [bwh: Backported to 4.4: adjust context] Signed-off-by: Ben Hutchings <ben.hutchings@codethink.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 59e0cd1 commit c50fd34

7 files changed

Lines changed: 71 additions & 2 deletions

File tree

Documentation/sysctl/fs.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ aio-nr can grow to.
265265

266266
==============================================================
267267

268+
mount-max:
269+
270+
This denotes the maximum number of mounts that may exist
271+
in a mount namespace.
272+
273+
==============================================================
274+
268275

269276
2. /proc/sys/fs/binfmt_misc
270277
----------------------------------------------------------

fs/mount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct mnt_namespace {
1313
u64 seq; /* Sequence number to prevent loops */
1414
wait_queue_head_t poll;
1515
u64 event;
16+
unsigned int mounts; /* # of mounts in the namespace */
17+
unsigned int pending_mounts;
1618
};
1719

1820
struct mnt_pcp {

fs/namespace.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "pnode.h"
2828
#include "internal.h"
2929

30+
/* Maximum number of mounts in a mount namespace */
31+
unsigned int sysctl_mount_max __read_mostly = 100000;
32+
3033
static unsigned int m_hash_mask __read_mostly;
3134
static unsigned int m_hash_shift __read_mostly;
3235
static unsigned int mp_hash_mask __read_mostly;
@@ -925,6 +928,9 @@ static void commit_tree(struct mount *mnt)
925928

926929
list_splice(&head, n->list.prev);
927930

931+
n->mounts += n->pending_mounts;
932+
n->pending_mounts = 0;
933+
928934
__attach_mnt(mnt, parent);
929935
touch_mnt_namespace(n);
930936
}
@@ -1445,11 +1451,16 @@ static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
14451451
propagate_umount(&tmp_list);
14461452

14471453
while (!list_empty(&tmp_list)) {
1454+
struct mnt_namespace *ns;
14481455
bool disconnect;
14491456
p = list_first_entry(&tmp_list, struct mount, mnt_list);
14501457
list_del_init(&p->mnt_expire);
14511458
list_del_init(&p->mnt_list);
1452-
__touch_mnt_namespace(p->mnt_ns);
1459+
ns = p->mnt_ns;
1460+
if (ns) {
1461+
ns->mounts--;
1462+
__touch_mnt_namespace(ns);
1463+
}
14531464
p->mnt_ns = NULL;
14541465
if (how & UMOUNT_SYNC)
14551466
p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
@@ -1850,6 +1861,28 @@ static int invent_group_ids(struct mount *mnt, bool recurse)
18501861
return 0;
18511862
}
18521863

1864+
int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1865+
{
1866+
unsigned int max = READ_ONCE(sysctl_mount_max);
1867+
unsigned int mounts = 0, old, pending, sum;
1868+
struct mount *p;
1869+
1870+
for (p = mnt; p; p = next_mnt(p, mnt))
1871+
mounts++;
1872+
1873+
old = ns->mounts;
1874+
pending = ns->pending_mounts;
1875+
sum = old + pending;
1876+
if ((old > sum) ||
1877+
(pending > sum) ||
1878+
(max < sum) ||
1879+
(mounts > (max - sum)))
1880+
return -ENOSPC;
1881+
1882+
ns->pending_mounts = pending + mounts;
1883+
return 0;
1884+
}
1885+
18531886
/*
18541887
* @source_mnt : mount tree to be attached
18551888
* @nd : place the mount tree @source_mnt is attached
@@ -1919,6 +1952,7 @@ static int attach_recursive_mnt(struct mount *source_mnt,
19191952
struct path *parent_path)
19201953
{
19211954
HLIST_HEAD(tree_list);
1955+
struct mnt_namespace *ns = dest_mnt->mnt_ns;
19221956
struct mountpoint *smp;
19231957
struct mount *child, *p;
19241958
struct hlist_node *n;
@@ -1931,6 +1965,13 @@ static int attach_recursive_mnt(struct mount *source_mnt,
19311965
if (IS_ERR(smp))
19321966
return PTR_ERR(smp);
19331967

1968+
/* Is there space to add these mounts to the mount namespace? */
1969+
if (!parent_path) {
1970+
err = count_mounts(ns, source_mnt);
1971+
if (err)
1972+
goto out;
1973+
}
1974+
19341975
if (IS_MNT_SHARED(dest_mnt)) {
19351976
err = invent_group_ids(source_mnt, true);
19361977
if (err)
@@ -1970,11 +2011,14 @@ static int attach_recursive_mnt(struct mount *source_mnt,
19702011
out_cleanup_ids:
19712012
while (!hlist_empty(&tree_list)) {
19722013
child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2014+
child->mnt_parent->mnt_ns->pending_mounts = 0;
19732015
umount_tree(child, UMOUNT_SYNC);
19742016
}
19752017
unlock_mount_hash();
19762018
cleanup_group_ids(source_mnt, NULL);
19772019
out:
2020+
ns->pending_mounts = 0;
2021+
19782022
read_seqlock_excl(&mount_lock);
19792023
put_mountpoint(smp);
19802024
read_sequnlock_excl(&mount_lock);
@@ -2804,6 +2848,8 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
28042848
init_waitqueue_head(&new_ns->poll);
28052849
new_ns->event = 0;
28062850
new_ns->user_ns = get_user_ns(user_ns);
2851+
new_ns->mounts = 0;
2852+
new_ns->pending_mounts = 0;
28072853
return new_ns;
28082854
}
28092855

@@ -2853,6 +2899,7 @@ struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
28532899
q = new;
28542900
while (p) {
28552901
q->mnt_ns = new_ns;
2902+
new_ns->mounts++;
28562903
if (new_fs) {
28572904
if (&p->mnt == new_fs->root.mnt) {
28582905
new_fs->root.mnt = mntget(&q->mnt);
@@ -2891,6 +2938,7 @@ static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
28912938
struct mount *mnt = real_mount(m);
28922939
mnt->mnt_ns = new_ns;
28932940
new_ns->root = mnt;
2941+
new_ns->mounts++;
28942942
list_add(&mnt->mnt_list, &new_ns->list);
28952943
} else {
28962944
mntput(m);

fs/pnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static int propagate_one(struct mount *m)
259259
read_sequnlock_excl(&mount_lock);
260260
}
261261
hlist_add_head(&child->mnt_hash, list);
262-
return 0;
262+
return count_mounts(m->mnt_ns, child);
263263
}
264264

265265
/*

fs/pnode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp,
5454
struct mount *copy_tree(struct mount *, struct dentry *, int);
5555
bool is_path_reachable(struct mount *, struct dentry *,
5656
const struct path *root);
57+
int count_mounts(struct mnt_namespace *ns, struct mount *mnt);
5758
#endif /* _LINUX_PNODE_H */

include/linux/mount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ extern void mark_mounts_for_expiry(struct list_head *mounts);
9595

9696
extern dev_t name_to_dev_t(const char *name);
9797

98+
extern unsigned int sysctl_mount_max;
99+
98100
#endif /* _LINUX_MOUNT_H */

kernel/sysctl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <linux/sched/sysctl.h>
6666
#include <linux/kexec.h>
6767
#include <linux/bpf.h>
68+
#include <linux/mount.h>
6869

6970
#include <asm/uaccess.h>
7071
#include <asm/processor.h>
@@ -1749,6 +1750,14 @@ static struct ctl_table fs_table[] = {
17491750
.mode = 0644,
17501751
.proc_handler = proc_doulongvec_minmax,
17511752
},
1753+
{
1754+
.procname = "mount-max",
1755+
.data = &sysctl_mount_max,
1756+
.maxlen = sizeof(unsigned int),
1757+
.mode = 0644,
1758+
.proc_handler = proc_dointvec_minmax,
1759+
.extra1 = &one,
1760+
},
17521761
{ }
17531762
};
17541763

0 commit comments

Comments
 (0)