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

Subversion リポジトリの参照

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

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

branches/ccs-patch/fs/tomoyo_network.c revision 2570 by kumaneko, Wed May 20 07:12:32 2009 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
  *  
  * Implementation of the Domain-Based Mandatory Access Control.  
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2009  NTT DATA CORPORATION
5   *   *
6   * Version: 1.6.8-pre   2009/05/08   * Version: 1.7.0-pre   2009/08/08
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/tomoyo_socket.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>
# Line 23  Line 17 
17  #include <net/ip.h>  #include <net/ip.h>
18  #include <net/ipv6.h>  #include <net/ipv6.h>
19  #include <net/udp.h>  #include <net/udp.h>
20    #include "internal.h"
 /* Index numbers for Network Controls. */  
 enum ccs_network_acl_index {  
         NETWORK_ACL_UDP_BIND,  
         NETWORK_ACL_UDP_CONNECT,  
         NETWORK_ACL_TCP_BIND,  
         NETWORK_ACL_TCP_LISTEN,  
         NETWORK_ACL_TCP_CONNECT,  
         NETWORK_ACL_TCP_ACCEPT,  
         NETWORK_ACL_RAW_BIND,  
         NETWORK_ACL_RAW_CONNECT  
 };  
21    
22  /**  /**
23   * ccs_audit_network_log - Audit network log.   * ccs_audit_network_log - Audit network log.
# Line 51  static int ccs_audit_network_log(struct Line 34  static int ccs_audit_network_log(struct
34                                   const char *operation, const char *address,                                   const char *operation, const char *address,
35                                   const u16 port, 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    
 /* The list for "struct ccs_address_group_entry". */  
 LIST_HEAD(ccs_address_group_list);  
   
 /**  
  * ccs_get_address_group - Allocate memory for "struct ccs_address_group_entry".  
  *  
  * @group_name: The name of address group.  
  *  
  * Returns pointer to "struct ccs_address_group_entry" on success,  
  * NULL otherwise.  
  */  
 static struct ccs_address_group_entry *ccs_get_address_group(const char *  
                                                              group_name)  
 {  
         struct ccs_address_group_entry *entry = NULL;  
         struct ccs_address_group_entry *group;  
         const struct ccs_path_info *saved_group_name;  
         int error = -ENOMEM;  
         if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||  
             !group_name[0])  
                 return NULL;  
         saved_group_name = ccs_get_name(group_name);  
         if (!saved_group_name)  
                 return NULL;  
         entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         /***** WRITER SECTION START *****/  
         down_write(&ccs_policy_lock);  
         list_for_each_entry(group, &ccs_address_group_list, list) {  
                 if (saved_group_name != group->group_name)  
                         continue;  
                 atomic_inc(&group->users);  
                 error = 0;  
                 break;  
         }  
         if (error && ccs_memory_ok(entry)) {  
                 INIT_LIST_HEAD(&entry->address_group_member_list);  
                 entry->group_name = saved_group_name;  
                 saved_group_name = NULL;  
                 atomic_set(&entry->users, 1);  
                 list_add_tail(&entry->list, &ccs_address_group_list);  
                 group = entry;  
                 entry = NULL;  
                 error = 0;  
         }  
         up_write(&ccs_policy_lock);  
         /***** WRITER SECTION END *****/  
         ccs_put_name(saved_group_name);  
         kfree(entry);  
         return !error ? group : NULL;  
 }  
   
 /**  
  * ccs_update_address_group_entry - Update "struct ccs_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 ccs_update_address_group_entry(const char *group_name,  
                                           const bool is_ipv6,  
                                           const u16 *min_address,  
                                           const u16 *max_address,  
                                           const bool is_delete)  
 {  
         struct ccs_address_group_entry *group;  
         struct ccs_address_group_member *entry = NULL;  
         struct ccs_address_group_member *member;  
         const struct in6_addr *saved_min_address = NULL;  
         const struct in6_addr *saved_max_address = NULL;  
         int error = is_delete ? -ENOENT : -ENOMEM;  
         const u32 min_ipv4_address = ntohl(*(u32 *) min_address);  
         const u32 max_ipv4_address = ntohl(*(u32 *) max_address);  
         group = ccs_get_address_group(group_name);  
         if (!group)  
                 return -ENOMEM;  
         if (is_ipv6) {  
                 saved_min_address  
                         = ccs_get_ipv6_address((struct in6_addr *)  
                                                min_address);  
                 saved_max_address  
                         = ccs_get_ipv6_address((struct in6_addr *)  
                                                max_address);  
                 if (!saved_min_address || !saved_max_address)  
                         goto out;  
         }  
         if (!is_delete)  
                 entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         /***** WRITER SECTION START *****/  
         down_write(&ccs_policy_lock);  
         list_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 != min_ipv4_address ||  
                             member->max.ipv4 != max_ipv4_address)  
                                 continue;  
                 }  
                 member->is_deleted = is_delete;  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_memory_ok(entry)) {  
                 entry->is_ipv6 = is_ipv6;  
                 if (is_ipv6) {  
                         entry->min.ipv6 = saved_min_address;  
                         saved_min_address = NULL;  
                         entry->max.ipv6 = saved_max_address;  
                         saved_max_address = NULL;  
                 } else {  
                         entry->min.ipv4 = min_ipv4_address;  
                         entry->max.ipv4 = max_ipv4_address;  
                 }  
                 list_add_tail(&entry->list, &group->address_group_member_list);  
                 entry = NULL;  
                 atomic_inc(&group->users);  
                 error = 0;  
         }  
         up_write(&ccs_policy_lock);  
         /***** WRITER SECTION END *****/  
  out:  
         ccs_put_ipv6_address(saved_min_address);  
         ccs_put_ipv6_address(saved_max_address);  
         ccs_put_address_group(group);  
         ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);  
         return error;  
 }  
   
45  /**  /**
46   * ccs_parse_ip_address - Parse an IP address.   * ccs_parse_ip_address - Parse an IP address.
47   *   *
# Line 199  static int ccs_update_address_group_entr Line 51  static int ccs_update_address_group_entr
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 ccs_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 233  static int ccs_parse_ip_address(char *ad Line 85  static int ccs_parse_ip_address(char *ad
85          return 0;          return 0;
86  }  }
87    
 /**  
  * ccs_write_address_group_policy - Write "struct ccs_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 (ccs_parse_ip_address(cp, min_address, max_address)) {  
         case 2:  
                 is_ipv6 = true;  
                 break;  
         case 1:  
                 is_ipv6 = false;  
                 break;  
         default:  
                 return -EINVAL;  
         }  
         return ccs_update_address_group_entry(data, is_ipv6, min_address,  
                                               max_address, is_delete);  
 }  
   
 /**  
  * ccs_address_matches_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 ccs_address_group_entry".  
  *  
  * Returns true if @address matches addresses in @group group, false otherwise.  
  *  
  * Caller holds ccs_policy_lockfor reading.  
  */  
 static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address,  
                                       const struct ccs_address_group_entry *  
                                       group)  
 {  
         struct ccs_address_group_member *member;  
         const u32 ip = ntohl(*address);  
         bool matched = false;  
         list_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) {  
                                 matched = true;  
                                 break;  
                         }  
                 } else {  
                         if (!is_ipv6 &&  
                             member->min.ipv4 <= ip && ip <= member->max.ipv4) {  
                                 matched = true;  
                                 break;  
                         }  
                 }  
         }  
         return matched;  
 }  
   
 /**  
  * ccs_read_address_group_policy - Read "struct ccs_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 list_head *gpos;  
         struct list_head *mpos;  
         bool done = true;  
         /***** READER SECTION START *****/  
         down_read(&ccs_policy_lock);  
         list_for_each_cookie(gpos, head->read_var1.u.list,  
                               &ccs_address_group_list) {  
                 struct ccs_address_group_entry *group;  
                 group = list_entry(gpos, struct ccs_address_group_entry, list);  
                 list_for_each_cookie(mpos, head->read_var2.u.list,  
                                       &group->address_group_member_list) {  
                         char buf[128];  
                         struct ccs_address_group_member *member;  
                         member = list_entry(mpos,  
                                              struct ccs_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));  
                                 }  
                         }  
                         done = ccs_io_printf(head, KEYWORD_ADDRESS_GROUP  
                                              "%s %s\n", group->group_name->name,  
                                              buf);  
                         if (!done)  
                                 break;  
                 }  
                 if (!done)  
                         break;  
         }  
         up_read(&ccs_policy_lock);  
         /***** READER SECTION END *****/  
         return done;  
 }  
   
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 407  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 436  const char *ccs_net2keyword(const u8 ope Line 149  const char *ccs_net2keyword(const u8 ope
149  }  }
150    
151  /**  /**
152   * ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list.   * ccs_check_network_entry2 - Check permission for network operation.
  *  
  * @operation:   Type of operation.  
  * @record_type: Type of address.  
  * @group:       Name of group. 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 ccs_domain_info".  
  * @condition:   Pointer to "struct ccs_condition_list". May be NULL.  
  * @is_delete:   True if it is a delete request.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_update_network_entry(const u8 operation, const u8 record_type,  
                                     const char *group_name,  
                                     const u32 *min_address,  
                                     const u32 *max_address,  
                                     const u16 min_port, const u16 max_port,  
                                     struct ccs_domain_info *domain,  
                                     struct ccs_condition_list *condition,  
                                     const bool is_delete)  
 {  
         struct ccs_ip_network_acl_record *entry = NULL;  
         struct ccs_acl_info *ptr;  
         int error = is_delete ? -ENOENT : -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;  
         struct ccs_address_group_entry *group = NULL;  
         if (!domain)  
                 return -EINVAL;  
         if (group_name) {  
                 group = ccs_get_address_group(group_name);  
                 if (!group)  
                         return -ENOMEM;  
         } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                 saved_min_address = ccs_get_ipv6_address((struct in6_addr *)  
                                                          min_address);  
                 saved_max_address = ccs_get_ipv6_address((struct in6_addr *)  
                                                          max_address);  
                 if (!saved_min_address || !saved_max_address)  
                         goto out;  
         }  
         if (is_delete)  
                 goto delete;  
         entry = kzalloc(sizeof(*entry), GFP_KERNEL);  
         /***** WRITER SECTION START *****/  
         down_write(&ccs_policy_lock);  
         list_for_each_entry(ptr, &domain->acl_info_list, list) {  
                 struct ccs_ip_network_acl_record *acl;  
                 if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)  
                         continue;  
                 if (ptr->cond != condition)  
                         continue;  
                 acl = container_of(ptr, struct ccs_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);  
                 break;  
         }  
         if (error && ccs_memory_ok(entry)) {  
                 entry->head.type = TYPE_IP_NETWORK_ACL;  
                 entry->head.cond = condition;  
                 entry->operation_type = operation;  
                 entry->record_type = record_type;  
                 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                         entry->u.group = group;  
                         group = NULL;  
                 } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                         entry->u.ipv4.min = min_ip;  
                         entry->u.ipv4.max = max_ip;  
                 } else {  
                         entry->u.ipv6.min = saved_min_address;  
                         saved_min_address = NULL;  
                         entry->u.ipv6.max = saved_max_address;  
                         saved_max_address = NULL;  
                 }  
                 entry->min_port = min_port;  
                 entry->max_port = max_port;  
                 error = ccs_add_domain_acl(domain, &entry->head);  
                 entry = NULL;  
         }  
         up_write(&ccs_policy_lock);  
         /***** WRITER SECTION END *****/  
         goto out;  
  delete:  
         /***** WRITER SECTION START *****/  
         down_write(&ccs_policy_lock);  
         list_for_each_entry(ptr, &domain->acl_info_list, list) {  
                 struct ccs_ip_network_acl_record *acl;  
                 if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)  
                         continue;  
                 if (ptr->cond != condition)  
                         continue;  
                 acl = container_of(ptr, struct ccs_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;  
         }  
         up_write(&ccs_policy_lock);  
         /***** WRITER SECTION END *****/  
  out:  
         ccs_put_ipv6_address(saved_min_address);  
         ccs_put_ipv6_address(saved_max_address);  
         ccs_put_address_group(group);  
         kfree(entry);  
         return error;  
 }  
   
 /**  
  * ccs_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 587  static int ccs_update_network_entry(cons Line 157  static int ccs_update_network_entry(cons
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 ccs_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 ccs_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;
         int error = -EPERM;  
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_MAC_FOR_NETWORK);  
179          is_enforce = (r.mode == 3);          is_enforce = (r.mode == 3);
         if (!r.mode) {  
                 error = 0;  
                 goto done;  
         }  
180   retry:   retry:
181          /***** READER SECTION START *****/          error = -EPERM;
182          down_read(&ccs_policy_lock);          list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {
183          list_for_each_entry(ptr, &r.cookie.u.domain->acl_info_list, list) {                  struct ccs_ip_network_acl *acl;
184                  struct ccs_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 ccs_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 (!ccs_address_matches_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.condition_cookie.u.cond = ptr->cond;                  r.cond = ptr->cond;
208                  found = true;                  error = 0;
209                  break;                  break;
210          }          }
         up_read(&ccs_policy_lock);  
         /***** READER SECTION END *****/  
211          memset(buf, 0, sizeof(buf));          memset(buf, 0, sizeof(buf));
212          if (is_ipv6)          if (is_ipv6)
213                  ccs_print_ipv6(buf, sizeof(buf),                  ccs_print_ipv6(buf, sizeof(buf),
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          ccs_audit_network_log(&r, keyword, buf, port, found);          ccs_audit_network_log(&r, keyword, buf, port, !error);
218          if (found) {          if (error)
219                    error = ccs_check_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK
220                                                 "%s %s %u\n", keyword, buf, port);
221            if (error == 1)
222                    goto retry;
223            if (!is_enforce)
224                  error = 0;                  error = 0;
                 goto done;  
         }  
         if (ccs_verbose_mode(r.cookie.u.domain))  
                 printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",  
                        ccs_get_msg(is_enforce), keyword, buf, port,  
                        ccs_get_last_name(r.cookie.u.domain));  
         if (is_enforce) {  
                 int err = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK  
                                                "%s %s %u\n", keyword, buf,  
                                                port);  
                 if (err == 1)  
                         goto retry;  
                 error = err;  
                 goto done;  
         }  
         if (r.mode == 1 && ccs_domain_quota_ok(r.cookie.u.domain))  
                 ccs_update_network_entry(operation, is_ipv6 ?  
                                          IP_RECORD_TYPE_IPv6 :  
                                          IP_RECORD_TYPE_IPv4,  
                                          NULL, address, address, port, port,  
                                          r.cookie.u.domain, ccs_handler_cond(),  
                                          false);  
         error = 0;  
  done:  
         ccs_exit_request_info(&r);  
225          return error;          return error;
226  }  }
227    
228  /**  /**
229   * ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list.   * ccs_check_network_entry - Check permission for network operation.
230     *
231     * @is_ipv6:   True if @address is an IPv6 address.
232     * @operation: Type of operation.
233     * @address:   An IPv4 or IPv6 address.
234     * @port:      Port number.
235     *
236     * Returns 0 on success, negative value otherwise.
237     */
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 ccs_ip_network_acl" list.
250   *   *
251   * @data:      String to parse.   * @data:      String to parse.
252   * @domain:    Pointer to "struct ccs_domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
253   * @condition: Pointer to "struct ccs_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 ccs_domain_info *domain,  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,
259                               struct ccs_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          const char *group_name = 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 (ccs_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_name = cp2 + 1;                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
333                  record_type = IP_RECORD_TYPE_ADDRESS_GROUP;                  e.address.group = ccs_get_address_group(w[2] + 1);
334                    if (!e.address.group)
335                            return -ENOMEM;
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 ccs_update_network_entry(operation, record_type, group_name,                          container_of(ptr, struct ccs_ip_network_acl,
346                                          (u32 *) min_address,                                       head);
347                                          (u32 *) max_address,                  if (ptr->type != CCS_TYPE_IP_NETWORK_ACL ||
348                                          min_port, max_port, domain, condition,                      ptr->cond != condition ||
349                                          is_delete);                      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 791  static inline int ccs_check_network_list Line 393  static inline int ccs_check_network_list
393                                                 const u8 *address,                                                 const u8 *address,
394                                                 const u16 port)                                                 const u16 port)
395  {  {
396          return ccs_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    
# Line 813  static inline int ccs_check_network_conn Line 415  static inline int ccs_check_network_conn
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 ccs_check_network_entry(is_ipv6, operation,          return ccs_check_network_entry(is_ipv6, operation,
427                                         (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
# Line 841  static int ccs_check_network_bind_acl(co Line 443  static int ccs_check_network_bind_acl(co
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 ccs_check_network_entry(is_ipv6, operation,          return ccs_check_network_entry(is_ipv6, operation,
455                                         (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
# Line 868  static inline int ccs_check_network_acce Line 470  static inline int ccs_check_network_acce
470  {  {
471          int retval;          int retval;
472          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
473          retval = ccs_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->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
476          return retval;          return retval;
# Line 891  static inline int ccs_check_network_send Line 493  static inline int ccs_check_network_send
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 ccs_check_network_entry(is_ipv6, operation,          return ccs_check_network_entry(is_ipv6, operation,
500                                         (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
501  }  }
# Line 916  static inline int ccs_check_network_recv Line 518  static inline int ccs_check_network_recv
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->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
523          retval = ccs_check_network_entry(is_ipv6, operation,          retval = ccs_check_network_entry(is_ipv6, operation,
524                                           (const u32 *) address, ntohs(port));                                           (const u32 *) address, ntohs(port));

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

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