Skip to content

Commit a97ee32

Browse files
jrjohansenopsiff
authored andcommitted
apparmor: refcount the pdb
[ Upstream commit 98b824f ] 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> Stable-dep-of: a4c9efa4dbad ("apparmor: make label_match return a consistent value") Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit 32928c1749e8a0162b168a5bb87c1b67e37281ff) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 483bb5c commit a97ee32

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
}
@@ -316,7 +316,7 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
316316
might_sleep();
317317

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

322322
for (i = 0; i < attach->xattr_count; i++) {
@@ -330,20 +330,20 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
330330
* that not present xattr can be distinguished from a 0
331331
* length value or rule that matches any value
332332
*/
333-
state = aa_dfa_null_transition(attach->xmatch.dfa,
333+
state = aa_dfa_null_transition(attach->xmatch->dfa,
334334
state);
335335
/* Check xattr value */
336-
state = aa_dfa_match_len(attach->xmatch.dfa, state,
336+
state = aa_dfa_match_len(attach->xmatch->dfa, state,
337337
value, size);
338-
index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
339-
perm = attach->xmatch.perms[index].allow;
338+
index = ACCEPT_TABLE(attach->xmatch->dfa)[state];
339+
perm = attach->xmatch->perms[index].allow;
340340
if (!(perm & MAY_EXEC)) {
341341
ret = -EINVAL;
342342
goto out;
343343
}
344344
}
345345
/* transition to next element */
346-
state = aa_dfa_outofband_transition(attach->xmatch.dfa, state);
346+
state = aa_dfa_outofband_transition(attach->xmatch->dfa, state);
347347
if (size < 0) {
348348
/*
349349
* No xattr match, so verify if transition to
@@ -412,16 +412,16 @@ static struct aa_label *find_attach(const struct linux_binprm *bprm,
412412
* as another profile, signal a conflict and refuse to
413413
* match.
414414
*/
415-
if (attach->xmatch.dfa) {
415+
if (attach->xmatch->dfa) {
416416
unsigned int count;
417417
aa_state_t state;
418418
u32 index, perm;
419419

420-
state = aa_dfa_leftmatch(attach->xmatch.dfa,
421-
attach->xmatch.start[AA_CLASS_XMATCH],
420+
state = aa_dfa_leftmatch(attach->xmatch->dfa,
421+
attach->xmatch->start[AA_CLASS_XMATCH],
422422
name, &count);
423-
index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
424-
perm = attach->xmatch.perms[index].allow;
423+
index = ACCEPT_TABLE(attach->xmatch->dfa)[state];
424+
perm = attach->xmatch->perms[index].allow;
425425
/* any accepting state means a valid match. */
426426
if (perm & MAY_EXEC) {
427427
int ret = 0;
@@ -524,7 +524,7 @@ struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
524524
/* TODO: move lookup parsing to unpack time so this is a straight
525525
* index into the resultant label
526526
*/
527-
for (*name = rules->file.trans.table[index]; !label && *name;
527+
for (*name = rules->file->trans.table[index]; !label && *name;
528528
*name = next_name(xtype, *name)) {
529529
if (xindex & AA_X_CHILD) {
530530
struct aa_profile *new_profile;
@@ -577,7 +577,7 @@ static struct aa_label *x_to_label(struct aa_profile *profile,
577577
break;
578578
case AA_X_TABLE:
579579
/* TODO: fix when perm mapping done at unload */
580-
stack = rules->file.trans.table[xindex & AA_X_INDEX_MASK];
580+
stack = rules->file->trans.table[xindex & AA_X_INDEX_MASK];
581581
if (*stack != '&') {
582582
/* released by caller */
583583
new = x_table_lookup(profile, xindex, lookupname);
@@ -636,7 +636,7 @@ static struct aa_label *profile_transition(const struct cred *subj_cred,
636636
typeof(*rules), list);
637637
struct aa_label *new = NULL;
638638
const char *info = NULL, *name = NULL, *target = NULL;
639-
aa_state_t state = rules->file.start[AA_CLASS_FILE];
639+
aa_state_t state = rules->file->start[AA_CLASS_FILE];
640640
struct aa_perms perms = {};
641641
bool nonewprivs = false;
642642
int error = 0;
@@ -670,7 +670,7 @@ static struct aa_label *profile_transition(const struct cred *subj_cred,
670670
}
671671

672672
/* find exec permissions for name */
673-
state = aa_str_perms(&(rules->file), state, name, cond, &perms);
673+
state = aa_str_perms(rules->file, state, name, cond, &perms);
674674
if (perms.allow & MAY_EXEC) {
675675
/* exec permission determine how to transition */
676676
new = x_to_label(profile, bprm, name, perms.xindex, &target,
@@ -736,7 +736,7 @@ static int profile_onexec(const struct cred *subj_cred,
736736
{
737737
struct aa_ruleset *rules = list_first_entry(&profile->rules,
738738
typeof(*rules), list);
739-
aa_state_t state = rules->file.start[AA_CLASS_FILE];
739+
aa_state_t state = rules->file->start[AA_CLASS_FILE];
740740
struct aa_perms perms = {};
741741
const char *xname = NULL, *info = "change_profile onexec";
742742
int error = -EACCES;
@@ -769,7 +769,7 @@ static int profile_onexec(const struct cred *subj_cred,
769769
}
770770

771771
/* find exec permissions for name */
772-
state = aa_str_perms(&(rules->file), state, xname, cond, &perms);
772+
state = aa_str_perms(rules->file, state, xname, cond, &perms);
773773
if (!(perms.allow & AA_MAY_ONEXEC)) {
774774
info = "no change_onexec valid for executable";
775775
goto audit;
@@ -778,7 +778,7 @@ static int profile_onexec(const struct cred *subj_cred,
778778
* onexec permission is linked to exec with a standard pairing
779779
* exec\0change_profile
780780
*/
781-
state = aa_dfa_null_transition(rules->file.dfa, state);
781+
state = aa_dfa_null_transition(rules->file->dfa, state);
782782
error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
783783
state, &perms);
784784
if (error) {
@@ -1298,7 +1298,7 @@ static int change_profile_perms_wrapper(const char *op, const char *name,
12981298

12991299
if (!error)
13001300
error = change_profile_perms(profile, target, stack, request,
1301-
rules->file.start[AA_CLASS_FILE],
1301+
rules->file->start[AA_CLASS_FILE],
13021302
perms);
13031303
if (error)
13041304
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)