genheaders.c 3.4 KB

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