Skip to contentSkip to conversation

fs: hardlink creation restrictions

PATCH New [email protected] 1 participant 1 message May 4, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 3/5 fs: hardlink creation restrictions

From: Kees Cook <[email protected]>
Subject: 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

[[email protected]: uninline may_linkat() and audit_log_link_denied()]
Cc Matthew Wilcox <[email protected]>
Cc Alexander Viro <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Federica Teodori <[email protected]>
Cc Lucian Adrian Grijincu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Eric Paris <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Dan Rosenberg <[email protected]>

---

 Documentation/sysctl/fs.txt |   21 +++++
 fs/Kconfig                  |   56 ++++++++++----
 fs/namei.c                  |  132 +++++++++++++++++++++++++++-------
 include/linux/fs.h          |    1 
 kernel/sysctl.c             |   11 ++
 5 files changed, 178 insertions(+), 43 deletions(-)
Documentation/sysctl/fs.txt renamed from Documentation/sysctl/fs.txt~fs-hardlink-creation-restrictions +21 -0
diff -puN Documentation/sysctl/fs.txt~fs-hardlink-creation-restrictions Documentation/sysctl/fs.txt
@@ -32,6 +32,7 @@ Currently, these files are in /proc/sys/
 - nr_open
 - overflowuid
 - overflowgid
+- protected_hardlinks
 - protected_symlinks
 - suid_dumpable
 - super-max
@@ -158,6 +159,26 @@ The default is 65534.
 
 ==============================================================
 
+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
fs/Kconfig renamed from fs/Kconfig~fs-hardlink-creation-restrictions +41 -15

78 lines not on this page · raw message

fs/namei.c renamed from fs/namei.c~fs-hardlink-creation-restrictions +105 -27

182 lines not on this page · raw message

include/linux/fs.h renamed from include/linux/fs.h~fs-hardlink-creation-restrictions +1 -0
diff -puN include/linux/fs.h~fs-hardlink-creation-restrictions include/linux/fs.h
@@ -428,6 +428,7 @@ extern int sysctl_nr_open;
 extern struct inodes_stat_t inodes_stat;
 extern int leases_enable, lease_break_time;
 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 renamed from kernel/sysctl.c~fs-hardlink-creation-restrictions +10 -1
diff -puN kernel/sysctl.c~fs-hardlink-creation-restrictions kernel/sysctl.c
@@ -1493,7 +1493,7 @@ static struct ctl_table fs_table[] = {
 	},
 #endif
 #endif
-#ifdef CONFIG_PROTECTED_SYMLINKS
+#ifdef CONFIG_PROTECTED_LINKS
 	{
 		.procname	= "protected_symlinks",
 		.data		= &sysctl_protected_symlinks,
@@ -1503,6 +1503,15 @@ static struct ctl_table fs_table[] = {
 		.extra1		= &zero,
 		.extra2		= &one,
 	},
+	{
+		.procname	= "protected_hardlinks",
+		.data		= &sysctl_protected_hardlinks,
+		.maxlen		= sizeof(int),
+		.mode		= 0600,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= &zero,
+		.extra2		= &one,
+	},
 #endif
 	{
 		.procname	= "suid_dumpable",

_