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.
--- Documentation/sysctl/fs.txt | 21 ++++++++++++ fs/Kconfig | 15 ++++++++ fs/namei.c | 77 +++++++++++++++++++++++++++++++++++++++--- kernel/sysctl.c | 10 +++++ 4 files changed, 117 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 +15 -0
@@ -278,3 +278,18 @@ source "fs/nls/Kconfig" source "fs/dlm/Kconfig" endmenu + +config PROTECTED_STICKY_SYMLINKS + bool "Protect symlink following 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 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 only + be followed 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 +71 -6
115 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 5 lines · Kees Cook
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.
Correct.
Quoted 5 lines
- 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.
Are there *known* applications that break?
Quoted 4 lines
- 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.
Right.
Quoted 4 lines
- 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)
Ugh - and people continue to get exploited from a preventable, fixable and already fixed VFS design flaw.
Quoted 9 lines
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.
fs/Kconfig +4
-0+config PROTECTED_STICKY_SYMLINKS + bool "Protect symlink following in sticky world-writable directories" + help + A long-standing class of security issues is the symlink-based
fs/Kconfig source "fs/nls/Kconfig" · line 295
Quoted 10 lines
fs/Kconfig +10
-0+ 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 only + be followed 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.
Unless there are known apps that regress, shouldnt this be default y? Even if we were forced to not actually release such a final kernel, doing it in -rc1 would flush weird apps out of the woodwork.
Quoted 1 line
+int
protected_sticky_symlinks=
__read_mostly, most emphatically.
Quoted 15 lines
fs/namei.c +15
-0+static inline int +may_follow_link (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 ; + +/* Allowed if owner and follower match. */ + cred =current_cred (); + inode = dentry->d_inode; +if (cred->fsuid == inode->i_uid)
fs/namei.c static inline void put_link(struct nameidata *nd, struct path *link, void *cooki · line 675
Quoted 10 lines
fs/namei.c +10
-0+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);
Taking the global /tmp and /lib, /usr/lib dentry spinlocks here every time we follow a symlink is a bit sad: there are a lot of high-profile symlinks in a Linux installation in those places, followed by non-owners.
Nor is it really a realistic race that happens often: neither /tmp nor the library directories are being renamed or their permissions changed all that often for this parent lock taking to matter in practice.
Couldnt we somehow export this information outside the dentry lock, allowing safe lockless access to it?
fs/namei.c static inline void put_link(struct nameidata *nd, struct path *link, void *cooki · line 682
Quoted 7 lines
fs/namei.c +7
-0+ +if (error) { +char name[sizeof (current->comm)]; +printk_ratelimited (KERN_NOTICE "non-matching-uid symlink " +"following attempted in sticky world-writable " +"directory by %s (fsuid %d)\n" , +get_task_comm (name, current), cred->fsuid);
I don't think the get_task_comm() extra layer is really necessary here - this is called in the *current* task's context
- and it's not like that tasks are changing their own names all that often while they are busy executing VFS code, right? The race with some other task writing to /proc/PID/comm is uninteresting.
The more important piece of forensic information to log would be the file name that got attempted and perhaps the directory name as well. If there's an unknown hole in an unknown privileged app then this warning alone does not give the admin any clues where to look - the name of the expoiting task is probably obfuscated anyway, it's the identity of the attack vector that matters - and that name can generally not be controlled and obfuscated by the attacker.
If those issues are resolved:
Thanks,
Ingo
Quoted 11 lines · Ingo Molnar
Quoted 7 lines · Kees Cook
Past objections and rebuttals could be summarized as:
...
- 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.Are there *known* applications that break?
Not that I've seen and none have been mentioned in any of the previous discussions I've been able to find. (From above: "Additionally, no applications have yet been found that rely on this behavior.")
Quoted 7 lines
Quoted 4 lines
- 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)Ugh - and people continue to get exploited from a preventable, fixable and already fixed VFS design flaw.
Right.
Quoted 11 lines
Quoted 11 lines
fs/Kconfig +11
-0+config PROTECTED_STICKY_SYMLINKS + bool "Protect symlink following 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 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 only
Quoted 8 lines
Quoted 3 lines
+ be followed 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.Unless there are known apps that regress, shouldnt this be default y? Even if we were forced to not actually release such a final kernel, doing it in -rc1 would flush weird apps out of the woodwork.
I would totally agree. I was trying to be conservative here, but would much rather this default to "y".
Quoted 3 lines
Quoted 1 line
+int
protected_sticky_symlinks=
__read_mostly, most emphatically.
Ah yes. I will fix this.
Quoted 31 lines
Quoted 15 lines
fs/namei.c +15
-0+static inline int +may_follow_link (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; + + /* Allowed if owner and follower match. */ + cred = current_cred(); + inode = dentry->d_inode; + if (cred->fsuid == inode->i_uid)fs/namei.c static inline void put_link(struct nameidata *nd, struct path *link, void *cooki · line 675
Quoted 10 lines
fs/namei.c +10
-0+ 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);Taking the global /tmp and /lib,
/usr/libdentry spinlocks here every time we follow a symlink is a bit sad: there are a lot of high-profile symlinks in a Linux installation in those places, followed by non-owners.
Quoted 7 lines
Nor is it really a realistic race that happens often: neither /tmp nor the library directories are being renamed or their permissions changed all that often for this parent lock taking to matter in practice.
Couldnt we somehow export this information outside the dentry lock, allowing safe lockless access to it?
I'm open to whatever is needed, though I think this change would be outside the scope of this patch's intent.
Quoted 7 lines
Quoted 6 lines
+ if (error) {
+ char name[sizeof(current->comm)];
+ printk_ratelimited(KERN_NOTICE "non-matching-uid symlink "
+ "following attempted in sticky world-writable "
+ "directory by %s (fsuid %d)\n",
+get_task_comm(name, current),cred->fsuid);
Quoted 6 lines
I don't think the
get_task_comm()extra layer is really necessary here - this is called in the *current* task's context
- and it's not like that tasks are changing their own names all that often while they are busy executing VFS code, right? The race with some other task writing to/proc/PID/commis uninteresting.
I would rather not have any kind of race here. It's only during the failure condition, so we don't need speed.
Quoted 8 lines
The more important piece of forensic information to log would be the file name that got attempted and perhaps the directory name as well. If there's an unknown hole in an unknown privileged app then this warning alone does not give the admin any clues where to look - the name of the expoiting task is probably obfuscated anyway, it's the identity of the attack vector that matters - and that name can generally not be controlled and obfuscated by the attacker.
Agreed. I will work on extracting this information and presenting it sanely.
Quoted 3 lines
If those issues are resolved:
Reviewed-by Ingo Molnar <[email protected]>
Thanks for looking it over!
-Kees
Signature · 2 lines
Kees Cook ChromeOS Security
Quoted 17 lines · Kees Cook
Documentation/sysctl/fs.txt | 21 ++++++++++++ fs/Kconfig | 15 ++++++++ fs/namei.c | 77 +++++++++++++++++++++++++++++++++++++++--- kernel/sysctl.c | 10 +++++ 4 files changed, 117 insertions(+), 6 deletions(-)fs/Kconfig +2
-0@@ -278,3 +278,18 @@ source "fs/nls/Kconfig" source "fs/dlm/Kconfig" endmenu + +config PROTECTED_STICKY_SYMLINKS
fs/Kconfig source "fs/nls/Kconfig" · line 292
Quoted 10 lines
fs/Kconfig +10
-0+ bool "Protect symlink following 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 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 only
better:
Enabling this solves the problem by permitting symlinks to be followed only when the uid ...
Quoted 10 lines
fs/Kconfig +3
-0+ be followed 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 +2
-0@@ -624,10 +625,69 @@ static inline void put_link(struct nameidata *nd, struct path *link, void *cooki + +/**
fs/namei.c static inline void put_link(struct nameidata *nd, struct path *link, void *cooki · line 636
Quoted 10 lines
fs/namei.c +10
-0+* 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_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 followed when outside a sticky
similar:
It will permit symlinks to be followed only when outside a sticky
Quoted 7 lines
fs/namei.c +7
-0+* world-writable directory, or when the uid of the symlink and follower +* 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)
Signature · 2 lines
~Randy *** Remember to use Documentation/SubmitChecklist when testing your code ***
Quoted 5 lines · Randy Dunlap
Quoted 1 line · Kees Cook
+ Enabling this solves the problem by permitting symlinks to only
better:
Quoted 8 lines
Enabling this solves the problem by permitting symlinks to be followed only when the uid ...
Quoted 1 line
+ * It will permit symlinks to only be followed when outside a sticky
similar:
It will permit symlinks to be followed only when outside a sticky
Thanks! I'll update these.
-Kees
Signature · 2 lines
Kees Cook ChromeOS Security
Quoted 3 lines · Kees Cook
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.
Ugh. I really dislike the implementation.
Wouldn't it be much nicer to instead actually use the symlink protection fields, and make the rules be:
- creating a symlink as root does the traditional "lrwxrwxrwx" thing
- creating a symlink in a directory you own similarly does "lrwxrwxrwx"
- creating a symlink anywhere else (which implies either sticky or
world-writable directory) does "lrwx------", so only you can use it.
That seems to be much nicer semantics, and makes the protection *visible* instead of some kind of hacky run-time random behavior depending on some invisible config option that people aren't even aware of.
Of course, it needs to handle renames too (and maybe lchown?), and it would still need to be an option you enable explicitly, but it seems much more polite to make things like this something you can actually see.
No, I have not thought this through deeply, but I really dislike your kind of "change random semantics in ways that are very hidden and subtle". The symlink protection approach seems to be much less hidden and subtle, and should result in largely the same result.
Notably, you can install a system without it on, and turn it on even after install - who cares if there are *long-term* symlinks with lrwxrwxrwx and that thus allow all access, it's the racily-created ones we need to worry about, so it should actually be perfectly ok to enable the symlink creation protection dynamically. In fact, it could be a inheritable per-*process* (or per-container) flag rather than a global flag that says how symlink creation acts.
I dunno.
Linus
Quoted 38 lines · Linus Torvalds
Quoted 3 lines · Kees Cook
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.
Ugh. I really dislike the implementation.
Wouldn't it be much nicer to instead actually use the symlink protection fields, and make the rules be:
- creating a symlink as root does the traditional "lrwxrwxrwx" thing
- creating a symlink in a directory you own similarly does "lrwxrwxrwx"
- creating a symlink anywhere else (which implies either sticky or world-writable directory) does "lrwx------", so only you can use it.That seems to be much nicer semantics, and makes the protection *visible* instead of some kind of hacky run-time random behavior depending on some invisible config option that people aren't even aware of.
Of course, it needs to handle renames too (and maybe lchown?), and it would still need to be an option you enable explicitly, but it seems much more polite to make things like this something you can actually see.
No, I have not thought this through deeply, but I really dislike your kind of "change random semantics in ways that are very hidden and subtle". The symlink protection approach seems to be much less hidden and subtle, and should result in largely the same result.
Notably, you can install a system without it on, and turn it on even after install - who cares if there are *long-term* symlinks with lrwxrwxrwx and that thus allow all access, it's the racily-created ones we need to worry about, so it should actually be perfectly ok to enable the symlink creation protection dynamically. In fact, it could be a inheritable per-*process* (or per-container) flag rather than a global flag that says how symlink creation acts.
Quoted 1 line
I dunno.
This seems like a much deeper and wider change. This would expand it to non-sticky, non-world-writable directories as well, meaning symlinks on fileservers couldn't be managed by non-directory owners any more, etc. I think there would be a lot of negative fall-out from that.
Also it wouldn't track the state of a potentially dangerous environment. Creation isn't the problem; existing is the problem. As an attacker, I could: "mkdir /tmp/foo; ln -s /etc/shadow /tmp/foo/evil-symlink; mv /tmp/foo/evil-symlink /tmp" and suddenly the lrwxrwxrwx would follow the mv. To fix this we'd have to change the permissions across a move, or other weird hidden behaviors.
Given that the patch is tweaking what "sticky directory" means, I don't think it's impolite. Only attackers have ever depended on this behavior, so let's just eliminate it.
-Kees
Signature · 2 lines
Kees Cook ChromeOS Security
Quoted 15 lines · Linus Torvalds
Quoted 3 lines · Kees Cook
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.
Ugh. I really dislike the implementation.
Wouldn't it be much nicer to instead actually use the symlink protection fields, and make the rules be:
- creating a symlink as root does the traditional "lrwxrwxrwx" thing
- creating a symlink in a directory you own similarly does "lrwxrwxrwx"
- creating a symlink anywhere else (which implies either sticky or
world-writable directory) does "lrwx------", so only you can use it.
Quoted 4 lines
That seems to be much nicer semantics, and makes the protection *visible* instead of some kind of hacky run-time random behavior depending on some invisible config option that people aren't even aware of.
This sounds really good way to go about it, but it seems that an implementation might be invasive.
Currently the symlinks are created in the file system e.g. ext4_symlink has the S_IFLNK|S_IRWXUGO buried in it. It is likely other fs's follow this model too.
Also, having tried creating such a link, it seems that the read-side permissions check (at least in generic_readlink) is missing.
Frank
Signature · 3 lines
------------------------------------------------------------------------ Frank A. Kingswood [email protected] Cambridge, United Kingdom +44-7545-209 100