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