]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/apparmor/policy_unpack.c
apparmor: audit policy ns specified in policy load
[karo-tx-linux.git] / security / apparmor / policy_unpack.c
1 /*
2  * AppArmor security module
3  *
4  * This file contains AppArmor functions for unpacking policy loaded from
5  * userspace.
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  *
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
13  * License.
14  *
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.
18  */
19
20 #include <asm/unaligned.h>
21 #include <linux/ctype.h>
22 #include <linux/errno.h>
23
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"
31
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))
36
37 #define v5      5       /* base version */
38 #define v6      6       /* per entry policydb mediation check */
39 #define v7      7       /* full network masking */
40
41 /*
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.
48  */
49
50 enum aa_code {
51         AA_U8,
52         AA_U16,
53         AA_U32,
54         AA_U64,
55         AA_NAME,                /* same as string except it is items name */
56         AA_STRING,
57         AA_BLOB,
58         AA_STRUCT,
59         AA_STRUCTEND,
60         AA_LIST,
61         AA_LISTEND,
62         AA_ARRAY,
63         AA_ARRAYEND,
64 };
65
66 /*
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.
70  */
71 struct aa_ext {
72         void *start;
73         void *end;
74         void *pos;              /* pointer to current position in the buffer */
75         u32 version;
76 };
77
78 /* audit callback for unpack fields */
79 static void audit_cb(struct audit_buffer *ab, void *va)
80 {
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);
86         }
87         if (sa->aad->iface.pos)
88                 audit_log_format(ab, " offset=%ld", sa->aad->iface.pos);
89 }
90
91 /**
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
98  * @error: error code
99  *
100  * Returns: %0 or error
101  */
102 static int audit_iface(struct aa_profile *new, const char *ns_name,
103                        const char *name, const char *info, struct aa_ext *e,
104                        int error)
105 {
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;
110         sa.aad = &aad;
111         aad.iface.ns = ns_name;
112         if (e)
113                 aad.iface.pos = e->pos - e->start;
114         aad.iface.target = new;
115         aad.name = name;
116         aad.info = info;
117         aad.error = error;
118
119         return aa_audit(AUDIT_APPARMOR_STATUS, profile, GFP_KERNEL, &sa,
120                         audit_cb);
121 }
122
123 void aa_loaddata_kref(struct kref *kref)
124 {
125         struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count);
126
127         if (d) {
128                 kzfree(d->hash);
129                 kvfree(d);
130         }
131 }
132
133 /* test if read will be in packed data bounds */
134 static bool inbounds(struct aa_ext *e, size_t size)
135 {
136         return (size <= e->end - e->pos);
137 }
138
139 /**
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)
143  *
144  * Returns: the size of chunk found with the read head at the end of the chunk.
145  */
146 static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
147 {
148         size_t size = 0;
149
150         if (!inbounds(e, sizeof(u16)))
151                 return 0;
152         size = le16_to_cpu(get_unaligned((u16 *) e->pos));
153         e->pos += sizeof(u16);
154         if (!inbounds(e, size))
155                 return 0;
156         *chunk = e->pos;
157         e->pos += size;
158         return size;
159 }
160
161 /* unpack control byte */
162 static bool unpack_X(struct aa_ext *e, enum aa_code code)
163 {
164         if (!inbounds(e, 1))
165                 return 0;
166         if (*(u8 *) e->pos != code)
167                 return 0;
168         e->pos++;
169         return 1;
170 }
171
172 /**
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)
175  * @code: type code
176  * @name: name to match to the serialized element.  (MAYBE NULL)
177  *
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.
182  *
183  * Returns 1 on success (both type code and name tests match) and the read
184  * head is advanced past the headers
185  *
186  * Returns: 0 if either match fails, the read head does not move
187  */
188 static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
189 {
190         /*
191          * May need to reset pos if name or type doesn't match
192          */
193         void *pos = e->pos;
194         /*
195          * Check for presence of a tagname, and if present name size
196          * AA_NAME tag value is a u16.
197          */
198         if (unpack_X(e, AA_NAME)) {
199                 char *tag = NULL;
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)))
203                         goto fail;
204         } else if (name) {
205                 /* if a name is specified and there is no name tag fail */
206                 goto fail;
207         }
208
209         /* now check if type code matches */
210         if (unpack_X(e, code))
211                 return 1;
212
213 fail:
214         e->pos = pos;
215         return 0;
216 }
217
218 static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
219 {
220         if (unpack_nameX(e, AA_U32, name)) {
221                 if (!inbounds(e, sizeof(u32)))
222                         return 0;
223                 if (data)
224                         *data = le32_to_cpu(get_unaligned((u32 *) e->pos));
225                 e->pos += sizeof(u32);
226                 return 1;
227         }
228         return 0;
229 }
230
231 static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
232 {
233         if (unpack_nameX(e, AA_U64, name)) {
234                 if (!inbounds(e, sizeof(u64)))
235                         return 0;
236                 if (data)
237                         *data = le64_to_cpu(get_unaligned((u64 *) e->pos));
238                 e->pos += sizeof(u64);
239                 return 1;
240         }
241         return 0;
242 }
243
244 static size_t unpack_array(struct aa_ext *e, const char *name)
245 {
246         if (unpack_nameX(e, AA_ARRAY, name)) {
247                 int size;
248                 if (!inbounds(e, sizeof(u16)))
249                         return 0;
250                 size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos));
251                 e->pos += sizeof(u16);
252                 return size;
253         }
254         return 0;
255 }
256
257 static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
258 {
259         if (unpack_nameX(e, AA_BLOB, name)) {
260                 u32 size;
261                 if (!inbounds(e, sizeof(u32)))
262                         return 0;
263                 size = le32_to_cpu(get_unaligned((u32 *) e->pos));
264                 e->pos += sizeof(u32);
265                 if (inbounds(e, (size_t) size)) {
266                         *blob = e->pos;
267                         e->pos += size;
268                         return size;
269                 }
270         }
271         return 0;
272 }
273
274 static int unpack_str(struct aa_ext *e, const char **string, const char *name)
275 {
276         char *src_str;
277         size_t size = 0;
278         void *pos = e->pos;
279         *string = NULL;
280         if (unpack_nameX(e, AA_STRING, name)) {
281                 size = unpack_u16_chunk(e, &src_str);
282                 if (size) {
283                         /* strings are null terminated, length is size - 1 */
284                         if (src_str[size - 1] != 0)
285                                 goto fail;
286                         *string = src_str;
287                 }
288         }
289         return size;
290
291 fail:
292         e->pos = pos;
293         return 0;
294 }
295
296 static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
297 {
298         const char *tmp;
299         void *pos = e->pos;
300         int res = unpack_str(e, &tmp, name);
301         *string = NULL;
302
303         if (!res)
304                 return 0;
305
306         *string = kmemdup(tmp, res, GFP_KERNEL);
307         if (!*string) {
308                 e->pos = pos;
309                 return 0;
310         }
311
312         return res;
313 }
314
315 #define DFA_VALID_PERM_MASK             0xffffffff
316 #define DFA_VALID_PERM2_MASK            0xffffffff
317
318 /**
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
322  *
323  * Returns: 1 if valid accept tables else 0 if error
324  */
325 static bool verify_accept(struct aa_dfa *dfa, int flags)
326 {
327         int i;
328
329         /* verify accept permissions */
330         for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
331                 int mode = ACCEPT_TABLE(dfa)[i];
332
333                 if (mode & ~DFA_VALID_PERM_MASK)
334                         return 0;
335
336                 if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
337                         return 0;
338         }
339         return 1;
340 }
341
342 /**
343  * unpack_dfa - unpack a file rule dfa
344  * @e: serialized data extent information (NOT NULL)
345  *
346  * returns dfa or ERR_PTR or NULL if no dfa
347  */
348 static struct aa_dfa *unpack_dfa(struct aa_ext *e)
349 {
350         char *blob = NULL;
351         size_t size;
352         struct aa_dfa *dfa = NULL;
353
354         size = unpack_blob(e, &blob, "aadfa");
355         if (size) {
356                 /*
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
360                  */
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);
367
368                 if (IS_ERR(dfa))
369                         return dfa;
370
371                 if (!verify_accept(dfa, flags))
372                         goto fail;
373         }
374
375         return dfa;
376
377 fail:
378         aa_put_dfa(dfa);
379         return ERR_PTR(-EPROTO);
380 }
381
382 /**
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)
386  *
387  * Returns: 1 if table successfully unpacked
388  */
389 static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
390 {
391         void *pos = e->pos;
392
393         /* exec table is optional */
394         if (unpack_nameX(e, AA_STRUCT, "xtable")) {
395                 int i, size;
396
397                 size = unpack_array(e, NULL);
398                 /* currently 4 exec bits and entries 0-3 are reserved iupcx */
399                 if (size > 16 - 4)
400                         goto fail;
401                 profile->file.trans.table = kzalloc(sizeof(char *) * size,
402                                                     GFP_KERNEL);
403                 if (!profile->file.trans.table)
404                         goto fail;
405
406                 profile->file.trans.size = size;
407                 for (i = 0; i < size; i++) {
408                         char *str;
409                         int c, j, size2 = unpack_strdup(e, &str, NULL);
410                         /* unpack_strdup verifies that the last character is
411                          * null termination byte.
412                          */
413                         if (!size2)
414                                 goto fail;
415                         profile->file.trans.table[i] = str;
416                         /* verify that name doesn't start with space */
417                         if (isspace(*str))
418                                 goto fail;
419
420                         /* count internal #  of internal \0 */
421                         for (c = j = 0; j < size2 - 2; j++) {
422                                 if (!str[j])
423                                         c++;
424                         }
425                         if (*str == ':') {
426                                 /* beginning with : requires an embedded \0,
427                                  * verify that exactly 1 internal \0 exists
428                                  * trailing \0 already verified by unpack_strdup
429                                  */
430                                 if (c != 1)
431                                         goto fail;
432                                 /* first character after : must be valid */
433                                 if (!str[1])
434                                         goto fail;
435                         } else if (c)
436                                 /* fail - all other cases with embedded \0 */
437                                 goto fail;
438                 }
439                 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
440                         goto fail;
441                 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
442                         goto fail;
443         }
444         return 1;
445
446 fail:
447         aa_free_domain_entries(&profile->file.trans);
448         e->pos = pos;
449         return 0;
450 }
451
452 static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
453 {
454         void *pos = e->pos;
455
456         /* rlimits are optional */
457         if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
458                 int i, size;
459                 u32 tmp = 0;
460                 if (!unpack_u32(e, &tmp, NULL))
461                         goto fail;
462                 profile->rlimits.mask = tmp;
463
464                 size = unpack_array(e, NULL);
465                 if (size > RLIM_NLIMITS)
466                         goto fail;
467                 for (i = 0; i < size; i++) {
468                         u64 tmp2 = 0;
469                         int a = aa_map_resource(i);
470                         if (!unpack_u64(e, &tmp2, NULL))
471                                 goto fail;
472                         profile->rlimits.limits[a].rlim_max = tmp2;
473                 }
474                 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
475                         goto fail;
476                 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
477                         goto fail;
478         }
479         return 1;
480
481 fail:
482         e->pos = pos;
483         return 0;
484 }
485
486 /**
487  * unpack_profile - unpack a serialized profile
488  * @e: serialized data extent information (NOT NULL)
489  *
490  * NOTE: unpack profile sets audit struct if there is a failure
491  */
492 static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name)
493 {
494         struct aa_profile *profile = NULL;
495         const char *tmpname, *tmpns = NULL, *name = NULL;
496         size_t ns_len;
497         int i, error = -EPROTO;
498         kernel_cap_t tmpcap;
499         u32 tmp;
500
501         *ns_name = NULL;
502
503         /* check that we have the right struct being passed */
504         if (!unpack_nameX(e, AA_STRUCT, "profile"))
505                 goto fail;
506         if (!unpack_str(e, &name, NULL))
507                 goto fail;
508         if (*name == '\0')
509                 goto fail;
510
511         tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len);
512         if (tmpns) {
513                 *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL);
514                 if (!*ns_name)
515                         goto fail;
516                 name = tmpname;
517         }
518
519         profile = aa_alloc_profile(name, GFP_KERNEL);
520         if (!profile)
521                 return ERR_PTR(-ENOMEM);
522
523         /* profile renaming is optional */
524         (void) unpack_str(e, &profile->rename, "rename");
525
526         /* attachment string is optional */
527         (void) unpack_str(e, &profile->attach, "attach");
528
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;
534                 goto fail;
535         }
536         /* xmatch_len is not optional if xmatch is set */
537         if (profile->xmatch) {
538                 if (!unpack_u32(e, &tmp, NULL))
539                         goto fail;
540                 profile->xmatch_len = tmp;
541         }
542
543         /* per profile debug flags (complain, audit) */
544         if (!unpack_nameX(e, AA_STRUCT, "flags"))
545                 goto fail;
546         if (!unpack_u32(e, &tmp, NULL))
547                 goto fail;
548         if (tmp & PACKED_FLAG_HAT)
549                 profile->flags |= PFLAG_HAT;
550         if (!unpack_u32(e, &tmp, NULL))
551                 goto fail;
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))
559                 goto fail;
560         if (tmp)
561                 profile->audit = AUDIT_ALL;
562
563         if (!unpack_nameX(e, AA_STRUCTEND, NULL))
564                 goto fail;
565
566         /* path_flags is optional */
567         if (unpack_u32(e, &profile->path_flags, "path_flags"))
568                 profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
569         else
570                 /* set a default value if path_flags field is not present */
571                 profile->path_flags = PFLAG_MEDIATE_DELETED;
572
573         if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
574                 goto fail;
575         if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
576                 goto fail;
577         if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
578                 goto fail;
579         if (!unpack_u32(e, &tmpcap.cap[0], NULL))
580                 goto fail;
581
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))
585                         goto fail;
586                 if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
587                         goto fail;
588                 if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
589                         goto fail;
590                 if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
591                         goto fail;
592                 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
593                         goto fail;
594         }
595
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))
599                         goto fail;
600                 if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
601                         goto fail;
602                 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
603                         goto fail;
604         }
605
606         if (!unpack_rlimits(e, profile))
607                 goto fail;
608
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;
615                         goto fail;
616                 } else if (!profile->policy.dfa) {
617                         error = -EPROTO;
618                         goto fail;
619                 }
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],
628                                             i);
629                 }
630                 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
631                         goto fail;
632         } else
633                 profile->policy.dfa = aa_get_dfa(nulldfa);
634
635         /* get file rules */
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;
640                 goto fail;
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];
649         } else
650                 profile->file.dfa = aa_get_dfa(nulldfa);
651
652         if (!unpack_trans_table(e, profile))
653                 goto fail;
654
655         if (!unpack_nameX(e, AA_STRUCTEND, NULL))
656                 goto fail;
657
658         return profile;
659
660 fail:
661         if (profile)
662                 name = NULL;
663         else if (!name)
664                 name = "unknown";
665         audit_iface(profile, NULL, name, "failed to unpack profile", e,
666                     error);
667         aa_free_profile(profile);
668
669         return ERR_PTR(error);
670 }
671
672 /**
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)
677  *
678  * Returns: error or 0 if header is good
679  */
680 static int verify_header(struct aa_ext *e, int required, const char **ns)
681 {
682         int error = -EPROTONOSUPPORT;
683         const char *name = NULL;
684         *ns = NULL;
685
686         /* get the interface version */
687         if (!unpack_u32(e, &e->version, "version")) {
688                 if (required) {
689                         audit_iface(NULL, NULL, NULL, "invalid profile format",
690                                     e, error);
691                         return error;
692                 }
693         }
694
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
698          */
699         if (VERSION_LT(e->version, v5) && VERSION_GT(e->version, v7)) {
700                 audit_iface(NULL, NULL, NULL, "unsupported interface version",
701                             e, error);
702                 return error;
703         }
704
705         /* read the namespace if present */
706         if (unpack_str(e, &name, "namespace")) {
707                 if (*name == '\0') {
708                         audit_iface(NULL, NULL, NULL, "invalid namespace name",
709                                     e, error);
710                         return error;
711                 }
712                 if (*ns && strcmp(*ns, name))
713                         audit_iface(NULL, NULL, NULL, "invalid ns change", e,
714                                     error);
715                 else if (!*ns)
716                         *ns = name;
717         }
718
719         return 0;
720 }
721
722 static bool verify_xindex(int xindex, int table_size)
723 {
724         int index, xtype;
725         xtype = xindex & AA_X_TYPE_MASK;
726         index = xindex & AA_X_INDEX_MASK;
727         if (xtype == AA_X_TABLE && index >= table_size)
728                 return 0;
729         return 1;
730 }
731
732 /* verify dfa xindexes are in range of transition tables */
733 static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
734 {
735         int i;
736         for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
737                 if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
738                         return 0;
739                 if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
740                         return 0;
741         }
742         return 1;
743 }
744
745 /**
746  * verify_profile - Do post unpack analysis to verify profile consistency
747  * @profile: profile to verify (NOT NULL)
748  *
749  * Returns: 0 if passes verification else error
750  */
751 static int verify_profile(struct aa_profile *profile)
752 {
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",
757                             NULL, -EPROTO);
758                 return -EPROTO;
759         }
760
761         return 0;
762 }
763
764 void aa_load_ent_free(struct aa_load_ent *ent)
765 {
766         if (ent) {
767                 aa_put_profile(ent->rename);
768                 aa_put_profile(ent->old);
769                 aa_put_profile(ent->new);
770                 kfree(ent->ns_name);
771                 kzfree(ent);
772         }
773 }
774
775 struct aa_load_ent *aa_load_ent_alloc(void)
776 {
777         struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
778         if (ent)
779                 INIT_LIST_HEAD(&ent->list);
780         return ent;
781 }
782
783 /**
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)
788  *
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
791  * or error
792  *
793  * Returns: profile(s) on @lh else error pointer if fails to unpack
794  */
795 int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
796               const char **ns)
797 {
798         struct aa_load_ent *tmp, *ent;
799         struct aa_profile *profile = NULL;
800         int error;
801         struct aa_ext e = {
802                 .start = udata->data,
803                 .end = udata->data + udata->size,
804                 .pos = udata->data,
805         };
806
807         *ns = NULL;
808         while (e.pos < e.end) {
809                 char *ns_name = NULL;
810                 void *start;
811                 error = verify_header(&e, e.pos == e.start, ns);
812                 if (error)
813                         goto fail;
814
815                 start = e.pos;
816                 profile = unpack_profile(&e, &ns_name);
817                 if (IS_ERR(profile)) {
818                         error = PTR_ERR(profile);
819                         goto fail;
820                 }
821
822                 error = verify_profile(profile);
823                 if (error)
824                         goto fail_profile;
825
826                 error = aa_calc_profile_hash(profile, e.version, start,
827                                                      e.pos - start);
828                 if (error)
829                         goto fail_profile;
830
831                 ent = aa_load_ent_alloc();
832                 if (!ent) {
833                         error = -ENOMEM;
834                         goto fail_profile;
835                 }
836
837                 ent->new = profile;
838                 ent->ns_name = ns_name;
839                 list_add_tail(&ent->list, lh);
840         }
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);
845                 udata->hash = NULL;
846                 goto fail;
847         }
848         return 0;
849
850 fail_profile:
851         aa_put_profile(profile);
852
853 fail:
854         list_for_each_entry_safe(ent, tmp, lh, list) {
855                 list_del_init(&ent->list);
856                 aa_load_ent_free(ent);
857         }
858
859         return error;
860 }