Skip to content

Commit 0df34a6

Browse files
committed
apparmor: add outofband transition and use it in xattr match
There are cases where the a special out of band transition that can not be triggered by input is useful in separating match conditions in the dfa encoding. The null_transition is currently used as an out of band transition for match conditions that can not contain a \0 in their input but apparmor needs an out of band transition for cases where the match condition is allowed to contain any input character. Achieve this by allowing for an explicit transition out of input range that can only be triggered by code. Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent f05841a commit 0df34a6

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎security/apparmor/apparmorfs.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
591591

592592
void __aa_bump_ns_revision(struct aa_ns *ns)
593593
{
594-
WRITE_ONCE(ns->revision, ns->revision + 1);
594+
WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1);
595595
wake_up_interruptible(&ns->wait);
596596
}
597597

@@ -2331,6 +2331,8 @@ static struct aa_sfs_entry aa_sfs_entry_versions[] = {
23312331
static struct aa_sfs_entry aa_sfs_entry_policy[] = {
23322332
AA_SFS_DIR("versions", aa_sfs_entry_versions),
23332333
AA_SFS_FILE_BOOLEAN("set_load", 1),
2334+
/* number of out of band transitions supported */
2335+
AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED),
23342336
{ }
23352337
};
23362338

‎security/apparmor/domain.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
320320
might_sleep();
321321

322322
/* transition from exec match to xattr set */
323-
state = aa_dfa_null_transition(profile->xmatch, state);
324-
323+
state = aa_dfa_outofband_transition(profile->xmatch, state);
325324
d = bprm->file->f_path.dentry;
326325

327326
for (i = 0; i < profile->xattr_count; i++) {
@@ -330,7 +329,13 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
330329
if (size >= 0) {
331330
u32 perm;
332331

333-
/* Check the xattr value, not just presence */
332+
/*
333+
* Check the xattr presence before value. This ensure
334+
* that not present xattr can be distinguished from a 0
335+
* length value or rule that matches any value
336+
*/
337+
state = aa_dfa_null_transition(profile->xmatch, state);
338+
/* Check xattr value */
334339
state = aa_dfa_match_len(profile->xmatch, state, value,
335340
size);
336341
perm = dfa_user_allow(profile->xmatch, state);
@@ -340,7 +345,7 @@ static int aa_xattrs_match(const struct linux_binprm *bprm,
340345
}
341346
}
342347
/* transition to next element */
343-
state = aa_dfa_null_transition(profile->xmatch, state);
348+
state = aa_dfa_outofband_transition(profile->xmatch, state);
344349
if (size < 0) {
345350
/*
346351
* No xattr match, so verify if transition to

‎security/apparmor/include/match.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737

3838
#define YYTH_MAGIC 0x1B5E783D
3939
#define YYTH_FLAG_DIFF_ENCODE 1
40+
#define YYTH_FLAG_OOB_TRANS 2
41+
#define YYTH_FLAGS (YYTH_FLAG_DIFF_ENCODE | YYTH_FLAG_OOB_TRANS)
42+
43+
#define MAX_OOB_SUPPORTED 1
4044

4145
struct table_set_header {
4246
u32 th_magic; /* YYTH_MAGIC */
@@ -94,6 +98,7 @@ struct table_header {
9498
struct aa_dfa {
9599
struct kref count;
96100
u16 flags;
101+
u32 max_oob;
97102
struct table_header *tables[YYTD_ID_TSIZE];
98103
};
99104

@@ -127,6 +132,8 @@ unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
127132
const char *str);
128133
unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
129134
const char c);
135+
unsigned int aa_dfa_outofband_transition(struct aa_dfa *dfa,
136+
unsigned int state);
130137
unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
131138
const char *str, const char **retpos);
132139
unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
@@ -183,7 +190,7 @@ static inline void aa_put_dfa(struct aa_dfa *dfa)
183190
#define MARK_DIFF_ENCODE 0x40000000
184191
#define MATCH_FLAG_OOB_TRANSITION 0x20000000
185192
#define MATCH_FLAGS_MASK 0xff000000
186-
#define MATCH_FLAGS_VALID MATCH_FLAG_DIFF_ENCODE
193+
#define MATCH_FLAGS_VALID (MATCH_FLAG_DIFF_ENCODE | MATCH_FLAG_OOB_TRANSITION)
187194
#define MATCH_FLAGS_INVALID (MATCH_FLAGS_MASK & ~MATCH_FLAGS_VALID)
188195

189196
#endif /* __AA_MATCH_H */

‎security/apparmor/match.c‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ static int verify_dfa(struct aa_dfa *dfa)
212212
goto out;
213213
}
214214
}
215+
if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_OOB_TRANSITION)) {
216+
if (base_idx(BASE_TABLE(dfa)[i]) < dfa->max_oob) {
217+
pr_err("AppArmor DFA out of bad transition out of range");
218+
goto out;
219+
}
220+
if (!(dfa->flags & YYTH_FLAG_OOB_TRANS)) {
221+
pr_err("AppArmor DFA out of bad transition state without header flag");
222+
goto out;
223+
}
224+
}
215225
if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
216226
pr_err("AppArmor DFA next/check upper bounds error\n");
217227
goto out;
@@ -314,9 +324,23 @@ struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
314324
goto fail;
315325

316326
dfa->flags = ntohs(*(__be16 *) (data + 12));
317-
if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE)
327+
if (dfa->flags & ~(YYTH_FLAGS))
318328
goto fail;
319329

330+
/*
331+
* TODO: needed for dfa to support more than 1 oob
332+
* if (dfa->flags & YYTH_FLAGS_OOB_TRANS) {
333+
* if (hsize < 16 + 4)
334+
* goto fail;
335+
* dfa->max_oob = ntol(*(__be32 *) (data + 16));
336+
* if (dfa->max <= MAX_OOB_SUPPORTED) {
337+
* pr_err("AppArmor DFA OOB greater than supported\n");
338+
* goto fail;
339+
* }
340+
* }
341+
*/
342+
dfa->max_oob = 1;
343+
320344
data += hsize;
321345
size -= hsize;
322346

@@ -505,6 +529,23 @@ unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
505529
return state;
506530
}
507531

532+
unsigned int aa_dfa_outofband_transition(struct aa_dfa *dfa, unsigned int state)
533+
{
534+
u16 *def = DEFAULT_TABLE(dfa);
535+
u32 *base = BASE_TABLE(dfa);
536+
u16 *next = NEXT_TABLE(dfa);
537+
u16 *check = CHECK_TABLE(dfa);
538+
u32 b = (base)[(state)];
539+
540+
if (!(b & MATCH_FLAG_OOB_TRANSITION))
541+
return DFA_NOMATCH;
542+
543+
/* No Equivalence class remapping for outofband transitions */
544+
match_char(state, def, base, next, check, -1);
545+
546+
return state;
547+
}
548+
508549
/**
509550
* aa_dfa_match_until - traverse @dfa until accept state or end of input
510551
* @dfa: the dfa to match @str against (NOT NULL)

0 commit comments

Comments
 (0)