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