1 // SPDX-License-Identifier: GPL-2.0
3 /* NOTE: we really do want to use the kernel headers here */
4 #define __EXPORTED_HEADERS__
12 #include <sys/socket.h>
14 struct security_class_mapping {
16 const char *perms[sizeof(unsigned) * 8 + 1];
20 #include "initial_sid_to_string.h"
24 static void usage(void)
26 printf("usage: %s flask.h av_permissions.h\n", progname);
30 static char *stoupperx(const char *s)
36 fprintf(stderr, "%s: out of memory\n", progname);
45 int main(int argc, char *argv[])
56 fout = fopen(argv[1], "w");
58 fprintf(stderr, "Could not open %s for writing: %s\n",
59 argv[1], strerror(errno));
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]);
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]);
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");
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);
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);
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");
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);
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");
108 fprintf(fout, "\n#endif\n");
111 fout = fopen(argv[2], "w");
113 fprintf(stderr, "Could not open %s for writing: %s\n",
114 argv[2], strerror(errno));
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");
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++) {
126 fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
127 map->name, map->perms[j]);
130 fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
131 39-len, map->perms[j], 1U<<j);
135 fprintf(fout, "\n#endif\n");