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