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