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

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

Legend:
Removed from v.1782  
changed lines
  Added in v.3694

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