blob: b769330e93807f17cf4eb43cbaa81f4cd2b0babf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * This is <linux/capability.h>
4 *
Serge E. Hallynb5376772007-10-16 23:31:36 -07005 * Andrew G. Morgan <morgan@kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Alexander Kjeldaas <astor@guardian.no>
7 * with help from Aleph1, Roland Buresund and Andrew Main.
8 *
9 * See here for the libcap library ("POSIX draft" compliance):
10 *
GeunSik Limbcf56442009-06-16 10:26:25 +020011 * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
Serge E. Hallynb5376772007-10-16 23:31:36 -070012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#ifndef _LINUX_CAPABILITY_H
14#define _LINUX_CAPABILITY_H
15
David Howells607ca462012-10-13 10:46:48 +010016#include <uapi/linux/capability.h>
Richard Guy Briggs2fec30e2019-01-23 21:36:25 -050017#include <linux/uidgid.h>
Andrew G. Morganca05a992008-05-27 22:05:17 -070018
19#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
20#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053022extern int file_caps_enabled;
Jaswinder Singh Rajput9fa91d92009-01-30 20:39:30 +053023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024typedef struct kernel_cap_struct {
Andrew G. Morganca05a992008-05-27 22:05:17 -070025 __u32 cap[_KERNEL_CAPABILITY_U32S];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026} kernel_cap_t;
27
Richard Guy Briggs2fec30e2019-01-23 21:36:25 -050028/* same as vfs_ns_cap_data but in cpu endian and always filled completely */
Eric Parisc0b00442008-11-11 21:48:10 +110029struct cpu_vfs_cap_data {
30 __u32 magic_etc;
31 kernel_cap_t permitted;
32 kernel_cap_t inheritable;
Richard Guy Briggs2fec30e2019-01-23 21:36:25 -050033 kuid_t rootid;
Eric Parisc0b00442008-11-11 21:48:10 +110034};
35
Andrew Morgane338d262008-02-04 22:29:42 -080036#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds935d8aa2013-04-14 10:06:31 -070040struct file;
Eric W. Biederman1a48e2a2011-11-14 16:24:06 -080041struct inode;
Serge E. Hallyn34867402011-03-23 16:43:17 -070042struct dentry;
Alexey Dobriyandb3f6002016-08-02 14:03:36 -070043struct task_struct;
Serge E. Hallyn34867402011-03-23 16:43:17 -070044struct user_namespace;
45
Serge E. Hallyn34867402011-03-23 16:43:17 -070046extern const kernel_cap_t __cap_empty_set;
Serge E. Hallyn34867402011-03-23 16:43:17 -070047extern const kernel_cap_t __cap_init_eff_set;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Internal kernel functions only
51 */
Serge E. Hallynb5376772007-10-16 23:31:36 -070052
Andrew Morgane338d262008-02-04 22:29:42 -080053#define CAP_FOR_EACH_U32(__capi) \
Andrew G. Morganca05a992008-05-27 22:05:17 -070054 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050056/*
57 * CAP_FS_MASK and CAP_NFSD_MASKS:
58 *
59 * The fs mask is all the privileges that fsuid==0 historically meant.
60 * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
61 *
62 * It has never meant setting security.* and trusted.* xattrs.
63 *
64 * We could also define fsmask as follows:
65 * 1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
66 * 2. The security.* and trusted.* xattrs are fs-related MAC permissions
67 */
68
Andrew Morgane338d262008-02-04 22:29:42 -080069# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050070 | CAP_TO_MASK(CAP_MKNOD) \
Andrew Morgane338d262008-02-04 22:29:42 -080071 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
72 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
73 | CAP_TO_MASK(CAP_FOWNER) \
74 | CAP_TO_MASK(CAP_FSETID))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Casey Schauflere114e472008-02-04 22:29:50 -080076# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
77
Andrew G. Morganca05a992008-05-27 22:05:17 -070078#if _KERNEL_CAPABILITY_U32S != 2
Andrew Morgane338d262008-02-04 22:29:42 -080079# error Fix up hand-coded capability macro initializers
80#else /* HAND-CODED capability initializers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric Paris7d8b6c62014-07-23 15:36:26 -040082#define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
83#define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
84
David Howells25f2ea92008-04-29 20:54:28 +010085# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
Eric Paris7d8b6c62014-07-23 15:36:26 -040086# define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050087# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
88 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
89 CAP_FS_MASK_B1 } })
J. Bruce Fields76a67ec2009-03-16 18:34:20 -040090# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
Serge E. Hallyn0ad30b82009-04-13 09:56:14 -050091 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
92 CAP_FS_MASK_B1 } })
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Andrew G. Morganca05a992008-05-27 22:05:17 -070094#endif /* _KERNEL_CAPABILITY_U32S != 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrew Morgane338d262008-02-04 22:29:42 -080096# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Andrew Morgane338d262008-02-04 22:29:42 -080098#define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
99#define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
100#define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
101
102#define CAP_BOP_ALL(c, a, b, OP) \
103do { \
104 unsigned __capi; \
105 CAP_FOR_EACH_U32(__capi) { \
106 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
107 } \
108} while (0)
109
110#define CAP_UOP_ALL(c, a, OP) \
111do { \
112 unsigned __capi; \
113 CAP_FOR_EACH_U32(__capi) { \
114 c.cap[__capi] = OP a.cap[__capi]; \
115 } \
116} while (0)
117
118static inline kernel_cap_t cap_combine(const kernel_cap_t a,
119 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Andrew Morgane338d262008-02-04 22:29:42 -0800121 kernel_cap_t dest;
122 CAP_BOP_ALL(dest, a, b, |);
123 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Andrew Morgane338d262008-02-04 22:29:42 -0800126static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
127 const kernel_cap_t b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Andrew Morgane338d262008-02-04 22:29:42 -0800129 kernel_cap_t dest;
130 CAP_BOP_ALL(dest, a, b, &);
131 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Andrew Morgane338d262008-02-04 22:29:42 -0800134static inline kernel_cap_t cap_drop(const kernel_cap_t a,
135 const kernel_cap_t drop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Andrew Morgane338d262008-02-04 22:29:42 -0800137 kernel_cap_t dest;
138 CAP_BOP_ALL(dest, a, drop, &~);
139 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Andrew Morgane338d262008-02-04 22:29:42 -0800142static inline kernel_cap_t cap_invert(const kernel_cap_t c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Andrew Morgane338d262008-02-04 22:29:42 -0800144 kernel_cap_t dest;
145 CAP_UOP_ALL(dest, c, ~);
146 return dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Yaowei Baie42852bf2015-11-17 15:25:24 +0800149static inline bool cap_isclear(const kernel_cap_t a)
Andrew Morgane338d262008-02-04 22:29:42 -0800150{
151 unsigned __capi;
152 CAP_FOR_EACH_U32(__capi) {
153 if (a.cap[__capi] != 0)
Yaowei Baie42852bf2015-11-17 15:25:24 +0800154 return false;
Andrew Morgane338d262008-02-04 22:29:42 -0800155 }
Yaowei Baie42852bf2015-11-17 15:25:24 +0800156 return true;
Andrew Morgane338d262008-02-04 22:29:42 -0800157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Eric Paris9d36be72008-11-11 21:48:07 +1100159/*
160 * Check if "a" is a subset of "set".
Yaowei Baie42852bf2015-11-17 15:25:24 +0800161 * return true if ALL of the capabilities in "a" are also in "set"
162 * cap_issubset(0101, 1111) will return true
163 * return false if ANY of the capabilities in "a" are not in "set"
164 * cap_issubset(1111, 0101) will return false
Eric Paris9d36be72008-11-11 21:48:07 +1100165 */
Yaowei Baie42852bf2015-11-17 15:25:24 +0800166static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
Andrew Morgane338d262008-02-04 22:29:42 -0800167{
168 kernel_cap_t dest;
169 dest = cap_drop(a, set);
170 return cap_isclear(dest);
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Andrew Morgane338d262008-02-04 22:29:42 -0800173/* Used to decide between falling back on the old suser() or fsuser(). */
174
Andrew Morgane338d262008-02-04 22:29:42 -0800175static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
176{
177 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
178 return cap_drop(a, __cap_fs_set);
179}
180
181static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
182 const kernel_cap_t permitted)
183{
184 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
185 return cap_combine(a,
186 cap_intersect(permitted, __cap_fs_set));
187}
188
189static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
190{
191 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
192 return cap_drop(a, __cap_fs_set);
193}
194
195static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
196 const kernel_cap_t permitted)
197{
198 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
199 return cap_combine(a,
200 cap_intersect(permitted, __cap_nfsd_set));
201}
202
Iulia Manda28138932015-04-15 16:16:41 -0700203#ifdef CONFIG_MULTIUSER
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);
Tyler Hicks98f368e2016-06-02 23:43:21 -0500212extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
Iulia Manda28138932015-04-15 16:16:41 -0700213#else
214static inline bool has_capability(struct task_struct *t, int cap)
215{
216 return true;
217}
218static inline bool has_ns_capability(struct task_struct *t,
219 struct user_namespace *ns, int cap)
220{
221 return true;
222}
223static inline bool has_capability_noaudit(struct task_struct *t, int cap)
224{
225 return true;
226}
227static inline bool has_ns_capability_noaudit(struct task_struct *t,
228 struct user_namespace *ns, int cap)
229{
230 return true;
231}
232static inline bool capable(int cap)
233{
234 return true;
235}
236static inline bool ns_capable(struct user_namespace *ns, int cap)
237{
238 return true;
239}
Tyler Hicks98f368e2016-06-02 23:43:21 -0500240static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
241{
242 return true;
243}
Iulia Manda28138932015-04-15 16:16:41 -0700244#endif /* CONFIG_MULTIUSER */
Eric W. Biedermanf84df2a2016-11-16 22:06:51 -0600245extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700246extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
Linus Torvalds935d8aa2013-04-14 10:06:31 -0700247extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
Eric W. Biederman64b875f2016-11-14 18:48:07 -0600248extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
Randy.Dunlapc59ede72006-01-11 12:17:46 -0800249
Eric Paris851f7ff2008-11-11 21:48:14 +1100250/* audit system wants to get cap info from files as well */
Eric Paris851f7ff2008-11-11 21:48:14 +1100251extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
252
Serge E. Hallyn8db6c342017-05-08 13:11:56 -0500253extern int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#endif /* !_LINUX_CAPABILITY_H */