genheaders.c 3.7 KB

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