Skip to content

Commit fe6bb31

Browse files
committed
apparmor: split out shared policy_XXX fns to lib
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 12557dc commit fe6bb31

4 files changed

Lines changed: 137 additions & 132 deletions

File tree

‎security/apparmor/include/lib.h‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,85 @@ static inline bool mediated_filesystem(struct dentry *dentry)
9191
return !(dentry->d_sb->s_flags & MS_NOUSER);
9292
}
9393

94+
/* struct aa_policy - common part of both namespaces and profiles
95+
* @name: name of the object
96+
* @hname - The hierarchical name
97+
* @list: list policy object is on
98+
* @profiles: head of the profiles list contained in the object
99+
*/
100+
struct aa_policy {
101+
char *name;
102+
char *hname;
103+
struct list_head list;
104+
struct list_head profiles;
105+
};
106+
107+
/**
108+
* hname_tail - find the last component of an hname
109+
* @name: hname to find the base profile name component of (NOT NULL)
110+
*
111+
* Returns: the tail (base profile name) name component of an hname
112+
*/
113+
static inline const char *hname_tail(const char *hname)
114+
{
115+
char *split;
116+
117+
hname = strim((char *)hname);
118+
for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
119+
hname = split + 2;
120+
121+
return hname;
122+
}
123+
124+
/**
125+
* __policy_find - find a policy by @name on a policy list
126+
* @head: list to search (NOT NULL)
127+
* @name: name to search for (NOT NULL)
128+
*
129+
* Requires: rcu_read_lock be held
130+
*
131+
* Returns: unrefcounted policy that match @name or NULL if not found
132+
*/
133+
static inline struct aa_policy *__policy_find(struct list_head *head,
134+
const char *name)
135+
{
136+
struct aa_policy *policy;
137+
138+
list_for_each_entry_rcu(policy, head, list) {
139+
if (!strcmp(policy->name, name))
140+
return policy;
141+
}
142+
return NULL;
143+
}
144+
145+
/**
146+
* __policy_strn_find - find a policy that's name matches @len chars of @str
147+
* @head: list to search (NOT NULL)
148+
* @str: string to search for (NOT NULL)
149+
* @len: length of match required
150+
*
151+
* Requires: rcu_read_lock be held
152+
*
153+
* Returns: unrefcounted policy that match @str or NULL if not found
154+
*
155+
* if @len == strlen(@strlen) then this is equiv to __policy_find
156+
* other wise it allows searching for policy by a partial match of name
157+
*/
158+
static inline struct aa_policy *__policy_strn_find(struct list_head *head,
159+
const char *str, int len)
160+
{
161+
struct aa_policy *policy;
162+
163+
list_for_each_entry_rcu(policy, head, list) {
164+
if (aa_strneq(policy->name, str, len))
165+
return policy;
166+
}
167+
168+
return NULL;
169+
}
170+
171+
bool aa_policy_init(struct aa_policy *policy, const char *prefix,
172+
const char *name);
173+
void aa_policy_destroy(struct aa_policy *policy);
174+
94175
#endif /* AA_LIB_H */

‎security/apparmor/include/policy.h‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,6 @@ enum profile_flags {
7777

7878
struct aa_profile;
7979

80-
/* struct aa_policy - common part of both namespaces and profiles
81-
* @name: name of the object
82-
* @hname - The hierarchical name
83-
* @list: list policy object is on
84-
* @profiles: head of the profiles list contained in the object
85-
*/
86-
struct aa_policy {
87-
char *name;
88-
char *hname;
89-
struct list_head list;
90-
struct list_head profiles;
91-
};
92-
9380
/* struct aa_ns_acct - accounting of profiles in namespace
9481
* @max_size: maximum space allowed for all profiles in namespace
9582
* @max_count: maximum number of profiles that can be in this namespace

‎security/apparmor/lib.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "include/audit.h"
2121
#include "include/apparmor.h"
2222
#include "include/lib.h"
23+
#include "include/policy.h"
2324

2425
/**
2526
* aa_split_fqname - split a fqname into a profile and namespace name
@@ -105,3 +106,54 @@ void *__aa_kvmalloc(size_t size, gfp_t flags)
105106
}
106107
return buffer;
107108
}
109+
110+
/**
111+
* aa_policy_init - initialize a policy structure
112+
* @policy: policy to initialize (NOT NULL)
113+
* @prefix: prefix name if any is required. (MAYBE NULL)
114+
* @name: name of the policy, init will make a copy of it (NOT NULL)
115+
*
116+
* Note: this fn creates a copy of strings passed in
117+
*
118+
* Returns: true if policy init successful
119+
*/
120+
bool aa_policy_init(struct aa_policy *policy, const char *prefix,
121+
const char *name)
122+
{
123+
/* freed by policy_free */
124+
if (prefix) {
125+
policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
126+
GFP_KERNEL);
127+
if (policy->hname)
128+
sprintf(policy->hname, "%s//%s", prefix, name);
129+
} else
130+
policy->hname = kstrdup(name, GFP_KERNEL);
131+
if (!policy->hname)
132+
return 0;
133+
/* base.name is a substring of fqname */
134+
policy->name = (char *)hname_tail(policy->hname);
135+
INIT_LIST_HEAD(&policy->list);
136+
INIT_LIST_HEAD(&policy->profiles);
137+
138+
return 1;
139+
}
140+
141+
/**
142+
* aa_policy_destroy - free the elements referenced by @policy
143+
* @policy: policy that is to have its elements freed (NOT NULL)
144+
*/
145+
void aa_policy_destroy(struct aa_policy *policy)
146+
{
147+
/* still contains profiles -- invalid */
148+
if (on_list_rcu(&policy->profiles)) {
149+
AA_ERROR("%s: internal error, policy '%s' contains profiles\n",
150+
__func__, policy->name);
151+
}
152+
if (on_list_rcu(&policy->list)) {
153+
AA_ERROR("%s: internal error, policy '%s' still on list\n",
154+
__func__, policy->name);
155+
}
156+
157+
/* don't free name as its a subset of hname */
158+
kzfree(policy->hname);
159+
}

‎security/apparmor/policy.c‎

Lines changed: 4 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -99,121 +99,6 @@ const char *const aa_profile_mode_names[] = {
9999
"unconfined",
100100
};
101101

102-
/**
103-
* hname_tail - find the last component of an hname
104-
* @name: hname to find the base profile name component of (NOT NULL)
105-
*
106-
* Returns: the tail (base profile name) name component of an hname
107-
*/
108-
static const char *hname_tail(const char *hname)
109-
{
110-
char *split;
111-
hname = strim((char *)hname);
112-
for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
113-
hname = split + 2;
114-
115-
return hname;
116-
}
117-
118-
/**
119-
* policy_init - initialize a policy structure
120-
* @policy: policy to initialize (NOT NULL)
121-
* @prefix: prefix name if any is required. (MAYBE NULL)
122-
* @name: name of the policy, init will make a copy of it (NOT NULL)
123-
*
124-
* Note: this fn creates a copy of strings passed in
125-
*
126-
* Returns: true if policy init successful
127-
*/
128-
static bool policy_init(struct aa_policy *policy, const char *prefix,
129-
const char *name)
130-
{
131-
/* freed by policy_free */
132-
if (prefix) {
133-
policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
134-
GFP_KERNEL);
135-
if (policy->hname)
136-
sprintf(policy->hname, "%s//%s", prefix, name);
137-
} else
138-
policy->hname = kstrdup(name, GFP_KERNEL);
139-
if (!policy->hname)
140-
return 0;
141-
/* base.name is a substring of fqname */
142-
policy->name = (char *)hname_tail(policy->hname);
143-
INIT_LIST_HEAD(&policy->list);
144-
INIT_LIST_HEAD(&policy->profiles);
145-
146-
return 1;
147-
}
148-
149-
/**
150-
* policy_destroy - free the elements referenced by @policy
151-
* @policy: policy that is to have its elements freed (NOT NULL)
152-
*/
153-
static void policy_destroy(struct aa_policy *policy)
154-
{
155-
/* still contains profiles -- invalid */
156-
if (on_list_rcu(&policy->profiles)) {
157-
AA_ERROR("%s: internal error, "
158-
"policy '%s' still contains profiles\n",
159-
__func__, policy->name);
160-
BUG();
161-
}
162-
if (on_list_rcu(&policy->list)) {
163-
AA_ERROR("%s: internal error, policy '%s' still on list\n",
164-
__func__, policy->name);
165-
BUG();
166-
}
167-
168-
/* don't free name as its a subset of hname */
169-
kzfree(policy->hname);
170-
}
171-
172-
/**
173-
* __policy_find - find a policy by @name on a policy list
174-
* @head: list to search (NOT NULL)
175-
* @name: name to search for (NOT NULL)
176-
*
177-
* Requires: rcu_read_lock be held
178-
*
179-
* Returns: unrefcounted policy that match @name or NULL if not found
180-
*/
181-
static struct aa_policy *__policy_find(struct list_head *head, const char *name)
182-
{
183-
struct aa_policy *policy;
184-
185-
list_for_each_entry_rcu(policy, head, list) {
186-
if (!strcmp(policy->name, name))
187-
return policy;
188-
}
189-
return NULL;
190-
}
191-
192-
/**
193-
* __policy_strn_find - find a policy that's name matches @len chars of @str
194-
* @head: list to search (NOT NULL)
195-
* @str: string to search for (NOT NULL)
196-
* @len: length of match required
197-
*
198-
* Requires: rcu_read_lock be held
199-
*
200-
* Returns: unrefcounted policy that match @str or NULL if not found
201-
*
202-
* if @len == strlen(@strlen) then this is equiv to __policy_find
203-
* other wise it allows searching for policy by a partial match of name
204-
*/
205-
static struct aa_policy *__policy_strn_find(struct list_head *head,
206-
const char *str, int len)
207-
{
208-
struct aa_policy *policy;
209-
210-
list_for_each_entry_rcu(policy, head, list) {
211-
if (aa_strneq(policy->name, str, len))
212-
return policy;
213-
}
214-
215-
return NULL;
216-
}
217102

218103
/*
219104
* Routines for AppArmor namespaces
@@ -280,7 +165,7 @@ static struct aa_namespace *alloc_namespace(const char *prefix,
280165
AA_DEBUG("%s(%p)\n", __func__, ns);
281166
if (!ns)
282167
return NULL;
283-
if (!policy_init(&ns->base, prefix, name))
168+
if (!aa_policy_init(&ns->base, prefix, name))
284169
goto fail_ns;
285170

286171
INIT_LIST_HEAD(&ns->sub_ns);
@@ -321,7 +206,7 @@ static void free_namespace(struct aa_namespace *ns)
321206
if (!ns)
322207
return;
323208

324-
policy_destroy(&ns->base);
209+
aa_policy_destroy(&ns->base);
325210
aa_put_namespace(ns->parent);
326211

327212
ns->unconfined->ns = NULL;
@@ -595,7 +480,7 @@ void aa_free_profile(struct aa_profile *profile)
595480
return;
596481

597482
/* free children profiles */
598-
policy_destroy(&profile->base);
483+
aa_policy_destroy(&profile->base);
599484
aa_put_profile(rcu_access_pointer(profile->parent));
600485

601486
aa_put_namespace(profile->ns);
@@ -657,7 +542,7 @@ struct aa_profile *aa_alloc_profile(const char *hname)
657542
goto fail;
658543
kref_init(&profile->replacedby->count);
659544

660-
if (!policy_init(&profile->base, NULL, hname))
545+
if (!aa_policy_init(&profile->base, NULL, hname))
661546
goto fail;
662547
kref_init(&profile->count);
663548

0 commit comments

Comments
 (0)