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