Skip to content

Commit 98b824f

Browse files
committed
apparmor: refcount the pdb
With the move to permission tables the dfa is no longer a stand alone entity when used, needing a minimum of a permission table. However it still could be shared among different pdbs each using a different permission table. Instead of duping the permission table when sharing a pdb, add a refcount to the pdb so it can be easily shared. Reviewed-by: Georgia Garcia <georgia.garcia@canonical.com> Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 75c77e9 commit 98b824f

File tree

15 files changed

+260
-210
lines changed

15 files changed

+260
-210
lines changed

security/apparmor/apparmorfs.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,23 +619,23 @@ static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
619619

620620
if (profile_unconfined(profile))
621621
return;
622-
if (rules->file.dfa && *match_str == AA_CLASS_FILE) {
623-
state = aa_dfa_match_len(rules->file.dfa,
624-
rules->file.start[AA_CLASS_FILE],
622+
if (rules->file->dfa && *match_str == AA_CLASS_FILE) {
623+
state = aa_dfa_match_len(rules->file->dfa,
624+
rules->file->start[AA_CLASS_FILE],
625625
match_str + 1, match_len - 1);
626626
if (state) {
627627
struct path_cond cond = { };
628628

629-
tmp = *(aa_lookup_fperms(&(rules->file), state, &cond));
629+
tmp = *(aa_lookup_fperms(rules->file, state, &cond));
630630
}
631-
} else if (rules->policy.dfa) {
631+
} else if (rules->policy->dfa) {
632632
if (!RULE_MEDIATES(rules, *match_str))
633633
return; /* no change to current perms */
634-
state = aa_dfa_match_len(rules->policy.dfa,
635-
rules->policy.start[0],
634+
state = aa_dfa_match_len(rules->policy->dfa,
635+
rules->policy->start[0],
636636
match_str, match_len);
637637
if (state)
638-
tmp = *aa_lookup_perms(&rules->policy, state);
638+
tmp = *aa_lookup_perms(rules->policy, state);
639639
}
640640
aa_apply_modes_to_perms(profile, &tmp);
641641
aa_perms_accum_raw(perms, &tmp);
@@ -1096,7 +1096,7 @@ static int seq_profile_attach_show(struct seq_file *seq, void *v)
10961096
struct aa_profile *profile = labels_profile(label);
10971097
if (profile->attach.xmatch_str)
10981098
seq_printf(seq, "%s\n", profile->attach.xmatch_str);
1099-
else if (profile->attach.xmatch.dfa)
1099+
else if (profile->attach.xmatch->dfa)
11001100
seq_puts(seq, "<unknown>\n");
11011101
else
11021102
seq_printf(seq, "%s\n", profile->base.name);

security/apparmor/domain.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int may_change_ptraced_domain(const struct cred *to_cred,
7777
/**** TODO: dedup to aa_label_match - needs perm and dfa, merging
7878
* specifically this is an exact copy of aa_label_match except
7979
* aa_compute_perms is replaced with aa_compute_fperms
80-
* and policy.dfa with file.dfa
80+
* and policy->dfa with file->dfa
8181
****/
8282
/* match a profile and its associated ns component if needed
8383
* Assumes visibility test has already been done.
@@ -93,16 +93,16 @@ static inline aa_state_t match_component(struct aa_profile *profile,
9393
const char *ns_name;
9494

9595
if (stack)
96-
state = aa_dfa_match(rules->file.dfa, state, "&");
96+
state = aa_dfa_match(rules->file->dfa, state, "&");
9797
if (profile->ns == tp->ns)
98-
return aa_dfa_match(rules->file.dfa, state, tp->base.hname);
98+
return aa_dfa_match(rules->file->dfa, state, tp->base.hname);
9999

100100
/* try matching with namespace name and then profile */
101101
ns_name = aa_ns_name(profile->ns, tp->ns, true);
102-
state = aa_dfa_match_len(rules->file.dfa, state, ":", 1);
103-
state = aa_dfa_match(rules->file.dfa, state, ns_name);
104-
state = aa_dfa_match_len(rules->file.dfa, state, ":", 1);
105-
return aa_dfa_match(rules->file.dfa, state, tp->base.hname);
102+
state = aa_dfa_match_len(rules->file->dfa, state, ":", 1);
103+
state = aa_dfa_match(rules->file->dfa, state, ns_name);
104+
state = aa_dfa_match_len(rules->file->dfa, state, ":", 1);
105+
return aa_dfa_match(rules->file->dfa, state, tp->base.hname);
106106
}
107107

108108
/**
@@ -150,12 +150,12 @@ static int label_compound_match(struct aa_profile *profile,
150150
label_for_each_cont(i, label, tp) {
151151
if (!aa_ns_visible(profile->ns, tp->ns, subns))
152152
continue;
153-
state = aa_dfa_match(rules->file.dfa, state, "//&");
153+
state = aa_dfa_match(rules->file->dfa, state, "//&");
154154
state = match_component(profile, tp, false, state);
155155
if (!state)
156156
goto fail;
157157
}
158-
*perms = *(aa_lookup_fperms(&(rules->file), state, &cond));
158+
*perms = *(aa_lookup_fperms(rules->file, state, &cond));
159159
aa_apply_modes_to_perms(profile, perms);
160160
if ((perms->allow & request) != request)
161161
return -EACCES;
@@ -210,7 +210,7 @@ static int label_components_match(struct aa_profile *profile,
210210
return 0;
211211

212212
next:
213-
tmp = *(aa_lookup_fperms(&(rules->file), state, &cond));
213+
tmp = *(aa_lookup_fperms(rules->file, state, &cond));
214214
aa_apply_modes_to_perms(profile, &tmp);
215215
aa_perms_accum(perms, &tmp);
216216
label_for_each_cont(i, label, tp) {
@@ -219,7 +219,7 @@ static int label_components_match(struct aa_profile *profile,
219219
state = match_component(profile, tp, stack, start);
220220
if (!state)
221221
goto fail;
222-
tmp = *(aa_lookup_fperms(&(rules->file), state, &cond));
222+
tmp = *(aa_lookup_fperms(rules->file, state, &cond));
223223
aa_apply_modes_to_perms(profile, &tmp);
224224
aa_perms_accum(perms, &tmp);
225225
}
@@ -317,7 +317,7 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
317317
might_sleep();
318318

319319
/* transition from exec match to xattr set */
320-
state = aa_dfa_outofband_transition(attach->xmatch.dfa, state);
320+
state = aa_dfa_outofband_transition(attach->xmatch->dfa, state);
321321
d = bprm->file->f_path.dentry;
322322

323323
for (i = 0; i < attach->xattr_count; i++) {
@@ -331,20 +331,20 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
331331
* that not present xattr can be distinguished from a 0
332332
* length value or rule that matches any value
333333
*/
334-
state = aa_dfa_null_transition(attach->xmatch.dfa,
334+
state = aa_dfa_null_transition(attach->xmatch->dfa,
335335
state);
336336
/* Check xattr value */
337-
state = aa_dfa_match_len(attach->xmatch.dfa, state,
337+
state = aa_dfa_match_len(attach->xmatch->dfa, state,
338338
value, size);
339-
index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
340-
perm = attach->xmatch.perms[index].allow;
339+
index = ACCEPT_TABLE(attach->xmatch->dfa)[state];
340+
perm = attach->xmatch->perms[index].allow;
341341
if (!(perm & MAY_EXEC)) {
342342
ret = -EINVAL;
343343
goto out;
344344
}
345345
}
346346
/* transition to next element */
347-
state = aa_dfa_outofband_transition(attach->xmatch.dfa, state);
347+
state = aa_dfa_outofband_transition(attach->xmatch->dfa, state);
348348
if (size < 0) {
349349
/*
350350
* No xattr match, so verify if transition to
@@ -413,16 +413,16 @@ static struct aa_label *find_attach(const struct linux_binprm *bprm,
413413
* as another profile, signal a conflict and refuse to
414414
* match.
415415
*/
416-
if (attach->xmatch.dfa) {
416+
if (attach->xmatch->dfa) {
417417
unsigned int count;
418418
aa_state_t state;
419419
u32 index, perm;
420420

421-
state = aa_dfa_leftmatch(attach->xmatch.dfa,
422-
attach->xmatch.start[AA_CLASS_XMATCH],
421+
state = aa_dfa_leftmatch(attach->xmatch->dfa,
422+
attach->xmatch->start[AA_CLASS_XMATCH],
423423
name, &count);
424-
index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
425-
perm = attach->xmatch.perms[index].allow;
424+
index = ACCEPT_TABLE(attach->xmatch->dfa)[state];
425+
perm = attach->xmatch->perms[index].allow;
426426
/* any accepting state means a valid match. */
427427
if (perm & MAY_EXEC) {
428428
int ret = 0;
@@ -525,7 +525,7 @@ struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
525525
/* TODO: move lookup parsing to unpack time so this is a straight
526526
* index into the resultant label
527527
*/
528-
for (*name = rules->file.trans.table[index]; !label && *name;
528+
for (*name = rules->file->trans.table[index]; !label && *name;
529529
*name = next_name(xtype, *name)) {
530530
if (xindex & AA_X_CHILD) {
531531
struct aa_profile *new_profile;
@@ -579,7 +579,7 @@ static struct aa_label *x_to_label(struct aa_profile *profile,
579579
break;
580580
case AA_X_TABLE:
581581
/* TODO: fix when perm mapping done at unload */
582-
stack = rules->file.trans.table[xindex & AA_X_INDEX_MASK];
582+
stack = rules->file->trans.table[xindex & AA_X_INDEX_MASK];
583583
if (*stack != '&') {
584584
/* released by caller */
585585
new = x_table_lookup(profile, xindex, lookupname);
@@ -638,7 +638,7 @@ static struct aa_label *profile_transition(const struct cred *subj_cred,
638638
typeof(*rules), list);
639639
struct aa_label *new = NULL;
640640
const char *info = NULL, *name = NULL, *target = NULL;
641-
aa_state_t state = rules->file.start[AA_CLASS_FILE];
641+
aa_state_t state = rules->file->start[AA_CLASS_FILE];
642642
struct aa_perms perms = {};
643643
bool nonewprivs = false;
644644
int error = 0;
@@ -672,7 +672,7 @@ static struct aa_label *profile_transition(const struct cred *subj_cred,
672672
}
673673

674674
/* find exec permissions for name */
675-
state = aa_str_perms(&(rules->file), state, name, cond, &perms);
675+
state = aa_str_perms(rules->file, state, name, cond, &perms);
676676
if (perms.allow & MAY_EXEC) {
677677
/* exec permission determine how to transition */
678678
new = x_to_label(profile, bprm, name, perms.xindex, &target,
@@ -738,7 +738,7 @@ static int profile_onexec(const struct cred *subj_cred,
738738
{
739739
struct aa_ruleset *rules = list_first_entry(&profile->rules,
740740
typeof(*rules), list);
741-
aa_state_t state = rules->file.start[AA_CLASS_FILE];
741+
aa_state_t state = rules->file->start[AA_CLASS_FILE];
742742
struct aa_perms perms = {};
743743
const char *xname = NULL, *info = "change_profile onexec";
744744
int error = -EACCES;
@@ -771,7 +771,7 @@ static int profile_onexec(const struct cred *subj_cred,
771771
}
772772

773773
/* find exec permissions for name */
774-
state = aa_str_perms(&(rules->file), state, xname, cond, &perms);
774+
state = aa_str_perms(rules->file, state, xname, cond, &perms);
775775
if (!(perms.allow & AA_MAY_ONEXEC)) {
776776
info = "no change_onexec valid for executable";
777777
goto audit;
@@ -780,7 +780,7 @@ static int profile_onexec(const struct cred *subj_cred,
780780
* onexec permission is linked to exec with a standard pairing
781781
* exec\0change_profile
782782
*/
783-
state = aa_dfa_null_transition(rules->file.dfa, state);
783+
state = aa_dfa_null_transition(rules->file->dfa, state);
784784
error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
785785
state, &perms);
786786
if (error) {
@@ -1300,7 +1300,7 @@ static int change_profile_perms_wrapper(const char *op, const char *name,
13001300

13011301
if (!error)
13021302
error = change_profile_perms(profile, target, stack, request,
1303-
rules->file.start[AA_CLASS_FILE],
1303+
rules->file->start[AA_CLASS_FILE],
13041304
perms);
13051305
if (error)
13061306
error = aa_audit_file(subj_cred, profile, perms, op, request,

security/apparmor/file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static int __aa_path_perm(const char *op, const struct cred *subj_cred,
236236

237237
if (profile_unconfined(profile))
238238
return 0;
239-
aa_str_perms(&(rules->file), rules->file.start[AA_CLASS_FILE],
239+
aa_str_perms(rules->file, rules->file->start[AA_CLASS_FILE],
240240
name, cond, perms);
241241
if (request & ~perms->allow)
242242
e = -EACCES;
@@ -353,16 +353,16 @@ static int profile_path_link(const struct cred *subj_cred,
353353

354354
error = -EACCES;
355355
/* aa_str_perms - handles the case of the dfa being NULL */
356-
state = aa_str_perms(&(rules->file),
357-
rules->file.start[AA_CLASS_FILE], lname,
356+
state = aa_str_perms(rules->file,
357+
rules->file->start[AA_CLASS_FILE], lname,
358358
cond, &lperms);
359359

360360
if (!(lperms.allow & AA_MAY_LINK))
361361
goto audit;
362362

363363
/* test to see if target can be paired with link */
364-
state = aa_dfa_null_transition(rules->file.dfa, state);
365-
aa_str_perms(&(rules->file), state, tname, cond, &perms);
364+
state = aa_dfa_null_transition(rules->file->dfa, state);
365+
aa_str_perms(rules->file, state, tname, cond, &perms);
366366

367367
/* force audit/quiet masks for link are stored in the second entry
368368
* in the link pair.
@@ -384,7 +384,7 @@ static int profile_path_link(const struct cred *subj_cred,
384384
/* Do link perm subset test requiring allowed permission on link are
385385
* a subset of the allowed permissions on target.
386386
*/
387-
aa_str_perms(&(rules->file), rules->file.start[AA_CLASS_FILE],
387+
aa_str_perms(rules->file, rules->file->start[AA_CLASS_FILE],
388388
tname, cond, &perms);
389389

390390
/* AA_MAY_LINK is not considered in the subset test */

security/apparmor/include/lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "match.h"
1818

19+
extern struct aa_dfa *stacksplitdfa;
20+
1921
/*
2022
* DEBUG remains global (no per profile flag) since it is mostly used in sysctl
2123
* which is not related to profile accesses.

security/apparmor/include/match.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ struct aa_dfa {
102102
struct table_header *tables[YYTD_ID_TSIZE];
103103
};
104104

105-
extern struct aa_dfa *nulldfa;
106-
extern struct aa_dfa *stacksplitdfa;
107-
108105
#define byte_to_byte(X) (X)
109106

110107
#define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \
@@ -122,9 +119,6 @@ static inline size_t table_size(size_t len, size_t el_size)
122119
return ALIGN(sizeof(struct table_header) + len * el_size, 8);
123120
}
124121

125-
int aa_setup_dfa_engine(void);
126-
void aa_teardown_dfa_engine(void);
127-
128122
#define aa_state_t unsigned int
129123

130124
struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);

0 commit comments

Comments
 (0)