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