policy.h revision fa2ac468db510c653499a47c1ec3deb045bf4763
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
8301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @rcu: rcu head used when removing from @list
84c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @profiles: head of the profiles list contained in the object
85c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
86c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_policy {
87c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	char *name;
88c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	char *hname;
89c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head list;
90c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct list_head profiles;
9101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen	struct rcu_head rcu;
92c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
93c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
94c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_ns_acct - accounting of profiles in namespace
95c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_size: maximum space allowed for all profiles in namespace
96c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_count: maximum number of profiles that can be in this namespace
97c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: current size of profiles
98c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @count: current count of profiles (includes null profiles)
99c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
100c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_ns_acct {
101c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int max_size;
102c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int max_count;
103c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int size;
104c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	int count;
105c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
106c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
107c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_namespace - namespace for a set of profiles
108c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base: common policy
109c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of namespace
110c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @lock: lock for modifying the object
111c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @acct: accounting for the namespace
112c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @unconfined: special unconfined profile for the namespace
113c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @sub_ns: list of namespaces under the current namespace.
114a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen * @uniq_null: uniq value used for null learning profiles
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;
138c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen};
139c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
140ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen/* struct aa_policydb - match engine for a policy
141ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * dfa: dfa pattern match
142ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * start: set of start states for the different classes of data
143ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen */
144ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansenstruct aa_policydb {
145ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	/* Generic policy DFA specific rule types will be subsections of it */
146ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	struct aa_dfa *dfa;
147ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen	unsigned int start[AA_CLASS_LAST + 1];
148ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
149ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen};
150ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen
15177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstruct aa_replacedby {
15277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct kref count;
15377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_profile __rcu *profile;
15477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen};
15577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
15677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
157c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_profile - basic confinement data
158c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base - base components of the profile (name, refcount, lists, lock ...)
159fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @count: reference count of the obj
160c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of profile
161c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @ns: namespace the profile is in
162c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @replacedby: is set to the profile that replaced this profile
163c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rename: optional profile name that this profile renamed
164c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch: optional extended matching for unconfined executables names
165c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @xmatch_len: xmatch prefix len, used to determine xmatch priority
166c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @audit: the auditing mode of the profile
167c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @mode: the enforcement mode of the profile
168c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @flags: flags controlling profile behavior
169c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @path_flags: flags controlling path generation behavior
170c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: the memory consumed by this profiles rules
171ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * @policy: general match rules governing policy
172c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @file: The set of rules governing basic file access and domain transitions
173c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @caps: capabilities for the profile
174c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @rlimits: rlimits for the profile
175c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
176c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * The AppArmor profile contains the basic confinement data.  Each profile
177c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * has a name, and exists in a namespace.  The @name and @exec_match are
178c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * used to determine profile attachment against unconfined tasks.  All other
179c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * attachments are determined by profile X transition rules.
180c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
18177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * The @replacedby struct is write protected by the profile lock.
182c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
183c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profiles have a hierarchy where hats and children profiles keep
184c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * a reference to their parent.
185c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen *
186c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Profile names can not begin with a : and can not contain the \0
187c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * character.  If a profile name begins with / it will be considered when
188c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * determining profile attachment on "unconfined" tasks.
189c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */
190c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile {
191c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	struct aa_policy base;
192fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	struct kref count;
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{
320fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (p) {
321fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		if (p->flags & PFLAG_NS_COUNT)
322fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen			kref_put(&p->count, aa_free_namespace_kref);
323fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		else
324fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen			kref_put(&p->count, aa_free_profile_kref);
325fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	}
326c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
327c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
32877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p)
32977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
33077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
33177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_get(&(p->count));
33277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	return p;
33477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
33577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
33677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void aa_put_replacedby(struct aa_replacedby *p)
33777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
33877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	if (p)
33977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen		kref_put(&p->count, aa_free_replacedby_kref);
34077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
34177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
34277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/* requires profile list write lock held */
34377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void __aa_update_replacedby(struct aa_profile *orig,
34477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen					  struct aa_profile *new)
34577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{
34677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	struct aa_profile *tmp = rcu_dereference(orig->replacedby->profile);
34777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new));
34877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	orig->flags |= PFLAG_INVALID;
34977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen	aa_put_profile(tmp);
35077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}
35177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen
352fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
353fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_get_namespace - increment references count on @ns
354fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to increment reference count of (MAYBE NULL)
355fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
356fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Returns: pointer to @ns, if @ns is NULL returns NULL
357fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Requires: @ns must be held with valid refcount when called
358fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
359fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns)
360fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
361fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
362fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_get_profile(ns->unconfined);
363fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
364fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	return ns;
365fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
366fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
367fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen/**
368fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * aa_put_namespace - decrement refcount on @ns
369fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * @ns: namespace to put reference of
370fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen *
371fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen * Decrement reference count of @ns and if no longer in use free it
372fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen */
373fa2ac468db510c653499a47c1ec3deb045bf4763John Johansenstatic inline void aa_put_namespace(struct aa_namespace *ns)
374fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen{
375fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen	if (ns)
376fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen		aa_put_profile(ns->unconfined);
377fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen}
378fa2ac468db510c653499a47c1ec3deb045bf4763John Johansen
379c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline int AUDIT_MODE(struct aa_profile *profile)
380c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{
381c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	if (aa_g_audit != AUDIT_NORMAL)
382c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen		return aa_g_audit;
383c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
384c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen	return profile->audit;
385c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}
386c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
387c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_may_manage_policy(int op);
388c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen
389c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#endif /* __AA_POLICY_H */
390