From patchwork Thu Nov 17 15:14:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattias Nissler X-Patchwork-Id: 9434587 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B27106047D for ; Thu, 17 Nov 2016 17:02:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A142D29669 for ; Thu, 17 Nov 2016 17:02:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 95B0B29665; Thu, 17 Nov 2016 17:02:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1C3F29667 for ; Thu, 17 Nov 2016 17:02:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933482AbcKQRCV (ORCPT ); Thu, 17 Nov 2016 12:02:21 -0500 Received: from mail-wm0-f53.google.com ([74.125.82.53]:38834 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933477AbcKQRCR (ORCPT ); Thu, 17 Nov 2016 12:02:17 -0500 Received: by mail-wm0-f53.google.com with SMTP id f82so158855773wmf.1 for ; Thu, 17 Nov 2016 09:02:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:cc:subject:date:message-id; bh=zvy8PoDlaH/qVV/CjAa7HhA9Bp+XBqcYn/c/EwXtTq4=; b=LFDXrGAnYwABghiJy8XcJao962pxB+/XtToaQBH1J61O0Roq1Oy4cqlWdvrkD3nqRM XrFruWWXfFPfRQv8R9aSQBbSWm1f4DPWwQpWNYyJHDVY/YKZiBaWbAScaHSB0AAvDfqV Pl25k0CBJ2Iy6AVbYrgtCnRx1fdnzdIlEWjUX3Zd8TWyO6TvnCX5zyFzmtGjqKyqFw4n LxkS4tQsptLjCO9MBKPI5gEZuMdtpTd8Dvc5/eIolgVJyJt3dqbEfJ4J0SJKdOtf82V2 H6yYrZS/fpT4IYqjs6FW27tZZyPZ/s4OwPjx4q89Y0NilmgvEvtppWwuDMqcaT5HAQ77 QpMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=zvy8PoDlaH/qVV/CjAa7HhA9Bp+XBqcYn/c/EwXtTq4=; b=AIbiqvx3ORYP+iH2Agi6zuYr+ZyA00ybAYR5EAFxLTr1Q1qKY52Y4DPemADqQ5dr+/ wWHTtfJIf6bU0pkxXn4zGxOxktt7FStrqu17QOTu18GtdmXMC7ylvNp8UZyFWis0hmj8 wtaCJuPPnVR1wpu1C1RFq1BD92eILsGM9x4/0qxnfpYpoV6/jX+w4LT1m8KyRfVnLS8Q 6L/Z2ig+c6PeTnp15u3bW4z6mXFtIAZcSjKKV/YsStwS0S/QcfY3pm3GM/LL+Sd2Xnox 9r2/WDqjSnpfT5Rr4A4B8AOCk9l/r651RH3TW5uoHFSp1ODjC5zctpBt03TvAoADWcwF 7RpQ== X-Gm-Message-State: AKaTC02RNEnV61qt89Sw0wr96ueQLCKCcGD+Xq6qErh4MOybOzCtrhRBonsy2peqF8STZgeJ X-Received: by 10.194.21.35 with SMTP id s3mr2462582wje.192.1479395674617; Thu, 17 Nov 2016 07:14:34 -0800 (PST) Received: from toroa.ham.corp.google.com ([172.16.99.5]) by smtp.gmail.com with ESMTPSA id l187sm4277398wml.6.2016.11.17.07.14.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Nov 2016 07:14:33 -0800 (PST) From: Mattias Nissler To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , linux-kernel@vger.kernel.org, Colin Walters , "Austin S . Hemmelgarn" Subject: [PATCH v3] Add a "nosymfollow" mount option. Date: Thu, 17 Nov 2016 16:14:22 +0100 Message-Id: <1479395662-86614-1-git-send-email-mnissler@chromium.org> X-Mailer: git-send-email 2.8.0.rc3.226.g39d4020 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. 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. Signed-off-by: Mattias Nissler --- Changes since v2: - Updated the option name to align with BSD naming. I have also uploaded the corresponding util-linux patch to make the "mount" command understand the new option here: https://gist.github.com/anonymous/317207ae499389235258f0d52ab22c03 fs/namei.c | 3 +++ fs/namespace.c | 9 ++++++--- fs/proc_namespace.c | 1 + fs/statfs.c | 2 ++ include/linux/mount.h | 3 ++- include/linux/statfs.h | 1 + include/uapi/linux/fs.h | 1 + 7 files changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 5b4eed2..4751e7f 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1021,6 +1021,9 @@ const char *get_link(struct nameidata *nd) touch_atime(&last->link); } + if (nd->path.mnt->mnt_flags & MNT_NOSYMFOLLOW) + return ERR_PTR(-EACCES); + error = security_inode_follow_link(dentry, inode, nd->flags & LOOKUP_RCU); if (unlikely(error)) diff --git a/fs/namespace.c b/fs/namespace.c index e6c234b..2bf244e 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2732,6 +2732,8 @@ long do_mount(const char *dev_name, const char __user *dir_name, mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); if (flags & MS_RDONLY) mnt_flags |= MNT_READONLY; + if (flags & MS_NOSYMFOLLOW) + mnt_flags |= MNT_NOSYMFOLLOW; /* The default atime for remount is preservation */ if ((flags & MS_REMOUNT) && @@ -2741,9 +2743,10 @@ long do_mount(const char *dev_name, const char __user *dir_name, mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; } - flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | - MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | - MS_STRICTATIME | MS_NOREMOTELOCK); + flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_NOSYMFOLLOW | + MS_ACTIVE | MS_BORN | MS_NOATIME | MS_NODIRATIME | + MS_RELATIME | MS_KERNMOUNT | MS_STRICTATIME | + MS_NOREMOTELOCK); if (flags & MS_REMOUNT) retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c index 3f1190d..366149d 100644 --- a/fs/proc_namespace.c +++ b/fs/proc_namespace.c @@ -67,6 +67,7 @@ static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt) { MNT_NOATIME, ",noatime" }, { MNT_NODIRATIME, ",nodiratime" }, { MNT_RELATIME, ",relatime" }, + { MNT_NOSYMFOLLOW, ",nosymfollow" }, { 0, NULL } }; const struct proc_fs_info *fs_infop; diff --git a/fs/statfs.c b/fs/statfs.c index 083dc0a..cfd4da8 100644 --- a/fs/statfs.c +++ b/fs/statfs.c @@ -27,6 +27,8 @@ static int flags_by_mnt(int mnt_flags) flags |= ST_NODIRATIME; if (mnt_flags & MNT_RELATIME) flags |= ST_RELATIME; + if (mnt_flags & MNT_NOSYMFOLLOW) + flags |= ST_NOSYMFOLLOW; return flags; } diff --git a/include/linux/mount.h b/include/linux/mount.h index 1172cce..c9e8d7b 100644 --- a/include/linux/mount.h +++ b/include/linux/mount.h @@ -28,6 +28,7 @@ struct mnt_namespace; #define MNT_NODIRATIME 0x10 #define MNT_RELATIME 0x20 #define MNT_READONLY 0x40 /* does the user want this to be r/o? */ +#define MNT_NOSYMFOLLOW 0x80 #define MNT_SHRINKABLE 0x100 #define MNT_WRITE_HOLD 0x200 @@ -44,7 +45,7 @@ struct mnt_namespace; #define MNT_SHARED_MASK (MNT_UNBINDABLE) #define MNT_USER_SETTABLE_MASK (MNT_NOSUID | MNT_NODEV | MNT_NOEXEC \ | MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME \ - | MNT_READONLY) + | MNT_READONLY | MNT_NOSYMFOLLOW) #define MNT_ATIME_MASK (MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME ) #define MNT_INTERNAL_FLAGS (MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | \ diff --git a/include/linux/statfs.h b/include/linux/statfs.h index 0166d32..6bab2b2 100644 --- a/include/linux/statfs.h +++ b/include/linux/statfs.h @@ -39,5 +39,6 @@ struct kstatfs { #define ST_NOATIME 0x0400 /* do not update access times */ #define ST_NODIRATIME 0x0800 /* do not update directory access times */ #define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */ +#define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */ #endif diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index acb2b61..c978b25 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -130,6 +130,7 @@ struct inodes_stat_t { #define MS_I_VERSION (1<<23) /* Update inode I_version field */ #define MS_STRICTATIME (1<<24) /* Always perform atime updates */ #define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */ +#define MS_NOSYMFOLLOW (1<<26) /* Do not follow symlinks */ /* These sb flags are internal to the kernel */ #define MS_NOREMOTELOCK (1<<27)