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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/network.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/ccs-patch/security/ccsecurity/network.c revision 3691 by kumaneko, Sun May 23 03:22:24 2010 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/network.c revision 3911 by kumaneko, Sun Aug 22 12:18:01 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   2010/04/01   * 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_network_log - Audit network log.   * ccs_audit_inet_log - Audit INET network log.
64   *   *
65   * @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.  
66   *   *
67   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
68   */   */
69  static int ccs_audit_network_log(struct ccs_request_info *r,  static int ccs_audit_inet_log(struct ccs_request_info *r)
70                                   const char *operation, const char *address,  {
71                                   const u16 port, const bool is_granted)          char buf[128];
72  {          const char *protocol =
73          if (!is_granted)                  ccs_inet_keyword[r->param.inet_network.protocol];
74                  ccs_warn_log(r, "%s %s %u", operation, address, port);          const char *operation = ccs_net_keyword[r->param.inet_network.operation];
75          return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK          const u32 *address = r->param.inet_network.address;
76                                     "%s %s %u\n", operation, address, port);          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_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  /**  /**
# Line 134  void ccs_print_ipv6(char *buffer, const Line 207  void ccs_print_ipv6(char *buffer, const
207                   NIP6(*max_ip));                   NIP6(*max_ip));
208  }  }
209    
210  /**  static bool ccs_check_inet_acl(struct ccs_request_info *r,
211   * ccs_net2keyword - Convert network operation index to network operation name.                                 const struct ccs_acl_info *ptr)
  *  
  * @operation: Type of operation.  
  *  
  * Returns the name of operation.  
  */  
 const char *ccs_net2keyword(const u8 operation)  
212  {  {
213          const char *keyword = "unknown";          const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
214          switch (operation) {          bool ret;
215          case CCS_NETWORK_UDP_BIND:          if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
216                  keyword = "UDP bind";              !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
217                    return false;
218            switch (acl->address_type) {
219            case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
220                    ret = ccs_address_matches_group(r->param.inet_network.is_ipv6,
221                                                    r->param.inet_network.address,
222                                                    acl->address.group);
223                  break;                  break;
224          case CCS_NETWORK_UDP_CONNECT:          case CCS_IP_ADDRESS_TYPE_IPv4:
225                  keyword = "UDP connect";                  ret = !r->param.inet_network.is_ipv6 &&
226                  break;                          acl->address.ipv4.min <= r->param.inet_network.ip &&
227          case CCS_NETWORK_TCP_BIND:                          r->param.inet_network.ip <= acl->address.ipv4.max;
                 keyword = "TCP bind";  
                 break;  
         case CCS_NETWORK_TCP_LISTEN:  
                 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_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_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[128];                  r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
308          const struct ccs_domain_info * const domain = ccs_current_domain();                  r.param.inet_network.address = address->inet.address;
309          if (ccs_init_request_info(&r, 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          if (is_ipv6)                  do {
313                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)                          ccs_check_acl(&r, ccs_check_inet_acl);
314                                 address, (const struct in6_addr *) address);                          error = ccs_audit_inet_log(&r);
315          else                  } while (error == CCS_RETRY_REQUEST);
316                  ccs_print_ipv4(buf, sizeof(buf), ip, ip);          }
317          do {          ccs_read_unlock(idx);
                 error = -EPERM;  
                 list_for_each_entry_rcu(ptr, &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_is_same_ip_network_acl(const struct ccs_acl_info *a,  static bool ccs_same_inet_acl(const struct ccs_acl_info *a,
364                                         const struct ccs_acl_info *b)                                        const struct ccs_acl_info *b)
365  {  {
366          const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1),          const struct ccs_inet_acl *p1 = container_of(a, typeof(*p1),
367                                                             head);                                                               head);
368          const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2),          const struct ccs_inet_acl *p2 = container_of(b, typeof(*p2),
369                                                             head);                                                               head);
370          return ccs_is_same_acl_head(&p1->head, &p2->head)          return ccs_same_acl_head(&p1->head, &p2->head)
371                    && p1->protocol == p2->protocol
372                  && p1->address_type == p2->address_type &&                  && p1->address_type == p2->address_type &&
373                  p1->address.ipv4.min == p2->address.ipv4.min &&                  p1->address.ipv4.min == p2->address.ipv4.min &&
374                  p1->address.ipv6.min == p2->address.ipv6.min &&                  p1->address.ipv6.min == p2->address.ipv6.min &&
375                  p1->address.ipv4.max == p2->address.ipv4.max &&                  p1->address.ipv4.max == p2->address.ipv4.max &&
376                  p1->address.ipv6.max == p2->address.ipv6.max &&                  p1->address.ipv6.max == p2->address.ipv6.max &&
377                  p1->address.group == p2->address.group &&                  p1->address.group == p2->address.group &&
378                  ccs_is_same_number_union(&p1->port, &p2->port);                  ccs_same_number_union(&p1->port, &p2->port);
379  }  }
380    
381  static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a,  static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
382                                       struct ccs_acl_info *b,                                        const struct ccs_acl_info *b)
                                      const bool is_delete)  
383  {  {
384          struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), head);          const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1),
385          const u16 perm = container_of(b, typeof(*p1), head)->perm;                                                               head);
386          if (is_delete) {          const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2),
387                  p1->perm &= ~perm;                                                               head);
388          } else {          return ccs_same_acl_head(&p1->head, &p2->head) &&
389                  if (p1->head.is_deleted)                  p1->protocol == p2->protocol &&
390                          p1->perm = 0;                  ccs_same_name_union(&p1->name, &p2->name);
391                  p1->perm |= perm;  }
392          }  
393          return !p1->perm;  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 e = {          struct ccs_inet_acl e = {
442                  .head.type = CCS_TYPE_IP_NETWORK_ACL,                  .head.type = CCS_TYPE_INET_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 CCS_IP_ADDRESS_TYPE_IPv6:          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;
# Line 399  int ccs_write_network_policy(char *data, Line 485  int ccs_write_network_policy(char *data,
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          error = ccs_update_domain_policy(&e.head, sizeof(e), is_delete, domain,          error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
489                                           ccs_is_same_ip_network_acl,                                    ccs_same_inet_acl, ccs_merge_inet_acl);
                                          ccs_merge_ip_network_acl);  
490   out:   out:
491          if (w[2][0] == '@')          if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
492                  ccs_put_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);
# Line 414  int ccs_write_network_policy(char *data, Line 499  int ccs_write_network_policy(char *data,
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 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 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;
   
 /**  
  * ccs_network_accept_acl - Check permission for accept() operation.  
  *  
  * @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 int ccs_network_accept_acl(const bool is_ipv6, const u8 *address,  
                                   const u16 port)  
 {  
         int retval;  
         current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT,  
                                    (const u32 *) address, ntohs(port));  
         current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         return retval;  
576  }  }
577    
578  /**  static void ccs_check_unix_address(struct sockaddr *addr,
579   * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.                                     const unsigned int addr_len,
580   *                                     struct ccs_unix_addr_info *address)
  * @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 int ccs_network_sendmsg_acl(const bool is_ipv6, const int sock_type,  
                                    const u8 *address, const u16 port)  
581  {  {
582          u8 operation;          address->addr = ((struct sockaddr_un *) addr)->sun_path;
583          if (sock_type == SOCK_DGRAM)          address->addr_len = addr_len;
584                  operation = CCS_NETWORK_UDP_CONNECT;          if (address->addr[0] && addr_len > sizeof(short) &&
585          else              addr_len <= sizeof(struct sockaddr_un))
586                  operation = CCS_NETWORK_RAW_CONNECT;                  ((char *) addr)[addr_len] = '\0';
         return ccs_network_entry(is_ipv6, operation,  
                                  (const u32 *) address, ntohs(port));  
587  }  }
588    
589  /**  static bool ccs_kernel_service(void)
  * ccs_network_recvmsg_acl - Check permission for recvmsg() operation.  
  *  
  * @is_ipv6:   True if @address is an IPv6 address.  
  * @sock_type: Type of socket. (UDP or RAW)  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static int ccs_network_recvmsg_acl(const bool is_ipv6, const int sock_type,  
                                    const u8 *address, const u16 port)  
590  {  {
591          int retval;          /* Nothing to do if I am a kernel service. */
592          const u8 operation          return segment_eq(get_fs(), KERNEL_DS);
                 = (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;  
593  }  }
594    
595  #ifndef CONFIG_NET  static u8 ccs_sock_family(struct socket *sock)
   
 void __init ccs_network_init(void)  
596  {  {
597            if (ccs_kernel_service())
598                    return 0;
599            switch (sock->sk->sk_family) {
600            case PF_INET:
601            case PF_INET6:
602                    return 1;
603            case PF_UNIX:
604                    return 2;
605            default:
606                    return 0;
607            }
608  }  }
609    
 #else  
   
 #define MAX_SOCK_ADDR 128 /* net/socket.c */  
   
610  /* Check permission for creating a socket. */  /* Check permission for creating a socket. */
611  static int __ccs_socket_create_permission(int family, int type, int protocol)  static int __ccs_socket_create_permission(int family, int type, int protocol)
612  {  {
613          int error = 0;          if (ccs_kernel_service())
         /* Nothing to do if I am a kernel service. */  
         if (segment_eq(get_fs(), KERNEL_DS))  
614                  return 0;                  return 0;
615          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
616                  return -EPERM;                  return -EPERM;
617          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
618                  return -EPERM;                  return -EPERM;
619          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;  
620  }  }
621    
622  /* Check permission for listening a TCP socket. */  /* Check permission for listening a socket. */
623  static int __ccs_socket_listen_permission(struct socket *sock)  static int __ccs_socket_listen_permission(struct socket *sock)
624  {  {
625          int error = 0;          struct sockaddr_storage addr;
626          char addr[MAX_SOCK_ADDR];          int error;
627          int addr_len;          int addr_len;
628          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
629          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
630                  return 0;          if (!family || (sock->type != SOCK_STREAM &&
631          if (sock->type != SOCK_STREAM)                          sock->type != SOCK_SEQPACKET))
632                  return 0;                  return 0;
633          switch (sock->sk->sk_family) {          error = sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len,
634          case PF_INET:                                     0);
635          case PF_INET6:          if (error)
636                  break;                  return error;
637          default:          address.operation = CCS_NETWORK_LISTEN;
638                  return 0;          if (family == 2) {
639          }                  address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
640          if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))                  ccs_check_unix_address((struct sockaddr *) &addr, addr_len,
641                  return -EPERM;                                         &address.unix0);
642          if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))                  error = ccs_unix_entry(&address);
643                  return -EPERM;          } else {
644          switch (((struct sockaddr *) addr)->sa_family) {                  address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
645                  struct sockaddr_in6 *addr6;                  if (ccs_check_inet_address((struct sockaddr *) &addr, addr_len,
646                  struct sockaddr_in *addr4;                                             &address.inet))
647          case AF_INET6:                          error = ccs_inet_entry(&address);
                 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;  
648          }          }
649          return error;          return error;
650  }  }
651    
652  /* Check permission for setting the remote IP address/port pair of a socket. */  /* Check permission for setting the remote address of a socket. */
653  static int __ccs_socket_connect_permission(struct socket *sock,  static int __ccs_socket_connect_permission(struct socket *sock,
654                                             struct sockaddr *addr, int addr_len)                                             struct sockaddr *addr, int addr_len)
655  {  {
656          int error = 0;          int error = 0;
657          const unsigned int type = sock->type;          const unsigned int type = sock->type;
658          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
659          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
660                  return 0;          if (!family)
661          switch (type) {                  return 0;
662          case SOCK_STREAM:          address.operation = CCS_NETWORK_CONNECT;
663          case SOCK_DGRAM:          if (family == 2) {
664          case SOCK_RAW:                  switch (type) {
665                  break;                  case SOCK_STREAM:
666          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)  
667                          break;                          break;
668                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_DGRAM:
669                  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_connect_acl(true, type,  
                                                 addr6->sin6_addr.s6_addr,  
                                                 port);  
                 break;  
         case AF_INET:  
                 if (addr_len < sizeof(struct sockaddr_in))  
670                          break;                          break;
671                  addr4 = (struct sockaddr_in *) addr;                  case SOCK_SEQPACKET:
672                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
673                          port = addr4->sin_port;                          break;
674                  else                  default:
675                          port = htons(sock->sk->sk_protocol);                          return 0;
676                  error = ccs_network_connect_acl(false, type,                  }
677                                                  (u8 *) &addr4->sin_addr,                  ccs_check_unix_address(addr, addr_len, &address.unix0);
678                                                  port);                  error = ccs_unix_entry(&address);
679                  break;          } else {
680          }                  switch (type) {
681          if (type != SOCK_STREAM)                  case SOCK_STREAM:
682                  return error;                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
683          switch (sock->sk->sk_family) {                          break;
684          case PF_INET:                  case SOCK_DGRAM:
685          case PF_INET6:                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
686                  if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))                          break;
687                          error = -EPERM;                  case SOCK_RAW:
688                  break;                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
689                            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          }          }
698          return error;          return error;
699  }  }
700    
701  /* Check permission for setting the local IP address/port pair of a socket. */  /* Check permission for setting the local address of a socket. */
702  static int __ccs_socket_bind_permission(struct socket *sock,  static int __ccs_socket_bind_permission(struct socket *sock,
703                                          struct sockaddr *addr, int addr_len)                                          struct sockaddr *addr, int addr_len)
704  {  {
705          int error = 0;          int error = 0;
706          const unsigned int type = sock->type;          const unsigned int type = sock->type;
707          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
708          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
709                  return 0;          if (!family)
710          switch (type) {                  return 0;
711          case SOCK_STREAM:          address.operation = CCS_NETWORK_BIND;
712          case SOCK_DGRAM:          if (family == 2) {
713          case SOCK_RAW:                  switch (type) {
714                  break;                  case SOCK_STREAM:
715          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)  
716                          break;                          break;
717                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_DGRAM:
718                  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))  
719                          break;                          break;
720                  addr4 = (struct sockaddr_in *) addr;                  case SOCK_SEQPACKET:
721                  if (type != SOCK_RAW)                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
722                          port = addr4->sin_port;                          break;
723                  else                  default:
724                          port = htons(sock->sk->sk_protocol);                          return 0;
725                  error = ccs_network_bind_acl(false, type,                  }
726                                               (u8 *) &addr4->sin_addr,                  ccs_check_unix_address(addr, addr_len, &address.unix0);
727                                               port);                  error = ccs_unix_entry(&address);
728                  break;          } else {
729          }                  switch (type) {
730          return error;                  case SOCK_STREAM:
731  }                          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
732                            break;
733  /*                  case SOCK_DGRAM:
734   * Check permission for accepting a TCP socket.                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
735   *                          break;
736   * Currently, the LSM hook for this purpose is not provided.                  case SOCK_RAW:
737   */                          address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
738  static int __ccs_socket_accept_permission(struct socket *sock,                          break;
739                                            struct sockaddr *addr)                  default:
740  {                          return 0;
741          int error = 0;                  }
742          int addr_len;                  ccs_check_inet_address(addr, addr_len, &address.inet);
743          /* Nothing to do if I am a kernel service. */                  if (type == SOCK_RAW)
744          if (segment_eq(get_fs(), KERNEL_DS))                          address.inet.port = htons(sock->sk->sk_protocol);
745                  return 0;                  error = ccs_inet_entry(&address);
         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;  
746          }          }
747          return error;          return error;
748  }  }
# Line 785  static int __ccs_socket_accept_permissio Line 751  static int __ccs_socket_accept_permissio
751  static int __ccs_socket_sendmsg_permission(struct socket *sock,  static int __ccs_socket_sendmsg_permission(struct socket *sock,
752                                             struct msghdr *msg, int size)                                             struct msghdr *msg, int size)
753  {  {
         struct sockaddr *addr = (struct sockaddr *) msg->msg_name;  
         const int addr_len = msg->msg_namelen;  
754          int error = 0;          int error = 0;
755          const int type = sock->type;          const int type = sock->type;
756          /* Nothing to do if I am a kernel service. */          struct ccs_addr_info address;
757          if (segment_eq(get_fs(), KERNEL_DS))          const u8 family = ccs_sock_family(sock);
758                  return 0;          if (!msg->msg_name || !family)
759          if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))                  return 0;
760                  return 0;          address.operation = CCS_NETWORK_SEND;
761          switch (addr->sa_family) {          if (family == 2) {
762                  struct sockaddr_in6 *addr6;                  if (type != SOCK_DGRAM)
763                  struct sockaddr_in *addr4;                          return 0;
764                  u16 port;                  address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
765          case AF_INET6:                  ccs_check_unix_address((struct sockaddr *) msg->msg_name,
766                  if (addr_len < SIN6_LEN_RFC2133)                                         msg->msg_namelen, &address.unix0);
767                    error = ccs_unix_entry(&address);
768            } else {
769                    switch (type) {
770                    case SOCK_DGRAM:
771                            address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
772                          break;                          break;
773                  addr6 = (struct sockaddr_in6 *) addr;                  case SOCK_RAW:
774                  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))  
775                          break;                          break;
776                  addr4 = (struct sockaddr_in *) addr;                  default:
777                  if (type == SOCK_DGRAM)                          return 0;
778                          port = addr4->sin_port;                  }
779                  else                  ccs_check_inet_address((struct sockaddr *) msg->msg_name,
780                          port = htons(sock->sk->sk_protocol);                                         msg->msg_namelen, &address.inet);
781                  error = ccs_network_sendmsg_acl(false, type,                  if (type == SOCK_RAW)
782                                                  (u8 *) &addr4->sin_addr, port);                          address.inet.port = htons(sock->sk->sk_protocol);
783                  break;                  error = ccs_inet_entry(&address);
784          }          }
785          return error;          return error;
786  }  }
787    
788    /* Check permission for accepting a TCP socket. */
789    static int __ccs_socket_post_accept_permission(struct socket *sock,
790                                                   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  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
815  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
816  #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2  #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
# Line 848  static inline struct ipv6hdr *ipv6_hdr(c Line 834  static inline struct ipv6hdr *ipv6_hdr(c
834  #endif  #endif
835  #endif  #endif
836    
837  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)  /* Check permission for receiving a datagram via a UDP or RAW socket. */
838  static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,  static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
839                                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)  
840  {  {
841            struct task_struct * const task = current;
842          int error = 0;          int error = 0;
843          const unsigned int type = sk->sk_type;          const unsigned int type = sk->sk_type;
844          if (type != SOCK_DGRAM && type != SOCK_RAW)          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;                  return 0;
858          /* Nothing to do if I am a kernel service. */          }
859          if (segment_eq(get_fs(), KERNEL_DS))          if (ccs_kernel_service())
860                  return 0;                  return 0;
   
861          switch (sk->sk_family) {          switch (sk->sk_family) {
                 struct in6_addr sin6;  
                 struct in_addr sin4;  
                 u16 port;  
862          case PF_INET6:          case PF_INET6:
863                  if (type == SOCK_DGRAM) { /* UDP IPv6 */                  address.inet.is_ipv6 = true;
864                          if (skb->protocol == htons(ETH_P_IP)) {                  if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP))
865                                  ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),                          ipv6_addr_set(&ip_address.sin6, 0, 0, htonl(0xffff),
866                                                ip_hdr(skb)->saddr);                                        ip_hdr(skb)->saddr);
867                          } else {                  else
868                                  ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);                          ipv6_addr_copy(&ip_address.sin6,
869                          }                                         &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);  
870                  break;                  break;
871          case PF_INET:          case PF_INET:
872                  if (type == SOCK_DGRAM) { /* UDP IPv4 */                  address.inet.is_ipv6 = false;
873                          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);  
874                  break;                  break;
875            default:
876                    goto skip;
877          }          }
878          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)  
879          if (type == SOCK_DGRAM)          if (type == SOCK_DGRAM)
880                  lock_sock(sk);                  address.inet.port = udp_hdr(skb)->source;
881  #endif          else
882          skb_kill_datagram(sk, skb, flags);                  address.inet.port = htons(sk->sk_protocol);
883  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)          address.operation = CCS_NETWORK_RECV;
884          if (type == SOCK_DGRAM)          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
885                  release_sock(sk);          error = ccs_inet_entry(&address);
886  #endif          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
887          /* Hope less harmful than -EPERM. */   skip:
888          return -ENOMEM;          return error;
889  }  }
890    
891  void __init ccs_network_init(void)  void __init ccs_network_init(void)
# Line 965  void __init ccs_network_init(void) Line 897  void __init ccs_network_init(void)
897          ccsecurity_ops.socket_connect_permission =          ccsecurity_ops.socket_connect_permission =
898                  __ccs_socket_connect_permission;                  __ccs_socket_connect_permission;
899          ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;          ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
900          ccsecurity_ops.socket_accept_permission =          ccsecurity_ops.socket_post_accept_permission =
901                  __ccs_socket_accept_permission;                  __ccs_socket_post_accept_permission;
902          ccsecurity_ops.socket_sendmsg_permission =          ccsecurity_ops.socket_sendmsg_permission =
903                  __ccs_socket_sendmsg_permission;                  __ccs_socket_sendmsg_permission;
904          ccsecurity_ops.socket_recvmsg_permission =          ccsecurity_ops.socket_post_recvmsg_permission =
905                  __ccs_socket_recvmsg_permission;                  __ccs_socket_post_recvmsg_permission;
906  }  }
907    
908  #endif  #endif

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

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