]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - security/security.c
UBUNTU: SAUCE: LSM stacking: provide a way to specify the default display lsm
[mirror_ubuntu-artful-kernel.git] / security / security.c
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>
7 * Copyright (C) 2016 Mellanox Technologies
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
15 #include <linux/capability.h>
16 #include <linux/dcache.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/lsm_hooks.h>
21 #include <linux/integrity.h>
22 #include <linux/ima.h>
23 #include <linux/evm.h>
24 #include <linux/fsnotify.h>
25 #include <linux/mman.h>
26 #include <linux/mount.h>
27 #include <linux/personality.h>
28 #include <linux/backing-dev.h>
29 #include <linux/string.h>
30 #include <linux/msg.h>
31 #include <linux/prctl.h>
32 #include <net/flow.h>
33 #include <net/sock.h>
34
35 #define MAX_LSM_EVM_XATTR 2
36
37 /* Maximum number of letters for an LSM name string */
38 #define SECURITY_NAME_MAX 10
39 #define SECURITY_CHOSEN_NAMES_MAX (SECURITY_NAME_MAX * LSM_MAX_MAJOR)
40 #define MODULE_STACK "(stacking)"
41
42 struct security_hook_heads security_hook_heads __lsm_ro_after_init;
43 static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);
44
45 char *lsm_names;
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 */
52 static struct lsm_blob_sizes blob_sizes = {
53 #ifdef CONFIG_SECURITY_STACKING
54 .lbs_task = SECURITY_NAME_MAX + 2,
55 #endif
56 };
57
58 /* Boot-time LSM user choice */
59 static __initdata char chosen_lsms[SECURITY_CHOSEN_NAMES_MAX + 1] =
60 #ifdef CONFIG_SECURITY_STACKING
61 MODULE_STACK;
62 #else
63 CONFIG_DEFAULT_SECURITY;
64 #endif
65 static __initdata char chosen_display_lsm[SECURITY_NAME_MAX + 1];
66 static char default_display_lsm[SECURITY_NAME_MAX + 1];
67
68 static 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 */
83 int __init security_init(void)
84 {
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]);
91 pr_info("Security Framework initialized\n");
92
93 /*
94 * Load minor LSMs, with the capability module always first.
95 */
96 capability_add_hooks();
97 yama_add_hooks();
98 loadpin_add_hooks();
99
100 /*
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.
110 */
111 do_security_initcalls();
112
113 #ifdef CONFIG_SECURITY_LSM_DEBUG
114 pr_info("LSM: cred blob size = %d\n", blob_sizes.lbs_cred);
115 pr_info("LSM: file blob size = %d\n", blob_sizes.lbs_file);
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);
124 pr_info("LSM: task blob size = %d\n", blob_sizes.lbs_task);
125 #endif /* CONFIG_SECURITY_LSM_DEBUG */
126
127 return 0;
128 }
129
130 /* Save user chosen LSM */
131 static int __init choose_lsm(char *str)
132 {
133 strncpy(chosen_lsms, str, SECURITY_CHOSEN_NAMES_MAX);
134 pr_info("LSM: command line set '%s' security module(s).\n",
135 chosen_lsms);
136 return 1;
137 }
138 __setup("security=", choose_lsm);
139
140 static 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
149 static 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
164 static int lsm_append(char *new, char **result)
165 {
166 char *cp;
167
168 if (*result == NULL) {
169 *result = kstrdup(new, GFP_KERNEL);
170 } else {
171 /* Check if it is the last registered name */
172 if (match_last_lsm(*result, new))
173 return 0;
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
183 /**
184 * security_module_enable - Load given security module on boot ?
185 * @module: the name of the module
186 * @stacked: indicates that the module wants to be stacked
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
190 * to check if your LSM is currently loaded during kernel initialization.
191 *
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 *
200 * Otherwise, return false.
201 */
202 #ifdef CONFIG_SECURITY_STACKING
203 static 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
224 bool __init security_module_enable(const char *lsm, const bool stacked)
225 {
226 #ifdef CONFIG_SECURITY_STACKING
227 /*
228 * Module defined on the command line security=XXXX
229 */
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 }
243 /*
244 * Module configured as stacked.
245 */
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
253 return stacked;
254 #else
255 if (strcmp(lsm, chosen_lsms) == 0) {
256 strcpy(default_display_lsm, lsm);
257 return true;
258 }
259 return false;
260 #endif
261 }
262
263 /*
264 * Keep the order of major modules for mapping secids.
265 */
266 static int lsm_next_major;
267
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 */
276 void __init security_add_hooks(struct security_hook_list *hooks, int count,
277 char *lsm)
278 {
279 int i;
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
285 for (i = 0; i < count; i++) {
286 hooks[i].lsm = lsm;
287 hooks[i].lsm_index = lsm_index;
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
294 int call_lsm_notifier(enum lsm_event event, void *data)
295 {
296 return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
297 }
298 EXPORT_SYMBOL(call_lsm_notifier);
299
300 int register_lsm_notifier(struct notifier_block *nb)
301 {
302 return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
303 }
304 EXPORT_SYMBOL(register_lsm_notifier);
305
306 int unregister_lsm_notifier(struct notifier_block *nb)
307 {
308 return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
309 }
310 EXPORT_SYMBOL(unregister_lsm_notifier);
311
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 */
321 int 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 */
342 void 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
355 static 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 */
376 void __init security_add_blobs(struct lsm_blob_sizes *needed)
377 {
378 lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
379 lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
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);
385 lsm_set_size(&needed->lbs_task, &blob_sizes.lbs_task);
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);
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 */
403 int 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;
416 }
417
418 #ifdef CONFIG_SECURITY_STACKING
419 static 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
430 struct 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 */
442 static 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
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 */
484 int 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;
496
497 /* inherit current display lsm */
498 #ifdef CONFIG_SECURITY_STACKING
499 if (current->security)
500 strcpy(task->security, lsm_of_task(current));
501 else
502 strcpy(task->security, default_display_lsm);
503 #endif
504 return 0;
505 }
506
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 */
515 int 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 */
536 void 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 */
557 int 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 */
581 int 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 */
605 int 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 */
629 int 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 */
652 int 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
667 /*
668 * Hook list operation macros.
669 *
670 * call_void_hook:
671 * This is a hook that does not return a value.
672 *
673 * call_int_hook:
674 * This is a hook that returns a value.
675 */
676
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 })
698
699 /* Security operations */
700
701 int security_binder_set_context_mgr(struct task_struct *mgr)
702 {
703 return call_int_hook(binder_set_context_mgr, 0, mgr);
704 }
705
706 int security_binder_transaction(struct task_struct *from,
707 struct task_struct *to)
708 {
709 return call_int_hook(binder_transaction, 0, from, to);
710 }
711
712 int security_binder_transfer_binder(struct task_struct *from,
713 struct task_struct *to)
714 {
715 return call_int_hook(binder_transfer_binder, 0, from, to);
716 }
717
718 int security_binder_transfer_file(struct task_struct *from,
719 struct task_struct *to, struct file *file)
720 {
721 return call_int_hook(binder_transfer_file, 0, from, to, file);
722 }
723
724 int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
725 {
726 return call_int_hook(ptrace_access_check, 0, child, mode);
727 }
728
729 int security_ptrace_traceme(struct task_struct *parent)
730 {
731 return call_int_hook(ptrace_traceme, 0, parent);
732 }
733
734 int security_capget(struct task_struct *target,
735 kernel_cap_t *effective,
736 kernel_cap_t *inheritable,
737 kernel_cap_t *permitted)
738 {
739 return call_int_hook(capget, 0, target,
740 effective, inheritable, permitted);
741 }
742
743 int 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)
747 {
748 return call_int_hook(capset, 0, new, old,
749 effective, inheritable, permitted);
750 }
751
752 int security_capable(const struct cred *cred, struct user_namespace *ns,
753 int cap)
754 {
755 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
756 }
757
758 int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns,
759 int cap)
760 {
761 return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT);
762 }
763
764 int security_quotactl(int cmds, int type, int id, struct super_block *sb)
765 {
766 return call_int_hook(quotactl, 0, cmds, type, id, sb);
767 }
768
769 int security_quota_on(struct dentry *dentry)
770 {
771 return call_int_hook(quota_on, 0, dentry);
772 }
773
774 int security_syslog(int type)
775 {
776 return call_int_hook(syslog, 0, type);
777 }
778
779 int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
780 {
781 return call_int_hook(settime, 0, ts, tz);
782 }
783
784 int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
785 {
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);
805 }
806
807 int security_bprm_set_creds(struct linux_binprm *bprm)
808 {
809 return call_int_hook(bprm_set_creds, 0, bprm);
810 }
811
812 int security_bprm_check(struct linux_binprm *bprm)
813 {
814 int ret;
815
816 ret = call_int_hook(bprm_check_security, 0, bprm);
817 if (ret)
818 return ret;
819 return ima_bprm_check(bprm);
820 }
821
822 void security_bprm_committing_creds(struct linux_binprm *bprm)
823 {
824 call_void_hook(bprm_committing_creds, bprm);
825 }
826
827 void security_bprm_committed_creds(struct linux_binprm *bprm)
828 {
829 call_void_hook(bprm_committed_creds, bprm);
830 }
831
832 int security_bprm_secureexec(struct linux_binprm *bprm)
833 {
834 return call_int_hook(bprm_secureexec, 0, bprm);
835 }
836
837 int security_sb_alloc(struct super_block *sb)
838 {
839 int rc = lsm_superblock_alloc(sb);
840
841 if (rc)
842 return rc;
843 return call_int_hook(sb_alloc_security, 0, sb);
844 }
845
846 void security_sb_free(struct super_block *sb)
847 {
848 call_void_hook(sb_free_security, sb);
849 kfree(sb->s_security);
850 sb->s_security = NULL;
851 }
852
853 int security_sb_copy_data(char *orig, char *copy)
854 {
855 return call_int_hook(sb_copy_data, 0, orig, copy);
856 }
857 EXPORT_SYMBOL(security_sb_copy_data);
858
859 int security_sb_remount(struct super_block *sb, void *data)
860 {
861 return call_int_hook(sb_remount, 0, sb, data);
862 }
863
864 int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
865 {
866 return call_int_hook(sb_kern_mount, 0, sb, flags, data);
867 }
868
869 int security_sb_show_options(struct seq_file *m, struct super_block *sb)
870 {
871 return call_int_hook(sb_show_options, 0, m, sb);
872 }
873
874 int security_sb_statfs(struct dentry *dentry)
875 {
876 return call_int_hook(sb_statfs, 0, dentry);
877 }
878
879 int security_sb_mount(const char *dev_name, const struct path *path,
880 const char *type, unsigned long flags, void *data)
881 {
882 return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
883 }
884
885 int security_sb_umount(struct vfsmount *mnt, int flags)
886 {
887 return call_int_hook(sb_umount, 0, mnt, flags);
888 }
889
890 int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
891 {
892 return call_int_hook(sb_pivotroot, 0, old_path, new_path);
893 }
894
895 int security_sb_set_mnt_opts(struct super_block *sb,
896 struct security_mnt_opts *opts,
897 unsigned long kern_flags,
898 unsigned long *set_kern_flags)
899 {
900 return call_int_hook(sb_set_mnt_opts,
901 opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
902 opts, kern_flags, set_kern_flags);
903 }
904 EXPORT_SYMBOL(security_sb_set_mnt_opts);
905
906 int security_sb_clone_mnt_opts(const struct super_block *oldsb,
907 struct super_block *newsb,
908 unsigned long kern_flags,
909 unsigned long *set_kern_flags)
910 {
911 return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
912 kern_flags, set_kern_flags);
913 }
914 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
915
916 int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
917 {
918 return call_int_hook(sb_parse_opts_str, 0, options, opts);
919 }
920 EXPORT_SYMBOL(security_sb_parse_opts_str);
921
922 int security_inode_alloc(struct inode *inode)
923 {
924 int rc = lsm_inode_alloc(inode);
925
926 if (rc)
927 return rc;
928 return call_int_hook(inode_alloc_security, 0, inode);
929 }
930
931 static 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
939 void security_inode_free(struct inode *inode)
940 {
941 integrity_inode_free(inode);
942 call_void_hook(inode_free_security, inode);
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 }
957 }
958
959 int security_dentry_init_security(struct dentry *dentry, int mode,
960 const struct qstr *name, void **ctx,
961 u32 *ctxlen)
962 {
963 return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
964 name, ctx, ctxlen);
965 }
966 EXPORT_SYMBOL(security_dentry_init_security);
967
968 int 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 }
975 EXPORT_SYMBOL(security_dentry_create_files_as);
976
977 int security_inode_init_security(struct inode *inode, struct inode *dir,
978 const struct qstr *qstr,
979 const initxattrs initxattrs, void *fs_data)
980 {
981 struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
982 struct xattr *lsm_xattr, *evm_xattr, *xattr;
983 int ret;
984
985 if (unlikely(IS_PRIVATE(inode)))
986 return 0;
987
988 if (!initxattrs)
989 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
990 dir, qstr, NULL, NULL, NULL);
991 memset(new_xattrs, 0, sizeof(new_xattrs));
992 lsm_xattr = new_xattrs;
993 ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
994 &lsm_xattr->name,
995 &lsm_xattr->value,
996 &lsm_xattr->value_len);
997 if (ret)
998 goto out;
999
1000 evm_xattr = lsm_xattr + 1;
1001 ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1002 if (ret)
1003 goto out;
1004 ret = initxattrs(inode, new_xattrs, fs_data);
1005 out:
1006 for (xattr = new_xattrs; xattr->value != NULL; xattr++)
1007 kfree(xattr->value);
1008 return (ret == -EOPNOTSUPP) ? 0 : ret;
1009 }
1010 EXPORT_SYMBOL(security_inode_init_security);
1011
1012 int security_old_inode_init_security(struct inode *inode, struct inode *dir,
1013 const struct qstr *qstr, const char **name,
1014 void **value, size_t *len)
1015 {
1016 if (unlikely(IS_PRIVATE(inode)))
1017 return -EOPNOTSUPP;
1018 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1019 qstr, name, value, len);
1020 }
1021 EXPORT_SYMBOL(security_old_inode_init_security);
1022
1023 #ifdef CONFIG_SECURITY_PATH
1024 int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
1025 unsigned int dev)
1026 {
1027 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1028 return 0;
1029 return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1030 }
1031 EXPORT_SYMBOL(security_path_mknod);
1032
1033 int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
1034 {
1035 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1036 return 0;
1037 return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1038 }
1039 EXPORT_SYMBOL(security_path_mkdir);
1040
1041 int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1042 {
1043 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1044 return 0;
1045 return call_int_hook(path_rmdir, 0, dir, dentry);
1046 }
1047 EXPORT_SYMBOL_GPL(security_path_rmdir);
1048
1049 int security_path_unlink(const struct path *dir, struct dentry *dentry)
1050 {
1051 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1052 return 0;
1053 return call_int_hook(path_unlink, 0, dir, dentry);
1054 }
1055 EXPORT_SYMBOL(security_path_unlink);
1056
1057 int security_path_symlink(const struct path *dir, struct dentry *dentry,
1058 const char *old_name)
1059 {
1060 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1061 return 0;
1062 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1063 }
1064 EXPORT_SYMBOL_GPL(security_path_symlink);
1065
1066 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1067 struct dentry *new_dentry)
1068 {
1069 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1070 return 0;
1071 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1072 }
1073 EXPORT_SYMBOL_GPL(security_path_link);
1074
1075 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1076 const struct path *new_dir, struct dentry *new_dentry,
1077 unsigned int flags)
1078 {
1079 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1080 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1081 return 0;
1082
1083 if (flags & RENAME_EXCHANGE) {
1084 int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
1085 old_dir, old_dentry);
1086 if (err)
1087 return err;
1088 }
1089
1090 return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1091 new_dentry);
1092 }
1093 EXPORT_SYMBOL(security_path_rename);
1094
1095 int security_path_truncate(const struct path *path)
1096 {
1097 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1098 return 0;
1099 return call_int_hook(path_truncate, 0, path);
1100 }
1101 EXPORT_SYMBOL_GPL(security_path_truncate);
1102
1103 int security_path_chmod(const struct path *path, umode_t mode)
1104 {
1105 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1106 return 0;
1107 return call_int_hook(path_chmod, 0, path, mode);
1108 }
1109 EXPORT_SYMBOL_GPL(security_path_chmod);
1110
1111 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1112 {
1113 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1114 return 0;
1115 return call_int_hook(path_chown, 0, path, uid, gid);
1116 }
1117 EXPORT_SYMBOL_GPL(security_path_chown);
1118
1119 int security_path_chroot(const struct path *path)
1120 {
1121 return call_int_hook(path_chroot, 0, path);
1122 }
1123 #endif
1124
1125 int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1126 {
1127 if (unlikely(IS_PRIVATE(dir)))
1128 return 0;
1129 return call_int_hook(inode_create, 0, dir, dentry, mode);
1130 }
1131 EXPORT_SYMBOL_GPL(security_inode_create);
1132
1133 int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1134 struct dentry *new_dentry)
1135 {
1136 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1137 return 0;
1138 return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
1139 }
1140
1141 int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1142 {
1143 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1144 return 0;
1145 return call_int_hook(inode_unlink, 0, dir, dentry);
1146 }
1147
1148 int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1149 const char *old_name)
1150 {
1151 if (unlikely(IS_PRIVATE(dir)))
1152 return 0;
1153 return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
1154 }
1155
1156 int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1157 {
1158 if (unlikely(IS_PRIVATE(dir)))
1159 return 0;
1160 return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
1161 }
1162 EXPORT_SYMBOL_GPL(security_inode_mkdir);
1163
1164 int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1165 {
1166 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1167 return 0;
1168 return call_int_hook(inode_rmdir, 0, dir, dentry);
1169 }
1170
1171 int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1172 {
1173 if (unlikely(IS_PRIVATE(dir)))
1174 return 0;
1175 return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
1176 }
1177
1178 int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
1179 struct inode *new_dir, struct dentry *new_dentry,
1180 unsigned int flags)
1181 {
1182 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1183 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
1184 return 0;
1185
1186 if (flags & RENAME_EXCHANGE) {
1187 int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
1188 old_dir, old_dentry);
1189 if (err)
1190 return err;
1191 }
1192
1193 return call_int_hook(inode_rename, 0, old_dir, old_dentry,
1194 new_dir, new_dentry);
1195 }
1196
1197 int security_inode_readlink(struct dentry *dentry)
1198 {
1199 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1200 return 0;
1201 return call_int_hook(inode_readlink, 0, dentry);
1202 }
1203 EXPORT_SYMBOL_GPL(security_inode_readlink);
1204
1205 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1206 bool rcu)
1207 {
1208 if (unlikely(IS_PRIVATE(inode)))
1209 return 0;
1210 return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
1211 }
1212
1213 int security_inode_permission(struct inode *inode, int mask)
1214 {
1215 if (unlikely(IS_PRIVATE(inode)))
1216 return 0;
1217 return call_int_hook(inode_permission, 0, inode, mask);
1218 }
1219 EXPORT_SYMBOL_GPL(security_inode_permission);
1220
1221 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1222 {
1223 int ret;
1224
1225 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1226 return 0;
1227 ret = call_int_hook(inode_setattr, 0, dentry, attr);
1228 if (ret)
1229 return ret;
1230 return evm_inode_setattr(dentry, attr);
1231 }
1232 EXPORT_SYMBOL_GPL(security_inode_setattr);
1233
1234 int security_inode_getattr(const struct path *path)
1235 {
1236 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1237 return 0;
1238 return call_int_hook(inode_getattr, 0, path);
1239 }
1240
1241 int security_inode_setxattr(struct dentry *dentry, const char *name,
1242 const void *value, size_t size, int flags)
1243 {
1244 int ret;
1245
1246 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1247 return 0;
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,
1253 flags);
1254
1255 if (ret == 1)
1256 ret = cap_inode_setxattr(dentry, name, value, size, flags);
1257 if (ret)
1258 return ret;
1259 ret = ima_inode_setxattr(dentry, name, value, size);
1260 if (ret)
1261 return ret;
1262 return evm_inode_setxattr(dentry, name, value, size);
1263 }
1264
1265 void security_inode_post_setxattr(struct dentry *dentry, const char *name,
1266 const void *value, size_t size, int flags)
1267 {
1268 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1269 return;
1270 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
1271 evm_inode_post_setxattr(dentry, name, value, size);
1272 }
1273
1274 int security_inode_getxattr(struct dentry *dentry, const char *name)
1275 {
1276 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1277 return 0;
1278 return call_int_hook(inode_getxattr, 0, dentry, name);
1279 }
1280
1281 int security_inode_listxattr(struct dentry *dentry)
1282 {
1283 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1284 return 0;
1285 return call_int_hook(inode_listxattr, 0, dentry);
1286 }
1287
1288 int security_inode_removexattr(struct dentry *dentry, const char *name)
1289 {
1290 int ret;
1291
1292 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1293 return 0;
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);
1301 if (ret)
1302 return ret;
1303 ret = ima_inode_removexattr(dentry, name);
1304 if (ret)
1305 return ret;
1306 return evm_inode_removexattr(dentry, name);
1307 }
1308
1309 int security_inode_need_killpriv(struct dentry *dentry)
1310 {
1311 return call_int_hook(inode_need_killpriv, 0, dentry);
1312 }
1313
1314 int security_inode_killpriv(struct dentry *dentry)
1315 {
1316 return call_int_hook(inode_killpriv, 0, dentry);
1317 }
1318
1319 int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
1320 {
1321 struct security_hook_list *hp;
1322 int rc;
1323
1324 if (unlikely(IS_PRIVATE(inode)))
1325 return -EOPNOTSUPP;
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;
1335 }
1336
1337 int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
1338 {
1339 struct security_hook_list *hp;
1340 int rc;
1341
1342 if (unlikely(IS_PRIVATE(inode)))
1343 return -EOPNOTSUPP;
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;
1354 }
1355
1356 int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
1357 {
1358 if (unlikely(IS_PRIVATE(inode)))
1359 return 0;
1360 return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
1361 }
1362 EXPORT_SYMBOL(security_inode_listsecurity);
1363
1364 void security_inode_getsecid(struct inode *inode, u32 *secid)
1365 {
1366 call_void_hook(inode_getsecid, inode, secid);
1367 }
1368
1369 int security_inode_copy_up(struct dentry *src, struct cred **new)
1370 {
1371 return call_int_hook(inode_copy_up, 0, src, new);
1372 }
1373 EXPORT_SYMBOL(security_inode_copy_up);
1374
1375 int security_inode_copy_up_xattr(const char *name)
1376 {
1377 return call_int_hook(inode_copy_up_xattr, -EOPNOTSUPP, name);
1378 }
1379 EXPORT_SYMBOL(security_inode_copy_up_xattr);
1380
1381 int security_file_permission(struct file *file, int mask)
1382 {
1383 int ret;
1384
1385 ret = call_int_hook(file_permission, 0, file, mask);
1386 if (ret)
1387 return ret;
1388
1389 return fsnotify_perm(file, mask);
1390 }
1391 EXPORT_SYMBOL_GPL(security_file_permission);
1392
1393 int security_file_alloc(struct file *file)
1394 {
1395 int rc = lsm_file_alloc(file);
1396
1397 if (rc)
1398 return rc;
1399 return call_int_hook(file_alloc_security, 0, file);
1400 }
1401
1402 void security_file_free(struct file *file)
1403 {
1404 call_void_hook(file_free_security, file);
1405
1406 kfree(file->f_security);
1407 file->f_security = NULL;
1408 }
1409
1410 int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1411 {
1412 return call_int_hook(file_ioctl, 0, file, cmd, arg);
1413 }
1414
1415 static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
1416 {
1417 /*
1418 * Does we have PROT_READ and does the application expect
1419 * it to imply PROT_EXEC? If not, nothing to talk about...
1420 */
1421 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
1422 return prot;
1423 if (!(current->personality & READ_IMPLIES_EXEC))
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
1432 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
1433 */
1434 if (!path_noexec(&file->f_path)) {
1435 #ifndef CONFIG_MMU
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 }
1441 #endif
1442 return prot | PROT_EXEC;
1443 }
1444 /* anything on noexec mount won't get PROT_EXEC */
1445 return prot;
1446 }
1447
1448 int security_mmap_file(struct file *file, unsigned long prot,
1449 unsigned long flags)
1450 {
1451 int ret;
1452 ret = call_int_hook(mmap_file, 0, file, prot,
1453 mmap_prot(file, prot), flags);
1454 if (ret)
1455 return ret;
1456 return ima_file_mmap(file, prot);
1457 }
1458 EXPORT_SYMBOL_GPL(security_mmap_file);
1459
1460 int security_mmap_addr(unsigned long addr)
1461 {
1462 return call_int_hook(mmap_addr, 0, addr);
1463 }
1464
1465 int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
1466 unsigned long prot)
1467 {
1468 return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
1469 }
1470
1471 int security_file_lock(struct file *file, unsigned int cmd)
1472 {
1473 return call_int_hook(file_lock, 0, file, cmd);
1474 }
1475
1476 int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1477 {
1478 return call_int_hook(file_fcntl, 0, file, cmd, arg);
1479 }
1480
1481 void security_file_set_fowner(struct file *file)
1482 {
1483 call_void_hook(file_set_fowner, file);
1484 }
1485
1486 int security_file_send_sigiotask(struct task_struct *tsk,
1487 struct fown_struct *fown, int sig)
1488 {
1489 return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
1490 }
1491
1492 int security_file_receive(struct file *file)
1493 {
1494 return call_int_hook(file_receive, 0, file);
1495 }
1496
1497 int security_file_open(struct file *file, const struct cred *cred)
1498 {
1499 int ret;
1500
1501 ret = call_int_hook(file_open, 0, file, cred);
1502 if (ret)
1503 return ret;
1504
1505 return fsnotify_perm(file, MAY_OPEN);
1506 }
1507
1508 int security_task_create(unsigned long clone_flags)
1509 {
1510 return call_int_hook(task_create, 0, clone_flags);
1511 }
1512
1513 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
1514 {
1515 int rc = lsm_task_alloc(task);
1516
1517 if (rc)
1518 return rc;
1519 return call_int_hook(task_alloc, 0, task, clone_flags);
1520 }
1521
1522 void security_task_free(struct task_struct *task)
1523 {
1524 call_void_hook(task_free, task);
1525
1526 kfree(task->security);
1527 task->security = NULL;
1528 }
1529
1530 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1531 {
1532 int rc = lsm_cred_alloc(cred, gfp);
1533
1534 if (rc)
1535 return rc;
1536
1537 return call_int_hook(cred_alloc_blank, 0, cred, gfp);
1538 }
1539
1540 void security_cred_free(struct cred *cred)
1541 {
1542 call_void_hook(cred_free, cred);
1543
1544 kfree(cred->security);
1545 cred->security = NULL;
1546 }
1547
1548 int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
1549 {
1550 int rc = lsm_cred_alloc(new, gfp);
1551
1552 if (rc)
1553 return rc;
1554
1555 return call_int_hook(cred_prepare, 0, new, old, gfp);
1556 }
1557
1558 void security_transfer_creds(struct cred *new, const struct cred *old)
1559 {
1560 call_void_hook(cred_transfer, new, old);
1561 }
1562
1563 int security_kernel_act_as(struct cred *new, u32 secid)
1564 {
1565 return call_int_hook(kernel_act_as, 0, new, secid);
1566 }
1567
1568 int security_kernel_create_files_as(struct cred *new, struct inode *inode)
1569 {
1570 return call_int_hook(kernel_create_files_as, 0, new, inode);
1571 }
1572
1573 int security_kernel_module_request(char *kmod_name)
1574 {
1575 return call_int_hook(kernel_module_request, 0, kmod_name);
1576 }
1577
1578 int 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 }
1587 EXPORT_SYMBOL_GPL(security_kernel_read_file);
1588
1589 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
1590 enum kernel_read_file_id id)
1591 {
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);
1598 }
1599 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
1600
1601 int security_task_fix_setuid(struct cred *new, const struct cred *old,
1602 int flags)
1603 {
1604 return call_int_hook(task_fix_setuid, 0, new, old, flags);
1605 }
1606
1607 int security_task_setpgid(struct task_struct *p, pid_t pgid)
1608 {
1609 return call_int_hook(task_setpgid, 0, p, pgid);
1610 }
1611
1612 int security_task_getpgid(struct task_struct *p)
1613 {
1614 return call_int_hook(task_getpgid, 0, p);
1615 }
1616
1617 int security_task_getsid(struct task_struct *p)
1618 {
1619 return call_int_hook(task_getsid, 0, p);
1620 }
1621
1622 void security_task_getsecid(struct task_struct *p, u32 *secid)
1623 {
1624 *secid = 0;
1625 call_void_hook(task_getsecid, p, secid);
1626 }
1627 EXPORT_SYMBOL(security_task_getsecid);
1628
1629 int security_task_setnice(struct task_struct *p, int nice)
1630 {
1631 return call_int_hook(task_setnice, 0, p, nice);
1632 }
1633
1634 int security_task_setioprio(struct task_struct *p, int ioprio)
1635 {
1636 return call_int_hook(task_setioprio, 0, p, ioprio);
1637 }
1638
1639 int security_task_getioprio(struct task_struct *p)
1640 {
1641 return call_int_hook(task_getioprio, 0, p);
1642 }
1643
1644 int 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
1650 int security_task_setrlimit(struct task_struct *p, unsigned int resource,
1651 struct rlimit *new_rlim)
1652 {
1653 return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
1654 }
1655
1656 int security_task_setscheduler(struct task_struct *p)
1657 {
1658 return call_int_hook(task_setscheduler, 0, p);
1659 }
1660
1661 int security_task_getscheduler(struct task_struct *p)
1662 {
1663 return call_int_hook(task_getscheduler, 0, p);
1664 }
1665
1666 int security_task_movememory(struct task_struct *p)
1667 {
1668 return call_int_hook(task_movememory, 0, p);
1669 }
1670
1671 int security_task_kill(struct task_struct *p, struct siginfo *info,
1672 int sig, u32 secid)
1673 {
1674 return call_int_hook(task_kill, 0, p, info, sig, secid);
1675 }
1676
1677 #ifdef CONFIG_SECURITY_STACKING
1678 static char *nolsm = "-default";
1679 #define NOLSMLEN 9
1680
1681 static int lsm_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1682 unsigned long arg4, unsigned long arg5)
1683 {
1684 char *lsm = lsm_of_task(current);
1685 char buffer[SECURITY_NAME_MAX + 1];
1686 __user char *optval = (__user char *)arg2;
1687 __user int *optlen = (__user int *)arg3;
1688 struct security_hook_list *hp;
1689 int dlen;
1690 int len;
1691
1692 switch (option) {
1693 case PR_GET_DISPLAY_LSM:
1694 len = arg4;
1695 if (lsm[0] == '\0') {
1696 lsm = nolsm;
1697 dlen = NOLSMLEN;
1698 } else
1699 dlen = strlen(lsm) + 1;
1700 if (dlen > len)
1701 return -ERANGE;
1702 if (copy_to_user(optval, lsm, dlen))
1703 return -EFAULT;
1704 if (put_user(dlen, optlen))
1705 return -EFAULT;
1706 break;
1707 case PR_SET_DISPLAY_LSM:
1708 len = arg3;
1709 if (len > SECURITY_NAME_MAX)
1710 return -EINVAL;
1711 if (copy_from_user(buffer, optval, len))
1712 return -EFAULT;
1713 buffer[len] = '\0';
1714 /* verify the requested LSM is registered */
1715 list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
1716 if (!strcmp(buffer, hp->lsm)) {
1717 strcpy(lsm, hp->lsm);
1718 goto out;
1719 }
1720 }
1721 if (!strncmp(buffer, nolsm, NOLSMLEN))
1722 lsm[0] = '\0';
1723 else
1724 return -ENOENT;
1725 break;
1726 default:
1727 return -ENOSYS;
1728 }
1729 out:
1730 return 0;
1731 }
1732 #endif
1733
1734 int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1735 unsigned long arg4, unsigned long arg5)
1736 {
1737 int thisrc;
1738 int rc = -ENOSYS;
1739 struct security_hook_list *hp;
1740
1741 #ifdef CONFIG_SECURITY_STACKING
1742 rc = lsm_task_prctl(option, arg2, arg3, arg4, arg5);
1743 if (rc != -ENOSYS)
1744 return rc;
1745 #endif
1746
1747 list_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
1748 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
1749 if (thisrc != -ENOSYS) {
1750 rc = thisrc;
1751 if (thisrc != 0)
1752 break;
1753 }
1754 }
1755 return rc;
1756 }
1757
1758 void security_task_to_inode(struct task_struct *p, struct inode *inode)
1759 {
1760 call_void_hook(task_to_inode, p, inode);
1761 }
1762
1763 int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
1764 {
1765 return call_int_hook(ipc_permission, 0, ipcp, flag);
1766 }
1767
1768 void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
1769 {
1770 *secid = 0;
1771 call_void_hook(ipc_getsecid, ipcp, secid);
1772 }
1773
1774 int security_msg_msg_alloc(struct msg_msg *msg)
1775 {
1776 int rc = lsm_msg_msg_alloc(msg);
1777
1778 if (rc)
1779 return rc;
1780 return call_int_hook(msg_msg_alloc_security, 0, msg);
1781 }
1782
1783 void security_msg_msg_free(struct msg_msg *msg)
1784 {
1785 call_void_hook(msg_msg_free_security, msg);
1786 kfree(msg->security);
1787 msg->security = NULL;
1788 }
1789
1790 int security_msg_queue_alloc(struct msg_queue *msq)
1791 {
1792 int rc = lsm_ipc_alloc(&msq->q_perm);
1793
1794 if (rc)
1795 return rc;
1796 return call_int_hook(msg_queue_alloc_security, 0, msq);
1797 }
1798
1799 void security_msg_queue_free(struct msg_queue *msq)
1800 {
1801 struct kern_ipc_perm *kip = &msq->q_perm;
1802
1803 call_void_hook(msg_queue_free_security, msq);
1804 kfree(kip->security);
1805 kip->security = NULL;
1806 }
1807
1808 int security_msg_queue_associate(struct msg_queue *msq, int msqflg)
1809 {
1810 return call_int_hook(msg_queue_associate, 0, msq, msqflg);
1811 }
1812
1813 int security_msg_queue_msgctl(struct msg_queue *msq, int cmd)
1814 {
1815 return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
1816 }
1817
1818 int security_msg_queue_msgsnd(struct msg_queue *msq,
1819 struct msg_msg *msg, int msqflg)
1820 {
1821 return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
1822 }
1823
1824 int security_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
1825 struct task_struct *target, long type, int mode)
1826 {
1827 return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
1828 }
1829
1830 int security_shm_alloc(struct shmid_kernel *shp)
1831 {
1832 int rc = lsm_ipc_alloc(&shp->shm_perm);
1833
1834 if (rc)
1835 return rc;
1836 return call_int_hook(shm_alloc_security, 0, shp);
1837 }
1838
1839 void security_shm_free(struct shmid_kernel *shp)
1840 {
1841 struct kern_ipc_perm *kip = &shp->shm_perm;
1842
1843 call_void_hook(shm_free_security, shp);
1844 kfree(kip->security);
1845 kip->security = NULL;
1846 }
1847
1848 int security_shm_associate(struct shmid_kernel *shp, int shmflg)
1849 {
1850 return call_int_hook(shm_associate, 0, shp, shmflg);
1851 }
1852
1853 int security_shm_shmctl(struct shmid_kernel *shp, int cmd)
1854 {
1855 return call_int_hook(shm_shmctl, 0, shp, cmd);
1856 }
1857
1858 int security_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, int shmflg)
1859 {
1860 return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
1861 }
1862
1863 int security_sem_alloc(struct sem_array *sma)
1864 {
1865 int rc = lsm_ipc_alloc(&sma->sem_perm);
1866
1867 if (rc)
1868 return rc;
1869 return call_int_hook(sem_alloc_security, 0, sma);
1870 }
1871
1872 void security_sem_free(struct sem_array *sma)
1873 {
1874 struct kern_ipc_perm *kip = &sma->sem_perm;
1875
1876 call_void_hook(sem_free_security, sma);
1877 kfree(kip->security);
1878 kip->security = NULL;
1879 }
1880
1881 int security_sem_associate(struct sem_array *sma, int semflg)
1882 {
1883 return call_int_hook(sem_associate, 0, sma, semflg);
1884 }
1885
1886 int security_sem_semctl(struct sem_array *sma, int cmd)
1887 {
1888 return call_int_hook(sem_semctl, 0, sma, cmd);
1889 }
1890
1891 int security_sem_semop(struct sem_array *sma, struct sembuf *sops,
1892 unsigned nsops, int alter)
1893 {
1894 return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
1895 }
1896
1897 void security_d_instantiate(struct dentry *dentry, struct inode *inode)
1898 {
1899 if (unlikely(inode && IS_PRIVATE(inode)))
1900 return;
1901 call_void_hook(d_instantiate, dentry, inode);
1902 }
1903 EXPORT_SYMBOL(security_d_instantiate);
1904
1905 int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
1906 char **value)
1907 {
1908 #ifdef CONFIG_SECURITY_STACKING
1909 char *speclsm = lsm_of_task(p);
1910 #endif
1911 struct security_hook_list *hp;
1912 char *vp;
1913 char *cp = NULL;
1914 int trc;
1915 int rc;
1916
1917 /*
1918 * "context" requires work here in addition to what
1919 * the modules provide.
1920 */
1921 if (strcmp(name, "context") == 0) {
1922 *value = NULL;
1923 rc = -EINVAL;
1924 list_for_each_entry(hp,
1925 &security_hook_heads.getprocattr, list) {
1926 if (lsm != NULL && strcmp(lsm, hp->lsm))
1927 continue;
1928 trc = hp->hook.getprocattr(p, "context", &vp);
1929 if (trc == -ENOENT)
1930 continue;
1931 if (trc <= 0) {
1932 kfree(*value);
1933 return trc;
1934 }
1935 rc = trc;
1936 if (*value == NULL) {
1937 *value = vp;
1938 } else {
1939 cp = kasprintf(GFP_KERNEL, "%s,%s", *value, vp);
1940 if (cp == NULL) {
1941 kfree(*value);
1942 kfree(vp);
1943 return -ENOMEM;
1944 }
1945 kfree(*value);
1946 kfree(vp);
1947 *value = cp;
1948 }
1949 }
1950 if (rc > 0)
1951 return strlen(*value);
1952 return rc;
1953 }
1954
1955 list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
1956 if (lsm != NULL && strcmp(lsm, hp->lsm))
1957 continue;
1958 #ifdef CONFIG_SECURITY_STACKING
1959 if (!lsm && speclsm && speclsm[0] && strcmp(speclsm, hp->lsm))
1960 continue;
1961 #endif
1962 rc = hp->hook.getprocattr(p, name, value);
1963 if (rc != -ENOSYS)
1964 return rc;
1965 }
1966 return -EINVAL;
1967 }
1968
1969 int security_setprocattr(const char *lsm, const char *name, void *value,
1970 size_t size)
1971 {
1972 #ifdef CONFIG_SECURITY_STACKING
1973 char *speclsm = lsm_of_task(current);
1974 struct lsm_value *lsm_value = NULL;
1975 int count;
1976 #else
1977 char *tvalue;
1978 #endif
1979 struct security_hook_list *hp;
1980 int rc;
1981 char *temp;
1982 char *cp;
1983
1984 /*
1985 * If lsm is NULL look at all the modules to find one
1986 * that processes name. If lsm is not NULL only look at
1987 * that module.
1988 *
1989 * "context" is handled directly here.
1990 */
1991 if (strcmp(name, "context") == 0) {
1992 rc = -EINVAL;
1993 temp = kmemdup(value, size + 1, GFP_KERNEL);
1994 if (!temp)
1995 return -ENOMEM;
1996
1997 temp[size] = '\0';
1998 cp = strrchr(temp, '\'');
1999 if (!cp)
2000 goto free_out;
2001
2002 cp[1] = '\0';
2003 #ifdef CONFIG_SECURITY_STACKING
2004 lsm_value = kzalloc(sizeof(*lsm_value) * LSM_MAX_MAJOR,
2005 GFP_KERNEL);
2006 if (!lsm_value) {
2007 rc = -ENOMEM;
2008 goto free_out;
2009 }
2010
2011 count = lsm_parse_context(temp, lsm_value);
2012 if (count <= 0)
2013 goto free_out;
2014
2015 for (count--; count >= 0; count--) {
2016 list_for_each_entry(hp,
2017 &security_hook_heads.setprocattr, list) {
2018
2019 if (lsm && strcmp(lsm, hp->lsm))
2020 continue;
2021 if (!strcmp(hp->lsm, lsm_value[count].lsm)) {
2022 rc = hp->hook.setprocattr("context",
2023 lsm_value[count].data,
2024 strlen(lsm_value[count].data));
2025 break;
2026 }
2027 }
2028 if (rc < 0 || (lsm && rc >0))
2029 break;
2030 }
2031 #else /* CONFIG_SECURITY_STACKING */
2032 cp = strstr(temp, "='");
2033 if (!cp)
2034 goto free_out;
2035 *cp = '\0';
2036 tvalue = strchr(cp + 2, '\'');
2037 if (!tvalue)
2038 goto free_out;
2039 list_for_each_entry(hp, &security_hook_heads.setprocattr,
2040 list) {
2041 if (lsm == NULL || !strcmp(lsm, hp->lsm)) {
2042 rc = hp->hook.setprocattr(name, tvalue, size);
2043 break;
2044 }
2045 }
2046 #endif /* CONFIG_SECURITY_STACKING */
2047 free_out:
2048 kfree(temp);
2049 #ifdef CONFIG_SECURITY_STACKING
2050 kfree(lsm_value);
2051 #endif
2052 if (rc >= 0)
2053 return size;
2054 return rc;
2055 }
2056
2057 list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
2058 if (lsm && strcmp(lsm, hp->lsm))
2059 continue;
2060 #ifdef CONFIG_SECURITY_STACKING
2061 if (!lsm && speclsm && speclsm[0] && strcmp(speclsm, hp->lsm))
2062 continue;
2063 #endif
2064 rc = hp->hook.setprocattr(name, value, size);
2065 if (rc)
2066 return rc;
2067 }
2068 return -EINVAL;
2069 }
2070
2071 int security_netlink_send(struct sock *sk, struct sk_buff *skb)
2072 {
2073 return call_int_hook(netlink_send, 0, sk, skb);
2074 }
2075
2076 int security_ismaclabel(const char *name)
2077 {
2078 return call_int_hook(ismaclabel, 0, name);
2079 }
2080 EXPORT_SYMBOL(security_ismaclabel);
2081
2082 int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2083 {
2084 return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
2085 seclen);
2086 }
2087 EXPORT_SYMBOL(security_secid_to_secctx);
2088
2089 int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
2090 {
2091 *secid = 0;
2092 return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
2093 }
2094 EXPORT_SYMBOL(security_secctx_to_secid);
2095
2096 void security_release_secctx(char *secdata, u32 seclen)
2097 {
2098 #ifdef CONFIG_SECURITY_STACKING
2099 char *speclsm = lsm_of_task(current);
2100 #endif
2101 struct security_hook_list *hp;
2102
2103 list_for_each_entry(hp, &security_hook_heads.release_secctx, list) {
2104 #ifdef CONFIG_SECURITY_STACKING
2105 if (speclsm[0] && strcmp(hp->lsm, speclsm))
2106 continue;
2107 #endif
2108 hp->hook.release_secctx(secdata, seclen);
2109 break;
2110 }
2111 }
2112 EXPORT_SYMBOL(security_release_secctx);
2113
2114 void security_inode_invalidate_secctx(struct inode *inode)
2115 {
2116 call_void_hook(inode_invalidate_secctx, inode);
2117 }
2118 EXPORT_SYMBOL(security_inode_invalidate_secctx);
2119
2120 int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
2121 {
2122 return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
2123 }
2124 EXPORT_SYMBOL(security_inode_notifysecctx);
2125
2126 int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
2127 {
2128 return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
2129 }
2130 EXPORT_SYMBOL(security_inode_setsecctx);
2131
2132 int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
2133 {
2134 return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
2135 }
2136 EXPORT_SYMBOL(security_inode_getsecctx);
2137
2138 #ifdef CONFIG_SECURITY_NETWORK
2139
2140 int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
2141 {
2142 return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
2143 }
2144 EXPORT_SYMBOL(security_unix_stream_connect);
2145
2146 int security_unix_may_send(struct socket *sock, struct socket *other)
2147 {
2148 return call_int_hook(unix_may_send, 0, sock, other);
2149 }
2150 EXPORT_SYMBOL(security_unix_may_send);
2151
2152 int security_socket_create(int family, int type, int protocol, int kern)
2153 {
2154 return call_int_hook(socket_create, 0, family, type, protocol, kern);
2155 }
2156
2157 int security_socket_post_create(struct socket *sock, int family,
2158 int type, int protocol, int kern)
2159 {
2160 return call_int_hook(socket_post_create, 0, sock, family, type,
2161 protocol, kern);
2162 }
2163
2164 int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
2165 {
2166 return call_int_hook(socket_bind, 0, sock, address, addrlen);
2167 }
2168
2169 int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
2170 {
2171 return call_int_hook(socket_connect, 0, sock, address, addrlen);
2172 }
2173
2174 int security_socket_listen(struct socket *sock, int backlog)
2175 {
2176 return call_int_hook(socket_listen, 0, sock, backlog);
2177 }
2178
2179 int security_socket_accept(struct socket *sock, struct socket *newsock)
2180 {
2181 return call_int_hook(socket_accept, 0, sock, newsock);
2182 }
2183
2184 int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
2185 {
2186 return call_int_hook(socket_sendmsg, 0, sock, msg, size);
2187 }
2188
2189 int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
2190 int size, int flags)
2191 {
2192 return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
2193 }
2194
2195 int security_socket_getsockname(struct socket *sock)
2196 {
2197 return call_int_hook(socket_getsockname, 0, sock);
2198 }
2199
2200 int security_socket_getpeername(struct socket *sock)
2201 {
2202 return call_int_hook(socket_getpeername, 0, sock);
2203 }
2204
2205 int security_socket_getsockopt(struct socket *sock, int level, int optname)
2206 {
2207 return call_int_hook(socket_getsockopt, 0, sock, level, optname);
2208 }
2209
2210 int security_socket_setsockopt(struct socket *sock, int level, int optname)
2211 {
2212 return call_int_hook(socket_setsockopt, 0, sock, level, optname);
2213 }
2214
2215 int security_socket_shutdown(struct socket *sock, int how)
2216 {
2217 return call_int_hook(socket_shutdown, 0, sock, how);
2218 }
2219
2220 int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2221 {
2222 return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
2223 }
2224 EXPORT_SYMBOL(security_sock_rcv_skb);
2225
2226 int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
2227 int __user *optlen, unsigned len)
2228 {
2229 #ifdef CONFIG_SECURITY_STACKING
2230 struct security_hook_list *hp;
2231 char *lsm = lsm_of_task(current);
2232
2233 list_for_each_entry(hp, &security_hook_heads.socket_getpeersec_stream,
2234 list) {
2235 if (!lsm || !lsm[0] || !strcmp(lsm, hp->lsm))
2236 return hp->hook.socket_getpeersec_stream(sock, optval,
2237 optlen, len);
2238 }
2239 return -ENOPROTOOPT;
2240 #else
2241 return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
2242 optval, optlen, len);
2243 #endif
2244 }
2245
2246 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
2247 {
2248 return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
2249 skb, secid);
2250 }
2251 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
2252
2253 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
2254 {
2255 int rc = lsm_sock_alloc(sk, priority);
2256
2257 if (rc)
2258 return rc;
2259 return call_int_hook(sk_alloc_security, 0, sk, family, priority);
2260 }
2261
2262 void security_sk_free(struct sock *sk)
2263 {
2264 call_void_hook(sk_free_security, sk);
2265 kfree(sk->sk_security);
2266 sk->sk_security = NULL;
2267 }
2268
2269 void security_sk_clone(const struct sock *sk, struct sock *newsk)
2270 {
2271 call_void_hook(sk_clone_security, sk, newsk);
2272 }
2273 EXPORT_SYMBOL(security_sk_clone);
2274
2275 void security_sk_classify_flow(struct sock *sk, struct flowi *fl)
2276 {
2277 call_void_hook(sk_getsecid, sk, &fl->flowi_secid);
2278 }
2279 EXPORT_SYMBOL(security_sk_classify_flow);
2280
2281 void security_req_classify_flow(const struct request_sock *req, struct flowi *fl)
2282 {
2283 call_void_hook(req_classify_flow, req, fl);
2284 }
2285 EXPORT_SYMBOL(security_req_classify_flow);
2286
2287 void security_sock_graft(struct sock *sk, struct socket *parent)
2288 {
2289 call_void_hook(sock_graft, sk, parent);
2290 }
2291 EXPORT_SYMBOL(security_sock_graft);
2292
2293 int security_inet_conn_request(struct sock *sk,
2294 struct sk_buff *skb, struct request_sock *req)
2295 {
2296 return call_int_hook(inet_conn_request, 0, sk, skb, req);
2297 }
2298 EXPORT_SYMBOL(security_inet_conn_request);
2299
2300 void security_inet_csk_clone(struct sock *newsk,
2301 const struct request_sock *req)
2302 {
2303 call_void_hook(inet_csk_clone, newsk, req);
2304 }
2305
2306 void security_inet_conn_established(struct sock *sk,
2307 struct sk_buff *skb)
2308 {
2309 call_void_hook(inet_conn_established, sk, skb);
2310 }
2311
2312 int security_secmark_relabel_packet(u32 secid)
2313 {
2314 return call_int_hook(secmark_relabel_packet, 0, secid);
2315 }
2316 EXPORT_SYMBOL(security_secmark_relabel_packet);
2317
2318 void security_secmark_refcount_inc(void)
2319 {
2320 call_void_hook(secmark_refcount_inc);
2321 }
2322 EXPORT_SYMBOL(security_secmark_refcount_inc);
2323
2324 void security_secmark_refcount_dec(void)
2325 {
2326 call_void_hook(secmark_refcount_dec);
2327 }
2328 EXPORT_SYMBOL(security_secmark_refcount_dec);
2329
2330 int security_tun_dev_alloc_security(void **security)
2331 {
2332 return call_int_hook(tun_dev_alloc_security, 0, security);
2333 }
2334 EXPORT_SYMBOL(security_tun_dev_alloc_security);
2335
2336 void security_tun_dev_free_security(void *security)
2337 {
2338 call_void_hook(tun_dev_free_security, security);
2339 }
2340 EXPORT_SYMBOL(security_tun_dev_free_security);
2341
2342 int security_tun_dev_create(void)
2343 {
2344 return call_int_hook(tun_dev_create, 0);
2345 }
2346 EXPORT_SYMBOL(security_tun_dev_create);
2347
2348 int security_tun_dev_attach_queue(void *security)
2349 {
2350 return call_int_hook(tun_dev_attach_queue, 0, security);
2351 }
2352 EXPORT_SYMBOL(security_tun_dev_attach_queue);
2353
2354 int security_tun_dev_attach(struct sock *sk, void *security)
2355 {
2356 return call_int_hook(tun_dev_attach, 0, sk, security);
2357 }
2358 EXPORT_SYMBOL(security_tun_dev_attach);
2359
2360 int security_tun_dev_open(void *security)
2361 {
2362 return call_int_hook(tun_dev_open, 0, security);
2363 }
2364 EXPORT_SYMBOL(security_tun_dev_open);
2365
2366 #endif /* CONFIG_SECURITY_NETWORK */
2367
2368 #ifdef CONFIG_SECURITY_INFINIBAND
2369
2370 int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
2371 {
2372 return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
2373 }
2374 EXPORT_SYMBOL(security_ib_pkey_access);
2375
2376 int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
2377 {
2378 return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
2379 }
2380 EXPORT_SYMBOL(security_ib_endport_manage_subnet);
2381
2382 int security_ib_alloc_security(void **sec)
2383 {
2384 return call_int_hook(ib_alloc_security, 0, sec);
2385 }
2386 EXPORT_SYMBOL(security_ib_alloc_security);
2387
2388 void security_ib_free_security(void *sec)
2389 {
2390 call_void_hook(ib_free_security, sec);
2391 }
2392 EXPORT_SYMBOL(security_ib_free_security);
2393 #endif /* CONFIG_SECURITY_INFINIBAND */
2394
2395 #ifdef CONFIG_SECURITY_NETWORK_XFRM
2396
2397 int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
2398 struct xfrm_user_sec_ctx *sec_ctx,
2399 gfp_t gfp)
2400 {
2401 return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
2402 }
2403 EXPORT_SYMBOL(security_xfrm_policy_alloc);
2404
2405 int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
2406 struct xfrm_sec_ctx **new_ctxp)
2407 {
2408 return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
2409 }
2410
2411 void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
2412 {
2413 call_void_hook(xfrm_policy_free_security, ctx);
2414 }
2415 EXPORT_SYMBOL(security_xfrm_policy_free);
2416
2417 int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
2418 {
2419 return call_int_hook(xfrm_policy_delete_security, 0, ctx);
2420 }
2421
2422 int security_xfrm_state_alloc(struct xfrm_state *x,
2423 struct xfrm_user_sec_ctx *sec_ctx)
2424 {
2425 return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
2426 }
2427 EXPORT_SYMBOL(security_xfrm_state_alloc);
2428
2429 int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
2430 struct xfrm_sec_ctx *polsec, u32 secid)
2431 {
2432 return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
2433 }
2434
2435 int security_xfrm_state_delete(struct xfrm_state *x)
2436 {
2437 return call_int_hook(xfrm_state_delete_security, 0, x);
2438 }
2439 EXPORT_SYMBOL(security_xfrm_state_delete);
2440
2441 void security_xfrm_state_free(struct xfrm_state *x)
2442 {
2443 call_void_hook(xfrm_state_free_security, x);
2444 }
2445
2446 int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid, u8 dir)
2447 {
2448 return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid, dir);
2449 }
2450
2451 int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
2452 struct xfrm_policy *xp,
2453 const struct flowi *fl)
2454 {
2455 struct security_hook_list *hp;
2456 int rc = 1;
2457
2458 /*
2459 * Since this function is expected to return 0 or 1, the judgment
2460 * becomes difficult if multiple LSMs supply this call. Fortunately,
2461 * we can use the first LSM's judgment because currently only SELinux
2462 * supplies this call.
2463 *
2464 * For speed optimization, we explicitly break the loop rather than
2465 * using the macro
2466 */
2467 list_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
2468 list) {
2469 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, fl);
2470 break;
2471 }
2472 return rc;
2473 }
2474
2475 int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
2476 {
2477 return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
2478 }
2479
2480 void security_skb_classify_flow(struct sk_buff *skb, struct flowi *fl)
2481 {
2482 int rc = call_int_hook(xfrm_decode_session, 0, skb, &fl->flowi_secid,
2483 0);
2484
2485 BUG_ON(rc);
2486 }
2487 EXPORT_SYMBOL(security_skb_classify_flow);
2488
2489 #endif /* CONFIG_SECURITY_NETWORK_XFRM */
2490
2491 #ifdef CONFIG_KEYS
2492
2493 int security_key_alloc(struct key *key, const struct cred *cred,
2494 unsigned long flags)
2495 {
2496 int rc = lsm_key_alloc(key);
2497
2498 if (rc)
2499 return rc;
2500 return call_int_hook(key_alloc, 0, key, cred, flags);
2501 }
2502
2503 void security_key_free(struct key *key)
2504 {
2505 call_void_hook(key_free, key);
2506 kfree(key->security);
2507 key->security = NULL;
2508 }
2509
2510 int security_key_permission(key_ref_t key_ref,
2511 const struct cred *cred, unsigned perm)
2512 {
2513 return call_int_hook(key_permission, 0, key_ref, cred, perm);
2514 }
2515
2516 int security_key_getsecurity(struct key *key, char **_buffer)
2517 {
2518 *_buffer = NULL;
2519 return call_int_hook(key_getsecurity, 0, key, _buffer);
2520 }
2521
2522 #endif /* CONFIG_KEYS */
2523
2524 #ifdef CONFIG_AUDIT
2525
2526 int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
2527 {
2528 return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
2529 }
2530
2531 int security_audit_rule_known(struct audit_krule *krule)
2532 {
2533 return call_int_hook(audit_rule_known, 0, krule);
2534 }
2535
2536 void security_audit_rule_free(void *lsmrule)
2537 {
2538 call_void_hook(audit_rule_free, lsmrule);
2539 }
2540
2541 int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
2542 struct audit_context *actx)
2543 {
2544 return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule,
2545 actx);
2546 }
2547 #endif /* CONFIG_AUDIT */