Skip to content

Commit dab741e

Browse files
saittamAl Viro
authored andcommitted
Add a "nosymfollow" mount option.
For mounts that have the new "nosymfollow" option, don't follow symlinks when resolving paths. The new option is similar in spirit to the existing "nodev", "noexec", and "nosuid" options, as well as to the LOOKUP_NO_SYMLINKS resolve flag in the openat2(2) syscall. Various BSD variants have been supporting the "nosymfollow" mount option for a long time with equivalent implementations. Note that symlinks may still be created on file systems mounted with the "nosymfollow" option present. readlink() remains functional, so user space code that is aware of symlinks can still choose to follow them explicitly. Setting the "nosymfollow" mount option helps prevent privileged writers from modifying files unintentionally in case there is an unexpected link along the accessed path. The "nosymfollow" option is thus useful as a defensive measure for systems that need to deal with untrusted file systems in privileged contexts. More information on the history and motivation for this patch can be found here: https://sites.google.com/a/chromium.org/dev/chromium-os/chromiumos-design-docs/hardening-against-malicious-stateful-data#TOC-Restricting-symlink-traversal Signed-off-by: Mattias Nissler <mnissler@chromium.org> Signed-off-by: Ross Zwisler <zwisler@google.com> Reviewed-by: Aleksa Sarai <cyphar@cyphar.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 9123e3a commit dab741e

7 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎fs/namei.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ static const char *pick_link(struct nameidata *nd, struct path *link,
16261626
return ERR_PTR(error);
16271627
}
16281628

1629-
if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS))
1629+
if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1630+
unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
16301631
return ERR_PTR(-ELOOP);
16311632

16321633
if (!(nd->flags & LOOKUP_RCU)) {

‎fs/namespace.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,8 @@ int path_mount(const char *dev_name, struct path *path,
31603160
mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
31613161
if (flags & MS_RDONLY)
31623162
mnt_flags |= MNT_READONLY;
3163+
if (flags & MS_NOSYMFOLLOW)
3164+
mnt_flags |= MNT_NOSYMFOLLOW;
31633165

31643166
/* The default atime for remount is preservation */
31653167
if ((flags & MS_REMOUNT) &&

‎fs/proc_namespace.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
7070
{ MNT_NOATIME, ",noatime" },
7171
{ MNT_NODIRATIME, ",nodiratime" },
7272
{ MNT_RELATIME, ",relatime" },
73+
{ MNT_NOSYMFOLLOW, ",nosymfollow" },
7374
{ 0, NULL }
7475
};
7576
const struct proc_fs_opts *fs_infop;

‎fs/statfs.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static int flags_by_mnt(int mnt_flags)
2929
flags |= ST_NODIRATIME;
3030
if (mnt_flags & MNT_RELATIME)
3131
flags |= ST_RELATIME;
32+
if (mnt_flags & MNT_NOSYMFOLLOW)
33+
flags |= ST_NOSYMFOLLOW;
3234
return flags;
3335
}
3436

‎include/linux/mount.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct fs_context;
3030
#define MNT_NODIRATIME 0x10
3131
#define MNT_RELATIME 0x20
3232
#define MNT_READONLY 0x40 /* does the user want this to be r/o? */
33+
#define MNT_NOSYMFOLLOW 0x80
3334

3435
#define MNT_SHRINKABLE 0x100
3536
#define MNT_WRITE_HOLD 0x200
@@ -46,7 +47,7 @@ struct fs_context;
4647
#define MNT_SHARED_MASK (MNT_UNBINDABLE)
4748
#define MNT_USER_SETTABLE_MASK (MNT_NOSUID | MNT_NODEV | MNT_NOEXEC \
4849
| MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME \
49-
| MNT_READONLY)
50+
| MNT_READONLY | MNT_NOSYMFOLLOW)
5051
#define MNT_ATIME_MASK (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME )
5152

5253
#define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | \

‎include/linux/statfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct kstatfs {
4040
#define ST_NOATIME 0x0400 /* do not update access times */
4141
#define ST_NODIRATIME 0x0800 /* do not update directory access times */
4242
#define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */
43+
#define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */
4344

4445
struct dentry;
4546
extern int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid);

‎include/uapi/linux/mount.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define MS_REMOUNT 32 /* Alter flags of a mounted FS */
1717
#define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */
1818
#define MS_DIRSYNC 128 /* Directory modifications are synchronous */
19+
#define MS_NOSYMFOLLOW 256 /* Do not follow symlinks */
1920
#define MS_NOATIME 1024 /* Do not update access times. */
2021
#define MS_NODIRATIME 2048 /* Do not update directory access times */
2122
#define MS_BIND 4096

0 commit comments

Comments
 (0)