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