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