1 |
kumaneko |
111 |
/* |
2 |
|
|
* fs/tomoyo_network.c |
3 |
|
|
* |
4 |
|
|
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
|
|
* |
6 |
kumaneko |
851 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
kumaneko |
111 |
* |
8 |
kumaneko |
851 |
* Version: 1.5.3-pre 2008/01/02 |
9 |
kumaneko |
111 |
* |
10 |
|
|
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
|
|
* See README.ccs for ChangeLog. |
12 |
|
|
* |
13 |
|
|
*/ |
14 |
|
|
/***** TOMOYO Linux start. *****/ |
15 |
|
|
|
16 |
|
|
#include <linux/ccs_common.h> |
17 |
|
|
#include <linux/tomoyo.h> |
18 |
|
|
#include <linux/realpath.h> |
19 |
|
|
#include <net/ip.h> |
20 |
|
|
|
21 |
|
|
/************************* VARIABLES *************************/ |
22 |
|
|
|
23 |
kumaneko |
652 |
extern struct mutex domain_acl_lock; |
24 |
kumaneko |
111 |
|
25 |
|
|
/************************* AUDIT FUNCTIONS *************************/ |
26 |
|
|
|
27 |
kumaneko |
851 |
static int AuditNetworkLog(const bool is_ipv6, const char *operation, const u32 *address, const u16 port, const bool is_granted, const u8 profile, const u8 mode) |
28 |
kumaneko |
111 |
{ |
29 |
|
|
char *buf; |
30 |
|
|
int len = 256; |
31 |
|
|
if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM; |
32 |
kumaneko |
815 |
if ((buf = InitAuditLog(&len, profile, mode)) == NULL) return -ENOMEM; |
33 |
kumaneko |
111 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation); |
34 |
|
|
if (is_ipv6) { |
35 |
kumaneko |
719 |
print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address); |
36 |
kumaneko |
111 |
} else { |
37 |
|
|
u32 ip = *address; |
38 |
|
|
snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip)); |
39 |
|
|
} |
40 |
|
|
snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port); |
41 |
|
|
return WriteAuditLog(buf, is_granted); |
42 |
|
|
} |
43 |
|
|
|
44 |
kumaneko |
719 |
/************************* UTILITY FUNCTIONS *************************/ |
45 |
|
|
|
46 |
|
|
/* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */ |
47 |
|
|
static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr) |
48 |
|
|
{ |
49 |
kumaneko |
853 |
static const u8 block_size = 16; |
50 |
kumaneko |
719 |
struct addr_list { |
51 |
|
|
struct in6_addr addr[block_size]; |
52 |
kumaneko |
731 |
struct list1_head list; |
53 |
kumaneko |
719 |
u32 in_use_count; |
54 |
|
|
}; |
55 |
kumaneko |
731 |
static LIST1_HEAD(address_list); |
56 |
kumaneko |
719 |
struct addr_list *ptr; |
57 |
|
|
static DEFINE_MUTEX(lock); |
58 |
kumaneko |
853 |
u8 i = block_size; |
59 |
kumaneko |
719 |
if (!addr) return NULL; |
60 |
|
|
mutex_lock(&lock); |
61 |
kumaneko |
731 |
list1_for_each_entry(ptr, &address_list, list) { |
62 |
kumaneko |
719 |
for (i = 0; i < ptr->in_use_count; i++) { |
63 |
|
|
if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0) goto ok; |
64 |
|
|
} |
65 |
|
|
if (i < block_size) break; |
66 |
|
|
} |
67 |
kumaneko |
731 |
if (i == block_size) { |
68 |
|
|
ptr = alloc_element(sizeof(*ptr)); |
69 |
|
|
if (!ptr) goto ok; |
70 |
|
|
list1_add_tail_mb(&ptr->list, &address_list); |
71 |
kumaneko |
719 |
i = 0; |
72 |
|
|
} |
73 |
kumaneko |
731 |
ptr->addr[ptr->in_use_count++] = *addr; |
74 |
kumaneko |
719 |
ok: |
75 |
|
|
mutex_unlock(&lock); |
76 |
|
|
return ptr ? &ptr->addr[i] : NULL; |
77 |
|
|
} |
78 |
|
|
|
79 |
kumaneko |
111 |
/************************* ADDRESS GROUP HANDLER *************************/ |
80 |
|
|
|
81 |
kumaneko |
722 |
static LIST1_HEAD(address_group_list); |
82 |
kumaneko |
111 |
|
83 |
kumaneko |
621 |
static int AddAddressGroupEntry(const char *group_name, const bool is_ipv6, const u16 *min_address, const u16 *max_address, const bool is_delete) |
84 |
kumaneko |
111 |
{ |
85 |
kumaneko |
652 |
static DEFINE_MUTEX(lock); |
86 |
kumaneko |
214 |
struct address_group_entry *new_group, *group; |
87 |
|
|
struct address_group_member *new_member, *member; |
88 |
kumaneko |
111 |
const struct path_info *saved_group_name; |
89 |
kumaneko |
719 |
const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL; |
90 |
kumaneko |
111 |
int error = -ENOMEM; |
91 |
kumaneko |
708 |
bool found = 0; |
92 |
kumaneko |
111 |
if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL; |
93 |
|
|
if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM; |
94 |
kumaneko |
719 |
if (is_ipv6) { |
95 |
|
|
if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL |
96 |
kumaneko |
722 |
|| (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM; |
97 |
kumaneko |
719 |
} |
98 |
kumaneko |
652 |
mutex_lock(&lock); |
99 |
kumaneko |
722 |
list1_for_each_entry(group, &address_group_list, list) { |
100 |
kumaneko |
111 |
if (saved_group_name != group->group_name) continue; |
101 |
kumaneko |
722 |
list1_for_each_entry(member, &group->address_group_member_list, list) { |
102 |
kumaneko |
111 |
if (member->is_ipv6 != is_ipv6) continue; |
103 |
|
|
if (is_ipv6) { |
104 |
kumaneko |
719 |
if (member->min.ipv6 != saved_min_address || member->max.ipv6 != saved_max_address) continue; |
105 |
kumaneko |
111 |
} else { |
106 |
|
|
if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue; |
107 |
|
|
} |
108 |
|
|
member->is_deleted = is_delete; |
109 |
|
|
error = 0; |
110 |
|
|
goto out; |
111 |
|
|
} |
112 |
kumaneko |
708 |
found = 1; |
113 |
kumaneko |
111 |
break; |
114 |
|
|
} |
115 |
|
|
if (is_delete) { |
116 |
|
|
error = -ENOENT; |
117 |
|
|
goto out; |
118 |
|
|
} |
119 |
kumaneko |
708 |
if (!found) { |
120 |
kumaneko |
214 |
if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out; |
121 |
kumaneko |
722 |
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
122 |
kumaneko |
111 |
new_group->group_name = saved_group_name; |
123 |
kumaneko |
722 |
list1_add_tail_mb(&new_group->list, &address_group_list); |
124 |
kumaneko |
111 |
group = new_group; |
125 |
|
|
} |
126 |
kumaneko |
214 |
if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out; |
127 |
kumaneko |
111 |
new_member->is_ipv6 = is_ipv6; |
128 |
|
|
if (is_ipv6) { |
129 |
kumaneko |
719 |
new_member->min.ipv6 = saved_min_address; |
130 |
|
|
new_member->max.ipv6 = saved_max_address; |
131 |
kumaneko |
111 |
} else { |
132 |
|
|
new_member->min.ipv4 = * (u32 *) min_address; |
133 |
|
|
new_member->max.ipv4 = * (u32 *) max_address; |
134 |
|
|
} |
135 |
kumaneko |
722 |
list1_add_tail_mb(&new_member->list, &group->address_group_member_list); |
136 |
kumaneko |
111 |
error = 0; |
137 |
|
|
out: |
138 |
kumaneko |
652 |
mutex_unlock(&lock); |
139 |
kumaneko |
111 |
return error; |
140 |
|
|
} |
141 |
|
|
|
142 |
kumaneko |
621 |
int AddAddressGroupPolicy(char *data, const bool is_delete) |
143 |
kumaneko |
111 |
{ |
144 |
kumaneko |
853 |
u8 count; |
145 |
|
|
bool is_ipv6; |
146 |
kumaneko |
111 |
u16 min_address[8], max_address[8]; |
147 |
|
|
char *cp = strchr(data, ' '); |
148 |
|
|
if (!cp) return -EINVAL; |
149 |
|
|
*cp++ = '\0'; |
150 |
|
|
if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
151 |
kumaneko |
719 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
152 |
|
|
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
153 |
|
|
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
154 |
|
|
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
155 |
kumaneko |
853 |
u8 i; |
156 |
kumaneko |
111 |
for (i = 0; i < 8; i++) { |
157 |
|
|
min_address[i] = htons(min_address[i]); |
158 |
|
|
max_address[i] = htons(max_address[i]); |
159 |
|
|
} |
160 |
|
|
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
161 |
|
|
is_ipv6 = 1; |
162 |
|
|
} else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
163 |
kumaneko |
719 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
164 |
|
|
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
165 |
kumaneko |
111 |
u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
166 |
|
|
* (u32 *) min_address = ip; |
167 |
|
|
if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
168 |
|
|
* (u32 *) max_address = ip; |
169 |
|
|
is_ipv6 = 0; |
170 |
|
|
} else { |
171 |
|
|
return -EINVAL; |
172 |
|
|
} |
173 |
|
|
return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete); |
174 |
|
|
} |
175 |
|
|
|
176 |
kumaneko |
214 |
static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name) |
177 |
kumaneko |
111 |
{ |
178 |
kumaneko |
853 |
u8 i; |
179 |
kumaneko |
214 |
struct address_group_entry *group; |
180 |
kumaneko |
111 |
for (i = 0; i <= 1; i++) { |
181 |
kumaneko |
722 |
list1_for_each_entry(group, &address_group_list, list) { |
182 |
kumaneko |
111 |
if (strcmp(group_name, group->group_name->name) == 0) return group; |
183 |
|
|
} |
184 |
|
|
if (i == 0) { |
185 |
|
|
const u16 dummy[2] = { 0, 0 }; |
186 |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 0); |
187 |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 1); |
188 |
|
|
} |
189 |
|
|
} |
190 |
|
|
return NULL; |
191 |
|
|
} |
192 |
|
|
|
193 |
kumaneko |
853 |
static bool AddressMatchesToGroup(const bool is_ipv6, const u32 *address, const struct address_group_entry *group) |
194 |
kumaneko |
111 |
{ |
195 |
kumaneko |
214 |
struct address_group_member *member; |
196 |
kumaneko |
111 |
const u32 ip = ntohl(*address); |
197 |
kumaneko |
722 |
list1_for_each_entry(member, &group->address_group_member_list, list) { |
198 |
kumaneko |
111 |
if (member->is_deleted) continue; |
199 |
|
|
if (member->is_ipv6) { |
200 |
kumaneko |
141 |
if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1; |
201 |
kumaneko |
111 |
} else { |
202 |
kumaneko |
141 |
if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1; |
203 |
kumaneko |
111 |
} |
204 |
|
|
} |
205 |
|
|
return 0; |
206 |
|
|
} |
207 |
|
|
|
208 |
kumaneko |
214 |
int ReadAddressGroupPolicy(struct io_buffer *head) |
209 |
kumaneko |
111 |
{ |
210 |
kumaneko |
722 |
struct list1_head *gpos; |
211 |
|
|
struct list1_head *mpos; |
212 |
|
|
list1_for_each_cookie(gpos, head->read_var1, &address_group_list) { |
213 |
kumaneko |
708 |
struct address_group_entry *group; |
214 |
kumaneko |
722 |
group = list1_entry(gpos, struct address_group_entry, list); |
215 |
|
|
list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) { |
216 |
kumaneko |
708 |
char buf[128]; |
217 |
|
|
struct address_group_member *member; |
218 |
kumaneko |
722 |
member = list1_entry(mpos, struct address_group_member, list); |
219 |
kumaneko |
708 |
if (member->is_deleted) continue; |
220 |
|
|
if (member->is_ipv6) { |
221 |
kumaneko |
719 |
const struct in6_addr *min_address = member->min.ipv6, *max_address = member->max.ipv6; |
222 |
kumaneko |
708 |
print_ipv6(buf, sizeof(buf), min_address); |
223 |
kumaneko |
719 |
if (min_address != max_address) { |
224 |
kumaneko |
708 |
char *cp = strchr(buf, '\0'); |
225 |
|
|
*cp++ = '-'; |
226 |
|
|
print_ipv6(cp, sizeof(buf) - strlen(buf), max_address); |
227 |
kumaneko |
111 |
} |
228 |
kumaneko |
708 |
} else { |
229 |
|
|
const u32 min_address = member->min.ipv4, max_address = member->max.ipv4; |
230 |
|
|
memset(buf, 0, sizeof(buf)); |
231 |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address)); |
232 |
|
|
if (min_address != max_address) { |
233 |
|
|
const int len = strlen(buf); |
234 |
|
|
snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address)); |
235 |
|
|
} |
236 |
kumaneko |
111 |
} |
237 |
kumaneko |
708 |
if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) return -ENOMEM; |
238 |
kumaneko |
111 |
} |
239 |
|
|
} |
240 |
kumaneko |
708 |
return 0; |
241 |
kumaneko |
111 |
} |
242 |
|
|
|
243 |
|
|
/************************* NETWORK NETWORK ACL HANDLER *************************/ |
244 |
|
|
|
245 |
kumaneko |
719 |
#if !defined(NIP6) |
246 |
|
|
#define NIP6(addr) \ |
247 |
|
|
ntohs((addr).s6_addr16[0]), \ |
248 |
|
|
ntohs((addr).s6_addr16[1]), \ |
249 |
|
|
ntohs((addr).s6_addr16[2]), \ |
250 |
|
|
ntohs((addr).s6_addr16[3]), \ |
251 |
|
|
ntohs((addr).s6_addr16[4]), \ |
252 |
|
|
ntohs((addr).s6_addr16[5]), \ |
253 |
|
|
ntohs((addr).s6_addr16[6]), \ |
254 |
|
|
ntohs((addr).s6_addr16[7]) |
255 |
|
|
#endif |
256 |
|
|
|
257 |
|
|
char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip) |
258 |
kumaneko |
111 |
{ |
259 |
|
|
memset(buffer, 0, buffer_len); |
260 |
kumaneko |
719 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
261 |
kumaneko |
111 |
return buffer; |
262 |
|
|
} |
263 |
|
|
|
264 |
kumaneko |
851 |
const char *net_operation2keyword(const u8 operation) |
265 |
kumaneko |
111 |
{ |
266 |
|
|
const char *keyword = "unknown"; |
267 |
|
|
switch (operation) { |
268 |
|
|
case NETWORK_ACL_UDP_BIND: |
269 |
|
|
keyword = "UDP bind"; |
270 |
|
|
break; |
271 |
|
|
case NETWORK_ACL_UDP_CONNECT: |
272 |
|
|
keyword = "UDP connect"; |
273 |
|
|
break; |
274 |
|
|
case NETWORK_ACL_TCP_BIND: |
275 |
|
|
keyword = "TCP bind"; |
276 |
|
|
break; |
277 |
|
|
case NETWORK_ACL_TCP_LISTEN: |
278 |
|
|
keyword = "TCP listen"; |
279 |
|
|
break; |
280 |
|
|
case NETWORK_ACL_TCP_CONNECT: |
281 |
|
|
keyword = "TCP connect"; |
282 |
|
|
break; |
283 |
|
|
case NETWORK_ACL_TCP_ACCEPT: |
284 |
|
|
keyword = "TCP accept"; |
285 |
|
|
break; |
286 |
|
|
case NETWORK_ACL_RAW_BIND: |
287 |
|
|
keyword = "RAW bind"; |
288 |
|
|
break; |
289 |
|
|
case NETWORK_ACL_RAW_CONNECT: |
290 |
|
|
keyword = "RAW connect"; |
291 |
|
|
break; |
292 |
|
|
} |
293 |
|
|
return keyword; |
294 |
|
|
} |
295 |
|
|
|
296 |
kumaneko |
621 |
static int AddNetworkEntry(const u8 operation, const u8 record_type, const struct address_group_entry *group, const u32 *min_address, const u32 *max_address, const u16 min_port, const u16 max_port, struct domain_info *domain, const struct condition_list *condition, const bool is_delete) |
297 |
kumaneko |
111 |
{ |
298 |
|
|
struct acl_info *ptr; |
299 |
kumaneko |
708 |
struct ip_network_acl_record *acl; |
300 |
kumaneko |
111 |
int error = -ENOMEM; |
301 |
|
|
const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/ |
302 |
kumaneko |
719 |
const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL; |
303 |
kumaneko |
111 |
if (!domain) return -EINVAL; |
304 |
kumaneko |
719 |
if (record_type == IP_RECORD_TYPE_IPv6) { |
305 |
|
|
if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL |
306 |
kumaneko |
722 |
|| (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM; |
307 |
kumaneko |
719 |
} |
308 |
kumaneko |
652 |
mutex_lock(&domain_acl_lock); |
309 |
kumaneko |
514 |
if (!is_delete) { |
310 |
kumaneko |
722 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
311 |
kumaneko |
712 |
acl = container_of(ptr, struct ip_network_acl_record, head); |
312 |
kumaneko |
708 |
if (ptr->type == TYPE_IP_NETWORK_ACL && acl->operation_type == operation && acl->record_type == record_type && ptr->cond == condition && acl->min_port == min_port && max_port == acl->max_port) { |
313 |
kumaneko |
111 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
314 |
kumaneko |
708 |
if (acl->u.group == group) { |
315 |
kumaneko |
111 |
ptr->is_deleted = 0; |
316 |
|
|
/* Found. Nothing to do. */ |
317 |
|
|
error = 0; |
318 |
kumaneko |
708 |
goto out; |
319 |
kumaneko |
111 |
} |
320 |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
321 |
kumaneko |
708 |
if (acl->u.ipv4.min == min_ip && max_ip == acl->u.ipv4.max) { |
322 |
kumaneko |
111 |
ptr->is_deleted = 0; |
323 |
|
|
/* Found. Nothing to do. */ |
324 |
|
|
error = 0; |
325 |
kumaneko |
708 |
goto out; |
326 |
kumaneko |
111 |
} |
327 |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
328 |
kumaneko |
719 |
if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) { |
329 |
kumaneko |
111 |
ptr->is_deleted = 0; |
330 |
|
|
/* Found. Nothing to do. */ |
331 |
|
|
error = 0; |
332 |
kumaneko |
708 |
goto out; |
333 |
kumaneko |
111 |
} |
334 |
|
|
} |
335 |
|
|
} |
336 |
|
|
} |
337 |
kumaneko |
708 |
/* Not found. Append it to the tail. */ |
338 |
|
|
if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out; |
339 |
|
|
acl->head.type = TYPE_IP_NETWORK_ACL; |
340 |
|
|
acl->operation_type = operation; |
341 |
|
|
acl->record_type = record_type; |
342 |
|
|
acl->head.cond = condition; |
343 |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
344 |
|
|
acl->u.group = group; |
345 |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
346 |
|
|
acl->u.ipv4.min = min_ip; |
347 |
|
|
acl->u.ipv4.max = max_ip; |
348 |
|
|
} else { |
349 |
kumaneko |
719 |
acl->u.ipv6.min = saved_min_address; |
350 |
|
|
acl->u.ipv6.max = saved_max_address; |
351 |
kumaneko |
708 |
} |
352 |
|
|
acl->min_port = min_port; |
353 |
|
|
acl->max_port = max_port; |
354 |
|
|
error = AddDomainACL(domain, &acl->head); |
355 |
kumaneko |
111 |
} else { |
356 |
|
|
error = -ENOENT; |
357 |
kumaneko |
722 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
358 |
kumaneko |
712 |
acl = container_of(ptr, struct ip_network_acl_record, head); |
359 |
kumaneko |
708 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || acl->record_type != record_type || ptr->cond != condition || acl->min_port != min_port || acl->max_port != max_port) continue; |
360 |
kumaneko |
111 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
361 |
kumaneko |
708 |
if (acl->u.group != group) continue; |
362 |
kumaneko |
111 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
363 |
kumaneko |
708 |
if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue; |
364 |
kumaneko |
111 |
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
365 |
kumaneko |
719 |
if (acl->u.ipv6.min != saved_min_address || saved_max_address != acl->u.ipv6.max) continue; |
366 |
kumaneko |
111 |
} |
367 |
|
|
error = DelDomainACL(ptr); |
368 |
|
|
break; |
369 |
|
|
} |
370 |
|
|
} |
371 |
kumaneko |
708 |
out: ; |
372 |
kumaneko |
652 |
mutex_unlock(&domain_acl_lock); |
373 |
kumaneko |
111 |
return error; |
374 |
|
|
} |
375 |
|
|
|
376 |
kumaneko |
851 |
static int CheckNetworkEntry(const bool is_ipv6, const u8 operation, const u32 *address, const u16 port) |
377 |
kumaneko |
111 |
{ |
378 |
|
|
struct domain_info * const domain = current->domain_info; |
379 |
|
|
struct acl_info *ptr; |
380 |
kumaneko |
851 |
const char *keyword = net_operation2keyword(operation); |
381 |
kumaneko |
815 |
const u8 profile = current->domain_info->profile; |
382 |
kumaneko |
851 |
const u8 mode = CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK); |
383 |
kumaneko |
815 |
const bool is_enforce = (mode == 3); |
384 |
kumaneko |
111 |
const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/ |
385 |
kumaneko |
708 |
bool found = 0; |
386 |
kumaneko |
815 |
if (!mode) return 0; |
387 |
kumaneko |
722 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
388 |
kumaneko |
708 |
struct ip_network_acl_record *acl; |
389 |
kumaneko |
712 |
acl = container_of(ptr, struct ip_network_acl_record, head); |
390 |
kumaneko |
708 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || port < acl->min_port || acl->max_port < port || CheckCondition(ptr->cond, NULL)) continue; |
391 |
|
|
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
392 |
|
|
if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue; |
393 |
|
|
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
394 |
|
|
if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue; |
395 |
kumaneko |
111 |
} else { |
396 |
kumaneko |
708 |
if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue; |
397 |
kumaneko |
111 |
} |
398 |
kumaneko |
708 |
found = 1; |
399 |
|
|
break; |
400 |
kumaneko |
111 |
} |
401 |
kumaneko |
815 |
AuditNetworkLog(is_ipv6, keyword, address, port, found, profile, mode); |
402 |
kumaneko |
708 |
if (found) return 0; |
403 |
kumaneko |
111 |
if (TomoyoVerboseMode()) { |
404 |
|
|
if (is_ipv6) { |
405 |
|
|
char buf[64]; |
406 |
kumaneko |
719 |
print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address); |
407 |
kumaneko |
111 |
printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain)); |
408 |
|
|
} else { |
409 |
|
|
printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain)); |
410 |
|
|
} |
411 |
|
|
} |
412 |
|
|
if (is_enforce) { |
413 |
|
|
if (is_ipv6) { |
414 |
|
|
char buf[64]; |
415 |
kumaneko |
719 |
print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address); |
416 |
kumaneko |
111 |
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port); |
417 |
|
|
} |
418 |
|
|
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port); |
419 |
|
|
} |
420 |
kumaneko |
815 |
if (mode == 1 && CheckDomainQuota(domain)) AddNetworkEntry(operation, is_ipv6 ? IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4, NULL, address, address, port, port, domain, NULL, 0); |
421 |
kumaneko |
111 |
return 0; |
422 |
|
|
} |
423 |
|
|
|
424 |
kumaneko |
621 |
int AddNetworkPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete) |
425 |
kumaneko |
111 |
{ |
426 |
|
|
u8 sock_type, operation, record_type; |
427 |
|
|
u16 min_address[8], max_address[8]; |
428 |
|
|
struct address_group_entry *group = NULL; |
429 |
|
|
u16 min_port, max_port; |
430 |
kumaneko |
853 |
u8 count; |
431 |
kumaneko |
111 |
char *cp1 = NULL, *cp2 = NULL; |
432 |
|
|
if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++; |
433 |
|
|
if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM; |
434 |
|
|
else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM; |
435 |
|
|
else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW; |
436 |
|
|
else goto out; |
437 |
|
|
if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++; |
438 |
|
|
if (strncmp(cp1, "bind ", 5) == 0) { |
439 |
|
|
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND; |
440 |
|
|
} else if (strncmp(cp1, "connect ", 8) == 0) { |
441 |
|
|
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
442 |
|
|
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) { |
443 |
|
|
operation = NETWORK_ACL_TCP_LISTEN; |
444 |
|
|
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) { |
445 |
|
|
operation = NETWORK_ACL_TCP_ACCEPT; |
446 |
|
|
} else { |
447 |
|
|
goto out; |
448 |
|
|
} |
449 |
|
|
if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0'; |
450 |
|
|
if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
451 |
kumaneko |
719 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
452 |
|
|
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
453 |
|
|
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
454 |
|
|
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
455 |
kumaneko |
853 |
u8 i; |
456 |
kumaneko |
111 |
for (i = 0; i < 8; i++) { |
457 |
|
|
min_address[i] = htons(min_address[i]); |
458 |
|
|
max_address[i] = htons(max_address[i]); |
459 |
|
|
} |
460 |
|
|
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
461 |
|
|
record_type = IP_RECORD_TYPE_IPv6; |
462 |
|
|
} else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
463 |
kumaneko |
719 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
464 |
|
|
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
465 |
kumaneko |
111 |
u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
466 |
|
|
* (u32 *) min_address = ip; |
467 |
|
|
if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
468 |
|
|
* (u32 *) max_address = ip; |
469 |
|
|
record_type = IP_RECORD_TYPE_IPv4; |
470 |
|
|
} else if (*cp2 == '@') { |
471 |
|
|
if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM; |
472 |
|
|
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
473 |
|
|
} else { |
474 |
|
|
goto out; |
475 |
|
|
} |
476 |
|
|
if (strchr(cp1, ' ')) goto out; |
477 |
|
|
if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) { |
478 |
|
|
if (count == 1) max_port = min_port; |
479 |
kumaneko |
514 |
return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, condition, is_delete); |
480 |
kumaneko |
111 |
} |
481 |
|
|
out: ; |
482 |
|
|
return -EINVAL; |
483 |
|
|
} |
484 |
|
|
|
485 |
kumaneko |
652 |
int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port) |
486 |
kumaneko |
111 |
{ |
487 |
|
|
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port)); |
488 |
|
|
} |
489 |
|
|
|
490 |
kumaneko |
652 |
int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
491 |
kumaneko |
111 |
{ |
492 |
|
|
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT), (const u32 *) address, ntohs(port)); |
493 |
|
|
} |
494 |
|
|
|
495 |
kumaneko |
652 |
int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
496 |
kumaneko |
111 |
{ |
497 |
|
|
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND), (const u32 *) address, ntohs(port)); |
498 |
|
|
} |
499 |
|
|
|
500 |
kumaneko |
652 |
int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port) |
501 |
kumaneko |
111 |
{ |
502 |
kumaneko |
708 |
int retval; |
503 |
|
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
504 |
|
|
retval = CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port)); |
505 |
|
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
506 |
|
|
return retval; |
507 |
kumaneko |
111 |
} |
508 |
|
|
|
509 |
kumaneko |
652 |
int CheckNetworkSendMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
510 |
kumaneko |
111 |
{ |
511 |
|
|
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
512 |
|
|
} |
513 |
|
|
|
514 |
kumaneko |
652 |
int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
515 |
kumaneko |
111 |
{ |
516 |
kumaneko |
708 |
int retval; |
517 |
|
|
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
518 |
|
|
retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
519 |
|
|
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
520 |
|
|
return retval; |
521 |
kumaneko |
111 |
} |
522 |
|
|
|
523 |
|
|
/***** TOMOYO Linux end. *****/ |