]>
Commit | Line | Data |
---|---|---|
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> | |
80594fc2 | 23 | #include <linux/string.h> |
b7370901 | 24 | #include <linux/jhash.h> |
736ec752 JJ |
25 | |
26 | #include "include/apparmor.h" | |
27 | #include "include/audit.h" | |
28 | #include "include/context.h" | |
f8eb8a13 | 29 | #include "include/crypto.h" |
736ec752 | 30 | #include "include/match.h" |
80594fc2 | 31 | #include "include/path.h" |
736ec752 JJ |
32 | #include "include/policy.h" |
33 | #include "include/policy_unpack.h" | |
736ec752 | 34 | |
80594fc2 JJ |
35 | #define K_ABI_MASK 0x3ff |
36 | #define FORCE_COMPLAIN_FLAG 0x800 | |
37 | #define VERSION_CMP(OP, X, Y) (((X) & K_ABI_MASK) OP ((Y) & K_ABI_MASK)) | |
38 | ||
39 | #define v5 5 /* base version */ | |
40 | #define v6 6 /* per entry policydb mediation check */ | |
41 | #define v7 7 /* full network masking */ | |
42 | ||
736ec752 JJ |
43 | /* |
44 | * The AppArmor interface treats data as a type byte followed by the | |
45 | * actual data. The interface has the notion of a a named entry | |
46 | * which has a name (AA_NAME typecode followed by name string) followed by | |
47 | * the entries typecode and data. Named types allow for optional | |
48 | * elements and extensions to be added and tested for without breaking | |
49 | * backwards compatibility. | |
50 | */ | |
51 | ||
52 | enum aa_code { | |
53 | AA_U8, | |
54 | AA_U16, | |
55 | AA_U32, | |
56 | AA_U64, | |
57 | AA_NAME, /* same as string except it is items name */ | |
58 | AA_STRING, | |
59 | AA_BLOB, | |
60 | AA_STRUCT, | |
61 | AA_STRUCTEND, | |
62 | AA_LIST, | |
63 | AA_LISTEND, | |
64 | AA_ARRAY, | |
65 | AA_ARRAYEND, | |
66 | }; | |
67 | ||
68 | /* | |
69 | * aa_ext is the read of the buffer containing the serialized profile. The | |
70 | * data is copied into a kernel buffer in apparmorfs and then handed off to | |
71 | * the unpack routines. | |
72 | */ | |
73 | struct aa_ext { | |
74 | void *start; | |
75 | void *end; | |
76 | void *pos; /* pointer to current position in the buffer */ | |
77 | u32 version; | |
78 | }; | |
79 | ||
80 | /* audit callback for unpack fields */ | |
81 | static void audit_cb(struct audit_buffer *ab, void *va) | |
82 | { | |
83 | struct common_audit_data *sa = va; | |
80594fc2 JJ |
84 | |
85 | if (aad(sa)->iface.ns) { | |
86 | audit_log_format(ab, " ns="); | |
87 | audit_log_untrustedstring(ab, aad(sa)->iface.ns); | |
88 | } | |
89 | if (aad(sa)->name) { | |
736ec752 | 90 | audit_log_format(ab, " name="); |
80594fc2 | 91 | audit_log_untrustedstring(ab, aad(sa)->name); |
736ec752 | 92 | } |
80594fc2 JJ |
93 | if (aad(sa)->iface.pos) |
94 | audit_log_format(ab, " offset=%ld", aad(sa)->iface.pos); | |
736ec752 JJ |
95 | } |
96 | ||
97 | /** | |
98 | * audit_iface - do audit message for policy unpacking/load/replace/remove | |
99 | * @new: profile if it has been allocated (MAYBE NULL) | |
80594fc2 | 100 | * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL) |
736ec752 JJ |
101 | * @name: name of the profile being manipulated (MAYBE NULL) |
102 | * @info: any extra info about the failure (MAYBE NULL) | |
b1b4bc2e | 103 | * @e: buffer position info |
736ec752 JJ |
104 | * @error: error code |
105 | * | |
106 | * Returns: %0 or error | |
107 | */ | |
80594fc2 JJ |
108 | static int audit_iface(struct aa_profile *new, const char *ns_name, |
109 | const char *name, const char *info, struct aa_ext *e, | |
110 | int error) | |
736ec752 | 111 | { |
80594fc2 JJ |
112 | struct aa_profile *profile = labels_profile(aa_current_raw_label()); |
113 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, NULL); | |
b1b4bc2e | 114 | if (e) |
80594fc2 JJ |
115 | aad(&sa)->iface.pos = e->pos - e->start; |
116 | ||
117 | aad(&sa)->iface.ns = ns_name; | |
118 | if (new) | |
119 | aad(&sa)->name = new->base.hname; | |
120 | else | |
121 | aad(&sa)->name = name; | |
122 | aad(&sa)->info = info; | |
123 | aad(&sa)->error = error; | |
124 | ||
125 | return aa_audit(AUDIT_APPARMOR_STATUS, profile, &sa, audit_cb); | |
736ec752 JJ |
126 | } |
127 | ||
3b8ad847 JJ |
128 | void aa_loaddata_kref(struct kref *kref) |
129 | { | |
130 | struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count); | |
131 | if (d) { | |
132 | kzfree(d->hash); | |
133 | kvfree(d); | |
134 | } | |
135 | } | |
136 | ||
736ec752 JJ |
137 | /* test if read will be in packed data bounds */ |
138 | static bool inbounds(struct aa_ext *e, size_t size) | |
139 | { | |
140 | return (size <= e->end - e->pos); | |
141 | } | |
142 | ||
143 | /** | |
144 | * aa_u16_chunck - test and do bounds checking for a u16 size based chunk | |
145 | * @e: serialized data read head (NOT NULL) | |
146 | * @chunk: start address for chunk of data (NOT NULL) | |
147 | * | |
148 | * Returns: the size of chunk found with the read head at the end of the chunk. | |
149 | */ | |
150 | static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk) | |
151 | { | |
152 | size_t size = 0; | |
153 | ||
154 | if (!inbounds(e, sizeof(u16))) | |
155 | return 0; | |
156 | size = le16_to_cpu(get_unaligned((u16 *) e->pos)); | |
157 | e->pos += sizeof(u16); | |
158 | if (!inbounds(e, size)) | |
159 | return 0; | |
160 | *chunk = e->pos; | |
161 | e->pos += size; | |
162 | return size; | |
163 | } | |
164 | ||
165 | /* unpack control byte */ | |
166 | static bool unpack_X(struct aa_ext *e, enum aa_code code) | |
167 | { | |
168 | if (!inbounds(e, 1)) | |
169 | return 0; | |
170 | if (*(u8 *) e->pos != code) | |
171 | return 0; | |
172 | e->pos++; | |
173 | return 1; | |
174 | } | |
175 | ||
176 | /** | |
177 | * unpack_nameX - check is the next element is of type X with a name of @name | |
178 | * @e: serialized data extent information (NOT NULL) | |
179 | * @code: type code | |
180 | * @name: name to match to the serialized element. (MAYBE NULL) | |
181 | * | |
182 | * check that the next serialized data element is of type X and has a tag | |
183 | * name @name. If @name is specified then there must be a matching | |
184 | * name element in the stream. If @name is NULL any name element will be | |
185 | * skipped and only the typecode will be tested. | |
186 | * | |
187 | * Returns 1 on success (both type code and name tests match) and the read | |
188 | * head is advanced past the headers | |
189 | * | |
190 | * Returns: 0 if either match fails, the read head does not move | |
191 | */ | |
192 | static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name) | |
193 | { | |
194 | /* | |
195 | * May need to reset pos if name or type doesn't match | |
196 | */ | |
197 | void *pos = e->pos; | |
198 | /* | |
199 | * Check for presence of a tagname, and if present name size | |
200 | * AA_NAME tag value is a u16. | |
201 | */ | |
202 | if (unpack_X(e, AA_NAME)) { | |
203 | char *tag = NULL; | |
204 | size_t size = unpack_u16_chunk(e, &tag); | |
205 | /* if a name is specified it must match. otherwise skip tag */ | |
206 | if (name && (!size || strcmp(name, tag))) | |
207 | goto fail; | |
208 | } else if (name) { | |
209 | /* if a name is specified and there is no name tag fail */ | |
210 | goto fail; | |
211 | } | |
212 | ||
213 | /* now check if type code matches */ | |
214 | if (unpack_X(e, code)) | |
215 | return 1; | |
216 | ||
217 | fail: | |
218 | e->pos = pos; | |
219 | return 0; | |
220 | } | |
221 | ||
80594fc2 JJ |
222 | static bool unpack_u16(struct aa_ext *e, u16 *data, const char *name) |
223 | { | |
224 | if (unpack_nameX(e, AA_U16, name)) { | |
225 | if (!inbounds(e, sizeof(u16))) | |
226 | return 0; | |
227 | if (data) | |
228 | *data = le16_to_cpu(get_unaligned((u16 *) e->pos)); | |
229 | e->pos += sizeof(u16); | |
230 | return 1; | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | ||
736ec752 JJ |
235 | static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name) |
236 | { | |
237 | if (unpack_nameX(e, AA_U32, name)) { | |
238 | if (!inbounds(e, sizeof(u32))) | |
239 | return 0; | |
240 | if (data) | |
241 | *data = le32_to_cpu(get_unaligned((u32 *) e->pos)); | |
242 | e->pos += sizeof(u32); | |
243 | return 1; | |
244 | } | |
245 | return 0; | |
246 | } | |
247 | ||
248 | static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name) | |
249 | { | |
250 | if (unpack_nameX(e, AA_U64, name)) { | |
251 | if (!inbounds(e, sizeof(u64))) | |
252 | return 0; | |
253 | if (data) | |
254 | *data = le64_to_cpu(get_unaligned((u64 *) e->pos)); | |
255 | e->pos += sizeof(u64); | |
256 | return 1; | |
257 | } | |
258 | return 0; | |
259 | } | |
260 | ||
261 | static size_t unpack_array(struct aa_ext *e, const char *name) | |
262 | { | |
263 | if (unpack_nameX(e, AA_ARRAY, name)) { | |
264 | int size; | |
265 | if (!inbounds(e, sizeof(u16))) | |
266 | return 0; | |
267 | size = (int)le16_to_cpu(get_unaligned((u16 *) e->pos)); | |
268 | e->pos += sizeof(u16); | |
269 | return size; | |
270 | } | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name) | |
275 | { | |
276 | if (unpack_nameX(e, AA_BLOB, name)) { | |
277 | u32 size; | |
278 | if (!inbounds(e, sizeof(u32))) | |
279 | return 0; | |
280 | size = le32_to_cpu(get_unaligned((u32 *) e->pos)); | |
281 | e->pos += sizeof(u32); | |
282 | if (inbounds(e, (size_t) size)) { | |
283 | *blob = e->pos; | |
284 | e->pos += size; | |
285 | return size; | |
286 | } | |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static int unpack_str(struct aa_ext *e, const char **string, const char *name) | |
292 | { | |
293 | char *src_str; | |
294 | size_t size = 0; | |
295 | void *pos = e->pos; | |
296 | *string = NULL; | |
297 | if (unpack_nameX(e, AA_STRING, name)) { | |
298 | size = unpack_u16_chunk(e, &src_str); | |
299 | if (size) { | |
300 | /* strings are null terminated, length is size - 1 */ | |
301 | if (src_str[size - 1] != 0) | |
302 | goto fail; | |
303 | *string = src_str; | |
304 | } | |
305 | } | |
306 | return size; | |
307 | ||
308 | fail: | |
309 | e->pos = pos; | |
310 | return 0; | |
311 | } | |
312 | ||
313 | static int unpack_strdup(struct aa_ext *e, char **string, const char *name) | |
314 | { | |
315 | const char *tmp; | |
316 | void *pos = e->pos; | |
317 | int res = unpack_str(e, &tmp, name); | |
318 | *string = NULL; | |
319 | ||
320 | if (!res) | |
321 | return 0; | |
322 | ||
323 | *string = kmemdup(tmp, res, GFP_KERNEL); | |
324 | if (!*string) { | |
325 | e->pos = pos; | |
326 | return 0; | |
327 | } | |
328 | ||
329 | return res; | |
330 | } | |
331 | ||
180a6f59 JJ |
332 | #define DFA_VALID_PERM_MASK 0xffffffff |
333 | #define DFA_VALID_PERM2_MASK 0xffffffff | |
334 | ||
736ec752 JJ |
335 | /** |
336 | * verify_accept - verify the accept tables of a dfa | |
337 | * @dfa: dfa to verify accept tables of (NOT NULL) | |
338 | * @flags: flags governing dfa | |
339 | * | |
340 | * Returns: 1 if valid accept tables else 0 if error | |
341 | */ | |
342 | static bool verify_accept(struct aa_dfa *dfa, int flags) | |
343 | { | |
344 | int i; | |
345 | ||
346 | /* verify accept permissions */ | |
347 | for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) { | |
348 | int mode = ACCEPT_TABLE(dfa)[i]; | |
349 | ||
350 | if (mode & ~DFA_VALID_PERM_MASK) | |
351 | return 0; | |
352 | ||
353 | if (ACCEPT_TABLE2(dfa)[i] & ~DFA_VALID_PERM2_MASK) | |
354 | return 0; | |
355 | } | |
356 | return 1; | |
357 | } | |
358 | ||
359 | /** | |
360 | * unpack_dfa - unpack a file rule dfa | |
361 | * @e: serialized data extent information (NOT NULL) | |
362 | * | |
363 | * returns dfa or ERR_PTR or NULL if no dfa | |
364 | */ | |
365 | static struct aa_dfa *unpack_dfa(struct aa_ext *e) | |
366 | { | |
367 | char *blob = NULL; | |
368 | size_t size; | |
369 | struct aa_dfa *dfa = NULL; | |
370 | ||
371 | size = unpack_blob(e, &blob, "aadfa"); | |
372 | if (size) { | |
373 | /* | |
374 | * The dfa is aligned with in the blob to 8 bytes | |
375 | * from the beginning of the stream. | |
dd51c848 | 376 | * alignment adjust needed by dfa unpack |
736ec752 | 377 | */ |
dd51c848 JJ |
378 | size_t sz = blob - (char *) e->start - |
379 | ((e->pos - e->start) & 7); | |
736ec752 JJ |
380 | size_t pad = ALIGN(sz, 8) - sz; |
381 | int flags = TO_ACCEPT1_FLAG(YYTD_DATA32) | | |
80594fc2 | 382 | TO_ACCEPT2_FLAG(YYTD_DATA32) | DFA_FLAG_VERIFY_STATES; |
736ec752 JJ |
383 | dfa = aa_dfa_unpack(blob + pad, size - pad, flags); |
384 | ||
385 | if (IS_ERR(dfa)) | |
386 | return dfa; | |
387 | ||
388 | if (!verify_accept(dfa, flags)) | |
389 | goto fail; | |
390 | } | |
391 | ||
392 | return dfa; | |
393 | ||
394 | fail: | |
395 | aa_put_dfa(dfa); | |
396 | return ERR_PTR(-EPROTO); | |
397 | } | |
398 | ||
399 | /** | |
400 | * unpack_trans_table - unpack a profile transition table | |
401 | * @e: serialized data extent information (NOT NULL) | |
402 | * @profile: profile to add the accept table to (NOT NULL) | |
403 | * | |
25985edc | 404 | * Returns: 1 if table successfully unpacked |
736ec752 JJ |
405 | */ |
406 | static bool unpack_trans_table(struct aa_ext *e, struct aa_profile *profile) | |
407 | { | |
408 | void *pos = e->pos; | |
409 | ||
410 | /* exec table is optional */ | |
411 | if (unpack_nameX(e, AA_STRUCT, "xtable")) { | |
412 | int i, size; | |
413 | ||
414 | size = unpack_array(e, NULL); | |
415 | /* currently 4 exec bits and entries 0-3 are reserved iupcx */ | |
416 | if (size > 16 - 4) | |
417 | goto fail; | |
418 | profile->file.trans.table = kzalloc(sizeof(char *) * size, | |
419 | GFP_KERNEL); | |
420 | if (!profile->file.trans.table) | |
421 | goto fail; | |
422 | ||
423 | profile->file.trans.size = size; | |
424 | for (i = 0; i < size; i++) { | |
425 | char *str; | |
80594fc2 | 426 | int c, j, pos, size2 = unpack_strdup(e, &str, NULL); |
736ec752 JJ |
427 | /* unpack_strdup verifies that the last character is |
428 | * null termination byte. | |
429 | */ | |
7ee95850 | 430 | if (!size2) |
736ec752 JJ |
431 | goto fail; |
432 | profile->file.trans.table[i] = str; | |
433 | /* verify that name doesn't start with space */ | |
434 | if (isspace(*str)) | |
435 | goto fail; | |
436 | ||
437 | /* count internal # of internal \0 */ | |
80594fc2 JJ |
438 | for (c = j = 0; j < size2 - 1; j++) { |
439 | if (!str[j]) { | |
440 | pos = j; | |
736ec752 | 441 | c++; |
80594fc2 | 442 | } |
736ec752 JJ |
443 | } |
444 | if (*str == ':') { | |
80594fc2 JJ |
445 | /* first character after : must be valid */ |
446 | if (!str[1]) | |
447 | goto fail; | |
736ec752 JJ |
448 | /* beginning with : requires an embedded \0, |
449 | * verify that exactly 1 internal \0 exists | |
450 | * trailing \0 already verified by unpack_strdup | |
451 | */ | |
80594fc2 JJ |
452 | if (c == 1) |
453 | /* convert \0 back to : for label_parse */ | |
454 | str[pos] = ':'; | |
455 | else if (c > 1) | |
736ec752 JJ |
456 | goto fail; |
457 | } else if (c) | |
458 | /* fail - all other cases with embedded \0 */ | |
459 | goto fail; | |
460 | } | |
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 | ||
468 | fail: | |
469 | aa_free_domain_entries(&profile->file.trans); | |
470 | e->pos = pos; | |
471 | return 0; | |
472 | } | |
473 | ||
474 | static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile) | |
475 | { | |
476 | void *pos = e->pos; | |
477 | ||
478 | /* rlimits are optional */ | |
479 | if (unpack_nameX(e, AA_STRUCT, "rlimits")) { | |
480 | int i, size; | |
481 | u32 tmp = 0; | |
482 | if (!unpack_u32(e, &tmp, NULL)) | |
483 | goto fail; | |
484 | profile->rlimits.mask = tmp; | |
485 | ||
486 | size = unpack_array(e, NULL); | |
487 | if (size > RLIM_NLIMITS) | |
488 | goto fail; | |
489 | for (i = 0; i < size; i++) { | |
7ee95850 | 490 | u64 tmp2 = 0; |
736ec752 | 491 | int a = aa_map_resource(i); |
7ee95850 | 492 | if (!unpack_u64(e, &tmp2, NULL)) |
736ec752 | 493 | goto fail; |
7ee95850 | 494 | profile->rlimits.limits[a].rlim_max = tmp2; |
736ec752 JJ |
495 | } |
496 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
497 | goto fail; | |
498 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
499 | goto fail; | |
500 | } | |
501 | return 1; | |
502 | ||
503 | fail: | |
504 | e->pos = pos; | |
505 | return 0; | |
506 | } | |
507 | ||
b7370901 WH |
508 | static void *kvmemdup(const void *src, size_t len) |
509 | { | |
510 | void *p = kvmalloc(len); | |
511 | ||
512 | if (p) | |
513 | memcpy(p, src, len); | |
514 | return p; | |
515 | } | |
516 | ||
517 | static u32 strhash(const void *data, u32 len, u32 seed) | |
518 | { | |
519 | const char * const *key = data; | |
520 | ||
521 | return jhash(*key, strlen(*key), seed); | |
522 | } | |
523 | ||
524 | static int datacmp(struct rhashtable_compare_arg *arg, const void *obj) | |
525 | { | |
526 | const struct aa_data *data = obj; | |
527 | const char * const *key = arg->key; | |
528 | ||
529 | return strcmp(data->key, *key); | |
530 | } | |
531 | ||
736ec752 JJ |
532 | /** |
533 | * unpack_profile - unpack a serialized profile | |
534 | * @e: serialized data extent information (NOT NULL) | |
535 | * | |
536 | * NOTE: unpack profile sets audit struct if there is a failure | |
537 | */ | |
80594fc2 | 538 | static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) |
736ec752 JJ |
539 | { |
540 | struct aa_profile *profile = NULL; | |
80594fc2 JJ |
541 | const char *tmpname, *tmpns = NULL, *name = NULL; |
542 | const char *info = "failed to unpack profile"; | |
543 | size_t size = 0, ns_len; | |
b7370901 WH |
544 | struct rhashtable_params params = { 0 }; |
545 | char *key = NULL; | |
546 | struct aa_data *data; | |
ad5ff3db | 547 | int i, error = -EPROTO; |
736ec752 JJ |
548 | kernel_cap_t tmpcap; |
549 | u32 tmp; | |
550 | ||
80594fc2 JJ |
551 | *ns_name = NULL; |
552 | ||
736ec752 JJ |
553 | /* check that we have the right struct being passed */ |
554 | if (!unpack_nameX(e, AA_STRUCT, "profile")) | |
555 | goto fail; | |
556 | if (!unpack_str(e, &name, NULL)) | |
557 | goto fail; | |
80594fc2 JJ |
558 | if (*name == '\0') |
559 | goto fail; | |
560 | ||
561 | tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len); | |
562 | if (tmpns) { | |
563 | *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL); | |
564 | if (!*ns_name) | |
565 | goto fail; | |
566 | name = tmpname; | |
567 | } | |
736ec752 | 568 | |
80594fc2 | 569 | profile = aa_alloc_profile(name, NULL, GFP_KERNEL); |
736ec752 JJ |
570 | if (!profile) |
571 | return ERR_PTR(-ENOMEM); | |
572 | ||
573 | /* profile renaming is optional */ | |
574 | (void) unpack_str(e, &profile->rename, "rename"); | |
575 | ||
556d0be7 JJ |
576 | /* attachment string is optional */ |
577 | (void) unpack_str(e, &profile->attach, "attach"); | |
578 | ||
736ec752 JJ |
579 | /* xmatch is optional and may be NULL */ |
580 | profile->xmatch = unpack_dfa(e); | |
581 | if (IS_ERR(profile->xmatch)) { | |
582 | error = PTR_ERR(profile->xmatch); | |
583 | profile->xmatch = NULL; | |
584 | goto fail; | |
585 | } | |
586 | /* xmatch_len is not optional if xmatch is set */ | |
587 | if (profile->xmatch) { | |
588 | if (!unpack_u32(e, &tmp, NULL)) | |
589 | goto fail; | |
590 | profile->xmatch_len = tmp; | |
591 | } | |
592 | ||
80594fc2 JJ |
593 | /* disconnected attachment string is optional */ |
594 | (void) unpack_str(e, &profile->disconnected, "disconnected"); | |
595 | ||
736ec752 JJ |
596 | /* per profile debug flags (complain, audit) */ |
597 | if (!unpack_nameX(e, AA_STRUCT, "flags")) | |
598 | goto fail; | |
599 | if (!unpack_u32(e, &tmp, NULL)) | |
600 | goto fail; | |
03816507 | 601 | if (tmp & PACKED_FLAG_HAT) |
80594fc2 | 602 | profile->label.flags |= FLAG_HAT; |
736ec752 JJ |
603 | if (!unpack_u32(e, &tmp, NULL)) |
604 | goto fail; | |
80594fc2 | 605 | if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG)) |
736ec752 | 606 | profile->mode = APPARMOR_COMPLAIN; |
03816507 JJ |
607 | else if (tmp == PACKED_MODE_KILL) |
608 | profile->mode = APPARMOR_KILL; | |
609 | else if (tmp == PACKED_MODE_UNCONFINED) | |
610 | profile->mode = APPARMOR_UNCONFINED; | |
736ec752 JJ |
611 | if (!unpack_u32(e, &tmp, NULL)) |
612 | goto fail; | |
613 | if (tmp) | |
614 | profile->audit = AUDIT_ALL; | |
615 | ||
616 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
617 | goto fail; | |
618 | ||
619 | /* path_flags is optional */ | |
80594fc2 | 620 | if (!unpack_u32(e, &profile->path_flags, "path_flags")) |
736ec752 | 621 | /* set a default value if path_flags field is not present */ |
80594fc2 | 622 | profile->path_flags = PATH_MEDIATE_DELETED; |
736ec752 JJ |
623 | |
624 | if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL)) | |
625 | goto fail; | |
626 | if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL)) | |
627 | goto fail; | |
628 | if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL)) | |
629 | goto fail; | |
630 | if (!unpack_u32(e, &tmpcap.cap[0], NULL)) | |
631 | goto fail; | |
632 | ||
633 | if (unpack_nameX(e, AA_STRUCT, "caps64")) { | |
634 | /* optional upper half of 64 bit caps */ | |
635 | if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL)) | |
636 | goto fail; | |
637 | if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL)) | |
638 | goto fail; | |
639 | if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL)) | |
640 | goto fail; | |
641 | if (!unpack_u32(e, &(tmpcap.cap[1]), NULL)) | |
642 | goto fail; | |
643 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
644 | goto fail; | |
645 | } | |
646 | ||
647 | if (unpack_nameX(e, AA_STRUCT, "capsx")) { | |
648 | /* optional extended caps mediation mask */ | |
649 | if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL)) | |
650 | goto fail; | |
651 | if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL)) | |
652 | goto fail; | |
cdbd2884 JJ |
653 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
654 | goto fail; | |
736ec752 JJ |
655 | } |
656 | ||
657 | if (!unpack_rlimits(e, profile)) | |
658 | goto fail; | |
659 | ||
80594fc2 JJ |
660 | size = unpack_array(e, "net_allowed_af"); |
661 | if (size) { | |
662 | ||
663 | for (i = 0; i < size; i++) { | |
664 | /* discard extraneous rules that this kernel will | |
665 | * never request | |
666 | */ | |
667 | if (i >= AF_MAX) { | |
668 | u16 tmp; | |
669 | if (!unpack_u16(e, &tmp, NULL) || | |
670 | !unpack_u16(e, &tmp, NULL) || | |
671 | !unpack_u16(e, &tmp, NULL)) | |
672 | goto fail; | |
673 | continue; | |
674 | } | |
675 | if (!unpack_u16(e, &profile->net.allow[i], NULL)) | |
676 | goto fail; | |
677 | if (!unpack_u16(e, &profile->net.audit[i], NULL)) | |
678 | goto fail; | |
679 | if (!unpack_u16(e, &profile->net.quiet[i], NULL)) | |
680 | goto fail; | |
681 | } | |
682 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
683 | goto fail; | |
684 | } | |
685 | if (VERSION_CMP(<, e->version, v7)) { | |
686 | /* old policy always allowed these too */ | |
687 | profile->net.allow[AF_UNIX] = 0xffff; | |
688 | profile->net.allow[AF_NETLINK] = 0xffff; | |
689 | } | |
690 | ||
ad5ff3db JJ |
691 | if (unpack_nameX(e, AA_STRUCT, "policydb")) { |
692 | /* generic policy dfa - optional and may be NULL */ | |
693 | profile->policy.dfa = unpack_dfa(e); | |
694 | if (IS_ERR(profile->policy.dfa)) { | |
695 | error = PTR_ERR(profile->policy.dfa); | |
696 | profile->policy.dfa = NULL; | |
697 | goto fail; | |
5f20fdfe JJ |
698 | } else if (!profile->policy.dfa) { |
699 | error = -EPROTO; | |
700 | goto fail; | |
ad5ff3db JJ |
701 | } |
702 | if (!unpack_u32(e, &profile->policy.start[0], "start")) | |
703 | /* default start state */ | |
704 | profile->policy.start[0] = DFA_START; | |
705 | /* setup class index */ | |
706 | for (i = AA_CLASS_FILE; i <= AA_CLASS_LAST; i++) { | |
707 | profile->policy.start[i] = | |
708 | aa_dfa_next(profile->policy.dfa, | |
709 | profile->policy.start[0], | |
710 | i); | |
711 | } | |
712 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
713 | goto fail; | |
80594fc2 JJ |
714 | } else |
715 | profile->policy.dfa = aa_get_dfa(nulldfa); | |
ad5ff3db | 716 | |
736ec752 JJ |
717 | /* get file rules */ |
718 | profile->file.dfa = unpack_dfa(e); | |
719 | if (IS_ERR(profile->file.dfa)) { | |
720 | error = PTR_ERR(profile->file.dfa); | |
721 | profile->file.dfa = NULL; | |
722 | goto fail; | |
80594fc2 JJ |
723 | } else if (profile->file.dfa) { |
724 | if (!unpack_u32(e, &profile->file.start, "dfa_start")) | |
725 | /* default start state */ | |
726 | profile->file.start = DFA_START; | |
727 | } else if (profile->policy.dfa && | |
728 | profile->policy.start[AA_CLASS_FILE]) { | |
729 | profile->file.dfa = aa_get_dfa(profile->policy.dfa); | |
730 | profile->file.start = profile->policy.start[AA_CLASS_FILE]; | |
731 | } else | |
732 | profile->file.dfa = aa_get_dfa(nulldfa); | |
736ec752 JJ |
733 | |
734 | if (!unpack_trans_table(e, profile)) | |
735 | goto fail; | |
736 | ||
b7370901 WH |
737 | if (unpack_nameX(e, AA_STRUCT, "data")) { |
738 | profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL); | |
739 | if (!profile->data) | |
740 | goto fail; | |
741 | ||
742 | params.nelem_hint = 3; | |
743 | params.key_len = sizeof(void *); | |
744 | params.key_offset = offsetof(struct aa_data, key); | |
745 | params.head_offset = offsetof(struct aa_data, head); | |
746 | params.hashfn = strhash; | |
747 | params.obj_cmpfn = datacmp; | |
748 | ||
749 | if (rhashtable_init(profile->data, ¶ms)) | |
750 | goto fail; | |
751 | ||
752 | while (unpack_strdup(e, &key, NULL)) { | |
753 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
754 | if (!data) { | |
755 | kzfree(key); | |
756 | goto fail; | |
757 | } | |
758 | ||
759 | data->key = key; | |
760 | data->size = unpack_blob(e, &data->data, NULL); | |
761 | data->data = kvmemdup(data->data, data->size); | |
762 | if (data->size && !data->data) { | |
763 | kzfree(data->key); | |
764 | kzfree(data); | |
765 | goto fail; | |
766 | } | |
767 | ||
768 | rhashtable_insert_fast(profile->data, &data->head, | |
769 | profile->data->p); | |
770 | } | |
771 | ||
772 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
773 | goto fail; | |
774 | } | |
775 | ||
736ec752 JJ |
776 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
777 | goto fail; | |
778 | ||
779 | return profile; | |
780 | ||
781 | fail: | |
782 | if (profile) | |
783 | name = NULL; | |
784 | else if (!name) | |
785 | name = "unknown"; | |
80594fc2 | 786 | audit_iface(profile, NULL, name, info, e, error); |
8651e1d6 | 787 | aa_free_profile(profile); |
736ec752 JJ |
788 | |
789 | return ERR_PTR(error); | |
790 | } | |
791 | ||
792 | /** | |
793 | * verify_head - unpack serialized stream header | |
794 | * @e: serialized data read head (NOT NULL) | |
dd51c848 | 795 | * @required: whether the header is required or optional |
736ec752 JJ |
796 | * @ns: Returns - namespace if one is specified else NULL (NOT NULL) |
797 | * | |
798 | * Returns: error or 0 if header is good | |
799 | */ | |
dd51c848 | 800 | static int verify_header(struct aa_ext *e, int required, const char **ns) |
736ec752 JJ |
801 | { |
802 | int error = -EPROTONOSUPPORT; | |
dd51c848 JJ |
803 | const char *name = NULL; |
804 | *ns = NULL; | |
805 | ||
736ec752 JJ |
806 | /* get the interface version */ |
807 | if (!unpack_u32(e, &e->version, "version")) { | |
dd51c848 | 808 | if (required) { |
80594fc2 | 809 | audit_iface(NULL, NULL, NULL, "invalid profile format", |
dd51c848 JJ |
810 | e, error); |
811 | return error; | |
812 | } | |
736ec752 JJ |
813 | } |
814 | ||
80594fc2 JJ |
815 | /* Check that the interface version is currently supported. |
816 | * if not specified use previous version | |
817 | * Mask off everything that is not kernel abi version | |
818 | */ | |
819 | if (VERSION_CMP(<, e->version, v5) && VERSION_CMP(>, e->version, v7)) { | |
820 | audit_iface(NULL, NULL, NULL, "unsupported interface version", | |
821 | e, error); | |
822 | return error; | |
823 | } | |
dd51c848 | 824 | |
736ec752 | 825 | /* read the namespace if present */ |
dd51c848 | 826 | if (unpack_str(e, &name, "namespace")) { |
80594fc2 JJ |
827 | if (*name == '\0') { |
828 | audit_iface(NULL, NULL, NULL, "invalid namespace name", | |
829 | e, error); | |
830 | return error; | |
831 | } | |
dd51c848 | 832 | if (*ns && strcmp(*ns, name)) |
80594fc2 JJ |
833 | audit_iface(NULL, NULL, NULL, "invalid ns change", e, |
834 | error); | |
dd51c848 JJ |
835 | else if (!*ns) |
836 | *ns = name; | |
837 | } | |
736ec752 JJ |
838 | |
839 | return 0; | |
840 | } | |
841 | ||
842 | static bool verify_xindex(int xindex, int table_size) | |
843 | { | |
844 | int index, xtype; | |
845 | xtype = xindex & AA_X_TYPE_MASK; | |
846 | index = xindex & AA_X_INDEX_MASK; | |
23ca7b64 | 847 | if (xtype == AA_X_TABLE && index >= table_size) |
736ec752 JJ |
848 | return 0; |
849 | return 1; | |
850 | } | |
851 | ||
852 | /* verify dfa xindexes are in range of transition tables */ | |
853 | static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size) | |
854 | { | |
855 | int i; | |
856 | for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) { | |
857 | if (!verify_xindex(dfa_user_xindex(dfa, i), table_size)) | |
858 | return 0; | |
859 | if (!verify_xindex(dfa_other_xindex(dfa, i), table_size)) | |
860 | return 0; | |
861 | } | |
862 | return 1; | |
863 | } | |
864 | ||
865 | /** | |
866 | * verify_profile - Do post unpack analysis to verify profile consistency | |
867 | * @profile: profile to verify (NOT NULL) | |
868 | * | |
869 | * Returns: 0 if passes verification else error | |
870 | */ | |
871 | static int verify_profile(struct aa_profile *profile) | |
872 | { | |
80594fc2 JJ |
873 | if (profile->file.dfa && |
874 | !verify_dfa_xindex(profile->file.dfa, | |
875 | profile->file.trans.size)) { | |
876 | audit_iface(profile, NULL, NULL, | |
877 | "Invalid named transition", NULL, -EPROTO); | |
878 | return -EPROTO; | |
736ec752 JJ |
879 | } |
880 | ||
881 | return 0; | |
882 | } | |
883 | ||
dd51c848 JJ |
884 | void aa_load_ent_free(struct aa_load_ent *ent) |
885 | { | |
886 | if (ent) { | |
887 | aa_put_profile(ent->rename); | |
888 | aa_put_profile(ent->old); | |
889 | aa_put_profile(ent->new); | |
80594fc2 | 890 | kfree(ent->ns_name); |
dd51c848 JJ |
891 | kzfree(ent); |
892 | } | |
893 | } | |
894 | ||
895 | struct aa_load_ent *aa_load_ent_alloc(void) | |
896 | { | |
897 | struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL); | |
898 | if (ent) | |
899 | INIT_LIST_HEAD(&ent->list); | |
900 | return ent; | |
901 | } | |
902 | ||
736ec752 | 903 | /** |
dd51c848 | 904 | * aa_unpack - unpack packed binary profile(s) data loaded from user space |
736ec752 | 905 | * @udata: user data copied to kmem (NOT NULL) |
dd51c848 | 906 | * @lh: list to place unpacked profiles in a aa_repl_ws |
736ec752 JJ |
907 | * @ns: Returns namespace profile is in if specified else NULL (NOT NULL) |
908 | * | |
dd51c848 JJ |
909 | * Unpack user data and return refcounted allocated profile(s) stored in |
910 | * @lh in order of discovery, with the list chain stored in base.list | |
911 | * or error | |
736ec752 | 912 | * |
dd51c848 | 913 | * Returns: profile(s) on @lh else error pointer if fails to unpack |
736ec752 | 914 | */ |
3b8ad847 | 915 | int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, const char **ns) |
736ec752 | 916 | { |
dd51c848 | 917 | struct aa_load_ent *tmp, *ent; |
736ec752 JJ |
918 | struct aa_profile *profile = NULL; |
919 | int error; | |
920 | struct aa_ext e = { | |
3b8ad847 JJ |
921 | .start = udata->data, |
922 | .end = udata->data + udata->size, | |
923 | .pos = udata->data, | |
736ec752 JJ |
924 | }; |
925 | ||
dd51c848 JJ |
926 | *ns = NULL; |
927 | while (e.pos < e.end) { | |
80594fc2 | 928 | char *ns_name = NULL; |
f8eb8a13 | 929 | void *start; |
dd51c848 JJ |
930 | error = verify_header(&e, e.pos == e.start, ns); |
931 | if (error) | |
932 | goto fail; | |
f8eb8a13 | 933 | start = e.pos; |
80594fc2 | 934 | profile = unpack_profile(&e, &ns_name); |
dd51c848 JJ |
935 | if (IS_ERR(profile)) { |
936 | error = PTR_ERR(profile); | |
937 | goto fail; | |
938 | } | |
939 | ||
940 | error = verify_profile(profile); | |
f8eb8a13 JJ |
941 | if (error) |
942 | goto fail_profile; | |
943 | ||
80594fc2 JJ |
944 | if (aa_g_hash_policy) |
945 | error = aa_calc_profile_hash(profile, e.version, start, | |
6059f71f | 946 | e.pos - start); |
f8eb8a13 JJ |
947 | if (error) |
948 | goto fail_profile; | |
dd51c848 JJ |
949 | |
950 | ent = aa_load_ent_alloc(); | |
951 | if (!ent) { | |
952 | error = -ENOMEM; | |
f8eb8a13 | 953 | goto fail_profile; |
dd51c848 | 954 | } |
736ec752 | 955 | |
dd51c848 | 956 | ent->new = profile; |
80594fc2 | 957 | ent->ns_name = ns_name; |
dd51c848 | 958 | list_add_tail(&ent->list, lh); |
736ec752 | 959 | } |
3b8ad847 JJ |
960 | udata->abi = e.version & K_ABI_MASK; |
961 | if (aa_g_hash_policy) { | |
962 | udata->hash = aa_calc_hash(udata->data, udata->size); | |
963 | if (IS_ERR(udata->hash)) { | |
964 | error = PTR_ERR(udata->hash); | |
965 | udata->hash = NULL; | |
966 | goto fail; | |
967 | } | |
968 | } | |
dd51c848 JJ |
969 | return 0; |
970 | ||
f8eb8a13 JJ |
971 | fail_profile: |
972 | aa_put_profile(profile); | |
973 | ||
dd51c848 JJ |
974 | fail: |
975 | list_for_each_entry_safe(ent, tmp, lh, list) { | |
976 | list_del_init(&ent->list); | |
977 | aa_load_ent_free(ent); | |
978 | } | |
979 | ||
980 | return error; | |
736ec752 | 981 | } |