policy.h revision 742058b0f3a2ed32e2a7349aff97989dc4e32452
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
322d4cee7e3a2b9f9c3237672cc136e20dbad0e2ceJan Engelhardtextern const char *const profile_mode_names[];
33c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define APPARMOR_NAMES_MAX_INDEX 3
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 */
59c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
60c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
61c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenenum profile_flags {
62c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_HAT = 1,			/* profile is a hat */
63c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	PFLAG_UNCONFINED = 2,		/* profile is an unconfined profile */
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
113c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
114c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * An aa_namespace defines the set profiles that are searched to determine
115c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * which profile to attach to a task.  Profiles can not be shared between
116c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_namespaces and profile names within a namespace are guaranteed to be
117c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * unique.  When profiles in separate namespaces have the same name they
118c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * are NOT considered to be equivalent.
119c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
120c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespaces are hierarchical and only namespaces and profiles below the
121c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * current namespace are visible.
122c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
123c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespace names must be unique and can not contain the characters :/\0
124c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
125c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * FIXME TODO: add vserver support of namespaces (can it all be done in
126c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *             userspace?)
127c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
128c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace {
129c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_policy base;
130c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_namespace *parent;
13101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct mutex lock;
132c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_ns_acct acct;
133c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_profile *unconfined;
134c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head sub_ns;
135a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen	atomic_t uniq_null;
136c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
137c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
138ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen/* struct aa_policydb - match engine for a policy
139ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * dfa: dfa pattern match
140ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * start: set of start states for the different classes of data
141ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen */
142ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansenstruct aa_policydb {
143ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	/* Generic policy DFA specific rule types will be subsections of it */
144ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	struct aa_dfa *dfa;
145ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	unsigned int start[AA_CLASS_LAST + 1];
146ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
147ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen};
148ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
14977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstruct aa_replacedby {
15077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct kref count;
15177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_profile __rcu *profile;
15277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen};
15377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
15477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
155c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_profile - basic confinement data
156c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base - base components of the profile (name, refcount, lists, lock ...)
157fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @count: reference count of the obj
158742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen * @rcu: rcu head used when removing from @list
159c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of profile
160c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @ns: namespace the profile is in
161c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @replacedby: is set to the profile that replaced this profile
162c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rename: optional profile name that this profile renamed
163c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch: optional extended matching for unconfined executables names
164c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch_len: xmatch prefix len, used to determine xmatch priority
165c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @audit: the auditing mode of the profile
166c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @mode: the enforcement mode of the profile
167c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @flags: flags controlling profile behavior
168c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @path_flags: flags controlling path generation behavior
169c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: the memory consumed by this profiles rules
170ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * @policy: general match rules governing policy
171c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @file: The set of rules governing basic file access and domain transitions
172c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @caps: capabilities for the profile
173c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rlimits: rlimits for the profile
174c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
175c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * The AppArmor profile contains the basic confinement data.  Each profile
176c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * has a name, and exists in a namespace.  The @name and @exec_match are
177c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * used to determine profile attachment against unconfined tasks.  All other
178c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * attachments are determined by profile X transition rules.
179c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
18077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * The @replacedby struct is write protected by the profile lock.
181c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
182c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profiles have a hierarchy where hats and children profiles keep
183c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * a reference to their parent.
184c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
185c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profile names can not begin with a : and can not contain the \0
186c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * character.  If a profile name begins with / it will be considered when
187c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * determining profile attachment on "unconfined" tasks.
188c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
189c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile {
190c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_policy base;
191fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	struct kref count;
192742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen	struct rcu_head rcu;
19301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct aa_profile __rcu *parent;
194c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
195c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_namespace *ns;
19677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_replacedby *replacedby;
197c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	const char *rename;
198c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
199c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_dfa *xmatch;
200c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int xmatch_len;
201c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	enum audit_mode audit;
202c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	enum profile_mode mode;
20377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	long flags;
204c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	u32 path_flags;
205c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int size;
206c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
207ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	struct aa_policydb policy;
208c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_file_rules file;
209c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_caps caps;
210c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_rlimit rlimits;
211c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
212c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
213c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern struct aa_namespace *root_ns;
214c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern enum profile_mode aa_g_profile_mode;
215c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
216c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_add_profile(struct aa_policy *common, struct aa_profile *profile);
217c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
218c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view);
219c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenconst char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child);
220c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenint aa_alloc_root_ns(void);
221c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_root_ns(void);
222c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_namespace_kref(struct kref *kref);
223c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
224c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace *aa_find_namespace(struct aa_namespace *root,
225c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen				       const char *name);
226c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
227c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
22877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenvoid aa_free_replacedby_kref(struct kref *kref);
229c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_alloc_profile(const char *name);
230c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat);
231c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_profile_kref(struct kref *kref);
232c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_find_child(struct aa_profile *parent, const char *name);
233c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name);
234c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name);
235c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
236c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace);
237c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_remove_profiles(char *name, size_t size);
238c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
239c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_ADD 1
240c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_REPLACE 0
241c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
242c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define unconfined(X) ((X)->flags & PFLAG_UNCONFINED)
243c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
244c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
245c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/**
246c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_get_profile - increment refcount on profile @p
247c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile  (MAYBE NULL)
248c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
249c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Returns: pointer to @p if @p is NULL will return NULL
250c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Requires: @p must be held with valid refcount when called
251c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
252c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline struct aa_profile *aa_get_profile(struct aa_profile *p)
253c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
254c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	if (p)
255fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		kref_get(&(p->count));
256c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
257c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	return p;
258c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
259c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
260c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/**
26101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_not0 - increment refcount on profile @p found via lookup
26201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: profile  (MAYBE NULL)
26301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *
26401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to @p if @p is NULL will return NULL
26501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Requires: @p must be held with valid refcount when called
26601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */
26701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p)
26801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{
269fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (p && kref_get_not0(&p->count))
27001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen		return p;
27101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
27201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	return NULL;
27301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen}
27401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
27501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/**
27601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_rcu - increment a refcount profile that can be replaced
27701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: pointer to profile that can be replaced (NOT NULL)
27801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *
27901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to a refcounted profile.
28001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen *     else NULL if no profile
28101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */
28201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p)
28301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{
28401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct aa_profile *c;
28501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
28601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	rcu_read_lock();
28701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	do {
28801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen		c = rcu_dereference(*p);
289fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	} while (c && !kref_get_not0(&c->count));
29001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	rcu_read_unlock();
29101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
29201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	return c;
29301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen}
29401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen
29501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/**
29677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * aa_get_newest_profile - find the newest version of @profile
29777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * @profile: the profile to check for newer versions of
29877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *
29977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * Returns: refcounted newest version of @profile taking into account
30077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *          replacement, renames and removals
30177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen *          return @profile.
30277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen */
30377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p)
30477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
30577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (!p)
30677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		return NULL;
30777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
30877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (PROFILE_INVALID(p))
30977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		return aa_get_profile_rcu(&p->replacedby->profile);
31077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
31177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	return aa_get_profile(p);
31277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
31377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
31477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/**
315c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_put_profile - decrement refcount on profile @p
316c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile  (MAYBE NULL)
317c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
318c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline void aa_put_profile(struct aa_profile *p)
319c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
320742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen	if (p)
321742058b0f3a2ed32e2a7349aff97989dc4e32452John Johansen		kref_put(&p->count, aa_free_profile_kref);
322c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
323c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
32477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
32577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
32677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
32777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_get(&(p->count));
32877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
32977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	return p;
33077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
33177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void aa_put_replacedby(struct aa_replacedby *p)
33377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
33477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
33577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_put(&p->count, aa_free_replacedby_kref);
33677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
33777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/* requires profile list write lock held */
33977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void __aa_update_replacedby(struct aa_profile *orig,
34077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen					  struct aa_profile *new)
34177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
34277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_profile *tmp = rcu_dereference(orig->replacedby->profile);
34377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
34477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	orig->flags |= PFLAG_INVALID;
34577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	aa_put_profile(tmp);
34677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
34777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
348fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
349fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_get_namespace - increment references count on @ns
350fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to increment reference count of (MAYBE NULL)
351fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
352fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Returns: pointer to @ns, if @ns is NULL returns NULL
353fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Requires: @ns must be held with valid refcount when called
354fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
355fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
356fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
357fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
358fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_get_profile(ns->unconfined);
359fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
360fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	return ns;
361fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
362fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
363fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
364fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_put_namespace - decrement refcount on @ns
365fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to put reference of
366fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
367fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Decrement reference count of @ns and if no longer in use free it
368fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
369fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline void aa_put_namespace(struct aa_namespace *ns)
370fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
371fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
372fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_put_profile(ns->unconfined);
373fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
374fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
375c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline int AUDIT_MODE(struct aa_profile *profile)
376c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
377c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	if (aa_g_audit != AUDIT_NORMAL)
378c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen		return aa_g_audit;
379c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
380c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	return profile->audit;
381c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
382c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
383c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_may_manage_policy(int op);
384c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
385c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#endif /* __AA_POLICY_H */
386