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