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