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

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

Legend:
Removed from v.912  
changed lines
  Added in v.3911

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