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

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

Legend:
Removed from v.776  
changed lines
  Added in v.2911

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