mdp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * mdp - make dummy policy
  5. *
  6. * When pointed at a kernel tree, builds a dummy policy for that kernel
  7. * with exactly one type with full rights to itself.
  8. *
  9. * Copyright (C) IBM Corporation, 2006
  10. *
  11. * Authors: Serge E. Hallyn <[email protected]>
  12. */
  13. /* NOTE: we really do want to use the kernel headers here */
  14. #define __EXPORTED_HEADERS__
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <linux/kconfig.h>
  20. static void usage(char *name)
  21. {
  22. printf("usage: %s [-m] policy_file context_file\n", name);
  23. exit(1);
  24. }
  25. /* Class/perm mapping support */
  26. struct security_class_mapping {
  27. const char *name;
  28. const char *perms[sizeof(unsigned) * 8 + 1];
  29. };
  30. #include "classmap.h"
  31. #include "initial_sid_to_string.h"
  32. #include "policycap_names.h"
  33. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  34. int main(int argc, char *argv[])
  35. {
  36. int i, j, mls = 0;
  37. int initial_sid_to_string_len;
  38. char **arg, *polout, *ctxout;
  39. FILE *fout;
  40. if (argc < 3)
  41. usage(argv[0]);
  42. arg = argv+1;
  43. if (argc==4 && strcmp(argv[1], "-m") == 0) {
  44. mls = 1;
  45. arg++;
  46. }
  47. polout = *arg++;
  48. ctxout = *arg;
  49. fout = fopen(polout, "w");
  50. if (!fout) {
  51. printf("Could not open %s for writing\n", polout);
  52. usage(argv[0]);
  53. }
  54. /* print out the classes */
  55. for (i = 0; secclass_map[i].name; i++)
  56. fprintf(fout, "class %s\n", secclass_map[i].name);
  57. fprintf(fout, "\n");
  58. initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
  59. /* print out the sids */
  60. for (i = 1; i < initial_sid_to_string_len; i++) {
  61. const char *name = initial_sid_to_string[i];
  62. if (name)
  63. fprintf(fout, "sid %s\n", name);
  64. else
  65. fprintf(fout, "sid unused%d\n", i);
  66. }
  67. fprintf(fout, "\n");
  68. /* print out the class permissions */
  69. for (i = 0; secclass_map[i].name; i++) {
  70. const struct security_class_mapping *map = &secclass_map[i];
  71. fprintf(fout, "class %s\n", map->name);
  72. fprintf(fout, "{\n");
  73. for (j = 0; map->perms[j]; j++)
  74. fprintf(fout, "\t%s\n", map->perms[j]);
  75. fprintf(fout, "}\n\n");
  76. }
  77. fprintf(fout, "\n");
  78. /* print out mls declarations and constraints */
  79. if (mls) {
  80. fprintf(fout, "sensitivity s0;\n");
  81. fprintf(fout, "sensitivity s1;\n");
  82. fprintf(fout, "dominance { s0 s1 }\n");
  83. fprintf(fout, "category c0;\n");
  84. fprintf(fout, "category c1;\n");
  85. fprintf(fout, "level s0:c0.c1;\n");
  86. fprintf(fout, "level s1:c0.c1;\n");
  87. #define SYSTEMLOW "s0"
  88. #define SYSTEMHIGH "s1:c0.c1"
  89. for (i = 0; secclass_map[i].name; i++) {
  90. const struct security_class_mapping *map = &secclass_map[i];
  91. fprintf(fout, "mlsconstrain %s {\n", map->name);
  92. for (j = 0; map->perms[j]; j++)
  93. fprintf(fout, "\t%s\n", map->perms[j]);
  94. /*
  95. * This requires all subjects and objects to be
  96. * single-level (l2 eq h2), and that the subject
  97. * level dominate the object level (h1 dom h2)
  98. * in order to have any permissions to it.
  99. */
  100. fprintf(fout, "} (l2 eq h2 and h1 dom h2);\n\n");
  101. }
  102. }
  103. /* enable all policy capabilities */
  104. for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
  105. fprintf(fout, "policycap %s;\n", selinux_policycap_names[i]);
  106. /* types, roles, and allows */
  107. fprintf(fout, "type base_t;\n");
  108. fprintf(fout, "role base_r;\n");
  109. fprintf(fout, "role base_r types { base_t };\n");
  110. for (i = 0; secclass_map[i].name; i++)
  111. fprintf(fout, "allow base_t base_t:%s *;\n",
  112. secclass_map[i].name);
  113. fprintf(fout, "user user_u roles { base_r }");
  114. if (mls)
  115. fprintf(fout, " level %s range %s - %s", SYSTEMLOW,
  116. SYSTEMLOW, SYSTEMHIGH);
  117. fprintf(fout, ";\n");
  118. #define SUBJUSERROLETYPE "user_u:base_r:base_t"
  119. #define OBJUSERROLETYPE "user_u:object_r:base_t"
  120. /* default sids */
  121. for (i = 1; i < initial_sid_to_string_len; i++) {
  122. const char *name = initial_sid_to_string[i];
  123. if (name)
  124. fprintf(fout, "sid %s ", name);
  125. else
  126. fprintf(fout, "sid unused%d\n", i);
  127. fprintf(fout, SUBJUSERROLETYPE "%s\n",
  128. mls ? ":" SYSTEMLOW : "");
  129. }
  130. fprintf(fout, "\n");
  131. #define FS_USE(behavior, fstype) \
  132. fprintf(fout, "fs_use_%s %s " OBJUSERROLETYPE "%s;\n", \
  133. behavior, fstype, mls ? ":" SYSTEMLOW : "")
  134. /*
  135. * Filesystems whose inode labels can be fetched via getxattr.
  136. */
  137. #ifdef CONFIG_EXT2_FS_SECURITY
  138. FS_USE("xattr", "ext2");
  139. #endif
  140. #ifdef CONFIG_EXT4_FS_SECURITY
  141. #ifdef CONFIG_EXT4_USE_FOR_EXT2
  142. FS_USE("xattr", "ext2");
  143. #endif
  144. FS_USE("xattr", "ext3");
  145. FS_USE("xattr", "ext4");
  146. #endif
  147. #ifdef CONFIG_JFS_SECURITY
  148. FS_USE("xattr", "jfs");
  149. #endif
  150. #ifdef CONFIG_REISERFS_FS_SECURITY
  151. FS_USE("xattr", "reiserfs");
  152. #endif
  153. #ifdef CONFIG_JFFS2_FS_SECURITY
  154. FS_USE("xattr", "jffs2");
  155. #endif
  156. #ifdef CONFIG_XFS_FS
  157. FS_USE("xattr", "xfs");
  158. #endif
  159. #ifdef CONFIG_GFS2_FS
  160. FS_USE("xattr", "gfs2");
  161. #endif
  162. #ifdef CONFIG_BTRFS_FS
  163. FS_USE("xattr", "btrfs");
  164. #endif
  165. #ifdef CONFIG_F2FS_FS_SECURITY
  166. FS_USE("xattr", "f2fs");
  167. #endif
  168. #ifdef CONFIG_OCFS2_FS
  169. FS_USE("xattr", "ocsfs2");
  170. #endif
  171. #ifdef CONFIG_OVERLAY_FS
  172. FS_USE("xattr", "overlay");
  173. #endif
  174. #ifdef CONFIG_SQUASHFS_XATTR
  175. FS_USE("xattr", "squashfs");
  176. #endif
  177. /*
  178. * Filesystems whose inodes are labeled from allocating task.
  179. */
  180. FS_USE("task", "pipefs");
  181. FS_USE("task", "sockfs");
  182. /*
  183. * Filesystems whose inode labels are computed from both
  184. * the allocating task and the superblock label.
  185. */
  186. #ifdef CONFIG_UNIX98_PTYS
  187. FS_USE("trans", "devpts");
  188. #endif
  189. #ifdef CONFIG_HUGETLBFS
  190. FS_USE("trans", "hugetlbfs");
  191. #endif
  192. #ifdef CONFIG_TMPFS
  193. FS_USE("trans", "tmpfs");
  194. #endif
  195. #ifdef CONFIG_DEVTMPFS
  196. FS_USE("trans", "devtmpfs");
  197. #endif
  198. #ifdef CONFIG_POSIX_MQUEUE
  199. FS_USE("trans", "mqueue");
  200. #endif
  201. #define GENFSCON(fstype, prefix) \
  202. fprintf(fout, "genfscon %s %s " OBJUSERROLETYPE "%s\n", \
  203. fstype, prefix, mls ? ":" SYSTEMLOW : "")
  204. /*
  205. * Filesystems whose inodes are labeled from path prefix match
  206. * relative to the filesystem root. Depending on the filesystem,
  207. * only a single label for all inodes may be supported. Here
  208. * we list the filesystem types for which per-file labeling is
  209. * supported using genfscon; any other filesystem type can also
  210. * be added by only with a single entry for all of its inodes.
  211. */
  212. #ifdef CONFIG_PROC_FS
  213. GENFSCON("proc", "/");
  214. #endif
  215. #ifdef CONFIG_SECURITY_SELINUX
  216. GENFSCON("selinuxfs", "/");
  217. #endif
  218. #ifdef CONFIG_SYSFS
  219. GENFSCON("sysfs", "/");
  220. #endif
  221. #ifdef CONFIG_DEBUG_FS
  222. GENFSCON("debugfs", "/");
  223. #endif
  224. #ifdef CONFIG_TRACING
  225. GENFSCON("tracefs", "/");
  226. #endif
  227. #ifdef CONFIG_PSTORE
  228. GENFSCON("pstore", "/");
  229. #endif
  230. GENFSCON("cgroup", "/");
  231. GENFSCON("cgroup2", "/");
  232. fclose(fout);
  233. fout = fopen(ctxout, "w");
  234. if (!fout) {
  235. printf("Wrote policy, but cannot open %s for writing\n", ctxout);
  236. usage(argv[0]);
  237. }
  238. fprintf(fout, "/ " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
  239. fprintf(fout, "/.* " OBJUSERROLETYPE "%s\n", mls ? ":" SYSTEMLOW : "");
  240. fclose(fout);
  241. return 0;
  242. }