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

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 trunk/1.7.x/ccs-patch/security/ccsecurity/network.c revision 3625 by kumaneko, Wed May 5 01:00:40 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-2007  NTT DATA CORPORATION   * Version: 1.7.2   2010/04/01
  *  
  * 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  /*************************  VARIABLES  *************************/  /**
23     * ccs_audit_network_log - Audit network log.
24  extern struct mutex domain_acl_lock;   *
25     * @r:          Pointer to "struct ccs_request_info".
26  /*************************  AUDIT FUNCTIONS  *************************/   * @operation:  The name of operation.
27     * @address:    An IPv4 or IPv6 address.
28  static int AuditNetworkLog(const bool is_ipv6, const char *operation, const u32 *address, const u16 port, const bool is_granted)   * @port:       Port number.
29  {   * @is_granted: True if this is a granted log.
30          char *buf;   *
31          int len = 256;   * Returns 0 on success, negative value otherwise.
32          if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM;   */
33          if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM;  static int ccs_audit_network_log(struct ccs_request_info *r,
34          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);                                   const char *operation, const char *address,
35          if (is_ipv6) {                                   const u16 port, const bool is_granted)
36                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address);  {
37          } else {          if (!is_granted)
38                  u32 ip = *address;                  ccs_warn_log(r, "%s %s %u", operation, address, port);
39                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));          return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK
40          }                                     "%s %s %u\n", operation, address, port);
         snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port);  
         return WriteAuditLog(buf, is_granted);  
 }  
   
 /*************************  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;  
41  }  }
42    
43  int AddAddressGroupPolicy(char *data, const bool is_delete)  /**
44     * ccs_parse_ip_address - Parse an IP address.
45     *
46     * @address: String to parse.
47     * @min:     Pointer to store min address.
48     * @max:     Pointer to store max address.
49     *
50     * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.
51     */
52    int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
53  {  {
54          int count, is_ipv6;          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
55          u16 min_address[8], max_address[8];                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
56          char *cp = strchr(data, ' ');                             &min[0], &min[1], &min[2], &min[3],
57          if (!cp) return -EINVAL;                             &min[4], &min[5], &min[6], &min[7],
58          *cp++ = '\0';                             &max[0], &max[1], &max[2], &max[3],
59          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]);
60                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],          if (count == 8 || count == 16) {
61                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],                  u8 i;
62                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                  if (count == 8)
63                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                          memmove(max, min, sizeof(u16) * 8);
                 int i;  
64                  for (i = 0; i < 8; i++) {                  for (i = 0; i < 8; i++) {
65                          min_address[i] = htons(min_address[i]);                          min[i] = htons(min[i]);
66                          max_address[i] = htons(max_address[i]);                          max[i] = htons(max[i]);
67                  }                  }
68                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                  return 2;
                 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;  
69          }          }
70          return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete);          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
71  }                         &min[0], &min[1], &min[2], &min[3],
72                           &max[0], &max[1], &max[2], &max[3]);
73  static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name)          if (count == 4 || count == 8) {
74  {                  u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
75          int i;                                 + (((u8) min[2]) << 8) + (u8) min[3]);
76          struct address_group_entry *group;                  memmove(min, &ip, sizeof(ip));
77          for (i = 0; i <= 1; i++) {                  if (count == 8)
78                  list1_for_each_entry(group, &address_group_list, list) {                          ip = htonl((((u8) max[0]) << 24)
79                          if (strcmp(group_name, group->group_name->name) == 0) return group;                                     + (((u8) max[1]) << 16)
80                  }                                     + (((u8) max[2]) << 8) + (u8) max[3]);
81                  if (i == 0) {                  memmove(max, &ip, sizeof(ip));
82                          const u16 dummy[2] = { 0, 0 };                  return 1;
                         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;  
                 }  
83          }          }
84          return 0;          return 0;
85  }  }
86    
 int ReadAddressGroupPolicy(struct io_buffer *head)  
 {  
         struct list1_head *gpos;  
         struct list1_head *mpos;  
         list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {  
                 struct address_group_entry *group;  
                 group = list1_entry(gpos, struct address_group_entry, list);  
                 list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) {  
                         char buf[128];  
                         struct address_group_member *member;  
                         member = list1_entry(mpos, struct address_group_member, list);  
                         if (member->is_deleted) continue;  
                         if (member->is_ipv6) {  
                                 const struct in6_addr *min_address = member->min.ipv6, *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;  
                 }  
         }  
         return 0;  
 }  
   
 /*************************  NETWORK NETWORK ACL HANDLER  *************************/  
   
87  #if !defined(NIP6)  #if !defined(NIP6)
88  #define NIP6(addr) \  #define NIP6(addr)                                                      \
89          ntohs((addr).s6_addr16[0]), \          ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]),         \
90          ntohs((addr).s6_addr16[1]), \                  ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
91          ntohs((addr).s6_addr16[2]), \                  ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
92          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])  
93  #endif  #endif
94    
95  char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)  /**
96     * ccs_print_ipv6 - Print an IPv6 address.
97     *
98     * @buffer:     Buffer to write to.
99     * @buffer_len: Size of @buffer.
100     * @ip:         Pointer to "struct in6_addr".
101     *
102     * Returns nothing.
103     */
104    void ccs_print_ipv6(char *buffer, const int buffer_len,
105                        const struct in6_addr *ip)
106  {  {
107          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
108          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;  
109  }  }
110    
111  const char *network2keyword(const unsigned int operation)  /**
112     * ccs_net2keyword - Convert network operation index to network operation name.
113     *
114     * @operation: Type of operation.
115     *
116     * Returns the name of operation.
117     */
118    const char *ccs_net2keyword(const u8 operation)
119  {  {
120          const char *keyword = "unknown";          const char *keyword = "unknown";
121          switch (operation) {          switch (operation) {
122          case NETWORK_ACL_UDP_BIND:          case CCS_NETWORK_UDP_BIND:
123                  keyword = "UDP bind";                  keyword = "UDP bind";
124                  break;                  break;
125          case NETWORK_ACL_UDP_CONNECT:          case CCS_NETWORK_UDP_CONNECT:
126                  keyword = "UDP connect";                  keyword = "UDP connect";
127                  break;                  break;
128          case NETWORK_ACL_TCP_BIND:          case CCS_NETWORK_TCP_BIND:
129                  keyword = "TCP bind";                  keyword = "TCP bind";
130                  break;                  break;
131          case NETWORK_ACL_TCP_LISTEN:          case CCS_NETWORK_TCP_LISTEN:
132                  keyword = "TCP listen";                  keyword = "TCP listen";
133                  break;                  break;
134          case NETWORK_ACL_TCP_CONNECT:          case CCS_NETWORK_TCP_CONNECT:
135                  keyword = "TCP connect";                  keyword = "TCP connect";
136                  break;                  break;
137          case NETWORK_ACL_TCP_ACCEPT:          case CCS_NETWORK_TCP_ACCEPT:
138                  keyword = "TCP accept";                  keyword = "TCP accept";
139                  break;                  break;
140          case NETWORK_ACL_RAW_BIND:          case CCS_NETWORK_RAW_BIND:
141                  keyword = "RAW bind";                  keyword = "RAW bind";
142                  break;                  break;
143          case NETWORK_ACL_RAW_CONNECT:          case CCS_NETWORK_RAW_CONNECT:
144                  keyword = "RAW connect";                  keyword = "RAW connect";
145                  break;                  break;
146          }          }
147          return keyword;          return keyword;
148  }  }
149    
150  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)  /**
151     * ccs_network_entry2 - Check permission for network operation.
152     *
153     * @is_ipv6:   True if @address is an IPv6 address.
154     * @operation: Type of operation.
155     * @address:   An IPv4 or IPv6 address.
156     * @port:      Port number.
157     *
158     * Returns 0 on success, negative value otherwise.
159     *
160     * Caller holds ccs_read_lock().
161     */
162    static int ccs_network_entry2(const bool is_ipv6, const u8 operation,
163                                  const u32 *address, const u16 port)
164  {  {
165          struct acl_info *ptr;          struct ccs_request_info r;
166          struct ip_network_acl_record *acl;          struct ccs_acl_info *ptr;
167          int error = -ENOMEM;          const char *keyword = ccs_net2keyword(operation);
168          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/          const u16 perm = 1 << operation;
169          const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;          /* using host byte order to allow u32 comparison than memcmp().*/
170          if (!domain) return -EINVAL;          const u32 ip = ntohl(*address);
171          if (record_type == IP_RECORD_TYPE_IPv6) {          int error;
172                  if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL          char buf[64];
173                      || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;          if (ccs_init_request_info(&r, NULL,
174          }                                    CCS_MAC_NETWORK_UDP_BIND + operation)
175          mutex_lock(&domain_acl_lock);              == CCS_CONFIG_DISABLED)
176          if (!is_delete) {                  return 0;
177                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {          memset(buf, 0, sizeof(buf));
178                          acl = container_of(ptr, struct ip_network_acl_record, head);          if (is_ipv6)
179                          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) {                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
180                                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                                 address);
181                                          if (acl->u.group == group) {          else
182                                                  ptr->is_deleted = 0;                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
183                                                  /* Found. Nothing to do. */          do {
184                                                  error = 0;                  error = -EPERM;
185                                                  goto out;                  list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {
186                                          }                          struct ccs_ip_network_acl *acl;
187                                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                          if (ptr->is_deleted ||
188                                          if (acl->u.ipv4.min == min_ip && max_ip == acl->u.ipv4.max) {                              ptr->type != CCS_TYPE_IP_NETWORK_ACL)
189                                                  ptr->is_deleted = 0;                                  continue;
190                                                  /* Found. Nothing to do. */                          acl = container_of(ptr, struct ccs_ip_network_acl,
191                                                  error = 0;                                             head);
192                                                  goto out;                          if (!(acl->perm & perm))
193                                          }                                  continue;
194                                  } else if (record_type == IP_RECORD_TYPE_IPv6) {                          if (!ccs_compare_number_union(port, &acl->port) ||
195                                          if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) {                              !ccs_condition(&r, ptr))
196                                                  ptr->is_deleted = 0;                                  continue;
197                                                  /* Found. Nothing to do. */                          switch (acl->address_type) {
198                                                  error = 0;                          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
199                                                  goto out;                                  if (!ccs_address_matches_group(is_ipv6,
200                                          }                                                                 address,
201                                  }                                                                 acl->address.
202                          }                                                                 group))
203                  }                                          continue;
204                  /* Not found. Append it to the tail. */                                  break;
205                  if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out;                          case CCS_IP_ADDRESS_TYPE_IPv4:
206                  acl->head.type = TYPE_IP_NETWORK_ACL;                                  if (is_ipv6 || ip < acl->address.ipv4.min ||
207                  acl->operation_type = operation;                                      acl->address.ipv4.max < ip)
208                  acl->record_type = record_type;                                          continue;
209                  acl->head.cond = condition;                                  break;
210                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                          default:
211                          acl->u.group = group;                                  if (!is_ipv6 ||
212                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                                      memcmp(acl->address.ipv6.min, address, 16)
213                          acl->u.ipv4.min = min_ip;                                      > 0 ||
214                          acl->u.ipv4.max = max_ip;                                      memcmp(address, acl->address.ipv6.max, 16)
215                  } else {                                      > 0)
216                          acl->u.ipv6.min = saved_min_address;                                          continue;
217                          acl->u.ipv6.max = saved_max_address;                                  break;
                 }  
                 acl->min_port = min_port;  
                 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;  
218                          }                          }
219                          error = DelDomainACL(ptr);                          r.cond = ptr->cond;
220                            error = 0;
221                          break;                          break;
222                  }                  }
223          }                  ccs_audit_network_log(&r, keyword, buf, port, !error);
224   out: ;                  if (!error)
225          mutex_unlock(&domain_acl_lock);                          break;
226                    error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK
227                                           "%s %s %u\n", keyword, buf, port);
228            } while (error == CCS_RETRY_REQUEST);
229            if (r.mode != CCS_CONFIG_ENFORCING)
230                    error = 0;
231          return error;          return error;
232  }  }
233    
234  static int CheckNetworkEntry(const bool is_ipv6, const int operation, const u32 *address, const u16 port)  /**
235     * ccs_network_entry - Check permission for network operation.
236     *
237     * @is_ipv6:   True if @address is an IPv6 address.
238     * @operation: Type of operation.
239     * @address:   An IPv4 or IPv6 address.
240     * @port:      Port number.
241     *
242     * Returns 0 on success, negative value otherwise.
243     */
244    static int ccs_network_entry(const bool is_ipv6, const u8 operation,
245                                 const u32 *address, const u16 port)
246  {  {
247          struct domain_info * const domain = current->domain_info;          const int idx = ccs_read_lock();
248          struct acl_info *ptr;          const int error = ccs_network_entry2(is_ipv6, operation, address,
249          const char *keyword = network2keyword(operation);                                               port);
250          const bool is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK);          ccs_read_unlock(idx);
251          const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/          return error;
252          bool found = 0;  }
253          if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0;  
254          list1_for_each_entry(ptr, &domain->acl_info_list, list) {  /**
255                  struct ip_network_acl_record *acl;   * ccs_write_network_policy - Write "struct ccs_ip_network_acl" list.
256                  acl = container_of(ptr, struct ip_network_acl_record, head);   *
257                  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.
258                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {   * @domain:    Pointer to "struct ccs_domain_info".
259                          if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue;   * @condition: Pointer to "struct ccs_condition". May be NULL.
260                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {   * @is_delete: True if it is a delete request.
261                          if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue;   *
262                  } else {   * Returns 0 on success, negative value otherwise.
263                          if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue;   */
264    int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,
265                                 struct ccs_condition *condition,
266                                 const bool is_delete)
267    {
268            struct ccs_acl_info *ptr;
269            struct ccs_ip_network_acl e = {
270                    .head.type = CCS_TYPE_IP_NETWORK_ACL,
271                    .head.cond = condition,
272            };
273            u16 min_address[8];
274            u16 max_address[8];
275            int error = is_delete ? -ENOENT : -ENOMEM;
276            u8 sock_type;
277            char *w[4];
278            if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
279                    return -EINVAL;
280            if (!strcmp(w[0], "TCP"))
281                    sock_type = SOCK_STREAM;
282            else if (!strcmp(w[0], "UDP"))
283                    sock_type = SOCK_DGRAM;
284            else if (!strcmp(w[0], "RAW"))
285                    sock_type = SOCK_RAW;
286            else
287                    return -EINVAL;
288            if (!strcmp(w[1], "bind"))
289                    switch (sock_type) {
290                    case SOCK_STREAM:
291                            e.perm = 1 << CCS_NETWORK_TCP_BIND;
292                            break;
293                    case SOCK_DGRAM:
294                            e.perm = 1 << CCS_NETWORK_UDP_BIND;
295                            break;
296                    default:
297                            e.perm = 1 << CCS_NETWORK_RAW_BIND;
298                            break;
299                    }
300            else if (!strcmp(w[1], "connect"))
301                    switch (sock_type) {
302                    case SOCK_STREAM:
303                            e.perm = 1 << CCS_NETWORK_TCP_CONNECT;
304                            break;
305                    case SOCK_DGRAM:
306                            e.perm = 1 << CCS_NETWORK_UDP_CONNECT;
307                            break;
308                    default:
309                            e.perm = 1 << CCS_NETWORK_RAW_CONNECT;
310                            break;
311                  }                  }
312                  found = 1;          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))
313                    e.perm = 1 << CCS_NETWORK_TCP_LISTEN;
314            else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))
315                    e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;
316            else
317                    return -EINVAL;
318            switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
319            case 2:
320                    e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
321                    e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
322                                                              min_address);
323                    e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
324                                                              max_address);
325                    if (!e.address.ipv6.min || !e.address.ipv6.max)
326                            goto out;
327                    break;
328            case 1:
329                    e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
330                    /* use host byte order to allow u32 comparison.*/
331                    e.address.ipv4.min = ntohl(*(u32 *) min_address);
332                    e.address.ipv4.max = ntohl(*(u32 *) max_address);
333                    break;
334            default:
335                    if (w[2][0] != '@')
336                            return -EINVAL;
337                    e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
338                    e.address.group = ccs_get_address_group(w[2] + 1);
339                    if (!e.address.group)
340                            return -ENOMEM;
341                  break;                  break;
                           
342          }          }
343          AuditNetworkLog(is_ipv6, keyword, address, port, found);          if (!ccs_parse_number_union(w[3], &e.port))
344          if (found) return 0;                  goto out;
345          if (TomoyoVerboseMode()) {          if (mutex_lock_interruptible(&ccs_policy_lock))
346                  if (is_ipv6) {                  goto out;
347                          char buf[64];          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
348                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                  struct ccs_ip_network_acl *acl =
349                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));                          container_of(ptr, struct ccs_ip_network_acl, head);
350                    if (!ccs_is_same_ip_network_acl(acl, &e))
351                            continue;
352                    if (is_delete) {
353                            acl->perm &= ~e.perm;
354                            if (!acl->perm)
355                                    ptr->is_deleted = true;
356                  } else {                  } else {
357                          printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain));                          if (ptr->is_deleted)
358                                    acl->perm = 0;
359                            acl->perm |= e.perm;
360                            ptr->is_deleted = false;
361                  }                  }
362                    error = 0;
363                    break;
364          }          }
365          AuditNetworkLog(is_ipv6, keyword, address, port, 0);          if (!is_delete && error) {
366          if (is_enforce) {                  struct ccs_ip_network_acl *entry =
367                  if (is_ipv6) {                          ccs_commit_ok(&e, sizeof(e));
368                          char buf[64];                  if (entry) {
369                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                          ccs_add_domain_acl(domain, &entry->head);
370                          return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);                          error = 0;
371                  }                  }
                 return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);  
372          }          }
373          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);
374          return 0;   out:
375            if (w[2][0] == '@')
376                    ccs_put_address_group(e.address.group);
377            else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
378                    ccs_put_ipv6_address(e.address.ipv6.min);
379                    ccs_put_ipv6_address(e.address.ipv6.max);
380            }
381            ccs_put_number_union(&e.port);
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_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 int ccs_network_listen_acl(const bool is_ipv6, const u8 *address,
395                                      const u16 port)
396  {  {
397          u8 sock_type, operation, record_type;          return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN,
398          u16 min_address[8], max_address[8];                                   (const u32 *) address, ntohs(port));
399          struct address_group_entry *group = NULL;  }
400          u16 min_port, max_port;  
401          int count;  /**
402          char *cp1 = NULL, *cp2 = NULL;   * ccs_network_connect_acl - Check permission for connect() operation.
403          if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++;   *
404          if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM;   * @is_ipv6:   True if @address is an IPv6 address.
405          else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM;   * @sock_type: Type of socket. (TCP or UDP or RAW)
406          else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW;   * @address:   An IPv4 or IPv6 address.
407          else goto out;   * @port:      Port number.
408          if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++;   *
409          if (strncmp(cp1, "bind ", 5) == 0) {   * Returns 0 on success, negative value otherwise.
410                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND;   */
411          } else if (strncmp(cp1, "connect ", 8) == 0) {  static int ccs_network_connect_acl(const bool is_ipv6, const int sock_type,
412                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                                     const u8 *address, const u16 port)
413          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) {  {
414                  operation = NETWORK_ACL_TCP_LISTEN;          u8 operation;
415          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) {          switch (sock_type) {
416                  operation = NETWORK_ACL_TCP_ACCEPT;          case SOCK_STREAM:
417          } else {                  operation = CCS_NETWORK_TCP_CONNECT;
418                  goto out;                  break;
419          }          case SOCK_DGRAM:
420          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';                  operation = CCS_NETWORK_UDP_CONNECT;
421          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                  break;
422                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],          default:
423                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],                  operation = CCS_NETWORK_RAW_CONNECT;
                             &max_address[0], &max_address[1], &max_address[2], &max_address[3],  
                             &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {  
                 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);  
424          }          }
425   out: ;          return ccs_network_entry(is_ipv6, operation,
426          return -EINVAL;                                   (const u32 *) address, ntohs(port));
427  }  }
428    
429  int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
430     * ccs_network_bind_acl - Check permission for bind() operation.
431     *
432     * @is_ipv6:   True if @address is an IPv6 address.
433     * @sock_type: Type of socket. (TCP or UDP or RAW)
434     * @address:   An IPv4 or IPv6 address.
435     * @port:      Port number.
436     *
437     * Returns 0 on success, negative value otherwise.
438     */
439    static int ccs_network_bind_acl(const bool is_ipv6, const int sock_type,
440                                    const u8 *address, const u16 port)
441  {  {
442          return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port));          u8 operation;
443            switch (sock_type) {
444            case SOCK_STREAM:
445                    operation = CCS_NETWORK_TCP_BIND;
446                    break;
447            case SOCK_DGRAM:
448                    operation = CCS_NETWORK_UDP_BIND;
449                    break;
450            default:
451                    operation = CCS_NETWORK_RAW_BIND;
452            }
453            return ccs_network_entry(is_ipv6, operation,
454                                     (const u32 *) address, ntohs(port));
455  }  }
 EXPORT_SYMBOL(CheckNetworkListenACL);  
456    
457  int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
458     * ccs_network_accept_acl - Check permission for accept() operation.
459     *
460     * @is_ipv6: True if @address is an IPv6 address.
461     * @address: An IPv4 or IPv6 address.
462     * @port:    Port number.
463     *
464     * Returns 0 on success, negative value otherwise.
465     */
466    static int ccs_network_accept_acl(const bool is_ipv6, const u8 *address,
467                                      const u16 port)
468  {  {
469          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;
470            current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
471            retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT,
472                                       (const u32 *) address, ntohs(port));
473            current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
474            return retval;
475  }  }
 EXPORT_SYMBOL(CheckNetworkConnectACL);  
476    
477  int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
478     * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.
479     *
480     * @is_ipv6:   True if @address is an IPv6 address.
481     * @sock_type: Type of socket. (UDP or RAW)
482     * @address:   An IPv4 or IPv6 address.
483     * @port:      Port number.
484     *
485     * Returns 0 on success, negative value otherwise.
486     */
487    static int ccs_network_sendmsg_acl(const bool is_ipv6, const int sock_type,
488                                       const u8 *address, const u16 port)
489  {  {
490          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));          u8 operation;
491            if (sock_type == SOCK_DGRAM)
492                    operation = CCS_NETWORK_UDP_CONNECT;
493            else
494                    operation = CCS_NETWORK_RAW_CONNECT;
495            return ccs_network_entry(is_ipv6, operation,
496                                     (const u32 *) address, ntohs(port));
497  }  }
 EXPORT_SYMBOL(CheckNetworkBindACL);  
498    
499  int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
500     * ccs_network_recvmsg_acl - Check permission for recvmsg() operation.
501     *
502     * @is_ipv6:   True if @address is an IPv6 address.
503     * @sock_type: Type of socket. (UDP or RAW)
504     * @address:   An IPv4 or IPv6 address.
505     * @port:      Port number.
506     *
507     * Returns 0 on success, negative value otherwise.
508     */
509    static int ccs_network_recvmsg_acl(const bool is_ipv6, const int sock_type,
510                                       const u8 *address, const u16 port)
511  {  {
512          int retval;          int retval;
513          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          const u8 operation
514          retval = CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port));                  = (sock_type == SOCK_DGRAM) ?
515          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                  CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT;
516            current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
517            retval = ccs_network_entry(is_ipv6, operation,
518                                       (const u32 *) address, ntohs(port));
519            current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
520          return retval;          return retval;
521  }  }
 EXPORT_SYMBOL(CheckNetworkAcceptACL);  
522    
523  int CheckNetworkSendMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  #ifndef CONFIG_NET
524    
525    void __init ccs_network_init(void)
526  {  {
         return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));  
527  }  }
 EXPORT_SYMBOL(CheckNetworkSendMsgACL);  
528    
529  int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  #else
530    
531    #define MAX_SOCK_ADDR 128 /* net/socket.c */
532    
533    /* Check permission for creating a socket. */
534    static int __ccs_socket_create_permission(int family, int type, int protocol)
535  {  {
536          int retval;          int error = 0;
537          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          /* Nothing to do if I am a kernel service. */
538          retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          if (segment_eq(get_fs(), KERNEL_DS))
539          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                  return 0;
540          return retval;          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(CheckNetworkRecvMsgACL);  
562    
563  /***** TOMOYO Linux end. *****/  /* Check permission for listening a TCP socket. */
564    static int __ccs_socket_listen_permission(struct socket *sock)
565    {
566            int error = 0;
567            char addr[MAX_SOCK_ADDR];
568            int addr_len;
569            /* Nothing to do if I am a kernel service. */
570            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_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_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    static int __ccs_socket_connect_permission(struct socket *sock,
606                                               struct sockaddr *addr, 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_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_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    }
661    
662    /* Check permission for setting the local IP address/port pair of a socket. */
663    static int __ccs_socket_bind_permission(struct socket *sock,
664                                            struct sockaddr *addr, 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_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_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    static int __ccs_socket_accept_permission(struct socket *sock,
717                                              struct sockaddr *addr)
718    {
719            int error = 0;
720            int addr_len;
721            /* Nothing to do if I am a kernel service. */
722            if (segment_eq(get_fs(), KERNEL_DS))
723                    return 0;
724            switch (sock->sk->sk_family) {
725            case PF_INET:
726            case PF_INET6:
727                    break;
728            default:
729                    return 0;
730            }
731            error = sock->ops->getname(sock, addr, &addr_len, 2);
732            if (error)
733                    return error;
734            switch (addr->sa_family) {
735                    struct sockaddr_in6 *addr6;
736                    struct sockaddr_in *addr4;
737            case AF_INET6:
738                    addr6 = (struct sockaddr_in6 *) addr;
739                    error = ccs_network_accept_acl(true,
740                                                   addr6->sin6_addr.s6_addr,
741                                                   addr6->sin6_port);
742                    break;
743            case AF_INET:
744                    addr4 = (struct sockaddr_in *) addr;
745                    error = ccs_network_accept_acl(false,
746                                                   (u8 *) &addr4->sin_addr,
747                                                   addr4->sin_port);
748                    break;
749            }
750            return error;
751    }
752    
753    /* Check permission for sending a datagram via a UDP or RAW socket. */
754    static int __ccs_socket_sendmsg_permission(struct socket *sock,
755                                               struct msghdr *msg, int size)
756    {
757            struct sockaddr *addr = (struct sockaddr *) msg->msg_name;
758            const int addr_len = msg->msg_namelen;
759            int error = 0;
760            const int type = sock->type;
761            /* Nothing to do if I am a kernel service. */
762            if (segment_eq(get_fs(), KERNEL_DS))
763                    return 0;
764            if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))
765                    return 0;
766            switch (addr->sa_family) {
767                    struct sockaddr_in6 *addr6;
768                    struct sockaddr_in *addr4;
769                    u16 port;
770            case AF_INET6:
771                    if (addr_len < SIN6_LEN_RFC2133)
772                            break;
773                    addr6 = (struct sockaddr_in6 *) addr;
774                    if (type == SOCK_DGRAM)
775                            port = addr6->sin6_port;
776                    else
777                            port = htons(sock->sk->sk_protocol);
778                    error = ccs_network_sendmsg_acl(true, type,
779                                                    addr6->sin6_addr.s6_addr,
780                                                    port);
781                    break;
782            case AF_INET:
783                    if (addr_len < sizeof(struct sockaddr_in))
784                            break;
785                    addr4 = (struct sockaddr_in *) addr;
786                    if (type == SOCK_DGRAM)
787                            port = addr4->sin_port;
788                    else
789                            port = htons(sock->sk->sk_protocol);
790                    error = ccs_network_sendmsg_acl(false, type,
791                                                    (u8 *) &addr4->sin_addr, port);
792                    break;
793            }
794            return error;
795    }
796    
797    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
798    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
799    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
800    
801    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
802    {
803            return skb->nh.iph;
804    }
805    
806    static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
807    {
808            return skb->h.uh;
809    }
810    
811    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
812    {
813            return skb->nh.ipv6h;
814    }
815    
816    #endif
817    #endif
818    #endif
819    
820    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
821    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
822                                  unsigned int flags)
823    {
824            /* Clear queue. */
825            if (flags & MSG_PEEK) {
826                    int clear = 0;
827                    spin_lock_irq(&sk->sk_receive_queue.lock);
828                    if (skb == skb_peek(&sk->sk_receive_queue)) {
829                            __skb_unlink(skb, &sk->sk_receive_queue);
830                            clear = 1;
831                    }
832                    spin_unlock_irq(&sk->sk_receive_queue.lock);
833                    if (clear)
834                            kfree_skb(skb);
835            }
836            skb_free_datagram(sk, skb);
837    }
838    #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
839    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
840                                  unsigned int flags)
841    {
842            /* Clear queue. */
843            if (flags & MSG_PEEK) {
844                    int clear = 0;
845                    spin_lock_bh(&sk->sk_receive_queue.lock);
846                    if (skb == skb_peek(&sk->sk_receive_queue)) {
847                            __skb_unlink(skb, &sk->sk_receive_queue);
848                            clear = 1;
849                    }
850                    spin_unlock_bh(&sk->sk_receive_queue.lock);
851                    if (clear)
852                            kfree_skb(skb);
853            }
854            skb_free_datagram(sk, skb);
855    }
856    #endif
857    
858    /*
859     * Check permission for receiving a datagram via a UDP or RAW socket.
860     *
861     * Currently, the LSM hook for this purpose is not provided.
862     */
863    static int __ccs_socket_recvmsg_permission(struct sock *sk,
864                                               struct sk_buff *skb,
865                                               const unsigned int flags)
866    {
867            int error = 0;
868            const unsigned int type = sk->sk_type;
869            if (type != SOCK_DGRAM && type != SOCK_RAW)
870                    return 0;
871            /* Nothing to do if I am a kernel service. */
872            if (segment_eq(get_fs(), KERNEL_DS))
873                    return 0;
874    
875            switch (sk->sk_family) {
876                    struct in6_addr sin6;
877                    struct in_addr sin4;
878                    u16 port;
879            case PF_INET6:
880                    if (type == SOCK_DGRAM) { /* UDP IPv6 */
881                            if (skb->protocol == htons(ETH_P_IP)) {
882                                    ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),
883                                                  ip_hdr(skb)->saddr);
884                            } else {
885                                    ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
886                            }
887                            port = udp_hdr(skb)->source;
888                    } else { /* RAW IPv6 */
889                            ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
890                            port = htons(sk->sk_protocol);
891                    }
892                    error = ccs_network_recvmsg_acl(true, type,
893                                                    (u8 *) &sin6, port);
894                    break;
895            case PF_INET:
896                    if (type == SOCK_DGRAM) { /* UDP IPv4 */
897                            sin4.s_addr = ip_hdr(skb)->saddr;
898                            port = udp_hdr(skb)->source;
899                    } else { /* RAW IPv4 */
900                            sin4.s_addr = ip_hdr(skb)->saddr;
901                            port = htons(sk->sk_protocol);
902                    }
903                    error = ccs_network_recvmsg_acl(false, type,
904                                                    (u8 *) &sin4, port);
905                    break;
906            }
907            if (!error)
908                    return 0;
909            /*
910             * Remove from queue if MSG_PEEK is used so that
911             * the head message from unwanted source in receive queue will not
912             * prevent the caller from picking up next message from wanted source
913             * when the caller is using MSG_PEEK flag for picking up.
914             */
915    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
916            if (type == SOCK_DGRAM)
917                    lock_sock(sk);
918    #endif
919            skb_kill_datagram(sk, skb, flags);
920    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
921            if (type == SOCK_DGRAM)
922                    release_sock(sk);
923    #endif
924            /* Hope less harmful than -EPERM. */
925            return -ENOMEM;
926    }
927    
928    void __init ccs_network_init(void)
929    {
930            ccsecurity_ops.socket_create_permission =
931                    __ccs_socket_create_permission;
932            ccsecurity_ops.socket_listen_permission =
933                    __ccs_socket_listen_permission;
934            ccsecurity_ops.socket_connect_permission =
935                    __ccs_socket_connect_permission;
936            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
937            ccsecurity_ops.socket_accept_permission =
938                    __ccs_socket_accept_permission;
939            ccsecurity_ops.socket_sendmsg_permission =
940                    __ccs_socket_sendmsg_permission;
941            ccsecurity_ops.socket_recvmsg_permission =
942                    __ccs_socket_recvmsg_permission;
943    }
944    
945    #endif

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

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