hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/scripts/selinux/genheaders/genheaders.c
....@@ -18,8 +18,6 @@
1818 #include "classmap.h"
1919 #include "initial_sid_to_string.h"
2020
21
-#define max(x, y) (((int)(x) > (int)(y)) ? x : y)
22
-
2321 const char *progname;
2422
2523 static void usage(void)
....@@ -45,11 +43,9 @@
4543
4644 int main(int argc, char *argv[])
4745 {
48
- int i, j, k;
46
+ int i, j;
4947 int isids_len;
5048 FILE *fout;
51
- const char *needle = "SOCKET";
52
- char *substr;
5349
5450 progname = argv[0];
5551
....@@ -71,28 +67,27 @@
7167 }
7268
7369 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]);
70
+ for (i = 1; i < isids_len; i++) {
71
+ const char *s = initial_sid_to_string[i];
72
+
73
+ if (s)
74
+ initial_sid_to_string[i] = stoupperx(s);
75
+ }
7676
7777 fprintf(fout, "/* This file is automatically generated. Do not edit. */\n");
7878 fprintf(fout, "#ifndef _SELINUX_FLASK_H_\n#define _SELINUX_FLASK_H_\n\n");
7979
8080 for (i = 0; secclass_map[i].name; i++) {
8181 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++)
84
- fprintf(fout, " ");
85
- fprintf(fout, "%2d\n", i+1);
82
+ fprintf(fout, "#define SECCLASS_%-39s %2d\n", map->name, i+1);
8683 }
8784
8885 fprintf(fout, "\n");
8986
9087 for (i = 1; i < isids_len; i++) {
9188 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++)
94
- fprintf(fout, " ");
95
- fprintf(fout, "%2d\n", i);
89
+ if (s)
90
+ fprintf(fout, "#define SECINITSID_%-39s %2d\n", s, i);
9691 }
9792 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
9893 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
....@@ -100,9 +95,10 @@
10095 fprintf(fout, "\tbool sock = false;\n\n");
10196 fprintf(fout, "\tswitch (kern_tclass) {\n");
10297 for (i = 0; secclass_map[i].name; i++) {
98
+ static char s[] = "SOCKET";
10399 struct security_class_mapping *map = &secclass_map[i];
104
- substr = strstr(map->name, needle);
105
- if (substr && strcmp(substr, needle) == 0)
100
+ int len = strlen(map->name), l = sizeof(s) - 1;
101
+ if (len >= l && memcmp(map->name + len - l, s, l) == 0)
106102 fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
107103 }
108104 fprintf(fout, "\t\tsock = true;\n");
....@@ -128,17 +124,15 @@
128124
129125 for (i = 0; secclass_map[i].name; i++) {
130126 struct security_class_mapping *map = &secclass_map[i];
127
+ int len = strlen(map->name);
131128 for (j = 0; map->perms[j]; j++) {
132129 if (j >= 32) {
133130 fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
134131 map->name, map->perms[j]);
135132 exit(5);
136133 }
137
- fprintf(fout, "#define %s__%s", map->name,
138
- map->perms[j]);
139
- for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
140
- fprintf(fout, " ");
141
- fprintf(fout, "0x%08xU\n", (1<<j));
134
+ fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
135
+ 39-len, map->perms[j], 1U<<j);
142136 }
143137 }
144138