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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/network.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1330  
changed lines
  Added in v.3808

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