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