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