apparmor: add support for force complain flag to support learning mode
[linux-2.6-block.git] / security / apparmor / policy_unpack.c
CommitLineData
736ec752
JJ
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 *
d410fa4e
RD
15 * AppArmor uses a serialized binary format for loading policy. To find
16 * policy format documentation look in Documentation/security/apparmor.txt
736ec752
JJ
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"
f8eb8a13 27#include "include/crypto.h"
736ec752
JJ
28#include "include/match.h"
29#include "include/policy.h"
30#include "include/policy_unpack.h"
736ec752 31
5ebfb128
JJ
32#define FORCE_COMPLAIN_FLAG 0x800
33
736ec752
JJ
34/*
35 * The AppArmor interface treats data as a type byte followed by the
36 * actual data. The interface has the notion of a a named entry
37 * which has a name (AA_NAME typecode followed by name string) followed by
38 * the entries typecode and data. Named types allow for optional
39 * elements and extensions to be added and tested for without breaking
40 * backwards compatibility.
41 */
42
43enum aa_code {
44 AA_U8,
45 AA_U16,
46 AA_U32,
47 AA_U64,
48 AA_NAME, /* same as string except it is items name */
49 AA_STRING,
50 AA_BLOB,
51 AA_STRUCT,
52 AA_STRUCTEND,
53 AA_LIST,
54 AA_LISTEND,
55 AA_ARRAY,
56 AA_ARRAYEND,
57};
58
59/*
60 * aa_ext is the read of the buffer containing the serialized profile. The
61 * data is copied into a kernel buffer in apparmorfs and then handed off to
62 * the unpack routines.
63 */
64struct aa_ext {
65 void *start;
66 void *end;
67 void *pos; /* pointer to current position in the buffer */
68 u32 version;
69};
70
71/* audit callback for unpack fields */
72static void audit_cb(struct audit_buffer *ab, void *va)
73{
74 struct common_audit_data *sa = va;
3b3b0e4f
EP
75 if (sa->aad->iface.target) {
76 struct aa_profile *name = sa->aad->iface.target;
736ec752
JJ
77 audit_log_format(ab, " name=");
78 audit_log_untrustedstring(ab, name->base.hname);
79 }
3b3b0e4f
EP
80 if (sa->aad->iface.pos)
81 audit_log_format(ab, " offset=%ld", sa->aad->iface.pos);
736ec752
JJ
82}
83
84/**
85 * audit_iface - do audit message for policy unpacking/load/replace/remove
86 * @new: profile if it has been allocated (MAYBE NULL)
87 * @name: name of the profile being manipulated (MAYBE NULL)
88 * @info: any extra info about the failure (MAYBE NULL)
b1b4bc2e 89 * @e: buffer position info
736ec752
JJ
90 * @error: error code
91 *
92 * Returns: %0 or error
93 */
94static int audit_iface(struct aa_profile *new, const char *name,
95 const char *info, struct aa_ext *e, int error)
96{
97 struct aa_profile *profile = __aa_current_profile();
98 struct common_audit_data sa;
3b3b0e4f 99 struct apparmor_audit_data aad = {0,};
50c205f5 100 sa.type = LSM_AUDIT_DATA_NONE;
3b3b0e4f 101 sa.aad = &aad;
b1b4bc2e 102 if (e)
3b3b0e4f
EP
103 aad.iface.pos = e->pos - e->start;
104 aad.iface.target = new;
105 aad.name = name;
106 aad.info = info;
107 aad.error = error;
736ec752
JJ
108
109 return aa_audit(AUDIT_APPARMOR_STATUS, profile, GFP_KERNEL, &sa,
110 audit_cb);
111}
112
113/* test if read will be in packed data bounds */
114static bool inbounds(struct aa_ext *e, size_t size)
115{
116 return (size <= e->end - e->pos);
117}
118
119/**
120 * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
121 * @e: serialized data read head (NOT NULL)
122 * @chunk: start address for chunk of data (NOT NULL)
123 *
124 * Returns: the size of chunk found with the read head at the end of the chunk.
125 */
126static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
127{
128 size_t size = 0;
129
130 if (!inbounds(e, sizeof(u16)))
131 return 0;
132 size = le16_to_cpu(get_unaligned((u16 *) e->pos));
133 e->pos += sizeof(u16);
134 if (!inbounds(e, size))
135 return 0;
136 *chunk = e->pos;
137 e->pos += size;
138 return size;
139}
140
141/* unpack control byte */
142static bool unpack_X(struct aa_ext *e, enum aa_code code)
143{
144 if (!inbounds(e, 1))
145 return 0;
146 if (*(u8 *) e->pos != code)
147 return 0;
148 e->pos++;
149 return 1;
150}
151
152/**
153 * unpack_nameX - check is the next element is of type X with a name of @name
154 * @e: serialized data extent information (NOT NULL)
155 * @code: type code
156 * @name: name to match to the serialized element. (MAYBE NULL)
157 *
158 * check that the next serialized data element is of type X and has a tag
159 * name @name. If @name is specified then there must be a matching
160 * name element in the stream. If @name is NULL any name element will be
161 * skipped and only the typecode will be tested.
162 *
163 * Returns 1 on success (both type code and name tests match) and the read
164 * head is advanced past the headers
165 *
166 * Returns: 0 if either match fails, the read head does not move
167 */
168static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
169{
170 /*
171 * May need to reset pos if name or type doesn't match
172 */
173 void *pos = e->pos;
174 /*
175 * Check for presence of a tagname, and if present name size
176 * AA_NAME tag value is a u16.
177 */
178 if (unpack_X(e, AA_NAME)) {
179 char *tag = NULL;
180 size_t size = unpack_u16_chunk(e, &tag);
181 /* if a name is specified it must match. otherwise skip tag */
182 if (name && (!size || strcmp(name, tag)))
183 goto fail;
184 } else if (name) {
185 /* if a name is specified and there is no name tag fail */
186 goto fail;
187 }
188
189 /* now check if type code matches */
190 if (unpack_X(e, code))
191 return 1;
192
193fail:
194 e->pos = pos;
195 return 0;
196}
197
198static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
199{
200 if (unpack_nameX(e, AA_U32, name)) {
201 if (!inbounds(e, sizeof(u32)))
202 return 0;
203 if (data)
204 *data = le32_to_cpu(get_unaligned((u32 *) e->pos));
205 e->pos += sizeof(u32);
206 return 1;
207 }
208 return 0;
209}
210
211static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
212{
213 if (unpack_nameX(e, AA_U64, name)) {
214 if (!inbounds(e, sizeof(u64)))
215 return 0;
216 if (data)
217 *data = le64_to_cpu(get_unaligned((u64 *) e->pos));
218 e->pos += sizeof(u64);
219 return 1;
220 }
221 return 0;
222}
223
224static size_t unpack_array(struct aa_ext *e, const char *name)
225{
226 if (unpack_nameX(e, AA_ARRAY, name)) {
227 int size;
228 if (!inbounds(e, sizeof(u16)))
229 return 0;
230 size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos));
231 e->pos += sizeof(u16);
232 return size;
233 }
234 return 0;
235}
236
237static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
238{
239 if (unpack_nameX(e, AA_BLOB, name)) {
240 u32 size;
241 if (!inbounds(e, sizeof(u32)))
242 return 0;
243 size = le32_to_cpu(get_unaligned((u32 *) e->pos));
244 e->pos += sizeof(u32);
245 if (inbounds(e, (size_t) size)) {
246 *blob = e->pos;
247 e->pos += size;
248 return size;
249 }
250 }
251 return 0;
252}
253
254static int unpack_str(struct aa_ext *e, const char **string, const char *name)
255{
256 char *src_str;
257 size_t size = 0;
258 void *pos = e->pos;
259 *string = NULL;
260 if (unpack_nameX(e, AA_STRING, name)) {
261 size = unpack_u16_chunk(e, &src_str);
262 if (size) {
263 /* strings are null terminated, length is size - 1 */
264 if (src_str[size - 1] != 0)
265 goto fail;
266 *string = src_str;
267 }
268 }
269 return size;
270
271fail:
272 e->pos = pos;
273 return 0;
274}
275
276static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
277{
278 const char *tmp;
279 void *pos = e->pos;
280 int res = unpack_str(e, &tmp, name);
281 *string = NULL;
282
283 if (!res)
284 return 0;
285
286 *string = kmemdup(tmp, res, GFP_KERNEL);
287 if (!*string) {
288 e->pos = pos;
289 return 0;
290 }
291
292 return res;
293}
294
180a6f59
JJ
295#define DFA_VALID_PERM_MASK 0xffffffff
296#define DFA_VALID_PERM2_MASK 0xffffffff
297
736ec752
JJ
298/**
299 * verify_accept - verify the accept tables of a dfa
300 * @dfa: dfa to verify accept tables of (NOT NULL)
301 * @flags: flags governing dfa
302 *
303 * Returns: 1 if valid accept tables else 0 if error
304 */
305static bool verify_accept(struct aa_dfa *dfa, int flags)
306{
307 int i;
308
309 /* verify accept permissions */
310 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
311 int mode = ACCEPT_TABLE(dfa)[i];
312
313 if (mode & ~DFA_VALID_PERM_MASK)
314 return 0;
315
316 if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
317 return 0;
318 }
319 return 1;
320}
321
322/**
323 * unpack_dfa - unpack a file rule dfa
324 * @e: serialized data extent information (NOT NULL)
325 *
326 * returns dfa or ERR_PTR or NULL if no dfa
327 */
328static struct aa_dfa *unpack_dfa(struct aa_ext *e)
329{
330 char *blob = NULL;
331 size_t size;
332 struct aa_dfa *dfa = NULL;
333
334 size = unpack_blob(e, &blob, "aadfa");
335 if (size) {
336 /*
337 * The dfa is aligned with in the blob to 8 bytes
338 * from the beginning of the stream.
dd51c848 339 * alignment adjust needed by dfa unpack
736ec752 340 */
dd51c848
JJ
341 size_t sz = blob - (char *) e->start -
342 ((e->pos - e->start) & 7);
736ec752
JJ
343 size_t pad = ALIGN(sz, 8) - sz;
344 int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
abbf8734 345 TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
736ec752
JJ
346 dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
347
348 if (IS_ERR(dfa))
349 return dfa;
350
351 if (!verify_accept(dfa, flags))
352 goto fail;
353 }
354
355 return dfa;
356
357fail:
358 aa_put_dfa(dfa);
359 return ERR_PTR(-EPROTO);
360}
361
362/**
363 * unpack_trans_table - unpack a profile transition table
364 * @e: serialized data extent information (NOT NULL)
365 * @profile: profile to add the accept table to (NOT NULL)
366 *
25985edc 367 * Returns: 1 if table successfully unpacked
736ec752
JJ
368 */
369static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
370{
371 void *pos = e->pos;
372
373 /* exec table is optional */
374 if (unpack_nameX(e, AA_STRUCT, "xtable")) {
375 int i, size;
376
377 size = unpack_array(e, NULL);
378 /* currently 4 exec bits and entries 0-3 are reserved iupcx */
379 if (size > 16 - 4)
380 goto fail;
381 profile->file.trans.table = kzalloc(sizeof(char *) * size,
382 GFP_KERNEL);
383 if (!profile->file.trans.table)
384 goto fail;
385
386 profile->file.trans.size = size;
387 for (i = 0; i < size; i++) {
388 char *str;
7ee95850 389 int c, j, size2 = unpack_strdup(e, &str, NULL);
736ec752
JJ
390 /* unpack_strdup verifies that the last character is
391 * null termination byte.
392 */
7ee95850 393 if (!size2)
736ec752
JJ
394 goto fail;
395 profile->file.trans.table[i] = str;
396 /* verify that name doesn't start with space */
397 if (isspace(*str))
398 goto fail;
399
400 /* count internal # of internal \0 */
7ee95850 401 for (c = j = 0; j < size2 - 2; j++) {
736ec752
JJ
402 if (!str[j])
403 c++;
404 }
405 if (*str == ':') {
406 /* beginning with : requires an embedded \0,
407 * verify that exactly 1 internal \0 exists
408 * trailing \0 already verified by unpack_strdup
409 */
410 if (c != 1)
411 goto fail;
412 /* first character after : must be valid */
413 if (!str[1])
414 goto fail;
415 } else if (c)
416 /* fail - all other cases with embedded \0 */
417 goto fail;
418 }
419 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
420 goto fail;
421 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
422 goto fail;
423 }
424 return 1;
425
426fail:
427 aa_free_domain_entries(&profile->file.trans);
428 e->pos = pos;
429 return 0;
430}
431
432static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
433{
434 void *pos = e->pos;
435
436 /* rlimits are optional */
437 if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
438 int i, size;
439 u32 tmp = 0;
440 if (!unpack_u32(e, &tmp, NULL))
441 goto fail;
442 profile->rlimits.mask = tmp;
443
444 size = unpack_array(e, NULL);
445 if (size > RLIM_NLIMITS)
446 goto fail;
447 for (i = 0; i < size; i++) {
7ee95850 448 u64 tmp2 = 0;
736ec752 449 int a = aa_map_resource(i);
7ee95850 450 if (!unpack_u64(e, &tmp2, NULL))
736ec752 451 goto fail;
7ee95850 452 profile->rlimits.limits[a].rlim_max = tmp2;
736ec752
JJ
453 }
454 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
455 goto fail;
456 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
457 goto fail;
458 }
459 return 1;
460
461fail:
462 e->pos = pos;
463 return 0;
464}
465
466/**
467 * unpack_profile - unpack a serialized profile
468 * @e: serialized data extent information (NOT NULL)
469 *
470 * NOTE: unpack profile sets audit struct if there is a failure
471 */
472static struct aa_profile *unpack_profile(struct aa_ext *e)
473{
474 struct aa_profile *profile = NULL;
475 const char *name = NULL;
ad5ff3db 476 int i, error = -EPROTO;
736ec752
JJ
477 kernel_cap_t tmpcap;
478 u32 tmp;
479
480 /* check that we have the right struct being passed */
481 if (!unpack_nameX(e, AA_STRUCT, "profile"))
482 goto fail;
483 if (!unpack_str(e, &name, NULL))
484 goto fail;
485
30b026a8 486 profile = aa_alloc_profile(name, GFP_KERNEL);
736ec752
JJ
487 if (!profile)
488 return ERR_PTR(-ENOMEM);
489
490 /* profile renaming is optional */
491 (void) unpack_str(e, &profile->rename, "rename");
492
556d0be7
JJ
493 /* attachment string is optional */
494 (void) unpack_str(e, &profile->attach, "attach");
495
736ec752
JJ
496 /* xmatch is optional and may be NULL */
497 profile->xmatch = unpack_dfa(e);
498 if (IS_ERR(profile->xmatch)) {
499 error = PTR_ERR(profile->xmatch);
500 profile->xmatch = NULL;
501 goto fail;
502 }
503 /* xmatch_len is not optional if xmatch is set */
504 if (profile->xmatch) {
505 if (!unpack_u32(e, &tmp, NULL))
506 goto fail;
507 profile->xmatch_len = tmp;
508 }
509
510 /* per profile debug flags (complain, audit) */
511 if (!unpack_nameX(e, AA_STRUCT, "flags"))
512 goto fail;
513 if (!unpack_u32(e, &tmp, NULL))
514 goto fail;
03816507 515 if (tmp & PACKED_FLAG_HAT)
736ec752
JJ
516 profile->flags |= PFLAG_HAT;
517 if (!unpack_u32(e, &tmp, NULL))
518 goto fail;
5ebfb128 519 if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
736ec752 520 profile->mode = APPARMOR_COMPLAIN;
03816507
JJ
521 else if (tmp == PACKED_MODE_KILL)
522 profile->mode = APPARMOR_KILL;
523 else if (tmp == PACKED_MODE_UNCONFINED)
524 profile->mode = APPARMOR_UNCONFINED;
736ec752
JJ
525 if (!unpack_u32(e, &tmp, NULL))
526 goto fail;
527 if (tmp)
528 profile->audit = AUDIT_ALL;
529
530 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
531 goto fail;
532
533 /* path_flags is optional */
534 if (unpack_u32(e, &profile->path_flags, "path_flags"))
535 profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
536 else
537 /* set a default value if path_flags field is not present */
538 profile->path_flags = PFLAG_MEDIATE_DELETED;
539
540 if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
541 goto fail;
542 if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
543 goto fail;
544 if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
545 goto fail;
546 if (!unpack_u32(e, &tmpcap.cap[0], NULL))
547 goto fail;
548
549 if (unpack_nameX(e, AA_STRUCT, "caps64")) {
550 /* optional upper half of 64 bit caps */
551 if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
552 goto fail;
553 if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
554 goto fail;
555 if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
556 goto fail;
557 if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
558 goto fail;
559 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
560 goto fail;
561 }
562
563 if (unpack_nameX(e, AA_STRUCT, "capsx")) {
564 /* optional extended caps mediation mask */
565 if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
566 goto fail;
567 if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
568 goto fail;
cdbd2884
JJ
569 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
570 goto fail;
736ec752
JJ
571 }
572
573 if (!unpack_rlimits(e, profile))
574 goto fail;
575
ad5ff3db
JJ
576 if (unpack_nameX(e, AA_STRUCT, "policydb")) {
577 /* generic policy dfa - optional and may be NULL */
578 profile->policy.dfa = unpack_dfa(e);
579 if (IS_ERR(profile->policy.dfa)) {
580 error = PTR_ERR(profile->policy.dfa);
581 profile->policy.dfa = NULL;
582 goto fail;
5f20fdfe
JJ
583 } else if (!profile->policy.dfa) {
584 error = -EPROTO;
585 goto fail;
ad5ff3db
JJ
586 }
587 if (!unpack_u32(e, &profile->policy.start[0], "start"))
588 /* default start state */
589 profile->policy.start[0] = DFA_START;
590 /* setup class index */
591 for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
592 profile->policy.start[i] =
593 aa_dfa_next(profile->policy.dfa,
594 profile->policy.start[0],
595 i);
596 }
597 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
598 goto fail;
599 }
600
736ec752
JJ
601 /* get file rules */
602 profile->file.dfa = unpack_dfa(e);
603 if (IS_ERR(profile->file.dfa)) {
604 error = PTR_ERR(profile->file.dfa);
605 profile->file.dfa = NULL;
606 goto fail;
607 }
608
609 if (!unpack_u32(e, &profile->file.start, "dfa_start"))
610 /* default start state */
611 profile->file.start = DFA_START;
612
613 if (!unpack_trans_table(e, profile))
614 goto fail;
615
616 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
617 goto fail;
618
619 return profile;
620
621fail:
622 if (profile)
623 name = NULL;
624 else if (!name)
625 name = "unknown";
626 audit_iface(profile, name, "failed to unpack profile", e, error);
8651e1d6 627 aa_free_profile(profile);
736ec752
JJ
628
629 return ERR_PTR(error);
630}
631
632/**
633 * verify_head - unpack serialized stream header
634 * @e: serialized data read head (NOT NULL)
dd51c848 635 * @required: whether the header is required or optional
736ec752
JJ
636 * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
637 *
638 * Returns: error or 0 if header is good
639 */
dd51c848 640static int verify_header(struct aa_ext *e, int required, const char **ns)
736ec752
JJ
641{
642 int error = -EPROTONOSUPPORT;
dd51c848
JJ
643 const char *name = NULL;
644 *ns = NULL;
645
736ec752
JJ
646 /* get the interface version */
647 if (!unpack_u32(e, &e->version, "version")) {
dd51c848
JJ
648 if (required) {
649 audit_iface(NULL, NULL, "invalid profile format", e,
650 error);
651 return error;
652 }
736ec752 653
dd51c848
JJ
654 /* check that the interface version is currently supported */
655 if (e->version != 5) {
656 audit_iface(NULL, NULL, "unsupported interface version",
657 e, error);
658 return error;
659 }
736ec752
JJ
660 }
661
dd51c848 662
736ec752 663 /* read the namespace if present */
dd51c848
JJ
664 if (unpack_str(e, &name, "namespace")) {
665 if (*ns && strcmp(*ns, name))
666 audit_iface(NULL, NULL, "invalid ns change", e, error);
667 else if (!*ns)
668 *ns = name;
669 }
736ec752
JJ
670
671 return 0;
672}
673
674static bool verify_xindex(int xindex, int table_size)
675{
676 int index, xtype;
677 xtype = xindex & AA_X_TYPE_MASK;
678 index = xindex & AA_X_INDEX_MASK;
23ca7b64 679 if (xtype == AA_X_TABLE && index >= table_size)
736ec752
JJ
680 return 0;
681 return 1;
682}
683
684/* verify dfa xindexes are in range of transition tables */
685static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
686{
687 int i;
688 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
689 if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
690 return 0;
691 if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
692 return 0;
693 }
694 return 1;
695}
696
697/**
698 * verify_profile - Do post unpack analysis to verify profile consistency
699 * @profile: profile to verify (NOT NULL)
700 *
701 * Returns: 0 if passes verification else error
702 */
703static int verify_profile(struct aa_profile *profile)
704{
abbf8734
JJ
705 if (profile->file.dfa &&
706 !verify_dfa_xindex(profile->file.dfa,
707 profile->file.trans.size)) {
708 audit_iface(profile, NULL, "Invalid named transition",
709 NULL, -EPROTO);
710 return -EPROTO;
736ec752
JJ
711 }
712
713 return 0;
714}
715
dd51c848
JJ
716void aa_load_ent_free(struct aa_load_ent *ent)
717{
718 if (ent) {
719 aa_put_profile(ent->rename);
720 aa_put_profile(ent->old);
721 aa_put_profile(ent->new);
722 kzfree(ent);
723 }
724}
725
726struct aa_load_ent *aa_load_ent_alloc(void)
727{
728 struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
729 if (ent)
730 INIT_LIST_HEAD(&ent->list);
731 return ent;
732}
733
736ec752 734/**
dd51c848 735 * aa_unpack - unpack packed binary profile(s) data loaded from user space
736ec752
JJ
736 * @udata: user data copied to kmem (NOT NULL)
737 * @size: the size of the user data
dd51c848 738 * @lh: list to place unpacked profiles in a aa_repl_ws
736ec752
JJ
739 * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
740 *
dd51c848
JJ
741 * Unpack user data and return refcounted allocated profile(s) stored in
742 * @lh in order of discovery, with the list chain stored in base.list
743 * or error
736ec752 744 *
dd51c848 745 * Returns: profile(s) on @lh else error pointer if fails to unpack
736ec752 746 */
dd51c848 747int aa_unpack(void *udata, size_t size, struct list_head *lh, const char **ns)
736ec752 748{
dd51c848 749 struct aa_load_ent *tmp, *ent;
736ec752
JJ
750 struct aa_profile *profile = NULL;
751 int error;
752 struct aa_ext e = {
753 .start = udata,
754 .end = udata + size,
755 .pos = udata,
756 };
757
dd51c848
JJ
758 *ns = NULL;
759 while (e.pos < e.end) {
f8eb8a13 760 void *start;
dd51c848
JJ
761 error = verify_header(&e, e.pos == e.start, ns);
762 if (error)
763 goto fail;
736ec752 764
f8eb8a13 765 start = e.pos;
dd51c848
JJ
766 profile = unpack_profile(&e);
767 if (IS_ERR(profile)) {
768 error = PTR_ERR(profile);
769 goto fail;
770 }
771
772 error = verify_profile(profile);
f8eb8a13
JJ
773 if (error)
774 goto fail_profile;
775
7616ac70 776 error = aa_calc_profile_hash(profile, e.version, start,
6059f71f 777 e.pos - start);
f8eb8a13
JJ
778 if (error)
779 goto fail_profile;
dd51c848
JJ
780
781 ent = aa_load_ent_alloc();
782 if (!ent) {
783 error = -ENOMEM;
f8eb8a13 784 goto fail_profile;
dd51c848 785 }
736ec752 786
dd51c848
JJ
787 ent->new = profile;
788 list_add_tail(&ent->list, lh);
736ec752
JJ
789 }
790
dd51c848
JJ
791 return 0;
792
f8eb8a13
JJ
793fail_profile:
794 aa_put_profile(profile);
795
dd51c848
JJ
796fail:
797 list_for_each_entry_safe(ent, tmp, lh, list) {
798 list_del_init(&ent->list);
799 aa_load_ent_free(ent);
800 }
801
802 return error;
736ec752 803}