Re: [PATCH v4] fs: allow protected cross-uid sticky symlinks

From: Dave Young
Date: Wed Jun 02 2010 - 20:51:36 EST


On Thu, Jun 3, 2010 at 6:23 AM, Kees Cook <kees.cook@xxxxxxxxxxxxx> wrote:
> 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 opening a file through a given
> symlink (i.e. a root process opens 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=/tmp
>
> The solution is to permit symlinks to only be opened when outside a sticky
> world-writable directory, or when the uid of the symlink and opener match,
> or when the directory owner matches the symlink's owner.
>
> Some pointers to the history of earlier discussion that I could find:
>
> Â1996 Aug, Zygo Blaxell
> Âhttp://marc.info/?l=bugtraq&m=87602167419830&w=2
> Â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Ãndez GarcÃa-Hierro
> Âhttp://lkml.indiana.edu/hypermail/linux/kernel/0502.0/1896.html
>
> 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 having
> Â Â the change aren't.
> Â- Applications should just use mkstemp() or O_CREATE|O_EXCL.
> Â - True, but applications are not perfect, and new software is written
> Â Â all the time that makes these mistakes; blocking this flaw at the
> Â Â kernel is a single solution to the entire class of vulnerability.
>
> This patch is based on the patch in Openwall and grsecurity, but with the
> scope changed to be only "opening" a symlink. ÂI have added a sysctl to
> enable the protected behavior, documentation, and a ratelimited warning.
>
> v2:
> Â- dropped redundant S_ISLNK check.
> Â- moved sysctl extern into security.h.

Not in v4?

> Â- asked to include CC to linux-fsdevel.
>
> v3:
> Â- move into VFS core.
> Â- add CONFIG entry for build-time default.
> Â- rename sysctl, invert logic.
> Â- use get_task_comm for task name.
> Â- lock dentry when checking parent.
>
> v4:
> Â- limit check to leaf symlink opening.
>
> Signed-off-by: Kees Cook <kees.cook@xxxxxxxxxxxxx>
> ---
> ÂDocumentation/sysctl/fs.txt | Â 15 ++++++++++
> Âfs/Kconfig         Â|  15 ++++++++++
> Âfs/namei.c         Â|  61 +++++++++++++++++++++++++++++++++++++++++++
> Âkernel/sysctl.c       |  10 +++++++
> Â4 files changed, 101 insertions(+), 0 deletions(-)
>
> diff --git a/Documentation/sysctl/fs.txt b/Documentation/sysctl/fs.txt
> index 6268250..9986bce 100644
> --- a/Documentation/sysctl/fs.txt
> +++ b/Documentation/sysctl/fs.txt
> @@ -32,6 +32,7 @@ Currently, these files are in /proc/sys/fs:
> Â- nr_open
> Â- overflowuid
> Â- overflowgid
> +- protected-sticky-symlinks
> Â- suid_dumpable
> Â- super-max
> Â- super-nr
> @@ -158,6 +159,20 @@ The default is 65534.
>
> Â==============================================================
>
> +protected-sticky-symlinks:
> +
> +Opening symlinks in sticky world-writable directories (like /tmp) can be
> +dangerous due to time-of-check-time-of-use races that frequently result
> +in security vulnerabilities.
> +
> +The default value is "0", leaving the behavior of symlink opening
> +unchanged from POSIX. ÂA value of "1" will enable the protection, causing
> +symlinks to be openable only if outside a sticky world-writable directory,
> +or if the symlink and the opener's uid match, or if the symlink and its
> +directory are owned by the same uid.
> +
> +==============================================================
> +
> Âsuid_dumpable:
>
> ÂThis value can be used to query and set the core dump mode for setuid
> diff --git a/fs/Kconfig b/fs/Kconfig
> index 5f85b59..48df7cd 100644
> --- a/fs/Kconfig
> +++ b/fs/Kconfig
> @@ -256,3 +256,18 @@ source "fs/nls/Kconfig"
> Âsource "fs/dlm/Kconfig"
>
> Âendmenu
> +
> +config PROTECTED_STICKY_SYMLINKS
> + Â Â Â bool "Protect symlink opening in sticky world-writable directories"
> + Â Â Â 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 opening a given symlink (i.e. a root process opens a
> + Â Â Â Â Âmalicious symlink belonging to another user).
> +
> + Â Â Â Â Enabling this solves the problem by permitting symlinks to only
> + Â Â Â Â be opened when outside a sticky world-writable directory, or
> + Â Â Â Â Âwhen the uid of the symlink and opener match, or when the
> + Â Â Â Â Âdirectory and symlink owners match.
> diff --git a/fs/namei.c b/fs/namei.c
> index 868d0cb..ee9d493 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -32,6 +32,7 @@
> Â#include <linux/fcntl.h>
> Â#include <linux/device_cgroup.h>
> Â#include <linux/fs_struct.h>
> +#include <linux/ratelimit.h>
> Â#include <asm/uaccess.h>
>
> Â#include "internal.h"
> @@ -530,6 +531,60 @@ static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
> Â Â Â Ând->path.dentry = path->dentry;
> Â}
>
> +int protected_sticky_symlinks = CONFIG_PROTECTED_STICKY_SYMLINKS;
> +
> +/**
> + * may_open_sticky_symlink - Check symlink opening for unsafe situations
> + * @dentry: The inode/dentry of the symlink
> + * @nameidata: The path data of the symlink
> + *
> + * In the case of the protected_sticky_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 only be opened when outside a sticky
> + * world-writable directory, or when the uid of the symlink and opener
> + * match, or when the directory owner matches the symlink's owner.
> + *
> + * Returns 0 if opening the symlink is allowed, -ve on error.
> + */
> +static __always_inline int
> +may_open_sticky_symlink(struct dentry *dentry, struct nameidata *nameidata)
> +{
> + Â Â Â int error = 0;
> + Â Â Â const struct inode *parent;
> + Â Â Â const struct inode *inode;
> + Â Â Â const struct cred *cred;
> +
> + Â Â Â if (!protected_sticky_symlinks)
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â /* owner and opener match? */
> + Â Â Â cred = current_cred();
> + Â Â Â inode = dentry->d_inode;
> + Â Â Â if (cred->fsuid == inode->i_uid)
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â /* check parent directory mode and owner */
> + Â Â Â spin_lock(&dentry->d_lock);
> + Â Â Â parent = dentry->d_parent->d_inode;
> + Â Â Â if ((parent->i_mode & (S_ISVTX|S_IWOTH)) == (S_ISVTX|S_IWOTH) &&
> + Â Â Â Â Â parent->i_uid != inode->i_uid) {
> + Â Â Â Â Â Â Â error = -EACCES;
> + Â Â Â }
> + Â Â Â spin_unlock(&dentry->d_lock);
> +
> + Â Â Â if (error) {
> + Â Â Â Â Â Â Â char name[sizeof(current->comm)];
> + Â Â Â Â Â Â Â printk_ratelimited(KERN_NOTICE "non-matching-uid symlink "
> + Â Â Â Â Â Â Â Â Â Â Â "opening attempted in sticky world-writable "
> + Â Â Â Â Â Â Â Â Â Â Â "directory by %s (fsuid %d)\n",
> + Â Â Â Â Â Â Â Â Â Â Â get_task_comm(name, current), cred->fsuid);
> + Â Â Â }
> + Â Â Â return error;
> +}
> +
> Âstatic __always_inline int
> Â__do_follow_link(struct path *path, struct nameidata *nd, void **p)
> Â{
> @@ -1844,6 +1899,12 @@ reval:
> Â Â Â Â Â Â Â Â Â Â Â Âgoto exit_dput;
> Â Â Â Â Â Â Â Âif (count++ == 32)
> Â Â Â Â Â Â Â Â Â Â Â Âgoto exit_dput;
> +
> + Â Â Â Â Â Â Â /* check if this symlink is in a sticky world-write dir */
> + Â Â Â Â Â Â Â error = may_open_sticky_symlink(path.dentry, &nd);
> + Â Â Â Â Â Â Â if (error)
> + Â Â Â Â Â Â Â Â Â Â Â goto exit_dput;
> +
> Â Â Â Â Â Â Â Â/*
> Â Â Â Â Â Â Â Â * This is subtle. Instead of calling do_follow_link() we do
> Â Â Â Â Â Â Â Â * the thing by hands. The reason is that this way we have zero
> diff --git a/kernel/sysctl.c b/kernel/sysctl.c
> index 997080f..56affd6 100644
> --- a/kernel/sysctl.c
> +++ b/kernel/sysctl.c
> @@ -87,6 +87,7 @@ extern int sysctl_oom_kill_allocating_task;
> Âextern int sysctl_oom_dump_tasks;
> Âextern int max_threads;
> Âextern int core_uses_pid;
> +extern int protected_sticky_symlinks;
> Âextern int suid_dumpable;
> Âextern char core_pattern[];
> Âextern unsigned int core_pipe_limit;
> @@ -1455,6 +1456,15 @@ static struct ctl_table fs_table[] = {
> Â#endif
> Â#endif
> Â Â Â Â{
> +        .procname    = "protected-sticky-symlinks",
> +        .data      = &protected_sticky_symlinks,
> +        .maxlen     = sizeof(int),
> +        .mode      = 0644,
> +        .proc_handler  = proc_dointvec_minmax,
> + Â Â Â Â Â Â Â .extra1 Â Â Â Â = &zero,
> + Â Â Â Â Â Â Â .extra2 Â Â Â Â = &one,
> + Â Â Â },
> + Â Â Â {
>        Â.procname    = "suid_dumpable",
>        Â.data      = &suid_dumpable,
>        Â.maxlen     = sizeof(int),
> --
> 1.7.0.4
>
>
> --
> Kees Cook
> Ubuntu Security Team
>



--
Regards
dave
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/