xref: /openbmc/linux/security/security.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Security plug functions
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
61da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
71da177e4SLinus Torvalds  * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
8d291f1a6SDaniel Jurgens  * Copyright (C) 2016 Mellanox Technologies
91661372cSPaul Moore  * Copyright (C) 2023 Microsoft Corporation <paul@paul-moore.com>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
129b8c7c14SKees Cook #define pr_fmt(fmt) "LSM: " fmt
139b8c7c14SKees Cook 
14afdb09c7SChenbo Feng #include <linux/bpf.h>
15c59ede7bSRandy.Dunlap #include <linux/capability.h>
16d47be3dfSDavid Quigley #include <linux/dcache.h>
17876979c9SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/kernel.h>
20b89999d0SScott Branden #include <linux/kernel_read_file.h>
213c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
22f381c272SMimi Zohar #include <linux/integrity.h>
236c21a7fbSMimi Zohar #include <linux/ima.h>
243e1be52dSMimi Zohar #include <linux/evm.h>
2540401530SAl Viro #include <linux/fsnotify.h>
268b3ec681SAl Viro #include <linux/mman.h>
278b3ec681SAl Viro #include <linux/mount.h>
288b3ec681SAl Viro #include <linux/personality.h>
2975331a59SPaul Mundt #include <linux/backing-dev.h>
303bb857e4SMickaël Salaün #include <linux/string.h>
31ecd5f82eSCasey Schaufler #include <linux/msg.h>
3240401530SAl Viro #include <net/flow.h>
331da177e4SLinus Torvalds 
342d4d5119SKees Cook /* How many LSMs were built into the kernel? */
352d4d5119SKees Cook #define LSM_COUNT (__end_lsm_info - __start_lsm_info)
362d4d5119SKees Cook 
3759438b46SStephen Smalley /*
3859438b46SStephen Smalley  * These are descriptions of the reasons that can be passed to the
3959438b46SStephen Smalley  * security_locked_down() LSM hook. Placing this array here allows
4059438b46SStephen Smalley  * all security modules to use the same descriptions for auditing
4159438b46SStephen Smalley  * purposes.
4259438b46SStephen Smalley  */
4359438b46SStephen Smalley const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX + 1] = {
4459438b46SStephen Smalley 	[LOCKDOWN_NONE] = "none",
4559438b46SStephen Smalley 	[LOCKDOWN_MODULE_SIGNATURE] = "unsigned module loading",
4659438b46SStephen Smalley 	[LOCKDOWN_DEV_MEM] = "/dev/mem,kmem,port",
4759438b46SStephen Smalley 	[LOCKDOWN_EFI_TEST] = "/dev/efi_test access",
4859438b46SStephen Smalley 	[LOCKDOWN_KEXEC] = "kexec of unsigned images",
4959438b46SStephen Smalley 	[LOCKDOWN_HIBERNATION] = "hibernation",
5059438b46SStephen Smalley 	[LOCKDOWN_PCI_ACCESS] = "direct PCI access",
5159438b46SStephen Smalley 	[LOCKDOWN_IOPORT] = "raw io port access",
5259438b46SStephen Smalley 	[LOCKDOWN_MSR] = "raw MSR access",
5359438b46SStephen Smalley 	[LOCKDOWN_ACPI_TABLES] = "modifying ACPI tables",
5499df7a28SNathan Lynch 	[LOCKDOWN_DEVICE_TREE] = "modifying device tree contents",
5559438b46SStephen Smalley 	[LOCKDOWN_PCMCIA_CIS] = "direct PCMCIA CIS storage",
5659438b46SStephen Smalley 	[LOCKDOWN_TIOCSSERIAL] = "reconfiguration of serial port IO",
5759438b46SStephen Smalley 	[LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
5859438b46SStephen Smalley 	[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
5959438b46SStephen Smalley 	[LOCKDOWN_DEBUGFS] = "debugfs access",
6059438b46SStephen Smalley 	[LOCKDOWN_XMON_WR] = "xmon write access",
6151e1bb9eSDaniel Borkmann 	[LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
62eadb2f47SDaniel Thompson 	[LOCKDOWN_DBG_WRITE_KERNEL] = "use of kgdb/kdb to write kernel RAM",
63b8f3e488SNathan Lynch 	[LOCKDOWN_RTAS_ERROR_INJECTION] = "RTAS error injection",
6459438b46SStephen Smalley 	[LOCKDOWN_INTEGRITY_MAX] = "integrity",
6559438b46SStephen Smalley 	[LOCKDOWN_KCORE] = "/proc/kcore access",
6659438b46SStephen Smalley 	[LOCKDOWN_KPROBES] = "use of kprobes",
6771330842SDaniel Borkmann 	[LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
68eadb2f47SDaniel Thompson 	[LOCKDOWN_DBG_READ_KERNEL] = "use of kgdb/kdb to read kernel RAM",
6959438b46SStephen Smalley 	[LOCKDOWN_PERF] = "unsafe use of perf",
7059438b46SStephen Smalley 	[LOCKDOWN_TRACEFS] = "use of tracefs",
7159438b46SStephen Smalley 	[LOCKDOWN_XMON_RW] = "xmon read and write access",
72c7a5899eSAntony Antony 	[LOCKDOWN_XFRM_SECRET] = "xfrm SA secret",
7359438b46SStephen Smalley 	[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
7459438b46SStephen Smalley };
7559438b46SStephen Smalley 
76f22f9aafSPaul Moore struct security_hook_heads security_hook_heads __ro_after_init;
7742df744cSJanne Karhunen static BLOCKING_NOTIFIER_HEAD(blocking_lsm_notifier_chain);
788f408ab6SDaniel Jurgens 
7933bf60caSCasey Schaufler static struct kmem_cache *lsm_file_cache;
80afb1cbe3SCasey Schaufler static struct kmem_cache *lsm_inode_cache;
8133bf60caSCasey Schaufler 
82d69dece5SCasey Schaufler char *lsm_names;
83f22f9aafSPaul Moore static struct lsm_blob_sizes blob_sizes __ro_after_init;
84bbd3662aSCasey Schaufler 
85076c54c5SAhmed S. Darwish /* Boot-time LSM user choice */
8679f7865dSKees Cook static __initdata const char *chosen_lsm_order;
875ef4e419SKees Cook static __initdata const char *chosen_major_lsm;
881da177e4SLinus Torvalds 
8913e735c0SKees Cook static __initconst const char *const builtin_lsm_order = CONFIG_LSM;
9013e735c0SKees Cook 
912d4d5119SKees Cook /* Ordered list of LSMs to initialize. */
922d4d5119SKees Cook static __initdata struct lsm_info **ordered_lsms;
9314bd99c8SKees Cook static __initdata struct lsm_info *exclusive;
942d4d5119SKees Cook 
959b8c7c14SKees Cook static __initdata bool debug;
969b8c7c14SKees Cook #define init_debug(...)						\
979b8c7c14SKees Cook 	do {							\
989b8c7c14SKees Cook 		if (debug)					\
999b8c7c14SKees Cook 			pr_info(__VA_ARGS__);			\
1009b8c7c14SKees Cook 	} while (0)
1011b1eeca7SArd Biesheuvel 
is_enabled(struct lsm_info * lsm)102f4941d75SKees Cook static bool __init is_enabled(struct lsm_info *lsm)
103f4941d75SKees Cook {
104a8027fb0SKees Cook 	if (!lsm->enabled)
105f4941d75SKees Cook 		return false;
106a8027fb0SKees Cook 
107a8027fb0SKees Cook 	return *lsm->enabled;
108f4941d75SKees Cook }
109f4941d75SKees Cook 
110f4941d75SKees Cook /* Mark an LSM's enabled flag. */
111f4941d75SKees Cook static int lsm_enabled_true __initdata = 1;
112f4941d75SKees Cook static int lsm_enabled_false __initdata = 0;
set_enabled(struct lsm_info * lsm,bool enabled)113f4941d75SKees Cook static void __init set_enabled(struct lsm_info *lsm, bool enabled)
114f4941d75SKees Cook {
115f4941d75SKees Cook 	/*
116f4941d75SKees Cook 	 * When an LSM hasn't configured an enable variable, we can use
117f4941d75SKees Cook 	 * a hard-coded location for storing the default enabled state.
118f4941d75SKees Cook 	 */
119f4941d75SKees Cook 	if (!lsm->enabled) {
120f4941d75SKees Cook 		if (enabled)
121f4941d75SKees Cook 			lsm->enabled = &lsm_enabled_true;
122f4941d75SKees Cook 		else
123f4941d75SKees Cook 			lsm->enabled = &lsm_enabled_false;
124f4941d75SKees Cook 	} else if (lsm->enabled == &lsm_enabled_true) {
125f4941d75SKees Cook 		if (!enabled)
126f4941d75SKees Cook 			lsm->enabled = &lsm_enabled_false;
127f4941d75SKees Cook 	} else if (lsm->enabled == &lsm_enabled_false) {
128f4941d75SKees Cook 		if (enabled)
129f4941d75SKees Cook 			lsm->enabled = &lsm_enabled_true;
130f4941d75SKees Cook 	} else {
131f4941d75SKees Cook 		*lsm->enabled = enabled;
132f4941d75SKees Cook 	}
133f4941d75SKees Cook }
134f4941d75SKees Cook 
1352d4d5119SKees Cook /* Is an LSM already listed in the ordered LSMs list? */
exists_ordered_lsm(struct lsm_info * lsm)1362d4d5119SKees Cook static bool __init exists_ordered_lsm(struct lsm_info *lsm)
1372d4d5119SKees Cook {
1382d4d5119SKees Cook 	struct lsm_info **check;
1392d4d5119SKees Cook 
1402d4d5119SKees Cook 	for (check = ordered_lsms; *check; check++)
1412d4d5119SKees Cook 		if (*check == lsm)
1422d4d5119SKees Cook 			return true;
1432d4d5119SKees Cook 
1442d4d5119SKees Cook 	return false;
1452d4d5119SKees Cook }
1462d4d5119SKees Cook 
1472d4d5119SKees Cook /* Append an LSM to the list of ordered LSMs to initialize. */
1482d4d5119SKees Cook static int last_lsm __initdata;
append_ordered_lsm(struct lsm_info * lsm,const char * from)1492d4d5119SKees Cook static void __init append_ordered_lsm(struct lsm_info *lsm, const char *from)
1502d4d5119SKees Cook {
1512d4d5119SKees Cook 	/* Ignore duplicate selections. */
1522d4d5119SKees Cook 	if (exists_ordered_lsm(lsm))
1532d4d5119SKees Cook 		return;
1542d4d5119SKees Cook 
1552d4d5119SKees Cook 	if (WARN(last_lsm == LSM_COUNT, "%s: out of LSM slots!?\n", from))
1562d4d5119SKees Cook 		return;
1572d4d5119SKees Cook 
158a8027fb0SKees Cook 	/* Enable this LSM, if it is not already set. */
159a8027fb0SKees Cook 	if (!lsm->enabled)
160a8027fb0SKees Cook 		lsm->enabled = &lsm_enabled_true;
1612d4d5119SKees Cook 	ordered_lsms[last_lsm++] = lsm;
162a8027fb0SKees Cook 
16386ef3c73SKees Cook 	init_debug("%s ordered: %s (%s)\n", from, lsm->name,
16486ef3c73SKees Cook 		   is_enabled(lsm) ? "enabled" : "disabled");
1652d4d5119SKees Cook }
1662d4d5119SKees Cook 
167f4941d75SKees Cook /* Is an LSM allowed to be initialized? */
lsm_allowed(struct lsm_info * lsm)168f4941d75SKees Cook static bool __init lsm_allowed(struct lsm_info *lsm)
169f4941d75SKees Cook {
170f4941d75SKees Cook 	/* Skip if the LSM is disabled. */
171f4941d75SKees Cook 	if (!is_enabled(lsm))
172f4941d75SKees Cook 		return false;
173f4941d75SKees Cook 
17414bd99c8SKees Cook 	/* Not allowed if another exclusive LSM already initialized. */
17514bd99c8SKees Cook 	if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && exclusive) {
17614bd99c8SKees Cook 		init_debug("exclusive disabled: %s\n", lsm->name);
17714bd99c8SKees Cook 		return false;
17814bd99c8SKees Cook 	}
17914bd99c8SKees Cook 
180f4941d75SKees Cook 	return true;
181f4941d75SKees Cook }
182f4941d75SKees Cook 
lsm_set_blob_size(int * need,int * lbs)183bbd3662aSCasey Schaufler static void __init lsm_set_blob_size(int *need, int *lbs)
184bbd3662aSCasey Schaufler {
185bbd3662aSCasey Schaufler 	int offset;
186bbd3662aSCasey Schaufler 
187b9f5ce27SGünther Noack 	if (*need <= 0)
188b9f5ce27SGünther Noack 		return;
189b9f5ce27SGünther Noack 
190b9f5ce27SGünther Noack 	offset = ALIGN(*lbs, sizeof(void *));
191b9f5ce27SGünther Noack 	*lbs = offset + *need;
192bbd3662aSCasey Schaufler 	*need = offset;
193bbd3662aSCasey Schaufler }
194bbd3662aSCasey Schaufler 
lsm_set_blob_sizes(struct lsm_blob_sizes * needed)195bbd3662aSCasey Schaufler static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
196bbd3662aSCasey Schaufler {
197bbd3662aSCasey Schaufler 	if (!needed)
198bbd3662aSCasey Schaufler 		return;
199bbd3662aSCasey Schaufler 
200bbd3662aSCasey Schaufler 	lsm_set_blob_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
20133bf60caSCasey Schaufler 	lsm_set_blob_size(&needed->lbs_file, &blob_sizes.lbs_file);
202afb1cbe3SCasey Schaufler 	/*
203afb1cbe3SCasey Schaufler 	 * The inode blob gets an rcu_head in addition to
204afb1cbe3SCasey Schaufler 	 * what the modules might need.
205afb1cbe3SCasey Schaufler 	 */
206afb1cbe3SCasey Schaufler 	if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
207afb1cbe3SCasey Schaufler 		blob_sizes.lbs_inode = sizeof(struct rcu_head);
208afb1cbe3SCasey Schaufler 	lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
209ecd5f82eSCasey Schaufler 	lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
210ecd5f82eSCasey Schaufler 	lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
2111aea7808SCasey Schaufler 	lsm_set_blob_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
212f4ad8f2cSCasey Schaufler 	lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
2136bcdfd2cSRoberto Sassu 	lsm_set_blob_size(&needed->lbs_xattr_count,
2146bcdfd2cSRoberto Sassu 			  &blob_sizes.lbs_xattr_count);
215bbd3662aSCasey Schaufler }
216bbd3662aSCasey Schaufler 
217d8e9bbd4SKees Cook /* Prepare LSM for initialization. */
prepare_lsm(struct lsm_info * lsm)218d8e9bbd4SKees Cook static void __init prepare_lsm(struct lsm_info *lsm)
219f4941d75SKees Cook {
220f4941d75SKees Cook 	int enabled = lsm_allowed(lsm);
221f4941d75SKees Cook 
222f4941d75SKees Cook 	/* Record enablement (to handle any following exclusive LSMs). */
223f4941d75SKees Cook 	set_enabled(lsm, enabled);
224f4941d75SKees Cook 
225d8e9bbd4SKees Cook 	/* If enabled, do pre-initialization work. */
226f4941d75SKees Cook 	if (enabled) {
22714bd99c8SKees Cook 		if ((lsm->flags & LSM_FLAG_EXCLUSIVE) && !exclusive) {
22814bd99c8SKees Cook 			exclusive = lsm;
22914bd99c8SKees Cook 			init_debug("exclusive chosen:   %s\n", lsm->name);
23014bd99c8SKees Cook 		}
231bbd3662aSCasey Schaufler 
232bbd3662aSCasey Schaufler 		lsm_set_blob_sizes(lsm->blobs);
233d8e9bbd4SKees Cook 	}
234d8e9bbd4SKees Cook }
235d8e9bbd4SKees Cook 
236d8e9bbd4SKees Cook /* Initialize a given LSM, if it is enabled. */
initialize_lsm(struct lsm_info * lsm)237d8e9bbd4SKees Cook static void __init initialize_lsm(struct lsm_info *lsm)
238d8e9bbd4SKees Cook {
239d8e9bbd4SKees Cook 	if (is_enabled(lsm)) {
240d8e9bbd4SKees Cook 		int ret;
24114bd99c8SKees Cook 
242f4941d75SKees Cook 		init_debug("initializing %s\n", lsm->name);
243f4941d75SKees Cook 		ret = lsm->init();
244f4941d75SKees Cook 		WARN(ret, "%s failed to initialize: %d\n", lsm->name, ret);
245f4941d75SKees Cook 	}
246f4941d75SKees Cook }
247f4941d75SKees Cook 
24813e735c0SKees Cook /* Populate ordered LSMs list from comma-separated LSM name list. */
ordered_lsm_parse(const char * order,const char * origin)2492d4d5119SKees Cook static void __init ordered_lsm_parse(const char *order, const char *origin)
250657d910bSKees Cook {
251657d910bSKees Cook 	struct lsm_info *lsm;
25213e735c0SKees Cook 	char *sep, *name, *next;
25313e735c0SKees Cook 
254e2bc445bSKees Cook 	/* LSM_ORDER_FIRST is always first. */
255e2bc445bSKees Cook 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
256e2bc445bSKees Cook 		if (lsm->order == LSM_ORDER_FIRST)
257e2bc445bSKees Cook 			append_ordered_lsm(lsm, "  first");
258e2bc445bSKees Cook 	}
259e2bc445bSKees Cook 
2607e611486SKees Cook 	/* Process "security=", if given. */
2617e611486SKees Cook 	if (chosen_major_lsm) {
2627e611486SKees Cook 		struct lsm_info *major;
2637e611486SKees Cook 
2647e611486SKees Cook 		/*
2657e611486SKees Cook 		 * To match the original "security=" behavior, this
2667e611486SKees Cook 		 * explicitly does NOT fallback to another Legacy Major
2677e611486SKees Cook 		 * if the selected one was separately disabled: disable
2687e611486SKees Cook 		 * all non-matching Legacy Major LSMs.
2697e611486SKees Cook 		 */
2707e611486SKees Cook 		for (major = __start_lsm_info; major < __end_lsm_info;
2717e611486SKees Cook 		     major++) {
2727e611486SKees Cook 			if ((major->flags & LSM_FLAG_LEGACY_MAJOR) &&
2737e611486SKees Cook 			    strcmp(major->name, chosen_major_lsm) != 0) {
2747e611486SKees Cook 				set_enabled(major, false);
27586ef3c73SKees Cook 				init_debug("security=%s disabled: %s (only one legacy major LSM)\n",
2767e611486SKees Cook 					   chosen_major_lsm, major->name);
2777e611486SKees Cook 			}
2787e611486SKees Cook 		}
2797e611486SKees Cook 	}
2805ef4e419SKees Cook 
28113e735c0SKees Cook 	sep = kstrdup(order, GFP_KERNEL);
28213e735c0SKees Cook 	next = sep;
28313e735c0SKees Cook 	/* Walk the list, looking for matching LSMs. */
28413e735c0SKees Cook 	while ((name = strsep(&next, ",")) != NULL) {
28513e735c0SKees Cook 		bool found = false;
286657d910bSKees Cook 
287657d910bSKees Cook 		for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
28842994ee3SRoberto Sassu 			if (strcmp(lsm->name, name) == 0) {
28942994ee3SRoberto Sassu 				if (lsm->order == LSM_ORDER_MUTABLE)
2902d4d5119SKees Cook 					append_ordered_lsm(lsm, origin);
29113e735c0SKees Cook 				found = true;
292657d910bSKees Cook 			}
293657d910bSKees Cook 		}
294657d910bSKees Cook 
29513e735c0SKees Cook 		if (!found)
29686ef3c73SKees Cook 			init_debug("%s ignored: %s (not built into kernel)\n",
29786ef3c73SKees Cook 				   origin, name);
29813e735c0SKees Cook 	}
299c91d8106SCasey Schaufler 
300c91d8106SCasey Schaufler 	/* Process "security=", if given. */
301c91d8106SCasey Schaufler 	if (chosen_major_lsm) {
302c91d8106SCasey Schaufler 		for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
303c91d8106SCasey Schaufler 			if (exists_ordered_lsm(lsm))
304c91d8106SCasey Schaufler 				continue;
305c91d8106SCasey Schaufler 			if (strcmp(lsm->name, chosen_major_lsm) == 0)
306c91d8106SCasey Schaufler 				append_ordered_lsm(lsm, "security=");
307c91d8106SCasey Schaufler 		}
308c91d8106SCasey Schaufler 	}
309c91d8106SCasey Schaufler 
31042994ee3SRoberto Sassu 	/* LSM_ORDER_LAST is always last. */
31142994ee3SRoberto Sassu 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
31242994ee3SRoberto Sassu 		if (lsm->order == LSM_ORDER_LAST)
31342994ee3SRoberto Sassu 			append_ordered_lsm(lsm, "   last");
31442994ee3SRoberto Sassu 	}
31542994ee3SRoberto Sassu 
316c91d8106SCasey Schaufler 	/* Disable all LSMs not in the ordered list. */
317c91d8106SCasey Schaufler 	for (lsm = __start_lsm_info; lsm < __end_lsm_info; lsm++) {
318c91d8106SCasey Schaufler 		if (exists_ordered_lsm(lsm))
319c91d8106SCasey Schaufler 			continue;
320c91d8106SCasey Schaufler 		set_enabled(lsm, false);
32186ef3c73SKees Cook 		init_debug("%s skipped: %s (not in requested order)\n",
32286ef3c73SKees Cook 			   origin, lsm->name);
323c91d8106SCasey Schaufler 	}
324c91d8106SCasey Schaufler 
32513e735c0SKees Cook 	kfree(sep);
32613e735c0SKees Cook }
32713e735c0SKees Cook 
3281cfb2a51STetsuo Handa static void __init lsm_early_cred(struct cred *cred);
3291cfb2a51STetsuo Handa static void __init lsm_early_task(struct task_struct *task);
3301cfb2a51STetsuo Handa 
331e6b1db98SMatthew Garrett static int lsm_append(const char *new, char **result);
332e6b1db98SMatthew Garrett 
report_lsm_order(void)33386ef3c73SKees Cook static void __init report_lsm_order(void)
33486ef3c73SKees Cook {
33586ef3c73SKees Cook 	struct lsm_info **lsm, *early;
33686ef3c73SKees Cook 	int first = 0;
33786ef3c73SKees Cook 
33886ef3c73SKees Cook 	pr_info("initializing lsm=");
33986ef3c73SKees Cook 
34086ef3c73SKees Cook 	/* Report each enabled LSM name, comma separated. */
34163c1845bSPaul Moore 	for (early = __start_early_lsm_info;
34263c1845bSPaul Moore 	     early < __end_early_lsm_info; early++)
34386ef3c73SKees Cook 		if (is_enabled(early))
34486ef3c73SKees Cook 			pr_cont("%s%s", first++ == 0 ? "" : ",", early->name);
34586ef3c73SKees Cook 	for (lsm = ordered_lsms; *lsm; lsm++)
34686ef3c73SKees Cook 		if (is_enabled(*lsm))
34786ef3c73SKees Cook 			pr_cont("%s%s", first++ == 0 ? "" : ",", (*lsm)->name);
34886ef3c73SKees Cook 
34986ef3c73SKees Cook 	pr_cont("\n");
35086ef3c73SKees Cook }
35186ef3c73SKees Cook 
ordered_lsm_init(void)3522d4d5119SKees Cook static void __init ordered_lsm_init(void)
3532d4d5119SKees Cook {
3542d4d5119SKees Cook 	struct lsm_info **lsm;
3552d4d5119SKees Cook 
3562d4d5119SKees Cook 	ordered_lsms = kcalloc(LSM_COUNT + 1, sizeof(*ordered_lsms),
3572d4d5119SKees Cook 			       GFP_KERNEL);
3582d4d5119SKees Cook 
35989a9684eSKees Cook 	if (chosen_lsm_order) {
36089a9684eSKees Cook 		if (chosen_major_lsm) {
36186ef3c73SKees Cook 			pr_warn("security=%s is ignored because it is superseded by lsm=%s\n",
36286ef3c73SKees Cook 				chosen_major_lsm, chosen_lsm_order);
36389a9684eSKees Cook 			chosen_major_lsm = NULL;
36489a9684eSKees Cook 		}
36579f7865dSKees Cook 		ordered_lsm_parse(chosen_lsm_order, "cmdline");
36689a9684eSKees Cook 	} else
36713e735c0SKees Cook 		ordered_lsm_parse(builtin_lsm_order, "builtin");
3682d4d5119SKees Cook 
3692d4d5119SKees Cook 	for (lsm = ordered_lsms; *lsm; lsm++)
370d8e9bbd4SKees Cook 		prepare_lsm(*lsm);
371d8e9bbd4SKees Cook 
37286ef3c73SKees Cook 	report_lsm_order();
37386ef3c73SKees Cook 
374bbd3662aSCasey Schaufler 	init_debug("cred blob size       = %d\n", blob_sizes.lbs_cred);
37533bf60caSCasey Schaufler 	init_debug("file blob size       = %d\n", blob_sizes.lbs_file);
376afb1cbe3SCasey Schaufler 	init_debug("inode blob size      = %d\n", blob_sizes.lbs_inode);
377ecd5f82eSCasey Schaufler 	init_debug("ipc blob size        = %d\n", blob_sizes.lbs_ipc);
378ecd5f82eSCasey Schaufler 	init_debug("msg_msg blob size    = %d\n", blob_sizes.lbs_msg_msg);
3791aea7808SCasey Schaufler 	init_debug("superblock blob size = %d\n", blob_sizes.lbs_superblock);
380f4ad8f2cSCasey Schaufler 	init_debug("task blob size       = %d\n", blob_sizes.lbs_task);
3816bcdfd2cSRoberto Sassu 	init_debug("xattr slots          = %d\n", blob_sizes.lbs_xattr_count);
38233bf60caSCasey Schaufler 
38333bf60caSCasey Schaufler 	/*
38433bf60caSCasey Schaufler 	 * Create any kmem_caches needed for blobs
38533bf60caSCasey Schaufler 	 */
38633bf60caSCasey Schaufler 	if (blob_sizes.lbs_file)
38733bf60caSCasey Schaufler 		lsm_file_cache = kmem_cache_create("lsm_file_cache",
38833bf60caSCasey Schaufler 						   blob_sizes.lbs_file, 0,
38933bf60caSCasey Schaufler 						   SLAB_PANIC, NULL);
390afb1cbe3SCasey Schaufler 	if (blob_sizes.lbs_inode)
391afb1cbe3SCasey Schaufler 		lsm_inode_cache = kmem_cache_create("lsm_inode_cache",
392afb1cbe3SCasey Schaufler 						    blob_sizes.lbs_inode, 0,
393afb1cbe3SCasey Schaufler 						    SLAB_PANIC, NULL);
394bbd3662aSCasey Schaufler 
3951cfb2a51STetsuo Handa 	lsm_early_cred((struct cred *) current->cred);
3961cfb2a51STetsuo Handa 	lsm_early_task(current);
397d8e9bbd4SKees Cook 	for (lsm = ordered_lsms; *lsm; lsm++)
398d8e9bbd4SKees Cook 		initialize_lsm(*lsm);
3992d4d5119SKees Cook 
4002d4d5119SKees Cook 	kfree(ordered_lsms);
4012d4d5119SKees Cook }
4022d4d5119SKees Cook 
early_security_init(void)403e6b1db98SMatthew Garrett int __init early_security_init(void)
404e6b1db98SMatthew Garrett {
405e6b1db98SMatthew Garrett 	struct lsm_info *lsm;
406e6b1db98SMatthew Garrett 
40775c1182eSBill Wendling #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
40875c1182eSBill Wendling 	INIT_HLIST_HEAD(&security_hook_heads.NAME);
40975c1182eSBill Wendling #include "linux/lsm_hook_defs.h"
41075c1182eSBill Wendling #undef LSM_HOOK
411e6b1db98SMatthew Garrett 
412e6b1db98SMatthew Garrett 	for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
413e6b1db98SMatthew Garrett 		if (!lsm->enabled)
414e6b1db98SMatthew Garrett 			lsm->enabled = &lsm_enabled_true;
415e6b1db98SMatthew Garrett 		prepare_lsm(lsm);
416e6b1db98SMatthew Garrett 		initialize_lsm(lsm);
417e6b1db98SMatthew Garrett 	}
418e6b1db98SMatthew Garrett 
419e6b1db98SMatthew Garrett 	return 0;
420e6b1db98SMatthew Garrett }
421e6b1db98SMatthew Garrett 
4221da177e4SLinus Torvalds /**
4231da177e4SLinus Torvalds  * security_init - initializes the security framework
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  * This should be called early in the kernel initialization sequence.
4261da177e4SLinus Torvalds  */
security_init(void)4271da177e4SLinus Torvalds int __init security_init(void)
4281da177e4SLinus Torvalds {
429e6b1db98SMatthew Garrett 	struct lsm_info *lsm;
4303dfc9b02STetsuo Handa 
43186ef3c73SKees Cook 	init_debug("legacy security=%s\n", chosen_major_lsm ? : " *unspecified*");
43286ef3c73SKees Cook 	init_debug("  CONFIG_LSM=%s\n", builtin_lsm_order);
43386ef3c73SKees Cook 	init_debug("boot arg lsm=%s\n", chosen_lsm_order ? : " *unspecified*");
43498d29170SKees Cook 
435e6b1db98SMatthew Garrett 	/*
436e6b1db98SMatthew Garrett 	 * Append the names of the early LSM modules now that kmalloc() is
437e6b1db98SMatthew Garrett 	 * available
438e6b1db98SMatthew Garrett 	 */
439e6b1db98SMatthew Garrett 	for (lsm = __start_early_lsm_info; lsm < __end_early_lsm_info; lsm++) {
44086ef3c73SKees Cook 		init_debug("  early started: %s (%s)\n", lsm->name,
44186ef3c73SKees Cook 			   is_enabled(lsm) ? "enabled" : "disabled");
442e6b1db98SMatthew Garrett 		if (lsm->enabled)
443e6b1db98SMatthew Garrett 			lsm_append(lsm->name, &lsm_names);
444e6b1db98SMatthew Garrett 	}
4451da177e4SLinus Torvalds 
446657d910bSKees Cook 	/* Load LSMs in specified order. */
447657d910bSKees Cook 	ordered_lsm_init();
448657d910bSKees Cook 
4491da177e4SLinus Torvalds 	return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452076c54c5SAhmed S. Darwish /* Save user chosen LSM */
choose_major_lsm(char * str)4535ef4e419SKees Cook static int __init choose_major_lsm(char *str)
454076c54c5SAhmed S. Darwish {
4555ef4e419SKees Cook 	chosen_major_lsm = str;
456076c54c5SAhmed S. Darwish 	return 1;
457076c54c5SAhmed S. Darwish }
4585ef4e419SKees Cook __setup("security=", choose_major_lsm);
459076c54c5SAhmed S. Darwish 
46079f7865dSKees Cook /* Explicitly choose LSM initialization order. */
choose_lsm_order(char * str)46179f7865dSKees Cook static int __init choose_lsm_order(char *str)
46279f7865dSKees Cook {
46379f7865dSKees Cook 	chosen_lsm_order = str;
46479f7865dSKees Cook 	return 1;
46579f7865dSKees Cook }
46679f7865dSKees Cook __setup("lsm=", choose_lsm_order);
46779f7865dSKees Cook 
4689b8c7c14SKees Cook /* Enable LSM order debugging. */
enable_debug(char * str)4699b8c7c14SKees Cook static int __init enable_debug(char *str)
4709b8c7c14SKees Cook {
4719b8c7c14SKees Cook 	debug = true;
4729b8c7c14SKees Cook 	return 1;
4739b8c7c14SKees Cook }
4749b8c7c14SKees Cook __setup("lsm.debug", enable_debug);
4759b8c7c14SKees Cook 
match_last_lsm(const char * list,const char * lsm)4763bb857e4SMickaël Salaün static bool match_last_lsm(const char *list, const char *lsm)
4773bb857e4SMickaël Salaün {
4783bb857e4SMickaël Salaün 	const char *last;
4793bb857e4SMickaël Salaün 
4803bb857e4SMickaël Salaün 	if (WARN_ON(!list || !lsm))
4813bb857e4SMickaël Salaün 		return false;
4823bb857e4SMickaël Salaün 	last = strrchr(list, ',');
4833bb857e4SMickaël Salaün 	if (last)
4843bb857e4SMickaël Salaün 		/* Pass the comma, strcmp() will check for '\0' */
4853bb857e4SMickaël Salaün 		last++;
4863bb857e4SMickaël Salaün 	else
4873bb857e4SMickaël Salaün 		last = list;
4883bb857e4SMickaël Salaün 	return !strcmp(last, lsm);
4893bb857e4SMickaël Salaün }
4903bb857e4SMickaël Salaün 
lsm_append(const char * new,char ** result)491e6b1db98SMatthew Garrett static int lsm_append(const char *new, char **result)
492d69dece5SCasey Schaufler {
493d69dece5SCasey Schaufler 	char *cp;
494d69dece5SCasey Schaufler 
495d69dece5SCasey Schaufler 	if (*result == NULL) {
496d69dece5SCasey Schaufler 		*result = kstrdup(new, GFP_KERNEL);
49787ea5843SEric Biggers 		if (*result == NULL)
49887ea5843SEric Biggers 			return -ENOMEM;
499d69dece5SCasey Schaufler 	} else {
5003bb857e4SMickaël Salaün 		/* Check if it is the last registered name */
5013bb857e4SMickaël Salaün 		if (match_last_lsm(*result, new))
5023bb857e4SMickaël Salaün 			return 0;
503d69dece5SCasey Schaufler 		cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
504d69dece5SCasey Schaufler 		if (cp == NULL)
505d69dece5SCasey Schaufler 			return -ENOMEM;
506d69dece5SCasey Schaufler 		kfree(*result);
507d69dece5SCasey Schaufler 		*result = cp;
508d69dece5SCasey Schaufler 	}
509d69dece5SCasey Schaufler 	return 0;
510d69dece5SCasey Schaufler }
511d69dece5SCasey Schaufler 
512076c54c5SAhmed S. Darwish /**
513d69dece5SCasey Schaufler  * security_add_hooks - Add a modules hooks to the hook lists.
514d69dece5SCasey Schaufler  * @hooks: the hooks to add
515d69dece5SCasey Schaufler  * @count: the number of hooks to add
516d69dece5SCasey Schaufler  * @lsm: the name of the security module
517d69dece5SCasey Schaufler  *
518d69dece5SCasey Schaufler  * Each LSM has to register its hooks with the infrastructure.
519d69dece5SCasey Schaufler  */
security_add_hooks(struct security_hook_list * hooks,int count,const char * lsm)520d69dece5SCasey Schaufler void __init security_add_hooks(struct security_hook_list *hooks, int count,
5211af0e4a0SChristian Göttsche 			       const char *lsm)
522d69dece5SCasey Schaufler {
523d69dece5SCasey Schaufler 	int i;
524d69dece5SCasey Schaufler 
525d69dece5SCasey Schaufler 	for (i = 0; i < count; i++) {
526d69dece5SCasey Schaufler 		hooks[i].lsm = lsm;
527df0ce173SSargun Dhillon 		hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
528d69dece5SCasey Schaufler 	}
529e6b1db98SMatthew Garrett 
530e6b1db98SMatthew Garrett 	/*
531e6b1db98SMatthew Garrett 	 * Don't try to append during early_security_init(), we'll come back
532e6b1db98SMatthew Garrett 	 * and fix this up afterwards.
533e6b1db98SMatthew Garrett 	 */
534e6b1db98SMatthew Garrett 	if (slab_is_available()) {
535d69dece5SCasey Schaufler 		if (lsm_append(lsm, &lsm_names) < 0)
536d69dece5SCasey Schaufler 			panic("%s - Cannot get early memory.\n", __func__);
537d69dece5SCasey Schaufler 	}
538e6b1db98SMatthew Garrett }
539d69dece5SCasey Schaufler 
call_blocking_lsm_notifier(enum lsm_event event,void * data)54042df744cSJanne Karhunen int call_blocking_lsm_notifier(enum lsm_event event, void *data)
5418f408ab6SDaniel Jurgens {
54242df744cSJanne Karhunen 	return blocking_notifier_call_chain(&blocking_lsm_notifier_chain,
54342df744cSJanne Karhunen 					    event, data);
5448f408ab6SDaniel Jurgens }
54542df744cSJanne Karhunen EXPORT_SYMBOL(call_blocking_lsm_notifier);
5468f408ab6SDaniel Jurgens 
register_blocking_lsm_notifier(struct notifier_block * nb)54742df744cSJanne Karhunen int register_blocking_lsm_notifier(struct notifier_block *nb)
5488f408ab6SDaniel Jurgens {
54942df744cSJanne Karhunen 	return blocking_notifier_chain_register(&blocking_lsm_notifier_chain,
55042df744cSJanne Karhunen 						nb);
5518f408ab6SDaniel Jurgens }
55242df744cSJanne Karhunen EXPORT_SYMBOL(register_blocking_lsm_notifier);
5538f408ab6SDaniel Jurgens 
unregister_blocking_lsm_notifier(struct notifier_block * nb)55442df744cSJanne Karhunen int unregister_blocking_lsm_notifier(struct notifier_block *nb)
5558f408ab6SDaniel Jurgens {
55642df744cSJanne Karhunen 	return blocking_notifier_chain_unregister(&blocking_lsm_notifier_chain,
55742df744cSJanne Karhunen 						  nb);
5588f408ab6SDaniel Jurgens }
55942df744cSJanne Karhunen EXPORT_SYMBOL(unregister_blocking_lsm_notifier);
5608f408ab6SDaniel Jurgens 
561bbd3662aSCasey Schaufler /**
562bbd3662aSCasey Schaufler  * lsm_cred_alloc - allocate a composite cred blob
563bbd3662aSCasey Schaufler  * @cred: the cred that needs a blob
564bbd3662aSCasey Schaufler  * @gfp: allocation type
565bbd3662aSCasey Schaufler  *
566bbd3662aSCasey Schaufler  * Allocate the cred blob for all the modules
567bbd3662aSCasey Schaufler  *
568bbd3662aSCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
569bbd3662aSCasey Schaufler  */
lsm_cred_alloc(struct cred * cred,gfp_t gfp)570bbd3662aSCasey Schaufler static int lsm_cred_alloc(struct cred *cred, gfp_t gfp)
571bbd3662aSCasey Schaufler {
572bbd3662aSCasey Schaufler 	if (blob_sizes.lbs_cred == 0) {
573bbd3662aSCasey Schaufler 		cred->security = NULL;
574bbd3662aSCasey Schaufler 		return 0;
575bbd3662aSCasey Schaufler 	}
576bbd3662aSCasey Schaufler 
577bbd3662aSCasey Schaufler 	cred->security = kzalloc(blob_sizes.lbs_cred, gfp);
578bbd3662aSCasey Schaufler 	if (cred->security == NULL)
579bbd3662aSCasey Schaufler 		return -ENOMEM;
580bbd3662aSCasey Schaufler 	return 0;
581bbd3662aSCasey Schaufler }
582bbd3662aSCasey Schaufler 
583bbd3662aSCasey Schaufler /**
584bbd3662aSCasey Schaufler  * lsm_early_cred - during initialization allocate a composite cred blob
585bbd3662aSCasey Schaufler  * @cred: the cred that needs a blob
586bbd3662aSCasey Schaufler  *
5871cfb2a51STetsuo Handa  * Allocate the cred blob for all the modules
588bbd3662aSCasey Schaufler  */
lsm_early_cred(struct cred * cred)5891cfb2a51STetsuo Handa static void __init lsm_early_cred(struct cred *cred)
590bbd3662aSCasey Schaufler {
5911cfb2a51STetsuo Handa 	int rc = lsm_cred_alloc(cred, GFP_KERNEL);
592bbd3662aSCasey Schaufler 
593bbd3662aSCasey Schaufler 	if (rc)
594bbd3662aSCasey Schaufler 		panic("%s: Early cred alloc failed.\n", __func__);
595bbd3662aSCasey Schaufler }
596bbd3662aSCasey Schaufler 
59733bf60caSCasey Schaufler /**
59833bf60caSCasey Schaufler  * lsm_file_alloc - allocate a composite file blob
59933bf60caSCasey Schaufler  * @file: the file that needs a blob
60033bf60caSCasey Schaufler  *
60133bf60caSCasey Schaufler  * Allocate the file blob for all the modules
60233bf60caSCasey Schaufler  *
60333bf60caSCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
60433bf60caSCasey Schaufler  */
lsm_file_alloc(struct file * file)60533bf60caSCasey Schaufler static int lsm_file_alloc(struct file *file)
60633bf60caSCasey Schaufler {
60733bf60caSCasey Schaufler 	if (!lsm_file_cache) {
60833bf60caSCasey Schaufler 		file->f_security = NULL;
60933bf60caSCasey Schaufler 		return 0;
61033bf60caSCasey Schaufler 	}
61133bf60caSCasey Schaufler 
61233bf60caSCasey Schaufler 	file->f_security = kmem_cache_zalloc(lsm_file_cache, GFP_KERNEL);
61333bf60caSCasey Schaufler 	if (file->f_security == NULL)
61433bf60caSCasey Schaufler 		return -ENOMEM;
61533bf60caSCasey Schaufler 	return 0;
61633bf60caSCasey Schaufler }
61733bf60caSCasey Schaufler 
618afb1cbe3SCasey Schaufler /**
619afb1cbe3SCasey Schaufler  * lsm_inode_alloc - allocate a composite inode blob
620afb1cbe3SCasey Schaufler  * @inode: the inode that needs a blob
621afb1cbe3SCasey Schaufler  *
622afb1cbe3SCasey Schaufler  * Allocate the inode blob for all the modules
623afb1cbe3SCasey Schaufler  *
624afb1cbe3SCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
625afb1cbe3SCasey Schaufler  */
lsm_inode_alloc(struct inode * inode)626afb1cbe3SCasey Schaufler int lsm_inode_alloc(struct inode *inode)
627afb1cbe3SCasey Schaufler {
628afb1cbe3SCasey Schaufler 	if (!lsm_inode_cache) {
629afb1cbe3SCasey Schaufler 		inode->i_security = NULL;
630afb1cbe3SCasey Schaufler 		return 0;
631afb1cbe3SCasey Schaufler 	}
632afb1cbe3SCasey Schaufler 
633afb1cbe3SCasey Schaufler 	inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
634afb1cbe3SCasey Schaufler 	if (inode->i_security == NULL)
635afb1cbe3SCasey Schaufler 		return -ENOMEM;
636afb1cbe3SCasey Schaufler 	return 0;
637afb1cbe3SCasey Schaufler }
638afb1cbe3SCasey Schaufler 
639f4ad8f2cSCasey Schaufler /**
640f4ad8f2cSCasey Schaufler  * lsm_task_alloc - allocate a composite task blob
641f4ad8f2cSCasey Schaufler  * @task: the task that needs a blob
642f4ad8f2cSCasey Schaufler  *
643f4ad8f2cSCasey Schaufler  * Allocate the task blob for all the modules
644f4ad8f2cSCasey Schaufler  *
645f4ad8f2cSCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
646f4ad8f2cSCasey Schaufler  */
lsm_task_alloc(struct task_struct * task)6473e8c7367SWei Yongjun static int lsm_task_alloc(struct task_struct *task)
648f4ad8f2cSCasey Schaufler {
649f4ad8f2cSCasey Schaufler 	if (blob_sizes.lbs_task == 0) {
650f4ad8f2cSCasey Schaufler 		task->security = NULL;
651f4ad8f2cSCasey Schaufler 		return 0;
652f4ad8f2cSCasey Schaufler 	}
653f4ad8f2cSCasey Schaufler 
654f4ad8f2cSCasey Schaufler 	task->security = kzalloc(blob_sizes.lbs_task, GFP_KERNEL);
655f4ad8f2cSCasey Schaufler 	if (task->security == NULL)
656f4ad8f2cSCasey Schaufler 		return -ENOMEM;
657f4ad8f2cSCasey Schaufler 	return 0;
658f4ad8f2cSCasey Schaufler }
659f4ad8f2cSCasey Schaufler 
660f4ad8f2cSCasey Schaufler /**
661ecd5f82eSCasey Schaufler  * lsm_ipc_alloc - allocate a composite ipc blob
662ecd5f82eSCasey Schaufler  * @kip: the ipc that needs a blob
663ecd5f82eSCasey Schaufler  *
664ecd5f82eSCasey Schaufler  * Allocate the ipc blob for all the modules
665ecd5f82eSCasey Schaufler  *
666ecd5f82eSCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
667ecd5f82eSCasey Schaufler  */
lsm_ipc_alloc(struct kern_ipc_perm * kip)6683e8c7367SWei Yongjun static int lsm_ipc_alloc(struct kern_ipc_perm *kip)
669ecd5f82eSCasey Schaufler {
670ecd5f82eSCasey Schaufler 	if (blob_sizes.lbs_ipc == 0) {
671ecd5f82eSCasey Schaufler 		kip->security = NULL;
672ecd5f82eSCasey Schaufler 		return 0;
673ecd5f82eSCasey Schaufler 	}
674ecd5f82eSCasey Schaufler 
675ecd5f82eSCasey Schaufler 	kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
676ecd5f82eSCasey Schaufler 	if (kip->security == NULL)
677ecd5f82eSCasey Schaufler 		return -ENOMEM;
678ecd5f82eSCasey Schaufler 	return 0;
679ecd5f82eSCasey Schaufler }
680ecd5f82eSCasey Schaufler 
681ecd5f82eSCasey Schaufler /**
682ecd5f82eSCasey Schaufler  * lsm_msg_msg_alloc - allocate a composite msg_msg blob
683ecd5f82eSCasey Schaufler  * @mp: the msg_msg that needs a blob
684ecd5f82eSCasey Schaufler  *
685ecd5f82eSCasey Schaufler  * Allocate the ipc blob for all the modules
686ecd5f82eSCasey Schaufler  *
687ecd5f82eSCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
688ecd5f82eSCasey Schaufler  */
lsm_msg_msg_alloc(struct msg_msg * mp)6893e8c7367SWei Yongjun static int lsm_msg_msg_alloc(struct msg_msg *mp)
690ecd5f82eSCasey Schaufler {
691ecd5f82eSCasey Schaufler 	if (blob_sizes.lbs_msg_msg == 0) {
692ecd5f82eSCasey Schaufler 		mp->security = NULL;
693ecd5f82eSCasey Schaufler 		return 0;
694ecd5f82eSCasey Schaufler 	}
695ecd5f82eSCasey Schaufler 
696ecd5f82eSCasey Schaufler 	mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
697ecd5f82eSCasey Schaufler 	if (mp->security == NULL)
698ecd5f82eSCasey Schaufler 		return -ENOMEM;
699ecd5f82eSCasey Schaufler 	return 0;
700ecd5f82eSCasey Schaufler }
701ecd5f82eSCasey Schaufler 
702ecd5f82eSCasey Schaufler /**
703f4ad8f2cSCasey Schaufler  * lsm_early_task - during initialization allocate a composite task blob
704f4ad8f2cSCasey Schaufler  * @task: the task that needs a blob
705f4ad8f2cSCasey Schaufler  *
7061cfb2a51STetsuo Handa  * Allocate the task blob for all the modules
707f4ad8f2cSCasey Schaufler  */
lsm_early_task(struct task_struct * task)7081cfb2a51STetsuo Handa static void __init lsm_early_task(struct task_struct *task)
709f4ad8f2cSCasey Schaufler {
7101cfb2a51STetsuo Handa 	int rc = lsm_task_alloc(task);
711f4ad8f2cSCasey Schaufler 
712f4ad8f2cSCasey Schaufler 	if (rc)
713f4ad8f2cSCasey Schaufler 		panic("%s: Early task alloc failed.\n", __func__);
714f4ad8f2cSCasey Schaufler }
715f4ad8f2cSCasey Schaufler 
7161aea7808SCasey Schaufler /**
7171aea7808SCasey Schaufler  * lsm_superblock_alloc - allocate a composite superblock blob
7181aea7808SCasey Schaufler  * @sb: the superblock that needs a blob
7191aea7808SCasey Schaufler  *
7201aea7808SCasey Schaufler  * Allocate the superblock blob for all the modules
7211aea7808SCasey Schaufler  *
7221aea7808SCasey Schaufler  * Returns 0, or -ENOMEM if memory can't be allocated.
7231aea7808SCasey Schaufler  */
lsm_superblock_alloc(struct super_block * sb)7241aea7808SCasey Schaufler static int lsm_superblock_alloc(struct super_block *sb)
7251aea7808SCasey Schaufler {
7261aea7808SCasey Schaufler 	if (blob_sizes.lbs_superblock == 0) {
7271aea7808SCasey Schaufler 		sb->s_security = NULL;
7281aea7808SCasey Schaufler 		return 0;
7291aea7808SCasey Schaufler 	}
7301aea7808SCasey Schaufler 
7311aea7808SCasey Schaufler 	sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
7321aea7808SCasey Schaufler 	if (sb->s_security == NULL)
7331aea7808SCasey Schaufler 		return -ENOMEM;
7341aea7808SCasey Schaufler 	return 0;
7351aea7808SCasey Schaufler }
7361aea7808SCasey Schaufler 
737f25fce3eSCasey Schaufler /*
73898e828a0SKP Singh  * The default value of the LSM hook is defined in linux/lsm_hook_defs.h and
73998e828a0SKP Singh  * can be accessed with:
74098e828a0SKP Singh  *
74198e828a0SKP Singh  *	LSM_RET_DEFAULT(<hook_name>)
74298e828a0SKP Singh  *
74398e828a0SKP Singh  * The macros below define static constants for the default value of each
74498e828a0SKP Singh  * LSM hook.
74598e828a0SKP Singh  */
74698e828a0SKP Singh #define LSM_RET_DEFAULT(NAME) (NAME##_default)
74798e828a0SKP Singh #define DECLARE_LSM_RET_DEFAULT_void(DEFAULT, NAME)
74898e828a0SKP Singh #define DECLARE_LSM_RET_DEFAULT_int(DEFAULT, NAME) \
74986dd9fd5SKees Cook 	static const int __maybe_unused LSM_RET_DEFAULT(NAME) = (DEFAULT);
75098e828a0SKP Singh #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
75198e828a0SKP Singh 	DECLARE_LSM_RET_DEFAULT_##RET(DEFAULT, NAME)
75298e828a0SKP Singh 
75398e828a0SKP Singh #include <linux/lsm_hook_defs.h>
75498e828a0SKP Singh #undef LSM_HOOK
75598e828a0SKP Singh 
75698e828a0SKP Singh /*
757b1d9e6b0SCasey Schaufler  * Hook list operation macros.
7581da177e4SLinus Torvalds  *
759f25fce3eSCasey Schaufler  * call_void_hook:
760f25fce3eSCasey Schaufler  *	This is a hook that does not return a value.
7611da177e4SLinus Torvalds  *
762f25fce3eSCasey Schaufler  * call_int_hook:
763f25fce3eSCasey Schaufler  *	This is a hook that returns a value.
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds 
766b1d9e6b0SCasey Schaufler #define call_void_hook(FUNC, ...)				\
767b1d9e6b0SCasey Schaufler 	do {							\
768b1d9e6b0SCasey Schaufler 		struct security_hook_list *P;			\
769b1d9e6b0SCasey Schaufler 								\
770df0ce173SSargun Dhillon 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
771b1d9e6b0SCasey Schaufler 			P->hook.FUNC(__VA_ARGS__);		\
772b1d9e6b0SCasey Schaufler 	} while (0)
7731da177e4SLinus Torvalds 
774b1d9e6b0SCasey Schaufler #define call_int_hook(FUNC, IRC, ...) ({			\
775b1d9e6b0SCasey Schaufler 	int RC = IRC;						\
776b1d9e6b0SCasey Schaufler 	do {							\
777b1d9e6b0SCasey Schaufler 		struct security_hook_list *P;			\
778b1d9e6b0SCasey Schaufler 								\
779df0ce173SSargun Dhillon 		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
780b1d9e6b0SCasey Schaufler 			RC = P->hook.FUNC(__VA_ARGS__);		\
781b1d9e6b0SCasey Schaufler 			if (RC != 0)				\
782b1d9e6b0SCasey Schaufler 				break;				\
783b1d9e6b0SCasey Schaufler 		}						\
784b1d9e6b0SCasey Schaufler 	} while (0);						\
785b1d9e6b0SCasey Schaufler 	RC;							\
786b1d9e6b0SCasey Schaufler })
7871da177e4SLinus Torvalds 
78820510f2fSJames Morris /* Security operations */
78920510f2fSJames Morris 
7901427ddbeSPaul Moore /**
7911427ddbeSPaul Moore  * security_binder_set_context_mgr() - Check if becoming binder ctx mgr is ok
7921427ddbeSPaul Moore  * @mgr: task credentials of current binder process
7931427ddbeSPaul Moore  *
7941427ddbeSPaul Moore  * Check whether @mgr is allowed to be the binder context manager.
7951427ddbeSPaul Moore  *
7961427ddbeSPaul Moore  * Return: Return 0 if permission is granted.
7971427ddbeSPaul Moore  */
security_binder_set_context_mgr(const struct cred * mgr)79852f88693STodd Kjos int security_binder_set_context_mgr(const struct cred *mgr)
79979af7307SStephen Smalley {
800f25fce3eSCasey Schaufler 	return call_int_hook(binder_set_context_mgr, 0, mgr);
80179af7307SStephen Smalley }
80279af7307SStephen Smalley 
8031427ddbeSPaul Moore /**
8041427ddbeSPaul Moore  * security_binder_transaction() - Check if a binder transaction is allowed
8051427ddbeSPaul Moore  * @from: sending process
8061427ddbeSPaul Moore  * @to: receiving process
8071427ddbeSPaul Moore  *
8081427ddbeSPaul Moore  * Check whether @from is allowed to invoke a binder transaction call to @to.
8091427ddbeSPaul Moore  *
8101427ddbeSPaul Moore  * Return: Returns 0 if permission is granted.
8111427ddbeSPaul Moore  */
security_binder_transaction(const struct cred * from,const struct cred * to)81252f88693STodd Kjos int security_binder_transaction(const struct cred *from,
81352f88693STodd Kjos 				const struct cred *to)
81479af7307SStephen Smalley {
815f25fce3eSCasey Schaufler 	return call_int_hook(binder_transaction, 0, from, to);
81679af7307SStephen Smalley }
81779af7307SStephen Smalley 
8181427ddbeSPaul Moore /**
8191427ddbeSPaul Moore  * security_binder_transfer_binder() - Check if a binder transfer is allowed
8201427ddbeSPaul Moore  * @from: sending process
8211427ddbeSPaul Moore  * @to: receiving process
8221427ddbeSPaul Moore  *
8231427ddbeSPaul Moore  * Check whether @from is allowed to transfer a binder reference to @to.
8241427ddbeSPaul Moore  *
8251427ddbeSPaul Moore  * Return: Returns 0 if permission is granted.
8261427ddbeSPaul Moore  */
security_binder_transfer_binder(const struct cred * from,const struct cred * to)82752f88693STodd Kjos int security_binder_transfer_binder(const struct cred *from,
82852f88693STodd Kjos 				    const struct cred *to)
82979af7307SStephen Smalley {
830f25fce3eSCasey Schaufler 	return call_int_hook(binder_transfer_binder, 0, from, to);
83179af7307SStephen Smalley }
83279af7307SStephen Smalley 
8331427ddbeSPaul Moore /**
8341427ddbeSPaul Moore  * security_binder_transfer_file() - Check if a binder file xfer is allowed
8351427ddbeSPaul Moore  * @from: sending process
8361427ddbeSPaul Moore  * @to: receiving process
8371427ddbeSPaul Moore  * @file: file being transferred
8381427ddbeSPaul Moore  *
8391427ddbeSPaul Moore  * Check whether @from is allowed to transfer @file to @to.
8401427ddbeSPaul Moore  *
8411427ddbeSPaul Moore  * Return: Returns 0 if permission is granted.
8421427ddbeSPaul Moore  */
security_binder_transfer_file(const struct cred * from,const struct cred * to,const struct file * file)84352f88693STodd Kjos int security_binder_transfer_file(const struct cred *from,
8448e4672d6SKhadija Kamran 				  const struct cred *to, const struct file *file)
84579af7307SStephen Smalley {
846f25fce3eSCasey Schaufler 	return call_int_hook(binder_transfer_file, 0, from, to, file);
84779af7307SStephen Smalley }
84879af7307SStephen Smalley 
849e261301cSPaul Moore /**
850e261301cSPaul Moore  * security_ptrace_access_check() - Check if tracing is allowed
851e261301cSPaul Moore  * @child: target process
852e261301cSPaul Moore  * @mode: PTRACE_MODE flags
853e261301cSPaul Moore  *
854e261301cSPaul Moore  * Check permission before allowing the current process to trace the @child
855e261301cSPaul Moore  * process.  Security modules may also want to perform a process tracing check
856e261301cSPaul Moore  * during an execve in the set_security or apply_creds hooks of tracing check
857e261301cSPaul Moore  * during an execve in the bprm_set_creds hook of binprm_security_ops if the
858e261301cSPaul Moore  * process is being traced and its security attributes would be changed by the
859e261301cSPaul Moore  * execve.
860e261301cSPaul Moore  *
861e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
862e261301cSPaul Moore  */
security_ptrace_access_check(struct task_struct * child,unsigned int mode)8639e48858fSIngo Molnar int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
86420510f2fSJames Morris {
865f25fce3eSCasey Schaufler 	return call_int_hook(ptrace_access_check, 0, child, mode);
8665cd9c58fSDavid Howells }
8675cd9c58fSDavid Howells 
868e261301cSPaul Moore /**
869e261301cSPaul Moore  * security_ptrace_traceme() - Check if tracing is allowed
870e261301cSPaul Moore  * @parent: tracing process
871e261301cSPaul Moore  *
872e261301cSPaul Moore  * Check that the @parent process has sufficient permission to trace the
873e261301cSPaul Moore  * current process before allowing the current process to present itself to the
874e261301cSPaul Moore  * @parent process for tracing.
875e261301cSPaul Moore  *
876e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
877e261301cSPaul Moore  */
security_ptrace_traceme(struct task_struct * parent)8785cd9c58fSDavid Howells int security_ptrace_traceme(struct task_struct *parent)
8795cd9c58fSDavid Howells {
880f25fce3eSCasey Schaufler 	return call_int_hook(ptrace_traceme, 0, parent);
88120510f2fSJames Morris }
88220510f2fSJames Morris 
883e261301cSPaul Moore /**
884e261301cSPaul Moore  * security_capget() - Get the capability sets for a process
885e261301cSPaul Moore  * @target: target process
886e261301cSPaul Moore  * @effective: effective capability set
887e261301cSPaul Moore  * @inheritable: inheritable capability set
888e261301cSPaul Moore  * @permitted: permitted capability set
889e261301cSPaul Moore  *
890e261301cSPaul Moore  * Get the @effective, @inheritable, and @permitted capability sets for the
891e261301cSPaul Moore  * @target process.  The hook may also perform permission checking to determine
892e261301cSPaul Moore  * if the current process is allowed to see the capability sets of the @target
893e261301cSPaul Moore  * process.
894e261301cSPaul Moore  *
895e261301cSPaul Moore  * Return: Returns 0 if the capability sets were successfully obtained.
896e261301cSPaul Moore  */
security_capget(const struct task_struct * target,kernel_cap_t * effective,kernel_cap_t * inheritable,kernel_cap_t * permitted)8976672efbbSKhadija Kamran int security_capget(const struct task_struct *target,
89820510f2fSJames Morris 		    kernel_cap_t *effective,
89920510f2fSJames Morris 		    kernel_cap_t *inheritable,
90020510f2fSJames Morris 		    kernel_cap_t *permitted)
90120510f2fSJames Morris {
902f25fce3eSCasey Schaufler 	return call_int_hook(capget, 0, target,
903f25fce3eSCasey Schaufler 			     effective, inheritable, permitted);
90420510f2fSJames Morris }
90520510f2fSJames Morris 
906e261301cSPaul Moore /**
907e261301cSPaul Moore  * security_capset() - Set the capability sets for a process
908e261301cSPaul Moore  * @new: new credentials for the target process
909e261301cSPaul Moore  * @old: current credentials of the target process
910e261301cSPaul Moore  * @effective: effective capability set
911e261301cSPaul Moore  * @inheritable: inheritable capability set
912e261301cSPaul Moore  * @permitted: permitted capability set
913e261301cSPaul Moore  *
914e261301cSPaul Moore  * Set the @effective, @inheritable, and @permitted capability sets for the
915e261301cSPaul Moore  * current process.
916e261301cSPaul Moore  *
917e261301cSPaul Moore  * Return: Returns 0 and update @new if permission is granted.
918e261301cSPaul Moore  */
security_capset(struct cred * new,const struct cred * old,const kernel_cap_t * effective,const kernel_cap_t * inheritable,const kernel_cap_t * permitted)919d84f4f99SDavid Howells int security_capset(struct cred *new, const struct cred *old,
920d84f4f99SDavid Howells 		    const kernel_cap_t *effective,
92115a2460eSDavid Howells 		    const kernel_cap_t *inheritable,
92215a2460eSDavid Howells 		    const kernel_cap_t *permitted)
92320510f2fSJames Morris {
924f25fce3eSCasey Schaufler 	return call_int_hook(capset, 0, new, old,
925d84f4f99SDavid Howells 			     effective, inheritable, permitted);
92620510f2fSJames Morris }
92720510f2fSJames Morris 
928e261301cSPaul Moore /**
929e261301cSPaul Moore  * security_capable() - Check if a process has the necessary capability
930e261301cSPaul Moore  * @cred: credentials to examine
931e261301cSPaul Moore  * @ns: user namespace
932e261301cSPaul Moore  * @cap: capability requested
933e261301cSPaul Moore  * @opts: capability check options
934e261301cSPaul Moore  *
935e261301cSPaul Moore  * Check whether the @tsk process has the @cap capability in the indicated
936e261301cSPaul Moore  * credentials.  @cap contains the capability <include/linux/capability.h>.
937e261301cSPaul Moore  * @opts contains options for the capable check <include/linux/security.h>.
938e261301cSPaul Moore  *
939e261301cSPaul Moore  * Return: Returns 0 if the capability is granted.
940e261301cSPaul Moore  */
security_capable(const struct cred * cred,struct user_namespace * ns,int cap,unsigned int opts)941c1a85a00SMicah Morton int security_capable(const struct cred *cred,
942c1a85a00SMicah Morton 		     struct user_namespace *ns,
943c1a85a00SMicah Morton 		     int cap,
944c1a85a00SMicah Morton 		     unsigned int opts)
94520510f2fSJames Morris {
946c1a85a00SMicah Morton 	return call_int_hook(capable, 0, cred, ns, cap, opts);
94720510f2fSJames Morris }
94820510f2fSJames Morris 
949e261301cSPaul Moore /**
950e261301cSPaul Moore  * security_quotactl() - Check if a quotactl() syscall is allowed for this fs
951e261301cSPaul Moore  * @cmds: commands
952e261301cSPaul Moore  * @type: type
953e261301cSPaul Moore  * @id: id
954e261301cSPaul Moore  * @sb: filesystem
955e261301cSPaul Moore  *
956e261301cSPaul Moore  * Check whether the quotactl syscall is allowed for this @sb.
957e261301cSPaul Moore  *
958e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
959e261301cSPaul Moore  */
security_quotactl(int cmds,int type,int id,struct super_block * sb)96020510f2fSJames Morris int security_quotactl(int cmds, int type, int id, struct super_block *sb)
96120510f2fSJames Morris {
962f25fce3eSCasey Schaufler 	return call_int_hook(quotactl, 0, cmds, type, id, sb);
96320510f2fSJames Morris }
96420510f2fSJames Morris 
965e261301cSPaul Moore /**
966e261301cSPaul Moore  * security_quota_on() - Check if QUOTAON is allowed for a dentry
967e261301cSPaul Moore  * @dentry: dentry
968e261301cSPaul Moore  *
969e261301cSPaul Moore  * Check whether QUOTAON is allowed for @dentry.
970e261301cSPaul Moore  *
971e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
972e261301cSPaul Moore  */
security_quota_on(struct dentry * dentry)97320510f2fSJames Morris int security_quota_on(struct dentry *dentry)
97420510f2fSJames Morris {
975f25fce3eSCasey Schaufler 	return call_int_hook(quota_on, 0, dentry);
97620510f2fSJames Morris }
97720510f2fSJames Morris 
978e261301cSPaul Moore /**
979e261301cSPaul Moore  * security_syslog() - Check if accessing the kernel message ring is allowed
980e261301cSPaul Moore  * @type: SYSLOG_ACTION_* type
981e261301cSPaul Moore  *
982e261301cSPaul Moore  * Check permission before accessing the kernel message ring or changing
983e261301cSPaul Moore  * logging to the console.  See the syslog(2) manual page for an explanation of
984e261301cSPaul Moore  * the @type values.
985e261301cSPaul Moore  *
986e261301cSPaul Moore  * Return: Return 0 if permission is granted.
987e261301cSPaul Moore  */
security_syslog(int type)98812b3052cSEric Paris int security_syslog(int type)
98920510f2fSJames Morris {
990f25fce3eSCasey Schaufler 	return call_int_hook(syslog, 0, type);
99120510f2fSJames Morris }
99220510f2fSJames Morris 
993e261301cSPaul Moore /**
994e261301cSPaul Moore  * security_settime64() - Check if changing the system time is allowed
995e261301cSPaul Moore  * @ts: new time
996e261301cSPaul Moore  * @tz: timezone
997e261301cSPaul Moore  *
998e261301cSPaul Moore  * Check permission to change the system time, struct timespec64 is defined in
999e261301cSPaul Moore  * <include/linux/time64.h> and timezone is defined in <include/linux/time.h>.
1000e261301cSPaul Moore  *
1001e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
1002e261301cSPaul Moore  */
security_settime64(const struct timespec64 * ts,const struct timezone * tz)1003457db29bSBaolin Wang int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
100420510f2fSJames Morris {
1005f25fce3eSCasey Schaufler 	return call_int_hook(settime, 0, ts, tz);
100620510f2fSJames Morris }
100720510f2fSJames Morris 
1008e261301cSPaul Moore /**
1009e261301cSPaul Moore  * security_vm_enough_memory_mm() - Check if allocating a new mem map is allowed
1010e261301cSPaul Moore  * @mm: mm struct
1011e261301cSPaul Moore  * @pages: number of pages
1012e261301cSPaul Moore  *
1013e261301cSPaul Moore  * Check permissions for allocating a new virtual mapping.  If all LSMs return
1014e261301cSPaul Moore  * a positive value, __vm_enough_memory() will be called with cap_sys_admin
1015e261301cSPaul Moore  * set. If at least one LSM returns 0 or negative, __vm_enough_memory() will be
1016e261301cSPaul Moore  * called with cap_sys_admin cleared.
1017e261301cSPaul Moore  *
1018e261301cSPaul Moore  * Return: Returns 0 if permission is granted by the LSM infrastructure to the
1019e261301cSPaul Moore  *         caller.
1020e261301cSPaul Moore  */
security_vm_enough_memory_mm(struct mm_struct * mm,long pages)102120510f2fSJames Morris int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
102220510f2fSJames Morris {
1023b1d9e6b0SCasey Schaufler 	struct security_hook_list *hp;
1024b1d9e6b0SCasey Schaufler 	int cap_sys_admin = 1;
1025b1d9e6b0SCasey Schaufler 	int rc;
1026b1d9e6b0SCasey Schaufler 
1027b1d9e6b0SCasey Schaufler 	/*
1028b1d9e6b0SCasey Schaufler 	 * The module will respond with a positive value if
1029b1d9e6b0SCasey Schaufler 	 * it thinks the __vm_enough_memory() call should be
1030b1d9e6b0SCasey Schaufler 	 * made with the cap_sys_admin set. If all of the modules
1031b1d9e6b0SCasey Schaufler 	 * agree that it should be set it will. If any module
1032b1d9e6b0SCasey Schaufler 	 * thinks it should not be set it won't.
1033b1d9e6b0SCasey Schaufler 	 */
1034df0ce173SSargun Dhillon 	hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
1035b1d9e6b0SCasey Schaufler 		rc = hp->hook.vm_enough_memory(mm, pages);
1036b1d9e6b0SCasey Schaufler 		if (rc <= 0) {
1037b1d9e6b0SCasey Schaufler 			cap_sys_admin = 0;
1038b1d9e6b0SCasey Schaufler 			break;
1039b1d9e6b0SCasey Schaufler 		}
1040b1d9e6b0SCasey Schaufler 	}
1041b1d9e6b0SCasey Schaufler 	return __vm_enough_memory(mm, pages, cap_sys_admin);
104220510f2fSJames Morris }
104320510f2fSJames Morris 
10441661372cSPaul Moore /**
10451661372cSPaul Moore  * security_bprm_creds_for_exec() - Prepare the credentials for exec()
10461661372cSPaul Moore  * @bprm: binary program information
10471661372cSPaul Moore  *
10481661372cSPaul Moore  * If the setup in prepare_exec_creds did not setup @bprm->cred->security
10491661372cSPaul Moore  * properly for executing @bprm->file, update the LSM's portion of
10501661372cSPaul Moore  * @bprm->cred->security to be what commit_creds needs to install for the new
10511661372cSPaul Moore  * program.  This hook may also optionally check permissions (e.g. for
10521661372cSPaul Moore  * transitions between security domains).  The hook must set @bprm->secureexec
10531661372cSPaul Moore  * to 1 if AT_SECURE should be set to request libc enable secure mode.  @bprm
10541661372cSPaul Moore  * contains the linux_binprm structure.
10551661372cSPaul Moore  *
10561661372cSPaul Moore  * Return: Returns 0 if the hook is successful and permission is granted.
10571661372cSPaul Moore  */
security_bprm_creds_for_exec(struct linux_binprm * bprm)1058b8bff599SEric W. Biederman int security_bprm_creds_for_exec(struct linux_binprm *bprm)
105920510f2fSJames Morris {
1060b8bff599SEric W. Biederman 	return call_int_hook(bprm_creds_for_exec, 0, bprm);
1061b8bff599SEric W. Biederman }
1062b8bff599SEric W. Biederman 
10631661372cSPaul Moore /**
10641661372cSPaul Moore  * security_bprm_creds_from_file() - Update linux_binprm creds based on file
10651661372cSPaul Moore  * @bprm: binary program information
10661661372cSPaul Moore  * @file: associated file
10671661372cSPaul Moore  *
10681661372cSPaul Moore  * If @file is setpcap, suid, sgid or otherwise marked to change privilege upon
10691661372cSPaul Moore  * exec, update @bprm->cred to reflect that change. This is called after
10701661372cSPaul Moore  * finding the binary that will be executed without an interpreter.  This
10711661372cSPaul Moore  * ensures that the credentials will not be derived from a script that the
10721661372cSPaul Moore  * binary will need to reopen, which when reopend may end up being a completely
10731661372cSPaul Moore  * different file.  This hook may also optionally check permissions (e.g. for
10741661372cSPaul Moore  * transitions between security domains).  The hook must set @bprm->secureexec
10751661372cSPaul Moore  * to 1 if AT_SECURE should be set to request libc enable secure mode.  The
10761661372cSPaul Moore  * hook must add to @bprm->per_clear any personality flags that should be
10771661372cSPaul Moore  * cleared from current->personality.  @bprm contains the linux_binprm
10781661372cSPaul Moore  * structure.
10791661372cSPaul Moore  *
10801661372cSPaul Moore  * Return: Returns 0 if the hook is successful and permission is granted.
10811661372cSPaul Moore  */
security_bprm_creds_from_file(struct linux_binprm * bprm,struct file * file)108256305aa9SEric W. Biederman int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
108320510f2fSJames Morris {
108456305aa9SEric W. Biederman 	return call_int_hook(bprm_creds_from_file, 0, bprm, file);
108520510f2fSJames Morris }
108620510f2fSJames Morris 
10871661372cSPaul Moore /**
10881661372cSPaul Moore  * security_bprm_check() - Mediate binary handler search
10891661372cSPaul Moore  * @bprm: binary program information
10901661372cSPaul Moore  *
10911661372cSPaul Moore  * This hook mediates the point when a search for a binary handler will begin.
10921661372cSPaul Moore  * It allows a check against the @bprm->cred->security value which was set in
10931661372cSPaul Moore  * the preceding creds_for_exec call.  The argv list and envp list are reliably
10941661372cSPaul Moore  * available in @bprm.  This hook may be called multiple times during a single
10951661372cSPaul Moore  * execve.  @bprm contains the linux_binprm structure.
10961661372cSPaul Moore  *
10971661372cSPaul Moore  * Return: Returns 0 if the hook is successful and permission is granted.
10981661372cSPaul Moore  */
security_bprm_check(struct linux_binprm * bprm)109920510f2fSJames Morris int security_bprm_check(struct linux_binprm *bprm)
110020510f2fSJames Morris {
11016c21a7fbSMimi Zohar 	int ret;
11026c21a7fbSMimi Zohar 
1103f25fce3eSCasey Schaufler 	ret = call_int_hook(bprm_check_security, 0, bprm);
11046c21a7fbSMimi Zohar 	if (ret)
11056c21a7fbSMimi Zohar 		return ret;
11066c21a7fbSMimi Zohar 	return ima_bprm_check(bprm);
110720510f2fSJames Morris }
110820510f2fSJames Morris 
11091661372cSPaul Moore /**
11101661372cSPaul Moore  * security_bprm_committing_creds() - Install creds for a process during exec()
11111661372cSPaul Moore  * @bprm: binary program information
11121661372cSPaul Moore  *
11131661372cSPaul Moore  * Prepare to install the new security attributes of a process being
11141661372cSPaul Moore  * transformed by an execve operation, based on the old credentials pointed to
11151661372cSPaul Moore  * by @current->cred and the information set in @bprm->cred by the
11161661372cSPaul Moore  * bprm_creds_for_exec hook.  @bprm points to the linux_binprm structure.  This
11171661372cSPaul Moore  * hook is a good place to perform state changes on the process such as closing
11181661372cSPaul Moore  * open file descriptors to which access will no longer be granted when the
11191661372cSPaul Moore  * attributes are changed.  This is called immediately before commit_creds().
11201661372cSPaul Moore  */
security_bprm_committing_creds(struct linux_binprm * bprm)1121a6f76f23SDavid Howells void security_bprm_committing_creds(struct linux_binprm *bprm)
1122a6f76f23SDavid Howells {
1123f25fce3eSCasey Schaufler 	call_void_hook(bprm_committing_creds, bprm);
1124a6f76f23SDavid Howells }
1125a6f76f23SDavid Howells 
11261661372cSPaul Moore /**
11271661372cSPaul Moore  * security_bprm_committed_creds() - Tidy up after cred install during exec()
11281661372cSPaul Moore  * @bprm: binary program information
11291661372cSPaul Moore  *
11301661372cSPaul Moore  * Tidy up after the installation of the new security attributes of a process
11311661372cSPaul Moore  * being transformed by an execve operation.  The new credentials have, by this
11321661372cSPaul Moore  * point, been set to @current->cred.  @bprm points to the linux_binprm
11331661372cSPaul Moore  * structure.  This hook is a good place to perform state changes on the
11341661372cSPaul Moore  * process such as clearing out non-inheritable signal state.  This is called
11351661372cSPaul Moore  * immediately after commit_creds().
11361661372cSPaul Moore  */
security_bprm_committed_creds(struct linux_binprm * bprm)1137a6f76f23SDavid Howells void security_bprm_committed_creds(struct linux_binprm *bprm)
1138a6f76f23SDavid Howells {
1139f25fce3eSCasey Schaufler 	call_void_hook(bprm_committed_creds, bprm);
1140a6f76f23SDavid Howells }
1141a6f76f23SDavid Howells 
114236819f18SPaul Moore /**
1143d80a8f1bSDavid Howells  * security_fs_context_submount() - Initialise fc->security
1144d80a8f1bSDavid Howells  * @fc: new filesystem context
1145d80a8f1bSDavid Howells  * @reference: dentry reference for submount/remount
1146d80a8f1bSDavid Howells  *
1147d80a8f1bSDavid Howells  * Fill out the ->security field for a new fs_context.
1148d80a8f1bSDavid Howells  *
1149d80a8f1bSDavid Howells  * Return: Returns 0 on success or negative error code on failure.
1150d80a8f1bSDavid Howells  */
security_fs_context_submount(struct fs_context * fc,struct super_block * reference)1151d80a8f1bSDavid Howells int security_fs_context_submount(struct fs_context *fc, struct super_block *reference)
1152d80a8f1bSDavid Howells {
1153d80a8f1bSDavid Howells 	return call_int_hook(fs_context_submount, 0, fc, reference);
1154d80a8f1bSDavid Howells }
1155d80a8f1bSDavid Howells 
1156d80a8f1bSDavid Howells /**
115736819f18SPaul Moore  * security_fs_context_dup() - Duplicate a fs_context LSM blob
115836819f18SPaul Moore  * @fc: destination filesystem context
115936819f18SPaul Moore  * @src_fc: source filesystem context
116036819f18SPaul Moore  *
116136819f18SPaul Moore  * Allocate and attach a security structure to sc->security.  This pointer is
116236819f18SPaul Moore  * initialised to NULL by the caller.  @fc indicates the new filesystem context.
116336819f18SPaul Moore  * @src_fc indicates the original filesystem context.
116436819f18SPaul Moore  *
116536819f18SPaul Moore  * Return: Returns 0 on success or a negative error code on failure.
116636819f18SPaul Moore  */
security_fs_context_dup(struct fs_context * fc,struct fs_context * src_fc)11670b52075eSAl Viro int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
11680b52075eSAl Viro {
11690b52075eSAl Viro 	return call_int_hook(fs_context_dup, 0, fc, src_fc);
11700b52075eSAl Viro }
11710b52075eSAl Viro 
117236819f18SPaul Moore /**
117336819f18SPaul Moore  * security_fs_context_parse_param() - Configure a filesystem context
117436819f18SPaul Moore  * @fc: filesystem context
117536819f18SPaul Moore  * @param: filesystem parameter
117636819f18SPaul Moore  *
117736819f18SPaul Moore  * Userspace provided a parameter to configure a superblock.  The LSM can
117836819f18SPaul Moore  * consume the parameter or return it to the caller for use elsewhere.
117936819f18SPaul Moore  *
118036819f18SPaul Moore  * Return: If the parameter is used by the LSM it should return 0, if it is
118136819f18SPaul Moore  *         returned to the caller -ENOPARAM is returned, otherwise a negative
118236819f18SPaul Moore  *         error code is returned.
118336819f18SPaul Moore  */
security_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)1184ecff3057SCasey Schaufler int security_fs_context_parse_param(struct fs_context *fc,
1185ecff3057SCasey Schaufler 				    struct fs_parameter *param)
1186da2441fdSDavid Howells {
1187ecff3057SCasey Schaufler 	struct security_hook_list *hp;
1188ecff3057SCasey Schaufler 	int trc;
1189ecff3057SCasey Schaufler 	int rc = -ENOPARAM;
1190ecff3057SCasey Schaufler 
1191ecff3057SCasey Schaufler 	hlist_for_each_entry(hp, &security_hook_heads.fs_context_parse_param,
1192ecff3057SCasey Schaufler 			     list) {
1193ecff3057SCasey Schaufler 		trc = hp->hook.fs_context_parse_param(fc, param);
1194ecff3057SCasey Schaufler 		if (trc == 0)
1195ecff3057SCasey Schaufler 			rc = 0;
1196ecff3057SCasey Schaufler 		else if (trc != -ENOPARAM)
1197ecff3057SCasey Schaufler 			return trc;
1198ecff3057SCasey Schaufler 	}
1199ecff3057SCasey Schaufler 	return rc;
1200da2441fdSDavid Howells }
1201da2441fdSDavid Howells 
120208526a90SPaul Moore /**
120308526a90SPaul Moore  * security_sb_alloc() - Allocate a super_block LSM blob
120408526a90SPaul Moore  * @sb: filesystem superblock
120508526a90SPaul Moore  *
120608526a90SPaul Moore  * Allocate and attach a security structure to the sb->s_security field.  The
120708526a90SPaul Moore  * s_security field is initialized to NULL when the structure is allocated.
120808526a90SPaul Moore  * @sb contains the super_block structure to be modified.
120908526a90SPaul Moore  *
121008526a90SPaul Moore  * Return: Returns 0 if operation was successful.
121108526a90SPaul Moore  */
security_sb_alloc(struct super_block * sb)121220510f2fSJames Morris int security_sb_alloc(struct super_block *sb)
121320510f2fSJames Morris {
12141aea7808SCasey Schaufler 	int rc = lsm_superblock_alloc(sb);
12151aea7808SCasey Schaufler 
12161aea7808SCasey Schaufler 	if (unlikely(rc))
12171aea7808SCasey Schaufler 		return rc;
12181aea7808SCasey Schaufler 	rc = call_int_hook(sb_alloc_security, 0, sb);
12191aea7808SCasey Schaufler 	if (unlikely(rc))
12201aea7808SCasey Schaufler 		security_sb_free(sb);
12211aea7808SCasey Schaufler 	return rc;
122220510f2fSJames Morris }
122320510f2fSJames Morris 
122408526a90SPaul Moore /**
122508526a90SPaul Moore  * security_sb_delete() - Release super_block LSM associated objects
122608526a90SPaul Moore  * @sb: filesystem superblock
122708526a90SPaul Moore  *
122808526a90SPaul Moore  * Release objects tied to a superblock (e.g. inodes).  @sb contains the
122908526a90SPaul Moore  * super_block structure being released.
123008526a90SPaul Moore  */
security_sb_delete(struct super_block * sb)123183e804f0SMickaël Salaün void security_sb_delete(struct super_block *sb)
123283e804f0SMickaël Salaün {
123383e804f0SMickaël Salaün 	call_void_hook(sb_delete, sb);
123420510f2fSJames Morris }
123520510f2fSJames Morris 
123608526a90SPaul Moore /**
123708526a90SPaul Moore  * security_sb_free() - Free a super_block LSM blob
123808526a90SPaul Moore  * @sb: filesystem superblock
123908526a90SPaul Moore  *
124008526a90SPaul Moore  * Deallocate and clear the sb->s_security field.  @sb contains the super_block
124108526a90SPaul Moore  * structure to be modified.
124208526a90SPaul Moore  */
security_sb_free(struct super_block * sb)124320510f2fSJames Morris void security_sb_free(struct super_block *sb)
124420510f2fSJames Morris {
1245f25fce3eSCasey Schaufler 	call_void_hook(sb_free_security, sb);
12461aea7808SCasey Schaufler 	kfree(sb->s_security);
12471aea7808SCasey Schaufler 	sb->s_security = NULL;
124820510f2fSJames Morris }
124920510f2fSJames Morris 
125008526a90SPaul Moore /**
125108526a90SPaul Moore  * security_free_mnt_opts() - Free memory associated with mount options
12521e2523d7SPaul Moore  * @mnt_opts: LSM processed mount options
125308526a90SPaul Moore  *
125408526a90SPaul Moore  * Free memory associated with @mnt_ops.
125508526a90SPaul Moore  */
security_free_mnt_opts(void ** mnt_opts)1256204cc0ccSAl Viro void security_free_mnt_opts(void **mnt_opts)
125720510f2fSJames Morris {
1258204cc0ccSAl Viro 	if (!*mnt_opts)
1259204cc0ccSAl Viro 		return;
1260204cc0ccSAl Viro 	call_void_hook(sb_free_mnt_opts, *mnt_opts);
1261204cc0ccSAl Viro 	*mnt_opts = NULL;
126220510f2fSJames Morris }
1263204cc0ccSAl Viro EXPORT_SYMBOL(security_free_mnt_opts);
126420510f2fSJames Morris 
126508526a90SPaul Moore /**
126608526a90SPaul Moore  * security_sb_eat_lsm_opts() - Consume LSM mount options
126708526a90SPaul Moore  * @options: mount options
12681e2523d7SPaul Moore  * @mnt_opts: LSM processed mount options
126908526a90SPaul Moore  *
127008526a90SPaul Moore  * Eat (scan @options) and save them in @mnt_opts.
127108526a90SPaul Moore  *
127208526a90SPaul Moore  * Return: Returns 0 on success, negative values on failure.
127308526a90SPaul Moore  */
security_sb_eat_lsm_opts(char * options,void ** mnt_opts)1274204cc0ccSAl Viro int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
1275ff36fe2cSEric Paris {
1276204cc0ccSAl Viro 	return call_int_hook(sb_eat_lsm_opts, 0, options, mnt_opts);
1277ff36fe2cSEric Paris }
1278f5c0c26dSAl Viro EXPORT_SYMBOL(security_sb_eat_lsm_opts);
1279ff36fe2cSEric Paris 
128008526a90SPaul Moore /**
128108526a90SPaul Moore  * security_sb_mnt_opts_compat() - Check if new mount options are allowed
128208526a90SPaul Moore  * @sb: filesystem superblock
128308526a90SPaul Moore  * @mnt_opts: new mount options
128408526a90SPaul Moore  *
128508526a90SPaul Moore  * Determine if the new mount options in @mnt_opts are allowed given the
128608526a90SPaul Moore  * existing mounted filesystem at @sb.  @sb superblock being compared.
128708526a90SPaul Moore  *
128808526a90SPaul Moore  * Return: Returns 0 if options are compatible.
128908526a90SPaul Moore  */
security_sb_mnt_opts_compat(struct super_block * sb,void * mnt_opts)129069c4a42dSOlga Kornievskaia int security_sb_mnt_opts_compat(struct super_block *sb,
129169c4a42dSOlga Kornievskaia 				void *mnt_opts)
129269c4a42dSOlga Kornievskaia {
129369c4a42dSOlga Kornievskaia 	return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
129469c4a42dSOlga Kornievskaia }
129569c4a42dSOlga Kornievskaia EXPORT_SYMBOL(security_sb_mnt_opts_compat);
129669c4a42dSOlga Kornievskaia 
129708526a90SPaul Moore /**
129808526a90SPaul Moore  * security_sb_remount() - Verify no incompatible mount changes during remount
129908526a90SPaul Moore  * @sb: filesystem superblock
130008526a90SPaul Moore  * @mnt_opts: (re)mount options
130108526a90SPaul Moore  *
130208526a90SPaul Moore  * Extracts security system specific mount options and verifies no changes are
130308526a90SPaul Moore  * being made to those options.
130408526a90SPaul Moore  *
130508526a90SPaul Moore  * Return: Returns 0 if permission is granted.
130608526a90SPaul Moore  */
security_sb_remount(struct super_block * sb,void * mnt_opts)1307c039bc3cSAl Viro int security_sb_remount(struct super_block *sb,
1308204cc0ccSAl Viro 			void *mnt_opts)
130920510f2fSJames Morris {
1310204cc0ccSAl Viro 	return call_int_hook(sb_remount, 0, sb, mnt_opts);
131120510f2fSJames Morris }
1312a65001e8SAl Viro EXPORT_SYMBOL(security_sb_remount);
131320510f2fSJames Morris 
131408526a90SPaul Moore /**
131508526a90SPaul Moore  * security_sb_kern_mount() - Check if a kernel mount is allowed
131608526a90SPaul Moore  * @sb: filesystem superblock
131708526a90SPaul Moore  *
131808526a90SPaul Moore  * Mount this @sb if allowed by permissions.
131908526a90SPaul Moore  *
132008526a90SPaul Moore  * Return: Returns 0 if permission is granted.
132108526a90SPaul Moore  */
security_sb_kern_mount(struct super_block * sb)1322a10d7c22SAl Viro int security_sb_kern_mount(struct super_block *sb)
132320510f2fSJames Morris {
1324a10d7c22SAl Viro 	return call_int_hook(sb_kern_mount, 0, sb);
132520510f2fSJames Morris }
132620510f2fSJames Morris 
132708526a90SPaul Moore /**
132808526a90SPaul Moore  * security_sb_show_options() - Output the mount options for a superblock
132908526a90SPaul Moore  * @m: output file
133008526a90SPaul Moore  * @sb: filesystem superblock
133108526a90SPaul Moore  *
133208526a90SPaul Moore  * Show (print on @m) mount options for this @sb.
133308526a90SPaul Moore  *
133408526a90SPaul Moore  * Return: Returns 0 on success, negative values on failure.
133508526a90SPaul Moore  */
security_sb_show_options(struct seq_file * m,struct super_block * sb)13362069f457SEric Paris int security_sb_show_options(struct seq_file *m, struct super_block *sb)
13372069f457SEric Paris {
1338f25fce3eSCasey Schaufler 	return call_int_hook(sb_show_options, 0, m, sb);
13392069f457SEric Paris }
13402069f457SEric Paris 
134108526a90SPaul Moore /**
134208526a90SPaul Moore  * security_sb_statfs() - Check if accessing fs stats is allowed
134308526a90SPaul Moore  * @dentry: superblock handle
134408526a90SPaul Moore  *
134508526a90SPaul Moore  * Check permission before obtaining filesystem statistics for the @mnt
134608526a90SPaul Moore  * mountpoint.  @dentry is a handle on the superblock for the filesystem.
134708526a90SPaul Moore  *
134808526a90SPaul Moore  * Return: Returns 0 if permission is granted.
134908526a90SPaul Moore  */
security_sb_statfs(struct dentry * dentry)135020510f2fSJames Morris int security_sb_statfs(struct dentry *dentry)
135120510f2fSJames Morris {
1352f25fce3eSCasey Schaufler 	return call_int_hook(sb_statfs, 0, dentry);
135320510f2fSJames Morris }
135420510f2fSJames Morris 
135508526a90SPaul Moore /**
135608526a90SPaul Moore  * security_sb_mount() - Check permission for mounting a filesystem
135708526a90SPaul Moore  * @dev_name: filesystem backing device
135808526a90SPaul Moore  * @path: mount point
135908526a90SPaul Moore  * @type: filesystem type
136008526a90SPaul Moore  * @flags: mount flags
136108526a90SPaul Moore  * @data: filesystem specific data
136208526a90SPaul Moore  *
136308526a90SPaul Moore  * Check permission before an object specified by @dev_name is mounted on the
136408526a90SPaul Moore  * mount point named by @nd.  For an ordinary mount, @dev_name identifies a
136508526a90SPaul Moore  * device if the file system type requires a device.  For a remount
136608526a90SPaul Moore  * (@flags & MS_REMOUNT), @dev_name is irrelevant.  For a loopback/bind mount
136708526a90SPaul Moore  * (@flags & MS_BIND), @dev_name identifies the	pathname of the object being
136808526a90SPaul Moore  * mounted.
136908526a90SPaul Moore  *
137008526a90SPaul Moore  * Return: Returns 0 if permission is granted.
137108526a90SPaul Moore  */
security_sb_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)13728a04c43bSAl Viro int security_sb_mount(const char *dev_name, const struct path *path,
1373808d4e3cSAl Viro 		      const char *type, unsigned long flags, void *data)
137420510f2fSJames Morris {
1375f25fce3eSCasey Schaufler 	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
137620510f2fSJames Morris }
137720510f2fSJames Morris 
137808526a90SPaul Moore /**
137908526a90SPaul Moore  * security_sb_umount() - Check permission for unmounting a filesystem
138008526a90SPaul Moore  * @mnt: mounted filesystem
138108526a90SPaul Moore  * @flags: unmount flags
138208526a90SPaul Moore  *
138308526a90SPaul Moore  * Check permission before the @mnt file system is unmounted.
138408526a90SPaul Moore  *
138508526a90SPaul Moore  * Return: Returns 0 if permission is granted.
138608526a90SPaul Moore  */
security_sb_umount(struct vfsmount * mnt,int flags)138720510f2fSJames Morris int security_sb_umount(struct vfsmount *mnt, int flags)
138820510f2fSJames Morris {
1389f25fce3eSCasey Schaufler 	return call_int_hook(sb_umount, 0, mnt, flags);
139020510f2fSJames Morris }
139120510f2fSJames Morris 
139208526a90SPaul Moore /**
139308526a90SPaul Moore  * security_sb_pivotroot() - Check permissions for pivoting the rootfs
139408526a90SPaul Moore  * @old_path: new location for current rootfs
139508526a90SPaul Moore  * @new_path: location of the new rootfs
139608526a90SPaul Moore  *
139708526a90SPaul Moore  * Check permission before pivoting the root filesystem.
139808526a90SPaul Moore  *
139908526a90SPaul Moore  * Return: Returns 0 if permission is granted.
140008526a90SPaul Moore  */
security_sb_pivotroot(const struct path * old_path,const struct path * new_path)140163c1845bSPaul Moore int security_sb_pivotroot(const struct path *old_path,
140263c1845bSPaul Moore 			  const struct path *new_path)
140320510f2fSJames Morris {
1404f25fce3eSCasey Schaufler 	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
140520510f2fSJames Morris }
140620510f2fSJames Morris 
140708526a90SPaul Moore /**
140808526a90SPaul Moore  * security_sb_set_mnt_opts() - Set the mount options for a filesystem
140908526a90SPaul Moore  * @sb: filesystem superblock
141008526a90SPaul Moore  * @mnt_opts: binary mount options
141108526a90SPaul Moore  * @kern_flags: kernel flags (in)
141208526a90SPaul Moore  * @set_kern_flags: kernel flags (out)
141308526a90SPaul Moore  *
141408526a90SPaul Moore  * Set the security relevant mount options used for a superblock.
141508526a90SPaul Moore  *
141608526a90SPaul Moore  * Return: Returns 0 on success, error on failure.
141708526a90SPaul Moore  */
security_sb_set_mnt_opts(struct super_block * sb,void * mnt_opts,unsigned long kern_flags,unsigned long * set_kern_flags)1418c9180a57SEric Paris int security_sb_set_mnt_opts(struct super_block *sb,
1419204cc0ccSAl Viro 			     void *mnt_opts,
1420649f6e77SDavid Quigley 			     unsigned long kern_flags,
1421649f6e77SDavid Quigley 			     unsigned long *set_kern_flags)
1422c9180a57SEric Paris {
1423b1d9e6b0SCasey Schaufler 	return call_int_hook(sb_set_mnt_opts,
1424204cc0ccSAl Viro 			     mnt_opts ? -EOPNOTSUPP : 0, sb,
1425204cc0ccSAl Viro 			     mnt_opts, kern_flags, set_kern_flags);
1426c9180a57SEric Paris }
1427e0007529SEric Paris EXPORT_SYMBOL(security_sb_set_mnt_opts);
1428c9180a57SEric Paris 
142908526a90SPaul Moore /**
143008526a90SPaul Moore  * security_sb_clone_mnt_opts() - Duplicate superblock mount options
14311e2523d7SPaul Moore  * @oldsb: source superblock
14321e2523d7SPaul Moore  * @newsb: destination superblock
143308526a90SPaul Moore  * @kern_flags: kernel flags (in)
143408526a90SPaul Moore  * @set_kern_flags: kernel flags (out)
143508526a90SPaul Moore  *
143608526a90SPaul Moore  * Copy all security options from a given superblock to another.
143708526a90SPaul Moore  *
143808526a90SPaul Moore  * Return: Returns 0 on success, error on failure.
143908526a90SPaul Moore  */
security_sb_clone_mnt_opts(const struct super_block * oldsb,struct super_block * newsb,unsigned long kern_flags,unsigned long * set_kern_flags)1440094f7b69SJeff Layton int security_sb_clone_mnt_opts(const struct super_block *oldsb,
14410b4d3452SScott Mayhew 			       struct super_block *newsb,
14420b4d3452SScott Mayhew 			       unsigned long kern_flags,
14430b4d3452SScott Mayhew 			       unsigned long *set_kern_flags)
1444c9180a57SEric Paris {
14450b4d3452SScott Mayhew 	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
14460b4d3452SScott Mayhew 			     kern_flags, set_kern_flags);
1447c9180a57SEric Paris }
1448e0007529SEric Paris EXPORT_SYMBOL(security_sb_clone_mnt_opts);
1449e0007529SEric Paris 
145008526a90SPaul Moore /**
145108526a90SPaul Moore  * security_move_mount() - Check permissions for moving a mount
145208526a90SPaul Moore  * @from_path: source mount point
145308526a90SPaul Moore  * @to_path: destination mount point
145408526a90SPaul Moore  *
145508526a90SPaul Moore  * Check permission before a mount is moved.
145608526a90SPaul Moore  *
145708526a90SPaul Moore  * Return: Returns 0 if permission is granted.
145808526a90SPaul Moore  */
security_move_mount(const struct path * from_path,const struct path * to_path)145963c1845bSPaul Moore int security_move_mount(const struct path *from_path,
146063c1845bSPaul Moore 			const struct path *to_path)
14612db154b3SDavid Howells {
14622db154b3SDavid Howells 	return call_int_hook(move_mount, 0, from_path, to_path);
14632db154b3SDavid Howells }
14642db154b3SDavid Howells 
1465916e3258SPaul Moore /**
1466916e3258SPaul Moore  * security_path_notify() - Check if setting a watch is allowed
1467916e3258SPaul Moore  * @path: file path
1468916e3258SPaul Moore  * @mask: event mask
1469916e3258SPaul Moore  * @obj_type: file path type
1470916e3258SPaul Moore  *
1471916e3258SPaul Moore  * Check permissions before setting a watch on events as defined by @mask, on
1472916e3258SPaul Moore  * an object at @path, whose type is defined by @obj_type.
1473916e3258SPaul Moore  *
1474916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1475916e3258SPaul Moore  */
security_path_notify(const struct path * path,u64 mask,unsigned int obj_type)1476ac5656d8SAaron Goidel int security_path_notify(const struct path *path, u64 mask,
1477ac5656d8SAaron Goidel 			 unsigned int obj_type)
1478ac5656d8SAaron Goidel {
1479ac5656d8SAaron Goidel 	return call_int_hook(path_notify, 0, path, mask, obj_type);
1480ac5656d8SAaron Goidel }
1481ac5656d8SAaron Goidel 
1482916e3258SPaul Moore /**
1483916e3258SPaul Moore  * security_inode_alloc() - Allocate an inode LSM blob
1484916e3258SPaul Moore  * @inode: the inode
1485916e3258SPaul Moore  *
1486916e3258SPaul Moore  * Allocate and attach a security structure to @inode->i_security.  The
1487916e3258SPaul Moore  * i_security field is initialized to NULL when the inode structure is
1488916e3258SPaul Moore  * allocated.
1489916e3258SPaul Moore  *
1490916e3258SPaul Moore  * Return: Return 0 if operation was successful.
1491916e3258SPaul Moore  */
security_inode_alloc(struct inode * inode)149220510f2fSJames Morris int security_inode_alloc(struct inode *inode)
149320510f2fSJames Morris {
1494afb1cbe3SCasey Schaufler 	int rc = lsm_inode_alloc(inode);
1495afb1cbe3SCasey Schaufler 
1496afb1cbe3SCasey Schaufler 	if (unlikely(rc))
1497afb1cbe3SCasey Schaufler 		return rc;
1498afb1cbe3SCasey Schaufler 	rc = call_int_hook(inode_alloc_security, 0, inode);
1499afb1cbe3SCasey Schaufler 	if (unlikely(rc))
1500afb1cbe3SCasey Schaufler 		security_inode_free(inode);
1501afb1cbe3SCasey Schaufler 	return rc;
1502afb1cbe3SCasey Schaufler }
1503afb1cbe3SCasey Schaufler 
inode_free_by_rcu(struct rcu_head * head)1504afb1cbe3SCasey Schaufler static void inode_free_by_rcu(struct rcu_head *head)
1505afb1cbe3SCasey Schaufler {
1506afb1cbe3SCasey Schaufler 	/*
1507afb1cbe3SCasey Schaufler 	 * The rcu head is at the start of the inode blob
1508afb1cbe3SCasey Schaufler 	 */
1509afb1cbe3SCasey Schaufler 	kmem_cache_free(lsm_inode_cache, head);
151020510f2fSJames Morris }
151120510f2fSJames Morris 
1512916e3258SPaul Moore /**
1513916e3258SPaul Moore  * security_inode_free() - Free an inode's LSM blob
1514916e3258SPaul Moore  * @inode: the inode
1515916e3258SPaul Moore  *
1516916e3258SPaul Moore  * Deallocate the inode security structure and set @inode->i_security to NULL.
1517916e3258SPaul Moore  */
security_inode_free(struct inode * inode)151820510f2fSJames Morris void security_inode_free(struct inode *inode)
151920510f2fSJames Morris {
1520f381c272SMimi Zohar 	integrity_inode_free(inode);
1521f25fce3eSCasey Schaufler 	call_void_hook(inode_free_security, inode);
1522afb1cbe3SCasey Schaufler 	/*
1523afb1cbe3SCasey Schaufler 	 * The inode may still be referenced in a path walk and
1524afb1cbe3SCasey Schaufler 	 * a call to security_inode_permission() can be made
1525afb1cbe3SCasey Schaufler 	 * after inode_free_security() is called. Ideally, the VFS
1526afb1cbe3SCasey Schaufler 	 * wouldn't do this, but fixing that is a much harder
1527afb1cbe3SCasey Schaufler 	 * job. For now, simply free the i_security via RCU, and
1528afb1cbe3SCasey Schaufler 	 * leave the current inode->i_security pointer intact.
1529afb1cbe3SCasey Schaufler 	 * The inode will be freed after the RCU grace period too.
1530afb1cbe3SCasey Schaufler 	 */
1531afb1cbe3SCasey Schaufler 	if (inode->i_security)
1532afb1cbe3SCasey Schaufler 		call_rcu((struct rcu_head *)inode->i_security,
1533afb1cbe3SCasey Schaufler 			 inode_free_by_rcu);
153420510f2fSJames Morris }
153520510f2fSJames Morris 
153608526a90SPaul Moore /**
153708526a90SPaul Moore  * security_dentry_init_security() - Perform dentry initialization
153808526a90SPaul Moore  * @dentry: the dentry to initialize
153908526a90SPaul Moore  * @mode: mode used to determine resource type
154008526a90SPaul Moore  * @name: name of the last path component
154108526a90SPaul Moore  * @xattr_name: name of the security/LSM xattr
154208526a90SPaul Moore  * @ctx: pointer to the resulting LSM context
154308526a90SPaul Moore  * @ctxlen: length of @ctx
154408526a90SPaul Moore  *
154508526a90SPaul Moore  * Compute a context for a dentry as the inode is not yet available since NFSv4
154608526a90SPaul Moore  * has no label backed by an EA anyway.  It is important to note that
154708526a90SPaul Moore  * @xattr_name does not need to be free'd by the caller, it is a static string.
154808526a90SPaul Moore  *
154908526a90SPaul Moore  * Return: Returns 0 on success, negative values on failure.
155008526a90SPaul Moore  */
security_dentry_init_security(struct dentry * dentry,int mode,const struct qstr * name,const char ** xattr_name,void ** ctx,u32 * ctxlen)1551d47be3dfSDavid Quigley int security_dentry_init_security(struct dentry *dentry, int mode,
155215bf3239SVivek Goyal 				  const struct qstr *name,
155315bf3239SVivek Goyal 				  const char **xattr_name, void **ctx,
1554d47be3dfSDavid Quigley 				  u32 *ctxlen)
1555d47be3dfSDavid Quigley {
15567f5056b9SVivek Goyal 	struct security_hook_list *hp;
15577f5056b9SVivek Goyal 	int rc;
15587f5056b9SVivek Goyal 
15597f5056b9SVivek Goyal 	/*
15607f5056b9SVivek Goyal 	 * Only one module will provide a security context.
15617f5056b9SVivek Goyal 	 */
156263c1845bSPaul Moore 	hlist_for_each_entry(hp, &security_hook_heads.dentry_init_security,
156363c1845bSPaul Moore 			     list) {
15647f5056b9SVivek Goyal 		rc = hp->hook.dentry_init_security(dentry, mode, name,
15657f5056b9SVivek Goyal 						   xattr_name, ctx, ctxlen);
15667f5056b9SVivek Goyal 		if (rc != LSM_RET_DEFAULT(dentry_init_security))
15677f5056b9SVivek Goyal 			return rc;
15687f5056b9SVivek Goyal 	}
15697f5056b9SVivek Goyal 	return LSM_RET_DEFAULT(dentry_init_security);
1570d47be3dfSDavid Quigley }
1571d47be3dfSDavid Quigley EXPORT_SYMBOL(security_dentry_init_security);
1572d47be3dfSDavid Quigley 
157308526a90SPaul Moore /**
157408526a90SPaul Moore  * security_dentry_create_files_as() - Perform dentry initialization
157508526a90SPaul Moore  * @dentry: the dentry to initialize
157608526a90SPaul Moore  * @mode: mode used to determine resource type
157708526a90SPaul Moore  * @name: name of the last path component
157808526a90SPaul Moore  * @old: creds to use for LSM context calculations
157908526a90SPaul Moore  * @new: creds to modify
158008526a90SPaul Moore  *
158108526a90SPaul Moore  * Compute a context for a dentry as the inode is not yet available and set
158208526a90SPaul Moore  * that context in passed in creds so that new files are created using that
158308526a90SPaul Moore  * context. Context is calculated using the passed in creds and not the creds
158408526a90SPaul Moore  * of the caller.
158508526a90SPaul Moore  *
158608526a90SPaul Moore  * Return: Returns 0 on success, error on failure.
158708526a90SPaul Moore  */
security_dentry_create_files_as(struct dentry * dentry,int mode,struct qstr * name,const struct cred * old,struct cred * new)15882602625bSVivek Goyal int security_dentry_create_files_as(struct dentry *dentry, int mode,
15892602625bSVivek Goyal 				    struct qstr *name,
15902602625bSVivek Goyal 				    const struct cred *old, struct cred *new)
15912602625bSVivek Goyal {
15922602625bSVivek Goyal 	return call_int_hook(dentry_create_files_as, 0, dentry, mode,
15932602625bSVivek Goyal 			     name, old, new);
15942602625bSVivek Goyal }
15952602625bSVivek Goyal EXPORT_SYMBOL(security_dentry_create_files_as);
15962602625bSVivek Goyal 
1597916e3258SPaul Moore /**
1598916e3258SPaul Moore  * security_inode_init_security() - Initialize an inode's LSM context
1599916e3258SPaul Moore  * @inode: the inode
1600916e3258SPaul Moore  * @dir: parent directory
1601916e3258SPaul Moore  * @qstr: last component of the pathname
1602916e3258SPaul Moore  * @initxattrs: callback function to write xattrs
1603916e3258SPaul Moore  * @fs_data: filesystem specific data
1604916e3258SPaul Moore  *
1605916e3258SPaul Moore  * Obtain the security attribute name suffix and value to set on a newly
1606916e3258SPaul Moore  * created inode and set up the incore security field for the new inode.  This
1607916e3258SPaul Moore  * hook is called by the fs code as part of the inode creation transaction and
1608916e3258SPaul Moore  * provides for atomic labeling of the inode, unlike the post_create/mkdir/...
16096bcdfd2cSRoberto Sassu  * hooks called by the VFS.
1610916e3258SPaul Moore  *
16116bcdfd2cSRoberto Sassu  * The hook function is expected to populate the xattrs array, by calling
16126bcdfd2cSRoberto Sassu  * lsm_get_xattr_slot() to retrieve the slots reserved by the security module
16136bcdfd2cSRoberto Sassu  * with the lbs_xattr_count field of the lsm_blob_sizes structure.  For each
16146bcdfd2cSRoberto Sassu  * slot, the hook function should set ->name to the attribute name suffix
16156bcdfd2cSRoberto Sassu  * (e.g. selinux), to allocate ->value (will be freed by the caller) and set it
16166bcdfd2cSRoberto Sassu  * to the attribute value, to set ->value_len to the length of the value.  If
16176bcdfd2cSRoberto Sassu  * the security module does not use security attributes or does not wish to put
16186bcdfd2cSRoberto Sassu  * a security attribute on this particular inode, then it should return
16196bcdfd2cSRoberto Sassu  * -EOPNOTSUPP to skip this processing.
1620916e3258SPaul Moore  *
1621faf302f5SRoberto Sassu  * Return: Returns 0 if the LSM successfully initialized all of the inode
1622faf302f5SRoberto Sassu  *         security attributes that are required, negative values otherwise.
1623916e3258SPaul Moore  */
security_inode_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,const initxattrs initxattrs,void * fs_data)162420510f2fSJames Morris int security_inode_init_security(struct inode *inode, struct inode *dir,
16259d8f13baSMimi Zohar 				 const struct qstr *qstr,
16269d8f13baSMimi Zohar 				 const initxattrs initxattrs, void *fs_data)
16279d8f13baSMimi Zohar {
16286bcdfd2cSRoberto Sassu 	struct security_hook_list *hp;
16296bcdfd2cSRoberto Sassu 	struct xattr *new_xattrs = NULL;
16306bcdfd2cSRoberto Sassu 	int ret = -EOPNOTSUPP, xattr_count = 0;
16319d8f13baSMimi Zohar 
16329d8f13baSMimi Zohar 	if (unlikely(IS_PRIVATE(inode)))
1633fb88c2b6SMimi Zohar 		return 0;
16349d8f13baSMimi Zohar 
16356bcdfd2cSRoberto Sassu 	if (!blob_sizes.lbs_xattr_count)
16366bcdfd2cSRoberto Sassu 		return 0;
16376bcdfd2cSRoberto Sassu 
16386bcdfd2cSRoberto Sassu 	if (initxattrs) {
16396bcdfd2cSRoberto Sassu 		/* Allocate +1 for EVM and +1 as terminator. */
16406bcdfd2cSRoberto Sassu 		new_xattrs = kcalloc(blob_sizes.lbs_xattr_count + 2,
16416bcdfd2cSRoberto Sassu 				     sizeof(*new_xattrs), GFP_NOFS);
16426bcdfd2cSRoberto Sassu 		if (!new_xattrs)
16436bcdfd2cSRoberto Sassu 			return -ENOMEM;
16446bcdfd2cSRoberto Sassu 	}
16456bcdfd2cSRoberto Sassu 
16466bcdfd2cSRoberto Sassu 	hlist_for_each_entry(hp, &security_hook_heads.inode_init_security,
16476bcdfd2cSRoberto Sassu 			     list) {
16486bcdfd2cSRoberto Sassu 		ret = hp->hook.inode_init_security(inode, dir, qstr, new_xattrs,
16496bcdfd2cSRoberto Sassu 						  &xattr_count);
16506bcdfd2cSRoberto Sassu 		if (ret && ret != -EOPNOTSUPP)
16516bcdfd2cSRoberto Sassu 			goto out;
16526bcdfd2cSRoberto Sassu 		/*
16536bcdfd2cSRoberto Sassu 		 * As documented in lsm_hooks.h, -EOPNOTSUPP in this context
16546bcdfd2cSRoberto Sassu 		 * means that the LSM is not willing to provide an xattr, not
16556bcdfd2cSRoberto Sassu 		 * that it wants to signal an error. Thus, continue to invoke
16566bcdfd2cSRoberto Sassu 		 * the remaining LSMs.
16576bcdfd2cSRoberto Sassu 		 */
16586bcdfd2cSRoberto Sassu 	}
16596bcdfd2cSRoberto Sassu 
16606bcdfd2cSRoberto Sassu 	/* If initxattrs() is NULL, xattr_count is zero, skip the call. */
16616bcdfd2cSRoberto Sassu 	if (!xattr_count)
16629d8f13baSMimi Zohar 		goto out;
1663823eb1ccSMimi Zohar 
16646db7d1deSRoberto Sassu 	ret = evm_inode_init_security(inode, dir, qstr, new_xattrs,
16656db7d1deSRoberto Sassu 				      &xattr_count);
1666823eb1ccSMimi Zohar 	if (ret)
1667823eb1ccSMimi Zohar 		goto out;
16689d8f13baSMimi Zohar 	ret = initxattrs(inode, new_xattrs, fs_data);
16699d8f13baSMimi Zohar out:
16706bcdfd2cSRoberto Sassu 	for (; xattr_count > 0; xattr_count--)
16716bcdfd2cSRoberto Sassu 		kfree(new_xattrs[xattr_count - 1].value);
16726bcdfd2cSRoberto Sassu 	kfree(new_xattrs);
16739d8f13baSMimi Zohar 	return (ret == -EOPNOTSUPP) ? 0 : ret;
16749d8f13baSMimi Zohar }
16759d8f13baSMimi Zohar EXPORT_SYMBOL(security_inode_init_security);
16769d8f13baSMimi Zohar 
1677916e3258SPaul Moore /**
1678916e3258SPaul Moore  * security_inode_init_security_anon() - Initialize an anonymous inode
1679916e3258SPaul Moore  * @inode: the inode
1680916e3258SPaul Moore  * @name: the anonymous inode class
1681916e3258SPaul Moore  * @context_inode: an optional related inode
1682916e3258SPaul Moore  *
1683916e3258SPaul Moore  * Set up the incore security field for the new anonymous inode and return
1684916e3258SPaul Moore  * whether the inode creation is permitted by the security module or not.
1685916e3258SPaul Moore  *
1686916e3258SPaul Moore  * Return: Returns 0 on success, -EACCES if the security module denies the
1687916e3258SPaul Moore  * creation of this inode, or another -errno upon other errors.
1688916e3258SPaul Moore  */
security_inode_init_security_anon(struct inode * inode,const struct qstr * name,const struct inode * context_inode)1689215b674bSLokesh Gidra int security_inode_init_security_anon(struct inode *inode,
1690215b674bSLokesh Gidra 				      const struct qstr *name,
1691215b674bSLokesh Gidra 				      const struct inode *context_inode)
1692215b674bSLokesh Gidra {
1693215b674bSLokesh Gidra 	return call_int_hook(inode_init_security_anon, 0, inode, name,
1694215b674bSLokesh Gidra 			     context_inode);
1695215b674bSLokesh Gidra }
1696215b674bSLokesh Gidra 
1697be6d3e56SKentaro Takeda #ifdef CONFIG_SECURITY_PATH
1698916e3258SPaul Moore /**
1699916e3258SPaul Moore  * security_path_mknod() - Check if creating a special file is allowed
1700916e3258SPaul Moore  * @dir: parent directory
1701916e3258SPaul Moore  * @dentry: new file
1702916e3258SPaul Moore  * @mode: new file mode
1703916e3258SPaul Moore  * @dev: device number
1704916e3258SPaul Moore  *
1705916e3258SPaul Moore  * Check permissions when creating a file. Note that this hook is called even
1706916e3258SPaul Moore  * if mknod operation is being done for a regular file.
1707916e3258SPaul Moore  *
1708916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1709916e3258SPaul Moore  */
security_path_mknod(const struct path * dir,struct dentry * dentry,umode_t mode,unsigned int dev)171063c1845bSPaul Moore int security_path_mknod(const struct path *dir, struct dentry *dentry,
171163c1845bSPaul Moore 			umode_t mode, unsigned int dev)
1712be6d3e56SKentaro Takeda {
1713c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1714be6d3e56SKentaro Takeda 		return 0;
1715f25fce3eSCasey Schaufler 	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
1716be6d3e56SKentaro Takeda }
1717be6d3e56SKentaro Takeda EXPORT_SYMBOL(security_path_mknod);
1718be6d3e56SKentaro Takeda 
1719916e3258SPaul Moore /**
1720916e3258SPaul Moore  * security_path_mkdir() - Check if creating a new directory is allowed
1721916e3258SPaul Moore  * @dir: parent directory
1722916e3258SPaul Moore  * @dentry: new directory
1723916e3258SPaul Moore  * @mode: new directory mode
1724916e3258SPaul Moore  *
1725916e3258SPaul Moore  * Check permissions to create a new directory in the existing directory.
1726916e3258SPaul Moore  *
1727916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1728916e3258SPaul Moore  */
security_path_mkdir(const struct path * dir,struct dentry * dentry,umode_t mode)172963c1845bSPaul Moore int security_path_mkdir(const struct path *dir, struct dentry *dentry,
173063c1845bSPaul Moore 			umode_t mode)
1731be6d3e56SKentaro Takeda {
1732c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1733be6d3e56SKentaro Takeda 		return 0;
1734f25fce3eSCasey Schaufler 	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
1735be6d3e56SKentaro Takeda }
173682140443SDavid Howells EXPORT_SYMBOL(security_path_mkdir);
1737be6d3e56SKentaro Takeda 
1738916e3258SPaul Moore /**
1739916e3258SPaul Moore  * security_path_rmdir() - Check if removing a directory is allowed
1740916e3258SPaul Moore  * @dir: parent directory
1741916e3258SPaul Moore  * @dentry: directory to remove
1742916e3258SPaul Moore  *
1743916e3258SPaul Moore  * Check the permission to remove a directory.
1744916e3258SPaul Moore  *
1745916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1746916e3258SPaul Moore  */
security_path_rmdir(const struct path * dir,struct dentry * dentry)1747989f74e0SAl Viro int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1748be6d3e56SKentaro Takeda {
1749c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1750be6d3e56SKentaro Takeda 		return 0;
1751f25fce3eSCasey Schaufler 	return call_int_hook(path_rmdir, 0, dir, dentry);
1752be6d3e56SKentaro Takeda }
1753be6d3e56SKentaro Takeda 
1754916e3258SPaul Moore /**
1755916e3258SPaul Moore  * security_path_unlink() - Check if removing a hard link is allowed
1756916e3258SPaul Moore  * @dir: parent directory
1757916e3258SPaul Moore  * @dentry: file
1758916e3258SPaul Moore  *
1759916e3258SPaul Moore  * Check the permission to remove a hard link to a file.
1760916e3258SPaul Moore  *
1761916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1762916e3258SPaul Moore  */
security_path_unlink(const struct path * dir,struct dentry * dentry)1763989f74e0SAl Viro int security_path_unlink(const struct path *dir, struct dentry *dentry)
1764be6d3e56SKentaro Takeda {
1765c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1766be6d3e56SKentaro Takeda 		return 0;
1767f25fce3eSCasey Schaufler 	return call_int_hook(path_unlink, 0, dir, dentry);
1768be6d3e56SKentaro Takeda }
176982140443SDavid Howells EXPORT_SYMBOL(security_path_unlink);
1770be6d3e56SKentaro Takeda 
1771916e3258SPaul Moore /**
1772916e3258SPaul Moore  * security_path_symlink() - Check if creating a symbolic link is allowed
1773916e3258SPaul Moore  * @dir: parent directory
1774916e3258SPaul Moore  * @dentry: symbolic link
1775916e3258SPaul Moore  * @old_name: file pathname
1776916e3258SPaul Moore  *
1777916e3258SPaul Moore  * Check the permission to create a symbolic link to a file.
1778916e3258SPaul Moore  *
1779916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1780916e3258SPaul Moore  */
security_path_symlink(const struct path * dir,struct dentry * dentry,const char * old_name)1781d3607752SAl Viro int security_path_symlink(const struct path *dir, struct dentry *dentry,
1782be6d3e56SKentaro Takeda 			  const char *old_name)
1783be6d3e56SKentaro Takeda {
1784c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
1785be6d3e56SKentaro Takeda 		return 0;
1786f25fce3eSCasey Schaufler 	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1787be6d3e56SKentaro Takeda }
1788be6d3e56SKentaro Takeda 
1789916e3258SPaul Moore /**
1790916e3258SPaul Moore  * security_path_link - Check if creating a hard link is allowed
1791916e3258SPaul Moore  * @old_dentry: existing file
1792916e3258SPaul Moore  * @new_dir: new parent directory
1793916e3258SPaul Moore  * @new_dentry: new link
1794916e3258SPaul Moore  *
1795916e3258SPaul Moore  * Check permission before creating a new hard link to a file.
1796916e3258SPaul Moore  *
1797916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1798916e3258SPaul Moore  */
security_path_link(struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry)17993ccee46aSAl Viro int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1800be6d3e56SKentaro Takeda 		       struct dentry *new_dentry)
1801be6d3e56SKentaro Takeda {
1802c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
1803be6d3e56SKentaro Takeda 		return 0;
1804f25fce3eSCasey Schaufler 	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1805be6d3e56SKentaro Takeda }
1806be6d3e56SKentaro Takeda 
1807916e3258SPaul Moore /**
1808916e3258SPaul Moore  * security_path_rename() - Check if renaming a file is allowed
1809916e3258SPaul Moore  * @old_dir: parent directory of the old file
1810916e3258SPaul Moore  * @old_dentry: the old file
1811916e3258SPaul Moore  * @new_dir: parent directory of the new file
1812916e3258SPaul Moore  * @new_dentry: the new file
1813916e3258SPaul Moore  * @flags: flags
1814916e3258SPaul Moore  *
1815916e3258SPaul Moore  * Check for permission to rename a file or directory.
1816916e3258SPaul Moore  *
1817916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1818916e3258SPaul Moore  */
security_path_rename(const struct path * old_dir,struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry,unsigned int flags)18193ccee46aSAl Viro int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
18203ccee46aSAl Viro 			 const struct path *new_dir, struct dentry *new_dentry,
18210b3974ebSMiklos Szeredi 			 unsigned int flags)
1822be6d3e56SKentaro Takeda {
1823c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
182463c1845bSPaul Moore 		     (d_is_positive(new_dentry) &&
182563c1845bSPaul Moore 		      IS_PRIVATE(d_backing_inode(new_dentry)))))
1826be6d3e56SKentaro Takeda 		return 0;
1827da1ce067SMiklos Szeredi 
1828f25fce3eSCasey Schaufler 	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
1829100f59d9SMickaël Salaün 			     new_dentry, flags);
1830be6d3e56SKentaro Takeda }
183182140443SDavid Howells EXPORT_SYMBOL(security_path_rename);
1832be6d3e56SKentaro Takeda 
1833916e3258SPaul Moore /**
1834916e3258SPaul Moore  * security_path_truncate() - Check if truncating a file is allowed
1835916e3258SPaul Moore  * @path: file
1836916e3258SPaul Moore  *
1837916e3258SPaul Moore  * Check permission before truncating the file indicated by path.  Note that
1838916e3258SPaul Moore  * truncation permissions may also be checked based on already opened files,
1839916e3258SPaul Moore  * using the security_file_truncate() hook.
1840916e3258SPaul Moore  *
1841916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1842916e3258SPaul Moore  */
security_path_truncate(const struct path * path)184381f4c506SAl Viro int security_path_truncate(const struct path *path)
1844be6d3e56SKentaro Takeda {
1845c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
1846be6d3e56SKentaro Takeda 		return 0;
1847f25fce3eSCasey Schaufler 	return call_int_hook(path_truncate, 0, path);
1848be6d3e56SKentaro Takeda }
184989eda068STetsuo Handa 
1850916e3258SPaul Moore /**
1851916e3258SPaul Moore  * security_path_chmod() - Check if changing the file's mode is allowed
1852916e3258SPaul Moore  * @path: file
1853916e3258SPaul Moore  * @mode: new mode
1854916e3258SPaul Moore  *
1855916e3258SPaul Moore  * Check for permission to change a mode of the file @path. The new mode is
1856916e3258SPaul Moore  * specified in @mode which is a bitmask of constants from
1857916e3258SPaul Moore  * <include/uapi/linux/stat.h>.
1858916e3258SPaul Moore  *
1859916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1860916e3258SPaul Moore  */
security_path_chmod(const struct path * path,umode_t mode)1861be01f9f2SAl Viro int security_path_chmod(const struct path *path, umode_t mode)
186289eda068STetsuo Handa {
1863c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
186489eda068STetsuo Handa 		return 0;
1865f25fce3eSCasey Schaufler 	return call_int_hook(path_chmod, 0, path, mode);
186689eda068STetsuo Handa }
186789eda068STetsuo Handa 
1868916e3258SPaul Moore /**
1869916e3258SPaul Moore  * security_path_chown() - Check if changing the file's owner/group is allowed
1870916e3258SPaul Moore  * @path: file
1871916e3258SPaul Moore  * @uid: file owner
1872916e3258SPaul Moore  * @gid: file group
1873916e3258SPaul Moore  *
1874916e3258SPaul Moore  * Check for permission to change owner/group of a file or directory.
1875916e3258SPaul Moore  *
1876916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1877916e3258SPaul Moore  */
security_path_chown(const struct path * path,kuid_t uid,kgid_t gid)18787fd25dacSAl Viro int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
187989eda068STetsuo Handa {
1880c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
188189eda068STetsuo Handa 		return 0;
1882f25fce3eSCasey Schaufler 	return call_int_hook(path_chown, 0, path, uid, gid);
188389eda068STetsuo Handa }
18848b8efb44STetsuo Handa 
1885916e3258SPaul Moore /**
1886916e3258SPaul Moore  * security_path_chroot() - Check if changing the root directory is allowed
1887916e3258SPaul Moore  * @path: directory
1888916e3258SPaul Moore  *
1889916e3258SPaul Moore  * Check for permission to change root directory.
1890916e3258SPaul Moore  *
1891916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1892916e3258SPaul Moore  */
security_path_chroot(const struct path * path)189377b286c0SAl Viro int security_path_chroot(const struct path *path)
18948b8efb44STetsuo Handa {
1895f25fce3eSCasey Schaufler 	return call_int_hook(path_chroot, 0, path);
18968b8efb44STetsuo Handa }
189763c1845bSPaul Moore #endif /* CONFIG_SECURITY_PATH */
1898be6d3e56SKentaro Takeda 
1899916e3258SPaul Moore /**
1900916e3258SPaul Moore  * security_inode_create() - Check if creating a file is allowed
1901916e3258SPaul Moore  * @dir: the parent directory
1902916e3258SPaul Moore  * @dentry: the file being created
1903916e3258SPaul Moore  * @mode: requested file mode
1904916e3258SPaul Moore  *
1905916e3258SPaul Moore  * Check permission to create a regular file.
1906916e3258SPaul Moore  *
1907916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1908916e3258SPaul Moore  */
security_inode_create(struct inode * dir,struct dentry * dentry,umode_t mode)190963c1845bSPaul Moore int security_inode_create(struct inode *dir, struct dentry *dentry,
191063c1845bSPaul Moore 			  umode_t mode)
191120510f2fSJames Morris {
191220510f2fSJames Morris 	if (unlikely(IS_PRIVATE(dir)))
191320510f2fSJames Morris 		return 0;
1914f25fce3eSCasey Schaufler 	return call_int_hook(inode_create, 0, dir, dentry, mode);
191520510f2fSJames Morris }
1916800a9647SDavid Howells EXPORT_SYMBOL_GPL(security_inode_create);
191720510f2fSJames Morris 
1918916e3258SPaul Moore /**
1919916e3258SPaul Moore  * security_inode_link() - Check if creating a hard link is allowed
1920916e3258SPaul Moore  * @old_dentry: existing file
1921916e3258SPaul Moore  * @dir: new parent directory
1922916e3258SPaul Moore  * @new_dentry: new link
1923916e3258SPaul Moore  *
1924916e3258SPaul Moore  * Check permission before creating a new hard link to a file.
1925916e3258SPaul Moore  *
1926916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1927916e3258SPaul Moore  */
security_inode_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)192820510f2fSJames Morris int security_inode_link(struct dentry *old_dentry, struct inode *dir,
192920510f2fSJames Morris 			struct dentry *new_dentry)
193020510f2fSJames Morris {
1931c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
193220510f2fSJames Morris 		return 0;
1933f25fce3eSCasey Schaufler 	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
193420510f2fSJames Morris }
193520510f2fSJames Morris 
1936916e3258SPaul Moore /**
1937916e3258SPaul Moore  * security_inode_unlink() - Check if removing a hard link is allowed
1938916e3258SPaul Moore  * @dir: parent directory
1939916e3258SPaul Moore  * @dentry: file
1940916e3258SPaul Moore  *
1941916e3258SPaul Moore  * Check the permission to remove a hard link to a file.
1942916e3258SPaul Moore  *
1943916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1944916e3258SPaul Moore  */
security_inode_unlink(struct inode * dir,struct dentry * dentry)194520510f2fSJames Morris int security_inode_unlink(struct inode *dir, struct dentry *dentry)
194620510f2fSJames Morris {
1947c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
194820510f2fSJames Morris 		return 0;
1949f25fce3eSCasey Schaufler 	return call_int_hook(inode_unlink, 0, dir, dentry);
195020510f2fSJames Morris }
195120510f2fSJames Morris 
1952916e3258SPaul Moore /**
19531e2523d7SPaul Moore  * security_inode_symlink() - Check if creating a symbolic link is allowed
1954916e3258SPaul Moore  * @dir: parent directory
1955916e3258SPaul Moore  * @dentry: symbolic link
1956916e3258SPaul Moore  * @old_name: existing filename
1957916e3258SPaul Moore  *
1958916e3258SPaul Moore  * Check the permission to create a symbolic link to a file.
1959916e3258SPaul Moore  *
1960916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1961916e3258SPaul Moore  */
security_inode_symlink(struct inode * dir,struct dentry * dentry,const char * old_name)196220510f2fSJames Morris int security_inode_symlink(struct inode *dir, struct dentry *dentry,
196320510f2fSJames Morris 			   const char *old_name)
196420510f2fSJames Morris {
196520510f2fSJames Morris 	if (unlikely(IS_PRIVATE(dir)))
196620510f2fSJames Morris 		return 0;
1967f25fce3eSCasey Schaufler 	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
196820510f2fSJames Morris }
196920510f2fSJames Morris 
1970916e3258SPaul Moore /**
1971916e3258SPaul Moore  * security_inode_mkdir() - Check if creation a new director is allowed
1972916e3258SPaul Moore  * @dir: parent directory
1973916e3258SPaul Moore  * @dentry: new directory
1974916e3258SPaul Moore  * @mode: new directory mode
1975916e3258SPaul Moore  *
1976916e3258SPaul Moore  * Check permissions to create a new directory in the existing directory
1977916e3258SPaul Moore  * associated with inode structure @dir.
1978916e3258SPaul Moore  *
1979916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1980916e3258SPaul Moore  */
security_inode_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)198118bb1db3SAl Viro int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
198220510f2fSJames Morris {
198320510f2fSJames Morris 	if (unlikely(IS_PRIVATE(dir)))
198420510f2fSJames Morris 		return 0;
1985f25fce3eSCasey Schaufler 	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
198620510f2fSJames Morris }
1987800a9647SDavid Howells EXPORT_SYMBOL_GPL(security_inode_mkdir);
198820510f2fSJames Morris 
1989916e3258SPaul Moore /**
1990916e3258SPaul Moore  * security_inode_rmdir() - Check if removing a directory is allowed
1991916e3258SPaul Moore  * @dir: parent directory
1992916e3258SPaul Moore  * @dentry: directory to be removed
1993916e3258SPaul Moore  *
1994916e3258SPaul Moore  * Check the permission to remove a directory.
1995916e3258SPaul Moore  *
1996916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
1997916e3258SPaul Moore  */
security_inode_rmdir(struct inode * dir,struct dentry * dentry)199820510f2fSJames Morris int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
199920510f2fSJames Morris {
2000c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
200120510f2fSJames Morris 		return 0;
2002f25fce3eSCasey Schaufler 	return call_int_hook(inode_rmdir, 0, dir, dentry);
200320510f2fSJames Morris }
200420510f2fSJames Morris 
2005916e3258SPaul Moore /**
2006916e3258SPaul Moore  * security_inode_mknod() - Check if creating a special file is allowed
2007916e3258SPaul Moore  * @dir: parent directory
2008916e3258SPaul Moore  * @dentry: new file
2009916e3258SPaul Moore  * @mode: new file mode
2010916e3258SPaul Moore  * @dev: device number
2011916e3258SPaul Moore  *
2012916e3258SPaul Moore  * Check permissions when creating a special file (or a socket or a fifo file
2013916e3258SPaul Moore  * created via the mknod system call).  Note that if mknod operation is being
2014916e3258SPaul Moore  * done for a regular file, then the create hook will be called and not this
2015916e3258SPaul Moore  * hook.
2016916e3258SPaul Moore  *
2017916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2018916e3258SPaul Moore  */
security_inode_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)201963c1845bSPaul Moore int security_inode_mknod(struct inode *dir, struct dentry *dentry,
202063c1845bSPaul Moore 			 umode_t mode, dev_t dev)
202120510f2fSJames Morris {
202220510f2fSJames Morris 	if (unlikely(IS_PRIVATE(dir)))
202320510f2fSJames Morris 		return 0;
2024f25fce3eSCasey Schaufler 	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
202520510f2fSJames Morris }
202620510f2fSJames Morris 
2027916e3258SPaul Moore /**
2028916e3258SPaul Moore  * security_inode_rename() - Check if renaming a file is allowed
2029916e3258SPaul Moore  * @old_dir: parent directory of the old file
2030916e3258SPaul Moore  * @old_dentry: the old file
2031916e3258SPaul Moore  * @new_dir: parent directory of the new file
2032916e3258SPaul Moore  * @new_dentry: the new file
2033916e3258SPaul Moore  * @flags: flags
2034916e3258SPaul Moore  *
2035916e3258SPaul Moore  * Check for permission to rename a file or directory.
2036916e3258SPaul Moore  *
2037916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2038916e3258SPaul Moore  */
security_inode_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)203920510f2fSJames Morris int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
20400b3974ebSMiklos Szeredi 			  struct inode *new_dir, struct dentry *new_dentry,
20410b3974ebSMiklos Szeredi 			  unsigned int flags)
204220510f2fSJames Morris {
2043c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
204463c1845bSPaul Moore 		     (d_is_positive(new_dentry) &&
204563c1845bSPaul Moore 		      IS_PRIVATE(d_backing_inode(new_dentry)))))
204620510f2fSJames Morris 		return 0;
2047da1ce067SMiklos Szeredi 
2048da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
2049f25fce3eSCasey Schaufler 		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
2050da1ce067SMiklos Szeredi 					old_dir, old_dentry);
2051da1ce067SMiklos Szeredi 		if (err)
2052da1ce067SMiklos Szeredi 			return err;
2053da1ce067SMiklos Szeredi 	}
2054da1ce067SMiklos Szeredi 
2055f25fce3eSCasey Schaufler 	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
205620510f2fSJames Morris 			     new_dir, new_dentry);
205720510f2fSJames Morris }
205820510f2fSJames Morris 
2059916e3258SPaul Moore /**
2060916e3258SPaul Moore  * security_inode_readlink() - Check if reading a symbolic link is allowed
2061916e3258SPaul Moore  * @dentry: link
2062916e3258SPaul Moore  *
2063916e3258SPaul Moore  * Check the permission to read the symbolic link.
2064916e3258SPaul Moore  *
2065916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2066916e3258SPaul Moore  */
security_inode_readlink(struct dentry * dentry)206720510f2fSJames Morris int security_inode_readlink(struct dentry *dentry)
206820510f2fSJames Morris {
2069c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
207020510f2fSJames Morris 		return 0;
2071f25fce3eSCasey Schaufler 	return call_int_hook(inode_readlink, 0, dentry);
207220510f2fSJames Morris }
207320510f2fSJames Morris 
2074916e3258SPaul Moore /**
2075916e3258SPaul Moore  * security_inode_follow_link() - Check if following a symbolic link is allowed
2076916e3258SPaul Moore  * @dentry: link dentry
2077916e3258SPaul Moore  * @inode: link inode
2078916e3258SPaul Moore  * @rcu: true if in RCU-walk mode
2079916e3258SPaul Moore  *
2080916e3258SPaul Moore  * Check permission to follow a symbolic link when looking up a pathname.  If
2081916e3258SPaul Moore  * @rcu is true, @inode is not stable.
2082916e3258SPaul Moore  *
2083916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2084916e3258SPaul Moore  */
security_inode_follow_link(struct dentry * dentry,struct inode * inode,bool rcu)2085bda0be7aSNeilBrown int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
2086bda0be7aSNeilBrown 			       bool rcu)
208720510f2fSJames Morris {
2088bda0be7aSNeilBrown 	if (unlikely(IS_PRIVATE(inode)))
208920510f2fSJames Morris 		return 0;
2090e22619a2SLinus Torvalds 	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
209120510f2fSJames Morris }
209220510f2fSJames Morris 
2093916e3258SPaul Moore /**
2094916e3258SPaul Moore  * security_inode_permission() - Check if accessing an inode is allowed
2095916e3258SPaul Moore  * @inode: inode
2096916e3258SPaul Moore  * @mask: access mask
2097916e3258SPaul Moore  *
2098916e3258SPaul Moore  * Check permission before accessing an inode.  This hook is called by the
2099916e3258SPaul Moore  * existing Linux permission function, so a security module can use it to
2100916e3258SPaul Moore  * provide additional checking for existing Linux permission checks.  Notice
2101916e3258SPaul Moore  * that this hook is called when a file is opened (as well as many other
2102916e3258SPaul Moore  * operations), whereas the file_security_ops permission hook is called when
2103916e3258SPaul Moore  * the actual read/write operations are performed.
2104916e3258SPaul Moore  *
2105916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2106916e3258SPaul Moore  */
security_inode_permission(struct inode * inode,int mask)2107b77b0646SAl Viro int security_inode_permission(struct inode *inode, int mask)
210820510f2fSJames Morris {
210920510f2fSJames Morris 	if (unlikely(IS_PRIVATE(inode)))
211020510f2fSJames Morris 		return 0;
2111f25fce3eSCasey Schaufler 	return call_int_hook(inode_permission, 0, inode, mask);
211220510f2fSJames Morris }
211320510f2fSJames Morris 
2114916e3258SPaul Moore /**
2115916e3258SPaul Moore  * security_inode_setattr() - Check if setting file attributes is allowed
2116916e3258SPaul Moore  * @idmap: idmap of the mount
2117916e3258SPaul Moore  * @dentry: file
2118916e3258SPaul Moore  * @attr: new attributes
2119916e3258SPaul Moore  *
2120916e3258SPaul Moore  * Check permission before setting file attributes.  Note that the kernel call
2121916e3258SPaul Moore  * to notify_change is performed from several locations, whenever file
2122916e3258SPaul Moore  * attributes change (such as when a file is truncated, chown/chmod operations,
2123916e3258SPaul Moore  * transferring disk quotas, etc).
2124916e3258SPaul Moore  *
2125916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2126916e3258SPaul Moore  */
security_inode_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)2127c1632a0fSChristian Brauner int security_inode_setattr(struct mnt_idmap *idmap,
21280e363cf3SChristian Brauner 			   struct dentry *dentry, struct iattr *attr)
212920510f2fSJames Morris {
2130817b54aaSMimi Zohar 	int ret;
2131817b54aaSMimi Zohar 
2132c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
213320510f2fSJames Morris 		return 0;
2134f25fce3eSCasey Schaufler 	ret = call_int_hook(inode_setattr, 0, dentry, attr);
2135817b54aaSMimi Zohar 	if (ret)
2136817b54aaSMimi Zohar 		return ret;
2137c1632a0fSChristian Brauner 	return evm_inode_setattr(idmap, dentry, attr);
213820510f2fSJames Morris }
2139b1da47e2SMiklos Szeredi EXPORT_SYMBOL_GPL(security_inode_setattr);
214020510f2fSJames Morris 
2141916e3258SPaul Moore /**
2142916e3258SPaul Moore  * security_inode_getattr() - Check if getting file attributes is allowed
2143916e3258SPaul Moore  * @path: file
2144916e3258SPaul Moore  *
2145916e3258SPaul Moore  * Check permission before obtaining file attributes.
2146916e3258SPaul Moore  *
2147916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2148916e3258SPaul Moore  */
security_inode_getattr(const struct path * path)21493f7036a0SAl Viro int security_inode_getattr(const struct path *path)
215020510f2fSJames Morris {
2151c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
215220510f2fSJames Morris 		return 0;
2153f25fce3eSCasey Schaufler 	return call_int_hook(inode_getattr, 0, path);
215420510f2fSJames Morris }
215520510f2fSJames Morris 
2156916e3258SPaul Moore /**
2157916e3258SPaul Moore  * security_inode_setxattr() - Check if setting file xattrs is allowed
2158916e3258SPaul Moore  * @idmap: idmap of the mount
2159916e3258SPaul Moore  * @dentry: file
2160916e3258SPaul Moore  * @name: xattr name
2161916e3258SPaul Moore  * @value: xattr value
21621e2523d7SPaul Moore  * @size: size of xattr value
2163916e3258SPaul Moore  * @flags: flags
2164916e3258SPaul Moore  *
2165916e3258SPaul Moore  * Check permission before setting the extended attributes.
2166916e3258SPaul Moore  *
2167916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2168916e3258SPaul Moore  */
security_inode_setxattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * name,const void * value,size_t size,int flags)216939f60c1cSChristian Brauner int security_inode_setxattr(struct mnt_idmap *idmap,
217071bc356fSChristian Brauner 			    struct dentry *dentry, const char *name,
21718f0cfa52SDavid Howells 			    const void *value, size_t size, int flags)
217220510f2fSJames Morris {
21733e1be52dSMimi Zohar 	int ret;
21743e1be52dSMimi Zohar 
2175c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
217620510f2fSJames Morris 		return 0;
2177b1d9e6b0SCasey Schaufler 	/*
2178b1d9e6b0SCasey Schaufler 	 * SELinux and Smack integrate the cap call,
2179b1d9e6b0SCasey Schaufler 	 * so assume that all LSMs supplying this call do so.
2180b1d9e6b0SCasey Schaufler 	 */
218139f60c1cSChristian Brauner 	ret = call_int_hook(inode_setxattr, 1, idmap, dentry, name, value,
218271bc356fSChristian Brauner 			    size, flags);
2183b1d9e6b0SCasey Schaufler 
2184b1d9e6b0SCasey Schaufler 	if (ret == 1)
2185b1d9e6b0SCasey Schaufler 		ret = cap_inode_setxattr(dentry, name, value, size, flags);
21863e1be52dSMimi Zohar 	if (ret)
21873e1be52dSMimi Zohar 		return ret;
218842c63330SMimi Zohar 	ret = ima_inode_setxattr(dentry, name, value, size);
218942c63330SMimi Zohar 	if (ret)
219042c63330SMimi Zohar 		return ret;
219139f60c1cSChristian Brauner 	return evm_inode_setxattr(idmap, dentry, name, value, size);
219220510f2fSJames Morris }
219320510f2fSJames Morris 
2194916e3258SPaul Moore /**
2195916e3258SPaul Moore  * security_inode_set_acl() - Check if setting posix acls is allowed
2196916e3258SPaul Moore  * @idmap: idmap of the mount
2197916e3258SPaul Moore  * @dentry: file
2198916e3258SPaul Moore  * @acl_name: acl name
2199916e3258SPaul Moore  * @kacl: acl struct
2200916e3258SPaul Moore  *
2201916e3258SPaul Moore  * Check permission before setting posix acls, the posix acls in @kacl are
2202916e3258SPaul Moore  * identified by @acl_name.
2203916e3258SPaul Moore  *
2204916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2205916e3258SPaul Moore  */
security_inode_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name,struct posix_acl * kacl)2206700b7940SChristian Brauner int security_inode_set_acl(struct mnt_idmap *idmap,
220772b3897eSChristian Brauner 			   struct dentry *dentry, const char *acl_name,
220872b3897eSChristian Brauner 			   struct posix_acl *kacl)
220972b3897eSChristian Brauner {
2210e61b135fSChristian Brauner 	int ret;
2211e61b135fSChristian Brauner 
221272b3897eSChristian Brauner 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
221372b3897eSChristian Brauner 		return 0;
2214700b7940SChristian Brauner 	ret = call_int_hook(inode_set_acl, 0, idmap, dentry, acl_name,
2215e61b135fSChristian Brauner 			    kacl);
2216e61b135fSChristian Brauner 	if (ret)
2217e61b135fSChristian Brauner 		return ret;
2218700b7940SChristian Brauner 	ret = ima_inode_set_acl(idmap, dentry, acl_name, kacl);
2219e61b135fSChristian Brauner 	if (ret)
2220e61b135fSChristian Brauner 		return ret;
2221700b7940SChristian Brauner 	return evm_inode_set_acl(idmap, dentry, acl_name, kacl);
222272b3897eSChristian Brauner }
222372b3897eSChristian Brauner 
2224916e3258SPaul Moore /**
2225916e3258SPaul Moore  * security_inode_get_acl() - Check if reading posix acls is allowed
2226916e3258SPaul Moore  * @idmap: idmap of the mount
2227916e3258SPaul Moore  * @dentry: file
2228916e3258SPaul Moore  * @acl_name: acl name
2229916e3258SPaul Moore  *
2230916e3258SPaul Moore  * Check permission before getting osix acls, the posix acls are identified by
2231916e3258SPaul Moore  * @acl_name.
2232916e3258SPaul Moore  *
2233916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2234916e3258SPaul Moore  */
security_inode_get_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name)2235700b7940SChristian Brauner int security_inode_get_acl(struct mnt_idmap *idmap,
223672b3897eSChristian Brauner 			   struct dentry *dentry, const char *acl_name)
223772b3897eSChristian Brauner {
223872b3897eSChristian Brauner 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
223972b3897eSChristian Brauner 		return 0;
2240700b7940SChristian Brauner 	return call_int_hook(inode_get_acl, 0, idmap, dentry, acl_name);
224172b3897eSChristian Brauner }
224272b3897eSChristian Brauner 
2243916e3258SPaul Moore /**
2244916e3258SPaul Moore  * security_inode_remove_acl() - Check if removing a posix acl is allowed
2245916e3258SPaul Moore  * @idmap: idmap of the mount
2246916e3258SPaul Moore  * @dentry: file
2247916e3258SPaul Moore  * @acl_name: acl name
2248916e3258SPaul Moore  *
2249916e3258SPaul Moore  * Check permission before removing posix acls, the posix acls are identified
2250916e3258SPaul Moore  * by @acl_name.
2251916e3258SPaul Moore  *
2252916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2253916e3258SPaul Moore  */
security_inode_remove_acl(struct mnt_idmap * idmap,struct dentry * dentry,const char * acl_name)2254700b7940SChristian Brauner int security_inode_remove_acl(struct mnt_idmap *idmap,
225572b3897eSChristian Brauner 			      struct dentry *dentry, const char *acl_name)
225672b3897eSChristian Brauner {
2257e61b135fSChristian Brauner 	int ret;
2258e61b135fSChristian Brauner 
225972b3897eSChristian Brauner 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
226072b3897eSChristian Brauner 		return 0;
2261700b7940SChristian Brauner 	ret = call_int_hook(inode_remove_acl, 0, idmap, dentry, acl_name);
2262e61b135fSChristian Brauner 	if (ret)
2263e61b135fSChristian Brauner 		return ret;
2264700b7940SChristian Brauner 	ret = ima_inode_remove_acl(idmap, dentry, acl_name);
2265e61b135fSChristian Brauner 	if (ret)
2266e61b135fSChristian Brauner 		return ret;
2267700b7940SChristian Brauner 	return evm_inode_remove_acl(idmap, dentry, acl_name);
226872b3897eSChristian Brauner }
226972b3897eSChristian Brauner 
2270916e3258SPaul Moore /**
2271916e3258SPaul Moore  * security_inode_post_setxattr() - Update the inode after a setxattr operation
2272916e3258SPaul Moore  * @dentry: file
2273916e3258SPaul Moore  * @name: xattr name
2274916e3258SPaul Moore  * @value: xattr value
2275916e3258SPaul Moore  * @size: xattr value size
2276916e3258SPaul Moore  * @flags: flags
2277916e3258SPaul Moore  *
2278916e3258SPaul Moore  * Update inode security field after successful setxattr operation.
2279916e3258SPaul Moore  */
security_inode_post_setxattr(struct dentry * dentry,const char * name,const void * value,size_t size,int flags)22808f0cfa52SDavid Howells void security_inode_post_setxattr(struct dentry *dentry, const char *name,
22818f0cfa52SDavid Howells 				  const void *value, size_t size, int flags)
228220510f2fSJames Morris {
2283c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
228420510f2fSJames Morris 		return;
2285f25fce3eSCasey Schaufler 	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
22863e1be52dSMimi Zohar 	evm_inode_post_setxattr(dentry, name, value, size);
228720510f2fSJames Morris }
228820510f2fSJames Morris 
2289916e3258SPaul Moore /**
2290916e3258SPaul Moore  * security_inode_getxattr() - Check if xattr access is allowed
2291916e3258SPaul Moore  * @dentry: file
2292916e3258SPaul Moore  * @name: xattr name
2293916e3258SPaul Moore  *
2294916e3258SPaul Moore  * Check permission before obtaining the extended attributes identified by
2295916e3258SPaul Moore  * @name for @dentry.
2296916e3258SPaul Moore  *
2297916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2298916e3258SPaul Moore  */
security_inode_getxattr(struct dentry * dentry,const char * name)22998f0cfa52SDavid Howells int security_inode_getxattr(struct dentry *dentry, const char *name)
230020510f2fSJames Morris {
2301c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
230220510f2fSJames Morris 		return 0;
2303f25fce3eSCasey Schaufler 	return call_int_hook(inode_getxattr, 0, dentry, name);
230420510f2fSJames Morris }
230520510f2fSJames Morris 
2306916e3258SPaul Moore /**
2307916e3258SPaul Moore  * security_inode_listxattr() - Check if listing xattrs is allowed
2308916e3258SPaul Moore  * @dentry: file
2309916e3258SPaul Moore  *
2310916e3258SPaul Moore  * Check permission before obtaining the list of extended attribute names for
2311916e3258SPaul Moore  * @dentry.
2312916e3258SPaul Moore  *
2313916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2314916e3258SPaul Moore  */
security_inode_listxattr(struct dentry * dentry)231520510f2fSJames Morris int security_inode_listxattr(struct dentry *dentry)
231620510f2fSJames Morris {
2317c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
231820510f2fSJames Morris 		return 0;
2319f25fce3eSCasey Schaufler 	return call_int_hook(inode_listxattr, 0, dentry);
232020510f2fSJames Morris }
232120510f2fSJames Morris 
2322916e3258SPaul Moore /**
2323916e3258SPaul Moore  * security_inode_removexattr() - Check if removing an xattr is allowed
2324916e3258SPaul Moore  * @idmap: idmap of the mount
2325916e3258SPaul Moore  * @dentry: file
2326916e3258SPaul Moore  * @name: xattr name
2327916e3258SPaul Moore  *
2328916e3258SPaul Moore  * Check permission before removing the extended attribute identified by @name
2329916e3258SPaul Moore  * for @dentry.
2330916e3258SPaul Moore  *
2331916e3258SPaul Moore  * Return: Returns 0 if permission is granted.
2332916e3258SPaul Moore  */
security_inode_removexattr(struct mnt_idmap * idmap,struct dentry * dentry,const char * name)233339f60c1cSChristian Brauner int security_inode_removexattr(struct mnt_idmap *idmap,
233471bc356fSChristian Brauner 			       struct dentry *dentry, const char *name)
233520510f2fSJames Morris {
23363e1be52dSMimi Zohar 	int ret;
23373e1be52dSMimi Zohar 
2338c6f493d6SDavid Howells 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
233920510f2fSJames Morris 		return 0;
2340b1d9e6b0SCasey Schaufler 	/*
2341b1d9e6b0SCasey Schaufler 	 * SELinux and Smack integrate the cap call,
2342b1d9e6b0SCasey Schaufler 	 * so assume that all LSMs supplying this call do so.
2343b1d9e6b0SCasey Schaufler 	 */
234439f60c1cSChristian Brauner 	ret = call_int_hook(inode_removexattr, 1, idmap, dentry, name);
2345b1d9e6b0SCasey Schaufler 	if (ret == 1)
234639f60c1cSChristian Brauner 		ret = cap_inode_removexattr(idmap, dentry, name);
23473e1be52dSMimi Zohar 	if (ret)
23483e1be52dSMimi Zohar 		return ret;
234942c63330SMimi Zohar 	ret = ima_inode_removexattr(dentry, name);
235042c63330SMimi Zohar 	if (ret)
235142c63330SMimi Zohar 		return ret;
235239f60c1cSChristian Brauner 	return evm_inode_removexattr(idmap, dentry, name);
235320510f2fSJames Morris }
235420510f2fSJames Morris 
2355916e3258SPaul Moore /**
2356916e3258SPaul Moore  * security_inode_need_killpriv() - Check if security_inode_killpriv() required
2357916e3258SPaul Moore  * @dentry: associated dentry
2358916e3258SPaul Moore  *
2359916e3258SPaul Moore  * Called when an inode has been changed to determine if
2360916e3258SPaul Moore  * security_inode_killpriv() should be called.
2361916e3258SPaul Moore  *
2362916e3258SPaul Moore  * Return: Return <0 on error to abort the inode change operation, return 0 if
2363916e3258SPaul Moore  *         security_inode_killpriv() does not need to be called, return >0 if
2364916e3258SPaul Moore  *         security_inode_killpriv() does need to be called.
2365916e3258SPaul Moore  */
security_inode_need_killpriv(struct dentry * dentry)2366b5376771SSerge E. Hallyn int security_inode_need_killpriv(struct dentry *dentry)
2367b5376771SSerge E. Hallyn {
2368f25fce3eSCasey Schaufler 	return call_int_hook(inode_need_killpriv, 0, dentry);
2369b5376771SSerge E. Hallyn }
2370b5376771SSerge E. Hallyn 
2371916e3258SPaul Moore /**
2372916e3258SPaul Moore  * security_inode_killpriv() - The setuid bit is removed, update LSM state
2373916e3258SPaul Moore  * @idmap: idmap of the mount
2374916e3258SPaul Moore  * @dentry: associated dentry
2375916e3258SPaul Moore  *
2376916e3258SPaul Moore  * The @dentry's setuid bit is being removed.  Remove similar security labels.
2377916e3258SPaul Moore  * Called with the dentry->d_inode->i_mutex held.
2378916e3258SPaul Moore  *
2379916e3258SPaul Moore  * Return: Return 0 on success.  If error is returned, then the operation
2380916e3258SPaul Moore  *         causing setuid bit removal is failed.
2381916e3258SPaul Moore  */
security_inode_killpriv(struct mnt_idmap * idmap,struct dentry * dentry)238239f60c1cSChristian Brauner int security_inode_killpriv(struct mnt_idmap *idmap,
238371bc356fSChristian Brauner 			    struct dentry *dentry)
2384b5376771SSerge E. Hallyn {
238539f60c1cSChristian Brauner 	return call_int_hook(inode_killpriv, 0, idmap, dentry);
2386b5376771SSerge E. Hallyn }
2387b5376771SSerge E. Hallyn 
2388916e3258SPaul Moore /**
2389916e3258SPaul Moore  * security_inode_getsecurity() - Get the xattr security label of an inode
2390916e3258SPaul Moore  * @idmap: idmap of the mount
2391916e3258SPaul Moore  * @inode: inode
2392916e3258SPaul Moore  * @name: xattr name
2393916e3258SPaul Moore  * @buffer: security label buffer
2394916e3258SPaul Moore  * @alloc: allocation flag
2395916e3258SPaul Moore  *
2396916e3258SPaul Moore  * Retrieve a copy of the extended attribute representation of the security
2397916e3258SPaul Moore  * label associated with @name for @inode via @buffer.  Note that @name is the
2398916e3258SPaul Moore  * remainder of the attribute name after the security prefix has been removed.
2399916e3258SPaul Moore  * @alloc is used to specify if the call should return a value via the buffer
2400916e3258SPaul Moore  * or just the value length.
2401916e3258SPaul Moore  *
2402916e3258SPaul Moore  * Return: Returns size of buffer on success.
2403916e3258SPaul Moore  */
security_inode_getsecurity(struct mnt_idmap * idmap,struct inode * inode,const char * name,void ** buffer,bool alloc)24044609e1f1SChristian Brauner int security_inode_getsecurity(struct mnt_idmap *idmap,
240571bc356fSChristian Brauner 			       struct inode *inode, const char *name,
240671bc356fSChristian Brauner 			       void **buffer, bool alloc)
240720510f2fSJames Morris {
24082885c1e3SCasey Schaufler 	struct security_hook_list *hp;
24092885c1e3SCasey Schaufler 	int rc;
24102885c1e3SCasey Schaufler 
241120510f2fSJames Morris 	if (unlikely(IS_PRIVATE(inode)))
241298e828a0SKP Singh 		return LSM_RET_DEFAULT(inode_getsecurity);
24132885c1e3SCasey Schaufler 	/*
24142885c1e3SCasey Schaufler 	 * Only one module will provide an attribute with a given name.
24152885c1e3SCasey Schaufler 	 */
2416df0ce173SSargun Dhillon 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
241763c1845bSPaul Moore 		rc = hp->hook.inode_getsecurity(idmap, inode, name, buffer,
241863c1845bSPaul Moore 						alloc);
241998e828a0SKP Singh 		if (rc != LSM_RET_DEFAULT(inode_getsecurity))
24202885c1e3SCasey Schaufler 			return rc;
24212885c1e3SCasey Schaufler 	}
242298e828a0SKP Singh 	return LSM_RET_DEFAULT(inode_getsecurity);
242320510f2fSJames Morris }
242420510f2fSJames Morris 
2425916e3258SPaul Moore /**
2426916e3258SPaul Moore  * security_inode_setsecurity() - Set the xattr security label of an inode
2427916e3258SPaul Moore  * @inode: inode
2428916e3258SPaul Moore  * @name: xattr name
2429916e3258SPaul Moore  * @value: security label
2430916e3258SPaul Moore  * @size: length of security label
2431916e3258SPaul Moore  * @flags: flags
2432916e3258SPaul Moore  *
2433916e3258SPaul Moore  * Set the security label associated with @name for @inode from the extended
2434916e3258SPaul Moore  * attribute value @value.  @size indicates the size of the @value in bytes.
2435916e3258SPaul Moore  * @flags may be XATTR_CREATE, XATTR_REPLACE, or 0. Note that @name is the
2436916e3258SPaul Moore  * remainder of the attribute name after the security. prefix has been removed.
2437916e3258SPaul Moore  *
2438916e3258SPaul Moore  * Return: Returns 0 on success.
2439916e3258SPaul Moore  */
security_inode_setsecurity(struct inode * inode,const char * name,const void * value,size_t size,int flags)244063c1845bSPaul Moore int security_inode_setsecurity(struct inode *inode, const char *name,
244163c1845bSPaul Moore 			       const void *value, size_t size, int flags)
244220510f2fSJames Morris {
24432885c1e3SCasey Schaufler 	struct security_hook_list *hp;
24442885c1e3SCasey Schaufler 	int rc;
24452885c1e3SCasey Schaufler 
244620510f2fSJames Morris 	if (unlikely(IS_PRIVATE(inode)))
244798e828a0SKP Singh 		return LSM_RET_DEFAULT(inode_setsecurity);
24482885c1e3SCasey Schaufler 	/*
24492885c1e3SCasey Schaufler 	 * Only one module will provide an attribute with a given name.
24502885c1e3SCasey Schaufler 	 */
2451df0ce173SSargun Dhillon 	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
24522885c1e3SCasey Schaufler 		rc = hp->hook.inode_setsecurity(inode, name, value, size,
24532885c1e3SCasey Schaufler 						flags);
245498e828a0SKP Singh 		if (rc != LSM_RET_DEFAULT(inode_setsecurity))
24552885c1e3SCasey Schaufler 			return rc;
24562885c1e3SCasey Schaufler 	}
245798e828a0SKP Singh 	return LSM_RET_DEFAULT(inode_setsecurity);
245820510f2fSJames Morris }
245920510f2fSJames Morris 
2460916e3258SPaul Moore /**
2461916e3258SPaul Moore  * security_inode_listsecurity() - List the xattr security label names
2462916e3258SPaul Moore  * @inode: inode
2463916e3258SPaul Moore  * @buffer: buffer
2464916e3258SPaul Moore  * @buffer_size: size of buffer
2465916e3258SPaul Moore  *
2466916e3258SPaul Moore  * Copy the extended attribute names for the security labels associated with
2467916e3258SPaul Moore  * @inode into @buffer.  The maximum size of @buffer is specified by
2468916e3258SPaul Moore  * @buffer_size.  @buffer may be NULL to request the size of the buffer
2469916e3258SPaul Moore  * required.
2470916e3258SPaul Moore  *
2471916e3258SPaul Moore  * Return: Returns number of bytes used/required on success.
2472916e3258SPaul Moore  */
security_inode_listsecurity(struct inode * inode,char * buffer,size_t buffer_size)247363c1845bSPaul Moore int security_inode_listsecurity(struct inode *inode,
247463c1845bSPaul Moore 				char *buffer, size_t buffer_size)
247520510f2fSJames Morris {
247620510f2fSJames Morris 	if (unlikely(IS_PRIVATE(inode)))
247720510f2fSJames Morris 		return 0;
2478f25fce3eSCasey Schaufler 	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
247920510f2fSJames Morris }
2480c9bccef6SDavid Quigley EXPORT_SYMBOL(security_inode_listsecurity);
248120510f2fSJames Morris 
2482916e3258SPaul Moore /**
2483916e3258SPaul Moore  * security_inode_getsecid() - Get an inode's secid
2484916e3258SPaul Moore  * @inode: inode
2485916e3258SPaul Moore  * @secid: secid to return
2486916e3258SPaul Moore  *
2487916e3258SPaul Moore  * Get the secid associated with the node.  In case of failure, @secid will be
2488916e3258SPaul Moore  * set to zero.
2489916e3258SPaul Moore  */
security_inode_getsecid(struct inode * inode,u32 * secid)2490d6335d77SAndreas Gruenbacher void security_inode_getsecid(struct inode *inode, u32 *secid)
24918a076191SAhmed S. Darwish {
2492f25fce3eSCasey Schaufler 	call_void_hook(inode_getsecid, inode, secid);
24938a076191SAhmed S. Darwish }
24948a076191SAhmed S. Darwish 
2495916e3258SPaul Moore /**
2496916e3258SPaul Moore  * security_inode_copy_up() - Create new creds for an overlayfs copy-up op
2497916e3258SPaul Moore  * @src: union dentry of copy-up file
2498916e3258SPaul Moore  * @new: newly created creds
2499916e3258SPaul Moore  *
2500916e3258SPaul Moore  * A file is about to be copied up from lower layer to upper layer of overlay
2501916e3258SPaul Moore  * filesystem. Security module can prepare a set of new creds and modify as
2502916e3258SPaul Moore  * need be and return new creds. Caller will switch to new creds temporarily to
2503916e3258SPaul Moore  * create new file and release newly allocated creds.
2504916e3258SPaul Moore  *
2505916e3258SPaul Moore  * Return: Returns 0 on success or a negative error code on error.
2506916e3258SPaul Moore  */
security_inode_copy_up(struct dentry * src,struct cred ** new)2507d8ad8b49SVivek Goyal int security_inode_copy_up(struct dentry *src, struct cred **new)
2508d8ad8b49SVivek Goyal {
2509d8ad8b49SVivek Goyal 	return call_int_hook(inode_copy_up, 0, src, new);
2510d8ad8b49SVivek Goyal }
2511d8ad8b49SVivek Goyal EXPORT_SYMBOL(security_inode_copy_up);
2512d8ad8b49SVivek Goyal 
2513916e3258SPaul Moore /**
2514916e3258SPaul Moore  * security_inode_copy_up_xattr() - Filter xattrs in an overlayfs copy-up op
2515916e3258SPaul Moore  * @name: xattr name
2516916e3258SPaul Moore  *
2517916e3258SPaul Moore  * Filter the xattrs being copied up when a unioned file is copied up from a
2518916e3258SPaul Moore  * lower layer to the union/overlay layer.   The caller is responsible for
2519916e3258SPaul Moore  * reading and writing the xattrs, this hook is merely a filter.
2520916e3258SPaul Moore  *
2521916e3258SPaul Moore  * Return: Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP
2522916e3258SPaul Moore  *         if the security module does not know about attribute, or a negative
2523916e3258SPaul Moore  *         error code to abort the copy up.
2524916e3258SPaul Moore  */
security_inode_copy_up_xattr(const char * name)2525121ab822SVivek Goyal int security_inode_copy_up_xattr(const char *name)
2526121ab822SVivek Goyal {
252723e390cdSKP Singh 	struct security_hook_list *hp;
252823e390cdSKP Singh 	int rc;
252923e390cdSKP Singh 
253023e390cdSKP Singh 	/*
253123e390cdSKP Singh 	 * The implementation can return 0 (accept the xattr), 1 (discard the
253223e390cdSKP Singh 	 * xattr), -EOPNOTSUPP if it does not know anything about the xattr or
253323e390cdSKP Singh 	 * any other error code in case of an error.
253423e390cdSKP Singh 	 */
253523e390cdSKP Singh 	hlist_for_each_entry(hp,
253623e390cdSKP Singh 			     &security_hook_heads.inode_copy_up_xattr, list) {
253723e390cdSKP Singh 		rc = hp->hook.inode_copy_up_xattr(name);
253823e390cdSKP Singh 		if (rc != LSM_RET_DEFAULT(inode_copy_up_xattr))
253923e390cdSKP Singh 			return rc;
254023e390cdSKP Singh 	}
254123e390cdSKP Singh 
2542*7a3e7f1eSMimi Zohar 	return evm_inode_copy_up_xattr(name);
2543121ab822SVivek Goyal }
2544121ab822SVivek Goyal EXPORT_SYMBOL(security_inode_copy_up_xattr);
2545121ab822SVivek Goyal 
25469348944bSPaul Moore /**
25479348944bSPaul Moore  * security_kernfs_init_security() - Init LSM context for a kernfs node
25489348944bSPaul Moore  * @kn_dir: parent kernfs node
25499348944bSPaul Moore  * @kn: the kernfs node to initialize
25509348944bSPaul Moore  *
25519348944bSPaul Moore  * Initialize the security context of a newly created kernfs node based on its
25529348944bSPaul Moore  * own and its parent's attributes.
25539348944bSPaul Moore  *
25549348944bSPaul Moore  * Return: Returns 0 if permission is granted.
25559348944bSPaul Moore  */
security_kernfs_init_security(struct kernfs_node * kn_dir,struct kernfs_node * kn)2556b230d5abSOndrej Mosnacek int security_kernfs_init_security(struct kernfs_node *kn_dir,
2557b230d5abSOndrej Mosnacek 				  struct kernfs_node *kn)
2558b230d5abSOndrej Mosnacek {
2559b230d5abSOndrej Mosnacek 	return call_int_hook(kernfs_init_security, 0, kn_dir, kn);
2560b230d5abSOndrej Mosnacek }
2561b230d5abSOndrej Mosnacek 
2562a0fd6480SPaul Moore /**
2563a0fd6480SPaul Moore  * security_file_permission() - Check file permissions
2564a0fd6480SPaul Moore  * @file: file
2565a0fd6480SPaul Moore  * @mask: requested permissions
2566a0fd6480SPaul Moore  *
2567a0fd6480SPaul Moore  * Check file permissions before accessing an open file.  This hook is called
2568a0fd6480SPaul Moore  * by various operations that read or write files.  A security module can use
2569a0fd6480SPaul Moore  * this hook to perform additional checking on these operations, e.g. to
2570a0fd6480SPaul Moore  * revalidate permissions on use to support privilege bracketing or policy
2571a0fd6480SPaul Moore  * changes.  Notice that this hook is used when the actual read/write
2572a0fd6480SPaul Moore  * operations are performed, whereas the inode_security_ops hook is called when
2573a0fd6480SPaul Moore  * a file is opened (as well as many other operations).  Although this hook can
2574a0fd6480SPaul Moore  * be used to revalidate permissions for various system call operations that
2575a0fd6480SPaul Moore  * read or write files, it does not address the revalidation of permissions for
2576a0fd6480SPaul Moore  * memory-mapped files.  Security modules must handle this separately if they
2577a0fd6480SPaul Moore  * need such revalidation.
2578a0fd6480SPaul Moore  *
2579a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2580a0fd6480SPaul Moore  */
security_file_permission(struct file * file,int mask)258120510f2fSJames Morris int security_file_permission(struct file *file, int mask)
258220510f2fSJames Morris {
2583c4ec54b4SEric Paris 	int ret;
2584c4ec54b4SEric Paris 
2585f25fce3eSCasey Schaufler 	ret = call_int_hook(file_permission, 0, file, mask);
2586c4ec54b4SEric Paris 	if (ret)
2587c4ec54b4SEric Paris 		return ret;
2588c4ec54b4SEric Paris 
2589c4ec54b4SEric Paris 	return fsnotify_perm(file, mask);
259020510f2fSJames Morris }
259120510f2fSJames Morris 
2592a0fd6480SPaul Moore /**
2593a0fd6480SPaul Moore  * security_file_alloc() - Allocate and init a file's LSM blob
2594a0fd6480SPaul Moore  * @file: the file
2595a0fd6480SPaul Moore  *
2596a0fd6480SPaul Moore  * Allocate and attach a security structure to the file->f_security field.  The
2597a0fd6480SPaul Moore  * security field is initialized to NULL when the structure is first created.
2598a0fd6480SPaul Moore  *
2599a0fd6480SPaul Moore  * Return: Return 0 if the hook is successful and permission is granted.
2600a0fd6480SPaul Moore  */
security_file_alloc(struct file * file)260120510f2fSJames Morris int security_file_alloc(struct file *file)
260220510f2fSJames Morris {
260333bf60caSCasey Schaufler 	int rc = lsm_file_alloc(file);
260433bf60caSCasey Schaufler 
260533bf60caSCasey Schaufler 	if (rc)
260633bf60caSCasey Schaufler 		return rc;
260733bf60caSCasey Schaufler 	rc = call_int_hook(file_alloc_security, 0, file);
260833bf60caSCasey Schaufler 	if (unlikely(rc))
260933bf60caSCasey Schaufler 		security_file_free(file);
261033bf60caSCasey Schaufler 	return rc;
261120510f2fSJames Morris }
261220510f2fSJames Morris 
2613a0fd6480SPaul Moore /**
2614a0fd6480SPaul Moore  * security_file_free() - Free a file's LSM blob
2615a0fd6480SPaul Moore  * @file: the file
2616a0fd6480SPaul Moore  *
2617a0fd6480SPaul Moore  * Deallocate and free any security structures stored in file->f_security.
2618a0fd6480SPaul Moore  */
security_file_free(struct file * file)261920510f2fSJames Morris void security_file_free(struct file *file)
262020510f2fSJames Morris {
262133bf60caSCasey Schaufler 	void *blob;
262233bf60caSCasey Schaufler 
2623f25fce3eSCasey Schaufler 	call_void_hook(file_free_security, file);
262433bf60caSCasey Schaufler 
262533bf60caSCasey Schaufler 	blob = file->f_security;
262633bf60caSCasey Schaufler 	if (blob) {
262733bf60caSCasey Schaufler 		file->f_security = NULL;
262833bf60caSCasey Schaufler 		kmem_cache_free(lsm_file_cache, blob);
262933bf60caSCasey Schaufler 	}
263020510f2fSJames Morris }
263120510f2fSJames Morris 
2632a0fd6480SPaul Moore /**
2633a0fd6480SPaul Moore  * security_file_ioctl() - Check if an ioctl is allowed
2634a0fd6480SPaul Moore  * @file: associated file
2635a0fd6480SPaul Moore  * @cmd: ioctl cmd
2636a0fd6480SPaul Moore  * @arg: ioctl arguments
2637a0fd6480SPaul Moore  *
2638a0fd6480SPaul Moore  * Check permission for an ioctl operation on @file.  Note that @arg sometimes
2639a0fd6480SPaul Moore  * represents a user space pointer; in other cases, it may be a simple integer
2640a0fd6480SPaul Moore  * value.  When @arg represents a user space pointer, it should never be used
2641a0fd6480SPaul Moore  * by the security module.
2642a0fd6480SPaul Moore  *
2643a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2644a0fd6480SPaul Moore  */
security_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)264520510f2fSJames Morris int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
264620510f2fSJames Morris {
2647f25fce3eSCasey Schaufler 	return call_int_hook(file_ioctl, 0, file, cmd, arg);
264820510f2fSJames Morris }
2649292f902aSMiklos Szeredi EXPORT_SYMBOL_GPL(security_file_ioctl);
265020510f2fSJames Morris 
2651820831deSAlfred Piccioni /**
2652820831deSAlfred Piccioni  * security_file_ioctl_compat() - Check if an ioctl is allowed in compat mode
2653820831deSAlfred Piccioni  * @file: associated file
2654820831deSAlfred Piccioni  * @cmd: ioctl cmd
2655820831deSAlfred Piccioni  * @arg: ioctl arguments
2656820831deSAlfred Piccioni  *
2657820831deSAlfred Piccioni  * Compat version of security_file_ioctl() that correctly handles 32-bit
2658820831deSAlfred Piccioni  * processes running on 64-bit kernels.
2659820831deSAlfred Piccioni  *
2660820831deSAlfred Piccioni  * Return: Returns 0 if permission is granted.
2661820831deSAlfred Piccioni  */
security_file_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)2662820831deSAlfred Piccioni int security_file_ioctl_compat(struct file *file, unsigned int cmd,
2663820831deSAlfred Piccioni 			       unsigned long arg)
2664820831deSAlfred Piccioni {
2665820831deSAlfred Piccioni 	return call_int_hook(file_ioctl_compat, 0, file, cmd, arg);
2666820831deSAlfred Piccioni }
2667820831deSAlfred Piccioni EXPORT_SYMBOL_GPL(security_file_ioctl_compat);
2668820831deSAlfred Piccioni 
mmap_prot(struct file * file,unsigned long prot)266998de59bfSAl Viro static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
267020510f2fSJames Morris {
26718b3ec681SAl Viro 	/*
267298de59bfSAl Viro 	 * Does we have PROT_READ and does the application expect
267398de59bfSAl Viro 	 * it to imply PROT_EXEC?  If not, nothing to talk about...
26748b3ec681SAl Viro 	 */
267598de59bfSAl Viro 	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
267698de59bfSAl Viro 		return prot;
26778b3ec681SAl Viro 	if (!(current->personality & READ_IMPLIES_EXEC))
267898de59bfSAl Viro 		return prot;
267998de59bfSAl Viro 	/*
268098de59bfSAl Viro 	 * if that's an anonymous mapping, let it.
268198de59bfSAl Viro 	 */
268298de59bfSAl Viro 	if (!file)
268398de59bfSAl Viro 		return prot | PROT_EXEC;
268498de59bfSAl Viro 	/*
268598de59bfSAl Viro 	 * ditto if it's not on noexec mount, except that on !MMU we need
2686b4caecd4SChristoph Hellwig 	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
268798de59bfSAl Viro 	 */
268890f8572bSEric W. Biederman 	if (!path_noexec(&file->f_path)) {
26898b3ec681SAl Viro #ifndef CONFIG_MMU
2690b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
2691b4caecd4SChristoph Hellwig 			unsigned caps = file->f_op->mmap_capabilities(file);
2692b4caecd4SChristoph Hellwig 			if (!(caps & NOMMU_MAP_EXEC))
269398de59bfSAl Viro 				return prot;
2694b4caecd4SChristoph Hellwig 		}
26958b3ec681SAl Viro #endif
269698de59bfSAl Viro 		return prot | PROT_EXEC;
26978b3ec681SAl Viro 	}
269898de59bfSAl Viro 	/* anything on noexec mount won't get PROT_EXEC */
269998de59bfSAl Viro 	return prot;
270098de59bfSAl Viro }
270198de59bfSAl Viro 
2702a0fd6480SPaul Moore /**
2703a0fd6480SPaul Moore  * security_mmap_file() - Check if mmap'ing a file is allowed
2704a0fd6480SPaul Moore  * @file: file
2705a0fd6480SPaul Moore  * @prot: protection applied by the kernel
2706a0fd6480SPaul Moore  * @flags: flags
2707a0fd6480SPaul Moore  *
2708a0fd6480SPaul Moore  * Check permissions for a mmap operation.  The @file may be NULL, e.g. if
2709a0fd6480SPaul Moore  * mapping anonymous memory.
2710a0fd6480SPaul Moore  *
2711a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2712a0fd6480SPaul Moore  */
security_mmap_file(struct file * file,unsigned long prot,unsigned long flags)271398de59bfSAl Viro int security_mmap_file(struct file *file, unsigned long prot,
271498de59bfSAl Viro 		       unsigned long flags)
271598de59bfSAl Viro {
27164971c268SRoberto Sassu 	unsigned long prot_adj = mmap_prot(file, prot);
271798de59bfSAl Viro 	int ret;
27184971c268SRoberto Sassu 
27194971c268SRoberto Sassu 	ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags);
27206c21a7fbSMimi Zohar 	if (ret)
27216c21a7fbSMimi Zohar 		return ret;
27224971c268SRoberto Sassu 	return ima_file_mmap(file, prot, prot_adj, flags);
272320510f2fSJames Morris }
272420510f2fSJames Morris 
2725a0fd6480SPaul Moore /**
2726a0fd6480SPaul Moore  * security_mmap_addr() - Check if mmap'ing an address is allowed
2727a0fd6480SPaul Moore  * @addr: address
2728a0fd6480SPaul Moore  *
2729a0fd6480SPaul Moore  * Check permissions for a mmap operation at @addr.
2730a0fd6480SPaul Moore  *
2731a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2732a0fd6480SPaul Moore  */
security_mmap_addr(unsigned long addr)2733e5467859SAl Viro int security_mmap_addr(unsigned long addr)
2734e5467859SAl Viro {
2735f25fce3eSCasey Schaufler 	return call_int_hook(mmap_addr, 0, addr);
2736e5467859SAl Viro }
2737e5467859SAl Viro 
2738a0fd6480SPaul Moore /**
2739a0fd6480SPaul Moore  * security_file_mprotect() - Check if changing memory protections is allowed
2740a0fd6480SPaul Moore  * @vma: memory region
2741a0fd6480SPaul Moore  * @reqprot: application requested protection
27421e2523d7SPaul Moore  * @prot: protection applied by the kernel
2743a0fd6480SPaul Moore  *
2744a0fd6480SPaul Moore  * Check permissions before changing memory access permissions.
2745a0fd6480SPaul Moore  *
2746a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2747a0fd6480SPaul Moore  */
security_file_mprotect(struct vm_area_struct * vma,unsigned long reqprot,unsigned long prot)274820510f2fSJames Morris int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
274920510f2fSJames Morris 			   unsigned long prot)
275020510f2fSJames Morris {
27518eb613c0SMimi Zohar 	int ret;
27528eb613c0SMimi Zohar 
27538eb613c0SMimi Zohar 	ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
27548eb613c0SMimi Zohar 	if (ret)
27558eb613c0SMimi Zohar 		return ret;
27568eb613c0SMimi Zohar 	return ima_file_mprotect(vma, prot);
275720510f2fSJames Morris }
275820510f2fSJames Morris 
2759a0fd6480SPaul Moore /**
2760a0fd6480SPaul Moore  * security_file_lock() - Check if a file lock is allowed
2761a0fd6480SPaul Moore  * @file: file
2762a0fd6480SPaul Moore  * @cmd: lock operation (e.g. F_RDLCK, F_WRLCK)
2763a0fd6480SPaul Moore  *
2764a0fd6480SPaul Moore  * Check permission before performing file locking operations.  Note the hook
2765a0fd6480SPaul Moore  * mediates both flock and fcntl style locks.
2766a0fd6480SPaul Moore  *
2767a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2768a0fd6480SPaul Moore  */
security_file_lock(struct file * file,unsigned int cmd)276920510f2fSJames Morris int security_file_lock(struct file *file, unsigned int cmd)
277020510f2fSJames Morris {
2771f25fce3eSCasey Schaufler 	return call_int_hook(file_lock, 0, file, cmd);
277220510f2fSJames Morris }
277320510f2fSJames Morris 
2774a0fd6480SPaul Moore /**
2775a0fd6480SPaul Moore  * security_file_fcntl() - Check if fcntl() op is allowed
2776a0fd6480SPaul Moore  * @file: file
2777ff72942cSPairman Guo  * @cmd: fcntl command
2778a0fd6480SPaul Moore  * @arg: command argument
2779a0fd6480SPaul Moore  *
2780a0fd6480SPaul Moore  * Check permission before allowing the file operation specified by @cmd from
2781a0fd6480SPaul Moore  * being performed on the file @file.  Note that @arg sometimes represents a
2782a0fd6480SPaul Moore  * user space pointer; in other cases, it may be a simple integer value.  When
2783a0fd6480SPaul Moore  * @arg represents a user space pointer, it should never be used by the
2784a0fd6480SPaul Moore  * security module.
2785a0fd6480SPaul Moore  *
2786a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2787a0fd6480SPaul Moore  */
security_file_fcntl(struct file * file,unsigned int cmd,unsigned long arg)278820510f2fSJames Morris int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
278920510f2fSJames Morris {
2790f25fce3eSCasey Schaufler 	return call_int_hook(file_fcntl, 0, file, cmd, arg);
279120510f2fSJames Morris }
279220510f2fSJames Morris 
2793a0fd6480SPaul Moore /**
2794a0fd6480SPaul Moore  * security_file_set_fowner() - Set the file owner info in the LSM blob
2795a0fd6480SPaul Moore  * @file: the file
2796a0fd6480SPaul Moore  *
2797a0fd6480SPaul Moore  * Save owner security information (typically from current->security) in
2798a0fd6480SPaul Moore  * file->f_security for later use by the send_sigiotask hook.
2799a0fd6480SPaul Moore  *
2800a0fd6480SPaul Moore  * Return: Returns 0 on success.
2801a0fd6480SPaul Moore  */
security_file_set_fowner(struct file * file)2802e0b93eddSJeff Layton void security_file_set_fowner(struct file *file)
280320510f2fSJames Morris {
2804f25fce3eSCasey Schaufler 	call_void_hook(file_set_fowner, file);
280520510f2fSJames Morris }
280620510f2fSJames Morris 
2807a0fd6480SPaul Moore /**
2808a0fd6480SPaul Moore  * security_file_send_sigiotask() - Check if sending SIGIO/SIGURG is allowed
2809a0fd6480SPaul Moore  * @tsk: target task
2810a0fd6480SPaul Moore  * @fown: signal sender
2811a0fd6480SPaul Moore  * @sig: signal to be sent, SIGIO is sent if 0
2812a0fd6480SPaul Moore  *
2813a0fd6480SPaul Moore  * Check permission for the file owner @fown to send SIGIO or SIGURG to the
2814a0fd6480SPaul Moore  * process @tsk.  Note that this hook is sometimes called from interrupt.  Note
2815a0fd6480SPaul Moore  * that the fown_struct, @fown, is never outside the context of a struct file,
2816a0fd6480SPaul Moore  * so the file structure (and associated security information) can always be
2817a0fd6480SPaul Moore  * obtained: container_of(fown, struct file, f_owner).
2818a0fd6480SPaul Moore  *
2819a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2820a0fd6480SPaul Moore  */
security_file_send_sigiotask(struct task_struct * tsk,struct fown_struct * fown,int sig)282120510f2fSJames Morris int security_file_send_sigiotask(struct task_struct *tsk,
282220510f2fSJames Morris 				 struct fown_struct *fown, int sig)
282320510f2fSJames Morris {
2824f25fce3eSCasey Schaufler 	return call_int_hook(file_send_sigiotask, 0, tsk, fown, sig);
282520510f2fSJames Morris }
282620510f2fSJames Morris 
2827a0fd6480SPaul Moore /**
2828a0fd6480SPaul Moore  * security_file_receive() - Check is receiving a file via IPC is allowed
2829a0fd6480SPaul Moore  * @file: file being received
2830a0fd6480SPaul Moore  *
2831a0fd6480SPaul Moore  * This hook allows security modules to control the ability of a process to
2832a0fd6480SPaul Moore  * receive an open file descriptor via socket IPC.
2833a0fd6480SPaul Moore  *
2834a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2835a0fd6480SPaul Moore  */
security_file_receive(struct file * file)283620510f2fSJames Morris int security_file_receive(struct file *file)
283720510f2fSJames Morris {
2838f25fce3eSCasey Schaufler 	return call_int_hook(file_receive, 0, file);
283920510f2fSJames Morris }
284020510f2fSJames Morris 
2841a0fd6480SPaul Moore /**
2842a0fd6480SPaul Moore  * security_file_open() - Save open() time state for late use by the LSM
2843a0fd6480SPaul Moore  * @file:
2844a0fd6480SPaul Moore  *
2845a0fd6480SPaul Moore  * Save open-time permission checking state for later use upon file_permission,
2846a0fd6480SPaul Moore  * and recheck access if anything has changed since inode_permission.
2847a0fd6480SPaul Moore  *
2848a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2849a0fd6480SPaul Moore  */
security_file_open(struct file * file)2850e3f20ae2SAl Viro int security_file_open(struct file *file)
285120510f2fSJames Morris {
2852c4ec54b4SEric Paris 	int ret;
2853c4ec54b4SEric Paris 
285494817692SAl Viro 	ret = call_int_hook(file_open, 0, file);
2855c4ec54b4SEric Paris 	if (ret)
2856c4ec54b4SEric Paris 		return ret;
2857c4ec54b4SEric Paris 
2858c4ec54b4SEric Paris 	return fsnotify_perm(file, MAY_OPEN);
285920510f2fSJames Morris }
286020510f2fSJames Morris 
2861a0fd6480SPaul Moore /**
2862a0fd6480SPaul Moore  * security_file_truncate() - Check if truncating a file is allowed
2863a0fd6480SPaul Moore  * @file: file
2864a0fd6480SPaul Moore  *
2865a0fd6480SPaul Moore  * Check permission before truncating a file, i.e. using ftruncate.  Note that
2866a0fd6480SPaul Moore  * truncation permission may also be checked based on the path, using the
2867a0fd6480SPaul Moore  * @path_truncate hook.
2868a0fd6480SPaul Moore  *
2869a0fd6480SPaul Moore  * Return: Returns 0 if permission is granted.
2870a0fd6480SPaul Moore  */
security_file_truncate(struct file * file)28713350607dSGünther Noack int security_file_truncate(struct file *file)
28723350607dSGünther Noack {
28733350607dSGünther Noack 	return call_int_hook(file_truncate, 0, file);
28743350607dSGünther Noack }
28753350607dSGünther Noack 
2876130c53bfSPaul Moore /**
2877130c53bfSPaul Moore  * security_task_alloc() - Allocate a task's LSM blob
2878130c53bfSPaul Moore  * @task: the task
2879130c53bfSPaul Moore  * @clone_flags: flags indicating what is being shared
2880130c53bfSPaul Moore  *
2881130c53bfSPaul Moore  * Handle allocation of task-related resources.
2882130c53bfSPaul Moore  *
2883130c53bfSPaul Moore  * Return: Returns a zero on success, negative values on failure.
2884130c53bfSPaul Moore  */
security_task_alloc(struct task_struct * task,unsigned long clone_flags)2885e4e55b47STetsuo Handa int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
2886e4e55b47STetsuo Handa {
2887f4ad8f2cSCasey Schaufler 	int rc = lsm_task_alloc(task);
2888f4ad8f2cSCasey Schaufler 
2889f4ad8f2cSCasey Schaufler 	if (rc)
2890f4ad8f2cSCasey Schaufler 		return rc;
2891f4ad8f2cSCasey Schaufler 	rc = call_int_hook(task_alloc, 0, task, clone_flags);
2892f4ad8f2cSCasey Schaufler 	if (unlikely(rc))
2893f4ad8f2cSCasey Schaufler 		security_task_free(task);
2894f4ad8f2cSCasey Schaufler 	return rc;
2895e4e55b47STetsuo Handa }
2896e4e55b47STetsuo Handa 
2897130c53bfSPaul Moore /**
2898130c53bfSPaul Moore  * security_task_free() - Free a task's LSM blob and related resources
2899130c53bfSPaul Moore  * @task: task
2900130c53bfSPaul Moore  *
2901130c53bfSPaul Moore  * Handle release of task-related resources.  Note that this can be called from
2902130c53bfSPaul Moore  * interrupt context.
2903130c53bfSPaul Moore  */
security_task_free(struct task_struct * task)29041a2a4d06SKees Cook void security_task_free(struct task_struct *task)
29051a2a4d06SKees Cook {
2906f25fce3eSCasey Schaufler 	call_void_hook(task_free, task);
2907f4ad8f2cSCasey Schaufler 
2908f4ad8f2cSCasey Schaufler 	kfree(task->security);
2909f4ad8f2cSCasey Schaufler 	task->security = NULL;
29101a2a4d06SKees Cook }
29111a2a4d06SKees Cook 
2912130c53bfSPaul Moore /**
2913130c53bfSPaul Moore  * security_cred_alloc_blank() - Allocate the min memory to allow cred_transfer
2914130c53bfSPaul Moore  * @cred: credentials
2915130c53bfSPaul Moore  * @gfp: gfp flags
2916130c53bfSPaul Moore  *
2917130c53bfSPaul Moore  * Only allocate sufficient memory and attach to @cred such that
2918130c53bfSPaul Moore  * cred_transfer() will not get ENOMEM.
2919130c53bfSPaul Moore  *
2920130c53bfSPaul Moore  * Return: Returns 0 on success, negative values on failure.
2921130c53bfSPaul Moore  */
security_cred_alloc_blank(struct cred * cred,gfp_t gfp)2922ee18d64cSDavid Howells int security_cred_alloc_blank(struct cred *cred, gfp_t gfp)
2923ee18d64cSDavid Howells {
2924bbd3662aSCasey Schaufler 	int rc = lsm_cred_alloc(cred, gfp);
2925bbd3662aSCasey Schaufler 
2926bbd3662aSCasey Schaufler 	if (rc)
2927bbd3662aSCasey Schaufler 		return rc;
2928bbd3662aSCasey Schaufler 
2929bbd3662aSCasey Schaufler 	rc = call_int_hook(cred_alloc_blank, 0, cred, gfp);
293033bf60caSCasey Schaufler 	if (unlikely(rc))
2931bbd3662aSCasey Schaufler 		security_cred_free(cred);
2932bbd3662aSCasey Schaufler 	return rc;
2933ee18d64cSDavid Howells }
2934ee18d64cSDavid Howells 
2935130c53bfSPaul Moore /**
2936130c53bfSPaul Moore  * security_cred_free() - Free the cred's LSM blob and associated resources
2937130c53bfSPaul Moore  * @cred: credentials
2938130c53bfSPaul Moore  *
2939130c53bfSPaul Moore  * Deallocate and clear the cred->security field in a set of credentials.
2940130c53bfSPaul Moore  */
security_cred_free(struct cred * cred)2941f1752eecSDavid Howells void security_cred_free(struct cred *cred)
294220510f2fSJames Morris {
2943a5795fd3SJames Morris 	/*
2944a5795fd3SJames Morris 	 * There is a failure case in prepare_creds() that
2945a5795fd3SJames Morris 	 * may result in a call here with ->security being NULL.
2946a5795fd3SJames Morris 	 */
2947a5795fd3SJames Morris 	if (unlikely(cred->security == NULL))
2948a5795fd3SJames Morris 		return;
2949a5795fd3SJames Morris 
2950f25fce3eSCasey Schaufler 	call_void_hook(cred_free, cred);
2951bbd3662aSCasey Schaufler 
2952bbd3662aSCasey Schaufler 	kfree(cred->security);
2953bbd3662aSCasey Schaufler 	cred->security = NULL;
295420510f2fSJames Morris }
295520510f2fSJames Morris 
2956130c53bfSPaul Moore /**
2957130c53bfSPaul Moore  * security_prepare_creds() - Prepare a new set of credentials
2958130c53bfSPaul Moore  * @new: new credentials
2959130c53bfSPaul Moore  * @old: original credentials
2960130c53bfSPaul Moore  * @gfp: gfp flags
2961130c53bfSPaul Moore  *
2962130c53bfSPaul Moore  * Prepare a new set of credentials by copying the data from the old set.
2963130c53bfSPaul Moore  *
2964130c53bfSPaul Moore  * Return: Returns 0 on success, negative values on failure.
2965130c53bfSPaul Moore  */
security_prepare_creds(struct cred * new,const struct cred * old,gfp_t gfp)2966d84f4f99SDavid Howells int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp)
2967d84f4f99SDavid Howells {
2968bbd3662aSCasey Schaufler 	int rc = lsm_cred_alloc(new, gfp);
2969bbd3662aSCasey Schaufler 
2970bbd3662aSCasey Schaufler 	if (rc)
2971bbd3662aSCasey Schaufler 		return rc;
2972bbd3662aSCasey Schaufler 
2973bbd3662aSCasey Schaufler 	rc = call_int_hook(cred_prepare, 0, new, old, gfp);
297433bf60caSCasey Schaufler 	if (unlikely(rc))
2975bbd3662aSCasey Schaufler 		security_cred_free(new);
2976bbd3662aSCasey Schaufler 	return rc;
2977d84f4f99SDavid Howells }
2978d84f4f99SDavid Howells 
2979130c53bfSPaul Moore /**
2980130c53bfSPaul Moore  * security_transfer_creds() - Transfer creds
2981130c53bfSPaul Moore  * @new: target credentials
2982130c53bfSPaul Moore  * @old: original credentials
2983130c53bfSPaul Moore  *
2984130c53bfSPaul Moore  * Transfer data from original creds to new creds.
2985130c53bfSPaul Moore  */
security_transfer_creds(struct cred * new,const struct cred * old)2986ee18d64cSDavid Howells void security_transfer_creds(struct cred *new, const struct cred *old)
2987ee18d64cSDavid Howells {
2988f25fce3eSCasey Schaufler 	call_void_hook(cred_transfer, new, old);
2989ee18d64cSDavid Howells }
2990ee18d64cSDavid Howells 
2991130c53bfSPaul Moore /**
2992130c53bfSPaul Moore  * security_cred_getsecid() - Get the secid from a set of credentials
2993130c53bfSPaul Moore  * @c: credentials
2994130c53bfSPaul Moore  * @secid: secid value
2995130c53bfSPaul Moore  *
2996130c53bfSPaul Moore  * Retrieve the security identifier of the cred structure @c.  In case of
2997130c53bfSPaul Moore  * failure, @secid will be set to zero.
2998130c53bfSPaul Moore  */
security_cred_getsecid(const struct cred * c,u32 * secid)29993ec30113SMatthew Garrett void security_cred_getsecid(const struct cred *c, u32 *secid)
30003ec30113SMatthew Garrett {
30013ec30113SMatthew Garrett 	*secid = 0;
30023ec30113SMatthew Garrett 	call_void_hook(cred_getsecid, c, secid);
30033ec30113SMatthew Garrett }
30043ec30113SMatthew Garrett EXPORT_SYMBOL(security_cred_getsecid);
30053ec30113SMatthew Garrett 
3006130c53bfSPaul Moore /**
3007130c53bfSPaul Moore  * security_kernel_act_as() - Set the kernel credentials to act as secid
3008130c53bfSPaul Moore  * @new: credentials
3009130c53bfSPaul Moore  * @secid: secid
3010130c53bfSPaul Moore  *
3011130c53bfSPaul Moore  * Set the credentials for a kernel service to act as (subjective context).
3012130c53bfSPaul Moore  * The current task must be the one that nominated @secid.
3013130c53bfSPaul Moore  *
3014130c53bfSPaul Moore  * Return: Returns 0 if successful.
3015130c53bfSPaul Moore  */
security_kernel_act_as(struct cred * new,u32 secid)30163a3b7ce9SDavid Howells int security_kernel_act_as(struct cred *new, u32 secid)
30173a3b7ce9SDavid Howells {
3018f25fce3eSCasey Schaufler 	return call_int_hook(kernel_act_as, 0, new, secid);
30193a3b7ce9SDavid Howells }
30203a3b7ce9SDavid Howells 
3021130c53bfSPaul Moore /**
3022130c53bfSPaul Moore  * security_kernel_create_files_as() - Set file creation context using an inode
3023130c53bfSPaul Moore  * @new: target credentials
3024130c53bfSPaul Moore  * @inode: reference inode
3025130c53bfSPaul Moore  *
3026130c53bfSPaul Moore  * Set the file creation context in a set of credentials to be the same as the
3027130c53bfSPaul Moore  * objective context of the specified inode.  The current task must be the one
3028130c53bfSPaul Moore  * that nominated @inode.
3029130c53bfSPaul Moore  *
3030130c53bfSPaul Moore  * Return: Returns 0 if successful.
3031130c53bfSPaul Moore  */
security_kernel_create_files_as(struct cred * new,struct inode * inode)30323a3b7ce9SDavid Howells int security_kernel_create_files_as(struct cred *new, struct inode *inode)
30333a3b7ce9SDavid Howells {
3034f25fce3eSCasey Schaufler 	return call_int_hook(kernel_create_files_as, 0, new, inode);
30353a3b7ce9SDavid Howells }
30363a3b7ce9SDavid Howells 
3037130c53bfSPaul Moore /**
3038130c53bfSPaul Moore  * security_kernel_module_request() - Check is loading a module is allowed
3039130c53bfSPaul Moore  * @kmod_name: module name
3040130c53bfSPaul Moore  *
3041130c53bfSPaul Moore  * Ability to trigger the kernel to automatically upcall to userspace for
3042130c53bfSPaul Moore  * userspace to load a kernel module with the given name.
3043130c53bfSPaul Moore  *
3044130c53bfSPaul Moore  * Return: Returns 0 if successful.
3045130c53bfSPaul Moore  */
security_kernel_module_request(char * kmod_name)3046dd8dbf2eSEric Paris int security_kernel_module_request(char *kmod_name)
30479188499cSEric Paris {
30486eb864c1SMikhail Kurinnoi 	int ret;
30496eb864c1SMikhail Kurinnoi 
30506eb864c1SMikhail Kurinnoi 	ret = call_int_hook(kernel_module_request, 0, kmod_name);
30516eb864c1SMikhail Kurinnoi 	if (ret)
30526eb864c1SMikhail Kurinnoi 		return ret;
30536eb864c1SMikhail Kurinnoi 	return integrity_kernel_module_request(kmod_name);
30549188499cSEric Paris }
30559188499cSEric Paris 
3056130c53bfSPaul Moore /**
3057130c53bfSPaul Moore  * security_kernel_read_file() - Read a file specified by userspace
3058130c53bfSPaul Moore  * @file: file
3059130c53bfSPaul Moore  * @id: file identifier
3060130c53bfSPaul Moore  * @contents: trust if security_kernel_post_read_file() will be called
3061130c53bfSPaul Moore  *
3062130c53bfSPaul Moore  * Read a file specified by userspace.
3063130c53bfSPaul Moore  *
3064130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3065130c53bfSPaul Moore  */
security_kernel_read_file(struct file * file,enum kernel_read_file_id id,bool contents)30662039bda1SKees Cook int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
30672039bda1SKees Cook 			      bool contents)
306839eeb4fbSMimi Zohar {
306939eeb4fbSMimi Zohar 	int ret;
307039eeb4fbSMimi Zohar 
30712039bda1SKees Cook 	ret = call_int_hook(kernel_read_file, 0, file, id, contents);
307239eeb4fbSMimi Zohar 	if (ret)
307339eeb4fbSMimi Zohar 		return ret;
30742039bda1SKees Cook 	return ima_read_file(file, id, contents);
307539eeb4fbSMimi Zohar }
307639eeb4fbSMimi Zohar EXPORT_SYMBOL_GPL(security_kernel_read_file);
307739eeb4fbSMimi Zohar 
3078130c53bfSPaul Moore /**
3079130c53bfSPaul Moore  * security_kernel_post_read_file() - Read a file specified by userspace
3080130c53bfSPaul Moore  * @file: file
3081130c53bfSPaul Moore  * @buf: file contents
3082130c53bfSPaul Moore  * @size: size of file contents
3083130c53bfSPaul Moore  * @id: file identifier
3084130c53bfSPaul Moore  *
3085130c53bfSPaul Moore  * Read a file specified by userspace.  This must be paired with a prior call
3086130c53bfSPaul Moore  * to security_kernel_read_file() call that indicated this hook would also be
3087130c53bfSPaul Moore  * called, see security_kernel_read_file() for more information.
3088130c53bfSPaul Moore  *
3089130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3090130c53bfSPaul Moore  */
security_kernel_post_read_file(struct file * file,char * buf,loff_t size,enum kernel_read_file_id id)3091bc8ca5b9SMimi Zohar int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
3092bc8ca5b9SMimi Zohar 				   enum kernel_read_file_id id)
3093b44a7dfcSMimi Zohar {
3094cf222217SMimi Zohar 	int ret;
3095cf222217SMimi Zohar 
3096cf222217SMimi Zohar 	ret = call_int_hook(kernel_post_read_file, 0, file, buf, size, id);
3097cf222217SMimi Zohar 	if (ret)
3098cf222217SMimi Zohar 		return ret;
3099cf222217SMimi Zohar 	return ima_post_read_file(file, buf, size, id);
3100b44a7dfcSMimi Zohar }
3101b44a7dfcSMimi Zohar EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
3102b44a7dfcSMimi Zohar 
3103130c53bfSPaul Moore /**
3104130c53bfSPaul Moore  * security_kernel_load_data() - Load data provided by userspace
3105130c53bfSPaul Moore  * @id: data identifier
3106130c53bfSPaul Moore  * @contents: true if security_kernel_post_load_data() will be called
3107130c53bfSPaul Moore  *
3108130c53bfSPaul Moore  * Load data provided by userspace.
3109130c53bfSPaul Moore  *
3110130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3111130c53bfSPaul Moore  */
security_kernel_load_data(enum kernel_load_data_id id,bool contents)3112b64fcae7SKees Cook int security_kernel_load_data(enum kernel_load_data_id id, bool contents)
3113377179cdSMimi Zohar {
311416c267aaSMimi Zohar 	int ret;
311516c267aaSMimi Zohar 
3116b64fcae7SKees Cook 	ret = call_int_hook(kernel_load_data, 0, id, contents);
311716c267aaSMimi Zohar 	if (ret)
311816c267aaSMimi Zohar 		return ret;
3119b64fcae7SKees Cook 	return ima_load_data(id, contents);
3120377179cdSMimi Zohar }
312183a68a06SArnd Bergmann EXPORT_SYMBOL_GPL(security_kernel_load_data);
3122377179cdSMimi Zohar 
3123130c53bfSPaul Moore /**
3124130c53bfSPaul Moore  * security_kernel_post_load_data() - Load userspace data from a non-file source
3125130c53bfSPaul Moore  * @buf: data
3126130c53bfSPaul Moore  * @size: size of data
3127130c53bfSPaul Moore  * @id: data identifier
3128130c53bfSPaul Moore  * @description: text description of data, specific to the id value
3129130c53bfSPaul Moore  *
3130130c53bfSPaul Moore  * Load data provided by a non-file source (usually userspace buffer).  This
3131130c53bfSPaul Moore  * must be paired with a prior security_kernel_load_data() call that indicated
3132130c53bfSPaul Moore  * this hook would also be called, see security_kernel_load_data() for more
3133130c53bfSPaul Moore  * information.
3134130c53bfSPaul Moore  *
3135130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3136130c53bfSPaul Moore  */
security_kernel_post_load_data(char * buf,loff_t size,enum kernel_load_data_id id,char * description)3137b64fcae7SKees Cook int security_kernel_post_load_data(char *buf, loff_t size,
3138b64fcae7SKees Cook 				   enum kernel_load_data_id id,
3139b64fcae7SKees Cook 				   char *description)
3140b64fcae7SKees Cook {
3141b64fcae7SKees Cook 	int ret;
3142b64fcae7SKees Cook 
3143b64fcae7SKees Cook 	ret = call_int_hook(kernel_post_load_data, 0, buf, size, id,
3144b64fcae7SKees Cook 			    description);
3145b64fcae7SKees Cook 	if (ret)
3146b64fcae7SKees Cook 		return ret;
3147b64fcae7SKees Cook 	return ima_post_load_data(buf, size, id, description);
3148b64fcae7SKees Cook }
3149b64fcae7SKees Cook EXPORT_SYMBOL_GPL(security_kernel_post_load_data);
3150b64fcae7SKees Cook 
3151130c53bfSPaul Moore /**
3152130c53bfSPaul Moore  * security_task_fix_setuid() - Update LSM with new user id attributes
3153130c53bfSPaul Moore  * @new: updated credentials
3154130c53bfSPaul Moore  * @old: credentials being replaced
3155130c53bfSPaul Moore  * @flags: LSM_SETID_* flag values
3156130c53bfSPaul Moore  *
3157130c53bfSPaul Moore  * Update the module's state after setting one or more of the user identity
3158130c53bfSPaul Moore  * attributes of the current process.  The @flags parameter indicates which of
3159130c53bfSPaul Moore  * the set*uid system calls invoked this hook.  If @new is the set of
3160130c53bfSPaul Moore  * credentials that will be installed.  Modifications should be made to this
3161130c53bfSPaul Moore  * rather than to @current->cred.
3162130c53bfSPaul Moore  *
3163130c53bfSPaul Moore  * Return: Returns 0 on success.
3164130c53bfSPaul Moore  */
security_task_fix_setuid(struct cred * new,const struct cred * old,int flags)3165d84f4f99SDavid Howells int security_task_fix_setuid(struct cred *new, const struct cred *old,
3166d84f4f99SDavid Howells 			     int flags)
316720510f2fSJames Morris {
3168f25fce3eSCasey Schaufler 	return call_int_hook(task_fix_setuid, 0, new, old, flags);
316920510f2fSJames Morris }
317020510f2fSJames Morris 
3171130c53bfSPaul Moore /**
3172130c53bfSPaul Moore  * security_task_fix_setgid() - Update LSM with new group id attributes
3173130c53bfSPaul Moore  * @new: updated credentials
3174130c53bfSPaul Moore  * @old: credentials being replaced
3175130c53bfSPaul Moore  * @flags: LSM_SETID_* flag value
3176130c53bfSPaul Moore  *
3177130c53bfSPaul Moore  * Update the module's state after setting one or more of the group identity
3178130c53bfSPaul Moore  * attributes of the current process.  The @flags parameter indicates which of
3179130c53bfSPaul Moore  * the set*gid system calls invoked this hook.  @new is the set of credentials
3180130c53bfSPaul Moore  * that will be installed.  Modifications should be made to this rather than to
3181130c53bfSPaul Moore  * @current->cred.
3182130c53bfSPaul Moore  *
3183130c53bfSPaul Moore  * Return: Returns 0 on success.
3184130c53bfSPaul Moore  */
security_task_fix_setgid(struct cred * new,const struct cred * old,int flags)318539030e13SThomas Cedeno int security_task_fix_setgid(struct cred *new, const struct cred *old,
318639030e13SThomas Cedeno 			     int flags)
318739030e13SThomas Cedeno {
318839030e13SThomas Cedeno 	return call_int_hook(task_fix_setgid, 0, new, old, flags);
318939030e13SThomas Cedeno }
319039030e13SThomas Cedeno 
3191130c53bfSPaul Moore /**
3192130c53bfSPaul Moore  * security_task_fix_setgroups() - Update LSM with new supplementary groups
3193130c53bfSPaul Moore  * @new: updated credentials
3194130c53bfSPaul Moore  * @old: credentials being replaced
3195130c53bfSPaul Moore  *
3196130c53bfSPaul Moore  * Update the module's state after setting the supplementary group identity
3197130c53bfSPaul Moore  * attributes of the current process.  @new is the set of credentials that will
3198130c53bfSPaul Moore  * be installed.  Modifications should be made to this rather than to
3199130c53bfSPaul Moore  * @current->cred.
3200130c53bfSPaul Moore  *
3201130c53bfSPaul Moore  * Return: Returns 0 on success.
3202130c53bfSPaul Moore  */
security_task_fix_setgroups(struct cred * new,const struct cred * old)3203fcfe0ac2SMicah Morton int security_task_fix_setgroups(struct cred *new, const struct cred *old)
3204fcfe0ac2SMicah Morton {
3205fcfe0ac2SMicah Morton 	return call_int_hook(task_fix_setgroups, 0, new, old);
3206fcfe0ac2SMicah Morton }
3207fcfe0ac2SMicah Morton 
3208130c53bfSPaul Moore /**
3209130c53bfSPaul Moore  * security_task_setpgid() - Check if setting the pgid is allowed
3210130c53bfSPaul Moore  * @p: task being modified
3211130c53bfSPaul Moore  * @pgid: new pgid
3212130c53bfSPaul Moore  *
3213130c53bfSPaul Moore  * Check permission before setting the process group identifier of the process
3214130c53bfSPaul Moore  * @p to @pgid.
3215130c53bfSPaul Moore  *
3216130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3217130c53bfSPaul Moore  */
security_task_setpgid(struct task_struct * p,pid_t pgid)321820510f2fSJames Morris int security_task_setpgid(struct task_struct *p, pid_t pgid)
321920510f2fSJames Morris {
3220f25fce3eSCasey Schaufler 	return call_int_hook(task_setpgid, 0, p, pgid);
322120510f2fSJames Morris }
322220510f2fSJames Morris 
3223130c53bfSPaul Moore /**
3224130c53bfSPaul Moore  * security_task_getpgid() - Check if getting the pgid is allowed
3225130c53bfSPaul Moore  * @p: task
3226130c53bfSPaul Moore  *
3227130c53bfSPaul Moore  * Check permission before getting the process group identifier of the process
3228130c53bfSPaul Moore  * @p.
3229130c53bfSPaul Moore  *
3230130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3231130c53bfSPaul Moore  */
security_task_getpgid(struct task_struct * p)323220510f2fSJames Morris int security_task_getpgid(struct task_struct *p)
323320510f2fSJames Morris {
3234f25fce3eSCasey Schaufler 	return call_int_hook(task_getpgid, 0, p);
323520510f2fSJames Morris }
323620510f2fSJames Morris 
3237130c53bfSPaul Moore /**
3238130c53bfSPaul Moore  * security_task_getsid() - Check if getting the session id is allowed
3239130c53bfSPaul Moore  * @p: task
3240130c53bfSPaul Moore  *
3241130c53bfSPaul Moore  * Check permission before getting the session identifier of the process @p.
3242130c53bfSPaul Moore  *
3243130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3244130c53bfSPaul Moore  */
security_task_getsid(struct task_struct * p)324520510f2fSJames Morris int security_task_getsid(struct task_struct *p)
324620510f2fSJames Morris {
3247f25fce3eSCasey Schaufler 	return call_int_hook(task_getsid, 0, p);
324820510f2fSJames Morris }
324920510f2fSJames Morris 
3250130c53bfSPaul Moore /**
3251130c53bfSPaul Moore  * security_current_getsecid_subj() - Get the current task's subjective secid
3252130c53bfSPaul Moore  * @secid: secid value
3253130c53bfSPaul Moore  *
3254130c53bfSPaul Moore  * Retrieve the subjective security identifier of the current task and return
3255130c53bfSPaul Moore  * it in @secid.  In case of failure, @secid will be set to zero.
3256130c53bfSPaul Moore  */
security_current_getsecid_subj(u32 * secid)32576326948fSPaul Moore void security_current_getsecid_subj(u32 *secid)
325820510f2fSJames Morris {
3259b1d9e6b0SCasey Schaufler 	*secid = 0;
32606326948fSPaul Moore 	call_void_hook(current_getsecid_subj, secid);
326120510f2fSJames Morris }
32626326948fSPaul Moore EXPORT_SYMBOL(security_current_getsecid_subj);
32634ebd7651SPaul Moore 
3264130c53bfSPaul Moore /**
3265130c53bfSPaul Moore  * security_task_getsecid_obj() - Get a task's objective secid
3266130c53bfSPaul Moore  * @p: target task
3267130c53bfSPaul Moore  * @secid: secid value
3268130c53bfSPaul Moore  *
3269130c53bfSPaul Moore  * Retrieve the objective security identifier of the task_struct in @p and
3270130c53bfSPaul Moore  * return it in @secid. In case of failure, @secid will be set to zero.
3271130c53bfSPaul Moore  */
security_task_getsecid_obj(struct task_struct * p,u32 * secid)32724ebd7651SPaul Moore void security_task_getsecid_obj(struct task_struct *p, u32 *secid)
32734ebd7651SPaul Moore {
32744ebd7651SPaul Moore 	*secid = 0;
32754ebd7651SPaul Moore 	call_void_hook(task_getsecid_obj, p, secid);
32764ebd7651SPaul Moore }
32774ebd7651SPaul Moore EXPORT_SYMBOL(security_task_getsecid_obj);
327820510f2fSJames Morris 
3279130c53bfSPaul Moore /**
3280130c53bfSPaul Moore  * security_task_setnice() - Check if setting a task's nice value is allowed
3281130c53bfSPaul Moore  * @p: target task
3282130c53bfSPaul Moore  * @nice: nice value
3283130c53bfSPaul Moore  *
3284130c53bfSPaul Moore  * Check permission before setting the nice value of @p to @nice.
3285130c53bfSPaul Moore  *
3286130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3287130c53bfSPaul Moore  */
security_task_setnice(struct task_struct * p,int nice)328820510f2fSJames Morris int security_task_setnice(struct task_struct *p, int nice)
328920510f2fSJames Morris {
3290f25fce3eSCasey Schaufler 	return call_int_hook(task_setnice, 0, p, nice);
329120510f2fSJames Morris }
329220510f2fSJames Morris 
3293130c53bfSPaul Moore /**
3294130c53bfSPaul Moore  * security_task_setioprio() - Check if setting a task's ioprio is allowed
3295130c53bfSPaul Moore  * @p: target task
3296130c53bfSPaul Moore  * @ioprio: ioprio value
3297130c53bfSPaul Moore  *
3298130c53bfSPaul Moore  * Check permission before setting the ioprio value of @p to @ioprio.
3299130c53bfSPaul Moore  *
3300130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3301130c53bfSPaul Moore  */
security_task_setioprio(struct task_struct * p,int ioprio)330220510f2fSJames Morris int security_task_setioprio(struct task_struct *p, int ioprio)
330320510f2fSJames Morris {
3304f25fce3eSCasey Schaufler 	return call_int_hook(task_setioprio, 0, p, ioprio);
330520510f2fSJames Morris }
330620510f2fSJames Morris 
3307130c53bfSPaul Moore /**
3308130c53bfSPaul Moore  * security_task_getioprio() - Check if getting a task's ioprio is allowed
3309130c53bfSPaul Moore  * @p: task
3310130c53bfSPaul Moore  *
3311130c53bfSPaul Moore  * Check permission before getting the ioprio value of @p.
3312130c53bfSPaul Moore  *
3313130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3314130c53bfSPaul Moore  */
security_task_getioprio(struct task_struct * p)331520510f2fSJames Morris int security_task_getioprio(struct task_struct *p)
331620510f2fSJames Morris {
3317f25fce3eSCasey Schaufler 	return call_int_hook(task_getioprio, 0, p);
331820510f2fSJames Morris }
331920510f2fSJames Morris 
3320130c53bfSPaul Moore /**
3321130c53bfSPaul Moore  * security_task_prlimit() - Check if get/setting resources limits is allowed
3322130c53bfSPaul Moore  * @cred: current task credentials
3323130c53bfSPaul Moore  * @tcred: target task credentials
3324130c53bfSPaul Moore  * @flags: LSM_PRLIMIT_* flag bits indicating a get/set/both
3325130c53bfSPaul Moore  *
3326130c53bfSPaul Moore  * Check permission before getting and/or setting the resource limits of
3327130c53bfSPaul Moore  * another task.
3328130c53bfSPaul Moore  *
3329130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3330130c53bfSPaul Moore  */
security_task_prlimit(const struct cred * cred,const struct cred * tcred,unsigned int flags)3331791ec491SStephen Smalley int security_task_prlimit(const struct cred *cred, const struct cred *tcred,
3332791ec491SStephen Smalley 			  unsigned int flags)
3333791ec491SStephen Smalley {
3334791ec491SStephen Smalley 	return call_int_hook(task_prlimit, 0, cred, tcred, flags);
3335791ec491SStephen Smalley }
3336791ec491SStephen Smalley 
3337130c53bfSPaul Moore /**
3338130c53bfSPaul Moore  * security_task_setrlimit() - Check if setting a new rlimit value is allowed
3339130c53bfSPaul Moore  * @p: target task's group leader
3340130c53bfSPaul Moore  * @resource: resource whose limit is being set
3341130c53bfSPaul Moore  * @new_rlim: new resource limit
3342130c53bfSPaul Moore  *
3343130c53bfSPaul Moore  * Check permission before setting the resource limits of process @p for
3344130c53bfSPaul Moore  * @resource to @new_rlim.  The old resource limit values can be examined by
3345130c53bfSPaul Moore  * dereferencing (p->signal->rlim + resource).
3346130c53bfSPaul Moore  *
3347130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3348130c53bfSPaul Moore  */
security_task_setrlimit(struct task_struct * p,unsigned int resource,struct rlimit * new_rlim)33498fd00b4dSJiri Slaby int security_task_setrlimit(struct task_struct *p, unsigned int resource,
33508fd00b4dSJiri Slaby 			    struct rlimit *new_rlim)
335120510f2fSJames Morris {
3352f25fce3eSCasey Schaufler 	return call_int_hook(task_setrlimit, 0, p, resource, new_rlim);
335320510f2fSJames Morris }
335420510f2fSJames Morris 
3355130c53bfSPaul Moore /**
3356130c53bfSPaul Moore  * security_task_setscheduler() - Check if setting sched policy/param is allowed
3357130c53bfSPaul Moore  * @p: target task
3358130c53bfSPaul Moore  *
3359130c53bfSPaul Moore  * Check permission before setting scheduling policy and/or parameters of
3360130c53bfSPaul Moore  * process @p.
3361130c53bfSPaul Moore  *
3362130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3363130c53bfSPaul Moore  */
security_task_setscheduler(struct task_struct * p)3364b0ae1981SKOSAKI Motohiro int security_task_setscheduler(struct task_struct *p)
336520510f2fSJames Morris {
3366f25fce3eSCasey Schaufler 	return call_int_hook(task_setscheduler, 0, p);
336720510f2fSJames Morris }
336820510f2fSJames Morris 
3369130c53bfSPaul Moore /**
3370130c53bfSPaul Moore  * security_task_getscheduler() - Check if getting scheduling info is allowed
3371130c53bfSPaul Moore  * @p: target task
3372130c53bfSPaul Moore  *
3373130c53bfSPaul Moore  * Check permission before obtaining scheduling information for process @p.
3374130c53bfSPaul Moore  *
3375130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3376130c53bfSPaul Moore  */
security_task_getscheduler(struct task_struct * p)337720510f2fSJames Morris int security_task_getscheduler(struct task_struct *p)
337820510f2fSJames Morris {
3379f25fce3eSCasey Schaufler 	return call_int_hook(task_getscheduler, 0, p);
338020510f2fSJames Morris }
338120510f2fSJames Morris 
3382130c53bfSPaul Moore /**
3383130c53bfSPaul Moore  * security_task_movememory() - Check if moving memory is allowed
3384130c53bfSPaul Moore  * @p: task
3385130c53bfSPaul Moore  *
3386130c53bfSPaul Moore  * Check permission before moving memory owned by process @p.
3387130c53bfSPaul Moore  *
3388130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3389130c53bfSPaul Moore  */
security_task_movememory(struct task_struct * p)339020510f2fSJames Morris int security_task_movememory(struct task_struct *p)
339120510f2fSJames Morris {
3392f25fce3eSCasey Schaufler 	return call_int_hook(task_movememory, 0, p);
339320510f2fSJames Morris }
339420510f2fSJames Morris 
3395130c53bfSPaul Moore /**
3396130c53bfSPaul Moore  * security_task_kill() - Check if sending a signal is allowed
3397130c53bfSPaul Moore  * @p: target process
3398130c53bfSPaul Moore  * @info: signal information
3399130c53bfSPaul Moore  * @sig: signal value
3400130c53bfSPaul Moore  * @cred: credentials of the signal sender, NULL if @current
3401130c53bfSPaul Moore  *
3402130c53bfSPaul Moore  * Check permission before sending signal @sig to @p.  @info can be NULL, the
3403130c53bfSPaul Moore  * constant 1, or a pointer to a kernel_siginfo structure.  If @info is 1 or
3404130c53bfSPaul Moore  * SI_FROMKERNEL(info) is true, then the signal should be viewed as coming from
3405130c53bfSPaul Moore  * the kernel and should typically be permitted.  SIGIO signals are handled
3406130c53bfSPaul Moore  * separately by the send_sigiotask hook in file_security_ops.
3407130c53bfSPaul Moore  *
3408130c53bfSPaul Moore  * Return: Returns 0 if permission is granted.
3409130c53bfSPaul Moore  */
security_task_kill(struct task_struct * p,struct kernel_siginfo * info,int sig,const struct cred * cred)3410ae7795bcSEric W. Biederman int security_task_kill(struct task_struct *p, struct kernel_siginfo *info,
34116b4f3d01SStephen Smalley 		       int sig, const struct cred *cred)
341220510f2fSJames Morris {
34136b4f3d01SStephen Smalley 	return call_int_hook(task_kill, 0, p, info, sig, cred);
341420510f2fSJames Morris }
341520510f2fSJames Morris 
3416130c53bfSPaul Moore /**
3417130c53bfSPaul Moore  * security_task_prctl() - Check if a prctl op is allowed
3418130c53bfSPaul Moore  * @option: operation
3419130c53bfSPaul Moore  * @arg2: argument
3420130c53bfSPaul Moore  * @arg3: argument
3421130c53bfSPaul Moore  * @arg4: argument
3422130c53bfSPaul Moore  * @arg5: argument
3423130c53bfSPaul Moore  *
3424130c53bfSPaul Moore  * Check permission before performing a process control operation on the
3425130c53bfSPaul Moore  * current process.
3426130c53bfSPaul Moore  *
3427130c53bfSPaul Moore  * Return: Return -ENOSYS if no-one wanted to handle this op, any other value
3428130c53bfSPaul Moore  *         to cause prctl() to return immediately with that value.
3429130c53bfSPaul Moore  */
security_task_prctl(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)343020510f2fSJames Morris int security_task_prctl(int option, unsigned long arg2, unsigned long arg3,
3431d84f4f99SDavid Howells 			unsigned long arg4, unsigned long arg5)
343220510f2fSJames Morris {
3433b1d9e6b0SCasey Schaufler 	int thisrc;
343498e828a0SKP Singh 	int rc = LSM_RET_DEFAULT(task_prctl);
3435b1d9e6b0SCasey Schaufler 	struct security_hook_list *hp;
3436b1d9e6b0SCasey Schaufler 
3437df0ce173SSargun Dhillon 	hlist_for_each_entry(hp, &security_hook_heads.task_prctl, list) {
3438b1d9e6b0SCasey Schaufler 		thisrc = hp->hook.task_prctl(option, arg2, arg3, arg4, arg5);
343998e828a0SKP Singh 		if (thisrc != LSM_RET_DEFAULT(task_prctl)) {
3440b1d9e6b0SCasey Schaufler 			rc = thisrc;
3441b1d9e6b0SCasey Schaufler 			if (thisrc != 0)
3442b1d9e6b0SCasey Schaufler 				break;
3443b1d9e6b0SCasey Schaufler 		}
3444b1d9e6b0SCasey Schaufler 	}
3445c6993e4aSKees Cook 	return rc;
344620510f2fSJames Morris }
344720510f2fSJames Morris 
3448130c53bfSPaul Moore /**
3449130c53bfSPaul Moore  * security_task_to_inode() - Set the security attributes of a task's inode
3450130c53bfSPaul Moore  * @p: task
3451130c53bfSPaul Moore  * @inode: inode
3452130c53bfSPaul Moore  *
3453130c53bfSPaul Moore  * Set the security attributes for an inode based on an associated task's
3454130c53bfSPaul Moore  * security attributes, e.g. for /proc/pid inodes.
3455130c53bfSPaul Moore  */
security_task_to_inode(struct task_struct * p,struct inode * inode)345620510f2fSJames Morris void security_task_to_inode(struct task_struct *p, struct inode *inode)
345720510f2fSJames Morris {
3458f25fce3eSCasey Schaufler 	call_void_hook(task_to_inode, p, inode);
345920510f2fSJames Morris }
346020510f2fSJames Morris 
3461130c53bfSPaul Moore /**
3462130c53bfSPaul Moore  * security_create_user_ns() - Check if creating a new userns is allowed
3463130c53bfSPaul Moore  * @cred: prepared creds
3464130c53bfSPaul Moore  *
3465130c53bfSPaul Moore  * Check permission prior to creating a new user namespace.
3466130c53bfSPaul Moore  *
3467130c53bfSPaul Moore  * Return: Returns 0 if successful, otherwise < 0 error code.
3468130c53bfSPaul Moore  */
security_create_user_ns(const struct cred * cred)34697cd4c5c2SFrederick Lawler int security_create_user_ns(const struct cred *cred)
34707cd4c5c2SFrederick Lawler {
34717cd4c5c2SFrederick Lawler 	return call_int_hook(userns_create, 0, cred);
34727cd4c5c2SFrederick Lawler }
34737cd4c5c2SFrederick Lawler 
347443fad282SPaul Moore /**
347543fad282SPaul Moore  * security_ipc_permission() - Check if sysv ipc access is allowed
347643fad282SPaul Moore  * @ipcp: ipc permission structure
34771e2523d7SPaul Moore  * @flag: requested permissions
347843fad282SPaul Moore  *
347943fad282SPaul Moore  * Check permissions for access to IPC.
348043fad282SPaul Moore  *
348143fad282SPaul Moore  * Return: Returns 0 if permission is granted.
348243fad282SPaul Moore  */
security_ipc_permission(struct kern_ipc_perm * ipcp,short flag)348320510f2fSJames Morris int security_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
348420510f2fSJames Morris {
3485f25fce3eSCasey Schaufler 	return call_int_hook(ipc_permission, 0, ipcp, flag);
348620510f2fSJames Morris }
348720510f2fSJames Morris 
348843fad282SPaul Moore /**
348943fad282SPaul Moore  * security_ipc_getsecid() - Get the sysv ipc object's secid
349043fad282SPaul Moore  * @ipcp: ipc permission structure
349143fad282SPaul Moore  * @secid: secid pointer
349243fad282SPaul Moore  *
349343fad282SPaul Moore  * Get the secid associated with the ipc object.  In case of failure, @secid
349443fad282SPaul Moore  * will be set to zero.
349543fad282SPaul Moore  */
security_ipc_getsecid(struct kern_ipc_perm * ipcp,u32 * secid)34968a076191SAhmed S. Darwish void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
34978a076191SAhmed S. Darwish {
3498b1d9e6b0SCasey Schaufler 	*secid = 0;
3499f25fce3eSCasey Schaufler 	call_void_hook(ipc_getsecid, ipcp, secid);
35008a076191SAhmed S. Darwish }
35018a076191SAhmed S. Darwish 
350243fad282SPaul Moore /**
350343fad282SPaul Moore  * security_msg_msg_alloc() - Allocate a sysv ipc message LSM blob
350443fad282SPaul Moore  * @msg: message structure
350543fad282SPaul Moore  *
350643fad282SPaul Moore  * Allocate and attach a security structure to the msg->security field.  The
350743fad282SPaul Moore  * security field is initialized to NULL when the structure is first created.
350843fad282SPaul Moore  *
350943fad282SPaul Moore  * Return: Return 0 if operation was successful and permission is granted.
351043fad282SPaul Moore  */
security_msg_msg_alloc(struct msg_msg * msg)351120510f2fSJames Morris int security_msg_msg_alloc(struct msg_msg *msg)
351220510f2fSJames Morris {
3513ecd5f82eSCasey Schaufler 	int rc = lsm_msg_msg_alloc(msg);
3514ecd5f82eSCasey Schaufler 
3515ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3516ecd5f82eSCasey Schaufler 		return rc;
3517ecd5f82eSCasey Schaufler 	rc = call_int_hook(msg_msg_alloc_security, 0, msg);
3518ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3519ecd5f82eSCasey Schaufler 		security_msg_msg_free(msg);
3520ecd5f82eSCasey Schaufler 	return rc;
352120510f2fSJames Morris }
352220510f2fSJames Morris 
352343fad282SPaul Moore /**
352443fad282SPaul Moore  * security_msg_msg_free() - Free a sysv ipc message LSM blob
352543fad282SPaul Moore  * @msg: message structure
352643fad282SPaul Moore  *
352743fad282SPaul Moore  * Deallocate the security structure for this message.
352843fad282SPaul Moore  */
security_msg_msg_free(struct msg_msg * msg)352920510f2fSJames Morris void security_msg_msg_free(struct msg_msg *msg)
353020510f2fSJames Morris {
3531f25fce3eSCasey Schaufler 	call_void_hook(msg_msg_free_security, msg);
3532ecd5f82eSCasey Schaufler 	kfree(msg->security);
3533ecd5f82eSCasey Schaufler 	msg->security = NULL;
353420510f2fSJames Morris }
353520510f2fSJames Morris 
353643fad282SPaul Moore /**
353743fad282SPaul Moore  * security_msg_queue_alloc() - Allocate a sysv ipc msg queue LSM blob
353843fad282SPaul Moore  * @msq: sysv ipc permission structure
353943fad282SPaul Moore  *
354043fad282SPaul Moore  * Allocate and attach a security structure to @msg. The security field is
354143fad282SPaul Moore  * initialized to NULL when the structure is first created.
354243fad282SPaul Moore  *
354343fad282SPaul Moore  * Return: Returns 0 if operation was successful and permission is granted.
354443fad282SPaul Moore  */
security_msg_queue_alloc(struct kern_ipc_perm * msq)3545d8c6e854SEric W. Biederman int security_msg_queue_alloc(struct kern_ipc_perm *msq)
354620510f2fSJames Morris {
3547ecd5f82eSCasey Schaufler 	int rc = lsm_ipc_alloc(msq);
3548ecd5f82eSCasey Schaufler 
3549ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3550ecd5f82eSCasey Schaufler 		return rc;
3551ecd5f82eSCasey Schaufler 	rc = call_int_hook(msg_queue_alloc_security, 0, msq);
3552ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3553ecd5f82eSCasey Schaufler 		security_msg_queue_free(msq);
3554ecd5f82eSCasey Schaufler 	return rc;
355520510f2fSJames Morris }
355620510f2fSJames Morris 
355743fad282SPaul Moore /**
355843fad282SPaul Moore  * security_msg_queue_free() - Free a sysv ipc msg queue LSM blob
355943fad282SPaul Moore  * @msq: sysv ipc permission structure
356043fad282SPaul Moore  *
356143fad282SPaul Moore  * Deallocate security field @perm->security for the message queue.
356243fad282SPaul Moore  */
security_msg_queue_free(struct kern_ipc_perm * msq)3563d8c6e854SEric W. Biederman void security_msg_queue_free(struct kern_ipc_perm *msq)
356420510f2fSJames Morris {
3565f25fce3eSCasey Schaufler 	call_void_hook(msg_queue_free_security, msq);
3566ecd5f82eSCasey Schaufler 	kfree(msq->security);
3567ecd5f82eSCasey Schaufler 	msq->security = NULL;
356820510f2fSJames Morris }
356920510f2fSJames Morris 
357043fad282SPaul Moore /**
357143fad282SPaul Moore  * security_msg_queue_associate() - Check if a msg queue operation is allowed
357243fad282SPaul Moore  * @msq: sysv ipc permission structure
357343fad282SPaul Moore  * @msqflg: operation flags
357443fad282SPaul Moore  *
357543fad282SPaul Moore  * Check permission when a message queue is requested through the msgget system
357643fad282SPaul Moore  * call. This hook is only called when returning the message queue identifier
357743fad282SPaul Moore  * for an existing message queue, not when a new message queue is created.
357843fad282SPaul Moore  *
357943fad282SPaul Moore  * Return: Return 0 if permission is granted.
358043fad282SPaul Moore  */
security_msg_queue_associate(struct kern_ipc_perm * msq,int msqflg)3581d8c6e854SEric W. Biederman int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
358220510f2fSJames Morris {
3583f25fce3eSCasey Schaufler 	return call_int_hook(msg_queue_associate, 0, msq, msqflg);
358420510f2fSJames Morris }
358520510f2fSJames Morris 
358643fad282SPaul Moore /**
358743fad282SPaul Moore  * security_msg_queue_msgctl() - Check if a msg queue operation is allowed
358843fad282SPaul Moore  * @msq: sysv ipc permission structure
358943fad282SPaul Moore  * @cmd: operation
359043fad282SPaul Moore  *
359143fad282SPaul Moore  * Check permission when a message control operation specified by @cmd is to be
359243fad282SPaul Moore  * performed on the message queue with permissions.
359343fad282SPaul Moore  *
359443fad282SPaul Moore  * Return: Returns 0 if permission is granted.
359543fad282SPaul Moore  */
security_msg_queue_msgctl(struct kern_ipc_perm * msq,int cmd)3596d8c6e854SEric W. Biederman int security_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
359720510f2fSJames Morris {
3598f25fce3eSCasey Schaufler 	return call_int_hook(msg_queue_msgctl, 0, msq, cmd);
359920510f2fSJames Morris }
360020510f2fSJames Morris 
360143fad282SPaul Moore /**
360243fad282SPaul Moore  * security_msg_queue_msgsnd() - Check if sending a sysv ipc message is allowed
360343fad282SPaul Moore  * @msq: sysv ipc permission structure
360443fad282SPaul Moore  * @msg: message
360543fad282SPaul Moore  * @msqflg: operation flags
360643fad282SPaul Moore  *
360743fad282SPaul Moore  * Check permission before a message, @msg, is enqueued on the message queue
360843fad282SPaul Moore  * with permissions specified in @msq.
360943fad282SPaul Moore  *
361043fad282SPaul Moore  * Return: Returns 0 if permission is granted.
361143fad282SPaul Moore  */
security_msg_queue_msgsnd(struct kern_ipc_perm * msq,struct msg_msg * msg,int msqflg)3612d8c6e854SEric W. Biederman int security_msg_queue_msgsnd(struct kern_ipc_perm *msq,
361320510f2fSJames Morris 			      struct msg_msg *msg, int msqflg)
361420510f2fSJames Morris {
3615f25fce3eSCasey Schaufler 	return call_int_hook(msg_queue_msgsnd, 0, msq, msg, msqflg);
361620510f2fSJames Morris }
361720510f2fSJames Morris 
361843fad282SPaul Moore /**
361943fad282SPaul Moore  * security_msg_queue_msgrcv() - Check if receiving a sysv ipc msg is allowed
362043fad282SPaul Moore  * @msq: sysv ipc permission structure
362143fad282SPaul Moore  * @msg: message
362243fad282SPaul Moore  * @target: target task
362343fad282SPaul Moore  * @type: type of message requested
362443fad282SPaul Moore  * @mode: operation flags
362543fad282SPaul Moore  *
362643fad282SPaul Moore  * Check permission before a message, @msg, is removed from the message	queue.
362743fad282SPaul Moore  * The @target task structure contains a pointer to the process that will be
362843fad282SPaul Moore  * receiving the message (not equal to the current process when inline receives
362943fad282SPaul Moore  * are being performed).
363043fad282SPaul Moore  *
363143fad282SPaul Moore  * Return: Returns 0 if permission is granted.
363243fad282SPaul Moore  */
security_msg_queue_msgrcv(struct kern_ipc_perm * msq,struct msg_msg * msg,struct task_struct * target,long type,int mode)3633d8c6e854SEric W. Biederman int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
363420510f2fSJames Morris 			      struct task_struct *target, long type, int mode)
363520510f2fSJames Morris {
3636f25fce3eSCasey Schaufler 	return call_int_hook(msg_queue_msgrcv, 0, msq, msg, target, type, mode);
363720510f2fSJames Morris }
363820510f2fSJames Morris 
363943fad282SPaul Moore /**
364043fad282SPaul Moore  * security_shm_alloc() - Allocate a sysv shm LSM blob
364143fad282SPaul Moore  * @shp: sysv ipc permission structure
364243fad282SPaul Moore  *
364343fad282SPaul Moore  * Allocate and attach a security structure to the @shp security field.  The
364443fad282SPaul Moore  * security field is initialized to NULL when the structure is first created.
364543fad282SPaul Moore  *
364643fad282SPaul Moore  * Return: Returns 0 if operation was successful and permission is granted.
364743fad282SPaul Moore  */
security_shm_alloc(struct kern_ipc_perm * shp)36487191adffSEric W. Biederman int security_shm_alloc(struct kern_ipc_perm *shp)
364920510f2fSJames Morris {
3650ecd5f82eSCasey Schaufler 	int rc = lsm_ipc_alloc(shp);
3651ecd5f82eSCasey Schaufler 
3652ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3653ecd5f82eSCasey Schaufler 		return rc;
3654ecd5f82eSCasey Schaufler 	rc = call_int_hook(shm_alloc_security, 0, shp);
3655ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3656ecd5f82eSCasey Schaufler 		security_shm_free(shp);
3657ecd5f82eSCasey Schaufler 	return rc;
365820510f2fSJames Morris }
365920510f2fSJames Morris 
366043fad282SPaul Moore /**
366143fad282SPaul Moore  * security_shm_free() - Free a sysv shm LSM blob
366243fad282SPaul Moore  * @shp: sysv ipc permission structure
366343fad282SPaul Moore  *
366443fad282SPaul Moore  * Deallocate the security structure @perm->security for the memory segment.
366543fad282SPaul Moore  */
security_shm_free(struct kern_ipc_perm * shp)36667191adffSEric W. Biederman void security_shm_free(struct kern_ipc_perm *shp)
366720510f2fSJames Morris {
3668f25fce3eSCasey Schaufler 	call_void_hook(shm_free_security, shp);
3669ecd5f82eSCasey Schaufler 	kfree(shp->security);
3670ecd5f82eSCasey Schaufler 	shp->security = NULL;
367120510f2fSJames Morris }
367220510f2fSJames Morris 
367343fad282SPaul Moore /**
367443fad282SPaul Moore  * security_shm_associate() - Check if a sysv shm operation is allowed
367543fad282SPaul Moore  * @shp: sysv ipc permission structure
367643fad282SPaul Moore  * @shmflg: operation flags
367743fad282SPaul Moore  *
367843fad282SPaul Moore  * Check permission when a shared memory region is requested through the shmget
367943fad282SPaul Moore  * system call. This hook is only called when returning the shared memory
368043fad282SPaul Moore  * region identifier for an existing region, not when a new shared memory
368143fad282SPaul Moore  * region is created.
368243fad282SPaul Moore  *
368343fad282SPaul Moore  * Return: Returns 0 if permission is granted.
368443fad282SPaul Moore  */
security_shm_associate(struct kern_ipc_perm * shp,int shmflg)36857191adffSEric W. Biederman int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
368620510f2fSJames Morris {
3687f25fce3eSCasey Schaufler 	return call_int_hook(shm_associate, 0, shp, shmflg);
368820510f2fSJames Morris }
368920510f2fSJames Morris 
369043fad282SPaul Moore /**
369143fad282SPaul Moore  * security_shm_shmctl() - Check if a sysv shm operation is allowed
369243fad282SPaul Moore  * @shp: sysv ipc permission structure
369343fad282SPaul Moore  * @cmd: operation
369443fad282SPaul Moore  *
369543fad282SPaul Moore  * Check permission when a shared memory control operation specified by @cmd is
369643fad282SPaul Moore  * to be performed on the shared memory region with permissions in @shp.
369743fad282SPaul Moore  *
369843fad282SPaul Moore  * Return: Return 0 if permission is granted.
369943fad282SPaul Moore  */
security_shm_shmctl(struct kern_ipc_perm * shp,int cmd)37007191adffSEric W. Biederman int security_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
370120510f2fSJames Morris {
3702f25fce3eSCasey Schaufler 	return call_int_hook(shm_shmctl, 0, shp, cmd);
370320510f2fSJames Morris }
370420510f2fSJames Morris 
370543fad282SPaul Moore /**
370643fad282SPaul Moore  * security_shm_shmat() - Check if a sysv shm attach operation is allowed
370743fad282SPaul Moore  * @shp: sysv ipc permission structure
370843fad282SPaul Moore  * @shmaddr: address of memory region to attach
370943fad282SPaul Moore  * @shmflg: operation flags
371043fad282SPaul Moore  *
371143fad282SPaul Moore  * Check permissions prior to allowing the shmat system call to attach the
371243fad282SPaul Moore  * shared memory segment with permissions @shp to the data segment of the
371343fad282SPaul Moore  * calling process. The attaching address is specified by @shmaddr.
371443fad282SPaul Moore  *
371543fad282SPaul Moore  * Return: Returns 0 if permission is granted.
371643fad282SPaul Moore  */
security_shm_shmat(struct kern_ipc_perm * shp,char __user * shmaddr,int shmflg)371763c1845bSPaul Moore int security_shm_shmat(struct kern_ipc_perm *shp,
371863c1845bSPaul Moore 		       char __user *shmaddr, int shmflg)
371920510f2fSJames Morris {
3720f25fce3eSCasey Schaufler 	return call_int_hook(shm_shmat, 0, shp, shmaddr, shmflg);
372120510f2fSJames Morris }
372220510f2fSJames Morris 
372343fad282SPaul Moore /**
372443fad282SPaul Moore  * security_sem_alloc() - Allocate a sysv semaphore LSM blob
372543fad282SPaul Moore  * @sma: sysv ipc permission structure
372643fad282SPaul Moore  *
372743fad282SPaul Moore  * Allocate and attach a security structure to the @sma security field. The
372843fad282SPaul Moore  * security field is initialized to NULL when the structure is first created.
372943fad282SPaul Moore  *
373043fad282SPaul Moore  * Return: Returns 0 if operation was successful and permission is granted.
373143fad282SPaul Moore  */
security_sem_alloc(struct kern_ipc_perm * sma)3732aefad959SEric W. Biederman int security_sem_alloc(struct kern_ipc_perm *sma)
373320510f2fSJames Morris {
3734ecd5f82eSCasey Schaufler 	int rc = lsm_ipc_alloc(sma);
3735ecd5f82eSCasey Schaufler 
3736ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3737ecd5f82eSCasey Schaufler 		return rc;
3738ecd5f82eSCasey Schaufler 	rc = call_int_hook(sem_alloc_security, 0, sma);
3739ecd5f82eSCasey Schaufler 	if (unlikely(rc))
3740ecd5f82eSCasey Schaufler 		security_sem_free(sma);
3741ecd5f82eSCasey Schaufler 	return rc;
374220510f2fSJames Morris }
374320510f2fSJames Morris 
374443fad282SPaul Moore /**
374543fad282SPaul Moore  * security_sem_free() - Free a sysv semaphore LSM blob
374643fad282SPaul Moore  * @sma: sysv ipc permission structure
374743fad282SPaul Moore  *
374843fad282SPaul Moore  * Deallocate security structure @sma->security for the semaphore.
374943fad282SPaul Moore  */
security_sem_free(struct kern_ipc_perm * sma)3750aefad959SEric W. Biederman void security_sem_free(struct kern_ipc_perm *sma)
375120510f2fSJames Morris {
3752f25fce3eSCasey Schaufler 	call_void_hook(sem_free_security, sma);
3753ecd5f82eSCasey Schaufler 	kfree(sma->security);
3754ecd5f82eSCasey Schaufler 	sma->security = NULL;
375520510f2fSJames Morris }
375620510f2fSJames Morris 
375743fad282SPaul Moore /**
375843fad282SPaul Moore  * security_sem_associate() - Check if a sysv semaphore operation is allowed
375943fad282SPaul Moore  * @sma: sysv ipc permission structure
376043fad282SPaul Moore  * @semflg: operation flags
376143fad282SPaul Moore  *
376243fad282SPaul Moore  * Check permission when a semaphore is requested through the semget system
376343fad282SPaul Moore  * call. This hook is only called when returning the semaphore identifier for
376443fad282SPaul Moore  * an existing semaphore, not when a new one must be created.
376543fad282SPaul Moore  *
376643fad282SPaul Moore  * Return: Returns 0 if permission is granted.
376743fad282SPaul Moore  */
security_sem_associate(struct kern_ipc_perm * sma,int semflg)3768aefad959SEric W. Biederman int security_sem_associate(struct kern_ipc_perm *sma, int semflg)
376920510f2fSJames Morris {
3770f25fce3eSCasey Schaufler 	return call_int_hook(sem_associate, 0, sma, semflg);
377120510f2fSJames Morris }
377220510f2fSJames Morris 
377343fad282SPaul Moore /**
37741e2523d7SPaul Moore  * security_sem_semctl() - Check if a sysv semaphore operation is allowed
377543fad282SPaul Moore  * @sma: sysv ipc permission structure
377643fad282SPaul Moore  * @cmd: operation
377743fad282SPaul Moore  *
377843fad282SPaul Moore  * Check permission when a semaphore operation specified by @cmd is to be
377943fad282SPaul Moore  * performed on the semaphore.
378043fad282SPaul Moore  *
378143fad282SPaul Moore  * Return: Returns 0 if permission is granted.
378243fad282SPaul Moore  */
security_sem_semctl(struct kern_ipc_perm * sma,int cmd)3783aefad959SEric W. Biederman int security_sem_semctl(struct kern_ipc_perm *sma, int cmd)
378420510f2fSJames Morris {
3785f25fce3eSCasey Schaufler 	return call_int_hook(sem_semctl, 0, sma, cmd);
378620510f2fSJames Morris }
378720510f2fSJames Morris 
378843fad282SPaul Moore /**
378943fad282SPaul Moore  * security_sem_semop() - Check if a sysv semaphore operation is allowed
379043fad282SPaul Moore  * @sma: sysv ipc permission structure
379143fad282SPaul Moore  * @sops: operations to perform
379243fad282SPaul Moore  * @nsops: number of operations
379343fad282SPaul Moore  * @alter: flag indicating changes will be made
379443fad282SPaul Moore  *
379543fad282SPaul Moore  * Check permissions before performing operations on members of the semaphore
379643fad282SPaul Moore  * set. If the @alter flag is nonzero, the semaphore set may be modified.
379743fad282SPaul Moore  *
379843fad282SPaul Moore  * Return: Returns 0 if permission is granted.
379943fad282SPaul Moore  */
security_sem_semop(struct kern_ipc_perm * sma,struct sembuf * sops,unsigned nsops,int alter)3800aefad959SEric W. Biederman int security_sem_semop(struct kern_ipc_perm *sma, struct sembuf *sops,
380120510f2fSJames Morris 		       unsigned nsops, int alter)
380220510f2fSJames Morris {
3803f25fce3eSCasey Schaufler 	return call_int_hook(sem_semop, 0, sma, sops, nsops, alter);
380420510f2fSJames Morris }
380520510f2fSJames Morris 
3806916e3258SPaul Moore /**
3807916e3258SPaul Moore  * security_d_instantiate() - Populate an inode's LSM state based on a dentry
3808916e3258SPaul Moore  * @dentry: dentry
3809916e3258SPaul Moore  * @inode: inode
3810916e3258SPaul Moore  *
3811916e3258SPaul Moore  * Fill in @inode security information for a @dentry if allowed.
3812916e3258SPaul Moore  */
security_d_instantiate(struct dentry * dentry,struct inode * inode)381320510f2fSJames Morris void security_d_instantiate(struct dentry *dentry, struct inode *inode)
381420510f2fSJames Morris {
381520510f2fSJames Morris 	if (unlikely(inode && IS_PRIVATE(inode)))
381620510f2fSJames Morris 		return;
3817f25fce3eSCasey Schaufler 	call_void_hook(d_instantiate, dentry, inode);
381820510f2fSJames Morris }
381920510f2fSJames Morris EXPORT_SYMBOL(security_d_instantiate);
382020510f2fSJames Morris 
3821916e3258SPaul Moore /**
3822916e3258SPaul Moore  * security_getprocattr() - Read an attribute for a task
3823916e3258SPaul Moore  * @p: the task
3824916e3258SPaul Moore  * @lsm: LSM name
3825916e3258SPaul Moore  * @name: attribute name
3826916e3258SPaul Moore  * @value: attribute value
3827916e3258SPaul Moore  *
3828916e3258SPaul Moore  * Read attribute @name for task @p and store it into @value if allowed.
3829916e3258SPaul Moore  *
3830916e3258SPaul Moore  * Return: Returns the length of @value on success, a negative value otherwise.
3831916e3258SPaul Moore  */
security_getprocattr(struct task_struct * p,const char * lsm,const char * name,char ** value)3832c8e477c6SAl Viro int security_getprocattr(struct task_struct *p, const char *lsm,
3833c8e477c6SAl Viro 			 const char *name, char **value)
383420510f2fSJames Morris {
38356d9c939dSCasey Schaufler 	struct security_hook_list *hp;
38366d9c939dSCasey Schaufler 
38376d9c939dSCasey Schaufler 	hlist_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
38386d9c939dSCasey Schaufler 		if (lsm != NULL && strcmp(lsm, hp->lsm))
38396d9c939dSCasey Schaufler 			continue;
38406d9c939dSCasey Schaufler 		return hp->hook.getprocattr(p, name, value);
38416d9c939dSCasey Schaufler 	}
384298e828a0SKP Singh 	return LSM_RET_DEFAULT(getprocattr);
384320510f2fSJames Morris }
384420510f2fSJames Morris 
3845916e3258SPaul Moore /**
3846916e3258SPaul Moore  * security_setprocattr() - Set an attribute for a task
3847916e3258SPaul Moore  * @lsm: LSM name
3848916e3258SPaul Moore  * @name: attribute name
3849916e3258SPaul Moore  * @value: attribute value
3850916e3258SPaul Moore  * @size: attribute value size
3851916e3258SPaul Moore  *
3852916e3258SPaul Moore  * Write (set) the current task's attribute @name to @value, size @size if
3853916e3258SPaul Moore  * allowed.
3854916e3258SPaul Moore  *
3855916e3258SPaul Moore  * Return: Returns bytes written on success, a negative value otherwise.
3856916e3258SPaul Moore  */
security_setprocattr(const char * lsm,const char * name,void * value,size_t size)38576d9c939dSCasey Schaufler int security_setprocattr(const char *lsm, const char *name, void *value,
38586d9c939dSCasey Schaufler 			 size_t size)
385920510f2fSJames Morris {
38606d9c939dSCasey Schaufler 	struct security_hook_list *hp;
38616d9c939dSCasey Schaufler 
38626d9c939dSCasey Schaufler 	hlist_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
38636d9c939dSCasey Schaufler 		if (lsm != NULL && strcmp(lsm, hp->lsm))
38646d9c939dSCasey Schaufler 			continue;
38656d9c939dSCasey Schaufler 		return hp->hook.setprocattr(name, value, size);
38666d9c939dSCasey Schaufler 	}
386798e828a0SKP Singh 	return LSM_RET_DEFAULT(setprocattr);
386820510f2fSJames Morris }
386920510f2fSJames Morris 
38702bcf51bfSPaul Moore /**
38712bcf51bfSPaul Moore  * security_netlink_send() - Save info and check if netlink sending is allowed
38722bcf51bfSPaul Moore  * @sk: sending socket
38732bcf51bfSPaul Moore  * @skb: netlink message
38742bcf51bfSPaul Moore  *
38752bcf51bfSPaul Moore  * Save security information for a netlink message so that permission checking
38762bcf51bfSPaul Moore  * can be performed when the message is processed.  The security information
38772bcf51bfSPaul Moore  * can be saved using the eff_cap field of the netlink_skb_parms structure.
38782bcf51bfSPaul Moore  * Also may be used to provide fine grained control over message transmission.
38792bcf51bfSPaul Moore  *
38802bcf51bfSPaul Moore  * Return: Returns 0 if the information was successfully saved and message is
38812bcf51bfSPaul Moore  *         allowed to be transmitted.
38822bcf51bfSPaul Moore  */
security_netlink_send(struct sock * sk,struct sk_buff * skb)388320510f2fSJames Morris int security_netlink_send(struct sock *sk, struct sk_buff *skb)
388420510f2fSJames Morris {
3885f25fce3eSCasey Schaufler 	return call_int_hook(netlink_send, 0, sk, skb);
388620510f2fSJames Morris }
388720510f2fSJames Morris 
3888e261301cSPaul Moore /**
3889e261301cSPaul Moore  * security_ismaclabel() - Check is the named attribute is a MAC label
3890e261301cSPaul Moore  * @name: full extended attribute name
3891e261301cSPaul Moore  *
3892e261301cSPaul Moore  * Check if the extended attribute specified by @name represents a MAC label.
3893e261301cSPaul Moore  *
3894e261301cSPaul Moore  * Return: Returns 1 if name is a MAC attribute otherwise returns 0.
3895e261301cSPaul Moore  */
security_ismaclabel(const char * name)3896746df9b5SDavid Quigley int security_ismaclabel(const char *name)
3897746df9b5SDavid Quigley {
3898f25fce3eSCasey Schaufler 	return call_int_hook(ismaclabel, 0, name);
3899746df9b5SDavid Quigley }
3900746df9b5SDavid Quigley EXPORT_SYMBOL(security_ismaclabel);
3901746df9b5SDavid Quigley 
3902e261301cSPaul Moore /**
3903e261301cSPaul Moore  * security_secid_to_secctx() - Convert a secid to a secctx
3904e261301cSPaul Moore  * @secid: secid
3905e261301cSPaul Moore  * @secdata: secctx
3906e261301cSPaul Moore  * @seclen: secctx length
3907e261301cSPaul Moore  *
3908e261301cSPaul Moore  * Convert secid to security context.  If @secdata is NULL the length of the
3909e261301cSPaul Moore  * result will be returned in @seclen, but no @secdata will be returned.  This
3910e261301cSPaul Moore  * does mean that the length could change between calls to check the length and
3911e261301cSPaul Moore  * the next call which actually allocates and returns the @secdata.
3912e261301cSPaul Moore  *
3913e261301cSPaul Moore  * Return: Return 0 on success, error on failure.
3914e261301cSPaul Moore  */
security_secid_to_secctx(u32 secid,char ** secdata,u32 * seclen)391520510f2fSJames Morris int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
391620510f2fSJames Morris {
39170550cfe8SKP Singh 	struct security_hook_list *hp;
39180550cfe8SKP Singh 	int rc;
39190550cfe8SKP Singh 
39200550cfe8SKP Singh 	/*
39210550cfe8SKP Singh 	 * Currently, only one LSM can implement secid_to_secctx (i.e this
39220550cfe8SKP Singh 	 * LSM hook is not "stackable").
39230550cfe8SKP Singh 	 */
39240550cfe8SKP Singh 	hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
39250550cfe8SKP Singh 		rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
39260550cfe8SKP Singh 		if (rc != LSM_RET_DEFAULT(secid_to_secctx))
39270550cfe8SKP Singh 			return rc;
39280550cfe8SKP Singh 	}
39290550cfe8SKP Singh 
39300550cfe8SKP Singh 	return LSM_RET_DEFAULT(secid_to_secctx);
393120510f2fSJames Morris }
393220510f2fSJames Morris EXPORT_SYMBOL(security_secid_to_secctx);
393320510f2fSJames Morris 
3934e261301cSPaul Moore /**
3935e261301cSPaul Moore  * security_secctx_to_secid() - Convert a secctx to a secid
3936e261301cSPaul Moore  * @secdata: secctx
3937e261301cSPaul Moore  * @seclen: length of secctx
3938e261301cSPaul Moore  * @secid: secid
3939e261301cSPaul Moore  *
3940e261301cSPaul Moore  * Convert security context to secid.
3941e261301cSPaul Moore  *
3942e261301cSPaul Moore  * Return: Returns 0 on success, error on failure.
3943e261301cSPaul Moore  */
security_secctx_to_secid(const char * secdata,u32 seclen,u32 * secid)39447bf570dcSDavid Howells int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
394563cb3449SDavid Howells {
3946b1d9e6b0SCasey Schaufler 	*secid = 0;
3947f25fce3eSCasey Schaufler 	return call_int_hook(secctx_to_secid, 0, secdata, seclen, secid);
394863cb3449SDavid Howells }
394963cb3449SDavid Howells EXPORT_SYMBOL(security_secctx_to_secid);
395063cb3449SDavid Howells 
3951e261301cSPaul Moore /**
3952e261301cSPaul Moore  * security_release_secctx() - Free a secctx buffer
3953e261301cSPaul Moore  * @secdata: secctx
3954e261301cSPaul Moore  * @seclen: length of secctx
3955e261301cSPaul Moore  *
3956e261301cSPaul Moore  * Release the security context.
3957e261301cSPaul Moore  */
security_release_secctx(char * secdata,u32 seclen)395820510f2fSJames Morris void security_release_secctx(char *secdata, u32 seclen)
395920510f2fSJames Morris {
3960f25fce3eSCasey Schaufler 	call_void_hook(release_secctx, secdata, seclen);
396120510f2fSJames Morris }
396220510f2fSJames Morris EXPORT_SYMBOL(security_release_secctx);
396320510f2fSJames Morris 
3964e261301cSPaul Moore /**
3965e261301cSPaul Moore  * security_inode_invalidate_secctx() - Invalidate an inode's security label
3966e261301cSPaul Moore  * @inode: inode
3967e261301cSPaul Moore  *
3968e261301cSPaul Moore  * Notify the security module that it must revalidate the security context of
3969e261301cSPaul Moore  * an inode.
3970e261301cSPaul Moore  */
security_inode_invalidate_secctx(struct inode * inode)39716f3be9f5SAndreas Gruenbacher void security_inode_invalidate_secctx(struct inode *inode)
39726f3be9f5SAndreas Gruenbacher {
39736f3be9f5SAndreas Gruenbacher 	call_void_hook(inode_invalidate_secctx, inode);
39746f3be9f5SAndreas Gruenbacher }
39756f3be9f5SAndreas Gruenbacher EXPORT_SYMBOL(security_inode_invalidate_secctx);
39766f3be9f5SAndreas Gruenbacher 
3977e261301cSPaul Moore /**
3978e261301cSPaul Moore  * security_inode_notifysecctx() - Nofify the LSM of an inode's security label
3979e261301cSPaul Moore  * @inode: inode
3980e261301cSPaul Moore  * @ctx: secctx
3981e261301cSPaul Moore  * @ctxlen: length of secctx
3982e261301cSPaul Moore  *
3983e261301cSPaul Moore  * Notify the security module of what the security context of an inode should
3984e261301cSPaul Moore  * be.  Initializes the incore security context managed by the security module
3985e261301cSPaul Moore  * for this inode.  Example usage: NFS client invokes this hook to initialize
3986e261301cSPaul Moore  * the security context in its incore inode to the value provided by the server
3987e261301cSPaul Moore  * for the file when the server returned the file's attributes to the client.
3988e261301cSPaul Moore  * Must be called with inode->i_mutex locked.
3989e261301cSPaul Moore  *
3990e261301cSPaul Moore  * Return: Returns 0 on success, error on failure.
3991e261301cSPaul Moore  */
security_inode_notifysecctx(struct inode * inode,void * ctx,u32 ctxlen)39921ee65e37SDavid P. Quigley int security_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
39931ee65e37SDavid P. Quigley {
3994f25fce3eSCasey Schaufler 	return call_int_hook(inode_notifysecctx, 0, inode, ctx, ctxlen);
39951ee65e37SDavid P. Quigley }
39961ee65e37SDavid P. Quigley EXPORT_SYMBOL(security_inode_notifysecctx);
39971ee65e37SDavid P. Quigley 
3998e261301cSPaul Moore /**
3999e261301cSPaul Moore  * security_inode_setsecctx() - Change the security label of an inode
4000e261301cSPaul Moore  * @dentry: inode
4001e261301cSPaul Moore  * @ctx: secctx
4002e261301cSPaul Moore  * @ctxlen: length of secctx
4003e261301cSPaul Moore  *
4004e261301cSPaul Moore  * Change the security context of an inode.  Updates the incore security
4005e261301cSPaul Moore  * context managed by the security module and invokes the fs code as needed
4006e261301cSPaul Moore  * (via __vfs_setxattr_noperm) to update any backing xattrs that represent the
4007e261301cSPaul Moore  * context.  Example usage: NFS server invokes this hook to change the security
4008e261301cSPaul Moore  * context in its incore inode and on the backing filesystem to a value
4009e261301cSPaul Moore  * provided by the client on a SETATTR operation.  Must be called with
4010e261301cSPaul Moore  * inode->i_mutex locked.
4011e261301cSPaul Moore  *
4012e261301cSPaul Moore  * Return: Returns 0 on success, error on failure.
4013e261301cSPaul Moore  */
security_inode_setsecctx(struct dentry * dentry,void * ctx,u32 ctxlen)40141ee65e37SDavid P. Quigley int security_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
40151ee65e37SDavid P. Quigley {
4016f25fce3eSCasey Schaufler 	return call_int_hook(inode_setsecctx, 0, dentry, ctx, ctxlen);
40171ee65e37SDavid P. Quigley }
40181ee65e37SDavid P. Quigley EXPORT_SYMBOL(security_inode_setsecctx);
40191ee65e37SDavid P. Quigley 
4020e261301cSPaul Moore /**
4021e261301cSPaul Moore  * security_inode_getsecctx() - Get the security label of an inode
4022e261301cSPaul Moore  * @inode: inode
4023e261301cSPaul Moore  * @ctx: secctx
4024e261301cSPaul Moore  * @ctxlen: length of secctx
4025e261301cSPaul Moore  *
4026e261301cSPaul Moore  * On success, returns 0 and fills out @ctx and @ctxlen with the security
4027e261301cSPaul Moore  * context for the given @inode.
4028e261301cSPaul Moore  *
4029e261301cSPaul Moore  * Return: Returns 0 on success, error on failure.
4030e261301cSPaul Moore  */
security_inode_getsecctx(struct inode * inode,void ** ctx,u32 * ctxlen)40311ee65e37SDavid P. Quigley int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
40321ee65e37SDavid P. Quigley {
40336d2a97b7SOndrej Mosnacek 	struct security_hook_list *hp;
40346d2a97b7SOndrej Mosnacek 	int rc;
40356d2a97b7SOndrej Mosnacek 
40366d2a97b7SOndrej Mosnacek 	/*
40376d2a97b7SOndrej Mosnacek 	 * Only one module will provide a security context.
40386d2a97b7SOndrej Mosnacek 	 */
40396d2a97b7SOndrej Mosnacek 	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecctx, list) {
40406d2a97b7SOndrej Mosnacek 		rc = hp->hook.inode_getsecctx(inode, ctx, ctxlen);
40416d2a97b7SOndrej Mosnacek 		if (rc != LSM_RET_DEFAULT(inode_getsecctx))
40426d2a97b7SOndrej Mosnacek 			return rc;
40436d2a97b7SOndrej Mosnacek 	}
40446d2a97b7SOndrej Mosnacek 
40456d2a97b7SOndrej Mosnacek 	return LSM_RET_DEFAULT(inode_getsecctx);
40461ee65e37SDavid P. Quigley }
40471ee65e37SDavid P. Quigley EXPORT_SYMBOL(security_inode_getsecctx);
40481ee65e37SDavid P. Quigley 
4049344fa64eSDavid Howells #ifdef CONFIG_WATCH_QUEUE
4050e261301cSPaul Moore /**
4051e261301cSPaul Moore  * security_post_notification() - Check if a watch notification can be posted
4052e261301cSPaul Moore  * @w_cred: credentials of the task that set the watch
4053e261301cSPaul Moore  * @cred: credentials of the task which triggered the watch
4054e261301cSPaul Moore  * @n: the notification
4055e261301cSPaul Moore  *
4056e261301cSPaul Moore  * Check to see if a watch notification can be posted to a particular queue.
4057e261301cSPaul Moore  *
4058e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
4059e261301cSPaul Moore  */
security_post_notification(const struct cred * w_cred,const struct cred * cred,struct watch_notification * n)4060344fa64eSDavid Howells int security_post_notification(const struct cred *w_cred,
4061344fa64eSDavid Howells 			       const struct cred *cred,
4062344fa64eSDavid Howells 			       struct watch_notification *n)
4063344fa64eSDavid Howells {
4064344fa64eSDavid Howells 	return call_int_hook(post_notification, 0, w_cred, cred, n);
4065344fa64eSDavid Howells }
4066344fa64eSDavid Howells #endif /* CONFIG_WATCH_QUEUE */
4067344fa64eSDavid Howells 
4068998f5040SDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
4069e261301cSPaul Moore /**
4070e261301cSPaul Moore  * security_watch_key() - Check if a task is allowed to watch for key events
4071e261301cSPaul Moore  * @key: the key to watch
4072e261301cSPaul Moore  *
4073e261301cSPaul Moore  * Check to see if a process is allowed to watch for event notifications from
4074e261301cSPaul Moore  * a key or keyring.
4075e261301cSPaul Moore  *
4076e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
4077e261301cSPaul Moore  */
security_watch_key(struct key * key)4078998f5040SDavid Howells int security_watch_key(struct key *key)
4079998f5040SDavid Howells {
4080998f5040SDavid Howells 	return call_int_hook(watch_key, 0, key);
4081998f5040SDavid Howells }
408263c1845bSPaul Moore #endif /* CONFIG_KEY_NOTIFICATIONS */
4083998f5040SDavid Howells 
408420510f2fSJames Morris #ifdef CONFIG_SECURITY_NETWORK
40852c2442fdSPaul Moore /**
40862c2442fdSPaul Moore  * security_unix_stream_connect() - Check if a AF_UNIX stream is allowed
40872c2442fdSPaul Moore  * @sock: originating sock
40882c2442fdSPaul Moore  * @other: peer sock
40892c2442fdSPaul Moore  * @newsk: new sock
40902c2442fdSPaul Moore  *
40912c2442fdSPaul Moore  * Check permissions before establishing a Unix domain stream connection
40922c2442fdSPaul Moore  * between @sock and @other.
40932c2442fdSPaul Moore  *
40942c2442fdSPaul Moore  * The @unix_stream_connect and @unix_may_send hooks were necessary because
40952c2442fdSPaul Moore  * Linux provides an alternative to the conventional file name space for Unix
40962c2442fdSPaul Moore  * domain sockets.  Whereas binding and connecting to sockets in the file name
40972c2442fdSPaul Moore  * space is mediated by the typical file permissions (and caught by the mknod
40982c2442fdSPaul Moore  * and permission hooks in inode_security_ops), binding and connecting to
40992c2442fdSPaul Moore  * sockets in the abstract name space is completely unmediated.  Sufficient
41002c2442fdSPaul Moore  * control of Unix domain sockets in the abstract name space isn't possible
41012c2442fdSPaul Moore  * using only the socket layer hooks, since we need to know the actual target
41022c2442fdSPaul Moore  * socket, which is not looked up until we are inside the af_unix code.
41032c2442fdSPaul Moore  *
41042c2442fdSPaul Moore  * Return: Returns 0 if permission is granted.
41052c2442fdSPaul Moore  */
security_unix_stream_connect(struct sock * sock,struct sock * other,struct sock * newsk)410663c1845bSPaul Moore int security_unix_stream_connect(struct sock *sock, struct sock *other,
410763c1845bSPaul Moore 				 struct sock *newsk)
410820510f2fSJames Morris {
4109f25fce3eSCasey Schaufler 	return call_int_hook(unix_stream_connect, 0, sock, other, newsk);
411020510f2fSJames Morris }
411120510f2fSJames Morris EXPORT_SYMBOL(security_unix_stream_connect);
411220510f2fSJames Morris 
41132c2442fdSPaul Moore /**
41142c2442fdSPaul Moore  * security_unix_may_send() - Check if AF_UNIX socket can send datagrams
41152c2442fdSPaul Moore  * @sock: originating sock
41162c2442fdSPaul Moore  * @other: peer sock
41172c2442fdSPaul Moore  *
41182c2442fdSPaul Moore  * Check permissions before connecting or sending datagrams from @sock to
41192c2442fdSPaul Moore  * @other.
41202c2442fdSPaul Moore  *
41212c2442fdSPaul Moore  * The @unix_stream_connect and @unix_may_send hooks were necessary because
41222c2442fdSPaul Moore  * Linux provides an alternative to the conventional file name space for Unix
41232c2442fdSPaul Moore  * domain sockets.  Whereas binding and connecting to sockets in the file name
41242c2442fdSPaul Moore  * space is mediated by the typical file permissions (and caught by the mknod
41252c2442fdSPaul Moore  * and permission hooks in inode_security_ops), binding and connecting to
41262c2442fdSPaul Moore  * sockets in the abstract name space is completely unmediated.  Sufficient
41272c2442fdSPaul Moore  * control of Unix domain sockets in the abstract name space isn't possible
41282c2442fdSPaul Moore  * using only the socket layer hooks, since we need to know the actual target
41292c2442fdSPaul Moore  * socket, which is not looked up until we are inside the af_unix code.
41302c2442fdSPaul Moore  *
41312c2442fdSPaul Moore  * Return: Returns 0 if permission is granted.
41322c2442fdSPaul Moore  */
security_unix_may_send(struct socket * sock,struct socket * other)413320510f2fSJames Morris int security_unix_may_send(struct socket *sock,  struct socket *other)
413420510f2fSJames Morris {
4135f25fce3eSCasey Schaufler 	return call_int_hook(unix_may_send, 0, sock, other);
413620510f2fSJames Morris }
413720510f2fSJames Morris EXPORT_SYMBOL(security_unix_may_send);
413820510f2fSJames Morris 
41396b6bbe8cSPaul Moore /**
41406b6bbe8cSPaul Moore  * security_socket_create() - Check if creating a new socket is allowed
41416b6bbe8cSPaul Moore  * @family: protocol family
41426b6bbe8cSPaul Moore  * @type: communications type
41436b6bbe8cSPaul Moore  * @protocol: requested protocol
41446b6bbe8cSPaul Moore  * @kern: set to 1 if a kernel socket is requested
41456b6bbe8cSPaul Moore  *
41466b6bbe8cSPaul Moore  * Check permissions prior to creating a new socket.
41476b6bbe8cSPaul Moore  *
41486b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
41496b6bbe8cSPaul Moore  */
security_socket_create(int family,int type,int protocol,int kern)415020510f2fSJames Morris int security_socket_create(int family, int type, int protocol, int kern)
415120510f2fSJames Morris {
4152f25fce3eSCasey Schaufler 	return call_int_hook(socket_create, 0, family, type, protocol, kern);
415320510f2fSJames Morris }
415420510f2fSJames Morris 
41556b6bbe8cSPaul Moore /**
41561e2523d7SPaul Moore  * security_socket_post_create() - Initialize a newly created socket
41576b6bbe8cSPaul Moore  * @sock: socket
41586b6bbe8cSPaul Moore  * @family: protocol family
41596b6bbe8cSPaul Moore  * @type: communications type
41606b6bbe8cSPaul Moore  * @protocol: requested protocol
41616b6bbe8cSPaul Moore  * @kern: set to 1 if a kernel socket is requested
41626b6bbe8cSPaul Moore  *
41636b6bbe8cSPaul Moore  * This hook allows a module to update or allocate a per-socket security
41646b6bbe8cSPaul Moore  * structure. Note that the security field was not added directly to the socket
41656b6bbe8cSPaul Moore  * structure, but rather, the socket security information is stored in the
41666b6bbe8cSPaul Moore  * associated inode.  Typically, the inode alloc_security hook will allocate
41676b6bbe8cSPaul Moore  * and attach security information to SOCK_INODE(sock)->i_security.  This hook
41686b6bbe8cSPaul Moore  * may be used to update the SOCK_INODE(sock)->i_security field with additional
41696b6bbe8cSPaul Moore  * information that wasn't available when the inode was allocated.
41706b6bbe8cSPaul Moore  *
41716b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
41726b6bbe8cSPaul Moore  */
security_socket_post_create(struct socket * sock,int family,int type,int protocol,int kern)417320510f2fSJames Morris int security_socket_post_create(struct socket *sock, int family,
417420510f2fSJames Morris 				int type, int protocol, int kern)
417520510f2fSJames Morris {
4176f25fce3eSCasey Schaufler 	return call_int_hook(socket_post_create, 0, sock, family, type,
417720510f2fSJames Morris 			     protocol, kern);
417820510f2fSJames Morris }
417920510f2fSJames Morris 
41806b6bbe8cSPaul Moore /**
41816b6bbe8cSPaul Moore  * security_socket_socketpair() - Check if creating a socketpair is allowed
41826b6bbe8cSPaul Moore  * @socka: first socket
41836b6bbe8cSPaul Moore  * @sockb: second socket
41846b6bbe8cSPaul Moore  *
41856b6bbe8cSPaul Moore  * Check permissions before creating a fresh pair of sockets.
41866b6bbe8cSPaul Moore  *
41876b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted and the connection was
41886b6bbe8cSPaul Moore  *         established.
41896b6bbe8cSPaul Moore  */
security_socket_socketpair(struct socket * socka,struct socket * sockb)4190aae7cfcbSDavid Herrmann int security_socket_socketpair(struct socket *socka, struct socket *sockb)
4191aae7cfcbSDavid Herrmann {
4192aae7cfcbSDavid Herrmann 	return call_int_hook(socket_socketpair, 0, socka, sockb);
4193aae7cfcbSDavid Herrmann }
4194aae7cfcbSDavid Herrmann EXPORT_SYMBOL(security_socket_socketpair);
4195aae7cfcbSDavid Herrmann 
41966b6bbe8cSPaul Moore /**
41976b6bbe8cSPaul Moore  * security_socket_bind() - Check if a socket bind operation is allowed
41986b6bbe8cSPaul Moore  * @sock: socket
41996b6bbe8cSPaul Moore  * @address: requested bind address
42006b6bbe8cSPaul Moore  * @addrlen: length of address
42016b6bbe8cSPaul Moore  *
42026b6bbe8cSPaul Moore  * Check permission before socket protocol layer bind operation is performed
42036b6bbe8cSPaul Moore  * and the socket @sock is bound to the address specified in the @address
42046b6bbe8cSPaul Moore  * parameter.
42056b6bbe8cSPaul Moore  *
42066b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42076b6bbe8cSPaul Moore  */
security_socket_bind(struct socket * sock,struct sockaddr * address,int addrlen)420863c1845bSPaul Moore int security_socket_bind(struct socket *sock,
420963c1845bSPaul Moore 			 struct sockaddr *address, int addrlen)
421020510f2fSJames Morris {
4211f25fce3eSCasey Schaufler 	return call_int_hook(socket_bind, 0, sock, address, addrlen);
421220510f2fSJames Morris }
421320510f2fSJames Morris 
42146b6bbe8cSPaul Moore /**
42156b6bbe8cSPaul Moore  * security_socket_connect() - Check if a socket connect operation is allowed
42166b6bbe8cSPaul Moore  * @sock: socket
42176b6bbe8cSPaul Moore  * @address: address of remote connection point
42186b6bbe8cSPaul Moore  * @addrlen: length of address
42196b6bbe8cSPaul Moore  *
42206b6bbe8cSPaul Moore  * Check permission before socket protocol layer connect operation attempts to
42216b6bbe8cSPaul Moore  * connect socket @sock to a remote address, @address.
42226b6bbe8cSPaul Moore  *
42236b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42246b6bbe8cSPaul Moore  */
security_socket_connect(struct socket * sock,struct sockaddr * address,int addrlen)422563c1845bSPaul Moore int security_socket_connect(struct socket *sock,
422663c1845bSPaul Moore 			    struct sockaddr *address, int addrlen)
422720510f2fSJames Morris {
4228f25fce3eSCasey Schaufler 	return call_int_hook(socket_connect, 0, sock, address, addrlen);
422920510f2fSJames Morris }
423020510f2fSJames Morris 
42316b6bbe8cSPaul Moore /**
42326b6bbe8cSPaul Moore  * security_socket_listen() - Check if a socket is allowed to listen
42336b6bbe8cSPaul Moore  * @sock: socket
42346b6bbe8cSPaul Moore  * @backlog: connection queue size
42356b6bbe8cSPaul Moore  *
42366b6bbe8cSPaul Moore  * Check permission before socket protocol layer listen operation.
42376b6bbe8cSPaul Moore  *
42386b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42396b6bbe8cSPaul Moore  */
security_socket_listen(struct socket * sock,int backlog)424020510f2fSJames Morris int security_socket_listen(struct socket *sock, int backlog)
424120510f2fSJames Morris {
4242f25fce3eSCasey Schaufler 	return call_int_hook(socket_listen, 0, sock, backlog);
424320510f2fSJames Morris }
424420510f2fSJames Morris 
42456b6bbe8cSPaul Moore /**
42466b6bbe8cSPaul Moore  * security_socket_accept() - Check if a socket is allowed to accept connections
42476b6bbe8cSPaul Moore  * @sock: listening socket
42486b6bbe8cSPaul Moore  * @newsock: newly creation connection socket
42496b6bbe8cSPaul Moore  *
42506b6bbe8cSPaul Moore  * Check permission before accepting a new connection.  Note that the new
42516b6bbe8cSPaul Moore  * socket, @newsock, has been created and some information copied to it, but
42526b6bbe8cSPaul Moore  * the accept operation has not actually been performed.
42536b6bbe8cSPaul Moore  *
42546b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42556b6bbe8cSPaul Moore  */
security_socket_accept(struct socket * sock,struct socket * newsock)425620510f2fSJames Morris int security_socket_accept(struct socket *sock, struct socket *newsock)
425720510f2fSJames Morris {
4258f25fce3eSCasey Schaufler 	return call_int_hook(socket_accept, 0, sock, newsock);
425920510f2fSJames Morris }
426020510f2fSJames Morris 
42616b6bbe8cSPaul Moore /**
42626b6bbe8cSPaul Moore  * security_socket_sendmsg() - Check is sending a message is allowed
42636b6bbe8cSPaul Moore  * @sock: sending socket
42646b6bbe8cSPaul Moore  * @msg: message to send
42656b6bbe8cSPaul Moore  * @size: size of message
42666b6bbe8cSPaul Moore  *
42676b6bbe8cSPaul Moore  * Check permission before transmitting a message to another socket.
42686b6bbe8cSPaul Moore  *
42696b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42706b6bbe8cSPaul Moore  */
security_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)427120510f2fSJames Morris int security_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
427220510f2fSJames Morris {
4273f25fce3eSCasey Schaufler 	return call_int_hook(socket_sendmsg, 0, sock, msg, size);
427420510f2fSJames Morris }
427520510f2fSJames Morris 
42766b6bbe8cSPaul Moore /**
42776b6bbe8cSPaul Moore  * security_socket_recvmsg() - Check if receiving a message is allowed
42786b6bbe8cSPaul Moore  * @sock: receiving socket
42796b6bbe8cSPaul Moore  * @msg: message to receive
42806b6bbe8cSPaul Moore  * @size: size of message
42816b6bbe8cSPaul Moore  * @flags: operational flags
42826b6bbe8cSPaul Moore  *
42836b6bbe8cSPaul Moore  * Check permission before receiving a message from a socket.
42846b6bbe8cSPaul Moore  *
42856b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
42866b6bbe8cSPaul Moore  */
security_socket_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags)428720510f2fSJames Morris int security_socket_recvmsg(struct socket *sock, struct msghdr *msg,
428820510f2fSJames Morris 			    int size, int flags)
428920510f2fSJames Morris {
4290f25fce3eSCasey Schaufler 	return call_int_hook(socket_recvmsg, 0, sock, msg, size, flags);
429120510f2fSJames Morris }
429220510f2fSJames Morris 
42936b6bbe8cSPaul Moore /**
42946b6bbe8cSPaul Moore  * security_socket_getsockname() - Check if reading the socket addr is allowed
42956b6bbe8cSPaul Moore  * @sock: socket
42966b6bbe8cSPaul Moore  *
42976b6bbe8cSPaul Moore  * Check permission before reading the local address (name) of the socket
42986b6bbe8cSPaul Moore  * object.
42996b6bbe8cSPaul Moore  *
43006b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43016b6bbe8cSPaul Moore  */
security_socket_getsockname(struct socket * sock)430220510f2fSJames Morris int security_socket_getsockname(struct socket *sock)
430320510f2fSJames Morris {
4304f25fce3eSCasey Schaufler 	return call_int_hook(socket_getsockname, 0, sock);
430520510f2fSJames Morris }
430620510f2fSJames Morris 
43076b6bbe8cSPaul Moore /**
43086b6bbe8cSPaul Moore  * security_socket_getpeername() - Check if reading the peer's addr is allowed
43096b6bbe8cSPaul Moore  * @sock: socket
43106b6bbe8cSPaul Moore  *
43116b6bbe8cSPaul Moore  * Check permission before the remote address (name) of a socket object.
43126b6bbe8cSPaul Moore  *
43136b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43146b6bbe8cSPaul Moore  */
security_socket_getpeername(struct socket * sock)431520510f2fSJames Morris int security_socket_getpeername(struct socket *sock)
431620510f2fSJames Morris {
4317f25fce3eSCasey Schaufler 	return call_int_hook(socket_getpeername, 0, sock);
431820510f2fSJames Morris }
431920510f2fSJames Morris 
43206b6bbe8cSPaul Moore /**
43216b6bbe8cSPaul Moore  * security_socket_getsockopt() - Check if reading a socket option is allowed
43226b6bbe8cSPaul Moore  * @sock: socket
43236b6bbe8cSPaul Moore  * @level: option's protocol level
43246b6bbe8cSPaul Moore  * @optname: option name
43256b6bbe8cSPaul Moore  *
43266b6bbe8cSPaul Moore  * Check permissions before retrieving the options associated with socket
43276b6bbe8cSPaul Moore  * @sock.
43286b6bbe8cSPaul Moore  *
43296b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43306b6bbe8cSPaul Moore  */
security_socket_getsockopt(struct socket * sock,int level,int optname)433120510f2fSJames Morris int security_socket_getsockopt(struct socket *sock, int level, int optname)
433220510f2fSJames Morris {
4333f25fce3eSCasey Schaufler 	return call_int_hook(socket_getsockopt, 0, sock, level, optname);
433420510f2fSJames Morris }
433520510f2fSJames Morris 
43366b6bbe8cSPaul Moore /**
43376b6bbe8cSPaul Moore  * security_socket_setsockopt() - Check if setting a socket option is allowed
43386b6bbe8cSPaul Moore  * @sock: socket
43396b6bbe8cSPaul Moore  * @level: option's protocol level
43406b6bbe8cSPaul Moore  * @optname: option name
43416b6bbe8cSPaul Moore  *
43426b6bbe8cSPaul Moore  * Check permissions before setting the options associated with socket @sock.
43436b6bbe8cSPaul Moore  *
43446b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43456b6bbe8cSPaul Moore  */
security_socket_setsockopt(struct socket * sock,int level,int optname)434620510f2fSJames Morris int security_socket_setsockopt(struct socket *sock, int level, int optname)
434720510f2fSJames Morris {
4348f25fce3eSCasey Schaufler 	return call_int_hook(socket_setsockopt, 0, sock, level, optname);
434920510f2fSJames Morris }
435020510f2fSJames Morris 
43516b6bbe8cSPaul Moore /**
43526b6bbe8cSPaul Moore  * security_socket_shutdown() - Checks if shutting down the socket is allowed
43536b6bbe8cSPaul Moore  * @sock: socket
43546b6bbe8cSPaul Moore  * @how: flag indicating how sends and receives are handled
43556b6bbe8cSPaul Moore  *
43566b6bbe8cSPaul Moore  * Checks permission before all or part of a connection on the socket @sock is
43576b6bbe8cSPaul Moore  * shut down.
43586b6bbe8cSPaul Moore  *
43596b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43606b6bbe8cSPaul Moore  */
security_socket_shutdown(struct socket * sock,int how)436120510f2fSJames Morris int security_socket_shutdown(struct socket *sock, int how)
436220510f2fSJames Morris {
4363f25fce3eSCasey Schaufler 	return call_int_hook(socket_shutdown, 0, sock, how);
436420510f2fSJames Morris }
436520510f2fSJames Morris 
43666b6bbe8cSPaul Moore /**
43676b6bbe8cSPaul Moore  * security_sock_rcv_skb() - Check if an incoming network packet is allowed
43686b6bbe8cSPaul Moore  * @sk: destination sock
43696b6bbe8cSPaul Moore  * @skb: incoming packet
43706b6bbe8cSPaul Moore  *
43716b6bbe8cSPaul Moore  * Check permissions on incoming network packets.  This hook is distinct from
43726b6bbe8cSPaul Moore  * Netfilter's IP input hooks since it is the first time that the incoming
43736b6bbe8cSPaul Moore  * sk_buff @skb has been associated with a particular socket, @sk.  Must not
43746b6bbe8cSPaul Moore  * sleep inside this hook because some callers hold spinlocks.
43756b6bbe8cSPaul Moore  *
43766b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
43776b6bbe8cSPaul Moore  */
security_sock_rcv_skb(struct sock * sk,struct sk_buff * skb)437820510f2fSJames Morris int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
437920510f2fSJames Morris {
4380f25fce3eSCasey Schaufler 	return call_int_hook(socket_sock_rcv_skb, 0, sk, skb);
438120510f2fSJames Morris }
438220510f2fSJames Morris EXPORT_SYMBOL(security_sock_rcv_skb);
438320510f2fSJames Morris 
43846b6bbe8cSPaul Moore /**
43856b6bbe8cSPaul Moore  * security_socket_getpeersec_stream() - Get the remote peer label
43866b6bbe8cSPaul Moore  * @sock: socket
43876b6bbe8cSPaul Moore  * @optval: destination buffer
43886b6bbe8cSPaul Moore  * @optlen: size of peer label copied into the buffer
43896b6bbe8cSPaul Moore  * @len: maximum size of the destination buffer
43906b6bbe8cSPaul Moore  *
43916b6bbe8cSPaul Moore  * This hook allows the security module to provide peer socket security state
43926b6bbe8cSPaul Moore  * for unix or connected tcp sockets to userspace via getsockopt SO_GETPEERSEC.
43936b6bbe8cSPaul Moore  * For tcp sockets this can be meaningful if the socket is associated with an
43946b6bbe8cSPaul Moore  * ipsec SA.
43956b6bbe8cSPaul Moore  *
43966b6bbe8cSPaul Moore  * Return: Returns 0 if all is well, otherwise, typical getsockopt return
43976b6bbe8cSPaul Moore  *         values.
43986b6bbe8cSPaul Moore  */
security_socket_getpeersec_stream(struct socket * sock,sockptr_t optval,sockptr_t optlen,unsigned int len)4399b10b9c34SPaul Moore int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
4400b10b9c34SPaul Moore 				      sockptr_t optlen, unsigned int len)
440120510f2fSJames Morris {
44028df43e53SOndrej Mosnacek 	struct security_hook_list *hp;
44038df43e53SOndrej Mosnacek 	int rc;
44048df43e53SOndrej Mosnacek 
44058df43e53SOndrej Mosnacek 	/*
44068df43e53SOndrej Mosnacek 	 * Only one module will provide a security context.
44078df43e53SOndrej Mosnacek 	 */
44088df43e53SOndrej Mosnacek 	hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_stream,
44098df43e53SOndrej Mosnacek 			     list) {
44108df43e53SOndrej Mosnacek 		rc = hp->hook.socket_getpeersec_stream(sock, optval, optlen,
44118df43e53SOndrej Mosnacek 						       len);
44128df43e53SOndrej Mosnacek 		if (rc != LSM_RET_DEFAULT(socket_getpeersec_stream))
44138df43e53SOndrej Mosnacek 			return rc;
44148df43e53SOndrej Mosnacek 	}
44158df43e53SOndrej Mosnacek 	return LSM_RET_DEFAULT(socket_getpeersec_stream);
441620510f2fSJames Morris }
441720510f2fSJames Morris 
44186b6bbe8cSPaul Moore /**
44196b6bbe8cSPaul Moore  * security_socket_getpeersec_dgram() - Get the remote peer label
44206b6bbe8cSPaul Moore  * @sock: socket
44216b6bbe8cSPaul Moore  * @skb: datagram packet
44226b6bbe8cSPaul Moore  * @secid: remote peer label secid
44236b6bbe8cSPaul Moore  *
44246b6bbe8cSPaul Moore  * This hook allows the security module to provide peer socket security state
44256b6bbe8cSPaul Moore  * for udp sockets on a per-packet basis to userspace via getsockopt
44266b6bbe8cSPaul Moore  * SO_GETPEERSEC. The application must first have indicated the IP_PASSSEC
44276b6bbe8cSPaul Moore  * option via getsockopt. It can then retrieve the security state returned by
44286b6bbe8cSPaul Moore  * this hook for a packet via the SCM_SECURITY ancillary message type.
44296b6bbe8cSPaul Moore  *
44306b6bbe8cSPaul Moore  * Return: Returns 0 on success, error on failure.
44316b6bbe8cSPaul Moore  */
security_socket_getpeersec_dgram(struct socket * sock,struct sk_buff * skb,u32 * secid)443263c1845bSPaul Moore int security_socket_getpeersec_dgram(struct socket *sock,
443363c1845bSPaul Moore 				     struct sk_buff *skb, u32 *secid)
443420510f2fSJames Morris {
44358df43e53SOndrej Mosnacek 	struct security_hook_list *hp;
44368df43e53SOndrej Mosnacek 	int rc;
44378df43e53SOndrej Mosnacek 
44388df43e53SOndrej Mosnacek 	/*
44398df43e53SOndrej Mosnacek 	 * Only one module will provide a security context.
44408df43e53SOndrej Mosnacek 	 */
44418df43e53SOndrej Mosnacek 	hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_dgram,
44428df43e53SOndrej Mosnacek 			     list) {
44438df43e53SOndrej Mosnacek 		rc = hp->hook.socket_getpeersec_dgram(sock, skb, secid);
44448df43e53SOndrej Mosnacek 		if (rc != LSM_RET_DEFAULT(socket_getpeersec_dgram))
44458df43e53SOndrej Mosnacek 			return rc;
44468df43e53SOndrej Mosnacek 	}
44478df43e53SOndrej Mosnacek 	return LSM_RET_DEFAULT(socket_getpeersec_dgram);
444820510f2fSJames Morris }
444920510f2fSJames Morris EXPORT_SYMBOL(security_socket_getpeersec_dgram);
445020510f2fSJames Morris 
44516b6bbe8cSPaul Moore /**
44526b6bbe8cSPaul Moore  * security_sk_alloc() - Allocate and initialize a sock's LSM blob
44536b6bbe8cSPaul Moore  * @sk: sock
44546b6bbe8cSPaul Moore  * @family: protocol family
44551e2523d7SPaul Moore  * @priority: gfp flags
44566b6bbe8cSPaul Moore  *
44576b6bbe8cSPaul Moore  * Allocate and attach a security structure to the sk->sk_security field, which
44586b6bbe8cSPaul Moore  * is used to copy security attributes between local stream sockets.
44596b6bbe8cSPaul Moore  *
44606b6bbe8cSPaul Moore  * Return: Returns 0 on success, error on failure.
44616b6bbe8cSPaul Moore  */
security_sk_alloc(struct sock * sk,int family,gfp_t priority)446220510f2fSJames Morris int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
446320510f2fSJames Morris {
4464f25fce3eSCasey Schaufler 	return call_int_hook(sk_alloc_security, 0, sk, family, priority);
446520510f2fSJames Morris }
446620510f2fSJames Morris 
44676b6bbe8cSPaul Moore /**
44686b6bbe8cSPaul Moore  * security_sk_free() - Free the sock's LSM blob
44696b6bbe8cSPaul Moore  * @sk: sock
44706b6bbe8cSPaul Moore  *
44716b6bbe8cSPaul Moore  * Deallocate security structure.
44726b6bbe8cSPaul Moore  */
security_sk_free(struct sock * sk)447320510f2fSJames Morris void security_sk_free(struct sock *sk)
447420510f2fSJames Morris {
4475f25fce3eSCasey Schaufler 	call_void_hook(sk_free_security, sk);
447620510f2fSJames Morris }
447720510f2fSJames Morris 
44786b6bbe8cSPaul Moore /**
44796b6bbe8cSPaul Moore  * security_sk_clone() - Clone a sock's LSM state
44806b6bbe8cSPaul Moore  * @sk: original sock
44816b6bbe8cSPaul Moore  * @newsk: target sock
44826b6bbe8cSPaul Moore  *
44836b6bbe8cSPaul Moore  * Clone/copy security structure.
44846b6bbe8cSPaul Moore  */
security_sk_clone(const struct sock * sk,struct sock * newsk)448520510f2fSJames Morris void security_sk_clone(const struct sock *sk, struct sock *newsk)
448620510f2fSJames Morris {
4487f25fce3eSCasey Schaufler 	call_void_hook(sk_clone_security, sk, newsk);
448820510f2fSJames Morris }
44896230c9b4SPaul Moore EXPORT_SYMBOL(security_sk_clone);
449020510f2fSJames Morris 
4491bd1f5934SKhadija Kamran /**
4492bd1f5934SKhadija Kamran  * security_sk_classify_flow() - Set a flow's secid based on socket
4493bd1f5934SKhadija Kamran  * @sk: original socket
4494bd1f5934SKhadija Kamran  * @flic: target flow
4495bd1f5934SKhadija Kamran  *
4496bd1f5934SKhadija Kamran  * Set the target flow's secid to socket's secid.
4497bd1f5934SKhadija Kamran  */
security_sk_classify_flow(const struct sock * sk,struct flowi_common * flic)44985b52ad34SGuillaume Nault void security_sk_classify_flow(const struct sock *sk, struct flowi_common *flic)
449920510f2fSJames Morris {
45003df98d79SPaul Moore 	call_void_hook(sk_getsecid, sk, &flic->flowic_secid);
450120510f2fSJames Morris }
450220510f2fSJames Morris EXPORT_SYMBOL(security_sk_classify_flow);
450320510f2fSJames Morris 
45046b6bbe8cSPaul Moore /**
45056b6bbe8cSPaul Moore  * security_req_classify_flow() - Set a flow's secid based on request_sock
45066b6bbe8cSPaul Moore  * @req: request_sock
45076b6bbe8cSPaul Moore  * @flic: target flow
45086b6bbe8cSPaul Moore  *
45096b6bbe8cSPaul Moore  * Sets @flic's secid to @req's secid.
45106b6bbe8cSPaul Moore  */
security_req_classify_flow(const struct request_sock * req,struct flowi_common * flic)45113df98d79SPaul Moore void security_req_classify_flow(const struct request_sock *req,
45123df98d79SPaul Moore 				struct flowi_common *flic)
451320510f2fSJames Morris {
45143df98d79SPaul Moore 	call_void_hook(req_classify_flow, req, flic);
451520510f2fSJames Morris }
451620510f2fSJames Morris EXPORT_SYMBOL(security_req_classify_flow);
451720510f2fSJames Morris 
45186b6bbe8cSPaul Moore /**
45196b6bbe8cSPaul Moore  * security_sock_graft() - Reconcile LSM state when grafting a sock on a socket
45206b6bbe8cSPaul Moore  * @sk: sock being grafted
45211e2523d7SPaul Moore  * @parent: target parent socket
45226b6bbe8cSPaul Moore  *
45231e2523d7SPaul Moore  * Sets @parent's inode secid to @sk's secid and update @sk with any necessary
45241e2523d7SPaul Moore  * LSM state from @parent.
45256b6bbe8cSPaul Moore  */
security_sock_graft(struct sock * sk,struct socket * parent)452620510f2fSJames Morris void security_sock_graft(struct sock *sk, struct socket *parent)
452720510f2fSJames Morris {
4528f25fce3eSCasey Schaufler 	call_void_hook(sock_graft, sk, parent);
452920510f2fSJames Morris }
453020510f2fSJames Morris EXPORT_SYMBOL(security_sock_graft);
453120510f2fSJames Morris 
45326b6bbe8cSPaul Moore /**
45336b6bbe8cSPaul Moore  * security_inet_conn_request() - Set request_sock state using incoming connect
45346b6bbe8cSPaul Moore  * @sk: parent listening sock
45356b6bbe8cSPaul Moore  * @skb: incoming connection
45366b6bbe8cSPaul Moore  * @req: new request_sock
45376b6bbe8cSPaul Moore  *
45386b6bbe8cSPaul Moore  * Initialize the @req LSM state based on @sk and the incoming connect in @skb.
45396b6bbe8cSPaul Moore  *
45406b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
45416b6bbe8cSPaul Moore  */
security_inet_conn_request(const struct sock * sk,struct sk_buff * skb,struct request_sock * req)454241dd9596SFlorian Westphal int security_inet_conn_request(const struct sock *sk,
454320510f2fSJames Morris 			       struct sk_buff *skb, struct request_sock *req)
454420510f2fSJames Morris {
4545f25fce3eSCasey Schaufler 	return call_int_hook(inet_conn_request, 0, sk, skb, req);
454620510f2fSJames Morris }
454720510f2fSJames Morris EXPORT_SYMBOL(security_inet_conn_request);
454820510f2fSJames Morris 
45496b6bbe8cSPaul Moore /**
45506b6bbe8cSPaul Moore  * security_inet_csk_clone() - Set new sock LSM state based on request_sock
45516b6bbe8cSPaul Moore  * @newsk: new sock
45526b6bbe8cSPaul Moore  * @req: connection request_sock
45536b6bbe8cSPaul Moore  *
45546b6bbe8cSPaul Moore  * Set that LSM state of @sock using the LSM state from @req.
45556b6bbe8cSPaul Moore  */
security_inet_csk_clone(struct sock * newsk,const struct request_sock * req)455620510f2fSJames Morris void security_inet_csk_clone(struct sock *newsk,
455720510f2fSJames Morris 			     const struct request_sock *req)
455820510f2fSJames Morris {
4559f25fce3eSCasey Schaufler 	call_void_hook(inet_csk_clone, newsk, req);
456020510f2fSJames Morris }
456120510f2fSJames Morris 
45626b6bbe8cSPaul Moore /**
45636b6bbe8cSPaul Moore  * security_inet_conn_established() - Update sock's LSM state with connection
45646b6bbe8cSPaul Moore  * @sk: sock
45656b6bbe8cSPaul Moore  * @skb: connection packet
45666b6bbe8cSPaul Moore  *
45676b6bbe8cSPaul Moore  * Update @sock's LSM state to represent a new connection from @skb.
45686b6bbe8cSPaul Moore  */
security_inet_conn_established(struct sock * sk,struct sk_buff * skb)456920510f2fSJames Morris void security_inet_conn_established(struct sock *sk,
457020510f2fSJames Morris 				    struct sk_buff *skb)
457120510f2fSJames Morris {
4572f25fce3eSCasey Schaufler 	call_void_hook(inet_conn_established, sk, skb);
457320510f2fSJames Morris }
457472e89f50SRichard Haines EXPORT_SYMBOL(security_inet_conn_established);
457520510f2fSJames Morris 
45766b6bbe8cSPaul Moore /**
45776b6bbe8cSPaul Moore  * security_secmark_relabel_packet() - Check if setting a secmark is allowed
45786b6bbe8cSPaul Moore  * @secid: new secmark value
45796b6bbe8cSPaul Moore  *
45806b6bbe8cSPaul Moore  * Check if the process should be allowed to relabel packets to @secid.
45816b6bbe8cSPaul Moore  *
45826b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
45836b6bbe8cSPaul Moore  */
security_secmark_relabel_packet(u32 secid)45842606fd1fSEric Paris int security_secmark_relabel_packet(u32 secid)
45852606fd1fSEric Paris {
4586f25fce3eSCasey Schaufler 	return call_int_hook(secmark_relabel_packet, 0, secid);
45872606fd1fSEric Paris }
45882606fd1fSEric Paris EXPORT_SYMBOL(security_secmark_relabel_packet);
45892606fd1fSEric Paris 
45906b6bbe8cSPaul Moore /**
45916b6bbe8cSPaul Moore  * security_secmark_refcount_inc() - Increment the secmark labeling rule count
45926b6bbe8cSPaul Moore  *
45936b6bbe8cSPaul Moore  * Tells the LSM to increment the number of secmark labeling rules loaded.
45946b6bbe8cSPaul Moore  */
security_secmark_refcount_inc(void)45952606fd1fSEric Paris void security_secmark_refcount_inc(void)
45962606fd1fSEric Paris {
4597f25fce3eSCasey Schaufler 	call_void_hook(secmark_refcount_inc);
45982606fd1fSEric Paris }
45992606fd1fSEric Paris EXPORT_SYMBOL(security_secmark_refcount_inc);
46002606fd1fSEric Paris 
46016b6bbe8cSPaul Moore /**
46026b6bbe8cSPaul Moore  * security_secmark_refcount_dec() - Decrement the secmark labeling rule count
46036b6bbe8cSPaul Moore  *
46046b6bbe8cSPaul Moore  * Tells the LSM to decrement the number of secmark labeling rules loaded.
46056b6bbe8cSPaul Moore  */
security_secmark_refcount_dec(void)46062606fd1fSEric Paris void security_secmark_refcount_dec(void)
46072606fd1fSEric Paris {
4608f25fce3eSCasey Schaufler 	call_void_hook(secmark_refcount_dec);
46092606fd1fSEric Paris }
46102606fd1fSEric Paris EXPORT_SYMBOL(security_secmark_refcount_dec);
46112606fd1fSEric Paris 
46126b6bbe8cSPaul Moore /**
46136b6bbe8cSPaul Moore  * security_tun_dev_alloc_security() - Allocate a LSM blob for a TUN device
46146b6bbe8cSPaul Moore  * @security: pointer to the LSM blob
46156b6bbe8cSPaul Moore  *
46166b6bbe8cSPaul Moore  * This hook allows a module to allocate a security structure for a TUN	device,
46176b6bbe8cSPaul Moore  * returning the pointer in @security.
46186b6bbe8cSPaul Moore  *
46196b6bbe8cSPaul Moore  * Return: Returns a zero on success, negative values on failure.
46206b6bbe8cSPaul Moore  */
security_tun_dev_alloc_security(void ** security)46215dbbaf2dSPaul Moore int security_tun_dev_alloc_security(void **security)
46225dbbaf2dSPaul Moore {
4623f25fce3eSCasey Schaufler 	return call_int_hook(tun_dev_alloc_security, 0, security);
46245dbbaf2dSPaul Moore }
46255dbbaf2dSPaul Moore EXPORT_SYMBOL(security_tun_dev_alloc_security);
46265dbbaf2dSPaul Moore 
46276b6bbe8cSPaul Moore /**
46286b6bbe8cSPaul Moore  * security_tun_dev_free_security() - Free a TUN device LSM blob
46296b6bbe8cSPaul Moore  * @security: LSM blob
46306b6bbe8cSPaul Moore  *
46316b6bbe8cSPaul Moore  * This hook allows a module to free the security structure for a TUN device.
46326b6bbe8cSPaul Moore  */
security_tun_dev_free_security(void * security)46335dbbaf2dSPaul Moore void security_tun_dev_free_security(void *security)
46345dbbaf2dSPaul Moore {
4635f25fce3eSCasey Schaufler 	call_void_hook(tun_dev_free_security, security);
46365dbbaf2dSPaul Moore }
46375dbbaf2dSPaul Moore EXPORT_SYMBOL(security_tun_dev_free_security);
46385dbbaf2dSPaul Moore 
46396b6bbe8cSPaul Moore /**
46406b6bbe8cSPaul Moore  * security_tun_dev_create() - Check if creating a TUN device is allowed
46416b6bbe8cSPaul Moore  *
46426b6bbe8cSPaul Moore  * Check permissions prior to creating a new TUN device.
46436b6bbe8cSPaul Moore  *
46446b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
46456b6bbe8cSPaul Moore  */
security_tun_dev_create(void)46462b980dbdSPaul Moore int security_tun_dev_create(void)
46472b980dbdSPaul Moore {
4648f25fce3eSCasey Schaufler 	return call_int_hook(tun_dev_create, 0);
46492b980dbdSPaul Moore }
46502b980dbdSPaul Moore EXPORT_SYMBOL(security_tun_dev_create);
46512b980dbdSPaul Moore 
46526b6bbe8cSPaul Moore /**
46536b6bbe8cSPaul Moore  * security_tun_dev_attach_queue() - Check if attaching a TUN queue is allowed
46546b6bbe8cSPaul Moore  * @security: TUN device LSM blob
46556b6bbe8cSPaul Moore  *
46566b6bbe8cSPaul Moore  * Check permissions prior to attaching to a TUN device queue.
46576b6bbe8cSPaul Moore  *
46586b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
46596b6bbe8cSPaul Moore  */
security_tun_dev_attach_queue(void * security)46605dbbaf2dSPaul Moore int security_tun_dev_attach_queue(void *security)
46612b980dbdSPaul Moore {
4662f25fce3eSCasey Schaufler 	return call_int_hook(tun_dev_attach_queue, 0, security);
46632b980dbdSPaul Moore }
46645dbbaf2dSPaul Moore EXPORT_SYMBOL(security_tun_dev_attach_queue);
46652b980dbdSPaul Moore 
46666b6bbe8cSPaul Moore /**
46676b6bbe8cSPaul Moore  * security_tun_dev_attach() - Update TUN device LSM state on attach
46686b6bbe8cSPaul Moore  * @sk: associated sock
46696b6bbe8cSPaul Moore  * @security: TUN device LSM blob
46706b6bbe8cSPaul Moore  *
46716b6bbe8cSPaul Moore  * This hook can be used by the module to update any security state associated
46726b6bbe8cSPaul Moore  * with the TUN device's sock structure.
46736b6bbe8cSPaul Moore  *
46746b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
46756b6bbe8cSPaul Moore  */
security_tun_dev_attach(struct sock * sk,void * security)46765dbbaf2dSPaul Moore int security_tun_dev_attach(struct sock *sk, void *security)
46772b980dbdSPaul Moore {
4678f25fce3eSCasey Schaufler 	return call_int_hook(tun_dev_attach, 0, sk, security);
46792b980dbdSPaul Moore }
46802b980dbdSPaul Moore EXPORT_SYMBOL(security_tun_dev_attach);
46812b980dbdSPaul Moore 
46826b6bbe8cSPaul Moore /**
46836b6bbe8cSPaul Moore  * security_tun_dev_open() - Update TUN device LSM state on open
46846b6bbe8cSPaul Moore  * @security: TUN device LSM blob
46856b6bbe8cSPaul Moore  *
46866b6bbe8cSPaul Moore  * This hook can be used by the module to update any security state associated
46876b6bbe8cSPaul Moore  * with the TUN device's security structure.
46886b6bbe8cSPaul Moore  *
46896b6bbe8cSPaul Moore  * Return: Returns 0 if permission is granted.
46906b6bbe8cSPaul Moore  */
security_tun_dev_open(void * security)46915dbbaf2dSPaul Moore int security_tun_dev_open(void *security)
46925dbbaf2dSPaul Moore {
4693f25fce3eSCasey Schaufler 	return call_int_hook(tun_dev_open, 0, security);
46945dbbaf2dSPaul Moore }
46955dbbaf2dSPaul Moore EXPORT_SYMBOL(security_tun_dev_open);
46965dbbaf2dSPaul Moore 
46974a49f592SPaul Moore /**
46984a49f592SPaul Moore  * security_sctp_assoc_request() - Update the LSM on a SCTP association req
46994a49f592SPaul Moore  * @asoc: SCTP association
47004a49f592SPaul Moore  * @skb: packet requesting the association
47014a49f592SPaul Moore  *
47024a49f592SPaul Moore  * Passes the @asoc and @chunk->skb of the association INIT packet to the LSM.
47034a49f592SPaul Moore  *
47044a49f592SPaul Moore  * Return: Returns 0 on success, error on failure.
47054a49f592SPaul Moore  */
security_sctp_assoc_request(struct sctp_association * asoc,struct sk_buff * skb)470663c1845bSPaul Moore int security_sctp_assoc_request(struct sctp_association *asoc,
470763c1845bSPaul Moore 				struct sk_buff *skb)
470872e89f50SRichard Haines {
4709c081d53fSXin Long 	return call_int_hook(sctp_assoc_request, 0, asoc, skb);
471072e89f50SRichard Haines }
471172e89f50SRichard Haines EXPORT_SYMBOL(security_sctp_assoc_request);
471272e89f50SRichard Haines 
47134a49f592SPaul Moore /**
47144a49f592SPaul Moore  * security_sctp_bind_connect() - Validate a list of addrs for a SCTP option
47154a49f592SPaul Moore  * @sk: socket
47164a49f592SPaul Moore  * @optname: SCTP option to validate
47174a49f592SPaul Moore  * @address: list of IP addresses to validate
47184a49f592SPaul Moore  * @addrlen: length of the address list
47194a49f592SPaul Moore  *
47204a49f592SPaul Moore  * Validiate permissions required for each address associated with sock	@sk.
47214a49f592SPaul Moore  * Depending on @optname, the addresses will be treated as either a connect or
47224a49f592SPaul Moore  * bind service. The @addrlen is calculated on each IPv4 and IPv6 address using
47234a49f592SPaul Moore  * sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6).
47244a49f592SPaul Moore  *
47254a49f592SPaul Moore  * Return: Returns 0 on success, error on failure.
47264a49f592SPaul Moore  */
security_sctp_bind_connect(struct sock * sk,int optname,struct sockaddr * address,int addrlen)472772e89f50SRichard Haines int security_sctp_bind_connect(struct sock *sk, int optname,
472872e89f50SRichard Haines 			       struct sockaddr *address, int addrlen)
472972e89f50SRichard Haines {
473072e89f50SRichard Haines 	return call_int_hook(sctp_bind_connect, 0, sk, optname,
473172e89f50SRichard Haines 			     address, addrlen);
473272e89f50SRichard Haines }
473372e89f50SRichard Haines EXPORT_SYMBOL(security_sctp_bind_connect);
473472e89f50SRichard Haines 
47354a49f592SPaul Moore /**
47364a49f592SPaul Moore  * security_sctp_sk_clone() - Clone a SCTP sock's LSM state
47374a49f592SPaul Moore  * @asoc: SCTP association
47384a49f592SPaul Moore  * @sk: original sock
47394a49f592SPaul Moore  * @newsk: target sock
47404a49f592SPaul Moore  *
47414a49f592SPaul Moore  * Called whenever a new socket is created by accept(2) (i.e. a TCP style
47424a49f592SPaul Moore  * socket) or when a socket is 'peeled off' e.g userspace calls
47434a49f592SPaul Moore  * sctp_peeloff(3).
47444a49f592SPaul Moore  */
security_sctp_sk_clone(struct sctp_association * asoc,struct sock * sk,struct sock * newsk)4745c081d53fSXin Long void security_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
474672e89f50SRichard Haines 			    struct sock *newsk)
474772e89f50SRichard Haines {
4748c081d53fSXin Long 	call_void_hook(sctp_sk_clone, asoc, sk, newsk);
474972e89f50SRichard Haines }
475072e89f50SRichard Haines EXPORT_SYMBOL(security_sctp_sk_clone);
475172e89f50SRichard Haines 
47524a49f592SPaul Moore /**
47534a49f592SPaul Moore  * security_sctp_assoc_established() - Update LSM state when assoc established
47544a49f592SPaul Moore  * @asoc: SCTP association
47554a49f592SPaul Moore  * @skb: packet establishing the association
47564a49f592SPaul Moore  *
47574a49f592SPaul Moore  * Passes the @asoc and @chunk->skb of the association COOKIE_ACK packet to the
47584a49f592SPaul Moore  * security module.
47594a49f592SPaul Moore  *
47604a49f592SPaul Moore  * Return: Returns 0 if permission is granted.
47614a49f592SPaul Moore  */
security_sctp_assoc_established(struct sctp_association * asoc,struct sk_buff * skb)47625e50f5d4SOndrej Mosnacek int security_sctp_assoc_established(struct sctp_association *asoc,
47635e50f5d4SOndrej Mosnacek 				    struct sk_buff *skb)
47645e50f5d4SOndrej Mosnacek {
47655e50f5d4SOndrej Mosnacek 	return call_int_hook(sctp_assoc_established, 0, asoc, skb);
47665e50f5d4SOndrej Mosnacek }
47675e50f5d4SOndrej Mosnacek EXPORT_SYMBOL(security_sctp_assoc_established);
47685e50f5d4SOndrej Mosnacek 
4769e3d9387fSPaolo Abeni /**
4770e3d9387fSPaolo Abeni  * security_mptcp_add_subflow() - Inherit the LSM label from the MPTCP socket
4771e3d9387fSPaolo Abeni  * @sk: the owning MPTCP socket
4772e3d9387fSPaolo Abeni  * @ssk: the new subflow
4773e3d9387fSPaolo Abeni  *
4774e3d9387fSPaolo Abeni  * Update the labeling for the given MPTCP subflow, to match the one of the
4775e3d9387fSPaolo Abeni  * owning MPTCP socket. This hook has to be called after the socket creation and
4776e3d9387fSPaolo Abeni  * initialization via the security_socket_create() and
4777e3d9387fSPaolo Abeni  * security_socket_post_create() LSM hooks.
4778e3d9387fSPaolo Abeni  *
4779e3d9387fSPaolo Abeni  * Return: Returns 0 on success or a negative error code on failure.
4780e3d9387fSPaolo Abeni  */
security_mptcp_add_subflow(struct sock * sk,struct sock * ssk)4781e3d9387fSPaolo Abeni int security_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
4782e3d9387fSPaolo Abeni {
4783e3d9387fSPaolo Abeni 	return call_int_hook(mptcp_add_subflow, 0, sk, ssk);
4784e3d9387fSPaolo Abeni }
4785e3d9387fSPaolo Abeni 
478620510f2fSJames Morris #endif	/* CONFIG_SECURITY_NETWORK */
478720510f2fSJames Morris 
4788d291f1a6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND
4789ac318aedSPaul Moore /**
4790ac318aedSPaul Moore  * security_ib_pkey_access() - Check if access to an IB pkey is allowed
4791ac318aedSPaul Moore  * @sec: LSM blob
4792ac318aedSPaul Moore  * @subnet_prefix: subnet prefix of the port
4793ac318aedSPaul Moore  * @pkey: IB pkey
4794ac318aedSPaul Moore  *
47954432b507SPaul Moore  * Check permission to access a pkey when modifying a QP.
4796ac318aedSPaul Moore  *
4797ac318aedSPaul Moore  * Return: Returns 0 if permission is granted.
4798ac318aedSPaul Moore  */
security_ib_pkey_access(void * sec,u64 subnet_prefix,u16 pkey)4799d291f1a6SDaniel Jurgens int security_ib_pkey_access(void *sec, u64 subnet_prefix, u16 pkey)
4800d291f1a6SDaniel Jurgens {
4801d291f1a6SDaniel Jurgens 	return call_int_hook(ib_pkey_access, 0, sec, subnet_prefix, pkey);
4802d291f1a6SDaniel Jurgens }
4803d291f1a6SDaniel Jurgens EXPORT_SYMBOL(security_ib_pkey_access);
4804d291f1a6SDaniel Jurgens 
4805ac318aedSPaul Moore /**
4806ac318aedSPaul Moore  * security_ib_endport_manage_subnet() - Check if SMPs traffic is allowed
4807ac318aedSPaul Moore  * @sec: LSM blob
4808ac318aedSPaul Moore  * @dev_name: IB device name
4809ac318aedSPaul Moore  * @port_num: port number
4810ac318aedSPaul Moore  *
4811ac318aedSPaul Moore  * Check permissions to send and receive SMPs on a end port.
4812ac318aedSPaul Moore  *
4813ac318aedSPaul Moore  * Return: Returns 0 if permission is granted.
4814ac318aedSPaul Moore  */
security_ib_endport_manage_subnet(void * sec,const char * dev_name,u8 port_num)481563c1845bSPaul Moore int security_ib_endport_manage_subnet(void *sec,
481663c1845bSPaul Moore 				      const char *dev_name, u8 port_num)
481747a2b338SDaniel Jurgens {
481863c1845bSPaul Moore 	return call_int_hook(ib_endport_manage_subnet, 0, sec,
481963c1845bSPaul Moore 			     dev_name, port_num);
482047a2b338SDaniel Jurgens }
482147a2b338SDaniel Jurgens EXPORT_SYMBOL(security_ib_endport_manage_subnet);
482247a2b338SDaniel Jurgens 
4823ac318aedSPaul Moore /**
4824ac318aedSPaul Moore  * security_ib_alloc_security() - Allocate an Infiniband LSM blob
4825ac318aedSPaul Moore  * @sec: LSM blob
4826ac318aedSPaul Moore  *
4827ac318aedSPaul Moore  * Allocate a security structure for Infiniband objects.
4828ac318aedSPaul Moore  *
4829ac318aedSPaul Moore  * Return: Returns 0 on success, non-zero on failure.
4830ac318aedSPaul Moore  */
security_ib_alloc_security(void ** sec)4831d291f1a6SDaniel Jurgens int security_ib_alloc_security(void **sec)
4832d291f1a6SDaniel Jurgens {
4833d291f1a6SDaniel Jurgens 	return call_int_hook(ib_alloc_security, 0, sec);
4834d291f1a6SDaniel Jurgens }
4835d291f1a6SDaniel Jurgens EXPORT_SYMBOL(security_ib_alloc_security);
4836d291f1a6SDaniel Jurgens 
4837ac318aedSPaul Moore /**
4838ac318aedSPaul Moore  * security_ib_free_security() - Free an Infiniband LSM blob
4839ac318aedSPaul Moore  * @sec: LSM blob
4840ac318aedSPaul Moore  *
4841ac318aedSPaul Moore  * Deallocate an Infiniband security structure.
4842ac318aedSPaul Moore  */
security_ib_free_security(void * sec)4843d291f1a6SDaniel Jurgens void security_ib_free_security(void *sec)
4844d291f1a6SDaniel Jurgens {
4845d291f1a6SDaniel Jurgens 	call_void_hook(ib_free_security, sec);
4846d291f1a6SDaniel Jurgens }
4847d291f1a6SDaniel Jurgens EXPORT_SYMBOL(security_ib_free_security);
4848d291f1a6SDaniel Jurgens #endif	/* CONFIG_SECURITY_INFINIBAND */
4849d291f1a6SDaniel Jurgens 
485020510f2fSJames Morris #ifdef CONFIG_SECURITY_NETWORK_XFRM
4851742b9945SPaul Moore /**
4852742b9945SPaul Moore  * security_xfrm_policy_alloc() - Allocate a xfrm policy LSM blob
4853742b9945SPaul Moore  * @ctxp: xfrm security context being added to the SPD
4854742b9945SPaul Moore  * @sec_ctx: security label provided by userspace
4855742b9945SPaul Moore  * @gfp: gfp flags
4856742b9945SPaul Moore  *
4857742b9945SPaul Moore  * Allocate a security structure to the xp->security field; the security field
4858742b9945SPaul Moore  * is initialized to NULL when the xfrm_policy is allocated.
4859742b9945SPaul Moore  *
4860742b9945SPaul Moore  * Return:  Return 0 if operation was successful.
4861742b9945SPaul Moore  */
security_xfrm_policy_alloc(struct xfrm_sec_ctx ** ctxp,struct xfrm_user_sec_ctx * sec_ctx,gfp_t gfp)486252a4c640SNikolay Aleksandrov int security_xfrm_policy_alloc(struct xfrm_sec_ctx **ctxp,
486352a4c640SNikolay Aleksandrov 			       struct xfrm_user_sec_ctx *sec_ctx,
486452a4c640SNikolay Aleksandrov 			       gfp_t gfp)
486520510f2fSJames Morris {
4866f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_policy_alloc_security, 0, ctxp, sec_ctx, gfp);
486720510f2fSJames Morris }
486820510f2fSJames Morris EXPORT_SYMBOL(security_xfrm_policy_alloc);
486920510f2fSJames Morris 
4870742b9945SPaul Moore /**
4871742b9945SPaul Moore  * security_xfrm_policy_clone() - Clone xfrm policy LSM state
4872742b9945SPaul Moore  * @old_ctx: xfrm security context
4873742b9945SPaul Moore  * @new_ctxp: target xfrm security context
4874742b9945SPaul Moore  *
4875742b9945SPaul Moore  * Allocate a security structure in new_ctxp that contains the information from
4876742b9945SPaul Moore  * the old_ctx structure.
4877742b9945SPaul Moore  *
4878742b9945SPaul Moore  * Return: Return 0 if operation was successful.
4879742b9945SPaul Moore  */
security_xfrm_policy_clone(struct xfrm_sec_ctx * old_ctx,struct xfrm_sec_ctx ** new_ctxp)488003e1ad7bSPaul Moore int security_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
488103e1ad7bSPaul Moore 			       struct xfrm_sec_ctx **new_ctxp)
488220510f2fSJames Morris {
4883f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_policy_clone_security, 0, old_ctx, new_ctxp);
488420510f2fSJames Morris }
488520510f2fSJames Morris 
4886742b9945SPaul Moore /**
4887742b9945SPaul Moore  * security_xfrm_policy_free() - Free a xfrm security context
4888742b9945SPaul Moore  * @ctx: xfrm security context
4889742b9945SPaul Moore  *
4890742b9945SPaul Moore  * Free LSM resources associated with @ctx.
4891742b9945SPaul Moore  */
security_xfrm_policy_free(struct xfrm_sec_ctx * ctx)489203e1ad7bSPaul Moore void security_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
489320510f2fSJames Morris {
4894f25fce3eSCasey Schaufler 	call_void_hook(xfrm_policy_free_security, ctx);
489520510f2fSJames Morris }
489620510f2fSJames Morris EXPORT_SYMBOL(security_xfrm_policy_free);
489720510f2fSJames Morris 
4898742b9945SPaul Moore /**
4899742b9945SPaul Moore  * security_xfrm_policy_delete() - Check if deleting a xfrm policy is allowed
4900742b9945SPaul Moore  * @ctx: xfrm security context
4901742b9945SPaul Moore  *
4902742b9945SPaul Moore  * Authorize deletion of a SPD entry.
4903742b9945SPaul Moore  *
4904742b9945SPaul Moore  * Return: Returns 0 if permission is granted.
4905742b9945SPaul Moore  */
security_xfrm_policy_delete(struct xfrm_sec_ctx * ctx)490603e1ad7bSPaul Moore int security_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
490720510f2fSJames Morris {
4908f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_policy_delete_security, 0, ctx);
490920510f2fSJames Morris }
491020510f2fSJames Morris 
4911742b9945SPaul Moore /**
4912742b9945SPaul Moore  * security_xfrm_state_alloc() - Allocate a xfrm state LSM blob
4913742b9945SPaul Moore  * @x: xfrm state being added to the SAD
4914742b9945SPaul Moore  * @sec_ctx: security label provided by userspace
4915742b9945SPaul Moore  *
4916742b9945SPaul Moore  * Allocate a security structure to the @x->security field; the security field
4917742b9945SPaul Moore  * is initialized to NULL when the xfrm_state is allocated. Set the context to
4918742b9945SPaul Moore  * correspond to @sec_ctx.
4919742b9945SPaul Moore  *
4920742b9945SPaul Moore  * Return: Return 0 if operation was successful.
4921742b9945SPaul Moore  */
security_xfrm_state_alloc(struct xfrm_state * x,struct xfrm_user_sec_ctx * sec_ctx)49222e5aa866SPaul Moore int security_xfrm_state_alloc(struct xfrm_state *x,
49232e5aa866SPaul Moore 			      struct xfrm_user_sec_ctx *sec_ctx)
492420510f2fSJames Morris {
4925f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_state_alloc, 0, x, sec_ctx);
492620510f2fSJames Morris }
492720510f2fSJames Morris EXPORT_SYMBOL(security_xfrm_state_alloc);
492820510f2fSJames Morris 
4929742b9945SPaul Moore /**
4930742b9945SPaul Moore  * security_xfrm_state_alloc_acquire() - Allocate a xfrm state LSM blob
4931742b9945SPaul Moore  * @x: xfrm state being added to the SAD
4932742b9945SPaul Moore  * @polsec: associated policy's security context
4933742b9945SPaul Moore  * @secid: secid from the flow
4934742b9945SPaul Moore  *
4935742b9945SPaul Moore  * Allocate a security structure to the x->security field; the security field
4936742b9945SPaul Moore  * is initialized to NULL when the xfrm_state is allocated.  Set the context to
4937742b9945SPaul Moore  * correspond to secid.
4938742b9945SPaul Moore  *
4939742b9945SPaul Moore  * Return: Returns 0 if operation was successful.
4940742b9945SPaul Moore  */
security_xfrm_state_alloc_acquire(struct xfrm_state * x,struct xfrm_sec_ctx * polsec,u32 secid)494120510f2fSJames Morris int security_xfrm_state_alloc_acquire(struct xfrm_state *x,
494220510f2fSJames Morris 				      struct xfrm_sec_ctx *polsec, u32 secid)
494320510f2fSJames Morris {
4944f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_state_alloc_acquire, 0, x, polsec, secid);
494520510f2fSJames Morris }
494620510f2fSJames Morris 
4947742b9945SPaul Moore /**
4948742b9945SPaul Moore  * security_xfrm_state_delete() - Check if deleting a xfrm state is allowed
4949742b9945SPaul Moore  * @x: xfrm state
4950742b9945SPaul Moore  *
4951742b9945SPaul Moore  * Authorize deletion of x->security.
4952742b9945SPaul Moore  *
4953742b9945SPaul Moore  * Return: Returns 0 if permission is granted.
4954742b9945SPaul Moore  */
security_xfrm_state_delete(struct xfrm_state * x)495520510f2fSJames Morris int security_xfrm_state_delete(struct xfrm_state *x)
495620510f2fSJames Morris {
4957f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_state_delete_security, 0, x);
495820510f2fSJames Morris }
495920510f2fSJames Morris EXPORT_SYMBOL(security_xfrm_state_delete);
496020510f2fSJames Morris 
4961742b9945SPaul Moore /**
4962742b9945SPaul Moore  * security_xfrm_state_free() - Free a xfrm state
4963742b9945SPaul Moore  * @x: xfrm state
4964742b9945SPaul Moore  *
4965742b9945SPaul Moore  * Deallocate x->security.
4966742b9945SPaul Moore  */
security_xfrm_state_free(struct xfrm_state * x)496720510f2fSJames Morris void security_xfrm_state_free(struct xfrm_state *x)
496820510f2fSJames Morris {
4969f25fce3eSCasey Schaufler 	call_void_hook(xfrm_state_free_security, x);
497020510f2fSJames Morris }
497120510f2fSJames Morris 
4972742b9945SPaul Moore /**
4973742b9945SPaul Moore  * security_xfrm_policy_lookup() - Check if using a xfrm policy is allowed
4974742b9945SPaul Moore  * @ctx: target xfrm security context
4975742b9945SPaul Moore  * @fl_secid: flow secid used to authorize access
4976742b9945SPaul Moore  *
4977742b9945SPaul Moore  * Check permission when a flow selects a xfrm_policy for processing XFRMs on a
4978742b9945SPaul Moore  * packet.  The hook is called when selecting either a per-socket policy or a
4979742b9945SPaul Moore  * generic xfrm policy.
4980742b9945SPaul Moore  *
4981742b9945SPaul Moore  * Return: Return 0 if permission is granted, -ESRCH otherwise, or -errno on
4982742b9945SPaul Moore  *         other errors.
4983742b9945SPaul Moore  */
security_xfrm_policy_lookup(struct xfrm_sec_ctx * ctx,u32 fl_secid)49848a922805SZhongjun Tan int security_xfrm_policy_lookup(struct xfrm_sec_ctx *ctx, u32 fl_secid)
498520510f2fSJames Morris {
49868a922805SZhongjun Tan 	return call_int_hook(xfrm_policy_lookup, 0, ctx, fl_secid);
498720510f2fSJames Morris }
498820510f2fSJames Morris 
4989742b9945SPaul Moore /**
4990742b9945SPaul Moore  * security_xfrm_state_pol_flow_match() - Check for a xfrm match
4991742b9945SPaul Moore  * @x: xfrm state to match
49921e2523d7SPaul Moore  * @xp: xfrm policy to check for a match
4993742b9945SPaul Moore  * @flic: flow to check for a match.
4994742b9945SPaul Moore  *
4995742b9945SPaul Moore  * Check @xp and @flic for a match with @x.
4996742b9945SPaul Moore  *
4997742b9945SPaul Moore  * Return: Returns 1 if there is a match.
4998742b9945SPaul Moore  */
security_xfrm_state_pol_flow_match(struct xfrm_state * x,struct xfrm_policy * xp,const struct flowi_common * flic)499920510f2fSJames Morris int security_xfrm_state_pol_flow_match(struct xfrm_state *x,
5000e33f7704SDavid S. Miller 				       struct xfrm_policy *xp,
50013df98d79SPaul Moore 				       const struct flowi_common *flic)
500220510f2fSJames Morris {
5003b1d9e6b0SCasey Schaufler 	struct security_hook_list *hp;
500498e828a0SKP Singh 	int rc = LSM_RET_DEFAULT(xfrm_state_pol_flow_match);
5005b1d9e6b0SCasey Schaufler 
5006b1d9e6b0SCasey Schaufler 	/*
5007b1d9e6b0SCasey Schaufler 	 * Since this function is expected to return 0 or 1, the judgment
5008b1d9e6b0SCasey Schaufler 	 * becomes difficult if multiple LSMs supply this call. Fortunately,
5009b1d9e6b0SCasey Schaufler 	 * we can use the first LSM's judgment because currently only SELinux
5010b1d9e6b0SCasey Schaufler 	 * supplies this call.
5011b1d9e6b0SCasey Schaufler 	 *
5012b1d9e6b0SCasey Schaufler 	 * For speed optimization, we explicitly break the loop rather than
5013b1d9e6b0SCasey Schaufler 	 * using the macro
5014b1d9e6b0SCasey Schaufler 	 */
5015df0ce173SSargun Dhillon 	hlist_for_each_entry(hp, &security_hook_heads.xfrm_state_pol_flow_match,
5016b1d9e6b0SCasey Schaufler 			     list) {
50173df98d79SPaul Moore 		rc = hp->hook.xfrm_state_pol_flow_match(x, xp, flic);
5018b1d9e6b0SCasey Schaufler 		break;
5019b1d9e6b0SCasey Schaufler 	}
5020b1d9e6b0SCasey Schaufler 	return rc;
502120510f2fSJames Morris }
502220510f2fSJames Morris 
5023742b9945SPaul Moore /**
5024742b9945SPaul Moore  * security_xfrm_decode_session() - Determine the xfrm secid for a packet
5025742b9945SPaul Moore  * @skb: xfrm packet
5026742b9945SPaul Moore  * @secid: secid
5027742b9945SPaul Moore  *
5028742b9945SPaul Moore  * Decode the packet in @skb and return the security label in @secid.
5029742b9945SPaul Moore  *
5030742b9945SPaul Moore  * Return: Return 0 if all xfrms used have the same secid.
5031742b9945SPaul Moore  */
security_xfrm_decode_session(struct sk_buff * skb,u32 * secid)503220510f2fSJames Morris int security_xfrm_decode_session(struct sk_buff *skb, u32 *secid)
503320510f2fSJames Morris {
5034f25fce3eSCasey Schaufler 	return call_int_hook(xfrm_decode_session, 0, skb, secid, 1);
503520510f2fSJames Morris }
503620510f2fSJames Morris 
security_skb_classify_flow(struct sk_buff * skb,struct flowi_common * flic)50373df98d79SPaul Moore void security_skb_classify_flow(struct sk_buff *skb, struct flowi_common *flic)
503820510f2fSJames Morris {
50393df98d79SPaul Moore 	int rc = call_int_hook(xfrm_decode_session, 0, skb, &flic->flowic_secid,
5040f25fce3eSCasey Schaufler 			       0);
504120510f2fSJames Morris 
504220510f2fSJames Morris 	BUG_ON(rc);
504320510f2fSJames Morris }
504420510f2fSJames Morris EXPORT_SYMBOL(security_skb_classify_flow);
504520510f2fSJames Morris #endif	/* CONFIG_SECURITY_NETWORK_XFRM */
504620510f2fSJames Morris 
504720510f2fSJames Morris #ifdef CONFIG_KEYS
5048ecc419a4SPaul Moore /**
5049ecc419a4SPaul Moore  * security_key_alloc() - Allocate and initialize a kernel key LSM blob
5050ecc419a4SPaul Moore  * @key: key
5051ecc419a4SPaul Moore  * @cred: credentials
5052ecc419a4SPaul Moore  * @flags: allocation flags
5053ecc419a4SPaul Moore  *
5054ecc419a4SPaul Moore  * Permit allocation of a key and assign security data. Note that key does not
5055ecc419a4SPaul Moore  * have a serial number assigned at this point.
5056ecc419a4SPaul Moore  *
5057ecc419a4SPaul Moore  * Return: Return 0 if permission is granted, -ve error otherwise.
5058ecc419a4SPaul Moore  */
security_key_alloc(struct key * key,const struct cred * cred,unsigned long flags)5059d84f4f99SDavid Howells int security_key_alloc(struct key *key, const struct cred *cred,
5060d84f4f99SDavid Howells 		       unsigned long flags)
506120510f2fSJames Morris {
5062f25fce3eSCasey Schaufler 	return call_int_hook(key_alloc, 0, key, cred, flags);
506320510f2fSJames Morris }
506420510f2fSJames Morris 
5065ecc419a4SPaul Moore /**
5066ecc419a4SPaul Moore  * security_key_free() - Free a kernel key LSM blob
5067ecc419a4SPaul Moore  * @key: key
5068ecc419a4SPaul Moore  *
5069ecc419a4SPaul Moore  * Notification of destruction; free security data.
5070ecc419a4SPaul Moore  */
security_key_free(struct key * key)507120510f2fSJames Morris void security_key_free(struct key *key)
507220510f2fSJames Morris {
5073f25fce3eSCasey Schaufler 	call_void_hook(key_free, key);
507420510f2fSJames Morris }
507520510f2fSJames Morris 
5076ecc419a4SPaul Moore /**
5077ecc419a4SPaul Moore  * security_key_permission() - Check if a kernel key operation is allowed
5078ecc419a4SPaul Moore  * @key_ref: key reference
5079ecc419a4SPaul Moore  * @cred: credentials of actor requesting access
5080ecc419a4SPaul Moore  * @need_perm: requested permissions
5081ecc419a4SPaul Moore  *
5082ecc419a4SPaul Moore  * See whether a specific operational right is granted to a process on a key.
5083ecc419a4SPaul Moore  *
5084ecc419a4SPaul Moore  * Return: Return 0 if permission is granted, -ve error otherwise.
5085ecc419a4SPaul Moore  */
security_key_permission(key_ref_t key_ref,const struct cred * cred,enum key_need_perm need_perm)50868c0637e9SDavid Howells int security_key_permission(key_ref_t key_ref, const struct cred *cred,
50878c0637e9SDavid Howells 			    enum key_need_perm need_perm)
508820510f2fSJames Morris {
50898c0637e9SDavid Howells 	return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
509020510f2fSJames Morris }
509120510f2fSJames Morris 
5092ecc419a4SPaul Moore /**
5093ecc419a4SPaul Moore  * security_key_getsecurity() - Get the key's security label
5094ecc419a4SPaul Moore  * @key: key
5095b3816cf8SPaul Moore  * @buffer: security label buffer
5096ecc419a4SPaul Moore  *
5097ecc419a4SPaul Moore  * Get a textual representation of the security context attached to a key for
5098ecc419a4SPaul Moore  * the purposes of honouring KEYCTL_GETSECURITY.  This function allocates the
5099ecc419a4SPaul Moore  * storage for the NUL-terminated string and the caller should free it.
5100ecc419a4SPaul Moore  *
5101b3816cf8SPaul Moore  * Return: Returns the length of @buffer (including terminating NUL) or -ve if
5102ecc419a4SPaul Moore  *         an error occurs.  May also return 0 (and a NULL buffer pointer) if
5103ecc419a4SPaul Moore  *         there is no security label assigned to the key.
5104ecc419a4SPaul Moore  */
security_key_getsecurity(struct key * key,char ** buffer)5105b3816cf8SPaul Moore int security_key_getsecurity(struct key *key, char **buffer)
510670a5bb72SDavid Howells {
5107b3816cf8SPaul Moore 	*buffer = NULL;
5108b3816cf8SPaul Moore 	return call_int_hook(key_getsecurity, 0, key, buffer);
510970a5bb72SDavid Howells }
511020510f2fSJames Morris #endif	/* CONFIG_KEYS */
511103d37d25SAhmed S. Darwish 
511203d37d25SAhmed S. Darwish #ifdef CONFIG_AUDIT
5113b14faf9cSPaul Moore /**
5114b14faf9cSPaul Moore  * security_audit_rule_init() - Allocate and init an LSM audit rule struct
5115b14faf9cSPaul Moore  * @field: audit action
5116b14faf9cSPaul Moore  * @op: rule operator
5117b14faf9cSPaul Moore  * @rulestr: rule context
5118b14faf9cSPaul Moore  * @lsmrule: receive buffer for audit rule struct
511928d0ecc5SGUO Zihua  * @gfp: GFP flag used for kmalloc
5120b14faf9cSPaul Moore  *
5121b14faf9cSPaul Moore  * Allocate and initialize an LSM audit rule structure.
5122b14faf9cSPaul Moore  *
5123b14faf9cSPaul Moore  * Return: Return 0 if @lsmrule has been successfully set, -EINVAL in case of
5124b14faf9cSPaul Moore  *         an invalid rule.
5125b14faf9cSPaul Moore  */
security_audit_rule_init(u32 field,u32 op,char * rulestr,void ** lsmrule,gfp_t gfp)512628d0ecc5SGUO Zihua int security_audit_rule_init(u32 field, u32 op, char *rulestr, void **lsmrule,
512728d0ecc5SGUO Zihua 			     gfp_t gfp)
512803d37d25SAhmed S. Darwish {
512928d0ecc5SGUO Zihua 	return call_int_hook(audit_rule_init, 0, field, op, rulestr, lsmrule, gfp);
513003d37d25SAhmed S. Darwish }
513103d37d25SAhmed S. Darwish 
5132b14faf9cSPaul Moore /**
5133b14faf9cSPaul Moore  * security_audit_rule_known() - Check if an audit rule contains LSM fields
5134b14faf9cSPaul Moore  * @krule: audit rule
5135b14faf9cSPaul Moore  *
5136b14faf9cSPaul Moore  * Specifies whether given @krule contains any fields related to the current
5137b14faf9cSPaul Moore  * LSM.
5138b14faf9cSPaul Moore  *
5139b14faf9cSPaul Moore  * Return: Returns 1 in case of relation found, 0 otherwise.
5140b14faf9cSPaul Moore  */
security_audit_rule_known(struct audit_krule * krule)514103d37d25SAhmed S. Darwish int security_audit_rule_known(struct audit_krule *krule)
514203d37d25SAhmed S. Darwish {
5143f25fce3eSCasey Schaufler 	return call_int_hook(audit_rule_known, 0, krule);
514403d37d25SAhmed S. Darwish }
514503d37d25SAhmed S. Darwish 
5146b14faf9cSPaul Moore /**
5147b14faf9cSPaul Moore  * security_audit_rule_free() - Free an LSM audit rule struct
5148b14faf9cSPaul Moore  * @lsmrule: audit rule struct
5149b14faf9cSPaul Moore  *
5150b14faf9cSPaul Moore  * Deallocate the LSM audit rule structure previously allocated by
5151b14faf9cSPaul Moore  * audit_rule_init().
5152b14faf9cSPaul Moore  */
security_audit_rule_free(void * lsmrule)515303d37d25SAhmed S. Darwish void security_audit_rule_free(void *lsmrule)
515403d37d25SAhmed S. Darwish {
5155f25fce3eSCasey Schaufler 	call_void_hook(audit_rule_free, lsmrule);
515603d37d25SAhmed S. Darwish }
515703d37d25SAhmed S. Darwish 
5158b14faf9cSPaul Moore /**
5159b14faf9cSPaul Moore  * security_audit_rule_match() - Check if a label matches an audit rule
5160b14faf9cSPaul Moore  * @secid: security label
5161b14faf9cSPaul Moore  * @field: LSM audit field
5162b14faf9cSPaul Moore  * @op: matching operator
5163b14faf9cSPaul Moore  * @lsmrule: audit rule
5164b14faf9cSPaul Moore  *
5165b14faf9cSPaul Moore  * Determine if given @secid matches a rule previously approved by
5166b14faf9cSPaul Moore  * security_audit_rule_known().
5167b14faf9cSPaul Moore  *
5168b14faf9cSPaul Moore  * Return: Returns 1 if secid matches the rule, 0 if it does not, -ERRNO on
5169b14faf9cSPaul Moore  *         failure.
5170b14faf9cSPaul Moore  */
security_audit_rule_match(u32 secid,u32 field,u32 op,void * lsmrule)517190462a5bSRichard Guy Briggs int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule)
517203d37d25SAhmed S. Darwish {
517390462a5bSRichard Guy Briggs 	return call_int_hook(audit_rule_match, 0, secid, field, op, lsmrule);
517403d37d25SAhmed S. Darwish }
517503d37d25SAhmed S. Darwish #endif /* CONFIG_AUDIT */
5176afdb09c7SChenbo Feng 
5177afdb09c7SChenbo Feng #ifdef CONFIG_BPF_SYSCALL
517855e85320SPaul Moore /**
517955e85320SPaul Moore  * security_bpf() - Check if the bpf syscall operation is allowed
518055e85320SPaul Moore  * @cmd: command
518155e85320SPaul Moore  * @attr: bpf attribute
518255e85320SPaul Moore  * @size: size
518355e85320SPaul Moore  *
518455e85320SPaul Moore  * Do a initial check for all bpf syscalls after the attribute is copied into
518555e85320SPaul Moore  * the kernel. The actual security module can implement their own rules to
518655e85320SPaul Moore  * check the specific cmd they need.
518755e85320SPaul Moore  *
518855e85320SPaul Moore  * Return: Returns 0 if permission is granted.
518955e85320SPaul Moore  */
security_bpf(int cmd,union bpf_attr * attr,unsigned int size)5190afdb09c7SChenbo Feng int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5191afdb09c7SChenbo Feng {
5192afdb09c7SChenbo Feng 	return call_int_hook(bpf, 0, cmd, attr, size);
5193afdb09c7SChenbo Feng }
519455e85320SPaul Moore 
519555e85320SPaul Moore /**
519655e85320SPaul Moore  * security_bpf_map() - Check if access to a bpf map is allowed
519755e85320SPaul Moore  * @map: bpf map
519855e85320SPaul Moore  * @fmode: mode
519955e85320SPaul Moore  *
520055e85320SPaul Moore  * Do a check when the kernel generates and returns a file descriptor for eBPF
520155e85320SPaul Moore  * maps.
520255e85320SPaul Moore  *
520355e85320SPaul Moore  * Return: Returns 0 if permission is granted.
520455e85320SPaul Moore  */
security_bpf_map(struct bpf_map * map,fmode_t fmode)5205afdb09c7SChenbo Feng int security_bpf_map(struct bpf_map *map, fmode_t fmode)
5206afdb09c7SChenbo Feng {
5207afdb09c7SChenbo Feng 	return call_int_hook(bpf_map, 0, map, fmode);
5208afdb09c7SChenbo Feng }
520955e85320SPaul Moore 
521055e85320SPaul Moore /**
521155e85320SPaul Moore  * security_bpf_prog() - Check if access to a bpf program is allowed
521255e85320SPaul Moore  * @prog: bpf program
521355e85320SPaul Moore  *
521455e85320SPaul Moore  * Do a check when the kernel generates and returns a file descriptor for eBPF
521555e85320SPaul Moore  * programs.
521655e85320SPaul Moore  *
521755e85320SPaul Moore  * Return: Returns 0 if permission is granted.
521855e85320SPaul Moore  */
security_bpf_prog(struct bpf_prog * prog)5219afdb09c7SChenbo Feng int security_bpf_prog(struct bpf_prog *prog)
5220afdb09c7SChenbo Feng {
5221afdb09c7SChenbo Feng 	return call_int_hook(bpf_prog, 0, prog);
5222afdb09c7SChenbo Feng }
522355e85320SPaul Moore 
522455e85320SPaul Moore /**
522555e85320SPaul Moore  * security_bpf_map_alloc() - Allocate a bpf map LSM blob
522655e85320SPaul Moore  * @map: bpf map
522755e85320SPaul Moore  *
522855e85320SPaul Moore  * Initialize the security field inside bpf map.
522955e85320SPaul Moore  *
523055e85320SPaul Moore  * Return: Returns 0 on success, error on failure.
523155e85320SPaul Moore  */
security_bpf_map_alloc(struct bpf_map * map)5232afdb09c7SChenbo Feng int security_bpf_map_alloc(struct bpf_map *map)
5233afdb09c7SChenbo Feng {
5234afdb09c7SChenbo Feng 	return call_int_hook(bpf_map_alloc_security, 0, map);
5235afdb09c7SChenbo Feng }
523655e85320SPaul Moore 
523755e85320SPaul Moore /**
523855e85320SPaul Moore  * security_bpf_prog_alloc() - Allocate a bpf program LSM blob
523955e85320SPaul Moore  * @aux: bpf program aux info struct
524055e85320SPaul Moore  *
524155e85320SPaul Moore  * Initialize the security field inside bpf program.
524255e85320SPaul Moore  *
524355e85320SPaul Moore  * Return: Returns 0 on success, error on failure.
524455e85320SPaul Moore  */
security_bpf_prog_alloc(struct bpf_prog_aux * aux)5245afdb09c7SChenbo Feng int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
5246afdb09c7SChenbo Feng {
5247afdb09c7SChenbo Feng 	return call_int_hook(bpf_prog_alloc_security, 0, aux);
5248afdb09c7SChenbo Feng }
524955e85320SPaul Moore 
525055e85320SPaul Moore /**
525155e85320SPaul Moore  * security_bpf_map_free() - Free a bpf map's LSM blob
525255e85320SPaul Moore  * @map: bpf map
525355e85320SPaul Moore  *
525455e85320SPaul Moore  * Clean up the security information stored inside bpf map.
525555e85320SPaul Moore  */
security_bpf_map_free(struct bpf_map * map)5256afdb09c7SChenbo Feng void security_bpf_map_free(struct bpf_map *map)
5257afdb09c7SChenbo Feng {
5258afdb09c7SChenbo Feng 	call_void_hook(bpf_map_free_security, map);
5259afdb09c7SChenbo Feng }
526055e85320SPaul Moore 
526155e85320SPaul Moore /**
526255e85320SPaul Moore  * security_bpf_prog_free() - Free a bpf program's LSM blob
526355e85320SPaul Moore  * @aux: bpf program aux info struct
526455e85320SPaul Moore  *
526555e85320SPaul Moore  * Clean up the security information stored inside bpf prog.
526655e85320SPaul Moore  */
security_bpf_prog_free(struct bpf_prog_aux * aux)5267afdb09c7SChenbo Feng void security_bpf_prog_free(struct bpf_prog_aux *aux)
5268afdb09c7SChenbo Feng {
5269afdb09c7SChenbo Feng 	call_void_hook(bpf_prog_free_security, aux);
5270afdb09c7SChenbo Feng }
5271afdb09c7SChenbo Feng #endif /* CONFIG_BPF_SYSCALL */
52729e47d31dSMatthew Garrett 
5273e261301cSPaul Moore /**
5274e261301cSPaul Moore  * security_locked_down() - Check if a kernel feature is allowed
5275e261301cSPaul Moore  * @what: requested kernel feature
5276e261301cSPaul Moore  *
5277e261301cSPaul Moore  * Determine whether a kernel feature that potentially enables arbitrary code
5278e261301cSPaul Moore  * execution in kernel space should be permitted.
5279e261301cSPaul Moore  *
5280e261301cSPaul Moore  * Return: Returns 0 if permission is granted.
5281e261301cSPaul Moore  */
security_locked_down(enum lockdown_reason what)52829e47d31dSMatthew Garrett int security_locked_down(enum lockdown_reason what)
52839e47d31dSMatthew Garrett {
52849e47d31dSMatthew Garrett 	return call_int_hook(locked_down, 0, what);
52859e47d31dSMatthew Garrett }
52869e47d31dSMatthew Garrett EXPORT_SYMBOL(security_locked_down);
5287da97e184SJoel Fernandes (Google) 
5288da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS
5289452b670cSPaul Moore /**
5290452b670cSPaul Moore  * security_perf_event_open() - Check if a perf event open is allowed
5291452b670cSPaul Moore  * @attr: perf event attribute
5292452b670cSPaul Moore  * @type: type of event
5293452b670cSPaul Moore  *
5294452b670cSPaul Moore  * Check whether the @type of perf_event_open syscall is allowed.
5295452b670cSPaul Moore  *
5296452b670cSPaul Moore  * Return: Returns 0 if permission is granted.
5297452b670cSPaul Moore  */
security_perf_event_open(struct perf_event_attr * attr,int type)5298da97e184SJoel Fernandes (Google) int security_perf_event_open(struct perf_event_attr *attr, int type)
5299da97e184SJoel Fernandes (Google) {
5300da97e184SJoel Fernandes (Google) 	return call_int_hook(perf_event_open, 0, attr, type);
5301da97e184SJoel Fernandes (Google) }
5302da97e184SJoel Fernandes (Google) 
5303452b670cSPaul Moore /**
5304452b670cSPaul Moore  * security_perf_event_alloc() - Allocate a perf event LSM blob
5305452b670cSPaul Moore  * @event: perf event
5306452b670cSPaul Moore  *
5307452b670cSPaul Moore  * Allocate and save perf_event security info.
5308452b670cSPaul Moore  *
5309452b670cSPaul Moore  * Return: Returns 0 on success, error on failure.
5310452b670cSPaul Moore  */
security_perf_event_alloc(struct perf_event * event)5311da97e184SJoel Fernandes (Google) int security_perf_event_alloc(struct perf_event *event)
5312da97e184SJoel Fernandes (Google) {
5313da97e184SJoel Fernandes (Google) 	return call_int_hook(perf_event_alloc, 0, event);
5314da97e184SJoel Fernandes (Google) }
5315da97e184SJoel Fernandes (Google) 
5316452b670cSPaul Moore /**
5317452b670cSPaul Moore  * security_perf_event_free() - Free a perf event LSM blob
5318452b670cSPaul Moore  * @event: perf event
5319452b670cSPaul Moore  *
5320452b670cSPaul Moore  * Release (free) perf_event security info.
5321452b670cSPaul Moore  */
security_perf_event_free(struct perf_event * event)5322da97e184SJoel Fernandes (Google) void security_perf_event_free(struct perf_event *event)
5323da97e184SJoel Fernandes (Google) {
5324da97e184SJoel Fernandes (Google) 	call_void_hook(perf_event_free, event);
5325da97e184SJoel Fernandes (Google) }
5326da97e184SJoel Fernandes (Google) 
5327452b670cSPaul Moore /**
5328452b670cSPaul Moore  * security_perf_event_read() - Check if reading a perf event label is allowed
5329452b670cSPaul Moore  * @event: perf event
5330452b670cSPaul Moore  *
5331452b670cSPaul Moore  * Read perf_event security info if allowed.
5332452b670cSPaul Moore  *
5333452b670cSPaul Moore  * Return: Returns 0 if permission is granted.
5334452b670cSPaul Moore  */
security_perf_event_read(struct perf_event * event)5335da97e184SJoel Fernandes (Google) int security_perf_event_read(struct perf_event *event)
5336da97e184SJoel Fernandes (Google) {
5337da97e184SJoel Fernandes (Google) 	return call_int_hook(perf_event_read, 0, event);
5338da97e184SJoel Fernandes (Google) }
5339da97e184SJoel Fernandes (Google) 
5340452b670cSPaul Moore /**
5341452b670cSPaul Moore  * security_perf_event_write() - Check if writing a perf event label is allowed
5342452b670cSPaul Moore  * @event: perf event
5343452b670cSPaul Moore  *
5344452b670cSPaul Moore  * Write perf_event security info if allowed.
5345452b670cSPaul Moore  *
5346452b670cSPaul Moore  * Return: Returns 0 if permission is granted.
5347452b670cSPaul Moore  */
security_perf_event_write(struct perf_event * event)5348da97e184SJoel Fernandes (Google) int security_perf_event_write(struct perf_event *event)
5349da97e184SJoel Fernandes (Google) {
5350da97e184SJoel Fernandes (Google) 	return call_int_hook(perf_event_write, 0, event);
5351da97e184SJoel Fernandes (Google) }
5352da97e184SJoel Fernandes (Google) #endif /* CONFIG_PERF_EVENTS */
5353cdc1404aSPaul Moore 
5354cdc1404aSPaul Moore #ifdef CONFIG_IO_URING
53551cd2aca6SPaul Moore /**
53561cd2aca6SPaul Moore  * security_uring_override_creds() - Check if overriding creds is allowed
53571cd2aca6SPaul Moore  * @new: new credentials
53581cd2aca6SPaul Moore  *
53591cd2aca6SPaul Moore  * Check if the current task, executing an io_uring operation, is allowed to
53601cd2aca6SPaul Moore  * override it's credentials with @new.
53611cd2aca6SPaul Moore  *
53621cd2aca6SPaul Moore  * Return: Returns 0 if permission is granted.
53631cd2aca6SPaul Moore  */
security_uring_override_creds(const struct cred * new)5364cdc1404aSPaul Moore int security_uring_override_creds(const struct cred *new)
5365cdc1404aSPaul Moore {
5366cdc1404aSPaul Moore 	return call_int_hook(uring_override_creds, 0, new);
5367cdc1404aSPaul Moore }
5368cdc1404aSPaul Moore 
53691cd2aca6SPaul Moore /**
53701cd2aca6SPaul Moore  * security_uring_sqpoll() - Check if IORING_SETUP_SQPOLL is allowed
53711cd2aca6SPaul Moore  *
53721cd2aca6SPaul Moore  * Check whether the current task is allowed to spawn a io_uring polling thread
53731cd2aca6SPaul Moore  * (IORING_SETUP_SQPOLL).
53741cd2aca6SPaul Moore  *
53751cd2aca6SPaul Moore  * Return: Returns 0 if permission is granted.
53761cd2aca6SPaul Moore  */
security_uring_sqpoll(void)5377cdc1404aSPaul Moore int security_uring_sqpoll(void)
5378cdc1404aSPaul Moore {
5379cdc1404aSPaul Moore 	return call_int_hook(uring_sqpoll, 0);
5380cdc1404aSPaul Moore }
53811cd2aca6SPaul Moore 
53821cd2aca6SPaul Moore /**
53831cd2aca6SPaul Moore  * security_uring_cmd() - Check if a io_uring passthrough command is allowed
53841cd2aca6SPaul Moore  * @ioucmd: command
53851cd2aca6SPaul Moore  *
53861cd2aca6SPaul Moore  * Check whether the file_operations uring_cmd is allowed to run.
53871cd2aca6SPaul Moore  *
53881cd2aca6SPaul Moore  * Return: Returns 0 if permission is granted.
53891cd2aca6SPaul Moore  */
security_uring_cmd(struct io_uring_cmd * ioucmd)53902a584012SLuis Chamberlain int security_uring_cmd(struct io_uring_cmd *ioucmd)
53912a584012SLuis Chamberlain {
53922a584012SLuis Chamberlain 	return call_int_hook(uring_cmd, 0, ioucmd);
53932a584012SLuis Chamberlain }
5394cdc1404aSPaul Moore #endif /* CONFIG_IO_URING */
5395