mdp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. *
  3. * mdp - make dummy policy
  4. *
  5. * When pointed at a kernel tree, builds a dummy policy for that kernel
  6. * with exactly one type with full rights to itself.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * Copyright (C) IBM Corporation, 2006
  23. *
  24. * Authors: Serge E. Hallyn <serue@us.ibm.com>
  25. */
  26. /* NOTE: we really do want to use the kernel headers here */
  27. #define __EXPORTED_HEADERS__
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <linux/kconfig.h>
  33. static void usage(char *name)
  34. {
  35. printf("usage: %s [-m] policy_file context_file\n", name);
  36. exit(1);
  37. }
  38. /* Class/perm mapping support */
  39. struct security_class_mapping {
  40. const char *name;
  41. const char *perms[sizeof(unsigned) * 8 + 1];
  42. };
  43. #include "classmap.h"
  44. #include "initial_sid_to_string.h"
  45. int main(int argc, char *argv[])
  46. {
  47. int i, j, mls = 0;
  48. int initial_sid_to_string_len;
  49. char **arg, *polout, *ctxout;
  50. FILE *fout;
  51. if (argc < 3)
  52. usage(argv[0]);
  53. arg = argv+1;
  54. if (argc==4 && strcmp(argv[1], "-m") == 0) {
  55. mls = 1;
  56. arg++;
  57. }
  58. polout = *arg++;
  59. ctxout = *arg;
  60. fout = fopen(polout, "w");
  61. if (!fout) {
  62. printf("Could not open %s for writing\n", polout);
  63. usage(argv[0]);
  64. }
  65. /* print out the classes */
  66. for (i = 0; secclass_map[i].name; i++)
  67. fprintf(fout, "class %s\n", secclass_map[i].name);
  68. fprintf(fout, "\n");
  69. initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
  70. /* print out the sids */
  71. for (i = 1; i < initial_sid_to_string_len; i++)
  72. fprintf(fout, "sid %s\n", initial_sid_to_string[i]);
  73. fprintf(fout, "\n");
  74. /* print out the class permissions */
  75. for (i = 0; secclass_map[i].name; i++) {
  76. struct security_class_mapping *map = &secclass_map[i];
  77. fprintf(fout, "class %s\n", map->name);
  78. fprintf(fout, "{\n");
  79. for (j = 0; map->perms[j]; j++)
  80. fprintf(fout, "\t%s\n", map->perms[j]);
  81. fprintf(fout, "}\n\n");
  82. }
  83. fprintf(fout, "\n");
  84. /* print out mls declarations and constraints */
  85. if (mls) {
  86. fprintf(fout, "sensitivity s0;\n");
  87. fprintf(fout, "sensitivity s1;\n");
  88. fprintf(fout, "dominance { s0 s1 }\n");
  89. fprintf(fout, "category c0;\n");
  90. fprintf(fout, "category c1;\n");
  91. fprintf(fout, "level s0:c0.c1;\n");
  92. fprintf(fout, "level s1:c0.c1;\n");
  93. #define SYSTEMLOW "s0"
  94. #define SYSTEMHIGH "s1:c0.c1"
  95. for (i = 0; secclass_map[i].name; i++) {
  96. struct security_class_mapping *map = &secclass_map[i];
  97. fprintf(fout, "mlsconstrain %s {\n", map->name);
  98. for (j = 0; map->perms[j]; j++)
  99. fprintf(fout, "\t%s\n", map->perms[j]);
  100. /*
  101. * This requires all subjects and objects to be
  102. * single-level (l2 eq h2), and that the subject
  103. * level dominate the object level (h1 dom h2)
  104. * in order to have any permissions to it.
  105. */
  106. fprintf(fout, "} (l2 eq h2 and h1 dom h2);\n\n");
  107. }
  108. }
  109. /* types, roles, and allows */
  110. fprintf(fout, "type base_t;\n");
  111. fprintf(fout, "role base_r;\n");
  112. fprintf(fout, "role base_r types { base_t };\n");
  113. for (i = 0; secclass_map[i].name; i++)
  114. fprintf(fout, "allow base_t base_t:%s *;\n",
  115. secclass_map[i].name);
  116. fprintf(fout, "user user_u roles { base_r }");
  117. if (mls)
  118. fprintf(fout, " level %s range %s - %s", SYSTEMLOW,
  119. SYSTEMLOW, SYSTEMHIGH);
  120. fprintf(fout, ";\n");
  121. #define SUBJUSERROLETYPE "user_u:base_r:base_t"
  122. #define OBJUSERROLETYPE "user_u:object_r:base_t"
  123. /* default sids */
  124. for (i = 1; i < initial_sid_to_string_len; i++)
  125. fprintf(fout, "sid %s " SUBJUSERROLETYPE "%s\n",
  126. initial_sid_to_string[i], mls ? ":" SYSTEMLOW : "");
  127. fprintf(fout, "\n");
  128. #define FS_USE(behavior, fstype) \
  129. fprintf(fout, "fs_use_%s %s " OBJUSERROLETYPE "%s;\n", \
  130. behavior, fstype, mls ? ":" SYSTEMLOW : "")
  131. /*
  132. * Filesystems whose inode labels can be fetched via getxattr.
  133. */
  134. #ifdef CONFIG_EXT2_FS_SECURITY
  135. FS_USE("xattr", "ext2");
  136. #endif
  137. #ifdef CONFIG_EXT4_FS_SECURITY
  138. #ifdef CONFIG_EXT4_USE_FOR_EXT2
  139. FS_USE("xattr", "ext2");
  140. #endif
  141. FS_USE("xattr", "ext3");
  142. FS_USE("xattr", "ext4");
  143. #endif
  144. #ifdef CONFIG_JFS_SECURITY
  145. FS_USE("xattr", "jfs");
  146. #endif
  147. #ifdef CONFIG_REISERFS_FS_SECURITY
  148. FS_USE("xattr", "reiserfs");
  149. #endif
  150. #ifdef CONFIG_JFFS2_FS_SECURITY
  151. FS_USE("xattr", "jffs2");
  152. #endif
  153. #ifdef CONFIG_XFS_FS
  154. FS_USE("xattr", "xfs");
  155. #endif
  156. #ifdef CONFIG_GFS2_FS
  157. FS_USE("xattr", "gfs2");
  158. #endif
  159. #ifdef CONFIG_BTRFS_FS
  160. FS_USE("xattr", "btrfs");
  161. #endif
  162. #ifdef CONFIG_F2FS_FS_SECURITY
  163. FS_USE("xattr", "f2fs");
  164. #endif
  165. #ifdef CONFIG_OCFS2_FS
  166. FS_USE("xattr", "ocsfs2");
  167. #endif
  168. #ifdef CONFIG_OVERLAY_FS
  169. FS_USE("xattr", "overlay");
  170. #endif
  171. #ifdef CONFIG_SQUASHFS_XATTR
  172. FS_USE("xattr", "squashfs");
  173. #endif
  174. #ifdef CONFIG_LUSTRE_FS_SECURITY
  175. FS_USE("xattr", "lustre");
  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. }