1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Security plug functions 4 * 5 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com> 6 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 7 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com> 8 * Copyright (C) 2016 Mellanox Technologies 9 * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com> 10 */ 11 12 #define pr_fmt(fmt) "LSM: " fmt 13 14 #include <linux/bpf.h> 15 #include <linux/capability.h> 16 #include <linux/dcache.h> 17 #include <linux/export.h> 18 #include <linux/init.h> 19 #include <linux/kernel.h> 20 #include <linux/kernel_read_file.h> 21 #include <linux/lsm_hooks.h> 22 #include <linux/fsnotify.h> 23 #include <linux/mman.h> 24 #include <linux/mount.h> 25 #include <linux/personality.h> 26 #include <linux/backing-dev.h> 27 #include <linux/string.h> 28 #include <linux/xattr.h> 29 #include <linux/msg.h> 30 #include <linux/overflow.h> 31 #include <linux/perf_event.h> 32 #include <linux/fs.h> 33 #include <net/flow.h> 34 #include <net/sock.h> 35 36 /* How many LSMs were built into the kernel? */ 37 #define LSM_COUNT (__end_lsm_info - __start_lsm_info) 38 39 /* 40 * How many LSMs are built into the kernel as determined at 41 * build time. Used to determine fixed array sizes. 42 * The capability module is accounted for by CONFIG_SECURITY 43 */ 44 #define LSM_CONFIG_COUNT ( \ 45 (IS_ENABLED(CONFIG_SECURITY) ? 1 : 0) + \ 46 (IS_ENABLED(CONFIG_SECURITY_SELINUX) ? 1 : 0) + \ 47 (IS_ENABLED(CONFIG_SECURITY_SMACK) ? 1 : 0) + \ 48 (IS_ENABLED(CONFIG_SECURITY_TOMOYO) ? 1 : 0) + \ 49 (IS_ENABLED(CONFIG_SECURITY_APPARMOR) ? 1 : 0) + \ 50 (IS_ENABLED(CONFIG_SECURITY_YAMA) ? 1 : 0) + \ 51 (IS_ENABLED(CONFIG_SECURITY_LOADPIN) ? 1 : 0) + \ 52 (IS_ENABLED(CONFIG_SECURITY_SAFESETID) ? 1 : 0) + \ 53 (IS_ENABLED(CONFIG_SECURITY_LOCKDOWN_LSM) ? 1 : 0) + \ 54 (IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \ 55 (IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0) + \ 56 (IS_ENABLED(CONFIG_IMA) ? 1 : 0) + \ 57 (IS_ENABLED(CONFIG_EVM) ? 1 : 0) + \ 58 (IS_ENABLED(CONFIG_SECURITY_IPE) ? 1 : 0)) 59 60 #define SECURITY_HOOK_ACTIVE_KEY(HOOK, IDX) security_hook_active_##HOOK##_##IDX 61 62 /* 63 * Identifier for the LSM static calls. 64 * HOOK is an LSM hook as defined in linux/lsm_hookdefs.h 65 * IDX is the index of the static call. 0 <= NUM < MAX_LSM_COUNT 66 */ 67 #define LSM_STATIC_CALL(HOOK, IDX) lsm_static_call_##HOOK##_##IDX 68 69 /* 70 * Call the macro M for each LSM hook MAX_LSM_COUNT times. 71 */ 72 #define LSM_LOOP_UNROLL(M, ...) \ 73 do { \ 74 UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__) \ 75 } while (0) 76 77 #define LSM_DEFINE_UNROLL(M, ...) UNROLL(MAX_LSM_COUNT, M, __VA_ARGS__) 78 79 /* 80 * These are descriptions of the reasons that can be passed to the 81 * security_locked_down() LSM hook. Placing this array here allows 82 * all security modules to use the same descriptions for auditing 83 * purposes. 84 */ 85 const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = { 86 [LOCKDOWN_NONE] = "none", 87 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading", 88 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port", 89 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access", 90 [LOCKDOWN_KEXEC] = "kexec of unsigned images", 91 [LOCKDOWN_HIBERNATION] = "hibernation", 92 [LOCKDOWN_PCI_ACCESS] = "direct PCI access", 93 [LOCKDOWN_IOPORT] = "raw io port access", 94 [LOCKDOWN_MSR] = "raw MSR access", 95 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables", 96 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents", 97 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage", 98 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO", 99 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters", 100 [LOCKDOWN_MMIOTRACE] = "unsafe mmio", 101 [LOCKDOWN_DEBUGFS] = "debugfs access", 102 [LOCKDOWN_XMON_WR] = "xmon write access", 103 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM", 104 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM", 105 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection", 106 [LOCKDOWN_INTEGRITY_MAX] = "integrity", 107 [LOCKDOWN_KCORE] = "/proc/kcore access", 108 [LOCKDOWN_KPROBES] = "use of kprobes", 109 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM", 110 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM", 111 [LOCKDOWN_PERF] = "unsafe use of perf", 112 [LOCKDOWN_TRACEFS] = "use of tracefs", 113 [LOCKDOWN_XMON_RW] = "xmon read and write access", 114 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret", 115 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", 116 }; 117 118 static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain); 119 120 static struct kmem_cache *lsm_file_cache; 121 static struct kmem_cache *lsm_inode_cache; 122 123 char *lsm_names; 124 static struct lsm_blob_sizes blob_sizes __ro_after_init; 125 126 /* Boot-time LSM user choice */ 127 static __initdata const char *chosen_lsm_order; 128 static __initdata const char *chosen_major_lsm; 129 130 static __initconst const char *const builtin_lsm_order = CONFIG_LSM; 131 132 /* Ordered list of LSMs to initialize. */ 133 static __initdata struct lsm_info **ordered_lsms; 134 static __initdata struct lsm_info *exclusive; 135 136 #ifdef CONFIG_HAVE_STATIC_CALL 137 #define LSM_HOOK_TRAMP(NAME, NUM) \ 138 &STATIC_CALL_TRAMP(LSM_STATIC_CALL(NAME, NUM)) 139 #else 140 #define LSM_HOOK_TRAMP(NAME, NUM) NULL 141 #endif 142 143 /* 144 * Define static calls and static keys for each LSM hook. 145 */ 146 #define DEFINE_LSM_STATIC_CALL(NUM, NAME, RET, ...) \ 147 DEFINE_STATIC_CALL_NULL(LSM_STATIC_CALL(NAME, NUM), \ 148 *((RET(*)(__VA_ARGS__))NULL)); \ 149 DEFINE_STATIC_KEY_FALSE(SECURITY_HOOK_ACTIVE_KEY(NAME, NUM)); 150 151 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ 152 LSM_DEFINE_UNROLL(DEFINE_LSM_STATIC_CALL, NAME, RET, __VA_ARGS__) 153 #include <linux/lsm_hook_defs.h> 154 #undef LSM_HOOK 155 #undef DEFINE_LSM_STATIC_CALL 156 157 /* 158 * Initialise a table of static calls for each LSM hook. 159 * DEFINE_STATIC_CALL_NULL invocation above generates a key (STATIC_CALL_KEY) 160 * and a trampoline (STATIC_CALL_TRAMP) which are used to call 161 * __static_call_update when updating the static call. 162 * 163 * The static calls table is used by early LSMs, some architectures can fault on 164 * unaligned accesses and the fault handling code may not be ready by then. 165 * Thus, the static calls table should be aligned to avoid any unhandled faults 166 * in early init. 167 */ 168 struct lsm_static_calls_table 169 static_calls_table __ro_after_init __aligned(sizeof(u64)) = { 170 #define INIT_LSM_STATIC_CALL(NUM, NAME) \ 171 (struct lsm_static_call) { \ 172 .key = &STATIC_CALL_KEY(LSM_STATIC_CALL(NAME, NUM)), \ 173 .trampoline = LSM_HOOK_TRAMP(NAME, NUM), \ 174 .active = &SECURITY_HOOK_ACTIVE_KEY(NAME, NUM), \ 175 }, 176 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ 177 .NAME = { \ 178 LSM_DEFINE_UNROLL(INIT_LSM_STATIC_CALL, NAME) \ 179 }, 180 #include <linux/lsm_hook_defs.h> 181 #undef LSM_HOOK 182 #undef INIT_LSM_STATIC_CALL 183 }; 184 185 static __initdata bool debug; 186 #define init_debug(...) \ 187 do { \ 188 if (debug) \ 189 pr_info(__VA_ARGS__); \ 190 } while (0) 191 192 static bool __init is_enabled(struct lsm_info *lsm) 193 { 194 if (!lsm->enabled) 195 return false; 196 197 return *lsm->enabled; 198 } 199 200 /* Mark an LSM's enabled flag. */ 201 static int lsm_enabled_true __initdata = 1; 202 static int lsm_enabled_false __initdata = 0; 203 static void __init set_enabled(struct lsm_info *lsm, bool enabled) 204 { 205 /* 206 * When an LSM hasn't configured an enable variable, we can use 207 * a hard-coded location for storing the default enabled state. 208 */ 209 if (!lsm->enabled) { 210 if (enabled) 211 lsm->enabled = &lsm_enabled_true; 212 else 213 lsm->enabled = &lsm_enabled_false; 214 } else if (lsm->enabled == &lsm_enabled_true) { 215 if (!enabled) 216 lsm->enabled = &lsm_enabled_false; 217 } else if (lsm->enabled == &lsm_enabled_false) { 218 if (enabled) 219 lsm->enabled = &lsm_enabled_true; 220 } else { 221 *lsm->enabled = enabled; 222 } 223 } 224 225 /* Is an LSM already listed in the ordered LSMs list? */ 226 static bool __init exists_ordered_lsm(struct lsm_info *lsm) 227 { 228 struct lsm_info **check; 229 230 for (check = ordered_lsms; *check; check++) 231 if (*check == lsm) 232 return true; 233 234 return false; 235 } 236 237 /* Append an LSM to the list of ordered LSMs to initialize. */ 238 static int last_lsm __initdata; 239 static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from) 240 { 241 /* Ignore duplicate selections. */ 242 if (exists_ordered_lsm(lsm)) 243 return; 244 245 if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM static calls!?\n", from)) 246 return; 247 248 /* Enable this LSM, if it is not already set. */ 249 if (!lsm->enabled) 250 lsm->enabled = &lsm_enabled_true; 251 ordered_lsms[last_lsm++] = lsm; 252 253 init_debug("%s ordered: %s (%s)\n", from, lsm->name, 254 is_enabled(lsm) ? "enabled" : "disabled"); 255 } 256 257 /* Is an LSM allowed to be initialized? */ 258 static bool __init lsm_allowed(struct lsm_info *lsm) 259 { 260 /* Skip if the LSM is disabled. */ 261 if (!is_enabled(lsm)) 262 return false; 263 264 /* Not allowed if another exclusive LSM already initialized. */ 265 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) { 266 init_debug("exclusive disabled: %s\n", lsm->name); 267 return false; 268 } 269 270 return true; 271 } 272 273 static void __init lsm_set_blob_size(int *need, int *lbs) 274 { 275 int offset; 276 277 if (*need <= 0) 278 return; 279 280 offset = ALIGN(*lbs, sizeof(void *)); 281 *lbs = offset + *need; 282 *need = offset; 283 } 284 285 static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed) 286 { 287 if (!needed) 288 return; 289 290 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred); 291 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file); 292 lsm_set_blob_size(&needed->lbs_ib, &blob_sizes.lbs_ib); 293 /* 294 * The inode blob gets an rcu_head in addition to 295 * what the modules might need. 296 */ 297 if (needed->lbs_inode && blob_sizes.lbs_inode == 0) 298 blob_sizes.lbs_inode = sizeof(struct rcu_head); 299 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode); 300 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc); 301 lsm_set_blob_size(&needed->lbs_key, &blob_sizes.lbs_key); 302 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg); 303 lsm_set_blob_size(&needed->lbs_perf_event, &blob_sizes.lbs_perf_event); 304 lsm_set_blob_size(&needed->lbs_sock, &blob_sizes.lbs_sock); 305 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock); 306 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task); 307 lsm_set_blob_size(&needed->lbs_tun_dev, &blob_sizes.lbs_tun_dev); 308 lsm_set_blob_size(&needed->lbs_xattr_count, 309 &blob_sizes.lbs_xattr_count); 310 lsm_set_blob_size(&needed->lbs_bdev, &blob_sizes.lbs_bdev); 311 } 312 313 /* Prepare LSM for initialization. */ 314 static void __init prepare_lsm(struct lsm_info *lsm) 315 { 316 int enabled = lsm_allowed(lsm); 317 318 /* Record enablement (to handle any following exclusive LSMs). */ 319 set_enabled(lsm, enabled); 320 321 /* If enabled, do pre-initialization work. */ 322 if (enabled) { 323 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) { 324 exclusive = lsm; 325 init_debug("exclusive chosen: %s\n", lsm->name); 326 } 327 328 lsm_set_blob_sizes(lsm->blobs); 329 } 330 } 331 332 /* Initialize a given LSM, if it is enabled. */ 333 static void __init initialize_lsm(struct lsm_info *lsm) 334 { 335 if (is_enabled(lsm)) { 336 int ret; 337 338 init_debug("initializing %s\n", lsm->name); 339 ret = lsm->init(); 340 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret); 341 } 342 } 343 344 /* 345 * Current index to use while initializing the lsm id list. 346 */ 347 u32 lsm_active_cnt __ro_after_init; 348 const struct lsm_id *lsm_idlist[LSM_CONFIG_COUNT]; 349 350 /* Populate ordered LSMs list from comma-separated LSM name list. */ 351 static void __init ordered_lsm_parse(const char *order, const char *origin) 352 { 353 struct lsm_info *lsm; 354 char *sep, *name, *next; 355 356 /* LSM_ORDER_FIRST is always first. */ 357 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { 358 if (lsm->order == LSM_ORDER_FIRST) 359 append_ordered_lsm(lsm, " first"); 360 } 361 362 /* Process "security=", if given. */ 363 if (chosen_major_lsm) { 364 struct lsm_info *major; 365 366 /* 367 * To match the original "security=" behavior, this 368 * explicitly does NOT fallback to another Legacy Major 369 * if the selected one was separately disabled: disable 370 * all non-matching Legacy Major LSMs. 371 */ 372 for (major = __start_lsm_info; major < __end_lsm_info; 373 major++) { 374 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) && 375 strcmp(major->name, chosen_major_lsm) != 0) { 376 set_enabled(major, false); 377 init_debug("security=%s disabled: %s (only one legacy major LSM)\n", 378 chosen_major_lsm, major->name); 379 } 380 } 381 } 382 383 sep = kstrdup(order, GFP_KERNEL); 384 next = sep; 385 /* Walk the list, looking for matching LSMs. */ 386 while ((name = strsep(&next, ",")) != NULL) { 387 bool found = false; 388 389 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { 390 if (strcmp(lsm->name, name) == 0) { 391 if (lsm->order == LSM_ORDER_MUTABLE) 392 append_ordered_lsm(lsm, origin); 393 found = true; 394 } 395 } 396 397 if (!found) 398 init_debug("%s ignored: %s (not built into kernel)\n", 399 origin, name); 400 } 401 402 /* Process "security=", if given. */ 403 if (chosen_major_lsm) { 404 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { 405 if (exists_ordered_lsm(lsm)) 406 continue; 407 if (strcmp(lsm->name, chosen_major_lsm) == 0) 408 append_ordered_lsm(lsm, "security="); 409 } 410 } 411 412 /* LSM_ORDER_LAST is always last. */ 413 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { 414 if (lsm->order == LSM_ORDER_LAST) 415 append_ordered_lsm(lsm, " last"); 416 } 417 418 /* Disable all LSMs not in the ordered list. */ 419 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) { 420 if (exists_ordered_lsm(lsm)) 421 continue; 422 set_enabled(lsm, false); 423 init_debug("%s skipped: %s (not in requested order)\n", 424 origin, lsm->name); 425 } 426 427 kfree(sep); 428 } 429 430 static void __init lsm_static_call_init(struct security_hook_list *hl) 431 { 432 struct lsm_static_call *scall = hl->scalls; 433 int i; 434 435 for (i = 0; i < MAX_LSM_COUNT; i++) { 436 /* Update the first static call that is not used yet */ 437 if (!scall->hl) { 438 __static_call_update(scall->key, scall->trampoline, 439 hl->hook.lsm_func_addr); 440 scall->hl = hl; 441 static_branch_enable(scall->active); 442 return; 443 } 444 scall++; 445 } 446 panic("%s - Ran out of static slots.\n", __func__); 447 } 448 449 static void __init lsm_early_cred(struct cred *cred); 450 static void __init lsm_early_task(struct task_struct *task); 451 452 static int lsm_append(const char *new, char **result); 453 454 static void __init report_lsm_order(void) 455 { 456 struct lsm_info **lsm, *early; 457 int first = 0; 458 459 pr_info("initializing lsm="); 460 461 /* Report each enabled LSM name, comma separated. */ 462 for (early = __start_early_lsm_info; 463 early < __end_early_lsm_info; early++) 464 if (is_enabled(early)) 465 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name); 466 for (lsm = ordered_lsms; *lsm; lsm++) 467 if (is_enabled(*lsm)) 468 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name); 469 470 pr_cont("\n"); 471 } 472 473 static void __init ordered_lsm_init(void) 474 { 475 struct lsm_info **lsm; 476 477 ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms), 478 GFP_KERNEL); 479 480 if (chosen_lsm_order) { 481 if (chosen_major_lsm) { 482 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n", 483 chosen_major_lsm, chosen_lsm_order); 484 chosen_major_lsm = NULL; 485 } 486 ordered_lsm_parse(chosen_lsm_order, "cmdline"); 487 } else 488 ordered_lsm_parse(builtin_lsm_order, "builtin"); 489 490 for (lsm = ordered_lsms; *lsm; lsm++) 491 prepare_lsm(*lsm); 492 493 report_lsm_order(); 494 495 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred); 496 init_debug("file blob size = %d\n", blob_sizes.lbs_file); 497 init_debug("ib blob size = %d\n", blob_sizes.lbs_ib); 498 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode); 499 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc); 500 #ifdef CONFIG_KEYS 501 init_debug("key blob size = %d\n", blob_sizes.lbs_key); 502 #endif /* CONFIG_KEYS */ 503 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg); 504 init_debug("sock blob size = %d\n", blob_sizes.lbs_sock); 505 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock); 506 init_debug("perf event blob size = %d\n", blob_sizes.lbs_perf_event); 507 init_debug("task blob size = %d\n", blob_sizes.lbs_task); 508 init_debug("tun device blob size = %d\n", blob_sizes.lbs_tun_dev); 509 init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count); 510 init_debug("bdev blob size = %d\n", blob_sizes.lbs_bdev); 511 512 /* 513 * Create any kmem_caches needed for blobs 514 */ 515 if (blob_sizes.lbs_file) 516 lsm_file_cache = kmem_cache_create("lsm_file_cache", 517 blob_sizes.lbs_file, 0, 518 SLAB_PANIC, NULL); 519 if (blob_sizes.lbs_inode) 520 lsm_inode_cache = kmem_cache_create("lsm_inode_cache", 521 blob_sizes.lbs_inode, 0, 522 SLAB_PANIC, NULL); 523 524 lsm_early_cred((struct cred *) current->cred); 525 lsm_early_task(current); 526 for (lsm = ordered_lsms; *lsm; lsm++) 527 initialize_lsm(*lsm); 528 529 kfree(ordered_lsms); 530 } 531 532 int __init early_security_init(void) 533 { 534 struct lsm_info *lsm; 535 536 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) { 537 if (!lsm->enabled) 538 lsm->enabled = &lsm_enabled_true; 539 prepare_lsm(lsm); 540 initialize_lsm(lsm); 541 } 542 543 return 0; 544 } 545 546 /** 547 * security_init - initializes the security framework 548 * 549 * This should be called early in the kernel initialization sequence. 550 */ 551 int __init security_init(void) 552 { 553 struct lsm_info *lsm; 554 555 init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*"); 556 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order); 557 init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*"); 558 559 /* 560 * Append the names of the early LSM modules now that kmalloc() is 561 * available 562 */ 563 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) { 564 init_debug(" early started: %s (%s)\n", lsm->name, 565 is_enabled(lsm) ? "enabled" : "disabled"); 566 if (lsm->enabled) 567 lsm_append(lsm->name, &lsm_names); 568 } 569 570 /* Load LSMs in specified order. */ 571 ordered_lsm_init(); 572 573 return 0; 574 } 575 576 /* Save user chosen LSM */ 577 static int __init choose_major_lsm(char *str) 578 { 579 chosen_major_lsm = str; 580 return 1; 581 } 582 __setup("security=", choose_major_lsm); 583 584 /* Explicitly choose LSM initialization order. */ 585 static int __init choose_lsm_order(char *str) 586 { 587 chosen_lsm_order = str; 588 return 1; 589 } 590 __setup("lsm=", choose_lsm_order); 591 592 /* Enable LSM order debugging. */ 593 static int __init enable_debug(char *str) 594 { 595 debug = true; 596 return 1; 597 } 598 __setup("lsm.debug", enable_debug); 599 600 static bool match_last_lsm(const char *list, const char *lsm) 601 { 602 const char *last; 603 604 if (WARN_ON(!list || !lsm)) 605 return false; 606 last = strrchr(list, ','); 607 if (last) 608 /* Pass the comma, strcmp() will check for '\0' */ 609 last++; 610 else 611 last = list; 612 return !strcmp(last, lsm); 613 } 614 615 static int lsm_append(const char *new, char **result) 616 { 617 char *cp; 618 619 if (*result == NULL) { 620 *result = kstrdup(new, GFP_KERNEL); 621 if (*result == NULL) 622 return -ENOMEM; 623 } else { 624 /* Check if it is the last registered name */ 625 if (match_last_lsm(*result, new)) 626 return 0; 627 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new); 628 if (cp == NULL) 629 return -ENOMEM; 630 kfree(*result); 631 *result = cp; 632 } 633 return 0; 634 } 635 636 /** 637 * security_add_hooks - Add a modules hooks to the hook lists. 638 * @hooks: the hooks to add 639 * @count: the number of hooks to add 640 * @lsmid: the identification information for the security module 641 * 642 * Each LSM has to register its hooks with the infrastructure. 643 */ 644 void __init security_add_hooks(struct security_hook_list *hooks, int count, 645 const struct lsm_id *lsmid) 646 { 647 int i; 648 649 /* 650 * A security module may call security_add_hooks() more 651 * than once during initialization, and LSM initialization 652 * is serialized. Landlock is one such case. 653 * Look at the previous entry, if there is one, for duplication. 654 */ 655 if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) { 656 if (lsm_active_cnt >= LSM_CONFIG_COUNT) 657 panic("%s Too many LSMs registered.\n", __func__); 658 lsm_idlist[lsm_active_cnt++] = lsmid; 659 } 660 661 for (i = 0; i < count; i++) { 662 hooks[i].lsmid = lsmid; 663 lsm_static_call_init(&hooks[i]); 664 } 665 666 /* 667 * Don't try to append during early_security_init(), we'll come back 668 * and fix this up afterwards. 669 */ 670 if (slab_is_available()) { 671 if (lsm_append(lsmid->name, &lsm_names) < 0) 672 panic("%s - Cannot get early memory.\n", __func__); 673 } 674 } 675 676 int call_blocking_lsm_notifier(enum lsm_event event, void *data) 677 { 678 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain, 679 event, data); 680 } 681 EXPORT_SYMBOL(call_blocking_lsm_notifier); 682 683 int register_blocking_lsm_notifier(struct notifier_block *nb) 684 { 685 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain, 686 nb); 687 } 688 EXPORT_SYMBOL(register_blocking_lsm_notifier); 689 690 int unregister_blocking_lsm_notifier(struct notifier_block *nb) 691 { 692 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain, 693 nb); 694 } 695 EXPORT_SYMBOL(unregister_blocking_lsm_notifier); 696 697 /** 698 * lsm_blob_alloc - allocate a composite blob 699 * @dest: the destination for the blob 700 * @size: the size of the blob 701 * @gfp: allocation type 702 * 703 * Allocate a blob for all the modules 704 * 705 * Returns 0, or -ENOMEM if memory can't be allocated. 706 */ 707 static int lsm_blob_alloc(void **dest, size_t size, gfp_t gfp) 708 { 709 if (size == 0) { 710 *dest = NULL; 711 return 0; 712 } 713 714 *dest = kzalloc(size, gfp); 715 if (*dest == NULL) 716 return -ENOMEM; 717 return 0; 718 } 719 720 /** 721 * lsm_cred_alloc - allocate a composite cred blob 722 * @cred: the cred that needs a blob 723 * @gfp: allocation type 724 * 725 * Allocate the cred blob for all the modules 726 * 727 * Returns 0, or -ENOMEM if memory can't be allocated. 728 */ 729 static int lsm_cred_alloc(struct cred *cred, gfp_t gfp) 730 { 731 return lsm_blob_alloc(&cred->security, blob_sizes.lbs_cred, gfp); 732 } 733 734 /** 735 * lsm_early_cred - during initialization allocate a composite cred blob 736 * @cred: the cred that needs a blob 737 * 738 * Allocate the cred blob for all the modules 739 */ 740 static void __init lsm_early_cred(struct cred *cred) 741 { 742 int rc = lsm_cred_alloc(cred, GFP_KERNEL); 743 744 if (rc) 745 panic("%s: Early cred alloc failed.\n", __func__); 746 } 747 748 /** 749 * lsm_file_alloc - allocate a composite file blob 750 * @file: the file that needs a blob 751 * 752 * Allocate the file blob for all the modules 753 * 754 * Returns 0, or -ENOMEM if memory can't be allocated. 755 */ 756 static int lsm_file_alloc(struct file *file) 757 { 758 if (!lsm_file_cache) { 759 file->f_security = NULL; 760 return 0; 761 } 762 763 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL); 764 if (file->f_security == NULL) 765 return -ENOMEM; 766 return 0; 767 } 768 769 /** 770 * lsm_inode_alloc - allocate a composite inode blob 771 * @inode: the inode that needs a blob 772 * 773 * Allocate the inode blob for all the modules 774 * 775 * Returns 0, or -ENOMEM if memory can't be allocated. 776 */ 777 static int lsm_inode_alloc(struct inode *inode) 778 { 779 if (!lsm_inode_cache) { 780 inode->i_security = NULL; 781 return 0; 782 } 783 784 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS); 785 if (inode->i_security == NULL) 786 return -ENOMEM; 787 return 0; 788 } 789 790 /** 791 * lsm_task_alloc - allocate a composite task blob 792 * @task: the task that needs a blob 793 * 794 * Allocate the task blob for all the modules 795 * 796 * Returns 0, or -ENOMEM if memory can't be allocated. 797 */ 798 static int lsm_task_alloc(struct task_struct *task) 799 { 800 return lsm_blob_alloc(&task->security, blob_sizes.lbs_task, GFP_KERNEL); 801 } 802 803 /** 804 * lsm_ipc_alloc - allocate a composite ipc blob 805 * @kip: the ipc that needs a blob 806 * 807 * Allocate the ipc blob for all the modules 808 * 809 * Returns 0, or -ENOMEM if memory can't be allocated. 810 */ 811 static int lsm_ipc_alloc(struct kern_ipc_perm *kip) 812 { 813 return lsm_blob_alloc(&kip->security, blob_sizes.lbs_ipc, GFP_KERNEL); 814 } 815 816 #ifdef CONFIG_KEYS 817 /** 818 * lsm_key_alloc - allocate a composite key blob 819 * @key: the key that needs a blob 820 * 821 * Allocate the key blob for all the modules 822 * 823 * Returns 0, or -ENOMEM if memory can't be allocated. 824 */ 825 static int lsm_key_alloc(struct key *key) 826 { 827 return lsm_blob_alloc(&key->security, blob_sizes.lbs_key, GFP_KERNEL); 828 } 829 #endif /* CONFIG_KEYS */ 830 831 /** 832 * lsm_msg_msg_alloc - allocate a composite msg_msg blob 833 * @mp: the msg_msg that needs a blob 834 * 835 * Allocate the ipc blob for all the modules 836 * 837 * Returns 0, or -ENOMEM if memory can't be allocated. 838 */ 839 static int lsm_msg_msg_alloc(struct msg_msg *mp) 840 { 841 return lsm_blob_alloc(&mp->security, blob_sizes.lbs_msg_msg, 842 GFP_KERNEL); 843 } 844 845 /** 846 * lsm_bdev_alloc - allocate a composite block_device blob 847 * @bdev: the block_device that needs a blob 848 * 849 * Allocate the block_device blob for all the modules 850 * 851 * Returns 0, or -ENOMEM if memory can't be allocated. 852 */ 853 static int lsm_bdev_alloc(struct block_device *bdev) 854 { 855 if (blob_sizes.lbs_bdev == 0) { 856 bdev->bd_security = NULL; 857 return 0; 858 } 859 860 bdev->bd_security = kzalloc(blob_sizes.lbs_bdev, GFP_KERNEL); 861 if (!bdev->bd_security) 862 return -ENOMEM; 863 864 return 0; 865 } 866 867 /** 868 * lsm_early_task - during initialization allocate a composite task blob 869 * @task: the task that needs a blob 870 * 871 * Allocate the task blob for all the modules 872 */ 873 static void __init lsm_early_task(struct task_struct *task) 874 { 875 int rc = lsm_task_alloc(task); 876 877 if (rc) 878 panic("%s: Early task alloc failed.\n", __func__); 879 } 880 881 /** 882 * lsm_superblock_alloc - allocate a composite superblock blob 883 * @sb: the superblock that needs a blob 884 * 885 * Allocate the superblock blob for all the modules 886 * 887 * Returns 0, or -ENOMEM if memory can't be allocated. 888 */ 889 static int lsm_superblock_alloc(struct super_block *sb) 890 { 891 return lsm_blob_alloc(&sb->s_security, blob_sizes.lbs_superblock, 892 GFP_KERNEL); 893 } 894 895 /** 896 * lsm_fill_user_ctx - Fill a user space lsm_ctx structure 897 * @uctx: a userspace LSM context to be filled 898 * @uctx_len: available uctx size (input), used uctx size (output) 899 * @val: the new LSM context value 900 * @val_len: the size of the new LSM context value 901 * @id: LSM id 902 * @flags: LSM defined flags 903 * 904 * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL 905 * simply calculate the required size to output via @utc_len and return 906 * success. 907 * 908 * Returns 0 on success, -E2BIG if userspace buffer is not large enough, 909 * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated. 910 */ 911 int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len, 912 void *val, size_t val_len, 913 u64 id, u64 flags) 914 { 915 struct lsm_ctx *nctx = NULL; 916 size_t nctx_len; 917 int rc = 0; 918 919 nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *)); 920 if (nctx_len > *uctx_len) { 921 rc = -E2BIG; 922 goto out; 923 } 924 925 /* no buffer - return success/0 and set @uctx_len to the req size */ 926 if (!uctx) 927 goto out; 928 929 nctx = kzalloc(nctx_len, GFP_KERNEL); 930 if (nctx == NULL) { 931 rc = -ENOMEM; 932 goto out; 933 } 934 nctx->id = id; 935 nctx->flags = flags; 936 nctx->len = nctx_len; 937 nctx->ctx_len = val_len; 938 memcpy(nctx->ctx, val, val_len); 939 940 if (copy_to_user(uctx, nctx, nctx_len)) 941 rc = -EFAULT; 942 943 out: 944 kfree(nctx); 945 *uctx_len = nctx_len; 946 return rc; 947 } 948 949 /* 950 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and 951 * can be accessed with: 952 * 953 * LSM_RET_DEFAULT(<hook_name>) 954 * 955 * The macros below define static constants for the default value of each 956 * LSM hook. 957 */ 958 #define LSM_RET_DEFAULT(NAME) (NAME##_default) 959 #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME) 960 #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \ 961 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT); 962 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ 963 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME) 964 965 #include <linux/lsm_hook_defs.h> 966 #undef LSM_HOOK 967 968 /* 969 * Hook list operation macros. 970 * 971 * call_void_hook: 972 * This is a hook that does not return a value. 973 * 974 * call_int_hook: 975 * This is a hook that returns a value. 976 */ 977 #define __CALL_STATIC_VOID(NUM, HOOK, ...) \ 978 do { \ 979 if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \ 980 static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \ 981 } \ 982 } while (0); 983 984 #define call_void_hook(HOOK, ...) \ 985 do { \ 986 LSM_LOOP_UNROLL(__CALL_STATIC_VOID, HOOK, __VA_ARGS__); \ 987 } while (0) 988 989 990 #define __CALL_STATIC_INT(NUM, R, HOOK, LABEL, ...) \ 991 do { \ 992 if (static_branch_unlikely(&SECURITY_HOOK_ACTIVE_KEY(HOOK, NUM))) { \ 993 R = static_call(LSM_STATIC_CALL(HOOK, NUM))(__VA_ARGS__); \ 994 if (R != LSM_RET_DEFAULT(HOOK)) \ 995 goto LABEL; \ 996 } \ 997 } while (0); 998 999 #define call_int_hook(HOOK, ...) \ 1000 ({ \ 1001 __label__ OUT; \ 1002 int RC = LSM_RET_DEFAULT(HOOK); \ 1003 \ 1004 LSM_LOOP_UNROLL(__CALL_STATIC_INT, RC, HOOK, OUT, __VA_ARGS__); \ 1005 OUT: \ 1006 RC; \ 1007 }) 1008 1009 #define lsm_for_each_hook(scall, NAME) \ 1010 for (scall = static_calls_table.NAME; \ 1011 scall - static_calls_table.NAME < MAX_LSM_COUNT; scall++) \ 1012 if (static_key_enabled(&scall->active->key)) 1013 1014 /* Security operations */ 1015 1016 /** 1017 * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok 1018 * @mgr: task credentials of current binder process 1019 * 1020 * Check whether @mgr is allowed to be the binder context manager. 1021 * 1022 * Return: Return 0 if permission is granted. 1023 */ 1024 int security_binder_set_context_mgr(const struct cred *mgr) 1025 { 1026 return call_int_hook(binder_set_context_mgr, mgr); 1027 } 1028 1029 /** 1030 * security_binder_transaction() - Check if a binder transaction is allowed 1031 * @from: sending process 1032 * @to: receiving process 1033 * 1034 * Check whether @from is allowed to invoke a binder transaction call to @to. 1035 * 1036 * Return: Returns 0 if permission is granted. 1037 */ 1038 int security_binder_transaction(const struct cred *from, 1039 const struct cred *to) 1040 { 1041 return call_int_hook(binder_transaction, from, to); 1042 } 1043 1044 /** 1045 * security_binder_transfer_binder() - Check if a binder transfer is allowed 1046 * @from: sending process 1047 * @to: receiving process 1048 * 1049 * Check whether @from is allowed to transfer a binder reference to @to. 1050 * 1051 * Return: Returns 0 if permission is granted. 1052 */ 1053 int security_binder_transfer_binder(const struct cred *from, 1054 const struct cred *to) 1055 { 1056 return call_int_hook(binder_transfer_binder, from, to); 1057 } 1058 1059 /** 1060 * security_binder_transfer_file() - Check if a binder file xfer is allowed 1061 * @from: sending process 1062 * @to: receiving process 1063 * @file: file being transferred 1064 * 1065 * Check whether @from is allowed to transfer @file to @to. 1066 * 1067 * Return: Returns 0 if permission is granted. 1068 */ 1069 int security_binder_transfer_file(const struct cred *from, 1070 const struct cred *to, const struct file *file) 1071 { 1072 return call_int_hook(binder_transfer_file, from, to, file); 1073 } 1074 1075 /** 1076 * security_ptrace_access_check() - Check if tracing is allowed 1077 * @child: target process 1078 * @mode: PTRACE_MODE flags 1079 * 1080 * Check permission before allowing the current process to trace the @child 1081 * process. Security modules may also want to perform a process tracing check 1082 * during an execve in the set_security or apply_creds hooks of tracing check 1083 * during an execve in the bprm_set_creds hook of binprm_security_ops if the 1084 * process is being traced and its security attributes would be changed by the 1085 * execve. 1086 * 1087 * Return: Returns 0 if permission is granted. 1088 */ 1089 int security_ptrace_access_check(struct task_struct *child, unsigned int mode) 1090 { 1091 return call_int_hook(ptrace_access_check, child, mode); 1092 } 1093 1094 /** 1095 * security_ptrace_traceme() - Check if tracing is allowed 1096 * @parent: tracing process 1097 * 1098 * Check that the @parent process has sufficient permission to trace the 1099 * current process before allowing the current process to present itself to the 1100 * @parent process for tracing. 1101 * 1102 * Return: Returns 0 if permission is granted. 1103 */ 1104 int security_ptrace_traceme(struct task_struct *parent) 1105 { 1106 return call_int_hook(ptrace_traceme, parent); 1107 } 1108 1109 /** 1110 * security_capget() - Get the capability sets for a process 1111 * @target: target process 1112 * @effective: effective capability set 1113 * @inheritable: inheritable capability set 1114 * @permitted: permitted capability set 1115 * 1116 * Get the @effective, @inheritable, and @permitted capability sets for the 1117 * @target process. The hook may also perform permission checking to determine 1118 * if the current process is allowed to see the capability sets of the @target 1119 * process. 1120 * 1121 * Return: Returns 0 if the capability sets were successfully obtained. 1122 */ 1123 int security_capget(const struct task_struct *target, 1124 kernel_cap_t *effective, 1125 kernel_cap_t *inheritable, 1126 kernel_cap_t *permitted) 1127 { 1128 return call_int_hook(capget, target, effective, inheritable, permitted); 1129 } 1130 1131 /** 1132 * security_capset() - Set the capability sets for a process 1133 * @new: new credentials for the target process 1134 * @old: current credentials of the target process 1135 * @effective: effective capability set 1136 * @inheritable: inheritable capability set 1137 * @permitted: permitted capability set 1138 * 1139 * Set the @effective, @inheritable, and @permitted capability sets for the 1140 * current process. 1141 * 1142 * Return: Returns 0 and update @new if permission is granted. 1143 */ 1144 int security_capset(struct cred *new, const struct cred *old, 1145 const kernel_cap_t *effective, 1146 const kernel_cap_t *inheritable, 1147 const kernel_cap_t *permitted) 1148 { 1149 return call_int_hook(capset, new, old, effective, inheritable, 1150 permitted); 1151 } 1152 1153 /** 1154 * security_capable() - Check if a process has the necessary capability 1155 * @cred: credentials to examine 1156 * @ns: user namespace 1157 * @cap: capability requested 1158 * @opts: capability check options 1159 * 1160 * Check whether the @tsk process has the @cap capability in the indicated 1161 * credentials. @cap contains the capability <include/linux/capability.h>. 1162 * @opts contains options for the capable check <include/linux/security.h>. 1163 * 1164 * Return: Returns 0 if the capability is granted. 1165 */ 1166 int security_capable(const struct cred *cred, 1167 struct user_namespace *ns, 1168 int cap, 1169 unsigned int opts) 1170 { 1171 return call_int_hook(capable, cred, ns, cap, opts); 1172 } 1173 1174 /** 1175 * security_quotactl() - Check if a quotactl() syscall is allowed for this fs 1176 * @cmds: commands 1177 * @type: type 1178 * @id: id 1179 * @sb: filesystem 1180 * 1181 * Check whether the quotactl syscall is allowed for this @sb. 1182 * 1183 * Return: Returns 0 if permission is granted. 1184 */ 1185 int security_quotactl(int cmds, int type, int id, const struct super_block *sb) 1186 { 1187 return call_int_hook(quotactl, cmds, type, id, sb); 1188 } 1189 1190 /** 1191 * security_quota_on() - Check if QUOTAON is allowed for a dentry 1192 * @dentry: dentry 1193 * 1194 * Check whether QUOTAON is allowed for @dentry. 1195 * 1196 * Return: Returns 0 if permission is granted. 1197 */ 1198 int security_quota_on(struct dentry *dentry) 1199 { 1200 return call_int_hook(quota_on, dentry); 1201 } 1202 1203 /** 1204 * security_syslog() - Check if accessing the kernel message ring is allowed 1205 * @type: SYSLOG_ACTION_* type 1206 * 1207 * Check permission before accessing the kernel message ring or changing 1208 * logging to the console. See the syslog(2) manual page for an explanation of 1209 * the @type values. 1210 * 1211 * Return: Return 0 if permission is granted. 1212 */ 1213 int security_syslog(int type) 1214 { 1215 return call_int_hook(syslog, type); 1216 } 1217 1218 /** 1219 * security_settime64() - Check if changing the system time is allowed 1220 * @ts: new time 1221 * @tz: timezone 1222 * 1223 * Check permission to change the system time, struct timespec64 is defined in 1224 * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>. 1225 * 1226 * Return: Returns 0 if permission is granted. 1227 */ 1228 int security_settime64(const struct timespec64 *ts, const struct timezone *tz) 1229 { 1230 return call_int_hook(settime, ts, tz); 1231 } 1232 1233 /** 1234 * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed 1235 * @mm: mm struct 1236 * @pages: number of pages 1237 * 1238 * Check permissions for allocating a new virtual mapping. If all LSMs return 1239 * a positive value, __vm_enough_memory() will be called with cap_sys_admin 1240 * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be 1241 * called with cap_sys_admin cleared. 1242 * 1243 * Return: Returns 0 if permission is granted by the LSM infrastructure to the 1244 * caller. 1245 */ 1246 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages) 1247 { 1248 struct lsm_static_call *scall; 1249 int cap_sys_admin = 1; 1250 int rc; 1251 1252 /* 1253 * The module will respond with 0 if it thinks the __vm_enough_memory() 1254 * call should be made with the cap_sys_admin set. If all of the modules 1255 * agree that it should be set it will. If any module thinks it should 1256 * not be set it won't. 1257 */ 1258 lsm_for_each_hook(scall, vm_enough_memory) { 1259 rc = scall->hl->hook.vm_enough_memory(mm, pages); 1260 if (rc < 0) { 1261 cap_sys_admin = 0; 1262 break; 1263 } 1264 } 1265 return __vm_enough_memory(mm, pages, cap_sys_admin); 1266 } 1267 1268 /** 1269 * security_bprm_creds_for_exec() - Prepare the credentials for exec() 1270 * @bprm: binary program information 1271 * 1272 * If the setup in prepare_exec_creds did not setup @bprm->cred->security 1273 * properly for executing @bprm->file, update the LSM's portion of 1274 * @bprm->cred->security to be what commit_creds needs to install for the new 1275 * program. This hook may also optionally check permissions (e.g. for 1276 * transitions between security domains). The hook must set @bprm->secureexec 1277 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm 1278 * contains the linux_binprm structure. 1279 * 1280 * Return: Returns 0 if the hook is successful and permission is granted. 1281 */ 1282 int security_bprm_creds_for_exec(struct linux_binprm *bprm) 1283 { 1284 return call_int_hook(bprm_creds_for_exec, bprm); 1285 } 1286 1287 /** 1288 * security_bprm_creds_from_file() - Update linux_binprm creds based on file 1289 * @bprm: binary program information 1290 * @file: associated file 1291 * 1292 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon 1293 * exec, update @bprm->cred to reflect that change. This is called after 1294 * finding the binary that will be executed without an interpreter. This 1295 * ensures that the credentials will not be derived from a script that the 1296 * binary will need to reopen, which when reopend may end up being a completely 1297 * different file. This hook may also optionally check permissions (e.g. for 1298 * transitions between security domains). The hook must set @bprm->secureexec 1299 * to 1 if AT_SECURE should be set to request libc enable secure mode. The 1300 * hook must add to @bprm->per_clear any personality flags that should be 1301 * cleared from current->personality. @bprm contains the linux_binprm 1302 * structure. 1303 * 1304 * Return: Returns 0 if the hook is successful and permission is granted. 1305 */ 1306 int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file) 1307 { 1308 return call_int_hook(bprm_creds_from_file, bprm, file); 1309 } 1310 1311 /** 1312 * security_bprm_check() - Mediate binary handler search 1313 * @bprm: binary program information 1314 * 1315 * This hook mediates the point when a search for a binary handler will begin. 1316 * It allows a check against the @bprm->cred->security value which was set in 1317 * the preceding creds_for_exec call. The argv list and envp list are reliably 1318 * available in @bprm. This hook may be called multiple times during a single 1319 * execve. @bprm contains the linux_binprm structure. 1320 * 1321 * Return: Returns 0 if the hook is successful and permission is granted. 1322 */ 1323 int security_bprm_check(struct linux_binprm *bprm) 1324 { 1325 return call_int_hook(bprm_check_security, bprm); 1326 } 1327 1328 /** 1329 * security_bprm_committing_creds() - Install creds for a process during exec() 1330 * @bprm: binary program information 1331 * 1332 * Prepare to install the new security attributes of a process being 1333 * transformed by an execve operation, based on the old credentials pointed to 1334 * by @current->cred and the information set in @bprm->cred by the 1335 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This 1336 * hook is a good place to perform state changes on the process such as closing 1337 * open file descriptors to which access will no longer be granted when the 1338 * attributes are changed. This is called immediately before commit_creds(). 1339 */ 1340 void security_bprm_committing_creds(const struct linux_binprm *bprm) 1341 { 1342 call_void_hook(bprm_committing_creds, bprm); 1343 } 1344 1345 /** 1346 * security_bprm_committed_creds() - Tidy up after cred install during exec() 1347 * @bprm: binary program information 1348 * 1349 * Tidy up after the installation of the new security attributes of a process 1350 * being transformed by an execve operation. The new credentials have, by this 1351 * point, been set to @current->cred. @bprm points to the linux_binprm 1352 * structure. This hook is a good place to perform state changes on the 1353 * process such as clearing out non-inheritable signal state. This is called 1354 * immediately after commit_creds(). 1355 */ 1356 void security_bprm_committed_creds(const struct linux_binprm *bprm) 1357 { 1358 call_void_hook(bprm_committed_creds, bprm); 1359 } 1360 1361 /** 1362 * security_fs_context_submount() - Initialise fc->security 1363 * @fc: new filesystem context 1364 * @reference: dentry reference for submount/remount 1365 * 1366 * Fill out the ->security field for a new fs_context. 1367 * 1368 * Return: Returns 0 on success or negative error code on failure. 1369 */ 1370 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference) 1371 { 1372 return call_int_hook(fs_context_submount, fc, reference); 1373 } 1374 1375 /** 1376 * security_fs_context_dup() - Duplicate a fs_context LSM blob 1377 * @fc: destination filesystem context 1378 * @src_fc: source filesystem context 1379 * 1380 * Allocate and attach a security structure to sc->security. This pointer is 1381 * initialised to NULL by the caller. @fc indicates the new filesystem context. 1382 * @src_fc indicates the original filesystem context. 1383 * 1384 * Return: Returns 0 on success or a negative error code on failure. 1385 */ 1386 int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) 1387 { 1388 return call_int_hook(fs_context_dup, fc, src_fc); 1389 } 1390 1391 /** 1392 * security_fs_context_parse_param() - Configure a filesystem context 1393 * @fc: filesystem context 1394 * @param: filesystem parameter 1395 * 1396 * Userspace provided a parameter to configure a superblock. The LSM can 1397 * consume the parameter or return it to the caller for use elsewhere. 1398 * 1399 * Return: If the parameter is used by the LSM it should return 0, if it is 1400 * returned to the caller -ENOPARAM is returned, otherwise a negative 1401 * error code is returned. 1402 */ 1403 int security_fs_context_parse_param(struct fs_context *fc, 1404 struct fs_parameter *param) 1405 { 1406 struct lsm_static_call *scall; 1407 int trc; 1408 int rc = -ENOPARAM; 1409 1410 lsm_for_each_hook(scall, fs_context_parse_param) { 1411 trc = scall->hl->hook.fs_context_parse_param(fc, param); 1412 if (trc == 0) 1413 rc = 0; 1414 else if (trc != -ENOPARAM) 1415 return trc; 1416 } 1417 return rc; 1418 } 1419 1420 /** 1421 * security_sb_alloc() - Allocate a super_block LSM blob 1422 * @sb: filesystem superblock 1423 * 1424 * Allocate and attach a security structure to the sb->s_security field. The 1425 * s_security field is initialized to NULL when the structure is allocated. 1426 * @sb contains the super_block structure to be modified. 1427 * 1428 * Return: Returns 0 if operation was successful. 1429 */ 1430 int security_sb_alloc(struct super_block *sb) 1431 { 1432 int rc = lsm_superblock_alloc(sb); 1433 1434 if (unlikely(rc)) 1435 return rc; 1436 rc = call_int_hook(sb_alloc_security, sb); 1437 if (unlikely(rc)) 1438 security_sb_free(sb); 1439 return rc; 1440 } 1441 1442 /** 1443 * security_sb_delete() - Release super_block LSM associated objects 1444 * @sb: filesystem superblock 1445 * 1446 * Release objects tied to a superblock (e.g. inodes). @sb contains the 1447 * super_block structure being released. 1448 */ 1449 void security_sb_delete(struct super_block *sb) 1450 { 1451 call_void_hook(sb_delete, sb); 1452 } 1453 1454 /** 1455 * security_sb_free() - Free a super_block LSM blob 1456 * @sb: filesystem superblock 1457 * 1458 * Deallocate and clear the sb->s_security field. @sb contains the super_block 1459 * structure to be modified. 1460 */ 1461 void security_sb_free(struct super_block *sb) 1462 { 1463 call_void_hook(sb_free_security, sb); 1464 kfree(sb->s_security); 1465 sb->s_security = NULL; 1466 } 1467 1468 /** 1469 * security_free_mnt_opts() - Free memory associated with mount options 1470 * @mnt_opts: LSM processed mount options 1471 * 1472 * Free memory associated with @mnt_ops. 1473 */ 1474 void security_free_mnt_opts(void **mnt_opts) 1475 { 1476 if (!*mnt_opts) 1477 return; 1478 call_void_hook(sb_free_mnt_opts, *mnt_opts); 1479 *mnt_opts = NULL; 1480 } 1481 EXPORT_SYMBOL(security_free_mnt_opts); 1482 1483 /** 1484 * security_sb_eat_lsm_opts() - Consume LSM mount options 1485 * @options: mount options 1486 * @mnt_opts: LSM processed mount options 1487 * 1488 * Eat (scan @options) and save them in @mnt_opts. 1489 * 1490 * Return: Returns 0 on success, negative values on failure. 1491 */ 1492 int security_sb_eat_lsm_opts(char *options, void **mnt_opts) 1493 { 1494 return call_int_hook(sb_eat_lsm_opts, options, mnt_opts); 1495 } 1496 EXPORT_SYMBOL(security_sb_eat_lsm_opts); 1497 1498 /** 1499 * security_sb_mnt_opts_compat() - Check if new mount options are allowed 1500 * @sb: filesystem superblock 1501 * @mnt_opts: new mount options 1502 * 1503 * Determine if the new mount options in @mnt_opts are allowed given the 1504 * existing mounted filesystem at @sb. @sb superblock being compared. 1505 * 1506 * Return: Returns 0 if options are compatible. 1507 */ 1508 int security_sb_mnt_opts_compat(struct super_block *sb, 1509 void *mnt_opts) 1510 { 1511 return call_int_hook(sb_mnt_opts_compat, sb, mnt_opts); 1512 } 1513 EXPORT_SYMBOL(security_sb_mnt_opts_compat); 1514 1515 /** 1516 * security_sb_remount() - Verify no incompatible mount changes during remount 1517 * @sb: filesystem superblock 1518 * @mnt_opts: (re)mount options 1519 * 1520 * Extracts security system specific mount options and verifies no changes are 1521 * being made to those options. 1522 * 1523 * Return: Returns 0 if permission is granted. 1524 */ 1525 int security_sb_remount(struct super_block *sb, 1526 void *mnt_opts) 1527 { 1528 return call_int_hook(sb_remount, sb, mnt_opts); 1529 } 1530 EXPORT_SYMBOL(security_sb_remount); 1531 1532 /** 1533 * security_sb_kern_mount() - Check if a kernel mount is allowed 1534 * @sb: filesystem superblock 1535 * 1536 * Mount this @sb if allowed by permissions. 1537 * 1538 * Return: Returns 0 if permission is granted. 1539 */ 1540 int security_sb_kern_mount(const struct super_block *sb) 1541 { 1542 return call_int_hook(sb_kern_mount, sb); 1543 } 1544 1545 /** 1546 * security_sb_show_options() - Output the mount options for a superblock 1547 * @m: output file 1548 * @sb: filesystem superblock 1549 * 1550 * Show (print on @m) mount options for this @sb. 1551 * 1552 * Return: Returns 0 on success, negative values on failure. 1553 */ 1554 int security_sb_show_options(struct seq_file *m, struct super_block *sb) 1555 { 1556 return call_int_hook(sb_show_options, m, sb); 1557 } 1558 1559 /** 1560 * security_sb_statfs() - Check if accessing fs stats is allowed 1561 * @dentry: superblock handle 1562 * 1563 * Check permission before obtaining filesystem statistics for the @mnt 1564 * mountpoint. @dentry is a handle on the superblock for the filesystem. 1565 * 1566 * Return: Returns 0 if permission is granted. 1567 */ 1568 int security_sb_statfs(struct dentry *dentry) 1569 { 1570 return call_int_hook(sb_statfs, dentry); 1571 } 1572 1573 /** 1574 * security_sb_mount() - Check permission for mounting a filesystem 1575 * @dev_name: filesystem backing device 1576 * @path: mount point 1577 * @type: filesystem type 1578 * @flags: mount flags 1579 * @data: filesystem specific data 1580 * 1581 * Check permission before an object specified by @dev_name is mounted on the 1582 * mount point named by @nd. For an ordinary mount, @dev_name identifies a 1583 * device if the file system type requires a device. For a remount 1584 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount 1585 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being 1586 * mounted. 1587 * 1588 * Return: Returns 0 if permission is granted. 1589 */ 1590 int security_sb_mount(const char *dev_name, const struct path *path, 1591 const char *type, unsigned long flags, void *data) 1592 { 1593 return call_int_hook(sb_mount, dev_name, path, type, flags, data); 1594 } 1595 1596 /** 1597 * security_sb_umount() - Check permission for unmounting a filesystem 1598 * @mnt: mounted filesystem 1599 * @flags: unmount flags 1600 * 1601 * Check permission before the @mnt file system is unmounted. 1602 * 1603 * Return: Returns 0 if permission is granted. 1604 */ 1605 int security_sb_umount(struct vfsmount *mnt, int flags) 1606 { 1607 return call_int_hook(sb_umount, mnt, flags); 1608 } 1609 1610 /** 1611 * security_sb_pivotroot() - Check permissions for pivoting the rootfs 1612 * @old_path: new location for current rootfs 1613 * @new_path: location of the new rootfs 1614 * 1615 * Check permission before pivoting the root filesystem. 1616 * 1617 * Return: Returns 0 if permission is granted. 1618 */ 1619 int security_sb_pivotroot(const struct path *old_path, 1620 const struct path *new_path) 1621 { 1622 return call_int_hook(sb_pivotroot, old_path, new_path); 1623 } 1624 1625 /** 1626 * security_sb_set_mnt_opts() - Set the mount options for a filesystem 1627 * @sb: filesystem superblock 1628 * @mnt_opts: binary mount options 1629 * @kern_flags: kernel flags (in) 1630 * @set_kern_flags: kernel flags (out) 1631 * 1632 * Set the security relevant mount options used for a superblock. 1633 * 1634 * Return: Returns 0 on success, error on failure. 1635 */ 1636 int security_sb_set_mnt_opts(struct super_block *sb, 1637 void *mnt_opts, 1638 unsigned long kern_flags, 1639 unsigned long *set_kern_flags) 1640 { 1641 struct lsm_static_call *scall; 1642 int rc = mnt_opts ? -EOPNOTSUPP : LSM_RET_DEFAULT(sb_set_mnt_opts); 1643 1644 lsm_for_each_hook(scall, sb_set_mnt_opts) { 1645 rc = scall->hl->hook.sb_set_mnt_opts(sb, mnt_opts, kern_flags, 1646 set_kern_flags); 1647 if (rc != LSM_RET_DEFAULT(sb_set_mnt_opts)) 1648 break; 1649 } 1650 return rc; 1651 } 1652 EXPORT_SYMBOL(security_sb_set_mnt_opts); 1653 1654 /** 1655 * security_sb_clone_mnt_opts() - Duplicate superblock mount options 1656 * @oldsb: source superblock 1657 * @newsb: destination superblock 1658 * @kern_flags: kernel flags (in) 1659 * @set_kern_flags: kernel flags (out) 1660 * 1661 * Copy all security options from a given superblock to another. 1662 * 1663 * Return: Returns 0 on success, error on failure. 1664 */ 1665 int security_sb_clone_mnt_opts(const struct super_block *oldsb, 1666 struct super_block *newsb, 1667 unsigned long kern_flags, 1668 unsigned long *set_kern_flags) 1669 { 1670 return call_int_hook(sb_clone_mnt_opts, oldsb, newsb, 1671 kern_flags, set_kern_flags); 1672 } 1673 EXPORT_SYMBOL(security_sb_clone_mnt_opts); 1674 1675 /** 1676 * security_move_mount() - Check permissions for moving a mount 1677 * @from_path: source mount point 1678 * @to_path: destination mount point 1679 * 1680 * Check permission before a mount is moved. 1681 * 1682 * Return: Returns 0 if permission is granted. 1683 */ 1684 int security_move_mount(const struct path *from_path, 1685 const struct path *to_path) 1686 { 1687 return call_int_hook(move_mount, from_path, to_path); 1688 } 1689 1690 /** 1691 * security_path_notify() - Check if setting a watch is allowed 1692 * @path: file path 1693 * @mask: event mask 1694 * @obj_type: file path type 1695 * 1696 * Check permissions before setting a watch on events as defined by @mask, on 1697 * an object at @path, whose type is defined by @obj_type. 1698 * 1699 * Return: Returns 0 if permission is granted. 1700 */ 1701 int security_path_notify(const struct path *path, u64 mask, 1702 unsigned int obj_type) 1703 { 1704 return call_int_hook(path_notify, path, mask, obj_type); 1705 } 1706 1707 /** 1708 * security_inode_alloc() - Allocate an inode LSM blob 1709 * @inode: the inode 1710 * 1711 * Allocate and attach a security structure to @inode->i_security. The 1712 * i_security field is initialized to NULL when the inode structure is 1713 * allocated. 1714 * 1715 * Return: Return 0 if operation was successful. 1716 */ 1717 int security_inode_alloc(struct inode *inode) 1718 { 1719 int rc = lsm_inode_alloc(inode); 1720 1721 if (unlikely(rc)) 1722 return rc; 1723 rc = call_int_hook(inode_alloc_security, inode); 1724 if (unlikely(rc)) 1725 security_inode_free(inode); 1726 return rc; 1727 } 1728 1729 static void inode_free_by_rcu(struct rcu_head *head) 1730 { 1731 /* The rcu head is at the start of the inode blob */ 1732 call_void_hook(inode_free_security_rcu, head); 1733 kmem_cache_free(lsm_inode_cache, head); 1734 } 1735 1736 /** 1737 * security_inode_free() - Free an inode's LSM blob 1738 * @inode: the inode 1739 * 1740 * Release any LSM resources associated with @inode, although due to the 1741 * inode's RCU protections it is possible that the resources will not be 1742 * fully released until after the current RCU grace period has elapsed. 1743 * 1744 * It is important for LSMs to note that despite being present in a call to 1745 * security_inode_free(), @inode may still be referenced in a VFS path walk 1746 * and calls to security_inode_permission() may be made during, or after, 1747 * a call to security_inode_free(). For this reason the inode->i_security 1748 * field is released via a call_rcu() callback and any LSMs which need to 1749 * retain inode state for use in security_inode_permission() should only 1750 * release that state in the inode_free_security_rcu() LSM hook callback. 1751 */ 1752 void security_inode_free(struct inode *inode) 1753 { 1754 call_void_hook(inode_free_security, inode); 1755 if (!inode->i_security) 1756 return; 1757 call_rcu((struct rcu_head *)inode->i_security, inode_free_by_rcu); 1758 } 1759 1760 /** 1761 * security_dentry_init_security() - Perform dentry initialization 1762 * @dentry: the dentry to initialize 1763 * @mode: mode used to determine resource type 1764 * @name: name of the last path component 1765 * @xattr_name: name of the security/LSM xattr 1766 * @ctx: pointer to the resulting LSM context 1767 * @ctxlen: length of @ctx 1768 * 1769 * Compute a context for a dentry as the inode is not yet available since NFSv4 1770 * has no label backed by an EA anyway. It is important to note that 1771 * @xattr_name does not need to be free'd by the caller, it is a static string. 1772 * 1773 * Return: Returns 0 on success, negative values on failure. 1774 */ 1775 int security_dentry_init_security(struct dentry *dentry, int mode, 1776 const struct qstr *name, 1777 const char **xattr_name, void **ctx, 1778 u32 *ctxlen) 1779 { 1780 return call_int_hook(dentry_init_security, dentry, mode, name, 1781 xattr_name, ctx, ctxlen); 1782 } 1783 EXPORT_SYMBOL(security_dentry_init_security); 1784 1785 /** 1786 * security_dentry_create_files_as() - Perform dentry initialization 1787 * @dentry: the dentry to initialize 1788 * @mode: mode used to determine resource type 1789 * @name: name of the last path component 1790 * @old: creds to use for LSM context calculations 1791 * @new: creds to modify 1792 * 1793 * Compute a context for a dentry as the inode is not yet available and set 1794 * that context in passed in creds so that new files are created using that 1795 * context. Context is calculated using the passed in creds and not the creds 1796 * of the caller. 1797 * 1798 * Return: Returns 0 on success, error on failure. 1799 */ 1800 int security_dentry_create_files_as(struct dentry *dentry, int mode, 1801 struct qstr *name, 1802 const struct cred *old, struct cred *new) 1803 { 1804 return call_int_hook(dentry_create_files_as, dentry, mode, 1805 name, old, new); 1806 } 1807 EXPORT_SYMBOL(security_dentry_create_files_as); 1808 1809 /** 1810 * security_inode_init_security() - Initialize an inode's LSM context 1811 * @inode: the inode 1812 * @dir: parent directory 1813 * @qstr: last component of the pathname 1814 * @initxattrs: callback function to write xattrs 1815 * @fs_data: filesystem specific data 1816 * 1817 * Obtain the security attribute name suffix and value to set on a newly 1818 * created inode and set up the incore security field for the new inode. This 1819 * hook is called by the fs code as part of the inode creation transaction and 1820 * provides for atomic labeling of the inode, unlike the post_create/mkdir/... 1821 * hooks called by the VFS. 1822 * 1823 * The hook function is expected to populate the xattrs array, by calling 1824 * lsm_get_xattr_slot() to retrieve the slots reserved by the security module 1825 * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each 1826 * slot, the hook function should set ->name to the attribute name suffix 1827 * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it 1828 * to the attribute value, to set ->value_len to the length of the value. If 1829 * the security module does not use security attributes or does not wish to put 1830 * a security attribute on this particular inode, then it should return 1831 * -EOPNOTSUPP to skip this processing. 1832 * 1833 * Return: Returns 0 if the LSM successfully initialized all of the inode 1834 * security attributes that are required, negative values otherwise. 1835 */ 1836 int security_inode_init_security(struct inode *inode, struct inode *dir, 1837 const struct qstr *qstr, 1838 const initxattrs initxattrs, void *fs_data) 1839 { 1840 struct lsm_static_call *scall; 1841 struct xattr *new_xattrs = NULL; 1842 int ret = -EOPNOTSUPP, xattr_count = 0; 1843 1844 if (unlikely(IS_PRIVATE(inode))) 1845 return 0; 1846 1847 if (!blob_sizes.lbs_xattr_count) 1848 return 0; 1849 1850 if (initxattrs) { 1851 /* Allocate +1 as terminator. */ 1852 new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 1, 1853 sizeof(*new_xattrs), GFP_NOFS); 1854 if (!new_xattrs) 1855 return -ENOMEM; 1856 } 1857 1858 lsm_for_each_hook(scall, inode_init_security) { 1859 ret = scall->hl->hook.inode_init_security(inode, dir, qstr, new_xattrs, 1860 &xattr_count); 1861 if (ret && ret != -EOPNOTSUPP) 1862 goto out; 1863 /* 1864 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context 1865 * means that the LSM is not willing to provide an xattr, not 1866 * that it wants to signal an error. Thus, continue to invoke 1867 * the remaining LSMs. 1868 */ 1869 } 1870 1871 /* If initxattrs() is NULL, xattr_count is zero, skip the call. */ 1872 if (!xattr_count) 1873 goto out; 1874 1875 ret = initxattrs(inode, new_xattrs, fs_data); 1876 out: 1877 for (; xattr_count > 0; xattr_count--) 1878 kfree(new_xattrs[xattr_count - 1].value); 1879 kfree(new_xattrs); 1880 return (ret == -EOPNOTSUPP) ? 0 : ret; 1881 } 1882 EXPORT_SYMBOL(security_inode_init_security); 1883 1884 /** 1885 * security_inode_init_security_anon() - Initialize an anonymous inode 1886 * @inode: the inode 1887 * @name: the anonymous inode class 1888 * @context_inode: an optional related inode 1889 * 1890 * Set up the incore security field for the new anonymous inode and return 1891 * whether the inode creation is permitted by the security module or not. 1892 * 1893 * Return: Returns 0 on success, -EACCES if the security module denies the 1894 * creation of this inode, or another -errno upon other errors. 1895 */ 1896 int security_inode_init_security_anon(struct inode *inode, 1897 const struct qstr *name, 1898 const struct inode *context_inode) 1899 { 1900 return call_int_hook(inode_init_security_anon, inode, name, 1901 context_inode); 1902 } 1903 1904 #ifdef CONFIG_SECURITY_PATH 1905 /** 1906 * security_path_mknod() - Check if creating a special file is allowed 1907 * @dir: parent directory 1908 * @dentry: new file 1909 * @mode: new file mode 1910 * @dev: device number 1911 * 1912 * Check permissions when creating a file. Note that this hook is called even 1913 * if mknod operation is being done for a regular file. 1914 * 1915 * Return: Returns 0 if permission is granted. 1916 */ 1917 int security_path_mknod(const struct path *dir, struct dentry *dentry, 1918 umode_t mode, unsigned int dev) 1919 { 1920 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) 1921 return 0; 1922 return call_int_hook(path_mknod, dir, dentry, mode, dev); 1923 } 1924 EXPORT_SYMBOL(security_path_mknod); 1925 1926 /** 1927 * security_path_post_mknod() - Update inode security after reg file creation 1928 * @idmap: idmap of the mount 1929 * @dentry: new file 1930 * 1931 * Update inode security field after a regular file has been created. 1932 */ 1933 void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry) 1934 { 1935 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 1936 return; 1937 call_void_hook(path_post_mknod, idmap, dentry); 1938 } 1939 1940 /** 1941 * security_path_mkdir() - Check if creating a new directory is allowed 1942 * @dir: parent directory 1943 * @dentry: new directory 1944 * @mode: new directory mode 1945 * 1946 * Check permissions to create a new directory in the existing directory. 1947 * 1948 * Return: Returns 0 if permission is granted. 1949 */ 1950 int security_path_mkdir(const struct path *dir, struct dentry *dentry, 1951 umode_t mode) 1952 { 1953 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) 1954 return 0; 1955 return call_int_hook(path_mkdir, dir, dentry, mode); 1956 } 1957 EXPORT_SYMBOL(security_path_mkdir); 1958 1959 /** 1960 * security_path_rmdir() - Check if removing a directory is allowed 1961 * @dir: parent directory 1962 * @dentry: directory to remove 1963 * 1964 * Check the permission to remove a directory. 1965 * 1966 * Return: Returns 0 if permission is granted. 1967 */ 1968 int security_path_rmdir(const struct path *dir, struct dentry *dentry) 1969 { 1970 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) 1971 return 0; 1972 return call_int_hook(path_rmdir, dir, dentry); 1973 } 1974 1975 /** 1976 * security_path_unlink() - Check if removing a hard link is allowed 1977 * @dir: parent directory 1978 * @dentry: file 1979 * 1980 * Check the permission to remove a hard link to a file. 1981 * 1982 * Return: Returns 0 if permission is granted. 1983 */ 1984 int security_path_unlink(const struct path *dir, struct dentry *dentry) 1985 { 1986 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) 1987 return 0; 1988 return call_int_hook(path_unlink, dir, dentry); 1989 } 1990 EXPORT_SYMBOL(security_path_unlink); 1991 1992 /** 1993 * security_path_symlink() - Check if creating a symbolic link is allowed 1994 * @dir: parent directory 1995 * @dentry: symbolic link 1996 * @old_name: file pathname 1997 * 1998 * Check the permission to create a symbolic link to a file. 1999 * 2000 * Return: Returns 0 if permission is granted. 2001 */ 2002 int security_path_symlink(const struct path *dir, struct dentry *dentry, 2003 const char *old_name) 2004 { 2005 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) 2006 return 0; 2007 return call_int_hook(path_symlink, dir, dentry, old_name); 2008 } 2009 2010 /** 2011 * security_path_link - Check if creating a hard link is allowed 2012 * @old_dentry: existing file 2013 * @new_dir: new parent directory 2014 * @new_dentry: new link 2015 * 2016 * Check permission before creating a new hard link to a file. 2017 * 2018 * Return: Returns 0 if permission is granted. 2019 */ 2020 int security_path_link(struct dentry *old_dentry, const struct path *new_dir, 2021 struct dentry *new_dentry) 2022 { 2023 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)))) 2024 return 0; 2025 return call_int_hook(path_link, old_dentry, new_dir, new_dentry); 2026 } 2027 2028 /** 2029 * security_path_rename() - Check if renaming a file is allowed 2030 * @old_dir: parent directory of the old file 2031 * @old_dentry: the old file 2032 * @new_dir: parent directory of the new file 2033 * @new_dentry: the new file 2034 * @flags: flags 2035 * 2036 * Check for permission to rename a file or directory. 2037 * 2038 * Return: Returns 0 if permission is granted. 2039 */ 2040 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry, 2041 const struct path *new_dir, struct dentry *new_dentry, 2042 unsigned int flags) 2043 { 2044 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) || 2045 (d_is_positive(new_dentry) && 2046 IS_PRIVATE(d_backing_inode(new_dentry))))) 2047 return 0; 2048 2049 return call_int_hook(path_rename, old_dir, old_dentry, new_dir, 2050 new_dentry, flags); 2051 } 2052 EXPORT_SYMBOL(security_path_rename); 2053 2054 /** 2055 * security_path_truncate() - Check if truncating a file is allowed 2056 * @path: file 2057 * 2058 * Check permission before truncating the file indicated by path. Note that 2059 * truncation permissions may also be checked based on already opened files, 2060 * using the security_file_truncate() hook. 2061 * 2062 * Return: Returns 0 if permission is granted. 2063 */ 2064 int security_path_truncate(const struct path *path) 2065 { 2066 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry)))) 2067 return 0; 2068 return call_int_hook(path_truncate, path); 2069 } 2070 2071 /** 2072 * security_path_chmod() - Check if changing the file's mode is allowed 2073 * @path: file 2074 * @mode: new mode 2075 * 2076 * Check for permission to change a mode of the file @path. The new mode is 2077 * specified in @mode which is a bitmask of constants from 2078 * <include/uapi/linux/stat.h>. 2079 * 2080 * Return: Returns 0 if permission is granted. 2081 */ 2082 int security_path_chmod(const struct path *path, umode_t mode) 2083 { 2084 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry)))) 2085 return 0; 2086 return call_int_hook(path_chmod, path, mode); 2087 } 2088 2089 /** 2090 * security_path_chown() - Check if changing the file's owner/group is allowed 2091 * @path: file 2092 * @uid: file owner 2093 * @gid: file group 2094 * 2095 * Check for permission to change owner/group of a file or directory. 2096 * 2097 * Return: Returns 0 if permission is granted. 2098 */ 2099 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid) 2100 { 2101 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry)))) 2102 return 0; 2103 return call_int_hook(path_chown, path, uid, gid); 2104 } 2105 2106 /** 2107 * security_path_chroot() - Check if changing the root directory is allowed 2108 * @path: directory 2109 * 2110 * Check for permission to change root directory. 2111 * 2112 * Return: Returns 0 if permission is granted. 2113 */ 2114 int security_path_chroot(const struct path *path) 2115 { 2116 return call_int_hook(path_chroot, path); 2117 } 2118 #endif /* CONFIG_SECURITY_PATH */ 2119 2120 /** 2121 * security_inode_create() - Check if creating a file is allowed 2122 * @dir: the parent directory 2123 * @dentry: the file being created 2124 * @mode: requested file mode 2125 * 2126 * Check permission to create a regular file. 2127 * 2128 * Return: Returns 0 if permission is granted. 2129 */ 2130 int security_inode_create(struct inode *dir, struct dentry *dentry, 2131 umode_t mode) 2132 { 2133 if (unlikely(IS_PRIVATE(dir))) 2134 return 0; 2135 return call_int_hook(inode_create, dir, dentry, mode); 2136 } 2137 EXPORT_SYMBOL_GPL(security_inode_create); 2138 2139 /** 2140 * security_inode_post_create_tmpfile() - Update inode security of new tmpfile 2141 * @idmap: idmap of the mount 2142 * @inode: inode of the new tmpfile 2143 * 2144 * Update inode security data after a tmpfile has been created. 2145 */ 2146 void security_inode_post_create_tmpfile(struct mnt_idmap *idmap, 2147 struct inode *inode) 2148 { 2149 if (unlikely(IS_PRIVATE(inode))) 2150 return; 2151 call_void_hook(inode_post_create_tmpfile, idmap, inode); 2152 } 2153 2154 /** 2155 * security_inode_link() - Check if creating a hard link is allowed 2156 * @old_dentry: existing file 2157 * @dir: new parent directory 2158 * @new_dentry: new link 2159 * 2160 * Check permission before creating a new hard link to a file. 2161 * 2162 * Return: Returns 0 if permission is granted. 2163 */ 2164 int security_inode_link(struct dentry *old_dentry, struct inode *dir, 2165 struct dentry *new_dentry) 2166 { 2167 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)))) 2168 return 0; 2169 return call_int_hook(inode_link, old_dentry, dir, new_dentry); 2170 } 2171 2172 /** 2173 * security_inode_unlink() - Check if removing a hard link is allowed 2174 * @dir: parent directory 2175 * @dentry: file 2176 * 2177 * Check the permission to remove a hard link to a file. 2178 * 2179 * Return: Returns 0 if permission is granted. 2180 */ 2181 int security_inode_unlink(struct inode *dir, struct dentry *dentry) 2182 { 2183 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2184 return 0; 2185 return call_int_hook(inode_unlink, dir, dentry); 2186 } 2187 2188 /** 2189 * security_inode_symlink() - Check if creating a symbolic link is allowed 2190 * @dir: parent directory 2191 * @dentry: symbolic link 2192 * @old_name: existing filename 2193 * 2194 * Check the permission to create a symbolic link to a file. 2195 * 2196 * Return: Returns 0 if permission is granted. 2197 */ 2198 int security_inode_symlink(struct inode *dir, struct dentry *dentry, 2199 const char *old_name) 2200 { 2201 if (unlikely(IS_PRIVATE(dir))) 2202 return 0; 2203 return call_int_hook(inode_symlink, dir, dentry, old_name); 2204 } 2205 2206 /** 2207 * security_inode_mkdir() - Check if creation a new director is allowed 2208 * @dir: parent directory 2209 * @dentry: new directory 2210 * @mode: new directory mode 2211 * 2212 * Check permissions to create a new directory in the existing directory 2213 * associated with inode structure @dir. 2214 * 2215 * Return: Returns 0 if permission is granted. 2216 */ 2217 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 2218 { 2219 if (unlikely(IS_PRIVATE(dir))) 2220 return 0; 2221 return call_int_hook(inode_mkdir, dir, dentry, mode); 2222 } 2223 EXPORT_SYMBOL_GPL(security_inode_mkdir); 2224 2225 /** 2226 * security_inode_rmdir() - Check if removing a directory is allowed 2227 * @dir: parent directory 2228 * @dentry: directory to be removed 2229 * 2230 * Check the permission to remove a directory. 2231 * 2232 * Return: Returns 0 if permission is granted. 2233 */ 2234 int security_inode_rmdir(struct inode *dir, struct dentry *dentry) 2235 { 2236 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2237 return 0; 2238 return call_int_hook(inode_rmdir, dir, dentry); 2239 } 2240 2241 /** 2242 * security_inode_mknod() - Check if creating a special file is allowed 2243 * @dir: parent directory 2244 * @dentry: new file 2245 * @mode: new file mode 2246 * @dev: device number 2247 * 2248 * Check permissions when creating a special file (or a socket or a fifo file 2249 * created via the mknod system call). Note that if mknod operation is being 2250 * done for a regular file, then the create hook will be called and not this 2251 * hook. 2252 * 2253 * Return: Returns 0 if permission is granted. 2254 */ 2255 int security_inode_mknod(struct inode *dir, struct dentry *dentry, 2256 umode_t mode, dev_t dev) 2257 { 2258 if (unlikely(IS_PRIVATE(dir))) 2259 return 0; 2260 return call_int_hook(inode_mknod, dir, dentry, mode, dev); 2261 } 2262 2263 /** 2264 * security_inode_rename() - Check if renaming a file is allowed 2265 * @old_dir: parent directory of the old file 2266 * @old_dentry: the old file 2267 * @new_dir: parent directory of the new file 2268 * @new_dentry: the new file 2269 * @flags: flags 2270 * 2271 * Check for permission to rename a file or directory. 2272 * 2273 * Return: Returns 0 if permission is granted. 2274 */ 2275 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry, 2276 struct inode *new_dir, struct dentry *new_dentry, 2277 unsigned int flags) 2278 { 2279 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) || 2280 (d_is_positive(new_dentry) && 2281 IS_PRIVATE(d_backing_inode(new_dentry))))) 2282 return 0; 2283 2284 if (flags & RENAME_EXCHANGE) { 2285 int err = call_int_hook(inode_rename, new_dir, new_dentry, 2286 old_dir, old_dentry); 2287 if (err) 2288 return err; 2289 } 2290 2291 return call_int_hook(inode_rename, old_dir, old_dentry, 2292 new_dir, new_dentry); 2293 } 2294 2295 /** 2296 * security_inode_readlink() - Check if reading a symbolic link is allowed 2297 * @dentry: link 2298 * 2299 * Check the permission to read the symbolic link. 2300 * 2301 * Return: Returns 0 if permission is granted. 2302 */ 2303 int security_inode_readlink(struct dentry *dentry) 2304 { 2305 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2306 return 0; 2307 return call_int_hook(inode_readlink, dentry); 2308 } 2309 2310 /** 2311 * security_inode_follow_link() - Check if following a symbolic link is allowed 2312 * @dentry: link dentry 2313 * @inode: link inode 2314 * @rcu: true if in RCU-walk mode 2315 * 2316 * Check permission to follow a symbolic link when looking up a pathname. If 2317 * @rcu is true, @inode is not stable. 2318 * 2319 * Return: Returns 0 if permission is granted. 2320 */ 2321 int security_inode_follow_link(struct dentry *dentry, struct inode *inode, 2322 bool rcu) 2323 { 2324 if (unlikely(IS_PRIVATE(inode))) 2325 return 0; 2326 return call_int_hook(inode_follow_link, dentry, inode, rcu); 2327 } 2328 2329 /** 2330 * security_inode_permission() - Check if accessing an inode is allowed 2331 * @inode: inode 2332 * @mask: access mask 2333 * 2334 * Check permission before accessing an inode. This hook is called by the 2335 * existing Linux permission function, so a security module can use it to 2336 * provide additional checking for existing Linux permission checks. Notice 2337 * that this hook is called when a file is opened (as well as many other 2338 * operations), whereas the file_security_ops permission hook is called when 2339 * the actual read/write operations are performed. 2340 * 2341 * Return: Returns 0 if permission is granted. 2342 */ 2343 int security_inode_permission(struct inode *inode, int mask) 2344 { 2345 if (unlikely(IS_PRIVATE(inode))) 2346 return 0; 2347 return call_int_hook(inode_permission, inode, mask); 2348 } 2349 2350 /** 2351 * security_inode_setattr() - Check if setting file attributes is allowed 2352 * @idmap: idmap of the mount 2353 * @dentry: file 2354 * @attr: new attributes 2355 * 2356 * Check permission before setting file attributes. Note that the kernel call 2357 * to notify_change is performed from several locations, whenever file 2358 * attributes change (such as when a file is truncated, chown/chmod operations, 2359 * transferring disk quotas, etc). 2360 * 2361 * Return: Returns 0 if permission is granted. 2362 */ 2363 int security_inode_setattr(struct mnt_idmap *idmap, 2364 struct dentry *dentry, struct iattr *attr) 2365 { 2366 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2367 return 0; 2368 return call_int_hook(inode_setattr, idmap, dentry, attr); 2369 } 2370 EXPORT_SYMBOL_GPL(security_inode_setattr); 2371 2372 /** 2373 * security_inode_post_setattr() - Update the inode after a setattr operation 2374 * @idmap: idmap of the mount 2375 * @dentry: file 2376 * @ia_valid: file attributes set 2377 * 2378 * Update inode security field after successful setting file attributes. 2379 */ 2380 void security_inode_post_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2381 int ia_valid) 2382 { 2383 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2384 return; 2385 call_void_hook(inode_post_setattr, idmap, dentry, ia_valid); 2386 } 2387 2388 /** 2389 * security_inode_getattr() - Check if getting file attributes is allowed 2390 * @path: file 2391 * 2392 * Check permission before obtaining file attributes. 2393 * 2394 * Return: Returns 0 if permission is granted. 2395 */ 2396 int security_inode_getattr(const struct path *path) 2397 { 2398 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry)))) 2399 return 0; 2400 return call_int_hook(inode_getattr, path); 2401 } 2402 2403 /** 2404 * security_inode_setxattr() - Check if setting file xattrs is allowed 2405 * @idmap: idmap of the mount 2406 * @dentry: file 2407 * @name: xattr name 2408 * @value: xattr value 2409 * @size: size of xattr value 2410 * @flags: flags 2411 * 2412 * This hook performs the desired permission checks before setting the extended 2413 * attributes (xattrs) on @dentry. It is important to note that we have some 2414 * additional logic before the main LSM implementation calls to detect if we 2415 * need to perform an additional capability check at the LSM layer. 2416 * 2417 * Normally we enforce a capability check prior to executing the various LSM 2418 * hook implementations, but if a LSM wants to avoid this capability check, 2419 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for 2420 * xattrs that it wants to avoid the capability check, leaving the LSM fully 2421 * responsible for enforcing the access control for the specific xattr. If all 2422 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook, 2423 * or return a 0 (the default return value), the capability check is still 2424 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability 2425 * check is performed. 2426 * 2427 * Return: Returns 0 if permission is granted. 2428 */ 2429 int security_inode_setxattr(struct mnt_idmap *idmap, 2430 struct dentry *dentry, const char *name, 2431 const void *value, size_t size, int flags) 2432 { 2433 int rc; 2434 2435 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2436 return 0; 2437 2438 /* enforce the capability checks at the lsm layer, if needed */ 2439 if (!call_int_hook(inode_xattr_skipcap, name)) { 2440 rc = cap_inode_setxattr(dentry, name, value, size, flags); 2441 if (rc) 2442 return rc; 2443 } 2444 2445 return call_int_hook(inode_setxattr, idmap, dentry, name, value, size, 2446 flags); 2447 } 2448 2449 /** 2450 * security_inode_set_acl() - Check if setting posix acls is allowed 2451 * @idmap: idmap of the mount 2452 * @dentry: file 2453 * @acl_name: acl name 2454 * @kacl: acl struct 2455 * 2456 * Check permission before setting posix acls, the posix acls in @kacl are 2457 * identified by @acl_name. 2458 * 2459 * Return: Returns 0 if permission is granted. 2460 */ 2461 int security_inode_set_acl(struct mnt_idmap *idmap, 2462 struct dentry *dentry, const char *acl_name, 2463 struct posix_acl *kacl) 2464 { 2465 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2466 return 0; 2467 return call_int_hook(inode_set_acl, idmap, dentry, acl_name, kacl); 2468 } 2469 2470 /** 2471 * security_inode_post_set_acl() - Update inode security from posix acls set 2472 * @dentry: file 2473 * @acl_name: acl name 2474 * @kacl: acl struct 2475 * 2476 * Update inode security data after successfully setting posix acls on @dentry. 2477 * The posix acls in @kacl are identified by @acl_name. 2478 */ 2479 void security_inode_post_set_acl(struct dentry *dentry, const char *acl_name, 2480 struct posix_acl *kacl) 2481 { 2482 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2483 return; 2484 call_void_hook(inode_post_set_acl, dentry, acl_name, kacl); 2485 } 2486 2487 /** 2488 * security_inode_get_acl() - Check if reading posix acls is allowed 2489 * @idmap: idmap of the mount 2490 * @dentry: file 2491 * @acl_name: acl name 2492 * 2493 * Check permission before getting osix acls, the posix acls are identified by 2494 * @acl_name. 2495 * 2496 * Return: Returns 0 if permission is granted. 2497 */ 2498 int security_inode_get_acl(struct mnt_idmap *idmap, 2499 struct dentry *dentry, const char *acl_name) 2500 { 2501 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2502 return 0; 2503 return call_int_hook(inode_get_acl, idmap, dentry, acl_name); 2504 } 2505 2506 /** 2507 * security_inode_remove_acl() - Check if removing a posix acl is allowed 2508 * @idmap: idmap of the mount 2509 * @dentry: file 2510 * @acl_name: acl name 2511 * 2512 * Check permission before removing posix acls, the posix acls are identified 2513 * by @acl_name. 2514 * 2515 * Return: Returns 0 if permission is granted. 2516 */ 2517 int security_inode_remove_acl(struct mnt_idmap *idmap, 2518 struct dentry *dentry, const char *acl_name) 2519 { 2520 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2521 return 0; 2522 return call_int_hook(inode_remove_acl, idmap, dentry, acl_name); 2523 } 2524 2525 /** 2526 * security_inode_post_remove_acl() - Update inode security after rm posix acls 2527 * @idmap: idmap of the mount 2528 * @dentry: file 2529 * @acl_name: acl name 2530 * 2531 * Update inode security data after successfully removing posix acls on 2532 * @dentry in @idmap. The posix acls are identified by @acl_name. 2533 */ 2534 void security_inode_post_remove_acl(struct mnt_idmap *idmap, 2535 struct dentry *dentry, const char *acl_name) 2536 { 2537 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2538 return; 2539 call_void_hook(inode_post_remove_acl, idmap, dentry, acl_name); 2540 } 2541 2542 /** 2543 * security_inode_post_setxattr() - Update the inode after a setxattr operation 2544 * @dentry: file 2545 * @name: xattr name 2546 * @value: xattr value 2547 * @size: xattr value size 2548 * @flags: flags 2549 * 2550 * Update inode security field after successful setxattr operation. 2551 */ 2552 void security_inode_post_setxattr(struct dentry *dentry, const char *name, 2553 const void *value, size_t size, int flags) 2554 { 2555 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2556 return; 2557 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags); 2558 } 2559 2560 /** 2561 * security_inode_getxattr() - Check if xattr access is allowed 2562 * @dentry: file 2563 * @name: xattr name 2564 * 2565 * Check permission before obtaining the extended attributes identified by 2566 * @name for @dentry. 2567 * 2568 * Return: Returns 0 if permission is granted. 2569 */ 2570 int security_inode_getxattr(struct dentry *dentry, const char *name) 2571 { 2572 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2573 return 0; 2574 return call_int_hook(inode_getxattr, dentry, name); 2575 } 2576 2577 /** 2578 * security_inode_listxattr() - Check if listing xattrs is allowed 2579 * @dentry: file 2580 * 2581 * Check permission before obtaining the list of extended attribute names for 2582 * @dentry. 2583 * 2584 * Return: Returns 0 if permission is granted. 2585 */ 2586 int security_inode_listxattr(struct dentry *dentry) 2587 { 2588 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2589 return 0; 2590 return call_int_hook(inode_listxattr, dentry); 2591 } 2592 2593 /** 2594 * security_inode_removexattr() - Check if removing an xattr is allowed 2595 * @idmap: idmap of the mount 2596 * @dentry: file 2597 * @name: xattr name 2598 * 2599 * This hook performs the desired permission checks before setting the extended 2600 * attributes (xattrs) on @dentry. It is important to note that we have some 2601 * additional logic before the main LSM implementation calls to detect if we 2602 * need to perform an additional capability check at the LSM layer. 2603 * 2604 * Normally we enforce a capability check prior to executing the various LSM 2605 * hook implementations, but if a LSM wants to avoid this capability check, 2606 * it can register a 'inode_xattr_skipcap' hook and return a value of 1 for 2607 * xattrs that it wants to avoid the capability check, leaving the LSM fully 2608 * responsible for enforcing the access control for the specific xattr. If all 2609 * of the enabled LSMs refrain from registering a 'inode_xattr_skipcap' hook, 2610 * or return a 0 (the default return value), the capability check is still 2611 * performed. If no 'inode_xattr_skipcap' hooks are registered the capability 2612 * check is performed. 2613 * 2614 * Return: Returns 0 if permission is granted. 2615 */ 2616 int security_inode_removexattr(struct mnt_idmap *idmap, 2617 struct dentry *dentry, const char *name) 2618 { 2619 int rc; 2620 2621 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2622 return 0; 2623 2624 /* enforce the capability checks at the lsm layer, if needed */ 2625 if (!call_int_hook(inode_xattr_skipcap, name)) { 2626 rc = cap_inode_removexattr(idmap, dentry, name); 2627 if (rc) 2628 return rc; 2629 } 2630 2631 return call_int_hook(inode_removexattr, idmap, dentry, name); 2632 } 2633 2634 /** 2635 * security_inode_post_removexattr() - Update the inode after a removexattr op 2636 * @dentry: file 2637 * @name: xattr name 2638 * 2639 * Update the inode after a successful removexattr operation. 2640 */ 2641 void security_inode_post_removexattr(struct dentry *dentry, const char *name) 2642 { 2643 if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 2644 return; 2645 call_void_hook(inode_post_removexattr, dentry, name); 2646 } 2647 2648 /** 2649 * security_inode_need_killpriv() - Check if security_inode_killpriv() required 2650 * @dentry: associated dentry 2651 * 2652 * Called when an inode has been changed to determine if 2653 * security_inode_killpriv() should be called. 2654 * 2655 * Return: Return <0 on error to abort the inode change operation, return 0 if 2656 * security_inode_killpriv() does not need to be called, return >0 if 2657 * security_inode_killpriv() does need to be called. 2658 */ 2659 int security_inode_need_killpriv(struct dentry *dentry) 2660 { 2661 return call_int_hook(inode_need_killpriv, dentry); 2662 } 2663 2664 /** 2665 * security_inode_killpriv() - The setuid bit is removed, update LSM state 2666 * @idmap: idmap of the mount 2667 * @dentry: associated dentry 2668 * 2669 * The @dentry's setuid bit is being removed. Remove similar security labels. 2670 * Called with the dentry->d_inode->i_mutex held. 2671 * 2672 * Return: Return 0 on success. If error is returned, then the operation 2673 * causing setuid bit removal is failed. 2674 */ 2675 int security_inode_killpriv(struct mnt_idmap *idmap, 2676 struct dentry *dentry) 2677 { 2678 return call_int_hook(inode_killpriv, idmap, dentry); 2679 } 2680 2681 /** 2682 * security_inode_getsecurity() - Get the xattr security label of an inode 2683 * @idmap: idmap of the mount 2684 * @inode: inode 2685 * @name: xattr name 2686 * @buffer: security label buffer 2687 * @alloc: allocation flag 2688 * 2689 * Retrieve a copy of the extended attribute representation of the security 2690 * label associated with @name for @inode via @buffer. Note that @name is the 2691 * remainder of the attribute name after the security prefix has been removed. 2692 * @alloc is used to specify if the call should return a value via the buffer 2693 * or just the value length. 2694 * 2695 * Return: Returns size of buffer on success. 2696 */ 2697 int security_inode_getsecurity(struct mnt_idmap *idmap, 2698 struct inode *inode, const char *name, 2699 void **buffer, bool alloc) 2700 { 2701 if (unlikely(IS_PRIVATE(inode))) 2702 return LSM_RET_DEFAULT(inode_getsecurity); 2703 2704 return call_int_hook(inode_getsecurity, idmap, inode, name, buffer, 2705 alloc); 2706 } 2707 2708 /** 2709 * security_inode_setsecurity() - Set the xattr security label of an inode 2710 * @inode: inode 2711 * @name: xattr name 2712 * @value: security label 2713 * @size: length of security label 2714 * @flags: flags 2715 * 2716 * Set the security label associated with @name for @inode from the extended 2717 * attribute value @value. @size indicates the size of the @value in bytes. 2718 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the 2719 * remainder of the attribute name after the security. prefix has been removed. 2720 * 2721 * Return: Returns 0 on success. 2722 */ 2723 int security_inode_setsecurity(struct inode *inode, const char *name, 2724 const void *value, size_t size, int flags) 2725 { 2726 if (unlikely(IS_PRIVATE(inode))) 2727 return LSM_RET_DEFAULT(inode_setsecurity); 2728 2729 return call_int_hook(inode_setsecurity, inode, name, value, size, 2730 flags); 2731 } 2732 2733 /** 2734 * security_inode_listsecurity() - List the xattr security label names 2735 * @inode: inode 2736 * @buffer: buffer 2737 * @buffer_size: size of buffer 2738 * 2739 * Copy the extended attribute names for the security labels associated with 2740 * @inode into @buffer. The maximum size of @buffer is specified by 2741 * @buffer_size. @buffer may be NULL to request the size of the buffer 2742 * required. 2743 * 2744 * Return: Returns number of bytes used/required on success. 2745 */ 2746 int security_inode_listsecurity(struct inode *inode, 2747 char *buffer, size_t buffer_size) 2748 { 2749 if (unlikely(IS_PRIVATE(inode))) 2750 return 0; 2751 return call_int_hook(inode_listsecurity, inode, buffer, buffer_size); 2752 } 2753 EXPORT_SYMBOL(security_inode_listsecurity); 2754 2755 /** 2756 * security_inode_getsecid() - Get an inode's secid 2757 * @inode: inode 2758 * @secid: secid to return 2759 * 2760 * Get the secid associated with the node. In case of failure, @secid will be 2761 * set to zero. 2762 */ 2763 void security_inode_getsecid(struct inode *inode, u32 *secid) 2764 { 2765 call_void_hook(inode_getsecid, inode, secid); 2766 } 2767 2768 /** 2769 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op 2770 * @src: union dentry of copy-up file 2771 * @new: newly created creds 2772 * 2773 * A file is about to be copied up from lower layer to upper layer of overlay 2774 * filesystem. Security module can prepare a set of new creds and modify as 2775 * need be and return new creds. Caller will switch to new creds temporarily to 2776 * create new file and release newly allocated creds. 2777 * 2778 * Return: Returns 0 on success or a negative error code on error. 2779 */ 2780 int security_inode_copy_up(struct dentry *src, struct cred **new) 2781 { 2782 return call_int_hook(inode_copy_up, src, new); 2783 } 2784 EXPORT_SYMBOL(security_inode_copy_up); 2785 2786 /** 2787 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op 2788 * @src: union dentry of copy-up file 2789 * @name: xattr name 2790 * 2791 * Filter the xattrs being copied up when a unioned file is copied up from a 2792 * lower layer to the union/overlay layer. The caller is responsible for 2793 * reading and writing the xattrs, this hook is merely a filter. 2794 * 2795 * Return: Returns 0 to accept the xattr, -ECANCELED to discard the xattr, 2796 * -EOPNOTSUPP if the security module does not know about attribute, 2797 * or a negative error code to abort the copy up. 2798 */ 2799 int security_inode_copy_up_xattr(struct dentry *src, const char *name) 2800 { 2801 int rc; 2802 2803 rc = call_int_hook(inode_copy_up_xattr, src, name); 2804 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr)) 2805 return rc; 2806 2807 return LSM_RET_DEFAULT(inode_copy_up_xattr); 2808 } 2809 EXPORT_SYMBOL(security_inode_copy_up_xattr); 2810 2811 /** 2812 * security_inode_setintegrity() - Set the inode's integrity data 2813 * @inode: inode 2814 * @type: type of integrity, e.g. hash digest, signature, etc 2815 * @value: the integrity value 2816 * @size: size of the integrity value 2817 * 2818 * Register a verified integrity measurement of a inode with LSMs. 2819 * LSMs should free the previously saved data if @value is NULL. 2820 * 2821 * Return: Returns 0 on success, negative values on failure. 2822 */ 2823 int security_inode_setintegrity(const struct inode *inode, 2824 enum lsm_integrity_type type, const void *value, 2825 size_t size) 2826 { 2827 return call_int_hook(inode_setintegrity, inode, type, value, size); 2828 } 2829 EXPORT_SYMBOL(security_inode_setintegrity); 2830 2831 /** 2832 * security_kernfs_init_security() - Init LSM context for a kernfs node 2833 * @kn_dir: parent kernfs node 2834 * @kn: the kernfs node to initialize 2835 * 2836 * Initialize the security context of a newly created kernfs node based on its 2837 * own and its parent's attributes. 2838 * 2839 * Return: Returns 0 if permission is granted. 2840 */ 2841 int security_kernfs_init_security(struct kernfs_node *kn_dir, 2842 struct kernfs_node *kn) 2843 { 2844 return call_int_hook(kernfs_init_security, kn_dir, kn); 2845 } 2846 2847 /** 2848 * security_file_permission() - Check file permissions 2849 * @file: file 2850 * @mask: requested permissions 2851 * 2852 * Check file permissions before accessing an open file. This hook is called 2853 * by various operations that read or write files. A security module can use 2854 * this hook to perform additional checking on these operations, e.g. to 2855 * revalidate permissions on use to support privilege bracketing or policy 2856 * changes. Notice that this hook is used when the actual read/write 2857 * operations are performed, whereas the inode_security_ops hook is called when 2858 * a file is opened (as well as many other operations). Although this hook can 2859 * be used to revalidate permissions for various system call operations that 2860 * read or write files, it does not address the revalidation of permissions for 2861 * memory-mapped files. Security modules must handle this separately if they 2862 * need such revalidation. 2863 * 2864 * Return: Returns 0 if permission is granted. 2865 */ 2866 int security_file_permission(struct file *file, int mask) 2867 { 2868 return call_int_hook(file_permission, file, mask); 2869 } 2870 2871 /** 2872 * security_file_alloc() - Allocate and init a file's LSM blob 2873 * @file: the file 2874 * 2875 * Allocate and attach a security structure to the file->f_security field. The 2876 * security field is initialized to NULL when the structure is first created. 2877 * 2878 * Return: Return 0 if the hook is successful and permission is granted. 2879 */ 2880 int security_file_alloc(struct file *file) 2881 { 2882 int rc = lsm_file_alloc(file); 2883 2884 if (rc) 2885 return rc; 2886 rc = call_int_hook(file_alloc_security, file); 2887 if (unlikely(rc)) 2888 security_file_free(file); 2889 return rc; 2890 } 2891 2892 /** 2893 * security_file_release() - Perform actions before releasing the file ref 2894 * @file: the file 2895 * 2896 * Perform actions before releasing the last reference to a file. 2897 */ 2898 void security_file_release(struct file *file) 2899 { 2900 call_void_hook(file_release, file); 2901 } 2902 2903 /** 2904 * security_file_free() - Free a file's LSM blob 2905 * @file: the file 2906 * 2907 * Deallocate and free any security structures stored in file->f_security. 2908 */ 2909 void security_file_free(struct file *file) 2910 { 2911 void *blob; 2912 2913 call_void_hook(file_free_security, file); 2914 2915 blob = file->f_security; 2916 if (blob) { 2917 file->f_security = NULL; 2918 kmem_cache_free(lsm_file_cache, blob); 2919 } 2920 } 2921 2922 /** 2923 * security_file_ioctl() - Check if an ioctl is allowed 2924 * @file: associated file 2925 * @cmd: ioctl cmd 2926 * @arg: ioctl arguments 2927 * 2928 * Check permission for an ioctl operation on @file. Note that @arg sometimes 2929 * represents a user space pointer; in other cases, it may be a simple integer 2930 * value. When @arg represents a user space pointer, it should never be used 2931 * by the security module. 2932 * 2933 * Return: Returns 0 if permission is granted. 2934 */ 2935 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2936 { 2937 return call_int_hook(file_ioctl, file, cmd, arg); 2938 } 2939 EXPORT_SYMBOL_GPL(security_file_ioctl); 2940 2941 /** 2942 * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode 2943 * @file: associated file 2944 * @cmd: ioctl cmd 2945 * @arg: ioctl arguments 2946 * 2947 * Compat version of security_file_ioctl() that correctly handles 32-bit 2948 * processes running on 64-bit kernels. 2949 * 2950 * Return: Returns 0 if permission is granted. 2951 */ 2952 int security_file_ioctl_compat(struct file *file, unsigned int cmd, 2953 unsigned long arg) 2954 { 2955 return call_int_hook(file_ioctl_compat, file, cmd, arg); 2956 } 2957 EXPORT_SYMBOL_GPL(security_file_ioctl_compat); 2958 2959 static inline unsigned long mmap_prot(struct file *file, unsigned long prot) 2960 { 2961 /* 2962 * Does we have PROT_READ and does the application expect 2963 * it to imply PROT_EXEC? If not, nothing to talk about... 2964 */ 2965 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ) 2966 return prot; 2967 if (!(current->personality & READ_IMPLIES_EXEC)) 2968 return prot; 2969 /* 2970 * if that's an anonymous mapping, let it. 2971 */ 2972 if (!file) 2973 return prot | PROT_EXEC; 2974 /* 2975 * ditto if it's not on noexec mount, except that on !MMU we need 2976 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case 2977 */ 2978 if (!path_noexec(&file->f_path)) { 2979 #ifndef CONFIG_MMU 2980 if (file->f_op->mmap_capabilities) { 2981 unsigned caps = file->f_op->mmap_capabilities(file); 2982 if (!(caps & NOMMU_MAP_EXEC)) 2983 return prot; 2984 } 2985 #endif 2986 return prot | PROT_EXEC; 2987 } 2988 /* anything on noexec mount won't get PROT_EXEC */ 2989 return prot; 2990 } 2991 2992 /** 2993 * security_mmap_file() - Check if mmap'ing a file is allowed 2994 * @file: file 2995 * @prot: protection applied by the kernel 2996 * @flags: flags 2997 * 2998 * Check permissions for a mmap operation. The @file may be NULL, e.g. if 2999 * mapping anonymous memory. 3000 * 3001 * Return: Returns 0 if permission is granted. 3002 */ 3003 int security_mmap_file(struct file *file, unsigned long prot, 3004 unsigned long flags) 3005 { 3006 return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot), 3007 flags); 3008 } 3009 3010 /** 3011 * security_mmap_addr() - Check if mmap'ing an address is allowed 3012 * @addr: address 3013 * 3014 * Check permissions for a mmap operation at @addr. 3015 * 3016 * Return: Returns 0 if permission is granted. 3017 */ 3018 int security_mmap_addr(unsigned long addr) 3019 { 3020 return call_int_hook(mmap_addr, addr); 3021 } 3022 3023 /** 3024 * security_file_mprotect() - Check if changing memory protections is allowed 3025 * @vma: memory region 3026 * @reqprot: application requested protection 3027 * @prot: protection applied by the kernel 3028 * 3029 * Check permissions before changing memory access permissions. 3030 * 3031 * Return: Returns 0 if permission is granted. 3032 */ 3033 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, 3034 unsigned long prot) 3035 { 3036 return call_int_hook(file_mprotect, vma, reqprot, prot); 3037 } 3038 3039 /** 3040 * security_file_lock() - Check if a file lock is allowed 3041 * @file: file 3042 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK) 3043 * 3044 * Check permission before performing file locking operations. Note the hook 3045 * mediates both flock and fcntl style locks. 3046 * 3047 * Return: Returns 0 if permission is granted. 3048 */ 3049 int security_file_lock(struct file *file, unsigned int cmd) 3050 { 3051 return call_int_hook(file_lock, file, cmd); 3052 } 3053 3054 /** 3055 * security_file_fcntl() - Check if fcntl() op is allowed 3056 * @file: file 3057 * @cmd: fcntl command 3058 * @arg: command argument 3059 * 3060 * Check permission before allowing the file operation specified by @cmd from 3061 * being performed on the file @file. Note that @arg sometimes represents a 3062 * user space pointer; in other cases, it may be a simple integer value. When 3063 * @arg represents a user space pointer, it should never be used by the 3064 * security module. 3065 * 3066 * Return: Returns 0 if permission is granted. 3067 */ 3068 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg) 3069 { 3070 return call_int_hook(file_fcntl, file, cmd, arg); 3071 } 3072 3073 /** 3074 * security_file_set_fowner() - Set the file owner info in the LSM blob 3075 * @file: the file 3076 * 3077 * Save owner security information (typically from current->security) in 3078 * file->f_security for later use by the send_sigiotask hook. 3079 * 3080 * Return: Returns 0 on success. 3081 */ 3082 void security_file_set_fowner(struct file *file) 3083 { 3084 call_void_hook(file_set_fowner, file); 3085 } 3086 3087 /** 3088 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed 3089 * @tsk: target task 3090 * @fown: signal sender 3091 * @sig: signal to be sent, SIGIO is sent if 0 3092 * 3093 * Check permission for the file owner @fown to send SIGIO or SIGURG to the 3094 * process @tsk. Note that this hook is sometimes called from interrupt. Note 3095 * that the fown_struct, @fown, is never outside the context of a struct file, 3096 * so the file structure (and associated security information) can always be 3097 * obtained: container_of(fown, struct file, f_owner). 3098 * 3099 * Return: Returns 0 if permission is granted. 3100 */ 3101 int security_file_send_sigiotask(struct task_struct *tsk, 3102 struct fown_struct *fown, int sig) 3103 { 3104 return call_int_hook(file_send_sigiotask, tsk, fown, sig); 3105 } 3106 3107 /** 3108 * security_file_receive() - Check if receiving a file via IPC is allowed 3109 * @file: file being received 3110 * 3111 * This hook allows security modules to control the ability of a process to 3112 * receive an open file descriptor via socket IPC. 3113 * 3114 * Return: Returns 0 if permission is granted. 3115 */ 3116 int security_file_receive(struct file *file) 3117 { 3118 return call_int_hook(file_receive, file); 3119 } 3120 3121 /** 3122 * security_file_open() - Save open() time state for late use by the LSM 3123 * @file: 3124 * 3125 * Save open-time permission checking state for later use upon file_permission, 3126 * and recheck access if anything has changed since inode_permission. 3127 * 3128 * Return: Returns 0 if permission is granted. 3129 */ 3130 int security_file_open(struct file *file) 3131 { 3132 int ret; 3133 3134 ret = call_int_hook(file_open, file); 3135 if (ret) 3136 return ret; 3137 3138 return fsnotify_open_perm(file); 3139 } 3140 3141 /** 3142 * security_file_post_open() - Evaluate a file after it has been opened 3143 * @file: the file 3144 * @mask: access mask 3145 * 3146 * Evaluate an opened file and the access mask requested with open(). The hook 3147 * is useful for LSMs that require the file content to be available in order to 3148 * make decisions. 3149 * 3150 * Return: Returns 0 if permission is granted. 3151 */ 3152 int security_file_post_open(struct file *file, int mask) 3153 { 3154 return call_int_hook(file_post_open, file, mask); 3155 } 3156 EXPORT_SYMBOL_GPL(security_file_post_open); 3157 3158 /** 3159 * security_file_truncate() - Check if truncating a file is allowed 3160 * @file: file 3161 * 3162 * Check permission before truncating a file, i.e. using ftruncate. Note that 3163 * truncation permission may also be checked based on the path, using the 3164 * @path_truncate hook. 3165 * 3166 * Return: Returns 0 if permission is granted. 3167 */ 3168 int security_file_truncate(struct file *file) 3169 { 3170 return call_int_hook(file_truncate, file); 3171 } 3172 3173 /** 3174 * security_task_alloc() - Allocate a task's LSM blob 3175 * @task: the task 3176 * @clone_flags: flags indicating what is being shared 3177 * 3178 * Handle allocation of task-related resources. 3179 * 3180 * Return: Returns a zero on success, negative values on failure. 3181 */ 3182 int security_task_alloc(struct task_struct *task, unsigned long clone_flags) 3183 { 3184 int rc = lsm_task_alloc(task); 3185 3186 if (rc) 3187 return rc; 3188 rc = call_int_hook(task_alloc, task, clone_flags); 3189 if (unlikely(rc)) 3190 security_task_free(task); 3191 return rc; 3192 } 3193 3194 /** 3195 * security_task_free() - Free a task's LSM blob and related resources 3196 * @task: task 3197 * 3198 * Handle release of task-related resources. Note that this can be called from 3199 * interrupt context. 3200 */ 3201 void security_task_free(struct task_struct *task) 3202 { 3203 call_void_hook(task_free, task); 3204 3205 kfree(task->security); 3206 task->security = NULL; 3207 } 3208 3209 /** 3210 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer 3211 * @cred: credentials 3212 * @gfp: gfp flags 3213 * 3214 * Only allocate sufficient memory and attach to @cred such that 3215 * cred_transfer() will not get ENOMEM. 3216 * 3217 * Return: Returns 0 on success, negative values on failure. 3218 */ 3219 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3220 { 3221 int rc = lsm_cred_alloc(cred, gfp); 3222 3223 if (rc) 3224 return rc; 3225 3226 rc = call_int_hook(cred_alloc_blank, cred, gfp); 3227 if (unlikely(rc)) 3228 security_cred_free(cred); 3229 return rc; 3230 } 3231 3232 /** 3233 * security_cred_free() - Free the cred's LSM blob and associated resources 3234 * @cred: credentials 3235 * 3236 * Deallocate and clear the cred->security field in a set of credentials. 3237 */ 3238 void security_cred_free(struct cred *cred) 3239 { 3240 /* 3241 * There is a failure case in prepare_creds() that 3242 * may result in a call here with ->security being NULL. 3243 */ 3244 if (unlikely(cred->security == NULL)) 3245 return; 3246 3247 call_void_hook(cred_free, cred); 3248 3249 kfree(cred->security); 3250 cred->security = NULL; 3251 } 3252 3253 /** 3254 * security_prepare_creds() - Prepare a new set of credentials 3255 * @new: new credentials 3256 * @old: original credentials 3257 * @gfp: gfp flags 3258 * 3259 * Prepare a new set of credentials by copying the data from the old set. 3260 * 3261 * Return: Returns 0 on success, negative values on failure. 3262 */ 3263 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp) 3264 { 3265 int rc = lsm_cred_alloc(new, gfp); 3266 3267 if (rc) 3268 return rc; 3269 3270 rc = call_int_hook(cred_prepare, new, old, gfp); 3271 if (unlikely(rc)) 3272 security_cred_free(new); 3273 return rc; 3274 } 3275 3276 /** 3277 * security_transfer_creds() - Transfer creds 3278 * @new: target credentials 3279 * @old: original credentials 3280 * 3281 * Transfer data from original creds to new creds. 3282 */ 3283 void security_transfer_creds(struct cred *new, const struct cred *old) 3284 { 3285 call_void_hook(cred_transfer, new, old); 3286 } 3287 3288 /** 3289 * security_cred_getsecid() - Get the secid from a set of credentials 3290 * @c: credentials 3291 * @secid: secid value 3292 * 3293 * Retrieve the security identifier of the cred structure @c. In case of 3294 * failure, @secid will be set to zero. 3295 */ 3296 void security_cred_getsecid(const struct cred *c, u32 *secid) 3297 { 3298 *secid = 0; 3299 call_void_hook(cred_getsecid, c, secid); 3300 } 3301 EXPORT_SYMBOL(security_cred_getsecid); 3302 3303 /** 3304 * security_kernel_act_as() - Set the kernel credentials to act as secid 3305 * @new: credentials 3306 * @secid: secid 3307 * 3308 * Set the credentials for a kernel service to act as (subjective context). 3309 * The current task must be the one that nominated @secid. 3310 * 3311 * Return: Returns 0 if successful. 3312 */ 3313 int security_kernel_act_as(struct cred *new, u32 secid) 3314 { 3315 return call_int_hook(kernel_act_as, new, secid); 3316 } 3317 3318 /** 3319 * security_kernel_create_files_as() - Set file creation context using an inode 3320 * @new: target credentials 3321 * @inode: reference inode 3322 * 3323 * Set the file creation context in a set of credentials to be the same as the 3324 * objective context of the specified inode. The current task must be the one 3325 * that nominated @inode. 3326 * 3327 * Return: Returns 0 if successful. 3328 */ 3329 int security_kernel_create_files_as(struct cred *new, struct inode *inode) 3330 { 3331 return call_int_hook(kernel_create_files_as, new, inode); 3332 } 3333 3334 /** 3335 * security_kernel_module_request() - Check if loading a module is allowed 3336 * @kmod_name: module name 3337 * 3338 * Ability to trigger the kernel to automatically upcall to userspace for 3339 * userspace to load a kernel module with the given name. 3340 * 3341 * Return: Returns 0 if successful. 3342 */ 3343 int security_kernel_module_request(char *kmod_name) 3344 { 3345 return call_int_hook(kernel_module_request, kmod_name); 3346 } 3347 3348 /** 3349 * security_kernel_read_file() - Read a file specified by userspace 3350 * @file: file 3351 * @id: file identifier 3352 * @contents: trust if security_kernel_post_read_file() will be called 3353 * 3354 * Read a file specified by userspace. 3355 * 3356 * Return: Returns 0 if permission is granted. 3357 */ 3358 int security_kernel_read_file(struct file *file, enum kernel_read_file_id id, 3359 bool contents) 3360 { 3361 return call_int_hook(kernel_read_file, file, id, contents); 3362 } 3363 EXPORT_SYMBOL_GPL(security_kernel_read_file); 3364 3365 /** 3366 * security_kernel_post_read_file() - Read a file specified by userspace 3367 * @file: file 3368 * @buf: file contents 3369 * @size: size of file contents 3370 * @id: file identifier 3371 * 3372 * Read a file specified by userspace. This must be paired with a prior call 3373 * to security_kernel_read_file() call that indicated this hook would also be 3374 * called, see security_kernel_read_file() for more information. 3375 * 3376 * Return: Returns 0 if permission is granted. 3377 */ 3378 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size, 3379 enum kernel_read_file_id id) 3380 { 3381 return call_int_hook(kernel_post_read_file, file, buf, size, id); 3382 } 3383 EXPORT_SYMBOL_GPL(security_kernel_post_read_file); 3384 3385 /** 3386 * security_kernel_load_data() - Load data provided by userspace 3387 * @id: data identifier 3388 * @contents: true if security_kernel_post_load_data() will be called 3389 * 3390 * Load data provided by userspace. 3391 * 3392 * Return: Returns 0 if permission is granted. 3393 */ 3394 int security_kernel_load_data(enum kernel_load_data_id id, bool contents) 3395 { 3396 return call_int_hook(kernel_load_data, id, contents); 3397 } 3398 EXPORT_SYMBOL_GPL(security_kernel_load_data); 3399 3400 /** 3401 * security_kernel_post_load_data() - Load userspace data from a non-file source 3402 * @buf: data 3403 * @size: size of data 3404 * @id: data identifier 3405 * @description: text description of data, specific to the id value 3406 * 3407 * Load data provided by a non-file source (usually userspace buffer). This 3408 * must be paired with a prior security_kernel_load_data() call that indicated 3409 * this hook would also be called, see security_kernel_load_data() for more 3410 * information. 3411 * 3412 * Return: Returns 0 if permission is granted. 3413 */ 3414 int security_kernel_post_load_data(char *buf, loff_t size, 3415 enum kernel_load_data_id id, 3416 char *description) 3417 { 3418 return call_int_hook(kernel_post_load_data, buf, size, id, description); 3419 } 3420 EXPORT_SYMBOL_GPL(security_kernel_post_load_data); 3421 3422 /** 3423 * security_task_fix_setuid() - Update LSM with new user id attributes 3424 * @new: updated credentials 3425 * @old: credentials being replaced 3426 * @flags: LSM_SETID_* flag values 3427 * 3428 * Update the module's state after setting one or more of the user identity 3429 * attributes of the current process. The @flags parameter indicates which of 3430 * the set*uid system calls invoked this hook. If @new is the set of 3431 * credentials that will be installed. Modifications should be made to this 3432 * rather than to @current->cred. 3433 * 3434 * Return: Returns 0 on success. 3435 */ 3436 int security_task_fix_setuid(struct cred *new, const struct cred *old, 3437 int flags) 3438 { 3439 return call_int_hook(task_fix_setuid, new, old, flags); 3440 } 3441 3442 /** 3443 * security_task_fix_setgid() - Update LSM with new group id attributes 3444 * @new: updated credentials 3445 * @old: credentials being replaced 3446 * @flags: LSM_SETID_* flag value 3447 * 3448 * Update the module's state after setting one or more of the group identity 3449 * attributes of the current process. The @flags parameter indicates which of 3450 * the set*gid system calls invoked this hook. @new is the set of credentials 3451 * that will be installed. Modifications should be made to this rather than to 3452 * @current->cred. 3453 * 3454 * Return: Returns 0 on success. 3455 */ 3456 int security_task_fix_setgid(struct cred *new, const struct cred *old, 3457 int flags) 3458 { 3459 return call_int_hook(task_fix_setgid, new, old, flags); 3460 } 3461 3462 /** 3463 * security_task_fix_setgroups() - Update LSM with new supplementary groups 3464 * @new: updated credentials 3465 * @old: credentials being replaced 3466 * 3467 * Update the module's state after setting the supplementary group identity 3468 * attributes of the current process. @new is the set of credentials that will 3469 * be installed. Modifications should be made to this rather than to 3470 * @current->cred. 3471 * 3472 * Return: Returns 0 on success. 3473 */ 3474 int security_task_fix_setgroups(struct cred *new, const struct cred *old) 3475 { 3476 return call_int_hook(task_fix_setgroups, new, old); 3477 } 3478 3479 /** 3480 * security_task_setpgid() - Check if setting the pgid is allowed 3481 * @p: task being modified 3482 * @pgid: new pgid 3483 * 3484 * Check permission before setting the process group identifier of the process 3485 * @p to @pgid. 3486 * 3487 * Return: Returns 0 if permission is granted. 3488 */ 3489 int security_task_setpgid(struct task_struct *p, pid_t pgid) 3490 { 3491 return call_int_hook(task_setpgid, p, pgid); 3492 } 3493 3494 /** 3495 * security_task_getpgid() - Check if getting the pgid is allowed 3496 * @p: task 3497 * 3498 * Check permission before getting the process group identifier of the process 3499 * @p. 3500 * 3501 * Return: Returns 0 if permission is granted. 3502 */ 3503 int security_task_getpgid(struct task_struct *p) 3504 { 3505 return call_int_hook(task_getpgid, p); 3506 } 3507 3508 /** 3509 * security_task_getsid() - Check if getting the session id is allowed 3510 * @p: task 3511 * 3512 * Check permission before getting the session identifier of the process @p. 3513 * 3514 * Return: Returns 0 if permission is granted. 3515 */ 3516 int security_task_getsid(struct task_struct *p) 3517 { 3518 return call_int_hook(task_getsid, p); 3519 } 3520 3521 /** 3522 * security_current_getsecid_subj() - Get the current task's subjective secid 3523 * @secid: secid value 3524 * 3525 * Retrieve the subjective security identifier of the current task and return 3526 * it in @secid. In case of failure, @secid will be set to zero. 3527 */ 3528 void security_current_getsecid_subj(u32 *secid) 3529 { 3530 *secid = 0; 3531 call_void_hook(current_getsecid_subj, secid); 3532 } 3533 EXPORT_SYMBOL(security_current_getsecid_subj); 3534 3535 /** 3536 * security_task_getsecid_obj() - Get a task's objective secid 3537 * @p: target task 3538 * @secid: secid value 3539 * 3540 * Retrieve the objective security identifier of the task_struct in @p and 3541 * return it in @secid. In case of failure, @secid will be set to zero. 3542 */ 3543 void security_task_getsecid_obj(struct task_struct *p, u32 *secid) 3544 { 3545 *secid = 0; 3546 call_void_hook(task_getsecid_obj, p, secid); 3547 } 3548 EXPORT_SYMBOL(security_task_getsecid_obj); 3549 3550 /** 3551 * security_task_setnice() - Check if setting a task's nice value is allowed 3552 * @p: target task 3553 * @nice: nice value 3554 * 3555 * Check permission before setting the nice value of @p to @nice. 3556 * 3557 * Return: Returns 0 if permission is granted. 3558 */ 3559 int security_task_setnice(struct task_struct *p, int nice) 3560 { 3561 return call_int_hook(task_setnice, p, nice); 3562 } 3563 3564 /** 3565 * security_task_setioprio() - Check if setting a task's ioprio is allowed 3566 * @p: target task 3567 * @ioprio: ioprio value 3568 * 3569 * Check permission before setting the ioprio value of @p to @ioprio. 3570 * 3571 * Return: Returns 0 if permission is granted. 3572 */ 3573 int security_task_setioprio(struct task_struct *p, int ioprio) 3574 { 3575 return call_int_hook(task_setioprio, p, ioprio); 3576 } 3577 3578 /** 3579 * security_task_getioprio() - Check if getting a task's ioprio is allowed 3580 * @p: task 3581 * 3582 * Check permission before getting the ioprio value of @p. 3583 * 3584 * Return: Returns 0 if permission is granted. 3585 */ 3586 int security_task_getioprio(struct task_struct *p) 3587 { 3588 return call_int_hook(task_getioprio, p); 3589 } 3590 3591 /** 3592 * security_task_prlimit() - Check if get/setting resources limits is allowed 3593 * @cred: current task credentials 3594 * @tcred: target task credentials 3595 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both 3596 * 3597 * Check permission before getting and/or setting the resource limits of 3598 * another task. 3599 * 3600 * Return: Returns 0 if permission is granted. 3601 */ 3602 int security_task_prlimit(const struct cred *cred, const struct cred *tcred, 3603 unsigned int flags) 3604 { 3605 return call_int_hook(task_prlimit, cred, tcred, flags); 3606 } 3607 3608 /** 3609 * security_task_setrlimit() - Check if setting a new rlimit value is allowed 3610 * @p: target task's group leader 3611 * @resource: resource whose limit is being set 3612 * @new_rlim: new resource limit 3613 * 3614 * Check permission before setting the resource limits of process @p for 3615 * @resource to @new_rlim. The old resource limit values can be examined by 3616 * dereferencing (p->signal->rlim + resource). 3617 * 3618 * Return: Returns 0 if permission is granted. 3619 */ 3620 int security_task_setrlimit(struct task_struct *p, unsigned int resource, 3621 struct rlimit *new_rlim) 3622 { 3623 return call_int_hook(task_setrlimit, p, resource, new_rlim); 3624 } 3625 3626 /** 3627 * security_task_setscheduler() - Check if setting sched policy/param is allowed 3628 * @p: target task 3629 * 3630 * Check permission before setting scheduling policy and/or parameters of 3631 * process @p. 3632 * 3633 * Return: Returns 0 if permission is granted. 3634 */ 3635 int security_task_setscheduler(struct task_struct *p) 3636 { 3637 return call_int_hook(task_setscheduler, p); 3638 } 3639 3640 /** 3641 * security_task_getscheduler() - Check if getting scheduling info is allowed 3642 * @p: target task 3643 * 3644 * Check permission before obtaining scheduling information for process @p. 3645 * 3646 * Return: Returns 0 if permission is granted. 3647 */ 3648 int security_task_getscheduler(struct task_struct *p) 3649 { 3650 return call_int_hook(task_getscheduler, p); 3651 } 3652 3653 /** 3654 * security_task_movememory() - Check if moving memory is allowed 3655 * @p: task 3656 * 3657 * Check permission before moving memory owned by process @p. 3658 * 3659 * Return: Returns 0 if permission is granted. 3660 */ 3661 int security_task_movememory(struct task_struct *p) 3662 { 3663 return call_int_hook(task_movememory, p); 3664 } 3665 3666 /** 3667 * security_task_kill() - Check if sending a signal is allowed 3668 * @p: target process 3669 * @info: signal information 3670 * @sig: signal value 3671 * @cred: credentials of the signal sender, NULL if @current 3672 * 3673 * Check permission before sending signal @sig to @p. @info can be NULL, the 3674 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or 3675 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from 3676 * the kernel and should typically be permitted. SIGIO signals are handled 3677 * separately by the send_sigiotask hook in file_security_ops. 3678 * 3679 * Return: Returns 0 if permission is granted. 3680 */ 3681 int security_task_kill(struct task_struct *p, struct kernel_siginfo *info, 3682 int sig, const struct cred *cred) 3683 { 3684 return call_int_hook(task_kill, p, info, sig, cred); 3685 } 3686 3687 /** 3688 * security_task_prctl() - Check if a prctl op is allowed 3689 * @option: operation 3690 * @arg2: argument 3691 * @arg3: argument 3692 * @arg4: argument 3693 * @arg5: argument 3694 * 3695 * Check permission before performing a process control operation on the 3696 * current process. 3697 * 3698 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value 3699 * to cause prctl() to return immediately with that value. 3700 */ 3701 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3, 3702 unsigned long arg4, unsigned long arg5) 3703 { 3704 int thisrc; 3705 int rc = LSM_RET_DEFAULT(task_prctl); 3706 struct lsm_static_call *scall; 3707 3708 lsm_for_each_hook(scall, task_prctl) { 3709 thisrc = scall->hl->hook.task_prctl(option, arg2, arg3, arg4, arg5); 3710 if (thisrc != LSM_RET_DEFAULT(task_prctl)) { 3711 rc = thisrc; 3712 if (thisrc != 0) 3713 break; 3714 } 3715 } 3716 return rc; 3717 } 3718 3719 /** 3720 * security_task_to_inode() - Set the security attributes of a task's inode 3721 * @p: task 3722 * @inode: inode 3723 * 3724 * Set the security attributes for an inode based on an associated task's 3725 * security attributes, e.g. for /proc/pid inodes. 3726 */ 3727 void security_task_to_inode(struct task_struct *p, struct inode *inode) 3728 { 3729 call_void_hook(task_to_inode, p, inode); 3730 } 3731 3732 /** 3733 * security_create_user_ns() - Check if creating a new userns is allowed 3734 * @cred: prepared creds 3735 * 3736 * Check permission prior to creating a new user namespace. 3737 * 3738 * Return: Returns 0 if successful, otherwise < 0 error code. 3739 */ 3740 int security_create_user_ns(const struct cred *cred) 3741 { 3742 return call_int_hook(userns_create, cred); 3743 } 3744 3745 /** 3746 * security_ipc_permission() - Check if sysv ipc access is allowed 3747 * @ipcp: ipc permission structure 3748 * @flag: requested permissions 3749 * 3750 * Check permissions for access to IPC. 3751 * 3752 * Return: Returns 0 if permission is granted. 3753 */ 3754 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 3755 { 3756 return call_int_hook(ipc_permission, ipcp, flag); 3757 } 3758 3759 /** 3760 * security_ipc_getsecid() - Get the sysv ipc object's secid 3761 * @ipcp: ipc permission structure 3762 * @secid: secid pointer 3763 * 3764 * Get the secid associated with the ipc object. In case of failure, @secid 3765 * will be set to zero. 3766 */ 3767 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 3768 { 3769 *secid = 0; 3770 call_void_hook(ipc_getsecid, ipcp, secid); 3771 } 3772 3773 /** 3774 * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob 3775 * @msg: message structure 3776 * 3777 * Allocate and attach a security structure to the msg->security field. The 3778 * security field is initialized to NULL when the structure is first created. 3779 * 3780 * Return: Return 0 if operation was successful and permission is granted. 3781 */ 3782 int security_msg_msg_alloc(struct msg_msg *msg) 3783 { 3784 int rc = lsm_msg_msg_alloc(msg); 3785 3786 if (unlikely(rc)) 3787 return rc; 3788 rc = call_int_hook(msg_msg_alloc_security, msg); 3789 if (unlikely(rc)) 3790 security_msg_msg_free(msg); 3791 return rc; 3792 } 3793 3794 /** 3795 * security_msg_msg_free() - Free a sysv ipc message LSM blob 3796 * @msg: message structure 3797 * 3798 * Deallocate the security structure for this message. 3799 */ 3800 void security_msg_msg_free(struct msg_msg *msg) 3801 { 3802 call_void_hook(msg_msg_free_security, msg); 3803 kfree(msg->security); 3804 msg->security = NULL; 3805 } 3806 3807 /** 3808 * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob 3809 * @msq: sysv ipc permission structure 3810 * 3811 * Allocate and attach a security structure to @msg. The security field is 3812 * initialized to NULL when the structure is first created. 3813 * 3814 * Return: Returns 0 if operation was successful and permission is granted. 3815 */ 3816 int security_msg_queue_alloc(struct kern_ipc_perm *msq) 3817 { 3818 int rc = lsm_ipc_alloc(msq); 3819 3820 if (unlikely(rc)) 3821 return rc; 3822 rc = call_int_hook(msg_queue_alloc_security, msq); 3823 if (unlikely(rc)) 3824 security_msg_queue_free(msq); 3825 return rc; 3826 } 3827 3828 /** 3829 * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob 3830 * @msq: sysv ipc permission structure 3831 * 3832 * Deallocate security field @perm->security for the message queue. 3833 */ 3834 void security_msg_queue_free(struct kern_ipc_perm *msq) 3835 { 3836 call_void_hook(msg_queue_free_security, msq); 3837 kfree(msq->security); 3838 msq->security = NULL; 3839 } 3840 3841 /** 3842 * security_msg_queue_associate() - Check if a msg queue operation is allowed 3843 * @msq: sysv ipc permission structure 3844 * @msqflg: operation flags 3845 * 3846 * Check permission when a message queue is requested through the msgget system 3847 * call. This hook is only called when returning the message queue identifier 3848 * for an existing message queue, not when a new message queue is created. 3849 * 3850 * Return: Return 0 if permission is granted. 3851 */ 3852 int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 3853 { 3854 return call_int_hook(msg_queue_associate, msq, msqflg); 3855 } 3856 3857 /** 3858 * security_msg_queue_msgctl() - Check if a msg queue operation is allowed 3859 * @msq: sysv ipc permission structure 3860 * @cmd: operation 3861 * 3862 * Check permission when a message control operation specified by @cmd is to be 3863 * performed on the message queue with permissions. 3864 * 3865 * Return: Returns 0 if permission is granted. 3866 */ 3867 int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 3868 { 3869 return call_int_hook(msg_queue_msgctl, msq, cmd); 3870 } 3871 3872 /** 3873 * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed 3874 * @msq: sysv ipc permission structure 3875 * @msg: message 3876 * @msqflg: operation flags 3877 * 3878 * Check permission before a message, @msg, is enqueued on the message queue 3879 * with permissions specified in @msq. 3880 * 3881 * Return: Returns 0 if permission is granted. 3882 */ 3883 int security_msg_queue_msgsnd(struct kern_ipc_perm *msq, 3884 struct msg_msg *msg, int msqflg) 3885 { 3886 return call_int_hook(msg_queue_msgsnd, msq, msg, msqflg); 3887 } 3888 3889 /** 3890 * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed 3891 * @msq: sysv ipc permission structure 3892 * @msg: message 3893 * @target: target task 3894 * @type: type of message requested 3895 * @mode: operation flags 3896 * 3897 * Check permission before a message, @msg, is removed from the message queue. 3898 * The @target task structure contains a pointer to the process that will be 3899 * receiving the message (not equal to the current process when inline receives 3900 * are being performed). 3901 * 3902 * Return: Returns 0 if permission is granted. 3903 */ 3904 int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 3905 struct task_struct *target, long type, int mode) 3906 { 3907 return call_int_hook(msg_queue_msgrcv, msq, msg, target, type, mode); 3908 } 3909 3910 /** 3911 * security_shm_alloc() - Allocate a sysv shm LSM blob 3912 * @shp: sysv ipc permission structure 3913 * 3914 * Allocate and attach a security structure to the @shp security field. The 3915 * security field is initialized to NULL when the structure is first created. 3916 * 3917 * Return: Returns 0 if operation was successful and permission is granted. 3918 */ 3919 int security_shm_alloc(struct kern_ipc_perm *shp) 3920 { 3921 int rc = lsm_ipc_alloc(shp); 3922 3923 if (unlikely(rc)) 3924 return rc; 3925 rc = call_int_hook(shm_alloc_security, shp); 3926 if (unlikely(rc)) 3927 security_shm_free(shp); 3928 return rc; 3929 } 3930 3931 /** 3932 * security_shm_free() - Free a sysv shm LSM blob 3933 * @shp: sysv ipc permission structure 3934 * 3935 * Deallocate the security structure @perm->security for the memory segment. 3936 */ 3937 void security_shm_free(struct kern_ipc_perm *shp) 3938 { 3939 call_void_hook(shm_free_security, shp); 3940 kfree(shp->security); 3941 shp->security = NULL; 3942 } 3943 3944 /** 3945 * security_shm_associate() - Check if a sysv shm operation is allowed 3946 * @shp: sysv ipc permission structure 3947 * @shmflg: operation flags 3948 * 3949 * Check permission when a shared memory region is requested through the shmget 3950 * system call. This hook is only called when returning the shared memory 3951 * region identifier for an existing region, not when a new shared memory 3952 * region is created. 3953 * 3954 * Return: Returns 0 if permission is granted. 3955 */ 3956 int security_shm_associate(struct kern_ipc_perm *shp, int shmflg) 3957 { 3958 return call_int_hook(shm_associate, shp, shmflg); 3959 } 3960 3961 /** 3962 * security_shm_shmctl() - Check if a sysv shm operation is allowed 3963 * @shp: sysv ipc permission structure 3964 * @cmd: operation 3965 * 3966 * Check permission when a shared memory control operation specified by @cmd is 3967 * to be performed on the shared memory region with permissions in @shp. 3968 * 3969 * Return: Return 0 if permission is granted. 3970 */ 3971 int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 3972 { 3973 return call_int_hook(shm_shmctl, shp, cmd); 3974 } 3975 3976 /** 3977 * security_shm_shmat() - Check if a sysv shm attach operation is allowed 3978 * @shp: sysv ipc permission structure 3979 * @shmaddr: address of memory region to attach 3980 * @shmflg: operation flags 3981 * 3982 * Check permissions prior to allowing the shmat system call to attach the 3983 * shared memory segment with permissions @shp to the data segment of the 3984 * calling process. The attaching address is specified by @shmaddr. 3985 * 3986 * Return: Returns 0 if permission is granted. 3987 */ 3988 int security_shm_shmat(struct kern_ipc_perm *shp, 3989 char __user *shmaddr, int shmflg) 3990 { 3991 return call_int_hook(shm_shmat, shp, shmaddr, shmflg); 3992 } 3993 3994 /** 3995 * security_sem_alloc() - Allocate a sysv semaphore LSM blob 3996 * @sma: sysv ipc permission structure 3997 * 3998 * Allocate and attach a security structure to the @sma security field. The 3999 * security field is initialized to NULL when the structure is first created. 4000 * 4001 * Return: Returns 0 if operation was successful and permission is granted. 4002 */ 4003 int security_sem_alloc(struct kern_ipc_perm *sma) 4004 { 4005 int rc = lsm_ipc_alloc(sma); 4006 4007 if (unlikely(rc)) 4008 return rc; 4009 rc = call_int_hook(sem_alloc_security, sma); 4010 if (unlikely(rc)) 4011 security_sem_free(sma); 4012 return rc; 4013 } 4014 4015 /** 4016 * security_sem_free() - Free a sysv semaphore LSM blob 4017 * @sma: sysv ipc permission structure 4018 * 4019 * Deallocate security structure @sma->security for the semaphore. 4020 */ 4021 void security_sem_free(struct kern_ipc_perm *sma) 4022 { 4023 call_void_hook(sem_free_security, sma); 4024 kfree(sma->security); 4025 sma->security = NULL; 4026 } 4027 4028 /** 4029 * security_sem_associate() - Check if a sysv semaphore operation is allowed 4030 * @sma: sysv ipc permission structure 4031 * @semflg: operation flags 4032 * 4033 * Check permission when a semaphore is requested through the semget system 4034 * call. This hook is only called when returning the semaphore identifier for 4035 * an existing semaphore, not when a new one must be created. 4036 * 4037 * Return: Returns 0 if permission is granted. 4038 */ 4039 int security_sem_associate(struct kern_ipc_perm *sma, int semflg) 4040 { 4041 return call_int_hook(sem_associate, sma, semflg); 4042 } 4043 4044 /** 4045 * security_sem_semctl() - Check if a sysv semaphore operation is allowed 4046 * @sma: sysv ipc permission structure 4047 * @cmd: operation 4048 * 4049 * Check permission when a semaphore operation specified by @cmd is to be 4050 * performed on the semaphore. 4051 * 4052 * Return: Returns 0 if permission is granted. 4053 */ 4054 int security_sem_semctl(struct kern_ipc_perm *sma, int cmd) 4055 { 4056 return call_int_hook(sem_semctl, sma, cmd); 4057 } 4058 4059 /** 4060 * security_sem_semop() - Check if a sysv semaphore operation is allowed 4061 * @sma: sysv ipc permission structure 4062 * @sops: operations to perform 4063 * @nsops: number of operations 4064 * @alter: flag indicating changes will be made 4065 * 4066 * Check permissions before performing operations on members of the semaphore 4067 * set. If the @alter flag is nonzero, the semaphore set may be modified. 4068 * 4069 * Return: Returns 0 if permission is granted. 4070 */ 4071 int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops, 4072 unsigned nsops, int alter) 4073 { 4074 return call_int_hook(sem_semop, sma, sops, nsops, alter); 4075 } 4076 4077 /** 4078 * security_d_instantiate() - Populate an inode's LSM state based on a dentry 4079 * @dentry: dentry 4080 * @inode: inode 4081 * 4082 * Fill in @inode security information for a @dentry if allowed. 4083 */ 4084 void security_d_instantiate(struct dentry *dentry, struct inode *inode) 4085 { 4086 if (unlikely(inode && IS_PRIVATE(inode))) 4087 return; 4088 call_void_hook(d_instantiate, dentry, inode); 4089 } 4090 EXPORT_SYMBOL(security_d_instantiate); 4091 4092 /* 4093 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c 4094 */ 4095 4096 /** 4097 * security_getselfattr - Read an LSM attribute of the current process. 4098 * @attr: which attribute to return 4099 * @uctx: the user-space destination for the information, or NULL 4100 * @size: pointer to the size of space available to receive the data 4101 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only 4102 * attributes associated with the LSM identified in the passed @ctx be 4103 * reported. 4104 * 4105 * A NULL value for @uctx can be used to get both the number of attributes 4106 * and the size of the data. 4107 * 4108 * Returns the number of attributes found on success, negative value 4109 * on error. @size is reset to the total size of the data. 4110 * If @size is insufficient to contain the data -E2BIG is returned. 4111 */ 4112 int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx, 4113 u32 __user *size, u32 flags) 4114 { 4115 struct lsm_static_call *scall; 4116 struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, }; 4117 u8 __user *base = (u8 __user *)uctx; 4118 u32 entrysize; 4119 u32 total = 0; 4120 u32 left; 4121 bool toobig = false; 4122 bool single = false; 4123 int count = 0; 4124 int rc; 4125 4126 if (attr == LSM_ATTR_UNDEF) 4127 return -EINVAL; 4128 if (size == NULL) 4129 return -EINVAL; 4130 if (get_user(left, size)) 4131 return -EFAULT; 4132 4133 if (flags) { 4134 /* 4135 * Only flag supported is LSM_FLAG_SINGLE 4136 */ 4137 if (flags != LSM_FLAG_SINGLE || !uctx) 4138 return -EINVAL; 4139 if (copy_from_user(&lctx, uctx, sizeof(lctx))) 4140 return -EFAULT; 4141 /* 4142 * If the LSM ID isn't specified it is an error. 4143 */ 4144 if (lctx.id == LSM_ID_UNDEF) 4145 return -EINVAL; 4146 single = true; 4147 } 4148 4149 /* 4150 * In the usual case gather all the data from the LSMs. 4151 * In the single case only get the data from the LSM specified. 4152 */ 4153 lsm_for_each_hook(scall, getselfattr) { 4154 if (single && lctx.id != scall->hl->lsmid->id) 4155 continue; 4156 entrysize = left; 4157 if (base) 4158 uctx = (struct lsm_ctx __user *)(base + total); 4159 rc = scall->hl->hook.getselfattr(attr, uctx, &entrysize, flags); 4160 if (rc == -EOPNOTSUPP) { 4161 rc = 0; 4162 continue; 4163 } 4164 if (rc == -E2BIG) { 4165 rc = 0; 4166 left = 0; 4167 toobig = true; 4168 } else if (rc < 0) 4169 return rc; 4170 else 4171 left -= entrysize; 4172 4173 total += entrysize; 4174 count += rc; 4175 if (single) 4176 break; 4177 } 4178 if (put_user(total, size)) 4179 return -EFAULT; 4180 if (toobig) 4181 return -E2BIG; 4182 if (count == 0) 4183 return LSM_RET_DEFAULT(getselfattr); 4184 return count; 4185 } 4186 4187 /* 4188 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c 4189 */ 4190 4191 /** 4192 * security_setselfattr - Set an LSM attribute on the current process. 4193 * @attr: which attribute to set 4194 * @uctx: the user-space source for the information 4195 * @size: the size of the data 4196 * @flags: reserved for future use, must be 0 4197 * 4198 * Set an LSM attribute for the current process. The LSM, attribute 4199 * and new value are included in @uctx. 4200 * 4201 * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT 4202 * if the user buffer is inaccessible, E2BIG if size is too big, or an 4203 * LSM specific failure. 4204 */ 4205 int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx, 4206 u32 size, u32 flags) 4207 { 4208 struct lsm_static_call *scall; 4209 struct lsm_ctx *lctx; 4210 int rc = LSM_RET_DEFAULT(setselfattr); 4211 u64 required_len; 4212 4213 if (flags) 4214 return -EINVAL; 4215 if (size < sizeof(*lctx)) 4216 return -EINVAL; 4217 if (size > PAGE_SIZE) 4218 return -E2BIG; 4219 4220 lctx = memdup_user(uctx, size); 4221 if (IS_ERR(lctx)) 4222 return PTR_ERR(lctx); 4223 4224 if (size < lctx->len || 4225 check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) || 4226 lctx->len < required_len) { 4227 rc = -EINVAL; 4228 goto free_out; 4229 } 4230 4231 lsm_for_each_hook(scall, setselfattr) 4232 if ((scall->hl->lsmid->id) == lctx->id) { 4233 rc = scall->hl->hook.setselfattr(attr, lctx, size, flags); 4234 break; 4235 } 4236 4237 free_out: 4238 kfree(lctx); 4239 return rc; 4240 } 4241 4242 /** 4243 * security_getprocattr() - Read an attribute for a task 4244 * @p: the task 4245 * @lsmid: LSM identification 4246 * @name: attribute name 4247 * @value: attribute value 4248 * 4249 * Read attribute @name for task @p and store it into @value if allowed. 4250 * 4251 * Return: Returns the length of @value on success, a negative value otherwise. 4252 */ 4253 int security_getprocattr(struct task_struct *p, int lsmid, const char *name, 4254 char **value) 4255 { 4256 struct lsm_static_call *scall; 4257 4258 lsm_for_each_hook(scall, getprocattr) { 4259 if (lsmid != 0 && lsmid != scall->hl->lsmid->id) 4260 continue; 4261 return scall->hl->hook.getprocattr(p, name, value); 4262 } 4263 return LSM_RET_DEFAULT(getprocattr); 4264 } 4265 4266 /** 4267 * security_setprocattr() - Set an attribute for a task 4268 * @lsmid: LSM identification 4269 * @name: attribute name 4270 * @value: attribute value 4271 * @size: attribute value size 4272 * 4273 * Write (set) the current task's attribute @name to @value, size @size if 4274 * allowed. 4275 * 4276 * Return: Returns bytes written on success, a negative value otherwise. 4277 */ 4278 int security_setprocattr(int lsmid, const char *name, void *value, size_t size) 4279 { 4280 struct lsm_static_call *scall; 4281 4282 lsm_for_each_hook(scall, setprocattr) { 4283 if (lsmid != 0 && lsmid != scall->hl->lsmid->id) 4284 continue; 4285 return scall->hl->hook.setprocattr(name, value, size); 4286 } 4287 return LSM_RET_DEFAULT(setprocattr); 4288 } 4289 4290 /** 4291 * security_netlink_send() - Save info and check if netlink sending is allowed 4292 * @sk: sending socket 4293 * @skb: netlink message 4294 * 4295 * Save security information for a netlink message so that permission checking 4296 * can be performed when the message is processed. The security information 4297 * can be saved using the eff_cap field of the netlink_skb_parms structure. 4298 * Also may be used to provide fine grained control over message transmission. 4299 * 4300 * Return: Returns 0 if the information was successfully saved and message is 4301 * allowed to be transmitted. 4302 */ 4303 int security_netlink_send(struct sock *sk, struct sk_buff *skb) 4304 { 4305 return call_int_hook(netlink_send, sk, skb); 4306 } 4307 4308 /** 4309 * security_ismaclabel() - Check if the named attribute is a MAC label 4310 * @name: full extended attribute name 4311 * 4312 * Check if the extended attribute specified by @name represents a MAC label. 4313 * 4314 * Return: Returns 1 if name is a MAC attribute otherwise returns 0. 4315 */ 4316 int security_ismaclabel(const char *name) 4317 { 4318 return call_int_hook(ismaclabel, name); 4319 } 4320 EXPORT_SYMBOL(security_ismaclabel); 4321 4322 /** 4323 * security_secid_to_secctx() - Convert a secid to a secctx 4324 * @secid: secid 4325 * @secdata: secctx 4326 * @seclen: secctx length 4327 * 4328 * Convert secid to security context. If @secdata is NULL the length of the 4329 * result will be returned in @seclen, but no @secdata will be returned. This 4330 * does mean that the length could change between calls to check the length and 4331 * the next call which actually allocates and returns the @secdata. 4332 * 4333 * Return: Return 0 on success, error on failure. 4334 */ 4335 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 4336 { 4337 return call_int_hook(secid_to_secctx, secid, secdata, seclen); 4338 } 4339 EXPORT_SYMBOL(security_secid_to_secctx); 4340 4341 /** 4342 * security_secctx_to_secid() - Convert a secctx to a secid 4343 * @secdata: secctx 4344 * @seclen: length of secctx 4345 * @secid: secid 4346 * 4347 * Convert security context to secid. 4348 * 4349 * Return: Returns 0 on success, error on failure. 4350 */ 4351 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 4352 { 4353 *secid = 0; 4354 return call_int_hook(secctx_to_secid, secdata, seclen, secid); 4355 } 4356 EXPORT_SYMBOL(security_secctx_to_secid); 4357 4358 /** 4359 * security_release_secctx() - Free a secctx buffer 4360 * @secdata: secctx 4361 * @seclen: length of secctx 4362 * 4363 * Release the security context. 4364 */ 4365 void security_release_secctx(char *secdata, u32 seclen) 4366 { 4367 call_void_hook(release_secctx, secdata, seclen); 4368 } 4369 EXPORT_SYMBOL(security_release_secctx); 4370 4371 /** 4372 * security_inode_invalidate_secctx() - Invalidate an inode's security label 4373 * @inode: inode 4374 * 4375 * Notify the security module that it must revalidate the security context of 4376 * an inode. 4377 */ 4378 void security_inode_invalidate_secctx(struct inode *inode) 4379 { 4380 call_void_hook(inode_invalidate_secctx, inode); 4381 } 4382 EXPORT_SYMBOL(security_inode_invalidate_secctx); 4383 4384 /** 4385 * security_inode_notifysecctx() - Notify the LSM of an inode's security label 4386 * @inode: inode 4387 * @ctx: secctx 4388 * @ctxlen: length of secctx 4389 * 4390 * Notify the security module of what the security context of an inode should 4391 * be. Initializes the incore security context managed by the security module 4392 * for this inode. Example usage: NFS client invokes this hook to initialize 4393 * the security context in its incore inode to the value provided by the server 4394 * for the file when the server returned the file's attributes to the client. 4395 * Must be called with inode->i_mutex locked. 4396 * 4397 * Return: Returns 0 on success, error on failure. 4398 */ 4399 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 4400 { 4401 return call_int_hook(inode_notifysecctx, inode, ctx, ctxlen); 4402 } 4403 EXPORT_SYMBOL(security_inode_notifysecctx); 4404 4405 /** 4406 * security_inode_setsecctx() - Change the security label of an inode 4407 * @dentry: inode 4408 * @ctx: secctx 4409 * @ctxlen: length of secctx 4410 * 4411 * Change the security context of an inode. Updates the incore security 4412 * context managed by the security module and invokes the fs code as needed 4413 * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the 4414 * context. Example usage: NFS server invokes this hook to change the security 4415 * context in its incore inode and on the backing filesystem to a value 4416 * provided by the client on a SETATTR operation. Must be called with 4417 * inode->i_mutex locked. 4418 * 4419 * Return: Returns 0 on success, error on failure. 4420 */ 4421 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 4422 { 4423 return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen); 4424 } 4425 EXPORT_SYMBOL(security_inode_setsecctx); 4426 4427 /** 4428 * security_inode_getsecctx() - Get the security label of an inode 4429 * @inode: inode 4430 * @ctx: secctx 4431 * @ctxlen: length of secctx 4432 * 4433 * On success, returns 0 and fills out @ctx and @ctxlen with the security 4434 * context for the given @inode. 4435 * 4436 * Return: Returns 0 on success, error on failure. 4437 */ 4438 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 4439 { 4440 return call_int_hook(inode_getsecctx, inode, ctx, ctxlen); 4441 } 4442 EXPORT_SYMBOL(security_inode_getsecctx); 4443 4444 #ifdef CONFIG_WATCH_QUEUE 4445 /** 4446 * security_post_notification() - Check if a watch notification can be posted 4447 * @w_cred: credentials of the task that set the watch 4448 * @cred: credentials of the task which triggered the watch 4449 * @n: the notification 4450 * 4451 * Check to see if a watch notification can be posted to a particular queue. 4452 * 4453 * Return: Returns 0 if permission is granted. 4454 */ 4455 int security_post_notification(const struct cred *w_cred, 4456 const struct cred *cred, 4457 struct watch_notification *n) 4458 { 4459 return call_int_hook(post_notification, w_cred, cred, n); 4460 } 4461 #endif /* CONFIG_WATCH_QUEUE */ 4462 4463 #ifdef CONFIG_KEY_NOTIFICATIONS 4464 /** 4465 * security_watch_key() - Check if a task is allowed to watch for key events 4466 * @key: the key to watch 4467 * 4468 * Check to see if a process is allowed to watch for event notifications from 4469 * a key or keyring. 4470 * 4471 * Return: Returns 0 if permission is granted. 4472 */ 4473 int security_watch_key(struct key *key) 4474 { 4475 return call_int_hook(watch_key, key); 4476 } 4477 #endif /* CONFIG_KEY_NOTIFICATIONS */ 4478 4479 #ifdef CONFIG_SECURITY_NETWORK 4480 /** 4481 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed 4482 * @sock: originating sock 4483 * @other: peer sock 4484 * @newsk: new sock 4485 * 4486 * Check permissions before establishing a Unix domain stream connection 4487 * between @sock and @other. 4488 * 4489 * The @unix_stream_connect and @unix_may_send hooks were necessary because 4490 * Linux provides an alternative to the conventional file name space for Unix 4491 * domain sockets. Whereas binding and connecting to sockets in the file name 4492 * space is mediated by the typical file permissions (and caught by the mknod 4493 * and permission hooks in inode_security_ops), binding and connecting to 4494 * sockets in the abstract name space is completely unmediated. Sufficient 4495 * control of Unix domain sockets in the abstract name space isn't possible 4496 * using only the socket layer hooks, since we need to know the actual target 4497 * socket, which is not looked up until we are inside the af_unix code. 4498 * 4499 * Return: Returns 0 if permission is granted. 4500 */ 4501 int security_unix_stream_connect(struct sock *sock, struct sock *other, 4502 struct sock *newsk) 4503 { 4504 return call_int_hook(unix_stream_connect, sock, other, newsk); 4505 } 4506 EXPORT_SYMBOL(security_unix_stream_connect); 4507 4508 /** 4509 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams 4510 * @sock: originating sock 4511 * @other: peer sock 4512 * 4513 * Check permissions before connecting or sending datagrams from @sock to 4514 * @other. 4515 * 4516 * The @unix_stream_connect and @unix_may_send hooks were necessary because 4517 * Linux provides an alternative to the conventional file name space for Unix 4518 * domain sockets. Whereas binding and connecting to sockets in the file name 4519 * space is mediated by the typical file permissions (and caught by the mknod 4520 * and permission hooks in inode_security_ops), binding and connecting to 4521 * sockets in the abstract name space is completely unmediated. Sufficient 4522 * control of Unix domain sockets in the abstract name space isn't possible 4523 * using only the socket layer hooks, since we need to know the actual target 4524 * socket, which is not looked up until we are inside the af_unix code. 4525 * 4526 * Return: Returns 0 if permission is granted. 4527 */ 4528 int security_unix_may_send(struct socket *sock, struct socket *other) 4529 { 4530 return call_int_hook(unix_may_send, sock, other); 4531 } 4532 EXPORT_SYMBOL(security_unix_may_send); 4533 4534 /** 4535 * security_socket_create() - Check if creating a new socket is allowed 4536 * @family: protocol family 4537 * @type: communications type 4538 * @protocol: requested protocol 4539 * @kern: set to 1 if a kernel socket is requested 4540 * 4541 * Check permissions prior to creating a new socket. 4542 * 4543 * Return: Returns 0 if permission is granted. 4544 */ 4545 int security_socket_create(int family, int type, int protocol, int kern) 4546 { 4547 return call_int_hook(socket_create, family, type, protocol, kern); 4548 } 4549 4550 /** 4551 * security_socket_post_create() - Initialize a newly created socket 4552 * @sock: socket 4553 * @family: protocol family 4554 * @type: communications type 4555 * @protocol: requested protocol 4556 * @kern: set to 1 if a kernel socket is requested 4557 * 4558 * This hook allows a module to update or allocate a per-socket security 4559 * structure. Note that the security field was not added directly to the socket 4560 * structure, but rather, the socket security information is stored in the 4561 * associated inode. Typically, the inode alloc_security hook will allocate 4562 * and attach security information to SOCK_INODE(sock)->i_security. This hook 4563 * may be used to update the SOCK_INODE(sock)->i_security field with additional 4564 * information that wasn't available when the inode was allocated. 4565 * 4566 * Return: Returns 0 if permission is granted. 4567 */ 4568 int security_socket_post_create(struct socket *sock, int family, 4569 int type, int protocol, int kern) 4570 { 4571 return call_int_hook(socket_post_create, sock, family, type, 4572 protocol, kern); 4573 } 4574 4575 /** 4576 * security_socket_socketpair() - Check if creating a socketpair is allowed 4577 * @socka: first socket 4578 * @sockb: second socket 4579 * 4580 * Check permissions before creating a fresh pair of sockets. 4581 * 4582 * Return: Returns 0 if permission is granted and the connection was 4583 * established. 4584 */ 4585 int security_socket_socketpair(struct socket *socka, struct socket *sockb) 4586 { 4587 return call_int_hook(socket_socketpair, socka, sockb); 4588 } 4589 EXPORT_SYMBOL(security_socket_socketpair); 4590 4591 /** 4592 * security_socket_bind() - Check if a socket bind operation is allowed 4593 * @sock: socket 4594 * @address: requested bind address 4595 * @addrlen: length of address 4596 * 4597 * Check permission before socket protocol layer bind operation is performed 4598 * and the socket @sock is bound to the address specified in the @address 4599 * parameter. 4600 * 4601 * Return: Returns 0 if permission is granted. 4602 */ 4603 int security_socket_bind(struct socket *sock, 4604 struct sockaddr *address, int addrlen) 4605 { 4606 return call_int_hook(socket_bind, sock, address, addrlen); 4607 } 4608 4609 /** 4610 * security_socket_connect() - Check if a socket connect operation is allowed 4611 * @sock: socket 4612 * @address: address of remote connection point 4613 * @addrlen: length of address 4614 * 4615 * Check permission before socket protocol layer connect operation attempts to 4616 * connect socket @sock to a remote address, @address. 4617 * 4618 * Return: Returns 0 if permission is granted. 4619 */ 4620 int security_socket_connect(struct socket *sock, 4621 struct sockaddr *address, int addrlen) 4622 { 4623 return call_int_hook(socket_connect, sock, address, addrlen); 4624 } 4625 4626 /** 4627 * security_socket_listen() - Check if a socket is allowed to listen 4628 * @sock: socket 4629 * @backlog: connection queue size 4630 * 4631 * Check permission before socket protocol layer listen operation. 4632 * 4633 * Return: Returns 0 if permission is granted. 4634 */ 4635 int security_socket_listen(struct socket *sock, int backlog) 4636 { 4637 return call_int_hook(socket_listen, sock, backlog); 4638 } 4639 4640 /** 4641 * security_socket_accept() - Check if a socket is allowed to accept connections 4642 * @sock: listening socket 4643 * @newsock: newly creation connection socket 4644 * 4645 * Check permission before accepting a new connection. Note that the new 4646 * socket, @newsock, has been created and some information copied to it, but 4647 * the accept operation has not actually been performed. 4648 * 4649 * Return: Returns 0 if permission is granted. 4650 */ 4651 int security_socket_accept(struct socket *sock, struct socket *newsock) 4652 { 4653 return call_int_hook(socket_accept, sock, newsock); 4654 } 4655 4656 /** 4657 * security_socket_sendmsg() - Check if sending a message is allowed 4658 * @sock: sending socket 4659 * @msg: message to send 4660 * @size: size of message 4661 * 4662 * Check permission before transmitting a message to another socket. 4663 * 4664 * Return: Returns 0 if permission is granted. 4665 */ 4666 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size) 4667 { 4668 return call_int_hook(socket_sendmsg, sock, msg, size); 4669 } 4670 4671 /** 4672 * security_socket_recvmsg() - Check if receiving a message is allowed 4673 * @sock: receiving socket 4674 * @msg: message to receive 4675 * @size: size of message 4676 * @flags: operational flags 4677 * 4678 * Check permission before receiving a message from a socket. 4679 * 4680 * Return: Returns 0 if permission is granted. 4681 */ 4682 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg, 4683 int size, int flags) 4684 { 4685 return call_int_hook(socket_recvmsg, sock, msg, size, flags); 4686 } 4687 4688 /** 4689 * security_socket_getsockname() - Check if reading the socket addr is allowed 4690 * @sock: socket 4691 * 4692 * Check permission before reading the local address (name) of the socket 4693 * object. 4694 * 4695 * Return: Returns 0 if permission is granted. 4696 */ 4697 int security_socket_getsockname(struct socket *sock) 4698 { 4699 return call_int_hook(socket_getsockname, sock); 4700 } 4701 4702 /** 4703 * security_socket_getpeername() - Check if reading the peer's addr is allowed 4704 * @sock: socket 4705 * 4706 * Check permission before the remote address (name) of a socket object. 4707 * 4708 * Return: Returns 0 if permission is granted. 4709 */ 4710 int security_socket_getpeername(struct socket *sock) 4711 { 4712 return call_int_hook(socket_getpeername, sock); 4713 } 4714 4715 /** 4716 * security_socket_getsockopt() - Check if reading a socket option is allowed 4717 * @sock: socket 4718 * @level: option's protocol level 4719 * @optname: option name 4720 * 4721 * Check permissions before retrieving the options associated with socket 4722 * @sock. 4723 * 4724 * Return: Returns 0 if permission is granted. 4725 */ 4726 int security_socket_getsockopt(struct socket *sock, int level, int optname) 4727 { 4728 return call_int_hook(socket_getsockopt, sock, level, optname); 4729 } 4730 4731 /** 4732 * security_socket_setsockopt() - Check if setting a socket option is allowed 4733 * @sock: socket 4734 * @level: option's protocol level 4735 * @optname: option name 4736 * 4737 * Check permissions before setting the options associated with socket @sock. 4738 * 4739 * Return: Returns 0 if permission is granted. 4740 */ 4741 int security_socket_setsockopt(struct socket *sock, int level, int optname) 4742 { 4743 return call_int_hook(socket_setsockopt, sock, level, optname); 4744 } 4745 4746 /** 4747 * security_socket_shutdown() - Checks if shutting down the socket is allowed 4748 * @sock: socket 4749 * @how: flag indicating how sends and receives are handled 4750 * 4751 * Checks permission before all or part of a connection on the socket @sock is 4752 * shut down. 4753 * 4754 * Return: Returns 0 if permission is granted. 4755 */ 4756 int security_socket_shutdown(struct socket *sock, int how) 4757 { 4758 return call_int_hook(socket_shutdown, sock, how); 4759 } 4760 4761 /** 4762 * security_sock_rcv_skb() - Check if an incoming network packet is allowed 4763 * @sk: destination sock 4764 * @skb: incoming packet 4765 * 4766 * Check permissions on incoming network packets. This hook is distinct from 4767 * Netfilter's IP input hooks since it is the first time that the incoming 4768 * sk_buff @skb has been associated with a particular socket, @sk. Must not 4769 * sleep inside this hook because some callers hold spinlocks. 4770 * 4771 * Return: Returns 0 if permission is granted. 4772 */ 4773 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 4774 { 4775 return call_int_hook(socket_sock_rcv_skb, sk, skb); 4776 } 4777 EXPORT_SYMBOL(security_sock_rcv_skb); 4778 4779 /** 4780 * security_socket_getpeersec_stream() - Get the remote peer label 4781 * @sock: socket 4782 * @optval: destination buffer 4783 * @optlen: size of peer label copied into the buffer 4784 * @len: maximum size of the destination buffer 4785 * 4786 * This hook allows the security module to provide peer socket security state 4787 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC. 4788 * For tcp sockets this can be meaningful if the socket is associated with an 4789 * ipsec SA. 4790 * 4791 * Return: Returns 0 if all is well, otherwise, typical getsockopt return 4792 * values. 4793 */ 4794 int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval, 4795 sockptr_t optlen, unsigned int len) 4796 { 4797 return call_int_hook(socket_getpeersec_stream, sock, optval, optlen, 4798 len); 4799 } 4800 4801 /** 4802 * security_socket_getpeersec_dgram() - Get the remote peer label 4803 * @sock: socket 4804 * @skb: datagram packet 4805 * @secid: remote peer label secid 4806 * 4807 * This hook allows the security module to provide peer socket security state 4808 * for udp sockets on a per-packet basis to userspace via getsockopt 4809 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC 4810 * option via getsockopt. It can then retrieve the security state returned by 4811 * this hook for a packet via the SCM_SECURITY ancillary message type. 4812 * 4813 * Return: Returns 0 on success, error on failure. 4814 */ 4815 int security_socket_getpeersec_dgram(struct socket *sock, 4816 struct sk_buff *skb, u32 *secid) 4817 { 4818 return call_int_hook(socket_getpeersec_dgram, sock, skb, secid); 4819 } 4820 EXPORT_SYMBOL(security_socket_getpeersec_dgram); 4821 4822 /** 4823 * lsm_sock_alloc - allocate a composite sock blob 4824 * @sock: the sock that needs a blob 4825 * @gfp: allocation mode 4826 * 4827 * Allocate the sock blob for all the modules 4828 * 4829 * Returns 0, or -ENOMEM if memory can't be allocated. 4830 */ 4831 static int lsm_sock_alloc(struct sock *sock, gfp_t gfp) 4832 { 4833 return lsm_blob_alloc(&sock->sk_security, blob_sizes.lbs_sock, gfp); 4834 } 4835 4836 /** 4837 * security_sk_alloc() - Allocate and initialize a sock's LSM blob 4838 * @sk: sock 4839 * @family: protocol family 4840 * @priority: gfp flags 4841 * 4842 * Allocate and attach a security structure to the sk->sk_security field, which 4843 * is used to copy security attributes between local stream sockets. 4844 * 4845 * Return: Returns 0 on success, error on failure. 4846 */ 4847 int security_sk_alloc(struct sock *sk, int family, gfp_t priority) 4848 { 4849 int rc = lsm_sock_alloc(sk, priority); 4850 4851 if (unlikely(rc)) 4852 return rc; 4853 rc = call_int_hook(sk_alloc_security, sk, family, priority); 4854 if (unlikely(rc)) 4855 security_sk_free(sk); 4856 return rc; 4857 } 4858 4859 /** 4860 * security_sk_free() - Free the sock's LSM blob 4861 * @sk: sock 4862 * 4863 * Deallocate security structure. 4864 */ 4865 void security_sk_free(struct sock *sk) 4866 { 4867 call_void_hook(sk_free_security, sk); 4868 kfree(sk->sk_security); 4869 sk->sk_security = NULL; 4870 } 4871 4872 /** 4873 * security_sk_clone() - Clone a sock's LSM state 4874 * @sk: original sock 4875 * @newsk: target sock 4876 * 4877 * Clone/copy security structure. 4878 */ 4879 void security_sk_clone(const struct sock *sk, struct sock *newsk) 4880 { 4881 call_void_hook(sk_clone_security, sk, newsk); 4882 } 4883 EXPORT_SYMBOL(security_sk_clone); 4884 4885 /** 4886 * security_sk_classify_flow() - Set a flow's secid based on socket 4887 * @sk: original socket 4888 * @flic: target flow 4889 * 4890 * Set the target flow's secid to socket's secid. 4891 */ 4892 void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic) 4893 { 4894 call_void_hook(sk_getsecid, sk, &flic->flowic_secid); 4895 } 4896 EXPORT_SYMBOL(security_sk_classify_flow); 4897 4898 /** 4899 * security_req_classify_flow() - Set a flow's secid based on request_sock 4900 * @req: request_sock 4901 * @flic: target flow 4902 * 4903 * Sets @flic's secid to @req's secid. 4904 */ 4905 void security_req_classify_flow(const struct request_sock *req, 4906 struct flowi_common *flic) 4907 { 4908 call_void_hook(req_classify_flow, req, flic); 4909 } 4910 EXPORT_SYMBOL(security_req_classify_flow); 4911 4912 /** 4913 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket 4914 * @sk: sock being grafted 4915 * @parent: target parent socket 4916 * 4917 * Sets @parent's inode secid to @sk's secid and update @sk with any necessary 4918 * LSM state from @parent. 4919 */ 4920 void security_sock_graft(struct sock *sk, struct socket *parent) 4921 { 4922 call_void_hook(sock_graft, sk, parent); 4923 } 4924 EXPORT_SYMBOL(security_sock_graft); 4925 4926 /** 4927 * security_inet_conn_request() - Set request_sock state using incoming connect 4928 * @sk: parent listening sock 4929 * @skb: incoming connection 4930 * @req: new request_sock 4931 * 4932 * Initialize the @req LSM state based on @sk and the incoming connect in @skb. 4933 * 4934 * Return: Returns 0 if permission is granted. 4935 */ 4936 int security_inet_conn_request(const struct sock *sk, 4937 struct sk_buff *skb, struct request_sock *req) 4938 { 4939 return call_int_hook(inet_conn_request, sk, skb, req); 4940 } 4941 EXPORT_SYMBOL(security_inet_conn_request); 4942 4943 /** 4944 * security_inet_csk_clone() - Set new sock LSM state based on request_sock 4945 * @newsk: new sock 4946 * @req: connection request_sock 4947 * 4948 * Set that LSM state of @sock using the LSM state from @req. 4949 */ 4950 void security_inet_csk_clone(struct sock *newsk, 4951 const struct request_sock *req) 4952 { 4953 call_void_hook(inet_csk_clone, newsk, req); 4954 } 4955 4956 /** 4957 * security_inet_conn_established() - Update sock's LSM state with connection 4958 * @sk: sock 4959 * @skb: connection packet 4960 * 4961 * Update @sock's LSM state to represent a new connection from @skb. 4962 */ 4963 void security_inet_conn_established(struct sock *sk, 4964 struct sk_buff *skb) 4965 { 4966 call_void_hook(inet_conn_established, sk, skb); 4967 } 4968 EXPORT_SYMBOL(security_inet_conn_established); 4969 4970 /** 4971 * security_secmark_relabel_packet() - Check if setting a secmark is allowed 4972 * @secid: new secmark value 4973 * 4974 * Check if the process should be allowed to relabel packets to @secid. 4975 * 4976 * Return: Returns 0 if permission is granted. 4977 */ 4978 int security_secmark_relabel_packet(u32 secid) 4979 { 4980 return call_int_hook(secmark_relabel_packet, secid); 4981 } 4982 EXPORT_SYMBOL(security_secmark_relabel_packet); 4983 4984 /** 4985 * security_secmark_refcount_inc() - Increment the secmark labeling rule count 4986 * 4987 * Tells the LSM to increment the number of secmark labeling rules loaded. 4988 */ 4989 void security_secmark_refcount_inc(void) 4990 { 4991 call_void_hook(secmark_refcount_inc); 4992 } 4993 EXPORT_SYMBOL(security_secmark_refcount_inc); 4994 4995 /** 4996 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count 4997 * 4998 * Tells the LSM to decrement the number of secmark labeling rules loaded. 4999 */ 5000 void security_secmark_refcount_dec(void) 5001 { 5002 call_void_hook(secmark_refcount_dec); 5003 } 5004 EXPORT_SYMBOL(security_secmark_refcount_dec); 5005 5006 /** 5007 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device 5008 * @security: pointer to the LSM blob 5009 * 5010 * This hook allows a module to allocate a security structure for a TUN device, 5011 * returning the pointer in @security. 5012 * 5013 * Return: Returns a zero on success, negative values on failure. 5014 */ 5015 int security_tun_dev_alloc_security(void **security) 5016 { 5017 int rc; 5018 5019 rc = lsm_blob_alloc(security, blob_sizes.lbs_tun_dev, GFP_KERNEL); 5020 if (rc) 5021 return rc; 5022 5023 rc = call_int_hook(tun_dev_alloc_security, *security); 5024 if (rc) { 5025 kfree(*security); 5026 *security = NULL; 5027 } 5028 return rc; 5029 } 5030 EXPORT_SYMBOL(security_tun_dev_alloc_security); 5031 5032 /** 5033 * security_tun_dev_free_security() - Free a TUN device LSM blob 5034 * @security: LSM blob 5035 * 5036 * This hook allows a module to free the security structure for a TUN device. 5037 */ 5038 void security_tun_dev_free_security(void *security) 5039 { 5040 kfree(security); 5041 } 5042 EXPORT_SYMBOL(security_tun_dev_free_security); 5043 5044 /** 5045 * security_tun_dev_create() - Check if creating a TUN device is allowed 5046 * 5047 * Check permissions prior to creating a new TUN device. 5048 * 5049 * Return: Returns 0 if permission is granted. 5050 */ 5051 int security_tun_dev_create(void) 5052 { 5053 return call_int_hook(tun_dev_create); 5054 } 5055 EXPORT_SYMBOL(security_tun_dev_create); 5056 5057 /** 5058 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed 5059 * @security: TUN device LSM blob 5060 * 5061 * Check permissions prior to attaching to a TUN device queue. 5062 * 5063 * Return: Returns 0 if permission is granted. 5064 */ 5065 int security_tun_dev_attach_queue(void *security) 5066 { 5067 return call_int_hook(tun_dev_attach_queue, security); 5068 } 5069 EXPORT_SYMBOL(security_tun_dev_attach_queue); 5070 5071 /** 5072 * security_tun_dev_attach() - Update TUN device LSM state on attach 5073 * @sk: associated sock 5074 * @security: TUN device LSM blob 5075 * 5076 * This hook can be used by the module to update any security state associated 5077 * with the TUN device's sock structure. 5078 * 5079 * Return: Returns 0 if permission is granted. 5080 */ 5081 int security_tun_dev_attach(struct sock *sk, void *security) 5082 { 5083 return call_int_hook(tun_dev_attach, sk, security); 5084 } 5085 EXPORT_SYMBOL(security_tun_dev_attach); 5086 5087 /** 5088 * security_tun_dev_open() - Update TUN device LSM state on open 5089 * @security: TUN device LSM blob 5090 * 5091 * This hook can be used by the module to update any security state associated 5092 * with the TUN device's security structure. 5093 * 5094 * Return: Returns 0 if permission is granted. 5095 */ 5096 int security_tun_dev_open(void *security) 5097 { 5098 return call_int_hook(tun_dev_open, security); 5099 } 5100 EXPORT_SYMBOL(security_tun_dev_open); 5101 5102 /** 5103 * security_sctp_assoc_request() - Update the LSM on a SCTP association req 5104 * @asoc: SCTP association 5105 * @skb: packet requesting the association 5106 * 5107 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM. 5108 * 5109 * Return: Returns 0 on success, error on failure. 5110 */ 5111 int security_sctp_assoc_request(struct sctp_association *asoc, 5112 struct sk_buff *skb) 5113 { 5114 return call_int_hook(sctp_assoc_request, asoc, skb); 5115 } 5116 EXPORT_SYMBOL(security_sctp_assoc_request); 5117 5118 /** 5119 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option 5120 * @sk: socket 5121 * @optname: SCTP option to validate 5122 * @address: list of IP addresses to validate 5123 * @addrlen: length of the address list 5124 * 5125 * Validiate permissions required for each address associated with sock @sk. 5126 * Depending on @optname, the addresses will be treated as either a connect or 5127 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using 5128 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6). 5129 * 5130 * Return: Returns 0 on success, error on failure. 5131 */ 5132 int security_sctp_bind_connect(struct sock *sk, int optname, 5133 struct sockaddr *address, int addrlen) 5134 { 5135 return call_int_hook(sctp_bind_connect, sk, optname, address, addrlen); 5136 } 5137 EXPORT_SYMBOL(security_sctp_bind_connect); 5138 5139 /** 5140 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state 5141 * @asoc: SCTP association 5142 * @sk: original sock 5143 * @newsk: target sock 5144 * 5145 * Called whenever a new socket is created by accept(2) (i.e. a TCP style 5146 * socket) or when a socket is 'peeled off' e.g userspace calls 5147 * sctp_peeloff(3). 5148 */ 5149 void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk, 5150 struct sock *newsk) 5151 { 5152 call_void_hook(sctp_sk_clone, asoc, sk, newsk); 5153 } 5154 EXPORT_SYMBOL(security_sctp_sk_clone); 5155 5156 /** 5157 * security_sctp_assoc_established() - Update LSM state when assoc established 5158 * @asoc: SCTP association 5159 * @skb: packet establishing the association 5160 * 5161 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the 5162 * security module. 5163 * 5164 * Return: Returns 0 if permission is granted. 5165 */ 5166 int security_sctp_assoc_established(struct sctp_association *asoc, 5167 struct sk_buff *skb) 5168 { 5169 return call_int_hook(sctp_assoc_established, asoc, skb); 5170 } 5171 EXPORT_SYMBOL(security_sctp_assoc_established); 5172 5173 /** 5174 * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket 5175 * @sk: the owning MPTCP socket 5176 * @ssk: the new subflow 5177 * 5178 * Update the labeling for the given MPTCP subflow, to match the one of the 5179 * owning MPTCP socket. This hook has to be called after the socket creation and 5180 * initialization via the security_socket_create() and 5181 * security_socket_post_create() LSM hooks. 5182 * 5183 * Return: Returns 0 on success or a negative error code on failure. 5184 */ 5185 int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk) 5186 { 5187 return call_int_hook(mptcp_add_subflow, sk, ssk); 5188 } 5189 5190 #endif /* CONFIG_SECURITY_NETWORK */ 5191 5192 #ifdef CONFIG_SECURITY_INFINIBAND 5193 /** 5194 * security_ib_pkey_access() - Check if access to an IB pkey is allowed 5195 * @sec: LSM blob 5196 * @subnet_prefix: subnet prefix of the port 5197 * @pkey: IB pkey 5198 * 5199 * Check permission to access a pkey when modifying a QP. 5200 * 5201 * Return: Returns 0 if permission is granted. 5202 */ 5203 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey) 5204 { 5205 return call_int_hook(ib_pkey_access, sec, subnet_prefix, pkey); 5206 } 5207 EXPORT_SYMBOL(security_ib_pkey_access); 5208 5209 /** 5210 * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed 5211 * @sec: LSM blob 5212 * @dev_name: IB device name 5213 * @port_num: port number 5214 * 5215 * Check permissions to send and receive SMPs on a end port. 5216 * 5217 * Return: Returns 0 if permission is granted. 5218 */ 5219 int security_ib_endport_manage_subnet(void *sec, 5220 const char *dev_name, u8 port_num) 5221 { 5222 return call_int_hook(ib_endport_manage_subnet, sec, dev_name, port_num); 5223 } 5224 EXPORT_SYMBOL(security_ib_endport_manage_subnet); 5225 5226 /** 5227 * security_ib_alloc_security() - Allocate an Infiniband LSM blob 5228 * @sec: LSM blob 5229 * 5230 * Allocate a security structure for Infiniband objects. 5231 * 5232 * Return: Returns 0 on success, non-zero on failure. 5233 */ 5234 int security_ib_alloc_security(void **sec) 5235 { 5236 int rc; 5237 5238 rc = lsm_blob_alloc(sec, blob_sizes.lbs_ib, GFP_KERNEL); 5239 if (rc) 5240 return rc; 5241 5242 rc = call_int_hook(ib_alloc_security, *sec); 5243 if (rc) { 5244 kfree(*sec); 5245 *sec = NULL; 5246 } 5247 return rc; 5248 } 5249 EXPORT_SYMBOL(security_ib_alloc_security); 5250 5251 /** 5252 * security_ib_free_security() - Free an Infiniband LSM blob 5253 * @sec: LSM blob 5254 * 5255 * Deallocate an Infiniband security structure. 5256 */ 5257 void security_ib_free_security(void *sec) 5258 { 5259 kfree(sec); 5260 } 5261 EXPORT_SYMBOL(security_ib_free_security); 5262 #endif /* CONFIG_SECURITY_INFINIBAND */ 5263 5264 #ifdef CONFIG_SECURITY_NETWORK_XFRM 5265 /** 5266 * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob 5267 * @ctxp: xfrm security context being added to the SPD 5268 * @sec_ctx: security label provided by userspace 5269 * @gfp: gfp flags 5270 * 5271 * Allocate a security structure to the xp->security field; the security field 5272 * is initialized to NULL when the xfrm_policy is allocated. 5273 * 5274 * Return: Return 0 if operation was successful. 5275 */ 5276 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp, 5277 struct xfrm_user_sec_ctx *sec_ctx, 5278 gfp_t gfp) 5279 { 5280 return call_int_hook(xfrm_policy_alloc_security, ctxp, sec_ctx, gfp); 5281 } 5282 EXPORT_SYMBOL(security_xfrm_policy_alloc); 5283 5284 /** 5285 * security_xfrm_policy_clone() - Clone xfrm policy LSM state 5286 * @old_ctx: xfrm security context 5287 * @new_ctxp: target xfrm security context 5288 * 5289 * Allocate a security structure in new_ctxp that contains the information from 5290 * the old_ctx structure. 5291 * 5292 * Return: Return 0 if operation was successful. 5293 */ 5294 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx, 5295 struct xfrm_sec_ctx **new_ctxp) 5296 { 5297 return call_int_hook(xfrm_policy_clone_security, old_ctx, new_ctxp); 5298 } 5299 5300 /** 5301 * security_xfrm_policy_free() - Free a xfrm security context 5302 * @ctx: xfrm security context 5303 * 5304 * Free LSM resources associated with @ctx. 5305 */ 5306 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx) 5307 { 5308 call_void_hook(xfrm_policy_free_security, ctx); 5309 } 5310 EXPORT_SYMBOL(security_xfrm_policy_free); 5311 5312 /** 5313 * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed 5314 * @ctx: xfrm security context 5315 * 5316 * Authorize deletion of a SPD entry. 5317 * 5318 * Return: Returns 0 if permission is granted. 5319 */ 5320 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx) 5321 { 5322 return call_int_hook(xfrm_policy_delete_security, ctx); 5323 } 5324 5325 /** 5326 * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob 5327 * @x: xfrm state being added to the SAD 5328 * @sec_ctx: security label provided by userspace 5329 * 5330 * Allocate a security structure to the @x->security field; the security field 5331 * is initialized to NULL when the xfrm_state is allocated. Set the context to 5332 * correspond to @sec_ctx. 5333 * 5334 * Return: Return 0 if operation was successful. 5335 */ 5336 int security_xfrm_state_alloc(struct xfrm_state *x, 5337 struct xfrm_user_sec_ctx *sec_ctx) 5338 { 5339 return call_int_hook(xfrm_state_alloc, x, sec_ctx); 5340 } 5341 EXPORT_SYMBOL(security_xfrm_state_alloc); 5342 5343 /** 5344 * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob 5345 * @x: xfrm state being added to the SAD 5346 * @polsec: associated policy's security context 5347 * @secid: secid from the flow 5348 * 5349 * Allocate a security structure to the x->security field; the security field 5350 * is initialized to NULL when the xfrm_state is allocated. Set the context to 5351 * correspond to secid. 5352 * 5353 * Return: Returns 0 if operation was successful. 5354 */ 5355 int security_xfrm_state_alloc_acquire(struct xfrm_state *x, 5356 struct xfrm_sec_ctx *polsec, u32 secid) 5357 { 5358 return call_int_hook(xfrm_state_alloc_acquire, x, polsec, secid); 5359 } 5360 5361 /** 5362 * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed 5363 * @x: xfrm state 5364 * 5365 * Authorize deletion of x->security. 5366 * 5367 * Return: Returns 0 if permission is granted. 5368 */ 5369 int security_xfrm_state_delete(struct xfrm_state *x) 5370 { 5371 return call_int_hook(xfrm_state_delete_security, x); 5372 } 5373 EXPORT_SYMBOL(security_xfrm_state_delete); 5374 5375 /** 5376 * security_xfrm_state_free() - Free a xfrm state 5377 * @x: xfrm state 5378 * 5379 * Deallocate x->security. 5380 */ 5381 void security_xfrm_state_free(struct xfrm_state *x) 5382 { 5383 call_void_hook(xfrm_state_free_security, x); 5384 } 5385 5386 /** 5387 * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed 5388 * @ctx: target xfrm security context 5389 * @fl_secid: flow secid used to authorize access 5390 * 5391 * Check permission when a flow selects a xfrm_policy for processing XFRMs on a 5392 * packet. The hook is called when selecting either a per-socket policy or a 5393 * generic xfrm policy. 5394 * 5395 * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on 5396 * other errors. 5397 */ 5398 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid) 5399 { 5400 return call_int_hook(xfrm_policy_lookup, ctx, fl_secid); 5401 } 5402 5403 /** 5404 * security_xfrm_state_pol_flow_match() - Check for a xfrm match 5405 * @x: xfrm state to match 5406 * @xp: xfrm policy to check for a match 5407 * @flic: flow to check for a match. 5408 * 5409 * Check @xp and @flic for a match with @x. 5410 * 5411 * Return: Returns 1 if there is a match. 5412 */ 5413 int security_xfrm_state_pol_flow_match(struct xfrm_state *x, 5414 struct xfrm_policy *xp, 5415 const struct flowi_common *flic) 5416 { 5417 struct lsm_static_call *scall; 5418 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match); 5419 5420 /* 5421 * Since this function is expected to return 0 or 1, the judgment 5422 * becomes difficult if multiple LSMs supply this call. Fortunately, 5423 * we can use the first LSM's judgment because currently only SELinux 5424 * supplies this call. 5425 * 5426 * For speed optimization, we explicitly break the loop rather than 5427 * using the macro 5428 */ 5429 lsm_for_each_hook(scall, xfrm_state_pol_flow_match) { 5430 rc = scall->hl->hook.xfrm_state_pol_flow_match(x, xp, flic); 5431 break; 5432 } 5433 return rc; 5434 } 5435 5436 /** 5437 * security_xfrm_decode_session() - Determine the xfrm secid for a packet 5438 * @skb: xfrm packet 5439 * @secid: secid 5440 * 5441 * Decode the packet in @skb and return the security label in @secid. 5442 * 5443 * Return: Return 0 if all xfrms used have the same secid. 5444 */ 5445 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid) 5446 { 5447 return call_int_hook(xfrm_decode_session, skb, secid, 1); 5448 } 5449 5450 void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic) 5451 { 5452 int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid, 5453 0); 5454 5455 BUG_ON(rc); 5456 } 5457 EXPORT_SYMBOL(security_skb_classify_flow); 5458 #endif /* CONFIG_SECURITY_NETWORK_XFRM */ 5459 5460 #ifdef CONFIG_KEYS 5461 /** 5462 * security_key_alloc() - Allocate and initialize a kernel key LSM blob 5463 * @key: key 5464 * @cred: credentials 5465 * @flags: allocation flags 5466 * 5467 * Permit allocation of a key and assign security data. Note that key does not 5468 * have a serial number assigned at this point. 5469 * 5470 * Return: Return 0 if permission is granted, -ve error otherwise. 5471 */ 5472 int security_key_alloc(struct key *key, const struct cred *cred, 5473 unsigned long flags) 5474 { 5475 int rc = lsm_key_alloc(key); 5476 5477 if (unlikely(rc)) 5478 return rc; 5479 rc = call_int_hook(key_alloc, key, cred, flags); 5480 if (unlikely(rc)) 5481 security_key_free(key); 5482 return rc; 5483 } 5484 5485 /** 5486 * security_key_free() - Free a kernel key LSM blob 5487 * @key: key 5488 * 5489 * Notification of destruction; free security data. 5490 */ 5491 void security_key_free(struct key *key) 5492 { 5493 kfree(key->security); 5494 key->security = NULL; 5495 } 5496 5497 /** 5498 * security_key_permission() - Check if a kernel key operation is allowed 5499 * @key_ref: key reference 5500 * @cred: credentials of actor requesting access 5501 * @need_perm: requested permissions 5502 * 5503 * See whether a specific operational right is granted to a process on a key. 5504 * 5505 * Return: Return 0 if permission is granted, -ve error otherwise. 5506 */ 5507 int security_key_permission(key_ref_t key_ref, const struct cred *cred, 5508 enum key_need_perm need_perm) 5509 { 5510 return call_int_hook(key_permission, key_ref, cred, need_perm); 5511 } 5512 5513 /** 5514 * security_key_getsecurity() - Get the key's security label 5515 * @key: key 5516 * @buffer: security label buffer 5517 * 5518 * Get a textual representation of the security context attached to a key for 5519 * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the 5520 * storage for the NUL-terminated string and the caller should free it. 5521 * 5522 * Return: Returns the length of @buffer (including terminating NUL) or -ve if 5523 * an error occurs. May also return 0 (and a NULL buffer pointer) if 5524 * there is no security label assigned to the key. 5525 */ 5526 int security_key_getsecurity(struct key *key, char **buffer) 5527 { 5528 *buffer = NULL; 5529 return call_int_hook(key_getsecurity, key, buffer); 5530 } 5531 5532 /** 5533 * security_key_post_create_or_update() - Notification of key create or update 5534 * @keyring: keyring to which the key is linked to 5535 * @key: created or updated key 5536 * @payload: data used to instantiate or update the key 5537 * @payload_len: length of payload 5538 * @flags: key flags 5539 * @create: flag indicating whether the key was created or updated 5540 * 5541 * Notify the caller of a key creation or update. 5542 */ 5543 void security_key_post_create_or_update(struct key *keyring, struct key *key, 5544 const void *payload, size_t payload_len, 5545 unsigned long flags, bool create) 5546 { 5547 call_void_hook(key_post_create_or_update, keyring, key, payload, 5548 payload_len, flags, create); 5549 } 5550 #endif /* CONFIG_KEYS */ 5551 5552 #ifdef CONFIG_AUDIT 5553 /** 5554 * security_audit_rule_init() - Allocate and init an LSM audit rule struct 5555 * @field: audit action 5556 * @op: rule operator 5557 * @rulestr: rule context 5558 * @lsmrule: receive buffer for audit rule struct 5559 * @gfp: GFP flag used for kmalloc 5560 * 5561 * Allocate and initialize an LSM audit rule structure. 5562 * 5563 * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of 5564 * an invalid rule. 5565 */ 5566 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule, 5567 gfp_t gfp) 5568 { 5569 return call_int_hook(audit_rule_init, field, op, rulestr, lsmrule, gfp); 5570 } 5571 5572 /** 5573 * security_audit_rule_known() - Check if an audit rule contains LSM fields 5574 * @krule: audit rule 5575 * 5576 * Specifies whether given @krule contains any fields related to the current 5577 * LSM. 5578 * 5579 * Return: Returns 1 in case of relation found, 0 otherwise. 5580 */ 5581 int security_audit_rule_known(struct audit_krule *krule) 5582 { 5583 return call_int_hook(audit_rule_known, krule); 5584 } 5585 5586 /** 5587 * security_audit_rule_free() - Free an LSM audit rule struct 5588 * @lsmrule: audit rule struct 5589 * 5590 * Deallocate the LSM audit rule structure previously allocated by 5591 * audit_rule_init(). 5592 */ 5593 void security_audit_rule_free(void *lsmrule) 5594 { 5595 call_void_hook(audit_rule_free, lsmrule); 5596 } 5597 5598 /** 5599 * security_audit_rule_match() - Check if a label matches an audit rule 5600 * @secid: security label 5601 * @field: LSM audit field 5602 * @op: matching operator 5603 * @lsmrule: audit rule 5604 * 5605 * Determine if given @secid matches a rule previously approved by 5606 * security_audit_rule_known(). 5607 * 5608 * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on 5609 * failure. 5610 */ 5611 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule) 5612 { 5613 return call_int_hook(audit_rule_match, secid, field, op, lsmrule); 5614 } 5615 #endif /* CONFIG_AUDIT */ 5616 5617 #ifdef CONFIG_BPF_SYSCALL 5618 /** 5619 * security_bpf() - Check if the bpf syscall operation is allowed 5620 * @cmd: command 5621 * @attr: bpf attribute 5622 * @size: size 5623 * 5624 * Do a initial check for all bpf syscalls after the attribute is copied into 5625 * the kernel. The actual security module can implement their own rules to 5626 * check the specific cmd they need. 5627 * 5628 * Return: Returns 0 if permission is granted. 5629 */ 5630 int security_bpf(int cmd, union bpf_attr *attr, unsigned int size) 5631 { 5632 return call_int_hook(bpf, cmd, attr, size); 5633 } 5634 5635 /** 5636 * security_bpf_map() - Check if access to a bpf map is allowed 5637 * @map: bpf map 5638 * @fmode: mode 5639 * 5640 * Do a check when the kernel generates and returns a file descriptor for eBPF 5641 * maps. 5642 * 5643 * Return: Returns 0 if permission is granted. 5644 */ 5645 int security_bpf_map(struct bpf_map *map, fmode_t fmode) 5646 { 5647 return call_int_hook(bpf_map, map, fmode); 5648 } 5649 5650 /** 5651 * security_bpf_prog() - Check if access to a bpf program is allowed 5652 * @prog: bpf program 5653 * 5654 * Do a check when the kernel generates and returns a file descriptor for eBPF 5655 * programs. 5656 * 5657 * Return: Returns 0 if permission is granted. 5658 */ 5659 int security_bpf_prog(struct bpf_prog *prog) 5660 { 5661 return call_int_hook(bpf_prog, prog); 5662 } 5663 5664 /** 5665 * security_bpf_map_create() - Check if BPF map creation is allowed 5666 * @map: BPF map object 5667 * @attr: BPF syscall attributes used to create BPF map 5668 * @token: BPF token used to grant user access 5669 * 5670 * Do a check when the kernel creates a new BPF map. This is also the 5671 * point where LSM blob is allocated for LSMs that need them. 5672 * 5673 * Return: Returns 0 on success, error on failure. 5674 */ 5675 int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr, 5676 struct bpf_token *token) 5677 { 5678 return call_int_hook(bpf_map_create, map, attr, token); 5679 } 5680 5681 /** 5682 * security_bpf_prog_load() - Check if loading of BPF program is allowed 5683 * @prog: BPF program object 5684 * @attr: BPF syscall attributes used to create BPF program 5685 * @token: BPF token used to grant user access to BPF subsystem 5686 * 5687 * Perform an access control check when the kernel loads a BPF program and 5688 * allocates associated BPF program object. This hook is also responsible for 5689 * allocating any required LSM state for the BPF program. 5690 * 5691 * Return: Returns 0 on success, error on failure. 5692 */ 5693 int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr, 5694 struct bpf_token *token) 5695 { 5696 return call_int_hook(bpf_prog_load, prog, attr, token); 5697 } 5698 5699 /** 5700 * security_bpf_token_create() - Check if creating of BPF token is allowed 5701 * @token: BPF token object 5702 * @attr: BPF syscall attributes used to create BPF token 5703 * @path: path pointing to BPF FS mount point from which BPF token is created 5704 * 5705 * Do a check when the kernel instantiates a new BPF token object from BPF FS 5706 * instance. This is also the point where LSM blob can be allocated for LSMs. 5707 * 5708 * Return: Returns 0 on success, error on failure. 5709 */ 5710 int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr, 5711 struct path *path) 5712 { 5713 return call_int_hook(bpf_token_create, token, attr, path); 5714 } 5715 5716 /** 5717 * security_bpf_token_cmd() - Check if BPF token is allowed to delegate 5718 * requested BPF syscall command 5719 * @token: BPF token object 5720 * @cmd: BPF syscall command requested to be delegated by BPF token 5721 * 5722 * Do a check when the kernel decides whether provided BPF token should allow 5723 * delegation of requested BPF syscall command. 5724 * 5725 * Return: Returns 0 on success, error on failure. 5726 */ 5727 int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd) 5728 { 5729 return call_int_hook(bpf_token_cmd, token, cmd); 5730 } 5731 5732 /** 5733 * security_bpf_token_capable() - Check if BPF token is allowed to delegate 5734 * requested BPF-related capability 5735 * @token: BPF token object 5736 * @cap: capabilities requested to be delegated by BPF token 5737 * 5738 * Do a check when the kernel decides whether provided BPF token should allow 5739 * delegation of requested BPF-related capabilities. 5740 * 5741 * Return: Returns 0 on success, error on failure. 5742 */ 5743 int security_bpf_token_capable(const struct bpf_token *token, int cap) 5744 { 5745 return call_int_hook(bpf_token_capable, token, cap); 5746 } 5747 5748 /** 5749 * security_bpf_map_free() - Free a bpf map's LSM blob 5750 * @map: bpf map 5751 * 5752 * Clean up the security information stored inside bpf map. 5753 */ 5754 void security_bpf_map_free(struct bpf_map *map) 5755 { 5756 call_void_hook(bpf_map_free, map); 5757 } 5758 5759 /** 5760 * security_bpf_prog_free() - Free a BPF program's LSM blob 5761 * @prog: BPF program struct 5762 * 5763 * Clean up the security information stored inside BPF program. 5764 */ 5765 void security_bpf_prog_free(struct bpf_prog *prog) 5766 { 5767 call_void_hook(bpf_prog_free, prog); 5768 } 5769 5770 /** 5771 * security_bpf_token_free() - Free a BPF token's LSM blob 5772 * @token: BPF token struct 5773 * 5774 * Clean up the security information stored inside BPF token. 5775 */ 5776 void security_bpf_token_free(struct bpf_token *token) 5777 { 5778 call_void_hook(bpf_token_free, token); 5779 } 5780 #endif /* CONFIG_BPF_SYSCALL */ 5781 5782 /** 5783 * security_locked_down() - Check if a kernel feature is allowed 5784 * @what: requested kernel feature 5785 * 5786 * Determine whether a kernel feature that potentially enables arbitrary code 5787 * execution in kernel space should be permitted. 5788 * 5789 * Return: Returns 0 if permission is granted. 5790 */ 5791 int security_locked_down(enum lockdown_reason what) 5792 { 5793 return call_int_hook(locked_down, what); 5794 } 5795 EXPORT_SYMBOL(security_locked_down); 5796 5797 /** 5798 * security_bdev_alloc() - Allocate a block device LSM blob 5799 * @bdev: block device 5800 * 5801 * Allocate and attach a security structure to @bdev->bd_security. The 5802 * security field is initialized to NULL when the bdev structure is 5803 * allocated. 5804 * 5805 * Return: Return 0 if operation was successful. 5806 */ 5807 int security_bdev_alloc(struct block_device *bdev) 5808 { 5809 int rc = 0; 5810 5811 rc = lsm_bdev_alloc(bdev); 5812 if (unlikely(rc)) 5813 return rc; 5814 5815 rc = call_int_hook(bdev_alloc_security, bdev); 5816 if (unlikely(rc)) 5817 security_bdev_free(bdev); 5818 5819 return rc; 5820 } 5821 EXPORT_SYMBOL(security_bdev_alloc); 5822 5823 /** 5824 * security_bdev_free() - Free a block device's LSM blob 5825 * @bdev: block device 5826 * 5827 * Deallocate the bdev security structure and set @bdev->bd_security to NULL. 5828 */ 5829 void security_bdev_free(struct block_device *bdev) 5830 { 5831 if (!bdev->bd_security) 5832 return; 5833 5834 call_void_hook(bdev_free_security, bdev); 5835 5836 kfree(bdev->bd_security); 5837 bdev->bd_security = NULL; 5838 } 5839 EXPORT_SYMBOL(security_bdev_free); 5840 5841 /** 5842 * security_bdev_setintegrity() - Set the device's integrity data 5843 * @bdev: block device 5844 * @type: type of integrity, e.g. hash digest, signature, etc 5845 * @value: the integrity value 5846 * @size: size of the integrity value 5847 * 5848 * Register a verified integrity measurement of a bdev with LSMs. 5849 * LSMs should free the previously saved data if @value is NULL. 5850 * Please note that the new hook should be invoked every time the security 5851 * information is updated to keep these data current. For example, in dm-verity, 5852 * if the mapping table is reloaded and configured to use a different dm-verity 5853 * target with a new roothash and signing information, the previously stored 5854 * data in the LSM blob will become obsolete. It is crucial to re-invoke the 5855 * hook to refresh these data and ensure they are up to date. This necessity 5856 * arises from the design of device-mapper, where a device-mapper device is 5857 * first created, and then targets are subsequently loaded into it. These 5858 * targets can be modified multiple times during the device's lifetime. 5859 * Therefore, while the LSM blob is allocated during the creation of the block 5860 * device, its actual contents are not initialized at this stage and can change 5861 * substantially over time. This includes alterations from data that the LSMs 5862 * 'trusts' to those they do not, making it essential to handle these changes 5863 * correctly. Failure to address this dynamic aspect could potentially allow 5864 * for bypassing LSM checks. 5865 * 5866 * Return: Returns 0 on success, negative values on failure. 5867 */ 5868 int security_bdev_setintegrity(struct block_device *bdev, 5869 enum lsm_integrity_type type, const void *value, 5870 size_t size) 5871 { 5872 return call_int_hook(bdev_setintegrity, bdev, type, value, size); 5873 } 5874 EXPORT_SYMBOL(security_bdev_setintegrity); 5875 5876 #ifdef CONFIG_PERF_EVENTS 5877 /** 5878 * security_perf_event_open() - Check if a perf event open is allowed 5879 * @attr: perf event attribute 5880 * @type: type of event 5881 * 5882 * Check whether the @type of perf_event_open syscall is allowed. 5883 * 5884 * Return: Returns 0 if permission is granted. 5885 */ 5886 int security_perf_event_open(struct perf_event_attr *attr, int type) 5887 { 5888 return call_int_hook(perf_event_open, attr, type); 5889 } 5890 5891 /** 5892 * security_perf_event_alloc() - Allocate a perf event LSM blob 5893 * @event: perf event 5894 * 5895 * Allocate and save perf_event security info. 5896 * 5897 * Return: Returns 0 on success, error on failure. 5898 */ 5899 int security_perf_event_alloc(struct perf_event *event) 5900 { 5901 int rc; 5902 5903 rc = lsm_blob_alloc(&event->security, blob_sizes.lbs_perf_event, 5904 GFP_KERNEL); 5905 if (rc) 5906 return rc; 5907 5908 rc = call_int_hook(perf_event_alloc, event); 5909 if (rc) { 5910 kfree(event->security); 5911 event->security = NULL; 5912 } 5913 return rc; 5914 } 5915 5916 /** 5917 * security_perf_event_free() - Free a perf event LSM blob 5918 * @event: perf event 5919 * 5920 * Release (free) perf_event security info. 5921 */ 5922 void security_perf_event_free(struct perf_event *event) 5923 { 5924 kfree(event->security); 5925 event->security = NULL; 5926 } 5927 5928 /** 5929 * security_perf_event_read() - Check if reading a perf event label is allowed 5930 * @event: perf event 5931 * 5932 * Read perf_event security info if allowed. 5933 * 5934 * Return: Returns 0 if permission is granted. 5935 */ 5936 int security_perf_event_read(struct perf_event *event) 5937 { 5938 return call_int_hook(perf_event_read, event); 5939 } 5940 5941 /** 5942 * security_perf_event_write() - Check if writing a perf event label is allowed 5943 * @event: perf event 5944 * 5945 * Write perf_event security info if allowed. 5946 * 5947 * Return: Returns 0 if permission is granted. 5948 */ 5949 int security_perf_event_write(struct perf_event *event) 5950 { 5951 return call_int_hook(perf_event_write, event); 5952 } 5953 #endif /* CONFIG_PERF_EVENTS */ 5954 5955 #ifdef CONFIG_IO_URING 5956 /** 5957 * security_uring_override_creds() - Check if overriding creds is allowed 5958 * @new: new credentials 5959 * 5960 * Check if the current task, executing an io_uring operation, is allowed to 5961 * override it's credentials with @new. 5962 * 5963 * Return: Returns 0 if permission is granted. 5964 */ 5965 int security_uring_override_creds(const struct cred *new) 5966 { 5967 return call_int_hook(uring_override_creds, new); 5968 } 5969 5970 /** 5971 * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed 5972 * 5973 * Check whether the current task is allowed to spawn a io_uring polling thread 5974 * (IORING_SETUP_SQPOLL). 5975 * 5976 * Return: Returns 0 if permission is granted. 5977 */ 5978 int security_uring_sqpoll(void) 5979 { 5980 return call_int_hook(uring_sqpoll); 5981 } 5982 5983 /** 5984 * security_uring_cmd() - Check if a io_uring passthrough command is allowed 5985 * @ioucmd: command 5986 * 5987 * Check whether the file_operations uring_cmd is allowed to run. 5988 * 5989 * Return: Returns 0 if permission is granted. 5990 */ 5991 int security_uring_cmd(struct io_uring_cmd *ioucmd) 5992 { 5993 return call_int_hook(uring_cmd, ioucmd); 5994 } 5995 #endif /* CONFIG_IO_URING */ 5996 5997 /** 5998 * security_initramfs_populated() - Notify LSMs that initramfs has been loaded 5999 * 6000 * Tells the LSMs the initramfs has been unpacked into the rootfs. 6001 */ 6002 void security_initramfs_populated(void) 6003 { 6004 call_void_hook(initramfs_populated); 6005 } 6006