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;
83 if (aad(sa)->iface.ns) {
84 audit_log_format(ab, " ns=");
85 audit_log_untrustedstring(ab, aad(sa)->iface.ns);
87 if (aad(sa)->iface.name) {
88 audit_log_format(ab, " name=");
89 audit_log_untrustedstring(ab, aad(sa)->iface.name);
91 if (aad(sa)->iface.pos)
92 audit_log_format(ab, " offset=%ld", aad(sa)->iface.pos);
96 * audit_iface - do audit message for policy unpacking/load/replace/remove
97 * @new: profile if it has been allocated (MAYBE NULL)
98 * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL)
99 * @name: name of the profile being manipulated (MAYBE NULL)
100 * @info: any extra info about the failure (MAYBE NULL)
101 * @e: buffer position info
104 * Returns: %0 or error
106 static int audit_iface(struct aa_profile *new, const char *ns_name,
107 const char *name, const char *info, struct aa_ext *e,
110 struct aa_profile *profile = __aa_current_profile();
111 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL);
113 aad(&sa)->iface.pos = e->pos - e->start;
114 aad(&sa)->iface.ns = ns_name;
116 aad(&sa)->iface.name = new->base.hname;
118 aad(&sa)->iface.name = name;
119 aad(&sa)->info = info;
120 aad(&sa)->error = error;
122 return aa_audit(AUDIT_APPARMOR_STATUS, profile, &sa, audit_cb);
125 void aa_loaddata_kref(struct kref *kref)
127 struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
135 /* test if read will be in packed data bounds */
136 static bool inbounds(struct aa_ext *e, size_t size)
138 return (size <= e->end - e->pos);
142 * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
143 * @e: serialized data read head (NOT NULL)
144 * @chunk: start address for chunk of data (NOT NULL)
146 * Returns: the size of chunk found with the read head at the end of the chunk.
148 static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
152 if (!inbounds(e, sizeof(u16)))
154 size = le16_to_cpu(get_unaligned((__le16 *) e->pos));
155 e->pos += sizeof(__le16);
156 if (!inbounds(e, size))
163 /* unpack control byte */
164 static bool unpack_X(struct aa_ext *e, enum aa_code code)
168 if (*(u8 *) e->pos != code)
175 * unpack_nameX - check is the next element is of type X with a name of @name
176 * @e: serialized data extent information (NOT NULL)
178 * @name: name to match to the serialized element. (MAYBE NULL)
180 * check that the next serialized data element is of type X and has a tag
181 * name @name. If @name is specified then there must be a matching
182 * name element in the stream. If @name is NULL any name element will be
183 * skipped and only the typecode will be tested.
185 * Returns 1 on success (both type code and name tests match) and the read
186 * head is advanced past the headers
188 * Returns: 0 if either match fails, the read head does not move
190 static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
193 * May need to reset pos if name or type doesn't match
197 * Check for presence of a tagname, and if present name size
198 * AA_NAME tag value is a u16.
200 if (unpack_X(e, AA_NAME)) {
202 size_t size = unpack_u16_chunk(e, &tag);
203 /* if a name is specified it must match. otherwise skip tag */
204 if (name && (!size || strcmp(name, tag)))
207 /* if a name is specified and there is no name tag fail */
211 /* now check if type code matches */
212 if (unpack_X(e, code))
220 static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
222 if (unpack_nameX(e, AA_U32, name)) {
223 if (!inbounds(e, sizeof(u32)))
226 *data = le32_to_cpu(get_unaligned((__le32 *) e->pos));
227 e->pos += sizeof(u32);
233 static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
235 if (unpack_nameX(e, AA_U64, name)) {
236 if (!inbounds(e, sizeof(u64)))
239 *data = le64_to_cpu(get_unaligned((__le64 *) e->pos));
240 e->pos += sizeof(u64);
246 static size_t unpack_array(struct aa_ext *e, const char *name)
248 if (unpack_nameX(e, AA_ARRAY, name)) {
250 if (!inbounds(e, sizeof(u16)))
252 size = (int)le16_to_cpu(get_unaligned((__le16 *) e->pos));
253 e->pos += sizeof(u16);
259 static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
261 if (unpack_nameX(e, AA_BLOB, name)) {
263 if (!inbounds(e, sizeof(u32)))
265 size = le32_to_cpu(get_unaligned((__le32 *) e->pos));
266 e->pos += sizeof(u32);
267 if (inbounds(e, (size_t) size)) {
276 static int unpack_str(struct aa_ext *e, const char **string, const char *name)
282 if (unpack_nameX(e, AA_STRING, name)) {
283 size = unpack_u16_chunk(e, &src_str);
285 /* strings are null terminated, length is size - 1 */
286 if (src_str[size - 1] != 0)
298 static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
302 int res = unpack_str(e, &tmp, name);
308 *string = kmemdup(tmp, res, GFP_KERNEL);
317 #define DFA_VALID_PERM_MASK 0xffffffff
318 #define DFA_VALID_PERM2_MASK 0xffffffff
321 * verify_accept - verify the accept tables of a dfa
322 * @dfa: dfa to verify accept tables of (NOT NULL)
323 * @flags: flags governing dfa
325 * Returns: 1 if valid accept tables else 0 if error
327 static bool verify_accept(struct aa_dfa *dfa, int flags)
331 /* verify accept permissions */
332 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
333 int mode = ACCEPT_TABLE(dfa)[i];
335 if (mode & ~DFA_VALID_PERM_MASK)
338 if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
345 * unpack_dfa - unpack a file rule dfa
346 * @e: serialized data extent information (NOT NULL)
348 * returns dfa or ERR_PTR or NULL if no dfa
350 static struct aa_dfa *unpack_dfa(struct aa_ext *e)
354 struct aa_dfa *dfa = NULL;
356 size = unpack_blob(e, &blob, "aadfa");
359 * The dfa is aligned with in the blob to 8 bytes
360 * from the beginning of the stream.
361 * alignment adjust needed by dfa unpack
363 size_t sz = blob - (char *) e->start -
364 ((e->pos - e->start) & 7);
365 size_t pad = ALIGN(sz, 8) - sz;
366 int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
367 TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
368 dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
373 if (!verify_accept(dfa, flags))
381 return ERR_PTR(-EPROTO);
385 * unpack_trans_table - unpack a profile transition table
386 * @e: serialized data extent information (NOT NULL)
387 * @profile: profile to add the accept table to (NOT NULL)
389 * Returns: 1 if table successfully unpacked
391 static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
395 /* exec table is optional */
396 if (unpack_nameX(e, AA_STRUCT, "xtable")) {
399 size = unpack_array(e, NULL);
400 /* currently 4 exec bits and entries 0-3 are reserved iupcx */
403 profile->file.trans.table = kzalloc(sizeof(char *) * size,
405 if (!profile->file.trans.table)
408 profile->file.trans.size = size;
409 for (i = 0; i < size; i++) {
411 int c, j, size2 = unpack_strdup(e, &str, NULL);
412 /* unpack_strdup verifies that the last character is
413 * null termination byte.
417 profile->file.trans.table[i] = str;
418 /* verify that name doesn't start with space */
422 /* count internal # of internal \0 */
423 for (c = j = 0; j < size2 - 2; j++) {
428 /* beginning with : requires an embedded \0,
429 * verify that exactly 1 internal \0 exists
430 * trailing \0 already verified by unpack_strdup
434 /* first character after : must be valid */
438 /* fail - all other cases with embedded \0 */
441 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
443 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
449 aa_free_domain_entries(&profile->file.trans);
454 static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
458 /* rlimits are optional */
459 if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
462 if (!unpack_u32(e, &tmp, NULL))
464 profile->rlimits.mask = tmp;
466 size = unpack_array(e, NULL);
467 if (size > RLIM_NLIMITS)
469 for (i = 0; i < size; i++) {
471 int a = aa_map_resource(i);
472 if (!unpack_u64(e, &tmp2, NULL))
474 profile->rlimits.limits[a].rlim_max = tmp2;
476 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
478 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
488 static void *kvmemdup(const void *src, size_t len)
490 void *p = kvmalloc(len);
497 static u32 strhash(const void *data, u32 len, u32 seed)
499 const char * const *key = data;
501 return jhash(*key, strlen(*key), seed);
504 static int datacmp(struct rhashtable_compare_arg *arg, const void *obj)
506 const struct aa_data *data = obj;
507 const char * const *key = arg->key;
509 return strcmp(data->key, *key);
513 * unpack_profile - unpack a serialized profile
514 * @e: serialized data extent information (NOT NULL)
516 * NOTE: unpack profile sets audit struct if there is a failure
518 static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
520 struct aa_profile *profile = NULL;
521 const char *tmpname, *tmpns = NULL, *name = NULL;
523 struct rhashtable_params params = { 0 };
525 struct aa_data *data;
526 int i, error = -EPROTO;
532 /* check that we have the right struct being passed */
533 if (!unpack_nameX(e, AA_STRUCT, "profile"))
535 if (!unpack_str(e, &name, NULL))
540 tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
542 *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
548 profile = aa_alloc_profile(name, GFP_KERNEL);
550 return ERR_PTR(-ENOMEM);
552 /* profile renaming is optional */
553 (void) unpack_str(e, &profile->rename, "rename");
555 /* attachment string is optional */
556 (void) unpack_str(e, &profile->attach, "attach");
558 /* xmatch is optional and may be NULL */
559 profile->xmatch = unpack_dfa(e);
560 if (IS_ERR(profile->xmatch)) {
561 error = PTR_ERR(profile->xmatch);
562 profile->xmatch = NULL;
565 /* xmatch_len is not optional if xmatch is set */
566 if (profile->xmatch) {
567 if (!unpack_u32(e, &tmp, NULL))
569 profile->xmatch_len = tmp;
572 /* per profile debug flags (complain, audit) */
573 if (!unpack_nameX(e, AA_STRUCT, "flags"))
575 if (!unpack_u32(e, &tmp, NULL))
577 if (tmp & PACKED_FLAG_HAT)
578 profile->flags |= PFLAG_HAT;
579 if (!unpack_u32(e, &tmp, NULL))
581 if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
582 profile->mode = APPARMOR_COMPLAIN;
583 else if (tmp == PACKED_MODE_KILL)
584 profile->mode = APPARMOR_KILL;
585 else if (tmp == PACKED_MODE_UNCONFINED)
586 profile->mode = APPARMOR_UNCONFINED;
587 if (!unpack_u32(e, &tmp, NULL))
590 profile->audit = AUDIT_ALL;
592 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
595 /* path_flags is optional */
596 if (unpack_u32(e, &profile->path_flags, "path_flags"))
597 profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
599 /* set a default value if path_flags field is not present */
600 profile->path_flags = PFLAG_MEDIATE_DELETED;
602 if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
604 if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
606 if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
608 if (!unpack_u32(e, &tmpcap.cap[0], NULL))
611 if (unpack_nameX(e, AA_STRUCT, "caps64")) {
612 /* optional upper half of 64 bit caps */
613 if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
615 if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
617 if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
619 if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
621 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
625 if (unpack_nameX(e, AA_STRUCT, "capsx")) {
626 /* optional extended caps mediation mask */
627 if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
629 if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
631 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
635 if (!unpack_rlimits(e, profile))
638 if (unpack_nameX(e, AA_STRUCT, "policydb")) {
639 /* generic policy dfa - optional and may be NULL */
640 profile->policy.dfa = unpack_dfa(e);
641 if (IS_ERR(profile->policy.dfa)) {
642 error = PTR_ERR(profile->policy.dfa);
643 profile->policy.dfa = NULL;
645 } else if (!profile->policy.dfa) {
649 if (!unpack_u32(e, &profile->policy.start[0], "start"))
650 /* default start state */
651 profile->policy.start[0] = DFA_START;
652 /* setup class index */
653 for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
654 profile->policy.start[i] =
655 aa_dfa_next(profile->policy.dfa,
656 profile->policy.start[0],
659 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
662 profile->policy.dfa = aa_get_dfa(nulldfa);
665 profile->file.dfa = unpack_dfa(e);
666 if (IS_ERR(profile->file.dfa)) {
667 error = PTR_ERR(profile->file.dfa);
668 profile->file.dfa = NULL;
670 } else if (profile->file.dfa) {
671 if (!unpack_u32(e, &profile->file.start, "dfa_start"))
672 /* default start state */
673 profile->file.start = DFA_START;
674 } else if (profile->policy.dfa &&
675 profile->policy.start[AA_CLASS_FILE]) {
676 profile->file.dfa = aa_get_dfa(profile->policy.dfa);
677 profile->file.start = profile->policy.start[AA_CLASS_FILE];
679 profile->file.dfa = aa_get_dfa(nulldfa);
681 if (!unpack_trans_table(e, profile))
684 if (unpack_nameX(e, AA_STRUCT, "data")) {
685 profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL);
689 params.nelem_hint = 3;
690 params.key_len = sizeof(void *);
691 params.key_offset = offsetof(struct aa_data, key);
692 params.head_offset = offsetof(struct aa_data, head);
693 params.hashfn = strhash;
694 params.obj_cmpfn = datacmp;
696 if (rhashtable_init(profile->data, ¶ms))
699 while (unpack_strdup(e, &key, NULL)) {
700 data = kzalloc(sizeof(*data), GFP_KERNEL);
707 data->size = unpack_blob(e, &data->data, NULL);
708 data->data = kvmemdup(data->data, data->size);
709 if (data->size && !data->data) {
715 rhashtable_insert_fast(profile->data, &data->head,
719 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
723 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
733 audit_iface(profile, NULL, name, "failed to unpack profile", e,
735 aa_free_profile(profile);
737 return ERR_PTR(error);
741 * verify_head - unpack serialized stream header
742 * @e: serialized data read head (NOT NULL)
743 * @required: whether the header is required or optional
744 * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
746 * Returns: error or 0 if header is good
748 static int verify_header(struct aa_ext *e, int required, const char **ns)
750 int error = -EPROTONOSUPPORT;
751 const char *name = NULL;
754 /* get the interface version */
755 if (!unpack_u32(e, &e->version, "version")) {
757 audit_iface(NULL, NULL, NULL, "invalid profile format",
763 /* Check that the interface version is currently supported.
764 * if not specified use previous version
765 * Mask off everything that is not kernel abi version
767 if (VERSION_LT(e->version, v5) && VERSION_GT(e->version, v7)) {
768 audit_iface(NULL, NULL, NULL, "unsupported interface version",
773 /* read the namespace if present */
774 if (unpack_str(e, &name, "namespace")) {
776 audit_iface(NULL, NULL, NULL, "invalid namespace name",
780 if (*ns && strcmp(*ns, name))
781 audit_iface(NULL, NULL, NULL, "invalid ns change", e,
790 static bool verify_xindex(int xindex, int table_size)
793 xtype = xindex & AA_X_TYPE_MASK;
794 index = xindex & AA_X_INDEX_MASK;
795 if (xtype == AA_X_TABLE && index >= table_size)
800 /* verify dfa xindexes are in range of transition tables */
801 static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
804 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
805 if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
807 if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
814 * verify_profile - Do post unpack analysis to verify profile consistency
815 * @profile: profile to verify (NOT NULL)
817 * Returns: 0 if passes verification else error
819 static int verify_profile(struct aa_profile *profile)
821 if (profile->file.dfa &&
822 !verify_dfa_xindex(profile->file.dfa,
823 profile->file.trans.size)) {
824 audit_iface(profile, NULL, NULL, "Invalid named transition",
832 void aa_load_ent_free(struct aa_load_ent *ent)
835 aa_put_profile(ent->rename);
836 aa_put_profile(ent->old);
837 aa_put_profile(ent->new);
843 struct aa_load_ent *aa_load_ent_alloc(void)
845 struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
847 INIT_LIST_HEAD(&ent->list);
852 * aa_unpack - unpack packed binary profile(s) data loaded from user space
853 * @udata: user data copied to kmem (NOT NULL)
854 * @lh: list to place unpacked profiles in a aa_repl_ws
855 * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
857 * Unpack user data and return refcounted allocated profile(s) stored in
858 * @lh in order of discovery, with the list chain stored in base.list
861 * Returns: profile(s) on @lh else error pointer if fails to unpack
863 int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
866 struct aa_load_ent *tmp, *ent;
867 struct aa_profile *profile = NULL;
870 .start = udata->data,
871 .end = udata->data + udata->size,
876 while (e.pos < e.end) {
877 char *ns_name = NULL;
879 error = verify_header(&e, e.pos == e.start, ns);
884 profile = unpack_profile(&e, &ns_name);
885 if (IS_ERR(profile)) {
886 error = PTR_ERR(profile);
890 error = verify_profile(profile);
894 if (aa_g_hash_policy)
895 error = aa_calc_profile_hash(profile, e.version, start,
900 ent = aa_load_ent_alloc();
907 ent->ns_name = ns_name;
908 list_add_tail(&ent->list, lh);
910 udata->abi = e.version & K_ABI_MASK;
911 if (aa_g_hash_policy) {
912 udata->hash = aa_calc_hash(udata->data, udata->size);
913 if (IS_ERR(udata->hash)) {
914 error = PTR_ERR(udata->hash);
922 aa_put_profile(profile);
925 list_for_each_entry_safe(ent, tmp, lh, list) {
926 list_del_init(&ent->list);
927 aa_load_ent_free(ent);