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