xref: /openbmc/linux/security/tomoyo/load_policy.c (revision c2554b47)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c3ef1500STetsuo Handa /*
3c3ef1500STetsuo Handa  * security/tomoyo/load_policy.c
4c3ef1500STetsuo Handa  *
50f2a55d5STetsuo Handa  * Copyright (C) 2005-2011  NTT DATA CORPORATION
6c3ef1500STetsuo Handa  */
7c3ef1500STetsuo Handa 
8c3ef1500STetsuo Handa #include "common.h"
9c3ef1500STetsuo Handa 
100e4ae0e0STetsuo Handa #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
110e4ae0e0STetsuo Handa 
120e4ae0e0STetsuo Handa /*
130e4ae0e0STetsuo Handa  * Path to the policy loader. (default = CONFIG_SECURITY_TOMOYO_POLICY_LOADER)
140e4ae0e0STetsuo Handa  */
150e4ae0e0STetsuo Handa static const char *tomoyo_loader;
160e4ae0e0STetsuo Handa 
170e4ae0e0STetsuo Handa /**
180e4ae0e0STetsuo Handa  * tomoyo_loader_setup - Set policy loader.
190e4ae0e0STetsuo Handa  *
200e4ae0e0STetsuo Handa  * @str: Program to use as a policy loader (e.g. /sbin/tomoyo-init ).
210e4ae0e0STetsuo Handa  *
220e4ae0e0STetsuo Handa  * Returns 0.
230e4ae0e0STetsuo Handa  */
tomoyo_loader_setup(char * str)240e4ae0e0STetsuo Handa static int __init tomoyo_loader_setup(char *str)
250e4ae0e0STetsuo Handa {
260e4ae0e0STetsuo Handa 	tomoyo_loader = str;
27*c2554b47SRandy Dunlap 	return 1;
280e4ae0e0STetsuo Handa }
290e4ae0e0STetsuo Handa 
300e4ae0e0STetsuo Handa __setup("TOMOYO_loader=", tomoyo_loader_setup);
31c3ef1500STetsuo Handa 
32c3ef1500STetsuo Handa /**
33c3ef1500STetsuo Handa  * tomoyo_policy_loader_exists - Check whether /sbin/tomoyo-init exists.
34c3ef1500STetsuo Handa  *
35c3ef1500STetsuo Handa  * Returns true if /sbin/tomoyo-init exists, false otherwise.
36c3ef1500STetsuo Handa  */
tomoyo_policy_loader_exists(void)37c3ef1500STetsuo Handa static bool tomoyo_policy_loader_exists(void)
38c3ef1500STetsuo Handa {
39c3ef1500STetsuo Handa 	struct path path;
40cdcf6723STetsuo Handa 
410e4ae0e0STetsuo Handa 	if (!tomoyo_loader)
420e4ae0e0STetsuo Handa 		tomoyo_loader = CONFIG_SECURITY_TOMOYO_POLICY_LOADER;
43c3ef1500STetsuo Handa 	if (kern_path(tomoyo_loader, LOOKUP_FOLLOW, &path)) {
44cdcf6723STetsuo Handa 		pr_info("Not activating Mandatory Access Control as %s does not exist.\n",
45cdcf6723STetsuo Handa 			tomoyo_loader);
46c3ef1500STetsuo Handa 		return false;
47c3ef1500STetsuo Handa 	}
48c3ef1500STetsuo Handa 	path_put(&path);
49c3ef1500STetsuo Handa 	return true;
50c3ef1500STetsuo Handa }
51c3ef1500STetsuo Handa 
520e4ae0e0STetsuo Handa /*
530e4ae0e0STetsuo Handa  * Path to the trigger. (default = CONFIG_SECURITY_TOMOYO_ACTIVATION_TRIGGER)
540e4ae0e0STetsuo Handa  */
550e4ae0e0STetsuo Handa static const char *tomoyo_trigger;
560e4ae0e0STetsuo Handa 
570e4ae0e0STetsuo Handa /**
580e4ae0e0STetsuo Handa  * tomoyo_trigger_setup - Set trigger for activation.
590e4ae0e0STetsuo Handa  *
600e4ae0e0STetsuo Handa  * @str: Program to use as an activation trigger (e.g. /sbin/init ).
610e4ae0e0STetsuo Handa  *
620e4ae0e0STetsuo Handa  * Returns 0.
630e4ae0e0STetsuo Handa  */
tomoyo_trigger_setup(char * str)640e4ae0e0STetsuo Handa static int __init tomoyo_trigger_setup(char *str)
650e4ae0e0STetsuo Handa {
660e4ae0e0STetsuo Handa 	tomoyo_trigger = str;
67*c2554b47SRandy Dunlap 	return 1;
680e4ae0e0STetsuo Handa }
690e4ae0e0STetsuo Handa 
700e4ae0e0STetsuo Handa __setup("TOMOYO_trigger=", tomoyo_trigger_setup);
710e4ae0e0STetsuo Handa 
72c3ef1500STetsuo Handa /**
73c3ef1500STetsuo Handa  * tomoyo_load_policy - Run external policy loader to load policy.
74c3ef1500STetsuo Handa  *
75c3ef1500STetsuo Handa  * @filename: The program about to start.
76c3ef1500STetsuo Handa  *
77c3ef1500STetsuo Handa  * This function checks whether @filename is /sbin/init , and if so
78c3ef1500STetsuo Handa  * invoke /sbin/tomoyo-init and wait for the termination of /sbin/tomoyo-init
79c3ef1500STetsuo Handa  * and then continues invocation of /sbin/init.
80c3ef1500STetsuo Handa  * /sbin/tomoyo-init reads policy files in /etc/tomoyo/ directory and
81c3ef1500STetsuo Handa  * writes to /sys/kernel/security/tomoyo/ interfaces.
82c3ef1500STetsuo Handa  *
83c3ef1500STetsuo Handa  * Returns nothing.
84c3ef1500STetsuo Handa  */
tomoyo_load_policy(const char * filename)85c3ef1500STetsuo Handa void tomoyo_load_policy(const char *filename)
86c3ef1500STetsuo Handa {
870e4ae0e0STetsuo Handa 	static bool done;
88c3ef1500STetsuo Handa 	char *argv[2];
89c3ef1500STetsuo Handa 	char *envp[3];
90c3ef1500STetsuo Handa 
910e4ae0e0STetsuo Handa 	if (tomoyo_policy_loaded || done)
92c3ef1500STetsuo Handa 		return;
930e4ae0e0STetsuo Handa 	if (!tomoyo_trigger)
940e4ae0e0STetsuo Handa 		tomoyo_trigger = CONFIG_SECURITY_TOMOYO_ACTIVATION_TRIGGER;
950e4ae0e0STetsuo Handa 	if (strcmp(filename, tomoyo_trigger))
96c3ef1500STetsuo Handa 		return;
97c3ef1500STetsuo Handa 	if (!tomoyo_policy_loader_exists())
98c3ef1500STetsuo Handa 		return;
990e4ae0e0STetsuo Handa 	done = true;
100cdcf6723STetsuo Handa 	pr_info("Calling %s to load policy. Please wait.\n", tomoyo_loader);
101c3ef1500STetsuo Handa 	argv[0] = (char *) tomoyo_loader;
102c3ef1500STetsuo Handa 	argv[1] = NULL;
103c3ef1500STetsuo Handa 	envp[0] = "HOME=/";
104c3ef1500STetsuo Handa 	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
105c3ef1500STetsuo Handa 	envp[2] = NULL;
10670834d30SOleg Nesterov 	call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
107c3ef1500STetsuo Handa 	tomoyo_check_profile();
108c3ef1500STetsuo Handa }
1090e4ae0e0STetsuo Handa 
1100e4ae0e0STetsuo Handa #endif
111