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