1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor policy definitions. 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #ifndef __AA_POLICY_H 12 #define __AA_POLICY_H 13 14 #include <linux/capability.h> 15 #include <linux/cred.h> 16 #include <linux/kref.h> 17 #include <linux/rhashtable.h> 18 #include <linux/sched.h> 19 #include <linux/slab.h> 20 #include <linux/socket.h> 21 22 #include "apparmor.h" 23 #include "audit.h" 24 #include "capability.h" 25 #include "domain.h" 26 #include "file.h" 27 #include "lib.h" 28 #include "label.h" 29 #include "perms.h" 30 #include "resource.h" 31 32 33 struct aa_ns; 34 35 extern int unprivileged_userns_apparmor_policy; 36 extern int aa_unprivileged_unconfined_restricted; 37 38 extern const char *const aa_profile_mode_names[]; 39 #define APPARMOR_MODE_NAMES_MAX_INDEX 4 40 41 #define PROFILE_MODE(_profile, _mode) \ 42 ((aa_g_profile_mode == (_mode)) || \ 43 ((_profile)->mode == (_mode))) 44 45 #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN) 46 47 #define USER_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_USER) 48 49 #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL) 50 51 #define PROFILE_IS_HAT(_profile) ((_profile)->label.flags & FLAG_HAT) 52 53 #define CHECK_DEBUG1(_profile) ((_profile)->label.flags & FLAG_DEBUG1) 54 55 #define CHECK_DEBUG2(_profile) ((_profile)->label.flags & FLAG_DEBUG2) 56 57 #define profile_is_stale(_profile) (label_is_stale(&(_profile)->label)) 58 59 #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2) 60 61 /* flags in the dfa accept2 table */ 62 enum dfa_accept_flags { 63 ACCEPT_FLAG_OWNER = 1, 64 }; 65 66 /* 67 * FIXME: currently need a clean way to replace and remove profiles as a 68 * set. It should be done at the namespace level. 69 * Either, with a set of profiles loaded at the namespace level or via 70 * a mark and remove marked interface. 71 */ 72 enum profile_mode { 73 APPARMOR_ENFORCE, /* enforce access rules */ 74 APPARMOR_COMPLAIN, /* allow and log access violations */ 75 APPARMOR_KILL, /* kill task on access violation */ 76 APPARMOR_UNCONFINED, /* profile set to unconfined */ 77 APPARMOR_USER, /* modified complain mode to userspace */ 78 }; 79 80 81 struct aa_tags_header { 82 u32 mask; /* bit mask matching permissions */ 83 u32 count; /* number of strings per entry */ 84 u32 size; /* size of all strings covered by count */ 85 u32 tags; /* index into string table */ 86 }; 87 88 struct aa_tags_struct { 89 struct { 90 u32 size; /* number of entries in tagsets */ 91 u32 *table; /* indexes into headers & strs */ 92 } sets; 93 struct { 94 u32 size; /* number of headers == num of strs */ 95 struct aa_tags_header *table; 96 } hdrs; 97 struct aa_str_table strs; 98 }; 99 100 /* struct aa_policydb - match engine for a policy 101 * @count: refcount for the pdb 102 * @dfa: dfa pattern match 103 * @perms: table of permissions 104 * @size: number of entries in @perms 105 * @trans: table of strings, index by x 106 * @tags: table of tags that perms->tag indexes 107 * @start:_states to start in for each class 108 * start: set of start states for the different classes of data 109 */ 110 struct aa_policydb { 111 struct kref count; 112 struct aa_dfa *dfa; 113 struct { 114 struct aa_perms *perms; 115 u32 size; 116 }; 117 struct aa_str_table trans; 118 struct aa_tags_struct tags; 119 aa_state_t start[AA_CLASS_LAST + 1]; 120 }; 121 122 extern struct aa_policydb *nullpdb; 123 124 void aa_destroy_tags(struct aa_tags_struct *tags); 125 struct aa_policydb *aa_alloc_pdb(gfp_t gfp); 126 void aa_pdb_free_kref(struct kref *kref); 127 128 /** 129 * aa_get_pdb - increment refcount on @pdb 130 * @pdb: policydb (MAYBE NULL) 131 * 132 * Returns: pointer to @pdb if @pdb is NULL will return NULL 133 * Requires: @pdb must be held with valid refcount when called 134 */ 135 static inline struct aa_policydb *aa_get_pdb(struct aa_policydb *pdb) 136 { 137 if (pdb) 138 kref_get(&(pdb->count)); 139 140 return pdb; 141 } 142 143 /** 144 * aa_put_pdb - put a pdb refcount 145 * @pdb: pdb to put refcount (MAYBE NULL) 146 * 147 * Requires: if @pdb != NULL that a valid refcount be held 148 */ 149 static inline void aa_put_pdb(struct aa_policydb *pdb) 150 { 151 if (pdb) 152 kref_put(&pdb->count, aa_pdb_free_kref); 153 } 154 155 /* lookup perm that doesn't have and object conditional */ 156 static inline struct aa_perms *aa_lookup_perms(struct aa_policydb *policy, 157 aa_state_t state) 158 { 159 unsigned int index = ACCEPT_TABLE(policy->dfa)[state]; 160 161 if (!(policy->perms)) 162 return &default_perms; 163 164 return &(policy->perms[index]); 165 } 166 167 /* struct aa_data - generic data structure 168 * key: name for retrieving this data 169 * size: size of data in bytes 170 * data: binary data 171 * head: reserved for rhashtable 172 */ 173 struct aa_data { 174 char *key; 175 u32 size; 176 char *data; 177 struct rhash_head head; 178 }; 179 180 /* struct aa_ruleset - data covering mediation rules 181 * @list: list the rule is on 182 * @size: the memory consumed by this ruleset 183 * @policy: general match rules governing policy 184 * @file: The set of rules governing basic file access and domain transitions 185 * @caps: capabilities for the profile 186 * @rlimits: rlimits for the profile 187 * @secmark_count: number of secmark entries 188 * @secmark: secmark label match info 189 */ 190 struct aa_ruleset { 191 int size; 192 193 /* TODO: merge policy and file */ 194 struct aa_policydb *policy; 195 struct aa_policydb *file; 196 struct aa_caps caps; 197 198 struct aa_rlimit rlimits; 199 200 int secmark_count; 201 struct aa_secmark *secmark; 202 }; 203 204 205 /* struct aa_attachment - data and rules for a profiles attachment 206 * @list: 207 * @xmatch_str: human readable attachment string 208 * @xmatch: optional extended matching for unconfined executables names 209 * @xmatch_len: xmatch prefix len, used to determine xmatch priority 210 * @xattr_count: number of xattrs in table 211 * @xattrs: table of xattrs 212 */ 213 struct aa_attachment { 214 const char *xmatch_str; 215 struct aa_policydb *xmatch; 216 unsigned int xmatch_len; 217 int xattr_count; 218 char **xattrs; 219 }; 220 221 /* struct aa_profile - basic confinement data 222 * @base - base components of the profile (name, refcount, lists, lock ...) 223 * @parent: parent of profile 224 * @ns: namespace the profile is in 225 * @rename: optional profile name that this profile renamed 226 * 227 * @audit: the auditing mode of the profile 228 * @mode: the enforcement mode of the profile 229 * @path_flags: flags controlling path generation behavior 230 * @signal: the signal that should be used when kill is used 231 * @disconnected: what to prepend if attach_disconnected is specified 232 * @attach: attachment rules for the profile 233 * @rules: rules to be enforced 234 * 235 * learning_cache: the accesses learned in complain mode 236 * raw_data: rawdata of the loaded profile policy 237 * hash: cryptographic hash of the profile 238 * @dents: dentries for the profiles file entries in apparmorfs 239 * @dirname: name of the profile dir in apparmorfs 240 * @dents: set of dentries associated with the profile 241 * @data: hashtable for free-form policy aa_data 242 * @label - label this profile is an extension of 243 * @rules - label with the rule vec on its end 244 * 245 * The AppArmor profile contains the basic confinement data. Each profile 246 * has a name, and exists in a namespace. The @name and @exec_match are 247 * used to determine profile attachment against unconfined tasks. All other 248 * attachments are determined by profile X transition rules. 249 * 250 * Profiles have a hierarchy where hats and children profiles keep 251 * a reference to their parent. 252 * 253 * Profile names can not begin with a : and can not contain the \0 254 * character. If a profile name begins with / it will be considered when 255 * determining profile attachment on "unconfined" tasks. 256 */ 257 struct aa_profile { 258 struct aa_policy base; 259 struct aa_profile __rcu *parent; 260 261 struct aa_ns *ns; 262 const char *rename; 263 264 enum audit_mode audit; 265 long mode; 266 u32 path_flags; 267 int signal; 268 const char *disconnected; 269 270 struct aa_attachment attach; 271 272 struct aa_loaddata *rawdata; 273 unsigned char *hash; 274 char *dirname; 275 struct dentry *dents[AAFS_PROF_SIZEOF]; 276 struct rhashtable *data; 277 278 int n_rules; 279 /* special - variable length must be last entry in profile */ 280 struct aa_label label; 281 }; 282 283 extern enum profile_mode aa_g_profile_mode; 284 285 #define AA_MAY_LOAD_POLICY AA_MAY_APPEND 286 #define AA_MAY_REPLACE_POLICY AA_MAY_WRITE 287 #define AA_MAY_REMOVE_POLICY AA_MAY_DELETE 288 289 #define profiles_ns(P) ((P)->ns) 290 #define name_is_shared(A, B) ((A)->hname && (A)->hname == (B)->hname) 291 292 struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp); 293 struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy, 294 gfp_t gfp); 295 struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name, 296 gfp_t gfp); 297 struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat, 298 const char *base, gfp_t gfp); 299 void aa_free_profile(struct aa_profile *profile); 300 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name); 301 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname, 302 size_t n); 303 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base, 304 const char *fqname, size_t n); 305 306 ssize_t aa_replace_profiles(struct aa_ns *view, struct aa_label *label, 307 u32 mask, struct aa_loaddata *udata, 308 char *compressed_profile, size_t compressed_size); 309 ssize_t aa_remove_profiles(struct aa_ns *view, struct aa_label *label, 310 char *name, size_t size); 311 void __aa_profile_list_release(struct list_head *head); 312 313 #define profile_unconfined(X) ((X)->mode == APPARMOR_UNCONFINED) 314 315 /** 316 * aa_get_newest_profile - simple wrapper fn to wrap the label version 317 * @p: profile (NOT NULL) 318 * 319 * Returns refcount to newest version of the profile (maybe @p) 320 * 321 * Requires: @p must be held with a valid refcount 322 */ 323 static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p) 324 { 325 return labels_profile(aa_get_newest_label(&p->label)); 326 } 327 328 static inline aa_state_t RULE_MEDIATES(struct aa_ruleset *rules, 329 unsigned char class) 330 { 331 if (class <= AA_CLASS_LAST) 332 return rules->policy->start[class]; 333 else 334 return aa_dfa_match_len(rules->policy->dfa, 335 rules->policy->start[0], &class, 1); 336 } 337 338 static inline aa_state_t RULE_MEDIATES_v9NET(struct aa_ruleset *rules) 339 { 340 return RULE_MEDIATES(rules, AA_CLASS_NETV9); 341 } 342 343 static inline aa_state_t RULE_MEDIATES_NET(struct aa_ruleset *rules) 344 { 345 /* can not use RULE_MEDIATE_v9AF here, because AF match fail 346 * can not be distiguished from class match fail, and we only 347 * fallback to checking older class on class match failure 348 */ 349 aa_state_t state = RULE_MEDIATES(rules, AA_CLASS_NETV9); 350 351 /* fallback and check v7/8 if v9 is NOT mediated */ 352 if (!state) 353 state = RULE_MEDIATES(rules, AA_CLASS_NET); 354 355 return state; 356 } 357 358 359 void aa_compute_profile_mediates(struct aa_profile *profile); 360 static inline bool profile_mediates(struct aa_profile *profile, 361 unsigned char class) 362 { 363 return label_mediates(&profile->label, class); 364 } 365 366 static inline bool profile_mediates_safe(struct aa_profile *profile, 367 unsigned char class) 368 { 369 return label_mediates_safe(&profile->label, class); 370 } 371 372 /** 373 * aa_get_profile - increment refcount on profile @p 374 * @p: profile (MAYBE NULL) 375 * 376 * Returns: pointer to @p if @p is NULL will return NULL 377 * Requires: @p must be held with valid refcount when called 378 */ 379 static inline struct aa_profile *aa_get_profile(struct aa_profile *p) 380 { 381 if (p) 382 kref_get(&(p->label.count.count)); 383 384 return p; 385 } 386 387 /** 388 * aa_get_profile_not0 - increment refcount on profile @p found via lookup 389 * @p: profile (MAYBE NULL) 390 * 391 * Returns: pointer to @p if @p is NULL will return NULL 392 * Requires: @p must be held with valid refcount when called 393 */ 394 static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p) 395 { 396 if (p && kref_get_unless_zero(&p->label.count.count)) 397 return p; 398 399 return NULL; 400 } 401 402 /** 403 * aa_get_profile_rcu - increment a refcount profile that can be replaced 404 * @p: pointer to profile that can be replaced (NOT NULL) 405 * 406 * Returns: pointer to a refcounted profile. 407 * else NULL if no profile 408 */ 409 static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p) 410 { 411 struct aa_profile *c; 412 413 rcu_read_lock(); 414 do { 415 c = rcu_dereference(*p); 416 } while (c && !kref_get_unless_zero(&c->label.count.count)); 417 rcu_read_unlock(); 418 419 return c; 420 } 421 422 /** 423 * aa_put_profile - decrement refcount on profile @p 424 * @p: profile (MAYBE NULL) 425 */ 426 static inline void aa_put_profile(struct aa_profile *p) 427 { 428 if (p) 429 kref_put(&p->label.count.count, aa_label_kref); 430 } 431 432 static inline int AUDIT_MODE(struct aa_profile *profile) 433 { 434 if (aa_g_audit != AUDIT_NORMAL) 435 return aa_g_audit; 436 437 return profile->audit; 438 } 439 440 bool aa_policy_view_capable(const struct cred *subj_cred, 441 struct aa_label *label, struct aa_ns *ns); 442 bool aa_policy_admin_capable(const struct cred *subj_cred, 443 struct aa_label *label, struct aa_ns *ns); 444 int aa_may_manage_policy(const struct cred *subj_cred, 445 struct aa_label *label, struct aa_ns *ns, 446 const struct cred *ocred, u32 mask); 447 bool aa_current_policy_view_capable(struct aa_ns *ns); 448 bool aa_current_policy_admin_capable(struct aa_ns *ns); 449 450 #endif /* __AA_POLICY_H */ 451