オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

Contents of /branches/ccs-patch/fs/tomoyo_network.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1658 - (show annotations) (download) (as text)
Tue Oct 7 04:02:50 2008 UTC (15 years, 8 months ago) by kumaneko
Original Path: trunk/1.6.x/ccs-patch/fs/tomoyo_network.c
File MIME type: text/x-csrc
File size: 25118 byte(s)


1 /*
2 * fs/tomoyo_network.c
3 *
4 * Implementation of the Domain-Based Mandatory Access Control.
5 *
6 * Copyright (C) 2005-2008 NTT DATA CORPORATION
7 *
8 * Version: 1.6.5-pre 2008/10/07
9 *
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
15 #include <linux/ccs_common.h>
16 #include <linux/tomoyo.h>
17 #include <linux/realpath.h>
18 #include <linux/net.h>
19 #include <linux/inet.h>
20 #include <linux/in.h>
21 #include <linux/in6.h>
22
23 /**
24 * audit_network_log - Audit network log.
25 *
26 * @r: Pointer to "struct ccs_request_info".
27 * @operation: The name of operation.
28 * @address: An IPv4 or IPv6 address.
29 * @port: Port number.
30 * @is_granted: True if this is a granted log.
31 *
32 * Returns 0 on success, negative value otherwise.
33 */
34 static int audit_network_log(struct ccs_request_info *r, const char *operation,
35 const char *address, const u16 port,
36 const bool is_granted)
37 {
38 return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK
39 "%s %s %u\n", operation, address, port);
40 }
41
42 /**
43 * save_ipv6_address - Keep the given IPv6 address on the RAM.
44 *
45 * @addr: Pointer to "struct in6_addr".
46 *
47 * Returns pointer to "struct in6_addr" on success, NULL otherwise.
48 *
49 * The RAM is shared, so NEVER try to modify or kfree() the returned address.
50 */
51 static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr)
52 {
53 static const u8 block_size = 16;
54 struct addr_list {
55 /* Workaround for gcc 4.3's bug. */
56 struct in6_addr addr[16]; /* = block_size */
57 struct list1_head list;
58 u32 in_use_count;
59 };
60 static LIST1_HEAD(address_list);
61 struct addr_list *ptr;
62 static DEFINE_MUTEX(lock);
63 u8 i = block_size;
64 if (!addr)
65 return NULL;
66 mutex_lock(&lock);
67 list1_for_each_entry(ptr, &address_list, list) {
68 for (i = 0; i < ptr->in_use_count; i++) {
69 if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))
70 goto ok;
71 }
72 if (i < block_size)
73 break;
74 }
75 if (i == block_size) {
76 ptr = ccs_alloc_element(sizeof(*ptr));
77 if (!ptr)
78 goto ok;
79 list1_add_tail_mb(&ptr->list, &address_list);
80 i = 0;
81 }
82 ptr->addr[ptr->in_use_count++] = *addr;
83 ok:
84 mutex_unlock(&lock);
85 return ptr ? &ptr->addr[i] : NULL;
86 }
87
88 /* The list for "struct address_group_entry". */
89 static LIST1_HEAD(address_group_list);
90
91 /**
92 * update_address_group_entry - Update "struct address_group_entry" list.
93 *
94 * @group_name: The name of address group.
95 * @is_ipv6: True if @min_address and @max_address are IPv6 addresses.
96 * @min_address: Start of IPv4 or IPv6 address range.
97 * @max_address: End of IPv4 or IPv6 address range.
98 * @is_delete: True if it is a delete request.
99 *
100 * Returns 0 on success, negative value otherwise.
101 */
102 static int update_address_group_entry(const char *group_name,
103 const bool is_ipv6,
104 const u16 *min_address,
105 const u16 *max_address,
106 const bool is_delete)
107 {
108 static DEFINE_MUTEX(lock);
109 struct address_group_entry *new_group;
110 struct address_group_entry *group;
111 struct address_group_member *new_member;
112 struct address_group_member *member;
113 const struct path_info *saved_group_name;
114 const struct in6_addr *saved_min_address = NULL;
115 const struct in6_addr *saved_max_address = NULL;
116 int error = -ENOMEM;
117 bool found = false;
118 if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||
119 !group_name[0])
120 return -EINVAL;
121 saved_group_name = ccs_save_name(group_name);
122 if (!saved_group_name)
123 return -ENOMEM;
124 if (!is_ipv6)
125 goto not_ipv6;
126 saved_min_address
127 = save_ipv6_address((struct in6_addr *) min_address);
128 saved_max_address
129 = save_ipv6_address((struct in6_addr *) max_address);
130 if (!saved_min_address || !saved_max_address)
131 return -ENOMEM;
132 not_ipv6:
133 mutex_lock(&lock);
134 list1_for_each_entry(group, &address_group_list, list) {
135 if (saved_group_name != group->group_name)
136 continue;
137 list1_for_each_entry(member, &group->address_group_member_list,
138 list) {
139 if (member->is_ipv6 != is_ipv6)
140 continue;
141 if (is_ipv6) {
142 if (member->min.ipv6 != saved_min_address ||
143 member->max.ipv6 != saved_max_address)
144 continue;
145 } else {
146 if (member->min.ipv4 != *(u32 *) min_address ||
147 member->max.ipv4 != *(u32 *) max_address)
148 continue;
149 }
150 member->is_deleted = is_delete;
151 error = 0;
152 goto out;
153 }
154 found = true;
155 break;
156 }
157 if (is_delete) {
158 error = -ENOENT;
159 goto out;
160 }
161 if (!found) {
162 new_group = ccs_alloc_element(sizeof(*new_group));
163 if (!new_group)
164 goto out;
165 INIT_LIST1_HEAD(&new_group->address_group_member_list);
166 new_group->group_name = saved_group_name;
167 list1_add_tail_mb(&new_group->list, &address_group_list);
168 group = new_group;
169 }
170 new_member = ccs_alloc_element(sizeof(*new_member));
171 if (!new_member)
172 goto out;
173 new_member->is_ipv6 = is_ipv6;
174 if (is_ipv6) {
175 new_member->min.ipv6 = saved_min_address;
176 new_member->max.ipv6 = saved_max_address;
177 } else {
178 new_member->min.ipv4 = *(u32 *) min_address;
179 new_member->max.ipv4 = *(u32 *) max_address;
180 }
181 list1_add_tail_mb(&new_member->list, &group->address_group_member_list);
182 error = 0;
183 out:
184 mutex_unlock(&lock);
185 ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);
186 return error;
187 }
188
189 /**
190 * parse_ip_address - Parse an IP address.
191 *
192 * @address: String to parse.
193 * @min: Pointer to store min address.
194 * @max: Pointer to store max address.
195 *
196 * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.
197 */
198 static int parse_ip_address(char *address, u16 *min, u16 *max)
199 {
200 int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
201 "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
202 &min[0], &min[1], &min[2], &min[3],
203 &min[4], &min[5], &min[6], &min[7],
204 &max[0], &max[1], &max[2], &max[3],
205 &max[4], &max[5], &max[6], &max[7]);
206 if (count == 8 || count == 16) {
207 u8 i;
208 if (count == 8)
209 memmove(max, min, sizeof(u16) * 8);
210 for (i = 0; i < 8; i++) {
211 min[i] = htons(min[i]);
212 max[i] = htons(max[i]);
213 }
214 return 2;
215 }
216 count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
217 &min[0], &min[1], &min[2], &min[3],
218 &max[0], &max[1], &max[2], &max[3]);
219 if (count == 4 || count == 8) {
220 u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
221 + (((u8) min[2]) << 8) + (u8) min[3]);
222 memmove(min, &ip, sizeof(ip));
223 if (count == 8)
224 ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)
225 + (((u8) max[2]) << 8) + (u8) max[3]);
226 memmove(max, &ip, sizeof(ip));
227 return 1;
228 }
229 return 0;
230 }
231
232 /**
233 * ccs_write_address_group_policy - Write "struct address_group_entry" list.
234 *
235 * @data: String to parse.
236 * @is_delete: True if it is a delete request.
237 *
238 * Returns 0 on success, negative value otherwise.
239 */
240 int ccs_write_address_group_policy(char *data, const bool is_delete)
241 {
242 bool is_ipv6;
243 u16 min_address[8];
244 u16 max_address[8];
245 char *cp = strchr(data, ' ');
246 if (!cp)
247 return -EINVAL;
248 *cp++ = '\0';
249 switch (parse_ip_address(cp, min_address, max_address)) {
250 case 2:
251 is_ipv6 = true;
252 break;
253 case 1:
254 is_ipv6 = false;
255 break;
256 default:
257 return -EINVAL;
258 }
259 return update_address_group_entry(data, is_ipv6,
260 min_address, max_address, is_delete);
261 }
262
263 /**
264 * find_or_assign_new_address_group - Create address group.
265 *
266 * @group_name: The name of address group.
267 *
268 * Returns pointer to "struct address_group_entry" on success, NULL otherwise.
269 */
270 static struct address_group_entry *
271 find_or_assign_new_address_group(const char *group_name)
272 {
273 u8 i;
274 struct address_group_entry *group;
275 for (i = 0; i <= 1; i++) {
276 list1_for_each_entry(group, &address_group_list, list) {
277 if (!strcmp(group_name, group->group_name->name))
278 return group;
279 }
280 if (!i) {
281 const u16 dummy[2] = { 0, 0 };
282 update_address_group_entry(group_name, false,
283 dummy, dummy, false);
284 update_address_group_entry(group_name, false,
285 dummy, dummy, true);
286 }
287 }
288 return NULL;
289 }
290
291 /**
292 * address_matches_to_group - Check whether the given address matches members of the given address group.
293 *
294 * @is_ipv6: True if @address is an IPv6 address.
295 * @address: An IPv4 or IPv6 address.
296 * @group: Pointer to "struct address_group_entry".
297 *
298 * Returns true if @address matches addresses in @group group, false otherwise.
299 */
300 static bool address_matches_to_group(const bool is_ipv6, const u32 *address,
301 const struct address_group_entry *group)
302 {
303 struct address_group_member *member;
304 const u32 ip = ntohl(*address);
305 list1_for_each_entry(member, &group->address_group_member_list, list) {
306 if (member->is_deleted)
307 continue;
308 if (member->is_ipv6) {
309 if (is_ipv6 &&
310 memcmp(member->min.ipv6, address, 16) <= 0 &&
311 memcmp(address, member->max.ipv6, 16) <= 0)
312 return true;
313 } else {
314 if (!is_ipv6 &&
315 member->min.ipv4 <= ip && ip <= member->max.ipv4)
316 return true;
317 }
318 }
319 return false;
320 }
321
322 /**
323 * ccs_read_address_group_policy - Read "struct address_group_entry" list.
324 *
325 * @head: Pointer to "struct ccs_io_buffer".
326 *
327 * Returns true on success, false otherwise.
328 */
329 bool ccs_read_address_group_policy(struct ccs_io_buffer *head)
330 {
331 struct list1_head *gpos;
332 struct list1_head *mpos;
333 list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {
334 struct address_group_entry *group;
335 group = list1_entry(gpos, struct address_group_entry, list);
336 list1_for_each_cookie(mpos, head->read_var2,
337 &group->address_group_member_list) {
338 char buf[128];
339 struct address_group_member *member;
340 member = list1_entry(mpos, struct address_group_member,
341 list);
342 if (member->is_deleted)
343 continue;
344 if (member->is_ipv6) {
345 const struct in6_addr *min_address
346 = member->min.ipv6;
347 const struct in6_addr *max_address
348 = member->max.ipv6;
349 ccs_print_ipv6(buf, sizeof(buf), min_address);
350 if (min_address != max_address) {
351 int len;
352 char *cp = strchr(buf, '\0');
353 *cp++ = '-';
354 len = strlen(buf);
355 ccs_print_ipv6(cp, sizeof(buf) - len,
356 max_address);
357 }
358 } else {
359 const u32 min_address = member->min.ipv4;
360 const u32 max_address = member->max.ipv4;
361 memset(buf, 0, sizeof(buf));
362 snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u",
363 HIPQUAD(min_address));
364 if (min_address != max_address) {
365 const int len = strlen(buf);
366 snprintf(buf + len,
367 sizeof(buf) - 1 - len,
368 "-%u.%u.%u.%u",
369 HIPQUAD(max_address));
370 }
371 }
372 if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP
373 "%s %s\n", group->group_name->name,
374 buf))
375 goto out;
376 }
377 }
378 return true;
379 out:
380 return false;
381 }
382
383 #if !defined(NIP6)
384 #define NIP6(addr) \
385 ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \
386 ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
387 ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
388 ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7])
389 #endif
390
391 /**
392 * ccs_print_ipv6 - Print an IPv6 address.
393 *
394 * @buffer: Buffer to write to.
395 * @buffer_len: Size of @buffer.
396 * @ip: Pointer to "struct in6_addr".
397 *
398 * Returns nothing.
399 */
400 void ccs_print_ipv6(char *buffer, const int buffer_len,
401 const struct in6_addr *ip)
402 {
403 memset(buffer, 0, buffer_len);
404 snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));
405 }
406
407 /**
408 * ccs_net2keyword - Convert network operation index to network operation name.
409 *
410 * @operation: Type of operation.
411 *
412 * Returns the name of operation.
413 */
414 const char *ccs_net2keyword(const u8 operation)
415 {
416 const char *keyword = "unknown";
417 switch (operation) {
418 case NETWORK_ACL_UDP_BIND:
419 keyword = "UDP bind";
420 break;
421 case NETWORK_ACL_UDP_CONNECT:
422 keyword = "UDP connect";
423 break;
424 case NETWORK_ACL_TCP_BIND:
425 keyword = "TCP bind";
426 break;
427 case NETWORK_ACL_TCP_LISTEN:
428 keyword = "TCP listen";
429 break;
430 case NETWORK_ACL_TCP_CONNECT:
431 keyword = "TCP connect";
432 break;
433 case NETWORK_ACL_TCP_ACCEPT:
434 keyword = "TCP accept";
435 break;
436 case NETWORK_ACL_RAW_BIND:
437 keyword = "RAW bind";
438 break;
439 case NETWORK_ACL_RAW_CONNECT:
440 keyword = "RAW connect";
441 break;
442 }
443 return keyword;
444 }
445
446 /**
447 * update_network_entry - Update "struct ip_network_acl_record" list.
448 *
449 * @operation: Type of operation.
450 * @record_type: Type of address.
451 * @group: Pointer to "struct address_group_entry". May be NULL.
452 * @min_address: Start of IPv4 or IPv6 address range.
453 * @max_address: End of IPv4 or IPv6 address range.
454 * @min_port: Start of port number range.
455 * @max_port: End of port number range.
456 * @domain: Pointer to "struct domain_info".
457 * @condition: Pointer to "struct condition_list". May be NULL.
458 * @is_delete: True if it is a delete request.
459 *
460 * Returns 0 on success, negative value otherwise.
461 */
462 static int update_network_entry(const u8 operation, const u8 record_type,
463 const struct address_group_entry *group,
464 const u32 *min_address, const u32 *max_address,
465 const u16 min_port, const u16 max_port,
466 struct domain_info *domain,
467 const struct condition_list *condition,
468 const bool is_delete)
469 {
470 struct acl_info *ptr;
471 struct ip_network_acl_record *acl;
472 int error = -ENOMEM;
473 /* using host byte order to allow u32 comparison than memcmp().*/
474 const u32 min_ip = ntohl(*min_address);
475 const u32 max_ip = ntohl(*max_address);
476 const struct in6_addr *saved_min_address = NULL;
477 const struct in6_addr *saved_max_address = NULL;
478 if (!domain)
479 return -EINVAL;
480 if (record_type != IP_RECORD_TYPE_IPv6)
481 goto not_ipv6;
482 saved_min_address = save_ipv6_address((struct in6_addr *) min_address);
483 saved_max_address = save_ipv6_address((struct in6_addr *) max_address);
484 if (!saved_min_address || !saved_max_address)
485 return -ENOMEM;
486 not_ipv6:
487 mutex_lock(&domain_acl_lock);
488 if (is_delete)
489 goto delete;
490 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
491 if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)
492 continue;
493 if (ccs_get_condition_part(ptr) != condition)
494 continue;
495 acl = container_of(ptr, struct ip_network_acl_record, head);
496 if (acl->operation_type != operation ||
497 acl->record_type != record_type ||
498 acl->min_port != min_port || max_port != acl->max_port)
499 continue;
500 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
501 if (acl->u.group != group)
502 continue;
503 } else if (record_type == IP_RECORD_TYPE_IPv4) {
504 if (acl->u.ipv4.min != min_ip ||
505 max_ip != acl->u.ipv4.max)
506 continue;
507 } else if (record_type == IP_RECORD_TYPE_IPv6) {
508 if (acl->u.ipv6.min != saved_min_address ||
509 saved_max_address != acl->u.ipv6.max)
510 continue;
511 }
512 error = ccs_add_domain_acl(NULL, ptr);
513 goto out;
514 }
515 /* Not found. Append it to the tail. */
516 acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition);
517 if (!acl)
518 goto out;
519 acl->operation_type = operation;
520 acl->record_type = record_type;
521 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
522 acl->u.group = group;
523 } else if (record_type == IP_RECORD_TYPE_IPv4) {
524 acl->u.ipv4.min = min_ip;
525 acl->u.ipv4.max = max_ip;
526 } else {
527 acl->u.ipv6.min = saved_min_address;
528 acl->u.ipv6.max = saved_max_address;
529 }
530 acl->min_port = min_port;
531 acl->max_port = max_port;
532 error = ccs_add_domain_acl(domain, &acl->head);
533 goto out;
534 delete:
535 error = -ENOENT;
536 list1_for_each_entry(ptr, &domain->acl_info_list, list) {
537 if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
538 continue;
539 if (ccs_get_condition_part(ptr) != condition)
540 continue;
541 acl = container_of(ptr, struct ip_network_acl_record, head);
542 if (acl->operation_type != operation ||
543 acl->record_type != record_type ||
544 acl->min_port != min_port || max_port != acl->max_port)
545 continue;
546 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
547 if (acl->u.group != group)
548 continue;
549 } else if (record_type == IP_RECORD_TYPE_IPv4) {
550 if (acl->u.ipv4.min != min_ip ||
551 max_ip != acl->u.ipv4.max)
552 continue;
553 } else if (record_type == IP_RECORD_TYPE_IPv6) {
554 if (acl->u.ipv6.min != saved_min_address ||
555 saved_max_address != acl->u.ipv6.max)
556 continue;
557 }
558 error = ccs_del_domain_acl(ptr);
559 break;
560 }
561 out:
562 mutex_unlock(&domain_acl_lock);
563 return error;
564 }
565
566 /**
567 * check_network_entry - Check permission for network operation.
568 *
569 * @is_ipv6: True if @address is an IPv6 address.
570 * @operation: Type of operation.
571 * @address: An IPv4 or IPv6 address.
572 * @port: Port number.
573 *
574 * Returns 0 on success, negative value otherwise.
575 */
576 static int check_network_entry(const bool is_ipv6, const u8 operation,
577 const u32 *address, const u16 port)
578 {
579 struct ccs_request_info r;
580 struct acl_info *ptr;
581 const char *keyword = ccs_net2keyword(operation);
582 bool is_enforce;
583 /* using host byte order to allow u32 comparison than memcmp().*/
584 const u32 ip = ntohl(*address);
585 bool found = false;
586 char buf[64];
587 if (!ccs_can_sleep())
588 return 0;
589 ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK);
590 is_enforce = (r.mode == 3);
591 if (!r.mode)
592 return 0;
593 retry:
594 list1_for_each_entry(ptr, &r.domain->acl_info_list, list) {
595 struct ip_network_acl_record *acl;
596 if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
597 continue;
598 acl = container_of(ptr, struct ip_network_acl_record, head);
599 if (acl->operation_type != operation || port < acl->min_port ||
600 acl->max_port < port || !ccs_check_condition(&r, ptr))
601 continue;
602 if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
603 if (!address_matches_to_group(is_ipv6, address,
604 acl->u.group))
605 continue;
606 } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {
607 if (is_ipv6 ||
608 ip < acl->u.ipv4.min || acl->u.ipv4.max < ip)
609 continue;
610 } else {
611 if (!is_ipv6 ||
612 memcmp(acl->u.ipv6.min, address, 16) > 0 ||
613 memcmp(address, acl->u.ipv6.max, 16) > 0)
614 continue;
615 }
616 ccs_update_condition(ptr);
617 found = true;
618 break;
619 }
620 memset(buf, 0, sizeof(buf));
621 if (is_ipv6)
622 ccs_print_ipv6(buf, sizeof(buf),
623 (const struct in6_addr *) address);
624 else
625 snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
626 audit_network_log(&r, keyword, buf, port, found);
627 if (found)
628 return 0;
629 if (ccs_verbose_mode(r.domain))
630 printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",
631 ccs_get_msg(is_enforce), keyword, buf, port,
632 ccs_get_last_name(r.domain));
633 if (is_enforce) {
634 int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK
635 "%s %s %u\n", keyword, buf,
636 port);
637 if (error == 1) {
638 r.retry++;
639 goto retry;
640 }
641 return error;
642 }
643 if (r.mode == 1 && ccs_check_domain_quota(r.domain))
644 update_network_entry(operation, is_ipv6 ?
645 IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4,
646 NULL, address, address, port, port,
647 r.domain, NULL, 0);
648 return 0;
649 }
650
651 /**
652 * ccs_write_network_policy - Write "struct ip_network_acl_record" list.
653 *
654 * @data: String to parse.
655 * @domain: Pointer to "struct domain_info".
656 * @condition: Pointer to "struct condition_list". May be NULL.
657 * @is_delete: True if it is a delete request.
658 *
659 * Returns 0 on success, negative value otherwise.
660 */
661 int ccs_write_network_policy(char *data, struct domain_info *domain,
662 const struct condition_list *condition,
663 const bool is_delete)
664 {
665 u8 sock_type;
666 u8 operation;
667 u8 record_type;
668 u16 min_address[8];
669 u16 max_address[8];
670 struct address_group_entry *group = NULL;
671 u16 min_port;
672 u16 max_port;
673 u8 count;
674 char *cp1 = strchr(data, ' ');
675 char *cp2;
676 if (!cp1)
677 goto out;
678 cp1++;
679 if (!strncmp(data, "TCP ", 4))
680 sock_type = SOCK_STREAM;
681 else if (!strncmp(data, "UDP ", 4))
682 sock_type = SOCK_DGRAM;
683 else if (!strncmp(data, "RAW ", 4))
684 sock_type = SOCK_RAW;
685 else
686 goto out;
687 cp2 = strchr(cp1, ' ');
688 if (!cp2)
689 goto out;
690 cp2++;
691 if (!strncmp(cp1, "bind ", 5))
692 switch (sock_type) {
693 case SOCK_STREAM:
694 operation = NETWORK_ACL_TCP_BIND;
695 break;
696 case SOCK_DGRAM:
697 operation = NETWORK_ACL_UDP_BIND;
698 break;
699 default:
700 operation = NETWORK_ACL_RAW_BIND;
701 }
702 else if (!strncmp(cp1, "connect ", 8))
703 switch (sock_type) {
704 case SOCK_STREAM:
705 operation = NETWORK_ACL_TCP_CONNECT;
706 break;
707 case SOCK_DGRAM:
708 operation = NETWORK_ACL_UDP_CONNECT;
709 break;
710 default:
711 operation = NETWORK_ACL_RAW_CONNECT;
712 }
713 else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7))
714 operation = NETWORK_ACL_TCP_LISTEN;
715 else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7))
716 operation = NETWORK_ACL_TCP_ACCEPT;
717 else
718 goto out;
719 cp1 = strchr(cp2, ' ');
720 if (!cp1)
721 goto out;
722 *cp1++ = '\0';
723 switch (parse_ip_address(cp2, min_address, max_address)) {
724 case 2:
725 record_type = IP_RECORD_TYPE_IPv6;
726 break;
727 case 1:
728 record_type = IP_RECORD_TYPE_IPv4;
729 break;
730 default:
731 if (*cp2 != '@')
732 goto out;
733 group = find_or_assign_new_address_group(cp2 + 1);
734 if (!group)
735 return -ENOMEM;
736 record_type = IP_RECORD_TYPE_ADDRESS_GROUP;
737 break;
738 }
739 if (strchr(cp1, ' '))
740 goto out;
741 count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);
742 if (count != 1 && count != 2)
743 goto out;
744 if (count == 1)
745 max_port = min_port;
746 return update_network_entry(operation, record_type, group,
747 (u32 *) min_address, (u32 *) max_address,
748 min_port, max_port, domain, condition,
749 is_delete);
750 out:
751 return -EINVAL;
752 }
753
754 /**
755 * ccs_check_network_listen_acl - Check permission for listen() operation.
756 *
757 * @is_ipv6: True if @address is an IPv6 address.
758 * @address: An IPv4 or IPv6 address.
759 * @port: Port number.
760 *
761 * Returns 0 on success, negative value otherwise.
762 */
763 int ccs_check_network_listen_acl(const _Bool is_ipv6, const u8 *address,
764 const u16 port)
765 {
766 return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,
767 (const u32 *) address, ntohs(port));
768 }
769
770 /**
771 * ccs_check_network_connect_acl - Check permission for connect() operation.
772 *
773 * @is_ipv6: True if @address is an IPv6 address.
774 * @sock_type: Type of socket. (TCP or UDP or RAW)
775 * @address: An IPv4 or IPv6 address.
776 * @port: Port number.
777 *
778 * Returns 0 on success, negative value otherwise.
779 */
780 int ccs_check_network_connect_acl(const _Bool is_ipv6, const int sock_type,
781 const u8 *address, const u16 port)
782 {
783 u8 operation;
784 switch (sock_type) {
785 case SOCK_STREAM:
786 operation = NETWORK_ACL_TCP_CONNECT;
787 break;
788 case SOCK_DGRAM:
789 operation = NETWORK_ACL_UDP_CONNECT;
790 break;
791 default:
792 operation = NETWORK_ACL_RAW_CONNECT;
793 }
794 return check_network_entry(is_ipv6, operation, (const u32 *) address,
795 ntohs(port));
796 }
797
798 /**
799 * ccs_check_network_bind_acl - Check permission for bind() operation.
800 *
801 * @is_ipv6: True if @address is an IPv6 address.
802 * @sock_type: Type of socket. (TCP or UDP or RAW)
803 * @address: An IPv4 or IPv6 address.
804 * @port: Port number.
805 *
806 * Returns 0 on success, negative value otherwise.
807 */
808 int ccs_check_network_bind_acl(const _Bool is_ipv6, const int sock_type,
809 const u8 *address, const u16 port)
810 {
811 u8 operation;
812 switch (sock_type) {
813 case SOCK_STREAM:
814 operation = NETWORK_ACL_TCP_BIND;
815 break;
816 case SOCK_DGRAM:
817 operation = NETWORK_ACL_UDP_BIND;
818 break;
819 default:
820 operation = NETWORK_ACL_RAW_BIND;
821 }
822 return check_network_entry(is_ipv6, operation, (const u32 *) address,
823 ntohs(port));
824 }
825
826 /**
827 * ccs_check_network_accept_acl - Check permission for accept() operation.
828 *
829 * @is_ipv6: True if @address is an IPv6 address.
830 * @address: An IPv4 or IPv6 address.
831 * @port: Port number.
832 *
833 * Returns 0 on success, negative value otherwise.
834 */
835 int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address,
836 const u16 port)
837 {
838 int retval;
839 current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
840 retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,
841 (const u32 *) address, ntohs(port));
842 current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
843 return retval;
844 }
845
846 /**
847 * ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation.
848 *
849 * @is_ipv6: True if @address is an IPv6 address.
850 * @sock_type: Type of socket. (UDP or RAW)
851 * @address: An IPv4 or IPv6 address.
852 * @port: Port number.
853 *
854 * Returns 0 on success, negative value otherwise.
855 */
856 int ccs_check_network_sendmsg_acl(const _Bool is_ipv6, const int sock_type,
857 const u8 *address, const u16 port)
858 {
859 u8 operation;
860 if (sock_type == SOCK_DGRAM)
861 operation = NETWORK_ACL_UDP_CONNECT;
862 else
863 operation = NETWORK_ACL_RAW_CONNECT;
864 return check_network_entry(is_ipv6, operation, (const u32 *) address,
865 ntohs(port));
866 }
867
868 /**
869 * ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation.
870 *
871 * @is_ipv6: True if @address is an IPv6 address.
872 * @sock_type: Type of socket. (UDP or RAW)
873 * @address: An IPv4 or IPv6 address.
874 * @port: Port number.
875 *
876 * Returns 0 on success, negative value otherwise.
877 */
878 int ccs_check_network_recvmsg_acl(const _Bool is_ipv6, const int sock_type,
879 const u8 *address, const u16 port)
880 {
881 int retval;
882 const u8 operation
883 = (sock_type == SOCK_DGRAM) ?
884 NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;
885 current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
886 retval = check_network_entry(is_ipv6, operation, (const u32 *) address,
887 ntohs(port));
888 current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
889 return retval;
890 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26