genheaders.c 3.4 KB

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