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