genheaders.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* NOTE: we really do want to use the kernel headers here */
  2. #define __EXPORTED_HEADERS__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <ctype.h>
  9. struct security_class_mapping {
  10. const char *name;
  11. const char *perms[sizeof(unsigned) * 8 + 1];
  12. };
  13. #include "classmap.h"
  14. #include "initial_sid_to_string.h"
  15. #define max(x, y) (((int)(x) > (int)(y)) ? x : y)
  16. const char *progname;
  17. static void usage(void)
  18. {
  19. printf("usage: %s flask.h av_permissions.h\n", progname);
  20. exit(1);
  21. }
  22. static char *stoupperx(const char *s)
  23. {
  24. char *s2 = strdup(s);
  25. char *p;
  26. if (!s2) {
  27. fprintf(stderr, "%s: out of memory\n", progname);
  28. exit(3);
  29. }
  30. for (p = s2; *p; p++)
  31. *p = toupper(*p);
  32. return s2;
  33. }
  34. int main(int argc, char *argv[])
  35. {
  36. int i, j, k;
  37. int isids_len;
  38. FILE *fout;
  39. const char *needle = "SOCKET";
  40. char *substr;
  41. progname = argv[0];
  42. if (argc < 3)
  43. usage();
  44. fout = fopen(argv[1], "w");
  45. if (!fout) {
  46. fprintf(stderr, "Could not open %s for writing: %s\n",
  47. argv[1], strerror(errno));
  48. exit(2);
  49. }
  50. for (i = 0; secclass_map[i].name; i++) {
  51. struct security_class_mapping *map = &secclass_map[i];
  52. map->name = stoupperx(map->name);
  53. for (j = 0; map->perms[j]; j++)
  54. map->perms[j] = stoupperx(map->perms[j]);
  55. }
  56. isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
  57. for (i = 1; i < isids_len; i++)
  58. initial_sid_to_string[i] = stoupperx(initial_sid_to_string[i]);
  59. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  60. fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
  61. for (i = 0; secclass_map[i].name; i++) {
  62. struct security_class_mapping *map = &secclass_map[i];
  63. fprintf(fout, "#define SECCLASS_%s", map->name);
  64. for (j = 0; j < max(1, 40 - strlen(map->name)); j++)
  65. fprintf(fout, " ");
  66. fprintf(fout, "%2d\n", i+1);
  67. }
  68. fprintf(fout, "\n");
  69. for (i = 1; i < isids_len; i++) {
  70. const char *s = initial_sid_to_string[i];
  71. fprintf(fout, "#define SECINITSID_%s", s);
  72. for (j = 0; j < max(1, 40 - strlen(s)); j++)
  73. fprintf(fout, " ");
  74. fprintf(fout, "%2d\n", i);
  75. }
  76. fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
  77. fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
  78. fprintf(fout, "{\n");
  79. fprintf(fout, "\tbool sock = false;\n\n");
  80. fprintf(fout, "\tswitch (kern_tclass) {\n");
  81. for (i = 0; secclass_map[i].name; i++) {
  82. struct security_class_mapping *map = &secclass_map[i];
  83. substr = strstr(map->name, needle);
  84. if (substr && strcmp(substr, needle) == 0)
  85. fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
  86. }
  87. fprintf(fout, "\t\tsock = true;\n");
  88. fprintf(fout, "\t\tbreak;\n");
  89. fprintf(fout, "\tdefault:\n");
  90. fprintf(fout, "\t\tbreak;\n");
  91. fprintf(fout, "\t}\n\n");
  92. fprintf(fout, "\treturn sock;\n");
  93. fprintf(fout, "}\n");
  94. fprintf(fout, "\n#endif\n");
  95. fclose(fout);
  96. fout = fopen(argv[2], "w");
  97. if (!fout) {
  98. fprintf(stderr, "Could not open %s for writing: %s\n",
  99. argv[2], strerror(errno));
  100. exit(4);
  101. }
  102. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  103. fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
  104. for (i = 0; secclass_map[i].name; i++) {
  105. struct security_class_mapping *map = &secclass_map[i];
  106. for (j = 0; map->perms[j]; j++) {
  107. fprintf(fout, "#define %s__%s", map->name,
  108. map->perms[j]);
  109. for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
  110. fprintf(fout, " ");
  111. fprintf(fout, "0x%08xUL\n", (1<<j));
  112. }
  113. }
  114. fprintf(fout, "\n#endif\n");
  115. fclose(fout);
  116. exit(0);
  117. }