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