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

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

Legend:
Removed from v.1076  
changed lines
  Added in v.2944

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