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

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 1084 by kumaneko, Thu Apr 3 09:54:46 2008 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/network.c revision 3927 by kumaneko, Fri Aug 27 03:48:02 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/08/01
  *  
  * Version: 1.6.0   2008/04/01  
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 <linux/un.h>
18    #include <net/sock.h>
19    #include <net/af_unix.h>
20    #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);  
                 }  
155          }          }
156          return NULL;          return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
157  }  }
158    
159  /**  /**
160   * 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".  
161   *   *
162   * Returns true if @address matches addresses in @group group, false otherwise.   * @buffer:     Buffer to write to.
163   */   * @buffer_len: Size of @buffer.
164  static bool address_matches_to_group(const bool is_ipv6, const u32 *address,   * @min_ip:     Min address in host byte order.
165                                       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".  
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 396  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 ccs_same_acl_head(&p1->head, &p2->head)
293                    && p1->protocol == p2->protocol
294                    && p1->address_type == p2->address_type &&
295                    p1->address.ipv4.min == p2->address.ipv4.min &&
296                    p1->address.ipv6.min == p2->address.ipv6.min &&
297                    p1->address.ipv4.max == p2->address.ipv4.max &&
298                    p1->address.ipv6.max == p2->address.ipv6.max &&
299                    p1->address.group == p2->address.group &&
300                    ccs_same_number_union(&p1->port, &p2->port);
301    }
302    
303    static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
304                                          const struct ccs_acl_info *b)
305    {
306            const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1), head);
307            const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2), head);
308            return ccs_same_acl_head(&p1->head, &p2->head) &&
309                    p1->protocol == p2->protocol &&
310                    ccs_same_name_union(&p1->name, &p2->name);
311    }
312    
313    static bool ccs_merge_inet_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
314                                   const bool is_delete)
315    {
316            u8 * const a_perm = &container_of(a, struct ccs_inet_acl, head)->perm;
317            u8 perm = *a_perm;
318            const u8 b_perm = container_of(b, struct ccs_inet_acl, head)->perm;
319            if (is_delete)
320                    perm &= ~b_perm;
321            else
322                    perm |= b_perm;
323            *a_perm = perm;
324            return !perm;
325    }
326    
327    static bool ccs_merge_unix_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
328                                   const bool is_delete)
329    {
330            u8 * const a_perm = &container_of(a, struct ccs_unix_acl, head)->perm;
331            u8 perm = *a_perm;
332            const u8 b_perm = container_of(b, struct ccs_unix_acl, head)->perm;
333            if (is_delete)
334                    perm &= ~b_perm;
335            else
336                    perm |= b_perm;
337            *a_perm = perm;
338            return !perm;
339  }  }
340    
341  /**  /**
342   * ccs_net2keyword - Convert network operation index to network operation name.   * ccs_write_inet_network - Write "struct ccs_inet_acl" list.
343   *   *
344   * @operation: Type of operation.   * @data:      String to parse.
345     * @domain:    Pointer to "struct ccs_domain_info".
346     * @condition: Pointer to "struct ccs_condition". Maybe NULL.
347     * @is_delete: True if it is a delete request.
348   *   *
349   * Returns the name of operation.   * Returns 0 on success, negative value otherwise.
350   */   */
351  const char *ccs_net2keyword(const u8 operation)  int ccs_write_inet_network(char *data, struct ccs_domain_info *domain,
352  {                             struct ccs_condition *condition,
353          const char *keyword = "unknown";                             const bool is_delete)
354          switch (operation) {  {
355          case NETWORK_ACL_UDP_BIND:          struct ccs_inet_acl e = {
356                  keyword = "UDP bind";                  .head.type = CCS_TYPE_INET_ACL,
357                  break;                  .head.cond = condition,
358          case NETWORK_ACL_UDP_CONNECT:          };
359                  keyword = "UDP connect";          u16 min_address[8];
360                  break;          u16 max_address[8];
361          case NETWORK_ACL_TCP_BIND:          int error = is_delete ? -ENOENT : -ENOMEM;
362                  keyword = "TCP bind";          char *w[4];
363                  break;          u8 type;
364          case NETWORK_ACL_TCP_LISTEN:          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
365                  keyword = "TCP listen";                  return -EINVAL;
366                  break;          for (e.protocol = 0; e.protocol < CCS_SOCK_MAX; e.protocol++)
367          case NETWORK_ACL_TCP_CONNECT:                  if (!strcmp(w[0], ccs_proto_keyword[e.protocol]))
368                  keyword = "TCP connect";                          break;
369                  break;          for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
370          case NETWORK_ACL_TCP_ACCEPT:                  if (ccs_permstr(w[1], ccs_socket_keyword[type]))
371                  keyword = "TCP accept";                          e.perm |= 1 << type;
372            if (e.protocol == CCS_SOCK_MAX || !e.perm)
373                    return -EINVAL;
374            switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
375            case CCS_IP_ADDRESS_TYPE_IPv6:
376                    e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
377                    e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
378                                                                min_address);
379                    e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
380                                                                max_address);
381                    if (!e.address.ipv6.min || !e.address.ipv6.max)
382                            goto out;
383                  break;                  break;
384          case NETWORK_ACL_RAW_BIND:          case CCS_IP_ADDRESS_TYPE_IPv4:
385                  keyword = "RAW bind";                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
386                    /* use host byte order to allow u32 comparison.*/
387                    e.address.ipv4.min = ntohl(*(u32 *) min_address);
388                    e.address.ipv4.max = ntohl(*(u32 *) max_address);
389                  break;                  break;
390          case NETWORK_ACL_RAW_CONNECT:          default:
391                  keyword = "RAW connect";                  if (w[2][0] != '@')
392                            return -EINVAL;
393                    e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
394                    e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP);
395                    if (!e.address.group)
396                            return -ENOMEM;
397                  break;                  break;
398          }          }
399          return keyword;          if (!ccs_parse_number_union(w[3], &e.port))
400                    goto out;
401            error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
402                                      ccs_same_inet_acl, ccs_merge_inet_acl);
403     out:
404            if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
405                    ccs_put_group(e.address.group);
406            else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
407                    ccs_put_ipv6_address(e.address.ipv6.min);
408                    ccs_put_ipv6_address(e.address.ipv6.max);
409            }
410            ccs_put_number_union(&e.port);
411            return error;
412  }  }
413    
414  /**  /**
415   * update_network_entry - Update "struct ip_network_acl_record" list.   * ccs_write_unix_network - Write "struct ccs_unix_acl" list.
416   *   *
417   * @operation:   Type of operation.   * @data:      String to parse.
418   * @record_type: Type of address.   * @domain:    Pointer to "struct ccs_domain_info".
419   * @group:       Pointer to "struct address_group_entry". May be NULL.   * @condition: Pointer to "struct ccs_condition". Maybe NULL.
420   * @min_address: Start of IPv4 or IPv6 address range.   * @is_delete: True if it is a delete request.
  * @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.  
421   *   *
422   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
423   */   */
424  static int update_network_entry(const u8 operation, const u8 record_type,  int ccs_write_unix_network(char *data, struct ccs_domain_info *domain,
425                                  const struct address_group_entry *group,                             struct ccs_condition *condition,
426                                  const u32 *min_address, const u32 *max_address,                             const bool is_delete)
427                                  const u16 min_port, const u16 max_port,  {
428                                  struct domain_info *domain,          struct ccs_unix_acl e = {
429                                  const struct condition_list *condition,                  .head.type = CCS_TYPE_UNIX_ACL,
430                                  const bool is_delete)                  .head.cond = condition,
431  {          };
432          struct acl_info *ptr;          int error = is_delete ? -ENOENT : -ENOMEM;
433          struct ip_network_acl_record *acl;          char *w[3];
434          int error = -ENOMEM;          u8 type;
435          /* using host byte order to allow u32 comparison than memcmp().*/          if (!ccs_tokenize(data, w, sizeof(w)) || !w[2][0])
         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)  
436                  return -EINVAL;                  return -EINVAL;
437          if (record_type != IP_RECORD_TYPE_IPv6)          for (e.protocol = 0; e.protocol < CCS_SOCK_MAX; e.protocol++)
438                  goto not_ipv6;                  if (!strcmp(w[0], ccs_proto_keyword[e.protocol]))
439          saved_min_address = save_ipv6_address((struct in6_addr *) min_address);                          break;
440          saved_max_address = save_ipv6_address((struct in6_addr *) max_address);          for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
441          if (!saved_min_address || !saved_max_address)                  if (ccs_permstr(w[1], ccs_socket_keyword[type]))
442                  return -ENOMEM;                          e.perm |= 1 << type;
443   not_ipv6:          if (e.protocol == CCS_SOCK_MAX || !e.perm)
444          mutex_lock(&domain_acl_lock);                  return -EINVAL;
445          if (is_delete)          if (!ccs_parse_name_union(w[2], &e.name))
446                  goto delete;                  return -EINVAL;
447          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
448                  if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)                                    ccs_same_unix_acl, ccs_merge_unix_acl);
449                          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);  
450          return error;          return error;
451  }  }
452    
453  /**  #ifndef CONFIG_NET
454   * check_network_entry - Check permission for network operation.  
455   *  void __init ccs_network_init(void)
456   * @is_ipv6:   True if @address is an IPv6 address.  {
457   * @operation: Type of operation.          u8 i;
458   * @address:   An IPv4 or IPv6 address.          for (i = 0; i < CCS_SOCK_MAX; i++)
459   * @port:      Port number.                  if (!ccs_proto_keyword[i])
460                            ccs_proto_keyword[i] = "unknown";
461    }
462    
463    #else
464    
465    /**
466     * ccs_inet_entry - Check permission for INET network operation.
467     *
468     * @address: Pointer to "struct ccs_addr_info".
469   *   *
470   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
471   */   */
472  static int check_network_entry(const bool is_ipv6, const u8 operation,  static int ccs_inet_entry(const struct ccs_addr_info *address)
473                                 const u32 *address, const u16 port)  {
474  {          const int idx = ccs_read_lock();
475          struct domain_info * const domain = current->domain_info;          struct ccs_request_info r;
476          struct acl_info *ptr;          int error = 0;
477          const char *keyword = ccs_net2keyword(operation);          const u8 type = ccs_inet2mac[address->protocol][address->operation];
478          const u8 profile = current->domain_info->profile;          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
479          const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_NETWORK);                  struct task_struct * const task = current;
480          const bool is_enforce = (mode == 3);                  const bool no_sleep = address->operation == CCS_NETWORK_ACCEPT
481          /* using host byte order to allow u32 comparison than memcmp().*/                          || address->operation == CCS_NETWORK_RECV;
482          const u32 ip = ntohl(*address);                  r.param_type = CCS_TYPE_INET_ACL;
483          bool found = false;                  r.param.inet_network.protocol = address->protocol;
484          char buf[64];                  r.param.inet_network.operation = address->operation;
485          if (!mode)                  r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
486                  return 0;                  r.param.inet_network.address = address->inet.address;
487          list1_for_each_entry(ptr, &domain->acl_info_list, list) {                  r.param.inet_network.port = ntohs(address->inet.port);
488                  struct ip_network_acl_record *acl;                  /* use host byte order to allow u32 comparison than memcmp().*/
489                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)                  r.param.inet_network.ip = ntohl(*address->inet.address);
490                          continue;                  if (no_sleep)
491                  acl = container_of(ptr, struct ip_network_acl_record, head);                          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
492                  if (acl->operation_type != operation || port < acl->min_port ||                  do {
493                      acl->max_port < port || !ccs_check_condition(ptr, NULL))                          ccs_check_acl(&r, ccs_check_inet_acl);
494                          continue;                          error = ccs_audit_inet_log(&r);
495                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  } while (error == CCS_RETRY_REQUEST);
496                          if (!address_matches_to_group(is_ipv6, address,                  if (no_sleep)
497                                                        acl->u.group))                          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
498                                  continue;          }
499                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {          ccs_read_unlock(idx);
500                          if (is_ipv6 ||          return error;
501                              ip < acl->u.ipv4.min || acl->u.ipv4.max < ip)  }
502                                  continue;  
503                  } else {  static int ccs_check_inet_address(const struct sockaddr *addr,
504                          if (!is_ipv6 ||                                    const unsigned int addr_len, const u16 port,
505                              memcmp(acl->u.ipv6.min, address, 16) > 0 ||                                    struct ccs_addr_info *address)
506                              memcmp(address, acl->u.ipv6.max, 16) > 0)  {
507                                  continue;          struct ccs_inet_addr_info *i = &address->inet;
508                  }          switch (addr->sa_family) {
509                  ccs_update_condition(ptr);          case AF_INET6:
510                  found = true;                  if (addr_len < SIN6_LEN_RFC2133)
511                            goto skip;
512                    i->is_ipv6 = true;
513                    i->address = (u32 *)
514                            ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
515                    i->port = ((struct sockaddr_in6 *) addr)->sin6_port;
516                    break;
517            case AF_INET:
518                    if (addr_len < sizeof(struct sockaddr_in))
519                            goto skip;
520                    i->is_ipv6 = false;
521                    i->address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
522                    i->port = ((struct sockaddr_in *) addr)->sin_port;
523                  break;                  break;
524            default:
525                    goto skip;
526          }          }
527          memset(buf, 0, sizeof(buf));          if (address->protocol == SOCK_RAW)
528          if (is_ipv6)                  i->port = htons(port);
529                  ccs_print_ipv6(buf, sizeof(buf),          return ccs_inet_entry(address);
530                                 (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);  
531          return 0;          return 0;
532  }  }
533    
534  /**  /**
535   * ccs_write_network_policy - Write "struct ip_network_acl_record" list.   * ccs_unix_entry - Check permission for UNIX network operation.
536   *   *
537   * @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.  
538   *   *
539   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
540   */   */
541  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)  
542  {  {
543          u8 sock_type;          const int idx = ccs_read_lock();
544          u8 operation;          struct ccs_request_info r;
545          u8 record_type;          int error = 0;
546          u16 min_address[8];          const u8 type = ccs_unix2mac[address->protocol][address->operation];
547          u16 max_address[8];          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
548          struct address_group_entry *group = NULL;                  char *buf;
549          u16 min_port;                  if (address->unix0.addr_len <= sizeof(sa_family_t))
550          u16 max_port;                          buf = ccs_encode2("anonymous", 9);                      
551          u8 count;                  else if (address->unix0.addr[0])
552          char *cp1 = strchr(data, ' ');                          buf = ccs_encode(address->unix0.addr);
553          char *cp2;                  else
554          if (!cp1)                          buf = ccs_encode2(address->unix0.addr,
555                  goto out;                                            address->unix0.addr_len
556          cp1++;                                            - sizeof(sa_family_t));
557          if (!strncmp(data, "TCP ", 4))                  if (buf) {
558                  sock_type = SOCK_STREAM;                          struct task_struct * const task = current;
559          else if (!strncmp(data, "UDP ", 4))                          const bool no_sleep =
560                  sock_type = SOCK_DGRAM;                                  address->operation == CCS_NETWORK_ACCEPT ||
561          else if (!strncmp(data, "RAW ", 4))                                  address->operation == CCS_NETWORK_RECV;
562                  sock_type = SOCK_RAW;                          struct ccs_path_info addr;
563          else                          addr.name = buf;
564                  goto out;                          ccs_fill_path_info(&addr);
565          cp2 = strchr(cp1, ' ');                          r.param_type = CCS_TYPE_UNIX_ACL;
566          if (!cp2)                          r.param.unix_network.protocol = address->protocol;
567                  goto out;                          r.param.unix_network.operation = address->operation;
568          cp2++;                          r.param.unix_network.address = &addr;
569          if (!strncmp(cp1, "bind ", 5))                          if (no_sleep)
570                  switch (sock_type) {                                  task->ccs_flags |=
571                  case SOCK_STREAM:                                          CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
572                          operation = NETWORK_ACL_TCP_BIND;                  
573                          break;                          do {
574                  case SOCK_DGRAM:                                  ccs_check_acl(&r, ccs_check_unix_acl);
575                          operation = NETWORK_ACL_UDP_BIND;                                  error = ccs_audit_unix_log(&r);
576                          break;                          } while (error == CCS_RETRY_REQUEST);
577                  default:                          if (no_sleep)
578                          operation = NETWORK_ACL_RAW_BIND;                                  task->ccs_flags &=
579                  }                                          ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
580          else if (!strncmp(cp1, "connect ", 8))                          kfree(buf);
581                  switch (sock_type) {                  } else
582                  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;  
583          }          }
584   out:          ccs_read_unlock(idx);
585          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);  
586  }  }
587    
588  /**  static int ccs_check_unix_address(struct sockaddr *addr,
589   * ccs_check_network_listen_acl - Check permission for listen() operation.                                    const unsigned int addr_len,
590   *                                    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)  
591  {  {
592          return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,          struct ccs_unix_addr_info *u = &address->unix0;
593                                     (const u32 *) address, ntohs(port));          if (addr->sa_family != AF_UNIX)
594                    return 0;
595            u->addr = ((struct sockaddr_un *) addr)->sun_path;
596            u->addr_len = addr_len;
597            /*
598             * Terminate pathname with '\0' like unix_mkname() does.
599             * This is needed because pathname was copied by move_addr_to_kernel()
600             * but not yet terminated by unix_mkname().
601             */
602            if (u->addr[0] && addr_len > sizeof(short) &&
603                addr_len <= sizeof(struct sockaddr_un))
604                    ((char *) u->addr)[addr_len] = '\0';
605            return ccs_unix_entry(address);
606  }  }
607    
608  /**  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)  
609  {  {
610          u8 operation;          /* Nothing to do if I am a kernel service. */
611          switch (sock_type) {          return segment_eq(get_fs(), KERNEL_DS);
612          case SOCK_STREAM:  }
613                  operation = NETWORK_ACL_TCP_CONNECT;  
614                  break;  static u8 ccs_sock_family(struct sock *sk)
615          case SOCK_DGRAM:  {
616                  operation = NETWORK_ACL_UDP_CONNECT;          u8 family;
617                  break;          if (ccs_kernel_service())
618                    return 0;
619            family = sk->sk_family;
620            switch (family) {
621            case PF_INET:
622            case PF_INET6:
623            case PF_UNIX:
624                    return family;
625          default:          default:
626                  operation = NETWORK_ACL_RAW_CONNECT;                  return 0;
627          }          }
         return check_network_entry(is_ipv6, operation, (const u32 *) address,  
                                    ntohs(port));  
628  }  }
629    
630  /**  /* Check permission for creating a socket. */
631   * 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)  
632  {  {
633          u8 operation;          if (ccs_kernel_service())
634          switch (sock_type) {                  return 0;
635            if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
636                    return -EPERM;
637            if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
638                    return -EPERM;
639            return 0;
640    }
641    
642    /* Check permission for listening a socket. */
643    static int __ccs_socket_listen_permission(struct socket *sock)
644    {
645            struct ccs_addr_info address;
646            const u8 family = ccs_sock_family(sock->sk);
647            const unsigned int type = sock->type;
648            struct sockaddr_storage addr;
649            int addr_len;
650            if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
651                    return 0;
652            {
653                    const int error = sock->ops->getname(sock, (struct sockaddr *)
654                                                         &addr, &addr_len, 0);
655                    if (error)
656                            return error;
657            }
658            address.protocol = type;
659            address.operation = CCS_NETWORK_LISTEN;
660            if (family == PF_UNIX)
661                    return ccs_check_unix_address((struct sockaddr *) &addr,
662                                                  addr_len, &address);
663            return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
664                                          &address);
665    }
666    
667    /* Check permission for setting the remote address of a socket. */
668    static int __ccs_socket_connect_permission(struct socket *sock,
669                                               struct sockaddr *addr, int addr_len)
670    {
671            struct ccs_addr_info address;
672            const u8 family = ccs_sock_family(sock->sk);
673            const unsigned int type = sock->type;
674            if (!family)
675                    return 0;
676            address.protocol = type;
677            switch (type) {
678            case SOCK_DGRAM:
679            case SOCK_RAW:
680                    address.operation = CCS_NETWORK_SEND;
681                    break;
682          case SOCK_STREAM:          case SOCK_STREAM:
683                  operation = NETWORK_ACL_TCP_BIND;          case SOCK_SEQPACKET:
684                    address.operation = CCS_NETWORK_CONNECT;
685                  break;                  break;
686            default:
687                    return 0;
688            }
689            if (family == PF_UNIX)
690                    return ccs_check_unix_address(addr, addr_len, &address);
691            return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
692                                          &address);
693    }
694    
695    /* Check permission for setting the local address of a socket. */
696    static int __ccs_socket_bind_permission(struct socket *sock,
697                                            struct sockaddr *addr, int addr_len)
698    {
699            struct ccs_addr_info address;
700            const u8 family = ccs_sock_family(sock->sk);
701            const unsigned int type = sock->type;
702            if (!family)
703                    return 0;
704            switch (type) {
705            case SOCK_STREAM:
706          case SOCK_DGRAM:          case SOCK_DGRAM:
707                  operation = NETWORK_ACL_UDP_BIND;          case SOCK_RAW:
708            case SOCK_SEQPACKET:
709                    address.protocol = type;
710                    address.operation = CCS_NETWORK_BIND;
711                  break;                  break;
712          default:          default:
713                  operation = NETWORK_ACL_RAW_BIND;                  return 0;
714          }          }
715          return check_network_entry(is_ipv6, operation, (const u32 *) address,          if (family == PF_UNIX)
716                                     ntohs(port));                  return ccs_check_unix_address(addr, addr_len, &address);
717            return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
718                                          &address);
719  }  }
720    
721  /**  /* Check permission for sending a datagram. */
722   * ccs_check_network_accept_acl - Check permission for accept() operation.  static int __ccs_socket_sendmsg_permission(struct socket *sock,
723   *                                             struct msghdr *msg, int size)
724   * @is_ipv6: True if @address is an IPv6 address.  {
725   * @address: An IPv4 or IPv6 address.          struct ccs_addr_info address;
726   * @port:    Port number.          const u8 family = ccs_sock_family(sock->sk);
727   *          const unsigned int type = sock->type;
728   * Returns 0 on success, negative value otherwise.          if (!msg->msg_name || !family ||
729   */              (type != SOCK_DGRAM && type != SOCK_RAW))
730  int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address,                  return 0;
731                                   const u16 port)          address.protocol = type;
732            address.operation = CCS_NETWORK_SEND;
733            if (family == PF_UNIX)
734                    return ccs_check_unix_address((struct sockaddr *)
735                                                  msg->msg_name, msg->msg_namelen,
736                                                  &address);
737            return ccs_check_inet_address((struct sockaddr *) msg->msg_name,
738                                          msg->msg_namelen, sock->sk->sk_protocol,
739                                          &address);
740    }
741    
742    /* Check permission for accepting a socket. */
743    static int __ccs_socket_post_accept_permission(struct socket *sock,
744                                                   struct socket *newsock)
745    {
746            struct ccs_addr_info address;
747            const u8 family = ccs_sock_family(sock->sk);
748            const unsigned int type = sock->type;
749            struct sockaddr_storage addr;
750            int addr_len;
751            if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
752                    return 0;
753            {
754                    const int error = newsock->ops->getname(newsock,
755                                                            (struct sockaddr *)
756                                                            &addr, &addr_len, 2);
757                    if (error)
758                            return error;
759            }
760            address.protocol = type;
761            address.operation = CCS_NETWORK_ACCEPT;
762            if (family == PF_UNIX)
763                    return ccs_check_unix_address((struct sockaddr *) &addr,
764                                                  addr_len, &address);
765            return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
766                                          &address);
767    }
768    
769    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
770    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
771    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
772    
773    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
774  {  {
775          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;  
776  }  }
777    
778  /**  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)  
779  {  {
780          u8 operation;          return skb->h.uh;
781          if (sock_type == SOCK_DGRAM)  }
782                  operation = NETWORK_ACL_UDP_CONNECT;  
783    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
784    {
785            return skb->nh.ipv6h;
786    }
787    
788    #endif
789    #endif
790    #endif
791    
792    /* Check permission for receiving a datagram. */
793    static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
794                                                    struct sk_buff *skb)
795    {
796            struct ccs_addr_info address;
797            const u8 family = ccs_sock_family(sk);
798            const unsigned int type = sk->sk_type;
799            struct sockaddr_storage addr;
800            if (!family)
801                    return 0;
802            switch (type) {
803            case SOCK_DGRAM:
804            case SOCK_RAW:
805                    address.protocol = type;
806                    break;
807            default:
808                    return 0;
809            }
810            address.operation = CCS_NETWORK_RECV;
811            switch (family) {
812            case PF_INET6:
813                    {
814                            struct in6_addr *sin6 = (struct in6_addr *) &addr;
815                            address.inet.is_ipv6 = true;
816                            if (type == SOCK_DGRAM &&
817                                skb->protocol == htons(ETH_P_IP))
818                                    ipv6_addr_set(sin6, 0, 0, htonl(0xffff),
819                                                  ip_hdr(skb)->saddr);
820                            else
821                                    ipv6_addr_copy(sin6, &ipv6_hdr(skb)->saddr);
822                            break;
823                    }
824            case PF_INET:
825                    {
826                            struct in_addr *sin4 = (struct in_addr *) &addr;
827                            address.inet.is_ipv6 = false;
828                            sin4->s_addr = ip_hdr(skb)->saddr;
829                            break;
830                    }
831            default: /* == PF_UNIX */
832                    {
833    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
834                            struct unix_address *u = unix_sk(skb->sk)->addr;
835    #else
836                            struct unix_address *u = skb->sk->protinfo.af_unix.addr;
837    #endif
838                            unsigned int addr_len;
839                            if (!u)
840                                    return 0;
841                            addr_len = u->len;
842                            if (addr_len >= sizeof(addr))
843                                    return 0;
844                            memcpy(&addr, u->name, addr_len);
845                            return ccs_check_unix_address((struct sockaddr *) &addr,
846                                                          addr_len, &address);
847                    }
848            }
849            address.inet.address = (u32 *) &addr;
850            if (type == SOCK_DGRAM)
851                    address.inet.port = udp_hdr(skb)->source;
852          else          else
853                  operation = NETWORK_ACL_RAW_CONNECT;                  address.inet.port = htons(sk->sk_protocol);
854          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_inet_entry(&address);
                                    ntohs(port));  
855  }  }
856    
857  /**  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)  
858  {  {
859          int retval;          u8 i;
860          const u8 operation          for (i = 0; i < CCS_SOCK_MAX; i++)
861                  = (sock_type == SOCK_DGRAM) ?                  if (!ccs_proto_keyword[i])
862                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                          ccs_proto_keyword[i] = "unknown";
863          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          ccsecurity_ops.socket_create_permission =
864          retval = check_network_entry(is_ipv6, operation, (const u32 *) address,                  __ccs_socket_create_permission;
865                                       ntohs(port));          ccsecurity_ops.socket_listen_permission =
866          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                  __ccs_socket_listen_permission;
867          return retval;          ccsecurity_ops.socket_connect_permission =
868                    __ccs_socket_connect_permission;
869            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
870            ccsecurity_ops.socket_post_accept_permission =
871                    __ccs_socket_post_accept_permission;
872            ccsecurity_ops.socket_sendmsg_permission =
873                    __ccs_socket_sendmsg_permission;
874            ccsecurity_ops.socket_post_recvmsg_permission =
875                    __ccs_socket_post_recvmsg_permission;
876  }  }
877    
878    #endif

Legend:
Removed from v.1084  
changed lines
  Added in v.3927

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