-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradm_net.c
293 lines (252 loc) · 7.24 KB
/
gradm_net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (C) 2002-2014 Bradley Spengler, Open Source Security, Inc.
* http://www.grsecurity.net [email protected]
*
* This file is part of gradm.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gradm.h"
struct family_set sock_families[] = {
{ "unspec", AF_UNSPEC },
{ "unix", AF_UNIX },
{ "local", AF_LOCAL },
{ "inet", AF_INET },
{ "ipv4", AF_INET },
{ "ax25", AF_AX25 },
{ "ipx", AF_IPX },
{ "appletalk", AF_APPLETALK },
{ "netrom", AF_NETROM },
{ "bridge", AF_BRIDGE },
{ "atmpvc", AF_ATMPVC },
{ "x25", AF_X25 },
{ "ipv6", AF_INET6 },
{ "inet6", AF_INET6 },
{ "rose", AF_ROSE },
{ "decnet", AF_DECnet },
{ "netbeui", AF_NETBEUI },
{ "security", AF_SECURITY },
{ "key", AF_KEY },
{ "netlink", AF_NETLINK },
{ "route", AF_ROUTE },
{ "packet", AF_PACKET },
{ "ash", AF_ASH },
{ "econet", AF_ECONET },
{ "atmsvc", AF_ATMSVC },
{ "rds", AF_RDS },
{ "sna", AF_SNA },
{ "irda", AF_IRDA },
{ "ppox", AF_PPOX },
{ "wanpipe", AF_WANPIPE },
{ "llc", AF_LLC },
{ "tipc", AF_TIPC },
{ "bluetooth", AF_BLUETOOTH },
{ "iucv", AF_IUCV },
{ "rxrpc", AF_RXRPC },
{ "isdn", AF_ISDN },
{ "phonet", AF_PHONET },
{ "ieee802154", AF_IEEE802154 },
{ "caif", AF_CAIF },
{ "alg", AF_ALG },
{ "nfc", AF_NFC },
{ "vsock", AF_VSOCK },
{ "all", -1 }
};
const char *
get_sock_family_from_val(int val)
{
int i;
for (i = 0; i < SIZE(sock_families); i++) {
if (sock_families[i].family_val == val)
return sock_families[i].family_name;
}
fprintf(stderr, "Invalid socket family detected.\n");
exit(EXIT_FAILURE);
return NULL;
}
void
add_sock_family(struct proc_acl *subject, const char *family)
{
int i;
if (!strcmp(family, "all")) {
for (i = 0; i < SIZE(subject->sock_families); i++) {
subject->sock_families[i] = -1;
}
return;
}
for (i = 0; i < SIZE(sock_families); i++) {
if (strcmp(sock_families[i].family_name, family))
continue;
else
break;
}
if (i == SIZE(sock_families)) {
fprintf(stderr, "Invalid socket family %s on line %lu of %s.\n",
family, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
subject->sock_families[sock_families[i].family_val / 32] |=
(1U << (sock_families[i].family_val % 32));
return;
}
void
add_role_allowed_host(struct role_acl *role, const char *host, u_int32_t netmask)
{
struct hostent *he;
char **p;
he = gethostbyname(host);
if (he == NULL) {
fprintf(stderr, "Error resolving hostname %s, on line %lu of %s\n", host, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (he->h_addrtype != AF_INET) {
fprintf(stderr, "Hostname %s on line %lu of %s does not resolve to an IPv4 address.\n", host, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
p = he->h_addr_list;
while (*p) {
add_role_allowed_ip(role, (u_int32_t)**p, netmask);
p++;
}
return;
}
void
add_role_allowed_ip(struct role_acl *role, u_int32_t addr, u_int32_t netmask)
{
struct role_allowed_ip **roleipp;
struct role_allowed_ip *roleip;
num_pointers++;
roleip =
(struct role_allowed_ip *) calloc(1,
sizeof (struct role_allowed_ip));
if (!roleip)
failure("calloc");
roleipp = &(role->allowed_ips);
if (*roleipp)
(*roleipp)->next = roleip;
roleip->prev = *roleipp;
roleip->addr = addr;
roleip->netmask = netmask;
*roleipp = roleip;
return;
}
void add_host_acl(struct proc_acl *subject, u_int8_t mode, const char *host, struct ip_acl *acl_tmp)
{
struct hostent *he;
char **p;
he = gethostbyname(host);
if (he == NULL) {
fprintf(stderr, "Error resolving hostname %s, on line %lu of %s\n", host, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
if (he->h_addrtype != AF_INET) {
fprintf(stderr, "Hostname %s on line %lu of %s does not resolve to an IPv4 address.\n", host, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
p = he->h_addr_list;
while (*p) {
memcpy(&(acl_tmp->addr), *p, sizeof(acl_tmp->addr));
add_ip_acl(subject, mode, acl_tmp);
p++;
}
return;
}
void
add_ip_acl(struct proc_acl *subject, u_int8_t mode, struct ip_acl *acl_tmp)
{
struct ip_acl *p;
int i;
if (!subject) {
fprintf(stderr, "Error on line %lu of %s.\n Definition "
"of an IP policy without a subject definition.\n"
"The RBAC system will not be allowed to be "
"enabled until this problem is fixed.\n",
lineno, current_acl_file);
exit(EXIT_FAILURE);
}
/* add one for the pointer to array of pointers */
if (subject->ips == NULL)
num_pointers++;
num_pointers++;
subject->ip_num++;
if (subject->ips == NULL)
subject->ips = (struct ip_acl **)gr_alloc(subject->ip_num * sizeof(struct ip_acl *));
else
subject->ips = (struct ip_acl **)gr_realloc(subject->ips, subject->ip_num * sizeof(struct ip_acl *));
p = (struct ip_acl *)gr_alloc(sizeof (struct ip_acl));
*(subject->ips + subject->ip_num - 1) = p;
p->mode = mode;
if (acl_tmp->iface != NULL)
num_pointers++;
p->iface = acl_tmp->iface;
p->addr = acl_tmp->addr;
p->netmask = acl_tmp->netmask;
p->low = acl_tmp->low;
p->high = acl_tmp->high;
memcpy(p->proto, acl_tmp->proto, sizeof (acl_tmp->proto));
p->type = acl_tmp->type;
for (i = 0; i < 8; i++)
subject->ip_proto[i] |= p->proto[i];
subject->ip_type |= p->type;
return;
}
u_int32_t
get_ip(char *ip)
{
struct in_addr address;
if (!inet_aton(ip, &address)) {
fprintf(stderr, "Invalid IP on line %lu of %s.\n", lineno,
current_acl_file);
exit(EXIT_FAILURE);
}
return address.s_addr;
}
void
conv_name_to_type(struct ip_acl *ip, const char *name)
{
struct protoent *proto;
unsigned short i;
if (!strcmp(name, "raw_proto"))
ip->proto[IPPROTO_RAW / 32] |= (1U << (IPPROTO_RAW % 32));
else if (!strcmp(name, "raw_sock"))
ip->type |= (1U << SOCK_RAW);
else if (!strcmp(name, "any_sock")) {
ip->type = ~0;
ip->type &= ~(1U << 0); // there is no sock type 0
} else if (!strcmp(name, "any_proto")) {
for (i = 0; i < 8; i++)
ip->proto[i] = ~0;
} else if (!strcmp(name, "stream"))
ip->type |= (1U << SOCK_STREAM);
else if (!strcmp(name, "dgram"))
ip->type |= (1U << SOCK_DGRAM);
else if (!strcmp(name, "rdm"))
ip->type |= (1U << SOCK_RDM);
else if (!strcmp(name, "tcp")) { // silly protocol 0
ip->proto[IPPROTO_IP / 32] |= (1U << (IPPROTO_IP % 32));
ip->proto[IPPROTO_TCP / 32] |= (1U << (IPPROTO_TCP % 32));
} else if (!strcmp(name, "udp")) { // silly protocol 0
ip->proto[IPPROTO_IP / 32] |= (1U << (IPPROTO_IP % 32));
ip->proto[IPPROTO_UDP / 32] |= (1U << (IPPROTO_UDP % 32));
} else if (!strncmp(name, "proto:", strlen("proto:"))) {
int pro = atoi(name+strlen("proto:"));
ip->proto[pro / 32] |= 1U << (pro % 32);
} else if ((proto = getprotobyname(name)))
ip->proto[proto->p_proto / 32] |= (1U << (proto->p_proto % 32));
else {
fprintf(stderr, "Invalid type/protocol: %s\n", name);
exit(EXIT_FAILURE);
}
return;
}