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

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 2944 by kumaneko, Mon Aug 24 05:00:52 2009 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/network.c revision 3916 by kumaneko, Mon Aug 23 08:15:53 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * security/ccsecurity/network.c   * security/ccsecurity/network.c
3   *   *
4   * Copyright (C) 2005-2009  NTT DATA CORPORATION   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Version: 1.7.0-pre   2009/08/24   * 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/sock.h>
19    #include <net/af_unix.h>
20  #include <net/ip.h>  #include <net/ip.h>
21  #include <net/ipv6.h>  #include <net/ipv6.h>
22  #include <net/udp.h>  #include <net/udp.h>
23  #include "internal.h"  #include "internal.h"
24    
25    struct ccs_inet_addr_info {
26            u16 port;           /* In network byte order. */
27            const u32 *address; /* In network byte order. */
28            bool is_ipv6;
29    };
30    
31    struct ccs_unix_addr_info {
32            u8 *addr;
33            unsigned int addr_len;
34    };
35    
36    struct ccs_addr_info {
37            u8 protocol;
38            u8 operation;
39            struct ccs_inet_addr_info inet;
40            struct ccs_unix_addr_info unix0;
41    };
42    
43    const char *ccs_inet_keyword[CCS_MAX_INET_PROTOCOL] = {
44            [CCS_NETWORK_INET_TCP_PROTOCOL] = "TCP",
45            [CCS_NETWORK_INET_UDP_PROTOCOL] = "UDP",
46            [CCS_NETWORK_INET_RAW_PROTOCOL] = "RAW",
47    };
48    
49    const char *ccs_unix_keyword[CCS_MAX_UNIX_PROTOCOL] = {
50            [CCS_NETWORK_UNIX_STREAM_PROTOCOL]    = "stream",
51            [CCS_NETWORK_UNIX_DGRAM_PROTOCOL]     = "dgram",
52            [CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL] = "seqpacket",
53    };
54    
55    const char *ccs_net_keyword[CCS_MAX_NETWORK_OPERATION] = {
56            [CCS_NETWORK_BIND]    = "bind",
57            [CCS_NETWORK_LISTEN]  = "listen",
58            [CCS_NETWORK_CONNECT] = "connect",
59            [CCS_NETWORK_ACCEPT]  = "accept",
60            [CCS_NETWORK_SEND]    = "send",
61            [CCS_NETWORK_RECV]    = "recv",
62    };
63    
64  /**  /**
65   * ccs_audit_network_log - Audit network log.   * ccs_audit_inet_log - Audit INET network log.
66   *   *
67   * @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.  
68   *   *
69   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
70   */   */
71  static int ccs_audit_network_log(struct ccs_request_info *r,  static int ccs_audit_inet_log(struct ccs_request_info *r)
72                                   const char *operation, const char *address,  {
73                                   const u16 port, const bool is_granted)          char buf[128];
74  {          const char *protocol =
75          if (!is_granted)                  ccs_inet_keyword[r->param.inet_network.protocol];
76                  ccs_warn_log(r, "%s %s %u", operation, address, port);          const char *operation = ccs_net_keyword[r->param.inet_network.operation];
77          return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK          const u32 *address = r->param.inet_network.address;
78                                     "%s %s %u\n", operation, address, port);          const u16 port = r->param.inet_network.port;
79            if (r->param.inet_network.is_ipv6)
80                    ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
81                                   address, (const struct in6_addr *) address);
82            else
83                    ccs_print_ipv4(buf, sizeof(buf), r->param.inet_network.ip,
84                                   r->param.inet_network.ip);
85            ccs_write_log(r, "network inet %s %s %s %u\n", protocol, operation,
86                          buf, port);
87            if (r->granted)
88                    return 0;
89            ccs_warn_log(r, "network inet %s %s %s %u", protocol, operation, buf,
90                         port);
91            return ccs_supervisor(r, "network inet %s %s %s %u\n", protocol,
92                                  operation, buf, port);
93    }
94    
95    /**
96     * ccs_audit_unix_log - Audit UNIX network log.
97     *
98     * @r: Pointer to "struct ccs_request_info".
99     *
100     * Returns 0 on success, negative value otherwise.
101     */
102    static int ccs_audit_unix_log(struct ccs_request_info *r)
103    {
104            const char *protocol =
105                    ccs_unix_keyword[r->param.unix_network.protocol];
106            const char *operation =
107                    ccs_net_keyword[r->param.unix_network.operation];
108            const char *address = r->param.unix_network.address->name;
109            ccs_write_log(r, "network unix %s %s %s\n", protocol, operation,
110                          address);
111            if (r->granted)
112                    return 0;
113            ccs_warn_log(r, "network unix %s %s %s", protocol, operation, address);
114            return ccs_supervisor(r, "network unix %s %s %s\n", protocol,
115                                  operation, address);
116  }  }
117    
118  /**  /**
# Line 47  static int ccs_audit_network_log(struct Line 122  static int ccs_audit_network_log(struct
122   * @min:     Pointer to store min address.   * @min:     Pointer to store min address.
123   * @max:     Pointer to store max address.   * @max:     Pointer to store max address.
124   *   *
125   * 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,
126     * CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4,
127     * CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise.
128   */   */
129  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
130  {  {
# Line 65  int ccs_parse_ip_address(char *address, Line 142  int ccs_parse_ip_address(char *address,
142                          min[i] = htons(min[i]);                          min[i] = htons(min[i]);
143                          max[i] = htons(max[i]);                          max[i] = htons(max[i]);
144                  }                  }
145                  return 2;                  return CCS_IP_ADDRESS_TYPE_IPv6;
146          }          }
147          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
148                         &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 152  int ccs_parse_ip_address(char *address,
152                                 + (((u8) min[2]) << 8) + (u8) min[3]);                                 + (((u8) min[2]) << 8) + (u8) min[3]);
153                  memmove(min, &ip, sizeof(ip));                  memmove(min, &ip, sizeof(ip));
154                  if (count == 8)                  if (count == 8)
155                          ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)                          ip = htonl((((u8) max[0]) << 24)
156                                       + (((u8) max[1]) << 16)
157                                     + (((u8) max[2]) << 8) + (u8) max[3]);                                     + (((u8) max[2]) << 8) + (u8) max[3]);
158                  memmove(max, &ip, sizeof(ip));                  memmove(max, &ip, sizeof(ip));
159                  return 1;                  return CCS_IP_ADDRESS_TYPE_IPv4;
160          }          }
161          return 0;          return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
162    }
163    
164    /**
165     * ccs_print_ipv4 - Print an IPv4 address.
166     *
167     * @buffer:     Buffer to write to.
168     * @buffer_len: Size of @buffer.
169     * @min_ip:     Min address in host byte order.
170     * @max_ip:     Max address in host byte order.
171     *
172     * Returns nothing.
173     */
174    void ccs_print_ipv4(char *buffer, const int buffer_len,
175                        const u32 min_ip, const u32 max_ip)
176    {
177            memset(buffer, 0, buffer_len);
178            snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u",
179                     HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-',
180                     HIPQUAD(max_ip));
181  }  }
182    
183  #if !defined(NIP6)  #if !defined(NIP6)
# Line 96  int ccs_parse_ip_address(char *address, Line 193  int ccs_parse_ip_address(char *address,
193   *   *
194   * @buffer:     Buffer to write to.   * @buffer:     Buffer to write to.
195   * @buffer_len: Size of @buffer.   * @buffer_len: Size of @buffer.
196   * @ip:         Pointer to "struct in6_addr".   * @min_ip:     Pointer to "struct in6_addr".
197     * @max_ip:     Pointer to "struct in6_addr".
198   *   *
199   * Returns nothing.   * Returns nothing.
200   */   */
201  void ccs_print_ipv6(char *buffer, const int buffer_len,  void ccs_print_ipv6(char *buffer, const int buffer_len,
202                      const struct in6_addr *ip)                      const struct in6_addr *min_ip,
203                        const struct in6_addr *max_ip)
204  {  {
205          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
206          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));          snprintf(buffer, buffer_len - 1,
207  }                   "%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x",
208                     NIP6(*min_ip), min_ip == max_ip ? '\0' : '-',
209  /**                   NIP6(*max_ip));
210   * ccs_net2keyword - Convert network operation index to network operation name.  }
211   *  
212   * @operation: Type of operation.  static bool ccs_check_inet_acl(struct ccs_request_info *r,
213   *                                 const struct ccs_acl_info *ptr)
214   * Returns the name of operation.  {
215   */          const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
216  const char *ccs_net2keyword(const u8 operation)          bool ret;
217  {          if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
218          const char *keyword = "unknown";              !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
219          switch (operation) {                  return false;
220          case CCS_NETWORK_UDP_BIND:          switch (acl->address_type) {
221                  keyword = "UDP bind";          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
222                  break;                  ret = ccs_address_matches_group(r->param.inet_network.is_ipv6,
223          case CCS_NETWORK_UDP_CONNECT:                                                  r->param.inet_network.address,
224                  keyword = "UDP connect";                                                  acl->address.group);
225                  break;                  break;
226          case CCS_NETWORK_TCP_BIND:          case CCS_IP_ADDRESS_TYPE_IPv4:
227                  keyword = "TCP bind";                  ret = !r->param.inet_network.is_ipv6 &&
228                  break;                          acl->address.ipv4.min <= r->param.inet_network.ip &&
229          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";  
230                  break;                  break;
231          case CCS_NETWORK_RAW_CONNECT:          default:
232                  keyword = "RAW connect";                  ret = r->param.inet_network.is_ipv6 &&
233                  break;                          memcmp(acl->address.ipv6.min,
234          }                                 r->param.inet_network.address, 16) <= 0 &&
235          return keyword;                          memcmp(r->param.inet_network.address,
236  }                                 acl->address.ipv6.max, 16) <= 0;
237                    break;
238            }
239            return ret;
240    }
241    
242    static bool ccs_check_unix_acl(struct ccs_request_info *r,
243                                   const struct ccs_acl_info *ptr)
244    {
245            const struct ccs_unix_acl *acl = container_of(ptr, typeof(*acl), head);
246            return (acl->perm & (1 << r->param.unix_network.operation)) &&
247                    ccs_compare_name_union(r->param.unix_network.address,
248                                           &acl->name);
249    }
250    
251    static const u8
252    ccs_inet2mac[CCS_MAX_INET_PROTOCOL][CCS_MAX_NETWORK_OPERATION] = {
253            [CCS_NETWORK_INET_TCP_PROTOCOL] = {
254                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_TCP_BIND,
255                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_INET_TCP_LISTEN,
256                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_INET_TCP_CONNECT,
257                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_INET_TCP_ACCEPT,
258            },
259            [CCS_NETWORK_INET_UDP_PROTOCOL] = {
260                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_UDP_BIND,
261                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_UDP_SEND,
262                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_UDP_RECV,
263            },
264            [CCS_NETWORK_INET_RAW_PROTOCOL]    = {
265                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_RAW_BIND,
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                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT,
278            },
279            [CCS_NETWORK_UNIX_DGRAM_PROTOCOL] = {
280                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_DGRAM_BIND,
281                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_UNIX_DGRAM_SEND,
282                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_UNIX_DGRAM_RECV,
283            },
284            [CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL] = {
285                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND,
286                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
287                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
288                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT,
289            },
290    };
291    
292  /**  /**
293   * ccs_network_entry2 - Check permission for network operation.   * ccs_inet_entry - Check permission for INET network operation.
294   *   *
295   * @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.  
296   *   *
297   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
  *  
  * Caller holds ccs_read_lock().  
298   */   */
299  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)  
300  {  {
301            const int idx = ccs_read_lock();
302          struct ccs_request_info r;          struct ccs_request_info r;
303          struct ccs_acl_info *ptr;          int error = 0;
304          const char *keyword = ccs_net2keyword(operation);          const u8 type = ccs_inet2mac[address->protocol][address->operation];
305          const u16 perm = 1 << operation;          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
306          /* using host byte order to allow u32 comparison than memcmp().*/                  struct task_struct * const task = current;
307          const u32 ip = ntohl(*address);                  const bool no_sleep = address->operation == CCS_NETWORK_ACCEPT
308          int error;                          || address->operation == CCS_NETWORK_RECV;
309          char buf[64];                  r.param_type = CCS_TYPE_INET_ACL;
310          ccs_assert_read_lock();                  r.param.inet_network.protocol = address->protocol;
311          if (ccs_init_request_info(&r, NULL,                  r.param.inet_network.operation = address->operation;
312                                    CCS_MAC_NETWORK_UDP_BIND + operation)                  r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
313              == CCS_MAC_MODE_DISABLED)                  r.param.inet_network.address = address->inet.address;
314                  return 0;                  r.param.inet_network.port = ntohs(address->inet.port);
315          memset(buf, 0, sizeof(buf));                  /* use host byte order to allow u32 comparison than memcmp().*/
316          if (is_ipv6)                  r.param.inet_network.ip = ntohl(*address->inet.address);
317                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)                  if (no_sleep)
318                                 address);                          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
319          else                  do {
320                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));                          ccs_check_acl(&r, ccs_check_inet_acl);
321          do {                          error = ccs_audit_inet_log(&r);
322                  error = -EPERM;                  } while (error == CCS_RETRY_REQUEST);
323                  list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {                  if (no_sleep)
324                          struct ccs_ip_network_acl *acl;                          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
325                          if (ptr->is_deleted ||          }
326                              ptr->type != CCS_TYPE_IP_NETWORK_ACL)          ccs_read_unlock(idx);
                                 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 == 1);  
         if (r.mode != CCS_MAC_MODE_ENFORCING)  
                 error = 0;  
327          return error;          return error;
328  }  }
329    
330  /**  /**
331   * ccs_network_entry - Check permission for network operation.   * ccs_unix_entry - Check permission for UNIX network operation.
332   *   *
333   * @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.  
334   *   *
335   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
336   */   */
337  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)  
338  {  {
339          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
340          const int error = ccs_network_entry2(is_ipv6, operation,          struct ccs_request_info r;
341                                               address, port);          int error = 0;
342            const u8 type = ccs_unix2mac[address->protocol][address->operation];
343            if (type && address->unix0.addr_len > sizeof(sa_family_t) &&
344                ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
345                    char *buf;
346                    if (address->unix0.addr[0])
347                            buf = ccs_encode(address->unix0.addr);
348                    else
349                            buf = ccs_encode2(address->unix0.addr,
350                                              address->unix0.addr_len
351                                              - sizeof(sa_family_t));
352                    if (buf) {
353                            struct task_struct * const task = current;
354                            const bool no_sleep =
355                                    address->operation == CCS_NETWORK_ACCEPT ||
356                                    address->operation == CCS_NETWORK_RECV;
357                            struct ccs_path_info addr;
358                            addr.name = buf;
359                            ccs_fill_path_info(&addr);
360                            r.param_type = CCS_TYPE_UNIX_ACL;
361                            r.param.unix_network.protocol = address->protocol;
362                            r.param.unix_network.operation = address->operation;
363                            r.param.unix_network.address = &addr;
364                            if (no_sleep)
365                                    task->ccs_flags |=
366                                            CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
367                    
368                            do {
369                                    ccs_check_acl(&r, ccs_check_unix_acl);
370                                    error = ccs_audit_unix_log(&r);
371                            } while (error == CCS_RETRY_REQUEST);
372                            if (no_sleep)
373                                    task->ccs_flags &=
374                                            ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
375                            kfree(buf);
376                    } else
377                            error = -ENOMEM;
378            }
379          ccs_read_unlock(idx);          ccs_read_unlock(idx);
380          return error;          return error;
381  }  }
382    
383    static bool ccs_same_inet_acl(const struct ccs_acl_info *a,
384                                          const struct ccs_acl_info *b)
385    {
386            const struct ccs_inet_acl *p1 = container_of(a, typeof(*p1), head);
387            const struct ccs_inet_acl *p2 = container_of(b, typeof(*p2), head);
388            return ccs_same_acl_head(&p1->head, &p2->head)
389                    && p1->protocol == p2->protocol
390                    && p1->address_type == p2->address_type &&
391                    p1->address.ipv4.min == p2->address.ipv4.min &&
392                    p1->address.ipv6.min == p2->address.ipv6.min &&
393                    p1->address.ipv4.max == p2->address.ipv4.max &&
394                    p1->address.ipv6.max == p2->address.ipv6.max &&
395                    p1->address.group == p2->address.group &&
396                    ccs_same_number_union(&p1->port, &p2->port);
397    }
398    
399    static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
400                                          const struct ccs_acl_info *b)
401    {
402            const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1), head);
403            const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2), head);
404            return ccs_same_acl_head(&p1->head, &p2->head) &&
405                    p1->protocol == p2->protocol &&
406                    ccs_same_name_union(&p1->name, &p2->name);
407    }
408    
409    static bool ccs_merge_inet_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
410                                   const bool is_delete)
411    {
412            u8 * const a_perm = &container_of(a, struct ccs_inet_acl, head)->perm;
413            u8 perm = *a_perm;
414            const u8 b_perm = container_of(b, struct ccs_inet_acl, head)->perm;
415            if (is_delete)
416                    perm &= ~b_perm;
417            else
418                    perm |= b_perm;
419            *a_perm = perm;
420            return !perm;
421    }
422    
423    static bool ccs_merge_unix_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
424                                   const bool is_delete)
425    {
426            u8 * const a_perm = &container_of(a, struct ccs_unix_acl, head)->perm;
427            u8 perm = *a_perm;
428            const u8 b_perm = container_of(b, struct ccs_unix_acl, head)->perm;
429            if (is_delete)
430                    perm &= ~b_perm;
431            else
432                    perm |= b_perm;
433            *a_perm = perm;
434            return !perm;
435    }
436    
437  /**  /**
438   * ccs_write_network_policy - Write "struct ccs_ip_network_acl" list.   * ccs_write_inet_network - Write "struct ccs_inet_acl" list.
439   *   *
440   * @data:      String to parse.   * @data:      String to parse.
441   * @domain:    Pointer to "struct ccs_domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
442   * @condition: Pointer to "struct ccs_condition". May be NULL.   * @condition: Pointer to "struct ccs_condition". Maybe NULL.
443   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
444   *   *
445   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
446   */   */
447  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,  int ccs_write_inet_network(char *data, struct ccs_domain_info *domain,
448                               struct ccs_condition *condition,                             struct ccs_condition *condition,
449                               const bool is_delete)                             const bool is_delete)
450  {  {
451          struct ccs_ip_network_acl *entry = NULL;          struct ccs_inet_acl e = {
452          struct ccs_acl_info *ptr;                  .head.type = CCS_TYPE_INET_ACL,
         struct ccs_ip_network_acl e = {  
                 .head.type = CCS_TYPE_IP_NETWORK_ACL,  
453                  .head.cond = condition,                  .head.cond = condition,
454          };          };
455          u16 min_address[8];          u16 min_address[8];
456          u16 max_address[8];          u16 max_address[8];
457          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
458          u8 sock_type;          u8 type;
459          char *w[4];          char *w[4];
460          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
461                  return -EINVAL;                  return -EINVAL;
462          if (!strcmp(w[0], "TCP"))          for (e.protocol = 0; e.protocol < CCS_MAX_INET_PROTOCOL; e.protocol++)
463                  sock_type = SOCK_STREAM;                  if (!strcmp(w[0], ccs_inet_keyword[e.protocol]))
         else if (!strcmp(w[0], "UDP"))  
                 sock_type = SOCK_DGRAM;  
         else if (!strcmp(w[0], "RAW"))  
                 sock_type = SOCK_RAW;  
         else  
                 return -EINVAL;  
         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;  
464                          break;                          break;
465                  }          for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
466          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))                  if (ccs_permstr(w[1], ccs_net_keyword[type]))
467                  e.perm = 1 << CCS_NETWORK_TCP_LISTEN;                          e.perm |= 1 << type;
468          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))          if (e.protocol == CCS_MAX_INET_PROTOCOL || !e.perm)
                 e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;  
         else  
469                  return -EINVAL;                  return -EINVAL;
470          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
471          case 2:          case CCS_IP_ADDRESS_TYPE_IPv6:
472                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
473                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
474                                                            min_address);                                                              min_address);
475                  e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)                  e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
476                                                            max_address);                                                              max_address);
477                  if (!e.address.ipv6.min || !e.address.ipv6.max)                  if (!e.address.ipv6.min || !e.address.ipv6.max)
478                          goto out;                          goto out;
479                  break;                  break;
480          case 1:          case CCS_IP_ADDRESS_TYPE_IPv4:
481                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
482                  /* use host byte order to allow u32 comparison.*/                  /* use host byte order to allow u32 comparison.*/
483                  e.address.ipv4.min = ntohl(*(u32 *) min_address);                  e.address.ipv4.min = ntohl(*(u32 *) min_address);
# Line 336  int ccs_write_network_policy(char *data, Line 487  int ccs_write_network_policy(char *data,
487                  if (w[2][0] != '@')                  if (w[2][0] != '@')
488                          return -EINVAL;                          return -EINVAL;
489                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
490                  e.address.group = ccs_get_address_group(w[2] + 1);                  e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP);
491                  if (!e.address.group)                  if (!e.address.group)
492                          return -ENOMEM;                          return -ENOMEM;
493                  break;                  break;
494          }          }
495          if (!ccs_parse_number_union(w[3], &e.port))          if (!ccs_parse_number_union(w[3], &e.port))
496                  goto out;                  goto out;
497          if (!is_delete)          error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
498                  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);  
499   out:   out:
500          if (w[2][0] == '@')          if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
501                  ccs_put_address_group(e.address.group);                  ccs_put_group(e.address.group);
502          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
503                  ccs_put_ipv6_address(e.address.ipv6.min);                  ccs_put_ipv6_address(e.address.ipv6.min);
504                  ccs_put_ipv6_address(e.address.ipv6.max);                  ccs_put_ipv6_address(e.address.ipv6.max);
505          }          }
506          ccs_put_number_union(&e.port);          ccs_put_number_union(&e.port);
         kfree(entry);  
507          return error;          return error;
508  }  }
509    
510  /**  /**
511   * ccs_network_listen_acl - Check permission for listen() operation.   * ccs_write_unix_network - Write "struct ccs_unix_acl" list.
512   *   *
513   * @is_ipv6: True if @address is an IPv6 address.   * @data:      String to parse.
514   * @address: An IPv4 or IPv6 address.   * @domain:    Pointer to "struct ccs_domain_info".
515   * @port:    Port number.   * @condition: Pointer to "struct ccs_condition". Maybe NULL.
516     * @is_delete: True if it is a delete request.
517   *   *
518   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
519   */   */
520  static inline int ccs_network_listen_acl(const bool is_ipv6,  int ccs_write_unix_network(char *data, struct ccs_domain_info *domain,
521                                           const u8 *address,                             struct ccs_condition *condition,
522                                           const u16 port)                             const bool is_delete)
523  {  {
524          return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN,          struct ccs_unix_acl e = {
525                                   (const u32 *) address, ntohs(port));                  .head.type = CCS_TYPE_UNIX_ACL,
526                    .head.cond = condition,
527            };
528            int error = is_delete ? -ENOENT : -ENOMEM;
529            u8 type;
530            char *w[3];
531            if (!ccs_tokenize(data, w, sizeof(w)) || !w[2][0])
532                    return -EINVAL;
533            for (e.protocol = 0; e.protocol < CCS_MAX_UNIX_PROTOCOL; e.protocol++)
534                    if (!strcmp(w[0], ccs_unix_keyword[e.protocol]))
535                            break;
536            for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
537                    if (ccs_permstr(w[1], ccs_net_keyword[type]))
538                            e.perm |= 1 << type;
539            if (e.protocol == CCS_MAX_UNIX_PROTOCOL || !e.perm)
540                    return -EINVAL;
541            if (!ccs_parse_name_union(w[2], &e.name))
542                    return -EINVAL;
543            error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
544                                      ccs_same_unix_acl, ccs_merge_unix_acl);
545            ccs_put_name_union(&e.name);
546            return error;
547  }  }
548    
549  /**  #ifndef CONFIG_NET
550   * ccs_network_connect_acl - Check permission for connect() operation.  
551   *  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)  
552  {  {
         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));  
553  }  }
554    
555  /**  #else
556   * ccs_network_bind_acl - Check permission for bind() operation.  
557   *  static int ccs_check_inet_address(const struct sockaddr *addr,
558   * @is_ipv6:   True if @address is an IPv6 address.                                    const unsigned int addr_len, const u16 port,
559   * @sock_type: Type of socket. (TCP or UDP or RAW)                                    struct ccs_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)  
560  {  {
561          u8 operation;          struct ccs_inet_addr_info *i = &address->inet;
562          switch (sock_type) {          switch (addr->sa_family) {
563          case SOCK_STREAM:          case AF_INET6:
564                  operation = CCS_NETWORK_TCP_BIND;                  if (addr_len < SIN6_LEN_RFC2133)
565                            goto skip;
566                    i->is_ipv6 = true;
567                    i->address = (u32 *)
568                            ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
569                    i->port = ((struct sockaddr_in6 *) addr)->sin6_port;
570                  break;                  break;
571          case SOCK_DGRAM:          case AF_INET:
572                  operation = CCS_NETWORK_UDP_BIND;                  if (addr_len < sizeof(struct sockaddr_in))
573                            goto skip;
574                    i->is_ipv6 = false;
575                    i->address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
576                    i->port = ((struct sockaddr_in *) addr)->sin_port;
577                  break;                  break;
578          default:          default:
579                  operation = CCS_NETWORK_RAW_BIND;                  goto skip;
580          }          }
581          return ccs_network_entry(is_ipv6, operation,          if (address->protocol == CCS_NETWORK_INET_RAW_PROTOCOL)
582                                   (const u32 *) address, ntohs(port));                  i->port = htons(port);
583            return ccs_inet_entry(address);
584     skip:
585            return 0;
586  }  }
587    
588  /**  static int ccs_unix_entry(const struct ccs_addr_info *address);
  * 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 inline 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;  
 }  
589    
590  /**  static int ccs_check_unix_address(struct sockaddr *addr,
591   * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.                                    const unsigned int addr_len,
592   *                                    struct ccs_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 inline int ccs_network_sendmsg_acl(const bool is_ipv6,  
                                           const int sock_type,  
                                           const u8 *address,  
                                           const u16 port)  
593  {  {
594          u8 operation;          struct ccs_unix_addr_info *u = &address->unix0;
595          if (sock_type == SOCK_DGRAM)          if (addr->sa_family != AF_UNIX)
596                  operation = CCS_NETWORK_UDP_CONNECT;                  return 0;
597          else          u->addr = ((struct sockaddr_un *) addr)->sun_path;
598                  operation = CCS_NETWORK_RAW_CONNECT;          u->addr_len = addr_len;
599          return ccs_network_entry(is_ipv6, operation,          /*
600                                   (const u32 *) address, ntohs(port));           * Terminate pathname with '\0' like unix_mkname() does.
601             * This is needed because pathname was copied by move_addr_to_kernel()
602             * but not yet terminated by unix_mkname().
603             */
604            if (u->addr[0] && addr_len > sizeof(short) &&
605                addr_len <= sizeof(struct sockaddr_un))
606                    ((char *) u->addr)[addr_len] = '\0';
607            return ccs_unix_entry(address);
608  }  }
609    
610  /**  static bool ccs_kernel_service(void)
611   * ccs_network_recvmsg_acl - Check permission for recvmsg() operation.  {
612   *          /* Nothing to do if I am a kernel service. */
613   * @is_ipv6:   True if @address is an IPv6 address.          return segment_eq(get_fs(), KERNEL_DS);
  * @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_recvmsg_acl(const bool is_ipv6,  
                                           const int sock_type,  
                                           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;  
614  }  }
615    
616  #define MAX_SOCK_ADDR 128 /* net/socket.c */  static u8 ccs_sock_family(struct sock *sk)
617    {
618            if (ccs_kernel_service())
619                    return 0;
620            switch (sk->sk_family) {
621            case PF_INET:
622            case PF_INET6:
623                    return 1;
624            case PF_UNIX:
625                    return 2;
626            default:
627                    return 0;
628            }
629    }
630    
631  /* Check permission for creating a socket. */  /* Check permission for creating a socket. */
632  int ccs_socket_create_permission(int family, int type, int protocol)  static int __ccs_socket_create_permission(int family, int type, int protocol)
633  {  {
634          int error = 0;          if (ccs_kernel_service())
         /* Nothing to do if I am a kernel service. */  
         if (segment_eq(get_fs(), KERNEL_DS))  
635                  return 0;                  return 0;
636          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))          if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
637                  return -EPERM;                  return -EPERM;
638          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))          if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
639                  return -EPERM;                  return -EPERM;
640          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;  
641  }  }
642    
643  /* Check permission for listening a TCP socket. */  /* Check permission for listening a socket. */
644  int ccs_socket_listen_permission(struct socket *sock)  static int __ccs_socket_listen_permission(struct socket *sock)
645  {  {
646          int error = 0;          struct ccs_addr_info address;
647          char addr[MAX_SOCK_ADDR];          const u8 family = ccs_sock_family(sock->sk);
648            const unsigned int type = sock->type;
649            struct sockaddr_storage addr;
650          int addr_len;          int addr_len;
651          /* Nothing to do if I am a kernel service. */          if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
         if (segment_eq(get_fs(), KERNEL_DS))  
                 return 0;  
         if (sock->type != SOCK_STREAM)  
                 return 0;  
         switch (sock->sk->sk_family) {  
         case PF_INET:  
         case PF_INET6:  
                 break;  
         default:  
652                  return 0;                  return 0;
653            {
654                    const int error = sock->ops->getname(sock, (struct sockaddr *)
655                                                         &addr, &addr_len, 0);
656                    if (error)
657                            return error;
658            }
659            address.operation = CCS_NETWORK_LISTEN;
660            if (family == 2) {
661                    if (type == SOCK_STREAM)
662                            address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
663                    else
664                            address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
665                    return ccs_check_unix_address((struct sockaddr *) &addr,
666                                                  addr_len, &address);
667          }          }
668          if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
669                  return -EPERM;          return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
670          if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))                                        &address);
                 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;  
671  }  }
672    
673  /* Check permission for setting the remote IP address/port pair of a socket. */  /* Check permission for setting the remote address of a socket. */
674  int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_connect_permission(struct socket *sock,
675                                    int addr_len)                                             struct sockaddr *addr, int addr_len)
676  {  {
677          int error = 0;          struct ccs_addr_info address;
678            const u8 family = ccs_sock_family(sock->sk);
679          const unsigned int type = sock->type;          const unsigned int type = sock->type;
680          /* Nothing to do if I am a kernel service. */          if (!family)
         if (segment_eq(get_fs(), KERNEL_DS))  
681                  return 0;                  return 0;
682            address.operation = CCS_NETWORK_CONNECT;
683            if (family == 2) {
684                    switch (type) {
685                    case SOCK_STREAM:
686                            address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
687                            break;
688                    case SOCK_DGRAM:
689                            address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
690                            address.operation = CCS_NETWORK_SEND;
691                            break;
692                    case SOCK_SEQPACKET:
693                            address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
694                            break;
695                    default:
696                            return 0;
697                    }
698                    return ccs_check_unix_address(addr, addr_len, &address);
699            }
700          switch (type) {          switch (type) {
701          case SOCK_STREAM:          case SOCK_STREAM:
702                    address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
703                    break;
704          case SOCK_DGRAM:          case SOCK_DGRAM:
705                    address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
706                    address.operation = CCS_NETWORK_SEND;
707                    break;
708          case SOCK_RAW:          case SOCK_RAW:
709                    address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
710                    address.operation = CCS_NETWORK_SEND;
711                  break;                  break;
712          default:          default:
713                  return 0;                  return 0;
714          }          }
715          switch (addr->sa_family) {          return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
716                  struct sockaddr_in6 *addr6;                                        &address);
                 struct sockaddr_in *addr4;  
                 u16 port;  
         case AF_INET6:  
                 if (addr_len < SIN6_LEN_RFC2133)  
                         break;  
                 addr6 = (struct sockaddr_in6 *) addr;  
                 if (type != SOCK_RAW)  
                         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))  
                         break;  
                 addr4 = (struct sockaddr_in *) addr;  
                 if (type != SOCK_RAW)  
                         port = addr4->sin_port;  
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_connect_acl(false, type,  
                                                 (u8 *) &addr4->sin_addr,  
                                                 port);  
                 break;  
         }  
         if (type != SOCK_STREAM)  
                 return error;  
         switch (sock->sk->sk_family) {  
         case PF_INET:  
         case PF_INET6:  
                 if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))  
                         error = -EPERM;  
                 break;  
         }  
         return error;  
717  }  }
718    
719  /* Check permission for setting the local IP address/port pair of a socket. */  /* Check permission for setting the local address of a socket. */
720  int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_bind_permission(struct socket *sock,
721                                 int addr_len)                                          struct sockaddr *addr, int addr_len)
722  {  {
723          int error = 0;          struct ccs_addr_info address;
724            const u8 family = ccs_sock_family(sock->sk);
725          const unsigned int type = sock->type;          const unsigned int type = sock->type;
726          /* Nothing to do if I am a kernel service. */          if (!family)
         if (segment_eq(get_fs(), KERNEL_DS))  
727                  return 0;                  return 0;
728            address.operation = CCS_NETWORK_BIND;
729            if (family == 2) {
730                    switch (type) {
731                    case SOCK_STREAM:
732                            address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
733                            break;
734                    case SOCK_DGRAM:
735                            address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
736                            break;
737                    case SOCK_SEQPACKET:
738                            address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
739                            break;
740                    default:
741                            return 0;
742                    }
743                    return ccs_check_unix_address(addr, addr_len, &address);
744            }
745          switch (type) {          switch (type) {
746          case SOCK_STREAM:          case SOCK_STREAM:
747                    address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
748                    break;
749          case SOCK_DGRAM:          case SOCK_DGRAM:
750                    address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
751                    break;
752          case SOCK_RAW:          case SOCK_RAW:
753                    address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
754                  break;                  break;
755          default:          default:
756                  return 0;                  return 0;
757          }          }
758          switch (addr->sa_family) {          return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
759                  struct sockaddr_in6 *addr6;                                        &address);
                 struct sockaddr_in *addr4;  
                 u16 port;  
         case AF_INET6:  
                 if (addr_len < SIN6_LEN_RFC2133)  
                         break;  
                 addr6 = (struct sockaddr_in6 *) addr;  
                 if (type != SOCK_RAW)  
                         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))  
                         break;  
                 addr4 = (struct sockaddr_in *) addr;  
                 if (type != SOCK_RAW)  
                         port = addr4->sin_port;  
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_bind_acl(false, type,  
                                              (u8 *) &addr4->sin_addr,  
                                              port);  
                 break;  
         }  
         return error;  
760  }  }
761    
762  /*  /* Check permission for sending a datagram via a UDP or RAW socket. */
763   * Check permission for accepting a TCP socket.  static int __ccs_socket_sendmsg_permission(struct socket *sock,
764   *                                             struct msghdr *msg, int size)
  * Currently, the LSM hook for this purpose is not provided.  
  */  
 int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr)  
765  {  {
766          int error = 0;          struct ccs_addr_info address;
767          int addr_len;          const u8 family = ccs_sock_family(sock->sk);
768          /* Nothing to do if I am a kernel service. */          const unsigned int type = sock->type;
769          if (segment_eq(get_fs(), KERNEL_DS))          if (!msg->msg_name || !family)
                 return 0;  
         switch (sock->sk->sk_family) {  
         case PF_INET:  
         case PF_INET6:  
                 break;  
         default:  
770                  return 0;                  return 0;
771            address.operation = CCS_NETWORK_SEND;
772            if (family == 2) {
773                    if (type != SOCK_DGRAM)
774                            return 0;
775                    address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
776                    return ccs_check_unix_address((struct sockaddr *)
777                                                  msg->msg_name, msg->msg_namelen,
778                                                  &address);
779          }          }
780          error = sock->ops->getname(sock, addr, &addr_len, 2);          switch (type) {
781          if (error)          case SOCK_DGRAM:
782                  return error;                  address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
         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);  
783                  break;                  break;
784          case AF_INET:          case SOCK_RAW:
785                  addr4 = (struct sockaddr_in *) addr;                  address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
                 error = ccs_network_accept_acl(false,  
                                                (u8 *) &addr4->sin_addr,  
                                                addr4->sin_port);  
786                  break;                  break;
787            default:
788                    return 0;
789          }          }
790          return error;          return ccs_check_inet_address((struct sockaddr *) msg->msg_name,
791                                          msg->msg_namelen, sock->sk->sk_protocol,
792                                          &address);
793  }  }
794    
795  /* Check permission for sending a datagram via a UDP or RAW socket. */  /* Check permission for accepting a socket. */
796  int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_post_accept_permission(struct socket *sock,
797                                    int addr_len)                                                 struct socket *newsock)
798  {  {
799          int error = 0;          struct ccs_addr_info address;
800          const int type = sock->type;          const u8 family = ccs_sock_family(sock->sk);
801          /* Nothing to do if I am a kernel service. */          const unsigned int type = sock->type;
802          if (segment_eq(get_fs(), KERNEL_DS))          struct sockaddr_storage addr;
803                  return 0;          int addr_len;
804          if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))          if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
805                  return 0;                  return 0;
806          switch (addr->sa_family) {          {
807                  struct sockaddr_in6 *addr6;                  const int error = newsock->ops->getname(newsock,
808                  struct sockaddr_in *addr4;                                                          (struct sockaddr *)
809                  u16 port;                                                          &addr, &addr_len, 2);
810          case AF_INET6:                  if (error)
811                  if (addr_len < SIN6_LEN_RFC2133)                          return error;
812                          break;          }
813                  addr6 = (struct sockaddr_in6 *) addr;          address.operation = CCS_NETWORK_ACCEPT;
814                  if (type == SOCK_DGRAM)          if (family == 2) {
815                          port = addr6->sin6_port;                  if (type == SOCK_STREAM)
816                  else                          address.protocol = CCS_NETWORK_UNIX_STREAM_PROTOCOL;
                         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))  
                         break;  
                 addr4 = (struct sockaddr_in *) addr;  
                 if (type == SOCK_DGRAM)  
                         port = addr4->sin_port;  
817                  else                  else
818                          port = htons(sock->sk->sk_protocol);                          address.protocol = CCS_NETWORK_UNIX_SEQPACKET_PROTOCOL;
819                  error = ccs_network_sendmsg_acl(false, type,                  return ccs_check_unix_address((struct sockaddr *) &addr,
820                                                  (u8 *) &addr4->sin_addr,                                                addr_len, &address);
821                                                  port);          }
822                  break;          address.protocol = CCS_NETWORK_INET_TCP_PROTOCOL;
823          }          return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
824          return error;                                        &address);
825  }  }
826    
827  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
828  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
829    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
830    
831  static inline struct iphdr *ip_hdr(const struct sk_buff *skb)  static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
832  {  {
# Line 816  static inline struct ipv6hdr *ipv6_hdr(c Line 845  static inline struct ipv6hdr *ipv6_hdr(c
845    
846  #endif  #endif
847  #endif  #endif
   
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)  
 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_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);  
 }  
848  #endif  #endif
849    
850  /*  /* Check permission for receiving a datagram. */
851   * Check permission for receiving a datagram via a UDP or RAW socket.  static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
852   *                                                  struct sk_buff *skb)
  * Currently, the LSM hook for this purpose is not provided.  
  */  
 int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb,  
                                   const unsigned int flags)  
853  {  {
854          int error = 0;          struct ccs_addr_info address;
855            const u8 family = ccs_sock_family(sk);
856          const unsigned int type = sk->sk_type;          const unsigned int type = sk->sk_type;
857          if (type != SOCK_DGRAM && type != SOCK_RAW)          struct sockaddr_storage addr;
858                  return 0;          if (!family)
         /* Nothing to do if I am a kernel service. */  
         if (segment_eq(get_fs(), KERNEL_DS))  
859                  return 0;                  return 0;
860            switch (type) {
861          switch (sk->sk_family) {          case SOCK_DGRAM:
862                  struct in6_addr sin6;                  if (family == 2)
863                  struct in_addr sin4;                          address.protocol = CCS_NETWORK_UNIX_DGRAM_PROTOCOL;
864                  u16 port;                  else
865          case PF_INET6:                          address.protocol = CCS_NETWORK_INET_UDP_PROTOCOL;
                 if (type == SOCK_DGRAM) { /* UDP IPv6 */  
                         if (skb->protocol == htons(ETH_P_IP)) {  
                                 ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),  
                                               ip_hdr(skb)->saddr);  
                         } else {  
                                 ipv6_addr_copy(&sin6, &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);  
866                  break;                  break;
867          case PF_INET:          case SOCK_RAW:
868                  if (type == SOCK_DGRAM) { /* UDP IPv4 */                  address.protocol = CCS_NETWORK_INET_RAW_PROTOCOL;
                         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);  
869                  break;                  break;
870          }          default:
         if (!error)  
871                  return 0;                  return 0;
872          /*          }
873           * Remove from queue if MSG_PEEK is used so that          address.operation = CCS_NETWORK_RECV;
874           * the head message from unwanted source in receive queue will not          if (family == 2) {
875           * prevent the caller from picking up next message from wanted source  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
876           * when the caller is using MSG_PEEK flag for picking up.                  struct unix_address *u = unix_sk(skb->sk)->addr;
877           */  #else
878  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)                  struct unix_address *u = skb->sk->protinfo.af_unix.addr;
         if (type == SOCK_DGRAM)  
                 lock_sock(sk);  
 #endif  
         skb_kill_datagram(sk, skb, flags);  
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  
         if (type == SOCK_DGRAM)  
                 release_sock(sk);  
879  #endif  #endif
880          /* Hope less harmful than -EPERM. */                  unsigned int addr_len;
881          return -ENOMEM;                  if (!u)
882                            return 0;
883                    addr_len = u->len;
884                    if (addr_len >= sizeof(addr))
885                            return 0;
886                    memcpy(&addr, u->name, addr_len);
887                    return ccs_check_unix_address((struct sockaddr *) &addr,
888                                                  addr_len, &address);
889            } else {
890                    struct in6_addr *sin6 = (struct in6_addr *) &addr;
891                    struct in_addr *sin4 = (struct in_addr *) &addr;
892                    switch (sk->sk_family) {
893                    case PF_INET6:
894                            address.inet.is_ipv6 = true;
895                            if (type == SOCK_DGRAM &&
896                                skb->protocol == htons(ETH_P_IP))
897                                    ipv6_addr_set(sin6, 0, 0, htonl(0xffff),
898                                                  ip_hdr(skb)->saddr);
899                            else
900                                    ipv6_addr_copy(sin6, &ipv6_hdr(skb)->saddr);
901                            break;
902                    case PF_INET:
903                            address.inet.is_ipv6 = false;
904                            sin4->s_addr = ip_hdr(skb)->saddr;
905                            break;
906                    default:
907                            return 0;
908                    }
909                    address.inet.address = (u32 *) &addr;
910                    if (type == SOCK_DGRAM)
911                            address.inet.port = udp_hdr(skb)->source;
912                    else
913                            address.inet.port = htons(sk->sk_protocol);
914                    return ccs_inet_entry(&address);
915            }
916  }  }
917  EXPORT_SYMBOL(ccs_socket_recvmsg_permission);  
918    void __init ccs_network_init(void)
919    {
920            ccsecurity_ops.socket_create_permission =
921                    __ccs_socket_create_permission;
922            ccsecurity_ops.socket_listen_permission =
923                    __ccs_socket_listen_permission;
924            ccsecurity_ops.socket_connect_permission =
925                    __ccs_socket_connect_permission;
926            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
927            ccsecurity_ops.socket_post_accept_permission =
928                    __ccs_socket_post_accept_permission;
929            ccsecurity_ops.socket_sendmsg_permission =
930                    __ccs_socket_sendmsg_permission;
931            ccsecurity_ops.socket_post_recvmsg_permission =
932                    __ccs_socket_post_recvmsg_permission;
933    }
934    
935    #endif

Legend:
Removed from v.2944  
changed lines
  Added in v.3916

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