policy.h revision 77b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1
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 */ 71c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 72c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen /* These flags must correspond with PATH_flags */ 73c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen PFLAG_MEDIATE_DELETED = 0x10000, /* mediate instead delegate deleted */ 74c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}; 75c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 76c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile; 77c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 78c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_policy - common part of both namespaces and profiles 79c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @name: name of the object 80c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @hname - The hierarchical name 81c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @count: reference count of the obj 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 kref count; 90c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct list_head list; 91c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct list_head profiles; 9201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen struct rcu_head rcu; 93c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}; 94c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 95c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_ns_acct - accounting of profiles in namespace 96c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_size: maximum space allowed for all profiles in namespace 97c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @max_count: maximum number of profiles that can be in this namespace 98c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @size: current size of profiles 99c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @count: current count of profiles (includes null profiles) 100c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 101c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_ns_acct { 102c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int max_size; 103c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int max_count; 104c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int size; 105c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int count; 106c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}; 107c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 108c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_namespace - namespace for a set of profiles 109c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base: common policy 110c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @parent: parent of namespace 111c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @lock: lock for modifying the object 112c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @acct: accounting for the namespace 113c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @unconfined: special unconfined profile for the namespace 114c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @sub_ns: list of namespaces under the current namespace. 115a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen * @uniq_null: uniq value used for null learning profiles 116c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 117c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * An aa_namespace defines the set profiles that are searched to determine 118c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * which profile to attach to a task. Profiles can not be shared between 119c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_namespaces and profile names within a namespace are guaranteed to be 120c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * unique. When profiles in separate namespaces have the same name they 121c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * are NOT considered to be equivalent. 122c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 123c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespaces are hierarchical and only namespaces and profiles below the 124c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * current namespace are visible. 125c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 126c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Namespace names must be unique and can not contain the characters :/\0 127c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 128c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * FIXME TODO: add vserver support of namespaces (can it all be done in 129c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * userspace?) 130c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 131c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace { 132c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_policy base; 133c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_namespace *parent; 13401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen struct mutex lock; 135c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_ns_acct acct; 136c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_profile *unconfined; 137c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct list_head sub_ns; 138a4987857d2c958b93b2faafe0811eea1a63ff59aJohn Johansen atomic_t uniq_null; 139c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}; 140c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 141ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen/* struct aa_policydb - match engine for a policy 142ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * dfa: dfa pattern match 143ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen * start: set of start states for the different classes of data 144ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen */ 145ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansenstruct aa_policydb { 146ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen /* Generic policy DFA specific rule types will be subsections of it */ 147ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen struct aa_dfa *dfa; 148ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen unsigned int start[AA_CLASS_LAST + 1]; 149ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen 150ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen}; 151ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen 15277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstruct aa_replacedby { 15377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen struct kref count; 15477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen struct aa_profile __rcu *profile; 15577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen}; 15677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 15777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 158c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/* struct aa_profile - basic confinement data 159c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @base - base components of the profile (name, refcount, lists, lock ...) 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; 19201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen struct aa_profile __rcu *parent; 193c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 194c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_namespace *ns; 19577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen struct aa_replacedby *replacedby; 196c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen const char *rename; 197c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 198c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_dfa *xmatch; 199c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int xmatch_len; 200c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen enum audit_mode audit; 201c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen enum profile_mode mode; 20277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen long flags; 203c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen u32 path_flags; 204c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen int size; 205c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 206ad5ff3db53c68c2f12936bc74ea5dfe0af943592John Johansen struct aa_policydb policy; 207c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_file_rules file; 208c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_caps caps; 209c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen struct aa_rlimit rlimits; 210c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen}; 211c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 212c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern struct aa_namespace *root_ns; 213c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenextern enum profile_mode aa_g_profile_mode; 214c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 215c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_add_profile(struct aa_policy *common, struct aa_profile *profile); 216c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 217c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view); 218c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenconst char *aa_ns_name(struct aa_namespace *parent, struct aa_namespace *child); 219c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenint aa_alloc_root_ns(void); 220c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_root_ns(void); 221c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_namespace_kref(struct kref *kref); 222c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 223c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_namespace *aa_find_namespace(struct aa_namespace *root, 224c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen const char *name); 225c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 226c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline struct aa_policy *aa_get_common(struct aa_policy *c) 227c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 228c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (c) 229c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen kref_get(&c->count); 230c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 231c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen return c; 232c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 233c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 234c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/** 235c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_get_namespace - increment references count on @ns 236c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @ns: namespace to increment reference count of (MAYBE NULL) 237c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 238c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Returns: pointer to @ns, if @ns is NULL returns NULL 239c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Requires: @ns must be held with valid refcount when called 240c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 241c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline struct aa_namespace *aa_get_namespace(struct aa_namespace *ns) 242c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 243c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (ns) 244c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen kref_get(&(ns->base.count)); 245c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 246c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen return ns; 247c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 248c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 249c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/** 250c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_put_namespace - decrement refcount on @ns 251c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @ns: namespace to put reference of 252c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 253c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Decrement reference count of @ns and if no longer in use free it 254c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 255c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline void aa_put_namespace(struct aa_namespace *ns) 256c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 257c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (ns) 258c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen kref_put(&ns->base.count, aa_free_namespace_kref); 259c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 260c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 26177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenvoid aa_free_replacedby_kref(struct kref *kref); 262c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_alloc_profile(const char *name); 263c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat); 264c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenvoid aa_free_profile_kref(struct kref *kref); 265c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_find_child(struct aa_profile *parent, const char *name); 266c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *name); 267c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstruct aa_profile *aa_match_profile(struct aa_namespace *ns, const char *name); 268c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 269c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace); 270c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenssize_t aa_remove_profiles(char *name, size_t size); 271c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 272c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_ADD 1 273c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define PROF_REPLACE 0 274c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 275c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#define unconfined(X) ((X)->flags & PFLAG_UNCONFINED) 276c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 277c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 278c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/** 279c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_get_profile - increment refcount on profile @p 280c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile (MAYBE NULL) 281c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * 282c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Returns: pointer to @p if @p is NULL will return NULL 283c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * Requires: @p must be held with valid refcount when called 284c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 285c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline struct aa_profile *aa_get_profile(struct aa_profile *p) 286c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 287c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (p) 288c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen kref_get(&(p->base.count)); 289c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 290c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen return p; 291c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 292c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 293c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen/** 29401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_not0 - increment refcount on profile @p found via lookup 29501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: profile (MAYBE NULL) 29601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * 29701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to @p if @p is NULL will return NULL 29801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Requires: @p must be held with valid refcount when called 29901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */ 30001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_not0(struct aa_profile *p) 30101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{ 30201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen if (p && kref_get_not0(&p->base.count)) 30301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen return p; 30401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen 30501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen return NULL; 30601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen} 30701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen 30801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/** 30901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * aa_get_profile_rcu - increment a refcount profile that can be replaced 31001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * @p: pointer to profile that can be replaced (NOT NULL) 31101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * 31201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * Returns: pointer to a refcounted profile. 31301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen * else NULL if no profile 31401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen */ 31501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansenstatic inline struct aa_profile *aa_get_profile_rcu(struct aa_profile __rcu **p) 31601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen{ 31701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen struct aa_profile *c; 31801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen 31901e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen rcu_read_lock(); 32001e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen do { 32101e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen c = rcu_dereference(*p); 32201e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen } while (c && !kref_get_not0(&c->base.count)); 32301e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen rcu_read_unlock(); 32401e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen 32501e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen return c; 32601e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen} 32701e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen 32801e2b670aa898a39259bc85c78e3d74820f4d3b6John Johansen/** 32977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * aa_get_newest_profile - find the newest version of @profile 33077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * @profile: the profile to check for newer versions of 33177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * 33277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * Returns: refcounted newest version of @profile taking into account 33377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * replacement, renames and removals 33477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen * return @profile. 33577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen */ 33677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_profile *aa_get_newest_profile(struct aa_profile *p) 33777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{ 33877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen if (!p) 33977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen return NULL; 34077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 34177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen if (PROFILE_INVALID(p)) 34277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen return aa_get_profile_rcu(&p->replacedby->profile); 34377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 34477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen return aa_get_profile(p); 34577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen} 34677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 34777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/** 348c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * aa_put_profile - decrement refcount on profile @p 349c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen * @p: profile (MAYBE NULL) 350c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen */ 351c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline void aa_put_profile(struct aa_profile *p) 352c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 353c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (p) 354c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen kref_put(&p->base.count, aa_free_profile_kref); 355c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 356c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 35777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline struct aa_replacedby *aa_get_replacedby(struct aa_replacedby *p) 35877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{ 35977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen if (p) 36077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen kref_get(&(p->count)); 36177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 36277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen return p; 36377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen} 36477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 36577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void aa_put_replacedby(struct aa_replacedby *p) 36677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{ 36777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen if (p) 36877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen kref_put(&p->count, aa_free_replacedby_kref); 36977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen} 37077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 37177b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen/* requires profile list write lock held */ 37277b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansenstatic inline void __aa_update_replacedby(struct aa_profile *orig, 37377b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen struct aa_profile *new) 37477b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen{ 37577b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen struct aa_profile *tmp = rcu_dereference(orig->replacedby->profile); 37677b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new)); 37777b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen orig->flags |= PFLAG_INVALID; 37877b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen aa_put_profile(tmp); 37977b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen} 38077b071b34045a0c65d0e1f85f3d47fd2b8b7a8a1John Johansen 381c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenstatic inline int AUDIT_MODE(struct aa_profile *profile) 382c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen{ 383c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen if (aa_g_audit != AUDIT_NORMAL) 384c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen return aa_g_audit; 385c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 386c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen return profile->audit; 387c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen} 388c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 389c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansenbool aa_may_manage_policy(int op); 390c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen 391c88d4c7b049e87998ac0a9f455aa545cc895ef92John Johansen#endif /* __AA_POLICY_H */ 392