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