1 // SPDX-License-Identifier: GPL-2.0
3 /* NOTE: we really do want to use the kernel headers here */
4 #define __EXPORTED_HEADERS__
13 struct security_class_mapping {
15 const char *perms[sizeof(unsigned) * 8 + 1];
19 #include "initial_sid_to_string.h"
23 static void usage(void)
25 printf("usage: %s flask.h av_permissions.h\n", progname);
29 static char *stoupperx(const char *s)
35 fprintf(stderr, "%s: out of memory\n", progname);
44 int main(int argc, char *argv[])
55 fout = fopen(argv[1], "w");
57 fprintf(stderr, "Could not open %s for writing: %s\n",
58 argv[1], strerror(errno));
62 for (i = 0; secclass_map[i].name; i++) {
63 struct security_class_mapping *map = &secclass_map[i];
64 map->name = stoupperx(map->name);
65 for (j = 0; map->perms[j]; j++)
66 map->perms[j] = stoupperx(map->perms[j]);
69 isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
70 for (i = 1; i < isids_len; i++)
71 initial_sid_to_string[i] = stoupperx(initial_sid_to_string[i]);
73 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
74 fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
76 for (i = 0; secclass_map[i].name; i++) {
77 struct security_class_mapping *map = &secclass_map[i];
78 fprintf(fout, "#define SECCLASS_%-39s %2d\n", map->name, i+1);
83 for (i = 1; i < isids_len; i++) {
84 const char *s = initial_sid_to_string[i];
85 fprintf(fout, "#define SECINITSID_%-39s %2d\n", s, i);
87 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
88 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
90 fprintf(fout, "\tbool sock = false;\n\n");
91 fprintf(fout, "\tswitch (kern_tclass) {\n");
92 for (i = 0; secclass_map[i].name; i++) {
93 static char s[] = "SOCKET";
94 struct security_class_mapping *map = &secclass_map[i];
95 int len = strlen(map->name), l = sizeof(s) - 1;
96 if (len >= l && memcmp(map->name + len - l, s, l) == 0)
97 fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
99 fprintf(fout, "\t\tsock = true;\n");
100 fprintf(fout, "\t\tbreak;\n");
101 fprintf(fout, "\tdefault:\n");
102 fprintf(fout, "\t\tbreak;\n");
103 fprintf(fout, "\t}\n\n");
104 fprintf(fout, "\treturn sock;\n");
105 fprintf(fout, "}\n");
107 fprintf(fout, "\n#endif\n");
110 fout = fopen(argv[2], "w");
112 fprintf(stderr, "Could not open %s for writing: %s\n",
113 argv[2], strerror(errno));
117 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
118 fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
120 for (i = 0; secclass_map[i].name; i++) {
121 struct security_class_mapping *map = &secclass_map[i];
122 int len = strlen(map->name);
123 for (j = 0; map->perms[j]; j++) {
125 fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
126 map->name, map->perms[j]);
129 fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
130 39-len, map->perms[j], 1U<<j);
134 fprintf(fout, "\n#endif\n");