]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/security.c
UBUNTU: SAUCE: LSM stacking: add Kconfig to set default display 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
469fbb39 58 .lbs_task = SECURITY_NAME_MAX + 6,
a69a0684
JJ
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
adf8a6ee
JJ
69static __initdata char chosen_display_lsm[SECURITY_NAME_MAX + 1]
70#ifdef CONFIG_SECURITY_STACKING
71 = CONFIG_SECURITY_DEFAULT_DISPLAY_NAME
72#endif
73;
6a3b4b47 74static char default_display_lsm[SECURITY_NAME_MAX + 1];
1da177e4 75
1da177e4
LT
76static void __init do_security_initcalls(void)
77{
78 initcall_t *call;
79 call = __security_initcall_start;
80 while (call < __security_initcall_end) {
81 (*call) ();
82 call++;
83 }
84}
85
86/**
87 * security_init - initializes the security framework
88 *
89 * This should be called early in the kernel initialization sequence.
90 */
91int __init security_init(void)
92{
3dfc9b02
TH
93 int i;
94 struct list_head *list = (struct list_head *) &security_hook_heads;
95
96 for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct list_head);
97 i++)
98 INIT_LIST_HEAD(&list[i]);
b1d9e6b0 99 pr_info("Security Framework initialized\n");
1da177e4 100
b1d9e6b0 101 /*
730daa16 102 * Load minor LSMs, with the capability module always first.
b1d9e6b0
CS
103 */
104 capability_add_hooks();
b1d9e6b0 105 yama_add_hooks();
9b091556 106 loadpin_add_hooks();
730daa16 107
b1d9e6b0 108 /*
f1efc8c4
CS
109 * The first call to a module specific init function
110 * updates the blob size requirements.
111 */
112 do_security_initcalls();
113
ab9b110b
CS
114 /*
115 * Create any kmem_caches needed for blobs
116 */
117 if (blob_sizes.lbs_file)
118 lsm_file_cache = kmem_cache_create("lsm_file_cache",
119 blob_sizes.lbs_file, 0,
120 SLAB_PANIC, NULL);
7a90fc39
CS
121 if (blob_sizes.lbs_inode)
122 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
123 blob_sizes.lbs_inode, 0,
124 SLAB_PANIC, NULL);
f1efc8c4
CS
125 /*
126 * The second call to a module specific init function
127 * adds hooks to the hook lists and does any other early
128 * initializations required.
b1d9e6b0 129 */
1da177e4
LT
130 do_security_initcalls();
131
f1efc8c4
CS
132#ifdef CONFIG_SECURITY_LSM_DEBUG
133 pr_info("LSM: cred blob size = %d\n", blob_sizes.lbs_cred);
ab9b110b 134 pr_info("LSM: file blob size = %d\n", blob_sizes.lbs_file);
7a90fc39
CS
135 pr_info("LSM: inode blob size = %d\n", blob_sizes.lbs_inode);
136 pr_info("LSM: ipc blob size = %d\n", blob_sizes.lbs_ipc);
137#ifdef CONFIG_KEYS
138 pr_info("LSM: key blob size = %d\n", blob_sizes.lbs_key);
139#endif /* CONFIG_KEYS */
140 pr_info("LSM: msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
141 pr_info("LSM: sock blob size = %d\n", blob_sizes.lbs_sock);
142 pr_info("LSM: superblock blob size = %d\n", blob_sizes.lbs_superblock);
7f79f508 143 pr_info("LSM: task blob size = %d\n", blob_sizes.lbs_task);
7a90fc39 144#endif /* CONFIG_SECURITY_LSM_DEBUG */
f1efc8c4 145
1da177e4
LT
146 return 0;
147}
148
076c54c5
AD
149/* Save user chosen LSM */
150static int __init choose_lsm(char *str)
151{
f9047a0c
JJ
152 strncpy(chosen_lsms, str, SECURITY_CHOSEN_NAMES_MAX);
153 pr_info("LSM: command line set '%s' security module(s).\n",
154 chosen_lsms);
076c54c5
AD
155 return 1;
156}
157__setup("security=", choose_lsm);
158
6a3b4b47
JJ
159static int __init choose_display_lsm(char *str)
160{
161 strncpy(chosen_display_lsm, str, SECURITY_NAME_MAX);
162 pr_info("LSM: command line set default display lsm %s'\n",
163 chosen_display_lsm);
164 return 1;
165}
166__setup("security.display=", choose_display_lsm);
167
3bb857e4
MS
168static bool match_last_lsm(const char *list, const char *lsm)
169{
170 const char *last;
171
172 if (WARN_ON(!list || !lsm))
173 return false;
174 last = strrchr(list, ',');
175 if (last)
176 /* Pass the comma, strcmp() will check for '\0' */
177 last++;
178 else
179 last = list;
180 return !strcmp(last, lsm);
181}
182
d69dece5
CS
183static int lsm_append(char *new, char **result)
184{
185 char *cp;
186
187 if (*result == NULL) {
188 *result = kstrdup(new, GFP_KERNEL);
189 } else {
3bb857e4
MS
190 /* Check if it is the last registered name */
191 if (match_last_lsm(*result, new))
192 return 0;
d69dece5
CS
193 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
194 if (cp == NULL)
195 return -ENOMEM;
196 kfree(*result);
197 *result = cp;
198 }
199 return 0;
200}
201
076c54c5
AD
202/**
203 * security_module_enable - Load given security module on boot ?
b1d9e6b0 204 * @module: the name of the module
54f4b36b 205 * @stacked: indicates that the module wants to be stacked
076c54c5
AD
206 *
207 * Each LSM must pass this method before registering its own operations
208 * to avoid security registration races. This method may also be used
7cea51be 209 * to check if your LSM is currently loaded during kernel initialization.
076c54c5 210 *
0e056eb5
MCC
211 * Returns:
212 *
213 * true if:
214 *
215 * - The passed LSM is the one chosen by user at boot time,
216 * - or the passed LSM is configured as the default and the user did not
217 * choose an alternate LSM at boot time.
218 *
076c54c5
AD
219 * Otherwise, return false.
220 */
f9047a0c
JJ
221#ifdef CONFIG_SECURITY_STACKING
222static bool __init cmp_lsms(const char *lsm)
223{
224 const char *str = chosen_lsms;
225 const char *split;
226 int len = strlen(lsm);
227
228 if (len > SECURITY_NAME_MAX) {
229 pr_info("LSM: security module name '%s' exceeds limit\n", lsm);
230 return false;
231 }
232 for (split = strchr(str, ','); split; split = strchr(str, ',')) {
233 if ((len == split - str) && !strncmp(lsm, str, split - str))
234 return true;
235 str = split + 1;
236 }
237 if ((len == strlen(str)) && !strncmp(lsm, str, strlen(str)))
238 return true;
239 return false;
240}
241#endif
242
54f4b36b 243bool __init security_module_enable(const char *lsm, const bool stacked)
076c54c5 244{
54f4b36b
CS
245#ifdef CONFIG_SECURITY_STACKING
246 /*
247 * Module defined on the command line security=XXXX
248 */
6a3b4b47
JJ
249 if (strcmp(chosen_lsms, MODULE_STACK)) {
250 if (cmp_lsms(lsm)) {
251 /* set to first LSM registered and then override */
252 if (!*default_display_lsm)
253 strcpy(default_display_lsm, lsm);
254 else if (*chosen_display_lsm && !strcmp(chosen_display_lsm, lsm)) {
255 strcpy(default_display_lsm, lsm);
256 pr_info("LSM: default display lsm '%s'\n", default_display_lsm);
257 }
258 return true;
259 }
260 return false;
261 }
54f4b36b
CS
262 /*
263 * Module configured as stacked.
264 */
6a3b4b47
JJ
265 if (stacked && !*default_display_lsm)
266 strcpy(default_display_lsm, lsm);
267 else if (stacked && *chosen_display_lsm && !strcmp(chosen_display_lsm, lsm)) {
268 strcpy(default_display_lsm, lsm);
269 pr_info("LSM: default display lsm '%s'\n", default_display_lsm);
270 }
271
54f4b36b
CS
272 return stacked;
273#else
6a3b4b47
JJ
274 if (strcmp(lsm, chosen_lsms) == 0) {
275 strcpy(default_display_lsm, lsm);
54f4b36b 276 return true;
6a3b4b47 277 }
54f4b36b
CS
278 return false;
279#endif
076c54c5
AD
280}
281
d0c8f0ad
JJ
282/*
283 * Keep the order of major modules for mapping secids.
284 */
285static int lsm_next_major;
286
d69dece5
CS
287/**
288 * security_add_hooks - Add a modules hooks to the hook lists.
289 * @hooks: the hooks to add
290 * @count: the number of hooks to add
291 * @lsm: the name of the security module
292 *
293 * Each LSM has to register its hooks with the infrastructure.
294 */
295void __init security_add_hooks(struct security_hook_list *hooks, int count,
296 char *lsm)
297{
298 int i;
d0c8f0ad
JJ
299 int lsm_index = lsm_next_major++;
300
301#ifdef CONFIG_SECURITY_LSM_DEBUG
302 pr_info("LSM: Security module %s gets index %d\n", lsm, lsm_index);
303#endif
d69dece5
CS
304 for (i = 0; i < count; i++) {
305 hooks[i].lsm = lsm;
d0c8f0ad 306 hooks[i].lsm_index = lsm_index;
d69dece5
CS
307 list_add_tail_rcu(&hooks[i].list, hooks[i].head);
308 }
309 if (lsm_append(lsm, &lsm_names) < 0)
310 panic("%s - Cannot get early memory.\n", __func__);
311}
312
8f408ab6
DJ
313int call_lsm_notifier(enum lsm_event event, void *data)
314{
315 return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
316}
317EXPORT_SYMBOL(call_lsm_notifier);
318
319int register_lsm_notifier(struct notifier_block *nb)
320{
321 return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
322}
323EXPORT_SYMBOL(register_lsm_notifier);
324
325int unregister_lsm_notifier(struct notifier_block *nb)
326{
327 return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
328}
329EXPORT_SYMBOL(unregister_lsm_notifier);
330
f1efc8c4
CS
331/**
332 * lsm_cred_alloc - allocate a composite cred blob
333 * @cred: the cred that needs a blob
334 * @gfp: allocation type
335 *
336 * Allocate the cred blob for all the modules
337 *
338 * Returns 0, or -ENOMEM if memory can't be allocated.
339 */
340int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
341{
342 if (blob_sizes.lbs_cred == 0) {
343 cred->security = NULL;
344 return 0;
345 }
346
347 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
348 if (cred->security == NULL)
349 return -ENOMEM;
350 return 0;
351}
352
353/**
354 * lsm_early_cred - during initialization allocate a composite cred blob
355 * @cred: the cred that needs a blob
356 *
357 * Allocate the cred blob for all the modules if it's not already there
358 */
359void lsm_early_cred(struct cred *cred)
360{
361 int rc;
362
363 if (cred == NULL)
364 panic("%s: NULL cred.\n", __func__);
365 if (cred->security != NULL)
366 return;
367 rc = lsm_cred_alloc(cred, GFP_KERNEL);
368 if (rc)
369 panic("%s: Early cred alloc failed.\n", __func__);
370}
371
372static void __init lsm_set_size(int *need, int *lbs)
373{
374 int offset;
375
376 if (*need > 0) {
377 offset = *lbs;
378 *lbs += *need;
379 *need = offset;
380 }
381}
382
383/**
384 * security_add_blobs - Report blob sizes
385 * @needed: the size of blobs needed by the module
386 *
387 * Each LSM has to register its blobs with the infrastructure.
388 * The "needed" data tells the infrastructure how much memory
389 * the module requires for each of its blobs. On return the
390 * structure is filled with the offset that module should use
391 * from the blob pointer.
392 */
393void __init security_add_blobs(struct lsm_blob_sizes *needed)
394{
395 lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
ab9b110b 396 lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
7a90fc39
CS
397 lsm_set_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
398 lsm_set_size(&needed->lbs_key, &blob_sizes.lbs_key);
399 lsm_set_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
400 lsm_set_size(&needed->lbs_sock, &blob_sizes.lbs_sock);
401 lsm_set_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
7f79f508 402 lsm_set_size(&needed->lbs_task, &blob_sizes.lbs_task);
7a90fc39
CS
403 /*
404 * The inode blob gets an rcu_head in addition to
405 * what the modules might need.
406 */
407 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
408 blob_sizes.lbs_inode = sizeof(struct rcu_head);
409 lsm_set_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
ab9b110b
CS
410}
411
412/**
413 * lsm_file_alloc - allocate a composite file blob
414 * @file: the file that needs a blob
415 *
416 * Allocate the file blob for all the modules
417 *
418 * Returns 0, or -ENOMEM if memory can't be allocated.
419 */
420int lsm_file_alloc(struct file *file)
421{
422 if (!lsm_file_cache) {
423 file->f_security = NULL;
424 return 0;
425 }
426
427 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
428 if (file->f_security == NULL)
429 return -ENOMEM;
430 return 0;
f1efc8c4
CS
431}
432
a69a0684
JJ
433#ifdef CONFIG_SECURITY_STACKING
434static inline char *lsm_of_task(struct task_struct *task)
435{
436#ifdef CONFIG_SECURITY_LSM_DEBUG
437 if (task->security == NULL)
438 pr_info("%s: task has no lsm name.\n", __func__);
439#endif
440 return task->security;
441}
442#endif
443
444#ifdef CONFIG_SECURITY_STACKING
445struct lsm_value {
446 char *lsm;
447 char *data;
448};
449
450/**
451 * lsm_parse_context - break a compound "context" into module data
452 * @cxt: the initial data, which will be modified
453 * @vlist: an array to receive the results
454 *
455 * Returns the number of entries, or -EINVAL if the cxt is unworkable.
456 */
457static int lsm_parse_context(char *cxt, struct lsm_value *vlist)
458{
459 char *lsm;
460 char *data;
461 char *cp;
462 int i;
463
464 lsm = cxt;
465 for (i = 0; i < LSM_MAX_MAJOR; i++) {
466 data = strstr(lsm, "='");
467 if (!data)
468 break;
469 *data = '\0';
470 data += 2;
471 cp = strchr(data, '\'');
472 if (!cp)
473 return -EINVAL;
474 *cp++ = '\0';
475 vlist[i].lsm = lsm;
476 vlist[i].data = data;
477 if (*cp == '\0') {
478 i++;
479 break;
480 }
481 if (*cp == ',')
482 cp++;
483 else
484 return -EINVAL;
485 lsm = cp;
486 }
487 return i;
488}
489#endif /* CONFIG_SECURITY_STACKING */
490
7f79f508
CS
491/**
492 * lsm_task_alloc - allocate a composite task blob
493 * @task: the task that needs a blob
494 *
495 * Allocate the task blob for all the modules
496 *
497 * Returns 0, or -ENOMEM if memory can't be allocated.
498 */
499int lsm_task_alloc(struct task_struct *task)
500{
501 if (blob_sizes.lbs_task == 0) {
502 task->security = NULL;
503 return 0;
504 }
505
506 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
507 if (task->security == NULL)
508 return -ENOMEM;
76afa12e
JJ
509
510 /* inherit current display lsm */
511#ifdef CONFIG_SECURITY_STACKING
512 if (current->security)
513 strcpy(task->security, lsm_of_task(current));
6a3b4b47
JJ
514 else
515 strcpy(task->security, default_display_lsm);
76afa12e 516#endif
7f79f508
CS
517 return 0;
518}
519
7a90fc39
CS
520/**
521 * lsm_inode_alloc - allocate a composite inode blob
522 * @inode: the inode that needs a blob
523 *
524 * Allocate the inode blob for all the modules
525 *
526 * Returns 0, or -ENOMEM if memory can't be allocated.
527 */
528int lsm_inode_alloc(struct inode *inode)
529{
530 if (!lsm_inode_cache) {
531 inode->i_security = NULL;
532 return 0;
533 }
534
535 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_KERNEL);
536 if (inode->i_security == NULL)
537 return -ENOMEM;
538 return 0;
539}
540
541/**
542 * lsm_early_inode - during initialization allocate a composite inode blob
543 * @inode: the inode that needs a blob
544 *
545 * Allocate the inode blob for all the modules if it's not already there
546 */
547void lsm_early_inode(struct inode *inode)
548{
549 int rc;
550
551 if (inode == NULL)
552 panic("%s: NULL inode.\n", __func__);
553 if (inode->i_security != NULL)
554 return;
555 rc = lsm_inode_alloc(inode);
556 if (rc)
557 panic("%s: Early inode alloc failed.\n", __func__);
558}
559
560/**
561 * lsm_ipc_alloc - allocate a composite ipc blob
562 * @kip: the ipc that needs a blob
563 *
564 * Allocate the ipc blob for all the modules
565 *
566 * Returns 0, or -ENOMEM if memory can't be allocated.
567 */
568int lsm_ipc_alloc(struct kern_ipc_perm *kip)
569{
570 if (blob_sizes.lbs_ipc == 0) {
571 kip->security = NULL;
572 return 0;
573 }
574
575 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
576 if (kip->security == NULL)
577 return -ENOMEM;
578 return 0;
579}
580
581#ifdef CONFIG_KEYS
582/**
583 * lsm_key_alloc - allocate a composite key blob
584 * @key: the key that needs a blob
585 *
586 * Allocate the key blob for all the modules
587 *
588 * Returns 0, or -ENOMEM if memory can't be allocated.
589 */
590int lsm_key_alloc(struct key *key)
591{
592 if (blob_sizes.lbs_key == 0) {
593 key->security = NULL;
594 return 0;
595 }
596
597 key->security = kzalloc(blob_sizes.lbs_key, GFP_KERNEL);
598 if (key->security == NULL)
599 return -ENOMEM;
600 return 0;
601}
602#endif /* CONFIG_KEYS */
603
604/**
605 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
606 * @mp: the msg_msg that needs a blob
607 *
608 * Allocate the ipc blob for all the modules
609 *
610 * Returns 0, or -ENOMEM if memory can't be allocated.
611 */
612int lsm_msg_msg_alloc(struct msg_msg *mp)
613{
614 if (blob_sizes.lbs_msg_msg == 0) {
615 mp->security = NULL;
616 return 0;
617 }
618
619 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
620 if (mp->security == NULL)
621 return -ENOMEM;
622 return 0;
623}
624
625/**
626 * lsm_sock_alloc - allocate a composite sock blob
627 * @sock: the sock that needs a blob
628 * @priority: allocation mode
629 *
630 * Allocate the sock blob for all the modules
631 *
632 * Returns 0, or -ENOMEM if memory can't be allocated.
633 */
634int lsm_sock_alloc(struct sock *sock, gfp_t priority)
635{
636 if (blob_sizes.lbs_sock == 0) {
637 sock->sk_security = NULL;
638 return 0;
639 }
640
641 sock->sk_security = kzalloc(blob_sizes.lbs_sock, priority);
642 if (sock->sk_security == NULL)
643 return -ENOMEM;
644 return 0;
645}
646
647/**
648 * lsm_superblock_alloc - allocate a composite superblock blob
649 * @sb: the superblock that needs a blob
650 *
651 * Allocate the superblock blob for all the modules
652 *
653 * Returns 0, or -ENOMEM if memory can't be allocated.
654 */
655int lsm_superblock_alloc(struct super_block *sb)
656{
657 if (blob_sizes.lbs_superblock == 0) {
658 sb->s_security = NULL;
659 return 0;
660 }
661
662 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
663 if (sb->s_security == NULL)
664 return -ENOMEM;
665 return 0;
666}
667
f25fce3e 668/*
b1d9e6b0 669 * Hook list operation macros.
1da177e4 670 *
f25fce3e
CS
671 * call_void_hook:
672 * This is a hook that does not return a value.
1da177e4 673 *
f25fce3e
CS
674 * call_int_hook:
675 * This is a hook that returns a value.
1da177e4 676 */
1da177e4 677
b1d9e6b0
CS
678#define call_void_hook(FUNC, ...) \
679 do { \
680 struct security_hook_list *P; \
681 \
682 list_for_each_entry(P, &security_hook_heads.FUNC, list) \
683 P->hook.FUNC(__VA_ARGS__); \
684 } while (0)
685
686#define call_int_hook(FUNC, IRC, ...) ({ \
687 int RC = IRC; \
688 do { \
689 struct security_hook_list *P; \
690 \
691 list_for_each_entry(P, &security_hook_heads.FUNC, list) { \
692 RC = P->hook.FUNC(__VA_ARGS__); \
693 if (RC != 0) \
694 break; \
695 } \
696 } while (0); \
697 RC; \
698})
1da177e4 699
20510f2f
JM
700/* Security operations */
701
79af7307
SS
702int security_binder_set_context_mgr(struct task_struct *mgr)
703{
f25fce3e 704 return call_int_hook(binder_set_context_mgr, 0, mgr);
79af7307
SS
705}
706
707int security_binder_transaction(struct task_struct *from,
708 struct task_struct *to)
709{
f25fce3e 710 return call_int_hook(binder_transaction, 0, from, to);
79af7307
SS
711}
712
713int security_binder_transfer_binder(struct task_struct *from,
714 struct task_struct *to)
715{
f25fce3e 716 return call_int_hook(binder_transfer_binder, 0, from, to);
79af7307
SS
717}
718
719int security_binder_transfer_file(struct task_struct *from,
720 struct task_struct *to, struct file *file)
721{
f25fce3e 722 return call_int_hook(binder_transfer_file, 0, from, to, file);
79af7307
SS
723}
724
9e48858f 725int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
20510f2f 726{
f25fce3e 727 return call_int_hook(ptrace_access_check, 0, child, mode);
5cd9c58f
DH
728}
729
730int security_ptrace_traceme(struct task_struct *parent)
731{
f25fce3e 732 return call_int_hook(ptrace_traceme, 0, parent);
20510f2f
JM
733}
734
735int security_capget(struct task_struct *target,
736 kernel_cap_t *effective,
737 kernel_cap_t *inheritable,
738 kernel_cap_t *permitted)
739{
f25fce3e
CS
740 return call_int_hook(capget, 0, target,
741 effective, inheritable, permitted);
20510f2f
JM
742}
743
d84f4f99
DH
744int security_capset(struct cred *new, const struct cred *old,
745 const kernel_cap_t *effective,
746 const kernel_cap_t *inheritable,
747 const kernel_cap_t *permitted)
20510f2f 748{
f25fce3e
CS
749 return call_int_hook(capset, 0, new, old,
750 effective, inheritable, permitted);
20510f2f
JM
751}
752
b7e724d3 753int security_capable(const struct cred *cred, struct user_namespace *ns,
3486740a 754 int cap)
20510f2f 755{
f25fce3e 756 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
06112163
EP
757}
758
c7eba4a9
EP
759int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns,
760 int cap)
06112163 761{
f25fce3e 762 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT);
20510f2f
JM
763}
764
20510f2f
JM
765int security_quotactl(int cmds, int type, int id, struct super_block *sb)
766{
f25fce3e 767 return call_int_hook(quotactl, 0, cmds, type, id, sb);
20510f2f
JM
768}
769
770int security_quota_on(struct dentry *dentry)
771{
f25fce3e 772 return call_int_hook(quota_on, 0, dentry);
20510f2f
JM
773}
774
12b3052c 775int security_syslog(int type)
20510f2f 776{
f25fce3e 777 return call_int_hook(syslog, 0, type);
20510f2f
JM
778}
779
457db29b 780int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
20510f2f 781{
f25fce3e 782 return call_int_hook(settime, 0, ts, tz);
20510f2f
JM
783}
784
20510f2f
JM
785int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
786{
b1d9e6b0
CS
787 struct security_hook_list *hp;
788 int cap_sys_admin = 1;
789 int rc;
790
791 /*
792 * The module will respond with a positive value if
793 * it thinks the __vm_enough_memory() call should be
794 * made with the cap_sys_admin set. If all of the modules
795 * agree that it should be set it will. If any module
796 * thinks it should not be set it won't.
797 */
798 list_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
799 rc = hp->hook.vm_enough_memory(mm, pages);
800 if (rc <= 0) {
801 cap_sys_admin = 0;
802 break;
803 }
804 }
805 return __vm_enough_memory(mm, pages, cap_sys_admin);
20510f2f
JM
806}
807
a6f76f23 808int security_bprm_set_creds(struct linux_binprm *bprm)
20510f2f 809{
f25fce3e 810 return call_int_hook(bprm_set_creds, 0, bprm);
20510f2f
JM
811}
812
a6f76f23 813int security_bprm_check(struct linux_binprm *bprm)
20510f2f 814{
6c21a7fb
MZ
815 int ret;
816
f25fce3e 817 ret = call_int_hook(bprm_check_security, 0, bprm);
6c21a7fb
MZ
818 if (ret)
819 return ret;
820 return ima_bprm_check(bprm);
20510f2f
JM
821}
822
a6f76f23 823void security_bprm_committing_creds(struct linux_binprm *bprm)
20510f2f 824{
f25fce3e 825 call_void_hook(bprm_committing_creds, bprm);
20510f2f
JM
826}
827
a6f76f23 828void security_bprm_committed_creds(struct linux_binprm *bprm)
20510f2f 829{
f25fce3e 830 call_void_hook(bprm_committed_creds, bprm);
20510f2f
JM
831}
832
20510f2f
JM
833int security_sb_alloc(struct super_block *sb)
834{
7a90fc39
CS
835 int rc = lsm_superblock_alloc(sb);
836
837 if (unlikely(rc))
838 return rc;
839 rc = call_int_hook(sb_alloc_security, 0, sb);
840 if (unlikely(rc))
841 security_sb_free(sb);
842 return rc;
20510f2f
JM
843}
844
845void security_sb_free(struct super_block *sb)
846{
f25fce3e 847 call_void_hook(sb_free_security, sb);
7a90fc39
CS
848 kfree(sb->s_security);
849 sb->s_security = NULL;
20510f2f
JM
850}
851
e0007529 852int security_sb_copy_data(char *orig, char *copy)
20510f2f 853{
f25fce3e 854 return call_int_hook(sb_copy_data, 0, orig, copy);
20510f2f 855}
e0007529 856EXPORT_SYMBOL(security_sb_copy_data);
20510f2f 857
ff36fe2c
EP
858int security_sb_remount(struct super_block *sb, void *data)
859{
f25fce3e 860 return call_int_hook(sb_remount, 0, sb, data);
ff36fe2c
EP
861}
862
12204e24 863int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
20510f2f 864{
f25fce3e 865 return call_int_hook(sb_kern_mount, 0, sb, flags, data);
20510f2f
JM
866}
867
2069f457
EP
868int security_sb_show_options(struct seq_file *m, struct super_block *sb)
869{
f25fce3e 870 return call_int_hook(sb_show_options, 0, m, sb);
2069f457
EP
871}
872
20510f2f
JM
873int security_sb_statfs(struct dentry *dentry)
874{
f25fce3e 875 return call_int_hook(sb_statfs, 0, dentry);
20510f2f
JM
876}
877
8a04c43b 878int security_sb_mount(const char *dev_name, const struct path *path,
808d4e3c 879 const char *type, unsigned long flags, void *data)
20510f2f 880{
f25fce3e 881 return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
20510f2f
JM
882}
883
20510f2f
JM
884int security_sb_umount(struct vfsmount *mnt, int flags)
885{
f25fce3e 886 return call_int_hook(sb_umount, 0, mnt, flags);
20510f2f
JM
887}
888
3b73b68c 889int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
20510f2f 890{
f25fce3e 891 return call_int_hook(sb_pivotroot, 0, old_path, new_path);
20510f2f
JM
892}
893
c9180a57 894int security_sb_set_mnt_opts(struct super_block *sb,
649f6e77
DQ
895 struct security_mnt_opts *opts,
896 unsigned long kern_flags,
897 unsigned long *set_kern_flags)
c9180a57 898{
b1d9e6b0
CS
899 return call_int_hook(sb_set_mnt_opts,
900 opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
901 opts, kern_flags, set_kern_flags);
c9180a57 902}
e0007529 903EXPORT_SYMBOL(security_sb_set_mnt_opts);
c9180a57 904
094f7b69 905int security_sb_clone_mnt_opts(const struct super_block *oldsb,
0b4d3452
SM
906 struct super_block *newsb,
907 unsigned long kern_flags,
908 unsigned long *set_kern_flags)
c9180a57 909{
0b4d3452
SM
910 return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
911 kern_flags, set_kern_flags);
c9180a57 912}
e0007529
EP
913EXPORT_SYMBOL(security_sb_clone_mnt_opts);
914
915int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
916{
f25fce3e 917 return call_int_hook(sb_parse_opts_str, 0, options, opts);
e0007529
EP
918}
919EXPORT_SYMBOL(security_sb_parse_opts_str);
c9180a57 920
20510f2f
JM
921int security_inode_alloc(struct inode *inode)
922{
7a90fc39
CS
923 int rc = lsm_inode_alloc(inode);
924
925 if (unlikely(rc))
926 return rc;
927 rc = call_int_hook(inode_alloc_security, 0, inode);
928 if (unlikely(rc))
929 security_inode_free(inode);
930 return rc;
931}
932
933static void inode_free_by_rcu(struct rcu_head *head)
934{
935 /*
936 * The rcu head is at the start of the inode blob
937 */
938 kmem_cache_free(lsm_inode_cache, head);
20510f2f
JM
939}
940
941void security_inode_free(struct inode *inode)
942{
f381c272 943 integrity_inode_free(inode);
f25fce3e 944 call_void_hook(inode_free_security, inode);
7a90fc39
CS
945 /*
946 * The inode may still be referenced in a path walk and
947 * a call to security_inode_permission() can be made
948 * after inode_free_security() is called. Ideally, the VFS
949 * wouldn't do this, but fixing that is a much harder
950 * job. For now, simply free the i_security via RCU, and
951 * leave the current inode->i_security pointer intact.
952 * The inode will be freed after the RCU grace period too.
953 */
954 if (inode->i_security)
955 call_rcu((struct rcu_head *)inode->i_security,
956 inode_free_by_rcu);
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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 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}
c088e31d 1391EXPORT_SYMBOL_GPL(security_file_permission);
20510f2f
JM
1392
1393int security_file_alloc(struct file *file)
1394{
ab9b110b
CS
1395 int rc = lsm_file_alloc(file);
1396
7a90fc39 1397 if (unlikely(rc))
ab9b110b
CS
1398 return rc;
1399 rc = call_int_hook(file_alloc_security, 0, file);
1400 if (unlikely(rc))
1401 security_file_free(file);
1402 return rc;
20510f2f
JM
1403}
1404
1405void security_file_free(struct file *file)
1406{
ab9b110b
CS
1407 void *blob;
1408
1409 if (!lsm_file_cache)
1410 return;
1411
f25fce3e 1412 call_void_hook(file_free_security, file);
ab9b110b
CS
1413
1414 blob = file->f_security;
1415 file->f_security = NULL;
1416 kmem_cache_free(lsm_file_cache, blob);
20510f2f
JM
1417}
1418
1419int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1420{
f25fce3e 1421 return call_int_hook(file_ioctl, 0, file, cmd, arg);
20510f2f
JM
1422}
1423
98de59bf 1424static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
20510f2f 1425{
8b3ec681 1426 /*
98de59bf
AV
1427 * Does we have PROT_READ and does the application expect
1428 * it to imply PROT_EXEC? If not, nothing to talk about...
8b3ec681 1429 */
98de59bf
AV
1430 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
1431 return prot;
8b3ec681 1432 if (!(current->personality & READ_IMPLIES_EXEC))
98de59bf
AV
1433 return prot;
1434 /*
1435 * if that's an anonymous mapping, let it.
1436 */
1437 if (!file)
1438 return prot | PROT_EXEC;
1439 /*
1440 * ditto if it's not on noexec mount, except that on !MMU we need
b4caecd4 1441 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
98de59bf 1442 */
90f8572b 1443 if (!path_noexec(&file->f_path)) {
8b3ec681 1444#ifndef CONFIG_MMU
b4caecd4
CH
1445 if (file->f_op->mmap_capabilities) {
1446 unsigned caps = file->f_op->mmap_capabilities(file);
1447 if (!(caps & NOMMU_MAP_EXEC))
1448 return prot;
1449 }
8b3ec681 1450#endif
98de59bf 1451 return prot | PROT_EXEC;
8b3ec681 1452 }
98de59bf
AV
1453 /* anything on noexec mount won't get PROT_EXEC */
1454 return prot;
1455}
1456
1457int security_mmap_file(struct file *file, unsigned long prot,
1458 unsigned long flags)
1459{
1460 int ret;
f25fce3e 1461 ret = call_int_hook(mmap_file, 0, file, prot,
98de59bf 1462 mmap_prot(file, prot), flags);
6c21a7fb
MZ
1463 if (ret)
1464 return ret;
1465 return ima_file_mmap(file, prot);
20510f2f 1466}
c088e31d 1467EXPORT_SYMBOL_GPL(security_mmap_file);
20510f2f 1468
e5467859
AV
1469int security_mmap_addr(unsigned long addr)
1470{
f25fce3e 1471 return call_int_hook(mmap_addr, 0, addr);
e5467859
AV
1472}
1473
20510f2f
JM
1474int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
1475 unsigned long prot)
1476{
f25fce3e 1477 return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
20510f2f
JM
1478}
1479
1480int security_file_lock(struct file *file, unsigned int cmd)
1481{
f25fce3e 1482 return call_int_hook(file_lock, 0, file, cmd);
20510f2f
JM
1483}
1484
1485int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1486{
f25fce3e 1487 return call_int_hook(file_fcntl, 0, file, cmd, arg);
20510f2f
JM
1488}
1489
e0b93edd 1490void security_file_set_fowner(struct file *file)
20510f2f 1491{
f25fce3e 1492 call_void_hook(file_set_fowner, file);
20510f2f
JM
1493}
1494
1495int security_file_send_sigiotask(struct task_struct *tsk,
1496 struct fown_struct *fown, int sig)
1497{
f25fce3e 1498 return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
20510f2f
JM
1499}
1500
1501int security_file_receive(struct file *file)
1502{
f25fce3e 1503 return call_int_hook(file_receive, 0, file);
20510f2f
JM
1504}
1505
83d49856 1506int security_file_open(struct file *file, const struct cred *cred)
20510f2f 1507{
c4ec54b4
EP
1508 int ret;
1509
f25fce3e 1510 ret = call_int_hook(file_open, 0, file, cred);
c4ec54b4
EP
1511 if (ret)
1512 return ret;
1513
1514 return fsnotify_perm(file, MAY_OPEN);
20510f2f
JM
1515}
1516
e4e55b47
TH
1517int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
1518{
7f79f508
CS
1519 int rc = lsm_task_alloc(task);
1520
7a90fc39 1521 if (unlikely(rc))
7f79f508
CS
1522 return rc;
1523 rc = call_int_hook(task_alloc, 0, task, clone_flags);
1524 if (unlikely(rc))
1525 security_task_free(task);
1526 return rc;
e4e55b47
TH
1527}
1528
1a2a4d06
KC
1529void security_task_free(struct task_struct *task)
1530{
f25fce3e 1531 call_void_hook(task_free, task);
7f79f508
CS
1532
1533 kfree(task->security);
1534 task->security = NULL;
1a2a4d06
KC
1535}
1536
ee18d64c
DH
1537int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1538{
f1efc8c4
CS
1539 int rc = lsm_cred_alloc(cred, gfp);
1540
7a90fc39 1541 if (unlikely(rc))
f1efc8c4
CS
1542 return rc;
1543
1544 rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
ab9b110b 1545 if (unlikely(rc))
f1efc8c4
CS
1546 security_cred_free(cred);
1547 return rc;
ee18d64c
DH
1548}
1549
d84f4f99 1550void security_cred_free(struct cred *cred)
20510f2f 1551{
f25fce3e 1552 call_void_hook(cred_free, cred);
f1efc8c4
CS
1553
1554 kfree(cred->security);
1555 cred->security = NULL;
20510f2f
JM
1556}
1557
d84f4f99 1558int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
20510f2f 1559{
f1efc8c4
CS
1560 int rc = lsm_cred_alloc(new, gfp);
1561
7a90fc39 1562 if (unlikely(rc))
f1efc8c4
CS
1563 return rc;
1564
1565 rc = call_int_hook(cred_prepare, 0, new, old, gfp);
ab9b110b 1566 if (unlikely(rc))
f1efc8c4
CS
1567 security_cred_free(new);
1568 return rc;
d84f4f99
DH
1569}
1570
ee18d64c
DH
1571void security_transfer_creds(struct cred *new, const struct cred *old)
1572{
f25fce3e 1573 call_void_hook(cred_transfer, new, old);
ee18d64c
DH
1574}
1575
3a3b7ce9
DH
1576int security_kernel_act_as(struct cred *new, u32 secid)
1577{
f25fce3e 1578 return call_int_hook(kernel_act_as, 0, new, secid);
3a3b7ce9
DH
1579}
1580
1581int security_kernel_create_files_as(struct cred *new, struct inode *inode)
1582{
f25fce3e 1583 return call_int_hook(kernel_create_files_as, 0, new, inode);
3a3b7ce9
DH
1584}
1585
dd8dbf2e 1586int security_kernel_module_request(char *kmod_name)
9188499c 1587{
f25fce3e 1588 return call_int_hook(kernel_module_request, 0, kmod_name);
9188499c
EP
1589}
1590
39eeb4fb
MZ
1591int security_kernel_read_file(struct file *file, enum kernel_read_file_id id)
1592{
1593 int ret;
1594
1595 ret = call_int_hook(kernel_read_file, 0, file, id);
1596 if (ret)
1597 return ret;
1598 return ima_read_file(file, id);
1599}
1600EXPORT_SYMBOL_GPL(security_kernel_read_file);
1601
bc8ca5b9
MZ
1602int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
1603 enum kernel_read_file_id id)
b44a7dfc 1604{
cf222217
MZ
1605 int ret;
1606
1607 ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
1608 if (ret)
1609 return ret;
1610 return ima_post_read_file(file, buf, size, id);
b44a7dfc
MZ
1611}
1612EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
1613
d84f4f99
DH
1614int security_task_fix_setuid(struct cred *new, const struct cred *old,
1615 int flags)
20510f2f 1616{
f25fce3e 1617 return call_int_hook(task_fix_setuid, 0, new, old, flags);
20510f2f
JM
1618}
1619
20510f2f
JM
1620int security_task_setpgid(struct task_struct *p, pid_t pgid)
1621{
f25fce3e 1622 return call_int_hook(task_setpgid, 0, p, pgid);
20510f2f
JM
1623}
1624
1625int security_task_getpgid(struct task_struct *p)
1626{
f25fce3e 1627 return call_int_hook(task_getpgid, 0, p);
20510f2f
JM
1628}
1629
1630int security_task_getsid(struct task_struct *p)
1631{
f25fce3e 1632 return call_int_hook(task_getsid, 0, p);
20510f2f
JM
1633}
1634
1635void security_task_getsecid(struct task_struct *p, u32 *secid)
1636{
b1d9e6b0 1637 *secid = 0;
f25fce3e 1638 call_void_hook(task_getsecid, p, secid);
20510f2f
JM
1639}
1640EXPORT_SYMBOL(security_task_getsecid);
1641
20510f2f
JM
1642int security_task_setnice(struct task_struct *p, int nice)
1643{
f25fce3e 1644 return call_int_hook(task_setnice, 0, p, nice);
20510f2f
JM
1645}
1646
1647int security_task_setioprio(struct task_struct *p, int ioprio)
1648{
f25fce3e 1649 return call_int_hook(task_setioprio, 0, p, ioprio);
20510f2f
JM
1650}
1651
1652int security_task_getioprio(struct task_struct *p)
1653{
f25fce3e 1654 return call_int_hook(task_getioprio, 0, p);
20510f2f
JM
1655}
1656
791ec491
SS
1657int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
1658 unsigned int flags)
1659{
1660 return call_int_hook(task_prlimit, 0, cred, tcred, flags);
1661}
1662
8fd00b4d
JS
1663int security_task_setrlimit(struct task_struct *p, unsigned int resource,
1664 struct rlimit *new_rlim)
20510f2f 1665{
f25fce3e 1666 return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
20510f2f
JM
1667}
1668
b0ae1981 1669int security_task_setscheduler(struct task_struct *p)
20510f2f 1670{
f25fce3e 1671 return call_int_hook(task_setscheduler, 0, p);
20510f2f
JM
1672}
1673
1674int security_task_getscheduler(struct task_struct *p)
1675{
f25fce3e 1676 return call_int_hook(task_getscheduler, 0, p);
20510f2f
JM
1677}
1678
1679int security_task_movememory(struct task_struct *p)
1680{
f25fce3e 1681 return call_int_hook(task_movememory, 0, p);
20510f2f
JM
1682}
1683
1684int security_task_kill(struct task_struct *p, struct siginfo *info,
1685 int sig, u32 secid)
1686{
f25fce3e 1687 return call_int_hook(task_kill, 0, p, info, sig, secid);
20510f2f
JM
1688}
1689
a69a0684
JJ
1690#ifdef CONFIG_SECURITY_STACKING
1691static char *nolsm = "-default";
1692#define NOLSMLEN 9
1693
c9353421
JJ
1694static bool is_registered_lsm(const char *str, size_t size)
1695{
1696 struct security_hook_list *hp;
1697
1698 list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
1699 if (size == strlen(hp->lsm) && !strncmp(str, hp->lsm, size))
1700 return true;
1701 }
1702
1703 return false;
1704}
1705
1706static bool set_lsm_of_current(const char *str, size_t size)
1707{
1708 char *lsm = lsm_of_task(current);
1709
1710 if (is_registered_lsm(str, size)) {
1711 strncpy(lsm, str, size);
1712 lsm[size] = '\0';
1713 } else if (size == NOLSMLEN && !strncmp(str, nolsm, size)) {
1714 lsm[0] = '\0';
1715 } else {
1716 return false;
1717 }
1718 return true;
1719}
1720
a69a0684
JJ
1721static int lsm_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1722 unsigned long arg4, unsigned long arg5)
1723{
1724 char *lsm = lsm_of_task(current);
1725 char buffer[SECURITY_NAME_MAX + 1];
1726 __user char *optval = (__user char *)arg2;
1727 __user int *optlen = (__user int *)arg3;
1728 int dlen;
1729 int len;
1730
1731 switch (option) {
1732 case PR_GET_DISPLAY_LSM:
1733 len = arg4;
1734 if (lsm[0] == '\0') {
1735 lsm = nolsm;
1736 dlen = NOLSMLEN;
1737 } else
1738 dlen = strlen(lsm) + 1;
1739 if (dlen > len)
1740 return -ERANGE;
1741 if (copy_to_user(optval, lsm, dlen))
1742 return -EFAULT;
1743 if (put_user(dlen, optlen))
1744 return -EFAULT;
1745 break;
1746 case PR_SET_DISPLAY_LSM:
1747 len = arg3;
1748 if (len > SECURITY_NAME_MAX)
1749 return -EINVAL;
1750 if (copy_from_user(buffer, optval, len))
1751 return -EFAULT;
1752 buffer[len] = '\0';
b3da8757 1753 /* verify the requested LSM is registered */
c9353421 1754 if (!set_lsm_of_current(buffer, len))
b3da8757 1755 return -ENOENT;
a69a0684
JJ
1756 break;
1757 default:
1758 return -ENOSYS;
1759 }
1760 return 0;
1761}
1762#endif
1763
20510f2f 1764int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
d84f4f99 1765 unsigned long arg4, unsigned long arg5)
20510f2f 1766{
b1d9e6b0
CS
1767 int thisrc;
1768 int rc = -ENOSYS;
1769 struct security_hook_list *hp;
1770
a69a0684
JJ
1771#ifdef CONFIG_SECURITY_STACKING
1772 rc = lsm_task_prctl(option, arg2, arg3, arg4, arg5);
1773 if (rc != -ENOSYS)
1774 return rc;
1775#endif
1776
b1d9e6b0
CS
1777 list_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
1778 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
1779 if (thisrc != -ENOSYS) {
1780 rc = thisrc;
1781 if (thisrc != 0)
1782 break;
1783 }
1784 }
1785 return rc;
20510f2f
JM
1786}
1787
1788void security_task_to_inode(struct task_struct *p, struct inode *inode)
1789{
f25fce3e 1790 call_void_hook(task_to_inode, p, inode);
20510f2f
JM
1791}
1792
1793int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
1794{
f25fce3e 1795 return call_int_hook(ipc_permission, 0, ipcp, flag);
20510f2f
JM
1796}
1797
8a076191
AD
1798void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
1799{
b1d9e6b0 1800 *secid = 0;
f25fce3e 1801 call_void_hook(ipc_getsecid, ipcp, secid);
8a076191
AD
1802}
1803
20510f2f
JM
1804int security_msg_msg_alloc(struct msg_msg *msg)
1805{
7a90fc39
CS
1806 int rc = lsm_msg_msg_alloc(msg);
1807
1808 if (unlikely(rc))
1809 return rc;
1810 rc = call_int_hook(msg_msg_alloc_security, 0, msg);
1811 if (unlikely(rc))
1812 security_msg_msg_free(msg);
1813 return rc;
20510f2f
JM
1814}
1815
1816void security_msg_msg_free(struct msg_msg *msg)
1817{
f25fce3e 1818 call_void_hook(msg_msg_free_security, msg);
7a90fc39
CS
1819 kfree(msg->security);
1820 msg->security = NULL;
20510f2f
JM
1821}
1822
1823int security_msg_queue_alloc(struct msg_queue *msq)
1824{
7a90fc39
CS
1825 int rc = lsm_ipc_alloc(&msq->q_perm);
1826
1827 if (unlikely(rc))
1828 return rc;
1829 rc = call_int_hook(msg_queue_alloc_security, 0, msq);
1830 if (unlikely(rc))
1831 security_msg_queue_free(msq);
1832 return rc;
20510f2f
JM
1833}
1834
1835void security_msg_queue_free(struct msg_queue *msq)
1836{
7a90fc39
CS
1837 struct kern_ipc_perm *kip = &msq->q_perm;
1838
f25fce3e 1839 call_void_hook(msg_queue_free_security, msq);
7a90fc39
CS
1840 kfree(kip->security);
1841 kip->security = NULL;
20510f2f
JM
1842}
1843
1844int security_msg_queue_associate(struct msg_queue *msq, int msqflg)
1845{
f25fce3e 1846 return call_int_hook(msg_queue_associate, 0, msq, msqflg);
20510f2f
JM
1847}
1848
1849int security_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1850{
f25fce3e 1851 return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
20510f2f
JM
1852}
1853
1854int security_msg_queue_msgsnd(struct msg_queue *msq,
1855 struct msg_msg *msg, int msqflg)
1856{
f25fce3e 1857 return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
20510f2f
JM
1858}
1859
1860int security_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1861 struct task_struct *target, long type, int mode)
1862{
f25fce3e 1863 return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
20510f2f
JM
1864}
1865
1866int security_shm_alloc(struct shmid_kernel *shp)
1867{
7a90fc39
CS
1868 int rc = lsm_ipc_alloc(&shp->shm_perm);
1869
1870 if (unlikely(rc))
1871 return rc;
1872 rc = call_int_hook(shm_alloc_security, 0, shp);
1873 if (unlikely(rc))
1874 security_shm_free(shp);
1875 return rc;
20510f2f
JM
1876}
1877
1878void security_shm_free(struct shmid_kernel *shp)
1879{
7a90fc39
CS
1880 struct kern_ipc_perm *kip = &shp->shm_perm;
1881
f25fce3e 1882 call_void_hook(shm_free_security, shp);
7a90fc39
CS
1883 kfree(kip->security);
1884 kip->security = NULL;
20510f2f
JM
1885}
1886
1887int security_shm_associate(struct shmid_kernel *shp, int shmflg)
1888{
f25fce3e 1889 return call_int_hook(shm_associate, 0, shp, shmflg);
20510f2f
JM
1890}
1891
1892int security_shm_shmctl(struct shmid_kernel *shp, int cmd)
1893{
f25fce3e 1894 return call_int_hook(shm_shmctl, 0, shp, cmd);
20510f2f
JM
1895}
1896
1897int security_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, int shmflg)
1898{
f25fce3e 1899 return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
20510f2f
JM
1900}
1901
1902int security_sem_alloc(struct sem_array *sma)
1903{
7a90fc39
CS
1904 int rc = lsm_ipc_alloc(&sma->sem_perm);
1905
1906 if (unlikely(rc))
1907 return rc;
1908 rc = call_int_hook(sem_alloc_security, 0, sma);
1909 if (unlikely(rc))
1910 security_sem_free(sma);
1911 return rc;
20510f2f
JM
1912}
1913
1914void security_sem_free(struct sem_array *sma)
1915{
7a90fc39
CS
1916 struct kern_ipc_perm *kip = &sma->sem_perm;
1917
f25fce3e 1918 call_void_hook(sem_free_security, sma);
7a90fc39
CS
1919 kfree(kip->security);
1920 kip->security = NULL;
20510f2f
JM
1921}
1922
1923int security_sem_associate(struct sem_array *sma, int semflg)
1924{
f25fce3e 1925 return call_int_hook(sem_associate, 0, sma, semflg);
20510f2f
JM
1926}
1927
1928int security_sem_semctl(struct sem_array *sma, int cmd)
1929{
f25fce3e 1930 return call_int_hook(sem_semctl, 0, sma, cmd);
20510f2f
JM
1931}
1932
1933int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
1934 unsigned nsops, int alter)
1935{
f25fce3e 1936 return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
20510f2f
JM
1937}
1938
1939void security_d_instantiate(struct dentry *dentry, struct inode *inode)
1940{
1941 if (unlikely(inode && IS_PRIVATE(inode)))
1942 return;
f25fce3e 1943 call_void_hook(d_instantiate, dentry, inode);
20510f2f
JM
1944}
1945EXPORT_SYMBOL(security_d_instantiate);
1946
dcd148a1
CS
1947int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
1948 char **value)
20510f2f 1949{
a69a0684
JJ
1950#ifdef CONFIG_SECURITY_STACKING
1951 char *speclsm = lsm_of_task(p);
1952#endif
dcd148a1 1953 struct security_hook_list *hp;
c9353421
JJ
1954 char *vp;
1955 char *cp = NULL;
1956 int trc;
1957 int rc;
1958
1959 /*
1960 * "context" requires work here in addition to what
1961 * the modules provide.
1962 */
1963 if (strcmp(name, "context") == 0) {
1964 *value = NULL;
1965 rc = -EINVAL;
1966 list_for_each_entry(hp,
1967 &security_hook_heads.getprocattr, list) {
1968 if (lsm != NULL && strcmp(lsm, hp->lsm))
1969 continue;
1970 trc = hp->hook.getprocattr(p, "context", &vp);
1971 if (trc == -ENOENT)
1972 continue;
1973 if (trc <= 0) {
1974 kfree(*value);
1975 return trc;
1976 }
1977 rc = trc;
1978 if (*value == NULL) {
1979 *value = vp;
1980 } else {
1981 cp = kasprintf(GFP_KERNEL, "%s,%s", *value, vp);
1982 if (cp == NULL) {
1983 kfree(*value);
1984 kfree(vp);
1985 return -ENOMEM;
1986 }
1987 kfree(*value);
1988 kfree(vp);
1989 *value = cp;
1990 }
1991 }
1992 if (rc > 0)
1993 return strlen(*value);
1994 return rc;
1995 } else if (strcmp(name, "display_lsm") == 0) {
1996 *value = kstrdup(current->security, GFP_KERNEL);
1997 if (*value == NULL)
1998 return -ENOMEM;
1999 return strlen(*value);
2000 }
dcd148a1
CS
2001
2002 list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
2003 if (lsm != NULL && strcmp(lsm, hp->lsm))
2004 continue;
a69a0684
JJ
2005#ifdef CONFIG_SECURITY_STACKING
2006 if (!lsm && speclsm && speclsm[0] && strcmp(speclsm, hp->lsm))
2007 continue;
2008#endif
2009 rc = hp->hook.getprocattr(p, name, value);
2010 if (rc != -ENOSYS)
2011 return rc;
dcd148a1
CS
2012 }
2013 return -EINVAL;
20510f2f
JM
2014}
2015
dcd148a1
CS
2016int security_setprocattr(const char *lsm, const char *name, void *value,
2017 size_t size)
20510f2f 2018{
a69a0684
JJ
2019#ifdef CONFIG_SECURITY_STACKING
2020 char *speclsm = lsm_of_task(current);
2021 struct lsm_value *lsm_value = NULL;
2022 int count;
2023#else
2024 char *tvalue;
2025#endif
dcd148a1 2026 struct security_hook_list *hp;
a69a0684
JJ
2027 int rc;
2028 char *temp;
2029 char *cp;
2030
2031 /*
2032 * If lsm is NULL look at all the modules to find one
2033 * that processes name. If lsm is not NULL only look at
2034 * that module.
2035 *
2036 * "context" is handled directly here.
2037 */
2038 if (strcmp(name, "context") == 0) {
2039 rc = -EINVAL;
2040 temp = kmemdup(value, size + 1, GFP_KERNEL);
2041 if (!temp)
2042 return -ENOMEM;
2043
2044 temp[size] = '\0';
2045 cp = strrchr(temp, '\'');
2046 if (!cp)
2047 goto free_out;
2048
2049 cp[1] = '\0';
2050#ifdef CONFIG_SECURITY_STACKING
2051 lsm_value = kzalloc(sizeof(*lsm_value) * LSM_MAX_MAJOR,
2052 GFP_KERNEL);
2053 if (!lsm_value) {
2054 rc = -ENOMEM;
2055 goto free_out;
2056 }
2057
2058 count = lsm_parse_context(temp, lsm_value);
2059 if (count <= 0)
2060 goto free_out;
2061
2062 for (count--; count >= 0; count--) {
2063 list_for_each_entry(hp,
2064 &security_hook_heads.setprocattr, list) {
2065
2066 if (lsm && strcmp(lsm, hp->lsm))
2067 continue;
2068 if (!strcmp(hp->lsm, lsm_value[count].lsm)) {
2069 rc = hp->hook.setprocattr("context",
2070 lsm_value[count].data,
2071 strlen(lsm_value[count].data));
2072 break;
2073 }
2074 }
2075 if (rc < 0 || (lsm && rc >0))
2076 break;
2077 }
2078#else /* CONFIG_SECURITY_STACKING */
2079 cp = strstr(temp, "='");
2080 if (!cp)
2081 goto free_out;
2082 *cp = '\0';
2083 tvalue = strchr(cp + 2, '\'');
2084 if (!tvalue)
2085 goto free_out;
2086 list_for_each_entry(hp, &security_hook_heads.setprocattr,
2087 list) {
2088 if (lsm == NULL || !strcmp(lsm, hp->lsm)) {
2089 rc = hp->hook.setprocattr(name, tvalue, size);
2090 break;
2091 }
2092 }
2093#endif /* CONFIG_SECURITY_STACKING */
2094free_out:
2095 kfree(temp);
2096#ifdef CONFIG_SECURITY_STACKING
2097 kfree(lsm_value);
2098#endif
2099 if (rc >= 0)
2100 return size;
2101 return rc;
c9353421
JJ
2102 } else if (strcmp(name, "display_lsm") == 0) {
2103#ifdef CONFIG_SECURITY_STACKING
2104 if (set_lsm_of_current(value, size))
2105 return size;
2106#endif
2107 return -EINVAL;
a69a0684 2108 }
dcd148a1
CS
2109
2110 list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
a69a0684
JJ
2111#ifdef CONFIG_SECURITY_STACKING
2112 if (!lsm && speclsm && speclsm[0] && strcmp(speclsm, hp->lsm))
dcd148a1 2113 continue;
a69a0684
JJ
2114#endif
2115 rc = hp->hook.setprocattr(name, value, size);
2116 if (rc)
2117 return rc;
dcd148a1
CS
2118 }
2119 return -EINVAL;
20510f2f
JM
2120}
2121
2122int security_netlink_send(struct sock *sk, struct sk_buff *skb)
2123{
f25fce3e 2124 return call_int_hook(netlink_send, 0, sk, skb);
20510f2f 2125}
20510f2f 2126
746df9b5
DQ
2127int security_ismaclabel(const char *name)
2128{
f25fce3e 2129 return call_int_hook(ismaclabel, 0, name);
746df9b5
DQ
2130}
2131EXPORT_SYMBOL(security_ismaclabel);
2132
20510f2f
JM
2133int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2134{
b1d9e6b0
CS
2135 return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
2136 seclen);
20510f2f
JM
2137}
2138EXPORT_SYMBOL(security_secid_to_secctx);
2139
7bf570dc 2140int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
63cb3449 2141{
b1d9e6b0 2142 *secid = 0;
f25fce3e 2143 return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
63cb3449
DH
2144}
2145EXPORT_SYMBOL(security_secctx_to_secid);
2146
20510f2f
JM
2147void security_release_secctx(char *secdata, u32 seclen)
2148{
a69a0684
JJ
2149#ifdef CONFIG_SECURITY_STACKING
2150 char *speclsm = lsm_of_task(current);
2151#endif
2152 struct security_hook_list *hp;
2153
2154 list_for_each_entry(hp, &security_hook_heads.release_secctx, list) {
2155#ifdef CONFIG_SECURITY_STACKING
2156 if (speclsm[0] && strcmp(hp->lsm, speclsm))
2157 continue;
2158#endif
2159 hp->hook.release_secctx(secdata, seclen);
2160 break;
2161 }
20510f2f
JM
2162}
2163EXPORT_SYMBOL(security_release_secctx);
2164
6f3be9f5
AG
2165void security_inode_invalidate_secctx(struct inode *inode)
2166{
2167 call_void_hook(inode_invalidate_secctx, inode);
2168}
2169EXPORT_SYMBOL(security_inode_invalidate_secctx);
2170
1ee65e37
DQ
2171int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
2172{
f25fce3e 2173 return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
1ee65e37
DQ
2174}
2175EXPORT_SYMBOL(security_inode_notifysecctx);
2176
2177int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
2178{
f25fce3e 2179 return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
1ee65e37
DQ
2180}
2181EXPORT_SYMBOL(security_inode_setsecctx);
2182
2183int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
2184{
b1d9e6b0 2185 return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
1ee65e37
DQ
2186}
2187EXPORT_SYMBOL(security_inode_getsecctx);
2188
20510f2f
JM
2189#ifdef CONFIG_SECURITY_NETWORK
2190
3610cda5 2191int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
20510f2f 2192{
f25fce3e 2193 return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
20510f2f
JM
2194}
2195EXPORT_SYMBOL(security_unix_stream_connect);
2196
2197int security_unix_may_send(struct socket *sock, struct socket *other)
2198{
f25fce3e 2199 return call_int_hook(unix_may_send, 0, sock, other);
20510f2f
JM
2200}
2201EXPORT_SYMBOL(security_unix_may_send);
2202
2203int security_socket_create(int family, int type, int protocol, int kern)
2204{
f25fce3e 2205 return call_int_hook(socket_create, 0, family, type, protocol, kern);
20510f2f
JM
2206}
2207
2208int security_socket_post_create(struct socket *sock, int family,
2209 int type, int protocol, int kern)
2210{
f25fce3e 2211 return call_int_hook(socket_post_create, 0, sock, family, type,
20510f2f
JM
2212 protocol, kern);
2213}
2214
2215int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
2216{
f25fce3e 2217 return call_int_hook(socket_bind, 0, sock, address, addrlen);
20510f2f
JM
2218}
2219
2220int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
2221{
f25fce3e 2222 return call_int_hook(socket_connect, 0, sock, address, addrlen);
20510f2f
JM
2223}
2224
2225int security_socket_listen(struct socket *sock, int backlog)
2226{
f25fce3e 2227 return call_int_hook(socket_listen, 0, sock, backlog);
20510f2f
JM
2228}
2229
2230int security_socket_accept(struct socket *sock, struct socket *newsock)
2231{
f25fce3e 2232 return call_int_hook(socket_accept, 0, sock, newsock);
20510f2f
JM
2233}
2234
20510f2f
JM
2235int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
2236{
f25fce3e 2237 return call_int_hook(socket_sendmsg, 0, sock, msg, size);
20510f2f
JM
2238}
2239
2240int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
2241 int size, int flags)
2242{
f25fce3e 2243 return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
20510f2f
JM
2244}
2245
2246int security_socket_getsockname(struct socket *sock)
2247{
f25fce3e 2248 return call_int_hook(socket_getsockname, 0, sock);
20510f2f
JM
2249}
2250
2251int security_socket_getpeername(struct socket *sock)
2252{
f25fce3e 2253 return call_int_hook(socket_getpeername, 0, sock);
20510f2f
JM
2254}
2255
2256int security_socket_getsockopt(struct socket *sock, int level, int optname)
2257{
f25fce3e 2258 return call_int_hook(socket_getsockopt, 0, sock, level, optname);
20510f2f
JM
2259}
2260
2261int security_socket_setsockopt(struct socket *sock, int level, int optname)
2262{
f25fce3e 2263 return call_int_hook(socket_setsockopt, 0, sock, level, optname);
20510f2f
JM
2264}
2265
2266int security_socket_shutdown(struct socket *sock, int how)
2267{
f25fce3e 2268 return call_int_hook(socket_shutdown, 0, sock, how);
20510f2f
JM
2269}
2270
2271int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2272{
f25fce3e 2273 return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
20510f2f
JM
2274}
2275EXPORT_SYMBOL(security_sock_rcv_skb);
2276
2277int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
2278 int __user *optlen, unsigned len)
2279{
8e42f92b
JJ
2280#ifdef CONFIG_SECURITY_STACKING
2281 struct security_hook_list *hp;
2282 char *lsm = lsm_of_task(current);
2283
2284 list_for_each_entry(hp, &security_hook_heads.socket_getpeersec_stream,
2285 list) {
2286 if (!lsm || !lsm[0] || !strcmp(lsm, hp->lsm))
2287 return hp->hook.socket_getpeersec_stream(sock, optval,
2288 optlen, len);
2289 }
2290 return -ENOPROTOOPT;
2291#else
b1d9e6b0
CS
2292 return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
2293 optval, optlen, len);
8e42f92b 2294#endif
20510f2f
JM
2295}
2296
2297int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
2298{
e308fd3b
JB
2299 return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
2300 skb, secid);
20510f2f
JM
2301}
2302EXPORT_SYMBOL(security_socket_getpeersec_dgram);
2303
2304int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
2305{
7a90fc39
CS
2306 int rc = lsm_sock_alloc(sk, priority);
2307
2308 if (unlikely(rc))
2309 return rc;
2310 rc = call_int_hook(sk_alloc_security, 0, sk, family, priority);
2311 if (unlikely(rc))
2312 security_sk_free(sk);
2313 return rc;
20510f2f
JM
2314}
2315
2316void security_sk_free(struct sock *sk)
2317{
f25fce3e 2318 call_void_hook(sk_free_security, sk);
7a90fc39
CS
2319 kfree(sk->sk_security);
2320 sk->sk_security = NULL;
20510f2f
JM
2321}
2322
2323void security_sk_clone(const struct sock *sk, struct sock *newsk)
2324{
f25fce3e 2325 call_void_hook(sk_clone_security, sk, newsk);
20510f2f 2326}
6230c9b4 2327EXPORT_SYMBOL(security_sk_clone);
20510f2f
JM
2328
2329void security_sk_classify_flow(struct sock *sk, struct flowi *fl)
2330{
f25fce3e 2331 call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
20510f2f
JM
2332}
2333EXPORT_SYMBOL(security_sk_classify_flow);
2334
2335void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)
2336{
f25fce3e 2337 call_void_hook(req_classify_flow, req, fl);
20510f2f
JM
2338}
2339EXPORT_SYMBOL(security_req_classify_flow);
2340
2341void security_sock_graft(struct sock *sk, struct socket *parent)
2342{
f25fce3e 2343 call_void_hook(sock_graft, sk, parent);
20510f2f
JM
2344}
2345EXPORT_SYMBOL(security_sock_graft);
2346
2347int security_inet_conn_request(struct sock *sk,
2348 struct sk_buff *skb, struct request_sock *req)
2349{
f25fce3e 2350 return call_int_hook(inet_conn_request, 0, sk, skb, req);
20510f2f
JM
2351}
2352EXPORT_SYMBOL(security_inet_conn_request);
2353
2354void security_inet_csk_clone(struct sock *newsk,
2355 const struct request_sock *req)
2356{
f25fce3e 2357 call_void_hook(inet_csk_clone, newsk, req);
20510f2f
JM
2358}
2359
2360void security_inet_conn_established(struct sock *sk,
2361 struct sk_buff *skb)
2362{
f25fce3e 2363 call_void_hook(inet_conn_established, sk, skb);
20510f2f
JM
2364}
2365
2606fd1f
EP
2366int security_secmark_relabel_packet(u32 secid)
2367{
f25fce3e 2368 return call_int_hook(secmark_relabel_packet, 0, secid);
2606fd1f
EP
2369}
2370EXPORT_SYMBOL(security_secmark_relabel_packet);
2371
2372void security_secmark_refcount_inc(void)
2373{
f25fce3e 2374 call_void_hook(secmark_refcount_inc);
2606fd1f
EP
2375}
2376EXPORT_SYMBOL(security_secmark_refcount_inc);
2377
2378void security_secmark_refcount_dec(void)
2379{
f25fce3e 2380 call_void_hook(secmark_refcount_dec);
2606fd1f
EP
2381}
2382EXPORT_SYMBOL(security_secmark_refcount_dec);
2383
5dbbaf2d
PM
2384int security_tun_dev_alloc_security(void **security)
2385{
f25fce3e 2386 return call_int_hook(tun_dev_alloc_security, 0, security);
5dbbaf2d
PM
2387}
2388EXPORT_SYMBOL(security_tun_dev_alloc_security);
2389
2390void security_tun_dev_free_security(void *security)
2391{
f25fce3e 2392 call_void_hook(tun_dev_free_security, security);
5dbbaf2d
PM
2393}
2394EXPORT_SYMBOL(security_tun_dev_free_security);
2395
2b980dbd
PM
2396int security_tun_dev_create(void)
2397{
f25fce3e 2398 return call_int_hook(tun_dev_create, 0);
2b980dbd
PM
2399}
2400EXPORT_SYMBOL(security_tun_dev_create);
2401
5dbbaf2d 2402int security_tun_dev_attach_queue(void *security)
2b980dbd 2403{
f25fce3e 2404 return call_int_hook(tun_dev_attach_queue, 0, security);
2b980dbd 2405}
5dbbaf2d 2406EXPORT_SYMBOL(security_tun_dev_attach_queue);
2b980dbd 2407
5dbbaf2d 2408int security_tun_dev_attach(struct sock *sk, void *security)
2b980dbd 2409{
f25fce3e 2410 return call_int_hook(tun_dev_attach, 0, sk, security);
2b980dbd
PM
2411}
2412EXPORT_SYMBOL(security_tun_dev_attach);
2413
5dbbaf2d
PM
2414int security_tun_dev_open(void *security)
2415{
f25fce3e 2416 return call_int_hook(tun_dev_open, 0, security);
5dbbaf2d
PM
2417}
2418EXPORT_SYMBOL(security_tun_dev_open);
2419
20510f2f
JM
2420#endif /* CONFIG_SECURITY_NETWORK */
2421
d291f1a6
DJ
2422#ifdef CONFIG_SECURITY_INFINIBAND
2423
2424int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
2425{
2426 return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
2427}
2428EXPORT_SYMBOL(security_ib_pkey_access);
2429
47a2b338
DJ
2430int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
2431{
2432 return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
2433}
2434EXPORT_SYMBOL(security_ib_endport_manage_subnet);
2435
d291f1a6
DJ
2436int security_ib_alloc_security(void **sec)
2437{
2438 return call_int_hook(ib_alloc_security, 0, sec);
2439}
2440EXPORT_SYMBOL(security_ib_alloc_security);
2441
2442void security_ib_free_security(void *sec)
2443{
2444 call_void_hook(ib_free_security, sec);
2445}
2446EXPORT_SYMBOL(security_ib_free_security);
2447#endif /* CONFIG_SECURITY_INFINIBAND */
2448
20510f2f
JM
2449#ifdef CONFIG_SECURITY_NETWORK_XFRM
2450
52a4c640
NA
2451int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
2452 struct xfrm_user_sec_ctx *sec_ctx,
2453 gfp_t gfp)
20510f2f 2454{
f25fce3e 2455 return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
20510f2f
JM
2456}
2457EXPORT_SYMBOL(security_xfrm_policy_alloc);
2458
03e1ad7b
PM
2459int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
2460 struct xfrm_sec_ctx **new_ctxp)
20510f2f 2461{
f25fce3e 2462 return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
20510f2f
JM
2463}
2464
03e1ad7b 2465void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
20510f2f 2466{
f25fce3e 2467 call_void_hook(xfrm_policy_free_security, ctx);
20510f2f
JM
2468}
2469EXPORT_SYMBOL(security_xfrm_policy_free);
2470
03e1ad7b 2471int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
20510f2f 2472{
f25fce3e 2473 return call_int_hook(xfrm_policy_delete_security, 0, ctx);
20510f2f
JM
2474}
2475
2e5aa866
PM
2476int security_xfrm_state_alloc(struct xfrm_state *x,
2477 struct xfrm_user_sec_ctx *sec_ctx)
20510f2f 2478{
f25fce3e 2479 return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
20510f2f
JM
2480}
2481EXPORT_SYMBOL(security_xfrm_state_alloc);
2482
2483int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
2484 struct xfrm_sec_ctx *polsec, u32 secid)
2485{
f25fce3e 2486 return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
20510f2f
JM
2487}
2488
2489int security_xfrm_state_delete(struct xfrm_state *x)
2490{
f25fce3e 2491 return call_int_hook(xfrm_state_delete_security, 0, x);
20510f2f
JM
2492}
2493EXPORT_SYMBOL(security_xfrm_state_delete);
2494
2495void security_xfrm_state_free(struct xfrm_state *x)
2496{
f25fce3e 2497 call_void_hook(xfrm_state_free_security, x);
20510f2f
JM
2498}
2499
03e1ad7b 2500int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
20510f2f 2501{
f25fce3e 2502 return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
20510f2f
JM
2503}
2504
2505int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
e33f7704
DM
2506 struct xfrm_policy *xp,
2507 const struct flowi *fl)
20510f2f 2508{
b1d9e6b0
CS
2509 struct security_hook_list *hp;
2510 int rc = 1;
2511
2512 /*
2513 * Since this function is expected to return 0 or 1, the judgment
2514 * becomes difficult if multiple LSMs supply this call. Fortunately,
2515 * we can use the first LSM's judgment because currently only SELinux
2516 * supplies this call.
2517 *
2518 * For speed optimization, we explicitly break the loop rather than
2519 * using the macro
2520 */
2521 list_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
2522 list) {
2523 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
2524 break;
2525 }
2526 return rc;
20510f2f
JM
2527}
2528
2529int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
2530{
f25fce3e 2531 return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
20510f2f
JM
2532}
2533
2534void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
2535{
f25fce3e
CS
2536 int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl->flowi_secid,
2537 0);
20510f2f
JM
2538
2539 BUG_ON(rc);
2540}
2541EXPORT_SYMBOL(security_skb_classify_flow);
2542
2543#endif /* CONFIG_SECURITY_NETWORK_XFRM */
2544
2545#ifdef CONFIG_KEYS
2546
d84f4f99
DH
2547int security_key_alloc(struct key *key, const struct cred *cred,
2548 unsigned long flags)
20510f2f 2549{
7a90fc39
CS
2550 int rc = lsm_key_alloc(key);
2551
2552 if (unlikely(rc))
2553 return rc;
2554 rc = call_int_hook(key_alloc, 0, key, cred, flags);
2555 if (unlikely(rc))
2556 security_key_free(key);
2557 return rc;
20510f2f
JM
2558}
2559
2560void security_key_free(struct key *key)
2561{
f25fce3e 2562 call_void_hook(key_free, key);
7a90fc39
CS
2563 kfree(key->security);
2564 key->security = NULL;
20510f2f
JM
2565}
2566
2567int security_key_permission(key_ref_t key_ref,
f5895943 2568 const struct cred *cred, unsigned perm)
20510f2f 2569{
f25fce3e 2570 return call_int_hook(key_permission, 0, key_ref, cred, perm);
20510f2f
JM
2571}
2572
70a5bb72
DH
2573int security_key_getsecurity(struct key *key, char **_buffer)
2574{
b1d9e6b0 2575 *_buffer = NULL;
f25fce3e 2576 return call_int_hook(key_getsecurity, 0, key, _buffer);
70a5bb72
DH
2577}
2578
20510f2f 2579#endif /* CONFIG_KEYS */
03d37d25
AD
2580
2581#ifdef CONFIG_AUDIT
2582
2583int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2584{
f25fce3e 2585 return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
03d37d25
AD
2586}
2587
2588int security_audit_rule_known(struct audit_krule *krule)
2589{
f25fce3e 2590 return call_int_hook(audit_rule_known, 0, krule);
03d37d25
AD
2591}
2592
2593void security_audit_rule_free(void *lsmrule)
2594{
f25fce3e 2595 call_void_hook(audit_rule_free, lsmrule);
03d37d25
AD
2596}
2597
2598int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
2599 struct audit_context *actx)
2600{
f25fce3e
CS
2601 return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule,
2602 actx);
03d37d25 2603}
b1d9e6b0 2604#endif /* CONFIG_AUDIT */
afdb09c7
CF
2605
2606#ifdef CONFIG_BPF_SYSCALL
2607int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
2608{
2609 return call_int_hook(bpf, 0, cmd, attr, size);
2610}
2611int security_bpf_map(struct bpf_map *map, fmode_t fmode)
2612{
2613 return call_int_hook(bpf_map, 0, map, fmode);
2614}
2615int security_bpf_prog(struct bpf_prog *prog)
2616{
2617 return call_int_hook(bpf_prog, 0, prog);
2618}
2619int security_bpf_map_alloc(struct bpf_map *map)
2620{
2621 return call_int_hook(bpf_map_alloc_security, 0, map);
2622}
2623int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
2624{
2625 return call_int_hook(bpf_prog_alloc_security, 0, aux);
2626}
2627void security_bpf_map_free(struct bpf_map *map)
2628{
2629 call_void_hook(bpf_map_free_security, map);
2630}
2631void security_bpf_prog_free(struct bpf_prog_aux *aux)
2632{
2633 call_void_hook(bpf_prog_free_security, aux);
2634}
2635#endif /* CONFIG_BPF_SYSCALL */