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