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

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.7.x/ccs-patch/security/ccsecurity/network.c revision 3502 by kumaneko, Mon Mar 8 08:44:55 2010 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/network.c revision 3915 by kumaneko, Mon Aug 23 02:55:04 2010 UTC
# Line 3  Line 3 
3   *   *
4   * Copyright (C) 2005-2010  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.2-pre   2010/03/08   * Version: 1.8.0-pre   2010/08/01
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 14  Line 14 
14  #include <linux/inet.h>  #include <linux/inet.h>
15  #include <linux/in.h>  #include <linux/in.h>
16  #include <linux/in6.h>  #include <linux/in6.h>
17    #include <linux/un.h>
18  #include <net/ip.h>  #include <net/ip.h>
19  #include <net/ipv6.h>  #include <net/ipv6.h>
20  #include <net/udp.h>  #include <net/udp.h>
21  #include "internal.h"  #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    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    /**
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  /**  /**
94   * ccs_audit_network_log - Audit network log.   * ccs_audit_unix_log - Audit UNIX network log.
95   *   *
96   * @r:          Pointer to "struct ccs_request_info".   * @r: Pointer to "struct ccs_request_info".
  * @operation:  The name of operation.  
  * @address:    An IPv4 or IPv6 address.  
  * @port:       Port number.  
  * @is_granted: True if this is a granted log.  
97   *   *
98   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
99   */   */
100  static int ccs_audit_network_log(struct ccs_request_info *r,  static int ccs_audit_unix_log(struct ccs_request_info *r)
101                                   const char *operation, const char *address,  {
102                                   const u16 port, const bool is_granted)          const char *protocol =
103  {                  ccs_unix_keyword[r->param.unix_network.protocol];
104          if (!is_granted)          const char *operation =
105                  ccs_warn_log(r, "%s %s %u", operation, address, port);                  ccs_net_keyword[r->param.unix_network.operation];
106          return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK          const char *address = r->param.unix_network.address->name;
107                                     "%s %s %u\n", operation, address, port);          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  /**  /**
# Line 47  static int ccs_audit_network_log(struct Line 120  static int ccs_audit_network_log(struct
120   * @min:     Pointer to store min address.   * @min:     Pointer to store min address.
121   * @max:     Pointer to store max address.   * @max:     Pointer to store max address.
122   *   *
123   * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.   * Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6,
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)  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
128  {  {
# Line 65  int ccs_parse_ip_address(char *address, Line 140  int ccs_parse_ip_address(char *address,
140                          min[i] = htons(min[i]);                          min[i] = htons(min[i]);
141                          max[i] = htons(max[i]);                          max[i] = htons(max[i]);
142                  }                  }
143                  return 2;                  return CCS_IP_ADDRESS_TYPE_IPv6;
144          }          }
145          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
146                         &min[0], &min[1], &min[2], &min[3],                         &min[0], &min[1], &min[2], &min[3],
# Line 75  int ccs_parse_ip_address(char *address, Line 150  int ccs_parse_ip_address(char *address,
150                                 + (((u8) min[2]) << 8) + (u8) min[3]);                                 + (((u8) min[2]) << 8) + (u8) min[3]);
151                  memmove(min, &ip, sizeof(ip));                  memmove(min, &ip, sizeof(ip));
152                  if (count == 8)                  if (count == 8)
153                          ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)                          ip = htonl((((u8) max[0]) << 24)
154                                       + (((u8) max[1]) << 16)
155                                     + (((u8) max[2]) << 8) + (u8) max[3]);                                     + (((u8) max[2]) << 8) + (u8) max[3]);
156                  memmove(max, &ip, sizeof(ip));                  memmove(max, &ip, sizeof(ip));
157                  return 1;                  return CCS_IP_ADDRESS_TYPE_IPv4;
158          }          }
159          return 0;          return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
160    }
161    
162    /**
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  #if !defined(NIP6)  #if !defined(NIP6)
# Line 96  int ccs_parse_ip_address(char *address, Line 191  int ccs_parse_ip_address(char *address,
191   *   *
192   * @buffer:     Buffer to write to.   * @buffer:     Buffer to write to.
193   * @buffer_len: Size of @buffer.   * @buffer_len: Size of @buffer.
194   * @ip:         Pointer to "struct in6_addr".   * @min_ip:     Pointer to "struct in6_addr".
195     * @max_ip:     Pointer to "struct in6_addr".
196   *   *
197   * Returns nothing.   * Returns nothing.
198   */   */
199  void ccs_print_ipv6(char *buffer, const int buffer_len,  void ccs_print_ipv6(char *buffer, const int buffer_len,
200                      const struct in6_addr *ip)                      const struct in6_addr *min_ip,
201                        const struct in6_addr *max_ip)
202  {  {
203          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
204          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));          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   * ccs_net2keyword - Convert network operation index to network operation name.  }
209   *  
210   * @operation: Type of operation.  static bool ccs_check_inet_acl(struct ccs_request_info *r,
211   *                                 const struct ccs_acl_info *ptr)
212   * Returns the name of operation.  {
213   */          const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
214  const char *ccs_net2keyword(const u8 operation)          bool ret;
215  {          if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
216          const char *keyword = "unknown";              !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
217          switch (operation) {                  return false;
218          case CCS_NETWORK_UDP_BIND:          switch (acl->address_type) {
219                  keyword = "UDP bind";          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
220                  break;                  ret = ccs_address_matches_group(r->param.inet_network.is_ipv6,
221          case CCS_NETWORK_UDP_CONNECT:                                                  r->param.inet_network.address,
222                  keyword = "UDP connect";                                                  acl->address.group);
223                  break;                  break;
224          case CCS_NETWORK_TCP_BIND:          case CCS_IP_ADDRESS_TYPE_IPv4:
225                  keyword = "TCP bind";                  ret = !r->param.inet_network.is_ipv6 &&
226                  break;                          acl->address.ipv4.min <= r->param.inet_network.ip &&
227          case CCS_NETWORK_TCP_LISTEN:                          r->param.inet_network.ip <= acl->address.ipv4.max;
                 keyword = "TCP listen";  
                 break;  
         case CCS_NETWORK_TCP_CONNECT:  
                 keyword = "TCP connect";  
                 break;  
         case CCS_NETWORK_TCP_ACCEPT:  
                 keyword = "TCP accept";  
                 break;  
         case CCS_NETWORK_RAW_BIND:  
                 keyword = "RAW bind";  
                 break;  
         case CCS_NETWORK_RAW_CONNECT:  
                 keyword = "RAW connect";  
228                  break;                  break;
229          }          default:
230          return keyword;                  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 ret;
238    }
239    
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_SEND]    = CCS_MAC_NETWORK_INET_UDP_SEND,
260                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_UDP_RECV,
261            },
262            [CCS_NETWORK_INET_RAW_PROTOCOL]    = {
263                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_RAW_BIND,
264                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_RAW_SEND,
265                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_RAW_RECV,
266            },
267    };
268    
269    static const u8
270    ccs_unix2mac[CCS_MAX_UNIX_PROTOCOL][CCS_MAX_NETWORK_OPERATION] = {
271            [CCS_NETWORK_UNIX_STREAM_PROTOCOL] = {
272                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_STREAM_BIND,
273                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_STREAM_LISTEN,
274                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_STREAM_CONNECT,
275                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT,
276            },
277            [CCS_NETWORK_UNIX_DGRAM_PROTOCOL] = {
278                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_DGRAM_BIND,
279                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_UNIX_DGRAM_SEND,
280                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_UNIX_DGRAM_RECV,
281            },
282            [CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL] = {
283                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND,
284                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
285                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
286                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT,
287            },
288    };
289    
290  /**  /**
291   * ccs_network_entry2 - Check permission for network operation.   * ccs_inet_entry - Check permission for INET network operation.
292   *   *
293   * @is_ipv6:   True if @address is an IPv6 address.   * @address: Pointer to "struct ccs_addr_info".
  * @operation: Type of operation.  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
294   *   *
295   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
  *  
  * Caller holds ccs_read_lock().  
296   */   */
297  static int ccs_network_entry2(const bool is_ipv6, const u8 operation,  static int ccs_inet_entry(const struct ccs_addr_info *address)
                               const u32 *address, const u16 port)  
298  {  {
299            const int idx = ccs_read_lock();
300          struct ccs_request_info r;          struct ccs_request_info r;
301          struct ccs_acl_info *ptr;          int error = 0;
302          const char *keyword = ccs_net2keyword(operation);          const u8 type = ccs_inet2mac[address->protocol][address->operation];
303          const u16 perm = 1 << operation;          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
304          /* using host byte order to allow u32 comparison than memcmp().*/                  r.param_type = CCS_TYPE_INET_ACL;
305          const u32 ip = ntohl(*address);                  r.param.inet_network.protocol = address->protocol;
306          int error;                  r.param.inet_network.operation = address->operation;
307          char buf[64];                  r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
308          if (ccs_init_request_info(&r, NULL,                  r.param.inet_network.address = address->inet.address;
309                                    CCS_MAC_NETWORK_UDP_BIND + operation)                  r.param.inet_network.port = ntohs(address->inet.port);
310              == CCS_CONFIG_DISABLED)                  /* use host byte order to allow u32 comparison than memcmp().*/
311                  return 0;                  r.param.inet_network.ip = ntohl(*address->inet.address);
312          memset(buf, 0, sizeof(buf));                  do {
313          if (is_ipv6)                          ccs_check_acl(&r, ccs_check_inet_acl);
314                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)                          error = ccs_audit_inet_log(&r);
315                                 address);                  } while (error == CCS_RETRY_REQUEST);
316          else          }
317                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));          ccs_read_unlock(idx);
         do {  
                 error = -EPERM;  
                 list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {  
                         struct ccs_ip_network_acl *acl;  
                         if (ptr->is_deleted ||  
                             ptr->type != CCS_TYPE_IP_NETWORK_ACL)  
                                 continue;  
                         acl = container_of(ptr, struct ccs_ip_network_acl,  
                                            head);  
                         if (!(acl->perm & perm))  
                                 continue;  
                         if (!ccs_compare_number_union(port, &acl->port) ||  
                             !ccs_condition(&r, ptr))  
                                 continue;  
                         switch (acl->address_type) {  
                         case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:  
                                 if (!ccs_address_matches_group(is_ipv6,  
                                                                address,  
                                                                acl->address.  
                                                                group))  
                                         continue;  
                                 break;  
                         case CCS_IP_ADDRESS_TYPE_IPv4:  
                                 if (is_ipv6 || ip < acl->address.ipv4.min ||  
                                     acl->address.ipv4.max < ip)  
                                         continue;  
                                 break;  
                         default:  
                                 if (!is_ipv6 ||  
                                     memcmp(acl->address.ipv6.min, address, 16)  
                                     > 0 ||  
                                     memcmp(address, acl->address.ipv6.max, 16)  
                                     > 0)  
                                         continue;  
                                 break;  
                         }  
                         r.cond = ptr->cond;  
                         error = 0;  
                         break;  
                 }  
                 ccs_audit_network_log(&r, keyword, buf, port, !error);  
                 if (!error)  
                         break;  
                 error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK  
                                        "%s %s %u\n", keyword, buf, port);  
         } while (error == CCS_RETRY_REQUEST);  
         if (r.mode != CCS_CONFIG_ENFORCING)  
                 error = 0;  
318          return error;          return error;
319  }  }
320    
321  /**  /**
322   * ccs_network_entry - Check permission for network operation.   * ccs_unix_entry - Check permission for UNIX network operation.
323   *   *
324   * @is_ipv6:   True if @address is an IPv6 address.   * @address: Pointer to "struct ccs_addr_info".
  * @operation: Type of operation.  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
325   *   *
326   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
327   */   */
328  static int ccs_network_entry(const bool is_ipv6, const u8 operation,  static int ccs_unix_entry(const struct ccs_addr_info *address)
                              const u32 *address, const u16 port)  
329  {  {
330          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
331          const int error = ccs_network_entry2(is_ipv6, operation, address,          struct ccs_request_info r;
332                                               port);          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          ccs_read_unlock(idx);          ccs_read_unlock(idx);
360          return error;          return error;
361  }  }
362    
363    static bool ccs_same_inet_acl(const struct ccs_acl_info *a,
364                                          const struct ccs_acl_info *b)
365    {
366            const struct ccs_inet_acl *p1 = container_of(a, typeof(*p1),
367                                                                 head);
368            const struct ccs_inet_acl *p2 = container_of(b, typeof(*p2),
369                                                                 head);
370            return ccs_same_acl_head(&p1->head, &p2->head)
371                    && p1->protocol == p2->protocol
372                    && p1->address_type == p2->address_type &&
373                    p1->address.ipv4.min == p2->address.ipv4.min &&
374                    p1->address.ipv6.min == p2->address.ipv6.min &&
375                    p1->address.ipv4.max == p2->address.ipv4.max &&
376                    p1->address.ipv6.max == p2->address.ipv6.max &&
377                    p1->address.group == p2->address.group &&
378                    ccs_same_number_union(&p1->port, &p2->port);
379    }
380    
381    static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
382                                          const struct ccs_acl_info *b)
383    {
384            const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1),
385                                                                 head);
386            const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2),
387                                                                 head);
388            return ccs_same_acl_head(&p1->head, &p2->head) &&
389                    p1->protocol == p2->protocol &&
390                    ccs_same_name_union(&p1->name, &p2->name);
391    }
392    
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_network_policy - Write "struct ccs_ip_network_acl" list.   * ccs_write_inet_network - Write "struct ccs_inet_acl" list.
429   *   *
430   * @data:      String to parse.   * @data:      String to parse.
431   * @domain:    Pointer to "struct ccs_domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
432   * @condition: Pointer to "struct ccs_condition". May be NULL.   * @condition: Pointer to "struct ccs_condition". Maybe NULL.
433   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
434   *   *
435   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
436   */   */
437  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,  int ccs_write_inet_network(char *data, struct ccs_domain_info *domain,
438                               struct ccs_condition *condition,                             struct ccs_condition *condition,
439                               const bool is_delete)                             const bool is_delete)
440  {  {
441          struct ccs_ip_network_acl *entry = NULL;          struct ccs_inet_acl e = {
442          struct ccs_acl_info *ptr;                  .head.type = CCS_TYPE_INET_ACL,
         struct ccs_ip_network_acl e = {  
                 .head.type = CCS_TYPE_IP_NETWORK_ACL,  
443                  .head.cond = condition,                  .head.cond = condition,
444          };          };
445          u16 min_address[8];          u16 min_address[8];
446          u16 max_address[8];          u16 max_address[8];
447          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
448          u8 sock_type;          u8 type;
449          char *w[4];          char *w[4];
450          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
451                  return -EINVAL;                  return -EINVAL;
452          if (!strcmp(w[0], "TCP"))          for (e.protocol = 0; e.protocol < CCS_MAX_INET_PROTOCOL;
453                  sock_type = SOCK_STREAM;               e.protocol++)
454          else if (!strcmp(w[0], "UDP"))                  if (!strcmp(w[0], ccs_inet_keyword[e.protocol]))
455                  sock_type = SOCK_DGRAM;                          break;
456          else if (!strcmp(w[0], "RAW"))          for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
457                  sock_type = SOCK_RAW;                  if (ccs_permstr(w[1], ccs_net_keyword[type]))
458          else                          e.perm |= 1 << type;
459                  return -EINVAL;          if (e.protocol == CCS_MAX_INET_PROTOCOL || !e.perm)
         if (!strcmp(w[1], "bind"))  
                 switch (sock_type) {  
                 case SOCK_STREAM:  
                         e.perm = 1 << CCS_NETWORK_TCP_BIND;  
                         break;  
                 case SOCK_DGRAM:  
                         e.perm = 1 << CCS_NETWORK_UDP_BIND;  
                         break;  
                 default:  
                         e.perm = 1 << CCS_NETWORK_RAW_BIND;  
                         break;  
                 }  
         else if (!strcmp(w[1], "connect"))  
                 switch (sock_type) {  
                 case SOCK_STREAM:  
                         e.perm = 1 << CCS_NETWORK_TCP_CONNECT;  
                         break;  
                 case SOCK_DGRAM:  
                         e.perm = 1 << CCS_NETWORK_UDP_CONNECT;  
                         break;  
                 default:  
                         e.perm = 1 << CCS_NETWORK_RAW_CONNECT;  
                         break;  
                 }  
         else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))  
                 e.perm = 1 << CCS_NETWORK_TCP_LISTEN;  
         else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))  
                 e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;  
         else  
460                  return -EINVAL;                  return -EINVAL;
461          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
462          case 2:          case CCS_IP_ADDRESS_TYPE_IPv6:
463                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
464                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
465                                                            min_address);                                                              min_address);
466                  e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)                  e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
467                                                            max_address);                                                              max_address);
468                  if (!e.address.ipv6.min || !e.address.ipv6.max)                  if (!e.address.ipv6.min || !e.address.ipv6.max)
469                          goto out;                          goto out;
470                  break;                  break;
471          case 1:          case CCS_IP_ADDRESS_TYPE_IPv4:
472                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
473                  /* use host byte order to allow u32 comparison.*/                  /* use host byte order to allow u32 comparison.*/
474                  e.address.ipv4.min = ntohl(*(u32 *) min_address);                  e.address.ipv4.min = ntohl(*(u32 *) min_address);
# Line 335  int ccs_write_network_policy(char *data, Line 478  int ccs_write_network_policy(char *data,
478                  if (w[2][0] != '@')                  if (w[2][0] != '@')
479                          return -EINVAL;                          return -EINVAL;
480                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
481                  e.address.group = ccs_get_address_group(w[2] + 1);                  e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP);
482                  if (!e.address.group)                  if (!e.address.group)
483                          return -ENOMEM;                          return -ENOMEM;
484                  break;                  break;
485          }          }
486          if (!ccs_parse_number_union(w[3], &e.port))          if (!ccs_parse_number_union(w[3], &e.port))
487                  goto out;                  goto out;
488          if (!is_delete)          error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
489                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    ccs_same_inet_acl, ccs_merge_inet_acl);
         mutex_lock(&ccs_policy_lock);  
         list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {  
                 struct ccs_ip_network_acl *acl =  
                         container_of(ptr, struct ccs_ip_network_acl, head);  
                 if (ptr->type != CCS_TYPE_IP_NETWORK_ACL ||  
                     ptr->cond != condition ||  
                     ccs_memcmp(acl, &e, offsetof(typeof(e), address_type),  
                                sizeof(e)))  
                         continue;  
                 if (is_delete) {  
                         acl->perm &= ~e.perm;  
                         if (!acl->perm)  
                                 ptr->is_deleted = true;  
                 } else {  
                         if (ptr->is_deleted)  
                                 acl->perm = 0;  
                         acl->perm |= e.perm;  
                         ptr->is_deleted = false;  
                 }  
                 error = 0;  
                 break;  
         }  
         if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) {  
                 ccs_add_domain_acl(domain, &entry->head);  
                 entry = NULL;  
                 error = 0;  
         }  
         mutex_unlock(&ccs_policy_lock);  
490   out:   out:
491          if (w[2][0] == '@')          if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
492                  ccs_put_address_group(e.address.group);                  ccs_put_group(e.address.group);
493          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
494                  ccs_put_ipv6_address(e.address.ipv6.min);                  ccs_put_ipv6_address(e.address.ipv6.min);
495                  ccs_put_ipv6_address(e.address.ipv6.max);                  ccs_put_ipv6_address(e.address.ipv6.max);
496          }          }
497          ccs_put_number_union(&e.port);          ccs_put_number_union(&e.port);
         kfree(entry);  
498          return error;          return error;
499  }  }
500    
501  /**  /**
502   * ccs_network_listen_acl - Check permission for listen() operation.   * ccs_write_unix_network - Write "struct ccs_unix_acl" list.
503   *   *
504   * @is_ipv6: True if @address is an IPv6 address.   * @data:      String to parse.
505   * @address: An IPv4 or IPv6 address.   * @domain:    Pointer to "struct ccs_domain_info".
506   * @port:    Port number.   * @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.   * Returns 0 on success, negative value otherwise.
510   */   */
511  static inline int ccs_network_listen_acl(const bool is_ipv6, const u8 *address,  int ccs_write_unix_network(char *data, struct ccs_domain_info *domain,
512                                           const u16 port)                             struct ccs_condition *condition,
513                               const bool is_delete)
514  {  {
515          return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN,          struct ccs_unix_acl e = {
516                                   (const u32 *) address, ntohs(port));                  .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  /**  #ifndef CONFIG_NET
542   * ccs_network_connect_acl - Check permission for connect() operation.  
543   *  void __init ccs_network_init(void)
  * @is_ipv6:   True if @address is an IPv6 address.  
  * @sock_type: Type of socket. (TCP or UDP or RAW)  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static inline int ccs_network_connect_acl(const bool is_ipv6,  
                                           const int sock_type,  
                                           const u8 *address, const u16 port)  
544  {  {
         u8 operation;  
         switch (sock_type) {  
         case SOCK_STREAM:  
                 operation = CCS_NETWORK_TCP_CONNECT;  
                 break;  
         case SOCK_DGRAM:  
                 operation = CCS_NETWORK_UDP_CONNECT;  
                 break;  
         default:  
                 operation = CCS_NETWORK_RAW_CONNECT;  
         }  
         return ccs_network_entry(is_ipv6, operation,  
                                  (const u32 *) address, ntohs(port));  
545  }  }
546    
547  /**  #else
548   * ccs_network_bind_acl - Check permission for bind() operation.  
549   *  static bool ccs_check_inet_address(const struct sockaddr *addr,
550   * @is_ipv6:   True if @address is an IPv6 address.                                     const unsigned int addr_len,
551   * @sock_type: Type of socket. (TCP or UDP or RAW)                                     struct ccs_inet_addr_info *address)
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_network_bind_acl(const bool is_ipv6, const int sock_type,  
                                 const u8 *address, const u16 port)  
552  {  {
553          u8 operation;          switch (addr->sa_family) {
554          switch (sock_type) {          case AF_INET6:
555          case SOCK_STREAM:                  if (addr_len < SIN6_LEN_RFC2133)
556                  operation = CCS_NETWORK_TCP_BIND;                          goto skip;
557                    address->is_ipv6 = true;
558                    address->address = (u32 *)
559                            ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
560                    address->port = ((struct sockaddr_in6 *) addr)->sin6_port;
561                  break;                  break;
562          case SOCK_DGRAM:          case AF_INET:
563                  operation = CCS_NETWORK_UDP_BIND;                  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:          default:
571                  operation = CCS_NETWORK_RAW_BIND;                  goto skip;
572          }          }
573          return ccs_network_entry(is_ipv6, operation,          return true;
574                                   (const u32 *) address, ntohs(port));   skip:
575            return false;
576  }  }
577    
578  /**  static bool ccs_check_unix_address(struct sockaddr *addr,
579   * ccs_network_accept_acl - Check permission for accept() operation.                                     const unsigned int addr_len,
580   *                                     struct ccs_unix_addr_info *address)
  * @is_ipv6: True if @address is an IPv6 address.  
  * @address: An IPv4 or IPv6 address.  
  * @port:    Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static inline int ccs_network_accept_acl(const bool is_ipv6, const u8 *address,  
                                          const u16 port)  
581  {  {
582          int retval;          if (addr->sa_family != AF_UNIX)
583          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;                  return false;
584          retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT,          address->addr = ((struct sockaddr_un *) addr)->sun_path;
585                                     (const u32 *) address, ntohs(port));          address->addr_len = addr_len;
586          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          /*
587          return retval;           * Terminate pathname with '\0' like unix_mkname() does.
588             * This is needed because pathname was copied by move_addr_to_kernel()
589             * but not yet terminated by unix_mkname().
590             */
591            if (address->addr[0] && addr_len > sizeof(short) &&
592                addr_len <= sizeof(struct sockaddr_un))
593                    ((char *) addr)[addr_len] = '\0';
594            return true;
595  }  }
596    
597  /**  static bool ccs_kernel_service(void)
  * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.  
  *  
  * @is_ipv6:   True if @address is an IPv6 address.  
  * @sock_type: Type of socket. (UDP or RAW)  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static inline int ccs_network_sendmsg_acl(const bool is_ipv6,  
                                           const int sock_type,  
                                           const u8 *address, const u16 port)  
598  {  {
599          u8 operation;          /* Nothing to do if I am a kernel service. */
600          if (sock_type == SOCK_DGRAM)          return segment_eq(get_fs(), KERNEL_DS);
                 operation = CCS_NETWORK_UDP_CONNECT;  
         else  
                 operation = CCS_NETWORK_RAW_CONNECT;  
         return ccs_network_entry(is_ipv6, operation,  
                                  (const u32 *) address, ntohs(port));  
601  }  }
602    
603  /**  static u8 ccs_sock_family(struct socket *sock)
604   * ccs_network_recvmsg_acl - Check permission for recvmsg() operation.  {
605   *          if (ccs_kernel_service())
606   * @is_ipv6:   True if @address is an IPv6 address.                  return 0;
607   * @sock_type: Type of socket. (UDP or RAW)          switch (sock->sk->sk_family) {
608   * @address:   An IPv4 or IPv6 address.          case PF_INET:
609   * @port:      Port number.          case PF_INET6:
610   *                  return 1;
611   * Returns 0 on success, negative value otherwise.          case PF_UNIX:
612   */                  return 2;
613  static inline int ccs_network_recvmsg_acl(const bool is_ipv6,          default:
614                                            const int sock_type,                  return 0;
615                                            const u8 *address, const u16 port)          }
 {  
         int retval;  
         const u8 operation  
                 = (sock_type == SOCK_DGRAM) ?  
                 CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT;  
         current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         retval = ccs_network_entry(is_ipv6, operation,  
                                    (const u32 *) address, ntohs(port));  
         current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         return retval;  
616  }  }
617    
 #define MAX_SOCK_ADDR 128 /* net/socket.c */  
   
618  /* Check permission for creating a socket. */  /* Check permission for creating a socket. */
619  static int __ccs_socket_create_permission(int family, int type, int protocol)  static int __ccs_socket_create_permission(int family, int type, int protocol)
620  {  {
621          int error = 0;          if (ccs_kernel_service())
         /* Nothing to do if I am a kernel service. */  
         if (segment_eq(get_fs(), KERNEL_DS))  
622                  return 0;                  return 0;
623          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
624                  return -EPERM;                  return -EPERM;
625          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
626                  return -EPERM;                  return -EPERM;
627          if (family != PF_INET && family != PF_INET6)          return 0;
                 return 0;  
         switch (type) {  
         case SOCK_STREAM:  
                 if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE))  
                         error = -EPERM;  
                 break;  
         case SOCK_DGRAM:  
                 if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET))  
                         error = -EPERM;  
                 break;  
         case SOCK_RAW:  
                 if (!ccs_capable(CCS_USE_INET_RAW_SOCKET))  
                         error = -EPERM;  
                 break;  
         }  
         return error;  
628  }  }
629    
630  /* Check permission for listening a TCP socket. */  /* Check permission for listening a socket. */
631  static int __ccs_socket_listen_permission(struct socket *sock)  static int __ccs_socket_listen_permission(struct socket *sock)
632  {  {
633          int error = 0;          struct sockaddr_storage addr;
634          char addr[MAX_SOCK_ADDR];          int error;
635          int addr_len;          int addr_len;
636          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
637          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
638                  return 0;          const unsigned int type = sock->type;
639          if (sock->type != SOCK_STREAM)          if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
                 return 0;  
         switch (sock->sk->sk_family) {  
         case PF_INET:  
         case PF_INET6:  
                 break;  
         default:  
640                  return 0;                  return 0;
641            error = sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len,
642                                       0);
643            if (error)
644                    return error;
645            address.operation = CCS_NETWORK_LISTEN;
646            if (family == 2) {
647                    if (type == SOCK_STREAM)
648                            address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
649                    else
650                            address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
651                    if (ccs_check_unix_address((struct sockaddr *) &addr, addr_len,
652                                               &address.unix0))
653                            error = ccs_unix_entry(&address);
654            } else {
655                    address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
656                    if (ccs_check_inet_address((struct sockaddr *) &addr, addr_len,
657                                               &address.inet))
658                            error = ccs_inet_entry(&address);
659          }          }
660          if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))          return error;
                 return -EPERM;  
         if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))  
                 return -EPERM;  
         switch (((struct sockaddr *) addr)->sa_family) {  
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
         case AF_INET6:  
                 addr6 = (struct sockaddr_in6 *) addr;  
                 error = ccs_network_listen_acl(true,  
                                                addr6->sin6_addr.s6_addr,  
                                                addr6->sin6_port);  
                 break;  
         case AF_INET:  
                 addr4 = (struct sockaddr_in *) addr;  
                 error = ccs_network_listen_acl(false,  
                                                (u8 *) &addr4->sin_addr,  
                                                addr4->sin_port);  
                 break;  
         }  
         return error;  
661  }  }
662    
663  /* Check permission for setting the remote IP address/port pair of a socket. */  /* Check permission for setting the remote address of a socket. */
664  static int __ccs_socket_connect_permission(struct socket *sock,  static int __ccs_socket_connect_permission(struct socket *sock,
665                                             struct sockaddr *addr, int addr_len)                                             struct sockaddr *addr, int addr_len)
666  {  {
667          int error = 0;          int error = 0;
668          const unsigned int type = sock->type;          const unsigned int type = sock->type;
669          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
670          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
671                  return 0;          if (!family)
672          switch (type) {                  return 0;
673          case SOCK_STREAM:          address.operation = CCS_NETWORK_CONNECT;
674          case SOCK_DGRAM:          if (family == 2) {
675          case SOCK_RAW:                  switch (type) {
676                  break;                  case SOCK_STREAM:
677          default:                          address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
                 return 0;  
         }  
         switch (addr->sa_family) {  
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
                 u16 port;  
         case AF_INET6:  
                 if (addr_len < SIN6_LEN_RFC2133)  
678                          break;                          break;
679                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_DGRAM:
680                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
681                          port = addr6->sin6_port;                          address.operation = CCS_NETWORK_SEND;
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_connect_acl(true, type,  
                                                 addr6->sin6_addr.s6_addr,  
                                                 port);  
                 break;  
         case AF_INET:  
                 if (addr_len < sizeof(struct sockaddr_in))  
682                          break;                          break;
683                  addr4 = (struct sockaddr_in *) addr;                  case SOCK_SEQPACKET:
684                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
685                          port = addr4->sin_port;                          break;
686                  else                  default:
687                          port = htons(sock->sk->sk_protocol);                          return 0;
688                  error = ccs_network_connect_acl(false, type,                  }
689                                                  (u8 *) &addr4->sin_addr,                  if (ccs_check_unix_address(addr, addr_len, &address.unix0))
690                                                  port);                          error = ccs_unix_entry(&address);
691                  break;          } else {
692          }                  switch (type) {
693          if (type != SOCK_STREAM)                  case SOCK_STREAM:
694                  return error;                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
695          switch (sock->sk->sk_family) {                          break;
696          case PF_INET:                  case SOCK_DGRAM:
697          case PF_INET6:                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
698                  if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))                          address.operation = CCS_NETWORK_SEND;
699                          error = -EPERM;                          break;
700                  break;                  case SOCK_RAW:
701                            address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
702                            address.operation = CCS_NETWORK_SEND;
703                            break;
704                    default:
705                            return 0;
706                    }
707                    if (ccs_check_inet_address(addr, addr_len, &address.inet)) {
708                            if (type == SOCK_RAW)
709                                    address.inet.port = htons(sock->sk->sk_protocol);
710                            error = ccs_inet_entry(&address);
711                    }
712          }          }
713          return error;          return error;
714  }  }
715    
716  /* Check permission for setting the local IP address/port pair of a socket. */  /* Check permission for setting the local address of a socket. */
717  static int __ccs_socket_bind_permission(struct socket *sock,  static int __ccs_socket_bind_permission(struct socket *sock,
718                                          struct sockaddr *addr, int addr_len)                                          struct sockaddr *addr, int addr_len)
719  {  {
720          int error = 0;          int error = 0;
721          const unsigned int type = sock->type;          const unsigned int type = sock->type;
722          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
723          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
724                  return 0;          if (!family)
725          switch (type) {                  return 0;
726          case SOCK_STREAM:          address.operation = CCS_NETWORK_BIND;
727          case SOCK_DGRAM:          if (family == 2) {
728          case SOCK_RAW:                  switch (type) {
729                  break;                  case SOCK_STREAM:
730          default:                          address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
                 return 0;  
         }  
         switch (addr->sa_family) {  
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
                 u16 port;  
         case AF_INET6:  
                 if (addr_len < SIN6_LEN_RFC2133)  
731                          break;                          break;
732                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_DGRAM:
733                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
                         port = addr6->sin6_port;  
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_bind_acl(true, type,  
                                              addr6->sin6_addr.s6_addr,  
                                              port);  
                 break;  
         case AF_INET:  
                 if (addr_len < sizeof(struct sockaddr_in))  
734                          break;                          break;
735                  addr4 = (struct sockaddr_in *) addr;                  case SOCK_SEQPACKET:
736                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
737                          port = addr4->sin_port;                          break;
738                  else                  default:
739                          port = htons(sock->sk->sk_protocol);                          return 0;
740                  error = ccs_network_bind_acl(false, type,                  }
741                                               (u8 *) &addr4->sin_addr,                  if (ccs_check_unix_address(addr, addr_len, &address.unix0))
742                                               port);                          error = ccs_unix_entry(&address);
743                  break;          } else {
744          }                  switch (type) {
745          return error;                  case SOCK_STREAM:
746  }                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
747                            break;
748  /*                  case SOCK_DGRAM:
749   * Check permission for accepting a TCP socket.                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
750   *                          break;
751   * Currently, the LSM hook for this purpose is not provided.                  case SOCK_RAW:
752   */                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
753  static int __ccs_socket_accept_permission(struct socket *sock,                          break;
754                                            struct sockaddr *addr)                  default:
755  {                          return 0;
756          int error = 0;                  }
757          int addr_len;                  if (ccs_check_inet_address(addr, addr_len, &address.inet)) {
758          /* Nothing to do if I am a kernel service. */                          if (type == SOCK_RAW)
759          if (segment_eq(get_fs(), KERNEL_DS))                                  address.inet.port = htons(sock->sk->sk_protocol);
760                  return 0;                          error = ccs_inet_entry(&address);
761          switch (sock->sk->sk_family) {                  }
         case PF_INET:  
         case PF_INET6:  
                 break;  
         default:  
                 return 0;  
         }  
         error = sock->ops->getname(sock, addr, &addr_len, 2);  
         if (error)  
                 return error;  
         switch (addr->sa_family) {  
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
         case AF_INET6:  
                 addr6 = (struct sockaddr_in6 *) addr;  
                 error = ccs_network_accept_acl(true,  
                                                addr6->sin6_addr.s6_addr,  
                                                addr6->sin6_port);  
                 break;  
         case AF_INET:  
                 addr4 = (struct sockaddr_in *) addr;  
                 error = ccs_network_accept_acl(false,  
                                                (u8 *) &addr4->sin_addr,  
                                                addr4->sin_port);  
                 break;  
762          }          }
763          return error;          return error;
764  }  }
# Line 751  static int __ccs_socket_accept_permissio Line 767  static int __ccs_socket_accept_permissio
767  static int __ccs_socket_sendmsg_permission(struct socket *sock,  static int __ccs_socket_sendmsg_permission(struct socket *sock,
768                                             struct msghdr *msg, int size)                                             struct msghdr *msg, int size)
769  {  {
         struct sockaddr *addr = (struct sockaddr *) msg->msg_name;  
         const int addr_len = msg->msg_namelen;  
770          int error = 0;          int error = 0;
771          const int type = sock->type;          const int type = sock->type;
772          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
773          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
774                  return 0;          if (!msg->msg_name || !family)
775          if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))                  return 0;
776                  return 0;          address.operation = CCS_NETWORK_SEND;
777          switch (addr->sa_family) {          if (family == 2) {
778                  struct sockaddr_in6 *addr6;                  if (type != SOCK_DGRAM)
779                  struct sockaddr_in *addr4;                          return 0;
780                  u16 port;                  address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
781          case AF_INET6:                  if (ccs_check_unix_address((struct sockaddr *) msg->msg_name,
782                  if (addr_len < SIN6_LEN_RFC2133)                                             msg->msg_namelen, &address.unix0))
783                            error = ccs_unix_entry(&address);
784            } else {
785                    switch (type) {
786                    case SOCK_DGRAM:
787                            address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
788                          break;                          break;
789                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_RAW:
790                  if (type == SOCK_DGRAM)                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
                         port = addr6->sin6_port;  
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_sendmsg_acl(true, type,  
                                                 addr6->sin6_addr.s6_addr,  
                                                 port);  
                 break;  
         case AF_INET:  
                 if (addr_len < sizeof(struct sockaddr_in))  
791                          break;                          break;
792                  addr4 = (struct sockaddr_in *) addr;                  default:
793                  if (type == SOCK_DGRAM)                          return 0;
794                          port = addr4->sin_port;                  }
795                    if (ccs_check_inet_address((struct sockaddr *) msg->msg_name,
796                                               msg->msg_namelen, &address.inet)) {
797                            if (type == SOCK_RAW)
798                                    address.inet.port = htons(sock->sk->sk_protocol);
799                            error = ccs_inet_entry(&address);
800                    }
801            }
802            return error;
803    }
804    
805    /* Check permission for accepting a socket. */
806    static int __ccs_socket_post_accept_permission(struct socket *sock,
807                                                   struct socket *newsock)
808    {
809            struct sockaddr_storage addr;
810            struct task_struct * const task = current;
811            int error;
812            int addr_len;
813            const u8 family = ccs_sock_family(sock);
814            struct ccs_addr_info address;
815            const unsigned int type = sock->type;
816            if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
817                    return 0;
818            error = newsock->ops->getname(newsock, (struct sockaddr *) &addr,
819                                          &addr_len, 2);
820            if (error)
821                    return error;
822            address.operation = CCS_NETWORK_ACCEPT;
823            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
824            if (family == 2) {
825                    if (type == SOCK_STREAM)
826                            address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
827                  else                  else
828                          port = htons(sock->sk->sk_protocol);                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
829                  error = ccs_network_sendmsg_acl(false, type,                  if (ccs_check_unix_address((struct sockaddr *) &addr, addr_len,
830                                                  (u8 *) &addr4->sin_addr, port);                                             &address.unix0))
831                  break;                          error = ccs_unix_entry(&address);
832            } else {
833                    address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
834                    if (ccs_check_inet_address((struct sockaddr *) &addr, addr_len,
835                                               &address.inet))
836                            error = ccs_inet_entry(&address);
837          }          }
838            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
839          return error;          return error;
840  }  }
841    
# Line 814  static inline struct ipv6hdr *ipv6_hdr(c Line 862  static inline struct ipv6hdr *ipv6_hdr(c
862  #endif  #endif
863  #endif  #endif
864    
865  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)  /* Check permission for receiving a datagram. */
866  static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,  static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
867                                unsigned int flags)                                                  struct sk_buff *skb)
 {  
         /* Clear queue. */  
         if (flags & MSG_PEEK) {  
                 int clear = 0;  
                 spin_lock_irq(&sk->sk_receive_queue.lock);  
                 if (skb == skb_peek(&sk->sk_receive_queue)) {  
                         __skb_unlink(skb, &sk->sk_receive_queue);  
                         clear = 1;  
                 }  
                 spin_unlock_irq(&sk->sk_receive_queue.lock);  
                 if (clear)  
                         kfree_skb(skb);  
         }  
         skb_free_datagram(sk, skb);  
 }  
 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)  
 static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,  
                               unsigned int flags)  
 {  
         /* Clear queue. */  
         if (flags & MSG_PEEK) {  
                 int clear = 0;  
                 spin_lock_bh(&sk->sk_receive_queue.lock);  
                 if (skb == skb_peek(&sk->sk_receive_queue)) {  
                         __skb_unlink(skb, &sk->sk_receive_queue);  
                         clear = 1;  
                 }  
                 spin_unlock_bh(&sk->sk_receive_queue.lock);  
                 if (clear)  
                         kfree_skb(skb);  
         }  
         skb_free_datagram(sk, skb);  
 }  
 #endif  
   
 /*  
  * Check permission for receiving a datagram via a UDP or RAW socket.  
  *  
  * Currently, the LSM hook for this purpose is not provided.  
  */  
 static int __ccs_socket_recvmsg_permission(struct sock *sk,  
                                            struct sk_buff *skb,  
                                            const unsigned int flags)  
868  {  {
869            struct task_struct * const task = current;
870          int error = 0;          int error = 0;
871          const unsigned int type = sk->sk_type;          const unsigned int type = sk->sk_type;
872          if (type != SOCK_DGRAM && type != SOCK_RAW)          struct ccs_addr_info address;
873            union {
874                    struct in6_addr sin6;
875                    struct in_addr sin4;
876            } ip_address;
877            switch (type) {
878            case SOCK_DGRAM:
879                    address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
880                    break;
881            case SOCK_RAW:
882                    address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
883                    break;
884            default:
885                  return 0;                  return 0;
886          /* Nothing to do if I am a kernel service. */          }
887          if (segment_eq(get_fs(), KERNEL_DS))          if (ccs_kernel_service())
888                  return 0;                  return 0;
   
889          switch (sk->sk_family) {          switch (sk->sk_family) {
                 struct in6_addr sin6;  
                 struct in_addr sin4;  
                 u16 port;  
890          case PF_INET6:          case PF_INET6:
891                  if (type == SOCK_DGRAM) { /* UDP IPv6 */                  address.inet.is_ipv6 = true;
892                          if (skb->protocol == htons(ETH_P_IP)) {                  if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP))
893                                  ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),                          ipv6_addr_set(&ip_address.sin6, 0, 0, htonl(0xffff),
894                                                ip_hdr(skb)->saddr);                                        ip_hdr(skb)->saddr);
895                          } else {                  else
896                                  ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);                          ipv6_addr_copy(&ip_address.sin6,
897                          }                                         &ipv6_hdr(skb)->saddr);
                         port = udp_hdr(skb)->source;  
                 } else { /* RAW IPv6 */  
                         ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);  
                         port = htons(sk->sk_protocol);  
                 }  
                 error = ccs_network_recvmsg_acl(true, type,  
                                                 (u8 *) &sin6, port);  
898                  break;                  break;
899          case PF_INET:          case PF_INET:
900                  if (type == SOCK_DGRAM) { /* UDP IPv4 */                  address.inet.is_ipv6 = false;
901                          sin4.s_addr = ip_hdr(skb)->saddr;                  ip_address.sin4.s_addr = ip_hdr(skb)->saddr;
                         port = udp_hdr(skb)->source;  
                 } else { /* RAW IPv4 */  
                         sin4.s_addr = ip_hdr(skb)->saddr;  
                         port = htons(sk->sk_protocol);  
                 }  
                 error = ccs_network_recvmsg_acl(false, type,  
                                                 (u8 *) &sin4, port);  
902                  break;                  break;
903            default:
904                    goto skip;
905          }          }
906          if (!error)          address.inet.address = (u32 *) &ip_address;
                 return 0;  
         /*  
          * Remove from queue if MSG_PEEK is used so that  
          * the head message from unwanted source in receive queue will not  
          * prevent the caller from picking up next message from wanted source  
          * when the caller is using MSG_PEEK flag for picking up.  
          */  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
         if (type == SOCK_DGRAM)  
                 lock_sock(sk);  
 #endif  
         skb_kill_datagram(sk, skb, flags);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
907          if (type == SOCK_DGRAM)          if (type == SOCK_DGRAM)
908                  release_sock(sk);                  address.inet.port = udp_hdr(skb)->source;
909  #endif          else
910          /* Hope less harmful than -EPERM. */                  address.inet.port = htons(sk->sk_protocol);
911          return -ENOMEM;          address.operation = CCS_NETWORK_RECV;
912            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
913            error = ccs_inet_entry(&address);
914            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
915     skip:
916            return error;
917  }  }
918    
919  void __init ccs_network_init(void)  void __init ccs_network_init(void)
# Line 931  void __init ccs_network_init(void) Line 925  void __init ccs_network_init(void)
925          ccsecurity_ops.socket_connect_permission =          ccsecurity_ops.socket_connect_permission =
926                  __ccs_socket_connect_permission;                  __ccs_socket_connect_permission;
927          ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;          ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
928          ccsecurity_ops.socket_accept_permission =          ccsecurity_ops.socket_post_accept_permission =
929                  __ccs_socket_accept_permission;                  __ccs_socket_post_accept_permission;
930          ccsecurity_ops.socket_sendmsg_permission =          ccsecurity_ops.socket_sendmsg_permission =
931                  __ccs_socket_sendmsg_permission;                  __ccs_socket_sendmsg_permission;
932          ccsecurity_ops.socket_recvmsg_permission =          ccsecurity_ops.socket_post_recvmsg_permission =
933                  __ccs_socket_recvmsg_permission;                  __ccs_socket_post_recvmsg_permission;
934  }  }
935    
936    #endif

Legend:
Removed from v.3502  
changed lines
  Added in v.3915

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