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