Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / linux / capability.h
CommitLineData
1da177e4
LT
1/*
2 * This is <linux/capability.h>
3 *
b5376771 4 * Andrew G. Morgan <morgan@kernel.org>
1da177e4
LT
5 * 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 *
bcf56442 10 * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
b5376771 11 */
1da177e4
LT
12#ifndef _LINUX_CAPABILITY_H
13#define _LINUX_CAPABILITY_H
14
607ca46e 15#include <uapi/linux/capability.h>
e338d263 16
ca05a99a
AM
17
18#define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
19#define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
1da177e4 20
9fa91d99 21extern int file_caps_enabled;
9fa91d99 22
1da177e4 23typedef struct kernel_cap_struct {
ca05a99a 24 __u32 cap[_KERNEL_CAPABILITY_U32S];
1da177e4
LT
25} kernel_cap_t;
26
c0b00441
EP
27/* 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
e338d263 34#define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
1da177e4
LT
35#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
36
1da177e4 37
935d8aab 38struct file;
1a48e2ac 39struct inode;
3486740a
SH
40struct dentry;
41struct user_namespace;
42
3486740a 43extern const kernel_cap_t __cap_empty_set;
3486740a
SH
44extern const kernel_cap_t __cap_init_eff_set;
45
1da177e4
LT
46/*
47 * Internal kernel functions only
48 */
b5376771 49
e338d263 50#define CAP_FOR_EACH_U32(__capi) \
ca05a99a 51 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
e338d263 52
0ad30b8f
SH
53/*
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
e338d263 66# define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
0ad30b8f 67 | CAP_TO_MASK(CAP_MKNOD) \
e338d263
AM
68 | 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))
72
e114e473
CS
73# define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
74
ca05a99a 75#if _KERNEL_CAPABILITY_U32S != 2
e338d263
AM
76# error Fix up hand-coded capability macro initializers
77#else /* HAND-CODED capability initializers */
78
76f01555
EP
79#define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
80#define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
81
25f2ea9f 82# define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
76f01555 83# define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
0ad30b8f
SH
84# define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
85 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
86 CAP_FS_MASK_B1 } })
76a67ec6 87# define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
0ad30b8f
SH
88 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
89 CAP_FS_MASK_B1 } })
e338d263 90
ca05a99a 91#endif /* _KERNEL_CAPABILITY_U32S != 2 */
e338d263 92
e338d263 93# define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
e338d263
AM
94
95#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)
117{
118 kernel_cap_t dest;
119 CAP_BOP_ALL(dest, a, b, |);
120 return dest;
121}
1da177e4 122
e338d263
AM
123static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
124 const kernel_cap_t b)
125{
126 kernel_cap_t dest;
127 CAP_BOP_ALL(dest, a, b, &);
128 return dest;
129}
1da177e4 130
e338d263
AM
131static inline kernel_cap_t cap_drop(const kernel_cap_t a,
132 const kernel_cap_t drop)
133{
134 kernel_cap_t dest;
135 CAP_BOP_ALL(dest, a, drop, &~);
136 return dest;
137}
1da177e4 138
e338d263
AM
139static inline kernel_cap_t cap_invert(const kernel_cap_t c)
140{
141 kernel_cap_t dest;
142 CAP_UOP_ALL(dest, c, ~);
143 return dest;
144}
1da177e4 145
e338d263
AM
146static inline int cap_isclear(const kernel_cap_t a)
147{
148 unsigned __capi;
149 CAP_FOR_EACH_U32(__capi) {
150 if (a.cap[__capi] != 0)
151 return 0;
152 }
153 return 1;
154}
1da177e4 155
9d36be76
EP
156/*
157 * Check if "a" is a subset of "set".
158 * return 1 if ALL of the capabilities in "a" are also in "set"
159 * cap_issubset(0101, 1111) will return 1
160 * return 0 if ANY of the capabilities in "a" are not in "set"
161 * cap_issubset(1111, 0101) will return 0
162 */
e338d263
AM
163static inline int cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
164{
165 kernel_cap_t dest;
166 dest = cap_drop(a, set);
167 return cap_isclear(dest);
168}
1da177e4 169
e338d263 170/* Used to decide between falling back on the old suser() or fsuser(). */
1da177e4 171
e338d263 172static inline int cap_is_fs_cap(int cap)
1da177e4 173{
e338d263
AM
174 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
175 return !!(CAP_TO_MASK(cap) & __cap_fs_set.cap[CAP_TO_INDEX(cap)]);
1da177e4
LT
176}
177
e338d263 178static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
1da177e4 179{
e338d263
AM
180 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
181 return cap_drop(a, __cap_fs_set);
1da177e4
LT
182}
183
e338d263
AM
184static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
185 const kernel_cap_t permitted)
1da177e4 186{
e338d263
AM
187 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
188 return cap_combine(a,
189 cap_intersect(permitted, __cap_fs_set));
1da177e4
LT
190}
191
e338d263 192static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
1da177e4 193{
e338d263
AM
194 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
195 return cap_drop(a, __cap_fs_set);
1da177e4
LT
196}
197
e338d263
AM
198static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
199 const kernel_cap_t permitted)
200{
201 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
202 return cap_combine(a,
203 cap_intersect(permitted, __cap_nfsd_set));
204}
1da177e4 205
3263245d
SH
206extern bool has_capability(struct task_struct *t, int cap);
207extern bool has_ns_capability(struct task_struct *t,
208 struct user_namespace *ns, int cap);
209extern bool has_capability_noaudit(struct task_struct *t, int cap);
7b61d648
EP
210extern bool has_ns_capability_noaudit(struct task_struct *t,
211 struct user_namespace *ns, int cap);
3486740a
SH
212extern bool capable(int cap);
213extern bool ns_capable(struct user_namespace *ns, int cap);
47a150ed 214extern bool nsown_capable(int cap);
4f80c6c1 215extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
935d8aab 216extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
c59ede7b 217
851f7ff5 218/* audit system wants to get cap info from files as well */
851f7ff5
EP
219extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
220
1da177e4 221#endif /* !_LINUX_CAPABILITY_H */