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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/network.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/1.6.x/ccs-patch/fs/tomoyo_network.c revision 1886 by kumaneko, Tue Nov 25 05:41:59 2008 UTC branches/ccs-patch/security/ccsecurity/network.c revision 2918 by kumaneko, Tue Aug 18 05:21:21 2009 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/tomoyo_network.c   * security/ccsecurity/network.c
3   *   *
4   * Implementation of the Domain-Based Mandatory Access Control.   * Copyright (C) 2005-2009  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2008  NTT DATA CORPORATION   * Version: 1.7.0-pre   2009/08/08
  *  
  * Version: 1.6.5   2008/11/11  
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
10   *   *
11   */   */
12    
 #include <linux/ccs_common.h>  
 #include <linux/tomoyo.h>  
 #include <linux/realpath.h>  
13  #include <linux/net.h>  #include <linux/net.h>
14  #include <linux/inet.h>  #include <linux/inet.h>
15  #include <linux/in.h>  #include <linux/in.h>
16  #include <linux/in6.h>  #include <linux/in6.h>
17    #include <net/ip.h>
18    #include <net/ipv6.h>
19    #include <net/udp.h>
20    #include "internal.h"
21    
22  /**  /**
23   * audit_network_log - Audit network log.   * ccs_audit_network_log - Audit network log.
24   *   *
25   * @r:          Pointer to "struct ccs_request_info".   * @r:          Pointer to "struct ccs_request_info".
26   * @operation:  The name of operation.   * @operation:  The name of operation.
# Line 31  Line 30 
30   *   *
31   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
32   */   */
33  static int audit_network_log(struct ccs_request_info *r, const char *operation,  static int ccs_audit_network_log(struct ccs_request_info *r,
34                               const char *address, const u16 port,                                   const char *operation, const char *address,
35                               const bool is_granted)                                   const u16 port, const bool is_granted)
36  {  {
37          return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK          if (!is_granted && ccs_verbose_mode(r->domain))
38                    printk(KERN_WARNING "%s: %s to %s %u denied for %s\n",
39                           ccs_get_msg(r->mode == 3), operation, address, port,
40                           ccs_get_last_name(r->domain));
41            return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK
42                                     "%s %s %u\n", operation, address, port);                                     "%s %s %u\n", operation, address, port);
43  }  }
44    
45  /**  /**
46   * save_ipv6_address - Keep the given IPv6 address on the RAM.   * ccs_parse_ip_address - Parse an IP address.
  *  
  * @addr: Pointer to "struct in6_addr".  
  *  
  * Returns pointer to "struct in6_addr" on success, NULL otherwise.  
  *  
  * The RAM is shared, so NEVER try to modify or kfree() the returned address.  
  */  
 static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr)  
 {  
         static const u8 block_size = 16;  
         struct addr_list {  
                 /* Workaround for gcc 4.3's bug. */  
                 struct in6_addr addr[16]; /* = block_size */  
                 struct list1_head list;  
                 u32 in_use_count;  
         };  
         static LIST1_HEAD(address_list);  
         struct addr_list *ptr;  
         static DEFINE_MUTEX(lock);  
         u8 i = block_size;  
         if (!addr)  
                 return NULL;  
         mutex_lock(&lock);  
         list1_for_each_entry(ptr, &address_list, list) {  
                 for (i = 0; i < ptr->in_use_count; i++) {  
                         if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))  
                                 goto ok;  
                 }  
                 if (i < block_size)  
                         break;  
         }  
         if (i == block_size) {  
                 ptr = ccs_alloc_element(sizeof(*ptr));  
                 if (!ptr)  
                         goto ok;  
                 list1_add_tail_mb(&ptr->list, &address_list);  
                 i = 0;  
         }  
         ptr->addr[ptr->in_use_count++] = *addr;  
  ok:  
         mutex_unlock(&lock);  
         return ptr ? &ptr->addr[i] : NULL;  
 }  
   
 /* The list for "struct address_group_entry". */  
 static LIST1_HEAD(address_group_list);  
   
 /**  
  * update_address_group_entry - Update "struct address_group_entry" list.  
  *  
  * @group_name:  The name of address group.  
  * @is_ipv6:     True if @min_address and @max_address are IPv6 addresses.  
  * @min_address: Start of IPv4 or IPv6 address range.  
  * @max_address: End of IPv4 or IPv6 address range.  
  * @is_delete:   True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int update_address_group_entry(const char *group_name,  
                                       const bool is_ipv6,  
                                       const u16 *min_address,  
                                       const u16 *max_address,  
                                       const bool is_delete)  
 {  
         static DEFINE_MUTEX(lock);  
         struct address_group_entry *new_group;  
         struct address_group_entry *group;  
         struct address_group_member *new_member;  
         struct address_group_member *member;  
         const struct path_info *saved_group_name;  
         const struct in6_addr *saved_min_address = NULL;  
         const struct in6_addr *saved_max_address = NULL;  
         int error = -ENOMEM;  
         bool found = false;  
         if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||  
             !group_name[0])  
                 return -EINVAL;  
         saved_group_name = ccs_save_name(group_name);  
         if (!saved_group_name)  
                 return -ENOMEM;  
         if (!is_ipv6)  
                 goto not_ipv6;  
         saved_min_address  
                 = save_ipv6_address((struct in6_addr *) min_address);  
         saved_max_address  
                 = save_ipv6_address((struct in6_addr *) max_address);  
         if (!saved_min_address || !saved_max_address)  
                 return -ENOMEM;  
  not_ipv6:  
         mutex_lock(&lock);  
         list1_for_each_entry(group, &address_group_list, list) {  
                 if (saved_group_name != group->group_name)  
                         continue;  
                 list1_for_each_entry(member, &group->address_group_member_list,  
                                      list) {  
                         if (member->is_ipv6 != is_ipv6)  
                                 continue;  
                         if (is_ipv6) {  
                                 if (member->min.ipv6 != saved_min_address ||  
                                     member->max.ipv6 != saved_max_address)  
                                         continue;  
                         } else {  
                                 if (member->min.ipv4 != *(u32 *) min_address ||  
                                     member->max.ipv4 != *(u32 *) max_address)  
                                         continue;  
                         }  
                         member->is_deleted = is_delete;  
                         error = 0;  
                         goto out;  
                 }  
                 found = true;  
                 break;  
         }  
         if (is_delete) {  
                 error = -ENOENT;  
                 goto out;  
         }  
         if (!found) {  
                 new_group = ccs_alloc_element(sizeof(*new_group));  
                 if (!new_group)  
                         goto out;  
                 INIT_LIST1_HEAD(&new_group->address_group_member_list);  
                 new_group->group_name = saved_group_name;  
                 list1_add_tail_mb(&new_group->list, &address_group_list);  
                 group = new_group;  
         }  
         new_member = ccs_alloc_element(sizeof(*new_member));  
         if (!new_member)  
                 goto out;  
         new_member->is_ipv6 = is_ipv6;  
         if (is_ipv6) {  
                 new_member->min.ipv6 = saved_min_address;  
                 new_member->max.ipv6 = saved_max_address;  
         } else {  
                 new_member->min.ipv4 = *(u32 *) min_address;  
                 new_member->max.ipv4 = *(u32 *) max_address;  
         }  
         list1_add_tail_mb(&new_member->list, &group->address_group_member_list);  
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);  
         return error;  
 }  
   
 /**  
  * parse_ip_address - Parse an IP address.  
47   *   *
48   * @address: String to parse.   * @address: String to parse.
49   * @min:     Pointer to store min address.   * @min:     Pointer to store min address.
# Line 195  static int update_address_group_entry(co Line 51  static int update_address_group_entry(co
51   *   *
52   * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.   * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.
53   */   */
54  static int parse_ip_address(char *address, u16 *min, u16 *max)  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
55  {  {
56          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
57                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
# Line 229  static int parse_ip_address(char *addres Line 85  static int parse_ip_address(char *addres
85          return 0;          return 0;
86  }  }
87    
 /**  
  * ccs_write_address_group_policy - Write "struct address_group_entry" list.  
  *  
  * @data:      String to parse.  
  * @is_delete: True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 int ccs_write_address_group_policy(char *data, const bool is_delete)  
 {  
         bool is_ipv6;  
         u16 min_address[8];  
         u16 max_address[8];  
         char *cp = strchr(data, ' ');  
         if (!cp)  
                 return -EINVAL;  
         *cp++ = '\0';  
         switch (parse_ip_address(cp, min_address, max_address)) {  
         case 2:  
                 is_ipv6 = true;  
                 break;  
         case 1:  
                 is_ipv6 = false;  
                 break;  
         default:  
                 return -EINVAL;  
         }  
         return update_address_group_entry(data, is_ipv6,  
                                           min_address, max_address, is_delete);  
 }  
   
 /**  
  * find_or_assign_new_address_group - Create address group.  
  *  
  * @group_name: The name of address group.  
  *  
  * Returns pointer to "struct address_group_entry" on success, NULL otherwise.  
  */  
 static struct address_group_entry *  
 find_or_assign_new_address_group(const char *group_name)  
 {  
         u8 i;  
         struct address_group_entry *group;  
         for (i = 0; i <= 1; i++) {  
                 list1_for_each_entry(group, &address_group_list, list) {  
                         if (!strcmp(group_name, group->group_name->name))  
                                 return group;  
                 }  
                 if (!i) {  
                         const u16 dummy[2] = { 0, 0 };  
                         update_address_group_entry(group_name, false,  
                                                    dummy, dummy, false);  
                         update_address_group_entry(group_name, false,  
                                                    dummy, dummy, true);  
                 }  
         }  
         return NULL;  
 }  
   
 /**  
  * address_matches_to_group - Check whether the given address matches members of the given address group.  
  *  
  * @is_ipv6: True if @address is an IPv6 address.  
  * @address: An IPv4 or IPv6 address.  
  * @group:   Pointer to "struct address_group_entry".  
  *  
  * Returns true if @address matches addresses in @group group, false otherwise.  
  */  
 static bool address_matches_to_group(const bool is_ipv6, const u32 *address,  
                                      const struct address_group_entry *group)  
 {  
         struct address_group_member *member;  
         const u32 ip = ntohl(*address);  
         list1_for_each_entry(member, &group->address_group_member_list, list) {  
                 if (member->is_deleted)  
                         continue;  
                 if (member->is_ipv6) {  
                         if (is_ipv6 &&  
                             memcmp(member->min.ipv6, address, 16) <= 0 &&  
                             memcmp(address, member->max.ipv6, 16) <= 0)  
                                 return true;  
                 } else {  
                         if (!is_ipv6 &&  
                             member->min.ipv4 <= ip && ip <= member->max.ipv4)  
                                 return true;  
                 }  
         }  
         return false;  
 }  
   
 /**  
  * ccs_read_address_group_policy - Read "struct address_group_entry" list.  
  *  
  * @head: Pointer to "struct ccs_io_buffer".  
  *  
  * Returns true on success, false otherwise.  
  */  
 bool ccs_read_address_group_policy(struct ccs_io_buffer *head)  
 {  
         struct list1_head *gpos;  
         struct list1_head *mpos;  
         list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {  
                 struct address_group_entry *group;  
                 group = list1_entry(gpos, struct address_group_entry, list);  
                 list1_for_each_cookie(mpos, head->read_var2,  
                                       &group->address_group_member_list) {  
                         char buf[128];  
                         struct address_group_member *member;  
                         member = list1_entry(mpos, struct address_group_member,  
                                              list);  
                         if (member->is_deleted)  
                                 continue;  
                         if (member->is_ipv6) {  
                                 const struct in6_addr *min_address  
                                         = member->min.ipv6;  
                                 const struct in6_addr *max_address  
                                         = member->max.ipv6;  
                                 ccs_print_ipv6(buf, sizeof(buf), min_address);  
                                 if (min_address != max_address) {  
                                         int len;  
                                         char *cp = buf + strlen(buf);  
                                         *cp++ = '-';  
                                         len = strlen(buf);  
                                         ccs_print_ipv6(cp, sizeof(buf) - len,  
                                                        max_address);  
                                 }  
                         } else {  
                                 const u32 min_address = member->min.ipv4;  
                                 const u32 max_address = member->max.ipv4;  
                                 memset(buf, 0, sizeof(buf));  
                                 snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u",  
                                          HIPQUAD(min_address));  
                                 if (min_address != max_address) {  
                                         const int len = strlen(buf);  
                                         snprintf(buf + len,  
                                                  sizeof(buf) - 1 - len,  
                                                  "-%u.%u.%u.%u",  
                                                  HIPQUAD(max_address));  
                                 }  
                         }  
                         if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP  
                                            "%s %s\n", group->group_name->name,  
                                            buf))  
                                 goto out;  
                 }  
         }  
         return true;  
  out:  
         return false;  
 }  
   
88  #if !defined(NIP6)  #if !defined(NIP6)
89  #define NIP6(addr)      \  #define NIP6(addr)      \
90          ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \          ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \
# Line 415  const char *ccs_net2keyword(const u8 ope Line 120  const char *ccs_net2keyword(const u8 ope
120  {  {
121          const char *keyword = "unknown";          const char *keyword = "unknown";
122          switch (operation) {          switch (operation) {
123          case NETWORK_ACL_UDP_BIND:          case CCS_NETWORK_UDP_BIND:
124                  keyword = "UDP bind";                  keyword = "UDP bind";
125                  break;                  break;
126          case NETWORK_ACL_UDP_CONNECT:          case CCS_NETWORK_UDP_CONNECT:
127                  keyword = "UDP connect";                  keyword = "UDP connect";
128                  break;                  break;
129          case NETWORK_ACL_TCP_BIND:          case CCS_NETWORK_TCP_BIND:
130                  keyword = "TCP bind";                  keyword = "TCP bind";
131                  break;                  break;
132          case NETWORK_ACL_TCP_LISTEN:          case CCS_NETWORK_TCP_LISTEN:
133                  keyword = "TCP listen";                  keyword = "TCP listen";
134                  break;                  break;
135          case NETWORK_ACL_TCP_CONNECT:          case CCS_NETWORK_TCP_CONNECT:
136                  keyword = "TCP connect";                  keyword = "TCP connect";
137                  break;                  break;
138          case NETWORK_ACL_TCP_ACCEPT:          case CCS_NETWORK_TCP_ACCEPT:
139                  keyword = "TCP accept";                  keyword = "TCP accept";
140                  break;                  break;
141          case NETWORK_ACL_RAW_BIND:          case CCS_NETWORK_RAW_BIND:
142                  keyword = "RAW bind";                  keyword = "RAW bind";
143                  break;                  break;
144          case NETWORK_ACL_RAW_CONNECT:          case CCS_NETWORK_RAW_CONNECT:
145                  keyword = "RAW connect";                  keyword = "RAW connect";
146                  break;                  break;
147          }          }
# Line 444  const char *ccs_net2keyword(const u8 ope Line 149  const char *ccs_net2keyword(const u8 ope
149  }  }
150    
151  /**  /**
152   * update_network_entry - Update "struct ip_network_acl_record" list.   * ccs_check_network_entry2 - Check permission for network operation.
  *  
  * @operation:   Type of operation.  
  * @record_type: Type of address.  
  * @group:       Pointer to "struct address_group_entry". May be NULL.  
  * @min_address: Start of IPv4 or IPv6 address range.  
  * @max_address: End of IPv4 or IPv6 address range.  
  * @min_port:    Start of port number range.  
  * @max_port:    End of port number range.  
  * @domain:      Pointer to "struct domain_info".  
  * @condition:   Pointer to "struct condition_list". May be NULL.  
  * @is_delete:   True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int update_network_entry(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)  
 {  
         static DEFINE_MUTEX(lock);  
         struct acl_info *ptr;  
         struct ip_network_acl_record *acl;  
         int error = -ENOMEM;  
         /* using host byte order to allow u32 comparison than memcmp().*/  
         const u32 min_ip = ntohl(*min_address);  
         const u32 max_ip = ntohl(*max_address);  
         const struct in6_addr *saved_min_address = NULL;  
         const struct in6_addr *saved_max_address = NULL;  
         if (!domain)  
                 return -EINVAL;  
         if (record_type != IP_RECORD_TYPE_IPv6)  
                 goto not_ipv6;  
         saved_min_address = save_ipv6_address((struct in6_addr *) min_address);  
         saved_max_address = save_ipv6_address((struct in6_addr *) max_address);  
         if (!saved_min_address || !saved_max_address)  
                 return -ENOMEM;  
  not_ipv6:  
         mutex_lock(&lock);  
         if (is_delete)  
                 goto delete;  
         list1_for_each_entry(ptr, &domain->acl_info_list, list) {  
                 if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)  
                         continue;  
                 if (ccs_get_condition_part(ptr) != condition)  
                         continue;  
                 acl = container_of(ptr, struct ip_network_acl_record, head);  
                 if (acl->operation_type != operation ||  
                     acl->record_type != record_type ||  
                     acl->min_port != min_port || max_port != acl->max_port)  
                         continue;  
                 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                         if (acl->u.group != group)  
                                 continue;  
                 } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                         if (acl->u.ipv4.min != min_ip ||  
                             max_ip != acl->u.ipv4.max)  
                                 continue;  
                 } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                         if (acl->u.ipv6.min != saved_min_address ||  
                             saved_max_address != acl->u.ipv6.max)  
                                 continue;  
                 }  
                 error = ccs_add_domain_acl(NULL, ptr);  
                 goto out;  
         }  
         /* Not found. Append it to the tail. */  
         acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition);  
         if (!acl)  
                 goto out;  
         acl->operation_type = operation;  
         acl->record_type = record_type;  
         if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                 acl->u.group = group;  
         } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                 acl->u.ipv4.min = min_ip;  
                 acl->u.ipv4.max = max_ip;  
         } else {  
                 acl->u.ipv6.min = saved_min_address;  
                 acl->u.ipv6.max = saved_max_address;  
         }  
         acl->min_port = min_port;  
         acl->max_port = max_port;  
         error = ccs_add_domain_acl(domain, &acl->head);  
         goto out;  
  delete:  
         error = -ENOENT;  
         list1_for_each_entry(ptr, &domain->acl_info_list, list) {  
                 if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)  
                         continue;  
                 if (ccs_get_condition_part(ptr) != condition)  
                         continue;  
                 acl = container_of(ptr, struct ip_network_acl_record, head);  
                 if (acl->operation_type != operation ||  
                     acl->record_type != record_type ||  
                     acl->min_port != min_port || max_port != acl->max_port)  
                         continue;  
                 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                         if (acl->u.group != group)  
                                 continue;  
                 } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                         if (acl->u.ipv4.min != min_ip ||  
                             max_ip != acl->u.ipv4.max)  
                                 continue;  
                 } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                         if (acl->u.ipv6.min != saved_min_address ||  
                             saved_max_address != acl->u.ipv6.max)  
                                 continue;  
                 }  
                 error = ccs_del_domain_acl(ptr);  
                 break;  
         }  
  out:  
         mutex_unlock(&lock);  
         return error;  
 }  
   
 /**  
  * check_network_entry - Check permission for network operation.  
153   *   *
154   * @is_ipv6:   True if @address is an IPv6 address.   * @is_ipv6:   True if @address is an IPv6 address.
155   * @operation: Type of operation.   * @operation: Type of operation.
# Line 573  static int update_network_entry(const u8 Line 157  static int update_network_entry(const u8
157   * @port:      Port number.   * @port:      Port number.
158   *   *
159   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
160     *
161     * Caller holds ccs_read_lock().
162   */   */
163  static int check_network_entry(const bool is_ipv6, const u8 operation,  static int ccs_check_network_entry2(const bool is_ipv6, const u8 operation,
164                                 const u32 *address, const u16 port)                                      const u32 *address, const u16 port)
165  {  {
166          struct ccs_request_info r;          struct ccs_request_info r;
167          struct acl_info *ptr;          struct ccs_acl_info *ptr;
168          const char *keyword = ccs_net2keyword(operation);          const char *keyword = ccs_net2keyword(operation);
169          bool is_enforce;          bool is_enforce;
170            const u16 perm = 1 << operation;
171          /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
172          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
173          bool found = false;          int error;
174          char buf[64];          char buf[64];
175          if (!ccs_can_sleep())          ccs_check_read_lock();
176            if (!ccs_can_sleep() ||
177                !ccs_init_request_info(&r, NULL, CCS_MAC_NETWORK))
178                  return 0;                  return 0;
         ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK);  
179          is_enforce = (r.mode == 3);          is_enforce = (r.mode == 3);
180          if (!r.mode)   retry:
181                  return 0;          error = -EPERM;
182  retry:          list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {
183          list1_for_each_entry(ptr, &r.domain->acl_info_list, list) {                  struct ccs_ip_network_acl *acl;
184                  struct ip_network_acl_record *acl;                  if (ptr->is_deleted || ptr->type != CCS_TYPE_IP_NETWORK_ACL)
185                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)                          continue;
186                    acl = container_of(ptr, struct ccs_ip_network_acl, head);
187                    if (!(acl->perm & perm))
188                          continue;                          continue;
189                  acl = container_of(ptr, struct ip_network_acl_record, head);                  if (!ccs_compare_number_union(port, &acl->port) ||
190                  if (acl->operation_type != operation || port < acl->min_port ||                      !ccs_check_condition(&r, ptr))
                     acl->max_port < port || !ccs_check_condition(&r, ptr))  
191                          continue;                          continue;
192                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  if (acl->address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP) {
193                          if (!address_matches_to_group(is_ipv6, address,                          if (!ccs_address_matches_group(is_ipv6, address,
194                                                        acl->u.group))                                                         acl->address.group))
195                                  continue;                                  continue;
196                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {                  } else if (acl->address_type == CCS_IP_ADDRESS_TYPE_IPv4) {
197                          if (is_ipv6 ||                          if (is_ipv6 ||
198                              ip < acl->u.ipv4.min || acl->u.ipv4.max < ip)                              ip < acl->address.ipv4.min ||
199                                acl->address.ipv4.max < ip)
200                                  continue;                                  continue;
201                  } else {                  } else {
202                          if (!is_ipv6 ||                          if (!is_ipv6 ||
203                              memcmp(acl->u.ipv6.min, address, 16) > 0 ||                              memcmp(acl->address.ipv6.min, address, 16) > 0 ||
204                              memcmp(address, acl->u.ipv6.max, 16) > 0)                              memcmp(address, acl->address.ipv6.max, 16) > 0)
205                                  continue;                                  continue;
206                  }                  }
207                  r.cond = ccs_get_condition_part(ptr);                  r.cond = ptr->cond;
208                  found = true;                  error = 0;
209                  break;                  break;
210          }          }
211          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
# Line 624  retry: Line 214  retry:
214                                 (const struct in6_addr *) address);                                 (const struct in6_addr *) address);
215          else          else
216                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
217          audit_network_log(&r, keyword, buf, port, found);          ccs_audit_network_log(&r, keyword, buf, port, !error);
218          if (found)          if (error)
219                  return 0;                  error = ccs_check_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK
220          if (ccs_verbose_mode(r.domain))                                               "%s %s %u\n", keyword, buf, port);
221                  printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",          if (error == 1)
222                         ccs_get_msg(is_enforce), keyword, buf, port,                  goto retry;
223                         ccs_get_last_name(r.domain));          if (!is_enforce)
224          if (is_enforce) {                  error = 0;
225                  int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK          return error;
226                                                   "%s %s %u\n", keyword, buf,  }
227                                                   port);  
228                  if (error == 1)  /**
229                          goto retry;   * ccs_check_network_entry - Check permission for network operation.
230                  return error;   *
231          }   * @is_ipv6:   True if @address is an IPv6 address.
232          if (r.mode == 1 && ccs_check_domain_quota(r.domain))   * @operation: Type of operation.
233                  update_network_entry(operation, is_ipv6 ?   * @address:   An IPv4 or IPv6 address.
234                                       IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4,   * @port:      Port number.
235                                       NULL, address, address, port, port,   *
236                                       r.domain, ccs_handler_cond(), 0);   * Returns 0 on success, negative value otherwise.
237          return 0;   */
238    static int ccs_check_network_entry(const bool is_ipv6, const u8 operation,
239                                       const u32 *address, const u16 port)
240    {
241            const int idx = ccs_read_lock();
242            const int error = ccs_check_network_entry2(is_ipv6, operation,
243                                                       address, port);
244            ccs_read_unlock(idx);
245            return error;
246  }  }
247    
248  /**  /**
249   * ccs_write_network_policy - Write "struct ip_network_acl_record" list.   * ccs_write_network_policy - Write "struct ccs_ip_network_acl" list.
250   *   *
251   * @data:      String to parse.   * @data:      String to parse.
252   * @domain:    Pointer to "struct domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
253   * @condition: Pointer to "struct condition_list". May be NULL.   * @condition: Pointer to "struct ccs_condition". May be NULL.
254   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
255   *   *
256   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
257   */   */
258  int ccs_write_network_policy(char *data, struct domain_info *domain,  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,
259                               const struct condition_list *condition,                               struct ccs_condition *condition,
260                               const bool is_delete)                               const bool is_delete)
261  {  {
262          u8 sock_type;          struct ccs_ip_network_acl *entry = NULL;
263          u8 operation;          struct ccs_acl_info *ptr;
264          u8 record_type;          struct ccs_ip_network_acl e = {
265                    .head.type = CCS_TYPE_IP_NETWORK_ACL,
266                    .head.cond = condition,
267            };
268          u16 min_address[8];          u16 min_address[8];
269          u16 max_address[8];          u16 max_address[8];
270          struct address_group_entry *group = NULL;          int error = is_delete ? -ENOENT : -ENOMEM;
271          u16 min_port;          u8 sock_type;
272          u16 max_port;          char *w[4];
273          u8 count;          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
274          char *cp1 = strchr(data, ' ');                  return -EINVAL;
275          char *cp2;          if (!strcmp(w[0], "TCP"))
         if (!cp1)  
                 goto out;  
         cp1++;  
         if (!strncmp(data, "TCP ", 4))  
276                  sock_type = SOCK_STREAM;                  sock_type = SOCK_STREAM;
277          else if (!strncmp(data, "UDP ", 4))          else if (!strcmp(w[0], "UDP"))
278                  sock_type = SOCK_DGRAM;                  sock_type = SOCK_DGRAM;
279          else if (!strncmp(data, "RAW ", 4))          else if (!strcmp(w[0], "RAW"))
280                  sock_type = SOCK_RAW;                  sock_type = SOCK_RAW;
281          else          else
282                  goto out;                  return -EINVAL;
283          cp2 = strchr(cp1, ' ');          if (!strcmp(w[1], "bind"))
         if (!cp2)  
                 goto out;  
         cp2++;  
         if (!strncmp(cp1, "bind ", 5))  
284                  switch (sock_type) {                  switch (sock_type) {
285                  case SOCK_STREAM:                  case SOCK_STREAM:
286                          operation = NETWORK_ACL_TCP_BIND;                          e.perm = 1 << CCS_NETWORK_TCP_BIND;
287                          break;                          break;
288                  case SOCK_DGRAM:                  case SOCK_DGRAM:
289                          operation = NETWORK_ACL_UDP_BIND;                          e.perm = 1 << CCS_NETWORK_UDP_BIND;
290                          break;                          break;
291                  default:                  default:
292                          operation = NETWORK_ACL_RAW_BIND;                          e.perm = 1 << CCS_NETWORK_RAW_BIND;
293                            break;
294                  }                  }
295          else if (!strncmp(cp1, "connect ", 8))          else if (!strcmp(w[1], "connect"))
296                  switch (sock_type) {                  switch (sock_type) {
297                  case SOCK_STREAM:                  case SOCK_STREAM:
298                          operation = NETWORK_ACL_TCP_CONNECT;                          e.perm = 1 << CCS_NETWORK_TCP_CONNECT;
299                          break;                          break;
300                  case SOCK_DGRAM:                  case SOCK_DGRAM:
301                          operation = NETWORK_ACL_UDP_CONNECT;                          e.perm = 1 << CCS_NETWORK_UDP_CONNECT;
302                          break;                          break;
303                  default:                  default:
304                          operation = NETWORK_ACL_RAW_CONNECT;                          e.perm = 1 << CCS_NETWORK_RAW_CONNECT;
305                            break;
306                  }                  }
307          else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))
308                  operation = NETWORK_ACL_TCP_LISTEN;                  e.perm = 1 << CCS_NETWORK_TCP_LISTEN;
309          else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))
310                  operation = NETWORK_ACL_TCP_ACCEPT;                  e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;
311          else          else
312                  goto out;                  return -EINVAL;
313          cp1 = strchr(cp2, ' ');          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
         if (!cp1)  
                 goto out;  
         *cp1++ = '\0';  
         switch (parse_ip_address(cp2, min_address, max_address)) {  
314          case 2:          case 2:
315                  record_type = IP_RECORD_TYPE_IPv6;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
316                    e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
317                                                              min_address);
318                    e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
319                                                              max_address);
320                    if (!e.address.ipv6.min || !e.address.ipv6.max)
321                            goto out;
322                  break;                  break;
323          case 1:          case 1:
324                  record_type = IP_RECORD_TYPE_IPv4;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
325                    /* use host byte order to allow u32 comparison.*/
326                    e.address.ipv4.min = ntohl(* (u32 *) min_address);
327                    e.address.ipv4.max = ntohl(* (u32 *) max_address);
328                  break;                  break;
329          default:          default:
330                  if (*cp2 != '@')                  if (w[2][0] != '@')
331                          goto out;                          return -EINVAL;
332                  group = find_or_assign_new_address_group(cp2 + 1);                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
333                  if (!group)                  e.address.group = ccs_get_address_group(w[2] + 1);
334                    if (!e.address.group)
335                          return -ENOMEM;                          return -ENOMEM;
                 record_type = IP_RECORD_TYPE_ADDRESS_GROUP;  
336                  break;                  break;
337          }          }
338          if (strchr(cp1, ' '))          if (!ccs_parse_number_union(w[3], &e.port))
339                  goto out;                  goto out;
340          count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);          if (!is_delete)
341          if (count != 1 && count != 2)                  entry = kmalloc(sizeof(e), GFP_KERNEL);
342                  goto out;          mutex_lock(&ccs_policy_lock);
343          if (count == 1)          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
344                  max_port = min_port;                  struct ccs_ip_network_acl *acl =
345          return update_network_entry(operation, record_type, group,                          container_of(ptr, struct ccs_ip_network_acl,
346                                      (u32 *) min_address, (u32 *) max_address,                                       head);
347                                      min_port, max_port, domain, condition,                  if (ptr->type != CCS_TYPE_IP_NETWORK_ACL ||
348                                      is_delete);                      ptr->cond != condition ||
349                        ccs_memcmp(acl, &e, offsetof(typeof(e), address_type),
350                                   sizeof(e)))
351                            continue;
352                    if (is_delete) {
353                            acl->perm &= ~e.perm;
354                            if (!acl->perm)
355                                    ptr->is_deleted = true;
356                    } else {
357                            if (ptr->is_deleted)
358                                    acl->perm = 0;
359                            acl->perm |= e.perm;
360                            ptr->is_deleted = false;
361                    }
362                    error = 0;
363                    break;
364            }
365            if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {
366                    ccs_add_domain_acl(domain, &entry->head);
367                    entry = NULL;
368                    error = 0;
369            }
370            mutex_unlock(&ccs_policy_lock);
371   out:   out:
372          return -EINVAL;          if (w[2][0] == '@')
373                    ccs_put_address_group(e.address.group);
374            else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
375                    ccs_put_ipv6_address(e.address.ipv6.min);
376                    ccs_put_ipv6_address(e.address.ipv6.max);
377            }
378            ccs_put_number_union(&e.port);
379            kfree(entry);
380            return error;
381  }  }
382    
383  /**  /**
# Line 759  int ccs_write_network_policy(char *data, Line 389  int ccs_write_network_policy(char *data,
389   *   *
390   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
391   */   */
392  int ccs_check_network_listen_acl(const _Bool is_ipv6, const u8 *address,  static inline int ccs_check_network_listen_acl(const bool is_ipv6,
393                                   const u16 port)                                                 const u8 *address,
394                                                   const u16 port)
395  {  {
396          return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,          return ccs_check_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN,
397                                     (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
398  }  }
399    
400  /**  /**
# Line 776  int ccs_check_network_listen_acl(const _ Line 407  int ccs_check_network_listen_acl(const _
407   *   *
408   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
409   */   */
410  int ccs_check_network_connect_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_connect_acl(const bool is_ipv6,
411                                    const u8 *address, const u16 port)                                                  const int sock_type,
412                                                    const u8 *address,
413                                                    const u16 port)
414  {  {
415          u8 operation;          u8 operation;
416          switch (sock_type) {          switch (sock_type) {
417          case SOCK_STREAM:          case SOCK_STREAM:
418                  operation = NETWORK_ACL_TCP_CONNECT;                  operation = CCS_NETWORK_TCP_CONNECT;
419                  break;                  break;
420          case SOCK_DGRAM:          case SOCK_DGRAM:
421                  operation = NETWORK_ACL_UDP_CONNECT;                  operation = CCS_NETWORK_UDP_CONNECT;
422                  break;                  break;
423          default:          default:
424                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = CCS_NETWORK_RAW_CONNECT;
425          }          }
426          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
427                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
428  }  }
429    
430  /**  /**
# Line 804  int ccs_check_network_connect_acl(const Line 437  int ccs_check_network_connect_acl(const
437   *   *
438   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
439   */   */
440  int ccs_check_network_bind_acl(const _Bool is_ipv6, const int sock_type,  static int ccs_check_network_bind_acl(const bool is_ipv6, const int sock_type,
441                                 const u8 *address, const u16 port)                                        const u8 *address, const u16 port)
442  {  {
443          u8 operation;          u8 operation;
444          switch (sock_type) {          switch (sock_type) {
445          case SOCK_STREAM:          case SOCK_STREAM:
446                  operation = NETWORK_ACL_TCP_BIND;                  operation = CCS_NETWORK_TCP_BIND;
447                  break;                  break;
448          case SOCK_DGRAM:          case SOCK_DGRAM:
449                  operation = NETWORK_ACL_UDP_BIND;                  operation = CCS_NETWORK_UDP_BIND;
450                  break;                  break;
451          default:          default:
452                  operation = NETWORK_ACL_RAW_BIND;                  operation = CCS_NETWORK_RAW_BIND;
453          }          }
454          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
455                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
456  }  }
457    
458  /**  /**
# Line 831  int ccs_check_network_bind_acl(const _Bo Line 464  int ccs_check_network_bind_acl(const _Bo
464   *   *
465   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
466   */   */
467  int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address,  static inline int ccs_check_network_accept_acl(const bool is_ipv6,
468                                   const u16 port)                                                 const u8 *address,
469                                                   const u16 port)
470  {  {
471          int retval;          int retval;
472          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
473          retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,          retval = ccs_check_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT,
474                                       (const u32 *) address, ntohs(port));                                           (const u32 *) address, ntohs(port));
475          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
476          return retval;          return retval;
477  }  }
478    
# Line 852  int ccs_check_network_accept_acl(const _ Line 486  int ccs_check_network_accept_acl(const _
486   *   *
487   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
488   */   */
489  int ccs_check_network_sendmsg_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6,
490                                    const u8 *address, const u16 port)                                                  const int sock_type,
491                                                    const u8 *address,
492                                                    const u16 port)
493  {  {
494          u8 operation;          u8 operation;
495          if (sock_type == SOCK_DGRAM)          if (sock_type == SOCK_DGRAM)
496                  operation = NETWORK_ACL_UDP_CONNECT;                  operation = CCS_NETWORK_UDP_CONNECT;
497          else          else
498                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = CCS_NETWORK_RAW_CONNECT;
499          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
500                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
501  }  }
502    
503  /**  /**
# Line 874  int ccs_check_network_sendmsg_acl(const Line 510  int ccs_check_network_sendmsg_acl(const
510   *   *
511   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
512   */   */
513  int ccs_check_network_recvmsg_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6,
514                                    const u8 *address, const u16 port)                                                  const int sock_type,
515                                                    const u8 *address,
516                                                    const u16 port)
517  {  {
518          int retval;          int retval;
519          const u8 operation          const u8 operation
520                  = (sock_type == SOCK_DGRAM) ?                  = (sock_type == SOCK_DGRAM) ?
521                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                  CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT;
522          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
523          retval = check_network_entry(is_ipv6, operation, (const u32 *) address,          retval = ccs_check_network_entry(is_ipv6, operation,
524                                       ntohs(port));                                           (const u32 *) address, ntohs(port));
525          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
526          return retval;          return retval;
527  }  }
528    
529    #define MAX_SOCK_ADDR 128 /* net/socket.c */
530    
531    /* Check permission for creating a socket. */
532    int ccs_socket_create_permission(int family, int type, int protocol)
533    {
534            int error = 0;
535            /* Nothing to do if I am a kernel service. */
536            if (segment_eq(get_fs(), KERNEL_DS))
537                    return 0;
538            if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
539                    return -EPERM;
540            if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
541                    return -EPERM;
542            if (family != PF_INET && family != PF_INET6)
543                    return 0;
544            switch (type) {
545            case SOCK_STREAM:
546                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE))
547                            error = -EPERM;
548                    break;
549            case SOCK_DGRAM:
550                    if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET))
551                            error = -EPERM;
552                    break;
553            case SOCK_RAW:
554                    if (!ccs_capable(CCS_USE_INET_RAW_SOCKET))
555                            error = -EPERM;
556                    break;
557            }
558            return error;
559    }
560    
561    /* Check permission for listening a TCP socket. */
562    int ccs_socket_listen_permission(struct socket *sock)
563    {
564            int error = 0;
565            char addr[MAX_SOCK_ADDR];
566            int addr_len;
567            /* Nothing to do if I am a kernel service. */
568            if (segment_eq(get_fs(), KERNEL_DS))
569                    return 0;
570            if (sock->type != SOCK_STREAM)
571                    return 0;
572            switch (sock->sk->sk_family) {
573            case PF_INET:
574            case PF_INET6:
575                    break;
576            default:
577                    return 0;
578            }
579            if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))
580                    return -EPERM;
581            if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))
582                    return -EPERM;
583            switch (((struct sockaddr *) addr)->sa_family) {
584                    struct sockaddr_in6 *addr6;
585                    struct sockaddr_in *addr4;
586            case AF_INET6:
587                    addr6 = (struct sockaddr_in6 *) addr;
588                    error = ccs_check_network_listen_acl(true,
589                                                         addr6->sin6_addr.s6_addr,
590                                                         addr6->sin6_port);
591                    break;
592            case AF_INET:
593                    addr4 = (struct sockaddr_in *) addr;
594                    error = ccs_check_network_listen_acl(false,
595                                                         (u8 *) &addr4->sin_addr,
596                                                         addr4->sin_port);
597                    break;
598            }
599            return error;
600    }
601    
602    /* Check permission for setting the remote IP address/port pair of a socket. */
603    int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr,
604                                      int addr_len)
605    {
606            int error = 0;
607            const unsigned int type = sock->type;
608            /* Nothing to do if I am a kernel service. */
609            if (segment_eq(get_fs(), KERNEL_DS))
610                    return 0;
611            switch (type) {
612            case SOCK_STREAM:
613            case SOCK_DGRAM:
614            case SOCK_RAW:
615                    break;
616            default:
617                    return 0;
618            }
619            switch (addr->sa_family) {
620                    struct sockaddr_in6 *addr6;
621                    struct sockaddr_in *addr4;
622                    u16 port;
623            case AF_INET6:
624                    if (addr_len < SIN6_LEN_RFC2133)
625                            break;
626                    addr6 = (struct sockaddr_in6 *) addr;
627                    if (type != SOCK_RAW)
628                            port = addr6->sin6_port;
629                    else
630                            port = htons(sock->sk->sk_protocol);
631                    error = ccs_check_network_connect_acl(true, type,
632                                                          addr6->sin6_addr.s6_addr,
633                                                          port);
634                    break;
635            case AF_INET:
636                    if (addr_len < sizeof(struct sockaddr_in))
637                            break;
638                    addr4 = (struct sockaddr_in *) addr;
639                    if (type != SOCK_RAW)
640                            port = addr4->sin_port;
641                    else
642                            port = htons(sock->sk->sk_protocol);
643                    error = ccs_check_network_connect_acl(false, type,
644                                                          (u8 *) &addr4->sin_addr,
645                                                          port);
646                    break;
647            }
648            if (type != SOCK_STREAM)
649                    return error;
650            switch (sock->sk->sk_family) {
651            case PF_INET:
652            case PF_INET6:
653                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))
654                            error = -EPERM;
655                    break;
656            }
657            return error;
658    }
659    
660    /* Check permission for setting the local IP address/port pair of a socket. */
661    int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
662                                   int addr_len)
663    {
664            int error = 0;
665            const unsigned int type = sock->type;
666            /* Nothing to do if I am a kernel service. */
667            if (segment_eq(get_fs(), KERNEL_DS))
668                    return 0;
669            switch (type) {
670            case SOCK_STREAM:
671            case SOCK_DGRAM:
672            case SOCK_RAW:
673                    break;
674            default:
675                    return 0;
676            }
677            switch (addr->sa_family) {
678                    struct sockaddr_in6 *addr6;
679                    struct sockaddr_in *addr4;
680                    u16 port;
681            case AF_INET6:
682                    if (addr_len < SIN6_LEN_RFC2133)
683                            break;
684                    addr6 = (struct sockaddr_in6 *) addr;
685                    if (type != SOCK_RAW)
686                            port = addr6->sin6_port;
687                    else
688                            port = htons(sock->sk->sk_protocol);
689                    error = ccs_check_network_bind_acl(true, type,
690                                                       addr6->sin6_addr.s6_addr,
691                                                       port);
692                    break;
693            case AF_INET:
694                    if (addr_len < sizeof(struct sockaddr_in))
695                            break;
696                    addr4 = (struct sockaddr_in *) addr;
697                    if (type != SOCK_RAW)
698                            port = addr4->sin_port;
699                    else
700                            port = htons(sock->sk->sk_protocol);
701                    error = ccs_check_network_bind_acl(false, type,
702                                                       (u8 *) &addr4->sin_addr,
703                                                       port);
704                    break;
705            }
706            return error;
707    }
708    
709    /*
710     * Check permission for accepting a TCP socket.
711     *
712     * Currently, the LSM hook for this purpose is not provided.
713     */
714    int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr)
715    {
716            int error = 0;
717            int addr_len;
718            /* Nothing to do if I am a kernel service. */
719            if (segment_eq(get_fs(), KERNEL_DS))
720                    return 0;
721            switch (sock->sk->sk_family) {
722            case PF_INET:
723            case PF_INET6:
724                    break;
725            default:
726                    return 0;
727            }
728            error = sock->ops->getname(sock, addr, &addr_len, 2);
729            if (error)
730                    return error;
731            switch (addr->sa_family) {
732                    struct sockaddr_in6 *addr6;
733                    struct sockaddr_in *addr4;
734            case AF_INET6:
735                    addr6 = (struct sockaddr_in6 *) addr;
736                    error = ccs_check_network_accept_acl(true,
737                                                         addr6->sin6_addr.s6_addr,
738                                                         addr6->sin6_port);
739                    break;
740            case AF_INET:
741                    addr4 = (struct sockaddr_in *) addr;
742                    error = ccs_check_network_accept_acl(false,
743                                                         (u8 *) &addr4->sin_addr,
744                                                         addr4->sin_port);
745                    break;
746            }
747            return error;
748    }
749    
750    /* Check permission for sending a datagram via a UDP or RAW socket. */
751    int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr,
752                                      int addr_len)
753    {
754            int error = 0;
755            const int type = sock->type;
756            /* Nothing to do if I am a kernel service. */
757            if (segment_eq(get_fs(), KERNEL_DS))
758                    return 0;
759            if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))
760                    return 0;
761            switch (addr->sa_family) {
762                    struct sockaddr_in6 *addr6;
763                    struct sockaddr_in *addr4;
764                    u16 port;
765            case AF_INET6:
766                    if (addr_len < SIN6_LEN_RFC2133)
767                            break;
768                    addr6 = (struct sockaddr_in6 *) addr;
769                    if (type == SOCK_DGRAM)
770                            port = addr6->sin6_port;
771                    else
772                            port = htons(sock->sk->sk_protocol);
773                    error = ccs_check_network_sendmsg_acl(true, type,
774                                                          addr6->sin6_addr.s6_addr,
775                                                          port);
776                    break;
777            case AF_INET:
778                    if (addr_len < sizeof(struct sockaddr_in))
779                            break;
780                    addr4 = (struct sockaddr_in *) addr;
781                    if (type == SOCK_DGRAM)
782                            port = addr4->sin_port;
783                    else
784                            port = htons(sock->sk->sk_protocol);
785                    error = ccs_check_network_sendmsg_acl(false, type,
786                                                          (u8 *) &addr4->sin_addr,
787                                                          port);
788                    break;
789            }
790            return error;
791    }
792    
793    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
794    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
795    
796    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
797    {
798            return skb->nh.iph;
799    }
800    
801    static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
802    {
803            return skb->h.uh;
804    }
805    
806    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
807    {
808            return skb->nh.ipv6h;
809    }
810    
811    #endif
812    #endif
813    
814    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
815    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
816                                  unsigned int flags)
817    {
818            /* Clear queue. */
819            if (flags & MSG_PEEK) {
820                    int clear = 0;
821                    spin_lock_irq(&sk->sk_receive_queue.lock);
822                    if (skb == skb_peek(&sk->sk_receive_queue)) {
823                            __skb_unlink(skb, &sk->sk_receive_queue);
824                            clear = 1;
825                    }
826                    spin_unlock_irq(&sk->sk_receive_queue.lock);
827                    if (clear)
828                            kfree_skb(skb);
829            }
830            skb_free_datagram(sk, skb);
831    }
832    #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
833    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
834                                  unsigned int flags)
835    {
836            /* Clear queue. */
837            if (flags & MSG_PEEK) {
838                    int clear = 0;
839                    spin_lock_bh(&sk->sk_receive_queue.lock);
840                    if (skb == skb_peek(&sk->sk_receive_queue)) {
841                            __skb_unlink(skb, &sk->sk_receive_queue);
842                            clear = 1;
843                    }
844                    spin_unlock_bh(&sk->sk_receive_queue.lock);
845                    if (clear)
846                            kfree_skb(skb);
847            }
848            skb_free_datagram(sk, skb);
849    }
850    #endif
851    
852    /*
853     * Check permission for receiving a datagram via a UDP or RAW socket.
854     *
855     * Currently, the LSM hook for this purpose is not provided.
856     */
857    int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb,
858                                      const unsigned int flags)
859    {
860            int error = 0;
861            const unsigned int type = sk->sk_type;
862            if (type != SOCK_DGRAM && type != SOCK_RAW)
863                    return 0;
864            /* Nothing to do if I am a kernel service. */
865            if (segment_eq(get_fs(), KERNEL_DS))
866                    return 0;
867    
868            switch (sk->sk_family) {
869                    struct in6_addr sin6;
870                    struct in_addr sin4;
871                    u16 port;
872            case PF_INET6:
873                    if (type == SOCK_DGRAM) { /* UDP IPv6 */
874                            if (skb->protocol == htons(ETH_P_IP)) {
875                                    ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),
876                                                  ip_hdr(skb)->saddr);
877                            } else {
878                                    ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
879                            }
880                            port = udp_hdr(skb)->source;
881                    } else { /* RAW IPv6 */
882                            ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
883                            port = htons(sk->sk_protocol);
884                    }
885                    error = ccs_check_network_recvmsg_acl(true, type,
886                                                          (u8 *) &sin6, port);
887                    break;
888            case PF_INET:
889                    if (type == SOCK_DGRAM) { /* UDP IPv4 */
890                            sin4.s_addr = ip_hdr(skb)->saddr;
891                            port = udp_hdr(skb)->source;
892                    } else { /* RAW IPv4 */
893                            sin4.s_addr = ip_hdr(skb)->saddr;
894                            port = htons(sk->sk_protocol);
895                    }
896                    error = ccs_check_network_recvmsg_acl(false, type,
897                                                          (u8 *) &sin4, port);
898                    break;
899            }
900            if (!error)
901                    return 0;
902            /*
903             * Remove from queue if MSG_PEEK is used so that
904             * the head message from unwanted source in receive queue will not
905             * prevent the caller from picking up next message from wanted source
906             * when the caller is using MSG_PEEK flag for picking up.
907             */
908    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
909            if (type == SOCK_DGRAM)
910                    lock_sock(sk);
911    #endif
912            skb_kill_datagram(sk, skb, flags);
913    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
914            if (type == SOCK_DGRAM)
915                    release_sock(sk);
916    #endif
917            /* Hope less harmful than -EPERM. */
918            return -ENOMEM;
919    }
920    EXPORT_SYMBOL(ccs_socket_recvmsg_permission);

Legend:
Removed from v.1886  
changed lines
  Added in v.2918

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