Skip to content

Commit 9caafbe

Browse files
mjg59jrjohansen
authored andcommitted
apparmor: Parse secmark policy
Add support for parsing secmark policy provided by userspace, and store that in the overall policy. Signed-off-by: Matthew Garrett <mjg59@google.com> Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 617a629 commit 9caafbe

4 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎security/apparmor/include/net.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ struct aa_sk_ctx {
8383
__e; \
8484
})
8585

86+
struct aa_secmark {
87+
u8 audit;
88+
u8 deny;
89+
u32 secid;
90+
char *label;
91+
};
92+
8693
extern struct aa_sfs_entry aa_sfs_entry_network[];
8794

8895
void audit_net_cb(struct audit_buffer *ab, void *va);
@@ -103,4 +110,7 @@ int aa_sk_perm(const char *op, u32 request, struct sock *sk);
103110
int aa_sock_file_perm(struct aa_label *label, const char *op, u32 request,
104111
struct socket *sock);
105112

113+
int apparmor_secmark_check(struct aa_label *label, char *op, u32 request,
114+
u32 secid, struct sock *sk);
115+
106116
#endif /* __AA_NET_H */

‎security/apparmor/include/policy.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ struct aa_profile {
155155

156156
struct aa_rlimit rlimits;
157157

158+
int secmark_count;
159+
struct aa_secmark *secmark;
160+
158161
struct aa_loaddata *rawdata;
159162
unsigned char *hash;
160163
char *dirname;

‎security/apparmor/policy.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ void aa_free_profile(struct aa_profile *profile)
231231
for (i = 0; i < profile->xattr_count; i++)
232232
kzfree(profile->xattrs[i]);
233233
kzfree(profile->xattrs);
234+
for (i=0; i < profile->secmark_count; i++)
235+
kzfree(profile->secmark[i].label);
236+
kzfree(profile->secmark);
234237
kzfree(profile->dirname);
235238
aa_put_dfa(profile->xmatch);
236239
aa_put_dfa(profile->policy.dfa);

‎security/apparmor/policy_unpack.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
292292
return 0;
293293
}
294294

295+
static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name)
296+
{
297+
if (unpack_nameX(e, AA_U8, name)) {
298+
if (!inbounds(e, sizeof(u8)))
299+
return 0;
300+
if (data)
301+
*data = get_unaligned((u8 *)e->pos);
302+
e->pos += sizeof(u8);
303+
return 1;
304+
}
305+
return 0;
306+
}
307+
295308
static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
296309
{
297310
if (unpack_nameX(e, AA_U32, name)) {
@@ -529,6 +542,49 @@ static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile)
529542
return 0;
530543
}
531544

545+
static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile)
546+
{
547+
void *pos = e->pos;
548+
int i, size;
549+
550+
if (unpack_nameX(e, AA_STRUCT, "secmark")) {
551+
size = unpack_array(e, NULL);
552+
553+
profile->secmark = kcalloc(size, sizeof(struct aa_secmark),
554+
GFP_KERNEL);
555+
if (!profile->secmark)
556+
goto fail;
557+
558+
profile->secmark_count = size;
559+
560+
for (i = 0; i < size; i++) {
561+
if (!unpack_u8(e, &profile->secmark[i].audit, NULL))
562+
goto fail;
563+
if (!unpack_u8(e, &profile->secmark[i].deny, NULL))
564+
goto fail;
565+
if (!unpack_strdup(e, &profile->secmark[i].label, NULL))
566+
goto fail;
567+
}
568+
if (!unpack_nameX(e, AA_ARRAYEND, NULL))
569+
goto fail;
570+
if (!unpack_nameX(e, AA_STRUCTEND, NULL))
571+
goto fail;
572+
}
573+
574+
return 1;
575+
576+
fail:
577+
if (profile->secmark) {
578+
for (i = 0; i < size; i++)
579+
kfree(profile->secmark[i].label);
580+
kfree(profile->secmark);
581+
profile->secmark_count = 0;
582+
}
583+
584+
e->pos = pos;
585+
return 0;
586+
}
587+
532588
static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
533589
{
534590
void *pos = e->pos;
@@ -727,6 +783,11 @@ static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
727783
goto fail;
728784
}
729785

786+
if (!unpack_secmark(e, profile)) {
787+
info = "failed to unpack profile secmark rules";
788+
goto fail;
789+
}
790+
730791
if (unpack_nameX(e, AA_STRUCT, "policydb")) {
731792
/* generic policy dfa - optional and may be NULL */
732793
info = "failed to unpack policydb";

0 commit comments

Comments
 (0)