Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Mattias Nissler and committed by
Al Viro
dab741e0 9123e3a7

+11 -2
+2 -1
fs/namei.c
··· 1626 1626 return ERR_PTR(error); 1627 1627 } 1628 1628 1629 - if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS)) 1629 + if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || 1630 + unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) 1630 1631 return ERR_PTR(-ELOOP); 1631 1632 1632 1633 if (!(nd->flags & LOOKUP_RCU)) {
+2
fs/namespace.c
··· 3160 3160 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 3161 3161 if (flags & MS_RDONLY) 3162 3162 mnt_flags |= MNT_READONLY; 3163 + if (flags & MS_NOSYMFOLLOW) 3164 + mnt_flags |= MNT_NOSYMFOLLOW; 3163 3165 3164 3166 /* The default atime for remount is preservation */ 3165 3167 if ((flags & MS_REMOUNT) &&
+1
fs/proc_namespace.c
··· 70 70 { MNT_NOATIME, ",noatime" }, 71 71 { MNT_NODIRATIME, ",nodiratime" }, 72 72 { MNT_RELATIME, ",relatime" }, 73 + { MNT_NOSYMFOLLOW, ",nosymfollow" }, 73 74 { 0, NULL } 74 75 }; 75 76 const struct proc_fs_opts *fs_infop;
+2
fs/statfs.c
··· 29 29 flags |= ST_NODIRATIME; 30 30 if (mnt_flags & MNT_RELATIME) 31 31 flags |= ST_RELATIME; 32 + if (mnt_flags & MNT_NOSYMFOLLOW) 33 + flags |= ST_NOSYMFOLLOW; 32 34 return flags; 33 35 } 34 36
+2 -1
include/linux/mount.h
··· 30 30 #define MNT_NODIRATIME 0x10 31 31 #define MNT_RELATIME 0x20 32 32 #define MNT_READONLY 0x40 /* does the user want this to be r/o? */ 33 + #define MNT_NOSYMFOLLOW 0x80 33 34 34 35 #define MNT_SHRINKABLE 0x100 35 36 #define MNT_WRITE_HOLD 0x200 ··· 47 46 #define MNT_SHARED_MASK (MNT_UNBINDABLE) 48 47 #define MNT_USER_SETTABLE_MASK (MNT_NOSUID | MNT_NODEV | MNT_NOEXEC \ 49 48 | MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME \ 50 - | MNT_READONLY) 49 + | MNT_READONLY | MNT_NOSYMFOLLOW) 51 50 #define MNT_ATIME_MASK (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME ) 52 51 53 52 #define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | \
+1
include/linux/statfs.h
··· 40 40 #define ST_NOATIME 0x0400 /* do not update access times */ 41 41 #define ST_NODIRATIME 0x0800 /* do not update directory access times */ 42 42 #define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */ 43 + #define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */ 43 44 44 45 struct dentry; 45 46 extern int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid);
+1
include/uapi/linux/mount.h
··· 16 16 #define MS_REMOUNT 32 /* Alter flags of a mounted FS */ 17 17 #define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */ 18 18 #define MS_DIRSYNC 128 /* Directory modifications are synchronous */ 19 + #define MS_NOSYMFOLLOW 256 /* Do not follow symlinks */ 19 20 #define MS_NOATIME 1024 /* Do not update access times. */ 20 21 #define MS_NODIRATIME 2048 /* Do not update directory access times */ 21 22 #define MS_BIND 4096