^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /* NOTE: we really do want to use the kernel headers here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #define __EXPORTED_HEADERS__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct security_class_mapping {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) const char *perms[sizeof(unsigned) * 8 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "classmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "initial_sid_to_string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) const char *progname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) printf("usage: %s flask.h av_permissions.h\n", progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static char *stoupperx(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) char *s2 = strdup(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!s2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) fprintf(stderr, "%s: out of memory\n", progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) exit(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) for (p = s2; *p; p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *p = toupper(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int isids_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) FILE *fout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) progname = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (argc < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) fout = fopen(argv[1], "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!fout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) fprintf(stderr, "Could not open %s for writing: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) argv[1], strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) for (i = 0; secclass_map[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct security_class_mapping *map = &secclass_map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) map->name = stoupperx(map->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) for (j = 0; map->perms[j]; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) map->perms[j] = stoupperx(map->perms[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) for (i = 1; i < isids_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const char *s = initial_sid_to_string[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) initial_sid_to_string[i] = stoupperx(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) for (i = 0; secclass_map[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct security_class_mapping *map = &secclass_map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) fprintf(fout, "#define SECCLASS_%-39s %2d\n", map->name, i+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) fprintf(fout, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (i = 1; i < isids_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const char *s = initial_sid_to_string[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fprintf(fout, "#define SECINITSID_%-39s %2d\n", s, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) fprintf(fout, "{\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) fprintf(fout, "\tbool sock = false;\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) fprintf(fout, "\tswitch (kern_tclass) {\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (i = 0; secclass_map[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static char s[] = "SOCKET";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct security_class_mapping *map = &secclass_map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int len = strlen(map->name), l = sizeof(s) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (len >= l && memcmp(map->name + len - l, s, l) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) fprintf(fout, "\t\tsock = true;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fprintf(fout, "\t\tbreak;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fprintf(fout, "\tdefault:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) fprintf(fout, "\t\tbreak;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) fprintf(fout, "\t}\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fprintf(fout, "\treturn sock;\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fprintf(fout, "}\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fprintf(fout, "\n#endif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fclose(fout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) fout = fopen(argv[2], "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!fout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fprintf(stderr, "Could not open %s for writing: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) argv[2], strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) exit(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (i = 0; secclass_map[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct security_class_mapping *map = &secclass_map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int len = strlen(map->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) for (j = 0; map->perms[j]; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (j >= 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) map->name, map->perms[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) exit(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 39-len, map->perms[j], 1U<<j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fprintf(fout, "\n#endif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) fclose(fout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }