xref: /openbmc/linux/scripts/selinux/mdp/mdp.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
293c06cbbSSerge E. Hallyn /*
393c06cbbSSerge E. Hallyn  *
493c06cbbSSerge E. Hallyn  * mdp - make dummy policy
593c06cbbSSerge E. Hallyn  *
693c06cbbSSerge E. Hallyn  * When pointed at a kernel tree, builds a dummy policy for that kernel
793c06cbbSSerge E. Hallyn  * with exactly one type with full rights to itself.
893c06cbbSSerge E. Hallyn  *
993c06cbbSSerge E. Hallyn  * Copyright (C) IBM Corporation, 2006
1093c06cbbSSerge E. Hallyn  *
1193c06cbbSSerge E. Hallyn  * Authors: Serge E. Hallyn <serue@us.ibm.com>
1293c06cbbSSerge E. Hallyn  */
1393c06cbbSSerge E. Hallyn 
14bfc5e3a6SPaul Moore 
15bfc5e3a6SPaul Moore /* NOTE: we really do want to use the kernel headers here */
16bfc5e3a6SPaul Moore #define __EXPORTED_HEADERS__
17bfc5e3a6SPaul Moore 
1893c06cbbSSerge E. Hallyn #include <stdio.h>
1993c06cbbSSerge E. Hallyn #include <stdlib.h>
2093c06cbbSSerge E. Hallyn #include <unistd.h>
2193c06cbbSSerge E. Hallyn #include <string.h>
22e37c1877SStephen Smalley #include <linux/kconfig.h>
2393c06cbbSSerge E. Hallyn 
usage(char * name)245c725138STrevor Keith static void usage(char *name)
2593c06cbbSSerge E. Hallyn {
2693c06cbbSSerge E. Hallyn 	printf("usage: %s [-m] policy_file context_file\n", name);
2793c06cbbSSerge E. Hallyn 	exit(1);
2893c06cbbSSerge E. Hallyn }
2993c06cbbSSerge E. Hallyn 
30c6d3aaa4SStephen Smalley /* Class/perm mapping support */
31c6d3aaa4SStephen Smalley struct security_class_mapping {
32c6d3aaa4SStephen Smalley 	const char *name;
33c6d3aaa4SStephen Smalley 	const char *perms[sizeof(unsigned) * 8 + 1];
3493c06cbbSSerge E. Hallyn };
3593c06cbbSSerge E. Hallyn 
36c6d3aaa4SStephen Smalley #include "classmap.h"
3793c06cbbSSerge E. Hallyn #include "initial_sid_to_string.h"
38339949beSStephen Smalley #include "policycap_names.h"
39339949beSStephen Smalley 
40339949beSStephen Smalley #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
4193c06cbbSSerge E. Hallyn 
main(int argc,char * argv[])4293c06cbbSSerge E. Hallyn int main(int argc, char *argv[])
4393c06cbbSSerge E. Hallyn {
4493c06cbbSSerge E. Hallyn 	int i, j, mls = 0;
45c6d3aaa4SStephen Smalley 	int initial_sid_to_string_len;
4693c06cbbSSerge E. Hallyn 	char **arg, *polout, *ctxout;
47c6d3aaa4SStephen Smalley 
4893c06cbbSSerge E. Hallyn 	FILE *fout;
4993c06cbbSSerge E. Hallyn 
5093c06cbbSSerge E. Hallyn 	if (argc < 3)
5193c06cbbSSerge E. Hallyn 		usage(argv[0]);
5293c06cbbSSerge E. Hallyn 	arg = argv+1;
5393c06cbbSSerge E. Hallyn 	if (argc==4 && strcmp(argv[1], "-m") == 0) {
5493c06cbbSSerge E. Hallyn 		mls = 1;
5593c06cbbSSerge E. Hallyn 		arg++;
5693c06cbbSSerge E. Hallyn 	}
5793c06cbbSSerge E. Hallyn 	polout = *arg++;
5893c06cbbSSerge E. Hallyn 	ctxout = *arg;
5993c06cbbSSerge E. Hallyn 
6093c06cbbSSerge E. Hallyn 	fout = fopen(polout, "w");
6193c06cbbSSerge E. Hallyn 	if (!fout) {
6293c06cbbSSerge E. Hallyn 		printf("Could not open %s for writing\n", polout);
6393c06cbbSSerge E. Hallyn 		usage(argv[0]);
6493c06cbbSSerge E. Hallyn 	}
6593c06cbbSSerge E. Hallyn 
6693c06cbbSSerge E. Hallyn 	/* print out the classes */
67c6d3aaa4SStephen Smalley 	for (i = 0; secclass_map[i].name; i++)
68c6d3aaa4SStephen Smalley 		fprintf(fout, "class %s\n", secclass_map[i].name);
6993c06cbbSSerge E. Hallyn 	fprintf(fout, "\n");
7093c06cbbSSerge E. Hallyn 
7193c06cbbSSerge E. Hallyn 	initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
7293c06cbbSSerge E. Hallyn 	/* print out the sids */
73382c2b5dSStephen Smalley 	for (i = 1; i < initial_sid_to_string_len; i++) {
74382c2b5dSStephen Smalley 		const char *name = initial_sid_to_string[i];
75382c2b5dSStephen Smalley 
76382c2b5dSStephen Smalley 		if (name)
77382c2b5dSStephen Smalley 			fprintf(fout, "sid %s\n", name);
78382c2b5dSStephen Smalley 		else
79382c2b5dSStephen Smalley 			fprintf(fout, "sid unused%d\n", i);
80382c2b5dSStephen Smalley 	}
8193c06cbbSSerge E. Hallyn 	fprintf(fout, "\n");
8293c06cbbSSerge E. Hallyn 
8393c06cbbSSerge E. Hallyn 	/* print out the class permissions */
84c6d3aaa4SStephen Smalley 	for (i = 0; secclass_map[i].name; i++) {
85*ded34574SChristian Göttsche 		const struct security_class_mapping *map = &secclass_map[i];
86c6d3aaa4SStephen Smalley 		fprintf(fout, "class %s\n", map->name);
8793c06cbbSSerge E. Hallyn 		fprintf(fout, "{\n");
88c6d3aaa4SStephen Smalley 		for (j = 0; map->perms[j]; j++)
89c6d3aaa4SStephen Smalley 			fprintf(fout, "\t%s\n", map->perms[j]);
9093c06cbbSSerge E. Hallyn 		fprintf(fout, "}\n\n");
9193c06cbbSSerge E. Hallyn 	}
9293c06cbbSSerge E. Hallyn 	fprintf(fout, "\n");
9393c06cbbSSerge E. Hallyn 
94e37c1877SStephen Smalley 	/* print out mls declarations and constraints */
9593c06cbbSSerge E. Hallyn 	if (mls) {
96e37c1877SStephen Smalley 		fprintf(fout, "sensitivity s0;\n");
97e37c1877SStephen Smalley 		fprintf(fout, "sensitivity s1;\n");
98e37c1877SStephen Smalley 		fprintf(fout, "dominance { s0 s1 }\n");
99e37c1877SStephen Smalley 		fprintf(fout, "category c0;\n");
100e37c1877SStephen Smalley 		fprintf(fout, "category c1;\n");
101e37c1877SStephen Smalley 		fprintf(fout, "level s0:c0.c1;\n");
102e37c1877SStephen Smalley 		fprintf(fout, "level s1:c0.c1;\n");
103e37c1877SStephen Smalley #define SYSTEMLOW "s0"
104e37c1877SStephen Smalley #define SYSTEMHIGH "s1:c0.c1"
105e37c1877SStephen Smalley 		for (i = 0; secclass_map[i].name; i++) {
106*ded34574SChristian Göttsche 			const struct security_class_mapping *map = &secclass_map[i];
107e37c1877SStephen Smalley 
108e37c1877SStephen Smalley 			fprintf(fout, "mlsconstrain %s {\n", map->name);
109e37c1877SStephen Smalley 			for (j = 0; map->perms[j]; j++)
110e37c1877SStephen Smalley 				fprintf(fout, "\t%s\n", map->perms[j]);
111e37c1877SStephen Smalley 			/*
112e37c1877SStephen Smalley 			 * This requires all subjects and objects to be
113e37c1877SStephen Smalley 			 * single-level (l2 eq h2), and that the subject
114e37c1877SStephen Smalley 			 * level dominate the object level (h1 dom h2)
115e37c1877SStephen Smalley 			 * in order to have any permissions to it.
116e37c1877SStephen Smalley 			 */
117e37c1877SStephen Smalley 			fprintf(fout, "} (l2 eq h2 and h1 dom h2);\n\n");
118e37c1877SStephen Smalley 		}
11993c06cbbSSerge E. Hallyn 	}
12093c06cbbSSerge E. Hallyn 
121339949beSStephen Smalley 	/* enable all policy capabilities */
122339949beSStephen Smalley 	for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
123339949beSStephen Smalley 		fprintf(fout, "policycap %s;\n", selinux_policycap_names[i]);
124339949beSStephen Smalley 
12593c06cbbSSerge E. Hallyn 	/* types, roles, and allows */
12693c06cbbSSerge E. Hallyn 	fprintf(fout, "type base_t;\n");
127fda4d578SLaurent Bigonville 	fprintf(fout, "role base_r;\n");
12893c06cbbSSerge E. Hallyn 	fprintf(fout, "role base_r types { base_t };\n");
129c6d3aaa4SStephen Smalley 	for (i = 0; secclass_map[i].name; i++)
130c6d3aaa4SStephen Smalley 		fprintf(fout, "allow base_t base_t:%s *;\n",
131c6d3aaa4SStephen Smalley 			secclass_map[i].name);
132e37c1877SStephen Smalley 	fprintf(fout, "user user_u roles { base_r }");
133e37c1877SStephen Smalley 	if (mls)
134e37c1877SStephen Smalley 		fprintf(fout, " level %s range %s - %s", SYSTEMLOW,
135e37c1877SStephen Smalley 			SYSTEMLOW, SYSTEMHIGH);
136e37c1877SStephen Smalley 	fprintf(fout, ";\n");
137e37c1877SStephen Smalley 
138e37c1877SStephen Smalley #define SUBJUSERROLETYPE "user_u:base_r:base_t"
139e37c1877SStephen Smalley #define OBJUSERROLETYPE "user_u:object_r:base_t"
14093c06cbbSSerge E. Hallyn 
14193c06cbbSSerge E. Hallyn 	/* default sids */
142382c2b5dSStephen Smalley 	for (i = 1; i < initial_sid_to_string_len; i++) {
143382c2b5dSStephen Smalley 		const char *name = initial_sid_to_string[i];
144382c2b5dSStephen Smalley 
145382c2b5dSStephen Smalley 		if (name)
146382c2b5dSStephen Smalley 			fprintf(fout, "sid %s ", name);
147382c2b5dSStephen Smalley 		else
148382c2b5dSStephen Smalley 			fprintf(fout, "sid unused%d\n", i);
149382c2b5dSStephen Smalley 		fprintf(fout, SUBJUSERROLETYPE "%s\n",
150382c2b5dSStephen Smalley 			mls ? ":" SYSTEMLOW : "");
151382c2b5dSStephen Smalley 	}
15293c06cbbSSerge E. Hallyn 	fprintf(fout, "\n");
15393c06cbbSSerge E. Hallyn 
154e37c1877SStephen Smalley #define FS_USE(behavior, fstype)			    \
155e37c1877SStephen Smalley 	fprintf(fout, "fs_use_%s %s " OBJUSERROLETYPE "%s;\n", \
156e37c1877SStephen Smalley 		behavior, fstype, mls ? ":" SYSTEMLOW : "")
15793c06cbbSSerge E. Hallyn 
158e37c1877SStephen Smalley 	/*
159e37c1877SStephen Smalley 	 * Filesystems whose inode labels can be fetched via getxattr.
160e37c1877SStephen Smalley 	 */
161e37c1877SStephen Smalley #ifdef CONFIG_EXT2_FS_SECURITY
162e37c1877SStephen Smalley 	FS_USE("xattr", "ext2");
163e37c1877SStephen Smalley #endif
164e37c1877SStephen Smalley #ifdef CONFIG_EXT4_FS_SECURITY
165e37c1877SStephen Smalley #ifdef CONFIG_EXT4_USE_FOR_EXT2
166e37c1877SStephen Smalley 	FS_USE("xattr", "ext2");
167e37c1877SStephen Smalley #endif
168e37c1877SStephen Smalley 	FS_USE("xattr", "ext3");
169e37c1877SStephen Smalley 	FS_USE("xattr", "ext4");
170e37c1877SStephen Smalley #endif
171e37c1877SStephen Smalley #ifdef CONFIG_JFS_SECURITY
172e37c1877SStephen Smalley 	FS_USE("xattr", "jfs");
173e37c1877SStephen Smalley #endif
174e37c1877SStephen Smalley #ifdef CONFIG_REISERFS_FS_SECURITY
175e37c1877SStephen Smalley 	FS_USE("xattr", "reiserfs");
176e37c1877SStephen Smalley #endif
177e37c1877SStephen Smalley #ifdef CONFIG_JFFS2_FS_SECURITY
178e37c1877SStephen Smalley 	FS_USE("xattr", "jffs2");
179e37c1877SStephen Smalley #endif
180e37c1877SStephen Smalley #ifdef CONFIG_XFS_FS
181e37c1877SStephen Smalley 	FS_USE("xattr", "xfs");
182e37c1877SStephen Smalley #endif
183e37c1877SStephen Smalley #ifdef CONFIG_GFS2_FS
184e37c1877SStephen Smalley 	FS_USE("xattr", "gfs2");
185e37c1877SStephen Smalley #endif
186e37c1877SStephen Smalley #ifdef CONFIG_BTRFS_FS
187e37c1877SStephen Smalley 	FS_USE("xattr", "btrfs");
188e37c1877SStephen Smalley #endif
189e37c1877SStephen Smalley #ifdef CONFIG_F2FS_FS_SECURITY
190e37c1877SStephen Smalley 	FS_USE("xattr", "f2fs");
191e37c1877SStephen Smalley #endif
192e37c1877SStephen Smalley #ifdef CONFIG_OCFS2_FS
193e37c1877SStephen Smalley 	FS_USE("xattr", "ocsfs2");
194e37c1877SStephen Smalley #endif
195e37c1877SStephen Smalley #ifdef CONFIG_OVERLAY_FS
196e37c1877SStephen Smalley 	FS_USE("xattr", "overlay");
197e37c1877SStephen Smalley #endif
198e37c1877SStephen Smalley #ifdef CONFIG_SQUASHFS_XATTR
199e37c1877SStephen Smalley 	FS_USE("xattr", "squashfs");
200e37c1877SStephen Smalley #endif
20193c06cbbSSerge E. Hallyn 
202e37c1877SStephen Smalley 	/*
203e37c1877SStephen Smalley 	 * Filesystems whose inodes are labeled from allocating task.
204e37c1877SStephen Smalley 	 */
205e37c1877SStephen Smalley 	FS_USE("task", "pipefs");
206e37c1877SStephen Smalley 	FS_USE("task", "sockfs");
20793c06cbbSSerge E. Hallyn 
208e37c1877SStephen Smalley 	/*
209e37c1877SStephen Smalley 	 * Filesystems whose inode labels are computed from both
210e37c1877SStephen Smalley 	 * the allocating task and the superblock label.
211e37c1877SStephen Smalley 	 */
212e37c1877SStephen Smalley #ifdef CONFIG_UNIX98_PTYS
213e37c1877SStephen Smalley 	FS_USE("trans", "devpts");
214e37c1877SStephen Smalley #endif
215e37c1877SStephen Smalley #ifdef CONFIG_HUGETLBFS
216e37c1877SStephen Smalley 	FS_USE("trans", "hugetlbfs");
217e37c1877SStephen Smalley #endif
218e37c1877SStephen Smalley #ifdef CONFIG_TMPFS
219e37c1877SStephen Smalley 	FS_USE("trans", "tmpfs");
220e37c1877SStephen Smalley #endif
221e37c1877SStephen Smalley #ifdef CONFIG_DEVTMPFS
222e37c1877SStephen Smalley 	FS_USE("trans", "devtmpfs");
223e37c1877SStephen Smalley #endif
224e37c1877SStephen Smalley #ifdef CONFIG_POSIX_MQUEUE
225e37c1877SStephen Smalley 	FS_USE("trans", "mqueue");
226e37c1877SStephen Smalley #endif
227e37c1877SStephen Smalley 
228e37c1877SStephen Smalley #define GENFSCON(fstype, prefix)			     \
229e37c1877SStephen Smalley 	fprintf(fout, "genfscon %s %s " OBJUSERROLETYPE "%s\n", \
230e37c1877SStephen Smalley 		fstype, prefix, mls ? ":" SYSTEMLOW : "")
231e37c1877SStephen Smalley 
232e37c1877SStephen Smalley 	/*
233e37c1877SStephen Smalley 	 * Filesystems whose inodes are labeled from path prefix match
234e37c1877SStephen Smalley 	 * relative to the filesystem root.  Depending on the filesystem,
235e37c1877SStephen Smalley 	 * only a single label for all inodes may be supported.  Here
236e37c1877SStephen Smalley 	 * we list the filesystem types for which per-file labeling is
237e37c1877SStephen Smalley 	 * supported using genfscon; any other filesystem type can also
238e37c1877SStephen Smalley 	 * be added by only with a single entry for all of its inodes.
239e37c1877SStephen Smalley 	 */
240e37c1877SStephen Smalley #ifdef CONFIG_PROC_FS
241e37c1877SStephen Smalley 	GENFSCON("proc", "/");
242e37c1877SStephen Smalley #endif
243e37c1877SStephen Smalley #ifdef CONFIG_SECURITY_SELINUX
244e37c1877SStephen Smalley 	GENFSCON("selinuxfs", "/");
245e37c1877SStephen Smalley #endif
246e37c1877SStephen Smalley #ifdef CONFIG_SYSFS
247e37c1877SStephen Smalley 	GENFSCON("sysfs", "/");
248e37c1877SStephen Smalley #endif
249e37c1877SStephen Smalley #ifdef CONFIG_DEBUG_FS
250e37c1877SStephen Smalley 	GENFSCON("debugfs", "/");
251e37c1877SStephen Smalley #endif
252e37c1877SStephen Smalley #ifdef CONFIG_TRACING
253e37c1877SStephen Smalley 	GENFSCON("tracefs", "/");
254e37c1877SStephen Smalley #endif
255e37c1877SStephen Smalley #ifdef CONFIG_PSTORE
256e37c1877SStephen Smalley 	GENFSCON("pstore", "/");
257e37c1877SStephen Smalley #endif
258e37c1877SStephen Smalley 	GENFSCON("cgroup", "/");
259e37c1877SStephen Smalley 	GENFSCON("cgroup2", "/");
26093c06cbbSSerge E. Hallyn 
26193c06cbbSSerge E. Hallyn 	fclose(fout);
26293c06cbbSSerge E. Hallyn 
26393c06cbbSSerge E. Hallyn 	fout = fopen(ctxout, "w");
26493c06cbbSSerge E. Hallyn 	if (!fout) {
26593c06cbbSSerge E. Hallyn 		printf("Wrote policy, but cannot open %s for writing\n", ctxout);
26693c06cbbSSerge E. Hallyn 		usage(argv[0]);
26793c06cbbSSerge E. Hallyn 	}
268e37c1877SStephen Smalley 	fprintf(fout, "/ " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
269e37c1877SStephen Smalley 	fprintf(fout, "/.* " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
27093c06cbbSSerge E. Hallyn 	fclose(fout);
27193c06cbbSSerge E. Hallyn 
27293c06cbbSSerge E. Hallyn 	return 0;
27393c06cbbSSerge E. Hallyn }
274