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"
21 #define max(x, y) (((int)(x) > (int)(y)) ? x : y)
25 static void usage(void)
27 printf("usage: %s flask.h av_permissions.h\n", progname);
31 static char *stoupperx(const char *s)
37 fprintf(stderr, "%s: out of memory\n", progname);
46 int main(int argc, char *argv[])
51 const char *needle = "SOCKET";
59 fout = fopen(argv[1], "w");
61 fprintf(stderr, "Could not open %s for writing: %s\n",
62 argv[1], strerror(errno));
66 for (i = 0; secclass_map[i].name; i++) {
67 struct security_class_mapping *map = &secclass_map[i];
68 map->name = stoupperx(map->name);
69 for (j = 0; map->perms[j]; j++)
70 map->perms[j] = stoupperx(map->perms[j]);
73 isids_len = sizeof(initial_sid_to_string) / sizeof (char *);
74 for (i = 1; i < isids_len; i++)
75 initial_sid_to_string[i] = stoupperx(initial_sid_to_string[i]);
77 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
78 fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
80 for (i = 0; secclass_map[i].name; i++) {
81 struct security_class_mapping *map = &secclass_map[i];
82 fprintf(fout, "#define SECCLASS_%s", map->name);
83 for (j = 0; j < max(1, 40 - strlen(map->name)); j++)
85 fprintf(fout, "%2d\n", i+1);
90 for (i = 1; i < isids_len; i++) {
91 const char *s = initial_sid_to_string[i];
92 fprintf(fout, "#define SECINITSID_%s", s);
93 for (j = 0; j < max(1, 40 - strlen(s)); j++)
95 fprintf(fout, "%2d\n", i);
97 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
98 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
100 fprintf(fout, "\tbool sock = false;\n\n");
101 fprintf(fout, "\tswitch (kern_tclass) {\n");
102 for (i = 0; secclass_map[i].name; i++) {
103 struct security_class_mapping *map = &secclass_map[i];
104 substr = strstr(map->name, needle);
105 if (substr && strcmp(substr, needle) == 0)
106 fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
108 fprintf(fout, "\t\tsock = true;\n");
109 fprintf(fout, "\t\tbreak;\n");
110 fprintf(fout, "\tdefault:\n");
111 fprintf(fout, "\t\tbreak;\n");
112 fprintf(fout, "\t}\n\n");
113 fprintf(fout, "\treturn sock;\n");
114 fprintf(fout, "}\n");
116 fprintf(fout, "\n#endif\n");
119 fout = fopen(argv[2], "w");
121 fprintf(stderr, "Could not open %s for writing: %s\n",
122 argv[2], strerror(errno));
126 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
127 fprintf(fout, "#ifndef _SELINUX_AV_PERMISSIONS_H_\n#define _SELINUX_AV_PERMISSIONS_H_\n\n");
129 for (i = 0; secclass_map[i].name; i++) {
130 struct security_class_mapping *map = &secclass_map[i];
131 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 fprintf(fout, "#define %s__%s", map->name,
139 for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
141 fprintf(fout, "0x%08xU\n", (1<<j));
145 fprintf(fout, "\n#endif\n");