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