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