blob: 98503b7923698e3b527dc278aa32d5db1da1dfe1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is <linux/capability.h>
3 *
Serge E. Hallynb5376772007-10-16 23:31:36 -07004 * Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexander Kjeldaas <astor@guardian.no>
6 * with help from Aleph1, Roland Buresund and Andrew Main.
7 *
8 * See here for the libcap library ("POSIX draft" compliance):
9 *
GeunSik Limbcf56442009-06-16 10:26:25 +020010 * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
Serge E. Hallynb5376772007-10-16 23:31:36 -070011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#ifndef _LINUX_CAPABILITY_H
13#define _LINUX_CAPABILITY_H
14
David Howells607ca462012-10-13 10:46:48 +010015#include <uapi/linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Andrew G. Morganca05a992008-05-27 22:05:17 -070017
18#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
19#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053021extern int file_caps_enabled;
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023typedef struct kernel_cap_struct {
Andrew G. Morganca05a992008-05-27 22:05:17 -070024 __u32 cap[_KERNEL_CAPABILITY_U32S];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025} kernel_cap_t;
26
Eric Parisc0b00442008-11-11 21:48:10 +110027/* exact same as vfs_cap_data but in cpu endian and always filled completely */
28struct cpu_vfs_cap_data {
29 __u32 magic_etc;
30 kernel_cap_t permitted;
31 kernel_cap_t inheritable;
32};
33
Andrew Morgane338d262008-02-04 22:29:42 -080034#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -080038struct inode;
Serge E. Hallyn34867402011-03-23 16:43:17 -070039struct dentry;
40struct user_namespace;
41
Serge E. Hallyn34867402011-03-23 16:43:17 -070042struct user_namespace *current_user_ns(void);
43
44extern const kernel_cap_t __cap_empty_set;
Serge E. Hallyn34867402011-03-23 16:43:17 -070045extern const kernel_cap_t __cap_init_eff_set;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Internal kernel functions only
49 */
Serge E. Hallynb5376772007-10-16 23:31:36 -070050
Andrew Morgane338d262008-02-04 22:29:42 -080051#define CAP_FOR_EACH_U32(__capi) \
Andrew G. Morganca05a992008-05-27 22:05:17 -070052 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050054/*
55 * CAP_FS_MASK and CAP_NFSD_MASKS:
56 *
57 * The fs mask is all the privileges that fsuid==0 historically meant.
58 * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
59 *
60 * It has never meant setting security.* and trusted.* xattrs.
61 *
62 * We could also define fsmask as follows:
63 * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
64 * 2. The security.* and trusted.* xattrs are fs-related MAC permissions
65 */
66
Andrew Morgane338d262008-02-04 22:29:42 -080067# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050068 | CAP_TO_MASK(CAP_MKNOD) \
Andrew Morgane338d262008-02-04 22:29:42 -080069 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
70 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
71 | CAP_TO_MASK(CAP_FOWNER) \
72 | CAP_TO_MASK(CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Casey Schauflere114e472008-02-04 22:29:50 -080074# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
75
Andrew G. Morganca05a992008-05-27 22:05:17 -070076#if _KERNEL_CAPABILITY_U32S != 2
Andrew Morgane338d262008-02-04 22:29:42 -080077# error Fix up hand-coded capability macro initializers
78#else /* HAND-CODED capability initializers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
David Howells25f2ea92008-04-29 20:54:28 +010080# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
81# define CAP_FULL_SET ((kernel_cap_t){{ ~0, ~0 }})
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050082# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
83 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
84 CAP_FS_MASK_B1 } })
J. Bruce Fields76a67ec2009-03-16 18:34:20 -040085# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050086 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
87 CAP_FS_MASK_B1 } })
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andrew G. Morganca05a992008-05-27 22:05:17 -070089#endif /* _KERNEL_CAPABILITY_U32S != 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Andrew Morgane338d262008-02-04 22:29:42 -080091# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Andrew Morgane338d262008-02-04 22:29:42 -080093#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
94#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
95#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
96
97#define CAP_BOP_ALL(c, a, b, OP) \
98do { \
99 unsigned __capi; \
100 CAP_FOR_EACH_U32(__capi) { \
101 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
102 } \
103} while (0)
104
105#define CAP_UOP_ALL(c, a, OP) \
106do { \
107 unsigned __capi; \
108 CAP_FOR_EACH_U32(__capi) { \
109 c.cap[__capi] = OP a.cap[__capi]; \
110 } \
111} while (0)
112
113static inline kernel_cap_t cap_combine(const kernel_cap_t a,
114 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Andrew Morgane338d262008-02-04 22:29:42 -0800116 kernel_cap_t dest;
117 CAP_BOP_ALL(dest, a, b, |);
118 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Andrew Morgane338d262008-02-04 22:29:42 -0800121static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
122 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Andrew Morgane338d262008-02-04 22:29:42 -0800124 kernel_cap_t dest;
125 CAP_BOP_ALL(dest, a, b, &);
126 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Andrew Morgane338d262008-02-04 22:29:42 -0800129static inline kernel_cap_t cap_drop(const kernel_cap_t a,
130 const kernel_cap_t drop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Andrew Morgane338d262008-02-04 22:29:42 -0800132 kernel_cap_t dest;
133 CAP_BOP_ALL(dest, a, drop, &~);
134 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Andrew Morgane338d262008-02-04 22:29:42 -0800137static inline kernel_cap_t cap_invert(const kernel_cap_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Andrew Morgane338d262008-02-04 22:29:42 -0800139 kernel_cap_t dest;
140 CAP_UOP_ALL(dest, c, ~);
141 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Andrew Morgane338d262008-02-04 22:29:42 -0800144static inline int cap_isclear(const kernel_cap_t a)
145{
146 unsigned __capi;
147 CAP_FOR_EACH_U32(__capi) {
148 if (a.cap[__capi] != 0)
149 return 0;
150 }
151 return 1;
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Paris9d36be72008-11-11 21:48:07 +1100154/*
155 * Check if "a" is a subset of "set".
156 * return 1 if ALL of the capabilities in "a" are also in "set"
157 * cap_issubset(0101, 1111) will return 1
158 * return 0 if ANY of the capabilities in "a" are not in "set"
159 * cap_issubset(1111, 0101) will return 0
160 */
Andrew Morgane338d262008-02-04 22:29:42 -0800161static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
162{
163 kernel_cap_t dest;
164 dest = cap_drop(a, set);
165 return cap_isclear(dest);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Andrew Morgane338d262008-02-04 22:29:42 -0800168/* Used to decide between falling back on the old suser() or fsuser(). */
169
170static inline int cap_is_fs_cap(int cap)
171{
172 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
173 return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
174}
175
176static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
177{
178 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
179 return cap_drop(a, __cap_fs_set);
180}
181
182static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
183 const kernel_cap_t permitted)
184{
185 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
186 return cap_combine(a,
187 cap_intersect(permitted, __cap_fs_set));
188}
189
190static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
191{
192 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
193 return cap_drop(a, __cap_fs_set);
194}
195
196static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
197 const kernel_cap_t permitted)
198{
199 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
200 return cap_combine(a,
201 cap_intersect(permitted, __cap_nfsd_set));
202}
203
Serge E. Hallyn32632452011-03-23 16:43:21 -0700204extern bool has_capability(struct task_struct *t, int cap);
205extern bool has_ns_capability(struct task_struct *t,
206 struct user_namespace *ns, int cap);
207extern bool has_capability_noaudit(struct task_struct *t, int cap);
Eric Paris7b61d642012-01-03 12:25:15 -0500208extern bool has_ns_capability_noaudit(struct task_struct *t,
209 struct user_namespace *ns, int cap);
Serge E. Hallyn34867402011-03-23 16:43:17 -0700210extern bool capable(int cap);
211extern bool ns_capable(struct user_namespace *ns, int cap);
Serge E. Hallyn47a150e2011-05-13 04:27:54 +0100212extern bool nsown_capable(int cap);
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -0800213extern bool inode_capable(const struct inode *inode, int cap);
Randy.Dunlapc59ede72006-01-11 12:17:46 -0800214
Eric Paris851f7ff2008-11-11 21:48:14 +1100215/* audit system wants to get cap info from files as well */
Eric Paris851f7ff2008-11-11 21:48:14 +1100216extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif /* !_LINUX_CAPABILITY_H */