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

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

Legend:
Removed from v.1782  
changed lines
  Added in v.3968

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