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