fs: symlink restrictions on sticky directories
Subsystems (3)
PATCH 1/1 fs: symlink restrictions on sticky directories
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 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 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.
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
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 having the change aren't. Additionally, no applications have yet been found 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 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 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 protected behavior, documentation, and a ratelimited warning.
---
v2011.2:
- Updated documentation, thanks to Randy Dunlap.
- Switched Kconfig default to "y", added __read_mostly to sysctl, thanks to Ingo Molnar.
- Switched to audit logging to gain safe path and name reporting when hitting the restriction.
v2011.1:
- back from hiatus
--- Documentation/sysctl/fs.txt | 21 +++++++++++ fs/Kconfig | 16 ++++++++ fs/namei.c | 85 ++++++++++++++++++++++++++++++++++++++++--- kernel/sysctl.c | 10 +++++ 4 files changed, 126 insertions(+), 6 deletions(-)
Documentation/sysctl/fs.txt +21 -0
@@ -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
@@ -157,6 +158,26 @@ The default is 65534. ============================================================== +protected_sticky_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 likely +incomplete list of hundreds of examples across the years, please see: +http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=/tmp + +When set to "0", symlink following behavior is unrestricted. + +When set to "1" symlinks are permitted 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 owner matches the symlink's owner. + +This protection is based on the restrictions in Openwall and grsecurity. + +============================================================== + suid_dumpable: This value can be used to query and set the core dump mode for setuid
fs/Kconfig +16 -0
@@ -278,3 +278,19 @@ source "fs/nls/Kconfig" source "fs/dlm/Kconfig" endmenu + +config PROTECTED_STICKY_SYMLINKS + bool "Protect symlink following in sticky world-writable directories" + 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 solves the problem 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.
fs/namei.c +79 -6
117 lines not on this page · raw message
kernel/sysctl.c +10 -0
@@ -92,6 +92,7 @@ extern int sysctl_overcommit_memory;extern int sysctl_overcommit_ratio;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;
@@ -1495,6 +1496,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),
Signature · 1 line
1.7.0.4
PATCH 1/1 fs: symlink restrictions on sticky directories
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 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 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.
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
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 having the change aren't. Additionally, no applications have yet been found 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 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 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 protected behavior, documentation, and an audit notification.
---
v2011.3:
- Oops, add pid/comm back to notification.
v2011.2:
- Updated documentation, thanks to Randy Dunlap.
- Switched Kconfig default to "y", added __read_mostly to sysctl, thanks to Ingo Molnar.
- Switched to audit logging to gain safe path and name reporting when hitting the restriction.
v2011.1:
- back from hiatus
--- Documentation/sysctl/fs.txt | 21 ++++++++++ fs/Kconfig | 16 ++++++++ fs/namei.c | 87 ++++++++++++++++++++++++++++++++++++++++--- kernel/sysctl.c | 10 +++++ 4 files changed, 128 insertions(+), 6 deletions(-)
Documentation/sysctl/fs.txt +21 -0
@@ -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
@@ -157,6 +158,26 @@ The default is 65534. ============================================================== +protected_sticky_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 likely +incomplete list of hundreds of examples across the years, please see: +http://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=/tmp + +When set to "0", symlink following behavior is unrestricted. + +When set to "1" symlinks are permitted 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 owner matches the symlink's owner. + +This protection is based on the restrictions in Openwall and grsecurity. + +============================================================== + suid_dumpable: This value can be used to query and set the core dump mode for setuid
fs/Kconfig +16 -0
@@ -278,3 +278,19 @@ source "fs/nls/Kconfig" source "fs/dlm/Kconfig" endmenu + +config PROTECTED_STICKY_SYMLINKS + bool "Protect symlink following in sticky world-writable directories" + 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 solves the problem 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.
fs/namei.c +81 -6
119 lines not on this page · raw message
kernel/sysctl.c +10 -0
@@ -92,6 +92,7 @@ extern int sysctl_overcommit_memory;extern int sysctl_overcommit_ratio;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;
@@ -1495,6 +1496,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),
Signature · 1 line
1.7.0.4
Quoted 15 lines · Kees Cook
fs/namei.c +15
-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); + +#ifdef CONFIG_AUDIT +if (error) { +struct audit_buffer *ab; + + ab =audit_log_start (current->audit_context, +GFP_ATOMIC ,AUDIT_AVC );
fs/namei.c static inline void put_link(struct nameidata *nd, struct path *link, void *cooki · line 691
Quoted 10 lines
fs/namei.c +10
-0+audit_log_format (ab,"op=follow_link action=denied" ); +audit_log_d_path (ab,"path=" , &nameidata->path); +audit_log_format (ab," name=" ); +audit_log_untrustedstring (ab, dentry->d_name.name); +audit_log_format (ab," dev=%s ino=%lu" , + inode->i_sb->s_id, + inode->i_ino); + audit_log_end(ab); + } +#endif
Hm, is GFP_ATOMIC really necessary here? Why not GFP_KERNEL? This is in VFS process context, not in some atomic context that has to be careful about allocations, right?
Thanks,
Ingo
Quoted 8 lines · Ingo Molnar
Quoted 2 lines · Kees Cook
+ ab = audit_log_start(current->audit_context,
+ GFP_ATOMIC,AUDIT_AVC);Hm, is
GFP_ATOMICreally necessary here? Why notGFP_KERNEL? This is in VFS process context, not in some atomic context that has to be careful about allocations, right?
Hm, that's true. I had started from the lsm_audit interface, but had to expand it, and in the process copied its use of audit_log_start(). But yeah, I don't see a reason it can't be GFP_KERNEL. I'll adjust it.
Thanks!
-Kees
Signature · 2 lines
Kees Cook ChromeOS Security