]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/3.14.8/fs-userns-change-inode_capable-to-capable_wrt_inode_uidgid.patch
drop queue-4.14/mips-make-sure-dt-memory-regions-are-valid.patch
[thirdparty/kernel/stable-queue.git] / releases / 3.14.8 / fs-userns-change-inode_capable-to-capable_wrt_inode_uidgid.patch
CommitLineData
90996ada
GKH
1From 23adbe12ef7d3d4195e80800ab36b37bee28cd03 Mon Sep 17 00:00:00 2001
2From: Andy Lutomirski <luto@amacapital.net>
3Date: Tue, 10 Jun 2014 12:45:42 -0700
4Subject: fs,userns: Change inode_capable to capable_wrt_inode_uidgid
5
6From: Andy Lutomirski <luto@amacapital.net>
7
8commit 23adbe12ef7d3d4195e80800ab36b37bee28cd03 upstream.
9
10The kernel has no concept of capabilities with respect to inodes; inodes
11exist independently of namespaces. For example, inode_capable(inode,
12CAP_LINUX_IMMUTABLE) would be nonsense.
13
14This patch changes inode_capable to check for uid and gid mappings and
15renames it to capable_wrt_inode_uidgid, which should make it more
16obvious what it does.
17
18Fixes CVE-2014-4014.
19
20Cc: Theodore Ts'o <tytso@mit.edu>
21Cc: Serge Hallyn <serge.hallyn@ubuntu.com>
22Cc: "Eric W. Biederman" <ebiederm@xmission.com>
23Cc: Dave Chinner <david@fromorbit.com>
24Signed-off-by: Andy Lutomirski <luto@amacapital.net>
25Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
26Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
27
28---
29 fs/attr.c | 8 ++++----
30 fs/inode.c | 10 +++++++---
31 fs/namei.c | 11 ++++++-----
32 fs/xfs/xfs_ioctl.c | 2 +-
33 include/linux/capability.h | 2 +-
34 kernel/capability.c | 20 ++++++++------------
35 6 files changed, 27 insertions(+), 26 deletions(-)
36
37--- a/fs/attr.c
38+++ b/fs/attr.c
39@@ -50,14 +50,14 @@ int inode_change_ok(const struct inode *
40 if ((ia_valid & ATTR_UID) &&
41 (!uid_eq(current_fsuid(), inode->i_uid) ||
42 !uid_eq(attr->ia_uid, inode->i_uid)) &&
43- !inode_capable(inode, CAP_CHOWN))
44+ !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
45 return -EPERM;
46
47 /* Make sure caller can chgrp. */
48 if ((ia_valid & ATTR_GID) &&
49 (!uid_eq(current_fsuid(), inode->i_uid) ||
50 (!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
51- !inode_capable(inode, CAP_CHOWN))
52+ !capable_wrt_inode_uidgid(inode, CAP_CHOWN))
53 return -EPERM;
54
55 /* Make sure a caller can chmod. */
56@@ -67,7 +67,7 @@ int inode_change_ok(const struct inode *
57 /* Also check the setgid bit! */
58 if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
59 inode->i_gid) &&
60- !inode_capable(inode, CAP_FSETID))
61+ !capable_wrt_inode_uidgid(inode, CAP_FSETID))
62 attr->ia_mode &= ~S_ISGID;
63 }
64
65@@ -160,7 +160,7 @@ void setattr_copy(struct inode *inode, c
66 umode_t mode = attr->ia_mode;
67
68 if (!in_group_p(inode->i_gid) &&
69- !inode_capable(inode, CAP_FSETID))
70+ !capable_wrt_inode_uidgid(inode, CAP_FSETID))
71 mode &= ~S_ISGID;
72 inode->i_mode = mode;
73 }
74--- a/fs/inode.c
75+++ b/fs/inode.c
76@@ -1840,14 +1840,18 @@ EXPORT_SYMBOL(inode_init_owner);
77 * inode_owner_or_capable - check current task permissions to inode
78 * @inode: inode being checked
79 *
80- * Return true if current either has CAP_FOWNER to the inode, or
81- * owns the file.
82+ * Return true if current either has CAP_FOWNER in a namespace with the
83+ * inode owner uid mapped, or owns the file.
84 */
85 bool inode_owner_or_capable(const struct inode *inode)
86 {
87+ struct user_namespace *ns;
88+
89 if (uid_eq(current_fsuid(), inode->i_uid))
90 return true;
91- if (inode_capable(inode, CAP_FOWNER))
92+
93+ ns = current_user_ns();
94+ if (ns_capable(ns, CAP_FOWNER) && kuid_has_mapping(ns, inode->i_uid))
95 return true;
96 return false;
97 }
98--- a/fs/namei.c
99+++ b/fs/namei.c
100@@ -332,10 +332,11 @@ int generic_permission(struct inode *ino
101
102 if (S_ISDIR(inode->i_mode)) {
103 /* DACs are overridable for directories */
104- if (inode_capable(inode, CAP_DAC_OVERRIDE))
105+ if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
106 return 0;
107 if (!(mask & MAY_WRITE))
108- if (inode_capable(inode, CAP_DAC_READ_SEARCH))
109+ if (capable_wrt_inode_uidgid(inode,
110+ CAP_DAC_READ_SEARCH))
111 return 0;
112 return -EACCES;
113 }
114@@ -345,7 +346,7 @@ int generic_permission(struct inode *ino
115 * at least one exec bit set.
116 */
117 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
118- if (inode_capable(inode, CAP_DAC_OVERRIDE))
119+ if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
120 return 0;
121
122 /*
123@@ -353,7 +354,7 @@ int generic_permission(struct inode *ino
124 */
125 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
126 if (mask == MAY_READ)
127- if (inode_capable(inode, CAP_DAC_READ_SEARCH))
128+ if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
129 return 0;
130
131 return -EACCES;
132@@ -2370,7 +2371,7 @@ static inline int check_sticky(struct in
133 return 0;
134 if (uid_eq(dir->i_uid, fsuid))
135 return 0;
136- return !inode_capable(inode, CAP_FOWNER);
137+ return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
138 }
139
140 /*
141--- a/fs/xfs/xfs_ioctl.c
142+++ b/fs/xfs/xfs_ioctl.c
143@@ -1241,7 +1241,7 @@ xfs_ioctl_setattr(
144 * cleared upon successful return from chown()
145 */
146 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
147- !inode_capable(VFS_I(ip), CAP_FSETID))
148+ !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
149 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
150
151 /*
152--- a/include/linux/capability.h
153+++ b/include/linux/capability.h
154@@ -210,7 +210,7 @@ extern bool has_ns_capability_noaudit(st
155 struct user_namespace *ns, int cap);
156 extern bool capable(int cap);
157 extern bool ns_capable(struct user_namespace *ns, int cap);
158-extern bool inode_capable(const struct inode *inode, int cap);
159+extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
160 extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
161
162 /* audit system wants to get cap info from files as well */
163--- a/kernel/capability.c
164+++ b/kernel/capability.c
165@@ -433,23 +433,19 @@ bool capable(int cap)
166 EXPORT_SYMBOL(capable);
167
168 /**
169- * inode_capable - Check superior capability over inode
170+ * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
171 * @inode: The inode in question
172 * @cap: The capability in question
173 *
174- * Return true if the current task has the given superior capability
175- * targeted at it's own user namespace and that the given inode is owned
176- * by the current user namespace or a child namespace.
177- *
178- * Currently we check to see if an inode is owned by the current
179- * user namespace by seeing if the inode's owner maps into the
180- * current user namespace.
181- *
182+ * Return true if the current task has the given capability targeted at
183+ * its own user namespace and that the given inode's uid and gid are
184+ * mapped into the current user namespace.
185 */
186-bool inode_capable(const struct inode *inode, int cap)
187+bool capable_wrt_inode_uidgid(const struct inode *inode, int cap)
188 {
189 struct user_namespace *ns = current_user_ns();
190
191- return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid);
192+ return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid) &&
193+ kgid_has_mapping(ns, inode->i_gid);
194 }
195-EXPORT_SYMBOL(inode_capable);
196+EXPORT_SYMBOL(capable_wrt_inode_uidgid);