Skip to content

Commit 23adbe1

Browse files
amlutotorvalds
authored andcommitted
fs,userns: Change inode_capable to capable_wrt_inode_uidgid
The kernel has no concept of capabilities with respect to inodes; inodes exist independently of namespaces. For example, inode_capable(inode, CAP_LINUX_IMMUTABLE) would be nonsense. This patch changes inode_capable to check for uid and gid mappings and renames it to capable_wrt_inode_uidgid, which should make it more obvious what it does. Fixes CVE-2014-4014. Cc: Theodore Ts'o <tytso@mit.edu> Cc: Serge Hallyn <serge.hallyn@ubuntu.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Dave Chinner <david@fromorbit.com> Cc: stable@vger.kernel.org Signed-off-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5b174fd commit 23adbe1

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

Diff for: fs/attr.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ int inode_change_ok(const struct inode *inode, struct iattr *attr)
5050
if ((ia_valid & ATTR_UID) &&
5151
(!uid_eq(current_fsuid(), inode->i_uid) ||
5252
!uid_eq(attr->ia_uid, inode->i_uid)) &&
53-
!inode_capable(inode, CAP_CHOWN))
53+
!capable_wrt_inode_uidgid(inode, CAP_CHOWN))
5454
return -EPERM;
5555

5656
/* Make sure caller can chgrp. */
5757
if ((ia_valid & ATTR_GID) &&
5858
(!uid_eq(current_fsuid(), inode->i_uid) ||
5959
(!in_group_p(attr->ia_gid) && !gid_eq(attr->ia_gid, inode->i_gid))) &&
60-
!inode_capable(inode, CAP_CHOWN))
60+
!capable_wrt_inode_uidgid(inode, CAP_CHOWN))
6161
return -EPERM;
6262

6363
/* Make sure a caller can chmod. */
@@ -67,7 +67,7 @@ int inode_change_ok(const struct inode *inode, struct iattr *attr)
6767
/* Also check the setgid bit! */
6868
if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
6969
inode->i_gid) &&
70-
!inode_capable(inode, CAP_FSETID))
70+
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
7171
attr->ia_mode &= ~S_ISGID;
7272
}
7373

@@ -160,7 +160,7 @@ void setattr_copy(struct inode *inode, const struct iattr *attr)
160160
umode_t mode = attr->ia_mode;
161161

162162
if (!in_group_p(inode->i_gid) &&
163-
!inode_capable(inode, CAP_FSETID))
163+
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
164164
mode &= ~S_ISGID;
165165
inode->i_mode = mode;
166166
}

Diff for: fs/inode.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -1839,14 +1839,18 @@ EXPORT_SYMBOL(inode_init_owner);
18391839
* inode_owner_or_capable - check current task permissions to inode
18401840
* @inode: inode being checked
18411841
*
1842-
* Return true if current either has CAP_FOWNER to the inode, or
1843-
* owns the file.
1842+
* Return true if current either has CAP_FOWNER in a namespace with the
1843+
* inode owner uid mapped, or owns the file.
18441844
*/
18451845
bool inode_owner_or_capable(const struct inode *inode)
18461846
{
1847+
struct user_namespace *ns;
1848+
18471849
if (uid_eq(current_fsuid(), inode->i_uid))
18481850
return true;
1849-
if (inode_capable(inode, CAP_FOWNER))
1851+
1852+
ns = current_user_ns();
1853+
if (ns_capable(ns, CAP_FOWNER) && kuid_has_mapping(ns, inode->i_uid))
18501854
return true;
18511855
return false;
18521856
}

Diff for: fs/namei.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,11 @@ int generic_permission(struct inode *inode, int mask)
332332

333333
if (S_ISDIR(inode->i_mode)) {
334334
/* DACs are overridable for directories */
335-
if (inode_capable(inode, CAP_DAC_OVERRIDE))
335+
if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
336336
return 0;
337337
if (!(mask & MAY_WRITE))
338-
if (inode_capable(inode, CAP_DAC_READ_SEARCH))
338+
if (capable_wrt_inode_uidgid(inode,
339+
CAP_DAC_READ_SEARCH))
339340
return 0;
340341
return -EACCES;
341342
}
@@ -345,15 +346,15 @@ int generic_permission(struct inode *inode, int mask)
345346
* at least one exec bit set.
346347
*/
347348
if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
348-
if (inode_capable(inode, CAP_DAC_OVERRIDE))
349+
if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
349350
return 0;
350351

351352
/*
352353
* Searching includes executable on directories, else just read.
353354
*/
354355
mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
355356
if (mask == MAY_READ)
356-
if (inode_capable(inode, CAP_DAC_READ_SEARCH))
357+
if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
357358
return 0;
358359

359360
return -EACCES;
@@ -2379,7 +2380,7 @@ static inline int check_sticky(struct inode *dir, struct inode *inode)
23792380
return 0;
23802381
if (uid_eq(dir->i_uid, fsuid))
23812382
return 0;
2382-
return !inode_capable(inode, CAP_FOWNER);
2383+
return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
23832384
}
23842385

23852386
/*

Diff for: fs/xfs/xfs_ioctl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ xfs_ioctl_setattr(
12151215
* cleared upon successful return from chown()
12161216
*/
12171217
if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
1218-
!inode_capable(VFS_I(ip), CAP_FSETID))
1218+
!capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
12191219
ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
12201220

12211221
/*

Diff for: include/linux/capability.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extern bool has_ns_capability_noaudit(struct task_struct *t,
210210
struct user_namespace *ns, int cap);
211211
extern bool capable(int cap);
212212
extern bool ns_capable(struct user_namespace *ns, int cap);
213-
extern bool inode_capable(const struct inode *inode, int cap);
213+
extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
214214
extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
215215

216216
/* audit system wants to get cap info from files as well */

Diff for: kernel/capability.c

+8-12
Original file line numberDiff line numberDiff line change
@@ -424,23 +424,19 @@ bool capable(int cap)
424424
EXPORT_SYMBOL(capable);
425425

426426
/**
427-
* inode_capable - Check superior capability over inode
427+
* capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
428428
* @inode: The inode in question
429429
* @cap: The capability in question
430430
*
431-
* Return true if the current task has the given superior capability
432-
* targeted at it's own user namespace and that the given inode is owned
433-
* by the current user namespace or a child namespace.
434-
*
435-
* Currently we check to see if an inode is owned by the current
436-
* user namespace by seeing if the inode's owner maps into the
437-
* current user namespace.
438-
*
431+
* Return true if the current task has the given capability targeted at
432+
* its own user namespace and that the given inode's uid and gid are
433+
* mapped into the current user namespace.
439434
*/
440-
bool inode_capable(const struct inode *inode, int cap)
435+
bool capable_wrt_inode_uidgid(const struct inode *inode, int cap)
441436
{
442437
struct user_namespace *ns = current_user_ns();
443438

444-
return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid);
439+
return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid) &&
440+
kgid_has_mapping(ns, inode->i_gid);
445441
}
446-
EXPORT_SYMBOL(inode_capable);
442+
EXPORT_SYMBOL(capable_wrt_inode_uidgid);

0 commit comments

Comments
 (0)