blob: 1ceedea847ddfec8ff856fb3a8a2110cd00343c3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Paul Moorebfc5e3a2016-12-21 10:39:25 -05002
3/* NOTE: we really do want to use the kernel headers here */
4#define __EXPORTED_HEADERS__
5
Stephen Smalley8753f6b2009-09-30 13:41:02 -04006#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <string.h>
10#include <errno.h>
11#include <ctype.h>
Nicolas Ioossc017c712017-03-05 15:01:52 +010012#include <sys/socket.h>
Stephen Smalley8753f6b2009-09-30 13:41:02 -040013
14struct security_class_mapping {
15 const char *name;
16 const char *perms[sizeof(unsigned) * 8 + 1];
17};
18
19#include "classmap.h"
20#include "initial_sid_to_string.h"
21
Stephen Smalley8753f6b2009-09-30 13:41:02 -040022const char *progname;
23
Alan Cox821d35a2009-11-18 14:39:51 +000024static void usage(void)
Stephen Smalley8753f6b2009-09-30 13:41:02 -040025{
26 printf("usage: %s flask.h av_permissions.h\n", progname);
27 exit(1);
28}
29
Alan Cox821d35a2009-11-18 14:39:51 +000030static char *stoupperx(const char *s)
Stephen Smalley8753f6b2009-09-30 13:41:02 -040031{
32 char *s2 = strdup(s);
33 char *p;
34
35 if (!s2) {
36 fprintf(stderr, "%s: out of memory\n", progname);
37 exit(3);
38 }
39
40 for (p = s2; *p; p++)
41 *p = toupper(*p);
42 return s2;
43}
44
45int main(int argc, char *argv[])
46{
Al Viroa40612e2018-12-10 03:40:11 -050047 int i, j;
Stephen Smalley8753f6b2009-09-30 13:41:02 -040048 int isids_len;
49 FILE *fout;
50
51 progname = argv[0];
52
53 if (argc < 3)
54 usage();
55
56 fout = fopen(argv[1], "w");
57 if (!fout) {
58 fprintf(stderr, "Could not open %s for writing: %s\n",
59 argv[1], strerror(errno));
60 exit(2);
61 }
62
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]);
68 }
69
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]);
73
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");
76
77 for (i = 0; secclass_map[i].name; i++) {
78 struct security_class_mapping *map = &secclass_map[i];
Al Viroa40612e2018-12-10 03:40:11 -050079 fprintf(fout, "#define SECCLASS_%-39s %2d\n", map->name, i+1);
Stephen Smalley8753f6b2009-09-30 13:41:02 -040080 }
81
82 fprintf(fout, "\n");
83
84 for (i = 1; i < isids_len; i++) {
James Morris310de042010-03-16 08:47:36 +110085 const char *s = initial_sid_to_string[i];
Al Viroa40612e2018-12-10 03:40:11 -050086 fprintf(fout, "#define SECINITSID_%-39s %2d\n", s, i);
Stephen Smalley8753f6b2009-09-30 13:41:02 -040087 }
88 fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
Harry Ciao4bc6c2d2011-03-02 13:46:08 +080089 fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
90 fprintf(fout, "{\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++) {
Al Viroa40612e2018-12-10 03:40:11 -050094 static char s[] = "SOCKET";
Harry Ciao4bc6c2d2011-03-02 13:46:08 +080095 struct security_class_mapping *map = &secclass_map[i];
Al Viroa40612e2018-12-10 03:40:11 -050096 int len = strlen(map->name), l = sizeof(s) - 1;
97 if (len >= l && memcmp(map->name + len - l, s, l) == 0)
Harry Ciao4bc6c2d2011-03-02 13:46:08 +080098 fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
99 }
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");
107
Stephen Smalley8753f6b2009-09-30 13:41:02 -0400108 fprintf(fout, "\n#endif\n");
109 fclose(fout);
110
111 fout = fopen(argv[2], "w");
112 if (!fout) {
113 fprintf(stderr, "Could not open %s for writing: %s\n",
114 argv[2], strerror(errno));
115 exit(4);
116 }
117
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");
120
121 for (i = 0; secclass_map[i].name; i++) {
122 struct security_class_mapping *map = &secclass_map[i];
Al Viroa40612e2018-12-10 03:40:11 -0500123 int len = strlen(map->name);
Stephen Smalley8753f6b2009-09-30 13:41:02 -0400124 for (j = 0; map->perms[j]; j++) {
Stephen Smalley20a8d622017-07-25 12:14:12 -0400125 if (j >= 32) {
126 fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
127 map->name, map->perms[j]);
128 exit(5);
129 }
Al Viroa40612e2018-12-10 03:40:11 -0500130 fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
131 39-len, map->perms[j], 1U<<j);
Stephen Smalley8753f6b2009-09-30 13:41:02 -0400132 }
133 }
134
135 fprintf(fout, "\n#endif\n");
136 fclose(fout);
137 exit(0);
138}