Merge tag 'perf-tools-for-v6.10-1-2024-05-21' of git://git.kernel.org/pub/scm/linux...
[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>
40401530 22#include <linux/fsnotify.h>
8b3ec681
AV
23#include <linux/mman.h>
24#include <linux/mount.h>
25#include <linux/personality.h>
75331a59 26#include <linux/backing-dev.h>
3bb857e4 27#include <linux/string.h>
92383111 28#include <linux/xattr.h>
ecd5f82e 29#include <linux/msg.h>
d8bdd795 30#include <linux/overflow.h>
40401530 31#include <net/flow.h>
1da177e4 32
2d4d5119
KC
33/* How many LSMs were built into the kernel? */
34#define LSM_COUNT (__end_lsm_info - __start_lsm_info)
35
9285c5ad
CS
36/*
37 * How many LSMs are built into the kernel as determined at
38 * build time. Used to determine fixed array sizes.
39 * The capability module is accounted for by CONFIG_SECURITY
40 */
41#define LSM_CONFIG_COUNT ( \
42 (IS_ENABLED(CONFIG_SECURITY) ? 1 : 0) + \
43 (IS_ENABLED(CONFIG_SECURITY_SELINUX) ? 1 : 0) + \
44 (IS_ENABLED(CONFIG_SECURITY_SMACK) ? 1 : 0) + \
45 (IS_ENABLED(CONFIG_SECURITY_TOMOYO) ? 1 : 0) + \
9285c5ad
CS
46 (IS_ENABLED(CONFIG_SECURITY_APPARMOR) ? 1 : 0) + \
47 (IS_ENABLED(CONFIG_SECURITY_YAMA) ? 1 : 0) + \
48 (IS_ENABLED(CONFIG_SECURITY_LOADPIN) ? 1 : 0) + \
49 (IS_ENABLED(CONFIG_SECURITY_SAFESETID) ? 1 : 0) + \
50 (IS_ENABLED(CONFIG_SECURITY_LOCKDOWN_LSM) ? 1 : 0) + \
51 (IS_ENABLED(CONFIG_BPF_LSM) ? 1 : 0) + \
cd3cec0a 52 (IS_ENABLED(CONFIG_SECURITY_LANDLOCK) ? 1 : 0) + \
92383111
RS
53 (IS_ENABLED(CONFIG_IMA) ? 1 : 0) + \
54 (IS_ENABLED(CONFIG_EVM) ? 1 : 0))
9285c5ad 55
59438b46
SS
56/*
57 * These are descriptions of the reasons that can be passed to the
58 * security_locked_down() LSM hook. Placing this array here allows
59 * all security modules to use the same descriptions for auditing
60 * purposes.
61 */
63c1845b 62const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
59438b46
SS
63 [LOCKDOWN_NONE] = "none",
64 [LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
65 [LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
66 [LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
67 [LOCKDOWN_KEXEC] = "kexec of unsigned images",
68 [LOCKDOWN_HIBERNATION] = "hibernation",
69 [LOCKDOWN_PCI_ACCESS] = "direct PCI access",
70 [LOCKDOWN_IOPORT] = "raw io port access",
71 [LOCKDOWN_MSR] = "raw MSR access",
72 [LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
99df7a28 73 [LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
59438b46
SS
74 [LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
75 [LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
76 [LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
77 [LOCKDOWN_MMIOTRACE] = "unsafe mmio",
78 [LOCKDOWN_DEBUGFS] = "debugfs access",
79 [LOCKDOWN_XMON_WR] = "xmon write access",
51e1bb9e 80 [LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
eadb2f47 81 [LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
b8f3e488 82 [LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
59438b46
SS
83 [LOCKDOWN_INTEGRITY_MAX] = "integrity",
84 [LOCKDOWN_KCORE] = "/proc/kcore access",
85 [LOCKDOWN_KPROBES] = "use of kprobes",
71330842 86 [LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
eadb2f47 87 [LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
59438b46
SS
88 [LOCKDOWN_PERF] = "unsafe use of perf",
89 [LOCKDOWN_TRACEFS] = "use of tracefs",
90 [LOCKDOWN_XMON_RW] = "xmon read and write access",
c7a5899e 91 [LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
59438b46
SS
92 [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
93};
94
f22f9aaf 95struct security_hook_heads security_hook_heads __ro_after_init;
42df744c 96static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
8f408ab6 97
33bf60ca 98static struct kmem_cache *lsm_file_cache;
afb1cbe3 99static struct kmem_cache *lsm_inode_cache;
33bf60ca 100
d69dece5 101char *lsm_names;
f22f9aaf 102static struct lsm_blob_sizes blob_sizes __ro_after_init;
bbd3662a 103
076c54c5 104/* Boot-time LSM user choice */
79f7865d 105static __initdata const char *chosen_lsm_order;
5ef4e419 106static __initdata const char *chosen_major_lsm;
1da177e4 107
63c1845b 108static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
13e735c0 109
2d4d5119
KC
110/* Ordered list of LSMs to initialize. */
111static __initdata struct lsm_info **ordered_lsms;
14bd99c8 112static __initdata struct lsm_info *exclusive;
2d4d5119 113
9b8c7c14
KC
114static __initdata bool debug;
115#define init_debug(...) \
116 do { \
117 if (debug) \
118 pr_info(__VA_ARGS__); \
119 } while (0)
120
f4941d75
KC
121static bool __init is_enabled(struct lsm_info *lsm)
122{
a8027fb0
KC
123 if (!lsm->enabled)
124 return false;
f4941d75 125
a8027fb0 126 return *lsm->enabled;
f4941d75
KC
127}
128
129/* Mark an LSM's enabled flag. */
130static int lsm_enabled_true __initdata = 1;
131static int lsm_enabled_false __initdata = 0;
132static void __init set_enabled(struct lsm_info *lsm, bool enabled)
133{
134 /*
135 * When an LSM hasn't configured an enable variable, we can use
136 * a hard-coded location for storing the default enabled state.
137 */
138 if (!lsm->enabled) {
139 if (enabled)
140 lsm->enabled = &lsm_enabled_true;
141 else
142 lsm->enabled = &lsm_enabled_false;
143 } else if (lsm->enabled == &lsm_enabled_true) {
144 if (!enabled)
145 lsm->enabled = &lsm_enabled_false;
146 } else if (lsm->enabled == &lsm_enabled_false) {
147 if (enabled)
148 lsm->enabled = &lsm_enabled_true;
149 } else {
150 *lsm->enabled = enabled;
151 }
152}
153
2d4d5119
KC
154/* Is an LSM already listed in the ordered LSMs list? */
155static bool __init exists_ordered_lsm(struct lsm_info *lsm)
156{
157 struct lsm_info **check;
158
159 for (check = ordered_lsms; *check; check++)
160 if (*check == lsm)
161 return true;
162
163 return false;
164}
165
166/* Append an LSM to the list of ordered LSMs to initialize. */
167static int last_lsm __initdata;
168static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
169{
170 /* Ignore duplicate selections. */
171 if (exists_ordered_lsm(lsm))
172 return;
173
174 if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
175 return;
176
a8027fb0
KC
177 /* Enable this LSM, if it is not already set. */
178 if (!lsm->enabled)
179 lsm->enabled = &lsm_enabled_true;
2d4d5119 180 ordered_lsms[last_lsm++] = lsm;
a8027fb0 181
86ef3c73
KC
182 init_debug("%s ordered: %s (%s)\n", from, lsm->name,
183 is_enabled(lsm) ? "enabled" : "disabled");
2d4d5119
KC
184}
185
f4941d75
KC
186/* Is an LSM allowed to be initialized? */
187static bool __init lsm_allowed(struct lsm_info *lsm)
188{
189 /* Skip if the LSM is disabled. */
190 if (!is_enabled(lsm))
191 return false;
192
14bd99c8
KC
193 /* Not allowed if another exclusive LSM already initialized. */
194 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
195 init_debug("exclusive disabled: %s\n", lsm->name);
196 return false;
197 }
198
f4941d75
KC
199 return true;
200}
201
bbd3662a
CS
202static void __init lsm_set_blob_size(int *need, int *lbs)
203{
204 int offset;
205
b9f5ce27
GN
206 if (*need <= 0)
207 return;
208
209 offset = ALIGN(*lbs, sizeof(void *));
210 *lbs = offset + *need;
211 *need = offset;
bbd3662a
CS
212}
213
214static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
215{
216 if (!needed)
217 return;
218
219 lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
33bf60ca 220 lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
afb1cbe3
CS
221 /*
222 * The inode blob gets an rcu_head in addition to
223 * what the modules might need.
224 */
225 if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
226 blob_sizes.lbs_inode = sizeof(struct rcu_head);
227 lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
ecd5f82e
CS
228 lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
229 lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
1aea7808 230 lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
f4ad8f2c 231 lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
6bcdfd2c
RS
232 lsm_set_blob_size(&needed->lbs_xattr_count,
233 &blob_sizes.lbs_xattr_count);
bbd3662a
CS
234}
235
d8e9bbd4
KC
236/* Prepare LSM for initialization. */
237static void __init prepare_lsm(struct lsm_info *lsm)
f4941d75
KC
238{
239 int enabled = lsm_allowed(lsm);
240
241 /* Record enablement (to handle any following exclusive LSMs). */
242 set_enabled(lsm, enabled);
243
d8e9bbd4 244 /* If enabled, do pre-initialization work. */
f4941d75 245 if (enabled) {
14bd99c8
KC
246 if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
247 exclusive = lsm;
86ef3c73 248 init_debug("exclusive chosen: %s\n", lsm->name);
14bd99c8 249 }
bbd3662a
CS
250
251 lsm_set_blob_sizes(lsm->blobs);
d8e9bbd4
KC
252 }
253}
254
255/* Initialize a given LSM, if it is enabled. */
256static void __init initialize_lsm(struct lsm_info *lsm)
257{
258 if (is_enabled(lsm)) {
259 int ret;
14bd99c8 260
f4941d75
KC
261 init_debug("initializing %s\n", lsm->name);
262 ret = lsm->init();
263 WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
264 }
265}
266
9285c5ad
CS
267/*
268 * Current index to use while initializing the lsm id list.
269 */
270u32 lsm_active_cnt __ro_after_init;
271const struct lsm_id *lsm_idlist[LSM_CONFIG_COUNT];
272
13e735c0 273/* Populate ordered LSMs list from comma-separated LSM name list. */
2d4d5119 274static void __init ordered_lsm_parse(const char *order, const char *origin)
657d910b
KC
275{
276 struct lsm_info *lsm;
13e735c0
KC
277 char *sep, *name, *next;
278
e2bc445b
KC
279 /* LSM_ORDER_FIRST is always first. */
280 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
281 if (lsm->order == LSM_ORDER_FIRST)
86ef3c73 282 append_ordered_lsm(lsm, " first");
e2bc445b
KC
283 }
284
7e611486 285 /* Process "security=", if given. */
7e611486
KC
286 if (chosen_major_lsm) {
287 struct lsm_info *major;
288
289 /*
290 * To match the original "security=" behavior, this
291 * explicitly does NOT fallback to another Legacy Major
292 * if the selected one was separately disabled: disable
293 * all non-matching Legacy Major LSMs.
294 */
295 for (major = __start_lsm_info; major < __end_lsm_info;
296 major++) {
297 if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
298 strcmp(major->name, chosen_major_lsm) != 0) {
299 set_enabled(major, false);
86ef3c73 300 init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
7e611486
KC
301 chosen_major_lsm, major->name);
302 }
303 }
304 }
5ef4e419 305
13e735c0
KC
306 sep = kstrdup(order, GFP_KERNEL);
307 next = sep;
308 /* Walk the list, looking for matching LSMs. */
309 while ((name = strsep(&next, ",")) != NULL) {
310 bool found = false;
311
312 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
42994ee3
RS
313 if (strcmp(lsm->name, name) == 0) {
314 if (lsm->order == LSM_ORDER_MUTABLE)
315 append_ordered_lsm(lsm, origin);
13e735c0
KC
316 found = true;
317 }
318 }
319
320 if (!found)
86ef3c73
KC
321 init_debug("%s ignored: %s (not built into kernel)\n",
322 origin, name);
657d910b 323 }
c91d8106
CS
324
325 /* Process "security=", if given. */
326 if (chosen_major_lsm) {
327 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
328 if (exists_ordered_lsm(lsm))
329 continue;
330 if (strcmp(lsm->name, chosen_major_lsm) == 0)
331 append_ordered_lsm(lsm, "security=");
332 }
333 }
334
42994ee3
RS
335 /* LSM_ORDER_LAST is always last. */
336 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
337 if (lsm->order == LSM_ORDER_LAST)
338 append_ordered_lsm(lsm, " last");
339 }
340
c91d8106
CS
341 /* Disable all LSMs not in the ordered list. */
342 for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
343 if (exists_ordered_lsm(lsm))
344 continue;
345 set_enabled(lsm, false);
86ef3c73
KC
346 init_debug("%s skipped: %s (not in requested order)\n",
347 origin, lsm->name);
c91d8106
CS
348 }
349
13e735c0 350 kfree(sep);
657d910b
KC
351}
352
1cfb2a51
TH
353static void __init lsm_early_cred(struct cred *cred);
354static void __init lsm_early_task(struct task_struct *task);
355
e6b1db98
MG
356static int lsm_append(const char *new, char **result);
357
86ef3c73
KC
358static void __init report_lsm_order(void)
359{
360 struct lsm_info **lsm, *early;
361 int first = 0;
362
363 pr_info("initializing lsm=");
364
365 /* Report each enabled LSM name, comma separated. */
63c1845b
PM
366 for (early = __start_early_lsm_info;
367 early < __end_early_lsm_info; early++)
86ef3c73
KC
368 if (is_enabled(early))
369 pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
370 for (lsm = ordered_lsms; *lsm; lsm++)
371 if (is_enabled(*lsm))
372 pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
373
374 pr_cont("\n");
375}
376
2d4d5119
KC
377static void __init ordered_lsm_init(void)
378{
379 struct lsm_info **lsm;
380
381 ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
63c1845b 382 GFP_KERNEL);
2d4d5119 383
89a9684e
KC
384 if (chosen_lsm_order) {
385 if (chosen_major_lsm) {
86ef3c73
KC
386 pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
387 chosen_major_lsm, chosen_lsm_order);
89a9684e
KC
388 chosen_major_lsm = NULL;
389 }
79f7865d 390 ordered_lsm_parse(chosen_lsm_order, "cmdline");
89a9684e 391 } else
79f7865d 392 ordered_lsm_parse(builtin_lsm_order, "builtin");
2d4d5119
KC
393
394 for (lsm = ordered_lsms; *lsm; lsm++)
d8e9bbd4
KC
395 prepare_lsm(*lsm);
396
86ef3c73
KC
397 report_lsm_order();
398
1aea7808
CS
399 init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
400 init_debug("file blob size = %d\n", blob_sizes.lbs_file);
401 init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
402 init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
403 init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
404 init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
405 init_debug("task blob size = %d\n", blob_sizes.lbs_task);
6bcdfd2c 406 init_debug("xattr slots = %d\n", blob_sizes.lbs_xattr_count);
33bf60ca
CS
407
408 /*
409 * Create any kmem_caches needed for blobs
410 */
411 if (blob_sizes.lbs_file)
412 lsm_file_cache = kmem_cache_create("lsm_file_cache",
413 blob_sizes.lbs_file, 0,
414 SLAB_PANIC, NULL);
afb1cbe3
CS
415 if (blob_sizes.lbs_inode)
416 lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
417 blob_sizes.lbs_inode, 0,
418 SLAB_PANIC, NULL);
bbd3662a 419
1cfb2a51
TH
420 lsm_early_cred((struct cred *) current->cred);
421 lsm_early_task(current);
d8e9bbd4
KC
422 for (lsm = ordered_lsms; *lsm; lsm++)
423 initialize_lsm(*lsm);
2d4d5119
KC
424
425 kfree(ordered_lsms);
426}
427
e6b1db98
MG
428int __init early_security_init(void)
429{
e6b1db98
MG
430 struct lsm_info *lsm;
431
75c1182e
BW
432#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
433 INIT_HLIST_HEAD(&security_hook_heads.NAME);
434#include "linux/lsm_hook_defs.h"
435#undef LSM_HOOK
e6b1db98
MG
436
437 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
438 if (!lsm->enabled)
439 lsm->enabled = &lsm_enabled_true;
440 prepare_lsm(lsm);
441 initialize_lsm(lsm);
442 }
443
444 return 0;
445}
446
1da177e4
LT
447/**
448 * security_init - initializes the security framework
449 *
450 * This should be called early in the kernel initialization sequence.
451 */
452int __init security_init(void)
453{
e6b1db98 454 struct lsm_info *lsm;
3dfc9b02 455
63c1845b 456 init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
86ef3c73 457 init_debug(" CONFIG_LSM=%s\n", builtin_lsm_order);
63c1845b 458 init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
98d29170 459
e6b1db98
MG
460 /*
461 * Append the names of the early LSM modules now that kmalloc() is
462 * available
463 */
464 for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
86ef3c73
KC
465 init_debug(" early started: %s (%s)\n", lsm->name,
466 is_enabled(lsm) ? "enabled" : "disabled");
e6b1db98
MG
467 if (lsm->enabled)
468 lsm_append(lsm->name, &lsm_names);
469 }
1da177e4 470
657d910b
KC
471 /* Load LSMs in specified order. */
472 ordered_lsm_init();
473
1da177e4
LT
474 return 0;
475}
476
076c54c5 477/* Save user chosen LSM */
5ef4e419 478static int __init choose_major_lsm(char *str)
076c54c5 479{
5ef4e419 480 chosen_major_lsm = str;
076c54c5
AD
481 return 1;
482}
5ef4e419 483__setup("security=", choose_major_lsm);
076c54c5 484
79f7865d
KC
485/* Explicitly choose LSM initialization order. */
486static int __init choose_lsm_order(char *str)
487{
488 chosen_lsm_order = str;
489 return 1;
490}
491__setup("lsm=", choose_lsm_order);
492
9b8c7c14
KC
493/* Enable LSM order debugging. */
494static int __init enable_debug(char *str)
495{
496 debug = true;
497 return 1;
498}
499__setup("lsm.debug", enable_debug);
500
3bb857e4
MS
501static bool match_last_lsm(const char *list, const char *lsm)
502{
503 const char *last;
504
505 if (WARN_ON(!list || !lsm))
506 return false;
507 last = strrchr(list, ',');
508 if (last)
509 /* Pass the comma, strcmp() will check for '\0' */
510 last++;
511 else
512 last = list;
513 return !strcmp(last, lsm);
514}
515
e6b1db98 516static int lsm_append(const char *new, char **result)
d69dece5
CS
517{
518 char *cp;
519
520 if (*result == NULL) {
521 *result = kstrdup(new, GFP_KERNEL);
87ea5843
EB
522 if (*result == NULL)
523 return -ENOMEM;
d69dece5 524 } else {
3bb857e4
MS
525 /* Check if it is the last registered name */
526 if (match_last_lsm(*result, new))
527 return 0;
d69dece5
CS
528 cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
529 if (cp == NULL)
530 return -ENOMEM;
531 kfree(*result);
532 *result = cp;
533 }
534 return 0;
535}
536
d69dece5
CS
537/**
538 * security_add_hooks - Add a modules hooks to the hook lists.
539 * @hooks: the hooks to add
540 * @count: the number of hooks to add
f3b8788c 541 * @lsmid: the identification information for the security module
d69dece5
CS
542 *
543 * Each LSM has to register its hooks with the infrastructure.
544 */
545void __init security_add_hooks(struct security_hook_list *hooks, int count,
f3b8788c 546 const struct lsm_id *lsmid)
d69dece5
CS
547{
548 int i;
549
9285c5ad
CS
550 /*
551 * A security module may call security_add_hooks() more
552 * than once during initialization, and LSM initialization
553 * is serialized. Landlock is one such case.
554 * Look at the previous entry, if there is one, for duplication.
555 */
556 if (lsm_active_cnt == 0 || lsm_idlist[lsm_active_cnt - 1] != lsmid) {
557 if (lsm_active_cnt >= LSM_CONFIG_COUNT)
558 panic("%s Too many LSMs registered.\n", __func__);
559 lsm_idlist[lsm_active_cnt++] = lsmid;
560 }
561
d69dece5 562 for (i = 0; i < count; i++) {
f3b8788c 563 hooks[i].lsmid = lsmid;
df0ce173 564 hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
d69dece5 565 }
e6b1db98
MG
566
567 /*
568 * Don't try to append during early_security_init(), we'll come back
569 * and fix this up afterwards.
570 */
571 if (slab_is_available()) {
f3b8788c 572 if (lsm_append(lsmid->name, &lsm_names) < 0)
e6b1db98
MG
573 panic("%s - Cannot get early memory.\n", __func__);
574 }
d69dece5
CS
575}
576
42df744c 577int call_blocking_lsm_notifier(enum lsm_event event, void *data)
8f408ab6 578{
42df744c
JK
579 return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
580 event, data);
8f408ab6 581}
42df744c 582EXPORT_SYMBOL(call_blocking_lsm_notifier);
8f408ab6 583
42df744c 584int register_blocking_lsm_notifier(struct notifier_block *nb)
8f408ab6 585{
42df744c
JK
586 return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
587 nb);
8f408ab6 588}
42df744c 589EXPORT_SYMBOL(register_blocking_lsm_notifier);
8f408ab6 590
42df744c 591int unregister_blocking_lsm_notifier(struct notifier_block *nb)
8f408ab6 592{
42df744c
JK
593 return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
594 nb);
8f408ab6 595}
42df744c 596EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
8f408ab6 597
bbd3662a
CS
598/**
599 * lsm_cred_alloc - allocate a composite cred blob
600 * @cred: the cred that needs a blob
601 * @gfp: allocation type
602 *
603 * Allocate the cred blob for all the modules
604 *
605 * Returns 0, or -ENOMEM if memory can't be allocated.
606 */
607static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
608{
609 if (blob_sizes.lbs_cred == 0) {
610 cred->security = NULL;
611 return 0;
612 }
613
614 cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
615 if (cred->security == NULL)
616 return -ENOMEM;
617 return 0;
618}
619
620/**
621 * lsm_early_cred - during initialization allocate a composite cred blob
622 * @cred: the cred that needs a blob
623 *
1cfb2a51 624 * Allocate the cred blob for all the modules
bbd3662a 625 */
1cfb2a51 626static void __init lsm_early_cred(struct cred *cred)
bbd3662a 627{
1cfb2a51 628 int rc = lsm_cred_alloc(cred, GFP_KERNEL);
bbd3662a 629
bbd3662a
CS
630 if (rc)
631 panic("%s: Early cred alloc failed.\n", __func__);
632}
633
33bf60ca
CS
634/**
635 * lsm_file_alloc - allocate a composite file blob
636 * @file: the file that needs a blob
637 *
638 * Allocate the file blob for all the modules
639 *
640 * Returns 0, or -ENOMEM if memory can't be allocated.
641 */
642static int lsm_file_alloc(struct file *file)
643{
644 if (!lsm_file_cache) {
645 file->f_security = NULL;
646 return 0;
647 }
648
649 file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
650 if (file->f_security == NULL)
651 return -ENOMEM;
652 return 0;
653}
654
afb1cbe3
CS
655/**
656 * lsm_inode_alloc - allocate a composite inode blob
657 * @inode: the inode that needs a blob
658 *
659 * Allocate the inode blob for all the modules
660 *
661 * Returns 0, or -ENOMEM if memory can't be allocated.
662 */
663int lsm_inode_alloc(struct inode *inode)
664{
665 if (!lsm_inode_cache) {
666 inode->i_security = NULL;
667 return 0;
668 }
669
670 inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
671 if (inode->i_security == NULL)
672 return -ENOMEM;
673 return 0;
674}
675
f4ad8f2c
CS
676/**
677 * lsm_task_alloc - allocate a composite task blob
678 * @task: the task that needs a blob
679 *
680 * Allocate the task blob for all the modules
681 *
682 * Returns 0, or -ENOMEM if memory can't be allocated.
683 */
3e8c7367 684static int lsm_task_alloc(struct task_struct *task)
f4ad8f2c
CS
685{
686 if (blob_sizes.lbs_task == 0) {
687 task->security = NULL;
688 return 0;
689 }
690
691 task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
692 if (task->security == NULL)
693 return -ENOMEM;
694 return 0;
695}
696
ecd5f82e
CS
697/**
698 * lsm_ipc_alloc - allocate a composite ipc blob
699 * @kip: the ipc that needs a blob
700 *
701 * Allocate the ipc blob for all the modules
702 *
703 * Returns 0, or -ENOMEM if memory can't be allocated.
704 */
3e8c7367 705static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
ecd5f82e
CS
706{
707 if (blob_sizes.lbs_ipc == 0) {
708 kip->security = NULL;
709 return 0;
710 }
711
712 kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
713 if (kip->security == NULL)
714 return -ENOMEM;
715 return 0;
716}
717
718/**
719 * lsm_msg_msg_alloc - allocate a composite msg_msg blob
720 * @mp: the msg_msg that needs a blob
721 *
722 * Allocate the ipc blob for all the modules
723 *
724 * Returns 0, or -ENOMEM if memory can't be allocated.
725 */
3e8c7367 726static int lsm_msg_msg_alloc(struct msg_msg *mp)
ecd5f82e
CS
727{
728 if (blob_sizes.lbs_msg_msg == 0) {
729 mp->security = NULL;
730 return 0;
731 }
732
733 mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
734 if (mp->security == NULL)
735 return -ENOMEM;
736 return 0;
737}
738
f4ad8f2c
CS
739/**
740 * lsm_early_task - during initialization allocate a composite task blob
741 * @task: the task that needs a blob
742 *
1cfb2a51 743 * Allocate the task blob for all the modules
f4ad8f2c 744 */
1cfb2a51 745static void __init lsm_early_task(struct task_struct *task)
f4ad8f2c 746{
1cfb2a51 747 int rc = lsm_task_alloc(task);
f4ad8f2c 748
f4ad8f2c
CS
749 if (rc)
750 panic("%s: Early task alloc failed.\n", __func__);
751}
752
1aea7808
CS
753/**
754 * lsm_superblock_alloc - allocate a composite superblock blob
755 * @sb: the superblock that needs a blob
756 *
757 * Allocate the superblock blob for all the modules
758 *
759 * Returns 0, or -ENOMEM if memory can't be allocated.
760 */
761static int lsm_superblock_alloc(struct super_block *sb)
762{
763 if (blob_sizes.lbs_superblock == 0) {
764 sb->s_security = NULL;
765 return 0;
766 }
767
768 sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
769 if (sb->s_security == NULL)
770 return -ENOMEM;
771 return 0;
772}
773
e1ca7129
CS
774/**
775 * lsm_fill_user_ctx - Fill a user space lsm_ctx structure
d7cf3412
PM
776 * @uctx: a userspace LSM context to be filled
777 * @uctx_len: available uctx size (input), used uctx size (output)
778 * @val: the new LSM context value
779 * @val_len: the size of the new LSM context value
e1ca7129
CS
780 * @id: LSM id
781 * @flags: LSM defined flags
782 *
eaf0e7a3
PM
783 * Fill all of the fields in a userspace lsm_ctx structure. If @uctx is NULL
784 * simply calculate the required size to output via @utc_len and return
785 * success.
e1ca7129 786 *
d7cf3412
PM
787 * Returns 0 on success, -E2BIG if userspace buffer is not large enough,
788 * -EFAULT on a copyout error, -ENOMEM if memory can't be allocated.
e1ca7129 789 */
a5a858f6 790int lsm_fill_user_ctx(struct lsm_ctx __user *uctx, u32 *uctx_len,
d7cf3412
PM
791 void *val, size_t val_len,
792 u64 id, u64 flags)
e1ca7129 793{
d7cf3412
PM
794 struct lsm_ctx *nctx = NULL;
795 size_t nctx_len;
e1ca7129
CS
796 int rc = 0;
797
41793202 798 nctx_len = ALIGN(struct_size(nctx, ctx, val_len), sizeof(void *));
d7cf3412
PM
799 if (nctx_len > *uctx_len) {
800 rc = -E2BIG;
801 goto out;
802 }
e1ca7129 803
eaf0e7a3
PM
804 /* no buffer - return success/0 and set @uctx_len to the req size */
805 if (!uctx)
806 goto out;
807
d7cf3412
PM
808 nctx = kzalloc(nctx_len, GFP_KERNEL);
809 if (nctx == NULL) {
810 rc = -ENOMEM;
811 goto out;
812 }
813 nctx->id = id;
814 nctx->flags = flags;
815 nctx->len = nctx_len;
816 nctx->ctx_len = val_len;
817 memcpy(nctx->ctx, val, val_len);
e1ca7129 818
d7cf3412 819 if (copy_to_user(uctx, nctx, nctx_len))
e1ca7129
CS
820 rc = -EFAULT;
821
d7cf3412
PM
822out:
823 kfree(nctx);
824 *uctx_len = nctx_len;
e1ca7129
CS
825 return rc;
826}
827
98e828a0
KS
828/*
829 * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
830 * can be accessed with:
831 *
832 * LSM_RET_DEFAULT(<hook_name>)
833 *
834 * The macros below define static constants for the default value of each
835 * LSM hook.
836 */
837#define LSM_RET_DEFAULT(NAME) (NAME##_default)
838#define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
839#define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
86dd9fd5 840 static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
98e828a0
KS
841#define LSM_HOOK(RET, DEFAULT, NAME, ...) \
842 DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
843
844#include <linux/lsm_hook_defs.h>
845#undef LSM_HOOK
846
f25fce3e 847/*
b1d9e6b0 848 * Hook list operation macros.
1da177e4 849 *
f25fce3e
CS
850 * call_void_hook:
851 * This is a hook that does not return a value.
1da177e4 852 *
f25fce3e
CS
853 * call_int_hook:
854 * This is a hook that returns a value.
1da177e4 855 */
1da177e4 856
b1d9e6b0
CS
857#define call_void_hook(FUNC, ...) \
858 do { \
859 struct security_hook_list *P; \
860 \
df0ce173 861 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
b1d9e6b0
CS
862 P->hook.FUNC(__VA_ARGS__); \
863 } while (0)
864
260017f3
OM
865#define call_int_hook(FUNC, ...) ({ \
866 int RC = LSM_RET_DEFAULT(FUNC); \
b1d9e6b0
CS
867 do { \
868 struct security_hook_list *P; \
869 \
df0ce173 870 hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
b1d9e6b0 871 RC = P->hook.FUNC(__VA_ARGS__); \
260017f3 872 if (RC != LSM_RET_DEFAULT(FUNC)) \
b1d9e6b0
CS
873 break; \
874 } \
875 } while (0); \
876 RC; \
877})
1da177e4 878
20510f2f
JM
879/* Security operations */
880
1427ddbe
PM
881/**
882 * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
883 * @mgr: task credentials of current binder process
884 *
885 * Check whether @mgr is allowed to be the binder context manager.
886 *
887 * Return: Return 0 if permission is granted.
888 */
52f88693 889int security_binder_set_context_mgr(const struct cred *mgr)
79af7307 890{
260017f3 891 return call_int_hook(binder_set_context_mgr, mgr);
79af7307
SS
892}
893
1427ddbe
PM
894/**
895 * security_binder_transaction() - Check if a binder transaction is allowed
896 * @from: sending process
897 * @to: receiving process
898 *
899 * Check whether @from is allowed to invoke a binder transaction call to @to.
900 *
901 * Return: Returns 0 if permission is granted.
902 */
52f88693
TK
903int security_binder_transaction(const struct cred *from,
904 const struct cred *to)
79af7307 905{
260017f3 906 return call_int_hook(binder_transaction, from, to);
79af7307
SS
907}
908
1427ddbe
PM
909/**
910 * security_binder_transfer_binder() - Check if a binder transfer is allowed
911 * @from: sending process
912 * @to: receiving process
913 *
914 * Check whether @from is allowed to transfer a binder reference to @to.
915 *
916 * Return: Returns 0 if permission is granted.
917 */
52f88693
TK
918int security_binder_transfer_binder(const struct cred *from,
919 const struct cred *to)
79af7307 920{
260017f3 921 return call_int_hook(binder_transfer_binder, from, to);
79af7307
SS
922}
923
1427ddbe
PM
924/**
925 * security_binder_transfer_file() - Check if a binder file xfer is allowed
926 * @from: sending process
927 * @to: receiving process
928 * @file: file being transferred
929 *
930 * Check whether @from is allowed to transfer @file to @to.
931 *
932 * Return: Returns 0 if permission is granted.
933 */
52f88693 934int security_binder_transfer_file(const struct cred *from,
8e4672d6 935 const struct cred *to, const struct file *file)
79af7307 936{
260017f3 937 return call_int_hook(binder_transfer_file, from, to, file);
79af7307
SS
938}
939
e261301c
PM
940/**
941 * security_ptrace_access_check() - Check if tracing is allowed
942 * @child: target process
943 * @mode: PTRACE_MODE flags
944 *
945 * Check permission before allowing the current process to trace the @child
946 * process. Security modules may also want to perform a process tracing check
947 * during an execve in the set_security or apply_creds hooks of tracing check
948 * during an execve in the bprm_set_creds hook of binprm_security_ops if the
949 * process is being traced and its security attributes would be changed by the
950 * execve.
951 *
952 * Return: Returns 0 if permission is granted.
953 */
9e48858f 954int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
20510f2f 955{
260017f3 956 return call_int_hook(ptrace_access_check, child, mode);
5cd9c58f
DH
957}
958
e261301c
PM
959/**
960 * security_ptrace_traceme() - Check if tracing is allowed
961 * @parent: tracing process
962 *
963 * Check that the @parent process has sufficient permission to trace the
964 * current process before allowing the current process to present itself to the
965 * @parent process for tracing.
966 *
967 * Return: Returns 0 if permission is granted.
968 */
5cd9c58f
DH
969int security_ptrace_traceme(struct task_struct *parent)
970{
260017f3 971 return call_int_hook(ptrace_traceme, parent);
20510f2f
JM
972}
973
e261301c
PM
974/**
975 * security_capget() - Get the capability sets for a process
976 * @target: target process
977 * @effective: effective capability set
978 * @inheritable: inheritable capability set
979 * @permitted: permitted capability set
980 *
981 * Get the @effective, @inheritable, and @permitted capability sets for the
982 * @target process. The hook may also perform permission checking to determine
983 * if the current process is allowed to see the capability sets of the @target
984 * process.
985 *
986 * Return: Returns 0 if the capability sets were successfully obtained.
987 */
6672efbb 988int security_capget(const struct task_struct *target,
63c1845b
PM
989 kernel_cap_t *effective,
990 kernel_cap_t *inheritable,
991 kernel_cap_t *permitted)
20510f2f 992{
260017f3 993 return call_int_hook(capget, target, effective, inheritable, permitted);
20510f2f
JM
994}
995
e261301c
PM
996/**
997 * security_capset() - Set the capability sets for a process
998 * @new: new credentials for the target process
999 * @old: current credentials of the target process
1000 * @effective: effective capability set
1001 * @inheritable: inheritable capability set
1002 * @permitted: permitted capability set
1003 *
1004 * Set the @effective, @inheritable, and @permitted capability sets for the
1005 * current process.
1006 *
1007 * Return: Returns 0 and update @new if permission is granted.
1008 */
d84f4f99
DH
1009int security_capset(struct cred *new, const struct cred *old,
1010 const kernel_cap_t *effective,
1011 const kernel_cap_t *inheritable,
1012 const kernel_cap_t *permitted)
20510f2f 1013{
260017f3
OM
1014 return call_int_hook(capset, new, old, effective, inheritable,
1015 permitted);
20510f2f
JM
1016}
1017
e261301c
PM
1018/**
1019 * security_capable() - Check if a process has the necessary capability
1020 * @cred: credentials to examine
1021 * @ns: user namespace
1022 * @cap: capability requested
1023 * @opts: capability check options
1024 *
1025 * Check whether the @tsk process has the @cap capability in the indicated
1026 * credentials. @cap contains the capability <include/linux/capability.h>.
1027 * @opts contains options for the capable check <include/linux/security.h>.
1028 *
1029 * Return: Returns 0 if the capability is granted.
1030 */
c1a85a00
MM
1031int security_capable(const struct cred *cred,
1032 struct user_namespace *ns,
1033 int cap,
1034 unsigned int opts)
20510f2f 1035{
260017f3 1036 return call_int_hook(capable, cred, ns, cap, opts);
20510f2f
JM
1037}
1038
e261301c
PM
1039/**
1040 * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
1041 * @cmds: commands
1042 * @type: type
1043 * @id: id
1044 * @sb: filesystem
1045 *
1046 * Check whether the quotactl syscall is allowed for this @sb.
1047 *
1048 * Return: Returns 0 if permission is granted.
1049 */
25cc71d1 1050int security_quotactl(int cmds, int type, int id, const struct super_block *sb)
20510f2f 1051{
260017f3 1052 return call_int_hook(quotactl, cmds, type, id, sb);
20510f2f
JM
1053}
1054
e261301c
PM
1055/**
1056 * security_quota_on() - Check if QUOTAON is allowed for a dentry
1057 * @dentry: dentry
1058 *
1059 * Check whether QUOTAON is allowed for @dentry.
1060 *
1061 * Return: Returns 0 if permission is granted.
1062 */
20510f2f
JM
1063int security_quota_on(struct dentry *dentry)
1064{
260017f3 1065 return call_int_hook(quota_on, dentry);
20510f2f
JM
1066}
1067
e261301c
PM
1068/**
1069 * security_syslog() - Check if accessing the kernel message ring is allowed
1070 * @type: SYSLOG_ACTION_* type
1071 *
1072 * Check permission before accessing the kernel message ring or changing
1073 * logging to the console. See the syslog(2) manual page for an explanation of
1074 * the @type values.
1075 *
1076 * Return: Return 0 if permission is granted.
1077 */
12b3052c 1078int security_syslog(int type)
20510f2f 1079{
260017f3 1080 return call_int_hook(syslog, type);
20510f2f
JM
1081}
1082
e261301c
PM
1083/**
1084 * security_settime64() - Check if changing the system time is allowed
1085 * @ts: new time
1086 * @tz: timezone
1087 *
1088 * Check permission to change the system time, struct timespec64 is defined in
1089 * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
1090 *
1091 * Return: Returns 0 if permission is granted.
1092 */
457db29b 1093int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
20510f2f 1094{
260017f3 1095 return call_int_hook(settime, ts, tz);
20510f2f
JM
1096}
1097
e261301c
PM
1098/**
1099 * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
1100 * @mm: mm struct
1101 * @pages: number of pages
1102 *
1103 * Check permissions for allocating a new virtual mapping. If all LSMs return
1104 * a positive value, __vm_enough_memory() will be called with cap_sys_admin
1105 * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
1106 * called with cap_sys_admin cleared.
1107 *
1108 * Return: Returns 0 if permission is granted by the LSM infrastructure to the
1109 * caller.
1110 */
20510f2f
JM
1111int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
1112{
b1d9e6b0
CS
1113 struct security_hook_list *hp;
1114 int cap_sys_admin = 1;
1115 int rc;
1116
1117 /*
1118 * The module will respond with a positive value if
1119 * it thinks the __vm_enough_memory() call should be
1120 * made with the cap_sys_admin set. If all of the modules
1121 * agree that it should be set it will. If any module
1122 * thinks it should not be set it won't.
1123 */
df0ce173 1124 hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
b1d9e6b0
CS
1125 rc = hp->hook.vm_enough_memory(mm, pages);
1126 if (rc <= 0) {
1127 cap_sys_admin = 0;
1128 break;
1129 }
1130 }
1131 return __vm_enough_memory(mm, pages, cap_sys_admin);
20510f2f
JM
1132}
1133
1661372c
PM
1134/**
1135 * security_bprm_creds_for_exec() - Prepare the credentials for exec()
1136 * @bprm: binary program information
1137 *
1138 * If the setup in prepare_exec_creds did not setup @bprm->cred->security
1139 * properly for executing @bprm->file, update the LSM's portion of
1140 * @bprm->cred->security to be what commit_creds needs to install for the new
1141 * program. This hook may also optionally check permissions (e.g. for
1142 * transitions between security domains). The hook must set @bprm->secureexec
1143 * to 1 if AT_SECURE should be set to request libc enable secure mode. @bprm
1144 * contains the linux_binprm structure.
1145 *
1146 * Return: Returns 0 if the hook is successful and permission is granted.
1147 */
b8bff599 1148int security_bprm_creds_for_exec(struct linux_binprm *bprm)
20510f2f 1149{
260017f3 1150 return call_int_hook(bprm_creds_for_exec, bprm);
b8bff599
EB
1151}
1152
1661372c
PM
1153/**
1154 * security_bprm_creds_from_file() - Update linux_binprm creds based on file
1155 * @bprm: binary program information
1156 * @file: associated file
1157 *
1158 * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
1159 * exec, update @bprm->cred to reflect that change. This is called after
1160 * finding the binary that will be executed without an interpreter. This
1161 * ensures that the credentials will not be derived from a script that the
1162 * binary will need to reopen, which when reopend may end up being a completely
1163 * different file. This hook may also optionally check permissions (e.g. for
1164 * transitions between security domains). The hook must set @bprm->secureexec
1165 * to 1 if AT_SECURE should be set to request libc enable secure mode. The
1166 * hook must add to @bprm->per_clear any personality flags that should be
1167 * cleared from current->personality. @bprm contains the linux_binprm
1168 * structure.
1169 *
1170 * Return: Returns 0 if the hook is successful and permission is granted.
1171 */
4a00c673 1172int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file)
20510f2f 1173{
260017f3 1174 return call_int_hook(bprm_creds_from_file, bprm, file);
20510f2f
JM
1175}
1176
1661372c
PM
1177/**
1178 * security_bprm_check() - Mediate binary handler search
1179 * @bprm: binary program information
1180 *
1181 * This hook mediates the point when a search for a binary handler will begin.
1182 * It allows a check against the @bprm->cred->security value which was set in
1183 * the preceding creds_for_exec call. The argv list and envp list are reliably
1184 * available in @bprm. This hook may be called multiple times during a single
1185 * execve. @bprm contains the linux_binprm structure.
1186 *
1187 * Return: Returns 0 if the hook is successful and permission is granted.
1188 */
a6f76f23 1189int security_bprm_check(struct linux_binprm *bprm)
20510f2f 1190{
260017f3 1191 return call_int_hook(bprm_check_security, bprm);
20510f2f
JM
1192}
1193
1661372c
PM
1194/**
1195 * security_bprm_committing_creds() - Install creds for a process during exec()
1196 * @bprm: binary program information
1197 *
1198 * Prepare to install the new security attributes of a process being
1199 * transformed by an execve operation, based on the old credentials pointed to
1200 * by @current->cred and the information set in @bprm->cred by the
1201 * bprm_creds_for_exec hook. @bprm points to the linux_binprm structure. This
1202 * hook is a good place to perform state changes on the process such as closing
1203 * open file descriptors to which access will no longer be granted when the
1204 * attributes are changed. This is called immediately before commit_creds().
1205 */
64fc9526 1206void security_bprm_committing_creds(const struct linux_binprm *bprm)
20510f2f 1207{
f25fce3e 1208 call_void_hook(bprm_committing_creds, bprm);
20510f2f
JM
1209}
1210
1661372c
PM
1211/**
1212 * security_bprm_committed_creds() - Tidy up after cred install during exec()
1213 * @bprm: binary program information
1214 *
1215 * Tidy up after the installation of the new security attributes of a process
1216 * being transformed by an execve operation. The new credentials have, by this
1217 * point, been set to @current->cred. @bprm points to the linux_binprm
1218 * structure. This hook is a good place to perform state changes on the
1219 * process such as clearing out non-inheritable signal state. This is called
1220 * immediately after commit_creds().
1221 */
a721f7b8 1222void security_bprm_committed_creds(const struct linux_binprm *bprm)
20510f2f 1223{
f25fce3e 1224 call_void_hook(bprm_committed_creds, bprm);
20510f2f
JM
1225}
1226
d80a8f1b
DH
1227/**
1228 * security_fs_context_submount() - Initialise fc->security
1229 * @fc: new filesystem context
1230 * @reference: dentry reference for submount/remount
1231 *
1232 * Fill out the ->security field for a new fs_context.
1233 *
1234 * Return: Returns 0 on success or negative error code on failure.
1235 */
1236int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
1237{
260017f3 1238 return call_int_hook(fs_context_submount, fc, reference);
d80a8f1b
DH
1239}
1240
36819f18
PM
1241/**
1242 * security_fs_context_dup() - Duplicate a fs_context LSM blob
1243 * @fc: destination filesystem context
1244 * @src_fc: source filesystem context
1245 *
1246 * Allocate and attach a security structure to sc->security. This pointer is
1247 * initialised to NULL by the caller. @fc indicates the new filesystem context.
1248 * @src_fc indicates the original filesystem context.
1249 *
1250 * Return: Returns 0 on success or a negative error code on failure.
1251 */
0b52075e
AV
1252int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
1253{
260017f3 1254 return call_int_hook(fs_context_dup, fc, src_fc);
0b52075e
AV
1255}
1256
36819f18
PM
1257/**
1258 * security_fs_context_parse_param() - Configure a filesystem context
1259 * @fc: filesystem context
1260 * @param: filesystem parameter
1261 *
1262 * Userspace provided a parameter to configure a superblock. The LSM can
1263 * consume the parameter or return it to the caller for use elsewhere.
1264 *
1265 * Return: If the parameter is used by the LSM it should return 0, if it is
1266 * returned to the caller -ENOPARAM is returned, otherwise a negative
1267 * error code is returned.
1268 */
ecff3057
CS
1269int security_fs_context_parse_param(struct fs_context *fc,
1270 struct fs_parameter *param)
da2441fd 1271{
ecff3057
CS
1272 struct security_hook_list *hp;
1273 int trc;
1274 int rc = -ENOPARAM;
1275
1276 hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1277 list) {
1278 trc = hp->hook.fs_context_parse_param(fc, param);
1279 if (trc == 0)
1280 rc = 0;
1281 else if (trc != -ENOPARAM)
1282 return trc;
1283 }
1284 return rc;
da2441fd
DH
1285}
1286
08526a90
PM
1287/**
1288 * security_sb_alloc() - Allocate a super_block LSM blob
1289 * @sb: filesystem superblock
1290 *
1291 * Allocate and attach a security structure to the sb->s_security field. The
1292 * s_security field is initialized to NULL when the structure is allocated.
1293 * @sb contains the super_block structure to be modified.
1294 *
1295 * Return: Returns 0 if operation was successful.
1296 */
20510f2f
JM
1297int security_sb_alloc(struct super_block *sb)
1298{
1aea7808
CS
1299 int rc = lsm_superblock_alloc(sb);
1300
1301 if (unlikely(rc))
1302 return rc;
260017f3 1303 rc = call_int_hook(sb_alloc_security, sb);
1aea7808
CS
1304 if (unlikely(rc))
1305 security_sb_free(sb);
1306 return rc;
20510f2f
JM
1307}
1308
08526a90
PM
1309/**
1310 * security_sb_delete() - Release super_block LSM associated objects
1311 * @sb: filesystem superblock
1312 *
1313 * Release objects tied to a superblock (e.g. inodes). @sb contains the
1314 * super_block structure being released.
1315 */
83e804f0
MS
1316void security_sb_delete(struct super_block *sb)
1317{
1318 call_void_hook(sb_delete, sb);
20510f2f
JM
1319}
1320
08526a90
PM
1321/**
1322 * security_sb_free() - Free a super_block LSM blob
1323 * @sb: filesystem superblock
1324 *
1325 * Deallocate and clear the sb->s_security field. @sb contains the super_block
1326 * structure to be modified.
1327 */
20510f2f
JM
1328void security_sb_free(struct super_block *sb)
1329{
f25fce3e 1330 call_void_hook(sb_free_security, sb);
1aea7808
CS
1331 kfree(sb->s_security);
1332 sb->s_security = NULL;
20510f2f
JM
1333}
1334
08526a90
PM
1335/**
1336 * security_free_mnt_opts() - Free memory associated with mount options
1e2523d7 1337 * @mnt_opts: LSM processed mount options
08526a90
PM
1338 *
1339 * Free memory associated with @mnt_ops.
1340 */
204cc0cc 1341void security_free_mnt_opts(void **mnt_opts)
20510f2f 1342{
204cc0cc
AV
1343 if (!*mnt_opts)
1344 return;
1345 call_void_hook(sb_free_mnt_opts, *mnt_opts);
1346 *mnt_opts = NULL;
20510f2f 1347}
204cc0cc 1348EXPORT_SYMBOL(security_free_mnt_opts);
20510f2f 1349
08526a90
PM
1350/**
1351 * security_sb_eat_lsm_opts() - Consume LSM mount options
1352 * @options: mount options
1e2523d7 1353 * @mnt_opts: LSM processed mount options
08526a90
PM
1354 *
1355 * Eat (scan @options) and save them in @mnt_opts.
1356 *
1357 * Return: Returns 0 on success, negative values on failure.
1358 */
204cc0cc 1359int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
ff36fe2c 1360{
260017f3 1361 return call_int_hook(sb_eat_lsm_opts, options, mnt_opts);
ff36fe2c 1362}
f5c0c26d 1363EXPORT_SYMBOL(security_sb_eat_lsm_opts);
ff36fe2c 1364
08526a90
PM
1365/**
1366 * security_sb_mnt_opts_compat() - Check if new mount options are allowed
1367 * @sb: filesystem superblock
1368 * @mnt_opts: new mount options
1369 *
1370 * Determine if the new mount options in @mnt_opts are allowed given the
1371 * existing mounted filesystem at @sb. @sb superblock being compared.
1372 *
1373 * Return: Returns 0 if options are compatible.
1374 */
69c4a42d
OK
1375int security_sb_mnt_opts_compat(struct super_block *sb,
1376 void *mnt_opts)
1377{
260017f3 1378 return call_int_hook(sb_mnt_opts_compat, sb, mnt_opts);
69c4a42d
OK
1379}
1380EXPORT_SYMBOL(security_sb_mnt_opts_compat);
1381
08526a90
PM
1382/**
1383 * security_sb_remount() - Verify no incompatible mount changes during remount
1384 * @sb: filesystem superblock
1385 * @mnt_opts: (re)mount options
1386 *
1387 * Extracts security system specific mount options and verifies no changes are
1388 * being made to those options.
1389 *
1390 * Return: Returns 0 if permission is granted.
1391 */
c039bc3c 1392int security_sb_remount(struct super_block *sb,
204cc0cc 1393 void *mnt_opts)
20510f2f 1394{
260017f3 1395 return call_int_hook(sb_remount, sb, mnt_opts);
ff36fe2c 1396}
a65001e8 1397EXPORT_SYMBOL(security_sb_remount);
ff36fe2c 1398
08526a90
PM
1399/**
1400 * security_sb_kern_mount() - Check if a kernel mount is allowed
1401 * @sb: filesystem superblock
1402 *
1403 * Mount this @sb if allowed by permissions.
1404 *
1405 * Return: Returns 0 if permission is granted.
1406 */
20a2aa47 1407int security_sb_kern_mount(const struct super_block *sb)
20510f2f 1408{
260017f3 1409 return call_int_hook(sb_kern_mount, sb);
20510f2f
JM
1410}
1411
08526a90
PM
1412/**
1413 * security_sb_show_options() - Output the mount options for a superblock
1414 * @m: output file
1415 * @sb: filesystem superblock
1416 *
1417 * Show (print on @m) mount options for this @sb.
1418 *
1419 * Return: Returns 0 on success, negative values on failure.
1420 */
2069f457
EP
1421int security_sb_show_options(struct seq_file *m, struct super_block *sb)
1422{
260017f3 1423 return call_int_hook(sb_show_options, m, sb);
2069f457
EP
1424}
1425
08526a90
PM
1426/**
1427 * security_sb_statfs() - Check if accessing fs stats is allowed
1428 * @dentry: superblock handle
1429 *
1430 * Check permission before obtaining filesystem statistics for the @mnt
1431 * mountpoint. @dentry is a handle on the superblock for the filesystem.
1432 *
1433 * Return: Returns 0 if permission is granted.
1434 */
20510f2f
JM
1435int security_sb_statfs(struct dentry *dentry)
1436{
260017f3 1437 return call_int_hook(sb_statfs, dentry);
20510f2f
JM
1438}
1439
08526a90
PM
1440/**
1441 * security_sb_mount() - Check permission for mounting a filesystem
1442 * @dev_name: filesystem backing device
1443 * @path: mount point
1444 * @type: filesystem type
1445 * @flags: mount flags
1446 * @data: filesystem specific data
1447 *
1448 * Check permission before an object specified by @dev_name is mounted on the
1449 * mount point named by @nd. For an ordinary mount, @dev_name identifies a
1450 * device if the file system type requires a device. For a remount
1451 * (@flags & MS_REMOUNT), @dev_name is irrelevant. For a loopback/bind mount
1452 * (@flags & MS_BIND), @dev_name identifies the pathname of the object being
1453 * mounted.
1454 *
1455 * Return: Returns 0 if permission is granted.
1456 */
8a04c43b 1457int security_sb_mount(const char *dev_name, const struct path *path,
63c1845b 1458 const char *type, unsigned long flags, void *data)
20510f2f 1459{
260017f3 1460 return call_int_hook(sb_mount, dev_name, path, type, flags, data);
20510f2f
JM
1461}
1462
08526a90
PM
1463/**
1464 * security_sb_umount() - Check permission for unmounting a filesystem
1465 * @mnt: mounted filesystem
1466 * @flags: unmount flags
1467 *
1468 * Check permission before the @mnt file system is unmounted.
1469 *
1470 * Return: Returns 0 if permission is granted.
1471 */
20510f2f
JM
1472int security_sb_umount(struct vfsmount *mnt, int flags)
1473{
260017f3 1474 return call_int_hook(sb_umount, mnt, flags);
20510f2f
JM
1475}
1476
08526a90
PM
1477/**
1478 * security_sb_pivotroot() - Check permissions for pivoting the rootfs
1479 * @old_path: new location for current rootfs
1480 * @new_path: location of the new rootfs
1481 *
1482 * Check permission before pivoting the root filesystem.
1483 *
1484 * Return: Returns 0 if permission is granted.
1485 */
63c1845b
PM
1486int security_sb_pivotroot(const struct path *old_path,
1487 const struct path *new_path)
20510f2f 1488{
260017f3 1489 return call_int_hook(sb_pivotroot, old_path, new_path);
20510f2f
JM
1490}
1491
08526a90
PM
1492/**
1493 * security_sb_set_mnt_opts() - Set the mount options for a filesystem
1494 * @sb: filesystem superblock
1495 * @mnt_opts: binary mount options
1496 * @kern_flags: kernel flags (in)
1497 * @set_kern_flags: kernel flags (out)
1498 *
1499 * Set the security relevant mount options used for a superblock.
1500 *
1501 * Return: Returns 0 on success, error on failure.
1502 */
c9180a57 1503int security_sb_set_mnt_opts(struct super_block *sb,
63c1845b
PM
1504 void *mnt_opts,
1505 unsigned long kern_flags,
1506 unsigned long *set_kern_flags)
c9180a57 1507{
260017f3
OM
1508 struct security_hook_list *hp;
1509 int rc = mnt_opts ? -EOPNOTSUPP : LSM_RET_DEFAULT(sb_set_mnt_opts);
1510
1511 hlist_for_each_entry(hp, &security_hook_heads.sb_set_mnt_opts,
1512 list) {
1513 rc = hp->hook.sb_set_mnt_opts(sb, mnt_opts, kern_flags,
1514 set_kern_flags);
1515 if (rc != LSM_RET_DEFAULT(sb_set_mnt_opts))
1516 break;
1517 }
1518 return rc;
c9180a57 1519}
e0007529 1520EXPORT_SYMBOL(security_sb_set_mnt_opts);
c9180a57 1521
08526a90
PM
1522/**
1523 * security_sb_clone_mnt_opts() - Duplicate superblock mount options
1e2523d7
PM
1524 * @oldsb: source superblock
1525 * @newsb: destination superblock
08526a90
PM
1526 * @kern_flags: kernel flags (in)
1527 * @set_kern_flags: kernel flags (out)
1528 *
1529 * Copy all security options from a given superblock to another.
1530 *
1531 * Return: Returns 0 on success, error on failure.
1532 */
094f7b69 1533int security_sb_clone_mnt_opts(const struct super_block *oldsb,
63c1845b
PM
1534 struct super_block *newsb,
1535 unsigned long kern_flags,
1536 unsigned long *set_kern_flags)
c9180a57 1537{
260017f3 1538 return call_int_hook(sb_clone_mnt_opts, oldsb, newsb,
63c1845b 1539 kern_flags, set_kern_flags);
c9180a57 1540}
e0007529
EP
1541EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1542
08526a90
PM
1543/**
1544 * security_move_mount() - Check permissions for moving a mount
1545 * @from_path: source mount point
1546 * @to_path: destination mount point
1547 *
1548 * Check permission before a mount is moved.
1549 *
1550 * Return: Returns 0 if permission is granted.
1551 */
63c1845b
PM
1552int security_move_mount(const struct path *from_path,
1553 const struct path *to_path)
2db154b3 1554{
260017f3 1555 return call_int_hook(move_mount, from_path, to_path);
2db154b3
DH
1556}
1557
916e3258
PM
1558/**
1559 * security_path_notify() - Check if setting a watch is allowed
1560 * @path: file path
1561 * @mask: event mask
1562 * @obj_type: file path type
1563 *
1564 * Check permissions before setting a watch on events as defined by @mask, on
1565 * an object at @path, whose type is defined by @obj_type.
1566 *
1567 * Return: Returns 0 if permission is granted.
1568 */
ac5656d8 1569int security_path_notify(const struct path *path, u64 mask,
63c1845b 1570 unsigned int obj_type)
ac5656d8 1571{
260017f3 1572 return call_int_hook(path_notify, path, mask, obj_type);
ac5656d8
AG
1573}
1574
916e3258
PM
1575/**
1576 * security_inode_alloc() - Allocate an inode LSM blob
1577 * @inode: the inode
1578 *
1579 * Allocate and attach a security structure to @inode->i_security. The
1580 * i_security field is initialized to NULL when the inode structure is
1581 * allocated.
1582 *
1583 * Return: Return 0 if operation was successful.
1584 */
20510f2f
JM
1585int security_inode_alloc(struct inode *inode)
1586{
afb1cbe3
CS
1587 int rc = lsm_inode_alloc(inode);
1588
1589 if (unlikely(rc))
1590 return rc;
260017f3 1591 rc = call_int_hook(inode_alloc_security, inode);
afb1cbe3
CS
1592 if (unlikely(rc))
1593 security_inode_free(inode);
1594 return rc;
1595}
1596
1597static void inode_free_by_rcu(struct rcu_head *head)
1598{
1599 /*
1600 * The rcu head is at the start of the inode blob
1601 */
1602 kmem_cache_free(lsm_inode_cache, head);
20510f2f
JM
1603}
1604
916e3258
PM
1605/**
1606 * security_inode_free() - Free an inode's LSM blob
1607 * @inode: the inode
1608 *
1609 * Deallocate the inode security structure and set @inode->i_security to NULL.
1610 */
20510f2f
JM
1611void security_inode_free(struct inode *inode)
1612{
f25fce3e 1613 call_void_hook(inode_free_security, inode);
afb1cbe3
CS
1614 /*
1615 * The inode may still be referenced in a path walk and
1616 * a call to security_inode_permission() can be made
1617 * after inode_free_security() is called. Ideally, the VFS
1618 * wouldn't do this, but fixing that is a much harder
1619 * job. For now, simply free the i_security via RCU, and
1620 * leave the current inode->i_security pointer intact.
1621 * The inode will be freed after the RCU grace period too.
1622 */
1623 if (inode->i_security)
1624 call_rcu((struct rcu_head *)inode->i_security,
63c1845b 1625 inode_free_by_rcu);
20510f2f
JM
1626}
1627
08526a90
PM
1628/**
1629 * security_dentry_init_security() - Perform dentry initialization
1630 * @dentry: the dentry to initialize
1631 * @mode: mode used to determine resource type
1632 * @name: name of the last path component
1633 * @xattr_name: name of the security/LSM xattr
1634 * @ctx: pointer to the resulting LSM context
1635 * @ctxlen: length of @ctx
1636 *
1637 * Compute a context for a dentry as the inode is not yet available since NFSv4
1638 * has no label backed by an EA anyway. It is important to note that
1639 * @xattr_name does not need to be free'd by the caller, it is a static string.
1640 *
1641 * Return: Returns 0 on success, negative values on failure.
1642 */
d47be3df 1643int security_dentry_init_security(struct dentry *dentry, int mode,
15bf3239
VG
1644 const struct qstr *name,
1645 const char **xattr_name, void **ctx,
1646 u32 *ctxlen)
d47be3df 1647{
260017f3
OM
1648 return call_int_hook(dentry_init_security, dentry, mode, name,
1649 xattr_name, ctx, ctxlen);
d47be3df
DQ
1650}
1651EXPORT_SYMBOL(security_dentry_init_security);
1652
08526a90
PM
1653/**
1654 * security_dentry_create_files_as() - Perform dentry initialization
1655 * @dentry: the dentry to initialize
1656 * @mode: mode used to determine resource type
1657 * @name: name of the last path component
1658 * @old: creds to use for LSM context calculations
1659 * @new: creds to modify
1660 *
1661 * Compute a context for a dentry as the inode is not yet available and set
1662 * that context in passed in creds so that new files are created using that
1663 * context. Context is calculated using the passed in creds and not the creds
1664 * of the caller.
1665 *
1666 * Return: Returns 0 on success, error on failure.
1667 */
2602625b
VG
1668int security_dentry_create_files_as(struct dentry *dentry, int mode,
1669 struct qstr *name,
1670 const struct cred *old, struct cred *new)
1671{
260017f3 1672 return call_int_hook(dentry_create_files_as, dentry, mode,
63c1845b 1673 name, old, new);
2602625b
VG
1674}
1675EXPORT_SYMBOL(security_dentry_create_files_as);
1676
916e3258
PM
1677/**
1678 * security_inode_init_security() - Initialize an inode's LSM context
1679 * @inode: the inode
1680 * @dir: parent directory
1681 * @qstr: last component of the pathname
1682 * @initxattrs: callback function to write xattrs
1683 * @fs_data: filesystem specific data
1684 *
1685 * Obtain the security attribute name suffix and value to set on a newly
1686 * created inode and set up the incore security field for the new inode. This
1687 * hook is called by the fs code as part of the inode creation transaction and
1688 * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
6bcdfd2c
RS
1689 * hooks called by the VFS.
1690 *
1691 * The hook function is expected to populate the xattrs array, by calling
1692 * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
1693 * with the lbs_xattr_count field of the lsm_blob_sizes structure. For each
1694 * slot, the hook function should set ->name to the attribute name suffix
1695 * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
1696 * to the attribute value, to set ->value_len to the length of the value. If
1697 * the security module does not use security attributes or does not wish to put
1698 * a security attribute on this particular inode, then it should return
1699 * -EOPNOTSUPP to skip this processing.
916e3258 1700 *
faf302f5
RS
1701 * Return: Returns 0 if the LSM successfully initialized all of the inode
1702 * security attributes that are required, negative values otherwise.
916e3258 1703 */
20510f2f 1704int security_inode_init_security(struct inode *inode, struct inode *dir,
9d8f13ba
MZ
1705 const struct qstr *qstr,
1706 const initxattrs initxattrs, void *fs_data)
20510f2f 1707{
6bcdfd2c
RS
1708 struct security_hook_list *hp;
1709 struct xattr *new_xattrs = NULL;
1710 int ret = -EOPNOTSUPP, xattr_count = 0;
9d8f13ba 1711
20510f2f 1712 if (unlikely(IS_PRIVATE(inode)))
fb88c2b6 1713 return 0;
9d8f13ba 1714
6bcdfd2c
RS
1715 if (!blob_sizes.lbs_xattr_count)
1716 return 0;
1717
1718 if (initxattrs) {
75a323e6
RS
1719 /* Allocate +1 as terminator. */
1720 new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 1,
6bcdfd2c
RS
1721 sizeof(*new_xattrs), GFP_NOFS);
1722 if (!new_xattrs)
1723 return -ENOMEM;
1724 }
1725
1726 hlist_for_each_entry(hp, &security_hook_heads.inode_init_security,
1727 list) {
1728 ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs,
1729 &xattr_count);
1730 if (ret && ret != -EOPNOTSUPP)
1731 goto out;
1732 /*
1733 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
1734 * means that the LSM is not willing to provide an xattr, not
1735 * that it wants to signal an error. Thus, continue to invoke
1736 * the remaining LSMs.
1737 */
1738 }
1739
1740 /* If initxattrs() is NULL, xattr_count is zero, skip the call. */
1741 if (!xattr_count)
9d8f13ba 1742 goto out;
823eb1cc 1743
9d8f13ba
MZ
1744 ret = initxattrs(inode, new_xattrs, fs_data);
1745out:
6bcdfd2c
RS
1746 for (; xattr_count > 0; xattr_count--)
1747 kfree(new_xattrs[xattr_count - 1].value);
1748 kfree(new_xattrs);
9d8f13ba
MZ
1749 return (ret == -EOPNOTSUPP) ? 0 : ret;
1750}
1751EXPORT_SYMBOL(security_inode_init_security);
1752
916e3258
PM
1753/**
1754 * security_inode_init_security_anon() - Initialize an anonymous inode
1755 * @inode: the inode
1756 * @name: the anonymous inode class
1757 * @context_inode: an optional related inode
1758 *
1759 * Set up the incore security field for the new anonymous inode and return
1760 * whether the inode creation is permitted by the security module or not.
1761 *
1762 * Return: Returns 0 on success, -EACCES if the security module denies the
1763 * creation of this inode, or another -errno upon other errors.
1764 */
215b674b
LG
1765int security_inode_init_security_anon(struct inode *inode,
1766 const struct qstr *name,
1767 const struct inode *context_inode)
1768{
260017f3 1769 return call_int_hook(inode_init_security_anon, inode, name,
215b674b
LG
1770 context_inode);
1771}
1772
be6d3e56 1773#ifdef CONFIG_SECURITY_PATH
916e3258
PM
1774/**
1775 * security_path_mknod() - Check if creating a special file is allowed
1776 * @dir: parent directory
1777 * @dentry: new file
1778 * @mode: new file mode
1779 * @dev: device number
1780 *
1781 * Check permissions when creating a file. Note that this hook is called even
1782 * if mknod operation is being done for a regular file.
1783 *
1784 * Return: Returns 0 if permission is granted.
1785 */
63c1845b
PM
1786int security_path_mknod(const struct path *dir, struct dentry *dentry,
1787 umode_t mode, unsigned int dev)
be6d3e56 1788{
c6f493d6 1789 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1790 return 0;
260017f3 1791 return call_int_hook(path_mknod, dir, dentry, mode, dev);
be6d3e56
KT
1792}
1793EXPORT_SYMBOL(security_path_mknod);
1794
08abce60 1795/**
701b3899 1796 * security_path_post_mknod() - Update inode security after reg file creation
08abce60
RS
1797 * @idmap: idmap of the mount
1798 * @dentry: new file
1799 *
701b3899 1800 * Update inode security field after a regular file has been created.
08abce60
RS
1801 */
1802void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
1803{
1804 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
1805 return;
1806 call_void_hook(path_post_mknod, idmap, dentry);
1807}
1808
916e3258
PM
1809/**
1810 * security_path_mkdir() - Check if creating a new directory is allowed
1811 * @dir: parent directory
1812 * @dentry: new directory
1813 * @mode: new directory mode
1814 *
1815 * Check permissions to create a new directory in the existing directory.
1816 *
1817 * Return: Returns 0 if permission is granted.
1818 */
63c1845b
PM
1819int security_path_mkdir(const struct path *dir, struct dentry *dentry,
1820 umode_t mode)
be6d3e56 1821{
c6f493d6 1822 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1823 return 0;
260017f3 1824 return call_int_hook(path_mkdir, dir, dentry, mode);
be6d3e56 1825}
82140443 1826EXPORT_SYMBOL(security_path_mkdir);
be6d3e56 1827
916e3258
PM
1828/**
1829 * security_path_rmdir() - Check if removing a directory is allowed
1830 * @dir: parent directory
1831 * @dentry: directory to remove
1832 *
1833 * Check the permission to remove a directory.
1834 *
1835 * Return: Returns 0 if permission is granted.
1836 */
989f74e0 1837int security_path_rmdir(const struct path *dir, struct dentry *dentry)
be6d3e56 1838{
c6f493d6 1839 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1840 return 0;
260017f3 1841 return call_int_hook(path_rmdir, dir, dentry);
be6d3e56
KT
1842}
1843
916e3258
PM
1844/**
1845 * security_path_unlink() - Check if removing a hard link is allowed
1846 * @dir: parent directory
1847 * @dentry: file
1848 *
1849 * Check the permission to remove a hard link to a file.
1850 *
1851 * Return: Returns 0 if permission is granted.
1852 */
989f74e0 1853int security_path_unlink(const struct path *dir, struct dentry *dentry)
be6d3e56 1854{
c6f493d6 1855 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1856 return 0;
260017f3 1857 return call_int_hook(path_unlink, dir, dentry);
be6d3e56 1858}
82140443 1859EXPORT_SYMBOL(security_path_unlink);
be6d3e56 1860
916e3258
PM
1861/**
1862 * security_path_symlink() - Check if creating a symbolic link is allowed
1863 * @dir: parent directory
1864 * @dentry: symbolic link
1865 * @old_name: file pathname
1866 *
1867 * Check the permission to create a symbolic link to a file.
1868 *
1869 * Return: Returns 0 if permission is granted.
1870 */
d3607752 1871int security_path_symlink(const struct path *dir, struct dentry *dentry,
be6d3e56
KT
1872 const char *old_name)
1873{
c6f493d6 1874 if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
be6d3e56 1875 return 0;
260017f3 1876 return call_int_hook(path_symlink, dir, dentry, old_name);
be6d3e56
KT
1877}
1878
916e3258
PM
1879/**
1880 * security_path_link - Check if creating a hard link is allowed
1881 * @old_dentry: existing file
1882 * @new_dir: new parent directory
1883 * @new_dentry: new link
1884 *
1885 * Check permission before creating a new hard link to a file.
1886 *
1887 * Return: Returns 0 if permission is granted.
1888 */
3ccee46a 1889int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
be6d3e56
KT
1890 struct dentry *new_dentry)
1891{
c6f493d6 1892 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
be6d3e56 1893 return 0;
260017f3 1894 return call_int_hook(path_link, old_dentry, new_dir, new_dentry);
be6d3e56
KT
1895}
1896
916e3258
PM
1897/**
1898 * security_path_rename() - Check if renaming a file is allowed
1899 * @old_dir: parent directory of the old file
1900 * @old_dentry: the old file
1901 * @new_dir: parent directory of the new file
1902 * @new_dentry: the new file
1903 * @flags: flags
1904 *
1905 * Check for permission to rename a file or directory.
1906 *
1907 * Return: Returns 0 if permission is granted.
1908 */
3ccee46a
AV
1909int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1910 const struct path *new_dir, struct dentry *new_dentry,
0b3974eb 1911 unsigned int flags)
be6d3e56 1912{
c6f493d6 1913 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
63c1845b
PM
1914 (d_is_positive(new_dentry) &&
1915 IS_PRIVATE(d_backing_inode(new_dentry)))))
be6d3e56 1916 return 0;
da1ce067 1917
260017f3 1918 return call_int_hook(path_rename, old_dir, old_dentry, new_dir,
63c1845b 1919 new_dentry, flags);
be6d3e56 1920}
82140443 1921EXPORT_SYMBOL(security_path_rename);
be6d3e56 1922
916e3258
PM
1923/**
1924 * security_path_truncate() - Check if truncating a file is allowed
1925 * @path: file
1926 *
1927 * Check permission before truncating the file indicated by path. Note that
1928 * truncation permissions may also be checked based on already opened files,
1929 * using the security_file_truncate() hook.
1930 *
1931 * Return: Returns 0 if permission is granted.
1932 */
81f4c506 1933int security_path_truncate(const struct path *path)
be6d3e56 1934{
c6f493d6 1935 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
be6d3e56 1936 return 0;
260017f3 1937 return call_int_hook(path_truncate, path);
be6d3e56 1938}
89eda068 1939
916e3258
PM
1940/**
1941 * security_path_chmod() - Check if changing the file's mode is allowed
1942 * @path: file
1943 * @mode: new mode
1944 *
1945 * Check for permission to change a mode of the file @path. The new mode is
1946 * specified in @mode which is a bitmask of constants from
1947 * <include/uapi/linux/stat.h>.
1948 *
1949 * Return: Returns 0 if permission is granted.
1950 */
be01f9f2 1951int security_path_chmod(const struct path *path, umode_t mode)
89eda068 1952{
c6f493d6 1953 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 1954 return 0;
260017f3 1955 return call_int_hook(path_chmod, path, mode);
89eda068
TH
1956}
1957
916e3258
PM
1958/**
1959 * security_path_chown() - Check if changing the file's owner/group is allowed
1960 * @path: file
1961 * @uid: file owner
1962 * @gid: file group
1963 *
1964 * Check for permission to change owner/group of a file or directory.
1965 *
1966 * Return: Returns 0 if permission is granted.
1967 */
7fd25dac 1968int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
89eda068 1969{
c6f493d6 1970 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
89eda068 1971 return 0;
260017f3 1972 return call_int_hook(path_chown, path, uid, gid);
89eda068 1973}
8b8efb44 1974
916e3258
PM
1975/**
1976 * security_path_chroot() - Check if changing the root directory is allowed
1977 * @path: directory
1978 *
1979 * Check for permission to change root directory.
1980 *
1981 * Return: Returns 0 if permission is granted.
1982 */
77b286c0 1983int security_path_chroot(const struct path *path)
8b8efb44 1984{
260017f3 1985 return call_int_hook(path_chroot, path);
8b8efb44 1986}
63c1845b 1987#endif /* CONFIG_SECURITY_PATH */
be6d3e56 1988
916e3258
PM
1989/**
1990 * security_inode_create() - Check if creating a file is allowed
1991 * @dir: the parent directory
1992 * @dentry: the file being created
1993 * @mode: requested file mode
1994 *
1995 * Check permission to create a regular file.
1996 *
1997 * Return: Returns 0 if permission is granted.
1998 */
63c1845b
PM
1999int security_inode_create(struct inode *dir, struct dentry *dentry,
2000 umode_t mode)
20510f2f
JM
2001{
2002 if (unlikely(IS_PRIVATE(dir)))
2003 return 0;
260017f3 2004 return call_int_hook(inode_create, dir, dentry, mode);
20510f2f 2005}
800a9647 2006EXPORT_SYMBOL_GPL(security_inode_create);
20510f2f 2007
a7811e34
RS
2008/**
2009 * security_inode_post_create_tmpfile() - Update inode security of new tmpfile
2010 * @idmap: idmap of the mount
2011 * @inode: inode of the new tmpfile
2012 *
2013 * Update inode security data after a tmpfile has been created.
2014 */
2015void security_inode_post_create_tmpfile(struct mnt_idmap *idmap,
2016 struct inode *inode)
2017{
2018 if (unlikely(IS_PRIVATE(inode)))
2019 return;
2020 call_void_hook(inode_post_create_tmpfile, idmap, inode);
2021}
2022
916e3258
PM
2023/**
2024 * security_inode_link() - Check if creating a hard link is allowed
2025 * @old_dentry: existing file
2026 * @dir: new parent directory
2027 * @new_dentry: new link
2028 *
2029 * Check permission before creating a new hard link to a file.
2030 *
2031 * Return: Returns 0 if permission is granted.
2032 */
20510f2f 2033int security_inode_link(struct dentry *old_dentry, struct inode *dir,
63c1845b 2034 struct dentry *new_dentry)
20510f2f 2035{
c6f493d6 2036 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
20510f2f 2037 return 0;
260017f3 2038 return call_int_hook(inode_link, old_dentry, dir, new_dentry);
20510f2f
JM
2039}
2040
916e3258
PM
2041/**
2042 * security_inode_unlink() - Check if removing a hard link is allowed
2043 * @dir: parent directory
2044 * @dentry: file
2045 *
2046 * Check the permission to remove a hard link to a file.
2047 *
2048 * Return: Returns 0 if permission is granted.
2049 */
20510f2f
JM
2050int security_inode_unlink(struct inode *dir, struct dentry *dentry)
2051{
c6f493d6 2052 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2053 return 0;
260017f3 2054 return call_int_hook(inode_unlink, dir, dentry);
20510f2f
JM
2055}
2056
916e3258 2057/**
1e2523d7 2058 * security_inode_symlink() - Check if creating a symbolic link is allowed
916e3258
PM
2059 * @dir: parent directory
2060 * @dentry: symbolic link
2061 * @old_name: existing filename
2062 *
2063 * Check the permission to create a symbolic link to a file.
2064 *
2065 * Return: Returns 0 if permission is granted.
2066 */
20510f2f 2067int security_inode_symlink(struct inode *dir, struct dentry *dentry,
63c1845b 2068 const char *old_name)
20510f2f
JM
2069{
2070 if (unlikely(IS_PRIVATE(dir)))
2071 return 0;
260017f3 2072 return call_int_hook(inode_symlink, dir, dentry, old_name);
20510f2f
JM
2073}
2074
916e3258
PM
2075/**
2076 * security_inode_mkdir() - Check if creation a new director is allowed
2077 * @dir: parent directory
2078 * @dentry: new directory
2079 * @mode: new directory mode
2080 *
2081 * Check permissions to create a new directory in the existing directory
2082 * associated with inode structure @dir.
2083 *
2084 * Return: Returns 0 if permission is granted.
2085 */
18bb1db3 2086int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20510f2f
JM
2087{
2088 if (unlikely(IS_PRIVATE(dir)))
2089 return 0;
260017f3 2090 return call_int_hook(inode_mkdir, dir, dentry, mode);
20510f2f 2091}
800a9647 2092EXPORT_SYMBOL_GPL(security_inode_mkdir);
20510f2f 2093
916e3258
PM
2094/**
2095 * security_inode_rmdir() - Check if removing a directory is allowed
2096 * @dir: parent directory
2097 * @dentry: directory to be removed
2098 *
2099 * Check the permission to remove a directory.
2100 *
2101 * Return: Returns 0 if permission is granted.
2102 */
20510f2f
JM
2103int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
2104{
c6f493d6 2105 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2106 return 0;
260017f3 2107 return call_int_hook(inode_rmdir, dir, dentry);
20510f2f
JM
2108}
2109
916e3258
PM
2110/**
2111 * security_inode_mknod() - Check if creating a special file is allowed
2112 * @dir: parent directory
2113 * @dentry: new file
2114 * @mode: new file mode
2115 * @dev: device number
2116 *
2117 * Check permissions when creating a special file (or a socket or a fifo file
2118 * created via the mknod system call). Note that if mknod operation is being
2119 * done for a regular file, then the create hook will be called and not this
2120 * hook.
2121 *
2122 * Return: Returns 0 if permission is granted.
2123 */
63c1845b
PM
2124int security_inode_mknod(struct inode *dir, struct dentry *dentry,
2125 umode_t mode, dev_t dev)
20510f2f
JM
2126{
2127 if (unlikely(IS_PRIVATE(dir)))
2128 return 0;
260017f3 2129 return call_int_hook(inode_mknod, dir, dentry, mode, dev);
20510f2f
JM
2130}
2131
916e3258
PM
2132/**
2133 * security_inode_rename() - Check if renaming a file is allowed
2134 * @old_dir: parent directory of the old file
2135 * @old_dentry: the old file
2136 * @new_dir: parent directory of the new file
2137 * @new_dentry: the new file
2138 * @flags: flags
2139 *
2140 * Check for permission to rename a file or directory.
2141 *
2142 * Return: Returns 0 if permission is granted.
2143 */
20510f2f 2144int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
63c1845b
PM
2145 struct inode *new_dir, struct dentry *new_dentry,
2146 unsigned int flags)
20510f2f 2147{
63c1845b
PM
2148 if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
2149 (d_is_positive(new_dentry) &&
2150 IS_PRIVATE(d_backing_inode(new_dentry)))))
20510f2f 2151 return 0;
da1ce067
MS
2152
2153 if (flags & RENAME_EXCHANGE) {
260017f3 2154 int err = call_int_hook(inode_rename, new_dir, new_dentry,
63c1845b 2155 old_dir, old_dentry);
da1ce067
MS
2156 if (err)
2157 return err;
2158 }
2159
260017f3 2160 return call_int_hook(inode_rename, old_dir, old_dentry,
63c1845b 2161 new_dir, new_dentry);
20510f2f
JM
2162}
2163
916e3258
PM
2164/**
2165 * security_inode_readlink() - Check if reading a symbolic link is allowed
2166 * @dentry: link
2167 *
2168 * Check the permission to read the symbolic link.
2169 *
2170 * Return: Returns 0 if permission is granted.
2171 */
20510f2f
JM
2172int security_inode_readlink(struct dentry *dentry)
2173{
c6f493d6 2174 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2175 return 0;
260017f3 2176 return call_int_hook(inode_readlink, dentry);
20510f2f
JM
2177}
2178
916e3258
PM
2179/**
2180 * security_inode_follow_link() - Check if following a symbolic link is allowed
2181 * @dentry: link dentry
2182 * @inode: link inode
2183 * @rcu: true if in RCU-walk mode
2184 *
2185 * Check permission to follow a symbolic link when looking up a pathname. If
2186 * @rcu is true, @inode is not stable.
2187 *
2188 * Return: Returns 0 if permission is granted.
2189 */
bda0be7a
N
2190int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
2191 bool rcu)
20510f2f 2192{
bda0be7a 2193 if (unlikely(IS_PRIVATE(inode)))
20510f2f 2194 return 0;
260017f3 2195 return call_int_hook(inode_follow_link, dentry, inode, rcu);
20510f2f
JM
2196}
2197
916e3258
PM
2198/**
2199 * security_inode_permission() - Check if accessing an inode is allowed
2200 * @inode: inode
2201 * @mask: access mask
2202 *
2203 * Check permission before accessing an inode. This hook is called by the
2204 * existing Linux permission function, so a security module can use it to
2205 * provide additional checking for existing Linux permission checks. Notice
2206 * that this hook is called when a file is opened (as well as many other
2207 * operations), whereas the file_security_ops permission hook is called when
2208 * the actual read/write operations are performed.
2209 *
2210 * Return: Returns 0 if permission is granted.
2211 */
b77b0646 2212int security_inode_permission(struct inode *inode, int mask)
20510f2f
JM
2213{
2214 if (unlikely(IS_PRIVATE(inode)))
2215 return 0;
260017f3 2216 return call_int_hook(inode_permission, inode, mask);
20510f2f
JM
2217}
2218
916e3258
PM
2219/**
2220 * security_inode_setattr() - Check if setting file attributes is allowed
2221 * @idmap: idmap of the mount
2222 * @dentry: file
2223 * @attr: new attributes
2224 *
2225 * Check permission before setting file attributes. Note that the kernel call
2226 * to notify_change is performed from several locations, whenever file
2227 * attributes change (such as when a file is truncated, chown/chmod operations,
2228 * transferring disk quotas, etc).
2229 *
2230 * Return: Returns 0 if permission is granted.
2231 */
c1632a0f 2232int security_inode_setattr(struct mnt_idmap *idmap,
0e363cf3 2233 struct dentry *dentry, struct iattr *attr)
20510f2f 2234{
c6f493d6 2235 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2236 return 0;
260017f3 2237 return call_int_hook(inode_setattr, idmap, dentry, attr);
20510f2f 2238}
b1da47e2 2239EXPORT_SYMBOL_GPL(security_inode_setattr);
20510f2f 2240
77fa6f31
RS
2241/**
2242 * security_inode_post_setattr() - Update the inode after a setattr operation
2243 * @idmap: idmap of the mount
2244 * @dentry: file
2245 * @ia_valid: file attributes set
2246 *
2247 * Update inode security field after successful setting file attributes.
2248 */
2249void security_inode_post_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2250 int ia_valid)
2251{
2252 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2253 return;
2254 call_void_hook(inode_post_setattr, idmap, dentry, ia_valid);
2255}
2256
916e3258
PM
2257/**
2258 * security_inode_getattr() - Check if getting file attributes is allowed
2259 * @path: file
2260 *
2261 * Check permission before obtaining file attributes.
2262 *
2263 * Return: Returns 0 if permission is granted.
2264 */
3f7036a0 2265int security_inode_getattr(const struct path *path)
20510f2f 2266{
c6f493d6 2267 if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
20510f2f 2268 return 0;
260017f3 2269 return call_int_hook(inode_getattr, path);
20510f2f
JM
2270}
2271
916e3258
PM
2272/**
2273 * security_inode_setxattr() - Check if setting file xattrs is allowed
2274 * @idmap: idmap of the mount
2275 * @dentry: file
2276 * @name: xattr name
2277 * @value: xattr value
1e2523d7 2278 * @size: size of xattr value
916e3258
PM
2279 * @flags: flags
2280 *
2281 * Check permission before setting the extended attributes.
2282 *
2283 * Return: Returns 0 if permission is granted.
2284 */
39f60c1c 2285int security_inode_setxattr(struct mnt_idmap *idmap,
71bc356f 2286 struct dentry *dentry, const char *name,
8f0cfa52 2287 const void *value, size_t size, int flags)
20510f2f 2288{
3e1be52d
MZ
2289 int ret;
2290
c6f493d6 2291 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2292 return 0;
b1d9e6b0
CS
2293 /*
2294 * SELinux and Smack integrate the cap call,
2295 * so assume that all LSMs supplying this call do so.
2296 */
260017f3
OM
2297 ret = call_int_hook(inode_setxattr, idmap, dentry, name, value, size,
2298 flags);
b1d9e6b0
CS
2299
2300 if (ret == 1)
2301 ret = cap_inode_setxattr(dentry, name, value, size, flags);
92383111 2302 return ret;
20510f2f
JM
2303}
2304
916e3258
PM
2305/**
2306 * security_inode_set_acl() - Check if setting posix acls is allowed
2307 * @idmap: idmap of the mount
2308 * @dentry: file
2309 * @acl_name: acl name
2310 * @kacl: acl struct
2311 *
2312 * Check permission before setting posix acls, the posix acls in @kacl are
2313 * identified by @acl_name.
2314 *
2315 * Return: Returns 0 if permission is granted.
2316 */
700b7940 2317int security_inode_set_acl(struct mnt_idmap *idmap,
72b3897e
CB
2318 struct dentry *dentry, const char *acl_name,
2319 struct posix_acl *kacl)
2320{
2321 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2322 return 0;
260017f3 2323 return call_int_hook(inode_set_acl, idmap, dentry, acl_name, kacl);
72b3897e
CB
2324}
2325
8b9d0b82
RS
2326/**
2327 * security_inode_post_set_acl() - Update inode security from posix acls set
2328 * @dentry: file
2329 * @acl_name: acl name
2330 * @kacl: acl struct
2331 *
2332 * Update inode security data after successfully setting posix acls on @dentry.
2333 * The posix acls in @kacl are identified by @acl_name.
2334 */
2335void security_inode_post_set_acl(struct dentry *dentry, const char *acl_name,
2336 struct posix_acl *kacl)
2337{
2338 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2339 return;
2340 call_void_hook(inode_post_set_acl, dentry, acl_name, kacl);
72b3897e
CB
2341}
2342
916e3258
PM
2343/**
2344 * security_inode_get_acl() - Check if reading posix acls is allowed
2345 * @idmap: idmap of the mount
2346 * @dentry: file
2347 * @acl_name: acl name
2348 *
2349 * Check permission before getting osix acls, the posix acls are identified by
2350 * @acl_name.
2351 *
2352 * Return: Returns 0 if permission is granted.
2353 */
700b7940 2354int security_inode_get_acl(struct mnt_idmap *idmap,
72b3897e
CB
2355 struct dentry *dentry, const char *acl_name)
2356{
2357 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2358 return 0;
260017f3 2359 return call_int_hook(inode_get_acl, idmap, dentry, acl_name);
72b3897e
CB
2360}
2361
916e3258
PM
2362/**
2363 * security_inode_remove_acl() - Check if removing a posix acl is allowed
2364 * @idmap: idmap of the mount
2365 * @dentry: file
2366 * @acl_name: acl name
2367 *
2368 * Check permission before removing posix acls, the posix acls are identified
2369 * by @acl_name.
2370 *
2371 * Return: Returns 0 if permission is granted.
2372 */
700b7940 2373int security_inode_remove_acl(struct mnt_idmap *idmap,
72b3897e
CB
2374 struct dentry *dentry, const char *acl_name)
2375{
2376 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2377 return 0;
260017f3 2378 return call_int_hook(inode_remove_acl, idmap, dentry, acl_name);
72b3897e
CB
2379}
2380
2d705d80
RS
2381/**
2382 * security_inode_post_remove_acl() - Update inode security after rm posix acls
2383 * @idmap: idmap of the mount
2384 * @dentry: file
2385 * @acl_name: acl name
2386 *
2387 * Update inode security data after successfully removing posix acls on
2388 * @dentry in @idmap. The posix acls are identified by @acl_name.
2389 */
2390void security_inode_post_remove_acl(struct mnt_idmap *idmap,
2391 struct dentry *dentry, const char *acl_name)
2392{
2393 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2394 return;
2395 call_void_hook(inode_post_remove_acl, idmap, dentry, acl_name);
72b3897e
CB
2396}
2397
916e3258
PM
2398/**
2399 * security_inode_post_setxattr() - Update the inode after a setxattr operation
2400 * @dentry: file
2401 * @name: xattr name
2402 * @value: xattr value
2403 * @size: xattr value size
2404 * @flags: flags
2405 *
2406 * Update inode security field after successful setxattr operation.
2407 */
8f0cfa52
DH
2408void security_inode_post_setxattr(struct dentry *dentry, const char *name,
2409 const void *value, size_t size, int flags)
20510f2f 2410{
c6f493d6 2411 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2412 return;
f25fce3e 2413 call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
20510f2f
JM
2414}
2415
916e3258
PM
2416/**
2417 * security_inode_getxattr() - Check if xattr access is allowed
2418 * @dentry: file
2419 * @name: xattr name
2420 *
2421 * Check permission before obtaining the extended attributes identified by
2422 * @name for @dentry.
2423 *
2424 * Return: Returns 0 if permission is granted.
2425 */
8f0cfa52 2426int security_inode_getxattr(struct dentry *dentry, const char *name)
20510f2f 2427{
c6f493d6 2428 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2429 return 0;
260017f3 2430 return call_int_hook(inode_getxattr, dentry, name);
20510f2f
JM
2431}
2432
916e3258
PM
2433/**
2434 * security_inode_listxattr() - Check if listing xattrs is allowed
2435 * @dentry: file
2436 *
2437 * Check permission before obtaining the list of extended attribute names for
2438 * @dentry.
2439 *
2440 * Return: Returns 0 if permission is granted.
2441 */
20510f2f
JM
2442int security_inode_listxattr(struct dentry *dentry)
2443{
c6f493d6 2444 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2445 return 0;
260017f3 2446 return call_int_hook(inode_listxattr, dentry);
20510f2f
JM
2447}
2448
916e3258
PM
2449/**
2450 * security_inode_removexattr() - Check if removing an xattr is allowed
2451 * @idmap: idmap of the mount
2452 * @dentry: file
2453 * @name: xattr name
2454 *
2455 * Check permission before removing the extended attribute identified by @name
2456 * for @dentry.
2457 *
2458 * Return: Returns 0 if permission is granted.
2459 */
39f60c1c 2460int security_inode_removexattr(struct mnt_idmap *idmap,
71bc356f 2461 struct dentry *dentry, const char *name)
20510f2f 2462{
3e1be52d
MZ
2463 int ret;
2464
c6f493d6 2465 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
20510f2f 2466 return 0;
b1d9e6b0
CS
2467 /*
2468 * SELinux and Smack integrate the cap call,
2469 * so assume that all LSMs supplying this call do so.
2470 */
260017f3 2471 ret = call_int_hook(inode_removexattr, idmap, dentry, name);
b1d9e6b0 2472 if (ret == 1)
39f60c1c 2473 ret = cap_inode_removexattr(idmap, dentry, name);
92383111 2474 return ret;
20510f2f
JM
2475}
2476
dae52cbf
RS
2477/**
2478 * security_inode_post_removexattr() - Update the inode after a removexattr op
2479 * @dentry: file
2480 * @name: xattr name
2481 *
2482 * Update the inode after a successful removexattr operation.
2483 */
2484void security_inode_post_removexattr(struct dentry *dentry, const char *name)
2485{
2486 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2487 return;
2488 call_void_hook(inode_post_removexattr, dentry, name);
20510f2f
JM
2489}
2490
916e3258
PM
2491/**
2492 * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2493 * @dentry: associated dentry
2494 *
2495 * Called when an inode has been changed to determine if
2496 * security_inode_killpriv() should be called.
2497 *
2498 * Return: Return <0 on error to abort the inode change operation, return 0 if
2499 * security_inode_killpriv() does not need to be called, return >0 if
2500 * security_inode_killpriv() does need to be called.
2501 */
b5376771
SH
2502int security_inode_need_killpriv(struct dentry *dentry)
2503{
260017f3 2504 return call_int_hook(inode_need_killpriv, dentry);
b5376771
SH
2505}
2506
916e3258
PM
2507/**
2508 * security_inode_killpriv() - The setuid bit is removed, update LSM state
2509 * @idmap: idmap of the mount
2510 * @dentry: associated dentry
2511 *
2512 * The @dentry's setuid bit is being removed. Remove similar security labels.
2513 * Called with the dentry->d_inode->i_mutex held.
2514 *
2515 * Return: Return 0 on success. If error is returned, then the operation
2516 * causing setuid bit removal is failed.
2517 */
39f60c1c 2518int security_inode_killpriv(struct mnt_idmap *idmap,
71bc356f 2519 struct dentry *dentry)
b5376771 2520{
260017f3 2521 return call_int_hook(inode_killpriv, idmap, dentry);
b5376771
SH
2522}
2523
916e3258
PM
2524/**
2525 * security_inode_getsecurity() - Get the xattr security label of an inode
2526 * @idmap: idmap of the mount
2527 * @inode: inode
2528 * @name: xattr name
2529 * @buffer: security label buffer
2530 * @alloc: allocation flag
2531 *
2532 * Retrieve a copy of the extended attribute representation of the security
2533 * label associated with @name for @inode via @buffer. Note that @name is the
2534 * remainder of the attribute name after the security prefix has been removed.
2535 * @alloc is used to specify if the call should return a value via the buffer
2536 * or just the value length.
2537 *
2538 * Return: Returns size of buffer on success.
2539 */
4609e1f1 2540int security_inode_getsecurity(struct mnt_idmap *idmap,
71bc356f
CB
2541 struct inode *inode, const char *name,
2542 void **buffer, bool alloc)
20510f2f
JM
2543{
2544 if (unlikely(IS_PRIVATE(inode)))
98e828a0 2545 return LSM_RET_DEFAULT(inode_getsecurity);
260017f3
OM
2546
2547 return call_int_hook(inode_getsecurity, idmap, inode, name, buffer,
2548 alloc);
20510f2f
JM
2549}
2550
916e3258
PM
2551/**
2552 * security_inode_setsecurity() - Set the xattr security label of an inode
2553 * @inode: inode
2554 * @name: xattr name
2555 * @value: security label
2556 * @size: length of security label
2557 * @flags: flags
2558 *
2559 * Set the security label associated with @name for @inode from the extended
2560 * attribute value @value. @size indicates the size of the @value in bytes.
2561 * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2562 * remainder of the attribute name after the security. prefix has been removed.
2563 *
2564 * Return: Returns 0 on success.
2565 */
63c1845b
PM
2566int security_inode_setsecurity(struct inode *inode, const char *name,
2567 const void *value, size_t size, int flags)
20510f2f
JM
2568{
2569 if (unlikely(IS_PRIVATE(inode)))
98e828a0 2570 return LSM_RET_DEFAULT(inode_setsecurity);
260017f3
OM
2571
2572 return call_int_hook(inode_setsecurity, inode, name, value, size,
2573 flags);
20510f2f
JM
2574}
2575
916e3258
PM
2576/**
2577 * security_inode_listsecurity() - List the xattr security label names
2578 * @inode: inode
2579 * @buffer: buffer
2580 * @buffer_size: size of buffer
2581 *
2582 * Copy the extended attribute names for the security labels associated with
2583 * @inode into @buffer. The maximum size of @buffer is specified by
2584 * @buffer_size. @buffer may be NULL to request the size of the buffer
2585 * required.
2586 *
2587 * Return: Returns number of bytes used/required on success.
2588 */
63c1845b
PM
2589int security_inode_listsecurity(struct inode *inode,
2590 char *buffer, size_t buffer_size)
20510f2f
JM
2591{
2592 if (unlikely(IS_PRIVATE(inode)))
2593 return 0;
260017f3 2594 return call_int_hook(inode_listsecurity, inode, buffer, buffer_size);
20510f2f 2595}
c9bccef6 2596EXPORT_SYMBOL(security_inode_listsecurity);
20510f2f 2597
916e3258
PM
2598/**
2599 * security_inode_getsecid() - Get an inode's secid
2600 * @inode: inode
2601 * @secid: secid to return
2602 *
2603 * Get the secid associated with the node. In case of failure, @secid will be
2604 * set to zero.
2605 */
d6335d77 2606void security_inode_getsecid(struct inode *inode, u32 *secid)
8a076191 2607{
f25fce3e 2608 call_void_hook(inode_getsecid, inode, secid);
8a076191
AD
2609}
2610
916e3258
PM
2611/**
2612 * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2613 * @src: union dentry of copy-up file
2614 * @new: newly created creds
2615 *
2616 * A file is about to be copied up from lower layer to upper layer of overlay
2617 * filesystem. Security module can prepare a set of new creds and modify as
2618 * need be and return new creds. Caller will switch to new creds temporarily to
2619 * create new file and release newly allocated creds.
2620 *
2621 * Return: Returns 0 on success or a negative error code on error.
2622 */
d8ad8b49
VG
2623int security_inode_copy_up(struct dentry *src, struct cred **new)
2624{
260017f3 2625 return call_int_hook(inode_copy_up, src, new);
d8ad8b49
VG
2626}
2627EXPORT_SYMBOL(security_inode_copy_up);
2628
916e3258
PM
2629/**
2630 * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
32538047 2631 * @src: union dentry of copy-up file
916e3258
PM
2632 * @name: xattr name
2633 *
2634 * Filter the xattrs being copied up when a unioned file is copied up from a
2635 * lower layer to the union/overlay layer. The caller is responsible for
2636 * reading and writing the xattrs, this hook is merely a filter.
2637 *
2638 * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2639 * if the security module does not know about attribute, or a negative
2640 * error code to abort the copy up.
2641 */
32538047 2642int security_inode_copy_up_xattr(struct dentry *src, const char *name)
121ab822 2643{
23e390cd
KS
2644 int rc;
2645
2646 /*
2647 * The implementation can return 0 (accept the xattr), 1 (discard the
2648 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
4432b507 2649 * any other error code in case of an error.
23e390cd 2650 */
32538047 2651 rc = call_int_hook(inode_copy_up_xattr, src, name);
260017f3
OM
2652 if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
2653 return rc;
23e390cd 2654
92383111 2655 return LSM_RET_DEFAULT(inode_copy_up_xattr);
121ab822
VG
2656}
2657EXPORT_SYMBOL(security_inode_copy_up_xattr);
2658
9348944b
PM
2659/**
2660 * security_kernfs_init_security() - Init LSM context for a kernfs node
2661 * @kn_dir: parent kernfs node
2662 * @kn: the kernfs node to initialize
2663 *
2664 * Initialize the security context of a newly created kernfs node based on its
2665 * own and its parent's attributes.
2666 *
2667 * Return: Returns 0 if permission is granted.
2668 */
b230d5ab
OM
2669int security_kernfs_init_security(struct kernfs_node *kn_dir,
2670 struct kernfs_node *kn)
2671{
260017f3 2672 return call_int_hook(kernfs_init_security, kn_dir, kn);
b230d5ab
OM
2673}
2674
a0fd6480
PM
2675/**
2676 * security_file_permission() - Check file permissions
2677 * @file: file
2678 * @mask: requested permissions
2679 *
2680 * Check file permissions before accessing an open file. This hook is called
2681 * by various operations that read or write files. A security module can use
2682 * this hook to perform additional checking on these operations, e.g. to
2683 * revalidate permissions on use to support privilege bracketing or policy
2684 * changes. Notice that this hook is used when the actual read/write
2685 * operations are performed, whereas the inode_security_ops hook is called when
2686 * a file is opened (as well as many other operations). Although this hook can
2687 * be used to revalidate permissions for various system call operations that
2688 * read or write files, it does not address the revalidation of permissions for
2689 * memory-mapped files. Security modules must handle this separately if they
2690 * need such revalidation.
2691 *
2692 * Return: Returns 0 if permission is granted.
2693 */
20510f2f
JM
2694int security_file_permission(struct file *file, int mask)
2695{
260017f3 2696 return call_int_hook(file_permission, file, mask);
20510f2f
JM
2697}
2698
a0fd6480
PM
2699/**
2700 * security_file_alloc() - Allocate and init a file's LSM blob
2701 * @file: the file
2702 *
2703 * Allocate and attach a security structure to the file->f_security field. The
2704 * security field is initialized to NULL when the structure is first created.
2705 *
2706 * Return: Return 0 if the hook is successful and permission is granted.
2707 */
20510f2f
JM
2708int security_file_alloc(struct file *file)
2709{
33bf60ca
CS
2710 int rc = lsm_file_alloc(file);
2711
2712 if (rc)
2713 return rc;
260017f3 2714 rc = call_int_hook(file_alloc_security, file);
33bf60ca
CS
2715 if (unlikely(rc))
2716 security_file_free(file);
2717 return rc;
20510f2f
JM
2718}
2719
f09068b5
RS
2720/**
2721 * security_file_release() - Perform actions before releasing the file ref
2722 * @file: the file
2723 *
2724 * Perform actions before releasing the last reference to a file.
2725 */
2726void security_file_release(struct file *file)
2727{
2728 call_void_hook(file_release, file);
2729}
2730
a0fd6480
PM
2731/**
2732 * security_file_free() - Free a file's LSM blob
2733 * @file: the file
2734 *
2735 * Deallocate and free any security structures stored in file->f_security.
2736 */
20510f2f
JM
2737void security_file_free(struct file *file)
2738{
33bf60ca
CS
2739 void *blob;
2740
f25fce3e 2741 call_void_hook(file_free_security, file);
33bf60ca
CS
2742
2743 blob = file->f_security;
2744 if (blob) {
2745 file->f_security = NULL;
2746 kmem_cache_free(lsm_file_cache, blob);
2747 }
20510f2f
JM
2748}
2749
a0fd6480
PM
2750/**
2751 * security_file_ioctl() - Check if an ioctl is allowed
2752 * @file: associated file
2753 * @cmd: ioctl cmd
2754 * @arg: ioctl arguments
2755 *
2756 * Check permission for an ioctl operation on @file. Note that @arg sometimes
2757 * represents a user space pointer; in other cases, it may be a simple integer
2758 * value. When @arg represents a user space pointer, it should never be used
2759 * by the security module.
2760 *
2761 * Return: Returns 0 if permission is granted.
2762 */
20510f2f
JM
2763int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2764{
260017f3 2765 return call_int_hook(file_ioctl, file, cmd, arg);
20510f2f 2766}
292f902a 2767EXPORT_SYMBOL_GPL(security_file_ioctl);
20510f2f 2768
f1bb47a3
AP
2769/**
2770 * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
2771 * @file: associated file
2772 * @cmd: ioctl cmd
2773 * @arg: ioctl arguments
2774 *
2775 * Compat version of security_file_ioctl() that correctly handles 32-bit
2776 * processes running on 64-bit kernels.
2777 *
2778 * Return: Returns 0 if permission is granted.
2779 */
2780int security_file_ioctl_compat(struct file *file, unsigned int cmd,
2781 unsigned long arg)
2782{
260017f3 2783 return call_int_hook(file_ioctl_compat, file, cmd, arg);
f1bb47a3
AP
2784}
2785EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
2786
98de59bf 2787static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
20510f2f 2788{
8b3ec681 2789 /*
98de59bf
AV
2790 * Does we have PROT_READ and does the application expect
2791 * it to imply PROT_EXEC? If not, nothing to talk about...
8b3ec681 2792 */
98de59bf
AV
2793 if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
2794 return prot;
8b3ec681 2795 if (!(current->personality & READ_IMPLIES_EXEC))
98de59bf
AV
2796 return prot;
2797 /*
2798 * if that's an anonymous mapping, let it.
2799 */
2800 if (!file)
2801 return prot | PROT_EXEC;
2802 /*
2803 * ditto if it's not on noexec mount, except that on !MMU we need
b4caecd4 2804 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
98de59bf 2805 */
90f8572b 2806 if (!path_noexec(&file->f_path)) {
8b3ec681 2807#ifndef CONFIG_MMU
b4caecd4
CH
2808 if (file->f_op->mmap_capabilities) {
2809 unsigned caps = file->f_op->mmap_capabilities(file);
2810 if (!(caps & NOMMU_MAP_EXEC))
2811 return prot;
2812 }
8b3ec681 2813#endif
98de59bf 2814 return prot | PROT_EXEC;
8b3ec681 2815 }
98de59bf
AV
2816 /* anything on noexec mount won't get PROT_EXEC */
2817 return prot;
2818}
2819
a0fd6480
PM
2820/**
2821 * security_mmap_file() - Check if mmap'ing a file is allowed
2822 * @file: file
2823 * @prot: protection applied by the kernel
2824 * @flags: flags
2825 *
2826 * Check permissions for a mmap operation. The @file may be NULL, e.g. if
2827 * mapping anonymous memory.
2828 *
2829 * Return: Returns 0 if permission is granted.
2830 */
98de59bf 2831int security_mmap_file(struct file *file, unsigned long prot,
63c1845b 2832 unsigned long flags)
98de59bf 2833{
260017f3 2834 return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot),
cd3cec0a 2835 flags);
20510f2f
JM
2836}
2837
a0fd6480
PM
2838/**
2839 * security_mmap_addr() - Check if mmap'ing an address is allowed
2840 * @addr: address
2841 *
2842 * Check permissions for a mmap operation at @addr.
2843 *
2844 * Return: Returns 0 if permission is granted.
2845 */
e5467859
AV
2846int security_mmap_addr(unsigned long addr)
2847{
260017f3 2848 return call_int_hook(mmap_addr, addr);
e5467859
AV
2849}
2850
a0fd6480
PM
2851/**
2852 * security_file_mprotect() - Check if changing memory protections is allowed
2853 * @vma: memory region
2854 * @reqprot: application requested protection
1e2523d7 2855 * @prot: protection applied by the kernel
a0fd6480
PM
2856 *
2857 * Check permissions before changing memory access permissions.
2858 *
2859 * Return: Returns 0 if permission is granted.
2860 */
20510f2f 2861int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
63c1845b 2862 unsigned long prot)
20510f2f 2863{
260017f3 2864 return call_int_hook(file_mprotect, vma, reqprot, prot);
20510f2f
JM
2865}
2866
a0fd6480
PM
2867/**
2868 * security_file_lock() - Check if a file lock is allowed
2869 * @file: file
2870 * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2871 *
2872 * Check permission before performing file locking operations. Note the hook
2873 * mediates both flock and fcntl style locks.
2874 *
2875 * Return: Returns 0 if permission is granted.
2876 */
20510f2f
JM
2877int security_file_lock(struct file *file, unsigned int cmd)
2878{
260017f3 2879 return call_int_hook(file_lock, file, cmd);
20510f2f
JM
2880}
2881
a0fd6480
PM
2882/**
2883 * security_file_fcntl() - Check if fcntl() op is allowed
2884 * @file: file
ff72942c 2885 * @cmd: fcntl command
a0fd6480
PM
2886 * @arg: command argument
2887 *
2888 * Check permission before allowing the file operation specified by @cmd from
2889 * being performed on the file @file. Note that @arg sometimes represents a
2890 * user space pointer; in other cases, it may be a simple integer value. When
2891 * @arg represents a user space pointer, it should never be used by the
2892 * security module.
2893 *
2894 * Return: Returns 0 if permission is granted.
2895 */
20510f2f
JM
2896int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2897{
260017f3 2898 return call_int_hook(file_fcntl, file, cmd, arg);
20510f2f
JM
2899}
2900
a0fd6480
PM
2901/**
2902 * security_file_set_fowner() - Set the file owner info in the LSM blob
2903 * @file: the file
2904 *
2905 * Save owner security information (typically from current->security) in
2906 * file->f_security for later use by the send_sigiotask hook.
2907 *
2908 * Return: Returns 0 on success.
2909 */
e0b93edd 2910void security_file_set_fowner(struct file *file)
20510f2f 2911{
f25fce3e 2912 call_void_hook(file_set_fowner, file);
20510f2f
JM
2913}
2914
a0fd6480
PM
2915/**
2916 * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2917 * @tsk: target task
2918 * @fown: signal sender
2919 * @sig: signal to be sent, SIGIO is sent if 0
2920 *
2921 * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2922 * process @tsk. Note that this hook is sometimes called from interrupt. Note
2923 * that the fown_struct, @fown, is never outside the context of a struct file,
2924 * so the file structure (and associated security information) can always be
2925 * obtained: container_of(fown, struct file, f_owner).
2926 *
2927 * Return: Returns 0 if permission is granted.
2928 */
20510f2f 2929int security_file_send_sigiotask(struct task_struct *tsk,
63c1845b 2930 struct fown_struct *fown, int sig)
20510f2f 2931{
260017f3 2932 return call_int_hook(file_send_sigiotask, tsk, fown, sig);
20510f2f
JM
2933}
2934
a0fd6480 2935/**
936615f6 2936 * security_file_receive() - Check if receiving a file via IPC is allowed
a0fd6480
PM
2937 * @file: file being received
2938 *
2939 * This hook allows security modules to control the ability of a process to
2940 * receive an open file descriptor via socket IPC.
2941 *
2942 * Return: Returns 0 if permission is granted.
2943 */
20510f2f
JM
2944int security_file_receive(struct file *file)
2945{
260017f3 2946 return call_int_hook(file_receive, file);
20510f2f
JM
2947}
2948
a0fd6480
PM
2949/**
2950 * security_file_open() - Save open() time state for late use by the LSM
2951 * @file:
2952 *
2953 * Save open-time permission checking state for later use upon file_permission,
2954 * and recheck access if anything has changed since inode_permission.
2955 *
2956 * Return: Returns 0 if permission is granted.
2957 */
e3f20ae2 2958int security_file_open(struct file *file)
20510f2f 2959{
c4ec54b4
EP
2960 int ret;
2961
260017f3 2962 ret = call_int_hook(file_open, file);
c4ec54b4
EP
2963 if (ret)
2964 return ret;
2965
36e28c42 2966 return fsnotify_open_perm(file);
20510f2f
JM
2967}
2968
8f46ff57
RS
2969/**
2970 * security_file_post_open() - Evaluate a file after it has been opened
2971 * @file: the file
2972 * @mask: access mask
2973 *
2974 * Evaluate an opened file and the access mask requested with open(). The hook
2975 * is useful for LSMs that require the file content to be available in order to
2976 * make decisions.
2977 *
2978 * Return: Returns 0 if permission is granted.
2979 */
2980int security_file_post_open(struct file *file, int mask)
2981{
260017f3 2982 return call_int_hook(file_post_open, file, mask);
8f46ff57
RS
2983}
2984EXPORT_SYMBOL_GPL(security_file_post_open);
2985
a0fd6480
PM
2986/**
2987 * security_file_truncate() - Check if truncating a file is allowed
2988 * @file: file
2989 *
2990 * Check permission before truncating a file, i.e. using ftruncate. Note that
2991 * truncation permission may also be checked based on the path, using the
2992 * @path_truncate hook.
2993 *
2994 * Return: Returns 0 if permission is granted.
2995 */
3350607d
GN
2996int security_file_truncate(struct file *file)
2997{
260017f3 2998 return call_int_hook(file_truncate, file);
3350607d
GN
2999}
3000
130c53bf
PM
3001/**
3002 * security_task_alloc() - Allocate a task's LSM blob
3003 * @task: the task
3004 * @clone_flags: flags indicating what is being shared
3005 *
3006 * Handle allocation of task-related resources.
3007 *
3008 * Return: Returns a zero on success, negative values on failure.
3009 */
e4e55b47
TH
3010int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
3011{
f4ad8f2c
CS
3012 int rc = lsm_task_alloc(task);
3013
3014 if (rc)
3015 return rc;
260017f3 3016 rc = call_int_hook(task_alloc, task, clone_flags);
f4ad8f2c
CS
3017 if (unlikely(rc))
3018 security_task_free(task);
3019 return rc;
e4e55b47
TH
3020}
3021
130c53bf
PM
3022/**
3023 * security_task_free() - Free a task's LSM blob and related resources
3024 * @task: task
3025 *
3026 * Handle release of task-related resources. Note that this can be called from
3027 * interrupt context.
3028 */
1a2a4d06
KC
3029void security_task_free(struct task_struct *task)
3030{
f25fce3e 3031 call_void_hook(task_free, task);
f4ad8f2c
CS
3032
3033 kfree(task->security);
3034 task->security = NULL;
1a2a4d06
KC
3035}
3036
130c53bf
PM
3037/**
3038 * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
3039 * @cred: credentials
3040 * @gfp: gfp flags
3041 *
3042 * Only allocate sufficient memory and attach to @cred such that
3043 * cred_transfer() will not get ENOMEM.
3044 *
3045 * Return: Returns 0 on success, negative values on failure.
3046 */
ee18d64c
DH
3047int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3048{
bbd3662a
CS
3049 int rc = lsm_cred_alloc(cred, gfp);
3050
3051 if (rc)
3052 return rc;
3053
260017f3 3054 rc = call_int_hook(cred_alloc_blank, cred, gfp);
33bf60ca 3055 if (unlikely(rc))
bbd3662a
CS
3056 security_cred_free(cred);
3057 return rc;
ee18d64c
DH
3058}
3059
130c53bf
PM
3060/**
3061 * security_cred_free() - Free the cred's LSM blob and associated resources
3062 * @cred: credentials
3063 *
3064 * Deallocate and clear the cred->security field in a set of credentials.
3065 */
d84f4f99 3066void security_cred_free(struct cred *cred)
20510f2f 3067{
a5795fd3
JM
3068 /*
3069 * There is a failure case in prepare_creds() that
3070 * may result in a call here with ->security being NULL.
3071 */
3072 if (unlikely(cred->security == NULL))
3073 return;
3074
f25fce3e 3075 call_void_hook(cred_free, cred);
bbd3662a
CS
3076
3077 kfree(cred->security);
3078 cred->security = NULL;
20510f2f
JM
3079}
3080
130c53bf
PM
3081/**
3082 * security_prepare_creds() - Prepare a new set of credentials
3083 * @new: new credentials
3084 * @old: original credentials
3085 * @gfp: gfp flags
3086 *
3087 * Prepare a new set of credentials by copying the data from the old set.
3088 *
3089 * Return: Returns 0 on success, negative values on failure.
3090 */
d84f4f99 3091int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
20510f2f 3092{
bbd3662a
CS
3093 int rc = lsm_cred_alloc(new, gfp);
3094
3095 if (rc)
3096 return rc;
3097
260017f3 3098 rc = call_int_hook(cred_prepare, new, old, gfp);
33bf60ca 3099 if (unlikely(rc))
bbd3662a
CS
3100 security_cred_free(new);
3101 return rc;
d84f4f99
DH
3102}
3103
130c53bf
PM
3104/**
3105 * security_transfer_creds() - Transfer creds
3106 * @new: target credentials
3107 * @old: original credentials
3108 *
3109 * Transfer data from original creds to new creds.
3110 */
ee18d64c
DH
3111void security_transfer_creds(struct cred *new, const struct cred *old)
3112{
f25fce3e 3113 call_void_hook(cred_transfer, new, old);
ee18d64c
DH
3114}
3115
130c53bf
PM
3116/**
3117 * security_cred_getsecid() - Get the secid from a set of credentials
3118 * @c: credentials
3119 * @secid: secid value
3120 *
3121 * Retrieve the security identifier of the cred structure @c. In case of
3122 * failure, @secid will be set to zero.
3123 */
3ec30113
MG
3124void security_cred_getsecid(const struct cred *c, u32 *secid)
3125{
3126 *secid = 0;
3127 call_void_hook(cred_getsecid, c, secid);
3128}
3129EXPORT_SYMBOL(security_cred_getsecid);
3130
130c53bf
PM
3131/**
3132 * security_kernel_act_as() - Set the kernel credentials to act as secid
3133 * @new: credentials
3134 * @secid: secid
3135 *
3136 * Set the credentials for a kernel service to act as (subjective context).
3137 * The current task must be the one that nominated @secid.
3138 *
3139 * Return: Returns 0 if successful.
3140 */
3a3b7ce9
DH
3141int security_kernel_act_as(struct cred *new, u32 secid)
3142{
260017f3 3143 return call_int_hook(kernel_act_as, new, secid);
3a3b7ce9
DH
3144}
3145
130c53bf
PM
3146/**
3147 * security_kernel_create_files_as() - Set file creation context using an inode
3148 * @new: target credentials
3149 * @inode: reference inode
3150 *
3151 * Set the file creation context in a set of credentials to be the same as the
3152 * objective context of the specified inode. The current task must be the one
3153 * that nominated @inode.
3154 *
3155 * Return: Returns 0 if successful.
3156 */
3a3b7ce9
DH
3157int security_kernel_create_files_as(struct cred *new, struct inode *inode)
3158{
260017f3 3159 return call_int_hook(kernel_create_files_as, new, inode);
3a3b7ce9
DH
3160}
3161
130c53bf 3162/**
936615f6 3163 * security_kernel_module_request() - Check if loading a module is allowed
130c53bf
PM
3164 * @kmod_name: module name
3165 *
3166 * Ability to trigger the kernel to automatically upcall to userspace for
3167 * userspace to load a kernel module with the given name.
3168 *
3169 * Return: Returns 0 if successful.
3170 */
dd8dbf2e 3171int security_kernel_module_request(char *kmod_name)
9188499c 3172{
260017f3 3173 return call_int_hook(kernel_module_request, kmod_name);
9188499c
EP
3174}
3175
130c53bf
PM
3176/**
3177 * security_kernel_read_file() - Read a file specified by userspace
3178 * @file: file
3179 * @id: file identifier
3180 * @contents: trust if security_kernel_post_read_file() will be called
3181 *
3182 * Read a file specified by userspace.
3183 *
3184 * Return: Returns 0 if permission is granted.
3185 */
2039bda1
KC
3186int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
3187 bool contents)
39eeb4fb 3188{
260017f3 3189 return call_int_hook(kernel_read_file, file, id, contents);
39eeb4fb
MZ
3190}
3191EXPORT_SYMBOL_GPL(security_kernel_read_file);
3192
130c53bf
PM
3193/**
3194 * security_kernel_post_read_file() - Read a file specified by userspace
3195 * @file: file
3196 * @buf: file contents
3197 * @size: size of file contents
3198 * @id: file identifier
3199 *
3200 * Read a file specified by userspace. This must be paired with a prior call
3201 * to security_kernel_read_file() call that indicated this hook would also be
3202 * called, see security_kernel_read_file() for more information.
3203 *
3204 * Return: Returns 0 if permission is granted.
3205 */
bc8ca5b9
MZ
3206int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3207 enum kernel_read_file_id id)
b44a7dfc 3208{
260017f3 3209 return call_int_hook(kernel_post_read_file, file, buf, size, id);
b44a7dfc
MZ
3210}
3211EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3212
130c53bf
PM
3213/**
3214 * security_kernel_load_data() - Load data provided by userspace
3215 * @id: data identifier
3216 * @contents: true if security_kernel_post_load_data() will be called
3217 *
3218 * Load data provided by userspace.
3219 *
3220 * Return: Returns 0 if permission is granted.
3221 */
b64fcae7 3222int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
377179cd 3223{
260017f3 3224 return call_int_hook(kernel_load_data, id, contents);
377179cd 3225}
83a68a06 3226EXPORT_SYMBOL_GPL(security_kernel_load_data);
377179cd 3227
130c53bf
PM
3228/**
3229 * security_kernel_post_load_data() - Load userspace data from a non-file source
3230 * @buf: data
3231 * @size: size of data
3232 * @id: data identifier
3233 * @description: text description of data, specific to the id value
3234 *
3235 * Load data provided by a non-file source (usually userspace buffer). This
3236 * must be paired with a prior security_kernel_load_data() call that indicated
3237 * this hook would also be called, see security_kernel_load_data() for more
3238 * information.
3239 *
3240 * Return: Returns 0 if permission is granted.
3241 */
b64fcae7
KC
3242int security_kernel_post_load_data(char *buf, loff_t size,
3243 enum kernel_load_data_id id,
3244 char *description)
3245{
260017f3 3246 return call_int_hook(kernel_post_load_data, buf, size, id, description);
b64fcae7
KC
3247}
3248EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3249
130c53bf
PM
3250/**
3251 * security_task_fix_setuid() - Update LSM with new user id attributes
3252 * @new: updated credentials
3253 * @old: credentials being replaced
3254 * @flags: LSM_SETID_* flag values
3255 *
3256 * Update the module's state after setting one or more of the user identity
3257 * attributes of the current process. The @flags parameter indicates which of
3258 * the set*uid system calls invoked this hook. If @new is the set of
3259 * credentials that will be installed. Modifications should be made to this
3260 * rather than to @current->cred.
3261 *
3262 * Return: Returns 0 on success.
3263 */
d84f4f99
DH
3264int security_task_fix_setuid(struct cred *new, const struct cred *old,
3265 int flags)
20510f2f 3266{
260017f3 3267 return call_int_hook(task_fix_setuid, new, old, flags);
20510f2f
JM
3268}
3269
130c53bf
PM
3270/**
3271 * security_task_fix_setgid() - Update LSM with new group id attributes
3272 * @new: updated credentials
3273 * @old: credentials being replaced
3274 * @flags: LSM_SETID_* flag value
3275 *
3276 * Update the module's state after setting one or more of the group identity
3277 * attributes of the current process. The @flags parameter indicates which of
3278 * the set*gid system calls invoked this hook. @new is the set of credentials
3279 * that will be installed. Modifications should be made to this rather than to
3280 * @current->cred.
3281 *
3282 * Return: Returns 0 on success.
3283 */
39030e13 3284int security_task_fix_setgid(struct cred *new, const struct cred *old,
63c1845b 3285 int flags)
39030e13 3286{
260017f3 3287 return call_int_hook(task_fix_setgid, new, old, flags);
39030e13
TC
3288}
3289
130c53bf
PM
3290/**
3291 * security_task_fix_setgroups() - Update LSM with new supplementary groups
3292 * @new: updated credentials
3293 * @old: credentials being replaced
3294 *
3295 * Update the module's state after setting the supplementary group identity
3296 * attributes of the current process. @new is the set of credentials that will
3297 * be installed. Modifications should be made to this rather than to
3298 * @current->cred.
3299 *
3300 * Return: Returns 0 on success.
3301 */
fcfe0ac2
MM
3302int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3303{
260017f3 3304 return call_int_hook(task_fix_setgroups, new, old);
fcfe0ac2
MM
3305}
3306
130c53bf
PM
3307/**
3308 * security_task_setpgid() - Check if setting the pgid is allowed
3309 * @p: task being modified
3310 * @pgid: new pgid
3311 *
3312 * Check permission before setting the process group identifier of the process
3313 * @p to @pgid.
3314 *
3315 * Return: Returns 0 if permission is granted.
3316 */
20510f2f
JM
3317int security_task_setpgid(struct task_struct *p, pid_t pgid)
3318{
260017f3 3319 return call_int_hook(task_setpgid, p, pgid);
20510f2f
JM
3320}
3321
130c53bf
PM
3322/**
3323 * security_task_getpgid() - Check if getting the pgid is allowed
3324 * @p: task
3325 *
3326 * Check permission before getting the process group identifier of the process
3327 * @p.
3328 *
3329 * Return: Returns 0 if permission is granted.
3330 */
20510f2f
JM
3331int security_task_getpgid(struct task_struct *p)
3332{
260017f3 3333 return call_int_hook(task_getpgid, p);
20510f2f
JM
3334}
3335
130c53bf
PM
3336/**
3337 * security_task_getsid() - Check if getting the session id is allowed
3338 * @p: task
3339 *
3340 * Check permission before getting the session identifier of the process @p.
3341 *
3342 * Return: Returns 0 if permission is granted.
3343 */
20510f2f
JM
3344int security_task_getsid(struct task_struct *p)
3345{
260017f3 3346 return call_int_hook(task_getsid, p);
20510f2f
JM
3347}
3348
130c53bf
PM
3349/**
3350 * security_current_getsecid_subj() - Get the current task's subjective secid
3351 * @secid: secid value
3352 *
3353 * Retrieve the subjective security identifier of the current task and return
3354 * it in @secid. In case of failure, @secid will be set to zero.
3355 */
6326948f 3356void security_current_getsecid_subj(u32 *secid)
20510f2f 3357{
b1d9e6b0 3358 *secid = 0;
6326948f 3359 call_void_hook(current_getsecid_subj, secid);
20510f2f 3360}
6326948f 3361EXPORT_SYMBOL(security_current_getsecid_subj);
4ebd7651 3362
130c53bf
PM
3363/**
3364 * security_task_getsecid_obj() - Get a task's objective secid
3365 * @p: target task
3366 * @secid: secid value
3367 *
3368 * Retrieve the objective security identifier of the task_struct in @p and
3369 * return it in @secid. In case of failure, @secid will be set to zero.
3370 */
4ebd7651
PM
3371void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
3372{
3373 *secid = 0;
3374 call_void_hook(task_getsecid_obj, p, secid);
3375}
3376EXPORT_SYMBOL(security_task_getsecid_obj);
20510f2f 3377
130c53bf
PM
3378/**
3379 * security_task_setnice() - Check if setting a task's nice value is allowed
3380 * @p: target task
3381 * @nice: nice value
3382 *
3383 * Check permission before setting the nice value of @p to @nice.
3384 *
3385 * Return: Returns 0 if permission is granted.
3386 */
20510f2f
JM
3387int security_task_setnice(struct task_struct *p, int nice)
3388{
260017f3 3389 return call_int_hook(task_setnice, p, nice);
20510f2f
JM
3390}
3391
130c53bf
PM
3392/**
3393 * security_task_setioprio() - Check if setting a task's ioprio is allowed
3394 * @p: target task
3395 * @ioprio: ioprio value
3396 *
3397 * Check permission before setting the ioprio value of @p to @ioprio.
3398 *
3399 * Return: Returns 0 if permission is granted.
3400 */
20510f2f
JM
3401int security_task_setioprio(struct task_struct *p, int ioprio)
3402{
260017f3 3403 return call_int_hook(task_setioprio, p, ioprio);
20510f2f
JM
3404}
3405
130c53bf
PM
3406/**
3407 * security_task_getioprio() - Check if getting a task's ioprio is allowed
3408 * @p: task
3409 *
3410 * Check permission before getting the ioprio value of @p.
3411 *
3412 * Return: Returns 0 if permission is granted.
3413 */
20510f2f
JM
3414int security_task_getioprio(struct task_struct *p)
3415{
260017f3 3416 return call_int_hook(task_getioprio, p);
20510f2f
JM
3417}
3418
130c53bf
PM
3419/**
3420 * security_task_prlimit() - Check if get/setting resources limits is allowed
3421 * @cred: current task credentials
3422 * @tcred: target task credentials
3423 * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3424 *
3425 * Check permission before getting and/or setting the resource limits of
3426 * another task.
3427 *
3428 * Return: Returns 0 if permission is granted.
3429 */
791ec491
SS
3430int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3431 unsigned int flags)
3432{
260017f3 3433 return call_int_hook(task_prlimit, cred, tcred, flags);
791ec491
SS
3434}
3435
130c53bf
PM
3436/**
3437 * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3438 * @p: target task's group leader
3439 * @resource: resource whose limit is being set
3440 * @new_rlim: new resource limit
3441 *
3442 * Check permission before setting the resource limits of process @p for
3443 * @resource to @new_rlim. The old resource limit values can be examined by
3444 * dereferencing (p->signal->rlim + resource).
3445 *
3446 * Return: Returns 0 if permission is granted.
3447 */
8fd00b4d 3448int security_task_setrlimit(struct task_struct *p, unsigned int resource,
63c1845b 3449 struct rlimit *new_rlim)
20510f2f 3450{
260017f3 3451 return call_int_hook(task_setrlimit, p, resource, new_rlim);
20510f2f
JM
3452}
3453
130c53bf
PM
3454/**
3455 * security_task_setscheduler() - Check if setting sched policy/param is allowed
3456 * @p: target task
3457 *
3458 * Check permission before setting scheduling policy and/or parameters of
3459 * process @p.
3460 *
3461 * Return: Returns 0 if permission is granted.
3462 */
b0ae1981 3463int security_task_setscheduler(struct task_struct *p)
20510f2f 3464{
260017f3 3465 return call_int_hook(task_setscheduler, p);
20510f2f
JM
3466}
3467
130c53bf
PM
3468/**
3469 * security_task_getscheduler() - Check if getting scheduling info is allowed
3470 * @p: target task
3471 *
3472 * Check permission before obtaining scheduling information for process @p.
3473 *
3474 * Return: Returns 0 if permission is granted.
3475 */
20510f2f
JM
3476int security_task_getscheduler(struct task_struct *p)
3477{
260017f3 3478 return call_int_hook(task_getscheduler, p);
20510f2f
JM
3479}
3480
130c53bf
PM
3481/**
3482 * security_task_movememory() - Check if moving memory is allowed
3483 * @p: task
3484 *
3485 * Check permission before moving memory owned by process @p.
3486 *
3487 * Return: Returns 0 if permission is granted.
3488 */
20510f2f
JM
3489int security_task_movememory(struct task_struct *p)
3490{
260017f3 3491 return call_int_hook(task_movememory, p);
20510f2f
JM
3492}
3493
130c53bf
PM
3494/**
3495 * security_task_kill() - Check if sending a signal is allowed
3496 * @p: target process
3497 * @info: signal information
3498 * @sig: signal value
3499 * @cred: credentials of the signal sender, NULL if @current
3500 *
3501 * Check permission before sending signal @sig to @p. @info can be NULL, the
3502 * constant 1, or a pointer to a kernel_siginfo structure. If @info is 1 or
3503 * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3504 * the kernel and should typically be permitted. SIGIO signals are handled
3505 * separately by the send_sigiotask hook in file_security_ops.
3506 *
3507 * Return: Returns 0 if permission is granted.
3508 */
ae7795bc 3509int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
63c1845b 3510 int sig, const struct cred *cred)
20510f2f 3511{
260017f3 3512 return call_int_hook(task_kill, p, info, sig, cred);
20510f2f
JM
3513}
3514
130c53bf
PM
3515/**
3516 * security_task_prctl() - Check if a prctl op is allowed
3517 * @option: operation
3518 * @arg2: argument
3519 * @arg3: argument
3520 * @arg4: argument
3521 * @arg5: argument
3522 *
3523 * Check permission before performing a process control operation on the
3524 * current process.
3525 *
3526 * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3527 * to cause prctl() to return immediately with that value.
3528 */
20510f2f 3529int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
63c1845b 3530 unsigned long arg4, unsigned long arg5)
20510f2f 3531{
b1d9e6b0 3532 int thisrc;
98e828a0 3533 int rc = LSM_RET_DEFAULT(task_prctl);
b1d9e6b0
CS
3534 struct security_hook_list *hp;
3535
df0ce173 3536 hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
b1d9e6b0 3537 thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
98e828a0 3538 if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
b1d9e6b0
CS
3539 rc = thisrc;
3540 if (thisrc != 0)
3541 break;
3542 }
3543 }
3544 return rc;
20510f2f
JM
3545}
3546
130c53bf
PM
3547/**
3548 * security_task_to_inode() - Set the security attributes of a task's inode
3549 * @p: task
3550 * @inode: inode
3551 *
3552 * Set the security attributes for an inode based on an associated task's
3553 * security attributes, e.g. for /proc/pid inodes.
3554 */
20510f2f
JM
3555void security_task_to_inode(struct task_struct *p, struct inode *inode)
3556{
f25fce3e 3557 call_void_hook(task_to_inode, p, inode);
20510f2f
JM
3558}
3559
130c53bf
PM
3560/**
3561 * security_create_user_ns() - Check if creating a new userns is allowed
3562 * @cred: prepared creds
3563 *
3564 * Check permission prior to creating a new user namespace.
3565 *
3566 * Return: Returns 0 if successful, otherwise < 0 error code.
3567 */
7cd4c5c2
FL
3568int security_create_user_ns(const struct cred *cred)
3569{
260017f3 3570 return call_int_hook(userns_create, cred);
7cd4c5c2 3571}
20510f2f 3572
43fad282
PM
3573/**
3574 * security_ipc_permission() - Check if sysv ipc access is allowed
3575 * @ipcp: ipc permission structure
1e2523d7 3576 * @flag: requested permissions
43fad282
PM
3577 *
3578 * Check permissions for access to IPC.
3579 *
3580 * Return: Returns 0 if permission is granted.
3581 */
20510f2f
JM
3582int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
3583{
260017f3 3584 return call_int_hook(ipc_permission, ipcp, flag);
20510f2f
JM
3585}
3586
43fad282
PM
3587/**
3588 * security_ipc_getsecid() - Get the sysv ipc object's secid
3589 * @ipcp: ipc permission structure
3590 * @secid: secid pointer
3591 *
3592 * Get the secid associated with the ipc object. In case of failure, @secid
3593 * will be set to zero.
3594 */
8a076191
AD
3595void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
3596{
b1d9e6b0 3597 *secid = 0;
f25fce3e 3598 call_void_hook(ipc_getsecid, ipcp, secid);
8a076191
AD
3599}
3600
43fad282
PM
3601/**
3602 * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
3603 * @msg: message structure
3604 *
3605 * Allocate and attach a security structure to the msg->security field. The
3606 * security field is initialized to NULL when the structure is first created.
3607 *
3608 * Return: Return 0 if operation was successful and permission is granted.
3609 */
20510f2f
JM
3610int security_msg_msg_alloc(struct msg_msg *msg)
3611{
ecd5f82e
CS
3612 int rc = lsm_msg_msg_alloc(msg);
3613
3614 if (unlikely(rc))
3615 return rc;
260017f3 3616 rc = call_int_hook(msg_msg_alloc_security, msg);
ecd5f82e
CS
3617 if (unlikely(rc))
3618 security_msg_msg_free(msg);
3619 return rc;
20510f2f
JM
3620}
3621
43fad282
PM
3622/**
3623 * security_msg_msg_free() - Free a sysv ipc message LSM blob
3624 * @msg: message structure
3625 *
3626 * Deallocate the security structure for this message.
3627 */
20510f2f
JM
3628void security_msg_msg_free(struct msg_msg *msg)
3629{
f25fce3e 3630 call_void_hook(msg_msg_free_security, msg);
ecd5f82e
CS
3631 kfree(msg->security);
3632 msg->security = NULL;
20510f2f
JM
3633}
3634
43fad282
PM
3635/**
3636 * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
3637 * @msq: sysv ipc permission structure
3638 *
3639 * Allocate and attach a security structure to @msg. The security field is
3640 * initialized to NULL when the structure is first created.
3641 *
3642 * Return: Returns 0 if operation was successful and permission is granted.
3643 */
d8c6e854 3644int security_msg_queue_alloc(struct kern_ipc_perm *msq)
20510f2f 3645{
ecd5f82e
CS
3646 int rc = lsm_ipc_alloc(msq);
3647
3648 if (unlikely(rc))
3649 return rc;
260017f3 3650 rc = call_int_hook(msg_queue_alloc_security, msq);
ecd5f82e
CS
3651 if (unlikely(rc))
3652 security_msg_queue_free(msq);
3653 return rc;
20510f2f
JM
3654}
3655
43fad282
PM
3656/**
3657 * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
3658 * @msq: sysv ipc permission structure
3659 *
3660 * Deallocate security field @perm->security for the message queue.
3661 */
d8c6e854 3662void security_msg_queue_free(struct kern_ipc_perm *msq)
20510f2f 3663{
f25fce3e 3664 call_void_hook(msg_queue_free_security, msq);
ecd5f82e
CS
3665 kfree(msq->security);
3666 msq->security = NULL;
20510f2f
JM
3667}
3668
43fad282
PM
3669/**
3670 * security_msg_queue_associate() - Check if a msg queue operation is allowed
3671 * @msq: sysv ipc permission structure
3672 * @msqflg: operation flags
3673 *
3674 * Check permission when a message queue is requested through the msgget system
3675 * call. This hook is only called when returning the message queue identifier
3676 * for an existing message queue, not when a new message queue is created.
3677 *
3678 * Return: Return 0 if permission is granted.
3679 */
d8c6e854 3680int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
20510f2f 3681{
260017f3 3682 return call_int_hook(msg_queue_associate, msq, msqflg);
20510f2f
JM
3683}
3684
43fad282
PM
3685/**
3686 * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
3687 * @msq: sysv ipc permission structure
3688 * @cmd: operation
3689 *
3690 * Check permission when a message control operation specified by @cmd is to be
3691 * performed on the message queue with permissions.
3692 *
3693 * Return: Returns 0 if permission is granted.
3694 */
d8c6e854 3695int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
20510f2f 3696{
260017f3 3697 return call_int_hook(msg_queue_msgctl, msq, cmd);
20510f2f
JM
3698}
3699
43fad282
PM
3700/**
3701 * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
3702 * @msq: sysv ipc permission structure
3703 * @msg: message
3704 * @msqflg: operation flags
3705 *
3706 * Check permission before a message, @msg, is enqueued on the message queue
3707 * with permissions specified in @msq.
3708 *
3709 * Return: Returns 0 if permission is granted.
3710 */
d8c6e854 3711int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
63c1845b 3712 struct msg_msg *msg, int msqflg)
20510f2f 3713{
260017f3 3714 return call_int_hook(msg_queue_msgsnd, msq, msg, msqflg);
20510f2f
JM
3715}
3716
43fad282
PM
3717/**
3718 * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
3719 * @msq: sysv ipc permission structure
3720 * @msg: message
3721 * @target: target task
3722 * @type: type of message requested
3723 * @mode: operation flags
3724 *
3725 * Check permission before a message, @msg, is removed from the message queue.
3726 * The @target task structure contains a pointer to the process that will be
3727 * receiving the message (not equal to the current process when inline receives
3728 * are being performed).
3729 *
3730 * Return: Returns 0 if permission is granted.
3731 */
d8c6e854 3732int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
63c1845b 3733 struct task_struct *target, long type, int mode)
20510f2f 3734{
260017f3 3735 return call_int_hook(msg_queue_msgrcv, msq, msg, target, type, mode);
20510f2f
JM
3736}
3737
43fad282
PM
3738/**
3739 * security_shm_alloc() - Allocate a sysv shm LSM blob
3740 * @shp: sysv ipc permission structure
3741 *
3742 * Allocate and attach a security structure to the @shp security field. The
3743 * security field is initialized to NULL when the structure is first created.
3744 *
3745 * Return: Returns 0 if operation was successful and permission is granted.
3746 */
7191adff 3747int security_shm_alloc(struct kern_ipc_perm *shp)
20510f2f 3748{
ecd5f82e
CS
3749 int rc = lsm_ipc_alloc(shp);
3750
3751 if (unlikely(rc))
3752 return rc;
260017f3 3753 rc = call_int_hook(shm_alloc_security, shp);
ecd5f82e
CS
3754 if (unlikely(rc))
3755 security_shm_free(shp);
3756 return rc;
20510f2f
JM
3757}
3758
43fad282
PM
3759/**
3760 * security_shm_free() - Free a sysv shm LSM blob
3761 * @shp: sysv ipc permission structure
3762 *
3763 * Deallocate the security structure @perm->security for the memory segment.
3764 */
7191adff 3765void security_shm_free(struct kern_ipc_perm *shp)
20510f2f 3766{
f25fce3e 3767 call_void_hook(shm_free_security, shp);
ecd5f82e
CS
3768 kfree(shp->security);
3769 shp->security = NULL;
20510f2f
JM
3770}
3771
43fad282
PM
3772/**
3773 * security_shm_associate() - Check if a sysv shm operation is allowed
3774 * @shp: sysv ipc permission structure
3775 * @shmflg: operation flags
3776 *
3777 * Check permission when a shared memory region is requested through the shmget
3778 * system call. This hook is only called when returning the shared memory
3779 * region identifier for an existing region, not when a new shared memory
3780 * region is created.
3781 *
3782 * Return: Returns 0 if permission is granted.
3783 */
7191adff 3784int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
20510f2f 3785{
260017f3 3786 return call_int_hook(shm_associate, shp, shmflg);
20510f2f
JM
3787}
3788
43fad282
PM
3789/**
3790 * security_shm_shmctl() - Check if a sysv shm operation is allowed
3791 * @shp: sysv ipc permission structure
3792 * @cmd: operation
3793 *
3794 * Check permission when a shared memory control operation specified by @cmd is
3795 * to be performed on the shared memory region with permissions in @shp.
3796 *
3797 * Return: Return 0 if permission is granted.
3798 */
7191adff 3799int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
20510f2f 3800{
260017f3 3801 return call_int_hook(shm_shmctl, shp, cmd);
20510f2f
JM
3802}
3803
43fad282
PM
3804/**
3805 * security_shm_shmat() - Check if a sysv shm attach operation is allowed
3806 * @shp: sysv ipc permission structure
3807 * @shmaddr: address of memory region to attach
3808 * @shmflg: operation flags
3809 *
3810 * Check permissions prior to allowing the shmat system call to attach the
3811 * shared memory segment with permissions @shp to the data segment of the
3812 * calling process. The attaching address is specified by @shmaddr.
3813 *
3814 * Return: Returns 0 if permission is granted.
3815 */
63c1845b
PM
3816int security_shm_shmat(struct kern_ipc_perm *shp,
3817 char __user *shmaddr, int shmflg)
20510f2f 3818{
260017f3 3819 return call_int_hook(shm_shmat, shp, shmaddr, shmflg);
20510f2f
JM
3820}
3821
43fad282
PM
3822/**
3823 * security_sem_alloc() - Allocate a sysv semaphore LSM blob
3824 * @sma: sysv ipc permission structure
3825 *
3826 * Allocate and attach a security structure to the @sma security field. The
3827 * security field is initialized to NULL when the structure is first created.
3828 *
3829 * Return: Returns 0 if operation was successful and permission is granted.
3830 */
aefad959 3831int security_sem_alloc(struct kern_ipc_perm *sma)
20510f2f 3832{
ecd5f82e
CS
3833 int rc = lsm_ipc_alloc(sma);
3834
3835 if (unlikely(rc))
3836 return rc;
260017f3 3837 rc = call_int_hook(sem_alloc_security, sma);
ecd5f82e
CS
3838 if (unlikely(rc))
3839 security_sem_free(sma);
3840 return rc;
20510f2f
JM
3841}
3842
43fad282
PM
3843/**
3844 * security_sem_free() - Free a sysv semaphore LSM blob
3845 * @sma: sysv ipc permission structure
3846 *
3847 * Deallocate security structure @sma->security for the semaphore.
3848 */
aefad959 3849void security_sem_free(struct kern_ipc_perm *sma)
20510f2f 3850{
f25fce3e 3851 call_void_hook(sem_free_security, sma);
ecd5f82e
CS
3852 kfree(sma->security);
3853 sma->security = NULL;
20510f2f
JM
3854}
3855
43fad282
PM
3856/**
3857 * security_sem_associate() - Check if a sysv semaphore operation is allowed
3858 * @sma: sysv ipc permission structure
3859 * @semflg: operation flags
3860 *
3861 * Check permission when a semaphore is requested through the semget system
3862 * call. This hook is only called when returning the semaphore identifier for
3863 * an existing semaphore, not when a new one must be created.
3864 *
3865 * Return: Returns 0 if permission is granted.
3866 */
aefad959 3867int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
20510f2f 3868{
260017f3 3869 return call_int_hook(sem_associate, sma, semflg);
20510f2f
JM
3870}
3871
43fad282 3872/**
1e2523d7 3873 * security_sem_semctl() - Check if a sysv semaphore operation is allowed
43fad282
PM
3874 * @sma: sysv ipc permission structure
3875 * @cmd: operation
3876 *
3877 * Check permission when a semaphore operation specified by @cmd is to be
3878 * performed on the semaphore.
3879 *
3880 * Return: Returns 0 if permission is granted.
3881 */
aefad959 3882int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
20510f2f 3883{
260017f3 3884 return call_int_hook(sem_semctl, sma, cmd);
20510f2f
JM
3885}
3886
43fad282
PM
3887/**
3888 * security_sem_semop() - Check if a sysv semaphore operation is allowed
3889 * @sma: sysv ipc permission structure
3890 * @sops: operations to perform
3891 * @nsops: number of operations
3892 * @alter: flag indicating changes will be made
3893 *
3894 * Check permissions before performing operations on members of the semaphore
3895 * set. If the @alter flag is nonzero, the semaphore set may be modified.
3896 *
3897 * Return: Returns 0 if permission is granted.
3898 */
aefad959 3899int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
63c1845b 3900 unsigned nsops, int alter)
20510f2f 3901{
260017f3 3902 return call_int_hook(sem_semop, sma, sops, nsops, alter);
20510f2f
JM
3903}
3904
916e3258
PM
3905/**
3906 * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3907 * @dentry: dentry
3908 * @inode: inode
3909 *
3910 * Fill in @inode security information for a @dentry if allowed.
3911 */
20510f2f
JM
3912void security_d_instantiate(struct dentry *dentry, struct inode *inode)
3913{
3914 if (unlikely(inode && IS_PRIVATE(inode)))
3915 return;
f25fce3e 3916 call_void_hook(d_instantiate, dentry, inode);
20510f2f
JM
3917}
3918EXPORT_SYMBOL(security_d_instantiate);
3919
a04a1198
CS
3920/*
3921 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
3922 */
3923
3924/**
3925 * security_getselfattr - Read an LSM attribute of the current process.
3926 * @attr: which attribute to return
3927 * @uctx: the user-space destination for the information, or NULL
3928 * @size: pointer to the size of space available to receive the data
3929 * @flags: special handling options. LSM_FLAG_SINGLE indicates that only
3930 * attributes associated with the LSM identified in the passed @ctx be
3931 * reported.
3932 *
3933 * A NULL value for @uctx can be used to get both the number of attributes
3934 * and the size of the data.
3935 *
3936 * Returns the number of attributes found on success, negative value
3937 * on error. @size is reset to the total size of the data.
3938 * If @size is insufficient to contain the data -E2BIG is returned.
3939 */
3940int security_getselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
a5a858f6 3941 u32 __user *size, u32 flags)
a04a1198
CS
3942{
3943 struct security_hook_list *hp;
3944 struct lsm_ctx lctx = { .id = LSM_ID_UNDEF, };
3945 u8 __user *base = (u8 __user *)uctx;
a5a858f6
CS
3946 u32 entrysize;
3947 u32 total = 0;
3948 u32 left;
a04a1198
CS
3949 bool toobig = false;
3950 bool single = false;
3951 int count = 0;
3952 int rc;
3953
3954 if (attr == LSM_ATTR_UNDEF)
3955 return -EINVAL;
3956 if (size == NULL)
3957 return -EINVAL;
3958 if (get_user(left, size))
3959 return -EFAULT;
3960
3961 if (flags) {
3962 /*
3963 * Only flag supported is LSM_FLAG_SINGLE
3964 */
fdcf699b 3965 if (flags != LSM_FLAG_SINGLE || !uctx)
a04a1198 3966 return -EINVAL;
fdcf699b 3967 if (copy_from_user(&lctx, uctx, sizeof(lctx)))
a04a1198
CS
3968 return -EFAULT;
3969 /*
3970 * If the LSM ID isn't specified it is an error.
3971 */
3972 if (lctx.id == LSM_ID_UNDEF)
3973 return -EINVAL;
3974 single = true;
3975 }
3976
3977 /*
3978 * In the usual case gather all the data from the LSMs.
3979 * In the single case only get the data from the LSM specified.
3980 */
3981 hlist_for_each_entry(hp, &security_hook_heads.getselfattr, list) {
3982 if (single && lctx.id != hp->lsmid->id)
3983 continue;
3984 entrysize = left;
3985 if (base)
3986 uctx = (struct lsm_ctx __user *)(base + total);
3987 rc = hp->hook.getselfattr(attr, uctx, &entrysize, flags);
3988 if (rc == -EOPNOTSUPP) {
3989 rc = 0;
3990 continue;
3991 }
3992 if (rc == -E2BIG) {
dc46db78 3993 rc = 0;
a04a1198 3994 left = 0;
dc46db78 3995 toobig = true;
a04a1198
CS
3996 } else if (rc < 0)
3997 return rc;
3998 else
3999 left -= entrysize;
4000
4001 total += entrysize;
4002 count += rc;
4003 if (single)
4004 break;
4005 }
4006 if (put_user(total, size))
4007 return -EFAULT;
4008 if (toobig)
4009 return -E2BIG;
4010 if (count == 0)
4011 return LSM_RET_DEFAULT(getselfattr);
4012 return count;
4013}
4014
4015/*
4016 * Please keep this in sync with it's counterpart in security/lsm_syscalls.c
4017 */
4018
4019/**
4020 * security_setselfattr - Set an LSM attribute on the current process.
4021 * @attr: which attribute to set
4022 * @uctx: the user-space source for the information
4023 * @size: the size of the data
4024 * @flags: reserved for future use, must be 0
4025 *
4026 * Set an LSM attribute for the current process. The LSM, attribute
4027 * and new value are included in @uctx.
4028 *
4029 * Returns 0 on success, -EINVAL if the input is inconsistent, -EFAULT
4030 * if the user buffer is inaccessible, E2BIG if size is too big, or an
4031 * LSM specific failure.
4032 */
4033int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
a5a858f6 4034 u32 size, u32 flags)
a04a1198
CS
4035{
4036 struct security_hook_list *hp;
4037 struct lsm_ctx *lctx;
4038 int rc = LSM_RET_DEFAULT(setselfattr);
d8bdd795 4039 u64 required_len;
a04a1198
CS
4040
4041 if (flags)
4042 return -EINVAL;
4043 if (size < sizeof(*lctx))
4044 return -EINVAL;
4045 if (size > PAGE_SIZE)
4046 return -E2BIG;
4047
9ba8802c
PM
4048 lctx = memdup_user(uctx, size);
4049 if (IS_ERR(lctx))
4050 return PTR_ERR(lctx);
a04a1198 4051
d8bdd795
JH
4052 if (size < lctx->len ||
4053 check_add_overflow(sizeof(*lctx), lctx->ctx_len, &required_len) ||
4054 lctx->len < required_len) {
a04a1198
CS
4055 rc = -EINVAL;
4056 goto free_out;
4057 }
4058
4059 hlist_for_each_entry(hp, &security_hook_heads.setselfattr, list)
4060 if ((hp->lsmid->id) == lctx->id) {
4061 rc = hp->hook.setselfattr(attr, lctx, size, flags);
4062 break;
4063 }
4064
4065free_out:
4066 kfree(lctx);
4067 return rc;
4068}
4069
916e3258
PM
4070/**
4071 * security_getprocattr() - Read an attribute for a task
4072 * @p: the task
267c068e 4073 * @lsmid: LSM identification
916e3258
PM
4074 * @name: attribute name
4075 * @value: attribute value
4076 *
4077 * Read attribute @name for task @p and store it into @value if allowed.
4078 *
4079 * Return: Returns the length of @value on success, a negative value otherwise.
4080 */
267c068e
CS
4081int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
4082 char **value)
20510f2f 4083{
6d9c939d
CS
4084 struct security_hook_list *hp;
4085
4086 hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
267c068e 4087 if (lsmid != 0 && lsmid != hp->lsmid->id)
6d9c939d
CS
4088 continue;
4089 return hp->hook.getprocattr(p, name, value);
4090 }
98e828a0 4091 return LSM_RET_DEFAULT(getprocattr);
20510f2f
JM
4092}
4093
916e3258
PM
4094/**
4095 * security_setprocattr() - Set an attribute for a task
267c068e 4096 * @lsmid: LSM identification
916e3258
PM
4097 * @name: attribute name
4098 * @value: attribute value
4099 * @size: attribute value size
4100 *
4101 * Write (set) the current task's attribute @name to @value, size @size if
4102 * allowed.
4103 *
4104 * Return: Returns bytes written on success, a negative value otherwise.
4105 */
267c068e 4106int security_setprocattr(int lsmid, const char *name, void *value, size_t size)
20510f2f 4107{
6d9c939d
CS
4108 struct security_hook_list *hp;
4109
4110 hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
267c068e 4111 if (lsmid != 0 && lsmid != hp->lsmid->id)
6d9c939d
CS
4112 continue;
4113 return hp->hook.setprocattr(name, value, size);
4114 }
98e828a0 4115 return LSM_RET_DEFAULT(setprocattr);
20510f2f
JM
4116}
4117
2bcf51bf
PM
4118/**
4119 * security_netlink_send() - Save info and check if netlink sending is allowed
4120 * @sk: sending socket
4121 * @skb: netlink message
4122 *
4123 * Save security information for a netlink message so that permission checking
4124 * can be performed when the message is processed. The security information
4125 * can be saved using the eff_cap field of the netlink_skb_parms structure.
4126 * Also may be used to provide fine grained control over message transmission.
4127 *
4128 * Return: Returns 0 if the information was successfully saved and message is
4129 * allowed to be transmitted.
4130 */
20510f2f
JM
4131int security_netlink_send(struct sock *sk, struct sk_buff *skb)
4132{
260017f3 4133 return call_int_hook(netlink_send, sk, skb);
20510f2f 4134}
20510f2f 4135
e261301c 4136/**
936615f6 4137 * security_ismaclabel() - Check if the named attribute is a MAC label
e261301c
PM
4138 * @name: full extended attribute name
4139 *
4140 * Check if the extended attribute specified by @name represents a MAC label.
4141 *
4142 * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
4143 */
746df9b5
DQ
4144int security_ismaclabel(const char *name)
4145{
260017f3 4146 return call_int_hook(ismaclabel, name);
746df9b5
DQ
4147}
4148EXPORT_SYMBOL(security_ismaclabel);
4149
e261301c
PM
4150/**
4151 * security_secid_to_secctx() - Convert a secid to a secctx
4152 * @secid: secid
4153 * @secdata: secctx
4154 * @seclen: secctx length
4155 *
4156 * Convert secid to security context. If @secdata is NULL the length of the
4157 * result will be returned in @seclen, but no @secdata will be returned. This
4158 * does mean that the length could change between calls to check the length and
4159 * the next call which actually allocates and returns the @secdata.
4160 *
4161 * Return: Return 0 on success, error on failure.
4162 */
20510f2f
JM
4163int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4164{
260017f3 4165 return call_int_hook(secid_to_secctx, secid, secdata, seclen);
20510f2f
JM
4166}
4167EXPORT_SYMBOL(security_secid_to_secctx);
4168
e261301c
PM
4169/**
4170 * security_secctx_to_secid() - Convert a secctx to a secid
4171 * @secdata: secctx
4172 * @seclen: length of secctx
4173 * @secid: secid
4174 *
4175 * Convert security context to secid.
4176 *
4177 * Return: Returns 0 on success, error on failure.
4178 */
7bf570dc 4179int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
63cb3449 4180{
b1d9e6b0 4181 *secid = 0;
260017f3 4182 return call_int_hook(secctx_to_secid, secdata, seclen, secid);
63cb3449
DH
4183}
4184EXPORT_SYMBOL(security_secctx_to_secid);
4185
e261301c
PM
4186/**
4187 * security_release_secctx() - Free a secctx buffer
4188 * @secdata: secctx
4189 * @seclen: length of secctx
4190 *
4191 * Release the security context.
4192 */
20510f2f
JM
4193void security_release_secctx(char *secdata, u32 seclen)
4194{
f25fce3e 4195 call_void_hook(release_secctx, secdata, seclen);
20510f2f
JM
4196}
4197EXPORT_SYMBOL(security_release_secctx);
4198
e261301c
PM
4199/**
4200 * security_inode_invalidate_secctx() - Invalidate an inode's security label
4201 * @inode: inode
4202 *
4203 * Notify the security module that it must revalidate the security context of
4204 * an inode.
4205 */
6f3be9f5
AG
4206void security_inode_invalidate_secctx(struct inode *inode)
4207{
4208 call_void_hook(inode_invalidate_secctx, inode);
4209}
4210EXPORT_SYMBOL(security_inode_invalidate_secctx);
4211
e261301c 4212/**
e5085606 4213 * security_inode_notifysecctx() - Notify the LSM of an inode's security label
e261301c
PM
4214 * @inode: inode
4215 * @ctx: secctx
4216 * @ctxlen: length of secctx
4217 *
4218 * Notify the security module of what the security context of an inode should
4219 * be. Initializes the incore security context managed by the security module
4220 * for this inode. Example usage: NFS client invokes this hook to initialize
4221 * the security context in its incore inode to the value provided by the server
4222 * for the file when the server returned the file's attributes to the client.
4223 * Must be called with inode->i_mutex locked.
4224 *
4225 * Return: Returns 0 on success, error on failure.
4226 */
1ee65e37
DQ
4227int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4228{
260017f3 4229 return call_int_hook(inode_notifysecctx, inode, ctx, ctxlen);
1ee65e37
DQ
4230}
4231EXPORT_SYMBOL(security_inode_notifysecctx);
4232
e261301c
PM
4233/**
4234 * security_inode_setsecctx() - Change the security label of an inode
4235 * @dentry: inode
4236 * @ctx: secctx
4237 * @ctxlen: length of secctx
4238 *
4239 * Change the security context of an inode. Updates the incore security
4240 * context managed by the security module and invokes the fs code as needed
4241 * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
4242 * context. Example usage: NFS server invokes this hook to change the security
4243 * context in its incore inode and on the backing filesystem to a value
4244 * provided by the client on a SETATTR operation. Must be called with
4245 * inode->i_mutex locked.
4246 *
4247 * Return: Returns 0 on success, error on failure.
4248 */
1ee65e37
DQ
4249int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4250{
260017f3 4251 return call_int_hook(inode_setsecctx, dentry, ctx, ctxlen);
1ee65e37
DQ
4252}
4253EXPORT_SYMBOL(security_inode_setsecctx);
4254
e261301c
PM
4255/**
4256 * security_inode_getsecctx() - Get the security label of an inode
4257 * @inode: inode
4258 * @ctx: secctx
4259 * @ctxlen: length of secctx
4260 *
4261 * On success, returns 0 and fills out @ctx and @ctxlen with the security
4262 * context for the given @inode.
4263 *
4264 * Return: Returns 0 on success, error on failure.
4265 */
1ee65e37
DQ
4266int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4267{
260017f3 4268 return call_int_hook(inode_getsecctx, inode, ctx, ctxlen);
1ee65e37
DQ
4269}
4270EXPORT_SYMBOL(security_inode_getsecctx);
4271
344fa64e 4272#ifdef CONFIG_WATCH_QUEUE
e261301c
PM
4273/**
4274 * security_post_notification() - Check if a watch notification can be posted
4275 * @w_cred: credentials of the task that set the watch
4276 * @cred: credentials of the task which triggered the watch
4277 * @n: the notification
4278 *
4279 * Check to see if a watch notification can be posted to a particular queue.
4280 *
4281 * Return: Returns 0 if permission is granted.
4282 */
344fa64e
DH
4283int security_post_notification(const struct cred *w_cred,
4284 const struct cred *cred,
4285 struct watch_notification *n)
4286{
260017f3 4287 return call_int_hook(post_notification, w_cred, cred, n);
344fa64e
DH
4288}
4289#endif /* CONFIG_WATCH_QUEUE */
4290
998f5040 4291#ifdef CONFIG_KEY_NOTIFICATIONS
e261301c
PM
4292/**
4293 * security_watch_key() - Check if a task is allowed to watch for key events
4294 * @key: the key to watch
4295 *
4296 * Check to see if a process is allowed to watch for event notifications from
4297 * a key or keyring.
4298 *
4299 * Return: Returns 0 if permission is granted.
4300 */
998f5040
DH
4301int security_watch_key(struct key *key)
4302{
260017f3 4303 return call_int_hook(watch_key, key);
998f5040 4304}
63c1845b 4305#endif /* CONFIG_KEY_NOTIFICATIONS */
998f5040 4306
20510f2f 4307#ifdef CONFIG_SECURITY_NETWORK
2c2442fd
PM
4308/**
4309 * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
4310 * @sock: originating sock
4311 * @other: peer sock
4312 * @newsk: new sock
4313 *
4314 * Check permissions before establishing a Unix domain stream connection
4315 * between @sock and @other.
4316 *
4317 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4318 * Linux provides an alternative to the conventional file name space for Unix
4319 * domain sockets. Whereas binding and connecting to sockets in the file name
4320 * space is mediated by the typical file permissions (and caught by the mknod
4321 * and permission hooks in inode_security_ops), binding and connecting to
4322 * sockets in the abstract name space is completely unmediated. Sufficient
4323 * control of Unix domain sockets in the abstract name space isn't possible
4324 * using only the socket layer hooks, since we need to know the actual target
4325 * socket, which is not looked up until we are inside the af_unix code.
4326 *
4327 * Return: Returns 0 if permission is granted.
4328 */
63c1845b
PM
4329int security_unix_stream_connect(struct sock *sock, struct sock *other,
4330 struct sock *newsk)
20510f2f 4331{
260017f3 4332 return call_int_hook(unix_stream_connect, sock, other, newsk);
20510f2f
JM
4333}
4334EXPORT_SYMBOL(security_unix_stream_connect);
4335
2c2442fd
PM
4336/**
4337 * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
4338 * @sock: originating sock
4339 * @other: peer sock
4340 *
4341 * Check permissions before connecting or sending datagrams from @sock to
4342 * @other.
4343 *
4344 * The @unix_stream_connect and @unix_may_send hooks were necessary because
4345 * Linux provides an alternative to the conventional file name space for Unix
4346 * domain sockets. Whereas binding and connecting to sockets in the file name
4347 * space is mediated by the typical file permissions (and caught by the mknod
4348 * and permission hooks in inode_security_ops), binding and connecting to
4349 * sockets in the abstract name space is completely unmediated. Sufficient
4350 * control of Unix domain sockets in the abstract name space isn't possible
4351 * using only the socket layer hooks, since we need to know the actual target
4352 * socket, which is not looked up until we are inside the af_unix code.
4353 *
4354 * Return: Returns 0 if permission is granted.
4355 */
20510f2f
JM
4356int security_unix_may_send(struct socket *sock, struct socket *other)
4357{
260017f3 4358 return call_int_hook(unix_may_send, sock, other);
20510f2f
JM
4359}
4360EXPORT_SYMBOL(security_unix_may_send);
4361
6b6bbe8c
PM
4362/**
4363 * security_socket_create() - Check if creating a new socket is allowed
4364 * @family: protocol family
4365 * @type: communications type
4366 * @protocol: requested protocol
4367 * @kern: set to 1 if a kernel socket is requested
4368 *
4369 * Check permissions prior to creating a new socket.
4370 *
4371 * Return: Returns 0 if permission is granted.
4372 */
20510f2f
JM
4373int security_socket_create(int family, int type, int protocol, int kern)
4374{
260017f3 4375 return call_int_hook(socket_create, family, type, protocol, kern);
20510f2f
JM
4376}
4377
6b6bbe8c 4378/**
1e2523d7 4379 * security_socket_post_create() - Initialize a newly created socket
6b6bbe8c
PM
4380 * @sock: socket
4381 * @family: protocol family
4382 * @type: communications type
4383 * @protocol: requested protocol
4384 * @kern: set to 1 if a kernel socket is requested
4385 *
4386 * This hook allows a module to update or allocate a per-socket security
4387 * structure. Note that the security field was not added directly to the socket
4388 * structure, but rather, the socket security information is stored in the
4389 * associated inode. Typically, the inode alloc_security hook will allocate
4390 * and attach security information to SOCK_INODE(sock)->i_security. This hook
4391 * may be used to update the SOCK_INODE(sock)->i_security field with additional
4392 * information that wasn't available when the inode was allocated.
4393 *
4394 * Return: Returns 0 if permission is granted.
4395 */
20510f2f
JM
4396int security_socket_post_create(struct socket *sock, int family,
4397 int type, int protocol, int kern)
4398{
260017f3 4399 return call_int_hook(socket_post_create, sock, family, type,
63c1845b 4400 protocol, kern);
20510f2f
JM
4401}
4402
6b6bbe8c
PM
4403/**
4404 * security_socket_socketpair() - Check if creating a socketpair is allowed
4405 * @socka: first socket
4406 * @sockb: second socket
4407 *
4408 * Check permissions before creating a fresh pair of sockets.
4409 *
4410 * Return: Returns 0 if permission is granted and the connection was
4411 * established.
4412 */
aae7cfcb
DH
4413int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4414{
260017f3 4415 return call_int_hook(socket_socketpair, socka, sockb);
aae7cfcb
DH
4416}
4417EXPORT_SYMBOL(security_socket_socketpair);
4418
6b6bbe8c
PM
4419/**
4420 * security_socket_bind() - Check if a socket bind operation is allowed
4421 * @sock: socket
4422 * @address: requested bind address
4423 * @addrlen: length of address
4424 *
4425 * Check permission before socket protocol layer bind operation is performed
4426 * and the socket @sock is bound to the address specified in the @address
4427 * parameter.
4428 *
4429 * Return: Returns 0 if permission is granted.
4430 */
63c1845b
PM
4431int security_socket_bind(struct socket *sock,
4432 struct sockaddr *address, int addrlen)
20510f2f 4433{
260017f3 4434 return call_int_hook(socket_bind, sock, address, addrlen);
20510f2f
JM
4435}
4436
6b6bbe8c
PM
4437/**
4438 * security_socket_connect() - Check if a socket connect operation is allowed
4439 * @sock: socket
4440 * @address: address of remote connection point
4441 * @addrlen: length of address
4442 *
4443 * Check permission before socket protocol layer connect operation attempts to
4444 * connect socket @sock to a remote address, @address.
4445 *
4446 * Return: Returns 0 if permission is granted.
4447 */
63c1845b
PM
4448int security_socket_connect(struct socket *sock,
4449 struct sockaddr *address, int addrlen)
20510f2f 4450{
260017f3 4451 return call_int_hook(socket_connect, sock, address, addrlen);
20510f2f
JM
4452}
4453
6b6bbe8c
PM
4454/**
4455 * security_socket_listen() - Check if a socket is allowed to listen
4456 * @sock: socket
4457 * @backlog: connection queue size
4458 *
4459 * Check permission before socket protocol layer listen operation.
4460 *
4461 * Return: Returns 0 if permission is granted.
4462 */
20510f2f
JM
4463int security_socket_listen(struct socket *sock, int backlog)
4464{
260017f3 4465 return call_int_hook(socket_listen, sock, backlog);
20510f2f
JM
4466}
4467
6b6bbe8c
PM
4468/**
4469 * security_socket_accept() - Check if a socket is allowed to accept connections
4470 * @sock: listening socket
4471 * @newsock: newly creation connection socket
4472 *
4473 * Check permission before accepting a new connection. Note that the new
4474 * socket, @newsock, has been created and some information copied to it, but
4475 * the accept operation has not actually been performed.
4476 *
4477 * Return: Returns 0 if permission is granted.
4478 */
20510f2f
JM
4479int security_socket_accept(struct socket *sock, struct socket *newsock)
4480{
260017f3 4481 return call_int_hook(socket_accept, sock, newsock);
20510f2f
JM
4482}
4483
6b6bbe8c 4484/**
936615f6 4485 * security_socket_sendmsg() - Check if sending a message is allowed
6b6bbe8c
PM
4486 * @sock: sending socket
4487 * @msg: message to send
4488 * @size: size of message
4489 *
4490 * Check permission before transmitting a message to another socket.
4491 *
4492 * Return: Returns 0 if permission is granted.
4493 */
20510f2f
JM
4494int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
4495{
260017f3 4496 return call_int_hook(socket_sendmsg, sock, msg, size);
20510f2f
JM
4497}
4498
6b6bbe8c
PM
4499/**
4500 * security_socket_recvmsg() - Check if receiving a message is allowed
4501 * @sock: receiving socket
4502 * @msg: message to receive
4503 * @size: size of message
4504 * @flags: operational flags
4505 *
4506 * Check permission before receiving a message from a socket.
4507 *
4508 * Return: Returns 0 if permission is granted.
4509 */
20510f2f
JM
4510int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4511 int size, int flags)
4512{
260017f3 4513 return call_int_hook(socket_recvmsg, sock, msg, size, flags);
20510f2f
JM
4514}
4515
6b6bbe8c
PM
4516/**
4517 * security_socket_getsockname() - Check if reading the socket addr is allowed
4518 * @sock: socket
4519 *
4520 * Check permission before reading the local address (name) of the socket
4521 * object.
4522 *
4523 * Return: Returns 0 if permission is granted.
4524 */
20510f2f
JM
4525int security_socket_getsockname(struct socket *sock)
4526{
260017f3 4527 return call_int_hook(socket_getsockname, sock);
20510f2f
JM
4528}
4529
6b6bbe8c
PM
4530/**
4531 * security_socket_getpeername() - Check if reading the peer's addr is allowed
4532 * @sock: socket
4533 *
4534 * Check permission before the remote address (name) of a socket object.
4535 *
4536 * Return: Returns 0 if permission is granted.
4537 */
20510f2f
JM
4538int security_socket_getpeername(struct socket *sock)
4539{
260017f3 4540 return call_int_hook(socket_getpeername, sock);
20510f2f
JM
4541}
4542
6b6bbe8c
PM
4543/**
4544 * security_socket_getsockopt() - Check if reading a socket option is allowed
4545 * @sock: socket
4546 * @level: option's protocol level
4547 * @optname: option name
4548 *
4549 * Check permissions before retrieving the options associated with socket
4550 * @sock.
4551 *
4552 * Return: Returns 0 if permission is granted.
4553 */
20510f2f
JM
4554int security_socket_getsockopt(struct socket *sock, int level, int optname)
4555{
260017f3 4556 return call_int_hook(socket_getsockopt, sock, level, optname);
20510f2f
JM
4557}
4558
6b6bbe8c
PM
4559/**
4560 * security_socket_setsockopt() - Check if setting a socket option is allowed
4561 * @sock: socket
4562 * @level: option's protocol level
4563 * @optname: option name
4564 *
4565 * Check permissions before setting the options associated with socket @sock.
4566 *
4567 * Return: Returns 0 if permission is granted.
4568 */
20510f2f
JM
4569int security_socket_setsockopt(struct socket *sock, int level, int optname)
4570{
260017f3 4571 return call_int_hook(socket_setsockopt, sock, level, optname);
20510f2f
JM
4572}
4573
6b6bbe8c
PM
4574/**
4575 * security_socket_shutdown() - Checks if shutting down the socket is allowed
4576 * @sock: socket
4577 * @how: flag indicating how sends and receives are handled
4578 *
4579 * Checks permission before all or part of a connection on the socket @sock is
4580 * shut down.
4581 *
4582 * Return: Returns 0 if permission is granted.
4583 */
20510f2f
JM
4584int security_socket_shutdown(struct socket *sock, int how)
4585{
260017f3 4586 return call_int_hook(socket_shutdown, sock, how);
20510f2f
JM
4587}
4588
6b6bbe8c
PM
4589/**
4590 * security_sock_rcv_skb() - Check if an incoming network packet is allowed
4591 * @sk: destination sock
4592 * @skb: incoming packet
4593 *
4594 * Check permissions on incoming network packets. This hook is distinct from
4595 * Netfilter's IP input hooks since it is the first time that the incoming
4596 * sk_buff @skb has been associated with a particular socket, @sk. Must not
4597 * sleep inside this hook because some callers hold spinlocks.
4598 *
4599 * Return: Returns 0 if permission is granted.
4600 */
20510f2f
JM
4601int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4602{
260017f3 4603 return call_int_hook(socket_sock_rcv_skb, sk, skb);
20510f2f
JM
4604}
4605EXPORT_SYMBOL(security_sock_rcv_skb);
4606
6b6bbe8c
PM
4607/**
4608 * security_socket_getpeersec_stream() - Get the remote peer label
4609 * @sock: socket
4610 * @optval: destination buffer
4611 * @optlen: size of peer label copied into the buffer
4612 * @len: maximum size of the destination buffer
4613 *
4614 * This hook allows the security module to provide peer socket security state
4615 * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
4616 * For tcp sockets this can be meaningful if the socket is associated with an
4617 * ipsec SA.
4618 *
4619 * Return: Returns 0 if all is well, otherwise, typical getsockopt return
4620 * values.
4621 */
b10b9c34
PM
4622int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4623 sockptr_t optlen, unsigned int len)
20510f2f 4624{
260017f3
OM
4625 return call_int_hook(socket_getpeersec_stream, sock, optval, optlen,
4626 len);
20510f2f
JM
4627}
4628
6b6bbe8c
PM
4629/**
4630 * security_socket_getpeersec_dgram() - Get the remote peer label
4631 * @sock: socket
4632 * @skb: datagram packet
4633 * @secid: remote peer label secid
4634 *
4635 * This hook allows the security module to provide peer socket security state
4636 * for udp sockets on a per-packet basis to userspace via getsockopt
4637 * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
4638 * option via getsockopt. It can then retrieve the security state returned by
4639 * this hook for a packet via the SCM_SECURITY ancillary message type.
4640 *
4641 * Return: Returns 0 on success, error on failure.
4642 */
63c1845b
PM
4643int security_socket_getpeersec_dgram(struct socket *sock,
4644 struct sk_buff *skb, u32 *secid)
20510f2f 4645{
260017f3 4646 return call_int_hook(socket_getpeersec_dgram, sock, skb, secid);
20510f2f
JM
4647}
4648EXPORT_SYMBOL(security_socket_getpeersec_dgram);
4649
6b6bbe8c
PM
4650/**
4651 * security_sk_alloc() - Allocate and initialize a sock's LSM blob
4652 * @sk: sock
4653 * @family: protocol family
1e2523d7 4654 * @priority: gfp flags
6b6bbe8c
PM
4655 *
4656 * Allocate and attach a security structure to the sk->sk_security field, which
4657 * is used to copy security attributes between local stream sockets.
4658 *
4659 * Return: Returns 0 on success, error on failure.
4660 */
20510f2f
JM
4661int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
4662{
260017f3 4663 return call_int_hook(sk_alloc_security, sk, family, priority);
20510f2f
JM
4664}
4665
6b6bbe8c
PM
4666/**
4667 * security_sk_free() - Free the sock's LSM blob
4668 * @sk: sock
4669 *
4670 * Deallocate security structure.
4671 */
20510f2f
JM
4672void security_sk_free(struct sock *sk)
4673{
f25fce3e 4674 call_void_hook(sk_free_security, sk);
20510f2f
JM
4675}
4676
6b6bbe8c
PM
4677/**
4678 * security_sk_clone() - Clone a sock's LSM state
4679 * @sk: original sock
4680 * @newsk: target sock
4681 *
4682 * Clone/copy security structure.
4683 */
20510f2f
JM
4684void security_sk_clone(const struct sock *sk, struct sock *newsk)
4685{
f25fce3e 4686 call_void_hook(sk_clone_security, sk, newsk);
20510f2f 4687}
6230c9b4 4688EXPORT_SYMBOL(security_sk_clone);
20510f2f 4689
bd1f5934
KK
4690/**
4691 * security_sk_classify_flow() - Set a flow's secid based on socket
4692 * @sk: original socket
4693 * @flic: target flow
4694 *
4695 * Set the target flow's secid to socket's secid.
4696 */
5b52ad34 4697void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
20510f2f 4698{
3df98d79 4699 call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
20510f2f
JM
4700}
4701EXPORT_SYMBOL(security_sk_classify_flow);
4702
6b6bbe8c
PM
4703/**
4704 * security_req_classify_flow() - Set a flow's secid based on request_sock
4705 * @req: request_sock
4706 * @flic: target flow
4707 *
4708 * Sets @flic's secid to @req's secid.
4709 */
3df98d79
PM
4710void security_req_classify_flow(const struct request_sock *req,
4711 struct flowi_common *flic)
20510f2f 4712{
3df98d79 4713 call_void_hook(req_classify_flow, req, flic);
20510f2f
JM
4714}
4715EXPORT_SYMBOL(security_req_classify_flow);
4716
6b6bbe8c
PM
4717/**
4718 * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
4719 * @sk: sock being grafted
1e2523d7 4720 * @parent: target parent socket
6b6bbe8c 4721 *
1e2523d7
PM
4722 * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
4723 * LSM state from @parent.
6b6bbe8c 4724 */
20510f2f
JM
4725void security_sock_graft(struct sock *sk, struct socket *parent)
4726{
f25fce3e 4727 call_void_hook(sock_graft, sk, parent);
20510f2f
JM
4728}
4729EXPORT_SYMBOL(security_sock_graft);
4730
6b6bbe8c
PM
4731/**
4732 * security_inet_conn_request() - Set request_sock state using incoming connect
4733 * @sk: parent listening sock
4734 * @skb: incoming connection
4735 * @req: new request_sock
4736 *
4737 * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
4738 *
4739 * Return: Returns 0 if permission is granted.
4740 */
41dd9596 4741int security_inet_conn_request(const struct sock *sk,
63c1845b 4742 struct sk_buff *skb, struct request_sock *req)
20510f2f 4743{
260017f3 4744 return call_int_hook(inet_conn_request, sk, skb, req);
20510f2f
JM
4745}
4746EXPORT_SYMBOL(security_inet_conn_request);
4747
6b6bbe8c
PM
4748/**
4749 * security_inet_csk_clone() - Set new sock LSM state based on request_sock
4750 * @newsk: new sock
4751 * @req: connection request_sock
4752 *
4753 * Set that LSM state of @sock using the LSM state from @req.
4754 */
20510f2f 4755void security_inet_csk_clone(struct sock *newsk,
63c1845b 4756 const struct request_sock *req)
20510f2f 4757{
f25fce3e 4758 call_void_hook(inet_csk_clone, newsk, req);
20510f2f
JM
4759}
4760
6b6bbe8c
PM
4761/**
4762 * security_inet_conn_established() - Update sock's LSM state with connection
4763 * @sk: sock
4764 * @skb: connection packet
4765 *
4766 * Update @sock's LSM state to represent a new connection from @skb.
4767 */
20510f2f 4768void security_inet_conn_established(struct sock *sk,
63c1845b 4769 struct sk_buff *skb)
20510f2f 4770{
f25fce3e 4771 call_void_hook(inet_conn_established, sk, skb);
20510f2f 4772}
72e89f50 4773EXPORT_SYMBOL(security_inet_conn_established);
20510f2f 4774
6b6bbe8c
PM
4775/**
4776 * security_secmark_relabel_packet() - Check if setting a secmark is allowed
4777 * @secid: new secmark value
4778 *
4779 * Check if the process should be allowed to relabel packets to @secid.
4780 *
4781 * Return: Returns 0 if permission is granted.
4782 */
2606fd1f
EP
4783int security_secmark_relabel_packet(u32 secid)
4784{
260017f3 4785 return call_int_hook(secmark_relabel_packet, secid);
2606fd1f
EP
4786}
4787EXPORT_SYMBOL(security_secmark_relabel_packet);
4788
6b6bbe8c
PM
4789/**
4790 * security_secmark_refcount_inc() - Increment the secmark labeling rule count
4791 *
4792 * Tells the LSM to increment the number of secmark labeling rules loaded.
4793 */
2606fd1f
EP
4794void security_secmark_refcount_inc(void)
4795{
f25fce3e 4796 call_void_hook(secmark_refcount_inc);
2606fd1f
EP
4797}
4798EXPORT_SYMBOL(security_secmark_refcount_inc);
4799
6b6bbe8c
PM
4800/**
4801 * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
4802 *
4803 * Tells the LSM to decrement the number of secmark labeling rules loaded.
4804 */
2606fd1f
EP
4805void security_secmark_refcount_dec(void)
4806{
f25fce3e 4807 call_void_hook(secmark_refcount_dec);
2606fd1f
EP
4808}
4809EXPORT_SYMBOL(security_secmark_refcount_dec);
4810
6b6bbe8c
PM
4811/**
4812 * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
4813 * @security: pointer to the LSM blob
4814 *
4815 * This hook allows a module to allocate a security structure for a TUN device,
4816 * returning the pointer in @security.
4817 *
4818 * Return: Returns a zero on success, negative values on failure.
4819 */
5dbbaf2d
PM
4820int security_tun_dev_alloc_security(void **security)
4821{
260017f3 4822 return call_int_hook(tun_dev_alloc_security, security);
5dbbaf2d
PM
4823}
4824EXPORT_SYMBOL(security_tun_dev_alloc_security);
4825
6b6bbe8c
PM
4826/**
4827 * security_tun_dev_free_security() - Free a TUN device LSM blob
4828 * @security: LSM blob
4829 *
4830 * This hook allows a module to free the security structure for a TUN device.
4831 */
5dbbaf2d
PM
4832void security_tun_dev_free_security(void *security)
4833{
f25fce3e 4834 call_void_hook(tun_dev_free_security, security);
5dbbaf2d
PM
4835}
4836EXPORT_SYMBOL(security_tun_dev_free_security);
4837
6b6bbe8c
PM
4838/**
4839 * security_tun_dev_create() - Check if creating a TUN device is allowed
4840 *
4841 * Check permissions prior to creating a new TUN device.
4842 *
4843 * Return: Returns 0 if permission is granted.
4844 */
2b980dbd
PM
4845int security_tun_dev_create(void)
4846{
260017f3 4847 return call_int_hook(tun_dev_create);
2b980dbd
PM
4848}
4849EXPORT_SYMBOL(security_tun_dev_create);
4850
6b6bbe8c
PM
4851/**
4852 * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
4853 * @security: TUN device LSM blob
4854 *
4855 * Check permissions prior to attaching to a TUN device queue.
4856 *
4857 * Return: Returns 0 if permission is granted.
4858 */
5dbbaf2d 4859int security_tun_dev_attach_queue(void *security)
2b980dbd 4860{
260017f3 4861 return call_int_hook(tun_dev_attach_queue, security);
2b980dbd 4862}
5dbbaf2d 4863EXPORT_SYMBOL(security_tun_dev_attach_queue);
2b980dbd 4864
6b6bbe8c
PM
4865/**
4866 * security_tun_dev_attach() - Update TUN device LSM state on attach
4867 * @sk: associated sock
4868 * @security: TUN device LSM blob
4869 *
4870 * This hook can be used by the module to update any security state associated
4871 * with the TUN device's sock structure.
4872 *
4873 * Return: Returns 0 if permission is granted.
4874 */
5dbbaf2d 4875int security_tun_dev_attach(struct sock *sk, void *security)
2b980dbd 4876{
260017f3 4877 return call_int_hook(tun_dev_attach, sk, security);
2b980dbd
PM
4878}
4879EXPORT_SYMBOL(security_tun_dev_attach);
4880
6b6bbe8c
PM
4881/**
4882 * security_tun_dev_open() - Update TUN device LSM state on open
4883 * @security: TUN device LSM blob
4884 *
4885 * This hook can be used by the module to update any security state associated
4886 * with the TUN device's security structure.
4887 *
4888 * Return: Returns 0 if permission is granted.
4889 */
5dbbaf2d
PM
4890int security_tun_dev_open(void *security)
4891{
260017f3 4892 return call_int_hook(tun_dev_open, security);
5dbbaf2d
PM
4893}
4894EXPORT_SYMBOL(security_tun_dev_open);
4895
4a49f592
PM
4896/**
4897 * security_sctp_assoc_request() - Update the LSM on a SCTP association req
4898 * @asoc: SCTP association
4899 * @skb: packet requesting the association
4900 *
4901 * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
4902 *
4903 * Return: Returns 0 on success, error on failure.
4904 */
63c1845b
PM
4905int security_sctp_assoc_request(struct sctp_association *asoc,
4906 struct sk_buff *skb)
72e89f50 4907{
260017f3 4908 return call_int_hook(sctp_assoc_request, asoc, skb);
72e89f50
RH
4909}
4910EXPORT_SYMBOL(security_sctp_assoc_request);
4911
4a49f592
PM
4912/**
4913 * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
4914 * @sk: socket
4915 * @optname: SCTP option to validate
4916 * @address: list of IP addresses to validate
4917 * @addrlen: length of the address list
4918 *
4919 * Validiate permissions required for each address associated with sock @sk.
4920 * Depending on @optname, the addresses will be treated as either a connect or
4921 * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
4922 * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
4923 *
4924 * Return: Returns 0 on success, error on failure.
4925 */
72e89f50
RH
4926int security_sctp_bind_connect(struct sock *sk, int optname,
4927 struct sockaddr *address, int addrlen)
4928{
260017f3 4929 return call_int_hook(sctp_bind_connect, sk, optname, address, addrlen);
72e89f50
RH
4930}
4931EXPORT_SYMBOL(security_sctp_bind_connect);
4932
4a49f592
PM
4933/**
4934 * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
4935 * @asoc: SCTP association
4936 * @sk: original sock
4937 * @newsk: target sock
4938 *
4939 * Called whenever a new socket is created by accept(2) (i.e. a TCP style
4940 * socket) or when a socket is 'peeled off' e.g userspace calls
4941 * sctp_peeloff(3).
4942 */
c081d53f 4943void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
72e89f50
RH
4944 struct sock *newsk)
4945{
c081d53f 4946 call_void_hook(sctp_sk_clone, asoc, sk, newsk);
72e89f50
RH
4947}
4948EXPORT_SYMBOL(security_sctp_sk_clone);
4949
4a49f592
PM
4950/**
4951 * security_sctp_assoc_established() - Update LSM state when assoc established
4952 * @asoc: SCTP association
4953 * @skb: packet establishing the association
4954 *
4955 * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
4956 * security module.
4957 *
4958 * Return: Returns 0 if permission is granted.
4959 */
5e50f5d4
OM
4960int security_sctp_assoc_established(struct sctp_association *asoc,
4961 struct sk_buff *skb)
4962{
260017f3 4963 return call_int_hook(sctp_assoc_established, asoc, skb);
5e50f5d4
OM
4964}
4965EXPORT_SYMBOL(security_sctp_assoc_established);
4966
e3d9387f
PA
4967/**
4968 * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
4969 * @sk: the owning MPTCP socket
4970 * @ssk: the new subflow
4971 *
4972 * Update the labeling for the given MPTCP subflow, to match the one of the
4973 * owning MPTCP socket. This hook has to be called after the socket creation and
4974 * initialization via the security_socket_create() and
4975 * security_socket_post_create() LSM hooks.
4976 *
4977 * Return: Returns 0 on success or a negative error code on failure.
4978 */
4979int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
4980{
260017f3 4981 return call_int_hook(mptcp_add_subflow, sk, ssk);
e3d9387f
PA
4982}
4983
20510f2f
JM
4984#endif /* CONFIG_SECURITY_NETWORK */
4985
d291f1a6 4986#ifdef CONFIG_SECURITY_INFINIBAND
ac318aed
PM
4987/**
4988 * security_ib_pkey_access() - Check if access to an IB pkey is allowed
4989 * @sec: LSM blob
4990 * @subnet_prefix: subnet prefix of the port
4991 * @pkey: IB pkey
4992 *
4432b507 4993 * Check permission to access a pkey when modifying a QP.
ac318aed
PM
4994 *
4995 * Return: Returns 0 if permission is granted.
4996 */
d291f1a6
DJ
4997int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
4998{
260017f3 4999 return call_int_hook(ib_pkey_access, sec, subnet_prefix, pkey);
d291f1a6
DJ
5000}
5001EXPORT_SYMBOL(security_ib_pkey_access);
5002
ac318aed
PM
5003/**
5004 * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
5005 * @sec: LSM blob
5006 * @dev_name: IB device name
5007 * @port_num: port number
5008 *
5009 * Check permissions to send and receive SMPs on a end port.
5010 *
5011 * Return: Returns 0 if permission is granted.
5012 */
63c1845b
PM
5013int security_ib_endport_manage_subnet(void *sec,
5014 const char *dev_name, u8 port_num)
47a2b338 5015{
260017f3 5016 return call_int_hook(ib_endport_manage_subnet, sec, dev_name, port_num);
47a2b338
DJ
5017}
5018EXPORT_SYMBOL(security_ib_endport_manage_subnet);
5019
ac318aed
PM
5020/**
5021 * security_ib_alloc_security() - Allocate an Infiniband LSM blob
5022 * @sec: LSM blob
5023 *
5024 * Allocate a security structure for Infiniband objects.
5025 *
5026 * Return: Returns 0 on success, non-zero on failure.
5027 */
d291f1a6
DJ
5028int security_ib_alloc_security(void **sec)
5029{
260017f3 5030 return call_int_hook(ib_alloc_security, sec);
d291f1a6
DJ
5031}
5032EXPORT_SYMBOL(security_ib_alloc_security);
5033
ac318aed
PM
5034/**
5035 * security_ib_free_security() - Free an Infiniband LSM blob
5036 * @sec: LSM blob
5037 *
5038 * Deallocate an Infiniband security structure.
5039 */
d291f1a6
DJ
5040void security_ib_free_security(void *sec)
5041{
5042 call_void_hook(ib_free_security, sec);
5043}
5044EXPORT_SYMBOL(security_ib_free_security);
5045#endif /* CONFIG_SECURITY_INFINIBAND */
5046
20510f2f 5047#ifdef CONFIG_SECURITY_NETWORK_XFRM
742b9945
PM
5048/**
5049 * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
5050 * @ctxp: xfrm security context being added to the SPD
5051 * @sec_ctx: security label provided by userspace
5052 * @gfp: gfp flags
5053 *
5054 * Allocate a security structure to the xp->security field; the security field
5055 * is initialized to NULL when the xfrm_policy is allocated.
5056 *
5057 * Return: Return 0 if operation was successful.
5058 */
52a4c640
NA
5059int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
5060 struct xfrm_user_sec_ctx *sec_ctx,
5061 gfp_t gfp)
20510f2f 5062{
260017f3 5063 return call_int_hook(xfrm_policy_alloc_security, ctxp, sec_ctx, gfp);
20510f2f
JM
5064}
5065EXPORT_SYMBOL(security_xfrm_policy_alloc);
5066
742b9945
PM
5067/**
5068 * security_xfrm_policy_clone() - Clone xfrm policy LSM state
5069 * @old_ctx: xfrm security context
5070 * @new_ctxp: target xfrm security context
5071 *
5072 * Allocate a security structure in new_ctxp that contains the information from
5073 * the old_ctx structure.
5074 *
5075 * Return: Return 0 if operation was successful.
5076 */
03e1ad7b 5077int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
63c1845b 5078 struct xfrm_sec_ctx **new_ctxp)
20510f2f 5079{
260017f3 5080 return call_int_hook(xfrm_policy_clone_security, old_ctx, new_ctxp);
20510f2f
JM
5081}
5082
742b9945
PM
5083/**
5084 * security_xfrm_policy_free() - Free a xfrm security context
5085 * @ctx: xfrm security context
5086 *
5087 * Free LSM resources associated with @ctx.
5088 */
03e1ad7b 5089void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
20510f2f 5090{
f25fce3e 5091 call_void_hook(xfrm_policy_free_security, ctx);
20510f2f
JM
5092}
5093EXPORT_SYMBOL(security_xfrm_policy_free);
5094
742b9945
PM
5095/**
5096 * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
5097 * @ctx: xfrm security context
5098 *
5099 * Authorize deletion of a SPD entry.
5100 *
5101 * Return: Returns 0 if permission is granted.
5102 */
03e1ad7b 5103int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
20510f2f 5104{
260017f3 5105 return call_int_hook(xfrm_policy_delete_security, ctx);
20510f2f
JM
5106}
5107
742b9945
PM
5108/**
5109 * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
5110 * @x: xfrm state being added to the SAD
5111 * @sec_ctx: security label provided by userspace
5112 *
5113 * Allocate a security structure to the @x->security field; the security field
5114 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5115 * correspond to @sec_ctx.
5116 *
5117 * Return: Return 0 if operation was successful.
5118 */
2e5aa866
PM
5119int security_xfrm_state_alloc(struct xfrm_state *x,
5120 struct xfrm_user_sec_ctx *sec_ctx)
20510f2f 5121{
260017f3 5122 return call_int_hook(xfrm_state_alloc, x, sec_ctx);
20510f2f
JM
5123}
5124EXPORT_SYMBOL(security_xfrm_state_alloc);
5125
742b9945
PM
5126/**
5127 * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
5128 * @x: xfrm state being added to the SAD
5129 * @polsec: associated policy's security context
5130 * @secid: secid from the flow
5131 *
5132 * Allocate a security structure to the x->security field; the security field
5133 * is initialized to NULL when the xfrm_state is allocated. Set the context to
5134 * correspond to secid.
5135 *
5136 * Return: Returns 0 if operation was successful.
5137 */
20510f2f
JM
5138int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
5139 struct xfrm_sec_ctx *polsec, u32 secid)
5140{
260017f3 5141 return call_int_hook(xfrm_state_alloc_acquire, x, polsec, secid);
20510f2f
JM
5142}
5143
742b9945
PM
5144/**
5145 * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
5146 * @x: xfrm state
5147 *
5148 * Authorize deletion of x->security.
5149 *
5150 * Return: Returns 0 if permission is granted.
5151 */
20510f2f
JM
5152int security_xfrm_state_delete(struct xfrm_state *x)
5153{
260017f3 5154 return call_int_hook(xfrm_state_delete_security, x);
20510f2f
JM
5155}
5156EXPORT_SYMBOL(security_xfrm_state_delete);
5157
742b9945
PM
5158/**
5159 * security_xfrm_state_free() - Free a xfrm state
5160 * @x: xfrm state
5161 *
5162 * Deallocate x->security.
5163 */
20510f2f
JM
5164void security_xfrm_state_free(struct xfrm_state *x)
5165{
f25fce3e 5166 call_void_hook(xfrm_state_free_security, x);
20510f2f
JM
5167}
5168
742b9945
PM
5169/**
5170 * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
5171 * @ctx: target xfrm security context
5172 * @fl_secid: flow secid used to authorize access
5173 *
5174 * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
5175 * packet. The hook is called when selecting either a per-socket policy or a
5176 * generic xfrm policy.
5177 *
5178 * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
5179 * other errors.
5180 */
8a922805 5181int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
20510f2f 5182{
260017f3 5183 return call_int_hook(xfrm_policy_lookup, ctx, fl_secid);
20510f2f
JM
5184}
5185
742b9945
PM
5186/**
5187 * security_xfrm_state_pol_flow_match() - Check for a xfrm match
5188 * @x: xfrm state to match
1e2523d7 5189 * @xp: xfrm policy to check for a match
742b9945
PM
5190 * @flic: flow to check for a match.
5191 *
5192 * Check @xp and @flic for a match with @x.
5193 *
5194 * Return: Returns 1 if there is a match.
5195 */
20510f2f 5196int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
e33f7704 5197 struct xfrm_policy *xp,
3df98d79 5198 const struct flowi_common *flic)
20510f2f 5199{
b1d9e6b0 5200 struct security_hook_list *hp;
98e828a0 5201 int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
b1d9e6b0
CS
5202
5203 /*
5204 * Since this function is expected to return 0 or 1, the judgment
5205 * becomes difficult if multiple LSMs supply this call. Fortunately,
5206 * we can use the first LSM's judgment because currently only SELinux
5207 * supplies this call.
5208 *
5209 * For speed optimization, we explicitly break the loop rather than
5210 * using the macro
5211 */
df0ce173 5212 hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
63c1845b 5213 list) {
3df98d79 5214 rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
b1d9e6b0
CS
5215 break;
5216 }
5217 return rc;
20510f2f
JM
5218}
5219
742b9945
PM
5220/**
5221 * security_xfrm_decode_session() - Determine the xfrm secid for a packet
5222 * @skb: xfrm packet
5223 * @secid: secid
5224 *
5225 * Decode the packet in @skb and return the security label in @secid.
5226 *
5227 * Return: Return 0 if all xfrms used have the same secid.
5228 */
20510f2f
JM
5229int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
5230{
260017f3 5231 return call_int_hook(xfrm_decode_session, skb, secid, 1);
20510f2f
JM
5232}
5233
3df98d79 5234void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
20510f2f 5235{
260017f3 5236 int rc = call_int_hook(xfrm_decode_session, skb, &flic->flowic_secid,
63c1845b 5237 0);
20510f2f
JM
5238
5239 BUG_ON(rc);
5240}
5241EXPORT_SYMBOL(security_skb_classify_flow);
20510f2f
JM
5242#endif /* CONFIG_SECURITY_NETWORK_XFRM */
5243
5244#ifdef CONFIG_KEYS
ecc419a4
PM
5245/**
5246 * security_key_alloc() - Allocate and initialize a kernel key LSM blob
5247 * @key: key
5248 * @cred: credentials
5249 * @flags: allocation flags
5250 *
5251 * Permit allocation of a key and assign security data. Note that key does not
5252 * have a serial number assigned at this point.
5253 *
5254 * Return: Return 0 if permission is granted, -ve error otherwise.
5255 */
d84f4f99
DH
5256int security_key_alloc(struct key *key, const struct cred *cred,
5257 unsigned long flags)
20510f2f 5258{
260017f3 5259 return call_int_hook(key_alloc, key, cred, flags);
20510f2f
JM
5260}
5261
ecc419a4
PM
5262/**
5263 * security_key_free() - Free a kernel key LSM blob
5264 * @key: key
5265 *
5266 * Notification of destruction; free security data.
5267 */
20510f2f
JM
5268void security_key_free(struct key *key)
5269{
f25fce3e 5270 call_void_hook(key_free, key);
20510f2f
JM
5271}
5272
ecc419a4
PM
5273/**
5274 * security_key_permission() - Check if a kernel key operation is allowed
5275 * @key_ref: key reference
5276 * @cred: credentials of actor requesting access
5277 * @need_perm: requested permissions
5278 *
5279 * See whether a specific operational right is granted to a process on a key.
5280 *
5281 * Return: Return 0 if permission is granted, -ve error otherwise.
5282 */
8c0637e9
DH
5283int security_key_permission(key_ref_t key_ref, const struct cred *cred,
5284 enum key_need_perm need_perm)
20510f2f 5285{
260017f3 5286 return call_int_hook(key_permission, key_ref, cred, need_perm);
20510f2f
JM
5287}
5288
ecc419a4
PM
5289/**
5290 * security_key_getsecurity() - Get the key's security label
5291 * @key: key
b3816cf8 5292 * @buffer: security label buffer
ecc419a4
PM
5293 *
5294 * Get a textual representation of the security context attached to a key for
5295 * the purposes of honouring KEYCTL_GETSECURITY. This function allocates the
5296 * storage for the NUL-terminated string and the caller should free it.
5297 *
b3816cf8 5298 * Return: Returns the length of @buffer (including terminating NUL) or -ve if
ecc419a4
PM
5299 * an error occurs. May also return 0 (and a NULL buffer pointer) if
5300 * there is no security label assigned to the key.
5301 */
b3816cf8 5302int security_key_getsecurity(struct key *key, char **buffer)
70a5bb72 5303{
b3816cf8 5304 *buffer = NULL;
260017f3 5305 return call_int_hook(key_getsecurity, key, buffer);
70a5bb72 5306}
b8d99703
RS
5307
5308/**
5309 * security_key_post_create_or_update() - Notification of key create or update
5310 * @keyring: keyring to which the key is linked to
5311 * @key: created or updated key
5312 * @payload: data used to instantiate or update the key
5313 * @payload_len: length of payload
5314 * @flags: key flags
5315 * @create: flag indicating whether the key was created or updated
5316 *
5317 * Notify the caller of a key creation or update.
5318 */
5319void security_key_post_create_or_update(struct key *keyring, struct key *key,
5320 const void *payload, size_t payload_len,
5321 unsigned long flags, bool create)
5322{
5323 call_void_hook(key_post_create_or_update, keyring, key, payload,
5324 payload_len, flags, create);
70a5bb72 5325}
20510f2f 5326#endif /* CONFIG_KEYS */
03d37d25
AD
5327
5328#ifdef CONFIG_AUDIT
b14faf9c
PM
5329/**
5330 * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5331 * @field: audit action
5332 * @op: rule operator
5333 * @rulestr: rule context
5334 * @lsmrule: receive buffer for audit rule struct
5335 *
5336 * Allocate and initialize an LSM audit rule structure.
5337 *
5338 * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5339 * an invalid rule.
5340 */
03d37d25
AD
5341int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule)
5342{
260017f3 5343 return call_int_hook(audit_rule_init, field, op, rulestr, lsmrule);
03d37d25
AD
5344}
5345
b14faf9c
PM
5346/**
5347 * security_audit_rule_known() - Check if an audit rule contains LSM fields
5348 * @krule: audit rule
5349 *
5350 * Specifies whether given @krule contains any fields related to the current
5351 * LSM.
5352 *
5353 * Return: Returns 1 in case of relation found, 0 otherwise.
5354 */
03d37d25
AD
5355int security_audit_rule_known(struct audit_krule *krule)
5356{
260017f3 5357 return call_int_hook(audit_rule_known, krule);
03d37d25
AD
5358}
5359
b14faf9c
PM
5360/**
5361 * security_audit_rule_free() - Free an LSM audit rule struct
5362 * @lsmrule: audit rule struct
5363 *
5364 * Deallocate the LSM audit rule structure previously allocated by
5365 * audit_rule_init().
5366 */
03d37d25
AD
5367void security_audit_rule_free(void *lsmrule)
5368{
f25fce3e 5369 call_void_hook(audit_rule_free, lsmrule);
03d37d25
AD
5370}
5371
b14faf9c
PM
5372/**
5373 * security_audit_rule_match() - Check if a label matches an audit rule
5374 * @secid: security label
5375 * @field: LSM audit field
5376 * @op: matching operator
5377 * @lsmrule: audit rule
5378 *
5379 * Determine if given @secid matches a rule previously approved by
5380 * security_audit_rule_known().
5381 *
5382 * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5383 * failure.
5384 */
90462a5b 5385int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
03d37d25 5386{
260017f3 5387 return call_int_hook(audit_rule_match, secid, field, op, lsmrule);
03d37d25 5388}
b1d9e6b0 5389#endif /* CONFIG_AUDIT */
afdb09c7
CF
5390
5391#ifdef CONFIG_BPF_SYSCALL
55e85320
PM
5392/**
5393 * security_bpf() - Check if the bpf syscall operation is allowed
5394 * @cmd: command
5395 * @attr: bpf attribute
5396 * @size: size
5397 *
5398 * Do a initial check for all bpf syscalls after the attribute is copied into
5399 * the kernel. The actual security module can implement their own rules to
5400 * check the specific cmd they need.
5401 *
5402 * Return: Returns 0 if permission is granted.
5403 */
afdb09c7
CF
5404int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5405{
260017f3 5406 return call_int_hook(bpf, cmd, attr, size);
afdb09c7 5407}
55e85320
PM
5408
5409/**
5410 * security_bpf_map() - Check if access to a bpf map is allowed
5411 * @map: bpf map
5412 * @fmode: mode
5413 *
5414 * Do a check when the kernel generates and returns a file descriptor for eBPF
5415 * maps.
5416 *
5417 * Return: Returns 0 if permission is granted.
5418 */
afdb09c7
CF
5419int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5420{
260017f3 5421 return call_int_hook(bpf_map, map, fmode);
afdb09c7 5422}
55e85320
PM
5423
5424/**
5425 * security_bpf_prog() - Check if access to a bpf program is allowed
5426 * @prog: bpf program
5427 *
5428 * Do a check when the kernel generates and returns a file descriptor for eBPF
5429 * programs.
5430 *
5431 * Return: Returns 0 if permission is granted.
5432 */
afdb09c7
CF
5433int security_bpf_prog(struct bpf_prog *prog)
5434{
260017f3 5435 return call_int_hook(bpf_prog, prog);
afdb09c7 5436}
55e85320
PM
5437
5438/**
a2431c7e
AN
5439 * security_bpf_map_create() - Check if BPF map creation is allowed
5440 * @map: BPF map object
5441 * @attr: BPF syscall attributes used to create BPF map
5442 * @token: BPF token used to grant user access
55e85320 5443 *
a2431c7e
AN
5444 * Do a check when the kernel creates a new BPF map. This is also the
5445 * point where LSM blob is allocated for LSMs that need them.
55e85320
PM
5446 *
5447 * Return: Returns 0 on success, error on failure.
5448 */
a2431c7e
AN
5449int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
5450 struct bpf_token *token)
afdb09c7 5451{
cc4a875c 5452 return call_int_hook(bpf_map_create, map, attr, token);
afdb09c7 5453}
55e85320
PM
5454
5455/**
1b67772e
AN
5456 * security_bpf_prog_load() - Check if loading of BPF program is allowed
5457 * @prog: BPF program object
5458 * @attr: BPF syscall attributes used to create BPF program
5459 * @token: BPF token used to grant user access to BPF subsystem
55e85320 5460 *
1b67772e
AN
5461 * Perform an access control check when the kernel loads a BPF program and
5462 * allocates associated BPF program object. This hook is also responsible for
5463 * allocating any required LSM state for the BPF program.
55e85320
PM
5464 *
5465 * Return: Returns 0 on success, error on failure.
5466 */
1b67772e
AN
5467int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5468 struct bpf_token *token)
afdb09c7 5469{
cc4a875c 5470 return call_int_hook(bpf_prog_load, prog, attr, token);
afdb09c7 5471}
55e85320 5472
f568a3d4
AN
5473/**
5474 * security_bpf_token_create() - Check if creating of BPF token is allowed
5475 * @token: BPF token object
5476 * @attr: BPF syscall attributes used to create BPF token
5477 * @path: path pointing to BPF FS mount point from which BPF token is created
5478 *
5479 * Do a check when the kernel instantiates a new BPF token object from BPF FS
5480 * instance. This is also the point where LSM blob can be allocated for LSMs.
5481 *
5482 * Return: Returns 0 on success, error on failure.
5483 */
5484int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
5485 struct path *path)
5486{
cc4a875c 5487 return call_int_hook(bpf_token_create, token, attr, path);
f568a3d4
AN
5488}
5489
5490/**
5491 * security_bpf_token_cmd() - Check if BPF token is allowed to delegate
5492 * requested BPF syscall command
5493 * @token: BPF token object
5494 * @cmd: BPF syscall command requested to be delegated by BPF token
5495 *
5496 * Do a check when the kernel decides whether provided BPF token should allow
5497 * delegation of requested BPF syscall command.
5498 *
5499 * Return: Returns 0 on success, error on failure.
5500 */
5501int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
5502{
cc4a875c 5503 return call_int_hook(bpf_token_cmd, token, cmd);
f568a3d4
AN
5504}
5505
5506/**
5507 * security_bpf_token_capable() - Check if BPF token is allowed to delegate
5508 * requested BPF-related capability
5509 * @token: BPF token object
5510 * @cap: capabilities requested to be delegated by BPF token
5511 *
5512 * Do a check when the kernel decides whether provided BPF token should allow
5513 * delegation of requested BPF-related capabilities.
5514 *
5515 * Return: Returns 0 on success, error on failure.
5516 */
5517int security_bpf_token_capable(const struct bpf_token *token, int cap)
5518{
cc4a875c 5519 return call_int_hook(bpf_token_capable, token, cap);
f568a3d4
AN
5520}
5521
55e85320
PM
5522/**
5523 * security_bpf_map_free() - Free a bpf map's LSM blob
5524 * @map: bpf map
5525 *
5526 * Clean up the security information stored inside bpf map.
5527 */
afdb09c7
CF
5528void security_bpf_map_free(struct bpf_map *map)
5529{
a2431c7e 5530 call_void_hook(bpf_map_free, map);
afdb09c7 5531}
55e85320
PM
5532
5533/**
1b67772e
AN
5534 * security_bpf_prog_free() - Free a BPF program's LSM blob
5535 * @prog: BPF program struct
55e85320 5536 *
1b67772e 5537 * Clean up the security information stored inside BPF program.
55e85320 5538 */
1b67772e 5539void security_bpf_prog_free(struct bpf_prog *prog)
afdb09c7 5540{
1b67772e 5541 call_void_hook(bpf_prog_free, prog);
afdb09c7 5542}
f568a3d4
AN
5543
5544/**
5545 * security_bpf_token_free() - Free a BPF token's LSM blob
5546 * @token: BPF token struct
5547 *
5548 * Clean up the security information stored inside BPF token.
5549 */
5550void security_bpf_token_free(struct bpf_token *token)
5551{
5552 call_void_hook(bpf_token_free, token);
5553}
afdb09c7 5554#endif /* CONFIG_BPF_SYSCALL */
9e47d31d 5555
e261301c
PM
5556/**
5557 * security_locked_down() - Check if a kernel feature is allowed
5558 * @what: requested kernel feature
5559 *
5560 * Determine whether a kernel feature that potentially enables arbitrary code
5561 * execution in kernel space should be permitted.
5562 *
5563 * Return: Returns 0 if permission is granted.
5564 */
9e47d31d
MG
5565int security_locked_down(enum lockdown_reason what)
5566{
260017f3 5567 return call_int_hook(locked_down, what);
9e47d31d
MG
5568}
5569EXPORT_SYMBOL(security_locked_down);
da97e184
JFG
5570
5571#ifdef CONFIG_PERF_EVENTS
452b670c
PM
5572/**
5573 * security_perf_event_open() - Check if a perf event open is allowed
5574 * @attr: perf event attribute
5575 * @type: type of event
5576 *
5577 * Check whether the @type of perf_event_open syscall is allowed.
5578 *
5579 * Return: Returns 0 if permission is granted.
5580 */
da97e184
JFG
5581int security_perf_event_open(struct perf_event_attr *attr, int type)
5582{
260017f3 5583 return call_int_hook(perf_event_open, attr, type);
da97e184
JFG
5584}
5585
452b670c
PM
5586/**
5587 * security_perf_event_alloc() - Allocate a perf event LSM blob
5588 * @event: perf event
5589 *
5590 * Allocate and save perf_event security info.
5591 *
5592 * Return: Returns 0 on success, error on failure.
5593 */
da97e184
JFG
5594int security_perf_event_alloc(struct perf_event *event)
5595{
260017f3 5596 return call_int_hook(perf_event_alloc, event);
da97e184
JFG
5597}
5598
452b670c
PM
5599/**
5600 * security_perf_event_free() - Free a perf event LSM blob
5601 * @event: perf event
5602 *
5603 * Release (free) perf_event security info.
5604 */
da97e184
JFG
5605void security_perf_event_free(struct perf_event *event)
5606{
5607 call_void_hook(perf_event_free, event);
5608}
5609
452b670c
PM
5610/**
5611 * security_perf_event_read() - Check if reading a perf event label is allowed
5612 * @event: perf event
5613 *
5614 * Read perf_event security info if allowed.
5615 *
5616 * Return: Returns 0 if permission is granted.
5617 */
da97e184
JFG
5618int security_perf_event_read(struct perf_event *event)
5619{
260017f3 5620 return call_int_hook(perf_event_read, event);
da97e184
JFG
5621}
5622
452b670c
PM
5623/**
5624 * security_perf_event_write() - Check if writing a perf event label is allowed
5625 * @event: perf event
5626 *
5627 * Write perf_event security info if allowed.
5628 *
5629 * Return: Returns 0 if permission is granted.
5630 */
da97e184
JFG
5631int security_perf_event_write(struct perf_event *event)
5632{
260017f3 5633 return call_int_hook(perf_event_write, event);
da97e184
JFG
5634}
5635#endif /* CONFIG_PERF_EVENTS */
cdc1404a
PM
5636
5637#ifdef CONFIG_IO_URING
1cd2aca6
PM
5638/**
5639 * security_uring_override_creds() - Check if overriding creds is allowed
5640 * @new: new credentials
5641 *
5642 * Check if the current task, executing an io_uring operation, is allowed to
5643 * override it's credentials with @new.
5644 *
5645 * Return: Returns 0 if permission is granted.
5646 */
cdc1404a
PM
5647int security_uring_override_creds(const struct cred *new)
5648{
260017f3 5649 return call_int_hook(uring_override_creds, new);
cdc1404a
PM
5650}
5651
1cd2aca6
PM
5652/**
5653 * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
5654 *
5655 * Check whether the current task is allowed to spawn a io_uring polling thread
5656 * (IORING_SETUP_SQPOLL).
5657 *
5658 * Return: Returns 0 if permission is granted.
5659 */
cdc1404a
PM
5660int security_uring_sqpoll(void)
5661{
260017f3 5662 return call_int_hook(uring_sqpoll);
cdc1404a 5663}
1cd2aca6
PM
5664
5665/**
5666 * security_uring_cmd() - Check if a io_uring passthrough command is allowed
5667 * @ioucmd: command
5668 *
5669 * Check whether the file_operations uring_cmd is allowed to run.
5670 *
5671 * Return: Returns 0 if permission is granted.
5672 */
2a584012
LC
5673int security_uring_cmd(struct io_uring_cmd *ioucmd)
5674{
260017f3 5675 return call_int_hook(uring_cmd, ioucmd);
2a584012 5676}
cdc1404a 5677#endif /* CONFIG_IO_URING */