1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * AppArmor security module
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file contains AppArmor policy definitions.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 1998-2008 Novell/SUSE
8*4882a593Smuzhiyun * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #ifndef __AA_POLICY_H
12*4882a593Smuzhiyun #define __AA_POLICY_H
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/capability.h>
15*4882a593Smuzhiyun #include <linux/cred.h>
16*4882a593Smuzhiyun #include <linux/kref.h>
17*4882a593Smuzhiyun #include <linux/rhashtable.h>
18*4882a593Smuzhiyun #include <linux/sched.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/socket.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "apparmor.h"
23*4882a593Smuzhiyun #include "audit.h"
24*4882a593Smuzhiyun #include "capability.h"
25*4882a593Smuzhiyun #include "domain.h"
26*4882a593Smuzhiyun #include "file.h"
27*4882a593Smuzhiyun #include "lib.h"
28*4882a593Smuzhiyun #include "label.h"
29*4882a593Smuzhiyun #include "net.h"
30*4882a593Smuzhiyun #include "perms.h"
31*4882a593Smuzhiyun #include "resource.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun struct aa_ns;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun extern int unprivileged_userns_apparmor_policy;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun extern const char *const aa_profile_mode_names[];
39*4882a593Smuzhiyun #define APPARMOR_MODE_NAMES_MAX_INDEX 4
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #define PROFILE_MODE(_profile, _mode) \
42*4882a593Smuzhiyun ((aa_g_profile_mode == (_mode)) || \
43*4882a593Smuzhiyun ((_profile)->mode == (_mode)))
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define COMPLAIN_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define PROFILE_IS_HAT(_profile) ((_profile)->label.flags & FLAG_HAT)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #define profile_is_stale(_profile) (label_is_stale(&(_profile)->label))
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * FIXME: currently need a clean way to replace and remove profiles as a
57*4882a593Smuzhiyun * set. It should be done at the namespace level.
58*4882a593Smuzhiyun * Either, with a set of profiles loaded at the namespace level or via
59*4882a593Smuzhiyun * a mark and remove marked interface.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun enum profile_mode {
62*4882a593Smuzhiyun APPARMOR_ENFORCE, /* enforce access rules */
63*4882a593Smuzhiyun APPARMOR_COMPLAIN, /* allow and log access violations */
64*4882a593Smuzhiyun APPARMOR_KILL, /* kill task on access violation */
65*4882a593Smuzhiyun APPARMOR_UNCONFINED, /* profile set to unconfined */
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* struct aa_policydb - match engine for a policy
70*4882a593Smuzhiyun * dfa: dfa pattern match
71*4882a593Smuzhiyun * start: set of start states for the different classes of data
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct aa_policydb {
74*4882a593Smuzhiyun /* Generic policy DFA specific rule types will be subsections of it */
75*4882a593Smuzhiyun struct aa_dfa *dfa;
76*4882a593Smuzhiyun unsigned int start[AA_CLASS_LAST + 1];
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* struct aa_data - generic data structure
81*4882a593Smuzhiyun * key: name for retrieving this data
82*4882a593Smuzhiyun * size: size of data in bytes
83*4882a593Smuzhiyun * data: binary data
84*4882a593Smuzhiyun * head: reserved for rhashtable
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun struct aa_data {
87*4882a593Smuzhiyun char *key;
88*4882a593Smuzhiyun u32 size;
89*4882a593Smuzhiyun char *data;
90*4882a593Smuzhiyun struct rhash_head head;
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* struct aa_profile - basic confinement data
95*4882a593Smuzhiyun * @base - base components of the profile (name, refcount, lists, lock ...)
96*4882a593Smuzhiyun * @label - label this profile is an extension of
97*4882a593Smuzhiyun * @parent: parent of profile
98*4882a593Smuzhiyun * @ns: namespace the profile is in
99*4882a593Smuzhiyun * @rename: optional profile name that this profile renamed
100*4882a593Smuzhiyun * @attach: human readable attachment string
101*4882a593Smuzhiyun * @xmatch: optional extended matching for unconfined executables names
102*4882a593Smuzhiyun * @xmatch_len: xmatch prefix len, used to determine xmatch priority
103*4882a593Smuzhiyun * @audit: the auditing mode of the profile
104*4882a593Smuzhiyun * @mode: the enforcement mode of the profile
105*4882a593Smuzhiyun * @path_flags: flags controlling path generation behavior
106*4882a593Smuzhiyun * @disconnected: what to prepend if attach_disconnected is specified
107*4882a593Smuzhiyun * @size: the memory consumed by this profiles rules
108*4882a593Smuzhiyun * @policy: general match rules governing policy
109*4882a593Smuzhiyun * @file: The set of rules governing basic file access and domain transitions
110*4882a593Smuzhiyun * @caps: capabilities for the profile
111*4882a593Smuzhiyun * @rlimits: rlimits for the profile
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * @dents: dentries for the profiles file entries in apparmorfs
114*4882a593Smuzhiyun * @dirname: name of the profile dir in apparmorfs
115*4882a593Smuzhiyun * @data: hashtable for free-form policy aa_data
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * The AppArmor profile contains the basic confinement data. Each profile
118*4882a593Smuzhiyun * has a name, and exists in a namespace. The @name and @exec_match are
119*4882a593Smuzhiyun * used to determine profile attachment against unconfined tasks. All other
120*4882a593Smuzhiyun * attachments are determined by profile X transition rules.
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * Profiles have a hierarchy where hats and children profiles keep
123*4882a593Smuzhiyun * a reference to their parent.
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * Profile names can not begin with a : and can not contain the \0
126*4882a593Smuzhiyun * character. If a profile name begins with / it will be considered when
127*4882a593Smuzhiyun * determining profile attachment on "unconfined" tasks.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun struct aa_profile {
130*4882a593Smuzhiyun struct aa_policy base;
131*4882a593Smuzhiyun struct aa_profile __rcu *parent;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun struct aa_ns *ns;
134*4882a593Smuzhiyun const char *rename;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun const char *attach;
137*4882a593Smuzhiyun struct aa_dfa *xmatch;
138*4882a593Smuzhiyun unsigned int xmatch_len;
139*4882a593Smuzhiyun enum audit_mode audit;
140*4882a593Smuzhiyun long mode;
141*4882a593Smuzhiyun u32 path_flags;
142*4882a593Smuzhiyun const char *disconnected;
143*4882a593Smuzhiyun int size;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun struct aa_policydb policy;
146*4882a593Smuzhiyun struct aa_file_rules file;
147*4882a593Smuzhiyun struct aa_caps caps;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun int xattr_count;
150*4882a593Smuzhiyun char **xattrs;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun struct aa_rlimit rlimits;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun int secmark_count;
155*4882a593Smuzhiyun struct aa_secmark *secmark;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun struct aa_loaddata *rawdata;
158*4882a593Smuzhiyun unsigned char *hash;
159*4882a593Smuzhiyun char *dirname;
160*4882a593Smuzhiyun struct dentry *dents[AAFS_PROF_SIZEOF];
161*4882a593Smuzhiyun struct rhashtable *data;
162*4882a593Smuzhiyun struct aa_label label;
163*4882a593Smuzhiyun };
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun extern enum profile_mode aa_g_profile_mode;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun #define AA_MAY_LOAD_POLICY AA_MAY_APPEND
168*4882a593Smuzhiyun #define AA_MAY_REPLACE_POLICY AA_MAY_WRITE
169*4882a593Smuzhiyun #define AA_MAY_REMOVE_POLICY AA_MAY_DELETE
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun #define profiles_ns(P) ((P)->ns)
172*4882a593Smuzhiyun #define name_is_shared(A, B) ((A)->hname && (A)->hname == (B)->hname)
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun void aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun void aa_free_proxy_kref(struct kref *kref);
178*4882a593Smuzhiyun struct aa_profile *aa_alloc_profile(const char *name, struct aa_proxy *proxy,
179*4882a593Smuzhiyun gfp_t gfp);
180*4882a593Smuzhiyun struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
181*4882a593Smuzhiyun const char *base, gfp_t gfp);
182*4882a593Smuzhiyun void aa_free_profile(struct aa_profile *profile);
183*4882a593Smuzhiyun void aa_free_profile_kref(struct kref *kref);
184*4882a593Smuzhiyun struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
185*4882a593Smuzhiyun struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
186*4882a593Smuzhiyun size_t n);
187*4882a593Smuzhiyun struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *name);
188*4882a593Smuzhiyun struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
189*4882a593Smuzhiyun const char *fqname, size_t n);
190*4882a593Smuzhiyun struct aa_profile *aa_match_profile(struct aa_ns *ns, const char *name);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun ssize_t aa_replace_profiles(struct aa_ns *view, struct aa_label *label,
193*4882a593Smuzhiyun u32 mask, struct aa_loaddata *udata);
194*4882a593Smuzhiyun ssize_t aa_remove_profiles(struct aa_ns *view, struct aa_label *label,
195*4882a593Smuzhiyun char *name, size_t size);
196*4882a593Smuzhiyun void __aa_profile_list_release(struct list_head *head);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #define PROF_ADD 1
199*4882a593Smuzhiyun #define PROF_REPLACE 0
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #define profile_unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun * aa_get_newest_profile - simple wrapper fn to wrap the label version
205*4882a593Smuzhiyun * @p: profile (NOT NULL)
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * Returns refcount to newest version of the profile (maybe @p)
208*4882a593Smuzhiyun *
209*4882a593Smuzhiyun * Requires: @p must be held with a valid refcount
210*4882a593Smuzhiyun */
aa_get_newest_profile(struct aa_profile * p)211*4882a593Smuzhiyun static inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun return labels_profile(aa_get_newest_label(&p->label));
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
PROFILE_MEDIATES(struct aa_profile * profile,unsigned char class)216*4882a593Smuzhiyun static inline unsigned int PROFILE_MEDIATES(struct aa_profile *profile,
217*4882a593Smuzhiyun unsigned char class)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun if (class <= AA_CLASS_LAST)
220*4882a593Smuzhiyun return profile->policy.start[class];
221*4882a593Smuzhiyun else
222*4882a593Smuzhiyun return aa_dfa_match_len(profile->policy.dfa,
223*4882a593Smuzhiyun profile->policy.start[0], &class, 1);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
PROFILE_MEDIATES_AF(struct aa_profile * profile,u16 AF)226*4882a593Smuzhiyun static inline unsigned int PROFILE_MEDIATES_AF(struct aa_profile *profile,
227*4882a593Smuzhiyun u16 AF) {
228*4882a593Smuzhiyun unsigned int state = PROFILE_MEDIATES(profile, AA_CLASS_NET);
229*4882a593Smuzhiyun __be16 be_af = cpu_to_be16(AF);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun if (!state)
232*4882a593Smuzhiyun return 0;
233*4882a593Smuzhiyun return aa_dfa_match_len(profile->policy.dfa, state, (char *) &be_af, 2);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun * aa_get_profile - increment refcount on profile @p
238*4882a593Smuzhiyun * @p: profile (MAYBE NULL)
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * Returns: pointer to @p if @p is NULL will return NULL
241*4882a593Smuzhiyun * Requires: @p must be held with valid refcount when called
242*4882a593Smuzhiyun */
aa_get_profile(struct aa_profile * p)243*4882a593Smuzhiyun static inline struct aa_profile *aa_get_profile(struct aa_profile *p)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun if (p)
246*4882a593Smuzhiyun kref_get(&(p->label.count));
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun return p;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun * aa_get_profile_not0 - increment refcount on profile @p found via lookup
253*4882a593Smuzhiyun * @p: profile (MAYBE NULL)
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * Returns: pointer to @p if @p is NULL will return NULL
256*4882a593Smuzhiyun * Requires: @p must be held with valid refcount when called
257*4882a593Smuzhiyun */
aa_get_profile_not0(struct aa_profile * p)258*4882a593Smuzhiyun static inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun if (p && kref_get_unless_zero(&p->label.count))
261*4882a593Smuzhiyun return p;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return NULL;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun * aa_get_profile_rcu - increment a refcount profile that can be replaced
268*4882a593Smuzhiyun * @p: pointer to profile that can be replaced (NOT NULL)
269*4882a593Smuzhiyun *
270*4882a593Smuzhiyun * Returns: pointer to a refcounted profile.
271*4882a593Smuzhiyun * else NULL if no profile
272*4882a593Smuzhiyun */
aa_get_profile_rcu(struct aa_profile __rcu ** p)273*4882a593Smuzhiyun static inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun struct aa_profile *c;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun rcu_read_lock();
278*4882a593Smuzhiyun do {
279*4882a593Smuzhiyun c = rcu_dereference(*p);
280*4882a593Smuzhiyun } while (c && !kref_get_unless_zero(&c->label.count));
281*4882a593Smuzhiyun rcu_read_unlock();
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun return c;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun /**
287*4882a593Smuzhiyun * aa_put_profile - decrement refcount on profile @p
288*4882a593Smuzhiyun * @p: profile (MAYBE NULL)
289*4882a593Smuzhiyun */
aa_put_profile(struct aa_profile * p)290*4882a593Smuzhiyun static inline void aa_put_profile(struct aa_profile *p)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun if (p)
293*4882a593Smuzhiyun kref_put(&p->label.count, aa_label_kref);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
AUDIT_MODE(struct aa_profile * profile)296*4882a593Smuzhiyun static inline int AUDIT_MODE(struct aa_profile *profile)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun if (aa_g_audit != AUDIT_NORMAL)
299*4882a593Smuzhiyun return aa_g_audit;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun return profile->audit;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun bool policy_view_capable(struct aa_ns *ns);
305*4882a593Smuzhiyun bool policy_admin_capable(struct aa_ns *ns);
306*4882a593Smuzhiyun int aa_may_manage_policy(struct aa_label *label, struct aa_ns *ns,
307*4882a593Smuzhiyun u32 mask);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun #endif /* __AA_POLICY_H */
310