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