mdp.c 6.67 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13
/*
 *
 * mdp - make dummy policy
 *
 * When pointed at a kernel tree, builds a dummy policy for that kernel
 * with exactly one type with full rights to itself.
 *
 * Copyright (C) IBM Corporation, 2006
 *
 * Authors: Serge E. Hallyn <serue@us.ibm.com>
 */

14 15 16 17

/* NOTE: we really do want to use the kernel headers here */
#define __EXPORTED_HEADERS__

18 19 20 21
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
22
#include <linux/kconfig.h>
23

24
static void usage(char *name)
25 26 27 28 29
{
	printf("usage: %s [-m] policy_file context_file\n", name);
	exit(1);
}

30 31 32 33
/* Class/perm mapping support */
struct security_class_mapping {
	const char *name;
	const char *perms[sizeof(unsigned) * 8 + 1];
34 35
};

36
#include "classmap.h"
37
#include "initial_sid_to_string.h"
38 39 40
#include "policycap_names.h"

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
41 42 43 44

int main(int argc, char *argv[])
{
	int i, j, mls = 0;
45
	int initial_sid_to_string_len;
46
	char **arg, *polout, *ctxout;
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	FILE *fout;

	if (argc < 3)
		usage(argv[0]);
	arg = argv+1;
	if (argc==4 && strcmp(argv[1], "-m") == 0) {
		mls = 1;
		arg++;
	}
	polout = *arg++;
	ctxout = *arg;

	fout = fopen(polout, "w");
	if (!fout) {
		printf("Could not open %s for writing\n", polout);
		usage(argv[0]);
	}

	/* print out the classes */
67 68
	for (i = 0; secclass_map[i].name; i++)
		fprintf(fout, "class %s\n", secclass_map[i].name);
69 70 71 72
	fprintf(fout, "\n");

	initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
	/* print out the sids */
73 74 75 76 77 78 79 80
	for (i = 1; i < initial_sid_to_string_len; i++) {
		const char *name = initial_sid_to_string[i];

		if (name)
			fprintf(fout, "sid %s\n", name);
		else
			fprintf(fout, "sid unused%d\n", i);
	}
81 82 83
	fprintf(fout, "\n");

	/* print out the class permissions */
84
	for (i = 0; secclass_map[i].name; i++) {
85
		const struct security_class_mapping *map = &secclass_map[i];
86 87 88 89 90
		fprintf(fout, "class %s\n", map->name);
		fprintf(fout, "{\n");
		for (j = 0; map->perms[j]; j++)
			fprintf(fout, "\t%s\n", map->perms[j]);
		fprintf(fout, "}\n\n");
91 92 93
	}
	fprintf(fout, "\n");

94
	/* print out mls declarations and constraints */
95
	if (mls) {
96 97 98 99 100 101 102 103 104 105
		fprintf(fout, "sensitivity s0;\n");
		fprintf(fout, "sensitivity s1;\n");
		fprintf(fout, "dominance { s0 s1 }\n");
		fprintf(fout, "category c0;\n");
		fprintf(fout, "category c1;\n");
		fprintf(fout, "level s0:c0.c1;\n");
		fprintf(fout, "level s1:c0.c1;\n");
#define SYSTEMLOW "s0"
#define SYSTEMHIGH "s1:c0.c1"
		for (i = 0; secclass_map[i].name; i++) {
106
			const struct security_class_mapping *map = &secclass_map[i];
107 108 109 110 111 112 113 114 115 116 117 118

			fprintf(fout, "mlsconstrain %s {\n", map->name);
			for (j = 0; map->perms[j]; j++)
				fprintf(fout, "\t%s\n", map->perms[j]);
			/*
			 * This requires all subjects and objects to be
			 * single-level (l2 eq h2), and that the subject
			 * level dominate the object level (h1 dom h2)
			 * in order to have any permissions to it.
			 */
			fprintf(fout, "} (l2 eq h2 and h1 dom h2);\n\n");
		}
119 120
	}

121 122 123 124
	/* enable all policy capabilities */
	for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
		fprintf(fout, "policycap %s;\n", selinux_policycap_names[i]);

125 126
	/* types, roles, and allows */
	fprintf(fout, "type base_t;\n");
127
	fprintf(fout, "role base_r;\n");
128
	fprintf(fout, "role base_r types { base_t };\n");
129 130 131
	for (i = 0; secclass_map[i].name; i++)
		fprintf(fout, "allow base_t base_t:%s *;\n",
			secclass_map[i].name);
132 133 134 135 136 137 138 139
	fprintf(fout, "user user_u roles { base_r }");
	if (mls)
		fprintf(fout, " level %s range %s - %s", SYSTEMLOW,
			SYSTEMLOW, SYSTEMHIGH);
	fprintf(fout, ";\n");

#define SUBJUSERROLETYPE "user_u:base_r:base_t"
#define OBJUSERROLETYPE "user_u:object_r:base_t"
140 141

	/* default sids */
142 143 144 145 146 147 148 149 150 151
	for (i = 1; i < initial_sid_to_string_len; i++) {
		const char *name = initial_sid_to_string[i];

		if (name)
			fprintf(fout, "sid %s ", name);
		else
			fprintf(fout, "sid unused%d\n", i);
		fprintf(fout, SUBJUSERROLETYPE "%s\n",
			mls ? ":" SYSTEMLOW : "");
	}
152 153
	fprintf(fout, "\n");

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#define FS_USE(behavior, fstype)			    \
	fprintf(fout, "fs_use_%s %s " OBJUSERROLETYPE "%s;\n", \
		behavior, fstype, mls ? ":" SYSTEMLOW : "")

	/*
	 * Filesystems whose inode labels can be fetched via getxattr.
	 */
#ifdef CONFIG_EXT2_FS_SECURITY
	FS_USE("xattr", "ext2");
#endif
#ifdef CONFIG_EXT4_FS_SECURITY
#ifdef CONFIG_EXT4_USE_FOR_EXT2
	FS_USE("xattr", "ext2");
#endif
	FS_USE("xattr", "ext3");
	FS_USE("xattr", "ext4");
#endif
#ifdef CONFIG_JFS_SECURITY
	FS_USE("xattr", "jfs");
#endif
#ifdef CONFIG_REISERFS_FS_SECURITY
	FS_USE("xattr", "reiserfs");
#endif
#ifdef CONFIG_JFFS2_FS_SECURITY
	FS_USE("xattr", "jffs2");
#endif
#ifdef CONFIG_XFS_FS
	FS_USE("xattr", "xfs");
#endif
#ifdef CONFIG_GFS2_FS
	FS_USE("xattr", "gfs2");
#endif
#ifdef CONFIG_BTRFS_FS
	FS_USE("xattr", "btrfs");
#endif
#ifdef CONFIG_F2FS_FS_SECURITY
	FS_USE("xattr", "f2fs");
#endif
#ifdef CONFIG_OCFS2_FS
	FS_USE("xattr", "ocsfs2");
#endif
#ifdef CONFIG_OVERLAY_FS
	FS_USE("xattr", "overlay");
#endif
#ifdef CONFIG_SQUASHFS_XATTR
	FS_USE("xattr", "squashfs");
#endif

	/*
	 * Filesystems whose inodes are labeled from allocating task.
	 */
	FS_USE("task", "pipefs");
	FS_USE("task", "sockfs");
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	/*
	 * Filesystems whose inode labels are computed from both
	 * the allocating task and the superblock label.
	 */
#ifdef CONFIG_UNIX98_PTYS
	FS_USE("trans", "devpts");
#endif
#ifdef CONFIG_HUGETLBFS
	FS_USE("trans", "hugetlbfs");
#endif
#ifdef CONFIG_TMPFS
	FS_USE("trans", "tmpfs");
#endif
#ifdef CONFIG_DEVTMPFS
	FS_USE("trans", "devtmpfs");
#endif
#ifdef CONFIG_POSIX_MQUEUE
	FS_USE("trans", "mqueue");
#endif
227

228 229 230
#define GENFSCON(fstype, prefix)			     \
	fprintf(fout, "genfscon %s %s " OBJUSERROLETYPE "%s\n", \
		fstype, prefix, mls ? ":" SYSTEMLOW : "")
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	/*
	 * Filesystems whose inodes are labeled from path prefix match
	 * relative to the filesystem root.  Depending on the filesystem,
	 * only a single label for all inodes may be supported.  Here
	 * we list the filesystem types for which per-file labeling is
	 * supported using genfscon; any other filesystem type can also
	 * be added by only with a single entry for all of its inodes.
	 */
#ifdef CONFIG_PROC_FS
	GENFSCON("proc", "/");
#endif
#ifdef CONFIG_SECURITY_SELINUX
	GENFSCON("selinuxfs", "/");
#endif
#ifdef CONFIG_SYSFS
	GENFSCON("sysfs", "/");
#endif
#ifdef CONFIG_DEBUG_FS
	GENFSCON("debugfs", "/");
#endif
#ifdef CONFIG_TRACING
	GENFSCON("tracefs", "/");
#endif
#ifdef CONFIG_PSTORE
	GENFSCON("pstore", "/");
#endif
	GENFSCON("cgroup", "/");
	GENFSCON("cgroup2", "/");
260 261 262 263 264 265 266 267

	fclose(fout);

	fout = fopen(ctxout, "w");
	if (!fout) {
		printf("Wrote policy, but cannot open %s for writing\n", ctxout);
		usage(argv[0]);
	}
268 269
	fprintf(fout, "/ " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
	fprintf(fout, "/.* " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
270 271 272 273
	fclose(fout);

	return 0;
}