1c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/*
2c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * AppArmor security module
3c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
4c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * This file contains AppArmor policy definitions.
5c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
6c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Copyright (C) 1998-2008 Novell/SUSE
7c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Copyright 2009-2010 Canonical Ltd.
8c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
9c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * This program is free software; you can redistribute it and/or
10c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * modify it under the terms of the GNU General Public License as
11c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * published by the Free Software Foundation, version 2 of the
12c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * License.
13c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
14c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
15c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#ifndef __AA_POLICY_H
16c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define __AA_POLICY_H
17c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
18c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/capability.h>
19c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/cred.h>
20c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/kref.h>
21c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/sched.h>
22c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/slab.h>
23c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include <linux/socket.h>
24c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
25c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "apparmor.h"
26c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "audit.h"
27c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "capability.h"
28c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "domain.h"
29c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "file.h"
30c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#include "resource.h"
31c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
320d259f043f5f60f74c4fd020aac190cb6450e918John Johansenextern const char *const aa_profile_mode_names[];
330d259f043f5f60f74c4fd020aac190cb6450e918John Johansen#define APPARMOR_MODE_NAMES_MAX_INDEX 4
34c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
3550c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen#define PROFILE_MODE(_profile, _mode)		\
3650c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen	((aa_g_profile_mode == (_mode)) ||	\
3750c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen	 ((_profile)->mode == (_mode)))
38c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
3950c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen#define COMPLAIN_MODE(_profile)	PROFILE_MODE((_profile), APPARMOR_COMPLAIN)
4050c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen
4150c5ecd5d8ffb0e549676b8fd9781e3b2fd751a0John Johansen#define KILL_MODE(_profile) PROFILE_MODE((_profile), APPARMOR_KILL)
42c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
43c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROFILE_IS_HAT(_profile) ((_profile)->flags & PFLAG_HAT)
44c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
4577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen#define PROFILE_INVALID(_profile) ((_profile)->flags & PFLAG_INVALID)
4677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
4701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen#define on_list_rcu(X) (!list_empty(X) && (X)->prev != LIST_POISON2)
4801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
49c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/*
50c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * FIXME: currently need a clean way to replace and remove profiles as a
51c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * set.  It should be done at the namespace level.
52c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Either, with a set of profiles loaded at the namespace level or via
53c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * a mark and remove marked interface.
54c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
55c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenenum profile_mode {
56c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	APPARMOR_ENFORCE,	/* enforce access rules */
57c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	APPARMOR_COMPLAIN,	/* allow and log access violations */
58c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	APPARMOR_KILL,		/* kill task on access violation */
59038165070aa55375d4bdd2f84b34a486feca63d6John Johansen	APPARMOR_UNCONFINED,	/* profile set to unconfined */
60c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
61c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
62c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenenum profile_flags {
63c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_HAT = 1,			/* profile is a hat */
64c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_NULL = 4,			/* profile is null learning profile */
65c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_IX_ON_NAME_ERROR = 8,	/* fallback to ix on name lookup fail */
66c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_IMMUTABLE = 0x10,		/* don't allow changes/replacement */
67c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_USER_DEFINED = 0x20,	/* user based profile - lower privs */
68c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_NO_LIST_REF = 0x40,	/* list doesn't keep profile ref */
69c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_OLD_NULL_TRANS = 0x100,	/* use // as the null transition */
7077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	PFLAG_INVALID = 0x200,		/* profile replaced/removed */
71fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	PFLAG_NS_COUNT = 0x400,		/* carries NS ref count */
72c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
73c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	/* These flags must correspond with PATH_flags */
74c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */
75c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
76c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
77c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile;
78c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
79c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_policy - common part of both namespaces and profiles
80c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @name: name of the object
81c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @hname - The hierarchical name
82c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @list: list policy object is on
83c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @profiles: head of the profiles list contained in the object
84c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
85c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_policy {
86c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	char *name;
87c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	char *hname;
88c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head list;
89c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head profiles;
90c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
91c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
92c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_ns_acct - accounting of profiles in namespace
93c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_size: maximum space allowed for all profiles in namespace
94c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_count: maximum number of profiles that can be in this namespace
95c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: current size of profiles
96c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @count: current count of profiles (includes null profiles)
97c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
98c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_ns_acct {
99c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int max_size;
100c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int max_count;
101c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int size;
102c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int count;
103c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
104c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
105c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_namespace - namespace for a set of profiles
106c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base: common policy
107c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of namespace
108c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @lock: lock for modifying the object
109c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @acct: accounting for the namespace
110c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @unconfined: special unconfined profile for the namespace
111c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @sub_ns: list of namespaces under the current namespace.
112a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen * @uniq_null: uniq value used for null learning profiles
1130d259f043f5f60f74c4fd020aac190cb6450e918John Johansen * @uniq_id: a unique id count for the profiles in the namespace
1140d259f043f5f60f74c4fd020aac190cb6450e918John Johansen * @dents: dentries for the namespaces file entries in apparmorfs
115c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
116c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * An aa_namespace defines the set profiles that are searched to determine
117c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * which profile to attach to a task.  Profiles can not be shared between
118c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_namespaces and profile names within a namespace are guaranteed to be
119c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * unique.  When profiles in separate namespaces have the same name they
120c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * are NOT considered to be equivalent.
121c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
122c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespaces are hierarchical and only namespaces and profiles below the
123c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * current namespace are visible.
124c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
125c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespace names must be unique and can not contain the characters :/\0
126c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
127c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * FIXME TODO: add vserver support of namespaces (can it all be done in
128c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *             userspace?)
129c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
130c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace {
131c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_policy base;
132c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_namespace *parent;
13301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct mutex lock;
134c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_ns_acct acct;
135c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_profile *unconfined;
136c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head sub_ns;
137a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen	atomic_t uniq_null;
1380d259f043f5f60f74c4fd020aac190cb6450e918John Johansen	long uniq_id;
1390d259f043f5f60f74c4fd020aac190cb6450e918John Johansen
1400d259f043f5f60f74c4fd020aac190cb6450e918John Johansen	struct dentry *dents[AAFS_NS_SIZEOF];
141c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
142c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
143ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen/* struct aa_policydb - match engine for a policy
144ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * dfa: dfa pattern match
145ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * start: set of start states for the different classes of data
146ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen */
147ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansenstruct aa_policydb {
148ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	/* Generic policy DFA specific rule types will be subsections of it */
149ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	struct aa_dfa *dfa;
150ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	unsigned int start[AA_CLASS_LAST + 1];
151ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
152ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen};
153ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
15477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstruct aa_replacedby {
15577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct kref count;
15677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_profile __rcu *profile;
15777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen};
15877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
15977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
160c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_profile - basic confinement data
161c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base - base components of the profile (name, refcount, lists, lock ...)
162fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @count: reference count of the obj
163742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen * @rcu: rcu head used when removing from @list
164c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of profile
165c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @ns: namespace the profile is in
166c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @replacedby: is set to the profile that replaced this profile
167c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rename: optional profile name that this profile renamed
168556d0be74b19cb6288e5eb2f3216eac247d87968John Johansen * @attach: human readable attachment string
169c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch: optional extended matching for unconfined executables names
170c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch_len: xmatch prefix len, used to determine xmatch priority
171c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @audit: the auditing mode of the profile
172c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @mode: the enforcement mode of the profile
173c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @flags: flags controlling profile behavior
174c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @path_flags: flags controlling path generation behavior
175c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: the memory consumed by this profiles rules
176ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * @policy: general match rules governing policy
177c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @file: The set of rules governing basic file access and domain transitions
178c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @caps: capabilities for the profile
179c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rlimits: rlimits for the profile
180c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
1810d259f043f5f60f74c4fd020aac190cb6450e918John Johansen * @dents: dentries for the profiles file entries in apparmorfs
1820d259f043f5f60f74c4fd020aac190cb6450e918John Johansen * @dirname: name of the profile dir in apparmorfs
1830d259f043f5f60f74c4fd020aac190cb6450e918John Johansen *
184c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * The AppArmor profile contains the basic confinement data.  Each profile
185c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * has a name, and exists in a namespace.  The @name and @exec_match are
186c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * used to determine profile attachment against unconfined tasks.  All other
187c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * attachments are determined by profile X transition rules.
188c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
18977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * The @replacedby struct is write protected by the profile lock.
190c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
191c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profiles have a hierarchy where hats and children profiles keep
192c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * a reference to their parent.
193c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
194c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profile names can not begin with a : and can not contain the \0
195c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * character.  If a profile name begins with / it will be considered when
196c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * determining profile attachment on "unconfined" tasks.
197c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
198c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile {
199c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_policy base;
200fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	struct kref count;
201742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen	struct rcu_head rcu;
20201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct aa_profile __rcu *parent;
203c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
204c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_namespace *ns;
20577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_replacedby *replacedby;
206c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	const char *rename;
207c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
208556d0be74b19cb6288e5eb2f3216eac247d87968John Johansen	const char *attach;
209c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_dfa *xmatch;
210c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int xmatch_len;
211c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	enum audit_mode audit;
212038165070aa55375d4bdd2f84b34a486feca63d6John Johansen	long mode;
21377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	long flags;
214c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	u32 path_flags;
215c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int size;
216c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
217ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	struct aa_policydb policy;
218c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_file_rules file;
219c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_caps caps;
220c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_rlimit rlimits;
2210d259f043f5f60f74c4fd020aac190cb6450e918John Johansen
222f8eb8a1324e81927b2c64823b2fc38386efd3fefJohn Johansen	unsigned char *hash;
2230d259f043f5f60f74c4fd020aac190cb6450e918John Johansen	char *dirname;
2240d259f043f5f60f74c4fd020aac190cb6450e918John Johansen	struct dentry *dents[AAFS_PROF_SIZEOF];
225c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
226c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
227c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern struct aa_namespace *root_ns;
228c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern enum profile_mode aa_g_profile_mode;
229c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
230c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
231c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
232c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
233c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenconst char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
234c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenint aa_alloc_root_ns(void);
235c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_root_ns(void);
236c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_namespace_kref(struct kref *kref);
237c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
238c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace *aa_find_namespace(struct aa_namespace *root,
239c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen				       const char *name);
240c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
241c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
24277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenvoid aa_free_replacedby_kref(struct kref *kref);
243c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_alloc_profile(const char *name);
244c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
2458651e1d6572bc2c061073f05fabcd7175789259dJohn Johansenvoid aa_free_profile(struct aa_profile *profile);
246c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_profile_kref(struct kref *kref);
247c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
248c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
249c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
250c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
251c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
252c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_remove_profiles(char *name, size_t size);
253c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
254c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_ADD 1
255c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_REPLACE 0
256c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
257038165070aa55375d4bdd2f84b34a486feca63d6John Johansen#define unconfined(X) ((X)->mode == APPARMOR_UNCONFINED)
258c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
259c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
2600d259f043f5f60f74c4fd020aac190cb6450e918John Johansenstatic inline struct aa_profile *aa_deref_parent(struct aa_profile *p)
2610d259f043f5f60f74c4fd020aac190cb6450e918John Johansen{
2620d259f043f5f60f74c4fd020aac190cb6450e918John Johansen	return rcu_dereference_protected(p->parent,
2630d259f043f5f60f74c4fd020aac190cb6450e918John Johansen					 mutex_is_locked(&p->ns->lock));
2640d259f043f5f60f74c4fd020aac190cb6450e918John Johansen}
2650d259f043f5f60f74c4fd020aac190cb6450e918John Johansen
266c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/**
267c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_get_profile - increment refcount on profile @p
268c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile  (MAYBE NULL)
269c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
270c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Returns: pointer to @p if @p is NULL will return NULL
271c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Requires: @p must be held with valid refcount when called
272c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
273c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline struct aa_profile *aa_get_profile(struct aa_profile *p)
274c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
275c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	if (p)
276fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		kref_get(&(p->count));
277c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
278c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	return p;
279c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
280c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
281c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/**
28201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_not0 - increment refcount on profile @p found via lookup
28301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: profile  (MAYBE NULL)
28401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *
28501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to @p if @p is NULL will return NULL
28601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Requires: @p must be held with valid refcount when called
28701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */
28801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
28901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{
290fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (p && kref_get_not0(&p->count))
29101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen		return p;
29201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
29301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	return NULL;
29401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen}
29501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
29601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/**
29701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_rcu - increment a refcount profile that can be replaced
29801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: pointer to profile that can be replaced (NOT NULL)
29901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *
30001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to a refcounted profile.
30101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *     else NULL if no profile
30201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */
30301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
30401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{
30501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct aa_profile *c;
30601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
30701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	rcu_read_lock();
30801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	do {
30901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen		c = rcu_dereference(*p);
310fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	} while (c && !kref_get_not0(&c->count));
31101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	rcu_read_unlock();
31201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
31301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	return c;
31401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen}
31501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
31601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/**
31777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * aa_get_newest_profile - find the newest version of @profile
31877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * @profile: the profile to check for newer versions of
31977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *
32077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * Returns: refcounted newest version of @profile taking into account
32177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *          replacement, renames and removals
32277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *          return @profile.
32377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen */
32477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
32577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
32677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (!p)
32777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		return NULL;
32877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
32977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (PROFILE_INVALID(p))
33077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		return aa_get_profile_rcu(&p->replacedby->profile);
33177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	return aa_get_profile(p);
33377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
33477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/**
336c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_put_profile - decrement refcount on profile @p
337c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile  (MAYBE NULL)
338c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
339c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline void aa_put_profile(struct aa_profile *p)
340c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
341742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen	if (p)
342742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen		kref_put(&p->count, aa_free_profile_kref);
343c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
344c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
34577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
34677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
34777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
34877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_get(&(p->count));
34977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
35077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	return p;
35177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
35277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
35377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void aa_put_replacedby(struct aa_replacedby *p)
35477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
35577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
35677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_put(&p->count, aa_free_replacedby_kref);
35777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
35877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
35977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/* requires profile list write lock held */
36077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void __aa_update_replacedby(struct aa_profile *orig,
36177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen					  struct aa_profile *new)
36277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
3634cd4fc77032dca46fe7475d81461e29145db247aJohn Johansen	struct aa_profile *tmp;
3644cd4fc77032dca46fe7475d81461e29145db247aJohn Johansen	tmp = rcu_dereference_protected(orig->replacedby->profile,
3654cd4fc77032dca46fe7475d81461e29145db247aJohn Johansen					mutex_is_locked(&orig->ns->lock));
36677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
36777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	orig->flags |= PFLAG_INVALID;
36877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	aa_put_profile(tmp);
36977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
37077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
371fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
372fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_get_namespace - increment references count on @ns
373fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to increment reference count of (MAYBE NULL)
374fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
375fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Returns: pointer to @ns, if @ns is NULL returns NULL
376fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Requires: @ns must be held with valid refcount when called
377fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
378fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
379fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
380fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
381fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_get_profile(ns->unconfined);
382fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
383fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	return ns;
384fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
385fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
386fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
387fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_put_namespace - decrement refcount on @ns
388fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to put reference of
389fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
390fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Decrement reference count of @ns and if no longer in use free it
391fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
392fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline void aa_put_namespace(struct aa_namespace *ns)
393fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
394fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
395fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_put_profile(ns->unconfined);
396fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
397fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
398c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline int AUDIT_MODE(struct aa_profile *profile)
399c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
400c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	if (aa_g_audit != AUDIT_NORMAL)
401c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen		return aa_g_audit;
402c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
403c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	return profile->audit;
404c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
405c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
406c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_may_manage_policy(int op);
407c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
408c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#endif /* __AA_POLICY_H */
409