File Index
Files and directories
Sign in
FILE

policy.c

File preview with syntax highlighting.

linux-6.19.14/security/apparmor/policy.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * AppArmor security module
   4 *
   5 * This file contains AppArmor policy manipulation functions
   6 *
   7 * Copyright (C) 1998-2008 Novell/SUSE
   8 * Copyright 2009-2010 Canonical Ltd.
   9 *
  10 * AppArmor policy is based around profiles, which contain the rules a
  11 * task is confined by.  Every task in the system has a profile attached
  12 * to it determined either by matching "unconfined" tasks against the
  13 * visible set of profiles or by following a profiles attachment rules.
  14 *
  15 * Each profile exists in a profile namespace which is a container of
  16 * visible profiles.  Each namespace contains a special "unconfined" profile,
  17 * which doesn't enforce any confinement on a task beyond DAC.
  18 *
  19 * Namespace and profile names can be written together in either
  20 * of two syntaxes.
  21 *	:namespace:profile - used by kernel interfaces for easy detection
  22 *	namespace://profile - used by policy
  23 *
  24 * Profile names can not start with : or @ or ^ and may not contain \0
  25 *
  26 * Reserved profile names
  27 *	unconfined - special automatically generated unconfined profile
  28 *	inherit - special name to indicate profile inheritance
  29 *	null-XXXX-YYYY - special automatically generated learning profiles
  30 *
  31 * Namespace names may not start with / or @ and may not contain \0 or :
  32 * Reserved namespace names
  33 *	user-XXXX - user defined profiles
  34 *
  35 * a // in a profile or namespace name indicates a hierarchical name with the
  36 * name before the // being the parent and the name after the child.
  37 *
  38 * Profile and namespace hierarchies serve two different but similar purposes.
  39 * The namespace contains the set of visible profiles that are considered
  40 * for attachment.  The hierarchy of namespaces allows for virtualizing
  41 * the namespace so that for example a chroot can have its own set of profiles
  42 * which may define some local user namespaces.
  43 * The profile hierarchy severs two distinct purposes,
  44 * -  it allows for sub profiles or hats, which allows an application to run
  45 *    subprograms under its own profile with different restriction than it
  46 *    self, and not have it use the system profile.
  47 *    eg. if a mail program starts an editor, the policy might make the
  48 *        restrictions tighter on the editor tighter than the mail program,
  49 *        and definitely different than general editor restrictions
  50 * - it allows for binary hierarchy of profiles, so that execution history
  51 *   is preserved.  This feature isn't exploited by AppArmor reference policy
  52 *   but is allowed.  NOTE: this is currently suboptimal because profile
  53 *   aliasing is not currently implemented so that a profile for each
  54 *   level must be defined.
  55 *   eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
  56 *       from /bin/bash
  57 *
  58 *   A profile or namespace name that can contain one or more // separators
  59 *   is referred to as an hname (hierarchical).
  60 *   eg.  /bin/bash//bin/ls
  61 *
  62 *   An fqname is a name that may contain both namespace and profile hnames.
  63 *   eg. :ns:/bin/bash//bin/ls
  64 *
  65 * NOTES:
  66 *   - locking of profile lists is currently fairly coarse.  All profile
  67 *     lists within a namespace use the namespace lock.
  68 * FIXME: move profile lists to using rcu_lists
  69 */
  70
  71#include <linux/slab.h>
  72#include <linux/spinlock.h>
  73#include <linux/string.h>
  74#include <linux/cred.h>
  75#include <linux/rculist.h>
  76#include <linux/user_namespace.h>
  77
  78#include "include/apparmor.h"
  79#include "include/capability.h"
  80#include "include/cred.h"
  81#include "include/file.h"
  82#include "include/ipc.h"
  83#include "include/match.h"
  84#include "include/path.h"
  85#include "include/policy.h"
  86#include "include/policy_ns.h"
  87#include "include/policy_unpack.h"
  88#include "include/resource.h"
  89
  90int unprivileged_userns_apparmor_policy = 1;
  91int aa_unprivileged_unconfined_restricted;
  92
  93const char *const aa_profile_mode_names[] = {
  94	"enforce",
  95	"complain",
  96	"kill",
  97	"unconfined",
  98	"user",
  99};
 100
 101
 102static void aa_free_pdb(struct aa_policydb *pdb)
 103{
 104	if (pdb) {
 105		aa_put_dfa(pdb->dfa);
 106		kvfree(pdb->perms);
 107		aa_free_str_table(&pdb->trans);
 108		kfree(pdb);
 109	}
 110}
 111
 112/**
 113 * aa_pdb_free_kref - free aa_policydb by kref (called by aa_put_pdb)
 114 * @kref: kref callback for freeing of a dfa  (NOT NULL)
 115 */
 116void aa_pdb_free_kref(struct kref *kref)
 117{
 118	struct aa_policydb *pdb = container_of(kref, struct aa_policydb, count);
 119
 120	aa_free_pdb(pdb);
 121}
 122
 123
 124struct aa_policydb *aa_alloc_pdb(gfp_t gfp)
 125{
 126	struct aa_policydb *pdb = kzalloc(sizeof(struct aa_policydb), gfp);
 127
 128	if (!pdb)
 129		return NULL;
 130
 131	kref_init(&pdb->count);
 132
 133	return pdb;
 134}
 135
 136
 137/**
 138 * __add_profile - add a profiles to list and label tree
 139 * @list: list to add it to  (NOT NULL)
 140 * @profile: the profile to add  (NOT NULL)
 141 *
 142 * refcount @profile, should be put by __list_remove_profile
 143 *
 144 * Requires: namespace lock be held, or list not be shared
 145 */
 146static void __add_profile(struct list_head *list, struct aa_profile *profile)
 147{
 148	struct aa_label *l;
 149
 150	AA_BUG(!list);
 151	AA_BUG(!profile);
 152	AA_BUG(!profile->ns);
 153	AA_BUG(!mutex_is_locked(&profile->ns->lock));
 154
 155	list_add_rcu(&profile->base.list, list);
 156	/* get list reference */
 157	aa_get_profile(profile);
 158	l = aa_label_insert(&profile->ns->labels, &profile->label);
 159	AA_BUG(l != &profile->label);
 160	aa_put_label(l);
 161}
 162
 163/**
 164 * __list_remove_profile - remove a profile from the list it is on
 165 * @profile: the profile to remove  (NOT NULL)
 166 *
 167 * remove a profile from the list, warning generally removal should
 168 * be done with __replace_profile as most profile removals are
 169 * replacements to the unconfined profile.
 170 *
 171 * put @profile list refcount
 172 *
 173 * Requires: namespace lock be held, or list not have been live
 174 */
 175static void __list_remove_profile(struct aa_profile *profile)
 176{
 177	AA_BUG(!profile);
 178	AA_BUG(!profile->ns);
 179	AA_BUG(!mutex_is_locked(&profile->ns->lock));
 180
 181	list_del_rcu(&profile->base.list);
 182	aa_put_profile(profile);
 183}
 184
 185/**
 186 * __remove_profile - remove profile, and children
 187 * @profile: profile to be removed  (NOT NULL)
 188 *
 189 * Requires: namespace list lock be held, or list not be shared
 190 */
 191static void __remove_profile(struct aa_profile *profile)
 192{
 193	struct aa_profile *curr, *to_remove;
 194
 195	AA_BUG(!profile);
 196	AA_BUG(!profile->ns);
 197	AA_BUG(!mutex_is_locked(&profile->ns->lock));
 198
 199	/* release any children lists first */
 200	if (!list_empty(&profile->base.profiles)) {
 201		curr = list_first_entry(&profile->base.profiles, struct aa_profile, base.list);
 202
 203		while (curr != profile) {
 204
 205			while (!list_empty(&curr->base.profiles))
 206				curr = list_first_entry(&curr->base.profiles,
 207							struct aa_profile, base.list);
 208
 209			to_remove = curr;
 210			if (!list_is_last(&to_remove->base.list,
 211					  &aa_deref_parent(curr)->base.profiles))
 212				curr = list_next_entry(to_remove, base.list);
 213			else
 214				curr = aa_deref_parent(curr);
 215
 216			/* released by free_profile */
 217			aa_label_remove(&to_remove->label);
 218			__aafs_profile_rmdir(to_remove);
 219			__list_remove_profile(to_remove);
 220		}
 221	}
 222
 223	/* released by free_profile */
 224	aa_label_remove(&profile->label);
 225	__aafs_profile_rmdir(profile);
 226	__list_remove_profile(profile);
 227}
 228
 229/**
 230 * __aa_profile_list_release - remove all profiles on the list and put refs
 231 * @head: list of profiles  (NOT NULL)
 232 *
 233 * Requires: namespace lock be held
 234 */
 235void __aa_profile_list_release(struct list_head *head)
 236{
 237	struct aa_profile *profile, *tmp;
 238	list_for_each_entry_safe(profile, tmp, head, base.list)
 239		__remove_profile(profile);
 240}
 241
 242/**
 243 * aa_free_data - free a data blob
 244 * @ptr: data to free
 245 * @arg: unused
 246 */
 247static void aa_free_data(void *ptr, void *arg)
 248{
 249	struct aa_data *data = ptr;
 250
 251	kvfree_sensitive(data->data, data->size);
 252	kfree_sensitive(data->key);
 253	kfree_sensitive(data);
 254}
 255
 256static void free_attachment(struct aa_attachment *attach)
 257{
 258	int i;
 259
 260	for (i = 0; i < attach->xattr_count; i++)
 261		kfree_sensitive(attach->xattrs[i]);
 262	kfree_sensitive(attach->xattrs);
 263	aa_put_pdb(attach->xmatch);
 264}
 265
 266static void free_ruleset(struct aa_ruleset *rules)
 267{
 268	int i;
 269
 270	if (!rules)
 271	  return;
 272
 273	aa_put_pdb(rules->file);
 274	aa_put_pdb(rules->policy);
 275	aa_free_cap_rules(&rules->caps);
 276	aa_free_rlimit_rules(&rules->rlimits);
 277
 278	for (i = 0; i < rules->secmark_count; i++)
 279		kfree_sensitive(rules->secmark[i].label);
 280	kfree_sensitive(rules->secmark);
 281	kfree_sensitive(rules);
 282}
 283
 284struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp)
 285{
 286	struct aa_ruleset *rules;
 287
 288	rules = kzalloc(sizeof(*rules), gfp);
 289
 290	return rules;
 291}
 292
 293/**
 294 * aa_free_profile - free a profile
 295 * @profile: the profile to free  (MAYBE NULL)
 296 *
 297 * Free a profile, its hats and null_profile. All references to the profile,
 298 * its hats and null_profile must have been put.
 299 *
 300 * If the profile was referenced from a task context, free_profile() will
 301 * be called from an rcu callback routine, so we must not sleep here.
 302 */
 303void aa_free_profile(struct aa_profile *profile)
 304{
 305	struct rhashtable *rht;
 306
 307	AA_DEBUG(DEBUG_POLICY, "%s(%p)\n", __func__, profile);
 308
 309	if (!profile)
 310		return;
 311
 312	/* free children profiles */
 313	aa_policy_destroy(&profile->base);
 314	aa_put_profile(rcu_access_pointer(profile->parent));
 315
 316	aa_put_ns(profile->ns);
 317	kfree_sensitive(profile->rename);
 318	kfree_sensitive(profile->disconnected);
 319
 320	free_attachment(&profile->attach);
 321
 322	/*
 323	 * at this point there are no tasks that can have a reference
 324	 * to rules
 325	 */
 326	for (int i = 0; i < profile->n_rules; i++)
 327		free_ruleset(profile->label.rules[i]);
 328
 329	kfree_sensitive(profile->dirname);
 330
 331	if (profile->data) {
 332		rht = profile->data;
 333		profile->data = NULL;
 334		rhashtable_free_and_destroy(rht, aa_free_data, NULL);
 335		kfree_sensitive(rht);
 336	}
 337
 338	kfree_sensitive(profile->hash);
 339	aa_put_profile_loaddata(profile->rawdata);
 340	aa_label_destroy(&profile->label);
 341
 342	kfree_sensitive(profile);
 343}
 344
 345/**
 346 * aa_alloc_profile - allocate, initialize and return a new profile
 347 * @hname: name of the profile  (NOT NULL)
 348 * @proxy: proxy to use OR null if to allocate a new one
 349 * @gfp: allocation type
 350 *
 351 * Returns: refcount profile or NULL on failure
 352 */
 353struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
 354				    gfp_t gfp)
 355{
 356	struct aa_profile *profile;
 357
 358	/* freed by free_profile - usually through aa_put_profile
 359	 * this adds space for a single ruleset in the rules section of the
 360	 * label
 361	 */
 362	profile = kzalloc(struct_size(profile, label.rules, 1), gfp);
 363	if (!profile)
 364		return NULL;
 365
 366	if (!aa_policy_init(&profile->base, NULL, hname, gfp))
 367		goto fail;
 368	if (!aa_label_init(&profile->label, 1, gfp))
 369		goto fail;
 370
 371	/* allocate the first ruleset, but leave it empty */
 372	profile->label.rules[0] = aa_alloc_ruleset(gfp);
 373	if (!profile->label.rules[0])
 374		goto fail;
 375	profile->n_rules = 1;
 376
 377	/* update being set needed by fs interface */
 378	if (!proxy) {
 379		proxy = aa_alloc_proxy(&profile->label, gfp);
 380		if (!proxy)
 381			goto fail;
 382	} else
 383		aa_get_proxy(proxy);
 384	profile->label.proxy = proxy;
 385
 386	profile->label.hname = profile->base.hname;
 387	profile->label.flags |= FLAG_PROFILE;
 388	profile->label.vec[0] = profile;
 389
 390	profile->signal = SIGKILL;
 391	/* refcount released by caller */
 392	return profile;
 393
 394fail:
 395	aa_free_profile(profile);
 396
 397	return NULL;
 398}
 399
 400static inline bool ANY_RULE_MEDIATES(struct aa_profile *profile,
 401				     unsigned char class)
 402{
 403	int i;
 404
 405	for (i = 0; i < profile->n_rules; i++) {
 406		if (RULE_MEDIATES(profile->label.rules[i], class))
 407			return true;
 408	}
 409	return false;
 410}
 411
 412/* set of rules that are mediated by unconfined */
 413static int unconfined_mediates[] = { AA_CLASS_NS, AA_CLASS_IO_URING, 0 };
 414
 415/* must be called after profile rulesets and start information is setup */
 416void aa_compute_profile_mediates(struct aa_profile *profile)
 417{
 418	int c;
 419
 420	if (profile_unconfined(profile)) {
 421		int *pos;
 422
 423		for (pos = unconfined_mediates; *pos; pos++) {
 424			if (ANY_RULE_MEDIATES(profile, *pos))
 425				profile->label.mediates |= ((u64) 1) << AA_CLASS_NS;
 426		}
 427		return;
 428	}
 429	for (c = 0; c <= AA_CLASS_LAST; c++) {
 430		if (ANY_RULE_MEDIATES(profile, c))
 431			profile->label.mediates |= ((u64) 1) << c;
 432	}
 433}
 434
 435/* TODO: profile accounting - setup in remove */
 436
 437/**
 438 * __strn_find_child - find a profile on @head list using substring of @name
 439 * @head: list to search  (NOT NULL)
 440 * @name: name of profile (NOT NULL)
 441 * @len: length of @name substring to match
 442 *
 443 * Requires: rcu_read_lock be held
 444 *
 445 * Returns: unrefcounted profile ptr, or NULL if not found
 446 */
 447static struct aa_profile *__strn_find_child(struct list_head *head,
 448					    const char *name, int len)
 449{
 450	return (struct aa_profile *)__policy_strn_find(head, name, len);
 451}
 452
 453/**
 454 * __find_child - find a profile on @head list with a name matching @name
 455 * @head: list to search  (NOT NULL)
 456 * @name: name of profile (NOT NULL)
 457 *
 458 * Requires: rcu_read_lock be held
 459 *
 460 * Returns: unrefcounted profile ptr, or NULL if not found
 461 */
 462static struct aa_profile *__find_child(struct list_head *head, const char *name)
 463{
 464	return __strn_find_child(head, name, strlen(name));
 465}
 466
 467/**
 468 * aa_find_child - find a profile by @name in @parent
 469 * @parent: profile to search  (NOT NULL)
 470 * @name: profile name to search for  (NOT NULL)
 471 *
 472 * Returns: a refcounted profile or NULL if not found
 473 */
 474struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
 475{
 476	struct aa_profile *profile;
 477
 478	rcu_read_lock();
 479	do {
 480		profile = __find_child(&parent->base.profiles, name);
 481	} while (profile && !aa_get_profile_not0(profile));
 482	rcu_read_unlock();
 483
 484	/* refcount released by caller */
 485	return profile;
 486}
 487
 488/**
 489 * __lookup_parent - lookup the parent of a profile of name @hname
 490 * @ns: namespace to lookup profile in  (NOT NULL)
 491 * @hname: hierarchical profile name to find parent of  (NOT NULL)
 492 *
 493 * Lookups up the parent of a fully qualified profile name, the profile
 494 * that matches hname does not need to exist, in general this
 495 * is used to load a new profile.
 496 *
 497 * Requires: rcu_read_lock be held
 498 *
 499 * Returns: unrefcounted policy or NULL if not found
 500 */
 501static struct aa_policy *__lookup_parent(struct aa_ns *ns,
 502					 const char *hname)
 503{
 504	struct aa_policy *policy;
 505	struct aa_profile *profile = NULL;
 506	char *split;
 507
 508	policy = &ns->base;
 509
 510	for (split = strstr(hname, "//"); split;) {
 511		profile = __strn_find_child(&policy->profiles, hname,
 512					    split - hname);
 513		if (!profile)
 514			return NULL;
 515		policy = &profile->base;
 516		hname = split + 2;
 517		split = strstr(hname, "//");
 518	}
 519	if (!profile)
 520		return &ns->base;
 521	return &profile->base;
 522}
 523
 524/**
 525 * __create_missing_ancestors - create place holders for missing ancestors
 526 * @ns: namespace to lookup profile in (NOT NULL)
 527 * @hname: hierarchical profile name to find parent of (NOT NULL)
 528 * @gfp: type of allocation.
 529 *
 530 * Requires: ns mutex lock held
 531 *
 532 * Return: unrefcounted parent policy on success or %NULL if error creating
 533 *          place holder profiles.
 534 */
 535static struct aa_policy *__create_missing_ancestors(struct aa_ns *ns,
 536						    const char *hname,
 537						    gfp_t gfp)
 538{
 539	struct aa_policy *policy;
 540	struct aa_profile *parent, *profile = NULL;
 541	char *split;
 542
 543	AA_BUG(!ns);
 544	AA_BUG(!hname);
 545
 546	policy = &ns->base;
 547
 548	for (split = strstr(hname, "//"); split;) {
 549		parent = profile;
 550		profile = __strn_find_child(&policy->profiles, hname,
 551					    split - hname);
 552		if (!profile) {
 553			const char *name = kstrndup(hname, split - hname,
 554						    gfp);
 555			if (!name)
 556				return NULL;
 557			profile = aa_alloc_null(parent, name, gfp);
 558			kfree(name);
 559			if (!profile)
 560				return NULL;
 561			if (!parent)
 562				profile->ns = aa_get_ns(ns);
 563		}
 564		policy = &profile->base;
 565		hname = split + 2;
 566		split = strstr(hname, "//");
 567	}
 568	if (!profile)
 569		return &ns->base;
 570	return &profile->base;
 571}
 572
 573/**
 574 * __lookupn_profile - lookup the profile matching @hname
 575 * @base: base list to start looking up profile name from  (NOT NULL)
 576 * @hname: hierarchical profile name  (NOT NULL)
 577 * @n: length of @hname
 578 *
 579 * Requires: rcu_read_lock be held
 580 *
 581 * Returns: unrefcounted profile pointer or NULL if not found
 582 *
 583 * Do a relative name lookup, recursing through profile tree.
 584 */
 585static struct aa_profile *__lookupn_profile(struct aa_policy *base,
 586					    const char *hname, size_t n)
 587{
 588	struct aa_profile *profile = NULL;
 589	const char *split;
 590
 591	for (split = strnstr(hname, "//", n); split;
 592	     split = strnstr(hname, "//", n)) {
 593		profile = __strn_find_child(&base->profiles, hname,
 594					    split - hname);
 595		if (!profile)
 596			return NULL;
 597
 598		base = &profile->base;
 599		n -= split + 2 - hname;
 600		hname = split + 2;
 601	}
 602
 603	if (n)
 604		return __strn_find_child(&base->profiles, hname, n);
 605	return NULL;
 606}
 607
 608static struct aa_profile *__lookup_profile(struct aa_policy *base,
 609					   const char *hname)
 610{
 611	return __lookupn_profile(base, hname, strlen(hname));
 612}
 613
 614/**
 615 * aa_lookupn_profile - find a profile by its full or partial name
 616 * @ns: the namespace to start from (NOT NULL)
 617 * @hname: name to do lookup on.  Does not contain namespace prefix (NOT NULL)
 618 * @n: size of @hname
 619 *
 620 * Returns: refcounted profile or NULL if not found
 621 */
 622struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
 623				      size_t n)
 624{
 625	struct aa_profile *profile;
 626
 627	rcu_read_lock();
 628	do {
 629		profile = __lookupn_profile(&ns->base, hname, n);
 630	} while (profile && !aa_get_profile_not0(profile));
 631	rcu_read_unlock();
 632
 633	/* the unconfined profile is not in the regular profile list */
 634	if (!profile && strncmp(hname, "unconfined", n) == 0)
 635		profile = aa_get_newest_profile(ns->unconfined);
 636
 637	/* refcount released by caller */
 638	return profile;
 639}
 640
 641struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
 642					const char *fqname, size_t n)
 643{
 644	struct aa_profile *profile;
 645	struct aa_ns *ns;
 646	const char *name, *ns_name;
 647	size_t ns_len;
 648
 649	name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
 650	if (ns_name) {
 651		ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
 652		if (!ns)
 653			return NULL;
 654	} else
 655		ns = aa_get_ns(labels_ns(base));
 656
 657	if (name)
 658		profile = aa_lookupn_profile(ns, name, n - (name - fqname));
 659	else if (ns)
 660		/* default profile for ns, currently unconfined */
 661		profile = aa_get_newest_profile(ns->unconfined);
 662	else
 663		profile = NULL;
 664	aa_put_ns(ns);
 665
 666	return profile;
 667}
 668
 669
 670struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
 671				 gfp_t gfp)
 672{
 673	struct aa_profile *profile;
 674	struct aa_ruleset *rules;
 675
 676	profile = aa_alloc_profile(name, NULL, gfp);
 677	if (!profile)
 678		return NULL;
 679
 680	/* TODO: ideally we should inherit abi from parent */
 681	profile->label.flags |= FLAG_NULL;
 682	profile->attach.xmatch = aa_get_pdb(nullpdb);
 683	rules = profile->label.rules[0];
 684	rules->file = aa_get_pdb(nullpdb);
 685	rules->policy = aa_get_pdb(nullpdb);
 686	aa_compute_profile_mediates(profile);
 687
 688	if (parent) {
 689		profile->path_flags = parent->path_flags;
 690		/* override/inherit what is mediated from parent */
 691		profile->label.mediates = parent->label.mediates;
 692		/* released on free_profile */
 693		rcu_assign_pointer(profile->parent, aa_get_profile(parent));
 694		profile->ns = aa_get_ns(parent->ns);
 695	}
 696
 697	return profile;
 698}
 699
 700/**
 701 * aa_new_learning_profile - create or find a null-X learning profile
 702 * @parent: profile that caused this profile to be created (NOT NULL)
 703 * @hat: true if the null- learning profile is a hat
 704 * @base: name to base the null profile off of
 705 * @gfp: type of allocation
 706 *
 707 * Find/Create a null- complain mode profile used in learning mode.  The
 708 * name of the profile is unique and follows the format of parent//null-XXX.
 709 * where XXX is based on the @name or if that fails or is not supplied
 710 * a unique number
 711 *
 712 * null profiles are added to the profile list but the list does not
 713 * hold a count on them so that they are automatically released when
 714 * not in use.
 715 *
 716 * Returns: new refcounted profile else NULL on failure
 717 */
 718struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
 719					   const char *base, gfp_t gfp)
 720{
 721	struct aa_profile *p, *profile;
 722	const char *bname;
 723	char *name = NULL;
 724
 725	AA_BUG(!parent);
 726
 727	if (base) {
 728		name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
 729			       gfp);
 730		if (name) {
 731			sprintf(name, "%s//null-%s", parent->base.hname, base);
 732			goto name;
 733		}
 734		/* fall through to try shorter uniq */
 735	}
 736
 737	name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
 738	if (!name)
 739		return NULL;
 740	sprintf(name, "%s//null-%x", parent->base.hname,
 741		atomic_inc_return(&parent->ns->uniq_null));
 742
 743name:
 744	/* lookup to see if this is a dup creation */
 745	bname = basename(name);
 746	profile = aa_find_child(parent, bname);
 747	if (profile)
 748		goto out;
 749
 750	profile = aa_alloc_null(parent, name, gfp);
 751	if (!profile)
 752		goto fail;
 753	profile->mode = APPARMOR_COMPLAIN;
 754	if (hat)
 755		profile->label.flags |= FLAG_HAT;
 756
 757	mutex_lock_nested(&profile->ns->lock, profile->ns->level);
 758	p = __find_child(&parent->base.profiles, bname);
 759	if (p) {
 760		aa_free_profile(profile);
 761		profile = aa_get_profile(p);
 762	} else {
 763		__add_profile(&parent->base.profiles, profile);
 764	}
 765	mutex_unlock(&profile->ns->lock);
 766
 767	/* refcount released by caller */
 768out:
 769	kfree(name);
 770
 771	return profile;
 772
 773fail:
 774	kfree(name);
 775	aa_free_profile(profile);
 776	return NULL;
 777}
 778
 779/**
 780 * replacement_allowed - test to see if replacement is allowed
 781 * @profile: profile to test if it can be replaced  (MAYBE NULL)
 782 * @noreplace: true if replacement shouldn't be allowed but addition is okay
 783 * @info: Returns - info about why replacement failed (NOT NULL)
 784 *
 785 * Returns: %0 if replacement allowed else error code
 786 */
 787static int replacement_allowed(struct aa_profile *profile, int noreplace,
 788			       const char **info)
 789{
 790	if (profile) {
 791		if (profile->label.flags & FLAG_IMMUTIBLE) {
 792			*info = "cannot replace immutable profile";
 793			return -EPERM;
 794		} else if (noreplace) {
 795			*info = "profile already exists";
 796			return -EEXIST;
 797		}
 798	}
 799	return 0;
 800}
 801
 802/* audit callback for net specific fields */
 803static void audit_cb(struct audit_buffer *ab, void *va)
 804{
 805	struct common_audit_data *sa = va;
 806	struct apparmor_audit_data *ad = aad(sa);
 807
 808	if (ad->iface.ns) {
 809		audit_log_format(ab, " ns=");
 810		audit_log_untrustedstring(ab, ad->iface.ns);
 811	}
 812}
 813
 814/**
 815 * audit_policy - Do auditing of policy changes
 816 * @subj_label: label to check if it can manage policy
 817 * @op: policy operation being performed
 818 * @ns_name: name of namespace being manipulated
 819 * @name: name of profile being manipulated (NOT NULL)
 820 * @info: any extra information to be audited (MAYBE NULL)
 821 * @error: error code
 822 *
 823 * Returns: the error to be returned after audit is done
 824 */
 825static int audit_policy(struct aa_label *subj_label, const char *op,
 826			const char *ns_name, const char *name,
 827			const char *info, int error)
 828{
 829	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, op);
 830
 831	ad.iface.ns = ns_name;
 832	ad.name = name;
 833	ad.info = info;
 834	ad.error = error;
 835	ad.subj_label = subj_label;
 836
 837	aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, audit_cb);
 838
 839	return error;
 840}
 841
 842/* don't call out to other LSMs in the stack for apparmor policy admin
 843 * permissions
 844 */
 845static int policy_ns_capable(const struct cred *subj_cred,
 846			     struct aa_label *label,
 847			     struct user_namespace *userns, int cap)
 848{
 849	int err;
 850
 851	/* check for MAC_ADMIN cap in cred */
 852	err = cap_capable(subj_cred, userns, cap, CAP_OPT_NONE);
 853	if (!err)
 854		err = aa_capable(subj_cred, label, cap, CAP_OPT_NONE);
 855
 856	return err;
 857}
 858
 859/**
 860 * aa_policy_view_capable - check if viewing policy in at @ns is allowed
 861 * @subj_cred: cred of subject
 862 * @label: label that is trying to view policy in ns
 863 * @ns: namespace being viewed by @label (may be NULL if @label's ns)
 864 *
 865 * Returns: true if viewing policy is allowed
 866 *
 867 * If @ns is NULL then the namespace being viewed is assumed to be the
 868 * tasks current namespace.
 869 */
 870bool aa_policy_view_capable(const struct cred *subj_cred,
 871			     struct aa_label *label, struct aa_ns *ns)
 872{
 873	struct user_namespace *user_ns = subj_cred->user_ns;
 874	struct aa_ns *view_ns = labels_view(label);
 875	bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
 876			       in_egroup_p(make_kgid(user_ns, 0));
 877	bool response = false;
 878	if (!ns)
 879		ns = view_ns;
 880
 881	if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
 882	    (user_ns == &init_user_ns ||
 883	     (unprivileged_userns_apparmor_policy != 0 &&
 884	      user_ns->level == view_ns->level)))
 885		response = true;
 886
 887	return response;
 888}
 889
 890bool aa_policy_admin_capable(const struct cred *subj_cred,
 891			     struct aa_label *label, struct aa_ns *ns)
 892{
 893	struct user_namespace *user_ns = subj_cred->user_ns;
 894	bool capable = policy_ns_capable(subj_cred, label, user_ns,
 895					 CAP_MAC_ADMIN) == 0;
 896
 897	AA_DEBUG(DEBUG_POLICY, "cap_mac_admin? %d\n", capable);
 898	AA_DEBUG(DEBUG_POLICY, "policy locked? %d\n", aa_g_lock_policy);
 899
 900	return aa_policy_view_capable(subj_cred, label, ns) && capable &&
 901		!aa_g_lock_policy;
 902}
 903
 904bool aa_current_policy_view_capable(struct aa_ns *ns)
 905{
 906	struct aa_label *label;
 907	bool needput, res;
 908
 909	label = __begin_current_label_crit_section(&needput);
 910	res = aa_policy_view_capable(current_cred(), label, ns);
 911	__end_current_label_crit_section(label, needput);
 912
 913	return res;
 914}
 915
 916bool aa_current_policy_admin_capable(struct aa_ns *ns)
 917{
 918	struct aa_label *label;
 919	bool needput, res;
 920
 921	label = __begin_current_label_crit_section(&needput);
 922	res = aa_policy_admin_capable(current_cred(), label, ns);
 923	__end_current_label_crit_section(label, needput);
 924
 925	return res;
 926}
 927
 928static bool is_subset_of_obj_privilege(const struct cred *cred,
 929				       struct aa_label *label,
 930				       const struct cred *ocred)
 931{
 932	if (cred == ocred)
 933		return true;
 934
 935	if (!aa_label_is_subset(label, cred_label(ocred)))
 936		return false;
 937	/* don't allow crossing userns for now */
 938	if (cred->user_ns != ocred->user_ns)
 939		return false;
 940	if (!cap_issubset(cred->cap_inheritable, ocred->cap_inheritable))
 941		return false;
 942	if (!cap_issubset(cred->cap_permitted, ocred->cap_permitted))
 943		return false;
 944	if (!cap_issubset(cred->cap_effective, ocred->cap_effective))
 945		return false;
 946	if (!cap_issubset(cred->cap_bset, ocred->cap_bset))
 947		return false;
 948	if (!cap_issubset(cred->cap_ambient, ocred->cap_ambient))
 949		return false;
 950	return true;
 951}
 952
 953
 954/**
 955 * aa_may_manage_policy - can the current task manage policy
 956 * @subj_cred: subjects cred
 957 * @label: label to check if it can manage policy
 958 * @ns: namespace being managed by @label (may be NULL if @label's ns)
 959 * @ocred: object cred if request is coming from an open object
 960 * @mask: contains the policy manipulation operation being done
 961 *
 962 * Returns: 0 if the task is allowed to manipulate policy else error
 963 */
 964int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
 965			 struct aa_ns *ns, const struct cred *ocred, u32 mask)
 966{
 967	const char *op;
 968
 969	if (mask & AA_MAY_REMOVE_POLICY)
 970		op = OP_PROF_RM;
 971	else if (mask & AA_MAY_REPLACE_POLICY)
 972		op = OP_PROF_REPL;
 973	else
 974		op = OP_PROF_LOAD;
 975
 976	/* check if loading policy is locked out */
 977	if (aa_g_lock_policy)
 978		return audit_policy(label, op, NULL, NULL, "policy_locked",
 979				    -EACCES);
 980
 981	if (ocred && !is_subset_of_obj_privilege(subj_cred, label, ocred))
 982		return audit_policy(label, op, NULL, NULL,
 983				    "not privileged for target profile",
 984				    -EACCES);
 985
 986	if (!aa_policy_admin_capable(subj_cred, label, ns))
 987		return audit_policy(label, op, NULL, NULL, "not policy admin",
 988				    -EACCES);
 989
 990	/* TODO: add fine grained mediation of policy loads */
 991	return 0;
 992}
 993
 994static struct aa_profile *__list_lookup_parent(struct list_head *lh,
 995					       struct aa_profile *profile)
 996{
 997	const char *base = basename(profile->base.hname);
 998	long len = base - profile->base.hname;
 999	struct aa_load_ent *ent;
1000
1001	/* parent won't have trailing // so remove from len */
1002	if (len <= 2)
1003		return NULL;
1004	len -= 2;
1005
1006	list_for_each_entry(ent, lh, list) {
1007		if (ent->new == profile)
1008			continue;
1009		if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
1010		    0 && ent->new->base.hname[len] == 0)
1011			return ent->new;
1012	}
1013
1014	return NULL;
1015}
1016
1017/**
1018 * __replace_profile - replace @old with @new on a list
1019 * @old: profile to be replaced  (NOT NULL)
1020 * @new: profile to replace @old with  (NOT NULL)
1021 *
1022 * Will duplicate and refcount elements that @new inherits from @old
1023 * and will inherit @old children.
1024 *
1025 * refcount @new for list, put @old list refcount
1026 *
1027 * Requires: namespace list lock be held, or list not be shared
1028 */
1029static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
1030{
1031	struct aa_profile *child, *tmp;
1032
1033	if (!list_empty(&old->base.profiles)) {
1034		LIST_HEAD(lh);
1035		list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
1036
1037		list_for_each_entry_safe(child, tmp, &lh, base.list) {
1038			struct aa_profile *p;
1039
1040			list_del_init(&child->base.list);
1041			p = __find_child(&new->base.profiles, child->base.name);
1042			if (p) {
1043				/* @p replaces @child  */
1044				__replace_profile(child, p);
1045				continue;
1046			}
1047
1048			/* inherit @child and its children */
1049			/* TODO: update hname of inherited children */
1050			/* list refcount transferred to @new */
1051			p = aa_deref_parent(child);
1052			rcu_assign_pointer(child->parent, aa_get_profile(new));
1053			list_add_rcu(&child->base.list, &new->base.profiles);
1054			aa_put_profile(p);
1055		}
1056	}
1057
1058	if (!rcu_access_pointer(new->parent)) {
1059		struct aa_profile *parent = aa_deref_parent(old);
1060		rcu_assign_pointer(new->parent, aa_get_profile(parent));
1061	}
1062	aa_label_replace(&old->label, &new->label);
1063	/* migrate dents must come after label replacement b/c update */
1064	__aafs_profile_migrate_dents(old, new);
1065
1066	if (list_empty(&new->base.list)) {
1067		/* new is not on a list already */
1068		list_replace_rcu(&old->base.list, &new->base.list);
1069		aa_get_profile(new);
1070		aa_put_profile(old);
1071	} else
1072		__list_remove_profile(old);
1073}
1074
1075/**
1076 * __lookup_replace - lookup replacement information for a profile
1077 * @ns: namespace the lookup occurs in
1078 * @hname: name of profile to lookup
1079 * @noreplace: true if not replacing an existing profile
1080 * @p: Returns - profile to be replaced
1081 * @info: Returns - info string on why lookup failed
1082 *
1083 * Returns: profile to replace (no ref) on success else ptr error
1084 */
1085static int __lookup_replace(struct aa_ns *ns, const char *hname,
1086			    bool noreplace, struct aa_profile **p,
1087			    const char **info)
1088{
1089	*p = aa_get_profile(__lookup_profile(&ns->base, hname));
1090	if (*p) {
1091		int error = replacement_allowed(*p, noreplace, info);
1092		if (error) {
1093			*info = "profile can not be replaced";
1094			return error;
1095		}
1096	}
1097
1098	return 0;
1099}
1100
1101static void share_name(struct aa_profile *old, struct aa_profile *new)
1102{
1103	aa_put_str(new->base.hname);
1104	aa_get_str(old->base.hname);
1105	new->base.hname = old->base.hname;
1106	new->base.name = old->base.name;
1107	new->label.hname = old->label.hname;
1108}
1109
1110/* Update to newest version of parent after previous replacements
1111 * Returns: unrefcount newest version of parent
1112 */
1113static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
1114{
1115	struct aa_profile *parent, *newest;
1116
1117	parent = rcu_dereference_protected(new->parent,
1118					   mutex_is_locked(&new->ns->lock));
1119	newest = aa_get_newest_profile(parent);
1120
1121	/* parent replaced in this atomic set? */
1122	if (newest != parent) {
1123		aa_put_profile(parent);
1124		rcu_assign_pointer(new->parent, newest);
1125	} else
1126		aa_put_profile(newest);
1127
1128	return newest;
1129}
1130
1131/**
1132 * aa_replace_profiles - replace profile(s) on the profile list
1133 * @policy_ns: namespace load is occurring on
1134 * @label: label that is attempting to load/replace policy
1135 * @mask: permission mask
1136 * @udata: serialized data stream  (NOT NULL)
1137 *
1138 * unpack and replace a profile on the profile list and uses of that profile
1139 * by any task creds via invalidating the old version of the profile, which
1140 * tasks will notice to update their own cred.  If the profile does not exist
1141 * on the profile list it is added.
1142 *
1143 * Returns: size of data consumed else error code on failure.
1144 */
1145ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
1146			    u32 mask, struct aa_loaddata *udata)
1147{
1148	const char *ns_name = NULL, *info = NULL;
1149	struct aa_ns *ns = NULL;
1150	struct aa_load_ent *ent, *tmp;
1151	struct aa_loaddata *rawdata_ent;
1152	const char *op;
1153	ssize_t count, error;
1154	LIST_HEAD(lh);
1155
1156	op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
1157	aa_get_profile_loaddata(udata);
1158	/* released below */
1159	error = aa_unpack(udata, &lh, &ns_name);
1160	if (error)
1161		goto out;
1162
1163	/* ensure that profiles are all for the same ns
1164	 * TODO: update locking to remove this constraint. All profiles in
1165	 *       the load set must succeed as a set or the load will
1166	 *       fail. Sort ent list and take ns locks in hierarchy order
1167	 */
1168	count = 0;
1169	list_for_each_entry(ent, &lh, list) {
1170		if (ns_name) {
1171			if (ent->ns_name &&
1172			    strcmp(ent->ns_name, ns_name) != 0) {
1173				info = "policy load has mixed namespaces";
1174				error = -EACCES;
1175				goto fail;
1176			}
1177		} else if (ent->ns_name) {
1178			if (count) {
1179				info = "policy load has mixed namespaces";
1180				error = -EACCES;
1181				goto fail;
1182			}
1183			ns_name = ent->ns_name;
1184			ent->ns_name = NULL;
1185		} else
1186			count++;
1187	}
1188	if (ns_name) {
1189		ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
1190				   ns_name);
1191		if (IS_ERR(ns)) {
1192			op = OP_PROF_LOAD;
1193			info = "failed to prepare namespace";
1194			error = PTR_ERR(ns);
1195			ns = NULL;
1196			ent = NULL;
1197			goto fail;
1198		}
1199	} else
1200		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
1201
1202	mutex_lock_nested(&ns->lock, ns->level);
1203	/* check for duplicate rawdata blobs: space and file dedup */
1204	if (!list_empty(&ns->rawdata_list)) {
1205		list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
1206			if (aa_rawdata_eq(rawdata_ent, udata)) {
1207				struct aa_loaddata *tmp;
1208
1209				tmp = aa_get_profile_loaddata(rawdata_ent);
1210				/* check we didn't fail the race */
1211				if (tmp) {
1212					aa_put_profile_loaddata(udata);
1213					udata = tmp;
1214					break;
1215				}
1216			}
1217		}
1218	}
1219	/* setup parent and ns info */
1220	list_for_each_entry(ent, &lh, list) {
1221		struct aa_policy *policy;
1222		struct aa_profile *p;
1223
1224		if (aa_g_export_binary)
1225			ent->new->rawdata = aa_get_profile_loaddata(udata);
1226		error = __lookup_replace(ns, ent->new->base.hname,
1227					 !(mask & AA_MAY_REPLACE_POLICY),
1228					 &ent->old, &info);
1229		if (error)
1230			goto fail_lock;
1231
1232		if (ent->new->rename) {
1233			error = __lookup_replace(ns, ent->new->rename,
1234						!(mask & AA_MAY_REPLACE_POLICY),
1235						&ent->rename, &info);
1236			if (error)
1237				goto fail_lock;
1238		}
1239
1240		/* released when @new is freed */
1241		ent->new->ns = aa_get_ns(ns);
1242
1243		if (ent->old || ent->rename)
1244			continue;
1245
1246		/* no ref on policy only use inside lock */
1247		p = NULL;
1248		policy = __lookup_parent(ns, ent->new->base.hname);
1249		if (!policy) {
1250			/* first check for parent in the load set */
1251			p = __list_lookup_parent(&lh, ent->new);
1252			if (!p) {
1253				/*
1254				 * fill in missing parent with null
1255				 * profile that doesn't have
1256				 * permissions. This allows for
1257				 * individual profile loading where
1258				 * the child is loaded before the
1259				 * parent, and outside of the current
1260				 * atomic set. This unfortunately can
1261				 * happen with some userspaces.  The
1262				 * null profile will be replaced once
1263				 * the parent is loaded.
1264				 */
1265				policy = __create_missing_ancestors(ns,
1266							ent->new->base.hname,
1267							GFP_KERNEL);
1268				if (!policy) {
1269					error = -ENOENT;
1270					info = "parent does not exist";
1271					goto fail_lock;
1272				}
1273			}
1274		}
1275		if (!p && policy != &ns->base)
1276			/* released on profile replacement or free_profile */
1277			p = (struct aa_profile *) policy;
1278		rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1279	}
1280
1281	/* create new fs entries for introspection if needed */
1282	if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
1283		error = __aa_fs_create_rawdata(ns, udata);
1284		if (error) {
1285			info = "failed to create raw_data dir and files";
1286			ent = NULL;
1287			goto fail_lock;
1288		}
1289	}
1290	list_for_each_entry(ent, &lh, list) {
1291		if (!ent->old) {
1292			struct dentry *parent;
1293			if (rcu_access_pointer(ent->new->parent)) {
1294				struct aa_profile *p;
1295				p = aa_deref_parent(ent->new);
1296				parent = prof_child_dir(p);
1297			} else
1298				parent = ns_subprofs_dir(ent->new->ns);
1299			error = __aafs_profile_mkdir(ent->new, parent);
1300		}
1301
1302		if (error) {
1303			info = "failed to create";
1304			goto fail_lock;
1305		}
1306	}
1307
1308	/* Done with checks that may fail - do actual replacement */
1309	__aa_bump_ns_revision(ns);
1310	if (aa_g_export_binary)
1311		__aa_loaddata_update(udata, ns->revision);
1312	list_for_each_entry_safe(ent, tmp, &lh, list) {
1313		list_del_init(&ent->list);
1314		op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1315
1316		if (ent->old && ent->old->rawdata == ent->new->rawdata &&
1317		    ent->new->rawdata) {
1318			/* dedup actual profile replacement */
1319			audit_policy(label, op, ns_name, ent->new->base.hname,
1320				     "same as current profile, skipping",
1321				     error);
1322			/* break refcount cycle with proxy. */
1323			aa_put_proxy(ent->new->label.proxy);
1324			ent->new->label.proxy = NULL;
1325			goto skip;
1326		}
1327
1328		/*
1329		 * TODO: finer dedup based on profile range in data. Load set
1330		 * can differ but profile may remain unchanged
1331		 */
1332		audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1333			     error);
1334
1335		if (ent->old) {
1336			share_name(ent->old, ent->new);
1337			__replace_profile(ent->old, ent->new);
1338		} else {
1339			struct list_head *lh;
1340
1341			if (rcu_access_pointer(ent->new->parent)) {
1342				struct aa_profile *parent;
1343
1344				parent = update_to_newest_parent(ent->new);
1345				lh = &parent->base.profiles;
1346			} else
1347				lh = &ns->base.profiles;
1348			__add_profile(lh, ent->new);
1349		}
1350	skip:
1351		aa_load_ent_free(ent);
1352	}
1353	__aa_labelset_update_subtree(ns);
1354	mutex_unlock(&ns->lock);
1355
1356out:
1357	aa_put_ns(ns);
1358	aa_put_profile_loaddata(udata);
1359	kfree(ns_name);
1360
1361	if (error)
1362		return error;
1363	return udata->size;
1364
1365fail_lock:
1366	mutex_unlock(&ns->lock);
1367
1368	/* audit cause of failure */
1369	op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1370fail:
1371	  audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
1372		       info, error);
1373	/* audit status that rest of profiles in the atomic set failed too */
1374	info = "valid profile in failed atomic policy load";
1375	list_for_each_entry(tmp, &lh, list) {
1376		if (tmp == ent) {
1377			info = "unchecked profile in failed atomic policy load";
1378			/* skip entry that caused failure */
1379			continue;
1380		}
1381		op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1382		audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1383			     error);
1384	}
1385	list_for_each_entry_safe(ent, tmp, &lh, list) {
1386		list_del_init(&ent->list);
1387		aa_load_ent_free(ent);
1388	}
1389
1390	goto out;
1391}
1392
1393/**
1394 * aa_remove_profiles - remove profile(s) from the system
1395 * @policy_ns: namespace the remove is being done from
1396 * @subj: label attempting to remove policy
1397 * @fqname: name of the profile or namespace to remove  (NOT NULL)
1398 * @size: size of the name
1399 *
1400 * Remove a profile or sub namespace from the current namespace, so that
1401 * they can not be found anymore and mark them as replaced by unconfined
1402 *
1403 * NOTE: removing confinement does not restore rlimits to preconfinement values
1404 *
1405 * Returns: size of data consume else error code if fails
1406 */
1407ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
1408			   char *fqname, size_t size)
1409{
1410	struct aa_ns *ns = NULL;
1411	struct aa_profile *profile = NULL;
1412	const char *name = fqname, *info = NULL;
1413	const char *ns_name = NULL;
1414	ssize_t error = 0;
1415
1416	if (*fqname == 0) {
1417		info = "no profile specified";
1418		error = -ENOENT;
1419		goto fail;
1420	}
1421
1422	if (fqname[0] == ':') {
1423		size_t ns_len;
1424
1425		name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1426		/* released below */
1427		ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1428				   ns_name, ns_len);
1429		if (!ns) {
1430			info = "namespace does not exist";
1431			error = -ENOENT;
1432			goto fail;
1433		}
1434	} else
1435		/* released below */
1436		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1437
1438	if (!name) {
1439		/* remove namespace - can only happen if fqname[0] == ':' */
1440		mutex_lock_nested(&ns->parent->lock, ns->parent->level);
1441		__aa_bump_ns_revision(ns);
1442		__aa_remove_ns(ns);
1443		mutex_unlock(&ns->parent->lock);
1444	} else {
1445		/* remove profile */
1446		mutex_lock_nested(&ns->lock, ns->level);
1447		profile = aa_get_profile(__lookup_profile(&ns->base, name));
1448		if (!profile) {
1449			error = -ENOENT;
1450			info = "profile does not exist";
1451			goto fail_ns_lock;
1452		}
1453		name = profile->base.hname;
1454		__aa_bump_ns_revision(ns);
1455		__remove_profile(profile);
1456		__aa_labelset_update_subtree(ns);
1457		mutex_unlock(&ns->lock);
1458	}
1459
1460	/* don't fail removal if audit fails */
1461	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1462			    error);
1463	aa_put_ns(ns);
1464	aa_put_profile(profile);
1465	return size;
1466
1467fail_ns_lock:
1468	mutex_unlock(&ns->lock);
1469	aa_put_ns(ns);
1470
1471fail:
1472	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1473			    error);
1474	return error;
1475}
~/ Read-only file preview.