genheaders.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* NOTE: we really do want to use the kernel headers here */
  3. #define __EXPORTED_HEADERS__
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <ctype.h>
  10. struct security_class_mapping {
  11. const char *name;
  12. const char *perms[sizeof(unsigned) * 8 + 1];
  13. };
  14. #include "classmap.h"
  15. #include "initial_sid_to_string.h"
  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;
  37. int isids_len;
  38. FILE *fout;
  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. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  49. fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
  50. for (i = 0; secclass_map[i].name; i++) {
  51. char *name = stoupperx(secclass_map[i].name);
  52. fprintf(fout, "#define SECCLASS_%-39s %2d\n", name, i+1);
  53. free(name);
  54. }
  55. fprintf(fout, "\n");
  56. isids_len = sizeof(initial_sid_to_string) / sizeof(char *);
  57. for (i = 1; i < isids_len; i++) {
  58. const char *s = initial_sid_to_string[i];
  59. if (s) {
  60. char *sidname = stoupperx(s);
  61. fprintf(fout, "#define SECINITSID_%-39s %2d\n", sidname, i);
  62. free(sidname);
  63. }
  64. }
  65. fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
  66. fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
  67. fprintf(fout, "{\n");
  68. fprintf(fout, "\tbool sock = false;\n\n");
  69. fprintf(fout, "\tswitch (kern_tclass) {\n");
  70. for (i = 0; secclass_map[i].name; i++) {
  71. static char s[] = "SOCKET";
  72. int len, l;
  73. char *name = stoupperx(secclass_map[i].name);
  74. len = strlen(name);
  75. l = sizeof(s) - 1;
  76. if (len >= l && memcmp(name + len - l, s, l) == 0)
  77. fprintf(fout, "\tcase SECCLASS_%s:\n", name);
  78. free(name);
  79. }
  80. fprintf(fout, "\t\tsock = true;\n");
  81. fprintf(fout, "\t\tbreak;\n");
  82. fprintf(fout, "\tdefault:\n");
  83. fprintf(fout, "\t\tbreak;\n");
  84. fprintf(fout, "\t}\n\n");
  85. fprintf(fout, "\treturn sock;\n");
  86. fprintf(fout, "}\n");
  87. fprintf(fout, "\n#endif\n");
  88. if (fclose(fout) != 0) {
  89. fprintf(stderr, "Could not successfully close %s: %s\n",
  90. argv[1], strerror(errno));
  91. exit(4);
  92. }
  93. fout = fopen(argv[2], "w");
  94. if (!fout) {
  95. fprintf(stderr, "Could not open %s for writing: %s\n",
  96. argv[2], strerror(errno));
  97. exit(5);
  98. }
  99. fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
  100. fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
  101. for (i = 0; secclass_map[i].name; i++) {
  102. const struct security_class_mapping *map = &secclass_map[i];
  103. int len;
  104. char *name = stoupperx(map->name);
  105. len = strlen(name);
  106. for (j = 0; map->perms[j]; j++) {
  107. char *permname;
  108. if (j >= 32) {
  109. fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
  110. map->name, map->perms[j]);
  111. exit(5);
  112. }
  113. permname = stoupperx(map->perms[j]);
  114. fprintf(fout, "#define %s__%-*s 0x%08xU\n", name,
  115. 39-len, permname, 1U<<j);
  116. free(permname);
  117. }
  118. free(name);
  119. }
  120. fprintf(fout, "\n#endif\n");
  121. if (fclose(fout) != 0) {
  122. fprintf(stderr, "Could not successfully close %s: %s\n",
  123. argv[2], strerror(errno));
  124. exit(6);
  125. }
  126. exit(0);
  127. }