From: akpm@linux-foundation.org Subject: [patch 2/6] fs: symlink restrictions on sticky directories Date: Tue, 29 May 2012 11:02:23 -0700 Message-ID: <20120529180223.2CE82A026A@akpm.mtv.corp.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: linux-fsdevel@vger.kernel.org, akpm@linux-foundation.org, keescook@chromium.org, a.p.zijlstra@chello.nl, drosenberg@vsecurity.com, eparis@redhat.com, federica.teodori@googlemail.com, lucian.grijincu@gmail.com, matthew@wil.cx, mingo@elte.hu, rdunlap@xenotime.net, riel@redhat.com To: viro@zeniv.linux.org.uk Return-path: Received: from mail-wg0-f74.google.com ([74.125.82.74]:35796 "EHLO mail-wg0-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754831Ab2E2SCZ (ORCPT ); Tue, 29 May 2012 14:02:25 -0400 Received: by wgbdt11 with SMTP id dt11so272489wgb.1 for ; Tue, 29 May 2012 11:02:24 -0700 (PDT) Sender: linux-fsdevel-owner@vger.kernel.org List-ID: =46rom: Kees Cook Subject: fs: symlink restrictions on sticky directories A longstanding class of security issues is the symlink-based time-of-check-time-of-use race, most commonly seen in world-writable directories like /tmp. The common method of exploitation of this flaw = is to cross privilege boundaries when following a given symlink (i.e. a r= oot process follows a symlink belonging to another user). For a likely incomplete list of hundreds of examples across the years, please see: http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=3D/tmp The solution is to permit symlinks to only be followed when outside a sticky world-writable directory, or when the uid of the symlink and follower match, or when the directory owner matches the symlink's owner= =2E Some pointers to the history of earlier discussion that I could find: 1996 Aug, Zygo Blaxell http://marc.info/?l=3Dbugtraq&m=3D87602167419830&w=3D2 1996 Oct, Andrew Tridgell http://lkml.indiana.edu/hypermail/linux/kernel/9610.2/0086.html 1997 Dec, Albert D Cahalan http://lkml.org/lkml/1997/12/16/4 2005 Feb, Lorenzo Hern=E1ndez Garc=EDa-Hierro http://lkml.indiana.edu/hypermail/linux/kernel/0502.0/1896.html 2010 May, Kees Cook https://lkml.org/lkml/2010/5/30/144 Past objections and rebuttals could be summarized as: - Violates POSIX. - POSIX didn't consider this situation and it's not useful to follow a broken specification at the cost of security. - Might break unknown applications that use this feature. - Applications that break because of the change are easy to spot and fix. Applications that are vulnerable to symlink ToCToU by not hav= ing the change aren't. Additionally, no applications have yet been fou= nd that rely on this behavior. - Applications should just use mkstemp() or O_CREATE|O_EXCL. - True, but applications are not perfect, and new software is writte= n all the time that makes these mistakes; blocking this flaw at the kernel is a single solution to the entire class of vulnerability. - This should live in the core VFS. - This should live in an LSM. (https://lkml.org/lkml/2010/5/31/135) - This should live in an LSM. - This should live in the core VFS. (https://lkml.org/lkml/2010/8/2/= 188) This patch is based on the patch in Openwall and grsecurity, along with suggestions from Al Viro. I have added a sysctl to enable the protecte= d behavior, documentation, and an audit notification. [akpm@linux-foundation.org: move sysctl_protected_sticky_symlinks decla= ration into .h] Signed-off-by: Kees Cook Reviewed-by: Ingo Molnar Cc: Matthew Wilcox Cc: Alexander Viro Cc: Rik van Riel Cc: Federica Teodori Cc: Lucian Adrian Grijincu Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Eric Paris Cc: Randy Dunlap Cc: Dan Rosenberg Signed-off-by: Andrew Morton --- Documentation/sysctl/fs.txt | 21 +++++++ fs/Kconfig | 34 ++++++++++++ fs/namei.c | 91 +++++++++++++++++++++++++++++++--- include/linux/fs.h | 1=20 kernel/sysctl.c | 11 ++++ 5 files changed, 152 insertions(+), 6 deletions(-) diff -puN Documentation/sysctl/fs.txt~fs-symlink-restrictions-on-sticky= -directories Documentation/sysctl/fs.txt --- a/Documentation/sysctl/fs.txt~fs-symlink-restrictions-on-sticky-dir= ectories +++ a/Documentation/sysctl/fs.txt @@ -32,6 +32,7 @@ Currently, these files are in /proc/sys/ - nr_open - overflowuid - overflowgid +- protected_symlinks - suid_dumpable - super-max - super-nr @@ -157,6 +158,26 @@ The default is 65534. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 +protected_symlinks: + +A long-standing class of security issues is the symlink-based +time-of-check-time-of-use race, most commonly seen in world-writable +directories like /tmp. The common method of exploitation of this flaw +is to cross privilege boundaries when following a given symlink (i.e. = a +root process follows a symlink belonging to another user). For a likel= y +incomplete list of hundreds of examples across the years, please see: +http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=3D/tmp + +When set to "0", symlink following behavior is unrestricted. + +When set to "1" symlinks are permitted to be followed only when outsid= e +a sticky world-writable directory, or when the uid of the symlink and +follower match, or when the directory owner matches the symlink's owne= r. + +This protection is based on the restrictions in Openwall and grsecurit= y. + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + suid_dumpable: =20 This value can be used to query and set the core dump mode for setuid diff -puN fs/Kconfig~fs-symlink-restrictions-on-sticky-directories fs/K= config --- a/fs/Kconfig~fs-symlink-restrictions-on-sticky-directories +++ a/fs/Kconfig @@ -276,4 +276,38 @@ endif # NETWORK_FILESYSTEMS source "fs/nls/Kconfig" source "fs/dlm/Kconfig" =20 +config PROTECTED_SYMLINKS + bool "Evaluate vulnerable symlink conditions" + default y + help + A long-standing class of security issues is the symlink-based + time-of-check-time-of-use race, most commonly seen in + world-writable directories like /tmp. The common method of + exploitation of this flaw is to cross privilege boundaries + when following a given symlink (i.e. a root process follows + a malicious symlink belonging to another user). + + Enabling this adds the logic to examine these dangerous symlink + conditions. Whether or not the dangerous symlink situations are + allowed is controlled by PROTECTED_SYMLINKS_ENABLED. + +config PROTECTED_SYMLINKS_ENABLED + depends on PROTECTED_SYMLINKS + bool "Disallow symlink following in sticky world-writable dirs" + default y + help + Solve ToCToU symlink race vulnerablities by permitting symlinks + to be followed only when outside a sticky world-writable directory, + or when the uid of the symlink and follower match, or when the + directory and symlink owners match. + + When PROC_SYSCTL is enabled, this setting can also be controlled + via /proc/sys/kernel/protected_symlinks. + +config PROTECTED_SYMLINKS_ENABLED_SYSCTL + depends on PROTECTED_SYMLINKS + int + default "1" if PROTECTED_SYMLINKS_ENABLED + default "0" + endmenu diff -puN fs/namei.c~fs-symlink-restrictions-on-sticky-directories fs/n= amei.c --- a/fs/namei.c~fs-symlink-restrictions-on-sticky-directories +++ a/fs/namei.c @@ -610,10 +610,84 @@ static inline void put_link(struct namei path_put(link); } =20 +#ifdef CONFIG_PROTECTED_SYMLINKS +int sysctl_protected_symlinks __read_mostly =3D + CONFIG_PROTECTED_SYMLINKS_ENABLED_SYSCTL; + +/** + * may_follow_link - Check symlink following for unsafe situations + * @dentry: The inode/dentry of the symlink + * @nameidata: The path data of the symlink + * + * In the case of the protected_symlinks sysctl being enabled, + * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is + * in a sticky world-writable directory. This is to protect privileged + * processes from failing races against path names that may change out + * from under them by way of other users creating malicious symlinks. + * It will permit symlinks to be followed only when outside a sticky + * world-writable directory, or when the uid of the symlink and follow= er + * match, or when the directory owner matches the symlink's owner. + * + * Returns 0 if following the symlink is allowed, -ve on error. + */ +static inline int +may_follow_link(struct dentry *dentry, struct nameidata *nameidata) +{ + int error =3D 0; + const struct inode *parent; + const struct inode *inode; + const struct cred *cred; + + if (!sysctl_protected_symlinks) + return 0; + + /* Allowed if owner and follower match. */ + cred =3D current_cred(); + inode =3D dentry->d_inode; + if (cred->fsuid =3D=3D inode->i_uid) + return 0; + + /* Check parent directory mode and owner. */ + spin_lock(&dentry->d_lock); + parent =3D dentry->d_parent->d_inode; + if ((parent->i_mode & (S_ISVTX|S_IWOTH)) =3D=3D (S_ISVTX|S_IWOTH) && + parent->i_uid !=3D inode->i_uid) { + error =3D -EACCES; + } + spin_unlock(&dentry->d_lock); + +#ifdef CONFIG_AUDIT + if (error) { + struct audit_buffer *ab; + + ab =3D audit_log_start(current->audit_context, + GFP_KERNEL, AUDIT_AVC); + audit_log_format(ab, "op=3Dfollow_link action=3Ddenied"); + audit_log_format(ab, " pid=3D%d comm=3D", current->pid); + audit_log_untrustedstring(ab, current->comm); + audit_log_d_path(ab, " path=3D", &nameidata->path); + audit_log_format(ab, " name=3D"); + audit_log_untrustedstring(ab, dentry->d_name.name); + audit_log_format(ab, " dev=3D"); + audit_log_untrustedstring(ab, inode->i_sb->s_id); + audit_log_format(ab, " ino=3D%lu", inode->i_ino); + audit_log_end(ab); + } +#endif + return error; +} +#else +static inline int +may_follow_link(struct dentry *dentry, struct nameidata *nameidata) +{ + return 0; +} +#endif + static __always_inline int -follow_link(struct path *link, struct nameidata *nd, void **p) +follow_link(struct path *link, struct nameidata *nd, void **p, bool se= nsitive) { - int error; + int error =3D 0; struct dentry *dentry =3D link->dentry; =20 BUG_ON(nd->flags & LOOKUP_RCU); @@ -632,7 +706,10 @@ follow_link(struct path *link, struct na touch_atime(link); nd_set_link(nd, NULL); =20 - error =3D security_inode_follow_link(link->dentry, nd); + if (sensitive) + error =3D may_follow_link(link->dentry, nd); + if (!error) + error =3D security_inode_follow_link(link->dentry, nd); if (error) { *p =3D ERR_PTR(error); /* no ->put_link(), please */ path_put(&nd->path); @@ -1351,7 +1428,7 @@ static inline int nested_symlink(struct=20 struct path link =3D *path; void *cookie; =20 - res =3D follow_link(&link, nd, &cookie); + res =3D follow_link(&link, nd, &cookie, false); if (!res) res =3D walk_component(nd, path, &nd->last, nd->last_type, LOOKUP_FOLLOW); @@ -1745,7 +1822,8 @@ static int path_lookupat(int dfd, const=20 void *cookie; struct path link =3D path; nd->flags |=3D LOOKUP_PARENT; - err =3D follow_link(&link, nd, &cookie); + + err =3D follow_link(&link, nd, &cookie, true); if (!err) err =3D lookup_last(nd, &path); put_link(nd, &link, cookie); @@ -2402,7 +2480,8 @@ static struct file *path_openat(int dfd, } nd->flags |=3D LOOKUP_PARENT; nd->flags &=3D ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); - error =3D follow_link(&link, nd, &cookie); + + error =3D follow_link(&link, nd, &cookie, true); if (unlikely(error)) filp =3D ERR_PTR(error); else diff -puN include/linux/fs.h~fs-symlink-restrictions-on-sticky-director= ies include/linux/fs.h --- a/include/linux/fs.h~fs-symlink-restrictions-on-sticky-directories +++ a/include/linux/fs.h @@ -428,6 +428,7 @@ extern unsigned long get_max_files(void) extern int sysctl_nr_open; extern struct inodes_stat_t inodes_stat; extern int leases_enable, lease_break_time; +extern int sysctl_protected_symlinks; =20 struct buffer_head; typedef int (get_block_t)(struct inode *inode, sector_t iblock, diff -puN kernel/sysctl.c~fs-symlink-restrictions-on-sticky-directories= kernel/sysctl.c --- a/kernel/sysctl.c~fs-symlink-restrictions-on-sticky-directories +++ a/kernel/sysctl.c @@ -1504,6 +1504,17 @@ static struct ctl_table fs_table[] =3D { }, #endif #endif +#ifdef CONFIG_PROTECTED_SYMLINKS + { + .procname =3D "protected_symlinks", + .data =3D &sysctl_protected_symlinks, + .maxlen =3D sizeof(int), + .mode =3D 0600, + .proc_handler =3D proc_dointvec_minmax, + .extra1 =3D &zero, + .extra2 =3D &one, + }, +#endif { .procname =3D "suid_dumpable", .data =3D &suid_dumpable, _ -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel= " in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html