mdp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include "flask.h"
  31. void usage(char *name)
  32. {
  33. printf("usage: %s [-m] policy_file context_file\n", name);
  34. exit(1);
  35. }
  36. void find_common_name(char *cname, char *dest, int len)
  37. {
  38. char *start, *end;
  39. start = strchr(cname, '_')+1;
  40. end = strchr(start, '_');
  41. if (!start || !end || start-cname > len || end-start > len) {
  42. printf("Error with commons defines\n");
  43. exit(1);
  44. }
  45. strncpy(dest, start, end-start);
  46. dest[end-start] = '\0';
  47. }
  48. #define S_(x) x,
  49. static char *classlist[] = {
  50. #include "class_to_string.h"
  51. NULL
  52. };
  53. #undef S_
  54. #include "initial_sid_to_string.h"
  55. #define TB_(x) char *x[] = {
  56. #define TE_(x) NULL };
  57. #define S_(x) x,
  58. #include "common_perm_to_string.h"
  59. #undef TB_
  60. #undef TE_
  61. #undef S_
  62. struct common {
  63. char *cname;
  64. char **perms;
  65. };
  66. struct common common[] = {
  67. #define TB_(x) { #x, x },
  68. #define S_(x)
  69. #define TE_(x)
  70. #include "common_perm_to_string.h"
  71. #undef TB_
  72. #undef TE_
  73. #undef S_
  74. };
  75. #define S_(x, y, z) {x, #y},
  76. struct av_inherit {
  77. int class;
  78. char *common;
  79. };
  80. struct av_inherit av_inherit[] = {
  81. #include "av_inherit.h"
  82. };
  83. #undef S_
  84. #include "av_permissions.h"
  85. #define S_(x, y, z) {x, y, z},
  86. struct av_perms {
  87. int class;
  88. int perm_i;
  89. char *perm_s;
  90. };
  91. struct av_perms av_perms[] = {
  92. #include "av_perm_to_string.h"
  93. };
  94. #undef S_
  95. int main(int argc, char *argv[])
  96. {
  97. int i, j, mls = 0;
  98. char **arg, *polout, *ctxout;
  99. int classlist_len, initial_sid_to_string_len;
  100. FILE *fout;
  101. if (argc < 3)
  102. usage(argv[0]);
  103. arg = argv+1;
  104. if (argc==4 && strcmp(argv[1], "-m") == 0) {
  105. mls = 1;
  106. arg++;
  107. }
  108. polout = *arg++;
  109. ctxout = *arg;
  110. fout = fopen(polout, "w");
  111. if (!fout) {
  112. printf("Could not open %s for writing\n", polout);
  113. usage(argv[0]);
  114. }
  115. classlist_len = sizeof(classlist) / sizeof(char *);
  116. /* print out the classes */
  117. for (i=1; i < classlist_len; i++) {
  118. if(classlist[i])
  119. fprintf(fout, "class %s\n", classlist[i]);
  120. else
  121. fprintf(fout, "class user%d\n", i);
  122. }
  123. fprintf(fout, "\n");
  124. initial_sid_to_string_len = sizeof(initial_sid_to_string) / sizeof (char *);
  125. /* print out the sids */
  126. for (i=1; i < initial_sid_to_string_len; i++)
  127. fprintf(fout, "sid %s\n", initial_sid_to_string[i]);
  128. fprintf(fout, "\n");
  129. /* print out the commons */
  130. for (i=0; i< sizeof(common)/sizeof(struct common); i++) {
  131. char cname[101];
  132. find_common_name(common[i].cname, cname, 100);
  133. cname[100] = '\0';
  134. fprintf(fout, "common %s\n{\n", cname);
  135. for (j=0; common[i].perms[j]; j++)
  136. fprintf(fout, "\t%s\n", common[i].perms[j]);
  137. fprintf(fout, "}\n\n");
  138. }
  139. fprintf(fout, "\n");
  140. /* print out the class permissions */
  141. for (i=1; i < classlist_len; i++) {
  142. if (classlist[i]) {
  143. int firstperm = -1, numperms = 0;
  144. fprintf(fout, "class %s\n", classlist[i]);
  145. /* does it inherit from a common? */
  146. for (j=0; j < sizeof(av_inherit)/sizeof(struct av_inherit); j++)
  147. if (av_inherit[j].class == i)
  148. fprintf(fout, "inherits %s\n", av_inherit[j].common);
  149. for (j=0; j < sizeof(av_perms)/sizeof(struct av_perms); j++) {
  150. if (av_perms[j].class == i) {
  151. if (firstperm == -1)
  152. firstperm = j;
  153. numperms++;
  154. }
  155. }
  156. if (!numperms) {
  157. fprintf(fout, "\n");
  158. continue;
  159. }
  160. fprintf(fout, "{\n");
  161. /* print out the av_perms */
  162. for (j=0; j < numperms; j++) {
  163. fprintf(fout, "\t%s\n", av_perms[firstperm+j].perm_s);
  164. }
  165. fprintf(fout, "}\n\n");
  166. }
  167. }
  168. fprintf(fout, "\n");
  169. /* NOW PRINT OUT MLS STUFF */
  170. if (mls) {
  171. printf("MLS not yet implemented\n");
  172. exit(1);
  173. }
  174. /* types, roles, and allows */
  175. fprintf(fout, "type base_t;\n");
  176. fprintf(fout, "role base_r types { base_t };\n");
  177. for (i=1; i < classlist_len; i++) {
  178. if (classlist[i])
  179. fprintf(fout, "allow base_t base_t:%s *;\n", classlist[i]);
  180. else
  181. fprintf(fout, "allow base_t base_t:user%d *;\n", i);
  182. }
  183. fprintf(fout, "user user_u roles { base_r };\n");
  184. fprintf(fout, "\n");
  185. /* default sids */
  186. for (i=1; i < initial_sid_to_string_len; i++)
  187. fprintf(fout, "sid %s user_u:base_r:base_t\n", initial_sid_to_string[i]);
  188. fprintf(fout, "\n");
  189. fprintf(fout, "fs_use_xattr ext2 user_u:base_r:base_t;\n");
  190. fprintf(fout, "fs_use_xattr ext3 user_u:base_r:base_t;\n");
  191. fprintf(fout, "fs_use_xattr jfs user_u:base_r:base_t;\n");
  192. fprintf(fout, "fs_use_xattr xfs user_u:base_r:base_t;\n");
  193. fprintf(fout, "fs_use_xattr reiserfs user_u:base_r:base_t;\n");
  194. fprintf(fout, "fs_use_task pipefs user_u:base_r:base_t;\n");
  195. fprintf(fout, "fs_use_task sockfs user_u:base_r:base_t;\n");
  196. fprintf(fout, "fs_use_trans devpts user_u:base_r:base_t;\n");
  197. fprintf(fout, "fs_use_trans tmpfs user_u:base_r:base_t;\n");
  198. fprintf(fout, "fs_use_trans shm user_u:base_r:base_t;\n");
  199. fprintf(fout, "genfscon proc / user_u:base_r:base_t\n");
  200. fclose(fout);
  201. fout = fopen(ctxout, "w");
  202. if (!fout) {
  203. printf("Wrote policy, but cannot open %s for writing\n", ctxout);
  204. usage(argv[0]);
  205. }
  206. fprintf(fout, "/ user_u:base_r:base_t\n");
  207. fprintf(fout, "/.* user_u:base_r:base_t\n");
  208. fclose(fout);
  209. return 0;
  210. }