blob: 6ac292fec55f9d1f1b17b82cc694039848a79146 [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 Dunlapd410fa42011-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"
John Johansenf8eb8a12013-08-14 11:27:36 -070027#include "include/crypto.h"
John Johansen736ec7522010-07-29 14:48:02 -070028#include "include/match.h"
29#include "include/policy.h"
30#include "include/policy_unpack.h"
John Johansen736ec7522010-07-29 14:48:02 -070031
John Johansen474d6b752017-01-16 00:42:39 -080032#define K_ABI_MASK 0x3ff
John Johansen5ebfb122017-01-16 00:42:38 -080033#define FORCE_COMPLAIN_FLAG 0x800
John Johansen474d6b752017-01-16 00:42:39 -080034#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 */
John Johansen5ebfb122017-01-16 00:42:38 -080040
John Johansen736ec7522010-07-29 14:48:02 -070041/*
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
50enum 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 */
71struct 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 */
79static void audit_cb(struct audit_buffer *ab, void *va)
80{
81 struct common_audit_data *sa = va;
Eric Paris3b3b0e42012-04-03 09:37:02 -070082 if (sa->aad->iface.target) {
83 struct aa_profile *name = sa->aad->iface.target;
John Johansen736ec7522010-07-29 14:48:02 -070084 audit_log_format(ab, " name=");
85 audit_log_untrustedstring(ab, name->base.hname);
86 }
Eric Paris3b3b0e42012-04-03 09:37:02 -070087 if (sa->aad->iface.pos)
88 audit_log_format(ab, " offset=%ld", sa->aad->iface.pos);
John Johansen736ec7522010-07-29 14:48:02 -070089}
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 * @name: name of the profile being manipulated (MAYBE NULL)
95 * @info: any extra info about the failure (MAYBE NULL)
John Johansenb1b4bc22012-03-10 11:25:30 -080096 * @e: buffer position info
John Johansen736ec7522010-07-29 14:48:02 -070097 * @error: error code
98 *
99 * Returns: %0 or error
100 */
101static int audit_iface(struct aa_profile *new, const char *name,
102 const char *info, struct aa_ext *e, int error)
103{
104 struct aa_profile *profile = __aa_current_profile();
105 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700106 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -0400107 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700108 sa.aad = &aad;
John Johansenb1b4bc22012-03-10 11:25:30 -0800109 if (e)
Eric Paris3b3b0e42012-04-03 09:37:02 -0700110 aad.iface.pos = e->pos - e->start;
111 aad.iface.target = new;
112 aad.name = name;
113 aad.info = info;
114 aad.error = error;
John Johansen736ec7522010-07-29 14:48:02 -0700115
116 return aa_audit(AUDIT_APPARMOR_STATUS, profile, GFP_KERNEL, &sa,
117 audit_cb);
118}
119
120/* test if read will be in packed data bounds */
121static bool inbounds(struct aa_ext *e, size_t size)
122{
123 return (size <= e->end - e->pos);
124}
125
126/**
127 * aa_u16_chunck - test and do bounds checking for a u16 size based chunk
128 * @e: serialized data read head (NOT NULL)
129 * @chunk: start address for chunk of data (NOT NULL)
130 *
131 * Returns: the size of chunk found with the read head at the end of the chunk.
132 */
133static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk)
134{
135 size_t size = 0;
136
137 if (!inbounds(e, sizeof(u16)))
138 return 0;
139 size = le16_to_cpu(get_unaligned((u16 *) e->pos));
140 e->pos += sizeof(u16);
141 if (!inbounds(e, size))
142 return 0;
143 *chunk = e->pos;
144 e->pos += size;
145 return size;
146}
147
148/* unpack control byte */
149static bool unpack_X(struct aa_ext *e, enum aa_code code)
150{
151 if (!inbounds(e, 1))
152 return 0;
153 if (*(u8 *) e->pos != code)
154 return 0;
155 e->pos++;
156 return 1;
157}
158
159/**
160 * unpack_nameX - check is the next element is of type X with a name of @name
161 * @e: serialized data extent information (NOT NULL)
162 * @code: type code
163 * @name: name to match to the serialized element. (MAYBE NULL)
164 *
165 * check that the next serialized data element is of type X and has a tag
166 * name @name. If @name is specified then there must be a matching
167 * name element in the stream. If @name is NULL any name element will be
168 * skipped and only the typecode will be tested.
169 *
170 * Returns 1 on success (both type code and name tests match) and the read
171 * head is advanced past the headers
172 *
173 * Returns: 0 if either match fails, the read head does not move
174 */
175static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name)
176{
177 /*
178 * May need to reset pos if name or type doesn't match
179 */
180 void *pos = e->pos;
181 /*
182 * Check for presence of a tagname, and if present name size
183 * AA_NAME tag value is a u16.
184 */
185 if (unpack_X(e, AA_NAME)) {
186 char *tag = NULL;
187 size_t size = unpack_u16_chunk(e, &tag);
188 /* if a name is specified it must match. otherwise skip tag */
189 if (name && (!size || strcmp(name, tag)))
190 goto fail;
191 } else if (name) {
192 /* if a name is specified and there is no name tag fail */
193 goto fail;
194 }
195
196 /* now check if type code matches */
197 if (unpack_X(e, code))
198 return 1;
199
200fail:
201 e->pos = pos;
202 return 0;
203}
204
205static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name)
206{
207 if (unpack_nameX(e, AA_U32, name)) {
208 if (!inbounds(e, sizeof(u32)))
209 return 0;
210 if (data)
211 *data = le32_to_cpu(get_unaligned((u32 *) e->pos));
212 e->pos += sizeof(u32);
213 return 1;
214 }
215 return 0;
216}
217
218static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name)
219{
220 if (unpack_nameX(e, AA_U64, name)) {
221 if (!inbounds(e, sizeof(u64)))
222 return 0;
223 if (data)
224 *data = le64_to_cpu(get_unaligned((u64 *) e->pos));
225 e->pos += sizeof(u64);
226 return 1;
227 }
228 return 0;
229}
230
231static size_t unpack_array(struct aa_ext *e, const char *name)
232{
233 if (unpack_nameX(e, AA_ARRAY, name)) {
234 int size;
235 if (!inbounds(e, sizeof(u16)))
236 return 0;
237 size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos));
238 e->pos += sizeof(u16);
239 return size;
240 }
241 return 0;
242}
243
244static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name)
245{
246 if (unpack_nameX(e, AA_BLOB, name)) {
247 u32 size;
248 if (!inbounds(e, sizeof(u32)))
249 return 0;
250 size = le32_to_cpu(get_unaligned((u32 *) e->pos));
251 e->pos += sizeof(u32);
252 if (inbounds(e, (size_t) size)) {
253 *blob = e->pos;
254 e->pos += size;
255 return size;
256 }
257 }
258 return 0;
259}
260
261static int unpack_str(struct aa_ext *e, const char **string, const char *name)
262{
263 char *src_str;
264 size_t size = 0;
265 void *pos = e->pos;
266 *string = NULL;
267 if (unpack_nameX(e, AA_STRING, name)) {
268 size = unpack_u16_chunk(e, &src_str);
269 if (size) {
270 /* strings are null terminated, length is size - 1 */
271 if (src_str[size - 1] != 0)
272 goto fail;
273 *string = src_str;
274 }
275 }
276 return size;
277
278fail:
279 e->pos = pos;
280 return 0;
281}
282
283static int unpack_strdup(struct aa_ext *e, char **string, const char *name)
284{
285 const char *tmp;
286 void *pos = e->pos;
287 int res = unpack_str(e, &tmp, name);
288 *string = NULL;
289
290 if (!res)
291 return 0;
292
293 *string = kmemdup(tmp, res, GFP_KERNEL);
294 if (!*string) {
295 e->pos = pos;
296 return 0;
297 }
298
299 return res;
300}
301
John Johansen180a6f52013-02-18 16:09:34 -0800302#define DFA_VALID_PERM_MASK 0xffffffff
303#define DFA_VALID_PERM2_MASK 0xffffffff
304
John Johansen736ec7522010-07-29 14:48:02 -0700305/**
306 * verify_accept - verify the accept tables of a dfa
307 * @dfa: dfa to verify accept tables of (NOT NULL)
308 * @flags: flags governing dfa
309 *
310 * Returns: 1 if valid accept tables else 0 if error
311 */
312static bool verify_accept(struct aa_dfa *dfa, int flags)
313{
314 int i;
315
316 /* verify accept permissions */
317 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
318 int mode = ACCEPT_TABLE(dfa)[i];
319
320 if (mode & ~DFA_VALID_PERM_MASK)
321 return 0;
322
323 if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK)
324 return 0;
325 }
326 return 1;
327}
328
329/**
330 * unpack_dfa - unpack a file rule dfa
331 * @e: serialized data extent information (NOT NULL)
332 *
333 * returns dfa or ERR_PTR or NULL if no dfa
334 */
335static struct aa_dfa *unpack_dfa(struct aa_ext *e)
336{
337 char *blob = NULL;
338 size_t size;
339 struct aa_dfa *dfa = NULL;
340
341 size = unpack_blob(e, &blob, "aadfa");
342 if (size) {
343 /*
344 * The dfa is aligned with in the blob to 8 bytes
345 * from the beginning of the stream.
John Johansendd51c8482013-07-10 21:05:43 -0700346 * alignment adjust needed by dfa unpack
John Johansen736ec7522010-07-29 14:48:02 -0700347 */
John Johansendd51c8482013-07-10 21:05:43 -0700348 size_t sz = blob - (char *) e->start -
349 ((e->pos - e->start) & 7);
John Johansen736ec7522010-07-29 14:48:02 -0700350 size_t pad = ALIGN(sz, 8) - sz;
351 int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) |
John Johansenabbf8732017-01-16 00:42:37 -0800352 TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES;
John Johansen736ec7522010-07-29 14:48:02 -0700353 dfa = aa_dfa_unpack(blob + pad, size - pad, flags);
354
355 if (IS_ERR(dfa))
356 return dfa;
357
358 if (!verify_accept(dfa, flags))
359 goto fail;
360 }
361
362 return dfa;
363
364fail:
365 aa_put_dfa(dfa);
366 return ERR_PTR(-EPROTO);
367}
368
369/**
370 * unpack_trans_table - unpack a profile transition table
371 * @e: serialized data extent information (NOT NULL)
372 * @profile: profile to add the accept table to (NOT NULL)
373 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * Returns: 1 if table successfully unpacked
John Johansen736ec7522010-07-29 14:48:02 -0700375 */
376static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile)
377{
378 void *pos = e->pos;
379
380 /* exec table is optional */
381 if (unpack_nameX(e, AA_STRUCT, "xtable")) {
382 int i, size;
383
384 size = unpack_array(e, NULL);
385 /* currently 4 exec bits and entries 0-3 are reserved iupcx */
386 if (size > 16 - 4)
387 goto fail;
388 profile->file.trans.table = kzalloc(sizeof(char *) * size,
389 GFP_KERNEL);
390 if (!profile->file.trans.table)
391 goto fail;
392
393 profile->file.trans.size = size;
394 for (i = 0; i < size; i++) {
395 char *str;
James Morris7ee95852011-08-29 11:43:02 +1000396 int c, j, size2 = unpack_strdup(e, &str, NULL);
John Johansen736ec7522010-07-29 14:48:02 -0700397 /* unpack_strdup verifies that the last character is
398 * null termination byte.
399 */
James Morris7ee95852011-08-29 11:43:02 +1000400 if (!size2)
John Johansen736ec7522010-07-29 14:48:02 -0700401 goto fail;
402 profile->file.trans.table[i] = str;
403 /* verify that name doesn't start with space */
404 if (isspace(*str))
405 goto fail;
406
407 /* count internal # of internal \0 */
James Morris7ee95852011-08-29 11:43:02 +1000408 for (c = j = 0; j < size2 - 2; j++) {
John Johansen736ec7522010-07-29 14:48:02 -0700409 if (!str[j])
410 c++;
411 }
412 if (*str == ':') {
413 /* beginning with : requires an embedded \0,
414 * verify that exactly 1 internal \0 exists
415 * trailing \0 already verified by unpack_strdup
416 */
417 if (c != 1)
418 goto fail;
419 /* first character after : must be valid */
420 if (!str[1])
421 goto fail;
422 } else if (c)
423 /* fail - all other cases with embedded \0 */
424 goto fail;
425 }
426 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
427 goto fail;
428 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
429 goto fail;
430 }
431 return 1;
432
433fail:
434 aa_free_domain_entries(&profile->file.trans);
435 e->pos = pos;
436 return 0;
437}
438
439static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile)
440{
441 void *pos = e->pos;
442
443 /* rlimits are optional */
444 if (unpack_nameX(e, AA_STRUCT, "rlimits")) {
445 int i, size;
446 u32 tmp = 0;
447 if (!unpack_u32(e, &tmp, NULL))
448 goto fail;
449 profile->rlimits.mask = tmp;
450
451 size = unpack_array(e, NULL);
452 if (size > RLIM_NLIMITS)
453 goto fail;
454 for (i = 0; i < size; i++) {
James Morris7ee95852011-08-29 11:43:02 +1000455 u64 tmp2 = 0;
John Johansen736ec7522010-07-29 14:48:02 -0700456 int a = aa_map_resource(i);
James Morris7ee95852011-08-29 11:43:02 +1000457 if (!unpack_u64(e, &tmp2, NULL))
John Johansen736ec7522010-07-29 14:48:02 -0700458 goto fail;
James Morris7ee95852011-08-29 11:43:02 +1000459 profile->rlimits.limits[a].rlim_max = tmp2;
John Johansen736ec7522010-07-29 14:48:02 -0700460 }
461 if (!unpack_nameX(e, AA_ARRAYEND, NULL))
462 goto fail;
463 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
464 goto fail;
465 }
466 return 1;
467
468fail:
469 e->pos = pos;
470 return 0;
471}
472
473/**
474 * unpack_profile - unpack a serialized profile
475 * @e: serialized data extent information (NOT NULL)
476 *
477 * NOTE: unpack profile sets audit struct if there is a failure
478 */
479static struct aa_profile *unpack_profile(struct aa_ext *e)
480{
481 struct aa_profile *profile = NULL;
482 const char *name = NULL;
John Johansenad5ff3d2012-02-16 07:07:53 -0800483 int i, error = -EPROTO;
John Johansen736ec7522010-07-29 14:48:02 -0700484 kernel_cap_t tmpcap;
485 u32 tmp;
486
487 /* check that we have the right struct being passed */
488 if (!unpack_nameX(e, AA_STRUCT, "profile"))
489 goto fail;
490 if (!unpack_str(e, &name, NULL))
491 goto fail;
492
John Johansen30b026a2017-01-16 00:42:35 -0800493 profile = aa_alloc_profile(name, GFP_KERNEL);
John Johansen736ec7522010-07-29 14:48:02 -0700494 if (!profile)
495 return ERR_PTR(-ENOMEM);
496
497 /* profile renaming is optional */
498 (void) unpack_str(e, &profile->rename, "rename");
499
John Johansen556d0be2013-07-10 21:17:43 -0700500 /* attachment string is optional */
501 (void) unpack_str(e, &profile->attach, "attach");
502
John Johansen736ec7522010-07-29 14:48:02 -0700503 /* xmatch is optional and may be NULL */
504 profile->xmatch = unpack_dfa(e);
505 if (IS_ERR(profile->xmatch)) {
506 error = PTR_ERR(profile->xmatch);
507 profile->xmatch = NULL;
508 goto fail;
509 }
510 /* xmatch_len is not optional if xmatch is set */
511 if (profile->xmatch) {
512 if (!unpack_u32(e, &tmp, NULL))
513 goto fail;
514 profile->xmatch_len = tmp;
515 }
516
517 /* per profile debug flags (complain, audit) */
518 if (!unpack_nameX(e, AA_STRUCT, "flags"))
519 goto fail;
520 if (!unpack_u32(e, &tmp, NULL))
521 goto fail;
John Johansen03816502013-07-10 21:12:43 -0700522 if (tmp & PACKED_FLAG_HAT)
John Johansen736ec7522010-07-29 14:48:02 -0700523 profile->flags |= PFLAG_HAT;
524 if (!unpack_u32(e, &tmp, NULL))
525 goto fail;
John Johansen5ebfb122017-01-16 00:42:38 -0800526 if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG))
John Johansen736ec7522010-07-29 14:48:02 -0700527 profile->mode = APPARMOR_COMPLAIN;
John Johansen03816502013-07-10 21:12:43 -0700528 else if (tmp == PACKED_MODE_KILL)
529 profile->mode = APPARMOR_KILL;
530 else if (tmp == PACKED_MODE_UNCONFINED)
531 profile->mode = APPARMOR_UNCONFINED;
John Johansen736ec7522010-07-29 14:48:02 -0700532 if (!unpack_u32(e, &tmp, NULL))
533 goto fail;
534 if (tmp)
535 profile->audit = AUDIT_ALL;
536
537 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
538 goto fail;
539
540 /* path_flags is optional */
541 if (unpack_u32(e, &profile->path_flags, "path_flags"))
542 profile->path_flags |= profile->flags & PFLAG_MEDIATE_DELETED;
543 else
544 /* set a default value if path_flags field is not present */
545 profile->path_flags = PFLAG_MEDIATE_DELETED;
546
547 if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL))
548 goto fail;
549 if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL))
550 goto fail;
551 if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL))
552 goto fail;
553 if (!unpack_u32(e, &tmpcap.cap[0], NULL))
554 goto fail;
555
556 if (unpack_nameX(e, AA_STRUCT, "caps64")) {
557 /* optional upper half of 64 bit caps */
558 if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL))
559 goto fail;
560 if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL))
561 goto fail;
562 if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL))
563 goto fail;
564 if (!unpack_u32(e, &(tmpcap.cap[1]), NULL))
565 goto fail;
566 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
567 goto fail;
568 }
569
570 if (unpack_nameX(e, AA_STRUCT, "capsx")) {
571 /* optional extended caps mediation mask */
572 if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL))
573 goto fail;
574 if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL))
575 goto fail;
John Johansencdbd2882012-02-16 07:06:41 -0800576 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
577 goto fail;
John Johansen736ec7522010-07-29 14:48:02 -0700578 }
579
580 if (!unpack_rlimits(e, profile))
581 goto fail;
582
John Johansenad5ff3d2012-02-16 07:07:53 -0800583 if (unpack_nameX(e, AA_STRUCT, "policydb")) {
584 /* generic policy dfa - optional and may be NULL */
585 profile->policy.dfa = unpack_dfa(e);
586 if (IS_ERR(profile->policy.dfa)) {
587 error = PTR_ERR(profile->policy.dfa);
588 profile->policy.dfa = NULL;
589 goto fail;
John Johansen5f20fdf2016-06-15 10:00:55 +0300590 } else if (!profile->policy.dfa) {
591 error = -EPROTO;
592 goto fail;
John Johansenad5ff3d2012-02-16 07:07:53 -0800593 }
594 if (!unpack_u32(e, &profile->policy.start[0], "start"))
595 /* default start state */
596 profile->policy.start[0] = DFA_START;
597 /* setup class index */
598 for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) {
599 profile->policy.start[i] =
600 aa_dfa_next(profile->policy.dfa,
601 profile->policy.start[0],
602 i);
603 }
604 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
605 goto fail;
606 }
607
John Johansen736ec7522010-07-29 14:48:02 -0700608 /* get file rules */
609 profile->file.dfa = unpack_dfa(e);
610 if (IS_ERR(profile->file.dfa)) {
611 error = PTR_ERR(profile->file.dfa);
612 profile->file.dfa = NULL;
613 goto fail;
614 }
615
616 if (!unpack_u32(e, &profile->file.start, "dfa_start"))
617 /* default start state */
618 profile->file.start = DFA_START;
619
620 if (!unpack_trans_table(e, profile))
621 goto fail;
622
623 if (!unpack_nameX(e, AA_STRUCTEND, NULL))
624 goto fail;
625
626 return profile;
627
628fail:
629 if (profile)
630 name = NULL;
631 else if (!name)
632 name = "unknown";
633 audit_iface(profile, name, "failed to unpack profile", e, error);
John Johansen8651e1d62013-07-10 21:11:43 -0700634 aa_free_profile(profile);
John Johansen736ec7522010-07-29 14:48:02 -0700635
636 return ERR_PTR(error);
637}
638
639/**
640 * verify_head - unpack serialized stream header
641 * @e: serialized data read head (NOT NULL)
John Johansendd51c8482013-07-10 21:05:43 -0700642 * @required: whether the header is required or optional
John Johansen736ec7522010-07-29 14:48:02 -0700643 * @ns: Returns - namespace if one is specified else NULL (NOT NULL)
644 *
645 * Returns: error or 0 if header is good
646 */
John Johansendd51c8482013-07-10 21:05:43 -0700647static int verify_header(struct aa_ext *e, int required, const char **ns)
John Johansen736ec7522010-07-29 14:48:02 -0700648{
649 int error = -EPROTONOSUPPORT;
John Johansendd51c8482013-07-10 21:05:43 -0700650 const char *name = NULL;
651 *ns = NULL;
652
John Johansen736ec7522010-07-29 14:48:02 -0700653 /* get the interface version */
654 if (!unpack_u32(e, &e->version, "version")) {
John Johansendd51c8482013-07-10 21:05:43 -0700655 if (required) {
John Johansen474d6b752017-01-16 00:42:39 -0800656 audit_iface(NULL, NULL, "invalid profile format",
John Johansendd51c8482013-07-10 21:05:43 -0700657 e, error);
658 return error;
659 }
John Johansen736ec7522010-07-29 14:48:02 -0700660 }
661
John Johansen474d6b752017-01-16 00:42:39 -0800662 /* Check that the interface version is currently supported.
663 * if not specified use previous version
664 * Mask off everything that is not kernel abi version
665 */
666 if (VERSION_LT(e->version, v5) && VERSION_GT(e->version, v7)) {
667 audit_iface(NULL, NULL, "unsupported interface version",
668 e, error);
669 return error;
670 }
John Johansen736ec7522010-07-29 14:48:02 -0700671
672 /* read the namespace if present */
John Johansendd51c8482013-07-10 21:05:43 -0700673 if (unpack_str(e, &name, "namespace")) {
674 if (*ns && strcmp(*ns, name))
675 audit_iface(NULL, NULL, "invalid ns change", e, error);
676 else if (!*ns)
677 *ns = name;
678 }
John Johansen736ec7522010-07-29 14:48:02 -0700679
680 return 0;
681}
682
683static bool verify_xindex(int xindex, int table_size)
684{
685 int index, xtype;
686 xtype = xindex & AA_X_TYPE_MASK;
687 index = xindex & AA_X_INDEX_MASK;
John Johansen23ca7b62016-03-17 12:02:54 -0700688 if (xtype == AA_X_TABLE && index >= table_size)
John Johansen736ec7522010-07-29 14:48:02 -0700689 return 0;
690 return 1;
691}
692
693/* verify dfa xindexes are in range of transition tables */
694static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size)
695{
696 int i;
697 for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) {
698 if (!verify_xindex(dfa_user_xindex(dfa, i), table_size))
699 return 0;
700 if (!verify_xindex(dfa_other_xindex(dfa, i), table_size))
701 return 0;
702 }
703 return 1;
704}
705
706/**
707 * verify_profile - Do post unpack analysis to verify profile consistency
708 * @profile: profile to verify (NOT NULL)
709 *
710 * Returns: 0 if passes verification else error
711 */
712static int verify_profile(struct aa_profile *profile)
713{
John Johansenabbf8732017-01-16 00:42:37 -0800714 if (profile->file.dfa &&
715 !verify_dfa_xindex(profile->file.dfa,
716 profile->file.trans.size)) {
717 audit_iface(profile, NULL, "Invalid named transition",
718 NULL, -EPROTO);
719 return -EPROTO;
John Johansen736ec7522010-07-29 14:48:02 -0700720 }
721
722 return 0;
723}
724
John Johansendd51c8482013-07-10 21:05:43 -0700725void aa_load_ent_free(struct aa_load_ent *ent)
726{
727 if (ent) {
728 aa_put_profile(ent->rename);
729 aa_put_profile(ent->old);
730 aa_put_profile(ent->new);
731 kzfree(ent);
732 }
733}
734
735struct aa_load_ent *aa_load_ent_alloc(void)
736{
737 struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL);
738 if (ent)
739 INIT_LIST_HEAD(&ent->list);
740 return ent;
741}
742
John Johansen736ec7522010-07-29 14:48:02 -0700743/**
John Johansendd51c8482013-07-10 21:05:43 -0700744 * aa_unpack - unpack packed binary profile(s) data loaded from user space
John Johansen736ec7522010-07-29 14:48:02 -0700745 * @udata: user data copied to kmem (NOT NULL)
746 * @size: the size of the user data
John Johansendd51c8482013-07-10 21:05:43 -0700747 * @lh: list to place unpacked profiles in a aa_repl_ws
John Johansen736ec7522010-07-29 14:48:02 -0700748 * @ns: Returns namespace profile is in if specified else NULL (NOT NULL)
749 *
John Johansendd51c8482013-07-10 21:05:43 -0700750 * Unpack user data and return refcounted allocated profile(s) stored in
751 * @lh in order of discovery, with the list chain stored in base.list
752 * or error
John Johansen736ec7522010-07-29 14:48:02 -0700753 *
John Johansendd51c8482013-07-10 21:05:43 -0700754 * Returns: profile(s) on @lh else error pointer if fails to unpack
John Johansen736ec7522010-07-29 14:48:02 -0700755 */
John Johansendd51c8482013-07-10 21:05:43 -0700756int aa_unpack(void *udata, size_t size, struct list_head *lh, const char **ns)
John Johansen736ec7522010-07-29 14:48:02 -0700757{
John Johansendd51c8482013-07-10 21:05:43 -0700758 struct aa_load_ent *tmp, *ent;
John Johansen736ec7522010-07-29 14:48:02 -0700759 struct aa_profile *profile = NULL;
760 int error;
761 struct aa_ext e = {
762 .start = udata,
763 .end = udata + size,
764 .pos = udata,
765 };
766
John Johansendd51c8482013-07-10 21:05:43 -0700767 *ns = NULL;
768 while (e.pos < e.end) {
John Johansenf8eb8a12013-08-14 11:27:36 -0700769 void *start;
John Johansendd51c8482013-07-10 21:05:43 -0700770 error = verify_header(&e, e.pos == e.start, ns);
771 if (error)
772 goto fail;
John Johansen736ec7522010-07-29 14:48:02 -0700773
John Johansenf8eb8a12013-08-14 11:27:36 -0700774 start = e.pos;
John Johansendd51c8482013-07-10 21:05:43 -0700775 profile = unpack_profile(&e);
776 if (IS_ERR(profile)) {
777 error = PTR_ERR(profile);
778 goto fail;
779 }
John Johansen736ec7522010-07-29 14:48:02 -0700780
John Johansendd51c8482013-07-10 21:05:43 -0700781 error = verify_profile(profile);
John Johansenf8eb8a12013-08-14 11:27:36 -0700782 if (error)
783 goto fail_profile;
784
Arnd Bergmann7616ac72016-07-25 10:59:07 -0700785 error = aa_calc_profile_hash(profile, e.version, start,
John Johansen6059f712014-10-24 09:16:14 -0700786 e.pos - start);
John Johansenf8eb8a12013-08-14 11:27:36 -0700787 if (error)
788 goto fail_profile;
John Johansendd51c8482013-07-10 21:05:43 -0700789
790 ent = aa_load_ent_alloc();
791 if (!ent) {
792 error = -ENOMEM;
John Johansenf8eb8a12013-08-14 11:27:36 -0700793 goto fail_profile;
John Johansendd51c8482013-07-10 21:05:43 -0700794 }
795
796 ent->new = profile;
797 list_add_tail(&ent->list, lh);
John Johansen736ec7522010-07-29 14:48:02 -0700798 }
799
John Johansendd51c8482013-07-10 21:05:43 -0700800 return 0;
801
John Johansenf8eb8a12013-08-14 11:27:36 -0700802fail_profile:
803 aa_put_profile(profile);
804
John Johansendd51c8482013-07-10 21:05:43 -0700805fail:
806 list_for_each_entry_safe(ent, tmp, lh, list) {
807 list_del_init(&ent->list);
808 aa_load_ent_free(ent);
809 }
810
811 return error;
John Johansen736ec7522010-07-29 14:48:02 -0700812}