0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 #include <linux/slab.h>
0072 #include <linux/spinlock.h>
0073 #include <linux/string.h>
0074 #include <linux/cred.h>
0075 #include <linux/rculist.h>
0076 #include <linux/user_namespace.h>
0077
0078 #include "include/apparmor.h"
0079 #include "include/capability.h"
0080 #include "include/cred.h"
0081 #include "include/file.h"
0082 #include "include/ipc.h"
0083 #include "include/match.h"
0084 #include "include/path.h"
0085 #include "include/policy.h"
0086 #include "include/policy_ns.h"
0087 #include "include/policy_unpack.h"
0088 #include "include/resource.h"
0089
0090 int unprivileged_userns_apparmor_policy = 1;
0091
0092 const char *const aa_profile_mode_names[] = {
0093 "enforce",
0094 "complain",
0095 "kill",
0096 "unconfined",
0097 };
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 static void __add_profile(struct list_head *list, struct aa_profile *profile)
0110 {
0111 struct aa_label *l;
0112
0113 AA_BUG(!list);
0114 AA_BUG(!profile);
0115 AA_BUG(!profile->ns);
0116 AA_BUG(!mutex_is_locked(&profile->ns->lock));
0117
0118 list_add_rcu(&profile->base.list, list);
0119
0120 aa_get_profile(profile);
0121 l = aa_label_insert(&profile->ns->labels, &profile->label);
0122 AA_BUG(l != &profile->label);
0123 aa_put_label(l);
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 static void __list_remove_profile(struct aa_profile *profile)
0139 {
0140 AA_BUG(!profile);
0141 AA_BUG(!profile->ns);
0142 AA_BUG(!mutex_is_locked(&profile->ns->lock));
0143
0144 list_del_rcu(&profile->base.list);
0145 aa_put_profile(profile);
0146 }
0147
0148
0149
0150
0151
0152
0153
0154 static void __remove_profile(struct aa_profile *profile)
0155 {
0156 AA_BUG(!profile);
0157 AA_BUG(!profile->ns);
0158 AA_BUG(!mutex_is_locked(&profile->ns->lock));
0159
0160
0161 __aa_profile_list_release(&profile->base.profiles);
0162
0163 aa_label_remove(&profile->label);
0164 __aafs_profile_rmdir(profile);
0165 __list_remove_profile(profile);
0166 }
0167
0168
0169
0170
0171
0172
0173
0174 void __aa_profile_list_release(struct list_head *head)
0175 {
0176 struct aa_profile *profile, *tmp;
0177 list_for_each_entry_safe(profile, tmp, head, base.list)
0178 __remove_profile(profile);
0179 }
0180
0181
0182
0183
0184
0185
0186 static void aa_free_data(void *ptr, void *arg)
0187 {
0188 struct aa_data *data = ptr;
0189
0190 kfree_sensitive(data->data);
0191 kfree_sensitive(data->key);
0192 kfree_sensitive(data);
0193 }
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 void aa_free_profile(struct aa_profile *profile)
0206 {
0207 struct rhashtable *rht;
0208 int i;
0209
0210 AA_DEBUG("%s(%p)\n", __func__, profile);
0211
0212 if (!profile)
0213 return;
0214
0215
0216 aa_policy_destroy(&profile->base);
0217 aa_put_profile(rcu_access_pointer(profile->parent));
0218
0219 aa_put_ns(profile->ns);
0220 kfree_sensitive(profile->rename);
0221
0222 aa_free_file_rules(&profile->file);
0223 aa_free_cap_rules(&profile->caps);
0224 aa_free_rlimit_rules(&profile->rlimits);
0225
0226 for (i = 0; i < profile->xattr_count; i++)
0227 kfree_sensitive(profile->xattrs[i]);
0228 kfree_sensitive(profile->xattrs);
0229 for (i = 0; i < profile->secmark_count; i++)
0230 kfree_sensitive(profile->secmark[i].label);
0231 kfree_sensitive(profile->secmark);
0232 kfree_sensitive(profile->dirname);
0233 aa_put_dfa(profile->xmatch);
0234 aa_put_dfa(profile->policy.dfa);
0235
0236 if (profile->data) {
0237 rht = profile->data;
0238 profile->data = NULL;
0239 rhashtable_free_and_destroy(rht, aa_free_data, NULL);
0240 kfree_sensitive(rht);
0241 }
0242
0243 kfree_sensitive(profile->hash);
0244 aa_put_loaddata(profile->rawdata);
0245 aa_label_destroy(&profile->label);
0246
0247 kfree_sensitive(profile);
0248 }
0249
0250
0251
0252
0253
0254
0255
0256
0257 struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
0258 gfp_t gfp)
0259 {
0260 struct aa_profile *profile;
0261
0262
0263 profile = kzalloc(struct_size(profile, label.vec, 2), gfp);
0264 if (!profile)
0265 return NULL;
0266
0267 if (!aa_policy_init(&profile->base, NULL, hname, gfp))
0268 goto fail;
0269 if (!aa_label_init(&profile->label, 1, gfp))
0270 goto fail;
0271
0272
0273 if (!proxy) {
0274 proxy = aa_alloc_proxy(&profile->label, gfp);
0275 if (!proxy)
0276 goto fail;
0277 } else
0278 aa_get_proxy(proxy);
0279 profile->label.proxy = proxy;
0280
0281 profile->label.hname = profile->base.hname;
0282 profile->label.flags |= FLAG_PROFILE;
0283 profile->label.vec[0] = profile;
0284
0285
0286 return profile;
0287
0288 fail:
0289 aa_free_profile(profile);
0290
0291 return NULL;
0292 }
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 static struct aa_profile *__strn_find_child(struct list_head *head,
0307 const char *name, int len)
0308 {
0309 return (struct aa_profile *)__policy_strn_find(head, name, len);
0310 }
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321 static struct aa_profile *__find_child(struct list_head *head, const char *name)
0322 {
0323 return __strn_find_child(head, name, strlen(name));
0324 }
0325
0326
0327
0328
0329
0330
0331
0332
0333 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
0334 {
0335 struct aa_profile *profile;
0336
0337 rcu_read_lock();
0338 do {
0339 profile = __find_child(&parent->base.profiles, name);
0340 } while (profile && !aa_get_profile_not0(profile));
0341 rcu_read_unlock();
0342
0343
0344 return profile;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 static struct aa_policy *__lookup_parent(struct aa_ns *ns,
0361 const char *hname)
0362 {
0363 struct aa_policy *policy;
0364 struct aa_profile *profile = NULL;
0365 char *split;
0366
0367 policy = &ns->base;
0368
0369 for (split = strstr(hname, "//"); split;) {
0370 profile = __strn_find_child(&policy->profiles, hname,
0371 split - hname);
0372 if (!profile)
0373 return NULL;
0374 policy = &profile->base;
0375 hname = split + 2;
0376 split = strstr(hname, "//");
0377 }
0378 if (!profile)
0379 return &ns->base;
0380 return &profile->base;
0381 }
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 static struct aa_profile *__lookupn_profile(struct aa_policy *base,
0396 const char *hname, size_t n)
0397 {
0398 struct aa_profile *profile = NULL;
0399 const char *split;
0400
0401 for (split = strnstr(hname, "//", n); split;
0402 split = strnstr(hname, "//", n)) {
0403 profile = __strn_find_child(&base->profiles, hname,
0404 split - hname);
0405 if (!profile)
0406 return NULL;
0407
0408 base = &profile->base;
0409 n -= split + 2 - hname;
0410 hname = split + 2;
0411 }
0412
0413 if (n)
0414 return __strn_find_child(&base->profiles, hname, n);
0415 return NULL;
0416 }
0417
0418 static struct aa_profile *__lookup_profile(struct aa_policy *base,
0419 const char *hname)
0420 {
0421 return __lookupn_profile(base, hname, strlen(hname));
0422 }
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
0433 size_t n)
0434 {
0435 struct aa_profile *profile;
0436
0437 rcu_read_lock();
0438 do {
0439 profile = __lookupn_profile(&ns->base, hname, n);
0440 } while (profile && !aa_get_profile_not0(profile));
0441 rcu_read_unlock();
0442
0443
0444 if (!profile && strncmp(hname, "unconfined", n) == 0)
0445 profile = aa_get_newest_profile(ns->unconfined);
0446
0447
0448 return profile;
0449 }
0450
0451 struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
0452 {
0453 return aa_lookupn_profile(ns, hname, strlen(hname));
0454 }
0455
0456 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
0457 const char *fqname, size_t n)
0458 {
0459 struct aa_profile *profile;
0460 struct aa_ns *ns;
0461 const char *name, *ns_name;
0462 size_t ns_len;
0463
0464 name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
0465 if (ns_name) {
0466 ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
0467 if (!ns)
0468 return NULL;
0469 } else
0470 ns = aa_get_ns(labels_ns(base));
0471
0472 if (name)
0473 profile = aa_lookupn_profile(ns, name, n - (name - fqname));
0474 else if (ns)
0475
0476 profile = aa_get_newest_profile(ns->unconfined);
0477 else
0478 profile = NULL;
0479 aa_put_ns(ns);
0480
0481 return profile;
0482 }
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502 struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
0503 const char *base, gfp_t gfp)
0504 {
0505 struct aa_profile *p, *profile;
0506 const char *bname;
0507 char *name = NULL;
0508
0509 AA_BUG(!parent);
0510
0511 if (base) {
0512 name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
0513 gfp);
0514 if (name) {
0515 sprintf(name, "%s//null-%s", parent->base.hname, base);
0516 goto name;
0517 }
0518
0519 }
0520
0521 name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
0522 if (!name)
0523 return NULL;
0524 sprintf(name, "%s//null-%x", parent->base.hname,
0525 atomic_inc_return(&parent->ns->uniq_null));
0526
0527 name:
0528
0529 bname = basename(name);
0530 profile = aa_find_child(parent, bname);
0531 if (profile)
0532 goto out;
0533
0534 profile = aa_alloc_profile(name, NULL, gfp);
0535 if (!profile)
0536 goto fail;
0537
0538 profile->mode = APPARMOR_COMPLAIN;
0539 profile->label.flags |= FLAG_NULL;
0540 if (hat)
0541 profile->label.flags |= FLAG_HAT;
0542 profile->path_flags = parent->path_flags;
0543
0544
0545 rcu_assign_pointer(profile->parent, aa_get_profile(parent));
0546 profile->ns = aa_get_ns(parent->ns);
0547 profile->file.dfa = aa_get_dfa(nulldfa);
0548 profile->policy.dfa = aa_get_dfa(nulldfa);
0549
0550 mutex_lock_nested(&profile->ns->lock, profile->ns->level);
0551 p = __find_child(&parent->base.profiles, bname);
0552 if (p) {
0553 aa_free_profile(profile);
0554 profile = aa_get_profile(p);
0555 } else {
0556 __add_profile(&parent->base.profiles, profile);
0557 }
0558 mutex_unlock(&profile->ns->lock);
0559
0560
0561 out:
0562 kfree(name);
0563
0564 return profile;
0565
0566 fail:
0567 kfree(name);
0568 aa_free_profile(profile);
0569 return NULL;
0570 }
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580 static int replacement_allowed(struct aa_profile *profile, int noreplace,
0581 const char **info)
0582 {
0583 if (profile) {
0584 if (profile->label.flags & FLAG_IMMUTIBLE) {
0585 *info = "cannot replace immutable profile";
0586 return -EPERM;
0587 } else if (noreplace) {
0588 *info = "profile already exists";
0589 return -EEXIST;
0590 }
0591 }
0592 return 0;
0593 }
0594
0595
0596 static void audit_cb(struct audit_buffer *ab, void *va)
0597 {
0598 struct common_audit_data *sa = va;
0599
0600 if (aad(sa)->iface.ns) {
0601 audit_log_format(ab, " ns=");
0602 audit_log_untrustedstring(ab, aad(sa)->iface.ns);
0603 }
0604 }
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617 static int audit_policy(struct aa_label *label, const char *op,
0618 const char *ns_name, const char *name,
0619 const char *info, int error)
0620 {
0621 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op);
0622
0623 aad(&sa)->iface.ns = ns_name;
0624 aad(&sa)->name = name;
0625 aad(&sa)->info = info;
0626 aad(&sa)->error = error;
0627 aad(&sa)->label = label;
0628
0629 aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, audit_cb);
0630
0631 return error;
0632 }
0633
0634
0635
0636
0637 static int policy_ns_capable(struct aa_label *label,
0638 struct user_namespace *userns, int cap)
0639 {
0640 int err;
0641
0642
0643 err = cap_capable(current_cred(), userns, cap, CAP_OPT_NONE);
0644 if (!err)
0645 err = aa_capable(label, cap, CAP_OPT_NONE);
0646
0647 return err;
0648 }
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659 bool aa_policy_view_capable(struct aa_label *label, struct aa_ns *ns)
0660 {
0661 struct user_namespace *user_ns = current_user_ns();
0662 struct aa_ns *view_ns = labels_view(label);
0663 bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
0664 in_egroup_p(make_kgid(user_ns, 0));
0665 bool response = false;
0666 if (!ns)
0667 ns = view_ns;
0668
0669 if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
0670 (user_ns == &init_user_ns ||
0671 (unprivileged_userns_apparmor_policy != 0 &&
0672 user_ns->level == view_ns->level)))
0673 response = true;
0674
0675 return response;
0676 }
0677
0678 bool aa_policy_admin_capable(struct aa_label *label, struct aa_ns *ns)
0679 {
0680 struct user_namespace *user_ns = current_user_ns();
0681 bool capable = policy_ns_capable(label, user_ns, CAP_MAC_ADMIN) == 0;
0682
0683 AA_DEBUG("cap_mac_admin? %d\n", capable);
0684 AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
0685
0686 return aa_policy_view_capable(label, ns) && capable &&
0687 !aa_g_lock_policy;
0688 }
0689
0690 bool aa_current_policy_view_capable(struct aa_ns *ns)
0691 {
0692 struct aa_label *label;
0693 bool res;
0694
0695 label = __begin_current_label_crit_section();
0696 res = aa_policy_view_capable(label, ns);
0697 __end_current_label_crit_section(label);
0698
0699 return res;
0700 }
0701
0702 bool aa_current_policy_admin_capable(struct aa_ns *ns)
0703 {
0704 struct aa_label *label;
0705 bool res;
0706
0707 label = __begin_current_label_crit_section();
0708 res = aa_policy_admin_capable(label, ns);
0709 __end_current_label_crit_section(label);
0710
0711 return res;
0712 }
0713
0714
0715
0716
0717
0718
0719
0720
0721 int aa_may_manage_policy(struct aa_label *label, struct aa_ns *ns, u32 mask)
0722 {
0723 const char *op;
0724
0725 if (mask & AA_MAY_REMOVE_POLICY)
0726 op = OP_PROF_RM;
0727 else if (mask & AA_MAY_REPLACE_POLICY)
0728 op = OP_PROF_REPL;
0729 else
0730 op = OP_PROF_LOAD;
0731
0732
0733 if (aa_g_lock_policy)
0734 return audit_policy(label, op, NULL, NULL, "policy_locked",
0735 -EACCES);
0736
0737 if (!aa_policy_admin_capable(label, ns))
0738 return audit_policy(label, op, NULL, NULL, "not policy admin",
0739 -EACCES);
0740
0741
0742 return 0;
0743 }
0744
0745 static struct aa_profile *__list_lookup_parent(struct list_head *lh,
0746 struct aa_profile *profile)
0747 {
0748 const char *base = basename(profile->base.hname);
0749 long len = base - profile->base.hname;
0750 struct aa_load_ent *ent;
0751
0752
0753 if (len <= 2)
0754 return NULL;
0755 len -= 2;
0756
0757 list_for_each_entry(ent, lh, list) {
0758 if (ent->new == profile)
0759 continue;
0760 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
0761 0 && ent->new->base.hname[len] == 0)
0762 return ent->new;
0763 }
0764
0765 return NULL;
0766 }
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781 static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
0782 {
0783 struct aa_profile *child, *tmp;
0784
0785 if (!list_empty(&old->base.profiles)) {
0786 LIST_HEAD(lh);
0787 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
0788
0789 list_for_each_entry_safe(child, tmp, &lh, base.list) {
0790 struct aa_profile *p;
0791
0792 list_del_init(&child->base.list);
0793 p = __find_child(&new->base.profiles, child->base.name);
0794 if (p) {
0795
0796 __replace_profile(child, p);
0797 continue;
0798 }
0799
0800
0801
0802
0803 p = aa_deref_parent(child);
0804 rcu_assign_pointer(child->parent, aa_get_profile(new));
0805 list_add_rcu(&child->base.list, &new->base.profiles);
0806 aa_put_profile(p);
0807 }
0808 }
0809
0810 if (!rcu_access_pointer(new->parent)) {
0811 struct aa_profile *parent = aa_deref_parent(old);
0812 rcu_assign_pointer(new->parent, aa_get_profile(parent));
0813 }
0814 aa_label_replace(&old->label, &new->label);
0815
0816 __aafs_profile_migrate_dents(old, new);
0817
0818 if (list_empty(&new->base.list)) {
0819
0820 list_replace_rcu(&old->base.list, &new->base.list);
0821 aa_get_profile(new);
0822 aa_put_profile(old);
0823 } else
0824 __list_remove_profile(old);
0825 }
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837 static int __lookup_replace(struct aa_ns *ns, const char *hname,
0838 bool noreplace, struct aa_profile **p,
0839 const char **info)
0840 {
0841 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
0842 if (*p) {
0843 int error = replacement_allowed(*p, noreplace, info);
0844 if (error) {
0845 *info = "profile can not be replaced";
0846 return error;
0847 }
0848 }
0849
0850 return 0;
0851 }
0852
0853 static void share_name(struct aa_profile *old, struct aa_profile *new)
0854 {
0855 aa_put_str(new->base.hname);
0856 aa_get_str(old->base.hname);
0857 new->base.hname = old->base.hname;
0858 new->base.name = old->base.name;
0859 new->label.hname = old->label.hname;
0860 }
0861
0862
0863
0864
0865 static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
0866 {
0867 struct aa_profile *parent, *newest;
0868
0869 parent = rcu_dereference_protected(new->parent,
0870 mutex_is_locked(&new->ns->lock));
0871 newest = aa_get_newest_profile(parent);
0872
0873
0874 if (newest != parent) {
0875 aa_put_profile(parent);
0876 rcu_assign_pointer(new->parent, newest);
0877 } else
0878 aa_put_profile(newest);
0879
0880 return newest;
0881 }
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897 ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
0898 u32 mask, struct aa_loaddata *udata)
0899 {
0900 const char *ns_name = NULL, *info = NULL;
0901 struct aa_ns *ns = NULL;
0902 struct aa_load_ent *ent, *tmp;
0903 struct aa_loaddata *rawdata_ent;
0904 const char *op;
0905 ssize_t count, error;
0906 LIST_HEAD(lh);
0907
0908 op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
0909 aa_get_loaddata(udata);
0910
0911 error = aa_unpack(udata, &lh, &ns_name);
0912 if (error)
0913 goto out;
0914
0915
0916
0917
0918
0919
0920 count = 0;
0921 list_for_each_entry(ent, &lh, list) {
0922 if (ns_name) {
0923 if (ent->ns_name &&
0924 strcmp(ent->ns_name, ns_name) != 0) {
0925 info = "policy load has mixed namespaces";
0926 error = -EACCES;
0927 goto fail;
0928 }
0929 } else if (ent->ns_name) {
0930 if (count) {
0931 info = "policy load has mixed namespaces";
0932 error = -EACCES;
0933 goto fail;
0934 }
0935 ns_name = ent->ns_name;
0936 } else
0937 count++;
0938 }
0939 if (ns_name) {
0940 ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
0941 ns_name);
0942 if (IS_ERR(ns)) {
0943 op = OP_PROF_LOAD;
0944 info = "failed to prepare namespace";
0945 error = PTR_ERR(ns);
0946 ns = NULL;
0947 ent = NULL;
0948 goto fail;
0949 }
0950 } else
0951 ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
0952
0953 mutex_lock_nested(&ns->lock, ns->level);
0954
0955 if (!list_empty(&ns->rawdata_list)) {
0956 list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
0957 if (aa_rawdata_eq(rawdata_ent, udata)) {
0958 struct aa_loaddata *tmp;
0959
0960 tmp = __aa_get_loaddata(rawdata_ent);
0961
0962 if (tmp) {
0963 aa_put_loaddata(udata);
0964 udata = tmp;
0965 break;
0966 }
0967 }
0968 }
0969 }
0970
0971 list_for_each_entry(ent, &lh, list) {
0972 struct aa_policy *policy;
0973
0974 if (aa_g_export_binary)
0975 ent->new->rawdata = aa_get_loaddata(udata);
0976 error = __lookup_replace(ns, ent->new->base.hname,
0977 !(mask & AA_MAY_REPLACE_POLICY),
0978 &ent->old, &info);
0979 if (error)
0980 goto fail_lock;
0981
0982 if (ent->new->rename) {
0983 error = __lookup_replace(ns, ent->new->rename,
0984 !(mask & AA_MAY_REPLACE_POLICY),
0985 &ent->rename, &info);
0986 if (error)
0987 goto fail_lock;
0988 }
0989
0990
0991 ent->new->ns = aa_get_ns(ns);
0992
0993 if (ent->old || ent->rename)
0994 continue;
0995
0996
0997 policy = __lookup_parent(ns, ent->new->base.hname);
0998 if (!policy) {
0999 struct aa_profile *p;
1000 p = __list_lookup_parent(&lh, ent->new);
1001 if (!p) {
1002 error = -ENOENT;
1003 info = "parent does not exist";
1004 goto fail_lock;
1005 }
1006 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1007 } else if (policy != &ns->base) {
1008
1009 struct aa_profile *p = (struct aa_profile *) policy;
1010 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1011 }
1012 }
1013
1014
1015 if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
1016 error = __aa_fs_create_rawdata(ns, udata);
1017 if (error) {
1018 info = "failed to create raw_data dir and files";
1019 ent = NULL;
1020 goto fail_lock;
1021 }
1022 }
1023 list_for_each_entry(ent, &lh, list) {
1024 if (!ent->old) {
1025 struct dentry *parent;
1026 if (rcu_access_pointer(ent->new->parent)) {
1027 struct aa_profile *p;
1028 p = aa_deref_parent(ent->new);
1029 parent = prof_child_dir(p);
1030 } else
1031 parent = ns_subprofs_dir(ent->new->ns);
1032 error = __aafs_profile_mkdir(ent->new, parent);
1033 }
1034
1035 if (error) {
1036 info = "failed to create";
1037 goto fail_lock;
1038 }
1039 }
1040
1041
1042 __aa_bump_ns_revision(ns);
1043 if (aa_g_export_binary)
1044 __aa_loaddata_update(udata, ns->revision);
1045 list_for_each_entry_safe(ent, tmp, &lh, list) {
1046 list_del_init(&ent->list);
1047 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1048
1049 if (ent->old && ent->old->rawdata == ent->new->rawdata &&
1050 ent->new->rawdata) {
1051
1052 audit_policy(label, op, ns_name, ent->new->base.hname,
1053 "same as current profile, skipping",
1054 error);
1055
1056 aa_put_proxy(ent->new->label.proxy);
1057 ent->new->label.proxy = NULL;
1058 goto skip;
1059 }
1060
1061
1062
1063
1064
1065 audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1066 error);
1067
1068 if (ent->old) {
1069 share_name(ent->old, ent->new);
1070 __replace_profile(ent->old, ent->new);
1071 } else {
1072 struct list_head *lh;
1073
1074 if (rcu_access_pointer(ent->new->parent)) {
1075 struct aa_profile *parent;
1076
1077 parent = update_to_newest_parent(ent->new);
1078 lh = &parent->base.profiles;
1079 } else
1080 lh = &ns->base.profiles;
1081 __add_profile(lh, ent->new);
1082 }
1083 skip:
1084 aa_load_ent_free(ent);
1085 }
1086 __aa_labelset_update_subtree(ns);
1087 mutex_unlock(&ns->lock);
1088
1089 out:
1090 aa_put_ns(ns);
1091 aa_put_loaddata(udata);
1092 kfree(ns_name);
1093
1094 if (error)
1095 return error;
1096 return udata->size;
1097
1098 fail_lock:
1099 mutex_unlock(&ns->lock);
1100
1101
1102 op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1103 fail:
1104 audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
1105 info, error);
1106
1107 info = "valid profile in failed atomic policy load";
1108 list_for_each_entry(tmp, &lh, list) {
1109 if (tmp == ent) {
1110 info = "unchecked profile in failed atomic policy load";
1111
1112 continue;
1113 }
1114 op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1115 audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1116 error);
1117 }
1118 list_for_each_entry_safe(ent, tmp, &lh, list) {
1119 list_del_init(&ent->list);
1120 aa_load_ent_free(ent);
1121 }
1122
1123 goto out;
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
1141 char *fqname, size_t size)
1142 {
1143 struct aa_ns *ns = NULL;
1144 struct aa_profile *profile = NULL;
1145 const char *name = fqname, *info = NULL;
1146 const char *ns_name = NULL;
1147 ssize_t error = 0;
1148
1149 if (*fqname == 0) {
1150 info = "no profile specified";
1151 error = -ENOENT;
1152 goto fail;
1153 }
1154
1155 if (fqname[0] == ':') {
1156 size_t ns_len;
1157
1158 name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1159
1160 ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1161 ns_name, ns_len);
1162 if (!ns) {
1163 info = "namespace does not exist";
1164 error = -ENOENT;
1165 goto fail;
1166 }
1167 } else
1168
1169 ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1170
1171 if (!name) {
1172
1173 mutex_lock_nested(&ns->parent->lock, ns->level);
1174 __aa_bump_ns_revision(ns);
1175 __aa_remove_ns(ns);
1176 mutex_unlock(&ns->parent->lock);
1177 } else {
1178
1179 mutex_lock_nested(&ns->lock, ns->level);
1180 profile = aa_get_profile(__lookup_profile(&ns->base, name));
1181 if (!profile) {
1182 error = -ENOENT;
1183 info = "profile does not exist";
1184 goto fail_ns_lock;
1185 }
1186 name = profile->base.hname;
1187 __aa_bump_ns_revision(ns);
1188 __remove_profile(profile);
1189 __aa_labelset_update_subtree(ns);
1190 mutex_unlock(&ns->lock);
1191 }
1192
1193
1194 (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1195 error);
1196 aa_put_ns(ns);
1197 aa_put_profile(profile);
1198 return size;
1199
1200 fail_ns_lock:
1201 mutex_unlock(&ns->lock);
1202 aa_put_ns(ns);
1203
1204 fail:
1205 (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1206 error);
1207 return error;
1208 }