apparmor: fix out-of-bounds write when null terminating a label vec
Subsystems (2)
PATCH 1/1 apparmor: fix out-of-bounds write when null terminating a label vec
aa_vec_unique() null terminates at vec[n - dups] when VEC_FLAG_TERMINATE is passed. If the components are all distinct no duplicates are dropped, dups is 0 and the terminator goes to vec[n], so the caller has to provide room for n + 1 entries.
aa_label_strn_parse() sets up its vector with vec_setup(profile, vec, len, gfp) and then calls aa_vec_unique(vec, len, VEC_FLAG_TERMINATE), but vec_setup() does not reserve the terminator entry. Up to LOCAL_VEC_ENTRIES it uses the local array of LOCAL_VEC_ENTRIES pointers, above that it allocates exactly len pointers. The terminator therefore lands one entry past the end of the local array when len is LOCAL_VEC_ENTRIES, and one entry past the end of the allocation when len is larger.
len comes from the number of "//&" separated components in the label name and label_count_strn_entries() does not bound it. An unprivileged task reaches the parse by writing to /proc/self/attr/apparmor/current or through lsm_set_self_attr(2), both of which go through do_setattr(), and the name is parsed before the change_profile permission is checked.
The query_label() path behind the securityfs .access file, which is mode 0666, performs no permission check at all. Every component has to resolve to a loaded profile, so a system with policy loaded is required.
The other two VEC_FLAG_TERMINATE users work on a label vec that aa_label_alloc() has already sized with "+ 1 for null terminator entry on vec". Reserve the same entry in vec_setup() and DEFINE_VEC(). Passing len + 1 from the caller instead would move len == LOCAL_VEC_ENTRIES out of the local array and into kzalloc().
--- security/apparmor/include/label.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
security/apparmor/include/label.h +3 -3
@@ -23,7 +23,7 @@ struct aa_ruleset;#define LOCAL_VEC_ENTRIES 8#define DEFINE_VEC (T ,V ) \ - struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \ + struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES + 1]; \ struct aa_ ## T **(V)#define vec_setup (T ,V ,N ,GFP ) \
@@ -31,10 +31,10 @@ struct aa_ruleset;if ((N ) <=LOCAL_VEC_ENTRIES ) { \typeof (N) i; \ (V) = (_ ## V ## _localtmp); \ - for (i = 0; i < (N); i++) \ + for (i = 0; i <= (N); i++) \ (V)[i] = NULL; \ } else \ - (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \ + (V) = kzalloc_objs(struct aa_ ## T *, (N) + 1, (GFP)); \ (V) ? 0 : -ENOMEM; \ })
Signature · 1 line
2.43.0
Quoted 28 lines · Hyunwoo Kim
aa_vec_unique()null terminates at vec[n - dups] whenVEC_FLAG_TERMINATEis passed. If the components are all distinct no duplicates are dropped, dups is 0 and the terminator goes to vec[n], so the caller has to provide room for n + 1 entries.
aa_label_strn_parse()sets up its vector withvec_setup(profile, vec, len, gfp)and then callsaa_vec_unique(vec, len, VEC_FLAG_TERMINATE), butvec_setup()does not reserve the terminator entry. Up toLOCAL_VEC_ENTRIESit uses the local array ofLOCAL_VEC_ENTRIESpointers, above that it allocates exactly len pointers. The terminator therefore lands one entry past the end of the local array when len isLOCAL_VEC_ENTRIES, and one entry past the end of the allocation when len is larger.len comes from the number of "//&" separated components in the label name and
label_count_strn_entries()does not bound it. An unprivileged task reaches the parse by writing to/proc/self/attr/apparmor/currentor throughlsm_set_self_attr(2), both of which go throughdo_setattr(), and the name is parsed before thechange_profilepermission is checked.
Thequery_label()path behind the securityfs .access file, which is mode 0666, performs no permission check at all. Every component has to resolve to a loaded profile, so a system with policy loaded is required.The other two
VEC_FLAG_TERMINATEusers work on a label vec thataa_label_alloc()has already sized with "+ 1 for null terminator entry on vec". Reserve the same entry invec_setup()andDEFINE_VEC(). Passing len + 1 from the caller instead would move len ==LOCAL_VEC_ENTRIESout of the local array and intokzalloc().
Quoted 3 lines
Fixes f1bd904175e8 ("apparmor: add the base fns() for domain labels")Signed-off-by Hyunwoo Kim <[email protected]>
I have pulled this into my tree
Quoted 30 lines
--- security/apparmor/include/label.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)security/apparmor/include/label.h +3
-3@@ -23,7 +23,7 @@ struct aa_ruleset;#define LOCAL_VEC_ENTRIES 8#define DEFINE_VEC (T ,V ) \ - struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES]; \ + struct aa_ ## T *(_ ## V ## _localtmp)[LOCAL_VEC_ENTRIES + 1]; \ struct aa_ ## T **(V)#define vec_setup (T ,V ,N ,GFP ) \@@ -31,10 +31,10 @@ struct aa_ruleset;if ((N ) <=LOCAL_VEC_ENTRIES ) { \typeof (N) i; \ (V) = (_ ## V ## _localtmp); \ - for (i = 0; i < (N); i++) \ + for (i = 0; i <= (N); i++) \ (V)[i] = NULL; \ } else \ - (V) = kzalloc(sizeof(struct aa_ ## T *) * (N), (GFP)); \ + (V) = kzalloc_objs(struct aa_ ## T *, (N) + 1, (GFP)); \ (V) ? 0 : -ENOMEM; \ })