fs: hardlink creation restrictions
Subsystems (3)
PATCH v2 1/1 fs: hardlink creation restrictions
On systems that have user-writable directories on the same partition as system files, a long-standing class of security issues is the hardlink-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 hardlink (i.e. a root process follows a hardlink created by another user). Additionally, an issue exists where users can "pin" a potentially vulnerable setuid/setgid file so that an administrator will not actually upgrade a system fully.
The solution is to permit hardlinks to only be created when the user is already the existing file's owner, or if they already have read/write access to the existing file.
Many Linux users are surprised when they learn they can link to files they have no access to, so this change appears to follow the doctrine of "least surprise". Additionally, this change does not violate POSIX, which states "the implementation may require that the calling process has permission to access the existing file"[1].
This change is known to break some implementations of the "at" daemon, though the version used by Fedora and Ubuntu has been fixed[2] for a while. Otherwise, the change has been undisruptive while in use in Ubuntu for the last 1.5 years.
This patch is based on the patch in Openwall and grsecurity. I have added a sysctl to enable the protected behavior, documentation, and an audit notification.
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html
[2] http://anonscm.debian.org/gitweb/?p=collab-maint/at.git;a=commitdiff;h=f4114656c3a6c6f6070e315ffdf940a49eda3279
---
v2:
- move may_linkat() before write intent, thanks to Marcin Slusarz.
- reduce length of CONFIG and sysctl names, suggested by Ingo Molnar.
- drop needless CONFIG_AUDIT check, thanks to Ingo Molnar.
- default to on, since it is safer and can be disabled at runtime.
- extract audit logs to single common function, suggested by Ingo Molnar.
--- Documentation/sysctl/fs.txt | 25 +++++++++- fs/Kconfig | 56 ++++++++++++++++------ fs/namei.c | 113 +++++++++++++++++++++++++++++++----------- include/linux/fs.h | 3 +- kernel/sysctl.c | 15 +++++- 5 files changed, 161 insertions(+), 51 deletions(-)
Documentation/sysctl/fs.txt +23 -2
@@ -32,7 +32,8 @@ Currently, these files are in /proc/sys/fs: - nr_open - overflowuid - overflowgid -- protected_sticky_symlinks +- protected_hardlinks +- protected_symlinks - suid_dumpable - super-max - super-nr
@@ -158,7 +159,27 @@ The default is 65534. ============================================================== -protected_sticky_symlinks: +protected_hardlinks: + +A long-standing class of security issues is the hardlink-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 hardlink (i.e. a +root process follows a hardlink created by another user). Additionally, +on systems without separated partitions, this stops unauthorized users +from "pinning" vulnerable setuid/setgid files against being upgraded by +the administrator, or linking to special files. + +When set to "0", hardlink creation behavior is unrestricted. + +When set to "1" hardlinks cannot be created by users if they do not +already own the source file, or do not have read/write access to it. + +This protection is based on the restrictions in Openwall and grsecurity. + +============================================================== + +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
fs/Kconfig +41 -15
79 lines not on this page · raw message
fs/namei.c +83 -30
160 lines not on this page · raw message
include/linux/fs.h +2 -1
@@ -422,7 +422,8 @@ 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_sticky_symlinks; +extern int sysctl_protected_symlinks; +extern int sysctl_protected_hardlinks;struct buffer_head ; typedef int (get_block_t)(struct inode *inode, sector_t iblock,
kernel/sysctl.c +12 -3
@@ -1503,10 +1503,19 @@ static struct ctl_table fs_table[] = { },#endif #endif -#ifdef CONFIG_PROTECTED_STICKY_SYMLINKS +#ifdef CONFIG_PROTECTED_LINKS { - .procname = "protected_sticky_symlinks", - .data = &sysctl_protected_sticky_symlinks, + .procname = "protected_symlinks", + .data = &sysctl_protected_symlinks, + .maxlen = sizeof(int), + .mode = 0600, + .proc_handler = proc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &one, + }, + { + .procname = "protected_hardlinks", + .data = &sysctl_protected_hardlinks, .maxlen = sizeof(int), .mode = 0600, .proc_handler = proc_dointvec_minmax,
Signature · 1 line
1.7.0.4
Quoted 25 lines · On Tue, 21 Feb 2012 13:58:00 -0800 Kees Cook <[email protected]> wrote:
On systems that have user-writable directories on the same partition as system files, a long-standing class of security issues is the hardlink-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 hardlink (i.e. a root process follows a hardlink created by another user). Additionally, an issue exists where users can "pin" a potentially vulnerable setuid/setgid file so that an administrator will not actually upgrade a system fully.
The solution is to permit hardlinks to only be created when the user is already the existing file's owner, or if they already have read/write access to the existing file.
Many Linux users are surprised when they learn they can link to files they have no access to, so this change appears to follow the doctrine of "least surprise". Additionally, this change does not violate POSIX, which states "the implementation may require that the calling process has permission to access the existing file"[1].
This change is known to break some implementations of the "at" daemon, though the version used by Fedora and Ubuntu has been fixed[2] for a while. Otherwise, the change has been undisruptive while in use in Ubuntu for the last 1.5 years.
Quoted 7 lines
This patch is based on the patch in Openwall and grsecurity. I have added a sysctl to enable the protected behavior, documentation, and an audit notification.
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html
[2] http://anonscm.debian.org/gitweb/?p=collab-maint/at.git;a=commitdiff;h=f4114656c3a6c6f6070e315ffdf940a49eda3279
Looks OKish to me.
Documentation/sysctl/fs.txt Currently, these files are in /proc/sys/fs: · line 36
Quoted 9 lines
Documentation/sysctl/fs.txt +2
-1@@ -32,7 +32,8 @@ Currently, these files are in /proc/sys/fs: - nr_open - overflowuid - overflowgid -- protected_sticky_symlinks +- protected_hardlinks +- protected_symlinks
It's silly to add protected_sticky_symlinks and to then rename it in the very next patch. I have reworked my copy of the earlier fs-symlink-restrictions-on-sticky-directories.patch so that it adds "protected_symlinks". I then reworked this patch (fs-hardlink-creation-restrictions.patch) to add protected_hardlinks. Nice and simple.
Quoted 8 lines
...
Documentation/sysctl/fs.txt +5
-0+static inline void +audit_log_link_denied(const char *operation, struct path *link) +{ + struct audit_buffer *ab; +
Documentation/sysctl/fs.txt
Quoted 10 lines
Documentation/sysctl/fs.txt +10
-0+ ab = audit_log_start(current->audit_context, GFP_KERNEL, AUDIT_AVC); + audit_log_format(ab, "op=%s action=denied", operation); + audit_log_format(ab, " pid=%d comm=", current->pid); + audit_log_untrustedstring(ab, current->comm); + audit_log_d_path(ab, " path=", link); + audit_log_format(ab, " dev="); + audit_log_untrustedstring(ab, link->dentry->d_inode->i_sb->s_id); + audit_log_format(ab, " ino=%lu", link->dentry->d_inode->i_ino); + audit_log_end(ab); +}
This is far too large to inline. It has two callsites and gcc is probably smart anough to uninline it for us. I queued a patch to remove the `inline'.
Quoted 18 lines
/**
*may_follow_link- Check symlink following for unsafe situations
- * @dentry: The inode/dentry of the symlink
- * @nameidata: The path data of the symlink
+ * @link: The path of the symlink
*
- * In the case of theprotected_sticky_symlinkssysctl being enabled,
+ * In the case of thesysctl_protected_symlinkssysctl being enabled,
*CAP_DAC_OVERRIDEneeds 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 outDocumentation/sysctl/fs.txt +1
-2@@ -643,19 +660,20 @@ int sysctl_protected_sticky_symlinks __read_mostly = * * Returns 0 if following the symlink is allowed, -ve on error. */ -static inline int -may_follow_link(struct dentry *dentry, struct nameidata *nameidata) +static inline int may_follow_link(struct path *link)
Documentation/sysctl/fs.txt int sysctl_protected_sticky_symlinks __read_mostly = · line 672
Quoted 10 lines
Documentation/sysctl/fs.txt +2
-1{ int error = 0; const struct inode *parent; const struct inode *inode; const struct cred *cred; + struct dentry *dentry; - if (!sysctl_protected_sticky_symlinks) + if (!sysctl_protected_symlinks) return 0;
This is also too large to inline. It doesn't matter a lot - it's not a hot path. I removed this `inline' as well. gcc will probably inline it anyway.
If we were really concerned about speed here, we might decide to inline the test of sysctl_protected_symlinks and to uninline the remainder of the function.
Quoted 19 lines
Documentation/sysctl/fs.txt +11
-0/* Allowed if owner and follower match. */ cred = current_cred(); + dentry = link->dentry; inode = dentry->d_inode; if (cred->fsuid == inode->i_uid) return 0; +static inline int may_linkat(struct path *link) +{ + int error = 0; + const struct cred *cred; + struct inode *inode; + int mode; + + if (!sysctl_protected_hardlinks) + return 0; +
Documentation/sysctl/fs.txt int sysctl_protected_sticky_symlinks __read_mostly =
Quoted 10 lines
Documentation/sysctl/fs.txt +10
-0+ cred = current_cred(); + inode = link->dentry->d_inode; + mode = inode->i_mode; + + if (cred->fsuid != inode->i_uid && + (!S_ISREG(mode) || (mode & S_ISUID) || + ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) || + (inode_permission(inode, MAY_READ | MAY_WRITE))) && + !capable(CAP_FOWNER)) + error = -EPERM;
omigod. Geeze man, whose side are you on?
I suggest something like this:
fs/namei.c renamed from fs/namei.c~fs-hardlink-creation-restrictions-fix-fix +19 -5
@@ -692,6 +692,23 @@ static inline int may_follow_link(structreturn error; } +static bool foobar (struct inode *inode,umode_t mode) +{ +/* nice comment */ +if (!S_ISREG (mode)) +return true; +/* nice comment */ +if (mode &S_ISUID ) +return true; +/* nice comment */ +if ((mode & (S_ISGID |S_IXGRP )) == (S_ISGID |S_IXGRP )) +return true; +/* nice comment */ +if (inode_permission (inode,MAY_READ |MAY_WRITE )) +return true; +return false; +} +/** * may_linkat - Check permissions for creating a hardlink * @link: the source to hardlink from
@@ -722,11 +739,8 @@ static int may_linkat(struct path *link) inode = link->dentry->d_inode; mode = inode->i_mode; -if (cred->fsuid != inode->i_uid && - (!S_ISREG (mode) || (mode &S_ISUID ) || - ((mode & (S_ISGID |S_IXGRP )) == (S_ISGID |S_IXGRP )) || - (inode_permission (inode,MAY_READ |MAY_WRITE ))) && - !capable (CAP_FOWNER )) +if (cred->fsuid != inode->i_uid &&foobar (inode, mode) && + !capable (CAP_FOWNER )) error = -EPERM ;if (error)
Please take a look at that, pick a replacement for foobar, fill in the nice comments which explain our reasoning, retest it and send it back at me?
Also please take a look at local variable "mode" and decide if there was a good reason for it being `int' instead of mode_t?
Quoted 32 lines · Andrew Morton
Quoted 29 lines · On Tue, 21 Feb 2012 13:58:00 -0800 Kees Cook <[email protected]> wrote:
On systems that have user-writable directories on the same partition as system files, a long-standing class of security issues is the hardlink-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 hardlink (i.e. a root process follows a hardlink created by another user). Additionally, an issue exists where users can "pin" a potentially vulnerable setuid/setgid file so that an administrator will not actually upgrade a system fully.
The solution is to permit hardlinks to only be created when the user is already the existing file's owner, or if they already have read/write access to the existing file.
Many Linux users are surprised when they learn they can link to files they have no access to, so this change appears to follow the doctrine of "least surprise". Additionally, this change does not violate POSIX, which states "the implementation may require that the calling process has permission to access the existing file"[1].
This change is known to break some implementations of the "at" daemon, though the version used by Fedora and Ubuntu has been fixed[2] for a while. Otherwise, the change has been undisruptive while in use in Ubuntu for the last 1.5 years.
This patch is based on the patch in Openwall and grsecurity. I have added a sysctl to enable the protected behavior, documentation, and an audit notification.
Quoted 5 lines
Quoted 3 lines
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html
[2] http://anonscm.debian.org/gitweb/?p=collab-maint/at.git;a=commitdiff;h=f4114656c3a6c6f6070e315ffdf940a49eda3279Looks OKish to me.
Thanks!
Quoted 8 lines
Quoted 8 lines
Documentation/sysctl/fs.txt +0
-0@@ -32,7 +32,8 @@ Currently, these files are in /proc/sys/fs:-
nr_open
- overflowuid
- overflowgid
--protected_sticky_symlinks
+-protected_hardlinks
Quoted 8 lines
Quoted 1 line
+-
protected_symlinksIt's silly to add
protected_sticky_symlinksand to then rename it in the very next patch. I have reworked my copy of the earlier fs-symlink-restrictions-on-sticky-directories.patch so that it adds "protected_symlinks". I then reworked this patch (fs-hardlink-creation-restrictions.patch) to addprotected_hardlinks. Nice and simple.
Okay, sounds good. Thanks for the clean-ups!
Quoted 14 lines
Quoted 14 lines
...
Documentation/sysctl/fs.txt +11
-0+static inline void +audit_log_link_denied(const char *operation, struct path *link) +{ + struct audit_buffer *ab; + + ab = audit_log_start(current->audit_context, GFP_KERNEL, AUDIT_AVC); + audit_log_format(ab, "op=%s action=denied", operation); + audit_log_format(ab, " pid=%d comm=", current->pid); + audit_log_untrustedstring(ab, current->comm); + audit_log_d_path(ab, " path=", link); + audit_log_format(ab, " dev=");
Quoted 8 lines
Quoted 4 lines
+
audit_log_untrustedstring(ab, link->dentry->d_inode->i_sb->s_id);
+audit_log_format(ab, " ino=%lu", link->dentry->d_inode->i_ino);
+audit_log_end(ab);
+}This is far too large to inline. It has two callsites and gcc is probably smart anough to uninline it for us. I queued a patch to remove the `inline'.
Okay, seems fine to me.
Quoted 61 lines
Quoted 18 lines
/**
*may_follow_link- Check symlink following for unsafe situations
- * @dentry: The inode/dentry of the symlink
- * @nameidata: The path data of the symlink
+ * @link: The path of the symlink
*
- * In the case of theprotected_sticky_symlinkssysctl being enabled,
+ * In the case of thesysctl_protected_symlinkssysctl being enabled,
*CAP_DAC_OVERRIDEneeds 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 outDocumentation/sysctl/fs.txt +1
-2@@ -643,19 +660,20 @@ int sysctl_protected_sticky_symlinks __read_mostly = * * Returns 0 if following the symlink is allowed, -ve on error. */ -static inline int -may_follow_link(struct dentry *dentry, struct nameidata *nameidata) +static inline int may_follow_link(struct path *link)Documentation/sysctl/fs.txt int sysctl_protected_sticky_symlinks __read_mostly = · line 672
Quoted 10 lines
Documentation/sysctl/fs.txt +2
-1{ int error = 0; const struct inode *parent; const struct inode *inode; const struct cred *cred; + struct dentry *dentry; - if (!sysctl_protected_sticky_symlinks) + if (!sysctl_protected_symlinks) return 0;This is also too large to inline. It doesn't matter a lot - it's not a hot path. I removed this `inline' as well. gcc will probably inline it anyway.
If we were really concerned about speed here, we might decide to inline the test of
sysctl_protected_symlinksand to uninline the remainder of the function.Quoted 23 lines
Documentation/sysctl/fs.txt +15
-0/* Allowed if owner and follower match. */ cred = current_cred(); + dentry = link->dentry; inode = dentry->d_inode; if (cred->fsuid == inode->i_uid) return 0; +static inline int may_linkat(struct path *link) +{ + int error = 0; + const struct cred *cred; + struct inode *inode; + int mode; + + if (!sysctl_protected_hardlinks) + return 0; + + cred = current_cred(); + inode = link->dentry->d_inode; + mode = inode->i_mode; +
Quoted 8 lines
Quoted 6 lines
+ if (
cred->fsuid!=inode->i_uid&&
+ (!S_ISREG(mode) || (mode &S_ISUID) ||
+ ((mode & (S_ISGID|S_IXGRP)) == (S_ISGID|S_IXGRP)) ||
+ (inode_permission(inode,MAY_READ|MAY_WRITE))) &&
+ !capable(CAP_FOWNER))
+ error =-EPERM;omigod. Geeze man, whose side are you on?
Heh. Yeah, this was not my favorite if statement.
Quoted 43 lines
I suggest something like this:
fs/namei.c renamed from fs/namei.c~fs-hardlink-creation-restrictions-fix-fix +19
-5@@ -692,6 +692,23 @@ static inline int may_follow_link(struct return error; } +static bool foobar (struct inode *inode,umode_t mode) +{ + /* nice comment */ + if (!S_ISREG(mode)) + return true; + /* nice comment */ + if (mode & S_ISUID) + return true; + /* nice comment */ + if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) + return true; + /* nice comment */ + if (inode_permission(inode, MAY_READ | MAY_WRITE)) + return true; + return false; +} + /*** may_linkat - Check permissions for creating a hardlink * @link: the source to hardlink from @@ -722,11 +739,8 @@ static int may_linkat(struct path *link) inode = link->dentry->d_inode; mode = inode->i_mode; - if (cred->fsuid != inode->i_uid && - (!S_ISREG(mode) || (mode & S_ISUID) || - ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) || - (inode_permission(inode, MAY_READ | MAY_WRITE))) && - !capable(CAP_FOWNER)) + if (cred->fsuid != inode->i_uid && foobar(inode, mode) && + !capable(CAP_FOWNER)) error = -EPERM; if (error)
Quoted 3 lines
Please take a look at that, pick a replacement for foobar, fill in the nice comments which explain our reasoning, retest it and send it back at me?
Sounds good -- I'll get it written and tested shortly.
Quoted 2 lines
Also please take a look at local variable "mode" and decide if there was a good reason for it being `int' instead of
mode_t?
Ah, sorry. That was an oversight on my part -- I'd ported these before mode_t existed. I'll fix that too.
Thanks!
-Kees
Signature · 2 lines
Kees Cook ChromeOS Security