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