1 // SPDX-License-Identifier: GPL-2.0
10 struct security_class_mapping {
12 const char *perms[sizeof(unsigned) * 8 + 1];
16 #include "initial_sid_to_string.h"
20 static void usage(void)
22 printf("usage: %s flask.h av_permissions.h\n", progname);
26 static char *stoupperx(const char *s)
32 fprintf(stderr, "%s: out of memory\n", progname);
41 int main(int argc, char *argv[])
52 fout = fopen(argv[1], "w");
54 fprintf(stderr, "Could not open %s for writing: %s\n",
55 argv[1], strerror(errno));
59 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
60 fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
62 for (i = 0; secclass_map[i].name; i++) {
63 char *name = stoupperx(secclass_map[i].name);
65 fprintf(fout, "#define SECCLASS_%-39s %2d\n", name, i+1);
71 isids_len = sizeof(initial_sid_to_string) / sizeof(char *);
72 for (i = 1; i < isids_len; i++) {
73 const char *s = initial_sid_to_string[i];
75 char *sidname = stoupperx(s);
77 fprintf(fout, "#define SECINITSID_%-39s %2d\n", sidname, i);
81 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
82 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
84 fprintf(fout, "\tbool sock = false;\n\n");
85 fprintf(fout, "\tswitch (kern_tclass) {\n");
86 for (i = 0; secclass_map[i].name; i++) {
87 static char s[] = "SOCKET";
89 char *name = stoupperx(secclass_map[i].name);
93 if (len >= l && memcmp(name + len - l, s, l) == 0)
94 fprintf(fout, "\tcase SECCLASS_%s:\n", name);
97 fprintf(fout, "\t\tsock = true;\n");
98 fprintf(fout, "\t\tbreak;\n");
99 fprintf(fout, "\tdefault:\n");
100 fprintf(fout, "\t\tbreak;\n");
101 fprintf(fout, "\t}\n\n");
102 fprintf(fout, "\treturn sock;\n");
103 fprintf(fout, "}\n");
105 fprintf(fout, "\n#endif\n");
107 if (fclose(fout) != 0) {
108 fprintf(stderr, "Could not successfully close %s: %s\n",
109 argv[1], strerror(errno));
113 fout = fopen(argv[2], "w");
115 fprintf(stderr, "Could not open %s for writing: %s\n",
116 argv[2], strerror(errno));
120 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
121 fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
123 for (i = 0; secclass_map[i].name; i++) {
124 const struct security_class_mapping *map = &secclass_map[i];
126 char *name = stoupperx(map->name);
129 for (j = 0; map->perms[j]; j++) {
133 fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
134 map->name, map->perms[j]);
137 permname = stoupperx(map->perms[j]);
138 fprintf(fout, "#define %s__%-*s 0x%08xU\n", name,
139 39-len, permname, 1U<<j);
145 fprintf(fout, "\n#endif\n");
147 if (fclose(fout) != 0) {
148 fprintf(stderr, "Could not successfully close %s: %s\n",
149 argv[2], strerror(errno));