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