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

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 808 by kumaneko, Mon Dec 17 04:06:21 2007 UTC trunk/1.7.x/ccs-patch/security/ccsecurity/network.c revision 3512 by kumaneko, Fri Mar 19 08:37:22 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-pre   2010/03/08
  *  
  * Version: 1.5.3-pre   2007/12/17  
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]);
                 }  
                 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;  
67                  }                  }
68                    return 2;
69          }          }
70          return 0;          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  int ReadAddressGroupPolicy(struct io_buffer *head)          if (count == 4 || count == 8) {
74  {                  u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
75          struct list1_head *gpos;                                 + (((u8) min[2]) << 8) + (u8) min[3]);
76          struct list1_head *mpos;                  memmove(min, &ip, sizeof(ip));
77          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {                  if (count == 8)
78                  struct address_group_entry *group;                          ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)
79                  group = list1_entry(gpos, struct address_group_entry, list);                                     + (((u8) max[2]) << 8) + (u8) max[3]);
80                  list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) {                  memmove(max, &ip, sizeof(ip));
81                          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;  
                 }  
82          }          }
83          return 0;          return 0;
84  }  }
85    
 /*************************  NETWORK NETWORK ACL HANDLER  *************************/  
   
86  #if !defined(NIP6)  #if !defined(NIP6)
87  #define NIP6(addr) \  #define NIP6(addr)                                                      \
88          ntohs((addr).s6_addr16[0]), \          ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]),         \
89          ntohs((addr).s6_addr16[1]), \                  ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
90          ntohs((addr).s6_addr16[2]), \                  ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
91          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])  
92  #endif  #endif
93    
94  char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)  /**
95     * ccs_print_ipv6 - Print an IPv6 address.
96     *
97     * @buffer:     Buffer to write to.
98     * @buffer_len: Size of @buffer.
99     * @ip:         Pointer to "struct in6_addr".
100     *
101     * Returns nothing.
102     */
103    void ccs_print_ipv6(char *buffer, const int buffer_len,
104                        const struct in6_addr *ip)
105  {  {
106          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
107          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;  
108  }  }
109    
110  const char *network2keyword(const unsigned int operation)  /**
111     * ccs_net2keyword - Convert network operation index to network operation name.
112     *
113     * @operation: Type of operation.
114     *
115     * Returns the name of operation.
116     */
117    const char *ccs_net2keyword(const u8 operation)
118  {  {
119          const char *keyword = "unknown";          const char *keyword = "unknown";
120          switch (operation) {          switch (operation) {
121          case NETWORK_ACL_UDP_BIND:          case CCS_NETWORK_UDP_BIND:
122                  keyword = "UDP bind";                  keyword = "UDP bind";
123                  break;                  break;
124          case NETWORK_ACL_UDP_CONNECT:          case CCS_NETWORK_UDP_CONNECT:
125                  keyword = "UDP connect";                  keyword = "UDP connect";
126                  break;                  break;
127          case NETWORK_ACL_TCP_BIND:          case CCS_NETWORK_TCP_BIND:
128                  keyword = "TCP bind";                  keyword = "TCP bind";
129                  break;                  break;
130          case NETWORK_ACL_TCP_LISTEN:          case CCS_NETWORK_TCP_LISTEN:
131                  keyword = "TCP listen";                  keyword = "TCP listen";
132                  break;                  break;
133          case NETWORK_ACL_TCP_CONNECT:          case CCS_NETWORK_TCP_CONNECT:
134                  keyword = "TCP connect";                  keyword = "TCP connect";
135                  break;                  break;
136          case NETWORK_ACL_TCP_ACCEPT:          case CCS_NETWORK_TCP_ACCEPT:
137                  keyword = "TCP accept";                  keyword = "TCP accept";
138                  break;                  break;
139          case NETWORK_ACL_RAW_BIND:          case CCS_NETWORK_RAW_BIND:
140                  keyword = "RAW bind";                  keyword = "RAW bind";
141                  break;                  break;
142          case NETWORK_ACL_RAW_CONNECT:          case CCS_NETWORK_RAW_CONNECT:
143                  keyword = "RAW connect";                  keyword = "RAW connect";
144                  break;                  break;
145          }          }
146          return keyword;          return keyword;
147  }  }
148    
149  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)  /**
150     * ccs_network_entry2 - Check permission for network operation.
151     *
152     * @is_ipv6:   True if @address is an IPv6 address.
153     * @operation: Type of operation.
154     * @address:   An IPv4 or IPv6 address.
155     * @port:      Port number.
156     *
157     * Returns 0 on success, negative value otherwise.
158     *
159     * Caller holds ccs_read_lock().
160     */
161    static int ccs_network_entry2(const bool is_ipv6, const u8 operation,
162                                  const u32 *address, const u16 port)
163  {  {
164          struct acl_info *ptr;          struct ccs_request_info r;
165          struct ip_network_acl_record *acl;          struct ccs_acl_info *ptr;
166          int error = -ENOMEM;          const char *keyword = ccs_net2keyword(operation);
167          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;
168          const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;          /* using host byte order to allow u32 comparison than memcmp().*/
169          if (!domain) return -EINVAL;          const u32 ip = ntohl(*address);
170          if (record_type == IP_RECORD_TYPE_IPv6) {          int error;
171                  if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL          char buf[64];
172                      || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;          if (ccs_init_request_info(&r, NULL,
173          }                                    CCS_MAC_NETWORK_UDP_BIND + operation)
174          mutex_lock(&domain_acl_lock);              == CCS_CONFIG_DISABLED)
175          if (!is_delete) {                  return 0;
176                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {          memset(buf, 0, sizeof(buf));
177                          acl = container_of(ptr, struct ip_network_acl_record, head);          if (is_ipv6)
178                          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 *)
179                                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                                 address);
180                                          if (acl->u.group == group) {          else
181                                                  ptr->is_deleted = 0;                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
182                                                  /* Found. Nothing to do. */          do {
183                                                  error = 0;                  error = -EPERM;
184                                                  goto out;                  list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {
185                                          }                          struct ccs_ip_network_acl *acl;
186                                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                          if (ptr->is_deleted ||
187                                          if (acl->u.ipv4.min == min_ip && max_ip == acl->u.ipv4.max) {                              ptr->type != CCS_TYPE_IP_NETWORK_ACL)
188                                                  ptr->is_deleted = 0;                                  continue;
189                                                  /* Found. Nothing to do. */                          acl = container_of(ptr, struct ccs_ip_network_acl,
190                                                  error = 0;                                             head);
191                                                  goto out;                          if (!(acl->perm & perm))
192                                          }                                  continue;
193                                  } else if (record_type == IP_RECORD_TYPE_IPv6) {                          if (!ccs_compare_number_union(port, &acl->port) ||
194                                          if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) {                              !ccs_condition(&r, ptr))
195                                                  ptr->is_deleted = 0;                                  continue;
196                                                  /* Found. Nothing to do. */                          switch (acl->address_type) {
197                                                  error = 0;                          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
198                                                  goto out;                                  if (!ccs_address_matches_group(is_ipv6,
199                                          }                                                                 address,
200                                  }                                                                 acl->address.
201                          }                                                                 group))
202                  }                                          continue;
203                  /* Not found. Append it to the tail. */                                  break;
204                  if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out;                          case CCS_IP_ADDRESS_TYPE_IPv4:
205                  acl->head.type = TYPE_IP_NETWORK_ACL;                                  if (is_ipv6 || ip < acl->address.ipv4.min ||
206                  acl->operation_type = operation;                                      acl->address.ipv4.max < ip)
207                  acl->record_type = record_type;                                          continue;
208                  acl->head.cond = condition;                                  break;
209                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                          default:
210                          acl->u.group = group;                                  if (!is_ipv6 ||
211                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                                      memcmp(acl->address.ipv6.min, address, 16)
212                          acl->u.ipv4.min = min_ip;                                      > 0 ||
213                          acl->u.ipv4.max = max_ip;                                      memcmp(address, acl->address.ipv6.max, 16)
214                  } else {                                      > 0)
215                          acl->u.ipv6.min = saved_min_address;                                          continue;
216                          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;  
217                          }                          }
218                          error = DelDomainACL(ptr);                          r.cond = ptr->cond;
219                            error = 0;
220                          break;                          break;
221                  }                  }
222          }                  ccs_audit_network_log(&r, keyword, buf, port, !error);
223   out: ;                  if (!error)
224          mutex_unlock(&domain_acl_lock);                          break;
225                    error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK
226                                           "%s %s %u\n", keyword, buf, port);
227            } while (error == CCS_RETRY_REQUEST);
228            if (r.mode != CCS_CONFIG_ENFORCING)
229                    error = 0;
230          return error;          return error;
231  }  }
232    
233  static int CheckNetworkEntry(const bool is_ipv6, const int operation, const u32 *address, const u16 port)  /**
234     * ccs_network_entry - Check permission for network operation.
235     *
236     * @is_ipv6:   True if @address is an IPv6 address.
237     * @operation: Type of operation.
238     * @address:   An IPv4 or IPv6 address.
239     * @port:      Port number.
240     *
241     * Returns 0 on success, negative value otherwise.
242     */
243    static int ccs_network_entry(const bool is_ipv6, const u8 operation,
244                                 const u32 *address, const u16 port)
245  {  {
246          struct domain_info * const domain = current->domain_info;          const int idx = ccs_read_lock();
247          struct acl_info *ptr;          const int error = ccs_network_entry2(is_ipv6, operation, address,
248          const char *keyword = network2keyword(operation);                                               port);
249          const bool is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK);          ccs_read_unlock(idx);
250          const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/          return error;
251          bool found = 0;  }
252          if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0;  
253          list1_for_each_entry(ptr, &domain->acl_info_list, list) {  /**
254                  struct ip_network_acl_record *acl;   * ccs_write_network_policy - Write "struct ccs_ip_network_acl" list.
255                  acl = container_of(ptr, struct ip_network_acl_record, head);   *
256                  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.
257                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {   * @domain:    Pointer to "struct ccs_domain_info".
258                          if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue;   * @condition: Pointer to "struct ccs_condition". May be NULL.
259                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {   * @is_delete: True if it is a delete request.
260                          if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue;   *
261                  } else {   * Returns 0 on success, negative value otherwise.
262                          if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue;   */
263    int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,
264                                 struct ccs_condition *condition,
265                                 const bool is_delete)
266    {
267            struct ccs_acl_info *ptr;
268            struct ccs_ip_network_acl e = {
269                    .head.type = CCS_TYPE_IP_NETWORK_ACL,
270                    .head.cond = condition,
271            };
272            u16 min_address[8];
273            u16 max_address[8];
274            int error = is_delete ? -ENOENT : -ENOMEM;
275            u8 sock_type;
276            char *w[4];
277            if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
278                    return -EINVAL;
279            if (!strcmp(w[0], "TCP"))
280                    sock_type = SOCK_STREAM;
281            else if (!strcmp(w[0], "UDP"))
282                    sock_type = SOCK_DGRAM;
283            else if (!strcmp(w[0], "RAW"))
284                    sock_type = SOCK_RAW;
285            else
286                    return -EINVAL;
287            if (!strcmp(w[1], "bind"))
288                    switch (sock_type) {
289                    case SOCK_STREAM:
290                            e.perm = 1 << CCS_NETWORK_TCP_BIND;
291                            break;
292                    case SOCK_DGRAM:
293                            e.perm = 1 << CCS_NETWORK_UDP_BIND;
294                            break;
295                    default:
296                            e.perm = 1 << CCS_NETWORK_RAW_BIND;
297                            break;
298                  }                  }
299                  found = 1;          else if (!strcmp(w[1], "connect"))
300                    switch (sock_type) {
301                    case SOCK_STREAM:
302                            e.perm = 1 << CCS_NETWORK_TCP_CONNECT;
303                            break;
304                    case SOCK_DGRAM:
305                            e.perm = 1 << CCS_NETWORK_UDP_CONNECT;
306                            break;
307                    default:
308                            e.perm = 1 << CCS_NETWORK_RAW_CONNECT;
309                            break;
310                    }
311            else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))
312                    e.perm = 1 << CCS_NETWORK_TCP_LISTEN;
313            else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))
314                    e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;
315            else
316                    return -EINVAL;
317            switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
318            case 2:
319                    e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
320                    e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
321                                                              min_address);
322                    e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
323                                                              max_address);
324                    if (!e.address.ipv6.min || !e.address.ipv6.max)
325                            goto out;
326                    break;
327            case 1:
328                    e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
329                    /* use host byte order to allow u32 comparison.*/
330                    e.address.ipv4.min = ntohl(*(u32 *) min_address);
331                    e.address.ipv4.max = ntohl(*(u32 *) max_address);
332                    break;
333            default:
334                    if (w[2][0] != '@')
335                            return -EINVAL;
336                    e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
337                    e.address.group = ccs_get_address_group(w[2] + 1);
338                    if (!e.address.group)
339                            return -ENOMEM;
340                  break;                  break;
                           
341          }          }
342          AuditNetworkLog(is_ipv6, keyword, address, port, found);          if (!ccs_parse_number_union(w[3], &e.port))
343          if (found) return 0;                  goto out;
344          if (TomoyoVerboseMode()) {          mutex_lock(&ccs_policy_lock);
345                  if (is_ipv6) {          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
346                          char buf[64];                  struct ccs_ip_network_acl *acl =
347                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                          container_of(ptr, struct ccs_ip_network_acl, head);
348                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));                  if (ptr->type != CCS_TYPE_IP_NETWORK_ACL ||
349                        ptr->cond != condition ||
350                        ccs_memcmp(acl, &e, offsetof(typeof(e), address_type),
351                                   sizeof(e)))
352                            continue;
353                    if (is_delete) {
354                            acl->perm &= ~e.perm;
355                            if (!acl->perm)
356                                    ptr->is_deleted = true;
357                  } else {                  } else {
358                          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)
359                                    acl->perm = 0;
360                            acl->perm |= e.perm;
361                            ptr->is_deleted = false;
362                  }                  }
363                    error = 0;
364                    break;
365          }          }
366          AuditNetworkLog(is_ipv6, keyword, address, port, 0);          if (!is_delete && error) {
367          if (is_enforce) {                  struct ccs_ip_network_acl *entry =
368                  if (is_ipv6) {                          ccs_commit_ok(&e, sizeof(e));
369                          char buf[64];                  if (entry) {
370                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                          ccs_add_domain_acl(domain, &entry->head);
371                          return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);                          error = 0;
372                  }                  }
                 return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);  
373          }          }
374          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);
375          return 0;   out:
376            if (w[2][0] == '@')
377                    ccs_put_address_group(e.address.group);
378            else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
379                    ccs_put_ipv6_address(e.address.ipv6.min);
380                    ccs_put_ipv6_address(e.address.ipv6.max);
381            }
382            ccs_put_number_union(&e.port);
383            return error;
384  }  }
385    
386  int AddNetworkPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete)  /**
387     * ccs_network_listen_acl - Check permission for listen() operation.
388     *
389     * @is_ipv6: True if @address is an IPv6 address.
390     * @address: An IPv4 or IPv6 address.
391     * @port:    Port number.
392     *
393     * Returns 0 on success, negative value otherwise.
394     */
395    static inline int ccs_network_listen_acl(const bool is_ipv6, const u8 *address,
396                                             const u16 port)
397  {  {
398          u8 sock_type, operation, record_type;          return ccs_network_entry(is_ipv6, CCS_NETWORK_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_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_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, const u16 port)
415                  operation = NETWORK_ACL_TCP_LISTEN;  {
416          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) {          u8 operation;
417                  operation = NETWORK_ACL_TCP_ACCEPT;          switch (sock_type) {
418          } else {          case SOCK_STREAM:
419                  goto out;                  operation = CCS_NETWORK_TCP_CONNECT;
420          }                  break;
421          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';          case SOCK_DGRAM:
422          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                  operation = CCS_NETWORK_UDP_CONNECT;
423                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],                  break;
424                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],          default:
425                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                  operation = CCS_NETWORK_RAW_CONNECT;
                             &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);  
426          }          }
427   out: ;          return ccs_network_entry(is_ipv6, operation,
428          return -EINVAL;                                   (const u32 *) address, ntohs(port));
429  }  }
430    
431  int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
432     * ccs_network_bind_acl - Check permission for bind() operation.
433     *
434     * @is_ipv6:   True if @address is an IPv6 address.
435     * @sock_type: Type of socket. (TCP or UDP or RAW)
436     * @address:   An IPv4 or IPv6 address.
437     * @port:      Port number.
438     *
439     * Returns 0 on success, negative value otherwise.
440     */
441    static int ccs_network_bind_acl(const bool is_ipv6, const int sock_type,
442                                    const u8 *address, const u16 port)
443  {  {
444          return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port));          u8 operation;
445            switch (sock_type) {
446            case SOCK_STREAM:
447                    operation = CCS_NETWORK_TCP_BIND;
448                    break;
449            case SOCK_DGRAM:
450                    operation = CCS_NETWORK_UDP_BIND;
451                    break;
452            default:
453                    operation = CCS_NETWORK_RAW_BIND;
454            }
455            return ccs_network_entry(is_ipv6, operation,
456                                     (const u32 *) address, ntohs(port));
457  }  }
458    
459  int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
460     * ccs_network_accept_acl - Check permission for accept() operation.
461     *
462     * @is_ipv6: True if @address is an IPv6 address.
463     * @address: An IPv4 or IPv6 address.
464     * @port:    Port number.
465     *
466     * Returns 0 on success, negative value otherwise.
467     */
468    static inline int ccs_network_accept_acl(const bool is_ipv6, const u8 *address,
469                                             const u16 port)
470  {  {
471          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;
472            current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
473            retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT,
474                                       (const u32 *) address, ntohs(port));
475            current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
476            return retval;
477  }  }
478    
479  int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
480  {   * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.
481          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));   *
482     * @is_ipv6:   True if @address is an IPv6 address.
483     * @sock_type: Type of socket. (UDP or RAW)
484     * @address:   An IPv4 or IPv6 address.
485     * @port:      Port number.
486     *
487     * Returns 0 on success, negative value otherwise.
488     */
489    static inline int ccs_network_sendmsg_acl(const bool is_ipv6,
490                                              const int sock_type,
491                                              const u8 *address, const u16 port)
492    {
493            u8 operation;
494            if (sock_type == SOCK_DGRAM)
495                    operation = CCS_NETWORK_UDP_CONNECT;
496            else
497                    operation = CCS_NETWORK_RAW_CONNECT;
498            return ccs_network_entry(is_ipv6, operation,
499                                     (const u32 *) address, ntohs(port));
500  }  }
501    
502  int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
503     * ccs_network_recvmsg_acl - Check permission for recvmsg() operation.
504     *
505     * @is_ipv6:   True if @address is an IPv6 address.
506     * @sock_type: Type of socket. (UDP or RAW)
507     * @address:   An IPv4 or IPv6 address.
508     * @port:      Port number.
509     *
510     * Returns 0 on success, negative value otherwise.
511     */
512    static inline int ccs_network_recvmsg_acl(const bool is_ipv6,
513                                              const int sock_type,
514                                              const u8 *address, const u16 port)
515  {  {
516          int retval;          int retval;
517          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          const u8 operation
518          retval = CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port));                  = (sock_type == SOCK_DGRAM) ?
519          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                  CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT;
520            current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
521            retval = ccs_network_entry(is_ipv6, operation,
522                                       (const u32 *) address, ntohs(port));
523            current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
524          return retval;          return retval;
525  }  }
526    
527  int CheckNetworkSendMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  #define MAX_SOCK_ADDR 128 /* net/socket.c */
528    
529    /* Check permission for creating a socket. */
530    static int __ccs_socket_create_permission(int family, int type, int protocol)
531  {  {
532          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          int error = 0;
533            /* Nothing to do if I am a kernel service. */
534            if (segment_eq(get_fs(), KERNEL_DS))
535                    return 0;
536            if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
537                    return -EPERM;
538            if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
539                    return -EPERM;
540            if (family != PF_INET && family != PF_INET6)
541                    return 0;
542            switch (type) {
543            case SOCK_STREAM:
544                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE))
545                            error = -EPERM;
546                    break;
547            case SOCK_DGRAM:
548                    if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET))
549                            error = -EPERM;
550                    break;
551            case SOCK_RAW:
552                    if (!ccs_capable(CCS_USE_INET_RAW_SOCKET))
553                            error = -EPERM;
554                    break;
555            }
556            return error;
557  }  }
558    
559  int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /* Check permission for listening a TCP socket. */
560    static int __ccs_socket_listen_permission(struct socket *sock)
561  {  {
562          int retval;          int error = 0;
563          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          char addr[MAX_SOCK_ADDR];
564          retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          int addr_len;
565          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          /* Nothing to do if I am a kernel service. */
566          return retval;          if (segment_eq(get_fs(), KERNEL_DS))
567                    return 0;
568            if (sock->type != SOCK_STREAM)
569                    return 0;
570            switch (sock->sk->sk_family) {
571            case PF_INET:
572            case PF_INET6:
573                    break;
574            default:
575                    return 0;
576            }
577            if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))
578                    return -EPERM;
579            if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))
580                    return -EPERM;
581            switch (((struct sockaddr *) addr)->sa_family) {
582                    struct sockaddr_in6 *addr6;
583                    struct sockaddr_in *addr4;
584            case AF_INET6:
585                    addr6 = (struct sockaddr_in6 *) addr;
586                    error = ccs_network_listen_acl(true,
587                                                   addr6->sin6_addr.s6_addr,
588                                                   addr6->sin6_port);
589                    break;
590            case AF_INET:
591                    addr4 = (struct sockaddr_in *) addr;
592                    error = ccs_network_listen_acl(false,
593                                                   (u8 *) &addr4->sin_addr,
594                                                   addr4->sin_port);
595                    break;
596            }
597            return error;
598    }
599    
600    /* Check permission for setting the remote IP address/port pair of a socket. */
601    static int __ccs_socket_connect_permission(struct socket *sock,
602                                               struct sockaddr *addr, int addr_len)
603    {
604            int error = 0;
605            const unsigned int type = sock->type;
606            /* Nothing to do if I am a kernel service. */
607            if (segment_eq(get_fs(), KERNEL_DS))
608                    return 0;
609            switch (type) {
610            case SOCK_STREAM:
611            case SOCK_DGRAM:
612            case SOCK_RAW:
613                    break;
614            default:
615                    return 0;
616            }
617            switch (addr->sa_family) {
618                    struct sockaddr_in6 *addr6;
619                    struct sockaddr_in *addr4;
620                    u16 port;
621            case AF_INET6:
622                    if (addr_len < SIN6_LEN_RFC2133)
623                            break;
624                    addr6 = (struct sockaddr_in6 *) addr;
625                    if (type != SOCK_RAW)
626                            port = addr6->sin6_port;
627                    else
628                            port = htons(sock->sk->sk_protocol);
629                    error = ccs_network_connect_acl(true, type,
630                                                    addr6->sin6_addr.s6_addr,
631                                                    port);
632                    break;
633            case AF_INET:
634                    if (addr_len < sizeof(struct sockaddr_in))
635                            break;
636                    addr4 = (struct sockaddr_in *) addr;
637                    if (type != SOCK_RAW)
638                            port = addr4->sin_port;
639                    else
640                            port = htons(sock->sk->sk_protocol);
641                    error = ccs_network_connect_acl(false, type,
642                                                    (u8 *) &addr4->sin_addr,
643                                                    port);
644                    break;
645            }
646            if (type != SOCK_STREAM)
647                    return error;
648            switch (sock->sk->sk_family) {
649            case PF_INET:
650            case PF_INET6:
651                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))
652                            error = -EPERM;
653                    break;
654            }
655            return error;
656  }  }
657    
658  /***** TOMOYO Linux end. *****/  /* Check permission for setting the local IP address/port pair of a socket. */
659    static int __ccs_socket_bind_permission(struct socket *sock,
660                                            struct sockaddr *addr, int addr_len)
661    {
662            int error = 0;
663            const unsigned int type = sock->type;
664            /* Nothing to do if I am a kernel service. */
665            if (segment_eq(get_fs(), KERNEL_DS))
666                    return 0;
667            switch (type) {
668            case SOCK_STREAM:
669            case SOCK_DGRAM:
670            case SOCK_RAW:
671                    break;
672            default:
673                    return 0;
674            }
675            switch (addr->sa_family) {
676                    struct sockaddr_in6 *addr6;
677                    struct sockaddr_in *addr4;
678                    u16 port;
679            case AF_INET6:
680                    if (addr_len < SIN6_LEN_RFC2133)
681                            break;
682                    addr6 = (struct sockaddr_in6 *) addr;
683                    if (type != SOCK_RAW)
684                            port = addr6->sin6_port;
685                    else
686                            port = htons(sock->sk->sk_protocol);
687                    error = ccs_network_bind_acl(true, type,
688                                                 addr6->sin6_addr.s6_addr,
689                                                 port);
690                    break;
691            case AF_INET:
692                    if (addr_len < sizeof(struct sockaddr_in))
693                            break;
694                    addr4 = (struct sockaddr_in *) addr;
695                    if (type != SOCK_RAW)
696                            port = addr4->sin_port;
697                    else
698                            port = htons(sock->sk->sk_protocol);
699                    error = ccs_network_bind_acl(false, type,
700                                                 (u8 *) &addr4->sin_addr,
701                                                 port);
702                    break;
703            }
704            return error;
705    }
706    
707    /*
708     * Check permission for accepting a TCP socket.
709     *
710     * Currently, the LSM hook for this purpose is not provided.
711     */
712    static int __ccs_socket_accept_permission(struct socket *sock,
713                                              struct sockaddr *addr)
714    {
715            int error = 0;
716            int addr_len;
717            /* Nothing to do if I am a kernel service. */
718            if (segment_eq(get_fs(), KERNEL_DS))
719                    return 0;
720            switch (sock->sk->sk_family) {
721            case PF_INET:
722            case PF_INET6:
723                    break;
724            default:
725                    return 0;
726            }
727            error = sock->ops->getname(sock, addr, &addr_len, 2);
728            if (error)
729                    return error;
730            switch (addr->sa_family) {
731                    struct sockaddr_in6 *addr6;
732                    struct sockaddr_in *addr4;
733            case AF_INET6:
734                    addr6 = (struct sockaddr_in6 *) addr;
735                    error = ccs_network_accept_acl(true,
736                                                   addr6->sin6_addr.s6_addr,
737                                                   addr6->sin6_port);
738                    break;
739            case AF_INET:
740                    addr4 = (struct sockaddr_in *) addr;
741                    error = ccs_network_accept_acl(false,
742                                                   (u8 *) &addr4->sin_addr,
743                                                   addr4->sin_port);
744                    break;
745            }
746            return error;
747    }
748    
749    /* Check permission for sending a datagram via a UDP or RAW socket. */
750    static int __ccs_socket_sendmsg_permission(struct socket *sock,
751                                               struct msghdr *msg, int size)
752    {
753            struct sockaddr *addr = (struct sockaddr *) msg->msg_name;
754            const int addr_len = msg->msg_namelen;
755            int error = 0;
756            const int type = sock->type;
757            /* Nothing to do if I am a kernel service. */
758            if (segment_eq(get_fs(), KERNEL_DS))
759                    return 0;
760            if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))
761                    return 0;
762            switch (addr->sa_family) {
763                    struct sockaddr_in6 *addr6;
764                    struct sockaddr_in *addr4;
765                    u16 port;
766            case AF_INET6:
767                    if (addr_len < SIN6_LEN_RFC2133)
768                            break;
769                    addr6 = (struct sockaddr_in6 *) addr;
770                    if (type == SOCK_DGRAM)
771                            port = addr6->sin6_port;
772                    else
773                            port = htons(sock->sk->sk_protocol);
774                    error = ccs_network_sendmsg_acl(true, type,
775                                                    addr6->sin6_addr.s6_addr,
776                                                    port);
777                    break;
778            case AF_INET:
779                    if (addr_len < sizeof(struct sockaddr_in))
780                            break;
781                    addr4 = (struct sockaddr_in *) addr;
782                    if (type == SOCK_DGRAM)
783                            port = addr4->sin_port;
784                    else
785                            port = htons(sock->sk->sk_protocol);
786                    error = ccs_network_sendmsg_acl(false, type,
787                                                    (u8 *) &addr4->sin_addr, port);
788                    break;
789            }
790            return error;
791    }
792    
793    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
794    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
795    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
796    
797    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
798    {
799            return skb->nh.iph;
800    }
801    
802    static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
803    {
804            return skb->h.uh;
805    }
806    
807    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
808    {
809            return skb->nh.ipv6h;
810    }
811    
812    #endif
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    static int __ccs_socket_recvmsg_permission(struct sock *sk,
860                                               struct sk_buff *skb,
861                                               const unsigned int flags)
862    {
863            int error = 0;
864            const unsigned int type = sk->sk_type;
865            if (type != SOCK_DGRAM && type != SOCK_RAW)
866                    return 0;
867            /* Nothing to do if I am a kernel service. */
868            if (segment_eq(get_fs(), KERNEL_DS))
869                    return 0;
870    
871            switch (sk->sk_family) {
872                    struct in6_addr sin6;
873                    struct in_addr sin4;
874                    u16 port;
875            case PF_INET6:
876                    if (type == SOCK_DGRAM) { /* UDP IPv6 */
877                            if (skb->protocol == htons(ETH_P_IP)) {
878                                    ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),
879                                                  ip_hdr(skb)->saddr);
880                            } else {
881                                    ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
882                            }
883                            port = udp_hdr(skb)->source;
884                    } else { /* RAW IPv6 */
885                            ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
886                            port = htons(sk->sk_protocol);
887                    }
888                    error = ccs_network_recvmsg_acl(true, type,
889                                                    (u8 *) &sin6, port);
890                    break;
891            case PF_INET:
892                    if (type == SOCK_DGRAM) { /* UDP IPv4 */
893                            sin4.s_addr = ip_hdr(skb)->saddr;
894                            port = udp_hdr(skb)->source;
895                    } else { /* RAW IPv4 */
896                            sin4.s_addr = ip_hdr(skb)->saddr;
897                            port = htons(sk->sk_protocol);
898                    }
899                    error = ccs_network_recvmsg_acl(false, type,
900                                                    (u8 *) &sin4, port);
901                    break;
902            }
903            if (!error)
904                    return 0;
905            /*
906             * Remove from queue if MSG_PEEK is used so that
907             * the head message from unwanted source in receive queue will not
908             * prevent the caller from picking up next message from wanted source
909             * when the caller is using MSG_PEEK flag for picking up.
910             */
911    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
912            if (type == SOCK_DGRAM)
913                    lock_sock(sk);
914    #endif
915            skb_kill_datagram(sk, skb, flags);
916    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
917            if (type == SOCK_DGRAM)
918                    release_sock(sk);
919    #endif
920            /* Hope less harmful than -EPERM. */
921            return -ENOMEM;
922    }
923    
924    void __init ccs_network_init(void)
925    {
926            ccsecurity_ops.socket_create_permission =
927                    __ccs_socket_create_permission;
928            ccsecurity_ops.socket_listen_permission =
929                    __ccs_socket_listen_permission;
930            ccsecurity_ops.socket_connect_permission =
931                    __ccs_socket_connect_permission;
932            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
933            ccsecurity_ops.socket_accept_permission =
934                    __ccs_socket_accept_permission;
935            ccsecurity_ops.socket_sendmsg_permission =
936                    __ccs_socket_sendmsg_permission;
937            ccsecurity_ops.socket_recvmsg_permission =
938                    __ccs_socket_recvmsg_permission;
939    }

Legend:
Removed from v.808  
changed lines
  Added in v.3512

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