2 * AppArmor security module
4 * This file contains AppArmor functions for unpacking policy loaded from
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2 of the
15 * AppArmor uses a serialized binary format for loading policy. To find
16 * policy format documentation look in Documentation/security/apparmor.txt
17 * All policy is validated before it is used.
20 #include <asm/unaligned.h>
21 #include <linux/ctype.h>
22 #include <linux/errno.h>
24 #include "include/apparmor.h"
25 #include "include/audit.h"
26 #include "include/context.h"
27 #include "include/crypto.h"
28 #include "include/match.h"
29 #include "include/policy.h"
30 #include "include/policy_unpack.h"
32 #define K_ABI_MASK 0x3ff
33 #define FORCE_COMPLAIN_FLAG 0x800
34 #define VERSION_LT(X, Y) (((X) & K_ABI_MASK) < ((Y) & K_ABI_MASK))
35 #define VERSION_GT(X, Y) (((X) & K_ABI_MASK) > ((Y) & K_ABI_MASK))
37 #define v5 5 /* base version */
38 #define v6 6 /* per entry policydb mediation check */
39 #define v7 7 /* full network masking */
42 * The AppArmor interface treats data as a type byte followed by the
43 * actual data. The interface has the notion of a a named entry
44 * which has a name (AA_NAME typecode followed by name string) followed by
45 * the entries typecode and data. Named types allow for optional
46 * elements and extensions to be added and tested for without breaking
47 * backwards compatibility.
55 AA_NAME, /* same as string except it is items name */
67 * aa_ext is the read of the buffer containing the serialized profile. The
68 * data is copied into a kernel buffer in apparmorfs and then handed off to
69 * the unpack routines.
74 void *pos; /* pointer to current position in the buffer */
78 /* audit callback for unpack fields */
79 static void audit_cb(struct audit_buffer *ab, void *va)
81 struct common_audit_data *sa = va;
82 if (sa->aad->iface.target) {
83 struct aa_profile *name = sa->aad->iface.target;
84 audit_log_format(ab, " name=");
85 audit_log_untrustedstring(ab, name->base.hname);
87 if (sa->aad->iface.pos)
88 audit_log_format(ab, " offset=%ld", sa->aad->iface.pos);
92 * audit_iface - do audit message for policy unpacking/load/replace/remove
93 * @new: profile if it has been allocated (MAYBE NULL)
94 * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL)
95 * @name: name of the profile being manipulated (MAYBE NULL)
96 * @info: any extra info about the failure (MAYBE NULL)
97 * @e: buffer position info
100 * Returns: %0 or error
102 static int audit_iface(struct aa_profile *new, const char *ns_name,
103 const char *name, const char *info, struct aa_ext *e,
106 struct aa_profile *profile = __aa_current_profile();
107 struct common_audit_data sa;
108 struct apparmor_audit_data aad = {0,};
109 sa.type = LSM_AUDIT_DATA_NONE;
111 aad.iface.ns = ns_name;
113 aad.iface.pos = e->pos - e->start;
114 aad.iface.target = new;
119 return aa_audit(AUDIT_APPARMOR_STATUS, profile, GFP_KERNEL, &sa,
123 void aa_loaddata_kref(struct kref *kref)
125 struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
133 /* test if read will be in packed data bounds */
134 static bool inbounds(struct aa_ext *e, size_t size)
136 return (size <= e->end - e->pos);
140 * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
141 * @e: serialized data read head (NOT NULL)
142 * @chunk: start address for chunk of data (NOT NULL)
144 * Returns: the size of chunk found with the read head at the end of the chunk.
146 static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
150 if (!inbounds(e, sizeof(u16)))
152 size = le16_to_cpu(get_unaligned((u16 *) e->pos));
153 e->pos += sizeof(u16);
154 if (!inbounds(e, size))
161 /* unpack control byte */
162 static bool unpack_X(struct aa_ext *e, enum aa_code code)
166 if (*(u8 *) e->pos != code)
173 * unpack_nameX - check is the next element is of type X with a name of @name
174 * @e: serialized data extent information (NOT NULL)
176 * @name: name to match to the serialized element. (MAYBE NULL)
178 * check that the next serialized data element is of type X and has a tag
179 * name @name. If @name is specified then there must be a matching
180 * name element in the stream. If @name is NULL any name element will be
181 * skipped and only the typecode will be tested.
183 * Returns 1 on success (both type code and name tests match) and the read
184 * head is advanced past the headers
186 * Returns: 0 if either match fails, the read head does not move
188 static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
191 * May need to reset pos if name or type doesn't match
195 * Check for presence of a tagname, and if present name size
196 * AA_NAME tag value is a u16.
198 if (unpack_X(e, AA_NAME)) {
200 size_t size = unpack_u16_chunk(e, &tag);
201 /* if a name is specified it must match. otherwise skip tag */
202 if (name && (!size || strcmp(name, tag)))
205 /* if a name is specified and there is no name tag fail */
209 /* now check if type code matches */
210 if (unpack_X(e, code))
218 static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
220 if (unpack_nameX(e, AA_U32, name)) {
221 if (!inbounds(e, sizeof(u32)))
224 *data = le32_to_cpu(get_unaligned((u32 *) e->pos));
225 e->pos += sizeof(u32);
231 static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
233 if (unpack_nameX(e, AA_U64, name)) {
234 if (!inbounds(e, sizeof(u64)))
237 *data = le64_to_cpu(get_unaligned((u64 *) e->pos));
238 e->pos += sizeof(u64);
244 static size_t unpack_array(struct aa_ext *e, const char *name)
246 if (unpack_nameX(e, AA_ARRAY, name)) {
248 if (!inbounds(e, sizeof(u16)))
250 size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos));
251 e->pos += sizeof(u16);
257 static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
259 if (unpack_nameX(e, AA_BLOB, name)) {
261 if (!inbounds(e, sizeof(u32)))
263 size = le32_to_cpu(get_unaligned((u32 *) e->pos));
264 e->pos += sizeof(u32);
265 if (inbounds(e, (size_t) size)) {
274 static int unpack_str(struct aa_ext *e, const char **string, const char *name)
280 if (unpack_nameX(e, AA_STRING, name)) {
281 size = unpack_u16_chunk(e, &src_str);
283 /* strings are null terminated, length is size - 1 */
284 if (src_str[size - 1] != 0)
296 static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
300 int res = unpack_str(e, &tmp, name);
306 *string = kmemdup(tmp, res, GFP_KERNEL);
315 #define DFA_VALID_PERM_MASK 0xffffffff
316 #define DFA_VALID_PERM2_MASK 0xffffffff
319 * verify_accept - verify the accept tables of a dfa
320 * @dfa: dfa to verify accept tables of (NOT NULL)
321 * @flags: flags governing dfa
323 * Returns: 1 if valid accept tables else 0 if error
325 static bool verify_accept(struct aa_dfa *dfa, int flags)
329 /* verify accept permissions */
330 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
331 int mode = ACCEPT_TABLE(dfa)[i];
333 if (mode & ~DFA_VALID_PERM_MASK)
336 if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
343 * unpack_dfa - unpack a file rule dfa
344 * @e: serialized data extent information (NOT NULL)
346 * returns dfa or ERR_PTR or NULL if no dfa
348 static struct aa_dfa *unpack_dfa(struct aa_ext *e)
352 struct aa_dfa *dfa = NULL;
354 size = unpack_blob(e, &blob, "aadfa");
357 * The dfa is aligned with in the blob to 8 bytes
358 * from the beginning of the stream.
359 * alignment adjust needed by dfa unpack
361 size_t sz = blob - (char *) e->start -
362 ((e->pos - e->start) & 7);
363 size_t pad = ALIGN(sz, 8) - sz;
364 int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
365 TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
366 dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
371 if (!verify_accept(dfa, flags))
379 return ERR_PTR(-EPROTO);
383 * unpack_trans_table - unpack a profile transition table
384 * @e: serialized data extent information (NOT NULL)
385 * @profile: profile to add the accept table to (NOT NULL)
387 * Returns: 1 if table successfully unpacked
389 static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
393 /* exec table is optional */
394 if (unpack_nameX(e, AA_STRUCT, "xtable")) {
397 size = unpack_array(e, NULL);
398 /* currently 4 exec bits and entries 0-3 are reserved iupcx */
401 profile->file.trans.table = kzalloc(sizeof(char *) * size,
403 if (!profile->file.trans.table)
406 profile->file.trans.size = size;
407 for (i = 0; i < size; i++) {
409 int c, j, size2 = unpack_strdup(e, &str, NULL);
410 /* unpack_strdup verifies that the last character is
411 * null termination byte.
415 profile->file.trans.table[i] = str;
416 /* verify that name doesn't start with space */
420 /* count internal # of internal \0 */
421 for (c = j = 0; j < size2 - 2; j++) {
426 /* beginning with : requires an embedded \0,
427 * verify that exactly 1 internal \0 exists
428 * trailing \0 already verified by unpack_strdup
432 /* first character after : must be valid */
436 /* fail - all other cases with embedded \0 */
439 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
441 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
447 aa_free_domain_entries(&profile->file.trans);
452 static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
456 /* rlimits are optional */
457 if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
460 if (!unpack_u32(e, &tmp, NULL))
462 profile->rlimits.mask = tmp;
464 size = unpack_array(e, NULL);
465 if (size > RLIM_NLIMITS)
467 for (i = 0; i < size; i++) {
469 int a = aa_map_resource(i);
470 if (!unpack_u64(e, &tmp2, NULL))
472 profile->rlimits.limits[a].rlim_max = tmp2;
474 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
476 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
487 * unpack_profile - unpack a serialized profile
488 * @e: serialized data extent information (NOT NULL)
490 * NOTE: unpack profile sets audit struct if there is a failure
492 static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
494 struct aa_profile *profile = NULL;
495 const char *tmpname, *tmpns = NULL, *name = NULL;
497 int i, error = -EPROTO;
503 /* check that we have the right struct being passed */
504 if (!unpack_nameX(e, AA_STRUCT, "profile"))
506 if (!unpack_str(e, &name, NULL))
511 tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
513 *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
519 profile = aa_alloc_profile(name, GFP_KERNEL);
521 return ERR_PTR(-ENOMEM);
523 /* profile renaming is optional */
524 (void) unpack_str(e, &profile->rename, "rename");
526 /* attachment string is optional */
527 (void) unpack_str(e, &profile->attach, "attach");
529 /* xmatch is optional and may be NULL */
530 profile->xmatch = unpack_dfa(e);
531 if (IS_ERR(profile->xmatch)) {
532 error = PTR_ERR(profile->xmatch);
533 profile->xmatch = NULL;
536 /* xmatch_len is not optional if xmatch is set */
537 if (profile->xmatch) {
538 if (!unpack_u32(e, &tmp, NULL))
540 profile->xmatch_len = tmp;
543 /* per profile debug flags (complain, audit) */
544 if (!unpack_nameX(e, AA_STRUCT, "flags"))
546 if (!unpack_u32(e, &tmp, NULL))
548 if (tmp & PACKED_FLAG_HAT)
549 profile->flags |= PFLAG_HAT;
550 if (!unpack_u32(e, &tmp, NULL))
552 if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
553 profile->mode = APPARMOR_COMPLAIN;
554 else if (tmp == PACKED_MODE_KILL)
555 profile->mode = APPARMOR_KILL;
556 else if (tmp == PACKED_MODE_UNCONFINED)
557 profile->mode = APPARMOR_UNCONFINED;
558 if (!unpack_u32(e, &tmp, NULL))
561 profile->audit = AUDIT_ALL;
563 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
566 /* path_flags is optional */
567 if (unpack_u32(e, &profile->path_flags, "path_flags"))
568 profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
570 /* set a default value if path_flags field is not present */
571 profile->path_flags = PFLAG_MEDIATE_DELETED;
573 if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
575 if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
577 if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
579 if (!unpack_u32(e, &tmpcap.cap[0], NULL))
582 if (unpack_nameX(e, AA_STRUCT, "caps64")) {
583 /* optional upper half of 64 bit caps */
584 if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
586 if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
588 if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
590 if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
592 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
596 if (unpack_nameX(e, AA_STRUCT, "capsx")) {
597 /* optional extended caps mediation mask */
598 if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
600 if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
602 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
606 if (!unpack_rlimits(e, profile))
609 if (unpack_nameX(e, AA_STRUCT, "policydb")) {
610 /* generic policy dfa - optional and may be NULL */
611 profile->policy.dfa = unpack_dfa(e);
612 if (IS_ERR(profile->policy.dfa)) {
613 error = PTR_ERR(profile->policy.dfa);
614 profile->policy.dfa = NULL;
616 } else if (!profile->policy.dfa) {
620 if (!unpack_u32(e, &profile->policy.start[0], "start"))
621 /* default start state */
622 profile->policy.start[0] = DFA_START;
623 /* setup class index */
624 for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
625 profile->policy.start[i] =
626 aa_dfa_next(profile->policy.dfa,
627 profile->policy.start[0],
630 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
633 profile->policy.dfa = aa_get_dfa(nulldfa);
636 profile->file.dfa = unpack_dfa(e);
637 if (IS_ERR(profile->file.dfa)) {
638 error = PTR_ERR(profile->file.dfa);
639 profile->file.dfa = NULL;
641 } else if (profile->file.dfa) {
642 if (!unpack_u32(e, &profile->file.start, "dfa_start"))
643 /* default start state */
644 profile->file.start = DFA_START;
645 } else if (profile->policy.dfa &&
646 profile->policy.start[AA_CLASS_FILE]) {
647 profile->file.dfa = aa_get_dfa(profile->policy.dfa);
648 profile->file.start = profile->policy.start[AA_CLASS_FILE];
650 profile->file.dfa = aa_get_dfa(nulldfa);
652 if (!unpack_trans_table(e, profile))
655 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
665 audit_iface(profile, NULL, name, "failed to unpack profile", e,
667 aa_free_profile(profile);
669 return ERR_PTR(error);
673 * verify_head - unpack serialized stream header
674 * @e: serialized data read head (NOT NULL)
675 * @required: whether the header is required or optional
676 * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
678 * Returns: error or 0 if header is good
680 static int verify_header(struct aa_ext *e, int required, const char **ns)
682 int error = -EPROTONOSUPPORT;
683 const char *name = NULL;
686 /* get the interface version */
687 if (!unpack_u32(e, &e->version, "version")) {
689 audit_iface(NULL, NULL, NULL, "invalid profile format",
695 /* Check that the interface version is currently supported.
696 * if not specified use previous version
697 * Mask off everything that is not kernel abi version
699 if (VERSION_LT(e->version, v5) && VERSION_GT(e->version, v7)) {
700 audit_iface(NULL, NULL, NULL, "unsupported interface version",
705 /* read the namespace if present */
706 if (unpack_str(e, &name, "namespace")) {
708 audit_iface(NULL, NULL, NULL, "invalid namespace name",
712 if (*ns && strcmp(*ns, name))
713 audit_iface(NULL, NULL, NULL, "invalid ns change", e,
722 static bool verify_xindex(int xindex, int table_size)
725 xtype = xindex & AA_X_TYPE_MASK;
726 index = xindex & AA_X_INDEX_MASK;
727 if (xtype == AA_X_TABLE && index >= table_size)
732 /* verify dfa xindexes are in range of transition tables */
733 static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
736 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
737 if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
739 if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
746 * verify_profile - Do post unpack analysis to verify profile consistency
747 * @profile: profile to verify (NOT NULL)
749 * Returns: 0 if passes verification else error
751 static int verify_profile(struct aa_profile *profile)
753 if (profile->file.dfa &&
754 !verify_dfa_xindex(profile->file.dfa,
755 profile->file.trans.size)) {
756 audit_iface(profile, NULL, NULL, "Invalid named transition",
764 void aa_load_ent_free(struct aa_load_ent *ent)
767 aa_put_profile(ent->rename);
768 aa_put_profile(ent->old);
769 aa_put_profile(ent->new);
775 struct aa_load_ent *aa_load_ent_alloc(void)
777 struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
779 INIT_LIST_HEAD(&ent->list);
784 * aa_unpack - unpack packed binary profile(s) data loaded from user space
785 * @udata: user data copied to kmem (NOT NULL)
786 * @lh: list to place unpacked profiles in a aa_repl_ws
787 * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
789 * Unpack user data and return refcounted allocated profile(s) stored in
790 * @lh in order of discovery, with the list chain stored in base.list
793 * Returns: profile(s) on @lh else error pointer if fails to unpack
795 int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
798 struct aa_load_ent *tmp, *ent;
799 struct aa_profile *profile = NULL;
802 .start = udata->data,
803 .end = udata->data + udata->size,
808 while (e.pos < e.end) {
809 char *ns_name = NULL;
811 error = verify_header(&e, e.pos == e.start, ns);
816 profile = unpack_profile(&e, &ns_name);
817 if (IS_ERR(profile)) {
818 error = PTR_ERR(profile);
822 error = verify_profile(profile);
826 error = aa_calc_profile_hash(profile, e.version, start,
831 ent = aa_load_ent_alloc();
838 ent->ns_name = ns_name;
839 list_add_tail(&ent->list, lh);
841 udata->abi = e.version & K_ABI_MASK;
842 udata->hash = aa_calc_hash(udata->data, udata->size);
843 if (IS_ERR(udata->hash)) {
844 error = PTR_ERR(udata->hash);
851 aa_put_profile(profile);
854 list_for_each_entry_safe(ent, tmp, lh, list) {
855 list_del_init(&ent->list);
856 aa_load_ent_free(ent);