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 
7798849dffSJohn Johansen bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view);
7898849dffSJohn Johansen const char *aa_ns_name(struct aa_ns *parent, struct aa_ns *child);
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);
8698849dffSJohn Johansen struct aa_ns *aa_prepare_ns(const char *name);
8798849dffSJohn Johansen void __aa_remove_ns(struct aa_ns *ns);
88cff281f6SJohn Johansen 
89cff281f6SJohn Johansen static inline struct aa_profile *aa_deref_parent(struct aa_profile *p)
90cff281f6SJohn Johansen {
91cff281f6SJohn Johansen 	return rcu_dereference_protected(p->parent,
92cff281f6SJohn Johansen 					 mutex_is_locked(&p->ns->lock));
93cff281f6SJohn Johansen }
94cff281f6SJohn Johansen 
95cff281f6SJohn Johansen /**
9698849dffSJohn Johansen  * aa_get_ns - increment references count on @ns
97cff281f6SJohn Johansen  * @ns: namespace to increment reference count of (MAYBE NULL)
98cff281f6SJohn Johansen  *
99cff281f6SJohn Johansen  * Returns: pointer to @ns, if @ns is NULL returns NULL
100cff281f6SJohn Johansen  * Requires: @ns must be held with valid refcount when called
101cff281f6SJohn Johansen  */
10298849dffSJohn Johansen static inline struct aa_ns *aa_get_ns(struct aa_ns *ns)
103cff281f6SJohn Johansen {
104cff281f6SJohn Johansen 	if (ns)
105cff281f6SJohn Johansen 		aa_get_profile(ns->unconfined);
106cff281f6SJohn Johansen 
107cff281f6SJohn Johansen 	return ns;
108cff281f6SJohn Johansen }
109cff281f6SJohn Johansen 
110cff281f6SJohn Johansen /**
11198849dffSJohn Johansen  * aa_put_ns - decrement refcount on @ns
112cff281f6SJohn Johansen  * @ns: namespace to put reference of
113cff281f6SJohn Johansen  *
114cff281f6SJohn Johansen  * Decrement reference count of @ns and if no longer in use free it
115cff281f6SJohn Johansen  */
11698849dffSJohn Johansen static inline void aa_put_ns(struct aa_ns *ns)
117cff281f6SJohn Johansen {
118cff281f6SJohn Johansen 	if (ns)
119cff281f6SJohn Johansen 		aa_put_profile(ns->unconfined);
120cff281f6SJohn Johansen }
121cff281f6SJohn Johansen 
122cff281f6SJohn Johansen /**
1239a2d40c1SJohn Johansen  * __aa_findn_ns - find a namespace on a list by @name
124cff281f6SJohn Johansen  * @head: list to search for namespace on  (NOT NULL)
125cff281f6SJohn Johansen  * @name: name of namespace to look for  (NOT NULL)
1269a2d40c1SJohn Johansen  * @n: length of @name
127cff281f6SJohn Johansen  * Returns: unrefcounted namespace
128cff281f6SJohn Johansen  *
129cff281f6SJohn Johansen  * Requires: rcu_read_lock be held
130cff281f6SJohn Johansen  */
1319a2d40c1SJohn Johansen static inline struct aa_ns *__aa_findn_ns(struct list_head *head,
1329a2d40c1SJohn Johansen 					  const char *name, size_t n)
1339a2d40c1SJohn Johansen {
1349a2d40c1SJohn Johansen 	return (struct aa_ns *)__policy_strn_find(head, name, n);
1359a2d40c1SJohn Johansen }
1369a2d40c1SJohn Johansen 
13798849dffSJohn Johansen static inline struct aa_ns *__aa_find_ns(struct list_head *head,
138cff281f6SJohn Johansen 					 const char *name)
139cff281f6SJohn Johansen {
1409a2d40c1SJohn Johansen 	return __aa_findn_ns(head, name, strlen(name));
141cff281f6SJohn Johansen }
142cff281f6SJohn Johansen 
143cff281f6SJohn Johansen #endif /* AA_NAMESPACE_H */
144