Skip to content

Commit a1bd627

Browse files
committed
apparmor: share profile name on replacement
The profile names are the same, leverage this. Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent cf797c0 commit a1bd627

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎security/apparmor/include/lib.h‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ static inline bool path_mediated_fs(struct dentry *dentry)
100100
return !(dentry->d_sb->s_flags & MS_NOUSER);
101101
}
102102

103+
104+
struct counted_str {
105+
struct kref count;
106+
char name[];
107+
};
108+
109+
#define str_to_counted(str) \
110+
((struct counted_str *)(str - offsetof(struct counted_str, name)))
111+
112+
#define __counted /* atm just a notation */
113+
114+
void aa_str_kref(struct kref *kref);
115+
char *aa_str_alloc(int size, gfp_t gfp);
116+
117+
118+
static inline __counted char *aa_get_str(__counted char *str)
119+
{
120+
if (str)
121+
kref_get(&(str_to_counted(str)->count));
122+
123+
return str;
124+
}
125+
126+
static inline void aa_put_str(__counted char *str)
127+
{
128+
if (str)
129+
kref_put(&str_to_counted(str)->count, aa_str_kref);
130+
}
131+
132+
103133
/* struct aa_policy - common part of both namespaces and profiles
104134
* @name: name of the object
105135
* @hname - The hierarchical name
@@ -108,7 +138,7 @@ static inline bool path_mediated_fs(struct dentry *dentry)
108138
*/
109139
struct aa_policy {
110140
const char *name;
111-
const char *hname;
141+
__counted char *hname;
112142
struct list_head list;
113143
struct list_head profiles;
114144
};

‎security/apparmor/lib.c‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ void aa_info_message(const char *str)
134134
printk(KERN_INFO "AppArmor: %s\n", str);
135135
}
136136

137+
__counted char *aa_str_alloc(int size, gfp_t gfp)
138+
{
139+
struct counted_str *str;
140+
141+
str = kmalloc(sizeof(struct counted_str) + size, gfp);
142+
if (!str)
143+
return NULL;
144+
145+
kref_init(&str->count);
146+
return str->name;
147+
}
148+
149+
void aa_str_kref(struct kref *kref)
150+
{
151+
kfree(container_of(kref, struct counted_str, count));
152+
}
153+
154+
137155
const char aa_file_perm_chrs[] = "xwracd km l ";
138156
const char *aa_file_perm_names[] = {
139157
"exec",
@@ -296,6 +314,7 @@ void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
296314
* @policy: policy to initialize (NOT NULL)
297315
* @prefix: prefix name if any is required. (MAYBE NULL)
298316
* @name: name of the policy, init will make a copy of it (NOT NULL)
317+
* @gfp: allocation mode
299318
*
300319
* Note: this fn creates a copy of strings passed in
301320
*
@@ -304,16 +323,21 @@ void aa_compute_perms(struct aa_dfa *dfa, unsigned int state,
304323
bool aa_policy_init(struct aa_policy *policy, const char *prefix,
305324
const char *name, gfp_t gfp)
306325
{
326+
char *hname;
327+
307328
/* freed by policy_free */
308329
if (prefix) {
309-
policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
310-
gfp);
311-
if (policy->hname)
312-
sprintf((char *)policy->hname, "%s//%s", prefix, name);
313-
} else
314-
policy->hname = kstrdup(name, gfp);
315-
if (!policy->hname)
330+
hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
331+
if (hname)
332+
sprintf(hname, "%s//%s", prefix, name);
333+
} else {
334+
hname = aa_str_alloc(strlen(name) + 1, gfp);
335+
if (hname)
336+
strcpy(hname, name);
337+
}
338+
if (!hname)
316339
return false;
340+
policy->hname = hname;
317341
/* base.name is a substring of fqname */
318342
policy->name = basename(policy->hname);
319343
INIT_LIST_HEAD(&policy->list);
@@ -332,5 +356,5 @@ void aa_policy_destroy(struct aa_policy *policy)
332356
AA_BUG(on_list_rcu(&policy->list));
333357

334358
/* don't free name as its a subset of hname */
335-
kzfree(policy->hname);
359+
aa_put_str(policy->hname);
336360
}

‎security/apparmor/policy.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,14 @@ static int __lookup_replace(struct aa_ns *ns, const char *hname,
829829
return 0;
830830
}
831831

832+
static void share_name(struct aa_profile *old, struct aa_profile *new)
833+
{
834+
aa_put_str(new->base.hname);
835+
aa_get_str(old->base.hname);
836+
new->base.hname = old->base.hname;
837+
new->base.name = old->base.name;
838+
}
839+
832840
/**
833841
* aa_replace_profiles - replace profile(s) on the profile list
834842
* @policy_ns: namespace load is occurring on
@@ -1013,6 +1021,7 @@ ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_profile *profile,
10131021
NULL, error);
10141022

10151023
if (ent->old) {
1024+
share_name(ent->old, ent->new);
10161025
__replace_profile(ent->old, ent->new, 1);
10171026
if (ent->rename) {
10181027
/* aafs interface uses proxy */

0 commit comments

Comments
 (0)