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