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