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