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