0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef _LINUX_CAPABILITY_H
0014 #define _LINUX_CAPABILITY_H
0015
0016 #include <uapi/linux/capability.h>
0017 #include <linux/uidgid.h>
0018
0019 #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
0020 #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
0021
0022 extern int file_caps_enabled;
0023
0024 typedef struct kernel_cap_struct {
0025 __u32 cap[_KERNEL_CAPABILITY_U32S];
0026 } kernel_cap_t;
0027
0028
0029 struct cpu_vfs_cap_data {
0030 __u32 magic_etc;
0031 kernel_cap_t permitted;
0032 kernel_cap_t inheritable;
0033 kuid_t rootid;
0034 };
0035
0036 #define _USER_CAP_HEADER_SIZE (sizeof(struct __user_cap_header_struct))
0037 #define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
0038
0039
0040 struct file;
0041 struct inode;
0042 struct dentry;
0043 struct task_struct;
0044 struct user_namespace;
0045
0046 extern const kernel_cap_t __cap_empty_set;
0047 extern const kernel_cap_t __cap_init_eff_set;
0048
0049
0050
0051
0052
0053 #define CAP_FOR_EACH_U32(__capi) \
0054 for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 # define CAP_FS_MASK_B0 (CAP_TO_MASK(CAP_CHOWN) \
0070 | CAP_TO_MASK(CAP_MKNOD) \
0071 | CAP_TO_MASK(CAP_DAC_OVERRIDE) \
0072 | CAP_TO_MASK(CAP_DAC_READ_SEARCH) \
0073 | CAP_TO_MASK(CAP_FOWNER) \
0074 | CAP_TO_MASK(CAP_FSETID))
0075
0076 # define CAP_FS_MASK_B1 (CAP_TO_MASK(CAP_MAC_OVERRIDE))
0077
0078 #if _KERNEL_CAPABILITY_U32S != 2
0079 # error Fix up hand-coded capability macro initializers
0080 #else
0081
0082 #define CAP_LAST_U32 ((_KERNEL_CAPABILITY_U32S) - 1)
0083 #define CAP_LAST_U32_VALID_MASK (CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
0084
0085 # define CAP_EMPTY_SET ((kernel_cap_t){{ 0, 0 }})
0086 # define CAP_FULL_SET ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
0087 # define CAP_FS_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
0088 | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
0089 CAP_FS_MASK_B1 } })
0090 # define CAP_NFSD_SET ((kernel_cap_t){{ CAP_FS_MASK_B0 \
0091 | CAP_TO_MASK(CAP_SYS_RESOURCE), \
0092 CAP_FS_MASK_B1 } })
0093
0094 #endif
0095
0096 # define cap_clear(c) do { (c) = __cap_empty_set; } while (0)
0097
0098 #define cap_raise(c, flag) ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
0099 #define cap_lower(c, flag) ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
0100 #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
0101
0102 #define CAP_BOP_ALL(c, a, b, OP) \
0103 do { \
0104 unsigned __capi; \
0105 CAP_FOR_EACH_U32(__capi) { \
0106 c.cap[__capi] = a.cap[__capi] OP b.cap[__capi]; \
0107 } \
0108 } while (0)
0109
0110 #define CAP_UOP_ALL(c, a, OP) \
0111 do { \
0112 unsigned __capi; \
0113 CAP_FOR_EACH_U32(__capi) { \
0114 c.cap[__capi] = OP a.cap[__capi]; \
0115 } \
0116 } while (0)
0117
0118 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
0119 const kernel_cap_t b)
0120 {
0121 kernel_cap_t dest;
0122 CAP_BOP_ALL(dest, a, b, |);
0123 return dest;
0124 }
0125
0126 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
0127 const kernel_cap_t b)
0128 {
0129 kernel_cap_t dest;
0130 CAP_BOP_ALL(dest, a, b, &);
0131 return dest;
0132 }
0133
0134 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
0135 const kernel_cap_t drop)
0136 {
0137 kernel_cap_t dest;
0138 CAP_BOP_ALL(dest, a, drop, &~);
0139 return dest;
0140 }
0141
0142 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
0143 {
0144 kernel_cap_t dest;
0145 CAP_UOP_ALL(dest, c, ~);
0146 return dest;
0147 }
0148
0149 static inline bool cap_isclear(const kernel_cap_t a)
0150 {
0151 unsigned __capi;
0152 CAP_FOR_EACH_U32(__capi) {
0153 if (a.cap[__capi] != 0)
0154 return false;
0155 }
0156 return true;
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166 static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
0167 {
0168 kernel_cap_t dest;
0169 dest = cap_drop(a, set);
0170 return cap_isclear(dest);
0171 }
0172
0173
0174
0175 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
0176 {
0177 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
0178 return cap_drop(a, __cap_fs_set);
0179 }
0180
0181 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
0182 const kernel_cap_t permitted)
0183 {
0184 const kernel_cap_t __cap_fs_set = CAP_FS_SET;
0185 return cap_combine(a,
0186 cap_intersect(permitted, __cap_fs_set));
0187 }
0188
0189 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
0190 {
0191 const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
0192 return cap_drop(a, __cap_fs_set);
0193 }
0194
0195 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
0196 const kernel_cap_t permitted)
0197 {
0198 const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
0199 return cap_combine(a,
0200 cap_intersect(permitted, __cap_nfsd_set));
0201 }
0202
0203 #ifdef CONFIG_MULTIUSER
0204 extern bool has_capability(struct task_struct *t, int cap);
0205 extern bool has_ns_capability(struct task_struct *t,
0206 struct user_namespace *ns, int cap);
0207 extern bool has_capability_noaudit(struct task_struct *t, int cap);
0208 extern bool has_ns_capability_noaudit(struct task_struct *t,
0209 struct user_namespace *ns, int cap);
0210 extern bool capable(int cap);
0211 extern bool ns_capable(struct user_namespace *ns, int cap);
0212 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
0213 extern bool ns_capable_setid(struct user_namespace *ns, int cap);
0214 #else
0215 static inline bool has_capability(struct task_struct *t, int cap)
0216 {
0217 return true;
0218 }
0219 static inline bool has_ns_capability(struct task_struct *t,
0220 struct user_namespace *ns, int cap)
0221 {
0222 return true;
0223 }
0224 static inline bool has_capability_noaudit(struct task_struct *t, int cap)
0225 {
0226 return true;
0227 }
0228 static inline bool has_ns_capability_noaudit(struct task_struct *t,
0229 struct user_namespace *ns, int cap)
0230 {
0231 return true;
0232 }
0233 static inline bool capable(int cap)
0234 {
0235 return true;
0236 }
0237 static inline bool ns_capable(struct user_namespace *ns, int cap)
0238 {
0239 return true;
0240 }
0241 static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
0242 {
0243 return true;
0244 }
0245 static inline bool ns_capable_setid(struct user_namespace *ns, int cap)
0246 {
0247 return true;
0248 }
0249 #endif
0250 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
0251 struct user_namespace *mnt_userns,
0252 const struct inode *inode);
0253 bool capable_wrt_inode_uidgid(struct user_namespace *mnt_userns,
0254 const struct inode *inode, int cap);
0255 extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
0256 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
0257 static inline bool perfmon_capable(void)
0258 {
0259 return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
0260 }
0261
0262 static inline bool bpf_capable(void)
0263 {
0264 return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
0265 }
0266
0267 static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
0268 {
0269 return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
0270 ns_capable(ns, CAP_SYS_ADMIN);
0271 }
0272
0273
0274 int get_vfs_caps_from_disk(struct user_namespace *mnt_userns,
0275 const struct dentry *dentry,
0276 struct cpu_vfs_cap_data *cpu_caps);
0277
0278 int cap_convert_nscap(struct user_namespace *mnt_userns, struct dentry *dentry,
0279 const void **ivalue, size_t size);
0280
0281 #endif