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