1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AppArmor security module 4 * 5 * This file contains AppArmor /sys/kernel/security/apparmor interface functions 6 * 7 * Copyright (C) 1998-2008 Novell/SUSE 8 * Copyright 2009-2010 Canonical Ltd. 9 */ 10 11 #include <linux/ctype.h> 12 #include <linux/slab.h> 13 #include <linux/security.h> 14 #include <linux/vmalloc.h> 15 #include <linux/init.h> 16 #include <linux/seq_file.h> 17 #include <linux/uaccess.h> 18 #include <linux/mount.h> 19 #include <linux/namei.h> 20 #include <linux/capability.h> 21 #include <linux/rcupdate.h> 22 #include <linux/fs.h> 23 #include <linux/fs_context.h> 24 #include <linux/poll.h> 25 #include <linux/zstd.h> 26 #include <linux/string.h> 27 #include <uapi/linux/major.h> 28 #include <uapi/linux/magic.h> 29 30 #include "include/apparmor.h" 31 #include "include/apparmorfs.h" 32 #include "include/audit.h" 33 #include "include/cred.h" 34 #include "include/crypto.h" 35 #include "include/ipc.h" 36 #include "include/label.h" 37 #include "include/net.h" 38 #include "include/lib.h" 39 #include "include/policy.h" 40 #include "include/policy_ns.h" 41 #include "include/resource.h" 42 #include "include/policy_unpack.h" 43 #include "include/task.h" 44 45 /* 46 * The apparmor filesystem interface used for policy load and introspection 47 * The interface is split into two main components based on their function 48 * a securityfs component: 49 * used for static files that are always available, and which allows 50 * userspace to specify the location of the security filesystem. 51 * 52 * fns and data are prefixed with 53 * aa_sfs_ 54 * 55 * an apparmorfs component: 56 * used loaded policy content and introspection. It is not part of a 57 * regular mounted filesystem and is available only through the magic 58 * policy symlink in the root of the securityfs apparmor/ directory. 59 * Tasks queries will be magically redirected to the correct portion 60 * of the policy tree based on their confinement. 61 * 62 * fns and data are prefixed with 63 * aafs_ 64 * 65 * The aa_fs_ prefix is used to indicate the fn is used by both the 66 * securityfs and apparmorfs filesystems. 67 */ 68 69 #define IREF_POISON 101 70 71 /* 72 * support fns 73 */ 74 75 struct rawdata_f_data { 76 struct aa_loaddata *loaddata; 77 DECLARE_FLEX_ARRAY(char, data); 78 }; 79 80 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 81 82 static void rawdata_f_data_free(struct rawdata_f_data *private) 83 { 84 if (!private) 85 return; 86 87 aa_put_i_loaddata(private->loaddata); 88 kvfree(private); 89 } 90 91 static struct rawdata_f_data *rawdata_f_data_alloc(size_t size) 92 { 93 struct rawdata_f_data *ret; 94 95 if (size > SIZE_MAX - sizeof(*ret)) 96 return ERR_PTR(-EINVAL); 97 98 ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL); 99 if (!ret) 100 return ERR_PTR(-ENOMEM); 101 102 return ret; 103 } 104 #endif 105 106 /** 107 * mangle_name - mangle a profile name to std profile layout form 108 * @name: profile name to mangle (NOT NULL) 109 * @target: buffer to store mangled name, same length as @name (MAYBE NULL) 110 * 111 * Returns: length of mangled name 112 */ 113 static int mangle_name(const char *name, char *target) 114 { 115 char *t = target; 116 117 while (*name == '/' || *name == '.') 118 name++; 119 120 if (target) { 121 for (; *name; name++) { 122 if (*name == '/') 123 *(t)++ = '.'; 124 else if (isspace(*name)) 125 *(t)++ = '_'; 126 else if (isalnum(*name) || strchr("._-", *name)) 127 *(t)++ = *name; 128 } 129 130 *t = 0; 131 } else { 132 int len = 0; 133 for (; *name; name++) { 134 if (isalnum(*name) || isspace(*name) || 135 strchr("/._-", *name)) 136 len++; 137 } 138 139 return len; 140 } 141 142 return t - target; 143 } 144 145 146 /* 147 * aafs - core fns and data for the policy tree 148 */ 149 150 #define AAFS_NAME "apparmorfs" 151 static struct vfsmount *aafs_mnt; 152 static int aafs_count; 153 154 155 static int aafs_show_path(struct seq_file *seq, struct dentry *dentry) 156 { 157 seq_printf(seq, "%s:[%llu]", AAFS_NAME, d_inode(dentry)->i_ino); 158 return 0; 159 } 160 161 static struct aa_ns *get_ns_common_ref(struct aa_common_ref *ref) 162 { 163 if (ref) { 164 struct aa_label *reflabel = container_of(ref, struct aa_label, 165 count); 166 return aa_get_ns(labels_ns(reflabel)); 167 } 168 169 return NULL; 170 } 171 172 static struct aa_proxy *get_proxy_common_ref(struct aa_common_ref *ref) 173 { 174 if (ref) 175 return aa_get_proxy(container_of(ref, struct aa_proxy, count)); 176 177 return NULL; 178 } 179 180 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 181 static struct aa_loaddata *get_loaddata_common_ref(struct aa_common_ref *ref) 182 { 183 if (ref) 184 return aa_get_i_loaddata(container_of(ref, struct aa_loaddata, 185 count)); 186 return NULL; 187 } 188 #endif 189 190 static void aa_put_common_ref(struct aa_common_ref *ref) 191 { 192 if (!ref) 193 return; 194 195 switch (ref->reftype) { 196 case REF_RAWDATA: 197 aa_put_i_loaddata(container_of(ref, struct aa_loaddata, 198 count)); 199 break; 200 case REF_PROXY: 201 aa_put_proxy(container_of(ref, struct aa_proxy, 202 count)); 203 break; 204 case REF_NS: 205 /* ns count is held on its unconfined label */ 206 aa_put_ns(labels_ns(container_of(ref, struct aa_label, count))); 207 break; 208 default: 209 AA_BUG(true, "unknown refcount type"); 210 break; 211 } 212 } 213 214 static void aa_get_common_ref(struct aa_common_ref *ref) 215 { 216 kref_get(&ref->count); 217 } 218 219 static void aafs_evict(struct inode *inode) 220 { 221 struct aa_common_ref *ref = inode->i_private; 222 223 clear_inode(inode); 224 aa_put_common_ref(ref); 225 inode->i_private = (void *) IREF_POISON; 226 } 227 228 static void aafs_free_inode(struct inode *inode) 229 { 230 if (S_ISLNK(inode->i_mode)) 231 kfree(inode->i_link); 232 free_inode_nonrcu(inode); 233 } 234 235 static const struct super_operations aafs_super_ops = { 236 .statfs = simple_statfs, 237 .evict_inode = aafs_evict, 238 .free_inode = aafs_free_inode, 239 .show_path = aafs_show_path, 240 }; 241 242 static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc) 243 { 244 static struct tree_descr files[] = { {""} }; 245 int error; 246 247 error = simple_fill_super(sb, AAFS_MAGIC, files); 248 if (error) 249 return error; 250 sb->s_op = &aafs_super_ops; 251 252 return 0; 253 } 254 255 static int apparmorfs_get_tree(struct fs_context *fc) 256 { 257 return get_tree_single(fc, apparmorfs_fill_super); 258 } 259 260 static const struct fs_context_operations apparmorfs_context_ops = { 261 .get_tree = apparmorfs_get_tree, 262 }; 263 264 static int apparmorfs_init_fs_context(struct fs_context *fc) 265 { 266 fc->ops = &apparmorfs_context_ops; 267 return 0; 268 } 269 270 static struct file_system_type aafs_ops = { 271 .owner = THIS_MODULE, 272 .name = AAFS_NAME, 273 .init_fs_context = apparmorfs_init_fs_context, 274 .kill_sb = kill_anon_super, 275 }; 276 277 /** 278 * __aafs_setup_d_inode - basic inode setup for apparmorfs 279 * @dir: parent directory for the dentry 280 * @dentry: dentry we are setting the inode up for 281 * @mode: permissions the file should have 282 * @data: data to store on inode.i_private, available in open() 283 * @link: if symlink, symlink target string 284 * @fops: struct file_operations that should be used 285 * @iops: struct of inode_operations that should be used 286 */ 287 static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry, 288 umode_t mode, void *data, char *link, 289 const struct file_operations *fops, 290 const struct inode_operations *iops) 291 { 292 struct inode *inode = new_inode(dir->i_sb); 293 294 AA_BUG(!dir); 295 AA_BUG(!dentry); 296 297 if (!inode) 298 return -ENOMEM; 299 300 inode->i_ino = get_next_ino(); 301 inode->i_mode = mode; 302 simple_inode_init_ts(inode); 303 inode->i_private = data; 304 if (S_ISDIR(mode)) { 305 inode->i_op = iops ? iops : &simple_dir_inode_operations; 306 inode->i_fop = &simple_dir_operations; 307 inc_nlink(inode); 308 inc_nlink(dir); 309 } else if (S_ISLNK(mode)) { 310 inode->i_op = iops ? iops : &simple_symlink_inode_operations; 311 inode->i_link = link; 312 } else { 313 inode->i_fop = fops; 314 } 315 d_instantiate(dentry, inode); 316 dget(dentry); 317 318 return 0; 319 } 320 321 /** 322 * aafs_create - create a dentry in the apparmorfs filesystem 323 * 324 * @name: name of dentry to create 325 * @mode: permissions the file should have 326 * @parent: parent directory for this dentry 327 * @data: data to store on inode.i_private, available in open() 328 * @link: if symlink, symlink target string 329 * @fops: struct file_operations that should be used for 330 * @iops: struct of inode_operations that should be used 331 * 332 * This is the basic "create a xxx" function for apparmorfs. 333 * 334 * Returns a pointer to a dentry if it succeeds, that must be free with 335 * aafs_remove(). Will return ERR_PTR on failure. 336 */ 337 static struct dentry *aafs_create(const char *name, umode_t mode, 338 struct dentry *parent, 339 struct aa_common_ref *data, void *link, 340 const struct file_operations *fops, 341 const struct inode_operations *iops) 342 { 343 struct dentry *dentry; 344 struct inode *dir; 345 int error; 346 347 AA_BUG(!name); 348 AA_BUG(!parent); 349 350 if (!(mode & S_IFMT)) 351 mode = (mode & S_IALLUGO) | S_IFREG; 352 353 error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 354 if (error) 355 return ERR_PTR(error); 356 357 dir = d_inode(parent); 358 359 dentry = simple_start_creating(parent, name); 360 if (IS_ERR(dentry)) { 361 error = PTR_ERR(dentry); 362 goto fail; 363 } 364 365 error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops); 366 simple_done_creating(dentry); 367 if (error) 368 goto fail; 369 370 if (data) 371 aa_get_common_ref(data); 372 373 return dentry; 374 375 fail: 376 simple_release_fs(&aafs_mnt, &aafs_count); 377 return ERR_PTR(error); 378 } 379 380 /** 381 * aafs_create_file - create a file in the apparmorfs filesystem 382 * 383 * @name: name of dentry to create 384 * @mode: permissions the file should have 385 * @parent: parent directory for this dentry 386 * @data: data to store on inode.i_private, available in open() 387 * @fops: struct file_operations that should be used for 388 * 389 * see aafs_create 390 */ 391 static struct dentry *aafs_create_file(const char *name, umode_t mode, 392 struct dentry *parent, 393 struct aa_common_ref *data, 394 const struct file_operations *fops) 395 { 396 return aafs_create(name, mode, parent, data, NULL, fops, NULL); 397 } 398 399 /** 400 * aafs_create_dir - create a directory in the apparmorfs filesystem 401 * 402 * @name: name of dentry to create 403 * @parent: parent directory for this dentry 404 * 405 * see aafs_create 406 */ 407 static struct dentry *aafs_create_dir(const char *name, struct dentry *parent) 408 { 409 return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL, 410 NULL); 411 } 412 413 /** 414 * aafs_remove - removes a file or directory from the apparmorfs filesystem 415 * 416 * @dentry: dentry of the file/directory/symlink to removed. 417 */ 418 static void aafs_remove(struct dentry *dentry) 419 { 420 struct inode *dir; 421 422 if (!dentry || IS_ERR(dentry)) 423 return; 424 425 /* ->d_parent is stable as rename is not supported */ 426 dir = d_inode(dentry->d_parent); 427 dentry = start_removing_dentry(dentry->d_parent, dentry); 428 if (!IS_ERR(dentry) && simple_positive(dentry)) { 429 if (d_is_dir(dentry)) { 430 if (!WARN_ON(!simple_empty(dentry))) { 431 __simple_rmdir(dir, dentry); 432 dput(dentry); 433 } 434 } else { 435 __simple_unlink(dir, dentry); 436 dput(dentry); 437 } 438 d_delete(dentry); 439 } 440 end_removing(dentry); 441 simple_release_fs(&aafs_mnt, &aafs_count); 442 } 443 444 445 /* 446 * aa_fs - policy load/replace/remove 447 */ 448 449 /** 450 * aa_simple_write_to_buffer - common routine for getting policy from user 451 * @userbuf: user buffer to copy data from (NOT NULL) 452 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) 453 * @copy_size: size of data to copy from user buffer 454 * @pos: position write is at in the file (NOT NULL) 455 * 456 * Returns: kernel buffer containing copy of user buffer data or an 457 * ERR_PTR on failure. 458 */ 459 static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, 460 size_t alloc_size, 461 size_t copy_size, 462 loff_t *pos) 463 { 464 struct aa_loaddata *data; 465 466 AA_BUG(copy_size > alloc_size); 467 468 if (*pos != 0) 469 /* only writes from pos 0, that is complete writes */ 470 return ERR_PTR(-ESPIPE); 471 472 /* freed by caller to simple_write_to_buffer */ 473 data = aa_loaddata_alloc(alloc_size); 474 if (IS_ERR(data)) 475 return data; 476 477 data->size = copy_size; 478 if (copy_from_user(data->data, userbuf, copy_size)) { 479 /* trigger free - don't need to put pcount */ 480 aa_put_i_loaddata(data); 481 return ERR_PTR(-EFAULT); 482 } 483 484 return data; 485 } 486 487 #if defined(CONFIG_SECURITY_APPARMOR_COMPRESSED_POLICY) || \ 488 defined(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY) 489 static int decompress_zstd(char *src, size_t slen, char *dst, size_t dlen) 490 { 491 if (slen < dlen) { 492 const size_t wksp_len = zstd_dctx_workspace_bound(); 493 zstd_dctx *ctx; 494 void *wksp; 495 size_t out_len; 496 int ret = 0; 497 498 wksp = kvzalloc(wksp_len, GFP_KERNEL); 499 if (!wksp) { 500 ret = -ENOMEM; 501 goto cleanup; 502 } 503 ctx = zstd_init_dctx(wksp, wksp_len); 504 if (ctx == NULL) { 505 ret = -ENOMEM; 506 goto cleanup; 507 } 508 out_len = zstd_decompress_dctx(ctx, dst, dlen, src, slen); 509 if (zstd_is_error(out_len)) { 510 ret = -EINVAL; 511 goto cleanup; 512 } 513 cleanup: 514 kvfree(wksp); 515 return ret; 516 } 517 518 if (dlen < slen) 519 return -EINVAL; 520 memcpy(dst, src, slen); 521 return 0; 522 } 523 #endif 524 525 526 #ifdef CONFIG_SECURITY_APPARMOR_COMPRESSED_POLICY 527 /** 528 * aa_get_data_from_compressed - common routine for getting compressed policy 529 * from user and get both compressed and uncompressed version. 530 * @userbuf: user buffer to copy data from (NOT NULL) 531 * @buffer_size: size of user buffer 532 * @pos: position write is at in the file (NOT NULL) 533 * @compressed_data: Ptr on compressed data. *compressed_data is allocated there 534 * 535 * Returns: kernel buffer containing copy of user buffer data or an 536 * ERR_PTR on failure. 537 */ 538 static struct aa_loaddata *aa_get_data_from_compressed(const char __user *userbuf, 539 size_t buffer_size, 540 loff_t *pos, 541 char **compressed_data) 542 { 543 struct aa_loaddata *data; 544 zstd_frame_header header; 545 int error; 546 547 if (!userbuf || !pos) 548 return ERR_PTR(-EINVAL); 549 if (*pos) 550 return ERR_PTR(-ESPIPE); 551 552 *compressed_data = kvmalloc(buffer_size, GFP_KERNEL); 553 if (!*compressed_data) 554 return ERR_PTR(-ENOMEM); 555 error = copy_from_user(*compressed_data, userbuf, buffer_size); 556 if (error) 557 goto fail; 558 559 error = zstd_get_frame_header(&header, *compressed_data, buffer_size); 560 if (error || header.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN || 561 header.frameContentSize == ZSTD_CONTENTSIZE_ERROR) { 562 error = -EINVAL; 563 goto fail; 564 } 565 566 data = aa_loaddata_alloc(header.frameContentSize); 567 if (IS_ERR(data)) { 568 error = PTR_ERR(data); 569 goto fail; 570 } 571 572 // We then decompress the data 573 error = decompress_zstd(*compressed_data, buffer_size, data->data, 574 header.frameContentSize); 575 if (error) 576 goto fail_decompress; 577 578 data->size = header.frameContentSize; 579 return data; 580 581 fail_decompress: 582 aa_put_i_loaddata(data); 583 fail: 584 kvfree(*compressed_data); 585 return ERR_PTR(error); 586 587 } 588 #else 589 static struct aa_loaddata *aa_get_data_from_compressed(const char __user *userbuf __always_unused, 590 size_t buffer_size __always_unused, 591 loff_t *pos __always_unused, 592 char **compressed_data __always_unused) 593 { 594 return ERR_PTR(-EINVAL); 595 } 596 #endif /* CONFIG_SECURITY_APPARMOR_COMPRESSED_POLICY */ 597 598 struct aa_user_hdr { 599 uint8_t version; 600 uint8_t compress_level; 601 uint8_t padding[6]; /* force 8-byte alignment */ 602 } __packed __aligned(8); 603 604 #define aa_hdr_magic "\x04\x08\x00\x76\x65\x72\x73\x69\x6f\x6e\x00\x02" 605 #define aa_hdr_magic_size 12 606 607 static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, 608 loff_t *pos, struct aa_ns *ns, 609 const struct cred *ocred) 610 { 611 struct aa_loaddata *data; 612 struct aa_label *label; 613 ssize_t error; 614 char *compressed_data = NULL; 615 __le32 magic_le; 616 bool is_compressed; 617 u8 aahdr[aa_hdr_magic_size]; 618 619 label = begin_current_label_crit_section(); 620 621 /* high level check about policy management - fine grained in 622 * below after unpack 623 */ 624 error = aa_may_manage_policy(current_cred(), label, ns, ocred, mask); 625 if (error) 626 goto end_section; 627 628 /* If the policy is userspace compressed we start by decompressing it 629 * to make the required checks (computing hash, verifying profile, ...) 630 * 631 * Getting a userspace-compressed version then decompressing it in the 632 * kernel actually makes sense since zstd decompression is ~3.5x faster 633 * than compression. This also allow to increase the compression level. 634 */ 635 636 if (size >= sizeof(__le32) && 637 !copy_from_user(&magic_le, buf, sizeof(magic_le)) && 638 le32_to_cpu(magic_le) == ZSTD_MAGICNUMBER) { 639 is_compressed = true; 640 } else if (size >= sizeof(struct aa_user_hdr) + sizeof(__le32) && 641 !copy_from_user(&magic_le, 642 buf + sizeof(struct aa_user_hdr), 643 sizeof(magic_le)) && 644 le32_to_cpu(magic_le) == ZSTD_MAGICNUMBER) { 645 is_compressed = true; 646 /* skip the userspace header if present */ 647 buf += sizeof(struct aa_user_hdr); 648 size -= sizeof(struct aa_user_hdr); 649 } else if (size >= sizeof(struct aa_user_hdr) + 650 aa_hdr_magic_size && 651 !copy_from_user(&aahdr, 652 buf + sizeof(struct aa_user_hdr), 653 aa_hdr_magic_size) && 654 memcmp(&aahdr, aa_hdr_magic, aa_hdr_magic_size) == 0) { 655 /* uncompressed blob with user hdr */ 656 buf += sizeof(struct aa_user_hdr); 657 size -= sizeof(struct aa_user_hdr); 658 is_compressed = false; 659 } else { 660 is_compressed = false; 661 } 662 if (is_compressed) { 663 664 data = aa_get_data_from_compressed(buf, size, pos, &compressed_data); 665 error = PTR_ERR(data); 666 } else { 667 data = aa_simple_write_to_buffer(buf, size, size, pos); 668 error = PTR_ERR(data); 669 } 670 671 if (!IS_ERR(data)) { 672 error = aa_replace_profiles(ns, label, mask, data, 673 compressed_data, size); 674 /* put pcount, which will put count and free if no 675 * profiles referencing it. 676 */ 677 aa_put_profile_loaddata(data); 678 } 679 end_section: 680 end_current_label_crit_section(label); 681 682 return error; 683 } 684 685 /* .load file hook fn to load policy */ 686 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 687 loff_t *pos) 688 { 689 struct aa_ns *ns = get_ns_common_ref(f->f_inode->i_private); 690 int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns, 691 f->f_cred); 692 693 aa_put_ns(ns); 694 695 return error; 696 } 697 698 static const struct file_operations aa_fs_profile_load = { 699 .write = profile_load, 700 .llseek = default_llseek, 701 }; 702 703 /* .replace file hook fn to load and/or replace policy */ 704 static ssize_t profile_replace(struct file *f, const char __user *buf, 705 size_t size, loff_t *pos) 706 { 707 struct aa_ns *ns = get_ns_common_ref(f->f_inode->i_private); 708 int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, 709 buf, size, pos, ns, f->f_cred); 710 aa_put_ns(ns); 711 712 return error; 713 } 714 715 static const struct file_operations aa_fs_profile_replace = { 716 .write = profile_replace, 717 .llseek = default_llseek, 718 }; 719 720 721 /* .remove file hook fn to remove loaded policy */ 722 static ssize_t profile_remove(struct file *f, const char __user *buf, 723 size_t size, loff_t *pos) 724 { 725 struct aa_loaddata *data; 726 struct aa_label *label; 727 ssize_t error; 728 struct aa_ns *ns = get_ns_common_ref(f->f_inode->i_private); 729 730 label = begin_current_label_crit_section(); 731 /* high level check about policy management - fine grained in 732 * below after unpack 733 */ 734 error = aa_may_manage_policy(current_cred(), label, ns, 735 f->f_cred, AA_MAY_REMOVE_POLICY); 736 if (error) 737 goto out; 738 739 /* 740 * aa_remove_profile needs a null terminated string so 1 extra 741 * byte is allocated and the copied data is null terminated. 742 */ 743 data = aa_simple_write_to_buffer(buf, size + 1, size, pos); 744 745 error = PTR_ERR(data); 746 if (!IS_ERR(data)) { 747 data->data[size] = 0; 748 error = aa_remove_profiles(ns, label, data->data, size); 749 aa_put_profile_loaddata(data); 750 } 751 out: 752 end_current_label_crit_section(label); 753 aa_put_ns(ns); 754 return error; 755 } 756 757 static const struct file_operations aa_fs_profile_remove = { 758 .write = profile_remove, 759 .llseek = default_llseek, 760 }; 761 762 struct aa_revision { 763 struct aa_ns *ns; 764 long last_read; 765 }; 766 767 /* revision file hook fn for policy loads */ 768 static int ns_revision_release(struct inode *inode, struct file *file) 769 { 770 struct aa_revision *rev = file->private_data; 771 772 if (rev) { 773 aa_put_ns(rev->ns); 774 kfree(rev); 775 } 776 777 return 0; 778 } 779 780 static ssize_t ns_revision_read(struct file *file, char __user *buf, 781 size_t size, loff_t *ppos) 782 { 783 struct aa_revision *rev = file->private_data; 784 char buffer[32]; 785 long last_read; 786 int avail; 787 788 mutex_lock_nested(&rev->ns->lock, rev->ns->level); 789 last_read = rev->last_read; 790 if (last_read == rev->ns->revision) { 791 mutex_unlock(&rev->ns->lock); 792 if (file->f_flags & O_NONBLOCK) 793 return -EAGAIN; 794 if (wait_event_interruptible(rev->ns->wait, 795 last_read != 796 READ_ONCE(rev->ns->revision))) 797 return -ERESTARTSYS; 798 mutex_lock_nested(&rev->ns->lock, rev->ns->level); 799 } 800 801 avail = sprintf(buffer, "%ld\n", rev->ns->revision); 802 if (*ppos + size > avail) { 803 rev->last_read = rev->ns->revision; 804 *ppos = 0; 805 } 806 mutex_unlock(&rev->ns->lock); 807 808 return simple_read_from_buffer(buf, size, ppos, buffer, avail); 809 } 810 811 static int ns_revision_open(struct inode *inode, struct file *file) 812 { 813 struct aa_revision *rev = kzalloc_obj(*rev); 814 815 if (!rev) 816 return -ENOMEM; 817 818 rev->ns = get_ns_common_ref(inode->i_private); 819 if (!rev->ns) 820 rev->ns = aa_get_current_ns(); 821 file->private_data = rev; 822 823 return 0; 824 } 825 826 static __poll_t ns_revision_poll(struct file *file, poll_table *pt) 827 { 828 struct aa_revision *rev = file->private_data; 829 __poll_t mask = 0; 830 831 if (rev) { 832 mutex_lock_nested(&rev->ns->lock, rev->ns->level); 833 poll_wait(file, &rev->ns->wait, pt); 834 if (rev->last_read < rev->ns->revision) 835 mask |= EPOLLIN | EPOLLRDNORM; 836 mutex_unlock(&rev->ns->lock); 837 } 838 839 return mask; 840 } 841 842 void __aa_bump_ns_revision(struct aa_ns *ns) 843 { 844 WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1); 845 wake_up_interruptible(&ns->wait); 846 } 847 848 static const struct file_operations aa_fs_ns_revision_fops = { 849 .owner = THIS_MODULE, 850 .open = ns_revision_open, 851 .poll = ns_revision_poll, 852 .read = ns_revision_read, 853 .llseek = generic_file_llseek, 854 .release = ns_revision_release, 855 }; 856 857 static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, 858 const char *match_str, size_t match_len) 859 { 860 struct aa_ruleset *rules = profile->label.rules[0]; 861 struct aa_perms tmp = { }; 862 aa_state_t state = DFA_NOMATCH; 863 864 if (profile_unconfined(profile)) 865 return; 866 if (rules->file->dfa && *match_str == AA_CLASS_FILE) { 867 state = aa_dfa_match_len(rules->file->dfa, 868 rules->file->start[AA_CLASS_FILE], 869 match_str + 1, match_len - 1); 870 if (state) { 871 struct path_cond cond = { }; 872 873 tmp = *(aa_lookup_condperms(current_fsuid(), 874 rules->file, state, &cond)); 875 } 876 } else if (rules->policy->dfa) { 877 if (!RULE_MEDIATES(rules, *match_str)) 878 return; /* no change to current perms */ 879 /* old user space does not correctly detect dbus mediation 880 * support so we may get dbus policy and requests when 881 * the abi doesn't support it. This can cause mediation 882 * regressions, so explicitly test for this situation. 883 */ 884 if (*match_str == AA_CLASS_DBUS && 885 !RULE_MEDIATES_v9NET(rules)) 886 return; /* no change to current perms */ 887 state = aa_dfa_match_len(rules->policy->dfa, 888 rules->policy->start[0], 889 match_str, match_len); 890 if (state) 891 tmp = *aa_lookup_perms(rules->policy, state); 892 } 893 aa_apply_modes_to_perms(profile, &tmp); 894 aa_perms_accum_raw(perms, &tmp); 895 } 896 897 898 /** 899 * query_data - queries a policy and writes its data to buf 900 * @buf: the resulting data is stored here (NOT NULL) 901 * @buf_len: size of buf 902 * @query: query string used to retrieve data 903 * @query_len: size of query including second NUL byte 904 * 905 * The buffers pointed to by buf and query may overlap. The query buffer is 906 * parsed before buf is written to. 907 * 908 * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of 909 * the security confinement context and <KEY> is the name of the data to 910 * retrieve. <LABEL> and <KEY> must not be NUL-terminated. 911 * 912 * Don't expect the contents of buf to be preserved on failure. 913 * 914 * Returns: number of characters written to buf or -errno on failure 915 */ 916 static ssize_t query_data(char *buf, size_t buf_len, 917 char *query, size_t query_len) 918 { 919 char *out; 920 const char *key; 921 struct label_it i; 922 struct aa_label *label, *curr; 923 struct aa_profile *profile; 924 struct aa_data *data; 925 u32 bytes, blocks; 926 __le32 outle32; 927 928 if (!query_len) 929 return -EINVAL; /* need a query */ 930 931 key = query + strnlen(query, query_len) + 1; 932 if (key + 1 >= query + query_len) 933 return -EINVAL; /* not enough space for a non-empty key */ 934 if (key + strnlen(key, query + query_len - key) >= query + query_len) 935 return -EINVAL; /* must end with NUL */ 936 937 if (buf_len < sizeof(bytes) + sizeof(blocks)) 938 return -EINVAL; /* not enough space */ 939 940 curr = begin_current_label_crit_section(); 941 label = aa_label_parse(curr, query, GFP_KERNEL, false, false); 942 end_current_label_crit_section(curr); 943 if (IS_ERR(label)) 944 return PTR_ERR(label); 945 946 /* We are going to leave space for two numbers. The first is the total 947 * number of bytes we are writing after the first number. This is so 948 * users can read the full output without reallocation. 949 * 950 * The second number is the number of data blocks we're writing. An 951 * application might be confined by multiple policies having data in 952 * the same key. 953 */ 954 memset(buf, 0, sizeof(bytes) + sizeof(blocks)); 955 out = buf + sizeof(bytes) + sizeof(blocks); 956 957 blocks = 0; 958 label_for_each_confined(i, label, profile) { 959 if (!profile->data) 960 continue; 961 962 data = rhashtable_lookup_fast(profile->data, &key, 963 profile->data->p); 964 965 if (data) { 966 if (out + sizeof(outle32) + data->size > buf + 967 buf_len) { 968 aa_put_label(label); 969 return -EINVAL; /* not enough space */ 970 } 971 outle32 = __cpu_to_le32(data->size); 972 memcpy(out, &outle32, sizeof(outle32)); 973 out += sizeof(outle32); 974 memcpy(out, data->data, data->size); 975 out += data->size; 976 blocks++; 977 } 978 } 979 aa_put_label(label); 980 981 outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); 982 memcpy(buf, &outle32, sizeof(outle32)); 983 outle32 = __cpu_to_le32(blocks); 984 memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32)); 985 986 return out - buf; 987 } 988 989 /** 990 * query_label - queries a label and writes permissions to buf 991 * @buf: the resulting permissions string is stored here (NOT NULL) 992 * @buf_len: size of buf 993 * @query: binary query string to match against the dfa 994 * @query_len: size of query 995 * @view_only: only compute for querier's view 996 * 997 * The buffers pointed to by buf and query may overlap. The query buffer is 998 * parsed before buf is written to. 999 * 1000 * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is 1001 * the name of the label, in the current namespace, that is to be queried and 1002 * DFA_STRING is a binary string to match against the label(s)'s DFA. 1003 * 1004 * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters 1005 * but must *not* be NUL terminated. 1006 * 1007 * Returns: number of characters written to buf or -errno on failure 1008 */ 1009 static ssize_t query_label(char *buf, size_t buf_len, 1010 char *query, size_t query_len, bool view_only) 1011 { 1012 struct aa_profile *profile; 1013 struct aa_label *label, *curr; 1014 char *label_name, *match_str; 1015 size_t label_name_len, match_len; 1016 struct aa_perms perms; 1017 struct label_it i; 1018 1019 if (!query_len) 1020 return -EINVAL; 1021 1022 label_name = query; 1023 label_name_len = strnlen(query, query_len); 1024 if (!label_name_len || label_name_len == query_len) 1025 return -EINVAL; 1026 1027 /** 1028 * The extra byte is to account for the null byte between the 1029 * profile name and dfa string. profile_name_len is greater 1030 * than zero and less than query_len, so a byte can be safely 1031 * added or subtracted. 1032 */ 1033 match_str = label_name + label_name_len + 1; 1034 match_len = query_len - label_name_len - 1; 1035 1036 curr = begin_current_label_crit_section(); 1037 label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); 1038 end_current_label_crit_section(curr); 1039 if (IS_ERR(label)) 1040 return PTR_ERR(label); 1041 1042 perms = allperms; 1043 if (view_only) { 1044 label_for_each_in_scope(i, labels_ns(label), label, profile) { 1045 profile_query_cb(profile, &perms, match_str, match_len); 1046 } 1047 } else { 1048 label_for_each(i, label, profile) { 1049 profile_query_cb(profile, &perms, match_str, match_len); 1050 } 1051 } 1052 aa_put_label(label); 1053 1054 return scnprintf(buf, buf_len, 1055 "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", 1056 perms.allow, perms.deny, perms.audit, perms.quiet); 1057 } 1058 1059 /* 1060 * Transaction based IO. 1061 * The file expects a write which triggers the transaction, and then 1062 * possibly a read(s) which collects the result - which is stored in a 1063 * file-local buffer. Once a new write is performed, a new set of results 1064 * are stored in the file-local buffer. 1065 */ 1066 struct multi_transaction { 1067 struct kref count; 1068 ssize_t size; 1069 char data[]; 1070 }; 1071 1072 #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction)) 1073 1074 static void multi_transaction_kref(struct kref *kref) 1075 { 1076 struct multi_transaction *t; 1077 1078 t = container_of(kref, struct multi_transaction, count); 1079 kfree(t); 1080 } 1081 1082 static struct multi_transaction * 1083 get_multi_transaction(struct multi_transaction *t) 1084 { 1085 if (t) 1086 kref_get(&(t->count)); 1087 1088 return t; 1089 } 1090 1091 static void put_multi_transaction(struct multi_transaction *t) 1092 { 1093 if (t) 1094 kref_put(&(t->count), multi_transaction_kref); 1095 } 1096 1097 /* does not increment @new's count */ 1098 static void multi_transaction_set(struct file *file, 1099 struct multi_transaction *new, size_t n) 1100 { 1101 struct multi_transaction *old; 1102 1103 AA_BUG(n > MULTI_TRANSACTION_LIMIT); 1104 1105 new->size = n; 1106 spin_lock(&file->f_lock); 1107 old = (struct multi_transaction *) file->private_data; 1108 file->private_data = new; 1109 spin_unlock(&file->f_lock); 1110 put_multi_transaction(old); 1111 } 1112 1113 static struct multi_transaction *multi_transaction_new(struct file *file, 1114 const char __user *buf, 1115 size_t size) 1116 { 1117 struct multi_transaction *t; 1118 1119 if (size > MULTI_TRANSACTION_LIMIT - 1) 1120 return ERR_PTR(-EFBIG); 1121 1122 t = kzalloc(PAGE_SIZE, GFP_KERNEL); 1123 if (!t) 1124 return ERR_PTR(-ENOMEM); 1125 kref_init(&t->count); 1126 if (copy_from_user(t->data, buf, size)) { 1127 put_multi_transaction(t); 1128 return ERR_PTR(-EFAULT); 1129 } 1130 1131 return t; 1132 } 1133 1134 static ssize_t multi_transaction_read(struct file *file, char __user *buf, 1135 size_t size, loff_t *pos) 1136 { 1137 struct multi_transaction *t; 1138 ssize_t ret; 1139 1140 spin_lock(&file->f_lock); 1141 t = get_multi_transaction(file->private_data); 1142 spin_unlock(&file->f_lock); 1143 1144 if (!t) 1145 return 0; 1146 1147 ret = simple_read_from_buffer(buf, size, pos, t->data, t->size); 1148 put_multi_transaction(t); 1149 1150 return ret; 1151 } 1152 1153 static int multi_transaction_release(struct inode *inode, struct file *file) 1154 { 1155 put_multi_transaction(file->private_data); 1156 1157 return 0; 1158 } 1159 1160 #define QUERY_CMD_LABEL "label\0" 1161 #define QUERY_CMD_LABEL_LEN 6 1162 #define QUERY_CMD_PROFILE "profile\0" 1163 #define QUERY_CMD_PROFILE_LEN 8 1164 #define QUERY_CMD_LABELALL "labelall\0" 1165 #define QUERY_CMD_LABELALL_LEN 9 1166 #define QUERY_CMD_DATA "data\0" 1167 #define QUERY_CMD_DATA_LEN 5 1168 1169 /** 1170 * aa_write_access - generic permissions and data query 1171 * @file: pointer to open apparmorfs/access file 1172 * @ubuf: user buffer containing the complete query string (NOT NULL) 1173 * @count: size of ubuf 1174 * @ppos: position in the file (MUST BE ZERO) 1175 * 1176 * Allows for one permissions or data query per open(), write(), and read() 1177 * sequence. The only queries currently supported are label-based queries for 1178 * permissions or data. 1179 * 1180 * For permissions queries, ubuf must begin with "label\0", followed by the 1181 * profile query specific format described in the query_label() function 1182 * documentation. 1183 * 1184 * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where 1185 * <LABEL> is the name of the security confinement context and <KEY> is the 1186 * name of the data to retrieve. 1187 * 1188 * Returns: number of bytes written or -errno on failure 1189 */ 1190 static ssize_t aa_write_access(struct file *file, const char __user *ubuf, 1191 size_t count, loff_t *ppos) 1192 { 1193 struct multi_transaction *t; 1194 ssize_t len; 1195 1196 if (*ppos) 1197 return -ESPIPE; 1198 1199 t = multi_transaction_new(file, ubuf, count); 1200 if (IS_ERR(t)) 1201 return PTR_ERR(t); 1202 1203 if (count > QUERY_CMD_PROFILE_LEN && 1204 !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { 1205 len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 1206 t->data + QUERY_CMD_PROFILE_LEN, 1207 count - QUERY_CMD_PROFILE_LEN, true); 1208 } else if (count > QUERY_CMD_LABEL_LEN && 1209 !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { 1210 len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 1211 t->data + QUERY_CMD_LABEL_LEN, 1212 count - QUERY_CMD_LABEL_LEN, true); 1213 } else if (count > QUERY_CMD_LABELALL_LEN && 1214 !memcmp(t->data, QUERY_CMD_LABELALL, 1215 QUERY_CMD_LABELALL_LEN)) { 1216 len = query_label(t->data, MULTI_TRANSACTION_LIMIT, 1217 t->data + QUERY_CMD_LABELALL_LEN, 1218 count - QUERY_CMD_LABELALL_LEN, false); 1219 } else if (count > QUERY_CMD_DATA_LEN && 1220 !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { 1221 len = query_data(t->data, MULTI_TRANSACTION_LIMIT, 1222 t->data + QUERY_CMD_DATA_LEN, 1223 count - QUERY_CMD_DATA_LEN); 1224 } else 1225 len = -EINVAL; 1226 1227 if (len < 0) { 1228 put_multi_transaction(t); 1229 return len; 1230 } 1231 1232 multi_transaction_set(file, t, len); 1233 1234 return count; 1235 } 1236 1237 static const struct file_operations aa_sfs_access = { 1238 .write = aa_write_access, 1239 .read = multi_transaction_read, 1240 .release = multi_transaction_release, 1241 .llseek = generic_file_llseek, 1242 }; 1243 1244 static int aa_sfs_seq_show(struct seq_file *seq, void *v) 1245 { 1246 struct aa_sfs_entry *fs_file = seq->private; 1247 1248 if (!fs_file) 1249 return 0; 1250 1251 switch (fs_file->v_type) { 1252 case AA_SFS_TYPE_BOOLEAN: 1253 seq_printf(seq, "%s\n", str_yes_no(fs_file->v.boolean)); 1254 break; 1255 case AA_SFS_TYPE_STRING: 1256 seq_printf(seq, "%s\n", fs_file->v.string); 1257 break; 1258 case AA_SFS_TYPE_U64: 1259 seq_printf(seq, "%#08lx\n", fs_file->v.u64); 1260 break; 1261 default: 1262 /* Ignore unprintable entry types. */ 1263 break; 1264 } 1265 1266 return 0; 1267 } 1268 1269 static int aa_sfs_seq_open(struct inode *inode, struct file *file) 1270 { 1271 return single_open(file, aa_sfs_seq_show, inode->i_private); 1272 } 1273 1274 const struct file_operations aa_sfs_seq_file_ops = { 1275 .owner = THIS_MODULE, 1276 .open = aa_sfs_seq_open, 1277 .read = seq_read, 1278 .llseek = seq_lseek, 1279 .release = single_release, 1280 }; 1281 1282 /* 1283 * profile based file operations 1284 * policy/profiles/XXXX/profiles/ * 1285 */ 1286 1287 #define SEQ_PROFILE_FOPS(NAME) \ 1288 static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\ 1289 { \ 1290 return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \ 1291 } \ 1292 \ 1293 static const struct file_operations seq_profile_ ##NAME ##_fops = { \ 1294 .owner = THIS_MODULE, \ 1295 .open = seq_profile_ ##NAME ##_open, \ 1296 .read = seq_read, \ 1297 .llseek = seq_lseek, \ 1298 .release = seq_profile_release, \ 1299 } \ 1300 1301 static int seq_profile_open(struct inode *inode, struct file *file, 1302 int (*show)(struct seq_file *, void *)) 1303 { 1304 struct aa_proxy *proxy = get_proxy_common_ref(inode->i_private); 1305 int error = single_open(file, show, proxy); 1306 1307 if (error) { 1308 file->private_data = NULL; 1309 aa_put_proxy(proxy); 1310 } 1311 1312 return error; 1313 } 1314 1315 static int seq_profile_release(struct inode *inode, struct file *file) 1316 { 1317 struct seq_file *seq = (struct seq_file *) file->private_data; 1318 if (seq) 1319 aa_put_proxy(seq->private); 1320 return single_release(inode, file); 1321 } 1322 1323 static int seq_profile_name_show(struct seq_file *seq, void *v) 1324 { 1325 struct aa_proxy *proxy = seq->private; 1326 struct aa_label *label = aa_get_label_rcu(&proxy->label); 1327 struct aa_profile *profile = labels_profile(label); 1328 seq_printf(seq, "%s\n", profile->base.name); 1329 aa_put_label(label); 1330 1331 return 0; 1332 } 1333 1334 static int seq_profile_mode_show(struct seq_file *seq, void *v) 1335 { 1336 struct aa_proxy *proxy = seq->private; 1337 struct aa_label *label = aa_get_label_rcu(&proxy->label); 1338 struct aa_profile *profile = labels_profile(label); 1339 seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); 1340 aa_put_label(label); 1341 1342 return 0; 1343 } 1344 1345 static int seq_profile_attach_show(struct seq_file *seq, void *v) 1346 { 1347 struct aa_proxy *proxy = seq->private; 1348 struct aa_label *label = aa_get_label_rcu(&proxy->label); 1349 struct aa_profile *profile = labels_profile(label); 1350 if (profile->attach.xmatch_str) 1351 seq_printf(seq, "%s\n", profile->attach.xmatch_str); 1352 else if (profile->attach.xmatch->dfa) 1353 seq_puts(seq, "<unknown>\n"); 1354 else 1355 seq_printf(seq, "%s\n", profile->base.name); 1356 aa_put_label(label); 1357 1358 return 0; 1359 } 1360 1361 static int seq_profile_hash_show(struct seq_file *seq, void *v) 1362 { 1363 struct aa_proxy *proxy = seq->private; 1364 struct aa_label *label = aa_get_label_rcu(&proxy->label); 1365 struct aa_profile *profile = labels_profile(label); 1366 unsigned int i, size = aa_hash_size(); 1367 1368 if (profile->hash) { 1369 for (i = 0; i < size; i++) 1370 seq_printf(seq, "%.2x", profile->hash[i]); 1371 seq_putc(seq, '\n'); 1372 } 1373 aa_put_label(label); 1374 1375 return 0; 1376 } 1377 1378 SEQ_PROFILE_FOPS(name); 1379 SEQ_PROFILE_FOPS(mode); 1380 SEQ_PROFILE_FOPS(attach); 1381 SEQ_PROFILE_FOPS(hash); 1382 1383 /* 1384 * namespace based files 1385 * several root files and 1386 * policy/ * 1387 */ 1388 1389 #define SEQ_NS_FOPS(NAME) \ 1390 static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \ 1391 { \ 1392 return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \ 1393 } \ 1394 \ 1395 static const struct file_operations seq_ns_ ##NAME ##_fops = { \ 1396 .owner = THIS_MODULE, \ 1397 .open = seq_ns_ ##NAME ##_open, \ 1398 .read = seq_read, \ 1399 .llseek = seq_lseek, \ 1400 .release = single_release, \ 1401 } \ 1402 1403 static int seq_ns_stacked_show(struct seq_file *seq, void *v) 1404 { 1405 struct aa_label *label; 1406 1407 label = begin_current_label_crit_section(); 1408 seq_printf(seq, "%s\n", str_yes_no(label->size > 1)); 1409 end_current_label_crit_section(label); 1410 1411 return 0; 1412 } 1413 1414 static int seq_ns_nsstacked_show(struct seq_file *seq, void *v) 1415 { 1416 struct aa_label *label; 1417 struct aa_profile *profile; 1418 struct label_it it; 1419 int count = 1; 1420 1421 label = begin_current_label_crit_section(); 1422 1423 if (label->size > 1) { 1424 label_for_each(it, label, profile) 1425 if (profile->ns != labels_ns(label)) { 1426 count++; 1427 break; 1428 } 1429 } 1430 1431 seq_printf(seq, "%s\n", str_yes_no(count > 1)); 1432 end_current_label_crit_section(label); 1433 1434 return 0; 1435 } 1436 1437 static int seq_ns_level_show(struct seq_file *seq, void *v) 1438 { 1439 struct aa_label *label; 1440 1441 label = begin_current_label_crit_section(); 1442 seq_printf(seq, "%d\n", labels_ns(label)->level); 1443 end_current_label_crit_section(label); 1444 1445 return 0; 1446 } 1447 1448 static int seq_ns_name_show(struct seq_file *seq, void *v) 1449 { 1450 struct aa_label *label = begin_current_label_crit_section(); 1451 seq_printf(seq, "%s\n", labels_ns(label)->base.name); 1452 end_current_label_crit_section(label); 1453 1454 return 0; 1455 } 1456 1457 SEQ_NS_FOPS(stacked); 1458 SEQ_NS_FOPS(nsstacked); 1459 SEQ_NS_FOPS(level); 1460 SEQ_NS_FOPS(name); 1461 1462 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 1463 static int seq_ns_compress_min_show(struct seq_file *seq, void *v) 1464 { 1465 seq_printf(seq, "%d\n", AA_MIN_CLEVEL); 1466 return 0; 1467 } 1468 1469 static int seq_ns_compress_max_show(struct seq_file *seq, void *v) 1470 { 1471 seq_printf(seq, "%d\n", AA_MAX_CLEVEL); 1472 return 0; 1473 } 1474 1475 SEQ_NS_FOPS(compress_min); 1476 SEQ_NS_FOPS(compress_max); 1477 1478 1479 /* policy/raw_data/ * file ops */ 1480 #define SEQ_RAWDATA_FOPS(NAME) \ 1481 static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\ 1482 { \ 1483 return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \ 1484 } \ 1485 \ 1486 static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \ 1487 .owner = THIS_MODULE, \ 1488 .open = seq_rawdata_ ##NAME ##_open, \ 1489 .read = seq_read, \ 1490 .llseek = seq_lseek, \ 1491 .release = seq_rawdata_release, \ 1492 } \ 1493 1494 static int seq_rawdata_open(struct inode *inode, struct file *file, 1495 int (*show)(struct seq_file *, void *)) 1496 { 1497 struct aa_loaddata *data = get_loaddata_common_ref(inode->i_private); 1498 int error; 1499 1500 if (!data) 1501 return -ENOENT; 1502 1503 error = single_open(file, show, data); 1504 if (error) { 1505 AA_BUG(file->private_data && 1506 ((struct seq_file *)file->private_data)->private); 1507 aa_put_i_loaddata(data); 1508 } 1509 1510 return error; 1511 } 1512 1513 static int seq_rawdata_release(struct inode *inode, struct file *file) 1514 { 1515 struct seq_file *seq = (struct seq_file *) file->private_data; 1516 1517 if (seq) 1518 aa_put_i_loaddata(seq->private); 1519 1520 return single_release(inode, file); 1521 } 1522 1523 static int seq_rawdata_abi_show(struct seq_file *seq, void *v) 1524 { 1525 struct aa_loaddata *data = seq->private; 1526 1527 seq_printf(seq, "v%d\n", data->abi); 1528 1529 return 0; 1530 } 1531 1532 static int seq_rawdata_revision_show(struct seq_file *seq, void *v) 1533 { 1534 struct aa_loaddata *data = seq->private; 1535 1536 seq_printf(seq, "%ld\n", data->revision); 1537 1538 return 0; 1539 } 1540 1541 static int seq_rawdata_hash_show(struct seq_file *seq, void *v) 1542 { 1543 struct aa_loaddata *data = seq->private; 1544 unsigned int i, size = aa_hash_size(); 1545 1546 if (data->hash) { 1547 for (i = 0; i < size; i++) 1548 seq_printf(seq, "%.2x", data->hash[i]); 1549 seq_putc(seq, '\n'); 1550 } 1551 1552 return 0; 1553 } 1554 1555 static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v) 1556 { 1557 struct aa_loaddata *data = seq->private; 1558 1559 seq_printf(seq, "%zu\n", data->compressed_size); 1560 1561 return 0; 1562 } 1563 1564 SEQ_RAWDATA_FOPS(abi); 1565 SEQ_RAWDATA_FOPS(revision); 1566 SEQ_RAWDATA_FOPS(hash); 1567 SEQ_RAWDATA_FOPS(compressed_size); 1568 1569 static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, 1570 loff_t *ppos) 1571 { 1572 struct rawdata_f_data *private = file->private_data; 1573 1574 return simple_read_from_buffer(buf, size, ppos, 1575 private->data, 1576 private->loaddata->size); 1577 } 1578 1579 static int rawdata_release(struct inode *inode, struct file *file) 1580 { 1581 rawdata_f_data_free(file->private_data); 1582 1583 return 0; 1584 } 1585 1586 static int rawdata_open(struct inode *inode, struct file *file) 1587 { 1588 int error; 1589 struct aa_loaddata *loaddata; 1590 struct rawdata_f_data *private; 1591 1592 if (!aa_current_policy_view_capable(NULL)) 1593 return -EACCES; 1594 1595 loaddata = get_loaddata_common_ref(inode->i_private); 1596 if (!loaddata) 1597 return -ENOENT; 1598 1599 private = rawdata_f_data_alloc(loaddata->size); 1600 if (IS_ERR(private)) { 1601 error = PTR_ERR(private); 1602 goto fail_private_alloc; 1603 } 1604 1605 private->loaddata = loaddata; 1606 1607 error = decompress_zstd(loaddata->data, loaddata->compressed_size, 1608 private->data, loaddata->size); 1609 if (error) 1610 goto fail_decompress; 1611 1612 file->private_data = private; 1613 return 0; 1614 1615 fail_decompress: 1616 rawdata_f_data_free(private); 1617 return error; 1618 1619 fail_private_alloc: 1620 aa_put_i_loaddata(loaddata); 1621 return error; 1622 } 1623 1624 static const struct file_operations rawdata_fops = { 1625 .open = rawdata_open, 1626 .read = rawdata_read, 1627 .llseek = generic_file_llseek, 1628 .release = rawdata_release, 1629 }; 1630 1631 static void remove_rawdata_dents(struct aa_loaddata *rawdata) 1632 { 1633 int i; 1634 1635 for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) { 1636 if (!IS_ERR_OR_NULL(rawdata->dents[i])) { 1637 aafs_remove(rawdata->dents[i]); 1638 rawdata->dents[i] = NULL; 1639 } 1640 } 1641 } 1642 1643 void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata) 1644 { 1645 AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock)); 1646 1647 if (rawdata->ns) { 1648 remove_rawdata_dents(rawdata); 1649 list_del_init(&rawdata->list); 1650 aa_put_ns(rawdata->ns); 1651 rawdata->ns = NULL; 1652 } 1653 } 1654 1655 int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata) 1656 { 1657 struct dentry *dent, *dir; 1658 1659 AA_BUG(!ns); 1660 AA_BUG(!rawdata); 1661 AA_BUG(!mutex_is_locked(&ns->lock)); 1662 AA_BUG(!ns_subdata_dir(ns)); 1663 1664 /* 1665 * just use ns revision dir was originally created at. This is 1666 * under ns->lock and if load is successful revision will be 1667 * bumped and is guaranteed to be unique 1668 */ 1669 rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision); 1670 if (!rawdata->name) 1671 return -ENOMEM; 1672 1673 dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns)); 1674 if (IS_ERR(dir)) 1675 /* ->name freed when rawdata freed */ 1676 return PTR_ERR(dir); 1677 rawdata->dents[AAFS_LOADDATA_DIR] = dir; 1678 1679 dent = aafs_create_file("abi", S_IFREG | 0444, dir, &rawdata->count, 1680 &seq_rawdata_abi_fops); 1681 if (IS_ERR(dent)) 1682 goto fail; 1683 rawdata->dents[AAFS_LOADDATA_ABI] = dent; 1684 1685 dent = aafs_create_file("revision", S_IFREG | 0444, dir, 1686 &rawdata->count, 1687 &seq_rawdata_revision_fops); 1688 if (IS_ERR(dent)) 1689 goto fail; 1690 rawdata->dents[AAFS_LOADDATA_REVISION] = dent; 1691 1692 if (aa_g_hash_policy) { 1693 dent = aafs_create_file("sha256", S_IFREG | 0444, dir, 1694 &rawdata->count, 1695 &seq_rawdata_hash_fops); 1696 if (IS_ERR(dent)) 1697 goto fail; 1698 rawdata->dents[AAFS_LOADDATA_HASH] = dent; 1699 } 1700 1701 dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir, 1702 &rawdata->count, 1703 &seq_rawdata_compressed_size_fops); 1704 if (IS_ERR(dent)) 1705 goto fail; 1706 rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent; 1707 1708 dent = aafs_create_file("raw_data", S_IFREG | 0444, dir, 1709 &rawdata->count, &rawdata_fops); 1710 if (IS_ERR(dent)) 1711 goto fail; 1712 rawdata->dents[AAFS_LOADDATA_DATA] = dent; 1713 d_inode(dent)->i_size = rawdata->size; 1714 1715 rawdata->ns = aa_get_ns(ns); 1716 list_add(&rawdata->list, &ns->rawdata_list); 1717 1718 return 0; 1719 1720 fail: 1721 remove_rawdata_dents(rawdata); 1722 return PTR_ERR(dent); 1723 } 1724 #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */ 1725 1726 1727 /** fns to setup dynamic per profile/namespace files **/ 1728 1729 /* 1730 * 1731 * Requires: @profile->ns->lock held 1732 */ 1733 void __aafs_profile_rmdir(struct aa_profile *profile) 1734 { 1735 struct aa_profile *child; 1736 int i; 1737 1738 if (!profile) 1739 return; 1740 1741 list_for_each_entry(child, &profile->base.profiles, base.list) 1742 __aafs_profile_rmdir(child); 1743 1744 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { 1745 if (!profile->dents[i]) 1746 continue; 1747 1748 aafs_remove(profile->dents[i]); 1749 profile->dents[i] = NULL; 1750 } 1751 } 1752 1753 /* 1754 * 1755 * Requires: @old->ns->lock held 1756 */ 1757 void __aafs_profile_migrate_dents(struct aa_profile *old, 1758 struct aa_profile *new) 1759 { 1760 int i; 1761 1762 AA_BUG(!old); 1763 AA_BUG(!new); 1764 AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); 1765 1766 for (i = 0; i < AAFS_PROF_SIZEOF; i++) { 1767 new->dents[i] = old->dents[i]; 1768 if (new->dents[i]) { 1769 struct inode *inode = d_inode(new->dents[i]); 1770 1771 inode_set_mtime_to_ts(inode, 1772 inode_set_ctime_current(inode)); 1773 } 1774 old->dents[i] = NULL; 1775 } 1776 } 1777 1778 static struct dentry *create_profile_file(struct dentry *dir, const char *name, 1779 struct aa_profile *profile, 1780 const struct file_operations *fops) 1781 { 1782 return aafs_create_file(name, S_IFREG | 0444, dir, &profile->label.proxy->count, fops); 1783 } 1784 1785 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 1786 static int profile_depth(struct aa_profile *profile) 1787 { 1788 int depth = 0; 1789 1790 rcu_read_lock(); 1791 for (depth = 0; profile; profile = rcu_access_pointer(profile->parent)) 1792 depth++; 1793 rcu_read_unlock(); 1794 1795 return depth; 1796 } 1797 1798 static char *gen_symlink_name(int depth, const char *dirname, const char *fname) 1799 { 1800 char *buffer, *s; 1801 int error; 1802 const char *path = "../../"; 1803 size_t path_len = strlen(path); 1804 int size; 1805 1806 /* Extra 11 bytes: "raw_data" (9) + two slashes "//" (2) */ 1807 size = depth * path_len + strlen(dirname) + strlen(fname) + 11; 1808 s = buffer = kmalloc(size, GFP_KERNEL); 1809 if (!buffer) 1810 return ERR_PTR(-ENOMEM); 1811 1812 for (; depth > 0; depth--) { 1813 memcpy(s, path, path_len); 1814 s += path_len; 1815 size -= path_len; 1816 } 1817 1818 error = snprintf(s, size, "raw_data/%s/%s", dirname, fname); 1819 if (error >= size || error < 0) { 1820 kfree(buffer); 1821 return ERR_PTR(-ENAMETOOLONG); 1822 } 1823 1824 return buffer; 1825 } 1826 1827 static const char *rawdata_get_link_base(struct dentry *dentry, 1828 struct inode *inode, 1829 struct delayed_call *done, 1830 const char *name) 1831 { 1832 struct aa_common_ref *ref = inode->i_private; 1833 struct aa_proxy *proxy = container_of(ref, struct aa_proxy, count); 1834 struct aa_label *label; 1835 struct aa_profile *profile; 1836 char *target; 1837 int depth; 1838 1839 if (!dentry) 1840 return ERR_PTR(-ECHILD); 1841 1842 label = aa_get_label_rcu(&proxy->label); 1843 profile = labels_profile(label); 1844 1845 /* rawdata can be null when aa_g_export_binary is unset during 1846 * runtime and a profile is replaced 1847 */ 1848 if (!profile->rawdata) { 1849 aa_put_label(label); 1850 return ERR_PTR(-ENOENT); 1851 } 1852 1853 depth = profile_depth(profile); 1854 target = gen_symlink_name(depth, profile->rawdata->name, name); 1855 aa_put_label(label); 1856 1857 if (IS_ERR(target)) 1858 return target; 1859 1860 set_delayed_call(done, kfree_link, target); 1861 1862 return target; 1863 } 1864 1865 static const char *rawdata_get_link_sha256(struct dentry *dentry, 1866 struct inode *inode, 1867 struct delayed_call *done) 1868 { 1869 return rawdata_get_link_base(dentry, inode, done, "sha256"); 1870 } 1871 1872 static const char *rawdata_get_link_abi(struct dentry *dentry, 1873 struct inode *inode, 1874 struct delayed_call *done) 1875 { 1876 return rawdata_get_link_base(dentry, inode, done, "abi"); 1877 } 1878 1879 static const char *rawdata_get_link_data(struct dentry *dentry, 1880 struct inode *inode, 1881 struct delayed_call *done) 1882 { 1883 return rawdata_get_link_base(dentry, inode, done, "raw_data"); 1884 } 1885 1886 static const struct inode_operations rawdata_link_sha256_iops = { 1887 .get_link = rawdata_get_link_sha256, 1888 }; 1889 1890 static const struct inode_operations rawdata_link_abi_iops = { 1891 .get_link = rawdata_get_link_abi, 1892 }; 1893 static const struct inode_operations rawdata_link_data_iops = { 1894 .get_link = rawdata_get_link_data, 1895 }; 1896 1897 /* 1898 * Requires: @profile->ns->lock held 1899 */ 1900 void __aa_remove_rawdata_symlink_dents(struct aa_profile *profile) 1901 { 1902 aafs_remove(profile->dents[AAFS_PROF_RAW_HASH]); 1903 profile->dents[AAFS_PROF_RAW_HASH] = NULL; 1904 aafs_remove(profile->dents[AAFS_PROF_RAW_ABI]); 1905 profile->dents[AAFS_PROF_RAW_ABI] = NULL; 1906 aafs_remove(profile->dents[AAFS_PROF_RAW_DATA]); 1907 profile->dents[AAFS_PROF_RAW_DATA] = NULL; 1908 } 1909 1910 static inline int create_symlink_dent(struct aa_profile *profile, 1911 const char *name, 1912 enum aafs_prof_type type, 1913 const struct inode_operations *iops) 1914 { 1915 struct dentry *dent = NULL; 1916 struct dentry *dir = prof_dir(profile); 1917 1918 if (profile->dents[type]) 1919 return 0; 1920 1921 dent = aafs_create(name, S_IFLNK | 0444, dir, 1922 &profile->label.proxy->count, NULL, NULL, iops); 1923 if (IS_ERR(dent)) 1924 return PTR_ERR(dent); 1925 1926 profile->dents[type] = dent; 1927 return 0; 1928 } 1929 1930 /* 1931 * Requires: @profile->ns->lock held 1932 */ 1933 int __aa_create_rawdata_symlink_dents(struct aa_profile *profile) 1934 { 1935 int error; 1936 1937 if (!profile || 1938 (profile->dents[AAFS_PROF_RAW_HASH] && 1939 profile->dents[AAFS_PROF_RAW_ABI] && 1940 profile->dents[AAFS_PROF_RAW_DATA])) 1941 return 0; 1942 1943 if (!profile->rawdata) 1944 return 0; 1945 1946 if (aa_g_hash_policy) { 1947 error = create_symlink_dent(profile, "raw_sha256", 1948 AAFS_PROF_RAW_HASH, 1949 &rawdata_link_sha256_iops); 1950 if (error) 1951 return error; 1952 } 1953 1954 error = create_symlink_dent(profile, "raw_abi", 1955 AAFS_PROF_RAW_ABI, 1956 &rawdata_link_abi_iops); 1957 if (error) 1958 return error; 1959 1960 1961 error = create_symlink_dent(profile, "raw_data", 1962 AAFS_PROF_RAW_DATA, 1963 &rawdata_link_data_iops); 1964 if (error) 1965 return error; 1966 1967 return 0; 1968 } 1969 1970 #endif /* CONFIG_SECURITY_APPARMOR_EXPORT_BINARY */ 1971 1972 /* 1973 * Requires: @profile->ns->lock held 1974 */ 1975 int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) 1976 { 1977 struct aa_profile *child; 1978 struct dentry *dent = NULL, *dir; 1979 int error; 1980 1981 AA_BUG(!profile); 1982 AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); 1983 1984 if (!parent) { 1985 struct aa_profile *p; 1986 p = aa_deref_parent(profile); 1987 dent = prof_dir(p); 1988 if (!dent) { 1989 error = -ENOENT; 1990 goto fail2; 1991 } 1992 /* adding to parent that previously didn't have children */ 1993 dent = aafs_create_dir("profiles", dent); 1994 if (IS_ERR(dent)) 1995 goto fail; 1996 prof_child_dir(p) = parent = dent; 1997 } 1998 1999 if (!profile->dirname) { 2000 int len, id_len; 2001 len = mangle_name(profile->base.name, NULL); 2002 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); 2003 2004 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); 2005 if (!profile->dirname) { 2006 error = -ENOMEM; 2007 goto fail2; 2008 } 2009 2010 mangle_name(profile->base.name, profile->dirname); 2011 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); 2012 } 2013 2014 dent = aafs_create_dir(profile->dirname, parent); 2015 if (IS_ERR(dent)) 2016 goto fail; 2017 prof_dir(profile) = dir = dent; 2018 2019 dent = create_profile_file(dir, "name", profile, 2020 &seq_profile_name_fops); 2021 if (IS_ERR(dent)) 2022 goto fail; 2023 profile->dents[AAFS_PROF_NAME] = dent; 2024 2025 dent = create_profile_file(dir, "mode", profile, 2026 &seq_profile_mode_fops); 2027 if (IS_ERR(dent)) 2028 goto fail; 2029 profile->dents[AAFS_PROF_MODE] = dent; 2030 2031 dent = create_profile_file(dir, "attach", profile, 2032 &seq_profile_attach_fops); 2033 if (IS_ERR(dent)) 2034 goto fail; 2035 profile->dents[AAFS_PROF_ATTACH] = dent; 2036 2037 if (profile->hash) { 2038 dent = create_profile_file(dir, "sha256", profile, 2039 &seq_profile_hash_fops); 2040 if (IS_ERR(dent)) 2041 goto fail; 2042 profile->dents[AAFS_PROF_HASH] = dent; 2043 } 2044 2045 error = __aa_create_rawdata_symlink_dents(profile); 2046 if (error) 2047 goto fail2; 2048 2049 list_for_each_entry(child, &profile->base.profiles, base.list) { 2050 error = __aafs_profile_mkdir(child, prof_child_dir(profile)); 2051 if (error) 2052 goto fail2; 2053 } 2054 2055 return 0; 2056 2057 fail: 2058 error = PTR_ERR(dent); 2059 2060 fail2: 2061 __aafs_profile_rmdir(profile); 2062 2063 return error; 2064 } 2065 2066 static struct dentry *ns_mkdir_op(struct mnt_idmap *idmap, struct inode *dir, 2067 struct dentry *dentry, umode_t mode) 2068 { 2069 struct aa_ns *ns, *parent; 2070 /* TODO: improve permission check */ 2071 struct aa_label *label; 2072 int error; 2073 2074 label = begin_current_label_crit_section(); 2075 error = aa_may_manage_policy(current_cred(), label, NULL, NULL, 2076 AA_MAY_LOAD_POLICY); 2077 end_current_label_crit_section(label); 2078 if (error) 2079 return ERR_PTR(error); 2080 2081 parent = get_ns_common_ref(dir->i_private); 2082 AA_BUG(d_inode(ns_subns_dir(parent)) != dir); 2083 2084 /* we have to unlock and then relock to get locking order right 2085 * for pin_fs 2086 */ 2087 inode_unlock(dir); 2088 error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count); 2089 mutex_lock_nested(&parent->lock, parent->level); 2090 inode_lock_nested(dir, I_MUTEX_PARENT); 2091 if (error) 2092 goto out; 2093 2094 error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, 2095 NULL, NULL, NULL); 2096 if (error) 2097 goto out_pin; 2098 2099 ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name), 2100 dentry); 2101 if (IS_ERR(ns)) { 2102 error = PTR_ERR(ns); 2103 ns = NULL; 2104 } 2105 2106 aa_put_ns(ns); /* list ref remains */ 2107 out_pin: 2108 if (error) 2109 simple_release_fs(&aafs_mnt, &aafs_count); 2110 out: 2111 mutex_unlock(&parent->lock); 2112 aa_put_ns(parent); 2113 2114 return error ? ERR_PTR(error) : NULL; 2115 } 2116 2117 static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) 2118 { 2119 struct aa_ns *ns, *parent; 2120 /* TODO: improve permission check */ 2121 struct aa_label *label; 2122 int error; 2123 2124 label = begin_current_label_crit_section(); 2125 error = aa_may_manage_policy(current_cred(), label, NULL, NULL, 2126 AA_MAY_LOAD_POLICY); 2127 end_current_label_crit_section(label); 2128 if (error) 2129 return error; 2130 2131 parent = get_ns_common_ref(dir->i_private); 2132 /* rmdir calls the generic securityfs functions to remove files 2133 * from the apparmor dir. It is up to the apparmor ns locking 2134 * to avoid races. 2135 */ 2136 inode_unlock(dir); 2137 inode_unlock(dentry->d_inode); 2138 2139 mutex_lock_nested(&parent->lock, parent->level); 2140 ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, 2141 dentry->d_name.len)); 2142 if (!ns) { 2143 error = -ENOENT; 2144 goto out; 2145 } 2146 AA_BUG(ns_dir(ns) != dentry); 2147 2148 __aa_remove_ns(ns); 2149 aa_put_ns(ns); 2150 2151 out: 2152 mutex_unlock(&parent->lock); 2153 inode_lock_nested(dir, I_MUTEX_PARENT); 2154 inode_lock(dentry->d_inode); 2155 aa_put_ns(parent); 2156 2157 return error; 2158 } 2159 2160 static const struct inode_operations ns_dir_inode_operations = { 2161 .lookup = simple_lookup, 2162 .mkdir = ns_mkdir_op, 2163 .rmdir = ns_rmdir_op, 2164 }; 2165 2166 static void __aa_fs_list_remove_rawdata(struct aa_ns *ns) 2167 { 2168 struct aa_loaddata *ent, *tmp; 2169 2170 AA_BUG(!mutex_is_locked(&ns->lock)); 2171 2172 list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list) 2173 __aa_fs_remove_rawdata(ent); 2174 } 2175 2176 /* 2177 * 2178 * Requires: @ns->lock held 2179 */ 2180 void __aafs_ns_rmdir(struct aa_ns *ns) 2181 { 2182 struct aa_ns *sub; 2183 struct aa_profile *child; 2184 int i; 2185 2186 if (!ns) 2187 return; 2188 AA_BUG(!mutex_is_locked(&ns->lock)); 2189 2190 list_for_each_entry(child, &ns->base.profiles, base.list) 2191 __aafs_profile_rmdir(child); 2192 2193 list_for_each_entry(sub, &ns->sub_ns, base.list) { 2194 mutex_lock_nested(&sub->lock, sub->level); 2195 __aafs_ns_rmdir(sub); 2196 mutex_unlock(&sub->lock); 2197 } 2198 2199 __aa_fs_list_remove_rawdata(ns); 2200 2201 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { 2202 aafs_remove(ns->dents[i]); 2203 ns->dents[i] = NULL; 2204 } 2205 } 2206 2207 /* assumes cleanup in caller */ 2208 static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) 2209 { 2210 struct dentry *dent; 2211 2212 AA_BUG(!ns); 2213 AA_BUG(!dir); 2214 2215 dent = aafs_create_dir("profiles", dir); 2216 if (IS_ERR(dent)) 2217 return PTR_ERR(dent); 2218 ns_subprofs_dir(ns) = dent; 2219 2220 dent = aafs_create_dir("raw_data", dir); 2221 if (IS_ERR(dent)) 2222 return PTR_ERR(dent); 2223 ns_subdata_dir(ns) = dent; 2224 2225 dent = aafs_create_file("revision", 0444, dir, 2226 &ns->unconfined->label.count, 2227 &aa_fs_ns_revision_fops); 2228 if (IS_ERR(dent)) 2229 return PTR_ERR(dent); 2230 ns_subrevision(ns) = dent; 2231 2232 dent = aafs_create_file(".load", 0640, dir, 2233 &ns->unconfined->label.count, 2234 &aa_fs_profile_load); 2235 if (IS_ERR(dent)) 2236 return PTR_ERR(dent); 2237 ns_subload(ns) = dent; 2238 2239 dent = aafs_create_file(".replace", 0640, dir, 2240 &ns->unconfined->label.count, 2241 &aa_fs_profile_replace); 2242 if (IS_ERR(dent)) 2243 return PTR_ERR(dent); 2244 ns_subreplace(ns) = dent; 2245 2246 dent = aafs_create_file(".remove", 0640, dir, 2247 &ns->unconfined->label.count, 2248 &aa_fs_profile_remove); 2249 if (IS_ERR(dent)) 2250 return PTR_ERR(dent); 2251 ns_subremove(ns) = dent; 2252 2253 /* use create_dentry so we can supply private data */ 2254 dent = aafs_create("namespaces", S_IFDIR | 0755, dir, 2255 &ns->unconfined->label.count, 2256 NULL, NULL, &ns_dir_inode_operations); 2257 if (IS_ERR(dent)) 2258 return PTR_ERR(dent); 2259 ns_subns_dir(ns) = dent; 2260 2261 return 0; 2262 } 2263 2264 /* 2265 * Requires: @ns->lock held 2266 */ 2267 int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, 2268 struct dentry *dent) 2269 { 2270 struct aa_ns *sub; 2271 struct aa_profile *child; 2272 struct dentry *dir; 2273 int error; 2274 2275 AA_BUG(!ns); 2276 AA_BUG(!parent); 2277 AA_BUG(!mutex_is_locked(&ns->lock)); 2278 2279 if (!name) 2280 name = ns->base.name; 2281 2282 if (!dent) { 2283 /* create ns dir if it doesn't already exist */ 2284 dent = aafs_create_dir(name, parent); 2285 if (IS_ERR(dent)) 2286 goto fail; 2287 } else 2288 dget(dent); 2289 ns_dir(ns) = dir = dent; 2290 error = __aafs_ns_mkdir_entries(ns, dir); 2291 if (error) 2292 goto fail2; 2293 2294 /* profiles */ 2295 list_for_each_entry(child, &ns->base.profiles, base.list) { 2296 error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns)); 2297 if (error) 2298 goto fail2; 2299 } 2300 2301 /* subnamespaces */ 2302 list_for_each_entry(sub, &ns->sub_ns, base.list) { 2303 mutex_lock_nested(&sub->lock, sub->level); 2304 error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); 2305 mutex_unlock(&sub->lock); 2306 if (error) 2307 goto fail2; 2308 } 2309 2310 return 0; 2311 2312 fail: 2313 error = PTR_ERR(dent); 2314 2315 fail2: 2316 __aafs_ns_rmdir(ns); 2317 2318 return error; 2319 } 2320 2321 /** 2322 * __next_ns - find the next namespace to list 2323 * @root: root namespace to stop search at (NOT NULL) 2324 * @ns: current ns position (NOT NULL) 2325 * 2326 * Find the next namespace from @ns under @root and handle all locking needed 2327 * while switching current namespace. 2328 * 2329 * Returns: next namespace or NULL if at last namespace under @root 2330 * Requires: ns->parent->lock to be held 2331 * NOTE: will not unlock root->lock 2332 */ 2333 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) 2334 { 2335 struct aa_ns *parent, *next; 2336 2337 AA_BUG(!root); 2338 AA_BUG(!ns); 2339 AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); 2340 2341 /* is next namespace a child */ 2342 if (!list_empty(&ns->sub_ns)) { 2343 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); 2344 mutex_lock_nested(&next->lock, next->level); 2345 return next; 2346 } 2347 2348 /* check if the next ns is a sibling, parent, gp, .. */ 2349 parent = ns->parent; 2350 while (ns != root) { 2351 mutex_unlock(&ns->lock); 2352 next = list_next_entry(ns, base.list); 2353 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { 2354 mutex_lock_nested(&next->lock, next->level); 2355 return next; 2356 } 2357 ns = parent; 2358 parent = parent->parent; 2359 } 2360 2361 return NULL; 2362 } 2363 2364 /** 2365 * __first_profile - find the first profile in a namespace 2366 * @root: namespace that is root of profiles being displayed (NOT NULL) 2367 * @ns: namespace to start in (NOT NULL) 2368 * 2369 * Returns: unrefcounted profile or NULL if no profile 2370 * Requires: profile->ns.lock to be held 2371 */ 2372 static struct aa_profile *__first_profile(struct aa_ns *root, 2373 struct aa_ns *ns) 2374 { 2375 AA_BUG(!root); 2376 AA_BUG(ns && !mutex_is_locked(&ns->lock)); 2377 2378 for (; ns; ns = __next_ns(root, ns)) { 2379 if (!list_empty(&ns->base.profiles)) 2380 return list_first_entry(&ns->base.profiles, 2381 struct aa_profile, base.list); 2382 } 2383 return NULL; 2384 } 2385 2386 /** 2387 * __next_profile - step to the next profile in a profile tree 2388 * @p: current profile in tree (NOT NULL) 2389 * 2390 * Perform a depth first traversal on the profile tree in a namespace 2391 * 2392 * Returns: next profile or NULL if done 2393 * Requires: profile->ns.lock to be held 2394 */ 2395 static struct aa_profile *__next_profile(struct aa_profile *p) 2396 { 2397 struct aa_profile *parent; 2398 struct aa_ns *ns = p->ns; 2399 2400 AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); 2401 2402 /* is next profile a child */ 2403 if (!list_empty(&p->base.profiles)) 2404 return list_first_entry(&p->base.profiles, typeof(*p), 2405 base.list); 2406 2407 /* is next profile a sibling, parent sibling, gp, sibling, .. */ 2408 parent = rcu_dereference_protected(p->parent, 2409 mutex_is_locked(&p->ns->lock)); 2410 while (parent) { 2411 p = list_next_entry(p, base.list); 2412 if (!list_entry_is_head(p, &parent->base.profiles, base.list)) 2413 return p; 2414 p = parent; 2415 parent = rcu_dereference_protected(parent->parent, 2416 mutex_is_locked(&parent->ns->lock)); 2417 } 2418 2419 /* is next another profile in the namespace */ 2420 p = list_next_entry(p, base.list); 2421 if (!list_entry_is_head(p, &ns->base.profiles, base.list)) 2422 return p; 2423 2424 return NULL; 2425 } 2426 2427 /** 2428 * next_profile - step to the next profile in where ever it may be 2429 * @root: root namespace (NOT NULL) 2430 * @profile: current profile (NOT NULL) 2431 * 2432 * Returns: next profile or NULL if there isn't one 2433 */ 2434 static struct aa_profile *next_profile(struct aa_ns *root, 2435 struct aa_profile *profile) 2436 { 2437 struct aa_profile *next = __next_profile(profile); 2438 if (next) 2439 return next; 2440 2441 /* finished all profiles in namespace move to next namespace */ 2442 return __first_profile(root, __next_ns(root, profile->ns)); 2443 } 2444 2445 /** 2446 * p_start - start a depth first traversal of profile tree 2447 * @f: seq_file to fill 2448 * @pos: current position 2449 * 2450 * Returns: first profile under current namespace or NULL if none found 2451 * 2452 * acquires first ns->lock 2453 */ 2454 static void *p_start(struct seq_file *f, loff_t *pos) 2455 { 2456 struct aa_profile *profile = NULL; 2457 struct aa_ns *root = aa_get_current_ns(); 2458 loff_t l = *pos; 2459 f->private = root; 2460 2461 /* find the first profile */ 2462 mutex_lock_nested(&root->lock, root->level); 2463 profile = __first_profile(root, root); 2464 2465 /* skip to position */ 2466 for (; profile && l > 0; l--) 2467 profile = next_profile(root, profile); 2468 2469 return profile; 2470 } 2471 2472 /** 2473 * p_next - read the next profile entry 2474 * @f: seq_file to fill 2475 * @p: profile previously returned 2476 * @pos: current position 2477 * 2478 * Returns: next profile after @p or NULL if none 2479 * 2480 * may acquire/release locks in namespace tree as necessary 2481 */ 2482 static void *p_next(struct seq_file *f, void *p, loff_t *pos) 2483 { 2484 struct aa_profile *profile = p; 2485 struct aa_ns *ns = f->private; 2486 (*pos)++; 2487 2488 return next_profile(ns, profile); 2489 } 2490 2491 /** 2492 * p_stop - stop depth first traversal 2493 * @f: seq_file we are filling 2494 * @p: the last profile written 2495 * 2496 * Release all locking done by p_start/p_next on namespace tree 2497 */ 2498 static void p_stop(struct seq_file *f, void *p) 2499 { 2500 struct aa_profile *profile = p; 2501 struct aa_ns *root = f->private, *ns; 2502 2503 if (profile) { 2504 for (ns = profile->ns; ns && ns != root; ns = ns->parent) 2505 mutex_unlock(&ns->lock); 2506 } 2507 mutex_unlock(&root->lock); 2508 aa_put_ns(root); 2509 } 2510 2511 /** 2512 * seq_show_profile - show a profile entry 2513 * @f: seq_file to file 2514 * @p: current position (profile) (NOT NULL) 2515 * 2516 * Returns: error on failure 2517 */ 2518 static int seq_show_profile(struct seq_file *f, void *p) 2519 { 2520 struct aa_profile *profile = (struct aa_profile *)p; 2521 struct aa_ns *root = f->private; 2522 2523 aa_label_seq_xprint(f, root, &profile->label, 2524 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); 2525 seq_putc(f, '\n'); 2526 2527 return 0; 2528 } 2529 2530 static const struct seq_operations aa_sfs_profiles_op = { 2531 .start = p_start, 2532 .next = p_next, 2533 .stop = p_stop, 2534 .show = seq_show_profile, 2535 }; 2536 2537 static int profiles_open(struct inode *inode, struct file *file) 2538 { 2539 if (!aa_current_policy_view_capable(NULL)) 2540 return -EACCES; 2541 2542 return seq_open(file, &aa_sfs_profiles_op); 2543 } 2544 2545 static int profiles_release(struct inode *inode, struct file *file) 2546 { 2547 return seq_release(inode, file); 2548 } 2549 2550 static const struct file_operations aa_sfs_profiles_fops = { 2551 .open = profiles_open, 2552 .read = seq_read, 2553 .llseek = seq_lseek, 2554 .release = profiles_release, 2555 }; 2556 2557 2558 /** Base file system setup **/ 2559 static struct aa_sfs_entry aa_sfs_entry_file[] = { 2560 AA_SFS_FILE_STRING("mask", 2561 "create read write exec append mmap_exec link lock"), 2562 { } 2563 }; 2564 2565 static struct aa_sfs_entry aa_sfs_entry_ptrace[] = { 2566 AA_SFS_FILE_STRING("mask", "read trace"), 2567 { } 2568 }; 2569 2570 static struct aa_sfs_entry aa_sfs_entry_signal[] = { 2571 AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK), 2572 { } 2573 }; 2574 2575 static struct aa_sfs_entry aa_sfs_entry_attach[] = { 2576 AA_SFS_FILE_BOOLEAN("xattr", 1), 2577 { } 2578 }; 2579 static struct aa_sfs_entry aa_sfs_entry_domain[] = { 2580 AA_SFS_FILE_BOOLEAN("change_hat", 1), 2581 AA_SFS_FILE_BOOLEAN("change_hatv", 1), 2582 AA_SFS_FILE_BOOLEAN("unconfined_allowed_children", 1), 2583 AA_SFS_FILE_BOOLEAN("change_onexec", 1), 2584 AA_SFS_FILE_BOOLEAN("change_profile", 1), 2585 AA_SFS_FILE_BOOLEAN("stack", 1), 2586 AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1), 2587 AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1), 2588 AA_SFS_FILE_BOOLEAN("computed_longest_left", 1), 2589 AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach), 2590 AA_SFS_FILE_BOOLEAN("disconnected.path", 1), 2591 AA_SFS_FILE_BOOLEAN("kill.signal", 1), 2592 AA_SFS_FILE_STRING("version", "1.2"), 2593 { } 2594 }; 2595 2596 static struct aa_sfs_entry aa_sfs_entry_unconfined[] = { 2597 AA_SFS_FILE_BOOLEAN("change_profile", 1), 2598 { } 2599 }; 2600 2601 static struct aa_sfs_entry aa_sfs_entry_versions[] = { 2602 AA_SFS_FILE_BOOLEAN("v5", 1), 2603 AA_SFS_FILE_BOOLEAN("v6", 1), 2604 AA_SFS_FILE_BOOLEAN("v7", 1), 2605 AA_SFS_FILE_BOOLEAN("v8", 1), 2606 AA_SFS_FILE_BOOLEAN("v9", 1), 2607 { } 2608 }; 2609 2610 #define PERMS32STR "allow deny subtree cond kill complain prompt audit quiet hide xindex tag label" 2611 static struct aa_sfs_entry aa_sfs_entry_policy[] = { 2612 AA_SFS_DIR("versions", aa_sfs_entry_versions), 2613 AA_SFS_FILE_BOOLEAN("set_load", 1), 2614 AA_SFS_FILE_BOOLEAN("diff-encode", 1), 2615 /* number of out of band transitions supported */ 2616 AA_SFS_FILE_U64("outofband", MAX_OOB_SUPPORTED), 2617 AA_SFS_FILE_U64("permstable32_version", 3), 2618 AA_SFS_FILE_STRING("permstable32", PERMS32STR), 2619 AA_SFS_FILE_U64("state32", 1), 2620 AA_SFS_DIR("unconfined_restrictions", aa_sfs_entry_unconfined), 2621 #ifdef CONFIG_SECURITY_APPARMOR_COMPRESSED_POLICY 2622 AA_SFS_FILE_BOOLEAN("compressed_load", 1), 2623 AA_SFS_FILE_BOOLEAN("extended_policy_header", 1), 2624 #endif 2625 { } 2626 }; 2627 2628 static struct aa_sfs_entry aa_sfs_entry_mount[] = { 2629 AA_SFS_FILE_STRING("mask", "mount umount pivot_root"), 2630 AA_SFS_FILE_STRING("move_mount", "detached"), 2631 { } 2632 }; 2633 2634 static struct aa_sfs_entry aa_sfs_entry_ns[] = { 2635 AA_SFS_FILE_BOOLEAN("profile", 1), 2636 AA_SFS_FILE_BOOLEAN("pivot_root", 0), 2637 AA_SFS_FILE_STRING("mask", "userns_create"), 2638 { } 2639 }; 2640 2641 static struct aa_sfs_entry aa_sfs_entry_dbus[] = { 2642 AA_SFS_FILE_STRING("mask", "acquire send receive"), 2643 { } 2644 }; 2645 2646 static struct aa_sfs_entry aa_sfs_entry_query_label[] = { 2647 AA_SFS_FILE_STRING("perms", "allow deny audit quiet"), 2648 AA_SFS_FILE_BOOLEAN("data", 1), 2649 AA_SFS_FILE_BOOLEAN("multi_transaction", 1), 2650 { } 2651 }; 2652 2653 static struct aa_sfs_entry aa_sfs_entry_query[] = { 2654 AA_SFS_DIR("label", aa_sfs_entry_query_label), 2655 { } 2656 }; 2657 2658 static struct aa_sfs_entry aa_sfs_entry_io_uring[] = { 2659 AA_SFS_FILE_STRING("mask", "sqpoll override_creds"), 2660 { } 2661 }; 2662 2663 static struct aa_sfs_entry aa_sfs_entry_features[] = { 2664 AA_SFS_DIR("policy", aa_sfs_entry_policy), 2665 AA_SFS_DIR("domain", aa_sfs_entry_domain), 2666 AA_SFS_DIR("file", aa_sfs_entry_file), 2667 AA_SFS_DIR("network_v8", aa_sfs_entry_network), 2668 AA_SFS_DIR("network_v9", aa_sfs_entry_networkv9), 2669 AA_SFS_DIR("mount", aa_sfs_entry_mount), 2670 AA_SFS_DIR("namespaces", aa_sfs_entry_ns), 2671 AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), 2672 AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit), 2673 AA_SFS_DIR("caps", aa_sfs_entry_caps), 2674 AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace), 2675 AA_SFS_DIR("signal", aa_sfs_entry_signal), 2676 AA_SFS_DIR("dbus", aa_sfs_entry_dbus), 2677 AA_SFS_DIR("query", aa_sfs_entry_query), 2678 AA_SFS_DIR("io_uring", aa_sfs_entry_io_uring), 2679 { } 2680 }; 2681 2682 static struct aa_sfs_entry aa_sfs_entry_apparmor[] = { 2683 AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access), 2684 AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops), 2685 AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops), 2686 AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops), 2687 AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops), 2688 AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops), 2689 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 2690 AA_SFS_FILE_FOPS("raw_data_compression_level_min", 0444, &seq_ns_compress_min_fops), 2691 AA_SFS_FILE_FOPS("raw_data_compression_level_max", 0444, &seq_ns_compress_max_fops), 2692 #endif 2693 AA_SFS_DIR("features", aa_sfs_entry_features), 2694 { } 2695 }; 2696 2697 static struct aa_sfs_entry aa_sfs_entry = 2698 AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor); 2699 2700 /** 2701 * entry_create_file - create a file entry in the apparmor securityfs 2702 * @fs_file: aa_sfs_entry to build an entry for (NOT NULL) 2703 * @parent: the parent dentry in the securityfs 2704 * 2705 * Use entry_remove_file to remove entries created with this fn. 2706 */ 2707 static int __init entry_create_file(struct aa_sfs_entry *fs_file, 2708 struct dentry *parent) 2709 { 2710 int error = 0; 2711 2712 fs_file->dentry = securityfs_create_file(fs_file->name, 2713 S_IFREG | fs_file->mode, 2714 parent, fs_file, 2715 fs_file->file_ops); 2716 if (IS_ERR(fs_file->dentry)) { 2717 error = PTR_ERR(fs_file->dentry); 2718 fs_file->dentry = NULL; 2719 } 2720 return error; 2721 } 2722 2723 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir); 2724 /** 2725 * entry_create_dir - recursively create a directory entry in the securityfs 2726 * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL) 2727 * @parent: the parent dentry in the securityfs 2728 * 2729 * Use entry_remove_dir to remove entries created with this fn. 2730 */ 2731 static int __init entry_create_dir(struct aa_sfs_entry *fs_dir, 2732 struct dentry *parent) 2733 { 2734 struct aa_sfs_entry *fs_file; 2735 struct dentry *dir; 2736 int error; 2737 2738 dir = securityfs_create_dir(fs_dir->name, parent); 2739 if (IS_ERR(dir)) 2740 return PTR_ERR(dir); 2741 fs_dir->dentry = dir; 2742 2743 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2744 if (fs_file->v_type == AA_SFS_TYPE_DIR) 2745 error = entry_create_dir(fs_file, fs_dir->dentry); 2746 else 2747 error = entry_create_file(fs_file, fs_dir->dentry); 2748 if (error) 2749 goto failed; 2750 } 2751 2752 return 0; 2753 2754 failed: 2755 entry_remove_dir(fs_dir); 2756 2757 return error; 2758 } 2759 2760 /** 2761 * entry_remove_file - drop a single file entry in the apparmor securityfs 2762 * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL) 2763 */ 2764 static void __init entry_remove_file(struct aa_sfs_entry *fs_file) 2765 { 2766 if (!fs_file->dentry) 2767 return; 2768 2769 securityfs_remove(fs_file->dentry); 2770 fs_file->dentry = NULL; 2771 } 2772 2773 /** 2774 * entry_remove_dir - recursively drop a directory entry from the securityfs 2775 * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL) 2776 */ 2777 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir) 2778 { 2779 struct aa_sfs_entry *fs_file; 2780 2781 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { 2782 if (fs_file->v_type == AA_SFS_TYPE_DIR) 2783 entry_remove_dir(fs_file); 2784 else 2785 entry_remove_file(fs_file); 2786 } 2787 2788 entry_remove_file(fs_dir); 2789 } 2790 2791 /** 2792 * aa_destroy_aafs - cleanup and free aafs 2793 * 2794 * releases dentries allocated by aa_create_aafs 2795 */ 2796 void __init aa_destroy_aafs(void) 2797 { 2798 entry_remove_dir(&aa_sfs_entry); 2799 } 2800 2801 2802 #define NULL_FILE_NAME ".null" 2803 struct path aa_null; 2804 2805 static int aa_mk_null_file(struct dentry *parent) 2806 { 2807 struct vfsmount *mount = NULL; 2808 struct dentry *dentry; 2809 struct inode *inode; 2810 int count = 0; 2811 int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); 2812 2813 if (error) 2814 return error; 2815 2816 dentry = simple_start_creating(parent, NULL_FILE_NAME); 2817 if (IS_ERR(dentry)) { 2818 error = PTR_ERR(dentry); 2819 goto out; 2820 } 2821 inode = new_inode(parent->d_inode->i_sb); 2822 if (!inode) { 2823 error = -ENOMEM; 2824 goto out; 2825 } 2826 2827 inode->i_ino = get_next_ino(); 2828 inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; 2829 simple_inode_init_ts(inode); 2830 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, 2831 MKDEV(MEM_MAJOR, 3)); 2832 d_instantiate(dentry, inode); 2833 aa_null.dentry = dget(dentry); 2834 aa_null.mnt = mntget(mount); 2835 2836 out: 2837 simple_done_creating(dentry); 2838 simple_release_fs(&mount, &count); 2839 return error; 2840 } 2841 2842 static const char *policy_get_link(struct dentry *dentry, 2843 struct inode *inode, 2844 struct delayed_call *done) 2845 { 2846 struct aa_ns *ns; 2847 struct path path; 2848 int error; 2849 2850 if (!dentry) 2851 return ERR_PTR(-ECHILD); 2852 2853 ns = aa_get_current_ns(); 2854 path.mnt = mntget(aafs_mnt); 2855 path.dentry = dget(ns_dir(ns)); 2856 error = nd_jump_link(&path); 2857 aa_put_ns(ns); 2858 2859 return ERR_PTR(error); 2860 } 2861 2862 static int policy_readlink(struct dentry *dentry, char __user *buffer, 2863 int buflen) 2864 { 2865 char name[32]; 2866 int res; 2867 2868 res = snprintf(name, sizeof(name), "%s:[%llu]", AAFS_NAME, 2869 d_inode(dentry)->i_ino); 2870 if (res > 0 && res < sizeof(name)) 2871 res = readlink_copy(buffer, buflen, name, strlen(name)); 2872 else 2873 res = -ENOENT; 2874 2875 return res; 2876 } 2877 2878 static const struct inode_operations policy_link_iops = { 2879 .readlink = policy_readlink, 2880 .get_link = policy_get_link, 2881 }; 2882 2883 2884 /** 2885 * aa_create_aafs - create the apparmor security filesystem 2886 * 2887 * dentries created here are released by aa_destroy_aafs 2888 * 2889 * Returns: error on failure 2890 */ 2891 int __init aa_create_aafs(void) 2892 { 2893 struct dentry *dent; 2894 int error; 2895 2896 if (!apparmor_initialized) 2897 return 0; 2898 2899 if (aa_sfs_entry.dentry) { 2900 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 2901 return -EEXIST; 2902 } 2903 2904 /* setup apparmorfs used to virtualize policy/ */ 2905 aafs_mnt = kern_mount(&aafs_ops); 2906 if (IS_ERR(aafs_mnt)) 2907 panic("can't set apparmorfs up\n"); 2908 aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; 2909 2910 /* Populate fs tree. */ 2911 error = entry_create_dir(&aa_sfs_entry, NULL); 2912 if (error) 2913 goto error; 2914 2915 dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry, 2916 NULL, &aa_fs_profile_load); 2917 if (IS_ERR(dent)) 2918 goto dent_error; 2919 ns_subload(root_ns) = dent; 2920 2921 dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry, 2922 NULL, &aa_fs_profile_replace); 2923 if (IS_ERR(dent)) 2924 goto dent_error; 2925 ns_subreplace(root_ns) = dent; 2926 2927 dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry, 2928 NULL, &aa_fs_profile_remove); 2929 if (IS_ERR(dent)) 2930 goto dent_error; 2931 ns_subremove(root_ns) = dent; 2932 2933 dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry, 2934 NULL, &aa_fs_ns_revision_fops); 2935 if (IS_ERR(dent)) 2936 goto dent_error; 2937 ns_subrevision(root_ns) = dent; 2938 2939 /* policy tree referenced by magic policy symlink */ 2940 mutex_lock_nested(&root_ns->lock, root_ns->level); 2941 error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy", 2942 aafs_mnt->mnt_root); 2943 mutex_unlock(&root_ns->lock); 2944 if (error) 2945 goto error; 2946 2947 /* magic symlink similar to nsfs redirects based on task policy */ 2948 dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry, 2949 NULL, &policy_link_iops); 2950 if (IS_ERR(dent)) 2951 goto dent_error; 2952 2953 error = aa_mk_null_file(aa_sfs_entry.dentry); 2954 if (error) 2955 goto error; 2956 2957 /* TODO: add default profile to apparmorfs */ 2958 2959 /* Report that AppArmor fs is enabled */ 2960 aa_info_message("AppArmor Filesystem Enabled"); 2961 return 0; 2962 2963 dent_error: 2964 error = PTR_ERR(dent); 2965 error: 2966 aa_destroy_aafs(); 2967 AA_ERROR("Error creating AppArmor securityfs\n"); 2968 return error; 2969 } 2970