lsm: move the SCTP hook comments to security/security.c
[linux-2.6-block.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
1661372c 9 * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com>
1da177e4
LT
10 */
11
9b8c7c14
KC
12#define pr_fmt(fmt) "LSM: " fmt
13
afdb09c7 14#include <linux/bpf.h>
c59ede7b 15#include <linux/capability.h>
d47be3df 16#include <linux/dcache.h>
876979c9 17#include <linux/export.h>
1da177e4
LT
18#include <linux/init.h>
19#include <linux/kernel.h>
b89999d0 20#include <linux/kernel_read_file.h>
3c4ed7bd 21#include <linux/lsm_hooks.h>
f381c272 22#include <linux/integrity.h>
6c21a7fb 23#include <linux/ima.h>
3e1be52d 24#include <linux/evm.h>
40401530 25#include <linux/fsnotify.h>
8b3ec681
AV
26#include <linux/mman.h>
27#include <linux/mount.h>
28#include <linux/personality.h>
75331a59 29#include <linux/backing-dev.h>
3bb857e4 30#include <linux/string.h>
ecd5f82e 31#include <linux/msg.h>
40401530 32#include <net/flow.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",
99df7a28 56 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
59438b46
SS
57 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
58 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
59 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
60 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
61 [LOCKDOWN_DEBUGFS] = "debugfs access",
62 [LOCKDOWN_XMON_WR] = "xmon write access",
51e1bb9e 63 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
eadb2f47 64 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
b8f3e488 65 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
59438b46
SS
66 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
67 [LOCKDOWN_KCORE] = "/proc/kcore access",
68 [LOCKDOWN_KPROBES] = "use of kprobes",
71330842 69 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
eadb2f47 70 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
59438b46
SS
71 [LOCKDOWN_PERF] = "unsafe use of perf",
72 [LOCKDOWN_TRACEFS] = "use of tracefs",
73 [LOCKDOWN_XMON_RW] = "xmon read and write access",
c7a5899e 74 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
59438b46
SS
75 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
76};
77
3dfc9b02 78struct security_hook_heads security_hook_heads __lsm_ro_after_init;
42df744c 79static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
8f408ab6 80
33bf60ca 81static struct kmem_cache *lsm_file_cache;
afb1cbe3 82static struct kmem_cache *lsm_inode_cache;
33bf60ca 83
d69dece5 84char *lsm_names;
bbd3662a
CS
85static struct lsm_blob_sizes blob_sizes __lsm_ro_after_init;
86
076c54c5 87/* Boot-time LSM user choice */
79f7865d 88static __initdata const char *chosen_lsm_order;
5ef4e419 89static __initdata const char *chosen_major_lsm;
1da177e4 90
13e735c0
KC
91static __initconst const char * const builtin_lsm_order = CONFIG_LSM;
92
2d4d5119
KC
93/* Ordered list of LSMs to initialize. */
94static __initdata struct lsm_info **ordered_lsms;
14bd99c8 95static __initdata struct lsm_info *exclusive;
2d4d5119 96
9b8c7c14
KC
97static __initdata bool debug;
98#define init_debug(...) \
99 do { \
100 if (debug) \
101 pr_info(__VA_ARGS__); \
102 } while (0)
103
f4941d75
KC
104static bool __init is_enabled(struct lsm_info *lsm)
105{
a8027fb0
KC
106 if (!lsm->enabled)
107 return false;
f4941d75 108
a8027fb0 109 return *lsm->enabled;
f4941d75
KC
110}
111
112/* Mark an LSM's enabled flag. */
113static int lsm_enabled_true __initdata = 1;
114static int lsm_enabled_false __initdata = 0;
115static void __init set_enabled(struct lsm_info *lsm, bool enabled)
116{
117 /*
118 * When an LSM hasn't configured an enable variable, we can use
119 * a hard-coded location for storing the default enabled state.
120 */
121 if (!lsm->enabled) {
122 if (enabled)
123 lsm->enabled = &lsm_enabled_true;
124 else
125 lsm->enabled = &lsm_enabled_false;
126 } else if (lsm->enabled == &lsm_enabled_true) {
127 if (!enabled)
128 lsm->enabled = &lsm_enabled_false;
129 } else if (lsm->enabled == &lsm_enabled_false) {
130 if (enabled)
131 lsm->enabled = &lsm_enabled_true;
132 } else {
133 *lsm->enabled = enabled;
134 }
135}
136
2d4d5119
KC
137/* Is an LSM already listed in the ordered LSMs list? */
138static bool __init exists_ordered_lsm(struct lsm_info *lsm)
139{
140 struct lsm_info **check;
141
142 for (check = ordered_lsms; *check; check++)
143 if (*check == lsm)
144 return true;
145
146 return false;
147}
148
149/* Append an LSM to the list of ordered LSMs to initialize. */
150static int last_lsm __initdata;
151static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
152{
153 /* Ignore duplicate selections. */
154 if (exists_ordered_lsm(lsm))
155 return;
156
157 if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
158 return;
159
a8027fb0
KC
160 /* Enable this LSM, if it is not already set. */
161 if (!lsm->enabled)
162 lsm->enabled = &lsm_enabled_true;
2d4d5119 163 ordered_lsms[last_lsm++] = lsm;
a8027fb0 164
86ef3c73
KC
165 init_debug("%s ordered: %s (%s)\n", from, lsm->name,
166 is_enabled(lsm) ? "enabled" : "disabled");
2d4d5119
KC
167}
168
f4941d75
KC
169/* Is an LSM allowed to be initialized? */
170static bool __init lsm_allowed(struct lsm_info *lsm)
171{
172 /* Skip if the LSM is disabled. */
173 if (!is_enabled(lsm))
174 return false;
175
14bd99c8
KC
176 /* Not allowed if another exclusive LSM already initialized. */
177 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
178 init_debug("exclusive disabled: %s\n", lsm->name);
179 return false;
180 }
181
f4941d75
KC
182 return true;
183}
184
bbd3662a
CS
185static void __init lsm_set_blob_size(int *need, int *lbs)
186{
187 int offset;
188
b9f5ce27
GN
189 if (*need <= 0)
190 return;
191
192 offset = ALIGN(*lbs, sizeof(void *));
193 *lbs = offset + *need;
194 *need = offset;
bbd3662a
CS
195}
196
197static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
198{
199 if (!needed)
200 return;
201
202 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
33bf60ca 203 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
afb1cbe3
CS
204 /*
205 * The inode blob gets an rcu_head in addition to
206 * what the modules might need.
207 */
208 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
209 blob_sizes.lbs_inode = sizeof(struct rcu_head);
210 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
ecd5f82e
CS
211 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
212 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
1aea7808 213 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
f4ad8f2c 214 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
bbd3662a
CS
215}
216
d8e9bbd4
KC
217/* Prepare LSM for initialization. */
218static void __init prepare_lsm(struct lsm_info *lsm)
f4941d75
KC
219{
220 int enabled = lsm_allowed(lsm);
221
222 /* Record enablement (to handle any following exclusive LSMs). */
223 set_enabled(lsm, enabled);
224
d8e9bbd4 225 /* If enabled, do pre-initialization work. */
f4941d75 226 if (enabled) {
14bd99c8
KC
227 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
228 exclusive = lsm;
86ef3c73 229 init_debug("exclusive chosen: %s\n", lsm->name);
14bd99c8 230 }
bbd3662a
CS
231
232 lsm_set_blob_sizes(lsm->blobs);
d8e9bbd4
KC
233 }
234}
235
236/* Initialize a given LSM, if it is enabled. */
237static void __init initialize_lsm(struct lsm_info *lsm)
238{
239 if (is_enabled(lsm)) {
240 int ret;
14bd99c8 241
f4941d75
KC
242 init_debug("initializing %s\n", lsm->name);
243 ret = lsm->init();
244 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
245 }
246}
247
13e735c0 248/* Populate ordered LSMs list from comma-separated LSM name list. */
2d4d5119 249static void __init ordered_lsm_parse(const char *order, const char *origin)
657d910b
KC
250{
251 struct lsm_info *lsm;
13e735c0
KC
252 char *sep, *name, *next;
253
e2bc445b
KC
254 /* LSM_ORDER_FIRST is always first. */
255 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
256 if (lsm->order == LSM_ORDER_FIRST)
86ef3c73 257 append_ordered_lsm(lsm, " first");
e2bc445b
KC
258 }
259
7e611486 260 /* Process "security=", if given. */
7e611486
KC
261 if (chosen_major_lsm) {
262 struct lsm_info *major;
263
264 /*
265 * To match the original "security=" behavior, this
266 * explicitly does NOT fallback to another Legacy Major
267 * if the selected one was separately disabled: disable
268 * all non-matching Legacy Major LSMs.
269 */
270 for (major = __start_lsm_info; major < __end_lsm_info;
271 major++) {
272 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
273 strcmp(major->name, chosen_major_lsm) != 0) {
274 set_enabled(major, false);
86ef3c73 275 init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
7e611486
KC
276 chosen_major_lsm, major->name);
277 }
278 }
279 }
5ef4e419 280
13e735c0
KC
281 sep = kstrdup(order, GFP_KERNEL);
282 next = sep;
283 /* Walk the list, looking for matching LSMs. */
284 while ((name = strsep(&next, ",")) != NULL) {
285 bool found = false;
286
287 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
e2bc445b
KC
288 if (lsm->order == LSM_ORDER_MUTABLE &&
289 strcmp(lsm->name, name) == 0) {
13e735c0
KC
290 append_ordered_lsm(lsm, origin);
291 found = true;
292 }
293 }
294
295 if (!found)
86ef3c73
KC
296 init_debug("%s ignored: %s (not built into kernel)\n",
297 origin, name);
657d910b 298 }
c91d8106
CS
299
300 /* Process "security=", if given. */
301 if (chosen_major_lsm) {
302 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
303 if (exists_ordered_lsm(lsm))
304 continue;
305 if (strcmp(lsm->name, chosen_major_lsm) == 0)
306 append_ordered_lsm(lsm, "security=");
307 }
308 }
309
310 /* Disable all LSMs not in the ordered list. */
311 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
312 if (exists_ordered_lsm(lsm))
313 continue;
314 set_enabled(lsm, false);
86ef3c73
KC
315 init_debug("%s skipped: %s (not in requested order)\n",
316 origin, lsm->name);
c91d8106
CS
317 }
318
13e735c0 319 kfree(sep);
657d910b
KC
320}
321
1cfb2a51
TH
322static void __init lsm_early_cred(struct cred *cred);
323static void __init lsm_early_task(struct task_struct *task);
324
e6b1db98
MG
325static int lsm_append(const char *new, char **result);
326
86ef3c73
KC
327static void __init report_lsm_order(void)
328{
329 struct lsm_info **lsm, *early;
330 int first = 0;
331
332 pr_info("initializing lsm=");
333
334 /* Report each enabled LSM name, comma separated. */
335 for (early = __start_early_lsm_info; early < __end_early_lsm_info; early++)
336 if (is_enabled(early))
337 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
338 for (lsm = ordered_lsms; *lsm; lsm++)
339 if (is_enabled(*lsm))
340 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
341
342 pr_cont("\n");
343}
344
2d4d5119
KC
345static void __init ordered_lsm_init(void)
346{
347 struct lsm_info **lsm;
348
349 ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
350 GFP_KERNEL);
351
89a9684e
KC
352 if (chosen_lsm_order) {
353 if (chosen_major_lsm) {
86ef3c73
KC
354 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
355 chosen_major_lsm, chosen_lsm_order);
89a9684e
KC
356 chosen_major_lsm = NULL;
357 }
79f7865d 358 ordered_lsm_parse(chosen_lsm_order, "cmdline");
89a9684e 359 } else
79f7865d 360 ordered_lsm_parse(builtin_lsm_order, "builtin");
2d4d5119
KC
361
362 for (lsm = ordered_lsms; *lsm; lsm++)
d8e9bbd4
KC
363 prepare_lsm(*lsm);
364
86ef3c73
KC
365 report_lsm_order();
366
1aea7808
CS
367 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
368 init_debug("file blob size = %d\n", blob_sizes.lbs_file);
369 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
370 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
371 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
372 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
373 init_debug("task blob size = %d\n", blob_sizes.lbs_task);
33bf60ca
CS
374
375 /*
376 * Create any kmem_caches needed for blobs
377 */
378 if (blob_sizes.lbs_file)
379 lsm_file_cache = kmem_cache_create("lsm_file_cache",
380 blob_sizes.lbs_file, 0,
381 SLAB_PANIC, NULL);
afb1cbe3
CS
382 if (blob_sizes.lbs_inode)
383 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
384 blob_sizes.lbs_inode, 0,
385 SLAB_PANIC, NULL);
bbd3662a 386
1cfb2a51
TH
387 lsm_early_cred((struct cred *) current->cred);
388 lsm_early_task(current);
d8e9bbd4
KC
389 for (lsm = ordered_lsms; *lsm; lsm++)
390 initialize_lsm(*lsm);
2d4d5119
KC
391
392 kfree(ordered_lsms);
393}
394
e6b1db98
MG
395int __init early_security_init(void)
396{
e6b1db98
MG
397 struct lsm_info *lsm;
398
75c1182e
BW
399#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
400 INIT_HLIST_HEAD(&security_hook_heads.NAME);
401#include "linux/lsm_hook_defs.h"
402#undef LSM_HOOK
e6b1db98
MG
403
404 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
405 if (!lsm->enabled)
406 lsm->enabled = &lsm_enabled_true;
407 prepare_lsm(lsm);
408 initialize_lsm(lsm);
409 }
410
411 return 0;
412}
413
1da177e4
LT
414/**
415 * security_init - initializes the security framework
416 *
417 * This should be called early in the kernel initialization sequence.
418 */
419int __init security_init(void)
420{
e6b1db98 421 struct lsm_info *lsm;
3dfc9b02 422
86ef3c73
KC
423 init_debug("legacy security=%s\n", chosen_major_lsm ?: " *unspecified*");
424 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
425 init_debug("boot arg lsm=%s\n", chosen_lsm_order ?: " *unspecified*");
98d29170 426
e6b1db98
MG
427 /*
428 * Append the names of the early LSM modules now that kmalloc() is
429 * available
430 */
431 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
86ef3c73
KC
432 init_debug(" early started: %s (%s)\n", lsm->name,
433 is_enabled(lsm) ? "enabled" : "disabled");
e6b1db98
MG
434 if (lsm->enabled)
435 lsm_append(lsm->name, &lsm_names);
436 }
1da177e4 437
657d910b
KC
438 /* Load LSMs in specified order. */
439 ordered_lsm_init();
440
1da177e4
LT
441 return 0;
442}
443
076c54c5 444/* Save user chosen LSM */
5ef4e419 445static int __init choose_major_lsm(char *str)
076c54c5 446{
5ef4e419 447 chosen_major_lsm = str;
076c54c5
AD
448 return 1;
449}
5ef4e419 450__setup("security=", choose_major_lsm);
076c54c5 451
79f7865d
KC
452/* Explicitly choose LSM initialization order. */
453static int __init choose_lsm_order(char *str)
454{
455 chosen_lsm_order = str;
456 return 1;
457}
458__setup("lsm=", choose_lsm_order);
459
9b8c7c14
KC
460/* Enable LSM order debugging. */
461static int __init enable_debug(char *str)
462{
463 debug = true;
464 return 1;
465}
466__setup("lsm.debug", enable_debug);
467
3bb857e4
MS
468static bool match_last_lsm(const char *list, const char *lsm)
469{
470 const char *last;
471
472 if (WARN_ON(!list || !lsm))
473 return false;
474 last = strrchr(list, ',');
475 if (last)
476 /* Pass the comma, strcmp() will check for '\0' */
477 last++;
478 else
479 last = list;
480 return !strcmp(last, lsm);
481}
482
e6b1db98 483static int lsm_append(const char *new, char **result)
d69dece5
CS
484{
485 char *cp;
486
487 if (*result == NULL) {
488 *result = kstrdup(new, GFP_KERNEL);
87ea5843
EB
489 if (*result == NULL)
490 return -ENOMEM;
d69dece5 491 } else {
3bb857e4
MS
492 /* Check if it is the last registered name */
493 if (match_last_lsm(*result, new))
494 return 0;
d69dece5
CS
495 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
496 if (cp == NULL)
497 return -ENOMEM;
498 kfree(*result);
499 *result = cp;
500 }
501 return 0;
502}
503
d69dece5
CS
504/**
505 * security_add_hooks - Add a modules hooks to the hook lists.
506 * @hooks: the hooks to add
507 * @count: the number of hooks to add
508 * @lsm: the name of the security module
509 *
510 * Each LSM has to register its hooks with the infrastructure.
511 */
512void __init security_add_hooks(struct security_hook_list *hooks, int count,
1af0e4a0 513 const char *lsm)
d69dece5
CS
514{
515 int i;
516
517 for (i = 0; i < count; i++) {
518 hooks[i].lsm = lsm;
df0ce173 519 hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
d69dece5 520 }
e6b1db98
MG
521
522 /*
523 * Don't try to append during early_security_init(), we'll come back
524 * and fix this up afterwards.
525 */
526 if (slab_is_available()) {
527 if (lsm_append(lsm, &lsm_names) < 0)
528 panic("%s - Cannot get early memory.\n", __func__);
529 }
d69dece5
CS
530}
531
42df744c 532int call_blocking_lsm_notifier(enum lsm_event event, void *data)
8f408ab6 533{
42df744c
JK
534 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
535 event, data);
8f408ab6 536}
42df744c 537EXPORT_SYMBOL(call_blocking_lsm_notifier);
8f408ab6 538
42df744c 539int register_blocking_lsm_notifier(struct notifier_block *nb)
8f408ab6 540{
42df744c
JK
541 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
542 nb);
8f408ab6 543}
42df744c 544EXPORT_SYMBOL(register_blocking_lsm_notifier);
8f408ab6 545
42df744c 546int unregister_blocking_lsm_notifier(struct notifier_block *nb)
8f408ab6 547{
42df744c
JK
548 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
549 nb);
8f408ab6 550}
42df744c 551EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
8f408ab6 552
bbd3662a
CS
553/**
554 * lsm_cred_alloc - allocate a composite cred blob
555 * @cred: the cred that needs a blob
556 * @gfp: allocation type
557 *
558 * Allocate the cred blob for all the modules
559 *
560 * Returns 0, or -ENOMEM if memory can't be allocated.
561 */
562static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
563{
564 if (blob_sizes.lbs_cred == 0) {
565 cred->security = NULL;
566 return 0;
567 }
568
569 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
570 if (cred->security == NULL)
571 return -ENOMEM;
572 return 0;
573}
574
575/**
576 * lsm_early_cred - during initialization allocate a composite cred blob
577 * @cred: the cred that needs a blob
578 *
1cfb2a51 579 * Allocate the cred blob for all the modules
bbd3662a 580 */
1cfb2a51 581static void __init lsm_early_cred(struct cred *cred)
bbd3662a 582{
1cfb2a51 583 int rc = lsm_cred_alloc(cred, GFP_KERNEL);
bbd3662a 584
bbd3662a
CS
585 if (rc)
586 panic("%s: Early cred alloc failed.\n", __func__);
587}
588
33bf60ca
CS
589/**
590 * lsm_file_alloc - allocate a composite file blob
591 * @file: the file that needs a blob
592 *
593 * Allocate the file blob for all the modules
594 *
595 * Returns 0, or -ENOMEM if memory can't be allocated.
596 */
597static int lsm_file_alloc(struct file *file)
598{
599 if (!lsm_file_cache) {
600 file->f_security = NULL;
601 return 0;
602 }
603
604 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
605 if (file->f_security == NULL)
606 return -ENOMEM;
607 return 0;
608}
609
afb1cbe3
CS
610/**
611 * lsm_inode_alloc - allocate a composite inode blob
612 * @inode: the inode that needs a blob
613 *
614 * Allocate the inode blob for all the modules
615 *
616 * Returns 0, or -ENOMEM if memory can't be allocated.
617 */
618int lsm_inode_alloc(struct inode *inode)
619{
620 if (!lsm_inode_cache) {
621 inode->i_security = NULL;
622 return 0;
623 }
624
625 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
626 if (inode->i_security == NULL)
627 return -ENOMEM;
628 return 0;
629}
630
f4ad8f2c
CS
631/**
632 * lsm_task_alloc - allocate a composite task blob
633 * @task: the task that needs a blob
634 *
635 * Allocate the task blob for all the modules
636 *
637 * Returns 0, or -ENOMEM if memory can't be allocated.
638 */
3e8c7367 639static int lsm_task_alloc(struct task_struct *task)
f4ad8f2c
CS
640{
641 if (blob_sizes.lbs_task == 0) {
642 task->security = NULL;
643 return 0;
644 }
645
646 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
647 if (task->security == NULL)
648 return -ENOMEM;
649 return 0;
650}
651
ecd5f82e
CS
652/**
653 * lsm_ipc_alloc - allocate a composite ipc blob
654 * @kip: the ipc that needs a blob
655 *
656 * Allocate the ipc blob for all the modules
657 *
658 * Returns 0, or -ENOMEM if memory can't be allocated.
659 */
3e8c7367 660static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
ecd5f82e
CS
661{
662 if (blob_sizes.lbs_ipc == 0) {
663 kip->security = NULL;
664 return 0;
665 }
666
667 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
668 if (kip->security == NULL)
669 return -ENOMEM;
670 return 0;
671}
672
673/**
674 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
675 * @mp: the msg_msg that needs a blob
676 *
677 * Allocate the ipc blob for all the modules
678 *
679 * Returns 0, or -ENOMEM if memory can't be allocated.
680 */
3e8c7367 681static int lsm_msg_msg_alloc(struct msg_msg *mp)
ecd5f82e
CS
682{
683 if (blob_sizes.lbs_msg_msg == 0) {
684 mp->security = NULL;
685 return 0;
686 }
687
688 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
689 if (mp->security == NULL)
690 return -ENOMEM;
691 return 0;
692}
693
f4ad8f2c
CS
694/**
695 * lsm_early_task - during initialization allocate a composite task blob
696 * @task: the task that needs a blob
697 *
1cfb2a51 698 * Allocate the task blob for all the modules
f4ad8f2c 699 */
1cfb2a51 700static void __init lsm_early_task(struct task_struct *task)
f4ad8f2c 701{
1cfb2a51 702 int rc = lsm_task_alloc(task);
f4ad8f2c 703
f4ad8f2c
CS
704 if (rc)
705 panic("%s: Early task alloc failed.\n", __func__);
706}
707
1aea7808
CS
708/**
709 * lsm_superblock_alloc - allocate a composite superblock blob
710 * @sb: the superblock that needs a blob
711 *
712 * Allocate the superblock blob for all the modules
713 *
714 * Returns 0, or -ENOMEM if memory can't be allocated.
715 */
716static int lsm_superblock_alloc(struct super_block *sb)
717{
718 if (blob_sizes.lbs_superblock == 0) {
719 sb->s_security = NULL;
720 return 0;
721 }
722
723 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
724 if (sb->s_security == NULL)
725 return -ENOMEM;
726 return 0;
727}
728
98e828a0
KS
729/*
730 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
731 * can be accessed with:
732 *
733 * LSM_RET_DEFAULT(<hook_name>)
734 *
735 * The macros below define static constants for the default value of each
736 * LSM hook.
737 */
738#define LSM_RET_DEFAULT(NAME) (NAME##_default)
739#define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
740#define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
86dd9fd5 741 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
98e828a0
KS
742#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
743 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
744
745#include <linux/lsm_hook_defs.h>
746#undef LSM_HOOK
747
f25fce3e 748/*
b1d9e6b0 749 * Hook list operation macros.
1da177e4 750 *
f25fce3e
CS
751 * call_void_hook:
752 * This is a hook that does not return a value.
1da177e4 753 *
f25fce3e
CS
754 * call_int_hook:
755 * This is a hook that returns a value.
1da177e4 756 */
1da177e4 757
b1d9e6b0
CS
758#define call_void_hook(FUNC, ...) \
759 do { \
760 struct security_hook_list *P; \
761 \
df0ce173 762 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
b1d9e6b0
CS
763 P->hook.FUNC(__VA_ARGS__); \
764 } while (0)
765
766#define call_int_hook(FUNC, IRC, ...) ({ \
767 int RC = IRC; \
768 do { \
769 struct security_hook_list *P; \
770 \
df0ce173 771 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
b1d9e6b0
CS
772 RC = P->hook.FUNC(__VA_ARGS__); \
773 if (RC != 0) \
774 break; \
775 } \
776 } while (0); \
777 RC; \
778})
1da177e4 779
20510f2f
JM
780/* Security operations */
781
52f88693 782int security_binder_set_context_mgr(const struct cred *mgr)
79af7307 783{
f25fce3e 784 return call_int_hook(binder_set_context_mgr, 0, mgr);
79af7307
SS
785}
786
52f88693
TK
787int security_binder_transaction(const struct cred *from,
788 const struct cred *to)
79af7307 789{
f25fce3e 790 return call_int_hook(binder_transaction, 0, from, to);
79af7307
SS
791}
792
52f88693
TK
793int security_binder_transfer_binder(const struct cred *from,
794 const struct cred *to)
79af7307 795{
f25fce3e 796 return call_int_hook(binder_transfer_binder, 0, from, to);
79af7307
SS
797}
798
52f88693
TK
799int security_binder_transfer_file(const struct cred *from,
800 const struct cred *to, struct file *file)
79af7307 801{
f25fce3e 802 return call_int_hook(binder_transfer_file, 0, from, to, file);
79af7307
SS
803}
804
9e48858f 805int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
20510f2f 806{
f25fce3e 807 return call_int_hook(ptrace_access_check, 0, child, mode);
5cd9c58f
DH
808}
809
810int security_ptrace_traceme(struct task_struct *parent)
811{
f25fce3e 812 return call_int_hook(ptrace_traceme, 0, parent);
20510f2f
JM
813}
814
815int security_capget(struct task_struct *target,
816 kernel_cap_t *effective,
817 kernel_cap_t *inheritable,
818 kernel_cap_t *permitted)
819{
f25fce3e
CS
820 return call_int_hook(capget, 0, target,
821 effective, inheritable, permitted);
20510f2f
JM
822}
823
d84f4f99
DH
824int security_capset(struct cred *new, const struct cred *old,
825 const kernel_cap_t *effective,
826 const kernel_cap_t *inheritable,
827 const kernel_cap_t *permitted)
20510f2f 828{
f25fce3e
CS
829 return call_int_hook(capset, 0, new, old,
830 effective, inheritable, permitted);
20510f2f
JM
831}
832
c1a85a00
MM
833int security_capable(const struct cred *cred,
834 struct user_namespace *ns,
835 int cap,
836 unsigned int opts)
20510f2f 837{
c1a85a00 838 return call_int_hook(capable, 0, cred, ns, cap, opts);
20510f2f
JM
839}
840
20510f2f
JM
841int security_quotactl(int cmds, int type, int id, struct super_block *sb)
842{
f25fce3e 843 return call_int_hook(quotactl, 0, cmds, type, id, sb);
20510f2f
JM
844}
845
846int security_quota_on(struct dentry *dentry)
847{
f25fce3e 848 return call_int_hook(quota_on, 0, dentry);
20510f2f
JM
849}
850
12b3052c 851int security_syslog(int type)
20510f2f 852{
f25fce3e 853 return call_int_hook(syslog, 0, type);
20510f2f
JM
854}
855
457db29b 856int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
20510f2f 857{
f25fce3e 858 return call_int_hook(settime, 0, ts, tz);
20510f2f
JM
859}
860
20510f2f
JM
861int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
862{
b1d9e6b0
CS
863 struct security_hook_list *hp;
864 int cap_sys_admin = 1;
865 int rc;
866
867 /*
868 * The module will respond with a positive value if
869 * it thinks the __vm_enough_memory() call should be
870 * made with the cap_sys_admin set. If all of the modules
871 * agree that it should be set it will. If any module
872 * thinks it should not be set it won't.
873 */
df0ce173 874 hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
b1d9e6b0
CS
875 rc = hp->hook.vm_enough_memory(mm, pages);
876 if (rc <= 0) {
877 cap_sys_admin = 0;
878 break;
879 }
880 }
881 return __vm_enough_memory(mm, pages, cap_sys_admin);
20510f2f
JM
882}
883
1661372c
PM
884/**
885 * security_bprm_creds_for_exec() - Prepare the credentials for exec()
886 * @bprm: binary program information
887 *
888 * If the setup in prepare_exec_creds did not setup @bprm->cred->security
889 * properly for executing @bprm->file, update the LSM's portion of
890 * @bprm->cred->security to be what commit_creds needs to install for the new
891 * program. This hook may also optionally check permissions (e.g. for
892 * transitions between security domains). The hook must set @bprm->secureexec
893 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
894 * contains the linux_binprm structure.
895 *
896 * Return: Returns 0 if the hook is successful and permission is granted.
897 */
b8bff599 898int security_bprm_creds_for_exec(struct linux_binprm *bprm)
20510f2f 899{
b8bff599
EB
900 return call_int_hook(bprm_creds_for_exec, 0, bprm);
901}
902
1661372c
PM
903/**
904 * security_bprm_creds_from_file() - Update linux_binprm creds based on file
905 * @bprm: binary program information
906 * @file: associated file
907 *
908 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
909 * exec, update @bprm->cred to reflect that change. This is called after
910 * finding the binary that will be executed without an interpreter. This
911 * ensures that the credentials will not be derived from a script that the
912 * binary will need to reopen, which when reopend may end up being a completely
913 * different file. This hook may also optionally check permissions (e.g. for
914 * transitions between security domains). The hook must set @bprm->secureexec
915 * to 1 if AT_SECURE should be set to request libc enable secure mode. The
916 * hook must add to @bprm->per_clear any personality flags that should be
917 * cleared from current->personality. @bprm contains the linux_binprm
918 * structure.
919 *
920 * Return: Returns 0 if the hook is successful and permission is granted.
921 */
56305aa9 922int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
20510f2f 923{
56305aa9 924 return call_int_hook(bprm_creds_from_file, 0, bprm, file);
20510f2f
JM
925}
926
1661372c
PM
927/**
928 * security_bprm_check() - Mediate binary handler search
929 * @bprm: binary program information
930 *
931 * This hook mediates the point when a search for a binary handler will begin.
932 * It allows a check against the @bprm->cred->security value which was set in
933 * the preceding creds_for_exec call. The argv list and envp list are reliably
934 * available in @bprm. This hook may be called multiple times during a single
935 * execve. @bprm contains the linux_binprm structure.
936 *
937 * Return: Returns 0 if the hook is successful and permission is granted.
938 */
a6f76f23 939int security_bprm_check(struct linux_binprm *bprm)
20510f2f 940{
6c21a7fb
MZ
941 int ret;
942
f25fce3e 943 ret = call_int_hook(bprm_check_security, 0, bprm);
6c21a7fb
MZ
944 if (ret)
945 return ret;
946 return ima_bprm_check(bprm);
20510f2f
JM
947}
948
1661372c
PM
949/**
950 * security_bprm_committing_creds() - Install creds for a process during exec()
951 * @bprm: binary program information
952 *
953 * Prepare to install the new security attributes of a process being
954 * transformed by an execve operation, based on the old credentials pointed to
955 * by @current->cred and the information set in @bprm->cred by the
956 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
957 * hook is a good place to perform state changes on the process such as closing
958 * open file descriptors to which access will no longer be granted when the
959 * attributes are changed. This is called immediately before commit_creds().
960 */
a6f76f23 961void security_bprm_committing_creds(struct linux_binprm *bprm)
20510f2f 962{
f25fce3e 963 call_void_hook(bprm_committing_creds, bprm);
20510f2f
JM
964}
965
1661372c
PM
966/**
967 * security_bprm_committed_creds() - Tidy up after cred install during exec()
968 * @bprm: binary program information
969 *
970 * Tidy up after the installation of the new security attributes of a process
971 * being transformed by an execve operation. The new credentials have, by this
972 * point, been set to @current->cred. @bprm points to the linux_binprm
973 * structure. This hook is a good place to perform state changes on the
974 * process such as clearing out non-inheritable signal state. This is called
975 * immediately after commit_creds().
976 */
a6f76f23 977void security_bprm_committed_creds(struct linux_binprm *bprm)
20510f2f 978{
f25fce3e 979 call_void_hook(bprm_committed_creds, bprm);
20510f2f
JM
980}
981
36819f18
PM
982/**
983 * security_fs_context_dup() - Duplicate a fs_context LSM blob
984 * @fc: destination filesystem context
985 * @src_fc: source filesystem context
986 *
987 * Allocate and attach a security structure to sc->security. This pointer is
988 * initialised to NULL by the caller. @fc indicates the new filesystem context.
989 * @src_fc indicates the original filesystem context.
990 *
991 * Return: Returns 0 on success or a negative error code on failure.
992 */
0b52075e
AV
993int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
994{
995 return call_int_hook(fs_context_dup, 0, fc, src_fc);
996}
997
36819f18
PM
998/**
999 * security_fs_context_parse_param() - Configure a filesystem context
1000 * @fc: filesystem context
1001 * @param: filesystem parameter
1002 *
1003 * Userspace provided a parameter to configure a superblock. The LSM can
1004 * consume the parameter or return it to the caller for use elsewhere.
1005 *
1006 * Return: If the parameter is used by the LSM it should return 0, if it is
1007 * returned to the caller -ENOPARAM is returned, otherwise a negative
1008 * error code is returned.
1009 */
ecff3057
CS
1010int security_fs_context_parse_param(struct fs_context *fc,
1011 struct fs_parameter *param)
da2441fd 1012{
ecff3057
CS
1013 struct security_hook_list *hp;
1014 int trc;
1015 int rc = -ENOPARAM;
1016
1017 hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1018 list) {
1019 trc = hp->hook.fs_context_parse_param(fc, param);
1020 if (trc == 0)
1021 rc = 0;
1022 else if (trc != -ENOPARAM)
1023 return trc;
1024 }
1025 return rc;
da2441fd
DH
1026}
1027
08526a90
PM
1028/**
1029 * security_sb_alloc() - Allocate a super_block LSM blob
1030 * @sb: filesystem superblock
1031 *
1032 * Allocate and attach a security structure to the sb->s_security field. The
1033 * s_security field is initialized to NULL when the structure is allocated.
1034 * @sb contains the super_block structure to be modified.
1035 *
1036 * Return: Returns 0 if operation was successful.
1037 */
20510f2f
JM
1038int security_sb_alloc(struct super_block *sb)
1039{
1aea7808
CS
1040 int rc = lsm_superblock_alloc(sb);
1041
1042 if (unlikely(rc))
1043 return rc;
1044 rc = call_int_hook(sb_alloc_security, 0, sb);
1045 if (unlikely(rc))
1046 security_sb_free(sb);
1047 return rc;
20510f2f
JM
1048}
1049
08526a90
PM
1050/**
1051 * security_sb_delete() - Release super_block LSM associated objects
1052 * @sb: filesystem superblock
1053 *
1054 * Release objects tied to a superblock (e.g. inodes). @sb contains the
1055 * super_block structure being released.
1056 */
83e804f0
MS
1057void security_sb_delete(struct super_block *sb)
1058{
1059 call_void_hook(sb_delete, sb);
20510f2f
JM
1060}
1061
08526a90
PM
1062/**
1063 * security_sb_free() - Free a super_block LSM blob
1064 * @sb: filesystem superblock
1065 *
1066 * Deallocate and clear the sb->s_security field. @sb contains the super_block
1067 * structure to be modified.
1068 */
20510f2f
JM
1069void security_sb_free(struct super_block *sb)
1070{
f25fce3e 1071 call_void_hook(sb_free_security, sb);
1aea7808
CS
1072 kfree(sb->s_security);
1073 sb->s_security = NULL;
20510f2f
JM
1074}
1075
08526a90
PM
1076/**
1077 * security_free_mnt_opts() - Free memory associated with mount options
1078 * @mnt_ops: LSM processed mount options
1079 *
1080 * Free memory associated with @mnt_ops.
1081 */
204cc0cc 1082void security_free_mnt_opts(void **mnt_opts)
20510f2f 1083{
204cc0cc
AV
1084 if (!*mnt_opts)
1085 return;
1086 call_void_hook(sb_free_mnt_opts, *mnt_opts);
1087 *mnt_opts = NULL;
20510f2f 1088}
204cc0cc 1089EXPORT_SYMBOL(security_free_mnt_opts);
20510f2f 1090
08526a90
PM
1091/**
1092 * security_sb_eat_lsm_opts() - Consume LSM mount options
1093 * @options: mount options
1094 * @mnt_ops: LSM processed mount options
1095 *
1096 * Eat (scan @options) and save them in @mnt_opts.
1097 *
1098 * Return: Returns 0 on success, negative values on failure.
1099 */
204cc0cc 1100int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
ff36fe2c 1101{
204cc0cc 1102 return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
ff36fe2c 1103}
f5c0c26d 1104EXPORT_SYMBOL(security_sb_eat_lsm_opts);
ff36fe2c 1105
08526a90
PM
1106/**
1107 * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1108 * @sb: filesystem superblock
1109 * @mnt_opts: new mount options
1110 *
1111 * Determine if the new mount options in @mnt_opts are allowed given the
1112 * existing mounted filesystem at @sb. @sb superblock being compared.
1113 *
1114 * Return: Returns 0 if options are compatible.
1115 */
69c4a42d
OK
1116int security_sb_mnt_opts_compat(struct super_block *sb,
1117 void *mnt_opts)
1118{
1119 return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
1120}
1121EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1122
08526a90
PM
1123/**
1124 * security_sb_remount() - Verify no incompatible mount changes during remount
1125 * @sb: filesystem superblock
1126 * @mnt_opts: (re)mount options
1127 *
1128 * Extracts security system specific mount options and verifies no changes are
1129 * being made to those options.
1130 *
1131 * Return: Returns 0 if permission is granted.
1132 */
c039bc3c 1133int security_sb_remount(struct super_block *sb,
204cc0cc 1134 void *mnt_opts)
20510f2f 1135{
204cc0cc 1136 return call_int_hook(sb_remount, 0, sb, mnt_opts);
ff36fe2c 1137}
a65001e8 1138EXPORT_SYMBOL(security_sb_remount);
ff36fe2c 1139
08526a90
PM
1140/**
1141 * security_sb_kern_mount() - Check if a kernel mount is allowed
1142 * @sb: filesystem superblock
1143 *
1144 * Mount this @sb if allowed by permissions.
1145 *
1146 * Return: Returns 0 if permission is granted.
1147 */
a10d7c22 1148int security_sb_kern_mount(struct super_block *sb)
20510f2f 1149{
a10d7c22 1150 return call_int_hook(sb_kern_mount, 0, sb);
20510f2f
JM
1151}
1152
08526a90
PM
1153/**
1154 * security_sb_show_options() - Output the mount options for a superblock
1155 * @m: output file
1156 * @sb: filesystem superblock
1157 *
1158 * Show (print on @m) mount options for this @sb.
1159 *
1160 * Return: Returns 0 on success, negative values on failure.
1161 */
2069f457
EP
1162int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1163{
f25fce3e 1164 return call_int_hook(sb_show_options, 0, m, sb);
2069f457
EP
1165}
1166
08526a90
PM
1167/**
1168 * security_sb_statfs() - Check if accessing fs stats is allowed
1169 * @dentry: superblock handle
1170 *
1171 * Check permission before obtaining filesystem statistics for the @mnt
1172 * mountpoint. @dentry is a handle on the superblock for the filesystem.
1173 *
1174 * Return: Returns 0 if permission is granted.
1175 */
20510f2f
JM
1176int security_sb_statfs(struct dentry *dentry)
1177{
f25fce3e 1178 return call_int_hook(sb_statfs, 0, dentry);
20510f2f
JM
1179}
1180
08526a90
PM
1181/**
1182 * security_sb_mount() - Check permission for mounting a filesystem
1183 * @dev_name: filesystem backing device
1184 * @path: mount point
1185 * @type: filesystem type
1186 * @flags: mount flags
1187 * @data: filesystem specific data
1188 *
1189 * Check permission before an object specified by @dev_name is mounted on the
1190 * mount point named by @nd. For an ordinary mount, @dev_name identifies a
1191 * device if the file system type requires a device. For a remount
1192 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
1193 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
1194 * mounted.
1195 *
1196 * Return: Returns 0 if permission is granted.
1197 */
8a04c43b 1198int security_sb_mount(const char *dev_name, const struct path *path,
808d4e3c 1199 const char *type, unsigned long flags, void *data)
20510f2f 1200{
f25fce3e 1201 return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
20510f2f
JM
1202}
1203
08526a90
PM
1204/**
1205 * security_sb_umount() - Check permission for unmounting a filesystem
1206 * @mnt: mounted filesystem
1207 * @flags: unmount flags
1208 *
1209 * Check permission before the @mnt file system is unmounted.
1210 *
1211 * Return: Returns 0 if permission is granted.
1212 */
20510f2f
JM
1213int security_sb_umount(struct vfsmount *mnt, int flags)
1214{
f25fce3e 1215 return call_int_hook(sb_umount, 0, mnt, flags);
20510f2f
JM
1216}
1217
08526a90
PM
1218/**
1219 * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1220 * @old_path: new location for current rootfs
1221 * @new_path: location of the new rootfs
1222 *
1223 * Check permission before pivoting the root filesystem.
1224 *
1225 * Return: Returns 0 if permission is granted.
1226 */
3b73b68c 1227int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
20510f2f 1228{
f25fce3e 1229 return call_int_hook(sb_pivotroot, 0, old_path, new_path);
20510f2f
JM
1230}
1231
08526a90
PM
1232/**
1233 * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1234 * @sb: filesystem superblock
1235 * @mnt_opts: binary mount options
1236 * @kern_flags: kernel flags (in)
1237 * @set_kern_flags: kernel flags (out)
1238 *
1239 * Set the security relevant mount options used for a superblock.
1240 *
1241 * Return: Returns 0 on success, error on failure.
1242 */
c9180a57 1243int security_sb_set_mnt_opts(struct super_block *sb,
204cc0cc 1244 void *mnt_opts,
649f6e77
DQ
1245 unsigned long kern_flags,
1246 unsigned long *set_kern_flags)
c9180a57 1247{
b1d9e6b0 1248 return call_int_hook(sb_set_mnt_opts,
204cc0cc
AV
1249 mnt_opts ? -EOPNOTSUPP : 0, sb,
1250 mnt_opts, kern_flags, set_kern_flags);
c9180a57 1251}
e0007529 1252EXPORT_SYMBOL(security_sb_set_mnt_opts);
c9180a57 1253
08526a90
PM
1254/**
1255 * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1256 * @olddb: source superblock
1257 * @newdb: destination superblock
1258 * @kern_flags: kernel flags (in)
1259 * @set_kern_flags: kernel flags (out)
1260 *
1261 * Copy all security options from a given superblock to another.
1262 *
1263 * Return: Returns 0 on success, error on failure.
1264 */
094f7b69 1265int security_sb_clone_mnt_opts(const struct super_block *oldsb,
0b4d3452
SM
1266 struct super_block *newsb,
1267 unsigned long kern_flags,
1268 unsigned long *set_kern_flags)
c9180a57 1269{
0b4d3452
SM
1270 return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
1271 kern_flags, set_kern_flags);
c9180a57 1272}
e0007529
EP
1273EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1274
08526a90
PM
1275/**
1276 * security_move_mount() - Check permissions for moving a mount
1277 * @from_path: source mount point
1278 * @to_path: destination mount point
1279 *
1280 * Check permission before a mount is moved.
1281 *
1282 * Return: Returns 0 if permission is granted.
1283 */
2db154b3
DH
1284int security_move_mount(const struct path *from_path, const struct path *to_path)
1285{
1286 return call_int_hook(move_mount, 0, from_path, to_path);
1287}
1288
916e3258
PM
1289/**
1290 * security_path_notify() - Check if setting a watch is allowed
1291 * @path: file path
1292 * @mask: event mask
1293 * @obj_type: file path type
1294 *
1295 * Check permissions before setting a watch on events as defined by @mask, on
1296 * an object at @path, whose type is defined by @obj_type.
1297 *
1298 * Return: Returns 0 if permission is granted.
1299 */
ac5656d8
AG
1300int security_path_notify(const struct path *path, u64 mask,
1301 unsigned int obj_type)
1302{
1303 return call_int_hook(path_notify, 0, path, mask, obj_type);
1304}
1305
916e3258
PM
1306/**
1307 * security_inode_alloc() - Allocate an inode LSM blob
1308 * @inode: the inode
1309 *
1310 * Allocate and attach a security structure to @inode->i_security. The
1311 * i_security field is initialized to NULL when the inode structure is
1312 * allocated.
1313 *
1314 * Return: Return 0 if operation was successful.
1315 */
20510f2f
JM
1316int security_inode_alloc(struct inode *inode)
1317{
afb1cbe3
CS
1318 int rc = lsm_inode_alloc(inode);
1319
1320 if (unlikely(rc))
1321 return rc;
1322 rc = call_int_hook(inode_alloc_security, 0, inode);
1323 if (unlikely(rc))
1324 security_inode_free(inode);
1325 return rc;
1326}
1327
1328static void inode_free_by_rcu(struct rcu_head *head)
1329{
1330 /*
1331 * The rcu head is at the start of the inode blob
1332 */
1333 kmem_cache_free(lsm_inode_cache, head);
20510f2f
JM
1334}
1335
916e3258
PM
1336/**
1337 * security_inode_free() - Free an inode's LSM blob
1338 * @inode: the inode
1339 *
1340 * Deallocate the inode security structure and set @inode->i_security to NULL.
1341 */
20510f2f
JM
1342void security_inode_free(struct inode *inode)
1343{
f381c272 1344 integrity_inode_free(inode);
f25fce3e 1345 call_void_hook(inode_free_security, inode);
afb1cbe3
CS
1346 /*
1347 * The inode may still be referenced in a path walk and
1348 * a call to security_inode_permission() can be made
1349 * after inode_free_security() is called. Ideally, the VFS
1350 * wouldn't do this, but fixing that is a much harder
1351 * job. For now, simply free the i_security via RCU, and
1352 * leave the current inode->i_security pointer intact.
1353 * The inode will be freed after the RCU grace period too.
1354 */
1355 if (inode->i_security)
1356 call_rcu((struct rcu_head *)inode->i_security,
1357 inode_free_by_rcu);
20510f2f
JM
1358}
1359
08526a90
PM
1360/**
1361 * security_dentry_init_security() - Perform dentry initialization
1362 * @dentry: the dentry to initialize
1363 * @mode: mode used to determine resource type
1364 * @name: name of the last path component
1365 * @xattr_name: name of the security/LSM xattr
1366 * @ctx: pointer to the resulting LSM context
1367 * @ctxlen: length of @ctx
1368 *
1369 * Compute a context for a dentry as the inode is not yet available since NFSv4
1370 * has no label backed by an EA anyway. It is important to note that
1371 * @xattr_name does not need to be free'd by the caller, it is a static string.
1372 *
1373 * Return: Returns 0 on success, negative values on failure.
1374 */
d47be3df 1375int security_dentry_init_security(struct dentry *dentry, int mode,
15bf3239
VG
1376 const struct qstr *name,
1377 const char **xattr_name, void **ctx,
1378 u32 *ctxlen)
d47be3df 1379{
7f5056b9
VG
1380 struct security_hook_list *hp;
1381 int rc;
1382
1383 /*
1384 * Only one module will provide a security context.
1385 */
1386 hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security, list) {
1387 rc = hp->hook.dentry_init_security(dentry, mode, name,
1388 xattr_name, ctx, ctxlen);
1389 if (rc != LSM_RET_DEFAULT(dentry_init_security))
1390 return rc;
1391 }
1392 return LSM_RET_DEFAULT(dentry_init_security);
d47be3df
DQ
1393}
1394EXPORT_SYMBOL(security_dentry_init_security);
1395
08526a90
PM
1396/**
1397 * security_dentry_create_files_as() - Perform dentry initialization
1398 * @dentry: the dentry to initialize
1399 * @mode: mode used to determine resource type
1400 * @name: name of the last path component
1401 * @old: creds to use for LSM context calculations
1402 * @new: creds to modify
1403 *
1404 * Compute a context for a dentry as the inode is not yet available and set
1405 * that context in passed in creds so that new files are created using that
1406 * context. Context is calculated using the passed in creds and not the creds
1407 * of the caller.
1408 *
1409 * Return: Returns 0 on success, error on failure.
1410 */
2602625b
VG
1411int security_dentry_create_files_as(struct dentry *dentry, int mode,
1412 struct qstr *name,
1413 const struct cred *old, struct cred *new)
1414{
1415 return call_int_hook(dentry_create_files_as, 0, dentry, mode,
1416 name, old, new);
1417}
1418EXPORT_SYMBOL(security_dentry_create_files_as);
1419
916e3258
PM
1420/**
1421 * security_inode_init_security() - Initialize an inode's LSM context
1422 * @inode: the inode
1423 * @dir: parent directory
1424 * @qstr: last component of the pathname
1425 * @initxattrs: callback function to write xattrs
1426 * @fs_data: filesystem specific data
1427 *
1428 * Obtain the security attribute name suffix and value to set on a newly
1429 * created inode and set up the incore security field for the new inode. This
1430 * hook is called by the fs code as part of the inode creation transaction and
1431 * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
1432 * hooks called by the VFS. The hook function is expected to allocate the name
1433 * and value via kmalloc, with the caller being responsible for calling kfree
1434 * after using them. If the security module does not use security attributes
1435 * or does not wish to put a security attribute on this particular inode, then
1436 * it should return -EOPNOTSUPP to skip this processing.
1437 *
1438 * Return: Returns 0 on success, -EOPNOTSUPP if no security attribute is
1439 * needed, or -ENOMEM on memory allocation failure.
1440 */
20510f2f 1441int security_inode_init_security(struct inode *inode, struct inode *dir,
9d8f13ba
MZ
1442 const struct qstr *qstr,
1443 const initxattrs initxattrs, void *fs_data)
20510f2f 1444{
823eb1cc
MZ
1445 struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
1446 struct xattr *lsm_xattr, *evm_xattr, *xattr;
9d8f13ba
MZ
1447 int ret;
1448
20510f2f 1449 if (unlikely(IS_PRIVATE(inode)))
fb88c2b6 1450 return 0;
9d8f13ba 1451
9d8f13ba 1452 if (!initxattrs)
e308fd3b
JB
1453 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
1454 dir, qstr, NULL, NULL, NULL);
9548906b 1455 memset(new_xattrs, 0, sizeof(new_xattrs));
9d8f13ba 1456 lsm_xattr = new_xattrs;
b1d9e6b0 1457 ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
9d8f13ba
MZ
1458 &lsm_xattr->name,
1459 &lsm_xattr->value,
1460 &lsm_xattr->value_len);
1461 if (ret)
1462 goto out;
823eb1cc
MZ
1463
1464 evm_xattr = lsm_xattr + 1;
1465 ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
1466 if (ret)
1467 goto out;
9d8f13ba
MZ
1468 ret = initxattrs(inode, new_xattrs, fs_data);
1469out:
9548906b 1470 for (xattr = new_xattrs; xattr->value != NULL; xattr++)
823eb1cc 1471 kfree(xattr->value);
9d8f13ba
MZ
1472 return (ret == -EOPNOTSUPP) ? 0 : ret;
1473}
1474EXPORT_SYMBOL(security_inode_init_security);
1475
916e3258
PM
1476/**
1477 * security_inode_init_security_anon() - Initialize an anonymous inode
1478 * @inode: the inode
1479 * @name: the anonymous inode class
1480 * @context_inode: an optional related inode
1481 *
1482 * Set up the incore security field for the new anonymous inode and return
1483 * whether the inode creation is permitted by the security module or not.
1484 *
1485 * Return: Returns 0 on success, -EACCES if the security module denies the
1486 * creation of this inode, or another -errno upon other errors.
1487 */
215b674b
LG
1488int security_inode_init_security_anon(struct inode *inode,
1489 const struct qstr *name,
1490 const struct inode *context_inode)
1491{
1492 return call_int_hook(inode_init_security_anon, 0, inode, name,
1493 context_inode);
1494}
1495
9d8f13ba 1496int security_old_inode_init_security(struct inode *inode, struct inode *dir,
9548906b 1497 const struct qstr *qstr, const char **name,
9d8f13ba 1498 void **value, size_t *len)
20510f2f
JM
1499{
1500 if (unlikely(IS_PRIVATE(inode)))
30e05324 1501 return -EOPNOTSUPP;
e308fd3b
JB
1502 return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
1503 qstr, name, value, len);
20510f2f 1504}
9d8f13ba 1505EXPORT_SYMBOL(security_old_inode_init_security);
20510f2f 1506
be6d3e56 1507#ifdef CONFIG_SECURITY_PATH
916e3258
PM
1508/**
1509 * security_path_mknod() - Check if creating a special file is allowed
1510 * @dir: parent directory
1511 * @dentry: new file
1512 * @mode: new file mode
1513 * @dev: device number
1514 *
1515 * Check permissions when creating a file. Note that this hook is called even
1516 * if mknod operation is being done for a regular file.
1517 *
1518 * Return: Returns 0 if permission is granted.
1519 */
d3607752 1520int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
be6d3e56
KT
1521 unsigned int dev)
1522{
c6f493d6 1523 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1524 return 0;
f25fce3e 1525 return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
be6d3e56
KT
1526}
1527EXPORT_SYMBOL(security_path_mknod);
1528
916e3258
PM
1529/**
1530 * security_path_mkdir() - Check if creating a new directory is allowed
1531 * @dir: parent directory
1532 * @dentry: new directory
1533 * @mode: new directory mode
1534 *
1535 * Check permissions to create a new directory in the existing directory.
1536 *
1537 * Return: Returns 0 if permission is granted.
1538 */
d3607752 1539int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
be6d3e56 1540{
c6f493d6 1541 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1542 return 0;
f25fce3e 1543 return call_int_hook(path_mkdir, 0, dir, dentry, mode);
be6d3e56 1544}
82140443 1545EXPORT_SYMBOL(security_path_mkdir);
be6d3e56 1546
916e3258
PM
1547/**
1548 * security_path_rmdir() - Check if removing a directory is allowed
1549 * @dir: parent directory
1550 * @dentry: directory to remove
1551 *
1552 * Check the permission to remove a directory.
1553 *
1554 * Return: Returns 0 if permission is granted.
1555 */
989f74e0 1556int security_path_rmdir(const struct path *dir, struct dentry *dentry)
be6d3e56 1557{
c6f493d6 1558 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1559 return 0;
f25fce3e 1560 return call_int_hook(path_rmdir, 0, dir, dentry);
be6d3e56
KT
1561}
1562
916e3258
PM
1563/**
1564 * security_path_unlink() - Check if removing a hard link is allowed
1565 * @dir: parent directory
1566 * @dentry: file
1567 *
1568 * Check the permission to remove a hard link to a file.
1569 *
1570 * Return: Returns 0 if permission is granted.
1571 */
989f74e0 1572int security_path_unlink(const struct path *dir, struct dentry *dentry)
be6d3e56 1573{
c6f493d6 1574 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1575 return 0;
f25fce3e 1576 return call_int_hook(path_unlink, 0, dir, dentry);
be6d3e56 1577}
82140443 1578EXPORT_SYMBOL(security_path_unlink);
be6d3e56 1579
916e3258
PM
1580/**
1581 * security_path_symlink() - Check if creating a symbolic link is allowed
1582 * @dir: parent directory
1583 * @dentry: symbolic link
1584 * @old_name: file pathname
1585 *
1586 * Check the permission to create a symbolic link to a file.
1587 *
1588 * Return: Returns 0 if permission is granted.
1589 */
d3607752 1590int security_path_symlink(const struct path *dir, struct dentry *dentry,
be6d3e56
KT
1591 const char *old_name)
1592{
c6f493d6 1593 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1594 return 0;
f25fce3e 1595 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
be6d3e56
KT
1596}
1597
916e3258
PM
1598/**
1599 * security_path_link - Check if creating a hard link is allowed
1600 * @old_dentry: existing file
1601 * @new_dir: new parent directory
1602 * @new_dentry: new link
1603 *
1604 * Check permission before creating a new hard link to a file.
1605 *
1606 * Return: Returns 0 if permission is granted.
1607 */
3ccee46a 1608int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
be6d3e56
KT
1609 struct dentry *new_dentry)
1610{
c6f493d6 1611 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
be6d3e56 1612 return 0;
f25fce3e 1613 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
be6d3e56
KT
1614}
1615
916e3258
PM
1616/**
1617 * security_path_rename() - Check if renaming a file is allowed
1618 * @old_dir: parent directory of the old file
1619 * @old_dentry: the old file
1620 * @new_dir: parent directory of the new file
1621 * @new_dentry: the new file
1622 * @flags: flags
1623 *
1624 * Check for permission to rename a file or directory.
1625 *
1626 * Return: Returns 0 if permission is granted.
1627 */
3ccee46a
AV
1628int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1629 const struct path *new_dir, struct dentry *new_dentry,
0b3974eb 1630 unsigned int flags)
be6d3e56 1631{
c6f493d6
DH
1632 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1633 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
be6d3e56 1634 return 0;
da1ce067 1635
f25fce3e 1636 return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
100f59d9 1637 new_dentry, flags);
be6d3e56 1638}
82140443 1639EXPORT_SYMBOL(security_path_rename);
be6d3e56 1640
916e3258
PM
1641/**
1642 * security_path_truncate() - Check if truncating a file is allowed
1643 * @path: file
1644 *
1645 * Check permission before truncating the file indicated by path. Note that
1646 * truncation permissions may also be checked based on already opened files,
1647 * using the security_file_truncate() hook.
1648 *
1649 * Return: Returns 0 if permission is granted.
1650 */
81f4c506 1651int security_path_truncate(const struct path *path)
be6d3e56 1652{
c6f493d6 1653 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
be6d3e56 1654 return 0;
f25fce3e 1655 return call_int_hook(path_truncate, 0, path);
be6d3e56 1656}
89eda068 1657
916e3258
PM
1658/**
1659 * security_path_chmod() - Check if changing the file's mode is allowed
1660 * @path: file
1661 * @mode: new mode
1662 *
1663 * Check for permission to change a mode of the file @path. The new mode is
1664 * specified in @mode which is a bitmask of constants from
1665 * <include/uapi/linux/stat.h>.
1666 *
1667 * Return: Returns 0 if permission is granted.
1668 */
be01f9f2 1669int security_path_chmod(const struct path *path, umode_t mode)
89eda068 1670{
c6f493d6 1671 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 1672 return 0;
f25fce3e 1673 return call_int_hook(path_chmod, 0, path, mode);
89eda068
TH
1674}
1675
916e3258
PM
1676/**
1677 * security_path_chown() - Check if changing the file's owner/group is allowed
1678 * @path: file
1679 * @uid: file owner
1680 * @gid: file group
1681 *
1682 * Check for permission to change owner/group of a file or directory.
1683 *
1684 * Return: Returns 0 if permission is granted.
1685 */
7fd25dac 1686int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
89eda068 1687{
c6f493d6 1688 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 1689 return 0;
f25fce3e 1690 return call_int_hook(path_chown, 0, path, uid, gid);
89eda068 1691}
8b8efb44 1692
916e3258
PM
1693/**
1694 * security_path_chroot() - Check if changing the root directory is allowed
1695 * @path: directory
1696 *
1697 * Check for permission to change root directory.
1698 *
1699 * Return: Returns 0 if permission is granted.
1700 */
77b286c0 1701int security_path_chroot(const struct path *path)
8b8efb44 1702{
f25fce3e 1703 return call_int_hook(path_chroot, 0, path);
8b8efb44 1704}
be6d3e56
KT
1705#endif
1706
916e3258
PM
1707/**
1708 * security_inode_create() - Check if creating a file is allowed
1709 * @dir: the parent directory
1710 * @dentry: the file being created
1711 * @mode: requested file mode
1712 *
1713 * Check permission to create a regular file.
1714 *
1715 * Return: Returns 0 if permission is granted.
1716 */
4acdaf27 1717int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
20510f2f
JM
1718{
1719 if (unlikely(IS_PRIVATE(dir)))
1720 return 0;
f25fce3e 1721 return call_int_hook(inode_create, 0, dir, dentry, mode);
20510f2f 1722}
800a9647 1723EXPORT_SYMBOL_GPL(security_inode_create);
20510f2f 1724
916e3258
PM
1725/**
1726 * security_inode_link() - Check if creating a hard link is allowed
1727 * @old_dentry: existing file
1728 * @dir: new parent directory
1729 * @new_dentry: new link
1730 *
1731 * Check permission before creating a new hard link to a file.
1732 *
1733 * Return: Returns 0 if permission is granted.
1734 */
20510f2f
JM
1735int security_inode_link(struct dentry *old_dentry, struct inode *dir,
1736 struct dentry *new_dentry)
1737{
c6f493d6 1738 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
20510f2f 1739 return 0;
f25fce3e 1740 return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
20510f2f
JM
1741}
1742
916e3258
PM
1743/**
1744 * security_inode_unlink() - Check if removing a hard link is allowed
1745 * @dir: parent directory
1746 * @dentry: file
1747 *
1748 * Check the permission to remove a hard link to a file.
1749 *
1750 * Return: Returns 0 if permission is granted.
1751 */
20510f2f
JM
1752int security_inode_unlink(struct inode *dir, struct dentry *dentry)
1753{
c6f493d6 1754 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1755 return 0;
f25fce3e 1756 return call_int_hook(inode_unlink, 0, dir, dentry);
20510f2f
JM
1757}
1758
916e3258
PM
1759/**
1760 * security_inode_symlink() Check if creating a symbolic link is allowed
1761 * @dir: parent directory
1762 * @dentry: symbolic link
1763 * @old_name: existing filename
1764 *
1765 * Check the permission to create a symbolic link to a file.
1766 *
1767 * Return: Returns 0 if permission is granted.
1768 */
20510f2f
JM
1769int security_inode_symlink(struct inode *dir, struct dentry *dentry,
1770 const char *old_name)
1771{
1772 if (unlikely(IS_PRIVATE(dir)))
1773 return 0;
f25fce3e 1774 return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
20510f2f
JM
1775}
1776
916e3258
PM
1777/**
1778 * security_inode_mkdir() - Check if creation a new director is allowed
1779 * @dir: parent directory
1780 * @dentry: new directory
1781 * @mode: new directory mode
1782 *
1783 * Check permissions to create a new directory in the existing directory
1784 * associated with inode structure @dir.
1785 *
1786 * Return: Returns 0 if permission is granted.
1787 */
18bb1db3 1788int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20510f2f
JM
1789{
1790 if (unlikely(IS_PRIVATE(dir)))
1791 return 0;
f25fce3e 1792 return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
20510f2f 1793}
800a9647 1794EXPORT_SYMBOL_GPL(security_inode_mkdir);
20510f2f 1795
916e3258
PM
1796/**
1797 * security_inode_rmdir() - Check if removing a directory is allowed
1798 * @dir: parent directory
1799 * @dentry: directory to be removed
1800 *
1801 * Check the permission to remove a directory.
1802 *
1803 * Return: Returns 0 if permission is granted.
1804 */
20510f2f
JM
1805int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
1806{
c6f493d6 1807 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1808 return 0;
f25fce3e 1809 return call_int_hook(inode_rmdir, 0, dir, dentry);
20510f2f
JM
1810}
1811
916e3258
PM
1812/**
1813 * security_inode_mknod() - Check if creating a special file is allowed
1814 * @dir: parent directory
1815 * @dentry: new file
1816 * @mode: new file mode
1817 * @dev: device number
1818 *
1819 * Check permissions when creating a special file (or a socket or a fifo file
1820 * created via the mknod system call). Note that if mknod operation is being
1821 * done for a regular file, then the create hook will be called and not this
1822 * hook.
1823 *
1824 * Return: Returns 0 if permission is granted.
1825 */
1a67aafb 1826int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
20510f2f
JM
1827{
1828 if (unlikely(IS_PRIVATE(dir)))
1829 return 0;
f25fce3e 1830 return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
20510f2f
JM
1831}
1832
916e3258
PM
1833/**
1834 * security_inode_rename() - Check if renaming a file is allowed
1835 * @old_dir: parent directory of the old file
1836 * @old_dentry: the old file
1837 * @new_dir: parent directory of the new file
1838 * @new_dentry: the new file
1839 * @flags: flags
1840 *
1841 * Check for permission to rename a file or directory.
1842 *
1843 * Return: Returns 0 if permission is granted.
1844 */
20510f2f 1845int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
0b3974eb
MS
1846 struct inode *new_dir, struct dentry *new_dentry,
1847 unsigned int flags)
20510f2f 1848{
c6f493d6
DH
1849 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
1850 (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
20510f2f 1851 return 0;
da1ce067
MS
1852
1853 if (flags & RENAME_EXCHANGE) {
f25fce3e 1854 int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
da1ce067
MS
1855 old_dir, old_dentry);
1856 if (err)
1857 return err;
1858 }
1859
f25fce3e 1860 return call_int_hook(inode_rename, 0, old_dir, old_dentry,
20510f2f
JM
1861 new_dir, new_dentry);
1862}
1863
916e3258
PM
1864/**
1865 * security_inode_readlink() - Check if reading a symbolic link is allowed
1866 * @dentry: link
1867 *
1868 * Check the permission to read the symbolic link.
1869 *
1870 * Return: Returns 0 if permission is granted.
1871 */
20510f2f
JM
1872int security_inode_readlink(struct dentry *dentry)
1873{
c6f493d6 1874 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1875 return 0;
f25fce3e 1876 return call_int_hook(inode_readlink, 0, dentry);
20510f2f
JM
1877}
1878
916e3258
PM
1879/**
1880 * security_inode_follow_link() - Check if following a symbolic link is allowed
1881 * @dentry: link dentry
1882 * @inode: link inode
1883 * @rcu: true if in RCU-walk mode
1884 *
1885 * Check permission to follow a symbolic link when looking up a pathname. If
1886 * @rcu is true, @inode is not stable.
1887 *
1888 * Return: Returns 0 if permission is granted.
1889 */
bda0be7a
N
1890int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1891 bool rcu)
20510f2f 1892{
bda0be7a 1893 if (unlikely(IS_PRIVATE(inode)))
20510f2f 1894 return 0;
e22619a2 1895 return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
20510f2f
JM
1896}
1897
916e3258
PM
1898/**
1899 * security_inode_permission() - Check if accessing an inode is allowed
1900 * @inode: inode
1901 * @mask: access mask
1902 *
1903 * Check permission before accessing an inode. This hook is called by the
1904 * existing Linux permission function, so a security module can use it to
1905 * provide additional checking for existing Linux permission checks. Notice
1906 * that this hook is called when a file is opened (as well as many other
1907 * operations), whereas the file_security_ops permission hook is called when
1908 * the actual read/write operations are performed.
1909 *
1910 * Return: Returns 0 if permission is granted.
1911 */
b77b0646 1912int security_inode_permission(struct inode *inode, int mask)
20510f2f
JM
1913{
1914 if (unlikely(IS_PRIVATE(inode)))
1915 return 0;
f25fce3e 1916 return call_int_hook(inode_permission, 0, inode, mask);
20510f2f
JM
1917}
1918
916e3258
PM
1919/**
1920 * security_inode_setattr() - Check if setting file attributes is allowed
1921 * @idmap: idmap of the mount
1922 * @dentry: file
1923 * @attr: new attributes
1924 *
1925 * Check permission before setting file attributes. Note that the kernel call
1926 * to notify_change is performed from several locations, whenever file
1927 * attributes change (such as when a file is truncated, chown/chmod operations,
1928 * transferring disk quotas, etc).
1929 *
1930 * Return: Returns 0 if permission is granted.
1931 */
c1632a0f 1932int security_inode_setattr(struct mnt_idmap *idmap,
0e363cf3 1933 struct dentry *dentry, struct iattr *attr)
20510f2f 1934{
817b54aa
MZ
1935 int ret;
1936
c6f493d6 1937 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1938 return 0;
f25fce3e 1939 ret = call_int_hook(inode_setattr, 0, dentry, attr);
817b54aa
MZ
1940 if (ret)
1941 return ret;
c1632a0f 1942 return evm_inode_setattr(idmap, dentry, attr);
20510f2f 1943}
b1da47e2 1944EXPORT_SYMBOL_GPL(security_inode_setattr);
20510f2f 1945
916e3258
PM
1946/**
1947 * security_inode_getattr() - Check if getting file attributes is allowed
1948 * @path: file
1949 *
1950 * Check permission before obtaining file attributes.
1951 *
1952 * Return: Returns 0 if permission is granted.
1953 */
3f7036a0 1954int security_inode_getattr(const struct path *path)
20510f2f 1955{
c6f493d6 1956 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
20510f2f 1957 return 0;
f25fce3e 1958 return call_int_hook(inode_getattr, 0, path);
20510f2f
JM
1959}
1960
916e3258
PM
1961/**
1962 * security_inode_setxattr() - Check if setting file xattrs is allowed
1963 * @idmap: idmap of the mount
1964 * @dentry: file
1965 * @name: xattr name
1966 * @value: xattr value
1967 * @flags: flags
1968 *
1969 * Check permission before setting the extended attributes.
1970 *
1971 * Return: Returns 0 if permission is granted.
1972 */
39f60c1c 1973int security_inode_setxattr(struct mnt_idmap *idmap,
71bc356f 1974 struct dentry *dentry, const char *name,
8f0cfa52 1975 const void *value, size_t size, int flags)
20510f2f 1976{
3e1be52d
MZ
1977 int ret;
1978
c6f493d6 1979 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 1980 return 0;
b1d9e6b0
CS
1981 /*
1982 * SELinux and Smack integrate the cap call,
1983 * so assume that all LSMs supplying this call do so.
1984 */
39f60c1c 1985 ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
71bc356f 1986 size, flags);
b1d9e6b0
CS
1987
1988 if (ret == 1)
1989 ret = cap_inode_setxattr(dentry, name, value, size, flags);
42c63330
MZ
1990 if (ret)
1991 return ret;
1992 ret = ima_inode_setxattr(dentry, name, value, size);
3e1be52d
MZ
1993 if (ret)
1994 return ret;
39f60c1c 1995 return evm_inode_setxattr(idmap, dentry, name, value, size);
20510f2f
JM
1996}
1997
916e3258
PM
1998/**
1999 * security_inode_set_acl() - Check if setting posix acls is allowed
2000 * @idmap: idmap of the mount
2001 * @dentry: file
2002 * @acl_name: acl name
2003 * @kacl: acl struct
2004 *
2005 * Check permission before setting posix acls, the posix acls in @kacl are
2006 * identified by @acl_name.
2007 *
2008 * Return: Returns 0 if permission is granted.
2009 */
700b7940 2010int security_inode_set_acl(struct mnt_idmap *idmap,
72b3897e
CB
2011 struct dentry *dentry, const char *acl_name,
2012 struct posix_acl *kacl)
2013{
e61b135f
CB
2014 int ret;
2015
72b3897e
CB
2016 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2017 return 0;
700b7940 2018 ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
e61b135f
CB
2019 kacl);
2020 if (ret)
2021 return ret;
700b7940 2022 ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
e61b135f
CB
2023 if (ret)
2024 return ret;
700b7940 2025 return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
72b3897e
CB
2026}
2027
916e3258
PM
2028/**
2029 * security_inode_get_acl() - Check if reading posix acls is allowed
2030 * @idmap: idmap of the mount
2031 * @dentry: file
2032 * @acl_name: acl name
2033 *
2034 * Check permission before getting osix acls, the posix acls are identified by
2035 * @acl_name.
2036 *
2037 * Return: Returns 0 if permission is granted.
2038 */
700b7940 2039int security_inode_get_acl(struct mnt_idmap *idmap,
72b3897e
CB
2040 struct dentry *dentry, const char *acl_name)
2041{
2042 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2043 return 0;
700b7940 2044 return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
72b3897e
CB
2045}
2046
916e3258
PM
2047/**
2048 * security_inode_remove_acl() - Check if removing a posix acl is allowed
2049 * @idmap: idmap of the mount
2050 * @dentry: file
2051 * @acl_name: acl name
2052 *
2053 * Check permission before removing posix acls, the posix acls are identified
2054 * by @acl_name.
2055 *
2056 * Return: Returns 0 if permission is granted.
2057 */
700b7940 2058int security_inode_remove_acl(struct mnt_idmap *idmap,
72b3897e
CB
2059 struct dentry *dentry, const char *acl_name)
2060{
e61b135f
CB
2061 int ret;
2062
72b3897e
CB
2063 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2064 return 0;
700b7940 2065 ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
e61b135f
CB
2066 if (ret)
2067 return ret;
700b7940 2068 ret = ima_inode_remove_acl(idmap, dentry, acl_name);
e61b135f
CB
2069 if (ret)
2070 return ret;
700b7940 2071 return evm_inode_remove_acl(idmap, dentry, acl_name);
72b3897e
CB
2072}
2073
916e3258
PM
2074/**
2075 * security_inode_post_setxattr() - Update the inode after a setxattr operation
2076 * @dentry: file
2077 * @name: xattr name
2078 * @value: xattr value
2079 * @size: xattr value size
2080 * @flags: flags
2081 *
2082 * Update inode security field after successful setxattr operation.
2083 */
8f0cfa52
DH
2084void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2085 const void *value, size_t size, int flags)
20510f2f 2086{
c6f493d6 2087 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2088 return;
f25fce3e 2089 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
3e1be52d 2090 evm_inode_post_setxattr(dentry, name, value, size);
20510f2f
JM
2091}
2092
916e3258
PM
2093/**
2094 * security_inode_getxattr() - Check if xattr access is allowed
2095 * @dentry: file
2096 * @name: xattr name
2097 *
2098 * Check permission before obtaining the extended attributes identified by
2099 * @name for @dentry.
2100 *
2101 * Return: Returns 0 if permission is granted.
2102 */
8f0cfa52 2103int security_inode_getxattr(struct dentry *dentry, const char *name)
20510f2f 2104{
c6f493d6 2105 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2106 return 0;
f25fce3e 2107 return call_int_hook(inode_getxattr, 0, dentry, name);
20510f2f
JM
2108}
2109
916e3258
PM
2110/**
2111 * security_inode_listxattr() - Check if listing xattrs is allowed
2112 * @dentry: file
2113 *
2114 * Check permission before obtaining the list of extended attribute names for
2115 * @dentry.
2116 *
2117 * Return: Returns 0 if permission is granted.
2118 */
20510f2f
JM
2119int security_inode_listxattr(struct dentry *dentry)
2120{
c6f493d6 2121 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2122 return 0;
f25fce3e 2123 return call_int_hook(inode_listxattr, 0, dentry);
20510f2f
JM
2124}
2125
916e3258
PM
2126/**
2127 * security_inode_removexattr() - Check if removing an xattr is allowed
2128 * @idmap: idmap of the mount
2129 * @dentry: file
2130 * @name: xattr name
2131 *
2132 * Check permission before removing the extended attribute identified by @name
2133 * for @dentry.
2134 *
2135 * Return: Returns 0 if permission is granted.
2136 */
39f60c1c 2137int security_inode_removexattr(struct mnt_idmap *idmap,
71bc356f 2138 struct dentry *dentry, const char *name)
20510f2f 2139{
3e1be52d
MZ
2140 int ret;
2141
c6f493d6 2142 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2143 return 0;
b1d9e6b0
CS
2144 /*
2145 * SELinux and Smack integrate the cap call,
2146 * so assume that all LSMs supplying this call do so.
2147 */
39f60c1c 2148 ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
b1d9e6b0 2149 if (ret == 1)
39f60c1c 2150 ret = cap_inode_removexattr(idmap, dentry, name);
42c63330
MZ
2151 if (ret)
2152 return ret;
2153 ret = ima_inode_removexattr(dentry, name);
3e1be52d
MZ
2154 if (ret)
2155 return ret;
39f60c1c 2156 return evm_inode_removexattr(idmap, dentry, name);
20510f2f
JM
2157}
2158
916e3258
PM
2159/**
2160 * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2161 * @dentry: associated dentry
2162 *
2163 * Called when an inode has been changed to determine if
2164 * security_inode_killpriv() should be called.
2165 *
2166 * Return: Return <0 on error to abort the inode change operation, return 0 if
2167 * security_inode_killpriv() does not need to be called, return >0 if
2168 * security_inode_killpriv() does need to be called.
2169 */
b5376771
SH
2170int security_inode_need_killpriv(struct dentry *dentry)
2171{
f25fce3e 2172 return call_int_hook(inode_need_killpriv, 0, dentry);
b5376771
SH
2173}
2174
916e3258
PM
2175/**
2176 * security_inode_killpriv() - The setuid bit is removed, update LSM state
2177 * @idmap: idmap of the mount
2178 * @dentry: associated dentry
2179 *
2180 * The @dentry's setuid bit is being removed. Remove similar security labels.
2181 * Called with the dentry->d_inode->i_mutex held.
2182 *
2183 * Return: Return 0 on success. If error is returned, then the operation
2184 * causing setuid bit removal is failed.
2185 */
39f60c1c 2186int security_inode_killpriv(struct mnt_idmap *idmap,
71bc356f 2187 struct dentry *dentry)
b5376771 2188{
39f60c1c 2189 return call_int_hook(inode_killpriv, 0, idmap, dentry);
b5376771
SH
2190}
2191
916e3258
PM
2192/**
2193 * security_inode_getsecurity() - Get the xattr security label of an inode
2194 * @idmap: idmap of the mount
2195 * @inode: inode
2196 * @name: xattr name
2197 * @buffer: security label buffer
2198 * @alloc: allocation flag
2199 *
2200 * Retrieve a copy of the extended attribute representation of the security
2201 * label associated with @name for @inode via @buffer. Note that @name is the
2202 * remainder of the attribute name after the security prefix has been removed.
2203 * @alloc is used to specify if the call should return a value via the buffer
2204 * or just the value length.
2205 *
2206 * Return: Returns size of buffer on success.
2207 */
4609e1f1 2208int security_inode_getsecurity(struct mnt_idmap *idmap,
71bc356f
CB
2209 struct inode *inode, const char *name,
2210 void **buffer, bool alloc)
20510f2f 2211{
2885c1e3
CS
2212 struct security_hook_list *hp;
2213 int rc;
2214
20510f2f 2215 if (unlikely(IS_PRIVATE(inode)))
98e828a0 2216 return LSM_RET_DEFAULT(inode_getsecurity);
2885c1e3
CS
2217 /*
2218 * Only one module will provide an attribute with a given name.
2219 */
df0ce173 2220 hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
4609e1f1 2221 rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer, alloc);
98e828a0 2222 if (rc != LSM_RET_DEFAULT(inode_getsecurity))
2885c1e3
CS
2223 return rc;
2224 }
98e828a0 2225 return LSM_RET_DEFAULT(inode_getsecurity);
20510f2f
JM
2226}
2227
916e3258
PM
2228/**
2229 * security_inode_setsecurity() - Set the xattr security label of an inode
2230 * @inode: inode
2231 * @name: xattr name
2232 * @value: security label
2233 * @size: length of security label
2234 * @flags: flags
2235 *
2236 * Set the security label associated with @name for @inode from the extended
2237 * attribute value @value. @size indicates the size of the @value in bytes.
2238 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2239 * remainder of the attribute name after the security. prefix has been removed.
2240 *
2241 * Return: Returns 0 on success.
2242 */
20510f2f
JM
2243int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
2244{
2885c1e3
CS
2245 struct security_hook_list *hp;
2246 int rc;
2247
20510f2f 2248 if (unlikely(IS_PRIVATE(inode)))
98e828a0 2249 return LSM_RET_DEFAULT(inode_setsecurity);
2885c1e3
CS
2250 /*
2251 * Only one module will provide an attribute with a given name.
2252 */
df0ce173 2253 hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
2885c1e3
CS
2254 rc = hp->hook.inode_setsecurity(inode, name, value, size,
2255 flags);
98e828a0 2256 if (rc != LSM_RET_DEFAULT(inode_setsecurity))
2885c1e3
CS
2257 return rc;
2258 }
98e828a0 2259 return LSM_RET_DEFAULT(inode_setsecurity);
20510f2f
JM
2260}
2261
916e3258
PM
2262/**
2263 * security_inode_listsecurity() - List the xattr security label names
2264 * @inode: inode
2265 * @buffer: buffer
2266 * @buffer_size: size of buffer
2267 *
2268 * Copy the extended attribute names for the security labels associated with
2269 * @inode into @buffer. The maximum size of @buffer is specified by
2270 * @buffer_size. @buffer may be NULL to request the size of the buffer
2271 * required.
2272 *
2273 * Return: Returns number of bytes used/required on success.
2274 */
20510f2f
JM
2275int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2276{
2277 if (unlikely(IS_PRIVATE(inode)))
2278 return 0;
f25fce3e 2279 return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
20510f2f 2280}
c9bccef6 2281EXPORT_SYMBOL(security_inode_listsecurity);
20510f2f 2282
916e3258
PM
2283/**
2284 * security_inode_getsecid() - Get an inode's secid
2285 * @inode: inode
2286 * @secid: secid to return
2287 *
2288 * Get the secid associated with the node. In case of failure, @secid will be
2289 * set to zero.
2290 */
d6335d77 2291void security_inode_getsecid(struct inode *inode, u32 *secid)
8a076191 2292{
f25fce3e 2293 call_void_hook(inode_getsecid, inode, secid);
8a076191
AD
2294}
2295
916e3258
PM
2296/**
2297 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2298 * @src: union dentry of copy-up file
2299 * @new: newly created creds
2300 *
2301 * A file is about to be copied up from lower layer to upper layer of overlay
2302 * filesystem. Security module can prepare a set of new creds and modify as
2303 * need be and return new creds. Caller will switch to new creds temporarily to
2304 * create new file and release newly allocated creds.
2305 *
2306 * Return: Returns 0 on success or a negative error code on error.
2307 */
d8ad8b49
VG
2308int security_inode_copy_up(struct dentry *src, struct cred **new)
2309{
2310 return call_int_hook(inode_copy_up, 0, src, new);
2311}
2312EXPORT_SYMBOL(security_inode_copy_up);
2313
916e3258
PM
2314/**
2315 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2316 * @name: xattr name
2317 *
2318 * Filter the xattrs being copied up when a unioned file is copied up from a
2319 * lower layer to the union/overlay layer. The caller is responsible for
2320 * reading and writing the xattrs, this hook is merely a filter.
2321 *
2322 * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2323 * if the security module does not know about attribute, or a negative
2324 * error code to abort the copy up.
2325 */
121ab822
VG
2326int security_inode_copy_up_xattr(const char *name)
2327{
23e390cd
KS
2328 struct security_hook_list *hp;
2329 int rc;
2330
2331 /*
2332 * The implementation can return 0 (accept the xattr), 1 (discard the
2333 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
2334 * any other error code incase of an error.
2335 */
2336 hlist_for_each_entry(hp,
2337 &security_hook_heads.inode_copy_up_xattr, list) {
2338 rc = hp->hook.inode_copy_up_xattr(name);
2339 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2340 return rc;
2341 }
2342
2343 return LSM_RET_DEFAULT(inode_copy_up_xattr);
121ab822
VG
2344}
2345EXPORT_SYMBOL(security_inode_copy_up_xattr);
2346
9348944b
PM
2347/**
2348 * security_kernfs_init_security() - Init LSM context for a kernfs node
2349 * @kn_dir: parent kernfs node
2350 * @kn: the kernfs node to initialize
2351 *
2352 * Initialize the security context of a newly created kernfs node based on its
2353 * own and its parent's attributes.
2354 *
2355 * Return: Returns 0 if permission is granted.
2356 */
b230d5ab
OM
2357int security_kernfs_init_security(struct kernfs_node *kn_dir,
2358 struct kernfs_node *kn)
2359{
2360 return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2361}
2362
a0fd6480
PM
2363/**
2364 * security_file_permission() - Check file permissions
2365 * @file: file
2366 * @mask: requested permissions
2367 *
2368 * Check file permissions before accessing an open file. This hook is called
2369 * by various operations that read or write files. A security module can use
2370 * this hook to perform additional checking on these operations, e.g. to
2371 * revalidate permissions on use to support privilege bracketing or policy
2372 * changes. Notice that this hook is used when the actual read/write
2373 * operations are performed, whereas the inode_security_ops hook is called when
2374 * a file is opened (as well as many other operations). Although this hook can
2375 * be used to revalidate permissions for various system call operations that
2376 * read or write files, it does not address the revalidation of permissions for
2377 * memory-mapped files. Security modules must handle this separately if they
2378 * need such revalidation.
2379 *
2380 * Return: Returns 0 if permission is granted.
2381 */
20510f2f
JM
2382int security_file_permission(struct file *file, int mask)
2383{
c4ec54b4
EP
2384 int ret;
2385
f25fce3e 2386 ret = call_int_hook(file_permission, 0, file, mask);
c4ec54b4
EP
2387 if (ret)
2388 return ret;
2389
2390 return fsnotify_perm(file, mask);
20510f2f
JM
2391}
2392
a0fd6480
PM
2393/**
2394 * security_file_alloc() - Allocate and init a file's LSM blob
2395 * @file: the file
2396 *
2397 * Allocate and attach a security structure to the file->f_security field. The
2398 * security field is initialized to NULL when the structure is first created.
2399 *
2400 * Return: Return 0 if the hook is successful and permission is granted.
2401 */
20510f2f
JM
2402int security_file_alloc(struct file *file)
2403{
33bf60ca
CS
2404 int rc = lsm_file_alloc(file);
2405
2406 if (rc)
2407 return rc;
2408 rc = call_int_hook(file_alloc_security, 0, file);
2409 if (unlikely(rc))
2410 security_file_free(file);
2411 return rc;
20510f2f
JM
2412}
2413
a0fd6480
PM
2414/**
2415 * security_file_free() - Free a file's LSM blob
2416 * @file: the file
2417 *
2418 * Deallocate and free any security structures stored in file->f_security.
2419 */
20510f2f
JM
2420void security_file_free(struct file *file)
2421{
33bf60ca
CS
2422 void *blob;
2423
f25fce3e 2424 call_void_hook(file_free_security, file);
33bf60ca
CS
2425
2426 blob = file->f_security;
2427 if (blob) {
2428 file->f_security = NULL;
2429 kmem_cache_free(lsm_file_cache, blob);
2430 }
20510f2f
JM
2431}
2432
a0fd6480
PM
2433/**
2434 * security_file_ioctl() - Check if an ioctl is allowed
2435 * @file: associated file
2436 * @cmd: ioctl cmd
2437 * @arg: ioctl arguments
2438 *
2439 * Check permission for an ioctl operation on @file. Note that @arg sometimes
2440 * represents a user space pointer; in other cases, it may be a simple integer
2441 * value. When @arg represents a user space pointer, it should never be used
2442 * by the security module.
2443 *
2444 * Return: Returns 0 if permission is granted.
2445 */
20510f2f
JM
2446int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2447{
f25fce3e 2448 return call_int_hook(file_ioctl, 0, file, cmd, arg);
20510f2f 2449}
292f902a 2450EXPORT_SYMBOL_GPL(security_file_ioctl);
20510f2f 2451
98de59bf 2452static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
20510f2f 2453{
8b3ec681 2454 /*
98de59bf
AV
2455 * Does we have PROT_READ and does the application expect
2456 * it to imply PROT_EXEC? If not, nothing to talk about...
8b3ec681 2457 */
98de59bf
AV
2458 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2459 return prot;
8b3ec681 2460 if (!(current->personality & READ_IMPLIES_EXEC))
98de59bf
AV
2461 return prot;
2462 /*
2463 * if that's an anonymous mapping, let it.
2464 */
2465 if (!file)
2466 return prot | PROT_EXEC;
2467 /*
2468 * ditto if it's not on noexec mount, except that on !MMU we need
b4caecd4 2469 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
98de59bf 2470 */
90f8572b 2471 if (!path_noexec(&file->f_path)) {
8b3ec681 2472#ifndef CONFIG_MMU
b4caecd4
CH
2473 if (file->f_op->mmap_capabilities) {
2474 unsigned caps = file->f_op->mmap_capabilities(file);
2475 if (!(caps & NOMMU_MAP_EXEC))
2476 return prot;
2477 }
8b3ec681 2478#endif
98de59bf 2479 return prot | PROT_EXEC;
8b3ec681 2480 }
98de59bf
AV
2481 /* anything on noexec mount won't get PROT_EXEC */
2482 return prot;
2483}
2484
a0fd6480
PM
2485/**
2486 * security_mmap_file() - Check if mmap'ing a file is allowed
2487 * @file: file
2488 * @prot: protection applied by the kernel
2489 * @flags: flags
2490 *
2491 * Check permissions for a mmap operation. The @file may be NULL, e.g. if
2492 * mapping anonymous memory.
2493 *
2494 * Return: Returns 0 if permission is granted.
2495 */
98de59bf
AV
2496int security_mmap_file(struct file *file, unsigned long prot,
2497 unsigned long flags)
2498{
4971c268 2499 unsigned long prot_adj = mmap_prot(file, prot);
98de59bf 2500 int ret;
4971c268
RS
2501
2502 ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
6c21a7fb
MZ
2503 if (ret)
2504 return ret;
4971c268 2505 return ima_file_mmap(file, prot, prot_adj, flags);
20510f2f
JM
2506}
2507
a0fd6480
PM
2508/**
2509 * security_mmap_addr() - Check if mmap'ing an address is allowed
2510 * @addr: address
2511 *
2512 * Check permissions for a mmap operation at @addr.
2513 *
2514 * Return: Returns 0 if permission is granted.
2515 */
e5467859
AV
2516int security_mmap_addr(unsigned long addr)
2517{
f25fce3e 2518 return call_int_hook(mmap_addr, 0, addr);
e5467859
AV
2519}
2520
a0fd6480
PM
2521/**
2522 * security_file_mprotect() - Check if changing memory protections is allowed
2523 * @vma: memory region
2524 * @reqprot: application requested protection
2525 * @prog: protection applied by the kernel
2526 *
2527 * Check permissions before changing memory access permissions.
2528 *
2529 * Return: Returns 0 if permission is granted.
2530 */
20510f2f
JM
2531int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
2532 unsigned long prot)
2533{
8eb613c0
MZ
2534 int ret;
2535
2536 ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
2537 if (ret)
2538 return ret;
2539 return ima_file_mprotect(vma, prot);
20510f2f
JM
2540}
2541
a0fd6480
PM
2542/**
2543 * security_file_lock() - Check if a file lock is allowed
2544 * @file: file
2545 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2546 *
2547 * Check permission before performing file locking operations. Note the hook
2548 * mediates both flock and fcntl style locks.
2549 *
2550 * Return: Returns 0 if permission is granted.
2551 */
20510f2f
JM
2552int security_file_lock(struct file *file, unsigned int cmd)
2553{
f25fce3e 2554 return call_int_hook(file_lock, 0, file, cmd);
20510f2f
JM
2555}
2556
a0fd6480
PM
2557/**
2558 * security_file_fcntl() - Check if fcntl() op is allowed
2559 * @file: file
2560 * @cmd: fnctl command
2561 * @arg: command argument
2562 *
2563 * Check permission before allowing the file operation specified by @cmd from
2564 * being performed on the file @file. Note that @arg sometimes represents a
2565 * user space pointer; in other cases, it may be a simple integer value. When
2566 * @arg represents a user space pointer, it should never be used by the
2567 * security module.
2568 *
2569 * Return: Returns 0 if permission is granted.
2570 */
20510f2f
JM
2571int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2572{
f25fce3e 2573 return call_int_hook(file_fcntl, 0, file, cmd, arg);
20510f2f
JM
2574}
2575
a0fd6480
PM
2576/**
2577 * security_file_set_fowner() - Set the file owner info in the LSM blob
2578 * @file: the file
2579 *
2580 * Save owner security information (typically from current->security) in
2581 * file->f_security for later use by the send_sigiotask hook.
2582 *
2583 * Return: Returns 0 on success.
2584 */
e0b93edd 2585void security_file_set_fowner(struct file *file)
20510f2f 2586{
f25fce3e 2587 call_void_hook(file_set_fowner, file);
20510f2f
JM
2588}
2589
a0fd6480
PM
2590/**
2591 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2592 * @tsk: target task
2593 * @fown: signal sender
2594 * @sig: signal to be sent, SIGIO is sent if 0
2595 *
2596 * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2597 * process @tsk. Note that this hook is sometimes called from interrupt. Note
2598 * that the fown_struct, @fown, is never outside the context of a struct file,
2599 * so the file structure (and associated security information) can always be
2600 * obtained: container_of(fown, struct file, f_owner).
2601 *
2602 * Return: Returns 0 if permission is granted.
2603 */
20510f2f
JM
2604int security_file_send_sigiotask(struct task_struct *tsk,
2605 struct fown_struct *fown, int sig)
2606{
f25fce3e 2607 return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
20510f2f
JM
2608}
2609
a0fd6480
PM
2610/**
2611 * security_file_receive() - Check is receiving a file via IPC is allowed
2612 * @file: file being received
2613 *
2614 * This hook allows security modules to control the ability of a process to
2615 * receive an open file descriptor via socket IPC.
2616 *
2617 * Return: Returns 0 if permission is granted.
2618 */
20510f2f
JM
2619int security_file_receive(struct file *file)
2620{
f25fce3e 2621 return call_int_hook(file_receive, 0, file);
20510f2f
JM
2622}
2623
a0fd6480
PM
2624/**
2625 * security_file_open() - Save open() time state for late use by the LSM
2626 * @file:
2627 *
2628 * Save open-time permission checking state for later use upon file_permission,
2629 * and recheck access if anything has changed since inode_permission.
2630 *
2631 * Return: Returns 0 if permission is granted.
2632 */
e3f20ae2 2633int security_file_open(struct file *file)
20510f2f 2634{
c4ec54b4
EP
2635 int ret;
2636
94817692 2637 ret = call_int_hook(file_open, 0, file);
c4ec54b4
EP
2638 if (ret)
2639 return ret;
2640
2641 return fsnotify_perm(file, MAY_OPEN);
20510f2f
JM
2642}
2643
a0fd6480
PM
2644/**
2645 * security_file_truncate() - Check if truncating a file is allowed
2646 * @file: file
2647 *
2648 * Check permission before truncating a file, i.e. using ftruncate. Note that
2649 * truncation permission may also be checked based on the path, using the
2650 * @path_truncate hook.
2651 *
2652 * Return: Returns 0 if permission is granted.
2653 */
3350607d
GN
2654int security_file_truncate(struct file *file)
2655{
2656 return call_int_hook(file_truncate, 0, file);
2657}
2658
130c53bf
PM
2659/**
2660 * security_task_alloc() - Allocate a task's LSM blob
2661 * @task: the task
2662 * @clone_flags: flags indicating what is being shared
2663 *
2664 * Handle allocation of task-related resources.
2665 *
2666 * Return: Returns a zero on success, negative values on failure.
2667 */
e4e55b47
TH
2668int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2669{
f4ad8f2c
CS
2670 int rc = lsm_task_alloc(task);
2671
2672 if (rc)
2673 return rc;
2674 rc = call_int_hook(task_alloc, 0, task, clone_flags);
2675 if (unlikely(rc))
2676 security_task_free(task);
2677 return rc;
e4e55b47
TH
2678}
2679
130c53bf
PM
2680/**
2681 * security_task_free() - Free a task's LSM blob and related resources
2682 * @task: task
2683 *
2684 * Handle release of task-related resources. Note that this can be called from
2685 * interrupt context.
2686 */
1a2a4d06
KC
2687void security_task_free(struct task_struct *task)
2688{
f25fce3e 2689 call_void_hook(task_free, task);
f4ad8f2c
CS
2690
2691 kfree(task->security);
2692 task->security = NULL;
1a2a4d06
KC
2693}
2694
130c53bf
PM
2695/**
2696 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
2697 * @cred: credentials
2698 * @gfp: gfp flags
2699 *
2700 * Only allocate sufficient memory and attach to @cred such that
2701 * cred_transfer() will not get ENOMEM.
2702 *
2703 * Return: Returns 0 on success, negative values on failure.
2704 */
ee18d64c
DH
2705int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2706{
bbd3662a
CS
2707 int rc = lsm_cred_alloc(cred, gfp);
2708
2709 if (rc)
2710 return rc;
2711
2712 rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
33bf60ca 2713 if (unlikely(rc))
bbd3662a
CS
2714 security_cred_free(cred);
2715 return rc;
ee18d64c
DH
2716}
2717
130c53bf
PM
2718/**
2719 * security_cred_free() - Free the cred's LSM blob and associated resources
2720 * @cred: credentials
2721 *
2722 * Deallocate and clear the cred->security field in a set of credentials.
2723 */
d84f4f99 2724void security_cred_free(struct cred *cred)
20510f2f 2725{
a5795fd3
JM
2726 /*
2727 * There is a failure case in prepare_creds() that
2728 * may result in a call here with ->security being NULL.
2729 */
2730 if (unlikely(cred->security == NULL))
2731 return;
2732
f25fce3e 2733 call_void_hook(cred_free, cred);
bbd3662a
CS
2734
2735 kfree(cred->security);
2736 cred->security = NULL;
20510f2f
JM
2737}
2738
130c53bf
PM
2739/**
2740 * security_prepare_creds() - Prepare a new set of credentials
2741 * @new: new credentials
2742 * @old: original credentials
2743 * @gfp: gfp flags
2744 *
2745 * Prepare a new set of credentials by copying the data from the old set.
2746 *
2747 * Return: Returns 0 on success, negative values on failure.
2748 */
d84f4f99 2749int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
20510f2f 2750{
bbd3662a
CS
2751 int rc = lsm_cred_alloc(new, gfp);
2752
2753 if (rc)
2754 return rc;
2755
2756 rc = call_int_hook(cred_prepare, 0, new, old, gfp);
33bf60ca 2757 if (unlikely(rc))
bbd3662a
CS
2758 security_cred_free(new);
2759 return rc;
d84f4f99
DH
2760}
2761
130c53bf
PM
2762/**
2763 * security_transfer_creds() - Transfer creds
2764 * @new: target credentials
2765 * @old: original credentials
2766 *
2767 * Transfer data from original creds to new creds.
2768 */
ee18d64c
DH
2769void security_transfer_creds(struct cred *new, const struct cred *old)
2770{
f25fce3e 2771 call_void_hook(cred_transfer, new, old);
ee18d64c
DH
2772}
2773
130c53bf
PM
2774/**
2775 * security_cred_getsecid() - Get the secid from a set of credentials
2776 * @c: credentials
2777 * @secid: secid value
2778 *
2779 * Retrieve the security identifier of the cred structure @c. In case of
2780 * failure, @secid will be set to zero.
2781 */
3ec30113
MG
2782void security_cred_getsecid(const struct cred *c, u32 *secid)
2783{
2784 *secid = 0;
2785 call_void_hook(cred_getsecid, c, secid);
2786}
2787EXPORT_SYMBOL(security_cred_getsecid);
2788
130c53bf
PM
2789/**
2790 * security_kernel_act_as() - Set the kernel credentials to act as secid
2791 * @new: credentials
2792 * @secid: secid
2793 *
2794 * Set the credentials for a kernel service to act as (subjective context).
2795 * The current task must be the one that nominated @secid.
2796 *
2797 * Return: Returns 0 if successful.
2798 */
3a3b7ce9
DH
2799int security_kernel_act_as(struct cred *new, u32 secid)
2800{
f25fce3e 2801 return call_int_hook(kernel_act_as, 0, new, secid);
3a3b7ce9
DH
2802}
2803
130c53bf
PM
2804/**
2805 * security_kernel_create_files_as() - Set file creation context using an inode
2806 * @new: target credentials
2807 * @inode: reference inode
2808 *
2809 * Set the file creation context in a set of credentials to be the same as the
2810 * objective context of the specified inode. The current task must be the one
2811 * that nominated @inode.
2812 *
2813 * Return: Returns 0 if successful.
2814 */
3a3b7ce9
DH
2815int security_kernel_create_files_as(struct cred *new, struct inode *inode)
2816{
f25fce3e 2817 return call_int_hook(kernel_create_files_as, 0, new, inode);
3a3b7ce9
DH
2818}
2819
130c53bf
PM
2820/**
2821 * security_kernel_module_request() - Check is loading a module is allowed
2822 * @kmod_name: module name
2823 *
2824 * Ability to trigger the kernel to automatically upcall to userspace for
2825 * userspace to load a kernel module with the given name.
2826 *
2827 * Return: Returns 0 if successful.
2828 */
dd8dbf2e 2829int security_kernel_module_request(char *kmod_name)
9188499c 2830{
6eb864c1
MK
2831 int ret;
2832
2833 ret = call_int_hook(kernel_module_request, 0, kmod_name);
2834 if (ret)
2835 return ret;
2836 return integrity_kernel_module_request(kmod_name);
9188499c
EP
2837}
2838
130c53bf
PM
2839/**
2840 * security_kernel_read_file() - Read a file specified by userspace
2841 * @file: file
2842 * @id: file identifier
2843 * @contents: trust if security_kernel_post_read_file() will be called
2844 *
2845 * Read a file specified by userspace.
2846 *
2847 * Return: Returns 0 if permission is granted.
2848 */
2039bda1
KC
2849int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
2850 bool contents)
39eeb4fb
MZ
2851{
2852 int ret;
2853
2039bda1 2854 ret = call_int_hook(kernel_read_file, 0, file, id, contents);
39eeb4fb
MZ
2855 if (ret)
2856 return ret;
2039bda1 2857 return ima_read_file(file, id, contents);
39eeb4fb
MZ
2858}
2859EXPORT_SYMBOL_GPL(security_kernel_read_file);
2860
130c53bf
PM
2861/**
2862 * security_kernel_post_read_file() - Read a file specified by userspace
2863 * @file: file
2864 * @buf: file contents
2865 * @size: size of file contents
2866 * @id: file identifier
2867 *
2868 * Read a file specified by userspace. This must be paired with a prior call
2869 * to security_kernel_read_file() call that indicated this hook would also be
2870 * called, see security_kernel_read_file() for more information.
2871 *
2872 * Return: Returns 0 if permission is granted.
2873 */
bc8ca5b9
MZ
2874int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
2875 enum kernel_read_file_id id)
b44a7dfc 2876{
cf222217
MZ
2877 int ret;
2878
2879 ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
2880 if (ret)
2881 return ret;
2882 return ima_post_read_file(file, buf, size, id);
b44a7dfc
MZ
2883}
2884EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
2885
130c53bf
PM
2886/**
2887 * security_kernel_load_data() - Load data provided by userspace
2888 * @id: data identifier
2889 * @contents: true if security_kernel_post_load_data() will be called
2890 *
2891 * Load data provided by userspace.
2892 *
2893 * Return: Returns 0 if permission is granted.
2894 */
b64fcae7 2895int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
377179cd 2896{
16c267aa
MZ
2897 int ret;
2898
b64fcae7 2899 ret = call_int_hook(kernel_load_data, 0, id, contents);
16c267aa
MZ
2900 if (ret)
2901 return ret;
b64fcae7 2902 return ima_load_data(id, contents);
377179cd 2903}
83a68a06 2904EXPORT_SYMBOL_GPL(security_kernel_load_data);
377179cd 2905
130c53bf
PM
2906/**
2907 * security_kernel_post_load_data() - Load userspace data from a non-file source
2908 * @buf: data
2909 * @size: size of data
2910 * @id: data identifier
2911 * @description: text description of data, specific to the id value
2912 *
2913 * Load data provided by a non-file source (usually userspace buffer). This
2914 * must be paired with a prior security_kernel_load_data() call that indicated
2915 * this hook would also be called, see security_kernel_load_data() for more
2916 * information.
2917 *
2918 * Return: Returns 0 if permission is granted.
2919 */
b64fcae7
KC
2920int security_kernel_post_load_data(char *buf, loff_t size,
2921 enum kernel_load_data_id id,
2922 char *description)
2923{
2924 int ret;
2925
2926 ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
2927 description);
2928 if (ret)
2929 return ret;
2930 return ima_post_load_data(buf, size, id, description);
2931}
2932EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
2933
130c53bf
PM
2934/**
2935 * security_task_fix_setuid() - Update LSM with new user id attributes
2936 * @new: updated credentials
2937 * @old: credentials being replaced
2938 * @flags: LSM_SETID_* flag values
2939 *
2940 * Update the module's state after setting one or more of the user identity
2941 * attributes of the current process. The @flags parameter indicates which of
2942 * the set*uid system calls invoked this hook. If @new is the set of
2943 * credentials that will be installed. Modifications should be made to this
2944 * rather than to @current->cred.
2945 *
2946 * Return: Returns 0 on success.
2947 */
d84f4f99
DH
2948int security_task_fix_setuid(struct cred *new, const struct cred *old,
2949 int flags)
20510f2f 2950{
f25fce3e 2951 return call_int_hook(task_fix_setuid, 0, new, old, flags);
20510f2f
JM
2952}
2953
130c53bf
PM
2954/**
2955 * security_task_fix_setgid() - Update LSM with new group id attributes
2956 * @new: updated credentials
2957 * @old: credentials being replaced
2958 * @flags: LSM_SETID_* flag value
2959 *
2960 * Update the module's state after setting one or more of the group identity
2961 * attributes of the current process. The @flags parameter indicates which of
2962 * the set*gid system calls invoked this hook. @new is the set of credentials
2963 * that will be installed. Modifications should be made to this rather than to
2964 * @current->cred.
2965 *
2966 * Return: Returns 0 on success.
2967 */
39030e13
TC
2968int security_task_fix_setgid(struct cred *new, const struct cred *old,
2969 int flags)
2970{
2971 return call_int_hook(task_fix_setgid, 0, new, old, flags);
2972}
2973
130c53bf
PM
2974/**
2975 * security_task_fix_setgroups() - Update LSM with new supplementary groups
2976 * @new: updated credentials
2977 * @old: credentials being replaced
2978 *
2979 * Update the module's state after setting the supplementary group identity
2980 * attributes of the current process. @new is the set of credentials that will
2981 * be installed. Modifications should be made to this rather than to
2982 * @current->cred.
2983 *
2984 * Return: Returns 0 on success.
2985 */
fcfe0ac2
MM
2986int security_task_fix_setgroups(struct cred *new, const struct cred *old)
2987{
2988 return call_int_hook(task_fix_setgroups, 0, new, old);
2989}
2990
130c53bf
PM
2991/**
2992 * security_task_setpgid() - Check if setting the pgid is allowed
2993 * @p: task being modified
2994 * @pgid: new pgid
2995 *
2996 * Check permission before setting the process group identifier of the process
2997 * @p to @pgid.
2998 *
2999 * Return: Returns 0 if permission is granted.
3000 */
20510f2f
JM
3001int security_task_setpgid(struct task_struct *p, pid_t pgid)
3002{
f25fce3e 3003 return call_int_hook(task_setpgid, 0, p, pgid);
20510f2f
JM
3004}
3005
130c53bf
PM
3006/**
3007 * security_task_getpgid() - Check if getting the pgid is allowed
3008 * @p: task
3009 *
3010 * Check permission before getting the process group identifier of the process
3011 * @p.
3012 *
3013 * Return: Returns 0 if permission is granted.
3014 */
20510f2f
JM
3015int security_task_getpgid(struct task_struct *p)
3016{
f25fce3e 3017 return call_int_hook(task_getpgid, 0, p);
20510f2f
JM
3018}
3019
130c53bf
PM
3020/**
3021 * security_task_getsid() - Check if getting the session id is allowed
3022 * @p: task
3023 *
3024 * Check permission before getting the session identifier of the process @p.
3025 *
3026 * Return: Returns 0 if permission is granted.
3027 */
20510f2f
JM
3028int security_task_getsid(struct task_struct *p)
3029{
f25fce3e 3030 return call_int_hook(task_getsid, 0, p);
20510f2f
JM
3031}
3032
130c53bf
PM
3033/**
3034 * security_current_getsecid_subj() - Get the current task's subjective secid
3035 * @secid: secid value
3036 *
3037 * Retrieve the subjective security identifier of the current task and return
3038 * it in @secid. In case of failure, @secid will be set to zero.
3039 */
6326948f 3040void security_current_getsecid_subj(u32 *secid)
20510f2f 3041{
b1d9e6b0 3042 *secid = 0;
6326948f 3043 call_void_hook(current_getsecid_subj, secid);
20510f2f 3044}
6326948f 3045EXPORT_SYMBOL(security_current_getsecid_subj);
4ebd7651 3046
130c53bf
PM
3047/**
3048 * security_task_getsecid_obj() - Get a task's objective secid
3049 * @p: target task
3050 * @secid: secid value
3051 *
3052 * Retrieve the objective security identifier of the task_struct in @p and
3053 * return it in @secid. In case of failure, @secid will be set to zero.
3054 */
4ebd7651
PM
3055void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3056{
3057 *secid = 0;
3058 call_void_hook(task_getsecid_obj, p, secid);
3059}
3060EXPORT_SYMBOL(security_task_getsecid_obj);
20510f2f 3061
130c53bf
PM
3062/**
3063 * security_task_setnice() - Check if setting a task's nice value is allowed
3064 * @p: target task
3065 * @nice: nice value
3066 *
3067 * Check permission before setting the nice value of @p to @nice.
3068 *
3069 * Return: Returns 0 if permission is granted.
3070 */
20510f2f
JM
3071int security_task_setnice(struct task_struct *p, int nice)
3072{
f25fce3e 3073 return call_int_hook(task_setnice, 0, p, nice);
20510f2f
JM
3074}
3075
130c53bf
PM
3076/**
3077 * security_task_setioprio() - Check if setting a task's ioprio is allowed
3078 * @p: target task
3079 * @ioprio: ioprio value
3080 *
3081 * Check permission before setting the ioprio value of @p to @ioprio.
3082 *
3083 * Return: Returns 0 if permission is granted.
3084 */
20510f2f
JM
3085int security_task_setioprio(struct task_struct *p, int ioprio)
3086{
f25fce3e 3087 return call_int_hook(task_setioprio, 0, p, ioprio);
20510f2f
JM
3088}
3089
130c53bf
PM
3090/**
3091 * security_task_getioprio() - Check if getting a task's ioprio is allowed
3092 * @p: task
3093 *
3094 * Check permission before getting the ioprio value of @p.
3095 *
3096 * Return: Returns 0 if permission is granted.
3097 */
20510f2f
JM
3098int security_task_getioprio(struct task_struct *p)
3099{
f25fce3e 3100 return call_int_hook(task_getioprio, 0, p);
20510f2f
JM
3101}
3102
130c53bf
PM
3103/**
3104 * security_task_prlimit() - Check if get/setting resources limits is allowed
3105 * @cred: current task credentials
3106 * @tcred: target task credentials
3107 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3108 *
3109 * Check permission before getting and/or setting the resource limits of
3110 * another task.
3111 *
3112 * Return: Returns 0 if permission is granted.
3113 */
791ec491
SS
3114int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3115 unsigned int flags)
3116{
3117 return call_int_hook(task_prlimit, 0, cred, tcred, flags);
3118}
3119
130c53bf
PM
3120/**
3121 * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3122 * @p: target task's group leader
3123 * @resource: resource whose limit is being set
3124 * @new_rlim: new resource limit
3125 *
3126 * Check permission before setting the resource limits of process @p for
3127 * @resource to @new_rlim. The old resource limit values can be examined by
3128 * dereferencing (p->signal->rlim + resource).
3129 *
3130 * Return: Returns 0 if permission is granted.
3131 */
8fd00b4d
JS
3132int security_task_setrlimit(struct task_struct *p, unsigned int resource,
3133 struct rlimit *new_rlim)
20510f2f 3134{
f25fce3e 3135 return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
20510f2f
JM
3136}
3137
130c53bf
PM
3138/**
3139 * security_task_setscheduler() - Check if setting sched policy/param is allowed
3140 * @p: target task
3141 *
3142 * Check permission before setting scheduling policy and/or parameters of
3143 * process @p.
3144 *
3145 * Return: Returns 0 if permission is granted.
3146 */
b0ae1981 3147int security_task_setscheduler(struct task_struct *p)
20510f2f 3148{
f25fce3e 3149 return call_int_hook(task_setscheduler, 0, p);
20510f2f
JM
3150}
3151
130c53bf
PM
3152/**
3153 * security_task_getscheduler() - Check if getting scheduling info is allowed
3154 * @p: target task
3155 *
3156 * Check permission before obtaining scheduling information for process @p.
3157 *
3158 * Return: Returns 0 if permission is granted.
3159 */
20510f2f
JM
3160int security_task_getscheduler(struct task_struct *p)
3161{
f25fce3e 3162 return call_int_hook(task_getscheduler, 0, p);
20510f2f
JM
3163}
3164
130c53bf
PM
3165/**
3166 * security_task_movememory() - Check if moving memory is allowed
3167 * @p: task
3168 *
3169 * Check permission before moving memory owned by process @p.
3170 *
3171 * Return: Returns 0 if permission is granted.
3172 */
20510f2f
JM
3173int security_task_movememory(struct task_struct *p)
3174{
f25fce3e 3175 return call_int_hook(task_movememory, 0, p);
20510f2f
JM
3176}
3177
130c53bf
PM
3178/**
3179 * security_task_kill() - Check if sending a signal is allowed
3180 * @p: target process
3181 * @info: signal information
3182 * @sig: signal value
3183 * @cred: credentials of the signal sender, NULL if @current
3184 *
3185 * Check permission before sending signal @sig to @p. @info can be NULL, the
3186 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
3187 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3188 * the kernel and should typically be permitted. SIGIO signals are handled
3189 * separately by the send_sigiotask hook in file_security_ops.
3190 *
3191 * Return: Returns 0 if permission is granted.
3192 */
ae7795bc 3193int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
6b4f3d01 3194 int sig, const struct cred *cred)
20510f2f 3195{
6b4f3d01 3196 return call_int_hook(task_kill, 0, p, info, sig, cred);
20510f2f
JM
3197}
3198
130c53bf
PM
3199/**
3200 * security_task_prctl() - Check if a prctl op is allowed
3201 * @option: operation
3202 * @arg2: argument
3203 * @arg3: argument
3204 * @arg4: argument
3205 * @arg5: argument
3206 *
3207 * Check permission before performing a process control operation on the
3208 * current process.
3209 *
3210 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3211 * to cause prctl() to return immediately with that value.
3212 */
20510f2f 3213int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
d84f4f99 3214 unsigned long arg4, unsigned long arg5)
20510f2f 3215{
b1d9e6b0 3216 int thisrc;
98e828a0 3217 int rc = LSM_RET_DEFAULT(task_prctl);
b1d9e6b0
CS
3218 struct security_hook_list *hp;
3219
df0ce173 3220 hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
b1d9e6b0 3221 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
98e828a0 3222 if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
b1d9e6b0
CS
3223 rc = thisrc;
3224 if (thisrc != 0)
3225 break;
3226 }
3227 }
3228 return rc;
20510f2f
JM
3229}
3230
130c53bf
PM
3231/**
3232 * security_task_to_inode() - Set the security attributes of a task's inode
3233 * @p: task
3234 * @inode: inode
3235 *
3236 * Set the security attributes for an inode based on an associated task's
3237 * security attributes, e.g. for /proc/pid inodes.
3238 */
20510f2f
JM
3239void security_task_to_inode(struct task_struct *p, struct inode *inode)
3240{
f25fce3e 3241 call_void_hook(task_to_inode, p, inode);
20510f2f
JM
3242}
3243
130c53bf
PM
3244/**
3245 * security_create_user_ns() - Check if creating a new userns is allowed
3246 * @cred: prepared creds
3247 *
3248 * Check permission prior to creating a new user namespace.
3249 *
3250 * Return: Returns 0 if successful, otherwise < 0 error code.
3251 */
7cd4c5c2
FL
3252int security_create_user_ns(const struct cred *cred)
3253{
3254 return call_int_hook(userns_create, 0, cred);
3255}
20510f2f
JM
3256
3257int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3258{
f25fce3e 3259 return call_int_hook(ipc_permission, 0, ipcp, flag);
20510f2f
JM
3260}
3261
8a076191
AD
3262void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3263{
b1d9e6b0 3264 *secid = 0;
f25fce3e 3265 call_void_hook(ipc_getsecid, ipcp, secid);
8a076191
AD
3266}
3267
20510f2f
JM
3268int security_msg_msg_alloc(struct msg_msg *msg)
3269{
ecd5f82e
CS
3270 int rc = lsm_msg_msg_alloc(msg);
3271
3272 if (unlikely(rc))
3273 return rc;
3274 rc = call_int_hook(msg_msg_alloc_security, 0, msg);
3275 if (unlikely(rc))
3276 security_msg_msg_free(msg);
3277 return rc;
20510f2f
JM
3278}
3279
3280void security_msg_msg_free(struct msg_msg *msg)
3281{
f25fce3e 3282 call_void_hook(msg_msg_free_security, msg);
ecd5f82e
CS
3283 kfree(msg->security);
3284 msg->security = NULL;
20510f2f
JM
3285}
3286
d8c6e854 3287int security_msg_queue_alloc(struct kern_ipc_perm *msq)
20510f2f 3288{
ecd5f82e
CS
3289 int rc = lsm_ipc_alloc(msq);
3290
3291 if (unlikely(rc))
3292 return rc;
3293 rc = call_int_hook(msg_queue_alloc_security, 0, msq);
3294 if (unlikely(rc))
3295 security_msg_queue_free(msq);
3296 return rc;
20510f2f
JM
3297}
3298
d8c6e854 3299void security_msg_queue_free(struct kern_ipc_perm *msq)
20510f2f 3300{
f25fce3e 3301 call_void_hook(msg_queue_free_security, msq);
ecd5f82e
CS
3302 kfree(msq->security);
3303 msq->security = NULL;
20510f2f
JM
3304}
3305
d8c6e854 3306int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
20510f2f 3307{
f25fce3e 3308 return call_int_hook(msg_queue_associate, 0, msq, msqflg);
20510f2f
JM
3309}
3310
d8c6e854 3311int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
20510f2f 3312{
f25fce3e 3313 return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
20510f2f
JM
3314}
3315
d8c6e854 3316int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
20510f2f
JM
3317 struct msg_msg *msg, int msqflg)
3318{
f25fce3e 3319 return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
20510f2f
JM
3320}
3321
d8c6e854 3322int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
20510f2f
JM
3323 struct task_struct *target, long type, int mode)
3324{
f25fce3e 3325 return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
20510f2f
JM
3326}
3327
7191adff 3328int security_shm_alloc(struct kern_ipc_perm *shp)
20510f2f 3329{
ecd5f82e
CS
3330 int rc = lsm_ipc_alloc(shp);
3331
3332 if (unlikely(rc))
3333 return rc;
3334 rc = call_int_hook(shm_alloc_security, 0, shp);
3335 if (unlikely(rc))
3336 security_shm_free(shp);
3337 return rc;
20510f2f
JM
3338}
3339
7191adff 3340void security_shm_free(struct kern_ipc_perm *shp)
20510f2f 3341{
f25fce3e 3342 call_void_hook(shm_free_security, shp);
ecd5f82e
CS
3343 kfree(shp->security);
3344 shp->security = NULL;
20510f2f
JM
3345}
3346
7191adff 3347int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
20510f2f 3348{
f25fce3e 3349 return call_int_hook(shm_associate, 0, shp, shmflg);
20510f2f
JM
3350}
3351
7191adff 3352int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
20510f2f 3353{
f25fce3e 3354 return call_int_hook(shm_shmctl, 0, shp, cmd);
20510f2f
JM
3355}
3356
7191adff 3357int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmflg)
20510f2f 3358{
f25fce3e 3359 return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
20510f2f
JM
3360}
3361
aefad959 3362int security_sem_alloc(struct kern_ipc_perm *sma)
20510f2f 3363{
ecd5f82e
CS
3364 int rc = lsm_ipc_alloc(sma);
3365
3366 if (unlikely(rc))
3367 return rc;
3368 rc = call_int_hook(sem_alloc_security, 0, sma);
3369 if (unlikely(rc))
3370 security_sem_free(sma);
3371 return rc;
20510f2f
JM
3372}
3373
aefad959 3374void security_sem_free(struct kern_ipc_perm *sma)
20510f2f 3375{
f25fce3e 3376 call_void_hook(sem_free_security, sma);
ecd5f82e
CS
3377 kfree(sma->security);
3378 sma->security = NULL;
20510f2f
JM
3379}
3380
aefad959 3381int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
20510f2f 3382{
f25fce3e 3383 return call_int_hook(sem_associate, 0, sma, semflg);
20510f2f
JM
3384}
3385
aefad959 3386int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
20510f2f 3387{
f25fce3e 3388 return call_int_hook(sem_semctl, 0, sma, cmd);
20510f2f
JM
3389}
3390
aefad959 3391int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
20510f2f
JM
3392 unsigned nsops, int alter)
3393{
f25fce3e 3394 return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
20510f2f
JM
3395}
3396
916e3258
PM
3397/**
3398 * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3399 * @dentry: dentry
3400 * @inode: inode
3401 *
3402 * Fill in @inode security information for a @dentry if allowed.
3403 */
20510f2f
JM
3404void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3405{
3406 if (unlikely(inode && IS_PRIVATE(inode)))
3407 return;
f25fce3e 3408 call_void_hook(d_instantiate, dentry, inode);
20510f2f
JM
3409}
3410EXPORT_SYMBOL(security_d_instantiate);
3411
916e3258
PM
3412/**
3413 * security_getprocattr() - Read an attribute for a task
3414 * @p: the task
3415 * @lsm: LSM name
3416 * @name: attribute name
3417 * @value: attribute value
3418 *
3419 * Read attribute @name for task @p and store it into @value if allowed.
3420 *
3421 * Return: Returns the length of @value on success, a negative value otherwise.
3422 */
c8e477c6
AV
3423int security_getprocattr(struct task_struct *p, const char *lsm,
3424 const char *name, char **value)
20510f2f 3425{
6d9c939d
CS
3426 struct security_hook_list *hp;
3427
3428 hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
3429 if (lsm != NULL && strcmp(lsm, hp->lsm))
3430 continue;
3431 return hp->hook.getprocattr(p, name, value);
3432 }
98e828a0 3433 return LSM_RET_DEFAULT(getprocattr);
20510f2f
JM
3434}
3435
916e3258
PM
3436/**
3437 * security_setprocattr() - Set an attribute for a task
3438 * @lsm: LSM name
3439 * @name: attribute name
3440 * @value: attribute value
3441 * @size: attribute value size
3442 *
3443 * Write (set) the current task's attribute @name to @value, size @size if
3444 * allowed.
3445 *
3446 * Return: Returns bytes written on success, a negative value otherwise.
3447 */
6d9c939d
CS
3448int security_setprocattr(const char *lsm, const char *name, void *value,
3449 size_t size)
20510f2f 3450{
6d9c939d
CS
3451 struct security_hook_list *hp;
3452
3453 hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
3454 if (lsm != NULL && strcmp(lsm, hp->lsm))
3455 continue;
3456 return hp->hook.setprocattr(name, value, size);
3457 }
98e828a0 3458 return LSM_RET_DEFAULT(setprocattr);
20510f2f
JM
3459}
3460
2bcf51bf
PM
3461/**
3462 * security_netlink_send() - Save info and check if netlink sending is allowed
3463 * @sk: sending socket
3464 * @skb: netlink message
3465 *
3466 * Save security information for a netlink message so that permission checking
3467 * can be performed when the message is processed. The security information
3468 * can be saved using the eff_cap field of the netlink_skb_parms structure.
3469 * Also may be used to provide fine grained control over message transmission.
3470 *
3471 * Return: Returns 0 if the information was successfully saved and message is
3472 * allowed to be transmitted.
3473 */
20510f2f
JM
3474int security_netlink_send(struct sock *sk, struct sk_buff *skb)
3475{
f25fce3e 3476 return call_int_hook(netlink_send, 0, sk, skb);
20510f2f 3477}
20510f2f 3478
746df9b5
DQ
3479int security_ismaclabel(const char *name)
3480{
f25fce3e 3481 return call_int_hook(ismaclabel, 0, name);
746df9b5
DQ
3482}
3483EXPORT_SYMBOL(security_ismaclabel);
3484
20510f2f
JM
3485int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3486{
0550cfe8
KS
3487 struct security_hook_list *hp;
3488 int rc;
3489
3490 /*
3491 * Currently, only one LSM can implement secid_to_secctx (i.e this
3492 * LSM hook is not "stackable").
3493 */
3494 hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
3495 rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
3496 if (rc != LSM_RET_DEFAULT(secid_to_secctx))
3497 return rc;
3498 }
3499
3500 return LSM_RET_DEFAULT(secid_to_secctx);
20510f2f
JM
3501}
3502EXPORT_SYMBOL(security_secid_to_secctx);
3503
7bf570dc 3504int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
63cb3449 3505{
b1d9e6b0 3506 *secid = 0;
f25fce3e 3507 return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
63cb3449
DH
3508}
3509EXPORT_SYMBOL(security_secctx_to_secid);
3510
20510f2f
JM
3511void security_release_secctx(char *secdata, u32 seclen)
3512{
f25fce3e 3513 call_void_hook(release_secctx, secdata, seclen);
20510f2f
JM
3514}
3515EXPORT_SYMBOL(security_release_secctx);
3516
6f3be9f5
AG
3517void security_inode_invalidate_secctx(struct inode *inode)
3518{
3519 call_void_hook(inode_invalidate_secctx, inode);
3520}
3521EXPORT_SYMBOL(security_inode_invalidate_secctx);
3522
1ee65e37
DQ
3523int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3524{
f25fce3e 3525 return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
1ee65e37
DQ
3526}
3527EXPORT_SYMBOL(security_inode_notifysecctx);
3528
3529int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3530{
f25fce3e 3531 return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
1ee65e37
DQ
3532}
3533EXPORT_SYMBOL(security_inode_setsecctx);
3534
3535int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3536{
b1d9e6b0 3537 return call_int_hook(inode_getsecctx, -EOPNOTSUPP, inode, ctx, ctxlen);
1ee65e37
DQ
3538}
3539EXPORT_SYMBOL(security_inode_getsecctx);
3540
344fa64e
DH
3541#ifdef CONFIG_WATCH_QUEUE
3542int security_post_notification(const struct cred *w_cred,
3543 const struct cred *cred,
3544 struct watch_notification *n)
3545{
3546 return call_int_hook(post_notification, 0, w_cred, cred, n);
3547}
3548#endif /* CONFIG_WATCH_QUEUE */
3549
998f5040
DH
3550#ifdef CONFIG_KEY_NOTIFICATIONS
3551int security_watch_key(struct key *key)
3552{
3553 return call_int_hook(watch_key, 0, key);
3554}
3555#endif
3556
20510f2f 3557#ifdef CONFIG_SECURITY_NETWORK
2c2442fd
PM
3558/**
3559 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
3560 * @sock: originating sock
3561 * @other: peer sock
3562 * @newsk: new sock
3563 *
3564 * Check permissions before establishing a Unix domain stream connection
3565 * between @sock and @other.
3566 *
3567 * The @unix_stream_connect and @unix_may_send hooks were necessary because
3568 * Linux provides an alternative to the conventional file name space for Unix
3569 * domain sockets. Whereas binding and connecting to sockets in the file name
3570 * space is mediated by the typical file permissions (and caught by the mknod
3571 * and permission hooks in inode_security_ops), binding and connecting to
3572 * sockets in the abstract name space is completely unmediated. Sufficient
3573 * control of Unix domain sockets in the abstract name space isn't possible
3574 * using only the socket layer hooks, since we need to know the actual target
3575 * socket, which is not looked up until we are inside the af_unix code.
3576 *
3577 * Return: Returns 0 if permission is granted.
3578 */
3610cda5 3579int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
20510f2f 3580{
f25fce3e 3581 return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
20510f2f
JM
3582}
3583EXPORT_SYMBOL(security_unix_stream_connect);
3584
2c2442fd
PM
3585/**
3586 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
3587 * @sock: originating sock
3588 * @other: peer sock
3589 *
3590 * Check permissions before connecting or sending datagrams from @sock to
3591 * @other.
3592 *
3593 * The @unix_stream_connect and @unix_may_send hooks were necessary because
3594 * Linux provides an alternative to the conventional file name space for Unix
3595 * domain sockets. Whereas binding and connecting to sockets in the file name
3596 * space is mediated by the typical file permissions (and caught by the mknod
3597 * and permission hooks in inode_security_ops), binding and connecting to
3598 * sockets in the abstract name space is completely unmediated. Sufficient
3599 * control of Unix domain sockets in the abstract name space isn't possible
3600 * using only the socket layer hooks, since we need to know the actual target
3601 * socket, which is not looked up until we are inside the af_unix code.
3602 *
3603 * Return: Returns 0 if permission is granted.
3604 */
20510f2f
JM
3605int security_unix_may_send(struct socket *sock, struct socket *other)
3606{
f25fce3e 3607 return call_int_hook(unix_may_send, 0, sock, other);
20510f2f
JM
3608}
3609EXPORT_SYMBOL(security_unix_may_send);
3610
6b6bbe8c
PM
3611/**
3612 * security_socket_create() - Check if creating a new socket is allowed
3613 * @family: protocol family
3614 * @type: communications type
3615 * @protocol: requested protocol
3616 * @kern: set to 1 if a kernel socket is requested
3617 *
3618 * Check permissions prior to creating a new socket.
3619 *
3620 * Return: Returns 0 if permission is granted.
3621 */
20510f2f
JM
3622int security_socket_create(int family, int type, int protocol, int kern)
3623{
f25fce3e 3624 return call_int_hook(socket_create, 0, family, type, protocol, kern);
20510f2f
JM
3625}
3626
6b6bbe8c
PM
3627/**
3628 * security_socket_create() - Initialize a newly created socket
3629 * @sock: socket
3630 * @family: protocol family
3631 * @type: communications type
3632 * @protocol: requested protocol
3633 * @kern: set to 1 if a kernel socket is requested
3634 *
3635 * This hook allows a module to update or allocate a per-socket security
3636 * structure. Note that the security field was not added directly to the socket
3637 * structure, but rather, the socket security information is stored in the
3638 * associated inode. Typically, the inode alloc_security hook will allocate
3639 * and attach security information to SOCK_INODE(sock)->i_security. This hook
3640 * may be used to update the SOCK_INODE(sock)->i_security field with additional
3641 * information that wasn't available when the inode was allocated.
3642 *
3643 * Return: Returns 0 if permission is granted.
3644 */
20510f2f
JM
3645int security_socket_post_create(struct socket *sock, int family,
3646 int type, int protocol, int kern)
3647{
f25fce3e 3648 return call_int_hook(socket_post_create, 0, sock, family, type,
20510f2f
JM
3649 protocol, kern);
3650}
3651
6b6bbe8c
PM
3652/**
3653 * security_socket_socketpair() - Check if creating a socketpair is allowed
3654 * @socka: first socket
3655 * @sockb: second socket
3656 *
3657 * Check permissions before creating a fresh pair of sockets.
3658 *
3659 * Return: Returns 0 if permission is granted and the connection was
3660 * established.
3661 */
aae7cfcb
DH
3662int security_socket_socketpair(struct socket *socka, struct socket *sockb)
3663{
3664 return call_int_hook(socket_socketpair, 0, socka, sockb);
3665}
3666EXPORT_SYMBOL(security_socket_socketpair);
3667
6b6bbe8c
PM
3668/**
3669 * security_socket_bind() - Check if a socket bind operation is allowed
3670 * @sock: socket
3671 * @address: requested bind address
3672 * @addrlen: length of address
3673 *
3674 * Check permission before socket protocol layer bind operation is performed
3675 * and the socket @sock is bound to the address specified in the @address
3676 * parameter.
3677 *
3678 * Return: Returns 0 if permission is granted.
3679 */
20510f2f
JM
3680int security_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3681{
f25fce3e 3682 return call_int_hook(socket_bind, 0, sock, address, addrlen);
20510f2f
JM
3683}
3684
6b6bbe8c
PM
3685/**
3686 * security_socket_connect() - Check if a socket connect operation is allowed
3687 * @sock: socket
3688 * @address: address of remote connection point
3689 * @addrlen: length of address
3690 *
3691 * Check permission before socket protocol layer connect operation attempts to
3692 * connect socket @sock to a remote address, @address.
3693 *
3694 * Return: Returns 0 if permission is granted.
3695 */
20510f2f
JM
3696int security_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3697{
f25fce3e 3698 return call_int_hook(socket_connect, 0, sock, address, addrlen);
20510f2f
JM
3699}
3700
6b6bbe8c
PM
3701/**
3702 * security_socket_listen() - Check if a socket is allowed to listen
3703 * @sock: socket
3704 * @backlog: connection queue size
3705 *
3706 * Check permission before socket protocol layer listen operation.
3707 *
3708 * Return: Returns 0 if permission is granted.
3709 */
20510f2f
JM
3710int security_socket_listen(struct socket *sock, int backlog)
3711{
f25fce3e 3712 return call_int_hook(socket_listen, 0, sock, backlog);
20510f2f
JM
3713}
3714
6b6bbe8c
PM
3715/**
3716 * security_socket_accept() - Check if a socket is allowed to accept connections
3717 * @sock: listening socket
3718 * @newsock: newly creation connection socket
3719 *
3720 * Check permission before accepting a new connection. Note that the new
3721 * socket, @newsock, has been created and some information copied to it, but
3722 * the accept operation has not actually been performed.
3723 *
3724 * Return: Returns 0 if permission is granted.
3725 */
20510f2f
JM
3726int security_socket_accept(struct socket *sock, struct socket *newsock)
3727{
f25fce3e 3728 return call_int_hook(socket_accept, 0, sock, newsock);
20510f2f
JM
3729}
3730
6b6bbe8c
PM
3731/**
3732 * security_socket_sendmsg() - Check is sending a message is allowed
3733 * @sock: sending socket
3734 * @msg: message to send
3735 * @size: size of message
3736 *
3737 * Check permission before transmitting a message to another socket.
3738 *
3739 * Return: Returns 0 if permission is granted.
3740 */
20510f2f
JM
3741int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
3742{
f25fce3e 3743 return call_int_hook(socket_sendmsg, 0, sock, msg, size);
20510f2f
JM
3744}
3745
6b6bbe8c
PM
3746/**
3747 * security_socket_recvmsg() - Check if receiving a message is allowed
3748 * @sock: receiving socket
3749 * @msg: message to receive
3750 * @size: size of message
3751 * @flags: operational flags
3752 *
3753 * Check permission before receiving a message from a socket.
3754 *
3755 * Return: Returns 0 if permission is granted.
3756 */
20510f2f
JM
3757int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
3758 int size, int flags)
3759{
f25fce3e 3760 return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
20510f2f
JM
3761}
3762
6b6bbe8c
PM
3763/**
3764 * security_socket_getsockname() - Check if reading the socket addr is allowed
3765 * @sock: socket
3766 *
3767 * Check permission before reading the local address (name) of the socket
3768 * object.
3769 *
3770 * Return: Returns 0 if permission is granted.
3771 */
20510f2f
JM
3772int security_socket_getsockname(struct socket *sock)
3773{
f25fce3e 3774 return call_int_hook(socket_getsockname, 0, sock);
20510f2f
JM
3775}
3776
6b6bbe8c
PM
3777/**
3778 * security_socket_getpeername() - Check if reading the peer's addr is allowed
3779 * @sock: socket
3780 *
3781 * Check permission before the remote address (name) of a socket object.
3782 *
3783 * Return: Returns 0 if permission is granted.
3784 */
20510f2f
JM
3785int security_socket_getpeername(struct socket *sock)
3786{
f25fce3e 3787 return call_int_hook(socket_getpeername, 0, sock);
20510f2f
JM
3788}
3789
6b6bbe8c
PM
3790/**
3791 * security_socket_getsockopt() - Check if reading a socket option is allowed
3792 * @sock: socket
3793 * @level: option's protocol level
3794 * @optname: option name
3795 *
3796 * Check permissions before retrieving the options associated with socket
3797 * @sock.
3798 *
3799 * Return: Returns 0 if permission is granted.
3800 */
20510f2f
JM
3801int security_socket_getsockopt(struct socket *sock, int level, int optname)
3802{
f25fce3e 3803 return call_int_hook(socket_getsockopt, 0, sock, level, optname);
20510f2f
JM
3804}
3805
6b6bbe8c
PM
3806/**
3807 * security_socket_setsockopt() - Check if setting a socket option is allowed
3808 * @sock: socket
3809 * @level: option's protocol level
3810 * @optname: option name
3811 *
3812 * Check permissions before setting the options associated with socket @sock.
3813 *
3814 * Return: Returns 0 if permission is granted.
3815 */
20510f2f
JM
3816int security_socket_setsockopt(struct socket *sock, int level, int optname)
3817{
f25fce3e 3818 return call_int_hook(socket_setsockopt, 0, sock, level, optname);
20510f2f
JM
3819}
3820
6b6bbe8c
PM
3821/**
3822 * security_socket_shutdown() - Checks if shutting down the socket is allowed
3823 * @sock: socket
3824 * @how: flag indicating how sends and receives are handled
3825 *
3826 * Checks permission before all or part of a connection on the socket @sock is
3827 * shut down.
3828 *
3829 * Return: Returns 0 if permission is granted.
3830 */
20510f2f
JM
3831int security_socket_shutdown(struct socket *sock, int how)
3832{
f25fce3e 3833 return call_int_hook(socket_shutdown, 0, sock, how);
20510f2f
JM
3834}
3835
6b6bbe8c
PM
3836/**
3837 * security_sock_rcv_skb() - Check if an incoming network packet is allowed
3838 * @sk: destination sock
3839 * @skb: incoming packet
3840 *
3841 * Check permissions on incoming network packets. This hook is distinct from
3842 * Netfilter's IP input hooks since it is the first time that the incoming
3843 * sk_buff @skb has been associated with a particular socket, @sk. Must not
3844 * sleep inside this hook because some callers hold spinlocks.
3845 *
3846 * Return: Returns 0 if permission is granted.
3847 */
20510f2f
JM
3848int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3849{
f25fce3e 3850 return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
20510f2f
JM
3851}
3852EXPORT_SYMBOL(security_sock_rcv_skb);
3853
6b6bbe8c
PM
3854/**
3855 * security_socket_getpeersec_stream() - Get the remote peer label
3856 * @sock: socket
3857 * @optval: destination buffer
3858 * @optlen: size of peer label copied into the buffer
3859 * @len: maximum size of the destination buffer
3860 *
3861 * This hook allows the security module to provide peer socket security state
3862 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
3863 * For tcp sockets this can be meaningful if the socket is associated with an
3864 * ipsec SA.
3865 *
3866 * Return: Returns 0 if all is well, otherwise, typical getsockopt return
3867 * values.
3868 */
b10b9c34
PM
3869int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
3870 sockptr_t optlen, unsigned int len)
20510f2f 3871{
b1d9e6b0 3872 return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
b10b9c34 3873 optval, optlen, len);
20510f2f
JM
3874}
3875
6b6bbe8c
PM
3876/**
3877 * security_socket_getpeersec_dgram() - Get the remote peer label
3878 * @sock: socket
3879 * @skb: datagram packet
3880 * @secid: remote peer label secid
3881 *
3882 * This hook allows the security module to provide peer socket security state
3883 * for udp sockets on a per-packet basis to userspace via getsockopt
3884 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
3885 * option via getsockopt. It can then retrieve the security state returned by
3886 * this hook for a packet via the SCM_SECURITY ancillary message type.
3887 *
3888 * Return: Returns 0 on success, error on failure.
3889 */
20510f2f
JM
3890int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
3891{
e308fd3b
JB
3892 return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
3893 skb, secid);
20510f2f
JM
3894}
3895EXPORT_SYMBOL(security_socket_getpeersec_dgram);
3896
6b6bbe8c
PM
3897/**
3898 * security_sk_alloc() - Allocate and initialize a sock's LSM blob
3899 * @sk: sock
3900 * @family: protocol family
3901 * @priotity: gfp flags
3902 *
3903 * Allocate and attach a security structure to the sk->sk_security field, which
3904 * is used to copy security attributes between local stream sockets.
3905 *
3906 * Return: Returns 0 on success, error on failure.
3907 */
20510f2f
JM
3908int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
3909{
f25fce3e 3910 return call_int_hook(sk_alloc_security, 0, sk, family, priority);
20510f2f
JM
3911}
3912
6b6bbe8c
PM
3913/**
3914 * security_sk_free() - Free the sock's LSM blob
3915 * @sk: sock
3916 *
3917 * Deallocate security structure.
3918 */
20510f2f
JM
3919void security_sk_free(struct sock *sk)
3920{
f25fce3e 3921 call_void_hook(sk_free_security, sk);
20510f2f
JM
3922}
3923
6b6bbe8c
PM
3924/**
3925 * security_sk_clone() - Clone a sock's LSM state
3926 * @sk: original sock
3927 * @newsk: target sock
3928 *
3929 * Clone/copy security structure.
3930 */
20510f2f
JM
3931void security_sk_clone(const struct sock *sk, struct sock *newsk)
3932{
f25fce3e 3933 call_void_hook(sk_clone_security, sk, newsk);
20510f2f 3934}
6230c9b4 3935EXPORT_SYMBOL(security_sk_clone);
20510f2f 3936
3df98d79 3937void security_sk_classify_flow(struct sock *sk, struct flowi_common *flic)
20510f2f 3938{
3df98d79 3939 call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
20510f2f
JM
3940}
3941EXPORT_SYMBOL(security_sk_classify_flow);
3942
6b6bbe8c
PM
3943/**
3944 * security_req_classify_flow() - Set a flow's secid based on request_sock
3945 * @req: request_sock
3946 * @flic: target flow
3947 *
3948 * Sets @flic's secid to @req's secid.
3949 */
3df98d79
PM
3950void security_req_classify_flow(const struct request_sock *req,
3951 struct flowi_common *flic)
20510f2f 3952{
3df98d79 3953 call_void_hook(req_classify_flow, req, flic);
20510f2f
JM
3954}
3955EXPORT_SYMBOL(security_req_classify_flow);
3956
6b6bbe8c
PM
3957/**
3958 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
3959 * @sk: sock being grafted
3960 * @sock: target socket
3961 *
3962 * Sets @sock's inode secid to @sk's secid and update @sk with any necessary
3963 * LSM state from @sock.
3964 */
20510f2f
JM
3965void security_sock_graft(struct sock *sk, struct socket *parent)
3966{
f25fce3e 3967 call_void_hook(sock_graft, sk, parent);
20510f2f
JM
3968}
3969EXPORT_SYMBOL(security_sock_graft);
3970
6b6bbe8c
PM
3971/**
3972 * security_inet_conn_request() - Set request_sock state using incoming connect
3973 * @sk: parent listening sock
3974 * @skb: incoming connection
3975 * @req: new request_sock
3976 *
3977 * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
3978 *
3979 * Return: Returns 0 if permission is granted.
3980 */
41dd9596 3981int security_inet_conn_request(const struct sock *sk,
20510f2f
JM
3982 struct sk_buff *skb, struct request_sock *req)
3983{
f25fce3e 3984 return call_int_hook(inet_conn_request, 0, sk, skb, req);
20510f2f
JM
3985}
3986EXPORT_SYMBOL(security_inet_conn_request);
3987
6b6bbe8c
PM
3988/**
3989 * security_inet_csk_clone() - Set new sock LSM state based on request_sock
3990 * @newsk: new sock
3991 * @req: connection request_sock
3992 *
3993 * Set that LSM state of @sock using the LSM state from @req.
3994 */
20510f2f
JM
3995void security_inet_csk_clone(struct sock *newsk,
3996 const struct request_sock *req)
3997{
f25fce3e 3998 call_void_hook(inet_csk_clone, newsk, req);
20510f2f
JM
3999}
4000
6b6bbe8c
PM
4001/**
4002 * security_inet_conn_established() - Update sock's LSM state with connection
4003 * @sk: sock
4004 * @skb: connection packet
4005 *
4006 * Update @sock's LSM state to represent a new connection from @skb.
4007 */
20510f2f
JM
4008void security_inet_conn_established(struct sock *sk,
4009 struct sk_buff *skb)
4010{
f25fce3e 4011 call_void_hook(inet_conn_established, sk, skb);
20510f2f 4012}
72e89f50 4013EXPORT_SYMBOL(security_inet_conn_established);
20510f2f 4014
6b6bbe8c
PM
4015/**
4016 * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4017 * @secid: new secmark value
4018 *
4019 * Check if the process should be allowed to relabel packets to @secid.
4020 *
4021 * Return: Returns 0 if permission is granted.
4022 */
2606fd1f
EP
4023int security_secmark_relabel_packet(u32 secid)
4024{
f25fce3e 4025 return call_int_hook(secmark_relabel_packet, 0, secid);
2606fd1f
EP
4026}
4027EXPORT_SYMBOL(security_secmark_relabel_packet);
4028
6b6bbe8c
PM
4029/**
4030 * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4031 *
4032 * Tells the LSM to increment the number of secmark labeling rules loaded.
4033 */
2606fd1f
EP
4034void security_secmark_refcount_inc(void)
4035{
f25fce3e 4036 call_void_hook(secmark_refcount_inc);
2606fd1f
EP
4037}
4038EXPORT_SYMBOL(security_secmark_refcount_inc);
4039
6b6bbe8c
PM
4040/**
4041 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4042 *
4043 * Tells the LSM to decrement the number of secmark labeling rules loaded.
4044 */
2606fd1f
EP
4045void security_secmark_refcount_dec(void)
4046{
f25fce3e 4047 call_void_hook(secmark_refcount_dec);
2606fd1f
EP
4048}
4049EXPORT_SYMBOL(security_secmark_refcount_dec);
4050
6b6bbe8c
PM
4051/**
4052 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4053 * @security: pointer to the LSM blob
4054 *
4055 * This hook allows a module to allocate a security structure for a TUN device,
4056 * returning the pointer in @security.
4057 *
4058 * Return: Returns a zero on success, negative values on failure.
4059 */
5dbbaf2d
PM
4060int security_tun_dev_alloc_security(void **security)
4061{
f25fce3e 4062 return call_int_hook(tun_dev_alloc_security, 0, security);
5dbbaf2d
PM
4063}
4064EXPORT_SYMBOL(security_tun_dev_alloc_security);
4065
6b6bbe8c
PM
4066/**
4067 * security_tun_dev_free_security() - Free a TUN device LSM blob
4068 * @security: LSM blob
4069 *
4070 * This hook allows a module to free the security structure for a TUN device.
4071 */
5dbbaf2d
PM
4072void security_tun_dev_free_security(void *security)
4073{
f25fce3e 4074 call_void_hook(tun_dev_free_security, security);
5dbbaf2d
PM
4075}
4076EXPORT_SYMBOL(security_tun_dev_free_security);
4077
6b6bbe8c
PM
4078/**
4079 * security_tun_dev_create() - Check if creating a TUN device is allowed
4080 *
4081 * Check permissions prior to creating a new TUN device.
4082 *
4083 * Return: Returns 0 if permission is granted.
4084 */
2b980dbd
PM
4085int security_tun_dev_create(void)
4086{
f25fce3e 4087 return call_int_hook(tun_dev_create, 0);
2b980dbd
PM
4088}
4089EXPORT_SYMBOL(security_tun_dev_create);
4090
6b6bbe8c
PM
4091/**
4092 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4093 * @security: TUN device LSM blob
4094 *
4095 * Check permissions prior to attaching to a TUN device queue.
4096 *
4097 * Return: Returns 0 if permission is granted.
4098 */
5dbbaf2d 4099int security_tun_dev_attach_queue(void *security)
2b980dbd 4100{
f25fce3e 4101 return call_int_hook(tun_dev_attach_queue, 0, security);
2b980dbd 4102}
5dbbaf2d 4103EXPORT_SYMBOL(security_tun_dev_attach_queue);
2b980dbd 4104
6b6bbe8c
PM
4105/**
4106 * security_tun_dev_attach() - Update TUN device LSM state on attach
4107 * @sk: associated sock
4108 * @security: TUN device LSM blob
4109 *
4110 * This hook can be used by the module to update any security state associated
4111 * with the TUN device's sock structure.
4112 *
4113 * Return: Returns 0 if permission is granted.
4114 */
5dbbaf2d 4115int security_tun_dev_attach(struct sock *sk, void *security)
2b980dbd 4116{
f25fce3e 4117 return call_int_hook(tun_dev_attach, 0, sk, security);
2b980dbd
PM
4118}
4119EXPORT_SYMBOL(security_tun_dev_attach);
4120
6b6bbe8c
PM
4121/**
4122 * security_tun_dev_open() - Update TUN device LSM state on open
4123 * @security: TUN device LSM blob
4124 *
4125 * This hook can be used by the module to update any security state associated
4126 * with the TUN device's security structure.
4127 *
4128 * Return: Returns 0 if permission is granted.
4129 */
5dbbaf2d
PM
4130int security_tun_dev_open(void *security)
4131{
f25fce3e 4132 return call_int_hook(tun_dev_open, 0, security);
5dbbaf2d
PM
4133}
4134EXPORT_SYMBOL(security_tun_dev_open);
4135
4a49f592
PM
4136/**
4137 * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4138 * @asoc: SCTP association
4139 * @skb: packet requesting the association
4140 *
4141 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4142 *
4143 * Return: Returns 0 on success, error on failure.
4144 */
c081d53f 4145int security_sctp_assoc_request(struct sctp_association *asoc, struct sk_buff *skb)
72e89f50 4146{
c081d53f 4147 return call_int_hook(sctp_assoc_request, 0, asoc, skb);
72e89f50
RH
4148}
4149EXPORT_SYMBOL(security_sctp_assoc_request);
4150
4a49f592
PM
4151/**
4152 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4153 * @sk: socket
4154 * @optname: SCTP option to validate
4155 * @address: list of IP addresses to validate
4156 * @addrlen: length of the address list
4157 *
4158 * Validiate permissions required for each address associated with sock @sk.
4159 * Depending on @optname, the addresses will be treated as either a connect or
4160 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4161 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4162 *
4163 * Return: Returns 0 on success, error on failure.
4164 */
72e89f50
RH
4165int security_sctp_bind_connect(struct sock *sk, int optname,
4166 struct sockaddr *address, int addrlen)
4167{
4168 return call_int_hook(sctp_bind_connect, 0, sk, optname,
4169 address, addrlen);
4170}
4171EXPORT_SYMBOL(security_sctp_bind_connect);
4172
4a49f592
PM
4173/**
4174 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4175 * @asoc: SCTP association
4176 * @sk: original sock
4177 * @newsk: target sock
4178 *
4179 * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4180 * socket) or when a socket is 'peeled off' e.g userspace calls
4181 * sctp_peeloff(3).
4182 */
c081d53f 4183void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
72e89f50
RH
4184 struct sock *newsk)
4185{
c081d53f 4186 call_void_hook(sctp_sk_clone, asoc, sk, newsk);
72e89f50
RH
4187}
4188EXPORT_SYMBOL(security_sctp_sk_clone);
4189
4a49f592
PM
4190/**
4191 * security_sctp_assoc_established() - Update LSM state when assoc established
4192 * @asoc: SCTP association
4193 * @skb: packet establishing the association
4194 *
4195 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4196 * security module.
4197 *
4198 * Return: Returns 0 if permission is granted.
4199 */
5e50f5d4
OM
4200int security_sctp_assoc_established(struct sctp_association *asoc,
4201 struct sk_buff *skb)
4202{
4203 return call_int_hook(sctp_assoc_established, 0, asoc, skb);
4204}
4205EXPORT_SYMBOL(security_sctp_assoc_established);
4206
20510f2f
JM
4207#endif /* CONFIG_SECURITY_NETWORK */
4208
d291f1a6
DJ
4209#ifdef CONFIG_SECURITY_INFINIBAND
4210
4211int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
4212{
4213 return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
4214}
4215EXPORT_SYMBOL(security_ib_pkey_access);
4216
47a2b338
DJ
4217int security_ib_endport_manage_subnet(void *sec, const char *dev_name, u8 port_num)
4218{
4219 return call_int_hook(ib_endport_manage_subnet, 0, sec, dev_name, port_num);
4220}
4221EXPORT_SYMBOL(security_ib_endport_manage_subnet);
4222
d291f1a6
DJ
4223int security_ib_alloc_security(void **sec)
4224{
4225 return call_int_hook(ib_alloc_security, 0, sec);
4226}
4227EXPORT_SYMBOL(security_ib_alloc_security);
4228
4229void security_ib_free_security(void *sec)
4230{
4231 call_void_hook(ib_free_security, sec);
4232}
4233EXPORT_SYMBOL(security_ib_free_security);
4234#endif /* CONFIG_SECURITY_INFINIBAND */
4235
20510f2f
JM
4236#ifdef CONFIG_SECURITY_NETWORK_XFRM
4237
52a4c640
NA
4238int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
4239 struct xfrm_user_sec_ctx *sec_ctx,
4240 gfp_t gfp)
20510f2f 4241{
f25fce3e 4242 return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
20510f2f
JM
4243}
4244EXPORT_SYMBOL(security_xfrm_policy_alloc);
4245
03e1ad7b
PM
4246int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
4247 struct xfrm_sec_ctx **new_ctxp)
20510f2f 4248{
f25fce3e 4249 return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
20510f2f
JM
4250}
4251
03e1ad7b 4252void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
20510f2f 4253{
f25fce3e 4254 call_void_hook(xfrm_policy_free_security, ctx);
20510f2f
JM
4255}
4256EXPORT_SYMBOL(security_xfrm_policy_free);
4257
03e1ad7b 4258int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
20510f2f 4259{
f25fce3e 4260 return call_int_hook(xfrm_policy_delete_security, 0, ctx);
20510f2f
JM
4261}
4262
2e5aa866
PM
4263int security_xfrm_state_alloc(struct xfrm_state *x,
4264 struct xfrm_user_sec_ctx *sec_ctx)
20510f2f 4265{
f25fce3e 4266 return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
20510f2f
JM
4267}
4268EXPORT_SYMBOL(security_xfrm_state_alloc);
4269
4270int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
4271 struct xfrm_sec_ctx *polsec, u32 secid)
4272{
f25fce3e 4273 return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
20510f2f
JM
4274}
4275
4276int security_xfrm_state_delete(struct xfrm_state *x)
4277{
f25fce3e 4278 return call_int_hook(xfrm_state_delete_security, 0, x);
20510f2f
JM
4279}
4280EXPORT_SYMBOL(security_xfrm_state_delete);
4281
4282void security_xfrm_state_free(struct xfrm_state *x)
4283{
f25fce3e 4284 call_void_hook(xfrm_state_free_security, x);
20510f2f
JM
4285}
4286
8a922805 4287int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
20510f2f 4288{
8a922805 4289 return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
20510f2f
JM
4290}
4291
4292int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
e33f7704 4293 struct xfrm_policy *xp,
3df98d79 4294 const struct flowi_common *flic)
20510f2f 4295{
b1d9e6b0 4296 struct security_hook_list *hp;
98e828a0 4297 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
b1d9e6b0
CS
4298
4299 /*
4300 * Since this function is expected to return 0 or 1, the judgment
4301 * becomes difficult if multiple LSMs supply this call. Fortunately,
4302 * we can use the first LSM's judgment because currently only SELinux
4303 * supplies this call.
4304 *
4305 * For speed optimization, we explicitly break the loop rather than
4306 * using the macro
4307 */
df0ce173 4308 hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
b1d9e6b0 4309 list) {
3df98d79 4310 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
b1d9e6b0
CS
4311 break;
4312 }
4313 return rc;
20510f2f
JM
4314}
4315
4316int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
4317{
f25fce3e 4318 return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
20510f2f
JM
4319}
4320
3df98d79 4321void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
20510f2f 4322{
3df98d79 4323 int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
f25fce3e 4324 0);
20510f2f
JM
4325
4326 BUG_ON(rc);
4327}
4328EXPORT_SYMBOL(security_skb_classify_flow);
4329
4330#endif /* CONFIG_SECURITY_NETWORK_XFRM */
4331
4332#ifdef CONFIG_KEYS
4333
d84f4f99
DH
4334int security_key_alloc(struct key *key, const struct cred *cred,
4335 unsigned long flags)
20510f2f 4336{
f25fce3e 4337 return call_int_hook(key_alloc, 0, key, cred, flags);
20510f2f
JM
4338}
4339
4340void security_key_free(struct key *key)
4341{
f25fce3e 4342 call_void_hook(key_free, key);
20510f2f
JM
4343}
4344
8c0637e9
DH
4345int security_key_permission(key_ref_t key_ref, const struct cred *cred,
4346 enum key_need_perm need_perm)
20510f2f 4347{
8c0637e9 4348 return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
20510f2f
JM
4349}
4350
70a5bb72
DH
4351int security_key_getsecurity(struct key *key, char **_buffer)
4352{
b1d9e6b0 4353 *_buffer = NULL;
f25fce3e 4354 return call_int_hook(key_getsecurity, 0, key, _buffer);
70a5bb72
DH
4355}
4356
20510f2f 4357#endif /* CONFIG_KEYS */
03d37d25
AD
4358
4359#ifdef CONFIG_AUDIT
4360
4361int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
4362{
f25fce3e 4363 return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule);
03d37d25
AD
4364}
4365
4366int security_audit_rule_known(struct audit_krule *krule)
4367{
f25fce3e 4368 return call_int_hook(audit_rule_known, 0, krule);
03d37d25
AD
4369}
4370
4371void security_audit_rule_free(void *lsmrule)
4372{
f25fce3e 4373 call_void_hook(audit_rule_free, lsmrule);
03d37d25
AD
4374}
4375
90462a5b 4376int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
03d37d25 4377{
90462a5b 4378 return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
03d37d25 4379}
b1d9e6b0 4380#endif /* CONFIG_AUDIT */
afdb09c7
CF
4381
4382#ifdef CONFIG_BPF_SYSCALL
4383int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
4384{
4385 return call_int_hook(bpf, 0, cmd, attr, size);
4386}
4387int security_bpf_map(struct bpf_map *map, fmode_t fmode)
4388{
4389 return call_int_hook(bpf_map, 0, map, fmode);
4390}
4391int security_bpf_prog(struct bpf_prog *prog)
4392{
4393 return call_int_hook(bpf_prog, 0, prog);
4394}
4395int security_bpf_map_alloc(struct bpf_map *map)
4396{
4397 return call_int_hook(bpf_map_alloc_security, 0, map);
4398}
4399int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
4400{
4401 return call_int_hook(bpf_prog_alloc_security, 0, aux);
4402}
4403void security_bpf_map_free(struct bpf_map *map)
4404{
4405 call_void_hook(bpf_map_free_security, map);
4406}
4407void security_bpf_prog_free(struct bpf_prog_aux *aux)
4408{
4409 call_void_hook(bpf_prog_free_security, aux);
4410}
4411#endif /* CONFIG_BPF_SYSCALL */
9e47d31d
MG
4412
4413int security_locked_down(enum lockdown_reason what)
4414{
4415 return call_int_hook(locked_down, 0, what);
4416}
4417EXPORT_SYMBOL(security_locked_down);
da97e184
JFG
4418
4419#ifdef CONFIG_PERF_EVENTS
4420int security_perf_event_open(struct perf_event_attr *attr, int type)
4421{
4422 return call_int_hook(perf_event_open, 0, attr, type);
4423}
4424
4425int security_perf_event_alloc(struct perf_event *event)
4426{
4427 return call_int_hook(perf_event_alloc, 0, event);
4428}
4429
4430void security_perf_event_free(struct perf_event *event)
4431{
4432 call_void_hook(perf_event_free, event);
4433}
4434
4435int security_perf_event_read(struct perf_event *event)
4436{
4437 return call_int_hook(perf_event_read, 0, event);
4438}
4439
4440int security_perf_event_write(struct perf_event *event)
4441{
4442 return call_int_hook(perf_event_write, 0, event);
4443}
4444#endif /* CONFIG_PERF_EVENTS */
cdc1404a
PM
4445
4446#ifdef CONFIG_IO_URING
4447int security_uring_override_creds(const struct cred *new)
4448{
4449 return call_int_hook(uring_override_creds, 0, new);
4450}
4451
4452int security_uring_sqpoll(void)
4453{
4454 return call_int_hook(uring_sqpoll, 0);
4455}
2a584012
LC
4456int security_uring_cmd(struct io_uring_cmd *ioucmd)
4457{
4458 return call_int_hook(uring_cmd, 0, ioucmd);
4459}
cdc1404a 4460#endif /* CONFIG_IO_URING */