Linux Audio

Check our new training course

Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * AppArmor security module
   4 *
   5 * This file contains AppArmor LSM hooks.
   6 *
   7 * Copyright (C) 1998-2008 Novell/SUSE
   8 * Copyright 2009-2010 Canonical Ltd.
   9 */
  10
  11#include <linux/lsm_hooks.h>
  12#include <linux/moduleparam.h>
  13#include <linux/mm.h>
  14#include <linux/mman.h>
  15#include <linux/mount.h>
  16#include <linux/namei.h>
  17#include <linux/ptrace.h>
  18#include <linux/ctype.h>
  19#include <linux/sysctl.h>
  20#include <linux/audit.h>
  21#include <linux/user_namespace.h>
  22#include <linux/netfilter_ipv4.h>
  23#include <linux/netfilter_ipv6.h>
 
  24#include <net/sock.h>
  25#include <uapi/linux/mount.h>
 
  26
  27#include "include/apparmor.h"
  28#include "include/apparmorfs.h"
  29#include "include/audit.h"
  30#include "include/capability.h"
  31#include "include/cred.h"
  32#include "include/file.h"
  33#include "include/ipc.h"
  34#include "include/net.h"
  35#include "include/path.h"
  36#include "include/label.h"
  37#include "include/policy.h"
  38#include "include/policy_ns.h"
  39#include "include/procattr.h"
  40#include "include/mount.h"
  41#include "include/secid.h"
  42
  43/* Flag indicating whether initialization completed */
  44int apparmor_initialized;
  45
  46DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
 
 
 
 
 
 
 
 
 
  47
 
 
 
 
 
 
 
  48
  49/*
  50 * LSM hook functions
  51 */
  52
  53/*
  54 * put the associated labels
  55 */
  56static void apparmor_cred_free(struct cred *cred)
  57{
  58	aa_put_label(cred_label(cred));
  59	set_cred_label(cred, NULL);
  60}
  61
  62/*
  63 * allocate the apparmor part of blank credentials
  64 */
  65static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  66{
  67	set_cred_label(cred, NULL);
  68	return 0;
  69}
  70
  71/*
  72 * prepare new cred label for modification by prepare_cred block
  73 */
  74static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  75				 gfp_t gfp)
  76{
  77	set_cred_label(new, aa_get_newest_label(cred_label(old)));
  78	return 0;
  79}
  80
  81/*
  82 * transfer the apparmor data to a blank set of creds
  83 */
  84static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  85{
  86	set_cred_label(new, aa_get_newest_label(cred_label(old)));
  87}
  88
  89static void apparmor_task_free(struct task_struct *task)
  90{
  91
  92	aa_free_task_ctx(task_ctx(task));
  93}
  94
  95static int apparmor_task_alloc(struct task_struct *task,
  96			       unsigned long clone_flags)
  97{
  98	struct aa_task_ctx *new = task_ctx(task);
  99
 100	aa_dup_task_ctx(new, task_ctx(current));
 101
 102	return 0;
 103}
 104
 105static int apparmor_ptrace_access_check(struct task_struct *child,
 106					unsigned int mode)
 107{
 108	struct aa_label *tracer, *tracee;
 
 109	int error;
 110
 
 
 111	tracer = __begin_current_label_crit_section();
 112	tracee = aa_get_task_label(child);
 113	error = aa_may_ptrace(tracer, tracee,
 114			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
 115						  : AA_PTRACE_TRACE);
 116	aa_put_label(tracee);
 117	__end_current_label_crit_section(tracer);
 
 118
 119	return error;
 120}
 121
 122static int apparmor_ptrace_traceme(struct task_struct *parent)
 123{
 124	struct aa_label *tracer, *tracee;
 
 125	int error;
 126
 127	tracee = __begin_current_label_crit_section();
 128	tracer = aa_get_task_label(parent);
 129	error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
 130	aa_put_label(tracer);
 
 
 131	__end_current_label_crit_section(tracee);
 132
 133	return error;
 134}
 135
 136/* Derived from security/commoncap.c:cap_capget */
 137static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
 138			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
 139{
 140	struct aa_label *label;
 141	const struct cred *cred;
 142
 143	rcu_read_lock();
 144	cred = __task_cred(target);
 145	label = aa_get_newest_cred_label(cred);
 146
 147	/*
 148	 * cap_capget is stacked ahead of this and will
 149	 * initialize effective and permitted.
 150	 */
 151	if (!unconfined(label)) {
 152		struct aa_profile *profile;
 153		struct label_it i;
 154
 155		label_for_each_confined(i, label, profile) {
 
 156			if (COMPLAIN_MODE(profile))
 157				continue;
 
 
 158			*effective = cap_intersect(*effective,
 159						   profile->caps.allow);
 160			*permitted = cap_intersect(*permitted,
 161						   profile->caps.allow);
 162		}
 163	}
 164	rcu_read_unlock();
 165	aa_put_label(label);
 166
 167	return 0;
 168}
 169
 170static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
 171			    int cap, unsigned int opts)
 172{
 173	struct aa_label *label;
 174	int error = 0;
 175
 176	label = aa_get_newest_cred_label(cred);
 177	if (!unconfined(label))
 178		error = aa_capable(label, cap, opts);
 179	aa_put_label(label);
 180
 181	return error;
 182}
 183
 184/**
 185 * common_perm - basic common permission check wrapper fn for paths
 186 * @op: operation being checked
 187 * @path: path to check permission of  (NOT NULL)
 188 * @mask: requested permissions mask
 189 * @cond: conditional info for the permission request  (NOT NULL)
 190 *
 191 * Returns: %0 else error code if error or permission denied
 192 */
 193static int common_perm(const char *op, const struct path *path, u32 mask,
 194		       struct path_cond *cond)
 195{
 196	struct aa_label *label;
 197	int error = 0;
 198
 199	label = __begin_current_label_crit_section();
 200	if (!unconfined(label))
 201		error = aa_path_perm(op, label, path, 0, mask, cond);
 
 202	__end_current_label_crit_section(label);
 203
 204	return error;
 205}
 206
 207/**
 208 * common_perm_cond - common permission wrapper around inode cond
 209 * @op: operation being checked
 210 * @path: location to check (NOT NULL)
 211 * @mask: requested permissions mask
 212 *
 213 * Returns: %0 else error code if error or permission denied
 214 */
 215static int common_perm_cond(const char *op, const struct path *path, u32 mask)
 216{
 217	struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
 218				  d_backing_inode(path->dentry)->i_mode
 
 
 
 219	};
 220
 221	if (!path_mediated_fs(path->dentry))
 222		return 0;
 223
 224	return common_perm(op, path, mask, &cond);
 225}
 226
 227/**
 228 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
 229 * @op: operation being checked
 230 * @dir: directory of the dentry  (NOT NULL)
 231 * @dentry: dentry to check  (NOT NULL)
 232 * @mask: requested permissions mask
 233 * @cond: conditional info for the permission request  (NOT NULL)
 234 *
 235 * Returns: %0 else error code if error or permission denied
 236 */
 237static int common_perm_dir_dentry(const char *op, const struct path *dir,
 238				  struct dentry *dentry, u32 mask,
 239				  struct path_cond *cond)
 240{
 241	struct path path = { .mnt = dir->mnt, .dentry = dentry };
 242
 243	return common_perm(op, &path, mask, cond);
 244}
 245
 246/**
 247 * common_perm_rm - common permission wrapper for operations doing rm
 248 * @op: operation being checked
 249 * @dir: directory that the dentry is in  (NOT NULL)
 250 * @dentry: dentry being rm'd  (NOT NULL)
 251 * @mask: requested permission mask
 252 *
 253 * Returns: %0 else error code if error or permission denied
 254 */
 255static int common_perm_rm(const char *op, const struct path *dir,
 256			  struct dentry *dentry, u32 mask)
 257{
 258	struct inode *inode = d_backing_inode(dentry);
 259	struct path_cond cond = { };
 
 260
 261	if (!inode || !path_mediated_fs(dentry))
 262		return 0;
 263
 264	cond.uid = inode->i_uid;
 
 265	cond.mode = inode->i_mode;
 266
 267	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
 268}
 269
 270/**
 271 * common_perm_create - common permission wrapper for operations doing create
 272 * @op: operation being checked
 273 * @dir: directory that dentry will be created in  (NOT NULL)
 274 * @dentry: dentry to create   (NOT NULL)
 275 * @mask: request permission mask
 276 * @mode: created file mode
 277 *
 278 * Returns: %0 else error code if error or permission denied
 279 */
 280static int common_perm_create(const char *op, const struct path *dir,
 281			      struct dentry *dentry, u32 mask, umode_t mode)
 282{
 283	struct path_cond cond = { current_fsuid(), mode };
 284
 285	if (!path_mediated_fs(dir->dentry))
 286		return 0;
 287
 288	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
 289}
 290
 291static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
 292{
 293	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
 294}
 295
 296static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
 297			       umode_t mode)
 298{
 299	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
 300				  S_IFDIR);
 301}
 302
 303static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
 304{
 305	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
 306}
 307
 308static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
 309			       umode_t mode, unsigned int dev)
 310{
 311	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
 312}
 313
 314static int apparmor_path_truncate(const struct path *path)
 315{
 316	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
 317}
 318
 
 
 
 
 
 319static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
 320				 const char *old_name)
 321{
 322	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
 323				  S_IFLNK);
 324}
 325
 326static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
 327			      struct dentry *new_dentry)
 328{
 329	struct aa_label *label;
 330	int error = 0;
 331
 332	if (!path_mediated_fs(old_dentry))
 333		return 0;
 334
 335	label = begin_current_label_crit_section();
 336	if (!unconfined(label))
 337		error = aa_path_link(label, old_dentry, new_dir, new_dentry);
 
 338	end_current_label_crit_section(label);
 339
 340	return error;
 341}
 342
 343static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
 344				const struct path *new_dir, struct dentry *new_dentry)
 
 345{
 346	struct aa_label *label;
 347	int error = 0;
 348
 349	if (!path_mediated_fs(old_dentry))
 350		return 0;
 
 
 351
 352	label = begin_current_label_crit_section();
 353	if (!unconfined(label)) {
 
 
 354		struct path old_path = { .mnt = old_dir->mnt,
 355					 .dentry = old_dentry };
 356		struct path new_path = { .mnt = new_dir->mnt,
 357					 .dentry = new_dentry };
 358		struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
 359					  d_backing_inode(old_dentry)->i_mode
 360		};
 
 
 361
 362		error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
 363				     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
 364				     AA_MAY_SETATTR | AA_MAY_DELETE,
 365				     &cond);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 366		if (!error)
 367			error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
 
 368					     0, MAY_WRITE | AA_MAY_SETATTR |
 369					     AA_MAY_CREATE, &cond);
 370
 371	}
 372	end_current_label_crit_section(label);
 373
 374	return error;
 375}
 376
 377static int apparmor_path_chmod(const struct path *path, umode_t mode)
 378{
 379	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
 380}
 381
 382static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
 383{
 384	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
 385}
 386
 387static int apparmor_inode_getattr(const struct path *path)
 388{
 389	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
 390}
 391
 392static int apparmor_file_open(struct file *file)
 393{
 394	struct aa_file_ctx *fctx = file_ctx(file);
 395	struct aa_label *label;
 396	int error = 0;
 
 397
 398	if (!path_mediated_fs(file->f_path.dentry))
 399		return 0;
 400
 401	/* If in exec, permission is handled by bprm hooks.
 402	 * Cache permissions granted by the previous exec check, with
 403	 * implicit read and executable mmap which are required to
 404	 * actually execute the image.
 
 
 405	 */
 406	if (current->in_execve) {
 407		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
 408		return 0;
 409	}
 410
 411	label = aa_get_newest_cred_label(file->f_cred);
 412	if (!unconfined(label)) {
 
 413		struct inode *inode = file_inode(file);
 414		struct path_cond cond = { inode->i_uid, inode->i_mode };
 
 
 
 
 
 415
 416		error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
 
 417				     aa_map_file_to_perms(file), &cond);
 418		/* todo cache full allowed permissions set and state */
 419		fctx->allow = aa_map_file_to_perms(file);
 420	}
 421	aa_put_label(label);
 422
 423	return error;
 424}
 425
 426static int apparmor_file_alloc_security(struct file *file)
 427{
 428	struct aa_file_ctx *ctx = file_ctx(file);
 429	struct aa_label *label = begin_current_label_crit_section();
 430
 431	spin_lock_init(&ctx->lock);
 432	rcu_assign_pointer(ctx->label, aa_get_label(label));
 433	end_current_label_crit_section(label);
 434	return 0;
 435}
 436
 437static void apparmor_file_free_security(struct file *file)
 438{
 439	struct aa_file_ctx *ctx = file_ctx(file);
 440
 441	if (ctx)
 442		aa_put_label(rcu_access_pointer(ctx->label));
 443}
 444
 445static int common_file_perm(const char *op, struct file *file, u32 mask)
 
 446{
 447	struct aa_label *label;
 448	int error = 0;
 449
 450	/* don't reaudit files closed during inheritance */
 451	if (file->f_path.dentry == aa_null.dentry)
 452		return -EACCES;
 453
 454	label = __begin_current_label_crit_section();
 455	error = aa_file_perm(op, label, file, mask);
 456	__end_current_label_crit_section(label);
 457
 458	return error;
 459}
 460
 461static int apparmor_file_receive(struct file *file)
 462{
 463	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
 
 464}
 465
 466static int apparmor_file_permission(struct file *file, int mask)
 467{
 468	return common_file_perm(OP_FPERM, file, mask);
 469}
 470
 471static int apparmor_file_lock(struct file *file, unsigned int cmd)
 472{
 473	u32 mask = AA_MAY_LOCK;
 474
 475	if (cmd == F_WRLCK)
 476		mask |= MAY_WRITE;
 477
 478	return common_file_perm(OP_FLOCK, file, mask);
 479}
 480
 481static int common_mmap(const char *op, struct file *file, unsigned long prot,
 482		       unsigned long flags)
 483{
 484	int mask = 0;
 485
 486	if (!file || !file_ctx(file))
 487		return 0;
 488
 489	if (prot & PROT_READ)
 490		mask |= MAY_READ;
 491	/*
 492	 * Private mappings don't require write perms since they don't
 493	 * write back to the files
 494	 */
 495	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
 496		mask |= MAY_WRITE;
 497	if (prot & PROT_EXEC)
 498		mask |= AA_EXEC_MMAP;
 499
 500	return common_file_perm(op, file, mask);
 501}
 502
 503static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
 504			      unsigned long prot, unsigned long flags)
 505{
 506	return common_mmap(OP_FMMAP, file, prot, flags);
 507}
 508
 509static int apparmor_file_mprotect(struct vm_area_struct *vma,
 510				  unsigned long reqprot, unsigned long prot)
 511{
 512	return common_mmap(OP_FMPROT, vma->vm_file, prot,
 513			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 514}
 515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 516static int apparmor_sb_mount(const char *dev_name, const struct path *path,
 517			     const char *type, unsigned long flags, void *data)
 518{
 519	struct aa_label *label;
 520	int error = 0;
 521
 522	/* Discard magic */
 523	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
 524		flags &= ~MS_MGC_MSK;
 525
 526	flags &= ~AA_MS_IGNORE_MASK;
 527
 528	label = __begin_current_label_crit_section();
 529	if (!unconfined(label)) {
 530		if (flags & MS_REMOUNT)
 531			error = aa_remount(label, path, flags, data);
 
 532		else if (flags & MS_BIND)
 533			error = aa_bind_mount(label, path, dev_name, flags);
 
 534		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
 535				  MS_UNBINDABLE))
 536			error = aa_mount_change_type(label, path, flags);
 
 537		else if (flags & MS_MOVE)
 538			error = aa_move_mount(label, path, dev_name);
 
 539		else
 540			error = aa_new_mount(label, dev_name, path, type,
 541					     flags, data);
 542	}
 543	__end_current_label_crit_section(label);
 544
 545	return error;
 546}
 547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 548static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
 549{
 550	struct aa_label *label;
 551	int error = 0;
 552
 553	label = __begin_current_label_crit_section();
 554	if (!unconfined(label))
 555		error = aa_umount(label, mnt, flags);
 556	__end_current_label_crit_section(label);
 557
 558	return error;
 559}
 560
 561static int apparmor_sb_pivotroot(const struct path *old_path,
 562				 const struct path *new_path)
 563{
 564	struct aa_label *label;
 565	int error = 0;
 566
 567	label = aa_get_current_label();
 568	if (!unconfined(label))
 569		error = aa_pivotroot(label, old_path, new_path);
 570	aa_put_label(label);
 571
 572	return error;
 573}
 574
 575static int apparmor_getprocattr(struct task_struct *task, char *name,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 576				char **value)
 577{
 578	int error = -ENOENT;
 579	/* released below */
 580	const struct cred *cred = get_task_cred(task);
 581	struct aa_task_ctx *ctx = task_ctx(current);
 582	struct aa_label *label = NULL;
 583
 584	if (strcmp(name, "current") == 0)
 585		label = aa_get_newest_label(cred_label(cred));
 586	else if (strcmp(name, "prev") == 0  && ctx->previous)
 587		label = aa_get_newest_label(ctx->previous);
 588	else if (strcmp(name, "exec") == 0 && ctx->onexec)
 589		label = aa_get_newest_label(ctx->onexec);
 590	else
 591		error = -EINVAL;
 592
 593	if (label)
 594		error = aa_getprocattr(label, value);
 595
 596	aa_put_label(label);
 597	put_cred(cred);
 598
 599	return error;
 600}
 601
 602static int apparmor_setprocattr(const char *name, void *value,
 603				size_t size)
 604{
 605	char *command, *largs = NULL, *args = value;
 606	size_t arg_size;
 607	int error;
 608	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
 
 609
 610	if (size == 0)
 611		return -EINVAL;
 612
 613	/* AppArmor requires that the buffer must be null terminated atm */
 614	if (args[size - 1] != '\0') {
 615		/* null terminate */
 616		largs = args = kmalloc(size + 1, GFP_KERNEL);
 617		if (!args)
 618			return -ENOMEM;
 619		memcpy(args, value, size);
 620		args[size] = '\0';
 621	}
 622
 623	error = -EINVAL;
 624	args = strim(args);
 625	command = strsep(&args, " ");
 626	if (!args)
 627		goto out;
 628	args = skip_spaces(args);
 629	if (!*args)
 630		goto out;
 631
 632	arg_size = size - (args - (largs ? largs : (char *) value));
 633	if (strcmp(name, "current") == 0) {
 634		if (strcmp(command, "changehat") == 0) {
 635			error = aa_setprocattr_changehat(args, arg_size,
 636							 AA_CHANGE_NOFLAGS);
 637		} else if (strcmp(command, "permhat") == 0) {
 638			error = aa_setprocattr_changehat(args, arg_size,
 639							 AA_CHANGE_TEST);
 640		} else if (strcmp(command, "changeprofile") == 0) {
 641			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
 642		} else if (strcmp(command, "permprofile") == 0) {
 643			error = aa_change_profile(args, AA_CHANGE_TEST);
 644		} else if (strcmp(command, "stack") == 0) {
 645			error = aa_change_profile(args, AA_CHANGE_STACK);
 646		} else
 647			goto fail;
 648	} else if (strcmp(name, "exec") == 0) {
 649		if (strcmp(command, "exec") == 0)
 650			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
 651		else if (strcmp(command, "stack") == 0)
 652			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
 653							 AA_CHANGE_STACK));
 654		else
 655			goto fail;
 656	} else
 657		/* only support the "current" and "exec" process attributes */
 658		goto fail;
 659
 660	if (!error)
 661		error = size;
 662out:
 663	kfree(largs);
 664	return error;
 665
 666fail:
 667	aad(&sa)->label = begin_current_label_crit_section();
 668	aad(&sa)->info = name;
 669	aad(&sa)->error = error = -EINVAL;
 670	aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
 671	end_current_label_crit_section(aad(&sa)->label);
 
 
 
 
 
 672	goto out;
 673}
 674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 675/**
 676 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
 677 * @bprm: binprm for the exec  (NOT NULL)
 678 */
 679static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
 680{
 681	struct aa_label *label = aa_current_raw_label();
 682	struct aa_label *new_label = cred_label(bprm->cred);
 683
 684	/* bail out if unconfined or not changing profile */
 685	if ((new_label->proxy == label->proxy) ||
 686	    (unconfined(new_label)))
 687		return;
 688
 689	aa_inherit_files(bprm->cred, current->files);
 690
 691	current->pdeath_signal = 0;
 692
 693	/* reset soft limits and set hard limits for the new label */
 694	__aa_transition_rlimits(label, new_label);
 695}
 696
 697/**
 698 * apparmor_bprm_committed_cred - do cleanup after new creds committed
 699 * @bprm: binprm for the exec  (NOT NULL)
 700 */
 701static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
 702{
 703	/* clear out temporary/transitional state from the context */
 704	aa_clear_task_ctx_trans(task_ctx(current));
 705
 706	return;
 707}
 708
 709static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
 
 
 
 
 
 
 
 
 
 710{
 711	struct aa_label *label = aa_get_task_label(p);
 712	*secid = label->secid;
 
 713	aa_put_label(label);
 714}
 715
 716static int apparmor_task_setrlimit(struct task_struct *task,
 717		unsigned int resource, struct rlimit *new_rlim)
 718{
 719	struct aa_label *label = __begin_current_label_crit_section();
 720	int error = 0;
 721
 722	if (!unconfined(label))
 723		error = aa_task_setrlimit(label, task, resource, new_rlim);
 
 724	__end_current_label_crit_section(label);
 725
 726	return error;
 727}
 728
 729static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
 730			      int sig, const struct cred *cred)
 731{
 
 732	struct aa_label *cl, *tl;
 733	int error;
 734
 
 
 735	if (cred) {
 736		/*
 737		 * Dealing with USB IO specific behavior
 738		 */
 739		cl = aa_get_newest_cred_label(cred);
 740		tl = aa_get_task_label(target);
 741		error = aa_may_signal(cl, tl, sig);
 742		aa_put_label(cl);
 743		aa_put_label(tl);
 744		return error;
 
 
 745	}
 746
 747	cl = __begin_current_label_crit_section();
 748	tl = aa_get_task_label(target);
 749	error = aa_may_signal(cl, tl, sig);
 750	aa_put_label(tl);
 751	__end_current_label_crit_section(cl);
 752
 753	return error;
 754}
 755
 756/**
 757 * apparmor_sk_alloc_security - allocate and attach the sk_security field
 758 */
 759static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
 760{
 761	struct aa_sk_ctx *ctx;
 
 
 
 
 762
 763	ctx = kzalloc(sizeof(*ctx), flags);
 764	if (!ctx)
 765		return -ENOMEM;
 766
 767	SK_CTX(sk) = ctx;
 
 
 
 
 
 
 768
 769	return 0;
 770}
 771
 772/**
 773 * apparmor_sk_free_security - free the sk_security field
 774 */
 775static void apparmor_sk_free_security(struct sock *sk)
 776{
 777	struct aa_sk_ctx *ctx = SK_CTX(sk);
 778
 779	SK_CTX(sk) = NULL;
 780	aa_put_label(ctx->label);
 781	aa_put_label(ctx->peer);
 782	kfree(ctx);
 783}
 784
 785/**
 786 * apparmor_clone_security - clone the sk_security field
 
 
 787 */
 788static void apparmor_sk_clone_security(const struct sock *sk,
 789				       struct sock *newsk)
 790{
 791	struct aa_sk_ctx *ctx = SK_CTX(sk);
 792	struct aa_sk_ctx *new = SK_CTX(newsk);
 793
 
 
 794	new->label = aa_get_label(ctx->label);
 
 
 
 795	new->peer = aa_get_label(ctx->peer);
 796}
 797
 798/**
 799 * apparmor_socket_create - check perms before creating a new socket
 800 */
 801static int apparmor_socket_create(int family, int type, int protocol, int kern)
 802{
 803	struct aa_label *label;
 804	int error = 0;
 805
 806	AA_BUG(in_interrupt());
 807
 808	label = begin_current_label_crit_section();
 809	if (!(kern || unconfined(label)))
 810		error = af_select(family,
 811				  create_perm(label, family, type, protocol),
 812				  aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
 
 813					     family, type, protocol));
 814	end_current_label_crit_section(label);
 815
 816	return error;
 817}
 818
 819/**
 820 * apparmor_socket_post_create - setup the per-socket security struct
 
 
 
 
 
 821 *
 822 * Note:
 823 * -   kernel sockets currently labeled unconfined but we may want to
 824 *     move to a special kernel label
 825 * -   socket may not have sk here if created with sock_create_lite or
 826 *     sock_alloc. These should be accept cases which will be handled in
 827 *     sock_graft.
 828 */
 829static int apparmor_socket_post_create(struct socket *sock, int family,
 830				       int type, int protocol, int kern)
 831{
 832	struct aa_label *label;
 833
 834	if (kern) {
 835		struct aa_ns *ns = aa_get_current_ns();
 836
 837		label = aa_get_label(ns_unconfined(ns));
 838		aa_put_ns(ns);
 839	} else
 840		label = aa_get_current_label();
 841
 842	if (sock->sk) {
 843		struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
 844
 845		aa_put_label(ctx->label);
 846		ctx->label = aa_get_label(label);
 847	}
 848	aa_put_label(label);
 849
 850	return 0;
 851}
 852
 853/**
 854 * apparmor_socket_bind - check perms before bind addr to socket
 855 */
 856static int apparmor_socket_bind(struct socket *sock,
 857				struct sockaddr *address, int addrlen)
 858{
 859	AA_BUG(!sock);
 860	AA_BUG(!sock->sk);
 861	AA_BUG(!address);
 862	AA_BUG(in_interrupt());
 863
 864	return af_select(sock->sk->sk_family,
 865			 bind_perm(sock, address, addrlen),
 866			 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
 867}
 868
 869/**
 870 * apparmor_socket_connect - check perms before connecting @sock to @address
 871 */
 872static int apparmor_socket_connect(struct socket *sock,
 873				   struct sockaddr *address, int addrlen)
 874{
 875	AA_BUG(!sock);
 876	AA_BUG(!sock->sk);
 877	AA_BUG(!address);
 878	AA_BUG(in_interrupt());
 879
 880	return af_select(sock->sk->sk_family,
 881			 connect_perm(sock, address, addrlen),
 882			 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
 883}
 884
 885/**
 886 * apparmor_socket_list - check perms before allowing listen
 887 */
 888static int apparmor_socket_listen(struct socket *sock, int backlog)
 889{
 890	AA_BUG(!sock);
 891	AA_BUG(!sock->sk);
 892	AA_BUG(in_interrupt());
 893
 894	return af_select(sock->sk->sk_family,
 895			 listen_perm(sock, backlog),
 896			 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
 897}
 898
 899/**
 900 * apparmor_socket_accept - check perms before accepting a new connection.
 901 *
 902 * Note: while @newsock is created and has some information, the accept
 903 *       has not been done.
 904 */
 905static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
 906{
 907	AA_BUG(!sock);
 908	AA_BUG(!sock->sk);
 909	AA_BUG(!newsock);
 910	AA_BUG(in_interrupt());
 911
 912	return af_select(sock->sk->sk_family,
 913			 accept_perm(sock, newsock),
 914			 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
 915}
 916
 917static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
 918			    struct msghdr *msg, int size)
 919{
 920	AA_BUG(!sock);
 921	AA_BUG(!sock->sk);
 922	AA_BUG(!msg);
 923	AA_BUG(in_interrupt());
 924
 925	return af_select(sock->sk->sk_family,
 926			 msg_perm(op, request, sock, msg, size),
 927			 aa_sk_perm(op, request, sock->sk));
 928}
 929
 930/**
 931 * apparmor_socket_sendmsg - check perms before sending msg to another socket
 932 */
 933static int apparmor_socket_sendmsg(struct socket *sock,
 934				   struct msghdr *msg, int size)
 935{
 936	return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
 937}
 938
 939/**
 940 * apparmor_socket_recvmsg - check perms before receiving a message
 941 */
 942static int apparmor_socket_recvmsg(struct socket *sock,
 943				   struct msghdr *msg, int size, int flags)
 944{
 945	return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
 946}
 947
 948/* revaliation, get/set attr, shutdown */
 949static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
 950{
 951	AA_BUG(!sock);
 952	AA_BUG(!sock->sk);
 953	AA_BUG(in_interrupt());
 954
 955	return af_select(sock->sk->sk_family,
 956			 sock_perm(op, request, sock),
 957			 aa_sk_perm(op, request, sock->sk));
 958}
 959
 960/**
 961 * apparmor_socket_getsockname - check perms before getting the local address
 962 */
 963static int apparmor_socket_getsockname(struct socket *sock)
 964{
 965	return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
 966}
 967
 968/**
 969 * apparmor_socket_getpeername - check perms before getting remote address
 970 */
 971static int apparmor_socket_getpeername(struct socket *sock)
 972{
 973	return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
 974}
 975
 976/* revaliation, get/set attr, opt */
 977static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
 978			    int level, int optname)
 979{
 980	AA_BUG(!sock);
 981	AA_BUG(!sock->sk);
 982	AA_BUG(in_interrupt());
 983
 984	return af_select(sock->sk->sk_family,
 985			 opt_perm(op, request, sock, level, optname),
 986			 aa_sk_perm(op, request, sock->sk));
 987}
 988
 989/**
 990 * apparmor_getsockopt - check perms before getting socket options
 991 */
 992static int apparmor_socket_getsockopt(struct socket *sock, int level,
 993				      int optname)
 994{
 995	return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
 996				level, optname);
 997}
 998
 999/**
1000 * apparmor_setsockopt - check perms before setting socket options
1001 */
1002static int apparmor_socket_setsockopt(struct socket *sock, int level,
1003				      int optname)
1004{
1005	return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1006				level, optname);
1007}
1008
1009/**
1010 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1011 */
1012static int apparmor_socket_shutdown(struct socket *sock, int how)
1013{
1014	return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1015}
1016
1017#ifdef CONFIG_NETWORK_SECMARK
1018/**
1019 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
 
 
1020 *
1021 * Note: can not sleep may be called with locks held
1022 *
1023 * dont want protocol specific in __skb_recv_datagram()
1024 * to deny an incoming connection  socket_sock_rcv_skb()
1025 */
1026static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1027{
1028	struct aa_sk_ctx *ctx = SK_CTX(sk);
1029
1030	if (!skb->secmark)
1031		return 0;
1032
 
 
 
 
 
 
 
1033	return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1034				      skb->secmark, sk);
1035}
1036#endif
1037
1038
1039static struct aa_label *sk_peer_label(struct sock *sk)
1040{
1041	struct aa_sk_ctx *ctx = SK_CTX(sk);
1042
1043	if (ctx->peer)
1044		return ctx->peer;
1045
1046	return ERR_PTR(-ENOPROTOOPT);
1047}
1048
1049/**
1050 * apparmor_socket_getpeersec_stream - get security context of peer
 
 
 
 
 
1051 *
1052 * Note: for tcp only valid if using ipsec or cipso on lan
1053 */
1054static int apparmor_socket_getpeersec_stream(struct socket *sock,
1055					     char __user *optval,
1056					     int __user *optlen,
1057					     unsigned int len)
1058{
1059	char *name;
1060	int slen, error = 0;
1061	struct aa_label *label;
1062	struct aa_label *peer;
1063
1064	label = begin_current_label_crit_section();
1065	peer = sk_peer_label(sock->sk);
1066	if (IS_ERR(peer)) {
1067		error = PTR_ERR(peer);
1068		goto done;
1069	}
1070	slen = aa_label_asxprint(&name, labels_ns(label), peer,
1071				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1072				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1073	/* don't include terminating \0 in slen, it breaks some apps */
1074	if (slen < 0) {
1075		error = -ENOMEM;
1076	} else {
1077		if (slen > len) {
1078			error = -ERANGE;
1079		} else if (copy_to_user(optval, name, slen)) {
1080			error = -EFAULT;
1081			goto out;
1082		}
1083		if (put_user(slen, optlen))
1084			error = -EFAULT;
1085out:
1086		kfree(name);
1087
1088	}
1089
 
 
 
 
 
1090done:
1091	end_current_label_crit_section(label);
1092
1093	return error;
1094}
1095
1096/**
1097 * apparmor_socket_getpeersec_dgram - get security label of packet
1098 * @sock: the peer socket
1099 * @skb: packet data
1100 * @secid: pointer to where to put the secid of the packet
1101 *
1102 * Sets the netlabel socket state on sk from parent
1103 */
1104static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1105					    struct sk_buff *skb, u32 *secid)
1106
1107{
1108	/* TODO: requires secid support */
1109	return -ENOPROTOOPT;
1110}
1111
1112/**
1113 * apparmor_sock_graft - Initialize newly created socket
1114 * @sk: child sock
1115 * @parent: parent socket
1116 *
1117 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1118 *       just set sk security information off of current creating process label
1119 *       Labeling of sk for accept case - probably should be sock based
1120 *       instead of task, because of the case where an implicitly labeled
1121 *       socket is shared by different tasks.
1122 */
1123static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1124{
1125	struct aa_sk_ctx *ctx = SK_CTX(sk);
1126
1127	if (!ctx->label)
1128		ctx->label = aa_get_current_label();
1129}
1130
1131#ifdef CONFIG_NETWORK_SECMARK
1132static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb,
1133				      struct request_sock *req)
1134{
1135	struct aa_sk_ctx *ctx = SK_CTX(sk);
1136
1137	if (!skb->secmark)
1138		return 0;
1139
1140	return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1141				      skb->secmark, sk);
1142}
1143#endif
1144
1145/*
1146 * The cred blob is a pointer to, not an instance of, an aa_task_ctx.
1147 */
1148struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
1149	.lbs_cred = sizeof(struct aa_task_ctx *),
1150	.lbs_file = sizeof(struct aa_file_ctx),
1151	.lbs_task = sizeof(struct aa_task_ctx),
 
1152};
1153
1154static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
 
 
 
 
 
1155	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1156	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1157	LSM_HOOK_INIT(capget, apparmor_capget),
1158	LSM_HOOK_INIT(capable, apparmor_capable),
1159
 
1160	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1161	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1162	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1163
1164	LSM_HOOK_INIT(path_link, apparmor_path_link),
1165	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1166	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1167	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1168	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1169	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1170	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1171	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1172	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1173	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1174	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1175
1176	LSM_HOOK_INIT(file_open, apparmor_file_open),
1177	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1178	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1179	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1180	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1181	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1182	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1183	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
 
1184
 
 
1185	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1186	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1187
1188	LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1189	LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1190	LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1191
1192	LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1193	LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1194	LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1195	LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1196	LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1197	LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1198	LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1199	LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1200	LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1201	LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1202	LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1203	LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1204	LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1205#ifdef CONFIG_NETWORK_SECMARK
1206	LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1207#endif
1208	LSM_HOOK_INIT(socket_getpeersec_stream,
1209		      apparmor_socket_getpeersec_stream),
1210	LSM_HOOK_INIT(socket_getpeersec_dgram,
1211		      apparmor_socket_getpeersec_dgram),
1212	LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1213#ifdef CONFIG_NETWORK_SECMARK
1214	LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1215#endif
1216
1217	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1218	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1219	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1220	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1221
1222	LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
1223	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1224	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1225
1226	LSM_HOOK_INIT(task_free, apparmor_task_free),
1227	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1228	LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
 
 
1229	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1230	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
 
1231
1232#ifdef CONFIG_AUDIT
1233	LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1234	LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1235	LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1236	LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1237#endif
1238
1239	LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
 
1240	LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1241	LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
 
 
 
 
 
1242};
1243
1244/*
1245 * AppArmor sysfs module parameters
1246 */
1247
1248static int param_set_aabool(const char *val, const struct kernel_param *kp);
1249static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1250#define param_check_aabool param_check_bool
1251static const struct kernel_param_ops param_ops_aabool = {
1252	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1253	.set = param_set_aabool,
1254	.get = param_get_aabool
1255};
1256
1257static int param_set_aauint(const char *val, const struct kernel_param *kp);
1258static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1259#define param_check_aauint param_check_uint
1260static const struct kernel_param_ops param_ops_aauint = {
1261	.set = param_set_aauint,
1262	.get = param_get_aauint
1263};
1264
 
 
 
 
 
 
 
 
 
 
1265static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1266static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1267#define param_check_aalockpolicy param_check_bool
1268static const struct kernel_param_ops param_ops_aalockpolicy = {
1269	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1270	.set = param_set_aalockpolicy,
1271	.get = param_get_aalockpolicy
1272};
1273
1274static int param_set_audit(const char *val, const struct kernel_param *kp);
1275static int param_get_audit(char *buffer, const struct kernel_param *kp);
1276
1277static int param_set_mode(const char *val, const struct kernel_param *kp);
1278static int param_get_mode(char *buffer, const struct kernel_param *kp);
1279
1280/* Flag values, also controllable via /sys/module/apparmor/parameters
1281 * We define special types as we want to do additional mediation.
1282 */
1283
1284/* AppArmor global enforcement switch - complain, enforce, kill */
1285enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1286module_param_call(mode, param_set_mode, param_get_mode,
1287		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1288
1289/* whether policy verification hashing is enabled */
1290bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1291#ifdef CONFIG_SECURITY_APPARMOR_HASH
1292module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1293#endif
1294
 
 
 
 
 
 
 
 
 
 
 
1295/* Debug mode */
1296bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1297module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1298
1299/* Audit mode */
1300enum audit_mode aa_g_audit;
1301module_param_call(audit, param_set_audit, param_get_audit,
1302		  &aa_g_audit, S_IRUSR | S_IWUSR);
1303
1304/* Determines if audit header is included in audited messages.  This
1305 * provides more context if the audit daemon is not running
1306 */
1307bool aa_g_audit_header = true;
1308module_param_named(audit_header, aa_g_audit_header, aabool,
1309		   S_IRUSR | S_IWUSR);
1310
1311/* lock out loading/removal of policy
1312 * TODO: add in at boot loading of policy, which is the only way to
1313 *       load policy, if lock_policy is set
1314 */
1315bool aa_g_lock_policy;
1316module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1317		   S_IRUSR | S_IWUSR);
1318
1319/* Syscall logging mode */
1320bool aa_g_logsyscall;
1321module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1322
1323/* Maximum pathname length before accesses will start getting rejected */
1324unsigned int aa_g_path_max = 2 * PATH_MAX;
1325module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1326
1327/* Determines how paranoid loading of policy is and how much verification
1328 * on the loaded policy is done.
1329 * DEPRECATED: read only as strict checking of load is always done now
1330 * that none root users (user namespaces) can load policy.
1331 */
1332bool aa_g_paranoid_load = true;
1333module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1334
1335static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1336static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1337#define param_check_aaintbool param_check_int
1338static const struct kernel_param_ops param_ops_aaintbool = {
1339	.set = param_set_aaintbool,
1340	.get = param_get_aaintbool
1341};
1342/* Boot time disable flag */
1343static int apparmor_enabled __lsm_ro_after_init = 1;
1344module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1345
1346static int __init apparmor_enabled_setup(char *str)
1347{
1348	unsigned long enabled;
1349	int error = kstrtoul(str, 0, &enabled);
1350	if (!error)
1351		apparmor_enabled = enabled ? 1 : 0;
1352	return 1;
1353}
1354
1355__setup("apparmor=", apparmor_enabled_setup);
1356
1357/* set global flag turning off the ability to load policy */
1358static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1359{
1360	if (!apparmor_enabled)
1361		return -EINVAL;
1362	if (apparmor_initialized && !policy_admin_capable(NULL))
1363		return -EPERM;
1364	return param_set_bool(val, kp);
1365}
1366
1367static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1368{
1369	if (!apparmor_enabled)
1370		return -EINVAL;
1371	if (apparmor_initialized && !policy_view_capable(NULL))
1372		return -EPERM;
1373	return param_get_bool(buffer, kp);
1374}
1375
1376static int param_set_aabool(const char *val, const struct kernel_param *kp)
1377{
1378	if (!apparmor_enabled)
1379		return -EINVAL;
1380	if (apparmor_initialized && !policy_admin_capable(NULL))
1381		return -EPERM;
1382	return param_set_bool(val, kp);
1383}
1384
1385static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1386{
1387	if (!apparmor_enabled)
1388		return -EINVAL;
1389	if (apparmor_initialized && !policy_view_capable(NULL))
1390		return -EPERM;
1391	return param_get_bool(buffer, kp);
1392}
1393
1394static int param_set_aauint(const char *val, const struct kernel_param *kp)
1395{
1396	int error;
1397
1398	if (!apparmor_enabled)
1399		return -EINVAL;
1400	/* file is ro but enforce 2nd line check */
1401	if (apparmor_initialized)
1402		return -EPERM;
1403
1404	error = param_set_uint(val, kp);
 
1405	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1406
1407	return error;
1408}
1409
1410static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1411{
1412	if (!apparmor_enabled)
1413		return -EINVAL;
1414	if (apparmor_initialized && !policy_view_capable(NULL))
1415		return -EPERM;
1416	return param_get_uint(buffer, kp);
1417}
1418
1419/* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1420static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1421{
1422	struct kernel_param kp_local;
1423	bool value;
1424	int error;
1425
1426	if (apparmor_initialized)
1427		return -EPERM;
1428
1429	/* Create local copy, with arg pointing to bool type. */
1430	value = !!*((int *)kp->arg);
1431	memcpy(&kp_local, kp, sizeof(kp_local));
1432	kp_local.arg = &value;
1433
1434	error = param_set_bool(val, &kp_local);
1435	if (!error)
1436		*((int *)kp->arg) = *((bool *)kp_local.arg);
1437	return error;
1438}
1439
1440/*
1441 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1442 * 1/0, this converts the "int that is actually bool" back to bool for
1443 * display in the /sys filesystem, while keeping it "int" for the LSM
1444 * infrastructure.
1445 */
1446static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1447{
1448	struct kernel_param kp_local;
1449	bool value;
1450
1451	/* Create local copy, with arg pointing to bool type. */
1452	value = !!*((int *)kp->arg);
1453	memcpy(&kp_local, kp, sizeof(kp_local));
1454	kp_local.arg = &value;
1455
1456	return param_get_bool(buffer, &kp_local);
1457}
1458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1459static int param_get_audit(char *buffer, const struct kernel_param *kp)
1460{
1461	if (!apparmor_enabled)
1462		return -EINVAL;
1463	if (apparmor_initialized && !policy_view_capable(NULL))
1464		return -EPERM;
1465	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1466}
1467
1468static int param_set_audit(const char *val, const struct kernel_param *kp)
1469{
1470	int i;
1471
1472	if (!apparmor_enabled)
1473		return -EINVAL;
1474	if (!val)
1475		return -EINVAL;
1476	if (apparmor_initialized && !policy_admin_capable(NULL))
1477		return -EPERM;
1478
1479	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1480	if (i < 0)
1481		return -EINVAL;
1482
1483	aa_g_audit = i;
1484	return 0;
1485}
1486
1487static int param_get_mode(char *buffer, const struct kernel_param *kp)
1488{
1489	if (!apparmor_enabled)
1490		return -EINVAL;
1491	if (apparmor_initialized && !policy_view_capable(NULL))
1492		return -EPERM;
1493
1494	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1495}
1496
1497static int param_set_mode(const char *val, const struct kernel_param *kp)
1498{
1499	int i;
1500
1501	if (!apparmor_enabled)
1502		return -EINVAL;
1503	if (!val)
1504		return -EINVAL;
1505	if (apparmor_initialized && !policy_admin_capable(NULL))
1506		return -EPERM;
1507
1508	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1509			 val);
1510	if (i < 0)
1511		return -EINVAL;
1512
1513	aa_g_profile_mode = i;
1514	return 0;
1515}
1516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1517/*
1518 * AppArmor init functions
1519 */
1520
1521/**
1522 * set_init_ctx - set a task context and profile on the first task.
1523 *
1524 * TODO: allow setting an alternate profile than unconfined
1525 */
1526static int __init set_init_ctx(void)
1527{
1528	struct cred *cred = (struct cred *)current->real_cred;
1529
1530	set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1531
1532	return 0;
1533}
1534
1535static void destroy_buffers(void)
1536{
1537	u32 i, j;
1538
1539	for_each_possible_cpu(i) {
1540		for_each_cpu_buffer(j) {
1541			kfree(per_cpu(aa_buffers, i).buf[j]);
1542			per_cpu(aa_buffers, i).buf[j] = NULL;
1543		}
 
 
 
1544	}
 
1545}
1546
1547static int __init alloc_buffers(void)
1548{
1549	u32 i, j;
 
1550
 
 
 
 
1551	for_each_possible_cpu(i) {
1552		for_each_cpu_buffer(j) {
1553			char *buffer;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1554
1555			if (cpu_to_node(i) > num_online_nodes())
1556				/* fallback to kmalloc for offline nodes */
1557				buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1558			else
1559				buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1560						      cpu_to_node(i));
1561			if (!buffer) {
1562				destroy_buffers();
1563				return -ENOMEM;
1564			}
1565			per_cpu(aa_buffers, i).buf[j] = buffer;
1566		}
 
1567	}
1568
1569	return 0;
1570}
1571
1572#ifdef CONFIG_SYSCTL
1573static int apparmor_dointvec(struct ctl_table *table, int write,
1574			     void __user *buffer, size_t *lenp, loff_t *ppos)
1575{
1576	if (!policy_admin_capable(NULL))
1577		return -EPERM;
1578	if (!apparmor_enabled)
1579		return -EINVAL;
1580
1581	return proc_dointvec(table, write, buffer, lenp, ppos);
1582}
1583
1584static struct ctl_path apparmor_sysctl_path[] = {
1585	{ .procname = "kernel", },
1586	{ }
1587};
1588
1589static struct ctl_table apparmor_sysctl_table[] = {
 
1590	{
1591		.procname       = "unprivileged_userns_apparmor_policy",
1592		.data           = &unprivileged_userns_apparmor_policy,
1593		.maxlen         = sizeof(int),
1594		.mode           = 0600,
1595		.proc_handler   = apparmor_dointvec,
1596	},
1597	{ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1598};
1599
1600static int __init apparmor_init_sysctl(void)
1601{
1602	return register_sysctl_paths(apparmor_sysctl_path,
1603				     apparmor_sysctl_table) ? 0 : -ENOMEM;
1604}
1605#else
1606static inline int apparmor_init_sysctl(void)
1607{
1608	return 0;
1609}
1610#endif /* CONFIG_SYSCTL */
1611
1612#if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
1613static unsigned int apparmor_ip_postroute(void *priv,
1614					  struct sk_buff *skb,
1615					  const struct nf_hook_state *state)
1616{
1617	struct aa_sk_ctx *ctx;
1618	struct sock *sk;
1619
1620	if (!skb->secmark)
1621		return NF_ACCEPT;
1622
1623	sk = skb_to_full_sk(skb);
1624	if (sk == NULL)
1625		return NF_ACCEPT;
1626
1627	ctx = SK_CTX(sk);
1628	if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1629				    skb->secmark, sk))
1630		return NF_ACCEPT;
1631
1632	return NF_DROP_ERR(-ECONNREFUSED);
1633
1634}
1635
1636static unsigned int apparmor_ipv4_postroute(void *priv,
1637					    struct sk_buff *skb,
1638					    const struct nf_hook_state *state)
1639{
1640	return apparmor_ip_postroute(priv, skb, state);
1641}
1642
1643#if IS_ENABLED(CONFIG_IPV6)
1644static unsigned int apparmor_ipv6_postroute(void *priv,
1645					    struct sk_buff *skb,
1646					    const struct nf_hook_state *state)
1647{
1648	return apparmor_ip_postroute(priv, skb, state);
1649}
1650#endif
1651
1652static const struct nf_hook_ops apparmor_nf_ops[] = {
1653	{
1654		.hook =         apparmor_ipv4_postroute,
1655		.pf =           NFPROTO_IPV4,
1656		.hooknum =      NF_INET_POST_ROUTING,
1657		.priority =     NF_IP_PRI_SELINUX_FIRST,
1658	},
1659#if IS_ENABLED(CONFIG_IPV6)
1660	{
1661		.hook =         apparmor_ipv6_postroute,
1662		.pf =           NFPROTO_IPV6,
1663		.hooknum =      NF_INET_POST_ROUTING,
1664		.priority =     NF_IP6_PRI_SELINUX_FIRST,
1665	},
1666#endif
1667};
1668
1669static int __net_init apparmor_nf_register(struct net *net)
1670{
1671	int ret;
1672
1673	ret = nf_register_net_hooks(net, apparmor_nf_ops,
1674				    ARRAY_SIZE(apparmor_nf_ops));
1675	return ret;
1676}
1677
1678static void __net_exit apparmor_nf_unregister(struct net *net)
1679{
1680	nf_unregister_net_hooks(net, apparmor_nf_ops,
1681				ARRAY_SIZE(apparmor_nf_ops));
1682}
1683
1684static struct pernet_operations apparmor_net_ops = {
1685	.init = apparmor_nf_register,
1686	.exit = apparmor_nf_unregister,
1687};
1688
1689static int __init apparmor_nf_ip_init(void)
1690{
1691	int err;
1692
1693	if (!apparmor_enabled)
1694		return 0;
1695
1696	err = register_pernet_subsys(&apparmor_net_ops);
1697	if (err)
1698		panic("Apparmor: register_pernet_subsys: error %d\n", err);
1699
1700	return 0;
1701}
1702__initcall(apparmor_nf_ip_init);
1703#endif
1704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1705static int __init apparmor_init(void)
1706{
1707	int error;
1708
1709	aa_secids_init();
1710
1711	error = aa_setup_dfa_engine();
1712	if (error) {
1713		AA_ERROR("Unable to setup dfa engine\n");
1714		goto alloc_out;
1715	}
1716
1717	error = aa_alloc_root_ns();
1718	if (error) {
1719		AA_ERROR("Unable to allocate default profile namespace\n");
1720		goto alloc_out;
1721	}
1722
1723	error = apparmor_init_sysctl();
1724	if (error) {
1725		AA_ERROR("Unable to register sysctls\n");
1726		goto alloc_out;
1727
1728	}
1729
1730	error = alloc_buffers();
1731	if (error) {
1732		AA_ERROR("Unable to allocate work buffers\n");
1733		goto buffers_out;
1734	}
1735
1736	error = set_init_ctx();
1737	if (error) {
1738		AA_ERROR("Failed to set context on init task\n");
1739		aa_free_root_ns();
1740		goto buffers_out;
1741	}
1742	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1743				"apparmor");
1744
1745	/* Report that AppArmor successfully initialized */
1746	apparmor_initialized = 1;
1747	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1748		aa_info_message("AppArmor initialized: complain mode enabled");
1749	else if (aa_g_profile_mode == APPARMOR_KILL)
1750		aa_info_message("AppArmor initialized: kill mode enabled");
1751	else
1752		aa_info_message("AppArmor initialized");
1753
1754	return error;
1755
1756buffers_out:
1757	destroy_buffers();
1758
1759alloc_out:
1760	aa_destroy_aafs();
1761	aa_teardown_dfa_engine();
1762
1763	apparmor_enabled = false;
1764	return error;
1765}
1766
1767DEFINE_LSM(apparmor) = {
1768	.name = "apparmor",
1769	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1770	.enabled = &apparmor_enabled,
1771	.blobs = &apparmor_blob_sizes,
1772	.init = apparmor_init,
1773};
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * AppArmor security module
   4 *
   5 * This file contains AppArmor LSM hooks.
   6 *
   7 * Copyright (C) 1998-2008 Novell/SUSE
   8 * Copyright 2009-2010 Canonical Ltd.
   9 */
  10
  11#include <linux/lsm_hooks.h>
  12#include <linux/moduleparam.h>
  13#include <linux/mm.h>
  14#include <linux/mman.h>
  15#include <linux/mount.h>
  16#include <linux/namei.h>
  17#include <linux/ptrace.h>
  18#include <linux/ctype.h>
  19#include <linux/sysctl.h>
  20#include <linux/audit.h>
  21#include <linux/user_namespace.h>
  22#include <linux/netfilter_ipv4.h>
  23#include <linux/netfilter_ipv6.h>
  24#include <linux/zstd.h>
  25#include <net/sock.h>
  26#include <uapi/linux/mount.h>
  27#include <uapi/linux/lsm.h>
  28
  29#include "include/apparmor.h"
  30#include "include/apparmorfs.h"
  31#include "include/audit.h"
  32#include "include/capability.h"
  33#include "include/cred.h"
  34#include "include/file.h"
  35#include "include/ipc.h"
  36#include "include/net.h"
  37#include "include/path.h"
  38#include "include/label.h"
  39#include "include/policy.h"
  40#include "include/policy_ns.h"
  41#include "include/procattr.h"
  42#include "include/mount.h"
  43#include "include/secid.h"
  44
  45/* Flag indicating whether initialization completed */
  46int apparmor_initialized;
  47
  48union aa_buffer {
  49	struct list_head list;
  50	DECLARE_FLEX_ARRAY(char, buffer);
  51};
  52
  53struct aa_local_cache {
  54	unsigned int hold;
  55	unsigned int count;
  56	struct list_head head;
  57};
  58
  59#define RESERVE_COUNT 2
  60static int reserve_count = RESERVE_COUNT;
  61static int buffer_count;
  62
  63static LIST_HEAD(aa_global_buffers);
  64static DEFINE_SPINLOCK(aa_buffers_lock);
  65static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
  66
  67/*
  68 * LSM hook functions
  69 */
  70
  71/*
  72 * put the associated labels
  73 */
  74static void apparmor_cred_free(struct cred *cred)
  75{
  76	aa_put_label(cred_label(cred));
  77	set_cred_label(cred, NULL);
  78}
  79
  80/*
  81 * allocate the apparmor part of blank credentials
  82 */
  83static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  84{
  85	set_cred_label(cred, NULL);
  86	return 0;
  87}
  88
  89/*
  90 * prepare new cred label for modification by prepare_cred block
  91 */
  92static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  93				 gfp_t gfp)
  94{
  95	set_cred_label(new, aa_get_newest_label(cred_label(old)));
  96	return 0;
  97}
  98
  99/*
 100 * transfer the apparmor data to a blank set of creds
 101 */
 102static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
 103{
 104	set_cred_label(new, aa_get_newest_label(cred_label(old)));
 105}
 106
 107static void apparmor_task_free(struct task_struct *task)
 108{
 109
 110	aa_free_task_ctx(task_ctx(task));
 111}
 112
 113static int apparmor_task_alloc(struct task_struct *task,
 114			       unsigned long clone_flags)
 115{
 116	struct aa_task_ctx *new = task_ctx(task);
 117
 118	aa_dup_task_ctx(new, task_ctx(current));
 119
 120	return 0;
 121}
 122
 123static int apparmor_ptrace_access_check(struct task_struct *child,
 124					unsigned int mode)
 125{
 126	struct aa_label *tracer, *tracee;
 127	const struct cred *cred;
 128	int error;
 129
 130	cred = get_task_cred(child);
 131	tracee = cred_label(cred);	/* ref count on cred */
 132	tracer = __begin_current_label_crit_section();
 133	error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
 
 134			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
 135						  : AA_PTRACE_TRACE);
 
 136	__end_current_label_crit_section(tracer);
 137	put_cred(cred);
 138
 139	return error;
 140}
 141
 142static int apparmor_ptrace_traceme(struct task_struct *parent)
 143{
 144	struct aa_label *tracer, *tracee;
 145	const struct cred *cred;
 146	int error;
 147
 148	tracee = __begin_current_label_crit_section();
 149	cred = get_task_cred(parent);
 150	tracer = cred_label(cred);	/* ref count on cred */
 151	error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
 152			      AA_PTRACE_TRACE);
 153	put_cred(cred);
 154	__end_current_label_crit_section(tracee);
 155
 156	return error;
 157}
 158
 159/* Derived from security/commoncap.c:cap_capget */
 160static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
 161			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
 162{
 163	struct aa_label *label;
 164	const struct cred *cred;
 165
 166	rcu_read_lock();
 167	cred = __task_cred(target);
 168	label = aa_get_newest_cred_label(cred);
 169
 170	/*
 171	 * cap_capget is stacked ahead of this and will
 172	 * initialize effective and permitted.
 173	 */
 174	if (!unconfined(label)) {
 175		struct aa_profile *profile;
 176		struct label_it i;
 177
 178		label_for_each_confined(i, label, profile) {
 179			struct aa_ruleset *rules;
 180			if (COMPLAIN_MODE(profile))
 181				continue;
 182			rules = list_first_entry(&profile->rules,
 183						 typeof(*rules), list);
 184			*effective = cap_intersect(*effective,
 185						   rules->caps.allow);
 186			*permitted = cap_intersect(*permitted,
 187						   rules->caps.allow);
 188		}
 189	}
 190	rcu_read_unlock();
 191	aa_put_label(label);
 192
 193	return 0;
 194}
 195
 196static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
 197			    int cap, unsigned int opts)
 198{
 199	struct aa_label *label;
 200	int error = 0;
 201
 202	label = aa_get_newest_cred_label(cred);
 203	if (!unconfined(label))
 204		error = aa_capable(cred, label, cap, opts);
 205	aa_put_label(label);
 206
 207	return error;
 208}
 209
 210/**
 211 * common_perm - basic common permission check wrapper fn for paths
 212 * @op: operation being checked
 213 * @path: path to check permission of  (NOT NULL)
 214 * @mask: requested permissions mask
 215 * @cond: conditional info for the permission request  (NOT NULL)
 216 *
 217 * Returns: %0 else error code if error or permission denied
 218 */
 219static int common_perm(const char *op, const struct path *path, u32 mask,
 220		       struct path_cond *cond)
 221{
 222	struct aa_label *label;
 223	int error = 0;
 224
 225	label = __begin_current_label_crit_section();
 226	if (!unconfined(label))
 227		error = aa_path_perm(op, current_cred(), label, path, 0, mask,
 228				     cond);
 229	__end_current_label_crit_section(label);
 230
 231	return error;
 232}
 233
 234/**
 235 * common_perm_cond - common permission wrapper around inode cond
 236 * @op: operation being checked
 237 * @path: location to check (NOT NULL)
 238 * @mask: requested permissions mask
 239 *
 240 * Returns: %0 else error code if error or permission denied
 241 */
 242static int common_perm_cond(const char *op, const struct path *path, u32 mask)
 243{
 244	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
 245					    d_backing_inode(path->dentry));
 246	struct path_cond cond = {
 247		vfsuid_into_kuid(vfsuid),
 248		d_backing_inode(path->dentry)->i_mode
 249	};
 250
 251	if (!path_mediated_fs(path->dentry))
 252		return 0;
 253
 254	return common_perm(op, path, mask, &cond);
 255}
 256
 257/**
 258 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
 259 * @op: operation being checked
 260 * @dir: directory of the dentry  (NOT NULL)
 261 * @dentry: dentry to check  (NOT NULL)
 262 * @mask: requested permissions mask
 263 * @cond: conditional info for the permission request  (NOT NULL)
 264 *
 265 * Returns: %0 else error code if error or permission denied
 266 */
 267static int common_perm_dir_dentry(const char *op, const struct path *dir,
 268				  struct dentry *dentry, u32 mask,
 269				  struct path_cond *cond)
 270{
 271	struct path path = { .mnt = dir->mnt, .dentry = dentry };
 272
 273	return common_perm(op, &path, mask, cond);
 274}
 275
 276/**
 277 * common_perm_rm - common permission wrapper for operations doing rm
 278 * @op: operation being checked
 279 * @dir: directory that the dentry is in  (NOT NULL)
 280 * @dentry: dentry being rm'd  (NOT NULL)
 281 * @mask: requested permission mask
 282 *
 283 * Returns: %0 else error code if error or permission denied
 284 */
 285static int common_perm_rm(const char *op, const struct path *dir,
 286			  struct dentry *dentry, u32 mask)
 287{
 288	struct inode *inode = d_backing_inode(dentry);
 289	struct path_cond cond = { };
 290	vfsuid_t vfsuid;
 291
 292	if (!inode || !path_mediated_fs(dentry))
 293		return 0;
 294
 295	vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
 296	cond.uid = vfsuid_into_kuid(vfsuid);
 297	cond.mode = inode->i_mode;
 298
 299	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
 300}
 301
 302/**
 303 * common_perm_create - common permission wrapper for operations doing create
 304 * @op: operation being checked
 305 * @dir: directory that dentry will be created in  (NOT NULL)
 306 * @dentry: dentry to create   (NOT NULL)
 307 * @mask: request permission mask
 308 * @mode: created file mode
 309 *
 310 * Returns: %0 else error code if error or permission denied
 311 */
 312static int common_perm_create(const char *op, const struct path *dir,
 313			      struct dentry *dentry, u32 mask, umode_t mode)
 314{
 315	struct path_cond cond = { current_fsuid(), mode };
 316
 317	if (!path_mediated_fs(dir->dentry))
 318		return 0;
 319
 320	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
 321}
 322
 323static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
 324{
 325	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
 326}
 327
 328static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
 329			       umode_t mode)
 330{
 331	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
 332				  S_IFDIR);
 333}
 334
 335static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
 336{
 337	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
 338}
 339
 340static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
 341			       umode_t mode, unsigned int dev)
 342{
 343	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
 344}
 345
 346static int apparmor_path_truncate(const struct path *path)
 347{
 348	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
 349}
 350
 351static int apparmor_file_truncate(struct file *file)
 352{
 353	return apparmor_path_truncate(&file->f_path);
 354}
 355
 356static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
 357				 const char *old_name)
 358{
 359	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
 360				  S_IFLNK);
 361}
 362
 363static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
 364			      struct dentry *new_dentry)
 365{
 366	struct aa_label *label;
 367	int error = 0;
 368
 369	if (!path_mediated_fs(old_dentry))
 370		return 0;
 371
 372	label = begin_current_label_crit_section();
 373	if (!unconfined(label))
 374		error = aa_path_link(current_cred(), label, old_dentry, new_dir,
 375				     new_dentry);
 376	end_current_label_crit_section(label);
 377
 378	return error;
 379}
 380
 381static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
 382				const struct path *new_dir, struct dentry *new_dentry,
 383				const unsigned int flags)
 384{
 385	struct aa_label *label;
 386	int error = 0;
 387
 388	if (!path_mediated_fs(old_dentry))
 389		return 0;
 390	if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
 391		return 0;
 392
 393	label = begin_current_label_crit_section();
 394	if (!unconfined(label)) {
 395		struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
 396		vfsuid_t vfsuid;
 397		struct path old_path = { .mnt = old_dir->mnt,
 398					 .dentry = old_dentry };
 399		struct path new_path = { .mnt = new_dir->mnt,
 400					 .dentry = new_dentry };
 401		struct path_cond cond = {
 402			.mode = d_backing_inode(old_dentry)->i_mode
 403		};
 404		vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
 405		cond.uid = vfsuid_into_kuid(vfsuid);
 406
 407		if (flags & RENAME_EXCHANGE) {
 408			struct path_cond cond_exchange = {
 409				.mode = d_backing_inode(new_dentry)->i_mode,
 410			};
 411			vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
 412			cond_exchange.uid = vfsuid_into_kuid(vfsuid);
 413
 414			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
 415					     label, &new_path, 0,
 416					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
 417					     AA_MAY_SETATTR | AA_MAY_DELETE,
 418					     &cond_exchange);
 419			if (!error)
 420				error = aa_path_perm(OP_RENAME_DEST, current_cred(),
 421						     label, &old_path,
 422						     0, MAY_WRITE | AA_MAY_SETATTR |
 423						     AA_MAY_CREATE, &cond_exchange);
 424		}
 425
 426		if (!error)
 427			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
 428					     label, &old_path, 0,
 429					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
 430					     AA_MAY_SETATTR | AA_MAY_DELETE,
 431					     &cond);
 432		if (!error)
 433			error = aa_path_perm(OP_RENAME_DEST, current_cred(),
 434					     label, &new_path,
 435					     0, MAY_WRITE | AA_MAY_SETATTR |
 436					     AA_MAY_CREATE, &cond);
 437
 438	}
 439	end_current_label_crit_section(label);
 440
 441	return error;
 442}
 443
 444static int apparmor_path_chmod(const struct path *path, umode_t mode)
 445{
 446	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
 447}
 448
 449static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
 450{
 451	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
 452}
 453
 454static int apparmor_inode_getattr(const struct path *path)
 455{
 456	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
 457}
 458
 459static int apparmor_file_open(struct file *file)
 460{
 461	struct aa_file_ctx *fctx = file_ctx(file);
 462	struct aa_label *label;
 463	int error = 0;
 464	bool needput;
 465
 466	if (!path_mediated_fs(file->f_path.dentry))
 467		return 0;
 468
 469	/* If in exec, permission is handled by bprm hooks.
 470	 * Cache permissions granted by the previous exec check, with
 471	 * implicit read and executable mmap which are required to
 472	 * actually execute the image.
 473	 *
 474	 * Illogically, FMODE_EXEC is in f_flags, not f_mode.
 475	 */
 476	if (file->f_flags & __FMODE_EXEC) {
 477		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
 478		return 0;
 479	}
 480
 481	label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
 482	if (!unconfined(label)) {
 483		struct mnt_idmap *idmap = file_mnt_idmap(file);
 484		struct inode *inode = file_inode(file);
 485		vfsuid_t vfsuid;
 486		struct path_cond cond = {
 487			.mode = inode->i_mode,
 488		};
 489		vfsuid = i_uid_into_vfsuid(idmap, inode);
 490		cond.uid = vfsuid_into_kuid(vfsuid);
 491
 492		error = aa_path_perm(OP_OPEN, file->f_cred,
 493				     label, &file->f_path, 0,
 494				     aa_map_file_to_perms(file), &cond);
 495		/* todo cache full allowed permissions set and state */
 496		fctx->allow = aa_map_file_to_perms(file);
 497	}
 498	aa_put_label_condref(label, needput);
 499
 500	return error;
 501}
 502
 503static int apparmor_file_alloc_security(struct file *file)
 504{
 505	struct aa_file_ctx *ctx = file_ctx(file);
 506	struct aa_label *label = begin_current_label_crit_section();
 507
 508	spin_lock_init(&ctx->lock);
 509	rcu_assign_pointer(ctx->label, aa_get_label(label));
 510	end_current_label_crit_section(label);
 511	return 0;
 512}
 513
 514static void apparmor_file_free_security(struct file *file)
 515{
 516	struct aa_file_ctx *ctx = file_ctx(file);
 517
 518	if (ctx)
 519		aa_put_label(rcu_access_pointer(ctx->label));
 520}
 521
 522static int common_file_perm(const char *op, struct file *file, u32 mask,
 523			    bool in_atomic)
 524{
 525	struct aa_label *label;
 526	int error = 0;
 527
 528	/* don't reaudit files closed during inheritance */
 529	if (file->f_path.dentry == aa_null.dentry)
 530		return -EACCES;
 531
 532	label = __begin_current_label_crit_section();
 533	error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
 534	__end_current_label_crit_section(label);
 535
 536	return error;
 537}
 538
 539static int apparmor_file_receive(struct file *file)
 540{
 541	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
 542				false);
 543}
 544
 545static int apparmor_file_permission(struct file *file, int mask)
 546{
 547	return common_file_perm(OP_FPERM, file, mask, false);
 548}
 549
 550static int apparmor_file_lock(struct file *file, unsigned int cmd)
 551{
 552	u32 mask = AA_MAY_LOCK;
 553
 554	if (cmd == F_WRLCK)
 555		mask |= MAY_WRITE;
 556
 557	return common_file_perm(OP_FLOCK, file, mask, false);
 558}
 559
 560static int common_mmap(const char *op, struct file *file, unsigned long prot,
 561		       unsigned long flags, bool in_atomic)
 562{
 563	int mask = 0;
 564
 565	if (!file || !file_ctx(file))
 566		return 0;
 567
 568	if (prot & PROT_READ)
 569		mask |= MAY_READ;
 570	/*
 571	 * Private mappings don't require write perms since they don't
 572	 * write back to the files
 573	 */
 574	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
 575		mask |= MAY_WRITE;
 576	if (prot & PROT_EXEC)
 577		mask |= AA_EXEC_MMAP;
 578
 579	return common_file_perm(op, file, mask, in_atomic);
 580}
 581
 582static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
 583			      unsigned long prot, unsigned long flags)
 584{
 585	return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
 586}
 587
 588static int apparmor_file_mprotect(struct vm_area_struct *vma,
 589				  unsigned long reqprot, unsigned long prot)
 590{
 591	return common_mmap(OP_FMPROT, vma->vm_file, prot,
 592			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
 593			   false);
 594}
 595
 596#ifdef CONFIG_IO_URING
 597static const char *audit_uring_mask(u32 mask)
 598{
 599	if (mask & AA_MAY_CREATE_SQPOLL)
 600		return "sqpoll";
 601	if (mask & AA_MAY_OVERRIDE_CRED)
 602		return "override_creds";
 603	return "";
 604}
 605
 606static void audit_uring_cb(struct audit_buffer *ab, void *va)
 607{
 608	struct apparmor_audit_data *ad = aad_of_va(va);
 609
 610	if (ad->request & AA_URING_PERM_MASK) {
 611		audit_log_format(ab, " requested=\"%s\"",
 612				 audit_uring_mask(ad->request));
 613		if (ad->denied & AA_URING_PERM_MASK) {
 614			audit_log_format(ab, " denied=\"%s\"",
 615					 audit_uring_mask(ad->denied));
 616		}
 617	}
 618	if (ad->uring.target) {
 619		audit_log_format(ab, " tcontext=");
 620		aa_label_xaudit(ab, labels_ns(ad->subj_label),
 621				ad->uring.target,
 622				FLAGS_NONE, GFP_ATOMIC);
 623	}
 624}
 625
 626static int profile_uring(struct aa_profile *profile, u32 request,
 627			 struct aa_label *new, int cap,
 628			 struct apparmor_audit_data *ad)
 629{
 630	unsigned int state;
 631	struct aa_ruleset *rules;
 632	int error = 0;
 633
 634	AA_BUG(!profile);
 635
 636	rules = list_first_entry(&profile->rules, typeof(*rules), list);
 637	state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
 638	if (state) {
 639		struct aa_perms perms = { };
 640
 641		if (new) {
 642			aa_label_match(profile, rules, new, state,
 643				       false, request, &perms);
 644		} else {
 645			perms = *aa_lookup_perms(rules->policy, state);
 646		}
 647		aa_apply_modes_to_perms(profile, &perms);
 648		error = aa_check_perms(profile, &perms, request, ad,
 649				       audit_uring_cb);
 650	}
 651
 652	return error;
 653}
 654
 655/**
 656 * apparmor_uring_override_creds - check the requested cred override
 657 * @new: the target creds
 658 *
 659 * Check to see if the current task is allowed to override it's credentials
 660 * to service an io_uring operation.
 661 */
 662static int apparmor_uring_override_creds(const struct cred *new)
 663{
 664	struct aa_profile *profile;
 665	struct aa_label *label;
 666	int error;
 667	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
 668			  OP_URING_OVERRIDE);
 669
 670	ad.uring.target = cred_label(new);
 671	label = __begin_current_label_crit_section();
 672	error = fn_for_each(label, profile,
 673			profile_uring(profile, AA_MAY_OVERRIDE_CRED,
 674				      cred_label(new), CAP_SYS_ADMIN, &ad));
 675	__end_current_label_crit_section(label);
 676
 677	return error;
 678}
 679
 680/**
 681 * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
 682 *
 683 * Check to see if the current task is allowed to create a new io_uring
 684 * kernel polling thread.
 685 */
 686static int apparmor_uring_sqpoll(void)
 687{
 688	struct aa_profile *profile;
 689	struct aa_label *label;
 690	int error;
 691	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
 692			  OP_URING_SQPOLL);
 693
 694	label = __begin_current_label_crit_section();
 695	error = fn_for_each(label, profile,
 696			profile_uring(profile, AA_MAY_CREATE_SQPOLL,
 697				      NULL, CAP_SYS_ADMIN, &ad));
 698	__end_current_label_crit_section(label);
 699
 700	return error;
 701}
 702#endif /* CONFIG_IO_URING */
 703
 704static int apparmor_sb_mount(const char *dev_name, const struct path *path,
 705			     const char *type, unsigned long flags, void *data)
 706{
 707	struct aa_label *label;
 708	int error = 0;
 709
 710	/* Discard magic */
 711	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
 712		flags &= ~MS_MGC_MSK;
 713
 714	flags &= ~AA_MS_IGNORE_MASK;
 715
 716	label = __begin_current_label_crit_section();
 717	if (!unconfined(label)) {
 718		if (flags & MS_REMOUNT)
 719			error = aa_remount(current_cred(), label, path, flags,
 720					   data);
 721		else if (flags & MS_BIND)
 722			error = aa_bind_mount(current_cred(), label, path,
 723					      dev_name, flags);
 724		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
 725				  MS_UNBINDABLE))
 726			error = aa_mount_change_type(current_cred(), label,
 727						     path, flags);
 728		else if (flags & MS_MOVE)
 729			error = aa_move_mount_old(current_cred(), label, path,
 730						  dev_name);
 731		else
 732			error = aa_new_mount(current_cred(), label, dev_name,
 733					     path, type, flags, data);
 734	}
 735	__end_current_label_crit_section(label);
 736
 737	return error;
 738}
 739
 740static int apparmor_move_mount(const struct path *from_path,
 741			       const struct path *to_path)
 742{
 743	struct aa_label *label;
 744	int error = 0;
 745
 746	label = __begin_current_label_crit_section();
 747	if (!unconfined(label))
 748		error = aa_move_mount(current_cred(), label, from_path,
 749				      to_path);
 750	__end_current_label_crit_section(label);
 751
 752	return error;
 753}
 754
 755static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
 756{
 757	struct aa_label *label;
 758	int error = 0;
 759
 760	label = __begin_current_label_crit_section();
 761	if (!unconfined(label))
 762		error = aa_umount(current_cred(), label, mnt, flags);
 763	__end_current_label_crit_section(label);
 764
 765	return error;
 766}
 767
 768static int apparmor_sb_pivotroot(const struct path *old_path,
 769				 const struct path *new_path)
 770{
 771	struct aa_label *label;
 772	int error = 0;
 773
 774	label = aa_get_current_label();
 775	if (!unconfined(label))
 776		error = aa_pivotroot(current_cred(), label, old_path, new_path);
 777	aa_put_label(label);
 778
 779	return error;
 780}
 781
 782static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
 783				u32 *size, u32 flags)
 784{
 785	int error = -ENOENT;
 786	struct aa_task_ctx *ctx = task_ctx(current);
 787	struct aa_label *label = NULL;
 788	char *value = NULL;
 789
 790	switch (attr) {
 791	case LSM_ATTR_CURRENT:
 792		label = aa_get_newest_label(cred_label(current_cred()));
 793		break;
 794	case LSM_ATTR_PREV:
 795		if (ctx->previous)
 796			label = aa_get_newest_label(ctx->previous);
 797		break;
 798	case LSM_ATTR_EXEC:
 799		if (ctx->onexec)
 800			label = aa_get_newest_label(ctx->onexec);
 801		break;
 802	default:
 803		error = -EOPNOTSUPP;
 804		break;
 805	}
 806
 807	if (label) {
 808		error = aa_getprocattr(label, &value, false);
 809		if (error > 0)
 810			error = lsm_fill_user_ctx(lx, size, value, error,
 811						  LSM_ID_APPARMOR, 0);
 812		kfree(value);
 813	}
 814
 815	aa_put_label(label);
 816
 817	if (error < 0)
 818		return error;
 819	return 1;
 820}
 821
 822static int apparmor_getprocattr(struct task_struct *task, const char *name,
 823				char **value)
 824{
 825	int error = -ENOENT;
 826	/* released below */
 827	const struct cred *cred = get_task_cred(task);
 828	struct aa_task_ctx *ctx = task_ctx(current);
 829	struct aa_label *label = NULL;
 830
 831	if (strcmp(name, "current") == 0)
 832		label = aa_get_newest_label(cred_label(cred));
 833	else if (strcmp(name, "prev") == 0  && ctx->previous)
 834		label = aa_get_newest_label(ctx->previous);
 835	else if (strcmp(name, "exec") == 0 && ctx->onexec)
 836		label = aa_get_newest_label(ctx->onexec);
 837	else
 838		error = -EINVAL;
 839
 840	if (label)
 841		error = aa_getprocattr(label, value, true);
 842
 843	aa_put_label(label);
 844	put_cred(cred);
 845
 846	return error;
 847}
 848
 849static int do_setattr(u64 attr, void *value, size_t size)
 
 850{
 851	char *command, *largs = NULL, *args = value;
 852	size_t arg_size;
 853	int error;
 854	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
 855			  OP_SETPROCATTR);
 856
 857	if (size == 0)
 858		return -EINVAL;
 859
 860	/* AppArmor requires that the buffer must be null terminated atm */
 861	if (args[size - 1] != '\0') {
 862		/* null terminate */
 863		largs = args = kmalloc(size + 1, GFP_KERNEL);
 864		if (!args)
 865			return -ENOMEM;
 866		memcpy(args, value, size);
 867		args[size] = '\0';
 868	}
 869
 870	error = -EINVAL;
 871	args = strim(args);
 872	command = strsep(&args, " ");
 873	if (!args)
 874		goto out;
 875	args = skip_spaces(args);
 876	if (!*args)
 877		goto out;
 878
 879	arg_size = size - (args - (largs ? largs : (char *) value));
 880	if (attr == LSM_ATTR_CURRENT) {
 881		if (strcmp(command, "changehat") == 0) {
 882			error = aa_setprocattr_changehat(args, arg_size,
 883							 AA_CHANGE_NOFLAGS);
 884		} else if (strcmp(command, "permhat") == 0) {
 885			error = aa_setprocattr_changehat(args, arg_size,
 886							 AA_CHANGE_TEST);
 887		} else if (strcmp(command, "changeprofile") == 0) {
 888			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
 889		} else if (strcmp(command, "permprofile") == 0) {
 890			error = aa_change_profile(args, AA_CHANGE_TEST);
 891		} else if (strcmp(command, "stack") == 0) {
 892			error = aa_change_profile(args, AA_CHANGE_STACK);
 893		} else
 894			goto fail;
 895	} else if (attr == LSM_ATTR_EXEC) {
 896		if (strcmp(command, "exec") == 0)
 897			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
 898		else if (strcmp(command, "stack") == 0)
 899			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
 900							 AA_CHANGE_STACK));
 901		else
 902			goto fail;
 903	} else
 904		/* only support the "current" and "exec" process attributes */
 905		goto fail;
 906
 907	if (!error)
 908		error = size;
 909out:
 910	kfree(largs);
 911	return error;
 912
 913fail:
 914	ad.subj_label = begin_current_label_crit_section();
 915	if (attr == LSM_ATTR_CURRENT)
 916		ad.info = "current";
 917	else if (attr == LSM_ATTR_EXEC)
 918		ad.info = "exec";
 919	else
 920		ad.info = "invalid";
 921	ad.error = error = -EINVAL;
 922	aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
 923	end_current_label_crit_section(ad.subj_label);
 924	goto out;
 925}
 926
 927static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
 928				u32 size, u32 flags)
 929{
 930	int rc;
 931
 932	if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
 933		return -EOPNOTSUPP;
 934
 935	rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
 936	if (rc > 0)
 937		return 0;
 938	return rc;
 939}
 940
 941static int apparmor_setprocattr(const char *name, void *value,
 942				size_t size)
 943{
 944	int attr = lsm_name_to_attr(name);
 945
 946	if (attr)
 947		return do_setattr(attr, value, size);
 948	return -EINVAL;
 949}
 950
 951/**
 952 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
 953 * @bprm: binprm for the exec  (NOT NULL)
 954 */
 955static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
 956{
 957	struct aa_label *label = aa_current_raw_label();
 958	struct aa_label *new_label = cred_label(bprm->cred);
 959
 960	/* bail out if unconfined or not changing profile */
 961	if ((new_label->proxy == label->proxy) ||
 962	    (unconfined(new_label)))
 963		return;
 964
 965	aa_inherit_files(bprm->cred, current->files);
 966
 967	current->pdeath_signal = 0;
 968
 969	/* reset soft limits and set hard limits for the new label */
 970	__aa_transition_rlimits(label, new_label);
 971}
 972
 973/**
 974 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
 975 * @bprm: binprm for the exec  (NOT NULL)
 976 */
 977static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
 978{
 979	/* clear out temporary/transitional state from the context */
 980	aa_clear_task_ctx_trans(task_ctx(current));
 981
 982	return;
 983}
 984
 985static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop)
 986{
 987	struct aa_label *label = __begin_current_label_crit_section();
 988
 989	prop->apparmor.label = label;
 990	__end_current_label_crit_section(label);
 991}
 992
 993static void apparmor_task_getlsmprop_obj(struct task_struct *p,
 994					  struct lsm_prop *prop)
 995{
 996	struct aa_label *label = aa_get_task_label(p);
 997
 998	prop->apparmor.label = label;
 999	aa_put_label(label);
1000}
1001
1002static int apparmor_task_setrlimit(struct task_struct *task,
1003		unsigned int resource, struct rlimit *new_rlim)
1004{
1005	struct aa_label *label = __begin_current_label_crit_section();
1006	int error = 0;
1007
1008	if (!unconfined(label))
1009		error = aa_task_setrlimit(current_cred(), label, task,
1010					  resource, new_rlim);
1011	__end_current_label_crit_section(label);
1012
1013	return error;
1014}
1015
1016static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
1017			      int sig, const struct cred *cred)
1018{
1019	const struct cred *tc;
1020	struct aa_label *cl, *tl;
1021	int error;
1022
1023	tc = get_task_cred(target);
1024	tl = aa_get_newest_cred_label(tc);
1025	if (cred) {
1026		/*
1027		 * Dealing with USB IO specific behavior
1028		 */
1029		cl = aa_get_newest_cred_label(cred);
1030		error = aa_may_signal(cred, cl, tc, tl, sig);
 
1031		aa_put_label(cl);
1032	} else {
1033		cl = __begin_current_label_crit_section();
1034		error = aa_may_signal(current_cred(), cl, tc, tl, sig);
1035		__end_current_label_crit_section(cl);
1036	}
 
 
 
 
1037	aa_put_label(tl);
1038	put_cred(tc);
1039
1040	return error;
1041}
1042
1043static int apparmor_userns_create(const struct cred *cred)
 
 
 
1044{
1045	struct aa_label *label;
1046	struct aa_profile *profile;
1047	int error = 0;
1048	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
1049			  OP_USERNS_CREATE);
1050
1051	ad.subj_cred = current_cred();
 
 
1052
1053	label = begin_current_label_crit_section();
1054	if (!unconfined(label)) {
1055		error = fn_for_each(label, profile,
1056				    aa_profile_ns_perm(profile, &ad,
1057						       AA_USERNS_CREATE));
1058	}
1059	end_current_label_crit_section(label);
1060
1061	return error;
1062}
1063
 
 
 
1064static void apparmor_sk_free_security(struct sock *sk)
1065{
1066	struct aa_sk_ctx *ctx = aa_sock(sk);
1067
 
1068	aa_put_label(ctx->label);
1069	aa_put_label(ctx->peer);
 
1070}
1071
1072/**
1073 * apparmor_sk_clone_security - clone the sk_security field
1074 * @sk: sock to have security cloned
1075 * @newsk: sock getting clone
1076 */
1077static void apparmor_sk_clone_security(const struct sock *sk,
1078				       struct sock *newsk)
1079{
1080	struct aa_sk_ctx *ctx = aa_sock(sk);
1081	struct aa_sk_ctx *new = aa_sock(newsk);
1082
1083	if (new->label)
1084		aa_put_label(new->label);
1085	new->label = aa_get_label(ctx->label);
1086
1087	if (new->peer)
1088		aa_put_label(new->peer);
1089	new->peer = aa_get_label(ctx->peer);
1090}
1091
 
 
 
1092static int apparmor_socket_create(int family, int type, int protocol, int kern)
1093{
1094	struct aa_label *label;
1095	int error = 0;
1096
1097	AA_BUG(in_interrupt());
1098
1099	label = begin_current_label_crit_section();
1100	if (!(kern || unconfined(label)))
1101		error = af_select(family,
1102				  create_perm(label, family, type, protocol),
1103				  aa_af_perm(current_cred(), label,
1104					     OP_CREATE, AA_MAY_CREATE,
1105					     family, type, protocol));
1106	end_current_label_crit_section(label);
1107
1108	return error;
1109}
1110
1111/**
1112 * apparmor_socket_post_create - setup the per-socket security struct
1113 * @sock: socket that is being setup
1114 * @family: family of socket being created
1115 * @type: type of the socket
1116 * @protocol: protocol of the socket
1117 * @kern: socket is a special kernel socket
1118 *
1119 * Note:
1120 * -   kernel sockets labeled kernel_t used to use unconfined
 
1121 * -   socket may not have sk here if created with sock_create_lite or
1122 *     sock_alloc. These should be accept cases which will be handled in
1123 *     sock_graft.
1124 */
1125static int apparmor_socket_post_create(struct socket *sock, int family,
1126				       int type, int protocol, int kern)
1127{
1128	struct aa_label *label;
1129
1130	if (kern) {
1131		label = aa_get_label(kernel_t);
 
 
 
1132	} else
1133		label = aa_get_current_label();
1134
1135	if (sock->sk) {
1136		struct aa_sk_ctx *ctx = aa_sock(sock->sk);
1137
1138		aa_put_label(ctx->label);
1139		ctx->label = aa_get_label(label);
1140	}
1141	aa_put_label(label);
1142
1143	return 0;
1144}
1145
 
 
 
1146static int apparmor_socket_bind(struct socket *sock,
1147				struct sockaddr *address, int addrlen)
1148{
1149	AA_BUG(!sock);
1150	AA_BUG(!sock->sk);
1151	AA_BUG(!address);
1152	AA_BUG(in_interrupt());
1153
1154	return af_select(sock->sk->sk_family,
1155			 bind_perm(sock, address, addrlen),
1156			 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
1157}
1158
 
 
 
1159static int apparmor_socket_connect(struct socket *sock,
1160				   struct sockaddr *address, int addrlen)
1161{
1162	AA_BUG(!sock);
1163	AA_BUG(!sock->sk);
1164	AA_BUG(!address);
1165	AA_BUG(in_interrupt());
1166
1167	return af_select(sock->sk->sk_family,
1168			 connect_perm(sock, address, addrlen),
1169			 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
1170}
1171
 
 
 
1172static int apparmor_socket_listen(struct socket *sock, int backlog)
1173{
1174	AA_BUG(!sock);
1175	AA_BUG(!sock->sk);
1176	AA_BUG(in_interrupt());
1177
1178	return af_select(sock->sk->sk_family,
1179			 listen_perm(sock, backlog),
1180			 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
1181}
1182
1183/*
 
 
1184 * Note: while @newsock is created and has some information, the accept
1185 *       has not been done.
1186 */
1187static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
1188{
1189	AA_BUG(!sock);
1190	AA_BUG(!sock->sk);
1191	AA_BUG(!newsock);
1192	AA_BUG(in_interrupt());
1193
1194	return af_select(sock->sk->sk_family,
1195			 accept_perm(sock, newsock),
1196			 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
1197}
1198
1199static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
1200			    struct msghdr *msg, int size)
1201{
1202	AA_BUG(!sock);
1203	AA_BUG(!sock->sk);
1204	AA_BUG(!msg);
1205	AA_BUG(in_interrupt());
1206
1207	return af_select(sock->sk->sk_family,
1208			 msg_perm(op, request, sock, msg, size),
1209			 aa_sk_perm(op, request, sock->sk));
1210}
1211
 
 
 
1212static int apparmor_socket_sendmsg(struct socket *sock,
1213				   struct msghdr *msg, int size)
1214{
1215	return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1216}
1217
 
 
 
1218static int apparmor_socket_recvmsg(struct socket *sock,
1219				   struct msghdr *msg, int size, int flags)
1220{
1221	return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1222}
1223
1224/* revaliation, get/set attr, shutdown */
1225static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1226{
1227	AA_BUG(!sock);
1228	AA_BUG(!sock->sk);
1229	AA_BUG(in_interrupt());
1230
1231	return af_select(sock->sk->sk_family,
1232			 sock_perm(op, request, sock),
1233			 aa_sk_perm(op, request, sock->sk));
1234}
1235
 
 
 
1236static int apparmor_socket_getsockname(struct socket *sock)
1237{
1238	return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1239}
1240
 
 
 
1241static int apparmor_socket_getpeername(struct socket *sock)
1242{
1243	return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1244}
1245
1246/* revaliation, get/set attr, opt */
1247static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1248			    int level, int optname)
1249{
1250	AA_BUG(!sock);
1251	AA_BUG(!sock->sk);
1252	AA_BUG(in_interrupt());
1253
1254	return af_select(sock->sk->sk_family,
1255			 opt_perm(op, request, sock, level, optname),
1256			 aa_sk_perm(op, request, sock->sk));
1257}
1258
 
 
 
1259static int apparmor_socket_getsockopt(struct socket *sock, int level,
1260				      int optname)
1261{
1262	return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1263				level, optname);
1264}
1265
 
 
 
1266static int apparmor_socket_setsockopt(struct socket *sock, int level,
1267				      int optname)
1268{
1269	return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1270				level, optname);
1271}
1272
 
 
 
1273static int apparmor_socket_shutdown(struct socket *sock, int how)
1274{
1275	return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1276}
1277
1278#ifdef CONFIG_NETWORK_SECMARK
1279/**
1280 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1281 * @sk: sk to associate @skb with
1282 * @skb: skb to check for perms
1283 *
1284 * Note: can not sleep may be called with locks held
1285 *
1286 * dont want protocol specific in __skb_recv_datagram()
1287 * to deny an incoming connection  socket_sock_rcv_skb()
1288 */
1289static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1290{
1291	struct aa_sk_ctx *ctx = aa_sock(sk);
1292
1293	if (!skb->secmark)
1294		return 0;
1295
1296	/*
1297	 * If reach here before socket_post_create hook is called, in which
1298	 * case label is null, drop the packet.
1299	 */
1300	if (!ctx->label)
1301		return -EACCES;
1302
1303	return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1304				      skb->secmark, sk);
1305}
1306#endif
1307
1308
1309static struct aa_label *sk_peer_label(struct sock *sk)
1310{
1311	struct aa_sk_ctx *ctx = aa_sock(sk);
1312
1313	if (ctx->peer)
1314		return ctx->peer;
1315
1316	return ERR_PTR(-ENOPROTOOPT);
1317}
1318
1319/**
1320 * apparmor_socket_getpeersec_stream - get security context of peer
1321 * @sock: socket that we are trying to get the peer context of
1322 * @optval: output - buffer to copy peer name to
1323 * @optlen: output - size of copied name in @optval
1324 * @len: size of @optval buffer
1325 * Returns: 0 on success, -errno of failure
1326 *
1327 * Note: for tcp only valid if using ipsec or cipso on lan
1328 */
1329static int apparmor_socket_getpeersec_stream(struct socket *sock,
1330					     sockptr_t optval, sockptr_t optlen,
 
1331					     unsigned int len)
1332{
1333	char *name = NULL;
1334	int slen, error = 0;
1335	struct aa_label *label;
1336	struct aa_label *peer;
1337
1338	label = begin_current_label_crit_section();
1339	peer = sk_peer_label(sock->sk);
1340	if (IS_ERR(peer)) {
1341		error = PTR_ERR(peer);
1342		goto done;
1343	}
1344	slen = aa_label_asxprint(&name, labels_ns(label), peer,
1345				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1346				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1347	/* don't include terminating \0 in slen, it breaks some apps */
1348	if (slen < 0) {
1349		error = -ENOMEM;
1350		goto done;
1351	}
1352	if (slen > len) {
1353		error = -ERANGE;
1354		goto done_len;
 
 
 
 
 
 
 
1355	}
1356
1357	if (copy_to_sockptr(optval, name, slen))
1358		error = -EFAULT;
1359done_len:
1360	if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1361		error = -EFAULT;
1362done:
1363	end_current_label_crit_section(label);
1364	kfree(name);
1365	return error;
1366}
1367
1368/**
1369 * apparmor_socket_getpeersec_dgram - get security label of packet
1370 * @sock: the peer socket
1371 * @skb: packet data
1372 * @secid: pointer to where to put the secid of the packet
1373 *
1374 * Sets the netlabel socket state on sk from parent
1375 */
1376static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1377					    struct sk_buff *skb, u32 *secid)
1378
1379{
1380	/* TODO: requires secid support */
1381	return -ENOPROTOOPT;
1382}
1383
1384/**
1385 * apparmor_sock_graft - Initialize newly created socket
1386 * @sk: child sock
1387 * @parent: parent socket
1388 *
1389 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1390 *       just set sk security information off of current creating process label
1391 *       Labeling of sk for accept case - probably should be sock based
1392 *       instead of task, because of the case where an implicitly labeled
1393 *       socket is shared by different tasks.
1394 */
1395static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1396{
1397	struct aa_sk_ctx *ctx = aa_sock(sk);
1398
1399	if (!ctx->label)
1400		ctx->label = aa_get_current_label();
1401}
1402
1403#ifdef CONFIG_NETWORK_SECMARK
1404static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1405				      struct request_sock *req)
1406{
1407	struct aa_sk_ctx *ctx = aa_sock(sk);
1408
1409	if (!skb->secmark)
1410		return 0;
1411
1412	return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1413				      skb->secmark, sk);
1414}
1415#endif
1416
1417/*
1418 * The cred blob is a pointer to, not an instance of, an aa_label.
1419 */
1420struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1421	.lbs_cred = sizeof(struct aa_label *),
1422	.lbs_file = sizeof(struct aa_file_ctx),
1423	.lbs_task = sizeof(struct aa_task_ctx),
1424	.lbs_sock = sizeof(struct aa_sk_ctx),
1425};
1426
1427static const struct lsm_id apparmor_lsmid = {
1428	.name = "apparmor",
1429	.id = LSM_ID_APPARMOR,
1430};
1431
1432static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1433	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1434	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1435	LSM_HOOK_INIT(capget, apparmor_capget),
1436	LSM_HOOK_INIT(capable, apparmor_capable),
1437
1438	LSM_HOOK_INIT(move_mount, apparmor_move_mount),
1439	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1440	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1441	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1442
1443	LSM_HOOK_INIT(path_link, apparmor_path_link),
1444	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1445	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1446	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1447	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1448	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1449	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1450	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1451	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1452	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1453	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1454
1455	LSM_HOOK_INIT(file_open, apparmor_file_open),
1456	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1457	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1458	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1459	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1460	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1461	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1462	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1463	LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1464
1465	LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
1466	LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
1467	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1468	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1469
 
1470	LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1471	LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1472
1473	LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1474	LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1475	LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1476	LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1477	LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1478	LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1479	LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1480	LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1481	LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1482	LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1483	LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1484	LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1485	LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1486#ifdef CONFIG_NETWORK_SECMARK
1487	LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1488#endif
1489	LSM_HOOK_INIT(socket_getpeersec_stream,
1490		      apparmor_socket_getpeersec_stream),
1491	LSM_HOOK_INIT(socket_getpeersec_dgram,
1492		      apparmor_socket_getpeersec_dgram),
1493	LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1494#ifdef CONFIG_NETWORK_SECMARK
1495	LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1496#endif
1497
1498	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1499	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1500	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1501	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1502
1503	LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1504	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1505	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1506
1507	LSM_HOOK_INIT(task_free, apparmor_task_free),
1508	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1509	LSM_HOOK_INIT(current_getlsmprop_subj,
1510		      apparmor_current_getlsmprop_subj),
1511	LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj),
1512	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1513	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1514	LSM_HOOK_INIT(userns_create, apparmor_userns_create),
1515
1516#ifdef CONFIG_AUDIT
1517	LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1518	LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1519	LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1520	LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1521#endif
1522
1523	LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1524	LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx),
1525	LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1526	LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1527
1528#ifdef CONFIG_IO_URING
1529	LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
1530	LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
1531#endif
1532};
1533
1534/*
1535 * AppArmor sysfs module parameters
1536 */
1537
1538static int param_set_aabool(const char *val, const struct kernel_param *kp);
1539static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1540#define param_check_aabool param_check_bool
1541static const struct kernel_param_ops param_ops_aabool = {
1542	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1543	.set = param_set_aabool,
1544	.get = param_get_aabool
1545};
1546
1547static int param_set_aauint(const char *val, const struct kernel_param *kp);
1548static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1549#define param_check_aauint param_check_uint
1550static const struct kernel_param_ops param_ops_aauint = {
1551	.set = param_set_aauint,
1552	.get = param_get_aauint
1553};
1554
1555static int param_set_aacompressionlevel(const char *val,
1556					const struct kernel_param *kp);
1557static int param_get_aacompressionlevel(char *buffer,
1558					const struct kernel_param *kp);
1559#define param_check_aacompressionlevel param_check_int
1560static const struct kernel_param_ops param_ops_aacompressionlevel = {
1561	.set = param_set_aacompressionlevel,
1562	.get = param_get_aacompressionlevel
1563};
1564
1565static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1566static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1567#define param_check_aalockpolicy param_check_bool
1568static const struct kernel_param_ops param_ops_aalockpolicy = {
1569	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1570	.set = param_set_aalockpolicy,
1571	.get = param_get_aalockpolicy
1572};
1573
1574static int param_set_audit(const char *val, const struct kernel_param *kp);
1575static int param_get_audit(char *buffer, const struct kernel_param *kp);
1576
1577static int param_set_mode(const char *val, const struct kernel_param *kp);
1578static int param_get_mode(char *buffer, const struct kernel_param *kp);
1579
1580/* Flag values, also controllable via /sys/module/apparmor/parameters
1581 * We define special types as we want to do additional mediation.
1582 */
1583
1584/* AppArmor global enforcement switch - complain, enforce, kill */
1585enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1586module_param_call(mode, param_set_mode, param_get_mode,
1587		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1588
1589/* whether policy verification hashing is enabled */
1590bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1591#ifdef CONFIG_SECURITY_APPARMOR_HASH
1592module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1593#endif
1594
1595/* whether policy exactly as loaded is retained for debug and checkpointing */
1596bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1597#ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1598module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1599#endif
1600
1601/* policy loaddata compression level */
1602int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1603module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1604		   aacompressionlevel, 0400);
1605
1606/* Debug mode */
1607bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1608module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1609
1610/* Audit mode */
1611enum audit_mode aa_g_audit;
1612module_param_call(audit, param_set_audit, param_get_audit,
1613		  &aa_g_audit, S_IRUSR | S_IWUSR);
1614
1615/* Determines if audit header is included in audited messages.  This
1616 * provides more context if the audit daemon is not running
1617 */
1618bool aa_g_audit_header = true;
1619module_param_named(audit_header, aa_g_audit_header, aabool,
1620		   S_IRUSR | S_IWUSR);
1621
1622/* lock out loading/removal of policy
1623 * TODO: add in at boot loading of policy, which is the only way to
1624 *       load policy, if lock_policy is set
1625 */
1626bool aa_g_lock_policy;
1627module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1628		   S_IRUSR | S_IWUSR);
1629
1630/* Syscall logging mode */
1631bool aa_g_logsyscall;
1632module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1633
1634/* Maximum pathname length before accesses will start getting rejected */
1635unsigned int aa_g_path_max = 2 * PATH_MAX;
1636module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1637
1638/* Determines how paranoid loading of policy is and how much verification
1639 * on the loaded policy is done.
1640 * DEPRECATED: read only as strict checking of load is always done now
1641 * that none root users (user namespaces) can load policy.
1642 */
1643bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1644module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1645
1646static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1647static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1648#define param_check_aaintbool param_check_int
1649static const struct kernel_param_ops param_ops_aaintbool = {
1650	.set = param_set_aaintbool,
1651	.get = param_get_aaintbool
1652};
1653/* Boot time disable flag */
1654static int apparmor_enabled __ro_after_init = 1;
1655module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1656
1657static int __init apparmor_enabled_setup(char *str)
1658{
1659	unsigned long enabled;
1660	int error = kstrtoul(str, 0, &enabled);
1661	if (!error)
1662		apparmor_enabled = enabled ? 1 : 0;
1663	return 1;
1664}
1665
1666__setup("apparmor=", apparmor_enabled_setup);
1667
1668/* set global flag turning off the ability to load policy */
1669static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1670{
1671	if (!apparmor_enabled)
1672		return -EINVAL;
1673	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1674		return -EPERM;
1675	return param_set_bool(val, kp);
1676}
1677
1678static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1679{
1680	if (!apparmor_enabled)
1681		return -EINVAL;
1682	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1683		return -EPERM;
1684	return param_get_bool(buffer, kp);
1685}
1686
1687static int param_set_aabool(const char *val, const struct kernel_param *kp)
1688{
1689	if (!apparmor_enabled)
1690		return -EINVAL;
1691	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1692		return -EPERM;
1693	return param_set_bool(val, kp);
1694}
1695
1696static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1697{
1698	if (!apparmor_enabled)
1699		return -EINVAL;
1700	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1701		return -EPERM;
1702	return param_get_bool(buffer, kp);
1703}
1704
1705static int param_set_aauint(const char *val, const struct kernel_param *kp)
1706{
1707	int error;
1708
1709	if (!apparmor_enabled)
1710		return -EINVAL;
1711	/* file is ro but enforce 2nd line check */
1712	if (apparmor_initialized)
1713		return -EPERM;
1714
1715	error = param_set_uint(val, kp);
1716	aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1717	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1718
1719	return error;
1720}
1721
1722static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1723{
1724	if (!apparmor_enabled)
1725		return -EINVAL;
1726	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1727		return -EPERM;
1728	return param_get_uint(buffer, kp);
1729}
1730
1731/* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1732static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1733{
1734	struct kernel_param kp_local;
1735	bool value;
1736	int error;
1737
1738	if (apparmor_initialized)
1739		return -EPERM;
1740
1741	/* Create local copy, with arg pointing to bool type. */
1742	value = !!*((int *)kp->arg);
1743	memcpy(&kp_local, kp, sizeof(kp_local));
1744	kp_local.arg = &value;
1745
1746	error = param_set_bool(val, &kp_local);
1747	if (!error)
1748		*((int *)kp->arg) = *((bool *)kp_local.arg);
1749	return error;
1750}
1751
1752/*
1753 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1754 * 1/0, this converts the "int that is actually bool" back to bool for
1755 * display in the /sys filesystem, while keeping it "int" for the LSM
1756 * infrastructure.
1757 */
1758static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1759{
1760	struct kernel_param kp_local;
1761	bool value;
1762
1763	/* Create local copy, with arg pointing to bool type. */
1764	value = !!*((int *)kp->arg);
1765	memcpy(&kp_local, kp, sizeof(kp_local));
1766	kp_local.arg = &value;
1767
1768	return param_get_bool(buffer, &kp_local);
1769}
1770
1771static int param_set_aacompressionlevel(const char *val,
1772					const struct kernel_param *kp)
1773{
1774	int error;
1775
1776	if (!apparmor_enabled)
1777		return -EINVAL;
1778	if (apparmor_initialized)
1779		return -EPERM;
1780
1781	error = param_set_int(val, kp);
1782
1783	aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
1784					       AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1785	pr_info("AppArmor: policy rawdata compression level set to %d\n",
1786		aa_g_rawdata_compression_level);
1787
1788	return error;
1789}
1790
1791static int param_get_aacompressionlevel(char *buffer,
1792					const struct kernel_param *kp)
1793{
1794	if (!apparmor_enabled)
1795		return -EINVAL;
1796	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1797		return -EPERM;
1798	return param_get_int(buffer, kp);
1799}
1800
1801static int param_get_audit(char *buffer, const struct kernel_param *kp)
1802{
1803	if (!apparmor_enabled)
1804		return -EINVAL;
1805	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1806		return -EPERM;
1807	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1808}
1809
1810static int param_set_audit(const char *val, const struct kernel_param *kp)
1811{
1812	int i;
1813
1814	if (!apparmor_enabled)
1815		return -EINVAL;
1816	if (!val)
1817		return -EINVAL;
1818	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1819		return -EPERM;
1820
1821	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1822	if (i < 0)
1823		return -EINVAL;
1824
1825	aa_g_audit = i;
1826	return 0;
1827}
1828
1829static int param_get_mode(char *buffer, const struct kernel_param *kp)
1830{
1831	if (!apparmor_enabled)
1832		return -EINVAL;
1833	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1834		return -EPERM;
1835
1836	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1837}
1838
1839static int param_set_mode(const char *val, const struct kernel_param *kp)
1840{
1841	int i;
1842
1843	if (!apparmor_enabled)
1844		return -EINVAL;
1845	if (!val)
1846		return -EINVAL;
1847	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1848		return -EPERM;
1849
1850	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1851			 val);
1852	if (i < 0)
1853		return -EINVAL;
1854
1855	aa_g_profile_mode = i;
1856	return 0;
1857}
1858
1859char *aa_get_buffer(bool in_atomic)
1860{
1861	union aa_buffer *aa_buf;
1862	struct aa_local_cache *cache;
1863	bool try_again = true;
1864	gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1865
1866	/* use per cpu cached buffers first */
1867	cache = get_cpu_ptr(&aa_local_buffers);
1868	if (!list_empty(&cache->head)) {
1869		aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
1870		list_del(&aa_buf->list);
1871		cache->hold--;
1872		cache->count--;
1873		put_cpu_ptr(&aa_local_buffers);
1874		return &aa_buf->buffer[0];
1875	}
1876	put_cpu_ptr(&aa_local_buffers);
1877
1878	if (!spin_trylock(&aa_buffers_lock)) {
1879		cache = get_cpu_ptr(&aa_local_buffers);
1880		cache->hold += 1;
1881		put_cpu_ptr(&aa_local_buffers);
1882		spin_lock(&aa_buffers_lock);
1883	} else {
1884		cache = get_cpu_ptr(&aa_local_buffers);
1885		put_cpu_ptr(&aa_local_buffers);
1886	}
1887retry:
1888	if (buffer_count > reserve_count ||
1889	    (in_atomic && !list_empty(&aa_global_buffers))) {
1890		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1891					  list);
1892		list_del(&aa_buf->list);
1893		buffer_count--;
1894		spin_unlock(&aa_buffers_lock);
1895		return aa_buf->buffer;
1896	}
1897	if (in_atomic) {
1898		/*
1899		 * out of reserve buffers and in atomic context so increase
1900		 * how many buffers to keep in reserve
1901		 */
1902		reserve_count++;
1903		flags = GFP_ATOMIC;
1904	}
1905	spin_unlock(&aa_buffers_lock);
1906
1907	if (!in_atomic)
1908		might_sleep();
1909	aa_buf = kmalloc(aa_g_path_max, flags);
1910	if (!aa_buf) {
1911		if (try_again) {
1912			try_again = false;
1913			spin_lock(&aa_buffers_lock);
1914			goto retry;
1915		}
1916		pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1917		return NULL;
1918	}
1919	return aa_buf->buffer;
1920}
1921
1922void aa_put_buffer(char *buf)
1923{
1924	union aa_buffer *aa_buf;
1925	struct aa_local_cache *cache;
1926
1927	if (!buf)
1928		return;
1929	aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1930
1931	cache = get_cpu_ptr(&aa_local_buffers);
1932	if (!cache->hold) {
1933		put_cpu_ptr(&aa_local_buffers);
1934
1935		if (spin_trylock(&aa_buffers_lock)) {
1936			/* put back on global list */
1937			list_add(&aa_buf->list, &aa_global_buffers);
1938			buffer_count++;
1939			spin_unlock(&aa_buffers_lock);
1940			cache = get_cpu_ptr(&aa_local_buffers);
1941			put_cpu_ptr(&aa_local_buffers);
1942			return;
1943		}
1944		/* contention on global list, fallback to percpu */
1945		cache = get_cpu_ptr(&aa_local_buffers);
1946		cache->hold += 1;
1947	}
1948
1949	/* cache in percpu list */
1950	list_add(&aa_buf->list, &cache->head);
1951	cache->count++;
1952	put_cpu_ptr(&aa_local_buffers);
1953}
1954
1955/*
1956 * AppArmor init functions
1957 */
1958
1959/**
1960 * set_init_ctx - set a task context and profile on the first task.
1961 *
1962 * TODO: allow setting an alternate profile than unconfined
1963 */
1964static int __init set_init_ctx(void)
1965{
1966	struct cred *cred = (__force struct cred *)current->real_cred;
1967
1968	set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1969
1970	return 0;
1971}
1972
1973static void destroy_buffers(void)
1974{
1975	union aa_buffer *aa_buf;
1976
1977	spin_lock(&aa_buffers_lock);
1978	while (!list_empty(&aa_global_buffers)) {
1979		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1980					 list);
1981		list_del(&aa_buf->list);
1982		spin_unlock(&aa_buffers_lock);
1983		kfree(aa_buf);
1984		spin_lock(&aa_buffers_lock);
1985	}
1986	spin_unlock(&aa_buffers_lock);
1987}
1988
1989static int __init alloc_buffers(void)
1990{
1991	union aa_buffer *aa_buf;
1992	int i, num;
1993
1994	/*
1995	 * per cpu set of cached allocated buffers used to help reduce
1996	 * lock contention
1997	 */
1998	for_each_possible_cpu(i) {
1999		per_cpu(aa_local_buffers, i).hold = 0;
2000		per_cpu(aa_local_buffers, i).count = 0;
2001		INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
2002	}
2003	/*
2004	 * A function may require two buffers at once. Usually the buffers are
2005	 * used for a short period of time and are shared. On UP kernel buffers
2006	 * two should be enough, with more CPUs it is possible that more
2007	 * buffers will be used simultaneously. The preallocated pool may grow.
2008	 * This preallocation has also the side-effect that AppArmor will be
2009	 * disabled early at boot if aa_g_path_max is extremly high.
2010	 */
2011	if (num_online_cpus() > 1)
2012		num = 4 + RESERVE_COUNT;
2013	else
2014		num = 2 + RESERVE_COUNT;
2015
2016	for (i = 0; i < num; i++) {
2017
2018		aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
2019				 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2020		if (!aa_buf) {
2021			destroy_buffers();
2022			return -ENOMEM;
 
 
 
 
2023		}
2024		aa_put_buffer(aa_buf->buffer);
2025	}
 
2026	return 0;
2027}
2028
2029#ifdef CONFIG_SYSCTL
2030static int apparmor_dointvec(const struct ctl_table *table, int write,
2031			     void *buffer, size_t *lenp, loff_t *ppos)
2032{
2033	if (!aa_current_policy_admin_capable(NULL))
2034		return -EPERM;
2035	if (!apparmor_enabled)
2036		return -EINVAL;
2037
2038	return proc_dointvec(table, write, buffer, lenp, ppos);
2039}
2040
 
 
 
 
 
2041static struct ctl_table apparmor_sysctl_table[] = {
2042#ifdef CONFIG_USER_NS
2043	{
2044		.procname       = "unprivileged_userns_apparmor_policy",
2045		.data           = &unprivileged_userns_apparmor_policy,
2046		.maxlen         = sizeof(int),
2047		.mode           = 0600,
2048		.proc_handler   = apparmor_dointvec,
2049	},
2050#endif /* CONFIG_USER_NS */
2051	{
2052		.procname       = "apparmor_display_secid_mode",
2053		.data           = &apparmor_display_secid_mode,
2054		.maxlen         = sizeof(int),
2055		.mode           = 0600,
2056		.proc_handler   = apparmor_dointvec,
2057	},
2058	{
2059		.procname       = "apparmor_restrict_unprivileged_unconfined",
2060		.data           = &aa_unprivileged_unconfined_restricted,
2061		.maxlen         = sizeof(int),
2062		.mode           = 0600,
2063		.proc_handler   = apparmor_dointvec,
2064	},
2065};
2066
2067static int __init apparmor_init_sysctl(void)
2068{
2069	return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
 
2070}
2071#else
2072static inline int apparmor_init_sysctl(void)
2073{
2074	return 0;
2075}
2076#endif /* CONFIG_SYSCTL */
2077
2078#if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
2079static unsigned int apparmor_ip_postroute(void *priv,
2080					  struct sk_buff *skb,
2081					  const struct nf_hook_state *state)
2082{
2083	struct aa_sk_ctx *ctx;
2084	struct sock *sk;
2085
2086	if (!skb->secmark)
2087		return NF_ACCEPT;
2088
2089	sk = skb_to_full_sk(skb);
2090	if (sk == NULL)
2091		return NF_ACCEPT;
2092
2093	ctx = aa_sock(sk);
2094	if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
2095				    skb->secmark, sk))
2096		return NF_ACCEPT;
2097
2098	return NF_DROP_ERR(-ECONNREFUSED);
2099
2100}
2101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2102static const struct nf_hook_ops apparmor_nf_ops[] = {
2103	{
2104		.hook =         apparmor_ip_postroute,
2105		.pf =           NFPROTO_IPV4,
2106		.hooknum =      NF_INET_POST_ROUTING,
2107		.priority =     NF_IP_PRI_SELINUX_FIRST,
2108	},
2109#if IS_ENABLED(CONFIG_IPV6)
2110	{
2111		.hook =         apparmor_ip_postroute,
2112		.pf =           NFPROTO_IPV6,
2113		.hooknum =      NF_INET_POST_ROUTING,
2114		.priority =     NF_IP6_PRI_SELINUX_FIRST,
2115	},
2116#endif
2117};
2118
2119static int __net_init apparmor_nf_register(struct net *net)
2120{
2121	return nf_register_net_hooks(net, apparmor_nf_ops,
 
 
2122				    ARRAY_SIZE(apparmor_nf_ops));
 
2123}
2124
2125static void __net_exit apparmor_nf_unregister(struct net *net)
2126{
2127	nf_unregister_net_hooks(net, apparmor_nf_ops,
2128				ARRAY_SIZE(apparmor_nf_ops));
2129}
2130
2131static struct pernet_operations apparmor_net_ops = {
2132	.init = apparmor_nf_register,
2133	.exit = apparmor_nf_unregister,
2134};
2135
2136static int __init apparmor_nf_ip_init(void)
2137{
2138	int err;
2139
2140	if (!apparmor_enabled)
2141		return 0;
2142
2143	err = register_pernet_subsys(&apparmor_net_ops);
2144	if (err)
2145		panic("Apparmor: register_pernet_subsys: error %d\n", err);
2146
2147	return 0;
2148}
2149__initcall(apparmor_nf_ip_init);
2150#endif
2151
2152static char nulldfa_src[] = {
2153	#include "nulldfa.in"
2154};
2155static struct aa_dfa *nulldfa;
2156
2157static char stacksplitdfa_src[] = {
2158	#include "stacksplitdfa.in"
2159};
2160struct aa_dfa *stacksplitdfa;
2161struct aa_policydb *nullpdb;
2162
2163static int __init aa_setup_dfa_engine(void)
2164{
2165	int error = -ENOMEM;
2166
2167	nullpdb = aa_alloc_pdb(GFP_KERNEL);
2168	if (!nullpdb)
2169		return -ENOMEM;
2170
2171	nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
2172			    TO_ACCEPT1_FLAG(YYTD_DATA32) |
2173			    TO_ACCEPT2_FLAG(YYTD_DATA32));
2174	if (IS_ERR(nulldfa)) {
2175		error = PTR_ERR(nulldfa);
2176		goto fail;
2177	}
2178	nullpdb->dfa = aa_get_dfa(nulldfa);
2179	nullpdb->perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
2180	if (!nullpdb->perms)
2181		goto fail;
2182	nullpdb->size = 2;
2183
2184	stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
2185				      sizeof(stacksplitdfa_src),
2186				      TO_ACCEPT1_FLAG(YYTD_DATA32) |
2187				      TO_ACCEPT2_FLAG(YYTD_DATA32));
2188	if (IS_ERR(stacksplitdfa)) {
2189		error = PTR_ERR(stacksplitdfa);
2190		goto fail;
2191	}
2192
2193	return 0;
2194
2195fail:
2196	aa_put_pdb(nullpdb);
2197	aa_put_dfa(nulldfa);
2198	nullpdb = NULL;
2199	nulldfa = NULL;
2200	stacksplitdfa = NULL;
2201
2202	return error;
2203}
2204
2205static void __init aa_teardown_dfa_engine(void)
2206{
2207	aa_put_dfa(stacksplitdfa);
2208	aa_put_dfa(nulldfa);
2209	aa_put_pdb(nullpdb);
2210	nullpdb = NULL;
2211	stacksplitdfa = NULL;
2212	nulldfa = NULL;
2213}
2214
2215static int __init apparmor_init(void)
2216{
2217	int error;
2218
 
 
2219	error = aa_setup_dfa_engine();
2220	if (error) {
2221		AA_ERROR("Unable to setup dfa engine\n");
2222		goto alloc_out;
2223	}
2224
2225	error = aa_alloc_root_ns();
2226	if (error) {
2227		AA_ERROR("Unable to allocate default profile namespace\n");
2228		goto alloc_out;
2229	}
2230
2231	error = apparmor_init_sysctl();
2232	if (error) {
2233		AA_ERROR("Unable to register sysctls\n");
2234		goto alloc_out;
2235
2236	}
2237
2238	error = alloc_buffers();
2239	if (error) {
2240		AA_ERROR("Unable to allocate work buffers\n");
2241		goto alloc_out;
2242	}
2243
2244	error = set_init_ctx();
2245	if (error) {
2246		AA_ERROR("Failed to set context on init task\n");
2247		aa_free_root_ns();
2248		goto buffers_out;
2249	}
2250	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
2251				&apparmor_lsmid);
2252
2253	/* Report that AppArmor successfully initialized */
2254	apparmor_initialized = 1;
2255	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
2256		aa_info_message("AppArmor initialized: complain mode enabled");
2257	else if (aa_g_profile_mode == APPARMOR_KILL)
2258		aa_info_message("AppArmor initialized: kill mode enabled");
2259	else
2260		aa_info_message("AppArmor initialized");
2261
2262	return error;
2263
2264buffers_out:
2265	destroy_buffers();
 
2266alloc_out:
2267	aa_destroy_aafs();
2268	aa_teardown_dfa_engine();
2269
2270	apparmor_enabled = false;
2271	return error;
2272}
2273
2274DEFINE_LSM(apparmor) = {
2275	.name = "apparmor",
2276	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
2277	.enabled = &apparmor_enabled,
2278	.blobs = &apparmor_blob_sizes,
2279	.init = apparmor_init,
2280};