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