Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
736ec752 JJ |
2 | /* |
3 | * AppArmor security module | |
4 | * | |
5 | * This file contains AppArmor functions for unpacking policy loaded from | |
6 | * userspace. | |
7 | * | |
8 | * Copyright (C) 1998-2008 Novell/SUSE | |
9 | * Copyright 2009-2010 Canonical Ltd. | |
10 | * | |
d410fa4e | 11 | * AppArmor uses a serialized binary format for loading policy. To find |
26fccd9e | 12 | * policy format documentation see Documentation/admin-guide/LSM/apparmor.rst |
736ec752 JJ |
13 | * All policy is validated before it is used. |
14 | */ | |
15 | ||
16 | #include <asm/unaligned.h> | |
17 | #include <linux/ctype.h> | |
18 | #include <linux/errno.h> | |
f4d6b94b | 19 | #include <linux/zstd.h> |
736ec752 JJ |
20 | |
21 | #include "include/apparmor.h" | |
22 | #include "include/audit.h" | |
d8889d49 | 23 | #include "include/cred.h" |
f8eb8a13 | 24 | #include "include/crypto.h" |
408d53e9 | 25 | #include "include/file.h" |
736ec752 | 26 | #include "include/match.h" |
637f688d | 27 | #include "include/path.h" |
736ec752 JJ |
28 | #include "include/policy.h" |
29 | #include "include/policy_unpack.h" | |
caa9f579 | 30 | #include "include/policy_compat.h" |
736ec752 | 31 | |
5ebfb128 | 32 | |
736ec752 JJ |
33 | /* |
34 | * The AppArmor interface treats data as a type byte followed by the | |
4af7c863 | 35 | * actual data. The interface has the notion of a named entry |
736ec752 JJ |
36 | * which has a name (AA_NAME typecode followed by name string) followed by |
37 | * the entries typecode and data. Named types allow for optional | |
38 | * elements and extensions to be added and tested for without breaking | |
39 | * backwards compatibility. | |
40 | */ | |
41 | ||
42 | enum aa_code { | |
43 | AA_U8, | |
44 | AA_U16, | |
45 | AA_U32, | |
46 | AA_U64, | |
47 | AA_NAME, /* same as string except it is items name */ | |
48 | AA_STRING, | |
49 | AA_BLOB, | |
50 | AA_STRUCT, | |
51 | AA_STRUCTEND, | |
52 | AA_LIST, | |
53 | AA_LISTEND, | |
54 | AA_ARRAY, | |
55 | AA_ARRAYEND, | |
56 | }; | |
57 | ||
58 | /* | |
59 | * aa_ext is the read of the buffer containing the serialized profile. The | |
60 | * data is copied into a kernel buffer in apparmorfs and then handed off to | |
61 | * the unpack routines. | |
62 | */ | |
63 | struct aa_ext { | |
64 | void *start; | |
65 | void *end; | |
66 | void *pos; /* pointer to current position in the buffer */ | |
67 | u32 version; | |
68 | }; | |
69 | ||
371e50a0 JJ |
70 | #define tri int |
71 | #define TRI_TRUE 1 | |
72 | #define TRI_NONE 0 | |
73 | #define TRI_FALSE -1 | |
74 | ||
736ec752 JJ |
75 | /* audit callback for unpack fields */ |
76 | static void audit_cb(struct audit_buffer *ab, void *va) | |
77 | { | |
78 | struct common_audit_data *sa = va; | |
ef88a7ac JJ |
79 | |
80 | if (aad(sa)->iface.ns) { | |
81 | audit_log_format(ab, " ns="); | |
82 | audit_log_untrustedstring(ab, aad(sa)->iface.ns); | |
83 | } | |
2410aa96 | 84 | if (aad(sa)->name) { |
736ec752 | 85 | audit_log_format(ab, " name="); |
2410aa96 | 86 | audit_log_untrustedstring(ab, aad(sa)->name); |
736ec752 | 87 | } |
ef88a7ac JJ |
88 | if (aad(sa)->iface.pos) |
89 | audit_log_format(ab, " offset=%ld", aad(sa)->iface.pos); | |
736ec752 JJ |
90 | } |
91 | ||
92 | /** | |
93 | * audit_iface - do audit message for policy unpacking/load/replace/remove | |
94 | * @new: profile if it has been allocated (MAYBE NULL) | |
04dc715e | 95 | * @ns_name: name of the ns the profile is to be loaded to (MAY BE NULL) |
736ec752 JJ |
96 | * @name: name of the profile being manipulated (MAYBE NULL) |
97 | * @info: any extra info about the failure (MAYBE NULL) | |
b1b4bc2e | 98 | * @e: buffer position info |
736ec752 JJ |
99 | * @error: error code |
100 | * | |
101 | * Returns: %0 or error | |
102 | */ | |
04dc715e JJ |
103 | static int audit_iface(struct aa_profile *new, const char *ns_name, |
104 | const char *name, const char *info, struct aa_ext *e, | |
105 | int error) | |
736ec752 | 106 | { |
637f688d | 107 | struct aa_profile *profile = labels_profile(aa_current_raw_label()); |
8c4b785a | 108 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, NULL); |
b1b4bc2e | 109 | if (e) |
ef88a7ac JJ |
110 | aad(&sa)->iface.pos = e->pos - e->start; |
111 | aad(&sa)->iface.ns = ns_name; | |
112 | if (new) | |
2410aa96 | 113 | aad(&sa)->name = new->base.hname; |
ef88a7ac | 114 | else |
2410aa96 | 115 | aad(&sa)->name = name; |
ef88a7ac JJ |
116 | aad(&sa)->info = info; |
117 | aad(&sa)->error = error; | |
736ec752 | 118 | |
ef88a7ac | 119 | return aa_audit(AUDIT_APPARMOR_STATUS, profile, &sa, audit_cb); |
736ec752 JJ |
120 | } |
121 | ||
5d5182ca JJ |
122 | void __aa_loaddata_update(struct aa_loaddata *data, long revision) |
123 | { | |
124 | AA_BUG(!data); | |
125 | AA_BUG(!data->ns); | |
5d5182ca JJ |
126 | AA_BUG(!mutex_is_locked(&data->ns->lock)); |
127 | AA_BUG(data->revision > revision); | |
128 | ||
129 | data->revision = revision; | |
d61c57fd JJ |
130 | if ((data->dents[AAFS_LOADDATA_REVISION])) { |
131 | d_inode(data->dents[AAFS_LOADDATA_DIR])->i_mtime = | |
132 | current_time(d_inode(data->dents[AAFS_LOADDATA_DIR])); | |
133 | d_inode(data->dents[AAFS_LOADDATA_REVISION])->i_mtime = | |
134 | current_time(d_inode(data->dents[AAFS_LOADDATA_REVISION])); | |
135 | } | |
5d5182ca JJ |
136 | } |
137 | ||
138 | bool aa_rawdata_eq(struct aa_loaddata *l, struct aa_loaddata *r) | |
139 | { | |
140 | if (l->size != r->size) | |
141 | return false; | |
63c16c3a CC |
142 | if (l->compressed_size != r->compressed_size) |
143 | return false; | |
5d5182ca JJ |
144 | if (aa_g_hash_policy && memcmp(l->hash, r->hash, aa_hash_size()) != 0) |
145 | return false; | |
63c16c3a | 146 | return memcmp(l->data, r->data, r->compressed_size ?: r->size) == 0; |
5d5182ca JJ |
147 | } |
148 | ||
149 | /* | |
150 | * need to take the ns mutex lock which is NOT safe most places that | |
151 | * put_loaddata is called, so we have to delay freeing it | |
152 | */ | |
153 | static void do_loaddata_free(struct work_struct *work) | |
154 | { | |
155 | struct aa_loaddata *d = container_of(work, struct aa_loaddata, work); | |
156 | struct aa_ns *ns = aa_get_ns(d->ns); | |
157 | ||
158 | if (ns) { | |
feb3c766 | 159 | mutex_lock_nested(&ns->lock, ns->level); |
5d5182ca JJ |
160 | __aa_fs_remove_rawdata(d); |
161 | mutex_unlock(&ns->lock); | |
162 | aa_put_ns(ns); | |
163 | } | |
164 | ||
453431a5 WL |
165 | kfree_sensitive(d->hash); |
166 | kfree_sensitive(d->name); | |
a6a52579 | 167 | kvfree(d->data); |
453431a5 | 168 | kfree_sensitive(d); |
5d5182ca JJ |
169 | } |
170 | ||
5ac8c355 JJ |
171 | void aa_loaddata_kref(struct kref *kref) |
172 | { | |
173 | struct aa_loaddata *d = container_of(kref, struct aa_loaddata, count); | |
174 | ||
175 | if (d) { | |
5d5182ca JJ |
176 | INIT_WORK(&d->work, do_loaddata_free); |
177 | schedule_work(&d->work); | |
5ac8c355 JJ |
178 | } |
179 | } | |
180 | ||
5d5182ca JJ |
181 | struct aa_loaddata *aa_loaddata_alloc(size_t size) |
182 | { | |
a6a52579 | 183 | struct aa_loaddata *d; |
5d5182ca | 184 | |
a6a52579 | 185 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
5d5182ca JJ |
186 | if (d == NULL) |
187 | return ERR_PTR(-ENOMEM); | |
a6a52579 JJ |
188 | d->data = kvzalloc(size, GFP_KERNEL); |
189 | if (!d->data) { | |
190 | kfree(d); | |
191 | return ERR_PTR(-ENOMEM); | |
192 | } | |
5d5182ca JJ |
193 | kref_init(&d->count); |
194 | INIT_LIST_HEAD(&d->list); | |
195 | ||
196 | return d; | |
197 | } | |
198 | ||
736ec752 JJ |
199 | /* test if read will be in packed data bounds */ |
200 | static bool inbounds(struct aa_ext *e, size_t size) | |
201 | { | |
202 | return (size <= e->end - e->pos); | |
203 | } | |
204 | ||
8e51f908 MG |
205 | static void *kvmemdup(const void *src, size_t len) |
206 | { | |
207 | void *p = kvmalloc(len, GFP_KERNEL); | |
208 | ||
209 | if (p) | |
210 | memcpy(p, src, len); | |
211 | return p; | |
212 | } | |
213 | ||
736ec752 | 214 | /** |
5ee5d374 | 215 | * unpack_u16_chunk - test and do bounds checking for a u16 size based chunk |
736ec752 JJ |
216 | * @e: serialized data read head (NOT NULL) |
217 | * @chunk: start address for chunk of data (NOT NULL) | |
218 | * | |
219 | * Returns: the size of chunk found with the read head at the end of the chunk. | |
220 | */ | |
221 | static size_t unpack_u16_chunk(struct aa_ext *e, char **chunk) | |
222 | { | |
223 | size_t size = 0; | |
156e4299 | 224 | void *pos = e->pos; |
736ec752 JJ |
225 | |
226 | if (!inbounds(e, sizeof(u16))) | |
156e4299 | 227 | goto fail; |
2c17cd36 JJ |
228 | size = le16_to_cpu(get_unaligned((__le16 *) e->pos)); |
229 | e->pos += sizeof(__le16); | |
736ec752 | 230 | if (!inbounds(e, size)) |
156e4299 | 231 | goto fail; |
736ec752 JJ |
232 | *chunk = e->pos; |
233 | e->pos += size; | |
234 | return size; | |
156e4299 MS |
235 | |
236 | fail: | |
237 | e->pos = pos; | |
238 | return 0; | |
736ec752 JJ |
239 | } |
240 | ||
241 | /* unpack control byte */ | |
242 | static bool unpack_X(struct aa_ext *e, enum aa_code code) | |
243 | { | |
244 | if (!inbounds(e, 1)) | |
e3798609 | 245 | return false; |
736ec752 | 246 | if (*(u8 *) e->pos != code) |
e3798609 | 247 | return false; |
736ec752 | 248 | e->pos++; |
e3798609 | 249 | return true; |
736ec752 JJ |
250 | } |
251 | ||
252 | /** | |
253 | * unpack_nameX - check is the next element is of type X with a name of @name | |
254 | * @e: serialized data extent information (NOT NULL) | |
255 | * @code: type code | |
256 | * @name: name to match to the serialized element. (MAYBE NULL) | |
257 | * | |
258 | * check that the next serialized data element is of type X and has a tag | |
259 | * name @name. If @name is specified then there must be a matching | |
260 | * name element in the stream. If @name is NULL any name element will be | |
261 | * skipped and only the typecode will be tested. | |
262 | * | |
e3798609 | 263 | * Returns true on success (both type code and name tests match) and the read |
736ec752 JJ |
264 | * head is advanced past the headers |
265 | * | |
e3798609 | 266 | * Returns: false if either match fails, the read head does not move |
736ec752 JJ |
267 | */ |
268 | static bool unpack_nameX(struct aa_ext *e, enum aa_code code, const char *name) | |
269 | { | |
270 | /* | |
271 | * May need to reset pos if name or type doesn't match | |
272 | */ | |
273 | void *pos = e->pos; | |
274 | /* | |
275 | * Check for presence of a tagname, and if present name size | |
276 | * AA_NAME tag value is a u16. | |
277 | */ | |
278 | if (unpack_X(e, AA_NAME)) { | |
279 | char *tag = NULL; | |
280 | size_t size = unpack_u16_chunk(e, &tag); | |
281 | /* if a name is specified it must match. otherwise skip tag */ | |
8404d7a6 | 282 | if (name && (!size || tag[size-1] != '\0' || strcmp(name, tag))) |
736ec752 JJ |
283 | goto fail; |
284 | } else if (name) { | |
285 | /* if a name is specified and there is no name tag fail */ | |
286 | goto fail; | |
287 | } | |
288 | ||
289 | /* now check if type code matches */ | |
290 | if (unpack_X(e, code)) | |
e3798609 | 291 | return true; |
736ec752 JJ |
292 | |
293 | fail: | |
294 | e->pos = pos; | |
e3798609 | 295 | return false; |
736ec752 JJ |
296 | } |
297 | ||
9caafbe2 MG |
298 | static bool unpack_u8(struct aa_ext *e, u8 *data, const char *name) |
299 | { | |
156e4299 MS |
300 | void *pos = e->pos; |
301 | ||
9caafbe2 MG |
302 | if (unpack_nameX(e, AA_U8, name)) { |
303 | if (!inbounds(e, sizeof(u8))) | |
156e4299 | 304 | goto fail; |
9caafbe2 | 305 | if (data) |
dd979d7a | 306 | *data = *((u8 *)e->pos); |
9caafbe2 | 307 | e->pos += sizeof(u8); |
e3798609 | 308 | return true; |
9caafbe2 | 309 | } |
156e4299 MS |
310 | |
311 | fail: | |
312 | e->pos = pos; | |
e3798609 | 313 | return false; |
9caafbe2 MG |
314 | } |
315 | ||
736ec752 JJ |
316 | static bool unpack_u32(struct aa_ext *e, u32 *data, const char *name) |
317 | { | |
156e4299 MS |
318 | void *pos = e->pos; |
319 | ||
736ec752 JJ |
320 | if (unpack_nameX(e, AA_U32, name)) { |
321 | if (!inbounds(e, sizeof(u32))) | |
156e4299 | 322 | goto fail; |
736ec752 | 323 | if (data) |
2c17cd36 | 324 | *data = le32_to_cpu(get_unaligned((__le32 *) e->pos)); |
736ec752 | 325 | e->pos += sizeof(u32); |
e3798609 | 326 | return true; |
736ec752 | 327 | } |
156e4299 MS |
328 | |
329 | fail: | |
330 | e->pos = pos; | |
e3798609 | 331 | return false; |
736ec752 JJ |
332 | } |
333 | ||
334 | static bool unpack_u64(struct aa_ext *e, u64 *data, const char *name) | |
335 | { | |
156e4299 MS |
336 | void *pos = e->pos; |
337 | ||
736ec752 JJ |
338 | if (unpack_nameX(e, AA_U64, name)) { |
339 | if (!inbounds(e, sizeof(u64))) | |
156e4299 | 340 | goto fail; |
736ec752 | 341 | if (data) |
2c17cd36 | 342 | *data = le64_to_cpu(get_unaligned((__le64 *) e->pos)); |
736ec752 | 343 | e->pos += sizeof(u64); |
e3798609 | 344 | return true; |
736ec752 | 345 | } |
156e4299 MS |
346 | |
347 | fail: | |
348 | e->pos = pos; | |
e3798609 | 349 | return false; |
736ec752 JJ |
350 | } |
351 | ||
371e50a0 | 352 | static tri unpack_array(struct aa_ext *e, const char *name, u16 *size) |
736ec752 | 353 | { |
156e4299 MS |
354 | void *pos = e->pos; |
355 | ||
736ec752 | 356 | if (unpack_nameX(e, AA_ARRAY, name)) { |
736ec752 | 357 | if (!inbounds(e, sizeof(u16))) |
156e4299 | 358 | goto fail; |
371e50a0 | 359 | *size = le16_to_cpu(get_unaligned((__le16 *) e->pos)); |
736ec752 | 360 | e->pos += sizeof(u16); |
371e50a0 | 361 | return TRI_TRUE; |
736ec752 | 362 | } |
156e4299 | 363 | |
371e50a0 | 364 | return TRI_NONE; |
156e4299 MS |
365 | fail: |
366 | e->pos = pos; | |
371e50a0 | 367 | return TRI_FALSE; |
736ec752 JJ |
368 | } |
369 | ||
370 | static size_t unpack_blob(struct aa_ext *e, char **blob, const char *name) | |
371 | { | |
156e4299 MS |
372 | void *pos = e->pos; |
373 | ||
736ec752 JJ |
374 | if (unpack_nameX(e, AA_BLOB, name)) { |
375 | u32 size; | |
376 | if (!inbounds(e, sizeof(u32))) | |
156e4299 | 377 | goto fail; |
2c17cd36 | 378 | size = le32_to_cpu(get_unaligned((__le32 *) e->pos)); |
736ec752 JJ |
379 | e->pos += sizeof(u32); |
380 | if (inbounds(e, (size_t) size)) { | |
381 | *blob = e->pos; | |
382 | e->pos += size; | |
383 | return size; | |
384 | } | |
385 | } | |
156e4299 MS |
386 | |
387 | fail: | |
388 | e->pos = pos; | |
736ec752 JJ |
389 | return 0; |
390 | } | |
391 | ||
392 | static int unpack_str(struct aa_ext *e, const char **string, const char *name) | |
393 | { | |
394 | char *src_str; | |
395 | size_t size = 0; | |
396 | void *pos = e->pos; | |
397 | *string = NULL; | |
398 | if (unpack_nameX(e, AA_STRING, name)) { | |
399 | size = unpack_u16_chunk(e, &src_str); | |
400 | if (size) { | |
401 | /* strings are null terminated, length is size - 1 */ | |
402 | if (src_str[size - 1] != 0) | |
403 | goto fail; | |
404 | *string = src_str; | |
156e4299 MS |
405 | |
406 | return size; | |
736ec752 JJ |
407 | } |
408 | } | |
736ec752 JJ |
409 | |
410 | fail: | |
411 | e->pos = pos; | |
412 | return 0; | |
413 | } | |
414 | ||
415 | static int unpack_strdup(struct aa_ext *e, char **string, const char *name) | |
416 | { | |
417 | const char *tmp; | |
418 | void *pos = e->pos; | |
419 | int res = unpack_str(e, &tmp, name); | |
420 | *string = NULL; | |
421 | ||
422 | if (!res) | |
423 | return 0; | |
424 | ||
425 | *string = kmemdup(tmp, res, GFP_KERNEL); | |
426 | if (!*string) { | |
427 | e->pos = pos; | |
428 | return 0; | |
429 | } | |
430 | ||
431 | return res; | |
432 | } | |
433 | ||
736ec752 JJ |
434 | |
435 | /** | |
436 | * unpack_dfa - unpack a file rule dfa | |
437 | * @e: serialized data extent information (NOT NULL) | |
fd1b2b95 | 438 | * @flags: dfa flags to check |
736ec752 JJ |
439 | * |
440 | * returns dfa or ERR_PTR or NULL if no dfa | |
441 | */ | |
fd1b2b95 | 442 | static struct aa_dfa *unpack_dfa(struct aa_ext *e, int flags) |
736ec752 JJ |
443 | { |
444 | char *blob = NULL; | |
445 | size_t size; | |
446 | struct aa_dfa *dfa = NULL; | |
447 | ||
448 | size = unpack_blob(e, &blob, "aadfa"); | |
449 | if (size) { | |
450 | /* | |
451 | * The dfa is aligned with in the blob to 8 bytes | |
452 | * from the beginning of the stream. | |
dd51c848 | 453 | * alignment adjust needed by dfa unpack |
736ec752 | 454 | */ |
dd51c848 JJ |
455 | size_t sz = blob - (char *) e->start - |
456 | ((e->pos - e->start) & 7); | |
736ec752 | 457 | size_t pad = ALIGN(sz, 8) - sz; |
5bfcbd22 JJ |
458 | if (aa_g_paranoid_load) |
459 | flags |= DFA_FLAG_VERIFY_STATES; | |
736ec752 JJ |
460 | dfa = aa_dfa_unpack(blob + pad, size - pad, flags); |
461 | ||
462 | if (IS_ERR(dfa)) | |
463 | return dfa; | |
464 | ||
736ec752 JJ |
465 | } |
466 | ||
467 | return dfa; | |
736ec752 JJ |
468 | } |
469 | ||
470 | /** | |
471 | * unpack_trans_table - unpack a profile transition table | |
472 | * @e: serialized data extent information (NOT NULL) | |
a0792e2c | 473 | * @table: str table to unpack to (NOT NULL) |
736ec752 | 474 | * |
a0792e2c | 475 | * Returns: true if table successfully unpacked or not present |
736ec752 | 476 | */ |
a0792e2c | 477 | static bool unpack_trans_table(struct aa_ext *e, struct aa_str_table *strs) |
736ec752 | 478 | { |
19fe43a5 | 479 | void *saved_pos = e->pos; |
a0792e2c | 480 | char **table; |
736ec752 JJ |
481 | |
482 | /* exec table is optional */ | |
483 | if (unpack_nameX(e, AA_STRUCT, "xtable")) { | |
371e50a0 JJ |
484 | u16 size; |
485 | int i; | |
736ec752 | 486 | |
371e50a0 JJ |
487 | if (unpack_array(e, NULL, &size) != TRI_TRUE || |
488 | size > (1 << 24)) | |
489 | /* currently 2^24 bits entries 0-3 */ | |
736ec752 | 490 | goto fail; |
a0792e2c JJ |
491 | table = kcalloc(size, sizeof(char *), GFP_KERNEL); |
492 | if (!table) | |
736ec752 JJ |
493 | goto fail; |
494 | ||
736ec752 JJ |
495 | for (i = 0; i < size; i++) { |
496 | char *str; | |
5379a331 | 497 | int c, j, pos, size2 = unpack_strdup(e, &str, NULL); |
736ec752 JJ |
498 | /* unpack_strdup verifies that the last character is |
499 | * null termination byte. | |
500 | */ | |
7ee95850 | 501 | if (!size2) |
736ec752 | 502 | goto fail; |
a0792e2c | 503 | table[i] = str; |
736ec752 JJ |
504 | /* verify that name doesn't start with space */ |
505 | if (isspace(*str)) | |
506 | goto fail; | |
507 | ||
508 | /* count internal # of internal \0 */ | |
5379a331 JJ |
509 | for (c = j = 0; j < size2 - 1; j++) { |
510 | if (!str[j]) { | |
511 | pos = j; | |
736ec752 | 512 | c++; |
5379a331 | 513 | } |
736ec752 JJ |
514 | } |
515 | if (*str == ':') { | |
5379a331 JJ |
516 | /* first character after : must be valid */ |
517 | if (!str[1]) | |
518 | goto fail; | |
736ec752 JJ |
519 | /* beginning with : requires an embedded \0, |
520 | * verify that exactly 1 internal \0 exists | |
521 | * trailing \0 already verified by unpack_strdup | |
5379a331 JJ |
522 | * |
523 | * convert \0 back to : for label_parse | |
736ec752 | 524 | */ |
5379a331 JJ |
525 | if (c == 1) |
526 | str[pos] = ':'; | |
527 | else if (c > 1) | |
736ec752 JJ |
528 | goto fail; |
529 | } else if (c) | |
530 | /* fail - all other cases with embedded \0 */ | |
531 | goto fail; | |
532 | } | |
533 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
534 | goto fail; | |
535 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
536 | goto fail; | |
a0792e2c JJ |
537 | |
538 | strs->table = table; | |
539 | strs->size = size; | |
736ec752 | 540 | } |
e3798609 | 541 | return true; |
736ec752 JJ |
542 | |
543 | fail: | |
a0792e2c | 544 | kfree_sensitive(table); |
19fe43a5 | 545 | e->pos = saved_pos; |
e3798609 | 546 | return false; |
736ec752 JJ |
547 | } |
548 | ||
8e51f908 MG |
549 | static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile) |
550 | { | |
551 | void *pos = e->pos; | |
552 | ||
553 | if (unpack_nameX(e, AA_STRUCT, "xattrs")) { | |
371e50a0 JJ |
554 | u16 size; |
555 | int i; | |
8e51f908 | 556 | |
371e50a0 JJ |
557 | if (unpack_array(e, NULL, &size) != TRI_TRUE) |
558 | goto fail; | |
8e51f908 | 559 | profile->xattr_count = size; |
73f488cd | 560 | profile->xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL); |
8e51f908 MG |
561 | if (!profile->xattrs) |
562 | goto fail; | |
563 | for (i = 0; i < size; i++) { | |
564 | if (!unpack_strdup(e, &profile->xattrs[i], NULL)) | |
565 | goto fail; | |
566 | } | |
567 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
568 | goto fail; | |
569 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
570 | goto fail; | |
571 | } | |
572 | ||
e3798609 | 573 | return true; |
8e51f908 MG |
574 | |
575 | fail: | |
576 | e->pos = pos; | |
e3798609 | 577 | return false; |
8e51f908 MG |
578 | } |
579 | ||
9caafbe2 MG |
580 | static bool unpack_secmark(struct aa_ext *e, struct aa_profile *profile) |
581 | { | |
582 | void *pos = e->pos; | |
371e50a0 JJ |
583 | u16 size; |
584 | int i; | |
9caafbe2 MG |
585 | |
586 | if (unpack_nameX(e, AA_STRUCT, "secmark")) { | |
371e50a0 JJ |
587 | if (unpack_array(e, NULL, &size) != TRI_TRUE) |
588 | goto fail; | |
9caafbe2 MG |
589 | |
590 | profile->secmark = kcalloc(size, sizeof(struct aa_secmark), | |
591 | GFP_KERNEL); | |
592 | if (!profile->secmark) | |
593 | goto fail; | |
594 | ||
595 | profile->secmark_count = size; | |
596 | ||
597 | for (i = 0; i < size; i++) { | |
598 | if (!unpack_u8(e, &profile->secmark[i].audit, NULL)) | |
599 | goto fail; | |
600 | if (!unpack_u8(e, &profile->secmark[i].deny, NULL)) | |
601 | goto fail; | |
602 | if (!unpack_strdup(e, &profile->secmark[i].label, NULL)) | |
603 | goto fail; | |
604 | } | |
605 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
606 | goto fail; | |
607 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
608 | goto fail; | |
609 | } | |
610 | ||
e3798609 | 611 | return true; |
9caafbe2 MG |
612 | |
613 | fail: | |
614 | if (profile->secmark) { | |
615 | for (i = 0; i < size; i++) | |
616 | kfree(profile->secmark[i].label); | |
617 | kfree(profile->secmark); | |
618 | profile->secmark_count = 0; | |
d8dbb581 | 619 | profile->secmark = NULL; |
9caafbe2 MG |
620 | } |
621 | ||
622 | e->pos = pos; | |
e3798609 | 623 | return false; |
9caafbe2 MG |
624 | } |
625 | ||
736ec752 JJ |
626 | static bool unpack_rlimits(struct aa_ext *e, struct aa_profile *profile) |
627 | { | |
628 | void *pos = e->pos; | |
629 | ||
630 | /* rlimits are optional */ | |
631 | if (unpack_nameX(e, AA_STRUCT, "rlimits")) { | |
371e50a0 JJ |
632 | u16 size; |
633 | int i; | |
736ec752 JJ |
634 | u32 tmp = 0; |
635 | if (!unpack_u32(e, &tmp, NULL)) | |
636 | goto fail; | |
637 | profile->rlimits.mask = tmp; | |
638 | ||
371e50a0 JJ |
639 | if (unpack_array(e, NULL, &size) != TRI_TRUE || |
640 | size > RLIM_NLIMITS) | |
736ec752 JJ |
641 | goto fail; |
642 | for (i = 0; i < size; i++) { | |
7ee95850 | 643 | u64 tmp2 = 0; |
736ec752 | 644 | int a = aa_map_resource(i); |
7ee95850 | 645 | if (!unpack_u64(e, &tmp2, NULL)) |
736ec752 | 646 | goto fail; |
7ee95850 | 647 | profile->rlimits.limits[a].rlim_max = tmp2; |
736ec752 JJ |
648 | } |
649 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
650 | goto fail; | |
651 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
652 | goto fail; | |
653 | } | |
e3798609 | 654 | return true; |
736ec752 JJ |
655 | |
656 | fail: | |
657 | e->pos = pos; | |
e3798609 | 658 | return false; |
736ec752 JJ |
659 | } |
660 | ||
fd1b2b95 JJ |
661 | static bool unpack_perm(struct aa_ext *e, u32 version, struct aa_perms *perm) |
662 | { | |
663 | bool res; | |
664 | ||
665 | if (version != 1) | |
666 | return false; | |
667 | ||
668 | res = unpack_u32(e, &perm->allow, NULL); | |
669 | res = res && unpack_u32(e, &perm->allow, NULL); | |
670 | res = res && unpack_u32(e, &perm->deny, NULL); | |
671 | res = res && unpack_u32(e, &perm->subtree, NULL); | |
672 | res = res && unpack_u32(e, &perm->cond, NULL); | |
673 | res = res && unpack_u32(e, &perm->kill, NULL); | |
674 | res = res && unpack_u32(e, &perm->complain, NULL); | |
675 | res = res && unpack_u32(e, &perm->prompt, NULL); | |
676 | res = res && unpack_u32(e, &perm->audit, NULL); | |
677 | res = res && unpack_u32(e, &perm->quiet, NULL); | |
678 | res = res && unpack_u32(e, &perm->hide, NULL); | |
679 | res = res && unpack_u32(e, &perm->xindex, NULL); | |
680 | res = res && unpack_u32(e, &perm->tag, NULL); | |
681 | res = res && unpack_u32(e, &perm->label, NULL); | |
682 | ||
683 | return res; | |
684 | } | |
685 | ||
686 | static ssize_t unpack_perms_table(struct aa_ext *e, struct aa_perms **perms) | |
687 | { | |
688 | void *pos = e->pos; | |
689 | u16 size = 0; | |
690 | ||
691 | AA_BUG(!perms); | |
692 | /* | |
693 | * policy perms are optional, in which case perms are embedded | |
694 | * in the dfa accept table | |
695 | */ | |
696 | if (unpack_nameX(e, AA_STRUCT, "perms")) { | |
697 | int i; | |
698 | u32 version; | |
699 | ||
700 | if (!unpack_u32(e, &version, "version")) | |
701 | goto fail_reset; | |
702 | if (unpack_array(e, NULL, &size) != TRI_TRUE) | |
703 | goto fail_reset; | |
704 | *perms = kcalloc(size, sizeof(struct aa_perms), GFP_KERNEL); | |
705 | if (!*perms) | |
706 | goto fail_reset; | |
707 | for (i = 0; i < size; i++) { | |
708 | if (!unpack_perm(e, version, &(*perms)[i])) | |
709 | goto fail; | |
710 | } | |
711 | if (!unpack_nameX(e, AA_ARRAYEND, NULL)) | |
712 | goto fail; | |
713 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
714 | goto fail; | |
715 | } else | |
716 | *perms = NULL; | |
717 | ||
718 | return size; | |
719 | ||
720 | fail: | |
721 | kfree(*perms); | |
722 | fail_reset: | |
723 | e->pos = pos; | |
724 | return -EPROTO; | |
725 | } | |
726 | ||
ad596ea7 JJ |
727 | static int unpack_pdb(struct aa_ext *e, struct aa_policydb *policy, |
728 | bool required_dfa, bool required_trans, | |
729 | const char **info) | |
730 | { | |
fd1b2b95 JJ |
731 | void *pos = e->pos; |
732 | int i, flags, error = -EPROTO; | |
ad596ea7 | 733 | |
fd1b2b95 JJ |
734 | policy->size = unpack_perms_table(e, &policy->perms); |
735 | if (policy->size < 0) { | |
736 | error = policy->size; | |
737 | policy->perms = NULL; | |
738 | *info = "failed to unpack - perms"; | |
739 | goto fail; | |
740 | } else if (policy->perms) { | |
741 | /* perms table present accept is index */ | |
742 | flags = TO_ACCEPT1_FLAG(YYTD_DATA32); | |
743 | } else { | |
744 | /* packed perms in accept1 and accept2 */ | |
745 | flags = TO_ACCEPT1_FLAG(YYTD_DATA32) | | |
746 | TO_ACCEPT2_FLAG(YYTD_DATA32); | |
747 | } | |
ad596ea7 | 748 | |
fd1b2b95 JJ |
749 | policy->dfa = unpack_dfa(e, flags); |
750 | if (IS_ERR(policy->dfa)) { | |
751 | error = PTR_ERR(policy->dfa); | |
ad596ea7 JJ |
752 | policy->dfa = NULL; |
753 | *info = "failed to unpack - dfa"; | |
fd1b2b95 | 754 | goto fail; |
ad596ea7 JJ |
755 | } else if (!policy->dfa) { |
756 | if (required_dfa) { | |
757 | *info = "missing required dfa"; | |
fd1b2b95 | 758 | goto fail; |
ad596ea7 JJ |
759 | } |
760 | goto out; | |
761 | } | |
762 | ||
763 | /* | |
764 | * only unpack the following if a dfa is present | |
765 | * | |
766 | * sadly start was given different names for file and policydb | |
767 | * but since it is optional we can try both | |
768 | */ | |
769 | if (!unpack_u32(e, &policy->start[0], "start")) | |
770 | /* default start state */ | |
771 | policy->start[0] = DFA_START; | |
772 | if (!unpack_u32(e, &policy->start[AA_CLASS_FILE], "dfa_start")) { | |
773 | /* default start state for xmatch and file dfa */ | |
774 | policy->start[AA_CLASS_FILE] = DFA_START; | |
775 | } /* setup class index */ | |
776 | for (i = AA_CLASS_FILE + 1; i <= AA_CLASS_LAST; i++) { | |
777 | policy->start[i] = aa_dfa_next(policy->dfa, policy->start[0], | |
778 | i); | |
779 | } | |
780 | if (!unpack_trans_table(e, &policy->trans) && required_trans) { | |
781 | *info = "failed to unpack profile transition table"; | |
fd1b2b95 | 782 | goto fail; |
ad596ea7 JJ |
783 | } |
784 | /* TODO: move compat mapping here, requires dfa merging first */ | |
785 | ||
786 | out: | |
787 | return 0; | |
fd1b2b95 JJ |
788 | |
789 | fail: | |
790 | e->pos = pos; | |
791 | return error; | |
ad596ea7 JJ |
792 | } |
793 | ||
e025be0f WH |
794 | static u32 strhash(const void *data, u32 len, u32 seed) |
795 | { | |
796 | const char * const *key = data; | |
797 | ||
798 | return jhash(*key, strlen(*key), seed); | |
799 | } | |
800 | ||
801 | static int datacmp(struct rhashtable_compare_arg *arg, const void *obj) | |
802 | { | |
803 | const struct aa_data *data = obj; | |
804 | const char * const *key = arg->key; | |
805 | ||
806 | return strcmp(data->key, *key); | |
807 | } | |
808 | ||
736ec752 JJ |
809 | /** |
810 | * unpack_profile - unpack a serialized profile | |
811 | * @e: serialized data extent information (NOT NULL) | |
5ee5d374 | 812 | * @ns_name: pointer of newly allocated copy of %NULL in case of error |
736ec752 JJ |
813 | * |
814 | * NOTE: unpack profile sets audit struct if there is a failure | |
815 | */ | |
04dc715e | 816 | static struct aa_profile *unpack_profile(struct aa_ext *e, char **ns_name) |
736ec752 JJ |
817 | { |
818 | struct aa_profile *profile = NULL; | |
04dc715e | 819 | const char *tmpname, *tmpns = NULL, *name = NULL; |
2410aa96 | 820 | const char *info = "failed to unpack profile"; |
80c094a4 | 821 | size_t ns_len; |
e025be0f WH |
822 | struct rhashtable_params params = { 0 }; |
823 | char *key = NULL; | |
824 | struct aa_data *data; | |
ad596ea7 | 825 | int error = -EPROTO; |
736ec752 JJ |
826 | kernel_cap_t tmpcap; |
827 | u32 tmp; | |
828 | ||
04dc715e JJ |
829 | *ns_name = NULL; |
830 | ||
736ec752 JJ |
831 | /* check that we have the right struct being passed */ |
832 | if (!unpack_nameX(e, AA_STRUCT, "profile")) | |
833 | goto fail; | |
834 | if (!unpack_str(e, &name, NULL)) | |
835 | goto fail; | |
04dc715e JJ |
836 | if (*name == '\0') |
837 | goto fail; | |
838 | ||
839 | tmpname = aa_splitn_fqname(name, strlen(name), &tmpns, &ns_len); | |
840 | if (tmpns) { | |
841 | *ns_name = kstrndup(tmpns, ns_len, GFP_KERNEL); | |
2410aa96 JJ |
842 | if (!*ns_name) { |
843 | info = "out of memory"; | |
04dc715e | 844 | goto fail; |
2410aa96 | 845 | } |
04dc715e JJ |
846 | name = tmpname; |
847 | } | |
736ec752 | 848 | |
637f688d | 849 | profile = aa_alloc_profile(name, NULL, GFP_KERNEL); |
736ec752 JJ |
850 | if (!profile) |
851 | return ERR_PTR(-ENOMEM); | |
852 | ||
853 | /* profile renaming is optional */ | |
854 | (void) unpack_str(e, &profile->rename, "rename"); | |
855 | ||
556d0be7 JJ |
856 | /* attachment string is optional */ |
857 | (void) unpack_str(e, &profile->attach, "attach"); | |
858 | ||
736ec752 | 859 | /* xmatch is optional and may be NULL */ |
ad596ea7 JJ |
860 | error = unpack_pdb(e, &profile->xmatch, false, false, &info); |
861 | if (error) | |
736ec752 | 862 | goto fail; |
ad596ea7 | 863 | |
b5b57993 | 864 | /* neither xmatch_len not xmatch_perms are optional if xmatch is set */ |
048d4954 | 865 | if (profile->xmatch.dfa) { |
2410aa96 JJ |
866 | if (!unpack_u32(e, &tmp, NULL)) { |
867 | info = "missing xmatch len"; | |
736ec752 | 868 | goto fail; |
2410aa96 | 869 | } |
736ec752 | 870 | profile->xmatch_len = tmp; |
048d4954 | 871 | profile->xmatch.start[AA_CLASS_XMATCH] = DFA_START; |
caa9f579 | 872 | if (aa_compat_map_xmatch(&profile->xmatch)) { |
e48ffd24 JJ |
873 | info = "failed to convert xmatch permission table"; |
874 | goto fail; | |
875 | } | |
736ec752 JJ |
876 | } |
877 | ||
72c8a768 JJ |
878 | /* disconnected attachment string is optional */ |
879 | (void) unpack_str(e, &profile->disconnected, "disconnected"); | |
880 | ||
736ec752 | 881 | /* per profile debug flags (complain, audit) */ |
2410aa96 JJ |
882 | if (!unpack_nameX(e, AA_STRUCT, "flags")) { |
883 | info = "profile missing flags"; | |
736ec752 | 884 | goto fail; |
2410aa96 JJ |
885 | } |
886 | info = "failed to unpack profile flags"; | |
736ec752 JJ |
887 | if (!unpack_u32(e, &tmp, NULL)) |
888 | goto fail; | |
03816507 | 889 | if (tmp & PACKED_FLAG_HAT) |
637f688d | 890 | profile->label.flags |= FLAG_HAT; |
c1ed5da1 JJ |
891 | if (tmp & PACKED_FLAG_DEBUG1) |
892 | profile->label.flags |= FLAG_DEBUG1; | |
893 | if (tmp & PACKED_FLAG_DEBUG2) | |
894 | profile->label.flags |= FLAG_DEBUG2; | |
736ec752 JJ |
895 | if (!unpack_u32(e, &tmp, NULL)) |
896 | goto fail; | |
3bbb7b2e | 897 | if (tmp == PACKED_MODE_COMPLAIN || (e->version & FORCE_COMPLAIN_FLAG)) { |
736ec752 | 898 | profile->mode = APPARMOR_COMPLAIN; |
3bbb7b2e | 899 | } else if (tmp == PACKED_MODE_ENFORCE) { |
f05841a9 | 900 | profile->mode = APPARMOR_ENFORCE; |
3bbb7b2e | 901 | } else if (tmp == PACKED_MODE_KILL) { |
03816507 | 902 | profile->mode = APPARMOR_KILL; |
3bbb7b2e | 903 | } else if (tmp == PACKED_MODE_UNCONFINED) { |
03816507 | 904 | profile->mode = APPARMOR_UNCONFINED; |
3bbb7b2e | 905 | profile->label.flags |= FLAG_UNCONFINED; |
22fac8a0 JJ |
906 | } else if (tmp == PACKED_MODE_USER) { |
907 | profile->mode = APPARMOR_USER; | |
3bbb7b2e | 908 | } else { |
f05841a9 | 909 | goto fail; |
3bbb7b2e | 910 | } |
736ec752 JJ |
911 | if (!unpack_u32(e, &tmp, NULL)) |
912 | goto fail; | |
913 | if (tmp) | |
914 | profile->audit = AUDIT_ALL; | |
915 | ||
916 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
917 | goto fail; | |
918 | ||
919 | /* path_flags is optional */ | |
920 | if (unpack_u32(e, &profile->path_flags, "path_flags")) | |
637f688d JJ |
921 | profile->path_flags |= profile->label.flags & |
922 | PATH_MEDIATE_DELETED; | |
736ec752 JJ |
923 | else |
924 | /* set a default value if path_flags field is not present */ | |
637f688d | 925 | profile->path_flags = PATH_MEDIATE_DELETED; |
736ec752 | 926 | |
2410aa96 | 927 | info = "failed to unpack profile capabilities"; |
736ec752 JJ |
928 | if (!unpack_u32(e, &(profile->caps.allow.cap[0]), NULL)) |
929 | goto fail; | |
930 | if (!unpack_u32(e, &(profile->caps.audit.cap[0]), NULL)) | |
931 | goto fail; | |
932 | if (!unpack_u32(e, &(profile->caps.quiet.cap[0]), NULL)) | |
933 | goto fail; | |
934 | if (!unpack_u32(e, &tmpcap.cap[0], NULL)) | |
935 | goto fail; | |
936 | ||
2410aa96 | 937 | info = "failed to unpack upper profile capabilities"; |
736ec752 JJ |
938 | if (unpack_nameX(e, AA_STRUCT, "caps64")) { |
939 | /* optional upper half of 64 bit caps */ | |
940 | if (!unpack_u32(e, &(profile->caps.allow.cap[1]), NULL)) | |
941 | goto fail; | |
942 | if (!unpack_u32(e, &(profile->caps.audit.cap[1]), NULL)) | |
943 | goto fail; | |
944 | if (!unpack_u32(e, &(profile->caps.quiet.cap[1]), NULL)) | |
945 | goto fail; | |
946 | if (!unpack_u32(e, &(tmpcap.cap[1]), NULL)) | |
947 | goto fail; | |
948 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) | |
949 | goto fail; | |
950 | } | |
951 | ||
2410aa96 | 952 | info = "failed to unpack extended profile capabilities"; |
736ec752 JJ |
953 | if (unpack_nameX(e, AA_STRUCT, "capsx")) { |
954 | /* optional extended caps mediation mask */ | |
955 | if (!unpack_u32(e, &(profile->caps.extended.cap[0]), NULL)) | |
956 | goto fail; | |
957 | if (!unpack_u32(e, &(profile->caps.extended.cap[1]), NULL)) | |
958 | goto fail; | |
cdbd2884 JJ |
959 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
960 | goto fail; | |
736ec752 JJ |
961 | } |
962 | ||
8e51f908 MG |
963 | if (!unpack_xattrs(e, profile)) { |
964 | info = "failed to unpack profile xattrs"; | |
965 | goto fail; | |
966 | } | |
967 | ||
2410aa96 JJ |
968 | if (!unpack_rlimits(e, profile)) { |
969 | info = "failed to unpack profile rlimits"; | |
736ec752 | 970 | goto fail; |
2410aa96 | 971 | } |
736ec752 | 972 | |
9caafbe2 MG |
973 | if (!unpack_secmark(e, profile)) { |
974 | info = "failed to unpack profile secmark rules"; | |
975 | goto fail; | |
976 | } | |
977 | ||
ad5ff3db JJ |
978 | if (unpack_nameX(e, AA_STRUCT, "policydb")) { |
979 | /* generic policy dfa - optional and may be NULL */ | |
2410aa96 | 980 | info = "failed to unpack policydb"; |
ad596ea7 JJ |
981 | error = unpack_pdb(e, &profile->policy, true, false, &info); |
982 | if (error) | |
983 | goto fail; | |
984 | /* Fixup: drop when we get rid of start array */ | |
985 | if (aa_dfa_next(profile->policy.dfa, profile->policy.start[0], | |
986 | AA_CLASS_FILE)) | |
987 | profile->policy.start[AA_CLASS_FILE] = | |
988 | aa_dfa_next(profile->policy.dfa, | |
989 | profile->policy.start[0], | |
990 | AA_CLASS_FILE); | |
ad5ff3db JJ |
991 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) |
992 | goto fail; | |
caa9f579 | 993 | if (aa_compat_map_policy(&profile->policy, e->version)) { |
e844fe9b JJ |
994 | info = "failed to remap policydb permission table"; |
995 | goto fail; | |
996 | } | |
11c236b8 JJ |
997 | } else |
998 | profile->policy.dfa = aa_get_dfa(nulldfa); | |
ad5ff3db | 999 | |
736ec752 | 1000 | /* get file rules */ |
ad596ea7 JJ |
1001 | error = unpack_pdb(e, &profile->file, false, true, &info); |
1002 | if (error) { | |
736ec752 | 1003 | goto fail; |
6604d4c1 | 1004 | } else if (profile->file.dfa) { |
caa9f579 | 1005 | if (aa_compat_map_file(&profile->file)) { |
7572fea3 JJ |
1006 | info = "failed to remap file permission table"; |
1007 | goto fail; | |
1008 | } | |
6604d4c1 JJ |
1009 | } else if (profile->policy.dfa && |
1010 | profile->policy.start[AA_CLASS_FILE]) { | |
1011 | profile->file.dfa = aa_get_dfa(profile->policy.dfa); | |
53bdc46f | 1012 | profile->file.start[AA_CLASS_FILE] = profile->policy.start[AA_CLASS_FILE]; |
11c236b8 JJ |
1013 | } else |
1014 | profile->file.dfa = aa_get_dfa(nulldfa); | |
736ec752 | 1015 | |
e025be0f | 1016 | if (unpack_nameX(e, AA_STRUCT, "data")) { |
2410aa96 | 1017 | info = "out of memory"; |
e025be0f WH |
1018 | profile->data = kzalloc(sizeof(*profile->data), GFP_KERNEL); |
1019 | if (!profile->data) | |
1020 | goto fail; | |
1021 | ||
1022 | params.nelem_hint = 3; | |
1023 | params.key_len = sizeof(void *); | |
1024 | params.key_offset = offsetof(struct aa_data, key); | |
1025 | params.head_offset = offsetof(struct aa_data, head); | |
1026 | params.hashfn = strhash; | |
1027 | params.obj_cmpfn = datacmp; | |
1028 | ||
2410aa96 JJ |
1029 | if (rhashtable_init(profile->data, ¶ms)) { |
1030 | info = "failed to init key, value hash table"; | |
e025be0f | 1031 | goto fail; |
2410aa96 | 1032 | } |
e025be0f WH |
1033 | |
1034 | while (unpack_strdup(e, &key, NULL)) { | |
1035 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
1036 | if (!data) { | |
453431a5 | 1037 | kfree_sensitive(key); |
e025be0f WH |
1038 | goto fail; |
1039 | } | |
1040 | ||
1041 | data->key = key; | |
1042 | data->size = unpack_blob(e, &data->data, NULL); | |
1043 | data->data = kvmemdup(data->data, data->size); | |
1044 | if (data->size && !data->data) { | |
453431a5 WL |
1045 | kfree_sensitive(data->key); |
1046 | kfree_sensitive(data); | |
e025be0f WH |
1047 | goto fail; |
1048 | } | |
1049 | ||
1050 | rhashtable_insert_fast(profile->data, &data->head, | |
1051 | profile->data->p); | |
1052 | } | |
1053 | ||
2410aa96 JJ |
1054 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) { |
1055 | info = "failed to unpack end of key, value data table"; | |
e025be0f | 1056 | goto fail; |
2410aa96 | 1057 | } |
e025be0f WH |
1058 | } |
1059 | ||
2410aa96 JJ |
1060 | if (!unpack_nameX(e, AA_STRUCTEND, NULL)) { |
1061 | info = "failed to unpack end of profile"; | |
736ec752 | 1062 | goto fail; |
2410aa96 | 1063 | } |
736ec752 JJ |
1064 | |
1065 | return profile; | |
1066 | ||
1067 | fail: | |
1068 | if (profile) | |
1069 | name = NULL; | |
1070 | else if (!name) | |
1071 | name = "unknown"; | |
2410aa96 | 1072 | audit_iface(profile, NULL, name, info, e, error); |
8651e1d6 | 1073 | aa_free_profile(profile); |
736ec752 JJ |
1074 | |
1075 | return ERR_PTR(error); | |
1076 | } | |
1077 | ||
1078 | /** | |
5ee5d374 | 1079 | * verify_header - unpack serialized stream header |
736ec752 | 1080 | * @e: serialized data read head (NOT NULL) |
dd51c848 | 1081 | * @required: whether the header is required or optional |
736ec752 JJ |
1082 | * @ns: Returns - namespace if one is specified else NULL (NOT NULL) |
1083 | * | |
1084 | * Returns: error or 0 if header is good | |
1085 | */ | |
dd51c848 | 1086 | static int verify_header(struct aa_ext *e, int required, const char **ns) |
736ec752 JJ |
1087 | { |
1088 | int error = -EPROTONOSUPPORT; | |
dd51c848 JJ |
1089 | const char *name = NULL; |
1090 | *ns = NULL; | |
1091 | ||
736ec752 JJ |
1092 | /* get the interface version */ |
1093 | if (!unpack_u32(e, &e->version, "version")) { | |
dd51c848 | 1094 | if (required) { |
04dc715e | 1095 | audit_iface(NULL, NULL, NULL, "invalid profile format", |
dd51c848 JJ |
1096 | e, error); |
1097 | return error; | |
1098 | } | |
736ec752 JJ |
1099 | } |
1100 | ||
474d6b75 JJ |
1101 | /* Check that the interface version is currently supported. |
1102 | * if not specified use previous version | |
1103 | * Mask off everything that is not kernel abi version | |
1104 | */ | |
3c076531 | 1105 | if (VERSION_LT(e->version, v5) || VERSION_GT(e->version, v9)) { |
04dc715e | 1106 | audit_iface(NULL, NULL, NULL, "unsupported interface version", |
474d6b75 JJ |
1107 | e, error); |
1108 | return error; | |
1109 | } | |
dd51c848 | 1110 | |
736ec752 | 1111 | /* read the namespace if present */ |
dd51c848 | 1112 | if (unpack_str(e, &name, "namespace")) { |
04dc715e JJ |
1113 | if (*name == '\0') { |
1114 | audit_iface(NULL, NULL, NULL, "invalid namespace name", | |
1115 | e, error); | |
1116 | return error; | |
1117 | } | |
145a0ef2 | 1118 | if (*ns && strcmp(*ns, name)) { |
04dc715e JJ |
1119 | audit_iface(NULL, NULL, NULL, "invalid ns change", e, |
1120 | error); | |
145a0ef2 JJ |
1121 | } else if (!*ns) { |
1122 | *ns = kstrdup(name, GFP_KERNEL); | |
1123 | if (!*ns) | |
1124 | return -ENOMEM; | |
1125 | } | |
dd51c848 | 1126 | } |
736ec752 JJ |
1127 | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static bool verify_xindex(int xindex, int table_size) | |
1132 | { | |
1133 | int index, xtype; | |
1134 | xtype = xindex & AA_X_TYPE_MASK; | |
1135 | index = xindex & AA_X_INDEX_MASK; | |
23ca7b64 | 1136 | if (xtype == AA_X_TABLE && index >= table_size) |
e3798609 ZW |
1137 | return false; |
1138 | return true; | |
736ec752 JJ |
1139 | } |
1140 | ||
1141 | /* verify dfa xindexes are in range of transition tables */ | |
1142 | static bool verify_dfa_xindex(struct aa_dfa *dfa, int table_size) | |
1143 | { | |
1144 | int i; | |
1145 | for (i = 0; i < dfa->tables[YYTD_ID_ACCEPT]->td_lolen; i++) { | |
7572fea3 | 1146 | if (!verify_xindex(ACCEPT_TABLE(dfa)[i], table_size)) |
e3798609 | 1147 | return false; |
736ec752 | 1148 | } |
e3798609 | 1149 | return true; |
736ec752 JJ |
1150 | } |
1151 | ||
1152 | /** | |
1153 | * verify_profile - Do post unpack analysis to verify profile consistency | |
1154 | * @profile: profile to verify (NOT NULL) | |
1155 | * | |
1156 | * Returns: 0 if passes verification else error | |
7572fea3 JJ |
1157 | * |
1158 | * This verification is post any unpack mapping or changes | |
736ec752 JJ |
1159 | */ |
1160 | static int verify_profile(struct aa_profile *profile) | |
1161 | { | |
e844fe9b | 1162 | if ((profile->file.dfa && |
7572fea3 | 1163 | !verify_dfa_xindex(profile->file.dfa, |
e844fe9b JJ |
1164 | profile->file.trans.size)) || |
1165 | (profile->policy.dfa && | |
1166 | !verify_dfa_xindex(profile->policy.dfa, | |
1167 | profile->policy.trans.size))) { | |
7572fea3 JJ |
1168 | audit_iface(profile, NULL, NULL, |
1169 | "Unpack: Invalid named transition", NULL, -EPROTO); | |
abbf8734 | 1170 | return -EPROTO; |
736ec752 JJ |
1171 | } |
1172 | ||
1173 | return 0; | |
1174 | } | |
1175 | ||
dd51c848 JJ |
1176 | void aa_load_ent_free(struct aa_load_ent *ent) |
1177 | { | |
1178 | if (ent) { | |
1179 | aa_put_profile(ent->rename); | |
1180 | aa_put_profile(ent->old); | |
1181 | aa_put_profile(ent->new); | |
04dc715e | 1182 | kfree(ent->ns_name); |
453431a5 | 1183 | kfree_sensitive(ent); |
dd51c848 JJ |
1184 | } |
1185 | } | |
1186 | ||
1187 | struct aa_load_ent *aa_load_ent_alloc(void) | |
1188 | { | |
1189 | struct aa_load_ent *ent = kzalloc(sizeof(*ent), GFP_KERNEL); | |
1190 | if (ent) | |
1191 | INIT_LIST_HEAD(&ent->list); | |
1192 | return ent; | |
1193 | } | |
1194 | ||
f4d6b94b | 1195 | static int compress_zstd(const char *src, size_t slen, char **dst, size_t *dlen) |
63c16c3a | 1196 | { |
f9da5b14 | 1197 | #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY |
f4d6b94b JT |
1198 | const zstd_parameters params = |
1199 | zstd_get_params(aa_g_rawdata_compression_level, slen); | |
1200 | const size_t wksp_len = zstd_cctx_workspace_bound(¶ms.cParams); | |
1201 | void *wksp = NULL; | |
1202 | zstd_cctx *ctx = NULL; | |
1203 | size_t out_len = zstd_compress_bound(slen); | |
1204 | void *out = NULL; | |
1205 | int ret = 0; | |
1206 | ||
1207 | out = kvzalloc(out_len, GFP_KERNEL); | |
1208 | if (!out) { | |
1209 | ret = -ENOMEM; | |
1210 | goto cleanup; | |
63c16c3a CC |
1211 | } |
1212 | ||
f4d6b94b JT |
1213 | wksp = kvzalloc(wksp_len, GFP_KERNEL); |
1214 | if (!wksp) { | |
1215 | ret = -ENOMEM; | |
1216 | goto cleanup; | |
63c16c3a CC |
1217 | } |
1218 | ||
f4d6b94b JT |
1219 | ctx = zstd_init_cctx(wksp, wksp_len); |
1220 | if (!ctx) { | |
1221 | ret = -EINVAL; | |
1222 | goto cleanup; | |
1223 | } | |
63c16c3a | 1224 | |
f4d6b94b JT |
1225 | out_len = zstd_compress_cctx(ctx, out, out_len, src, slen, ¶ms); |
1226 | if (zstd_is_error(out_len)) { | |
1227 | ret = -EINVAL; | |
1228 | goto cleanup; | |
63c16c3a | 1229 | } |
63c16c3a | 1230 | |
f4d6b94b JT |
1231 | if (is_vmalloc_addr(out)) { |
1232 | *dst = kvzalloc(out_len, GFP_KERNEL); | |
1233 | if (*dst) { | |
1234 | memcpy(*dst, out, out_len); | |
1235 | kvfree(out); | |
1236 | out = NULL; | |
63c16c3a | 1237 | } |
f4d6b94b | 1238 | } else { |
63c16c3a CC |
1239 | /* |
1240 | * If the staging buffer was kmalloc'd, then using krealloc is | |
1241 | * probably going to be faster. The destination buffer will | |
1242 | * always be smaller, so it's just shrunk, avoiding a memcpy | |
1243 | */ | |
f4d6b94b JT |
1244 | *dst = krealloc(out, out_len, GFP_KERNEL); |
1245 | } | |
63c16c3a | 1246 | |
f4d6b94b JT |
1247 | if (!*dst) { |
1248 | ret = -ENOMEM; | |
1249 | goto cleanup; | |
63c16c3a CC |
1250 | } |
1251 | ||
f4d6b94b | 1252 | *dlen = out_len; |
63c16c3a | 1253 | |
f4d6b94b JT |
1254 | cleanup: |
1255 | if (ret) { | |
1256 | kvfree(out); | |
1257 | *dst = NULL; | |
1258 | } | |
63c16c3a | 1259 | |
f4d6b94b JT |
1260 | kvfree(wksp); |
1261 | return ret; | |
f9da5b14 JJ |
1262 | #else |
1263 | *dlen = slen; | |
1264 | return 0; | |
1265 | #endif | |
63c16c3a CC |
1266 | } |
1267 | ||
1268 | static int compress_loaddata(struct aa_loaddata *data) | |
1269 | { | |
63c16c3a CC |
1270 | AA_BUG(data->compressed_size > 0); |
1271 | ||
1272 | /* | |
1273 | * Shortcut the no compression case, else we increase the amount of | |
1274 | * storage required by a small amount | |
1275 | */ | |
1276 | if (aa_g_rawdata_compression_level != 0) { | |
1277 | void *udata = data->data; | |
f4d6b94b JT |
1278 | int error = compress_zstd(udata, data->size, &data->data, |
1279 | &data->compressed_size); | |
63c16c3a CC |
1280 | if (error) |
1281 | return error; | |
1282 | ||
f9da5b14 JJ |
1283 | if (udata != data->data) |
1284 | kvfree(udata); | |
63c16c3a CC |
1285 | } else |
1286 | data->compressed_size = data->size; | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
736ec752 | 1291 | /** |
dd51c848 | 1292 | * aa_unpack - unpack packed binary profile(s) data loaded from user space |
736ec752 | 1293 | * @udata: user data copied to kmem (NOT NULL) |
dd51c848 | 1294 | * @lh: list to place unpacked profiles in a aa_repl_ws |
736ec752 JJ |
1295 | * @ns: Returns namespace profile is in if specified else NULL (NOT NULL) |
1296 | * | |
dd51c848 JJ |
1297 | * Unpack user data and return refcounted allocated profile(s) stored in |
1298 | * @lh in order of discovery, with the list chain stored in base.list | |
1299 | * or error | |
736ec752 | 1300 | * |
dd51c848 | 1301 | * Returns: profile(s) on @lh else error pointer if fails to unpack |
736ec752 | 1302 | */ |
5ac8c355 JJ |
1303 | int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, |
1304 | const char **ns) | |
736ec752 | 1305 | { |
dd51c848 | 1306 | struct aa_load_ent *tmp, *ent; |
736ec752 JJ |
1307 | struct aa_profile *profile = NULL; |
1308 | int error; | |
1309 | struct aa_ext e = { | |
5ac8c355 JJ |
1310 | .start = udata->data, |
1311 | .end = udata->data + udata->size, | |
1312 | .pos = udata->data, | |
736ec752 JJ |
1313 | }; |
1314 | ||
dd51c848 JJ |
1315 | *ns = NULL; |
1316 | while (e.pos < e.end) { | |
04dc715e | 1317 | char *ns_name = NULL; |
f8eb8a13 | 1318 | void *start; |
dd51c848 JJ |
1319 | error = verify_header(&e, e.pos == e.start, ns); |
1320 | if (error) | |
1321 | goto fail; | |
736ec752 | 1322 | |
f8eb8a13 | 1323 | start = e.pos; |
04dc715e | 1324 | profile = unpack_profile(&e, &ns_name); |
dd51c848 JJ |
1325 | if (IS_ERR(profile)) { |
1326 | error = PTR_ERR(profile); | |
1327 | goto fail; | |
1328 | } | |
1329 | ||
1330 | error = verify_profile(profile); | |
f8eb8a13 JJ |
1331 | if (error) |
1332 | goto fail_profile; | |
1333 | ||
31f75bfe JJ |
1334 | if (aa_g_hash_policy) |
1335 | error = aa_calc_profile_hash(profile, e.version, start, | |
6059f71f | 1336 | e.pos - start); |
f8eb8a13 JJ |
1337 | if (error) |
1338 | goto fail_profile; | |
dd51c848 JJ |
1339 | |
1340 | ent = aa_load_ent_alloc(); | |
1341 | if (!ent) { | |
1342 | error = -ENOMEM; | |
f8eb8a13 | 1343 | goto fail_profile; |
dd51c848 | 1344 | } |
736ec752 | 1345 | |
dd51c848 | 1346 | ent->new = profile; |
04dc715e | 1347 | ent->ns_name = ns_name; |
dd51c848 | 1348 | list_add_tail(&ent->list, lh); |
736ec752 | 1349 | } |
5ac8c355 | 1350 | udata->abi = e.version & K_ABI_MASK; |
31f75bfe JJ |
1351 | if (aa_g_hash_policy) { |
1352 | udata->hash = aa_calc_hash(udata->data, udata->size); | |
1353 | if (IS_ERR(udata->hash)) { | |
1354 | error = PTR_ERR(udata->hash); | |
1355 | udata->hash = NULL; | |
1356 | goto fail; | |
1357 | } | |
5ac8c355 | 1358 | } |
d61c57fd JJ |
1359 | |
1360 | if (aa_g_export_binary) { | |
1361 | error = compress_loaddata(udata); | |
1362 | if (error) | |
1363 | goto fail; | |
1364 | } | |
dd51c848 JJ |
1365 | return 0; |
1366 | ||
f8eb8a13 JJ |
1367 | fail_profile: |
1368 | aa_put_profile(profile); | |
1369 | ||
dd51c848 JJ |
1370 | fail: |
1371 | list_for_each_entry_safe(ent, tmp, lh, list) { | |
1372 | list_del_init(&ent->list); | |
1373 | aa_load_ent_free(ent); | |
1374 | } | |
1375 | ||
1376 | return error; | |
736ec752 | 1377 | } |
4d944bcd MS |
1378 | |
1379 | #ifdef CONFIG_SECURITY_APPARMOR_KUNIT_TEST | |
1380 | #include "policy_unpack_test.c" | |
1381 | #endif /* CONFIG_SECURITY_APPARMOR_KUNIT_TEST */ |