]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/security.c
UBUNTU: SAUCE: LSM stacking: LSM: Manage remaining security blobs
[mirror_ubuntu-bionic-kernel.git] / security / security.c
CommitLineData
1da177e4
LT
1/*
2 * Security plug functions
3 *
4 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
d291f1a6 7 * Copyright (C) 2016 Mellanox Technologies
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
afdb09c7 15#include <linux/bpf.h>
c59ede7b 16#include <linux/capability.h>
d47be3df 17#include <linux/dcache.h>
1da177e4
LT
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
3c4ed7bd 21#include <linux/lsm_hooks.h>
f381c272 22#include <linux/integrity.h>
6c21a7fb 23#include <linux/ima.h>
3e1be52d 24#include <linux/evm.h>
40401530 25#include <linux/fsnotify.h>
8b3ec681
AV
26#include <linux/mman.h>
27#include <linux/mount.h>
28#include <linux/personality.h>
75331a59 29#include <linux/backing-dev.h>
3bb857e4 30#include <linux/string.h>
7a90fc39 31#include <linux/msg.h>
40401530 32#include <net/flow.h>
7a90fc39 33#include <net/sock.h>
1da177e4 34
823eb1cc 35#define MAX_LSM_EVM_XATTR 2
1da177e4 36
b1d9e6b0
CS
37/* Maximum number of letters for an LSM name string */
38#define SECURITY_NAME_MAX 10
39
3dfc9b02 40struct security_hook_heads security_hook_heads __lsm_ro_after_init;
8f408ab6
DJ
41static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
42
ab9b110b 43static struct kmem_cache *lsm_file_cache;
7a90fc39 44static struct kmem_cache *lsm_inode_cache;
ab9b110b 45
d69dece5 46char *lsm_names;
f1efc8c4
CS
47static struct lsm_blob_sizes blob_sizes;
48
076c54c5 49/* Boot-time LSM user choice */
6e65f92f
JJ
50static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] =
51 CONFIG_DEFAULT_SECURITY;
1da177e4 52
1da177e4
LT
53static void __init do_security_initcalls(void)
54{
55 initcall_t *call;
56 call = __security_initcall_start;
57 while (call < __security_initcall_end) {
58 (*call) ();
59 call++;
60 }
61}
62
63/**
64 * security_init - initializes the security framework
65 *
66 * This should be called early in the kernel initialization sequence.
67 */
68int __init security_init(void)
69{
3dfc9b02
TH
70 int i;
71 struct list_head *list = (struct list_head *) &security_hook_heads;
72
73 for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct list_head);
74 i++)
75 INIT_LIST_HEAD(&list[i]);
b1d9e6b0 76 pr_info("Security Framework initialized\n");
1da177e4 77
b1d9e6b0 78 /*
730daa16 79 * Load minor LSMs, with the capability module always first.
b1d9e6b0
CS
80 */
81 capability_add_hooks();
b1d9e6b0 82 yama_add_hooks();
9b091556 83 loadpin_add_hooks();
730daa16 84
b1d9e6b0 85 /*
f1efc8c4
CS
86 * The first call to a module specific init function
87 * updates the blob size requirements.
88 */
89 do_security_initcalls();
90
ab9b110b
CS
91 /*
92 * Create any kmem_caches needed for blobs
93 */
94 if (blob_sizes.lbs_file)
95 lsm_file_cache = kmem_cache_create("lsm_file_cache",
96 blob_sizes.lbs_file, 0,
97 SLAB_PANIC, NULL);
7a90fc39
CS
98 if (blob_sizes.lbs_inode)
99 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
100 blob_sizes.lbs_inode, 0,
101 SLAB_PANIC, NULL);
f1efc8c4
CS
102 /*
103 * The second call to a module specific init function
104 * adds hooks to the hook lists and does any other early
105 * initializations required.
b1d9e6b0 106 */
1da177e4
LT
107 do_security_initcalls();
108
f1efc8c4
CS
109#ifdef CONFIG_SECURITY_LSM_DEBUG
110 pr_info("LSM: cred blob size = %d\n", blob_sizes.lbs_cred);
ab9b110b 111 pr_info("LSM: file blob size = %d\n", blob_sizes.lbs_file);
7a90fc39
CS
112 pr_info("LSM: inode blob size = %d\n", blob_sizes.lbs_inode);
113 pr_info("LSM: ipc blob size = %d\n", blob_sizes.lbs_ipc);
114#ifdef CONFIG_KEYS
115 pr_info("LSM: key blob size = %d\n", blob_sizes.lbs_key);
116#endif /* CONFIG_KEYS */
117 pr_info("LSM: msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
118 pr_info("LSM: sock blob size = %d\n", blob_sizes.lbs_sock);
119 pr_info("LSM: superblock blob size = %d\n", blob_sizes.lbs_superblock);
7f79f508 120 pr_info("LSM: task blob size = %d\n", blob_sizes.lbs_task);
7a90fc39 121#endif /* CONFIG_SECURITY_LSM_DEBUG */
f1efc8c4 122
1da177e4
LT
123 return 0;
124}
125
076c54c5
AD
126/* Save user chosen LSM */
127static int __init choose_lsm(char *str)
128{
129 strncpy(chosen_lsm, str, SECURITY_NAME_MAX);
130 return 1;
131}
132__setup("security=", choose_lsm);
133
3bb857e4
MS
134static bool match_last_lsm(const char *list, const char *lsm)
135{
136 const char *last;
137
138 if (WARN_ON(!list || !lsm))
139 return false;
140 last = strrchr(list, ',');
141 if (last)
142 /* Pass the comma, strcmp() will check for '\0' */
143 last++;
144 else
145 last = list;
146 return !strcmp(last, lsm);
147}
148
d69dece5
CS
149static int lsm_append(char *new, char **result)
150{
151 char *cp;
152
153 if (*result == NULL) {
154 *result = kstrdup(new, GFP_KERNEL);
155 } else {
3bb857e4
MS
156 /* Check if it is the last registered name */
157 if (match_last_lsm(*result, new))
158 return 0;
d69dece5
CS
159 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
160 if (cp == NULL)
161 return -ENOMEM;
162 kfree(*result);
163 *result = cp;
164 }
165 return 0;
166}
167
076c54c5
AD
168/**
169 * security_module_enable - Load given security module on boot ?
b1d9e6b0 170 * @module: the name of the module
076c54c5
AD
171 *
172 * Each LSM must pass this method before registering its own operations
173 * to avoid security registration races. This method may also be used
7cea51be 174 * to check if your LSM is currently loaded during kernel initialization.
076c54c5 175 *
0e056eb5
MCC
176 * Returns:
177 *
178 * true if:
179 *
180 * - The passed LSM is the one chosen by user at boot time,
181 * - or the passed LSM is configured as the default and the user did not
182 * choose an alternate LSM at boot time.
183 *
076c54c5
AD
184 * Otherwise, return false.
185 */
b1d9e6b0 186int __init security_module_enable(const char *module)
076c54c5 187{
b1d9e6b0 188 return !strcmp(module, chosen_lsm);
076c54c5
AD
189}
190
d69dece5
CS
191/**
192 * security_add_hooks - Add a modules hooks to the hook lists.
193 * @hooks: the hooks to add
194 * @count: the number of hooks to add
195 * @lsm: the name of the security module
196 *
197 * Each LSM has to register its hooks with the infrastructure.
198 */
199void __init security_add_hooks(struct security_hook_list *hooks, int count,
200 char *lsm)
201{
202 int i;
203
204 for (i = 0; i < count; i++) {
205 hooks[i].lsm = lsm;
206 list_add_tail_rcu(&hooks[i].list, hooks[i].head);
207 }
208 if (lsm_append(lsm, &lsm_names) < 0)
209 panic("%s - Cannot get early memory.\n", __func__);
210}
211
8f408ab6
DJ
212int call_lsm_notifier(enum lsm_event event, void *data)
213{
214 return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
215}
216EXPORT_SYMBOL(call_lsm_notifier);
217
218int register_lsm_notifier(struct notifier_block *nb)
219{
220 return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
221}
222EXPORT_SYMBOL(register_lsm_notifier);
223
224int unregister_lsm_notifier(struct notifier_block *nb)
225{
226 return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
227}
228EXPORT_SYMBOL(unregister_lsm_notifier);
229
f1efc8c4
CS
230/**
231 * lsm_cred_alloc - allocate a composite cred blob
232 * @cred: the cred that needs a blob
233 * @gfp: allocation type
234 *
235 * Allocate the cred blob for all the modules
236 *
237 * Returns 0, or -ENOMEM if memory can't be allocated.
238 */
239int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
240{
241 if (blob_sizes.lbs_cred == 0) {
242 cred->security = NULL;
243 return 0;
244 }
245
246 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
247 if (cred->security == NULL)
248 return -ENOMEM;
249 return 0;
250}
251
252/**
253 * lsm_early_cred - during initialization allocate a composite cred blob
254 * @cred: the cred that needs a blob
255 *
256 * Allocate the cred blob for all the modules if it's not already there
257 */
258void lsm_early_cred(struct cred *cred)
259{
260 int rc;
261
262 if (cred == NULL)
263 panic("%s: NULL cred.\n", __func__);
264 if (cred->security != NULL)
265 return;
266 rc = lsm_cred_alloc(cred, GFP_KERNEL);
267 if (rc)
268 panic("%s: Early cred alloc failed.\n", __func__);
269}
270
271static void __init lsm_set_size(int *need, int *lbs)
272{
273 int offset;
274
275 if (*need > 0) {
276 offset = *lbs;
277 *lbs += *need;
278 *need = offset;
279 }
280}
281
282/**
283 * security_add_blobs - Report blob sizes
284 * @needed: the size of blobs needed by the module
285 *
286 * Each LSM has to register its blobs with the infrastructure.
287 * The "needed" data tells the infrastructure how much memory
288 * the module requires for each of its blobs. On return the
289 * structure is filled with the offset that module should use
290 * from the blob pointer.
291 */
292void __init security_add_blobs(struct lsm_blob_sizes *needed)
293{
294 lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
ab9b110b 295 lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
7a90fc39
CS
296 lsm_set_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
297 lsm_set_size(&needed->lbs_key, &blob_sizes.lbs_key);
298 lsm_set_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
299 lsm_set_size(&needed->lbs_sock, &blob_sizes.lbs_sock);
300 lsm_set_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
7f79f508 301 lsm_set_size(&needed->lbs_task, &blob_sizes.lbs_task);
7a90fc39
CS
302 /*
303 * The inode blob gets an rcu_head in addition to
304 * what the modules might need.
305 */
306 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
307 blob_sizes.lbs_inode = sizeof(struct rcu_head);
308 lsm_set_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
ab9b110b
CS
309}
310
311/**
312 * lsm_file_alloc - allocate a composite file blob
313 * @file: the file that needs a blob
314 *
315 * Allocate the file blob for all the modules
316 *
317 * Returns 0, or -ENOMEM if memory can't be allocated.
318 */
319int lsm_file_alloc(struct file *file)
320{
321 if (!lsm_file_cache) {
322 file->f_security = NULL;
323 return 0;
324 }
325
326 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
327 if (file->f_security == NULL)
328 return -ENOMEM;
329 return 0;
f1efc8c4
CS
330}
331
7f79f508
CS
332/**
333 * lsm_task_alloc - allocate a composite task blob
334 * @task: the task that needs a blob
335 *
336 * Allocate the task blob for all the modules
337 *
338 * Returns 0, or -ENOMEM if memory can't be allocated.
339 */
340int lsm_task_alloc(struct task_struct *task)
341{
342 if (blob_sizes.lbs_task == 0) {
343 task->security = NULL;
344 return 0;
345 }
346
347 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
348 if (task->security == NULL)
349 return -ENOMEM;
350 return 0;
351}
352
7a90fc39
CS
353/**
354 * lsm_inode_alloc - allocate a composite inode blob
355 * @inode: the inode that needs a blob
356 *
357 * Allocate the inode blob for all the modules
358 *
359 * Returns 0, or -ENOMEM if memory can't be allocated.
360 */
361int lsm_inode_alloc(struct inode *inode)
362{
363 if (!lsm_inode_cache) {
364 inode->i_security = NULL;
365 return 0;
366 }
367
368 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_KERNEL);
369 if (inode->i_security == NULL)
370 return -ENOMEM;
371 return 0;
372}
373
374/**
375 * lsm_early_inode - during initialization allocate a composite inode blob
376 * @inode: the inode that needs a blob
377 *
378 * Allocate the inode blob for all the modules if it's not already there
379 */
380void lsm_early_inode(struct inode *inode)
381{
382 int rc;
383
384 if (inode == NULL)
385 panic("%s: NULL inode.\n", __func__);
386 if (inode->i_security != NULL)
387 return;
388 rc = lsm_inode_alloc(inode);
389 if (rc)
390 panic("%s: Early inode alloc failed.\n", __func__);
391}
392
393/**
394 * lsm_ipc_alloc - allocate a composite ipc blob
395 * @kip: the ipc that needs a blob
396 *
397 * Allocate the ipc blob for all the modules
398 *
399 * Returns 0, or -ENOMEM if memory can't be allocated.
400 */
401int lsm_ipc_alloc(struct kern_ipc_perm *kip)
402{
403 if (blob_sizes.lbs_ipc == 0) {
404 kip->security = NULL;
405 return 0;
406 }
407
408 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
409 if (kip->security == NULL)
410 return -ENOMEM;
411 return 0;
412}
413
414#ifdef CONFIG_KEYS
415/**
416 * lsm_key_alloc - allocate a composite key blob
417 * @key: the key that needs a blob
418 *
419 * Allocate the key blob for all the modules
420 *
421 * Returns 0, or -ENOMEM if memory can't be allocated.
422 */
423int lsm_key_alloc(struct key *key)
424{
425 if (blob_sizes.lbs_key == 0) {
426 key->security = NULL;
427 return 0;
428 }
429
430 key->security = kzalloc(blob_sizes.lbs_key, GFP_KERNEL);
431 if (key->security == NULL)
432 return -ENOMEM;
433 return 0;
434}
435#endif /* CONFIG_KEYS */
436
437/**
438 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
439 * @mp: the msg_msg that needs a blob
440 *
441 * Allocate the ipc blob for all the modules
442 *
443 * Returns 0, or -ENOMEM if memory can't be allocated.
444 */
445int lsm_msg_msg_alloc(struct msg_msg *mp)
446{
447 if (blob_sizes.lbs_msg_msg == 0) {
448 mp->security = NULL;
449 return 0;
450 }
451
452 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
453 if (mp->security == NULL)
454 return -ENOMEM;
455 return 0;
456}
457
458/**
459 * lsm_sock_alloc - allocate a composite sock blob
460 * @sock: the sock that needs a blob
461 * @priority: allocation mode
462 *
463 * Allocate the sock blob for all the modules
464 *
465 * Returns 0, or -ENOMEM if memory can't be allocated.
466 */
467int lsm_sock_alloc(struct sock *sock, gfp_t priority)
468{
469 if (blob_sizes.lbs_sock == 0) {
470 sock->sk_security = NULL;
471 return 0;
472 }
473
474 sock->sk_security = kzalloc(blob_sizes.lbs_sock, priority);
475 if (sock->sk_security == NULL)
476 return -ENOMEM;
477 return 0;
478}
479
480/**
481 * lsm_superblock_alloc - allocate a composite superblock blob
482 * @sb: the superblock that needs a blob
483 *
484 * Allocate the superblock blob for all the modules
485 *
486 * Returns 0, or -ENOMEM if memory can't be allocated.
487 */
488int lsm_superblock_alloc(struct super_block *sb)
489{
490 if (blob_sizes.lbs_superblock == 0) {
491 sb->s_security = NULL;
492 return 0;
493 }
494
495 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
496 if (sb->s_security == NULL)
497 return -ENOMEM;
498 return 0;
499}
500
f25fce3e 501/*
b1d9e6b0 502 * Hook list operation macros.
1da177e4 503 *
f25fce3e
CS
504 * call_void_hook:
505 * This is a hook that does not return a value.
1da177e4 506 *
f25fce3e
CS
507 * call_int_hook:
508 * This is a hook that returns a value.
1da177e4 509 */
1da177e4 510
b1d9e6b0
CS
511#define call_void_hook(FUNC, ...) \
512 do { \
513 struct security_hook_list *P; \
514 \
515 list_for_each_entry(P, &security_hook_heads.FUNC, list) \
516 P->hook.FUNC(__VA_ARGS__); \
517 } while (0)
518
519#define call_int_hook(FUNC, IRC, ...) ({ \
520 int RC = IRC; \
521 do { \
522 struct security_hook_list *P; \
523 \
524 list_for_each_entry(P, &security_hook_heads.FUNC, list) { \
525 RC = P->hook.FUNC(__VA_ARGS__); \
526 if (RC != 0) \
527 break; \
528 } \
529 } while (0); \
530 RC; \
531})
1da177e4 532
20510f2f
JM
533/* Security operations */
534
79af7307
SS
535int security_binder_set_context_mgr(struct task_struct *mgr)
536{
f25fce3e 537 return call_int_hook(binder_set_context_mgr, 0, mgr);
79af7307
SS
538}
539
540int security_binder_transaction(struct task_struct *from,
541 struct task_struct *to)
542{
f25fce3e 543 return call_int_hook(binder_transaction, 0, from, to);
79af7307
SS
544}
545
546int security_binder_transfer_binder(struct task_struct *from,
547 struct task_struct *to)
548{
f25fce3e 549 return call_int_hook(binder_transfer_binder, 0, from, to);
79af7307
SS
550}
551
552int security_binder_transfer_file(struct task_struct *from,
553 struct task_struct *to, struct file *file)
554{
f25fce3e 555 return call_int_hook(binder_transfer_file, 0, from, to, file);
79af7307
SS
556}
557
9e48858f 558int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
20510f2f 559{
f25fce3e 560 return call_int_hook(ptrace_access_check, 0, child, mode);
5cd9c58f
DH
561}
562
563int security_ptrace_traceme(struct task_struct *parent)
564{
f25fce3e 565 return call_int_hook(ptrace_traceme, 0, parent);
20510f2f
JM
566}
567
568int security_capget(struct task_struct *target,
569 kernel_cap_t *effective,
570 kernel_cap_t *inheritable,
571 kernel_cap_t *permitted)
572{
f25fce3e
CS
573 return call_int_hook(capget, 0, target,
574 effective, inheritable, permitted);
20510f2f
JM
575}
576
d84f4f99
DH
577int security_capset(struct cred *new, const struct cred *old,
578 const kernel_cap_t *effective,
579 const kernel_cap_t *inheritable,
580 const kernel_cap_t *permitted)
20510f2f 581{
f25fce3e
CS
582 return call_int_hook(capset, 0, new, old,
583 effective, inheritable, permitted);
20510f2f
JM
584}
585
b7e724d3 586int security_capable(const struct cred *cred, struct user_namespace *ns,
3486740a 587 int cap)
20510f2f 588{
f25fce3e 589 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
06112163
EP
590}
591
c7eba4a9
EP
592int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns,
593 int cap)
06112163 594{
f25fce3e 595 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT);
20510f2f
JM
596}
597
20510f2f
JM
598int security_quotactl(int cmds, int type, int id, struct super_block *sb)
599{
f25fce3e 600 return call_int_hook(quotactl, 0, cmds, type, id, sb);
20510f2f
JM
601}
602
603int security_quota_on(struct dentry *dentry)
604{
f25fce3e 605 return call_int_hook(quota_on, 0, dentry);
20510f2f
JM
606}
607
12b3052c 608int security_syslog(int type)
20510f2f 609{
f25fce3e 610 return call_int_hook(syslog, 0, type);
20510f2f
JM
611}
612
457db29b 613int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
20510f2f 614{
f25fce3e 615 return call_int_hook(settime, 0, ts, tz);
20510f2f
JM
616}
617
20510f2f
JM
618int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
619{
b1d9e6b0
CS
620 struct security_hook_list *hp;
621 int cap_sys_admin = 1;
622 int rc;
623
624 /*
625 * The module will respond with a positive value if
626 * it thinks the __vm_enough_memory() call should be
627 * made with the cap_sys_admin set. If all of the modules
628 * agree that it should be set it will. If any module
629 * thinks it should not be set it won't.
630 */
631 list_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
632 rc = hp->hook.vm_enough_memory(mm, pages);
633 if (rc <= 0) {
634 cap_sys_admin = 0;
635 break;
636 }
637 }
638 return __vm_enough_memory(mm, pages, cap_sys_admin);
20510f2f
JM
639}
640
a6f76f23 641int security_bprm_set_creds(struct linux_binprm *bprm)
20510f2f 642{
f25fce3e 643 return call_int_hook(bprm_set_creds, 0, bprm);
20510f2f
JM
644}
645
a6f76f23 646int security_bprm_check(struct linux_binprm *bprm)
20510f2f 647{
6c21a7fb
MZ
648 int ret;
649
f25fce3e 650 ret = call_int_hook(bprm_check_security, 0, bprm);
6c21a7fb
MZ
651 if (ret)
652 return ret;
653 return ima_bprm_check(bprm);
20510f2f
JM
654}
655
a6f76f23 656void security_bprm_committing_creds(struct linux_binprm *bprm)
20510f2f 657{
f25fce3e 658 call_void_hook(bprm_committing_creds, bprm);
20510f2f
JM
659}
660
a6f76f23 661void security_bprm_committed_creds(struct linux_binprm *bprm)
20510f2f 662{
f25fce3e 663 call_void_hook(bprm_committed_creds, bprm);
20510f2f
JM
664}
665
20510f2f
JM
666int security_sb_alloc(struct super_block *sb)
667{
7a90fc39
CS
668 int rc = lsm_superblock_alloc(sb);
669
670 if (unlikely(rc))
671 return rc;
672 rc = call_int_hook(sb_alloc_security, 0, sb);
673 if (unlikely(rc))
674 security_sb_free(sb);
675 return rc;
20510f2f
JM
676}
677
678void security_sb_free(struct super_block *sb)
679{
f25fce3e 680 call_void_hook(sb_free_security, sb);
7a90fc39
CS
681 kfree(sb->s_security);
682 sb->s_security = NULL;
20510f2f
JM
683}
684
e0007529 685int security_sb_copy_data(char *orig, char *copy)
20510f2f 686{
f25fce3e 687 return call_int_hook(sb_copy_data, 0, orig, copy);
20510f2f 688}
e0007529 689EXPORT_SYMBOL(security_sb_copy_data);
20510f2f 690
ff36fe2c
EP
691int security_sb_remount(struct super_block *sb, void *data)
692{
f25fce3e 693 return call_int_hook(sb_remount, 0, sb, data);
ff36fe2c
EP
694}
695
12204e24 696int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
20510f2f 697{
f25fce3e 698 return call_int_hook(sb_kern_mount, 0, sb, flags, data);
20510f2f
JM
699}
700
2069f457
EP
701int security_sb_show_options(struct seq_file *m, struct super_block *sb)
702{
f25fce3e 703 return call_int_hook(sb_show_options, 0, m, sb);
2069f457
EP
704}
705
20510f2f
JM
706int security_sb_statfs(struct dentry *dentry)
707{
f25fce3e 708 return call_int_hook(sb_statfs, 0, dentry);
20510f2f
JM
709}
710
8a04c43b 711int security_sb_mount(const char *dev_name, const struct path *path,
808d4e3c 712 const char *type, unsigned long flags, void *data)
20510f2f 713{
f25fce3e 714 return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
20510f2f
JM
715}
716
20510f2f
JM
717int security_sb_umount(struct vfsmount *mnt, int flags)
718{
f25fce3e 719 return call_int_hook(sb_umount, 0, mnt, flags);
20510f2f
JM
720}
721
3b73b68c 722int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
20510f2f 723{
f25fce3e 724 return call_int_hook(sb_pivotroot, 0, old_path, new_path);
20510f2f
JM
725}
726
c9180a57 727int security_sb_set_mnt_opts(struct super_block *sb,
649f6e77
DQ
728 struct security_mnt_opts *opts,
729 unsigned long kern_flags,
730 unsigned long *set_kern_flags)
c9180a57 731{
b1d9e6b0
CS
732 return call_int_hook(sb_set_mnt_opts,
733 opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
734 opts, kern_flags, set_kern_flags);
c9180a57 735}
e0007529 736EXPORT_SYMBOL(security_sb_set_mnt_opts);
c9180a57 737
094f7b69 738int security_sb_clone_mnt_opts(const struct super_block *oldsb,
0b4d3452
SM
739 struct super_block *newsb,
740 unsigned long kern_flags,
741 unsigned long *set_kern_flags)
c9180a57 742{
0b4d3452
SM
743 return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
744 kern_flags, set_kern_flags);
c9180a57 745}
e0007529
EP
746EXPORT_SYMBOL(security_sb_clone_mnt_opts);
747
748int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
749{
f25fce3e 750 return call_int_hook(sb_parse_opts_str, 0, options, opts);
e0007529
EP
751}
752EXPORT_SYMBOL(security_sb_parse_opts_str);
c9180a57 753
20510f2f
JM
754int security_inode_alloc(struct inode *inode)
755{
7a90fc39
CS
756 int rc = lsm_inode_alloc(inode);
757
758 if (unlikely(rc))
759 return rc;
760 rc = call_int_hook(inode_alloc_security, 0, inode);
761 if (unlikely(rc))
762 security_inode_free(inode);
763 return rc;
764}
765
766static void inode_free_by_rcu(struct rcu_head *head)
767{
768 /*
769 * The rcu head is at the start of the inode blob
770 */
771 kmem_cache_free(lsm_inode_cache, head);
20510f2f
JM
772}
773
774void security_inode_free(struct inode *inode)
775{
f381c272 776 integrity_inode_free(inode);
f25fce3e 777 call_void_hook(inode_free_security, inode);
7a90fc39
CS
778 /*
779 * The inode may still be referenced in a path walk and
780 * a call to security_inode_permission() can be made
781 * after inode_free_security() is called. Ideally, the VFS
782 * wouldn't do this, but fixing that is a much harder
783 * job. For now, simply free the i_security via RCU, and
784 * leave the current inode->i_security pointer intact.
785 * The inode will be freed after the RCU grace period too.
786 */
787 if (inode->i_security)
788 call_rcu((struct rcu_head *)inode->i_security,
789 inode_free_by_rcu);
20510f2f
JM
790}
791
d47be3df 792int security_dentry_init_security(struct dentry *dentry, int mode,
4f3ccd76 793 const struct qstr *name, void **ctx,
d47be3df
DQ
794 u32 *ctxlen)
795{
b1d9e6b0
CS
796 return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
797 name, ctx, ctxlen);
d47be3df
DQ
798}
799EXPORT_SYMBOL(security_dentry_init_security);
800
2602625b
VG
801int security_dentry_create_files_as(struct dentry *dentry, int mode,
802 struct qstr *name,
803 const struct cred *old, struct cred *new)
804{
805 return call_int_hook(dentry_create_files_as, 0, dentry, mode,
806 name, old, new);
807}
808EXPORT_SYMBOL(security_dentry_create_files_as);
809
20510f2f 810int security_inode_init_security(struct inode *inode, struct inode *dir,
9d8f13ba
MZ
811 const struct qstr *qstr,
812 const initxattrs initxattrs, void *fs_data)
20510f2f 813{
823eb1cc
MZ
814 struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
815 struct xattr *lsm_xattr, *evm_xattr, *xattr;
9d8f13ba
MZ
816 int ret;
817
20510f2f 818 if (unlikely(IS_PRIVATE(inode)))
fb88c2b6 819 return 0;
9d8f13ba 820
9d8f13ba 821 if (!initxattrs)
e308fd3b
JB
822 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
823 dir, qstr, NULL, NULL, NULL);
9548906b 824 memset(new_xattrs, 0, sizeof(new_xattrs));
9d8f13ba 825 lsm_xattr = new_xattrs;
b1d9e6b0 826 ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
9d8f13ba
MZ
827 &lsm_xattr->name,
828 &lsm_xattr->value,
829 &lsm_xattr->value_len);
830 if (ret)
831 goto out;
823eb1cc
MZ
832
833 evm_xattr = lsm_xattr + 1;
834 ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
835 if (ret)
836 goto out;
9d8f13ba
MZ
837 ret = initxattrs(inode, new_xattrs, fs_data);
838out:
9548906b 839 for (xattr = new_xattrs; xattr->value != NULL; xattr++)
823eb1cc 840 kfree(xattr->value);
9d8f13ba
MZ
841 return (ret == -EOPNOTSUPP) ? 0 : ret;
842}
843EXPORT_SYMBOL(security_inode_init_security);
844
845int security_old_inode_init_security(struct inode *inode, struct inode *dir,
9548906b 846 const struct qstr *qstr, const char **name,
9d8f13ba 847 void **value, size_t *len)
20510f2f
JM
848{
849 if (unlikely(IS_PRIVATE(inode)))
30e05324 850 return -EOPNOTSUPP;
e308fd3b
JB
851 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
852 qstr, name, value, len);
20510f2f 853}
9d8f13ba 854EXPORT_SYMBOL(security_old_inode_init_security);
20510f2f 855
be6d3e56 856#ifdef CONFIG_SECURITY_PATH
d3607752 857int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
be6d3e56
KT
858 unsigned int dev)
859{
c6f493d6 860 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 861 return 0;
f25fce3e 862 return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
be6d3e56
KT
863}
864EXPORT_SYMBOL(security_path_mknod);
865
d3607752 866int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
be6d3e56 867{
c6f493d6 868 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 869 return 0;
f25fce3e 870 return call_int_hook(path_mkdir, 0, dir, dentry, mode);
be6d3e56 871}
82140443 872EXPORT_SYMBOL(security_path_mkdir);
be6d3e56 873
989f74e0 874int security_path_rmdir(const struct path *dir, struct dentry *dentry)
be6d3e56 875{
c6f493d6 876 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 877 return 0;
f25fce3e 878 return call_int_hook(path_rmdir, 0, dir, dentry);
be6d3e56 879}
c088e31d 880EXPORT_SYMBOL_GPL(security_path_rmdir);
be6d3e56 881
989f74e0 882int security_path_unlink(const struct path *dir, struct dentry *dentry)
be6d3e56 883{
c6f493d6 884 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 885 return 0;
f25fce3e 886 return call_int_hook(path_unlink, 0, dir, dentry);
be6d3e56 887}
82140443 888EXPORT_SYMBOL(security_path_unlink);
be6d3e56 889
d3607752 890int security_path_symlink(const struct path *dir, struct dentry *dentry,
be6d3e56
KT
891 const char *old_name)
892{
c6f493d6 893 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 894 return 0;
f25fce3e 895 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
be6d3e56 896}
c088e31d 897EXPORT_SYMBOL_GPL(security_path_symlink);
be6d3e56 898
3ccee46a 899int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
be6d3e56
KT
900 struct dentry *new_dentry)
901{
c6f493d6 902 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
be6d3e56 903 return 0;
f25fce3e 904 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
be6d3e56 905}
c088e31d 906EXPORT_SYMBOL_GPL(security_path_link);
be6d3e56 907
3ccee46a
AV
908int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
909 const struct path *new_dir, struct dentry *new_dentry,
0b3974eb 910 unsigned int flags)
be6d3e56 911{
c6f493d6
DH
912 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
913 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
be6d3e56 914 return 0;
da1ce067
MS
915
916 if (flags & RENAME_EXCHANGE) {
f25fce3e
CS
917 int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
918 old_dir, old_dentry);
da1ce067
MS
919 if (err)
920 return err;
921 }
922
f25fce3e
CS
923 return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
924 new_dentry);
be6d3e56 925}
82140443 926EXPORT_SYMBOL(security_path_rename);
be6d3e56 927
81f4c506 928int security_path_truncate(const struct path *path)
be6d3e56 929{
c6f493d6 930 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
be6d3e56 931 return 0;
f25fce3e 932 return call_int_hook(path_truncate, 0, path);
be6d3e56 933}
c088e31d 934EXPORT_SYMBOL_GPL(security_path_truncate);
89eda068 935
be01f9f2 936int security_path_chmod(const struct path *path, umode_t mode)
89eda068 937{
c6f493d6 938 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 939 return 0;
f25fce3e 940 return call_int_hook(path_chmod, 0, path, mode);
89eda068 941}
c088e31d 942EXPORT_SYMBOL_GPL(security_path_chmod);
89eda068 943
7fd25dac 944int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
89eda068 945{
c6f493d6 946 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 947 return 0;
f25fce3e 948 return call_int_hook(path_chown, 0, path, uid, gid);
89eda068 949}
c088e31d 950EXPORT_SYMBOL_GPL(security_path_chown);
8b8efb44 951
77b286c0 952int security_path_chroot(const struct path *path)
8b8efb44 953{
f25fce3e 954 return call_int_hook(path_chroot, 0, path);
8b8efb44 955}
be6d3e56
KT
956#endif
957
4acdaf27 958int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
20510f2f
JM
959{
960 if (unlikely(IS_PRIVATE(dir)))
961 return 0;
f25fce3e 962 return call_int_hook(inode_create, 0, dir, dentry, mode);
20510f2f 963}
800a9647 964EXPORT_SYMBOL_GPL(security_inode_create);
20510f2f
JM
965
966int security_inode_link(struct dentry *old_dentry, struct inode *dir,
967 struct dentry *new_dentry)
968{
c6f493d6 969 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
20510f2f 970 return 0;
f25fce3e 971 return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
20510f2f
JM
972}
973
974int security_inode_unlink(struct inode *dir, struct dentry *dentry)
975{
c6f493d6 976 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 977 return 0;
f25fce3e 978 return call_int_hook(inode_unlink, 0, dir, dentry);
20510f2f
JM
979}
980
981int security_inode_symlink(struct inode *dir, struct dentry *dentry,
982 const char *old_name)
983{
984 if (unlikely(IS_PRIVATE(dir)))
985 return 0;
f25fce3e 986 return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
20510f2f
JM
987}
988
18bb1db3 989int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20510f2f
JM
990{
991 if (unlikely(IS_PRIVATE(dir)))
992 return 0;
f25fce3e 993 return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
20510f2f 994}
800a9647 995EXPORT_SYMBOL_GPL(security_inode_mkdir);
20510f2f
JM
996
997int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
998{
c6f493d6 999 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1000 return 0;
f25fce3e 1001 return call_int_hook(inode_rmdir, 0, dir, dentry);
20510f2f
JM
1002}
1003
1a67aafb 1004int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
20510f2f
JM
1005{
1006 if (unlikely(IS_PRIVATE(dir)))
1007 return 0;
f25fce3e 1008 return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
20510f2f
JM
1009}
1010
1011int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
0b3974eb
MS
1012 struct inode *new_dir, struct dentry *new_dentry,
1013 unsigned int flags)
20510f2f 1014{
c6f493d6
DH
1015 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1016 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
20510f2f 1017 return 0;
da1ce067
MS
1018
1019 if (flags & RENAME_EXCHANGE) {
f25fce3e 1020 int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
da1ce067
MS
1021 old_dir, old_dentry);
1022 if (err)
1023 return err;
1024 }
1025
f25fce3e 1026 return call_int_hook(inode_rename, 0, old_dir, old_dentry,
20510f2f
JM
1027 new_dir, new_dentry);
1028}
1029
1030int security_inode_readlink(struct dentry *dentry)
1031{
c6f493d6 1032 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1033 return 0;
f25fce3e 1034 return call_int_hook(inode_readlink, 0, dentry);
20510f2f 1035}
c088e31d 1036EXPORT_SYMBOL_GPL(security_inode_readlink);
20510f2f 1037
bda0be7a
N
1038int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1039 bool rcu)
20510f2f 1040{
bda0be7a 1041 if (unlikely(IS_PRIVATE(inode)))
20510f2f 1042 return 0;
e22619a2 1043 return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
20510f2f
JM
1044}
1045
b77b0646 1046int security_inode_permission(struct inode *inode, int mask)
20510f2f
JM
1047{
1048 if (unlikely(IS_PRIVATE(inode)))
1049 return 0;
f25fce3e 1050 return call_int_hook(inode_permission, 0, inode, mask);
20510f2f 1051}
c088e31d 1052EXPORT_SYMBOL_GPL(security_inode_permission);
20510f2f
JM
1053
1054int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1055{
817b54aa
MZ
1056 int ret;
1057
c6f493d6 1058 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1059 return 0;
f25fce3e 1060 ret = call_int_hook(inode_setattr, 0, dentry, attr);
817b54aa
MZ
1061 if (ret)
1062 return ret;
1063 return evm_inode_setattr(dentry, attr);
20510f2f 1064}
b1da47e2 1065EXPORT_SYMBOL_GPL(security_inode_setattr);
20510f2f 1066
3f7036a0 1067int security_inode_getattr(const struct path *path)
20510f2f 1068{
c6f493d6 1069 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
20510f2f 1070 return 0;
f25fce3e 1071 return call_int_hook(inode_getattr, 0, path);
20510f2f
JM
1072}
1073
8f0cfa52
DH
1074int security_inode_setxattr(struct dentry *dentry, const char *name,
1075 const void *value, size_t size, int flags)
20510f2f 1076{
3e1be52d
MZ
1077 int ret;
1078
c6f493d6 1079 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1080 return 0;
b1d9e6b0
CS
1081 /*
1082 * SELinux and Smack integrate the cap call,
1083 * so assume that all LSMs supplying this call do so.
1084 */
1085 ret = call_int_hook(inode_setxattr, 1, dentry, name, value, size,
f25fce3e 1086 flags);
b1d9e6b0
CS
1087
1088 if (ret == 1)
1089 ret = cap_inode_setxattr(dentry, name, value, size, flags);
42c63330
MZ
1090 if (ret)
1091 return ret;
1092 ret = ima_inode_setxattr(dentry, name, value, size);
3e1be52d
MZ
1093 if (ret)
1094 return ret;
1095 return evm_inode_setxattr(dentry, name, value, size);
20510f2f
JM
1096}
1097
8f0cfa52
DH
1098void security_inode_post_setxattr(struct dentry *dentry, const char *name,
1099 const void *value, size_t size, int flags)
20510f2f 1100{
c6f493d6 1101 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1102 return;
f25fce3e 1103 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
3e1be52d 1104 evm_inode_post_setxattr(dentry, name, value, size);
20510f2f
JM
1105}
1106
8f0cfa52 1107int security_inode_getxattr(struct dentry *dentry, const char *name)
20510f2f 1108{
c6f493d6 1109 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1110 return 0;
f25fce3e 1111 return call_int_hook(inode_getxattr, 0, dentry, name);
20510f2f
JM
1112}
1113
1114int security_inode_listxattr(struct dentry *dentry)
1115{
c6f493d6 1116 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1117 return 0;
f25fce3e 1118 return call_int_hook(inode_listxattr, 0, dentry);
20510f2f
JM
1119}
1120
8f0cfa52 1121int security_inode_removexattr(struct dentry *dentry, const char *name)
20510f2f 1122{
3e1be52d
MZ
1123 int ret;
1124
c6f493d6 1125 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1126 return 0;
b1d9e6b0
CS
1127 /*
1128 * SELinux and Smack integrate the cap call,
1129 * so assume that all LSMs supplying this call do so.
1130 */
1131 ret = call_int_hook(inode_removexattr, 1, dentry, name);
1132 if (ret == 1)
1133 ret = cap_inode_removexattr(dentry, name);
42c63330
MZ
1134 if (ret)
1135 return ret;
1136 ret = ima_inode_removexattr(dentry, name);
3e1be52d
MZ
1137 if (ret)
1138 return ret;
1139 return evm_inode_removexattr(dentry, name);
20510f2f
JM
1140}
1141
b5376771
SH
1142int security_inode_need_killpriv(struct dentry *dentry)
1143{
f25fce3e 1144 return call_int_hook(inode_need_killpriv, 0, dentry);
b5376771
SH
1145}
1146
1147int security_inode_killpriv(struct dentry *dentry)
1148{
f25fce3e 1149 return call_int_hook(inode_killpriv, 0, dentry);
b5376771
SH
1150}
1151
ea861dfd 1152int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
20510f2f 1153{
2885c1e3
CS
1154 struct security_hook_list *hp;
1155 int rc;
1156
20510f2f 1157 if (unlikely(IS_PRIVATE(inode)))
8d952504 1158 return -EOPNOTSUPP;
2885c1e3
CS
1159 /*
1160 * Only one module will provide an attribute with a given name.
1161 */
1162 list_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
1163 rc = hp->hook.inode_getsecurity(inode, name, buffer, alloc);
1164 if (rc != -EOPNOTSUPP)
1165 return rc;
1166 }
1167 return -EOPNOTSUPP;
20510f2f
JM
1168}
1169
1170int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
1171{
2885c1e3
CS
1172 struct security_hook_list *hp;
1173 int rc;
1174
20510f2f 1175 if (unlikely(IS_PRIVATE(inode)))
8d952504 1176 return -EOPNOTSUPP;
2885c1e3
CS
1177 /*
1178 * Only one module will provide an attribute with a given name.
1179 */
1180 list_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
1181 rc = hp->hook.inode_setsecurity(inode, name, value, size,
1182 flags);
1183 if (rc != -EOPNOTSUPP)
1184 return rc;
1185 }
1186 return -EOPNOTSUPP;
20510f2f
JM
1187}
1188
1189int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
1190{
1191 if (unlikely(IS_PRIVATE(inode)))
1192 return 0;
f25fce3e 1193 return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
20510f2f 1194}
c9bccef6 1195EXPORT_SYMBOL(security_inode_listsecurity);
20510f2f 1196
d6335d77 1197void security_inode_getsecid(struct inode *inode, u32 *secid)
8a076191 1198{
f25fce3e 1199 call_void_hook(inode_getsecid, inode, secid);
8a076191
AD
1200}
1201
d8ad8b49
VG
1202int security_inode_copy_up(struct dentry *src, struct cred **new)
1203{
1204 return call_int_hook(inode_copy_up, 0, src, new);
1205}
1206EXPORT_SYMBOL(security_inode_copy_up);
1207
121ab822
VG
1208int security_inode_copy_up_xattr(const char *name)
1209{
1210 return call_int_hook(inode_copy_up_xattr, -EOPNOTSUPP, name);
1211}
1212EXPORT_SYMBOL(security_inode_copy_up_xattr);
1213
20510f2f
JM
1214int security_file_permission(struct file *file, int mask)
1215{
c4ec54b4
EP
1216 int ret;
1217
f25fce3e 1218 ret = call_int_hook(file_permission, 0, file, mask);
c4ec54b4
EP
1219 if (ret)
1220 return ret;
1221
1222 return fsnotify_perm(file, mask);
20510f2f 1223}
c088e31d 1224EXPORT_SYMBOL_GPL(security_file_permission);
20510f2f
JM
1225
1226int security_file_alloc(struct file *file)
1227{
ab9b110b
CS
1228 int rc = lsm_file_alloc(file);
1229
7a90fc39 1230 if (unlikely(rc))
ab9b110b
CS
1231 return rc;
1232 rc = call_int_hook(file_alloc_security, 0, file);
1233 if (unlikely(rc))
1234 security_file_free(file);
1235 return rc;
20510f2f
JM
1236}
1237
1238void security_file_free(struct file *file)
1239{
ab9b110b
CS
1240 void *blob;
1241
1242 if (!lsm_file_cache)
1243 return;
1244
f25fce3e 1245 call_void_hook(file_free_security, file);
ab9b110b
CS
1246
1247 blob = file->f_security;
1248 file->f_security = NULL;
1249 kmem_cache_free(lsm_file_cache, blob);
20510f2f
JM
1250}
1251
1252int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1253{
f25fce3e 1254 return call_int_hook(file_ioctl, 0, file, cmd, arg);
20510f2f
JM
1255}
1256
98de59bf 1257static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
20510f2f 1258{
8b3ec681 1259 /*
98de59bf
AV
1260 * Does we have PROT_READ and does the application expect
1261 * it to imply PROT_EXEC? If not, nothing to talk about...
8b3ec681 1262 */
98de59bf
AV
1263 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
1264 return prot;
8b3ec681 1265 if (!(current->personality & READ_IMPLIES_EXEC))
98de59bf
AV
1266 return prot;
1267 /*
1268 * if that's an anonymous mapping, let it.
1269 */
1270 if (!file)
1271 return prot | PROT_EXEC;
1272 /*
1273 * ditto if it's not on noexec mount, except that on !MMU we need
b4caecd4 1274 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
98de59bf 1275 */
90f8572b 1276 if (!path_noexec(&file->f_path)) {
8b3ec681 1277#ifndef CONFIG_MMU
b4caecd4
CH
1278 if (file->f_op->mmap_capabilities) {
1279 unsigned caps = file->f_op->mmap_capabilities(file);
1280 if (!(caps & NOMMU_MAP_EXEC))
1281 return prot;
1282 }
8b3ec681 1283#endif
98de59bf 1284 return prot | PROT_EXEC;
8b3ec681 1285 }
98de59bf
AV
1286 /* anything on noexec mount won't get PROT_EXEC */
1287 return prot;
1288}
1289
1290int security_mmap_file(struct file *file, unsigned long prot,
1291 unsigned long flags)
1292{
1293 int ret;
f25fce3e 1294 ret = call_int_hook(mmap_file, 0, file, prot,
98de59bf 1295 mmap_prot(file, prot), flags);
6c21a7fb
MZ
1296 if (ret)
1297 return ret;
1298 return ima_file_mmap(file, prot);
20510f2f 1299}
c088e31d 1300EXPORT_SYMBOL_GPL(security_mmap_file);
20510f2f 1301
e5467859
AV
1302int security_mmap_addr(unsigned long addr)
1303{
f25fce3e 1304 return call_int_hook(mmap_addr, 0, addr);
e5467859
AV
1305}
1306
20510f2f
JM
1307int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
1308 unsigned long prot)
1309{
f25fce3e 1310 return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
20510f2f
JM
1311}
1312
1313int security_file_lock(struct file *file, unsigned int cmd)
1314{
f25fce3e 1315 return call_int_hook(file_lock, 0, file, cmd);
20510f2f
JM
1316}
1317
1318int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1319{
f25fce3e 1320 return call_int_hook(file_fcntl, 0, file, cmd, arg);
20510f2f
JM
1321}
1322
e0b93edd 1323void security_file_set_fowner(struct file *file)
20510f2f 1324{
f25fce3e 1325 call_void_hook(file_set_fowner, file);
20510f2f
JM
1326}
1327
1328int security_file_send_sigiotask(struct task_struct *tsk,
1329 struct fown_struct *fown, int sig)
1330{
f25fce3e 1331 return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
20510f2f
JM
1332}
1333
1334int security_file_receive(struct file *file)
1335{
f25fce3e 1336 return call_int_hook(file_receive, 0, file);
20510f2f
JM
1337}
1338
83d49856 1339int security_file_open(struct file *file, const struct cred *cred)
20510f2f 1340{
c4ec54b4
EP
1341 int ret;
1342
f25fce3e 1343 ret = call_int_hook(file_open, 0, file, cred);
c4ec54b4
EP
1344 if (ret)
1345 return ret;
1346
1347 return fsnotify_perm(file, MAY_OPEN);
20510f2f
JM
1348}
1349
e4e55b47
TH
1350int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
1351{
7f79f508
CS
1352 int rc = lsm_task_alloc(task);
1353
7a90fc39 1354 if (unlikely(rc))
7f79f508
CS
1355 return rc;
1356 rc = call_int_hook(task_alloc, 0, task, clone_flags);
1357 if (unlikely(rc))
1358 security_task_free(task);
1359 return rc;
e4e55b47
TH
1360}
1361
1a2a4d06
KC
1362void security_task_free(struct task_struct *task)
1363{
f25fce3e 1364 call_void_hook(task_free, task);
7f79f508
CS
1365
1366 kfree(task->security);
1367 task->security = NULL;
1a2a4d06
KC
1368}
1369
ee18d64c
DH
1370int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1371{
f1efc8c4
CS
1372 int rc = lsm_cred_alloc(cred, gfp);
1373
7a90fc39 1374 if (unlikely(rc))
f1efc8c4
CS
1375 return rc;
1376
1377 rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
ab9b110b 1378 if (unlikely(rc))
f1efc8c4
CS
1379 security_cred_free(cred);
1380 return rc;
ee18d64c
DH
1381}
1382
d84f4f99 1383void security_cred_free(struct cred *cred)
20510f2f 1384{
f25fce3e 1385 call_void_hook(cred_free, cred);
f1efc8c4
CS
1386
1387 kfree(cred->security);
1388 cred->security = NULL;
20510f2f
JM
1389}
1390
d84f4f99 1391int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
20510f2f 1392{
f1efc8c4
CS
1393 int rc = lsm_cred_alloc(new, gfp);
1394
7a90fc39 1395 if (unlikely(rc))
f1efc8c4
CS
1396 return rc;
1397
1398 rc = call_int_hook(cred_prepare, 0, new, old, gfp);
ab9b110b 1399 if (unlikely(rc))
f1efc8c4
CS
1400 security_cred_free(new);
1401 return rc;
d84f4f99
DH
1402}
1403
ee18d64c
DH
1404void security_transfer_creds(struct cred *new, const struct cred *old)
1405{
f25fce3e 1406 call_void_hook(cred_transfer, new, old);
ee18d64c
DH
1407}
1408
3a3b7ce9
DH
1409int security_kernel_act_as(struct cred *new, u32 secid)
1410{
f25fce3e 1411 return call_int_hook(kernel_act_as, 0, new, secid);
3a3b7ce9
DH
1412}
1413
1414int security_kernel_create_files_as(struct cred *new, struct inode *inode)
1415{
f25fce3e 1416 return call_int_hook(kernel_create_files_as, 0, new, inode);
3a3b7ce9
DH
1417}
1418
dd8dbf2e 1419int security_kernel_module_request(char *kmod_name)
9188499c 1420{
f25fce3e 1421 return call_int_hook(kernel_module_request, 0, kmod_name);
9188499c
EP
1422}
1423
39eeb4fb
MZ
1424int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
1425{
1426 int ret;
1427
1428 ret = call_int_hook(kernel_read_file, 0, file, id);
1429 if (ret)
1430 return ret;
1431 return ima_read_file(file, id);
1432}
1433EXPORT_SYMBOL_GPL(security_kernel_read_file);
1434
bc8ca5b9
MZ
1435int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
1436 enum kernel_read_file_id id)
b44a7dfc 1437{
cf222217
MZ
1438 int ret;
1439
1440 ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
1441 if (ret)
1442 return ret;
1443 return ima_post_read_file(file, buf, size, id);
b44a7dfc
MZ
1444}
1445EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
1446
d84f4f99
DH
1447int security_task_fix_setuid(struct cred *new, const struct cred *old,
1448 int flags)
20510f2f 1449{
f25fce3e 1450 return call_int_hook(task_fix_setuid, 0, new, old, flags);
20510f2f
JM
1451}
1452
20510f2f
JM
1453int security_task_setpgid(struct task_struct *p, pid_t pgid)
1454{
f25fce3e 1455 return call_int_hook(task_setpgid, 0, p, pgid);
20510f2f
JM
1456}
1457
1458int security_task_getpgid(struct task_struct *p)
1459{
f25fce3e 1460 return call_int_hook(task_getpgid, 0, p);
20510f2f
JM
1461}
1462
1463int security_task_getsid(struct task_struct *p)
1464{
f25fce3e 1465 return call_int_hook(task_getsid, 0, p);
20510f2f
JM
1466}
1467
1468void security_task_getsecid(struct task_struct *p, u32 *secid)
1469{
b1d9e6b0 1470 *secid = 0;
f25fce3e 1471 call_void_hook(task_getsecid, p, secid);
20510f2f
JM
1472}
1473EXPORT_SYMBOL(security_task_getsecid);
1474
20510f2f
JM
1475int security_task_setnice(struct task_struct *p, int nice)
1476{
f25fce3e 1477 return call_int_hook(task_setnice, 0, p, nice);
20510f2f
JM
1478}
1479
1480int security_task_setioprio(struct task_struct *p, int ioprio)
1481{
f25fce3e 1482 return call_int_hook(task_setioprio, 0, p, ioprio);
20510f2f
JM
1483}
1484
1485int security_task_getioprio(struct task_struct *p)
1486{
f25fce3e 1487 return call_int_hook(task_getioprio, 0, p);
20510f2f
JM
1488}
1489
791ec491
SS
1490int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
1491 unsigned int flags)
1492{
1493 return call_int_hook(task_prlimit, 0, cred, tcred, flags);
1494}
1495
8fd00b4d
JS
1496int security_task_setrlimit(struct task_struct *p, unsigned int resource,
1497 struct rlimit *new_rlim)
20510f2f 1498{
f25fce3e 1499 return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
20510f2f
JM
1500}
1501
b0ae1981 1502int security_task_setscheduler(struct task_struct *p)
20510f2f 1503{
f25fce3e 1504 return call_int_hook(task_setscheduler, 0, p);
20510f2f
JM
1505}
1506
1507int security_task_getscheduler(struct task_struct *p)
1508{
f25fce3e 1509 return call_int_hook(task_getscheduler, 0, p);
20510f2f
JM
1510}
1511
1512int security_task_movememory(struct task_struct *p)
1513{
f25fce3e 1514 return call_int_hook(task_movememory, 0, p);
20510f2f
JM
1515}
1516
1517int security_task_kill(struct task_struct *p, struct siginfo *info,
1518 int sig, u32 secid)
1519{
f25fce3e 1520 return call_int_hook(task_kill, 0, p, info, sig, secid);
20510f2f
JM
1521}
1522
20510f2f 1523int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
d84f4f99 1524 unsigned long arg4, unsigned long arg5)
20510f2f 1525{
b1d9e6b0
CS
1526 int thisrc;
1527 int rc = -ENOSYS;
1528 struct security_hook_list *hp;
1529
1530 list_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
1531 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
1532 if (thisrc != -ENOSYS) {
1533 rc = thisrc;
1534 if (thisrc != 0)
1535 break;
1536 }
1537 }
1538 return rc;
20510f2f
JM
1539}
1540
1541void security_task_to_inode(struct task_struct *p, struct inode *inode)
1542{
f25fce3e 1543 call_void_hook(task_to_inode, p, inode);
20510f2f
JM
1544}
1545
1546int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
1547{
f25fce3e 1548 return call_int_hook(ipc_permission, 0, ipcp, flag);
20510f2f
JM
1549}
1550
8a076191
AD
1551void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
1552{
b1d9e6b0 1553 *secid = 0;
f25fce3e 1554 call_void_hook(ipc_getsecid, ipcp, secid);
8a076191
AD
1555}
1556
20510f2f
JM
1557int security_msg_msg_alloc(struct msg_msg *msg)
1558{
7a90fc39
CS
1559 int rc = lsm_msg_msg_alloc(msg);
1560
1561 if (unlikely(rc))
1562 return rc;
1563 rc = call_int_hook(msg_msg_alloc_security, 0, msg);
1564 if (unlikely(rc))
1565 security_msg_msg_free(msg);
1566 return rc;
20510f2f
JM
1567}
1568
1569void security_msg_msg_free(struct msg_msg *msg)
1570{
f25fce3e 1571 call_void_hook(msg_msg_free_security, msg);
7a90fc39
CS
1572 kfree(msg->security);
1573 msg->security = NULL;
20510f2f
JM
1574}
1575
1576int security_msg_queue_alloc(struct msg_queue *msq)
1577{
7a90fc39
CS
1578 int rc = lsm_ipc_alloc(&msq->q_perm);
1579
1580 if (unlikely(rc))
1581 return rc;
1582 rc = call_int_hook(msg_queue_alloc_security, 0, msq);
1583 if (unlikely(rc))
1584 security_msg_queue_free(msq);
1585 return rc;
20510f2f
JM
1586}
1587
1588void security_msg_queue_free(struct msg_queue *msq)
1589{
7a90fc39
CS
1590 struct kern_ipc_perm *kip = &msq->q_perm;
1591
f25fce3e 1592 call_void_hook(msg_queue_free_security, msq);
7a90fc39
CS
1593 kfree(kip->security);
1594 kip->security = NULL;
20510f2f
JM
1595}
1596
1597int security_msg_queue_associate(struct msg_queue *msq, int msqflg)
1598{
f25fce3e 1599 return call_int_hook(msg_queue_associate, 0, msq, msqflg);
20510f2f
JM
1600}
1601
1602int security_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1603{
f25fce3e 1604 return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
20510f2f
JM
1605}
1606
1607int security_msg_queue_msgsnd(struct msg_queue *msq,
1608 struct msg_msg *msg, int msqflg)
1609{
f25fce3e 1610 return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
20510f2f
JM
1611}
1612
1613int security_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1614 struct task_struct *target, long type, int mode)
1615{
f25fce3e 1616 return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
20510f2f
JM
1617}
1618
1619int security_shm_alloc(struct shmid_kernel *shp)
1620{
7a90fc39
CS
1621 int rc = lsm_ipc_alloc(&shp->shm_perm);
1622
1623 if (unlikely(rc))
1624 return rc;
1625 rc = call_int_hook(shm_alloc_security, 0, shp);
1626 if (unlikely(rc))
1627 security_shm_free(shp);
1628 return rc;
20510f2f
JM
1629}
1630
1631void security_shm_free(struct shmid_kernel *shp)
1632{
7a90fc39
CS
1633 struct kern_ipc_perm *kip = &shp->shm_perm;
1634
f25fce3e 1635 call_void_hook(shm_free_security, shp);
7a90fc39
CS
1636 kfree(kip->security);
1637 kip->security = NULL;
20510f2f
JM
1638}
1639
1640int security_shm_associate(struct shmid_kernel *shp, int shmflg)
1641{
f25fce3e 1642 return call_int_hook(shm_associate, 0, shp, shmflg);
20510f2f
JM
1643}
1644
1645int security_shm_shmctl(struct shmid_kernel *shp, int cmd)
1646{
f25fce3e 1647 return call_int_hook(shm_shmctl, 0, shp, cmd);
20510f2f
JM
1648}
1649
1650int security_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, int shmflg)
1651{
f25fce3e 1652 return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
20510f2f
JM
1653}
1654
1655int security_sem_alloc(struct sem_array *sma)
1656{
7a90fc39
CS
1657 int rc = lsm_ipc_alloc(&sma->sem_perm);
1658
1659 if (unlikely(rc))
1660 return rc;
1661 rc = call_int_hook(sem_alloc_security, 0, sma);
1662 if (unlikely(rc))
1663 security_sem_free(sma);
1664 return rc;
20510f2f
JM
1665}
1666
1667void security_sem_free(struct sem_array *sma)
1668{
7a90fc39
CS
1669 struct kern_ipc_perm *kip = &sma->sem_perm;
1670
f25fce3e 1671 call_void_hook(sem_free_security, sma);
7a90fc39
CS
1672 kfree(kip->security);
1673 kip->security = NULL;
20510f2f
JM
1674}
1675
1676int security_sem_associate(struct sem_array *sma, int semflg)
1677{
f25fce3e 1678 return call_int_hook(sem_associate, 0, sma, semflg);
20510f2f
JM
1679}
1680
1681int security_sem_semctl(struct sem_array *sma, int cmd)
1682{
f25fce3e 1683 return call_int_hook(sem_semctl, 0, sma, cmd);
20510f2f
JM
1684}
1685
1686int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
1687 unsigned nsops, int alter)
1688{
f25fce3e 1689 return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
20510f2f
JM
1690}
1691
1692void security_d_instantiate(struct dentry *dentry, struct inode *inode)
1693{
1694 if (unlikely(inode && IS_PRIVATE(inode)))
1695 return;
f25fce3e 1696 call_void_hook(d_instantiate, dentry, inode);
20510f2f
JM
1697}
1698EXPORT_SYMBOL(security_d_instantiate);
1699
dcd148a1
CS
1700int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
1701 char **value)
20510f2f 1702{
dcd148a1
CS
1703 struct security_hook_list *hp;
1704
1705 list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
1706 if (lsm != NULL && strcmp(lsm, hp->lsm))
1707 continue;
1708 return hp->hook.getprocattr(p, name, value);
1709 }
1710 return -EINVAL;
20510f2f
JM
1711}
1712
dcd148a1
CS
1713int security_setprocattr(const char *lsm, const char *name, void *value,
1714 size_t size)
20510f2f 1715{
dcd148a1
CS
1716 struct security_hook_list *hp;
1717
1718 list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
1719 if (lsm != NULL && strcmp(lsm, hp->lsm))
1720 continue;
1721 return hp->hook.setprocattr(name, value, size);
1722 }
1723 return -EINVAL;
20510f2f
JM
1724}
1725
1726int security_netlink_send(struct sock *sk, struct sk_buff *skb)
1727{
f25fce3e 1728 return call_int_hook(netlink_send, 0, sk, skb);
20510f2f 1729}
20510f2f 1730
746df9b5
DQ
1731int security_ismaclabel(const char *name)
1732{
f25fce3e 1733 return call_int_hook(ismaclabel, 0, name);
746df9b5
DQ
1734}
1735EXPORT_SYMBOL(security_ismaclabel);
1736
20510f2f
JM
1737int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
1738{
b1d9e6b0
CS
1739 return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
1740 seclen);
20510f2f
JM
1741}
1742EXPORT_SYMBOL(security_secid_to_secctx);
1743
7bf570dc 1744int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
63cb3449 1745{
b1d9e6b0 1746 *secid = 0;
f25fce3e 1747 return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
63cb3449
DH
1748}
1749EXPORT_SYMBOL(security_secctx_to_secid);
1750
20510f2f
JM
1751void security_release_secctx(char *secdata, u32 seclen)
1752{
f25fce3e 1753 call_void_hook(release_secctx, secdata, seclen);
20510f2f
JM
1754}
1755EXPORT_SYMBOL(security_release_secctx);
1756
6f3be9f5
AG
1757void security_inode_invalidate_secctx(struct inode *inode)
1758{
1759 call_void_hook(inode_invalidate_secctx, inode);
1760}
1761EXPORT_SYMBOL(security_inode_invalidate_secctx);
1762
1ee65e37
DQ
1763int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
1764{
f25fce3e 1765 return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
1ee65e37
DQ
1766}
1767EXPORT_SYMBOL(security_inode_notifysecctx);
1768
1769int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
1770{
f25fce3e 1771 return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
1ee65e37
DQ
1772}
1773EXPORT_SYMBOL(security_inode_setsecctx);
1774
1775int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
1776{
b1d9e6b0 1777 return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
1ee65e37
DQ
1778}
1779EXPORT_SYMBOL(security_inode_getsecctx);
1780
20510f2f
JM
1781#ifdef CONFIG_SECURITY_NETWORK
1782
3610cda5 1783int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
20510f2f 1784{
f25fce3e 1785 return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
20510f2f
JM
1786}
1787EXPORT_SYMBOL(security_unix_stream_connect);
1788
1789int security_unix_may_send(struct socket *sock, struct socket *other)
1790{
f25fce3e 1791 return call_int_hook(unix_may_send, 0, sock, other);
20510f2f
JM
1792}
1793EXPORT_SYMBOL(security_unix_may_send);
1794
1795int security_socket_create(int family, int type, int protocol, int kern)
1796{
f25fce3e 1797 return call_int_hook(socket_create, 0, family, type, protocol, kern);
20510f2f
JM
1798}
1799
1800int security_socket_post_create(struct socket *sock, int family,
1801 int type, int protocol, int kern)
1802{
f25fce3e 1803 return call_int_hook(socket_post_create, 0, sock, family, type,
20510f2f
JM
1804 protocol, kern);
1805}
1806
1807int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
1808{
f25fce3e 1809 return call_int_hook(socket_bind, 0, sock, address, addrlen);
20510f2f
JM
1810}
1811
1812int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
1813{
f25fce3e 1814 return call_int_hook(socket_connect, 0, sock, address, addrlen);
20510f2f
JM
1815}
1816
1817int security_socket_listen(struct socket *sock, int backlog)
1818{
f25fce3e 1819 return call_int_hook(socket_listen, 0, sock, backlog);
20510f2f
JM
1820}
1821
1822int security_socket_accept(struct socket *sock, struct socket *newsock)
1823{
f25fce3e 1824 return call_int_hook(socket_accept, 0, sock, newsock);
20510f2f
JM
1825}
1826
20510f2f
JM
1827int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
1828{
f25fce3e 1829 return call_int_hook(socket_sendmsg, 0, sock, msg, size);
20510f2f
JM
1830}
1831
1832int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
1833 int size, int flags)
1834{
f25fce3e 1835 return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
20510f2f
JM
1836}
1837
1838int security_socket_getsockname(struct socket *sock)
1839{
f25fce3e 1840 return call_int_hook(socket_getsockname, 0, sock);
20510f2f
JM
1841}
1842
1843int security_socket_getpeername(struct socket *sock)
1844{
f25fce3e 1845 return call_int_hook(socket_getpeername, 0, sock);
20510f2f
JM
1846}
1847
1848int security_socket_getsockopt(struct socket *sock, int level, int optname)
1849{
f25fce3e 1850 return call_int_hook(socket_getsockopt, 0, sock, level, optname);
20510f2f
JM
1851}
1852
1853int security_socket_setsockopt(struct socket *sock, int level, int optname)
1854{
f25fce3e 1855 return call_int_hook(socket_setsockopt, 0, sock, level, optname);
20510f2f
JM
1856}
1857
1858int security_socket_shutdown(struct socket *sock, int how)
1859{
f25fce3e 1860 return call_int_hook(socket_shutdown, 0, sock, how);
20510f2f
JM
1861}
1862
1863int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1864{
f25fce3e 1865 return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
20510f2f
JM
1866}
1867EXPORT_SYMBOL(security_sock_rcv_skb);
1868
1869int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
1870 int __user *optlen, unsigned len)
1871{
b1d9e6b0
CS
1872 return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
1873 optval, optlen, len);
20510f2f
JM
1874}
1875
1876int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
1877{
e308fd3b
JB
1878 return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
1879 skb, secid);
20510f2f
JM
1880}
1881EXPORT_SYMBOL(security_socket_getpeersec_dgram);
1882
1883int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
1884{
7a90fc39
CS
1885 int rc = lsm_sock_alloc(sk, priority);
1886
1887 if (unlikely(rc))
1888 return rc;
1889 rc = call_int_hook(sk_alloc_security, 0, sk, family, priority);
1890 if (unlikely(rc))
1891 security_sk_free(sk);
1892 return rc;
20510f2f
JM
1893}
1894
1895void security_sk_free(struct sock *sk)
1896{
f25fce3e 1897 call_void_hook(sk_free_security, sk);
7a90fc39
CS
1898 kfree(sk->sk_security);
1899 sk->sk_security = NULL;
20510f2f
JM
1900}
1901
1902void security_sk_clone(const struct sock *sk, struct sock *newsk)
1903{
f25fce3e 1904 call_void_hook(sk_clone_security, sk, newsk);
20510f2f 1905}
6230c9b4 1906EXPORT_SYMBOL(security_sk_clone);
20510f2f
JM
1907
1908void security_sk_classify_flow(struct sock *sk, struct flowi *fl)
1909{
f25fce3e 1910 call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
20510f2f
JM
1911}
1912EXPORT_SYMBOL(security_sk_classify_flow);
1913
1914void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)
1915{
f25fce3e 1916 call_void_hook(req_classify_flow, req, fl);
20510f2f
JM
1917}
1918EXPORT_SYMBOL(security_req_classify_flow);
1919
1920void security_sock_graft(struct sock *sk, struct socket *parent)
1921{
f25fce3e 1922 call_void_hook(sock_graft, sk, parent);
20510f2f
JM
1923}
1924EXPORT_SYMBOL(security_sock_graft);
1925
1926int security_inet_conn_request(struct sock *sk,
1927 struct sk_buff *skb, struct request_sock *req)
1928{
f25fce3e 1929 return call_int_hook(inet_conn_request, 0, sk, skb, req);
20510f2f
JM
1930}
1931EXPORT_SYMBOL(security_inet_conn_request);
1932
1933void security_inet_csk_clone(struct sock *newsk,
1934 const struct request_sock *req)
1935{
f25fce3e 1936 call_void_hook(inet_csk_clone, newsk, req);
20510f2f
JM
1937}
1938
1939void security_inet_conn_established(struct sock *sk,
1940 struct sk_buff *skb)
1941{
f25fce3e 1942 call_void_hook(inet_conn_established, sk, skb);
20510f2f
JM
1943}
1944
2606fd1f
EP
1945int security_secmark_relabel_packet(u32 secid)
1946{
f25fce3e 1947 return call_int_hook(secmark_relabel_packet, 0, secid);
2606fd1f
EP
1948}
1949EXPORT_SYMBOL(security_secmark_relabel_packet);
1950
1951void security_secmark_refcount_inc(void)
1952{
f25fce3e 1953 call_void_hook(secmark_refcount_inc);
2606fd1f
EP
1954}
1955EXPORT_SYMBOL(security_secmark_refcount_inc);
1956
1957void security_secmark_refcount_dec(void)
1958{
f25fce3e 1959 call_void_hook(secmark_refcount_dec);
2606fd1f
EP
1960}
1961EXPORT_SYMBOL(security_secmark_refcount_dec);
1962
5dbbaf2d
PM
1963int security_tun_dev_alloc_security(void **security)
1964{
f25fce3e 1965 return call_int_hook(tun_dev_alloc_security, 0, security);
5dbbaf2d
PM
1966}
1967EXPORT_SYMBOL(security_tun_dev_alloc_security);
1968
1969void security_tun_dev_free_security(void *security)
1970{
f25fce3e 1971 call_void_hook(tun_dev_free_security, security);
5dbbaf2d
PM
1972}
1973EXPORT_SYMBOL(security_tun_dev_free_security);
1974
2b980dbd
PM
1975int security_tun_dev_create(void)
1976{
f25fce3e 1977 return call_int_hook(tun_dev_create, 0);
2b980dbd
PM
1978}
1979EXPORT_SYMBOL(security_tun_dev_create);
1980
5dbbaf2d 1981int security_tun_dev_attach_queue(void *security)
2b980dbd 1982{
f25fce3e 1983 return call_int_hook(tun_dev_attach_queue, 0, security);
2b980dbd 1984}
5dbbaf2d 1985EXPORT_SYMBOL(security_tun_dev_attach_queue);
2b980dbd 1986
5dbbaf2d 1987int security_tun_dev_attach(struct sock *sk, void *security)
2b980dbd 1988{
f25fce3e 1989 return call_int_hook(tun_dev_attach, 0, sk, security);
2b980dbd
PM
1990}
1991EXPORT_SYMBOL(security_tun_dev_attach);
1992
5dbbaf2d
PM
1993int security_tun_dev_open(void *security)
1994{
f25fce3e 1995 return call_int_hook(tun_dev_open, 0, security);
5dbbaf2d
PM
1996}
1997EXPORT_SYMBOL(security_tun_dev_open);
1998
20510f2f
JM
1999#endif /* CONFIG_SECURITY_NETWORK */
2000
d291f1a6
DJ
2001#ifdef CONFIG_SECURITY_INFINIBAND
2002
2003int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
2004{
2005 return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
2006}
2007EXPORT_SYMBOL(security_ib_pkey_access);
2008
47a2b338
DJ
2009int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
2010{
2011 return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
2012}
2013EXPORT_SYMBOL(security_ib_endport_manage_subnet);
2014
d291f1a6
DJ
2015int security_ib_alloc_security(void **sec)
2016{
2017 return call_int_hook(ib_alloc_security, 0, sec);
2018}
2019EXPORT_SYMBOL(security_ib_alloc_security);
2020
2021void security_ib_free_security(void *sec)
2022{
2023 call_void_hook(ib_free_security, sec);
2024}
2025EXPORT_SYMBOL(security_ib_free_security);
2026#endif /* CONFIG_SECURITY_INFINIBAND */
2027
20510f2f
JM
2028#ifdef CONFIG_SECURITY_NETWORK_XFRM
2029
52a4c640
NA
2030int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
2031 struct xfrm_user_sec_ctx *sec_ctx,
2032 gfp_t gfp)
20510f2f 2033{
f25fce3e 2034 return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
20510f2f
JM
2035}
2036EXPORT_SYMBOL(security_xfrm_policy_alloc);
2037
03e1ad7b
PM
2038int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
2039 struct xfrm_sec_ctx **new_ctxp)
20510f2f 2040{
f25fce3e 2041 return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
20510f2f
JM
2042}
2043
03e1ad7b 2044void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
20510f2f 2045{
f25fce3e 2046 call_void_hook(xfrm_policy_free_security, ctx);
20510f2f
JM
2047}
2048EXPORT_SYMBOL(security_xfrm_policy_free);
2049
03e1ad7b 2050int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
20510f2f 2051{
f25fce3e 2052 return call_int_hook(xfrm_policy_delete_security, 0, ctx);
20510f2f
JM
2053}
2054
2e5aa866
PM
2055int security_xfrm_state_alloc(struct xfrm_state *x,
2056 struct xfrm_user_sec_ctx *sec_ctx)
20510f2f 2057{
f25fce3e 2058 return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
20510f2f
JM
2059}
2060EXPORT_SYMBOL(security_xfrm_state_alloc);
2061
2062int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
2063 struct xfrm_sec_ctx *polsec, u32 secid)
2064{
f25fce3e 2065 return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
20510f2f
JM
2066}
2067
2068int security_xfrm_state_delete(struct xfrm_state *x)
2069{
f25fce3e 2070 return call_int_hook(xfrm_state_delete_security, 0, x);
20510f2f
JM
2071}
2072EXPORT_SYMBOL(security_xfrm_state_delete);
2073
2074void security_xfrm_state_free(struct xfrm_state *x)
2075{
f25fce3e 2076 call_void_hook(xfrm_state_free_security, x);
20510f2f
JM
2077}
2078
03e1ad7b 2079int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
20510f2f 2080{
f25fce3e 2081 return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
20510f2f
JM
2082}
2083
2084int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
e33f7704
DM
2085 struct xfrm_policy *xp,
2086 const struct flowi *fl)
20510f2f 2087{
b1d9e6b0
CS
2088 struct security_hook_list *hp;
2089 int rc = 1;
2090
2091 /*
2092 * Since this function is expected to return 0 or 1, the judgment
2093 * becomes difficult if multiple LSMs supply this call. Fortunately,
2094 * we can use the first LSM's judgment because currently only SELinux
2095 * supplies this call.
2096 *
2097 * For speed optimization, we explicitly break the loop rather than
2098 * using the macro
2099 */
2100 list_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
2101 list) {
2102 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
2103 break;
2104 }
2105 return rc;
20510f2f
JM
2106}
2107
2108int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
2109{
f25fce3e 2110 return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
20510f2f
JM
2111}
2112
2113void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
2114{
f25fce3e
CS
2115 int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl->flowi_secid,
2116 0);
20510f2f
JM
2117
2118 BUG_ON(rc);
2119}
2120EXPORT_SYMBOL(security_skb_classify_flow);
2121
2122#endif /* CONFIG_SECURITY_NETWORK_XFRM */
2123
2124#ifdef CONFIG_KEYS
2125
d84f4f99
DH
2126int security_key_alloc(struct key *key, const struct cred *cred,
2127 unsigned long flags)
20510f2f 2128{
7a90fc39
CS
2129 int rc = lsm_key_alloc(key);
2130
2131 if (unlikely(rc))
2132 return rc;
2133 rc = call_int_hook(key_alloc, 0, key, cred, flags);
2134 if (unlikely(rc))
2135 security_key_free(key);
2136 return rc;
20510f2f
JM
2137}
2138
2139void security_key_free(struct key *key)
2140{
f25fce3e 2141 call_void_hook(key_free, key);
7a90fc39
CS
2142 kfree(key->security);
2143 key->security = NULL;
20510f2f
JM
2144}
2145
2146int security_key_permission(key_ref_t key_ref,
f5895943 2147 const struct cred *cred, unsigned perm)
20510f2f 2148{
f25fce3e 2149 return call_int_hook(key_permission, 0, key_ref, cred, perm);
20510f2f
JM
2150}
2151
70a5bb72
DH
2152int security_key_getsecurity(struct key *key, char **_buffer)
2153{
b1d9e6b0 2154 *_buffer = NULL;
f25fce3e 2155 return call_int_hook(key_getsecurity, 0, key, _buffer);
70a5bb72
DH
2156}
2157
20510f2f 2158#endif /* CONFIG_KEYS */
03d37d25
AD
2159
2160#ifdef CONFIG_AUDIT
2161
2162int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2163{
f25fce3e 2164 return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
03d37d25
AD
2165}
2166
2167int security_audit_rule_known(struct audit_krule *krule)
2168{
f25fce3e 2169 return call_int_hook(audit_rule_known, 0, krule);
03d37d25
AD
2170}
2171
2172void security_audit_rule_free(void *lsmrule)
2173{
f25fce3e 2174 call_void_hook(audit_rule_free, lsmrule);
03d37d25
AD
2175}
2176
2177int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
2178 struct audit_context *actx)
2179{
f25fce3e
CS
2180 return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule,
2181 actx);
03d37d25 2182}
b1d9e6b0 2183#endif /* CONFIG_AUDIT */
afdb09c7
CF
2184
2185#ifdef CONFIG_BPF_SYSCALL
2186int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
2187{
2188 return call_int_hook(bpf, 0, cmd, attr, size);
2189}
2190int security_bpf_map(struct bpf_map *map, fmode_t fmode)
2191{
2192 return call_int_hook(bpf_map, 0, map, fmode);
2193}
2194int security_bpf_prog(struct bpf_prog *prog)
2195{
2196 return call_int_hook(bpf_prog, 0, prog);
2197}
2198int security_bpf_map_alloc(struct bpf_map *map)
2199{
2200 return call_int_hook(bpf_map_alloc_security, 0, map);
2201}
2202int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
2203{
2204 return call_int_hook(bpf_prog_alloc_security, 0, aux);
2205}
2206void security_bpf_map_free(struct bpf_map *map)
2207{
2208 call_void_hook(bpf_map_free_security, map);
2209}
2210void security_bpf_prog_free(struct bpf_prog_aux *aux)
2211{
2212 call_void_hook(bpf_prog_free_security, aux);
2213}
2214#endif /* CONFIG_BPF_SYSCALL */