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

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

Legend:
Removed from v.1064  
changed lines
  Added in v.3949

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