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