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

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.5.x/ccs-patch/fs/tomoyo_network.c revision 860 by kumaneko, Fri Jan 4 04:41:41 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.5.3-pre   2008/01/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   */   */
 /***** 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            unsigned int addr_len;
32    };
33    
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  extern struct mutex domain_acl_lock;  /**
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[128];
72            const char *protocol =
73                    ccs_inet_keyword[r->param.inet_network.protocol];
74            const char *operation = ccs_net_keyword[r->param.inet_network.operation];
75            const u32 *address = r->param.inet_network.address;
76            const u16 port = r->param.inet_network.port;
77            if (r->param.inet_network.is_ipv6)
78                    ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
79                                   address, (const struct in6_addr *) address);
80            else
81                    ccs_print_ipv4(buf, sizeof(buf), r->param.inet_network.ip,
82                                   r->param.inet_network.ip);
83            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  /*************************  AUDIT 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  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)  /**
117     * 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          char *buf;          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
130          int len = 256;                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
131          if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM;                             &min[0], &min[1], &min[2], &min[3],
132          if ((buf = InitAuditLog(&len, profile, mode)) == NULL) return -ENOMEM;                             &min[4], &min[5], &min[6], &min[7],
133          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);                             &max[0], &max[1], &max[2], &max[3],
134          if (is_ipv6) {                             &max[4], &max[5], &max[6], &max[7]);
135                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address);          if (count == 8 || count == 16) {
136          } else {                  u8 i;
137                  u32 ip = *address;                  if (count == 8)
138                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));                          memmove(max, min, sizeof(u16) * 8);
139                    for (i = 0; i < 8; i++) {
140                            min[i] = htons(min[i]);
141                            max[i] = htons(max[i]);
142                    }
143                    return CCS_IP_ADDRESS_TYPE_IPv6;
144          }          }
145          snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port);          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
146          return WriteAuditLog(buf, is_granted);                         &min[0], &min[1], &min[2], &min[3],
147                           &max[0], &max[1], &max[2], &max[3]);
148            if (count == 4 || count == 8) {
149                    u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
150                                   + (((u8) min[2]) << 8) + (u8) min[3]);
151                    memmove(min, &ip, sizeof(ip));
152                    if (count == 8)
153                            ip = htonl((((u8) max[0]) << 24)
154                                       + (((u8) max[1]) << 16)
155                                       + (((u8) max[2]) << 8) + (u8) max[3]);
156                    memmove(max, &ip, sizeof(ip));
157                    return CCS_IP_ADDRESS_TYPE_IPv4;
158            }
159            return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
160  }  }
161    
162  /*************************  UTILITY FUNCTIONS  *************************/  /**
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            memset(buffer, 0, buffer_len);
176            snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u",
177                     HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-',
178                     HIPQUAD(max_ip));
179    }
180    
181  /* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */  #if !defined(NIP6)
182  static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr)  #define NIP6(addr)                                                      \
183            ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]),         \
184                    ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
185                    ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
186                    ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7])
187    #endif
188    
189    /**
190     * ccs_print_ipv6 - Print an IPv6 address.
191     *
192     * @buffer:     Buffer to write to.
193     * @buffer_len: Size of @buffer.
194     * @min_ip:     Pointer to "struct in6_addr".
195     * @max_ip:     Pointer to "struct in6_addr".
196     *
197     * Returns nothing.
198     */
199    void ccs_print_ipv6(char *buffer, const int buffer_len,
200                        const struct in6_addr *min_ip,
201                        const struct in6_addr *max_ip)
202  {  {
203          static const u8 block_size = 16;          memset(buffer, 0, buffer_len);
204          struct addr_list {          snprintf(buffer, buffer_len - 1,
205                  struct in6_addr addr[block_size];                   "%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x",
206                  struct list1_head list;                   NIP6(*min_ip), min_ip == max_ip ? '\0' : '-',
207                  u32 in_use_count;                   NIP6(*max_ip));
208          };  }
209          static LIST1_HEAD(address_list);  
210          struct addr_list *ptr;  static bool ccs_check_inet_acl(struct ccs_request_info *r,
211          static DEFINE_MUTEX(lock);                                 const struct ccs_acl_info *ptr)
212          u8 i = block_size;  {
213          if (!addr) return NULL;          const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
214          mutex_lock(&lock);          bool ret;
215          list1_for_each_entry(ptr, &address_list, list) {          if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
216                  for (i = 0; i < ptr->in_use_count; i++) {              !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
217                          if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0) goto ok;                  return false;
218                  }          switch (acl->address_type) {
219                  if (i < block_size) break;          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
220          }                  ret = ccs_address_matches_group(r->param.inet_network.is_ipv6,
221          if (i == block_size) {                                                  r->param.inet_network.address,
222                  ptr = alloc_element(sizeof(*ptr));                                                  acl->address.group);
223                  if (!ptr) goto ok;                  break;
224                  list1_add_tail_mb(&ptr->list, &address_list);          case CCS_IP_ADDRESS_TYPE_IPv4:
225                  i = 0;                  ret = !r->param.inet_network.is_ipv6 &&
226          }                          acl->address.ipv4.min <= r->param.inet_network.ip &&
227          ptr->addr[ptr->in_use_count++] = *addr;                          r->param.inet_network.ip <= acl->address.ipv4.max;
228  ok:                  break;
229          mutex_unlock(&lock);          default:
230          return ptr ? &ptr->addr[i] : NULL;                  ret = r->param.inet_network.is_ipv6 &&
231  }                          memcmp(acl->address.ipv6.min,
232                                   r->param.inet_network.address, 16) <= 0 &&
233  /*************************  ADDRESS GROUP HANDLER  *************************/                          memcmp(r->param.inet_network.address,
234                                   acl->address.ipv6.max, 16) <= 0;
 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;  
                 }  
                 found = 1;  
235                  break;                  break;
236          }          }
237          if (is_delete) {          return ret;
238                  error = -ENOENT;  }
239                  goto out;  
240    static bool ccs_check_unix_acl(struct ccs_request_info *r,
241                                   const struct ccs_acl_info *ptr)
242    {
243            const struct ccs_unix_acl *acl = container_of(ptr, typeof(*acl), head);
244            return (acl->perm & (1 << r->param.unix_network.operation)) &&
245                    ccs_compare_name_union(r->param.unix_network.address,
246                                           &acl->name);
247    }
248    
249    static const u8
250    ccs_inet2mac[CCS_MAX_INET_PROTOCOL][CCS_MAX_NETWORK_OPERATION] = {
251            [CCS_NETWORK_INET_TCP_PROTOCOL] = {
252                    [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          if (!found) {          ccs_read_unlock(idx);
318                  if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out;          return error;
319                  INIT_LIST1_HEAD(&new_group->address_group_member_list);  }
320                  new_group->group_name = saved_group_name;  
321                  list1_add_tail_mb(&new_group->list, &address_group_list);  /**
322                  group = new_group;   * ccs_unix_entry - Check permission for UNIX network operation.
323          }   *
324          if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out;   * @address: Pointer to "struct ccs_addr_info".
325          new_member->is_ipv6 = is_ipv6;   *
326          if (is_ipv6) {   * Returns 0 on success, negative value otherwise.
327                  new_member->min.ipv6 = saved_min_address;   */
328                  new_member->max.ipv6 = saved_max_address;  static int ccs_unix_entry(const struct ccs_addr_info *address)
329          } else {  {
330                  new_member->min.ipv4 = * (u32 *) min_address;          const int idx = ccs_read_lock();
331                  new_member->max.ipv4 = * (u32 *) max_address;          struct ccs_request_info r;
332            int error = 0;
333            const u8 type = ccs_unix2mac[address->protocol][address->operation];
334            if (type && address->unix0.addr_len > sizeof(sa_family_t) &&
335                ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
336                    char *buf;
337                    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          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);          ccs_read_unlock(idx);
         error = 0;  
  out:  
         mutex_unlock(&lock);  
360          return error;          return error;
361  }  }
362    
363  int AddAddressGroupPolicy(char *data, const bool is_delete)  static bool ccs_same_inet_acl(const struct ccs_acl_info *a,
364                                          const struct ccs_acl_info *b)
365  {  {
366          u8 count;          const struct ccs_inet_acl *p1 = container_of(a, typeof(*p1),
367          bool is_ipv6;                                                               head);
368          u16 min_address[8], max_address[8];          const struct ccs_inet_acl *p2 = container_of(b, typeof(*p2),
369          char *cp = strchr(data, ' ');                                                               head);
370          if (!cp) return -EINVAL;          return ccs_same_acl_head(&p1->head, &p2->head)
371          *cp++ = '\0';                  && p1->protocol == p2->protocol
372          if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                  && p1->address_type == p2->address_type &&
373                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],                  p1->address.ipv4.min == p2->address.ipv4.min &&
374                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],                  p1->address.ipv6.min == p2->address.ipv6.min &&
375                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                  p1->address.ipv4.max == p2->address.ipv4.max &&
376                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                  p1->address.ipv6.max == p2->address.ipv6.max &&
377                  u8 i;                  p1->address.group == p2->address.group &&
378                  for (i = 0; i < 8; i++) {                  ccs_same_number_union(&p1->port, &p2->port);
379                          min_address[i] = htons(min_address[i]);  }
380                          max_address[i] = htons(max_address[i]);  
381                  }  static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
382                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                                        const struct ccs_acl_info *b)
383                  is_ipv6 = 1;  {
384          } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1),
385                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],                                                               head);
386                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {          const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2),
387                  u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);                                                               head);
388                  * (u32 *) min_address = ip;          return ccs_same_acl_head(&p1->head, &p2->head) &&
389                  if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);                  p1->protocol == p2->protocol &&
390                  * (u32 *) max_address = ip;                  ccs_same_name_union(&p1->name, &p2->name);
391                  is_ipv6 = 0;  }
392          } else {  
393    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;                  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            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          return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete);          ccs_put_number_union(&e.port);
498            return error;
499    }
500    
501    /**
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  static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name)  #ifndef CONFIG_NET
542    
543    void __init ccs_network_init(void)
544  {  {
         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) == 0) return group;  
                 }  
                 if (i == 0) {  
                         const u16 dummy[2] = { 0, 0 };  
                         AddAddressGroupEntry(group_name, 0, dummy, dummy, 0);  
                         AddAddressGroupEntry(group_name, 0, dummy, dummy, 1);  
                 }  
         }  
         return NULL;  
545  }  }
546    
547  static bool AddressMatchesToGroup(const bool is_ipv6, const u32 *address, const struct address_group_entry *group)  #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          struct address_group_member *member;          switch (addr->sa_family) {
554          const u32 ip = ntohl(*address);          case AF_INET6:
555          list1_for_each_entry(member, &group->address_group_member_list, list) {                  if (addr_len < SIN6_LEN_RFC2133)
556                  if (member->is_deleted) continue;                          goto skip;
557                  if (member->is_ipv6) {                  address->is_ipv6 = true;
558                          if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1;                  address->address = (u32 *)
559                  } else {                          ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
560                          if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1;                  address->port = ((struct sockaddr_in6 *) addr)->sin6_port;
561                  }                  break;
562            case AF_INET:
563                    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;
570            default:
571                    goto skip;
572          }          }
573          return 0;          return true;
574     skip:
575            return false;
576  }  }
577    
578  int ReadAddressGroupPolicy(struct io_buffer *head)  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 list1_head *gpos;          address->addr = ((struct sockaddr_un *) addr)->sun_path;
583          struct list1_head *mpos;          address->addr_len = addr_len;
584          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {          if (address->addr[0] && addr_len > sizeof(short) &&
585                  struct address_group_entry *group;              addr_len <= sizeof(struct sockaddr_un))
586                  group = list1_entry(gpos, struct address_group_entry, list);                  ((char *) addr)[addr_len] = '\0';
                 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, *max_address = member->max.ipv6;  
                                 print_ipv6(buf, sizeof(buf), min_address);  
                                 if (min_address != max_address) {  
                                         char *cp = strchr(buf, '\0');  
                                         *cp++ = '-';  
                                         print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);  
                                 }  
                         } else {  
                                 const u32 min_address = member->min.ipv4, 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 (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) return -ENOMEM;  
                 }  
         }  
         return 0;  
587  }  }
588    
589  /*************************  NETWORK NETWORK ACL HANDLER  *************************/  static bool ccs_kernel_service(void)
590    {
591            /* Nothing to do if I am a kernel service. */
592            return segment_eq(get_fs(), KERNEL_DS);
593    }
594    
595  #if !defined(NIP6)  static u8 ccs_sock_family(struct socket *sock)
596  #define NIP6(addr) \  {
597          ntohs((addr).s6_addr16[0]), \          if (ccs_kernel_service())
598          ntohs((addr).s6_addr16[1]), \                  return 0;
599          ntohs((addr).s6_addr16[2]), \          switch (sock->sk->sk_family) {
600          ntohs((addr).s6_addr16[3]), \          case PF_INET:
601          ntohs((addr).s6_addr16[4]), \          case PF_INET6:
602          ntohs((addr).s6_addr16[5]), \                  return 1;
603          ntohs((addr).s6_addr16[6]), \          case PF_UNIX:
604          ntohs((addr).s6_addr16[7])                  return 2;
605  #endif          default:
606                    return 0;
607            }
608    }
609    
610  char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)  /* Check permission for creating a socket. */
611    static int __ccs_socket_create_permission(int family, int type, int protocol)
612  {  {
613          memset(buffer, 0, buffer_len);          if (ccs_kernel_service())
614          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));                  return 0;
615          return buffer;          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  const char *net_operation2keyword(const u8 operation)  /* Check permission for listening a socket. */
623    static int __ccs_socket_listen_permission(struct socket *sock)
624  {  {
625          const char *keyword = "unknown";          struct sockaddr_storage addr;
626          switch (operation) {          int error;
627          case NETWORK_ACL_UDP_BIND:          int addr_len;
628                  keyword = "UDP bind";          struct ccs_addr_info address;
629                  break;          const u8 family = ccs_sock_family(sock);
630          case NETWORK_ACL_UDP_CONNECT:          if (!family || (sock->type != SOCK_STREAM &&
631                  keyword = "UDP connect";                          sock->type != SOCK_SEQPACKET))
632                  break;                  return 0;
633          case NETWORK_ACL_TCP_BIND:          error = sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len,
634                  keyword = "TCP bind";                                     0);
635                  break;          if (error)
636          case NETWORK_ACL_TCP_LISTEN:                  return error;
637                  keyword = "TCP listen";          address.operation = CCS_NETWORK_LISTEN;
638                  break;          if (family == 2) {
639          case NETWORK_ACL_TCP_CONNECT:                  address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
640                  keyword = "TCP connect";                  ccs_check_unix_address((struct sockaddr *) &addr, addr_len,
641                  break;                                         &address.unix0);
642          case NETWORK_ACL_TCP_ACCEPT:                  error = ccs_unix_entry(&address);
643                  keyword = "TCP accept";          } else {
644                  break;                  address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
645          case NETWORK_ACL_RAW_BIND:                  if (ccs_check_inet_address((struct sockaddr *) &addr, addr_len,
646                  keyword = "RAW bind";                                             &address.inet))
647                  break;                          error = ccs_inet_entry(&address);
648          case NETWORK_ACL_RAW_CONNECT:          }
649                  keyword = "RAW connect";          return error;
650                  break;  }
651          }  
652          return keyword;  /* 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  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)  {
656  {          int error = 0;
657          struct acl_info *ptr;          const unsigned int type = sock->type;
658          struct ip_network_acl_record *acl;          struct ccs_addr_info address;
659          struct ip_network_acl_record_with_condition *p;          const u8 family = ccs_sock_family(sock);
660          int error = -ENOMEM;          if (!family)
661          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/                  return 0;
662          const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;          address.operation = CCS_NETWORK_CONNECT;
663          if (!domain) return -EINVAL;          if (family == 2) {
664          if (record_type == IP_RECORD_TYPE_IPv6) {                  switch (type) {
665                  if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL                  case SOCK_STREAM:
666                      || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;                          address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
667          }                          break;
668          mutex_lock(&domain_acl_lock);                  case SOCK_DGRAM:
669          if (!is_delete) {                          address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
670                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {                          break;
671                          switch (ptr->type) {                  case SOCK_SEQPACKET:
672                          case TYPE_IP_NETWORK_ACL:                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
673                                  if (condition) continue;                          break;
674                                  acl = container_of(ptr, struct ip_network_acl_record, head);                  default:
675                                  break;                          return 0;
                         case TYPE_IP_NETWORK_ACL_WITH_CONDITION:  
                                 p = container_of(ptr, struct ip_network_acl_record_with_condition, record.head);  
                                 if (p->condition != condition) continue;  
                                 acl = &p->record;  
                                 break;  
                         default:  
                                 continue;  
                         }  
                         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;  
                                 acl->is_deleted = 0;  
                                 /* Found. Nothing to do. */  
                                 error = 0;  
                                 goto out;  
                         } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                                 if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue;  
                                 acl->is_deleted = 0;  
                                 /* Found. Nothing to do. */  
                                 error = 0;  
                                 goto out;  
                         } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                                 if (acl->u.ipv6.min != saved_min_address || saved_max_address != acl->u.ipv6.max) continue;  
                                 acl->is_deleted = 0;  
                                 /* Found. Nothing to do. */  
                                 error = 0;  
                                 goto out;  
                         }  
                 }  
                 /* Not found. Append it to the tail. */  
                 if (condition) {  
                         if ((p = alloc_element(sizeof(*p))) == NULL) goto out;  
                         acl = &p->record;  
                         p->condition = condition;  
                         acl->head.type = TYPE_IP_NETWORK_ACL_WITH_CONDITION;  
                 } else {  
                         if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out;  
                         acl->head.type = TYPE_IP_NETWORK_ACL;  
                 }  
                 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;  
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                          switch (ptr->type) {                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
683                          case TYPE_IP_NETWORK_ACL:                          break;
684                                  if (condition) continue;                  case SOCK_DGRAM:
685                                  acl = container_of(ptr, struct ip_network_acl_record, head);                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
686                                  break;                          break;
687                          case TYPE_IP_NETWORK_ACL_WITH_CONDITION:                  case SOCK_RAW:
688                                  p = container_of(ptr, struct ip_network_acl_record_with_condition, record.head);                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
                                 if (p->condition != condition) continue;  
                                 acl = &p->record;  
                                 break;  
                         default:  
                                 continue;  
                         }  
                         if (acl->operation_type != operation || acl->record_type != record_type || acl->min_port != min_port || max_port != acl->max_port || acl->is_deleted) 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;  
                         }  
                         acl->is_deleted = 1;  
                         error = DelDomainACL();  
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                  struct ip_network_acl_record_with_condition *p;                  case SOCK_STREAM:
715                  const struct condition_list *cond;                          address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
                 switch (ptr->type) {  
                 default:  
                         continue;  
                 case TYPE_IP_NETWORK_ACL:  
                         acl = container_of(ptr, struct ip_network_acl_record, head);  
                         cond = NULL;  
                         break;  
                 case TYPE_IP_NETWORK_ACL_WITH_CONDITION:  
                         p = container_of(ptr, struct ip_network_acl_record_with_condition, record.head);  
                         acl = &p->record;  
                         cond = p->condition;  
716                          break;                          break;
717                    case SOCK_DGRAM:
718                            address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
719                            break;
720                    case SOCK_SEQPACKET:
721                            address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
722                            break;
723                    default:
724                            return 0;
725                  }                  }
726                  if (acl->is_deleted || acl->operation_type != operation || port < acl->min_port || acl->max_port < port || !CheckCondition(cond, NULL)) continue;                  ccs_check_unix_address(addr, addr_len, &address.unix0);
727                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  error = ccs_unix_entry(&address);
728                          if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue;          } else {
729                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {                  switch (type) {
730                          if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue;                  case SOCK_STREAM:
731                  } else {                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
732                          if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue;                          break;
733                  }                  case SOCK_DGRAM:
734                  found = 1;                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
735                  break;                          break;
736          }                  case SOCK_RAW:
737          AuditNetworkLog(is_ipv6, keyword, address, port, found, profile, mode);                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
738          if (found) return 0;                          break;
739          if (TomoyoVerboseMode()) {                  default:
740                  if (is_ipv6) {                          return 0;
                         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));  
                 }  
         }  
         if (is_enforce) {  
                 if (is_ipv6) {  
                         char buf[64];  
                         print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);  
                         return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);  
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.860  
changed lines
  Added in v.3911

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