1cff281f6SJohn Johansen /* 2cff281f6SJohn Johansen * AppArmor security module 3cff281f6SJohn Johansen * 4cff281f6SJohn Johansen * This file contains AppArmor policy definitions. 5cff281f6SJohn Johansen * 6cff281f6SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 7cff281f6SJohn Johansen * Copyright 2009-2017 Canonical Ltd. 8cff281f6SJohn Johansen * 9cff281f6SJohn Johansen * This program is free software; you can redistribute it and/or 10cff281f6SJohn Johansen * modify it under the terms of the GNU General Public License as 11cff281f6SJohn Johansen * published by the Free Software Foundation, version 2 of the 12cff281f6SJohn Johansen * License. 13cff281f6SJohn Johansen */ 14cff281f6SJohn Johansen 15cff281f6SJohn Johansen #ifndef __AA_NAMESPACE_H 16cff281f6SJohn Johansen #define __AA_NAMESPACE_H 17cff281f6SJohn Johansen 18cff281f6SJohn Johansen #include <linux/kref.h> 19cff281f6SJohn Johansen 20cff281f6SJohn Johansen #include "apparmor.h" 21cff281f6SJohn Johansen #include "apparmorfs.h" 22cff281f6SJohn Johansen #include "policy.h" 23cff281f6SJohn Johansen 24cff281f6SJohn Johansen 25cff281f6SJohn Johansen /* struct aa_ns_acct - accounting of profiles in namespace 26cff281f6SJohn Johansen * @max_size: maximum space allowed for all profiles in namespace 27cff281f6SJohn Johansen * @max_count: maximum number of profiles that can be in this namespace 28cff281f6SJohn Johansen * @size: current size of profiles 29cff281f6SJohn Johansen * @count: current count of profiles (includes null profiles) 30cff281f6SJohn Johansen */ 31cff281f6SJohn Johansen struct aa_ns_acct { 32cff281f6SJohn Johansen int max_size; 33cff281f6SJohn Johansen int max_count; 34cff281f6SJohn Johansen int size; 35cff281f6SJohn Johansen int count; 36cff281f6SJohn Johansen }; 37cff281f6SJohn Johansen 3898849dffSJohn Johansen /* struct aa_ns - namespace for a set of profiles 39cff281f6SJohn Johansen * @base: common policy 40cff281f6SJohn Johansen * @parent: parent of namespace 41cff281f6SJohn Johansen * @lock: lock for modifying the object 42cff281f6SJohn Johansen * @acct: accounting for the namespace 43cff281f6SJohn Johansen * @unconfined: special unconfined profile for the namespace 44cff281f6SJohn Johansen * @sub_ns: list of namespaces under the current namespace. 45cff281f6SJohn Johansen * @uniq_null: uniq value used for null learning profiles 46cff281f6SJohn Johansen * @uniq_id: a unique id count for the profiles in the namespace 47cff281f6SJohn Johansen * @dents: dentries for the namespaces file entries in apparmorfs 48cff281f6SJohn Johansen * 4931617ddfSJohn Johansen * An aa_ns defines the set profiles that are searched to determine which 5031617ddfSJohn Johansen * profile to attach to a task. Profiles can not be shared between aa_ns 5131617ddfSJohn Johansen * and profile names within a namespace are guaranteed to be unique. When 5231617ddfSJohn Johansen * profiles in separate namespaces have the same name they are NOT considered 5331617ddfSJohn Johansen * to be equivalent. 54cff281f6SJohn Johansen * 55cff281f6SJohn Johansen * Namespaces are hierarchical and only namespaces and profiles below the 56cff281f6SJohn Johansen * current namespace are visible. 57cff281f6SJohn Johansen * 58cff281f6SJohn Johansen * Namespace names must be unique and can not contain the characters :/\0 59cff281f6SJohn Johansen */ 6098849dffSJohn Johansen struct aa_ns { 61cff281f6SJohn Johansen struct aa_policy base; 6298849dffSJohn Johansen struct aa_ns *parent; 63cff281f6SJohn Johansen struct mutex lock; 64cff281f6SJohn Johansen struct aa_ns_acct acct; 65cff281f6SJohn Johansen struct aa_profile *unconfined; 66cff281f6SJohn Johansen struct list_head sub_ns; 67cff281f6SJohn Johansen atomic_t uniq_null; 68cff281f6SJohn Johansen long uniq_id; 69cff281f6SJohn Johansen 70cff281f6SJohn Johansen struct dentry *dents[AAFS_NS_SIZEOF]; 71cff281f6SJohn Johansen }; 72cff281f6SJohn Johansen 7398849dffSJohn Johansen extern struct aa_ns *root_ns; 74cff281f6SJohn Johansen 75cff281f6SJohn Johansen extern const char *aa_hidden_ns_name; 76cff281f6SJohn Johansen 7792b6d8efSJohn Johansen bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns); 7892b6d8efSJohn Johansen const char *aa_ns_name(struct aa_ns *parent, struct aa_ns *child, bool subns); 7998849dffSJohn Johansen void aa_free_ns(struct aa_ns *ns); 80cff281f6SJohn Johansen int aa_alloc_root_ns(void); 81cff281f6SJohn Johansen void aa_free_root_ns(void); 8298849dffSJohn Johansen void aa_free_ns_kref(struct kref *kref); 83cff281f6SJohn Johansen 8498849dffSJohn Johansen struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name); 859a2d40c1SJohn Johansen struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n); 8673688d1eSJohn Johansen struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name, 8773688d1eSJohn Johansen struct dentry *dir); 8873688d1eSJohn Johansen struct aa_ns *aa_prepare_ns(struct aa_ns *root, const char *name); 8998849dffSJohn Johansen void __aa_remove_ns(struct aa_ns *ns); 90cff281f6SJohn Johansen 91cff281f6SJohn Johansen static inline struct aa_profile *aa_deref_parent(struct aa_profile *p) 92cff281f6SJohn Johansen { 93cff281f6SJohn Johansen return rcu_dereference_protected(p->parent, 94cff281f6SJohn Johansen mutex_is_locked(&p->ns->lock)); 95cff281f6SJohn Johansen } 96cff281f6SJohn Johansen 97cff281f6SJohn Johansen /** 9898849dffSJohn Johansen * aa_get_ns - increment references count on @ns 99cff281f6SJohn Johansen * @ns: namespace to increment reference count of (MAYBE NULL) 100cff281f6SJohn Johansen * 101cff281f6SJohn Johansen * Returns: pointer to @ns, if @ns is NULL returns NULL 102cff281f6SJohn Johansen * Requires: @ns must be held with valid refcount when called 103cff281f6SJohn Johansen */ 10498849dffSJohn Johansen static inline struct aa_ns *aa_get_ns(struct aa_ns *ns) 105cff281f6SJohn Johansen { 106cff281f6SJohn Johansen if (ns) 107cff281f6SJohn Johansen aa_get_profile(ns->unconfined); 108cff281f6SJohn Johansen 109cff281f6SJohn Johansen return ns; 110cff281f6SJohn Johansen } 111cff281f6SJohn Johansen 112cff281f6SJohn Johansen /** 11398849dffSJohn Johansen * aa_put_ns - decrement refcount on @ns 114cff281f6SJohn Johansen * @ns: namespace to put reference of 115cff281f6SJohn Johansen * 116cff281f6SJohn Johansen * Decrement reference count of @ns and if no longer in use free it 117cff281f6SJohn Johansen */ 11898849dffSJohn Johansen static inline void aa_put_ns(struct aa_ns *ns) 119cff281f6SJohn Johansen { 120cff281f6SJohn Johansen if (ns) 121cff281f6SJohn Johansen aa_put_profile(ns->unconfined); 122cff281f6SJohn Johansen } 123cff281f6SJohn Johansen 124cff281f6SJohn Johansen /** 1259a2d40c1SJohn Johansen * __aa_findn_ns - find a namespace on a list by @name 126cff281f6SJohn Johansen * @head: list to search for namespace on (NOT NULL) 127cff281f6SJohn Johansen * @name: name of namespace to look for (NOT NULL) 1289a2d40c1SJohn Johansen * @n: length of @name 129cff281f6SJohn Johansen * Returns: unrefcounted namespace 130cff281f6SJohn Johansen * 131cff281f6SJohn Johansen * Requires: rcu_read_lock be held 132cff281f6SJohn Johansen */ 1339a2d40c1SJohn Johansen static inline struct aa_ns *__aa_findn_ns(struct list_head *head, 1349a2d40c1SJohn Johansen const char *name, size_t n) 1359a2d40c1SJohn Johansen { 1369a2d40c1SJohn Johansen return (struct aa_ns *)__policy_strn_find(head, name, n); 1379a2d40c1SJohn Johansen } 1389a2d40c1SJohn Johansen 13998849dffSJohn Johansen static inline struct aa_ns *__aa_find_ns(struct list_head *head, 140cff281f6SJohn Johansen const char *name) 141cff281f6SJohn Johansen { 1429a2d40c1SJohn Johansen return __aa_findn_ns(head, name, strlen(name)); 143cff281f6SJohn Johansen } 144cff281f6SJohn Johansen 145cff281f6SJohn Johansen #endif /* AA_NAMESPACE_H */ 146