blob: 00690ff92edfed99956b04947d2c09b9ebac580f [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
Linus Torvalds935d8aa2013-04-14 10:06:31 -070038struct file;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -080039struct inode;
Serge E. Hallyn34867402011-03-23 16:43:17 -070040struct dentry;
41struct user_namespace;
42
Serge E. Hallyn34867402011-03-23 16:43:17 -070043extern const kernel_cap_t __cap_empty_set;
Serge E. Hallyn34867402011-03-23 16:43:17 -070044extern const kernel_cap_t __cap_init_eff_set;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Internal kernel functions only
48 */
Serge E. Hallynb5376772007-10-16 23:31:36 -070049
Andrew Morgane338d262008-02-04 22:29:42 -080050#define CAP_FOR_EACH_U32(__capi) \
Andrew G. Morganca05a992008-05-27 22:05:17 -070051 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050053/*
54 * CAP_FS_MASK and CAP_NFSD_MASKS:
55 *
56 * The fs mask is all the privileges that fsuid==0 historically meant.
57 * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
58 *
59 * It has never meant setting security.* and trusted.* xattrs.
60 *
61 * We could also define fsmask as follows:
62 * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
63 * 2. The security.* and trusted.* xattrs are fs-related MAC permissions
64 */
65
Andrew Morgane338d262008-02-04 22:29:42 -080066# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050067 | CAP_TO_MASK(CAP_MKNOD) \
Andrew Morgane338d262008-02-04 22:29:42 -080068 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
69 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
70 | CAP_TO_MASK(CAP_FOWNER) \
71 | CAP_TO_MASK(CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Casey Schauflere114e472008-02-04 22:29:50 -080073# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
74
Andrew G. Morganca05a992008-05-27 22:05:17 -070075#if _KERNEL_CAPABILITY_U32S != 2
Andrew Morgane338d262008-02-04 22:29:42 -080076# error Fix up hand-coded capability macro initializers
77#else /* HAND-CODED capability initializers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Eric Paris7d8b6c62014-07-23 15:36:26 -040079#define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
80#define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
81
David Howells25f2ea92008-04-29 20:54:28 +010082# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
Eric Paris7d8b6c62014-07-23 15:36:26 -040083# define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050084# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
85 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
86 CAP_FS_MASK_B1 } })
J. Bruce Fields76a67ec2009-03-16 18:34:20 -040087# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050088 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
89 CAP_FS_MASK_B1 } })
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Andrew G. Morganca05a992008-05-27 22:05:17 -070091#endif /* _KERNEL_CAPABILITY_U32S != 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Andrew Morgane338d262008-02-04 22:29:42 -080093# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andrew Morgane338d262008-02-04 22:29:42 -080095#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
96#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
97#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
98
99#define CAP_BOP_ALL(c, a, b, OP) \
100do { \
101 unsigned __capi; \
102 CAP_FOR_EACH_U32(__capi) { \
103 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
104 } \
105} while (0)
106
107#define CAP_UOP_ALL(c, a, OP) \
108do { \
109 unsigned __capi; \
110 CAP_FOR_EACH_U32(__capi) { \
111 c.cap[__capi] = OP a.cap[__capi]; \
112 } \
113} while (0)
114
115static inline kernel_cap_t cap_combine(const kernel_cap_t a,
116 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Andrew Morgane338d262008-02-04 22:29:42 -0800118 kernel_cap_t dest;
119 CAP_BOP_ALL(dest, a, b, |);
120 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Andrew Morgane338d262008-02-04 22:29:42 -0800123static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
124 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Andrew Morgane338d262008-02-04 22:29:42 -0800126 kernel_cap_t dest;
127 CAP_BOP_ALL(dest, a, b, &);
128 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Andrew Morgane338d262008-02-04 22:29:42 -0800131static inline kernel_cap_t cap_drop(const kernel_cap_t a,
132 const kernel_cap_t drop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Andrew Morgane338d262008-02-04 22:29:42 -0800134 kernel_cap_t dest;
135 CAP_BOP_ALL(dest, a, drop, &~);
136 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Andrew Morgane338d262008-02-04 22:29:42 -0800139static inline kernel_cap_t cap_invert(const kernel_cap_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Andrew Morgane338d262008-02-04 22:29:42 -0800141 kernel_cap_t dest;
142 CAP_UOP_ALL(dest, c, ~);
143 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Yaowei Baie42852bf2015-11-17 15:25:24 +0800146static inline bool cap_isclear(const kernel_cap_t a)
Andrew Morgane338d262008-02-04 22:29:42 -0800147{
148 unsigned __capi;
149 CAP_FOR_EACH_U32(__capi) {
150 if (a.cap[__capi] != 0)
Yaowei Baie42852bf2015-11-17 15:25:24 +0800151 return false;
Andrew Morgane338d262008-02-04 22:29:42 -0800152 }
Yaowei Baie42852bf2015-11-17 15:25:24 +0800153 return true;
Andrew Morgane338d262008-02-04 22:29:42 -0800154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Eric Paris9d36be72008-11-11 21:48:07 +1100156/*
157 * Check if "a" is a subset of "set".
Yaowei Baie42852bf2015-11-17 15:25:24 +0800158 * return true if ALL of the capabilities in "a" are also in "set"
159 * cap_issubset(0101, 1111) will return true
160 * return false if ANY of the capabilities in "a" are not in "set"
161 * cap_issubset(1111, 0101) will return false
Eric Paris9d36be72008-11-11 21:48:07 +1100162 */
Yaowei Baie42852bf2015-11-17 15:25:24 +0800163static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
Andrew Morgane338d262008-02-04 22:29:42 -0800164{
165 kernel_cap_t dest;
166 dest = cap_drop(a, set);
167 return cap_isclear(dest);
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Andrew Morgane338d262008-02-04 22:29:42 -0800170/* Used to decide between falling back on the old suser() or fsuser(). */
171
Andrew Morgane338d262008-02-04 22:29:42 -0800172static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
173{
174 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
175 return cap_drop(a, __cap_fs_set);
176}
177
178static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
179 const kernel_cap_t permitted)
180{
181 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
182 return cap_combine(a,
183 cap_intersect(permitted, __cap_fs_set));
184}
185
186static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
187{
188 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
189 return cap_drop(a, __cap_fs_set);
190}
191
192static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
193 const kernel_cap_t permitted)
194{
195 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
196 return cap_combine(a,
197 cap_intersect(permitted, __cap_nfsd_set));
198}
199
Iulia Manda28138932015-04-15 16:16:41 -0700200#ifdef CONFIG_MULTIUSER
Serge E. Hallyn32632452011-03-23 16:43:21 -0700201extern bool has_capability(struct task_struct *t, int cap);
202extern bool has_ns_capability(struct task_struct *t,
203 struct user_namespace *ns, int cap);
204extern bool has_capability_noaudit(struct task_struct *t, int cap);
Eric Paris7b61d642012-01-03 12:25:15 -0500205extern bool has_ns_capability_noaudit(struct task_struct *t,
206 struct user_namespace *ns, int cap);
Serge E. Hallyn34867402011-03-23 16:43:17 -0700207extern bool capable(int cap);
208extern bool ns_capable(struct user_namespace *ns, int cap);
Iulia Manda28138932015-04-15 16:16:41 -0700209#else
210static inline bool has_capability(struct task_struct *t, int cap)
211{
212 return true;
213}
214static inline bool has_ns_capability(struct task_struct *t,
215 struct user_namespace *ns, int cap)
216{
217 return true;
218}
219static inline bool has_capability_noaudit(struct task_struct *t, int cap)
220{
221 return true;
222}
223static inline bool has_ns_capability_noaudit(struct task_struct *t,
224 struct user_namespace *ns, int cap)
225{
226 return true;
227}
228static inline bool capable(int cap)
229{
230 return true;
231}
232static inline bool ns_capable(struct user_namespace *ns, int cap)
233{
234 return true;
235}
236#endif /* CONFIG_MULTIUSER */
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700237extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
Linus Torvalds935d8aa2013-04-14 10:06:31 -0700238extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
Randy.Dunlapc59ede72006-01-11 12:17:46 -0800239
Eric Paris851f7ff2008-11-11 21:48:14 +1100240/* audit system wants to get cap info from files as well */
Eric Paris851f7ff2008-11-11 21:48:14 +1100241extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif /* !_LINUX_CAPABILITY_H */