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

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 branches/ccs-patch/security/ccsecurity/network.c revision 3731 by kumaneko, Fri Jun 4 01:42:54 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.7.2+   2010/06/04
7   *   *
8   * This file is applicable to both 2.4.30 and 2.6.11 and later.   * This file is applicable to both 2.4.30 and 2.6.11 and later.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 22  Line 22 
22  /**  /**
23   * ccs_audit_network_log - Audit network log.   * ccs_audit_network_log - Audit network log.
24   *   *
25   * @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.  
26   *   *
27   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
28   */   */
29  static int ccs_audit_network_log(struct ccs_request_info *r,  static int ccs_audit_network_log(struct ccs_request_info *r)
30                                   const char *operation, const char *address,  {
31                                   const u16 port, const bool is_granted)          char buf[128];
32  {          const char *operation = ccs_net2keyword(r->param.network.operation);
33          if (!is_granted)          const u32 *address = r->param.network.address;
34                  ccs_warn_log(r, "%s %s %u", operation, address, port);          const u16 port = r->param.network.port;
35          return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK          if (r->param.network.is_ipv6)
36                                     "%s %s %u\n", operation, address, port);                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
37                                   address, (const struct in6_addr *) address);
38            else
39                    ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip,
40                                   r->param.network.ip);
41            ccs_write_log(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n", operation,
42                          buf, port);
43            if (r->granted)
44                    return 0;
45            ccs_warn_log(r, "%s %s %u", operation, buf, port);
46            return ccs_supervisor(r, CCS_KEYWORD_ALLOW_NETWORK "%s %s %u\n",
47                                  operation, buf, port);
48  }  }
49    
50  /**  /**
# Line 47  static int ccs_audit_network_log(struct Line 54  static int ccs_audit_network_log(struct
54   * @min:     Pointer to store min address.   * @min:     Pointer to store min address.
55   * @max:     Pointer to store max address.   * @max:     Pointer to store max address.
56   *   *
57   * 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,
58     * CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4,
59     * CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise.
60   */   */
61  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)  int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
62  {  {
# Line 65  int ccs_parse_ip_address(char *address, Line 74  int ccs_parse_ip_address(char *address,
74                          min[i] = htons(min[i]);                          min[i] = htons(min[i]);
75                          max[i] = htons(max[i]);                          max[i] = htons(max[i]);
76                  }                  }
77                  return 2;                  return CCS_IP_ADDRESS_TYPE_IPv6;
78          }          }
79          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
80                         &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 84  int ccs_parse_ip_address(char *address,
84                                 + (((u8) min[2]) << 8) + (u8) min[3]);                                 + (((u8) min[2]) << 8) + (u8) min[3]);
85                  memmove(min, &ip, sizeof(ip));                  memmove(min, &ip, sizeof(ip));
86                  if (count == 8)                  if (count == 8)
87                          ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)                          ip = htonl((((u8) max[0]) << 24)
88                                       + (((u8) max[1]) << 16)
89                                     + (((u8) max[2]) << 8) + (u8) max[3]);                                     + (((u8) max[2]) << 8) + (u8) max[3]);
90                  memmove(max, &ip, sizeof(ip));                  memmove(max, &ip, sizeof(ip));
91                  return 1;                  return CCS_IP_ADDRESS_TYPE_IPv4;
92          }          }
93          return 0;          return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
94    }
95    
96    /**
97     * ccs_print_ipv4 - Print an IPv4 address.
98     *
99     * @buffer:     Buffer to write to.
100     * @buffer_len: Size of @buffer.
101     * @min_ip:     Min address in host byte order.
102     * @max_ip:     Max address in host byte order.
103     *
104     * Returns nothing.
105     */
106    void ccs_print_ipv4(char *buffer, const int buffer_len,
107                        const u32 min_ip, const u32 max_ip)
108    {
109            memset(buffer, 0, buffer_len);
110            snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u",
111                     HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-',
112                     HIPQUAD(max_ip));
113  }  }
114    
115  #if !defined(NIP6)  #if !defined(NIP6)
# Line 96  int ccs_parse_ip_address(char *address, Line 125  int ccs_parse_ip_address(char *address,
125   *   *
126   * @buffer:     Buffer to write to.   * @buffer:     Buffer to write to.
127   * @buffer_len: Size of @buffer.   * @buffer_len: Size of @buffer.
128   * @ip:         Pointer to "struct in6_addr".   * @min_ip:     Pointer to "struct in6_addr".
129     * @max_ip:     Pointer to "struct in6_addr".
130   *   *
131   * Returns nothing.   * Returns nothing.
132   */   */
133  void ccs_print_ipv6(char *buffer, const int buffer_len,  void ccs_print_ipv6(char *buffer, const int buffer_len,
134                      const struct in6_addr *ip)                      const struct in6_addr *min_ip,
135                        const struct in6_addr *max_ip)
136  {  {
137          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
138          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));          snprintf(buffer, buffer_len - 1,
139                     "%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x",
140                     NIP6(*min_ip), min_ip == max_ip ? '\0' : '-',
141                     NIP6(*max_ip));
142  }  }
143    
144  /**  /**
# Line 146  const char *ccs_net2keyword(const u8 ope Line 180  const char *ccs_net2keyword(const u8 ope
180          return keyword;          return keyword;
181  }  }
182    
183  /**  static bool ccs_check_network_acl(const struct ccs_request_info *r,
184   * ccs_network_entry2 - Check permission for network operation.                                    const struct ccs_acl_info *ptr)
  *  
  * @is_ipv6:   True if @address is an IPv6 address.  
  * @operation: Type of operation.  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  *  
  * Caller holds ccs_read_lock().  
  */  
 static int ccs_network_entry2(const bool is_ipv6, const u8 operation,  
                               const u32 *address, const u16 port)  
185  {  {
186          struct ccs_request_info r;          const struct ccs_ip_network_acl *acl =
187          struct ccs_acl_info *ptr;                  container_of(ptr, typeof(*acl), head);
188          const char *keyword = ccs_net2keyword(operation);          bool ret;
189          const u16 perm = 1 << operation;          if (!(acl->perm & (1 << r->param.network.operation)) ||
190          /* using host byte order to allow u32 comparison than memcmp().*/              !ccs_compare_number_union(r->param.network.port, &acl->port))
191          const u32 ip = ntohl(*address);                  return false;
192          int error;          switch (acl->address_type) {
193          char buf[64];          case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
194          ccs_assert_read_lock();                  ret = ccs_address_matches_group(r->param.network.is_ipv6,
195          if (ccs_init_request_info(&r, NULL,                                                  r->param.network.address,
196                                    CCS_MAC_NETWORK_UDP_BIND + operation)                                                  acl->address.group);
197              == CCS_MAC_MODE_DISABLED)                  break;
198                  return 0;          case CCS_IP_ADDRESS_TYPE_IPv4:
199          memset(buf, 0, sizeof(buf));                  ret = !r->param.network.is_ipv6 &&
200          if (is_ipv6)                          acl->address.ipv4.min <= r->param.network.ip &&
201                  ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)                          r->param.network.ip <= acl->address.ipv4.max;
202                                 address);                  break;
203          else          default:
204                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));                  ret = r->param.network.is_ipv6 &&
205          do {                          memcmp(acl->address.ipv6.min, r->param.network.address,
206                  error = -EPERM;                                 16) <= 0 &&
207                  list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {                          memcmp(r->param.network.address, acl->address.ipv6.max,
208                          struct ccs_ip_network_acl *acl;                                 16) <= 0;
209                          if (ptr->is_deleted ||                  break;
210                              ptr->type != CCS_TYPE_IP_NETWORK_ACL)          }
211                                  continue;          return ret;
                         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;  
         return error;  
212  }  }
213    
214  /**  /**
# Line 236  static int ccs_network_entry2(const bool Line 216  static int ccs_network_entry2(const bool
216   *   *
217   * @is_ipv6:   True if @address is an IPv6 address.   * @is_ipv6:   True if @address is an IPv6 address.
218   * @operation: Type of operation.   * @operation: Type of operation.
219   * @address:   An IPv4 or IPv6 address.   * @address:   An IPv4 or IPv6 address in network byte order.
220   * @port:      Port number.   * @port:      Port number in network byte order.
221   *   *
222   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
223   */   */
# Line 245  static int ccs_network_entry(const bool Line 225  static int ccs_network_entry(const bool
225                               const u32 *address, const u16 port)                               const u32 *address, const u16 port)
226  {  {
227          const int idx = ccs_read_lock();          const int idx = ccs_read_lock();
228          const int error = ccs_network_entry2(is_ipv6, operation,          struct ccs_request_info r;
229                                               address, port);          int error = 0;
230            if (ccs_init_request_info(&r, CCS_MAC_NETWORK_UDP_BIND + operation)
231                != CCS_CONFIG_DISABLED) {
232                    r.param_type = CCS_TYPE_IP_NETWORK_ACL;
233                    r.param.network.operation = operation;
234                    r.param.network.is_ipv6 = is_ipv6;
235                    r.param.network.address = address;
236                    r.param.network.port = ntohs(port);
237                    /* use host byte order to allow u32 comparison than memcmp().*/
238                    r.param.network.ip = ntohl(*address);
239                    do {
240                            ccs_check_acl(&r, ccs_check_network_acl);
241                            error = ccs_audit_network_log(&r);
242                    } while (error == CCS_RETRY_REQUEST);
243            }
244          ccs_read_unlock(idx);          ccs_read_unlock(idx);
245          return error;          return error;
246  }  }
247    
248    static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a,
249                                        const struct ccs_acl_info *b)
250    {
251            const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1),
252                                                               head);
253            const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2),
254                                                               head);
255            return ccs_same_acl_head(&p1->head, &p2->head)
256                    && p1->address_type == p2->address_type &&
257                    p1->address.ipv4.min == p2->address.ipv4.min &&
258                    p1->address.ipv6.min == p2->address.ipv6.min &&
259                    p1->address.ipv4.max == p2->address.ipv4.max &&
260                    p1->address.ipv6.max == p2->address.ipv6.max &&
261                    p1->address.group == p2->address.group &&
262                    ccs_same_number_union(&p1->port, &p2->port);
263    }
264    
265    static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a,
266                                         struct ccs_acl_info *b,
267                                         const bool is_delete)
268    {
269            u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head)
270                    ->perm;
271            u8 perm = *a_perm;
272            const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head)
273                    ->perm;
274            if (is_delete)
275                    perm &= ~b_perm;
276            else
277                    perm |= b_perm;
278            *a_perm = perm;
279            return !perm;
280    }
281    
282  /**  /**
283   * ccs_write_network_policy - Write "struct ccs_ip_network_acl" list.   * ccs_write_network - Write "struct ccs_ip_network_acl" list.
284   *   *
285   * @data:      String to parse.   * @data:      String to parse.
286   * @domain:    Pointer to "struct ccs_domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
# Line 261  static int ccs_network_entry(const bool Line 289  static int ccs_network_entry(const bool
289   *   *
290   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
291   */   */
292  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,  int ccs_write_network(char *data, struct ccs_domain_info *domain,
293                               struct ccs_condition *condition,                        struct ccs_condition *condition, const bool is_delete)
                              const bool is_delete)  
294  {  {
         struct ccs_ip_network_acl *entry = NULL;  
         struct ccs_acl_info *ptr;  
295          struct ccs_ip_network_acl e = {          struct ccs_ip_network_acl e = {
296                  .head.type = CCS_TYPE_IP_NETWORK_ACL,                  .head.type = CCS_TYPE_IP_NETWORK_ACL,
297                  .head.cond = condition,                  .head.cond = condition,
# Line 275  int ccs_write_network_policy(char *data, Line 300  int ccs_write_network_policy(char *data,
300          u16 max_address[8];          u16 max_address[8];
301          int error = is_delete ? -ENOENT : -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
302          u8 sock_type;          u8 sock_type;
303            u8 operation;
304          char *w[4];          char *w[4];
305          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
306                  return -EINVAL;                  return -EINVAL;
# Line 289  int ccs_write_network_policy(char *data, Line 315  int ccs_write_network_policy(char *data,
315          if (!strcmp(w[1], "bind"))          if (!strcmp(w[1], "bind"))
316                  switch (sock_type) {                  switch (sock_type) {
317                  case SOCK_STREAM:                  case SOCK_STREAM:
318                          e.perm = 1 << CCS_NETWORK_TCP_BIND;                          operation = CCS_NETWORK_TCP_BIND;
319                          break;                          break;
320                  case SOCK_DGRAM:                  case SOCK_DGRAM:
321                          e.perm = 1 << CCS_NETWORK_UDP_BIND;                          operation = CCS_NETWORK_UDP_BIND;
322                          break;                          break;
323                  default:                  default:
324                          e.perm = 1 << CCS_NETWORK_RAW_BIND;                          operation = CCS_NETWORK_RAW_BIND;
325                          break;                          break;
326                  }                  }
327          else if (!strcmp(w[1], "connect"))          else if (!strcmp(w[1], "connect"))
328                  switch (sock_type) {                  switch (sock_type) {
329                  case SOCK_STREAM:                  case SOCK_STREAM:
330                          e.perm = 1 << CCS_NETWORK_TCP_CONNECT;                          operation = CCS_NETWORK_TCP_CONNECT;
331                          break;                          break;
332                  case SOCK_DGRAM:                  case SOCK_DGRAM:
333                          e.perm = 1 << CCS_NETWORK_UDP_CONNECT;                          operation = CCS_NETWORK_UDP_CONNECT;
334                          break;                          break;
335                  default:                  default:
336                          e.perm = 1 << CCS_NETWORK_RAW_CONNECT;                          operation = CCS_NETWORK_RAW_CONNECT;
337                          break;                          break;
338                  }                  }
339          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))
340                  e.perm = 1 << CCS_NETWORK_TCP_LISTEN;                  operation = CCS_NETWORK_TCP_LISTEN;
341          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))
342                  e.perm = 1 << CCS_NETWORK_TCP_ACCEPT;                  operation = CCS_NETWORK_TCP_ACCEPT;
343          else          else
344                  return -EINVAL;                  return -EINVAL;
345            e.perm = 1 << operation;                        
346          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
347          case 2:          case CCS_IP_ADDRESS_TYPE_IPv6:
348                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
349                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
350                                                            min_address);                                                            min_address);
# Line 326  int ccs_write_network_policy(char *data, Line 353  int ccs_write_network_policy(char *data,
353                  if (!e.address.ipv6.min || !e.address.ipv6.max)                  if (!e.address.ipv6.min || !e.address.ipv6.max)
354                          goto out;                          goto out;
355                  break;                  break;
356          case 1:          case CCS_IP_ADDRESS_TYPE_IPv4:
357                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
358                  /* use host byte order to allow u32 comparison.*/                  /* use host byte order to allow u32 comparison.*/
359                  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 363  int ccs_write_network_policy(char *data,
363                  if (w[2][0] != '@')                  if (w[2][0] != '@')
364                          return -EINVAL;                          return -EINVAL;
365                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;                  e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
366                  e.address.group = ccs_get_address_group(w[2] + 1);                  e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP);
367                  if (!e.address.group)                  if (!e.address.group)
368                          return -ENOMEM;                          return -ENOMEM;
369                  break;                  break;
370          }          }
371          if (!ccs_parse_number_union(w[3], &e.port))          if (!ccs_parse_number_union(w[3], &e.port))
372                  goto out;                  goto out;
373          if (!is_delete)          error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain,
374                  entry = kmalloc(sizeof(e), GFP_KERNEL);                                    ccs_same_ip_network_acl,
375          mutex_lock(&ccs_policy_lock);                                    ccs_merge_ip_network_acl);
         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);  
376   out:   out:
377          if (w[2][0] == '@')          if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
378                  ccs_put_address_group(e.address.group);                  ccs_put_group(e.address.group);
379          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {          else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
380                  ccs_put_ipv6_address(e.address.ipv6.min);                  ccs_put_ipv6_address(e.address.ipv6.min);
381                  ccs_put_ipv6_address(e.address.ipv6.max);                  ccs_put_ipv6_address(e.address.ipv6.max);
382          }          }
383          ccs_put_number_union(&e.port);          ccs_put_number_union(&e.port);
         kfree(entry);  
384          return error;          return error;
385  }  }
386    
387  /**  #ifndef CONFIG_NET
  * ccs_network_listen_acl - Check permission for listen() 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_listen_acl(const bool is_ipv6,  
                                          const u8 *address,  
                                          const u16 port)  
 {  
         return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN,  
                                  (const u32 *) address, ntohs(port));  
 }  
   
 /**  
  * ccs_network_connect_acl - Check permission for connect() operation.  
  *  
  * @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)  
 {  
         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));  
 }  
388    
389  /**  void __init ccs_network_init(void)
  * ccs_network_bind_acl - Check permission for bind() operation.  
  *  
  * @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_bind_acl(const bool is_ipv6, const int sock_type,  
                                 const u8 *address, const u16 port)  
390  {  {
         u8 operation;  
         switch (sock_type) {  
         case SOCK_STREAM:  
                 operation = CCS_NETWORK_TCP_BIND;  
                 break;  
         case SOCK_DGRAM:  
                 operation = CCS_NETWORK_UDP_BIND;  
                 break;  
         default:  
                 operation = CCS_NETWORK_RAW_BIND;  
         }  
         return ccs_network_entry(is_ipv6, operation,  
                                  (const u32 *) address, ntohs(port));  
391  }  }
392    
393  /**  #else
  * 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;  
 }  
   
 /**  
  * ccs_network_sendmsg_acl - Check permission for sendmsg() operation.  
  *  
  * @is_ipv6:   True if @address is an IPv6 address.  
  * @sock_type: Type of socket. (UDP or RAW)  
  * @address:   An IPv4 or IPv6 address.  
  * @port:      Port number.  
  *  
  * Returns 0 on success, negative value otherwise.  
  */  
 static inline int ccs_network_sendmsg_acl(const bool is_ipv6,  
                                           const int sock_type,  
                                           const u8 *address,  
                                           const u16 port)  
 {  
         u8 operation;  
         if (sock_type == SOCK_DGRAM)  
                 operation = CCS_NETWORK_UDP_CONNECT;  
         else  
                 operation = CCS_NETWORK_RAW_CONNECT;  
         return ccs_network_entry(is_ipv6, operation,  
                                  (const u32 *) address, ntohs(port));  
 }  
   
 /**  
  * 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 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;  
 }  
394    
395  #define MAX_SOCK_ADDR 128 /* net/socket.c */  #define MAX_SOCK_ADDR 128 /* net/socket.c */
396    
397  /* Check permission for creating a socket. */  /* Check permission for creating a socket. */
398  int ccs_socket_create_permission(int family, int type, int protocol)  static int __ccs_socket_create_permission(int family, int type, int protocol)
399  {  {
400          int error = 0;          int error = 0;
401          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
# Line 565  int ccs_socket_create_permission(int fam Line 425  int ccs_socket_create_permission(int fam
425  }  }
426    
427  /* Check permission for listening a TCP socket. */  /* Check permission for listening a TCP socket. */
428  int ccs_socket_listen_permission(struct socket *sock)  static int __ccs_socket_listen_permission(struct socket *sock)
429  {  {
430          int error = 0;          int error = 0;
431          char addr[MAX_SOCK_ADDR];          char addr[MAX_SOCK_ADDR];
432          int addr_len;          int addr_len;
433            u32 *address;
434            u16 port;
435            bool is_ipv6;
436          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
437          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
438                  return 0;                  return 0;
# Line 587  int ccs_socket_listen_permission(struct Line 450  int ccs_socket_listen_permission(struct
450          if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))          if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))
451                  return -EPERM;                  return -EPERM;
452          switch (((struct sockaddr *) addr)->sa_family) {          switch (((struct sockaddr *) addr)->sa_family) {
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
453          case AF_INET6:          case AF_INET6:
454                  addr6 = (struct sockaddr_in6 *) addr;                  is_ipv6 = true;
455                  error = ccs_network_listen_acl(true,                  address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr
456                                                 addr6->sin6_addr.s6_addr,                          .s6_addr;
457                                                 addr6->sin6_port);                  port = ((struct sockaddr_in6 *) addr)->sin6_port;
458                  break;                  break;
459          case AF_INET:          case AF_INET:
460                  addr4 = (struct sockaddr_in *) addr;                  is_ipv6 = false;
461                  error = ccs_network_listen_acl(false,                  address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
462                                                 (u8 *) &addr4->sin_addr,                  port = ((struct sockaddr_in *) addr)->sin_port;
                                                addr4->sin_port);  
463                  break;                  break;
464            default:
465                    goto skip;
466          }          }
467            error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, address,
468                                      port);
469     skip:
470          return error;          return error;
471  }  }
472    
473  /* Check permission for setting the remote IP address/port pair of a socket. */  /* Check permission for setting the remote IP address/port pair of a socket. */
474  int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_connect_permission(struct socket *sock,
475                                    int addr_len)                                             struct sockaddr *addr, int addr_len)
476  {  {
477          int error = 0;          int error = 0;
478          const unsigned int type = sock->type;          const unsigned int type = sock->type;
479            u32 *address;
480            u16 port;
481            u8 operation;
482            bool is_ipv6;
483          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
484          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
485                  return 0;                  return 0;
486          switch (type) {          switch (type) {
487          case SOCK_STREAM:          case SOCK_STREAM:
488                    operation = CCS_NETWORK_TCP_CONNECT;
489                    break;
490          case SOCK_DGRAM:          case SOCK_DGRAM:
491                    operation = CCS_NETWORK_UDP_CONNECT;
492                    break;
493          case SOCK_RAW:          case SOCK_RAW:
494                    operation = CCS_NETWORK_RAW_CONNECT;
495                  break;                  break;
496          default:          default:
497                  return 0;                  return 0;
498          }          }
499          switch (addr->sa_family) {          switch (addr->sa_family) {
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
                 u16 port;  
500          case AF_INET6:          case AF_INET6:
501                  if (addr_len < SIN6_LEN_RFC2133)                  if (addr_len < SIN6_LEN_RFC2133)
502                          break;                          goto skip;
503                  addr6 = (struct sockaddr_in6 *) addr;                  is_ipv6 = true;
504                  if (type != SOCK_RAW)                  address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr
505                          port = addr6->sin6_port;                          .s6_addr;
506                  else                  port = ((struct sockaddr_in6 *) addr)->sin6_port;
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_connect_acl(true, type,  
                                                 addr6->sin6_addr.s6_addr,  
                                                 port);  
507                  break;                  break;
508          case AF_INET:          case AF_INET:
509                  if (addr_len < sizeof(struct sockaddr_in))                  if (addr_len < sizeof(struct sockaddr_in))
510                          break;                          goto skip;
511                  addr4 = (struct sockaddr_in *) addr;                  is_ipv6 = false;
512                  if (type != SOCK_RAW)                  address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
513                          port = addr4->sin_port;                  port = ((struct sockaddr_in *) addr)->sin_port;
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_connect_acl(false, type,  
                                                 (u8 *) &addr4->sin_addr,  
                                                 port);  
514                  break;                  break;
515            default:
516                    goto skip;
517          }          }
518            if (type == SOCK_RAW)
519                    port = htons(sock->sk->sk_protocol);
520            error = ccs_network_entry(is_ipv6, operation, address, port);
521     skip:
522          if (type != SOCK_STREAM)          if (type != SOCK_STREAM)
523                  return error;                  return error;
524          switch (sock->sk->sk_family) {          switch (sock->sk->sk_family) {
# Line 664  int ccs_socket_connect_permission(struct Line 532  int ccs_socket_connect_permission(struct
532  }  }
533    
534  /* Check permission for setting the local IP address/port pair of a socket. */  /* Check permission for setting the local IP address/port pair of a socket. */
535  int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_bind_permission(struct socket *sock,
536                                 int addr_len)                                          struct sockaddr *addr, int addr_len)
537  {  {
538          int error = 0;          int error = 0;
539          const unsigned int type = sock->type;          const unsigned int type = sock->type;
540            const u32 *address;
541            u16 port;
542            u8 operation;
543            bool is_ipv6;
544          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
545          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
546                  return 0;                  return 0;
547          switch (type) {          switch (type) {
548          case SOCK_STREAM:          case SOCK_STREAM:
549                    operation = CCS_NETWORK_TCP_BIND;
550                    break;
551          case SOCK_DGRAM:          case SOCK_DGRAM:
552                    operation = CCS_NETWORK_UDP_BIND;
553                    break;
554          case SOCK_RAW:          case SOCK_RAW:
555                    operation = CCS_NETWORK_RAW_BIND;
556                  break;                  break;
557          default:          default:
558                  return 0;                  return 0;
559          }          }
560          switch (addr->sa_family) {          switch (addr->sa_family) {
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
                 u16 port;  
561          case AF_INET6:          case AF_INET6:
562                  if (addr_len < SIN6_LEN_RFC2133)                  if (addr_len < SIN6_LEN_RFC2133)
563                          break;                          goto skip;
564                  addr6 = (struct sockaddr_in6 *) addr;                  is_ipv6 = true;
565                  if (type != SOCK_RAW)                  address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr
566                          port = addr6->sin6_port;                          .s6_addr;
567                  else                  port = ((struct sockaddr_in6 *) addr)->sin6_port;
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_bind_acl(true, type,  
                                              addr6->sin6_addr.s6_addr,  
                                              port);  
568                  break;                  break;
569          case AF_INET:          case AF_INET:
570                  if (addr_len < sizeof(struct sockaddr_in))                  if (addr_len < sizeof(struct sockaddr_in))
571                          break;                          goto skip;
572                  addr4 = (struct sockaddr_in *) addr;                  is_ipv6 = false;
573                  if (type != SOCK_RAW)                  address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
574                          port = addr4->sin_port;                  port = ((struct sockaddr_in *) addr)->sin_port;
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_bind_acl(false, type,  
                                              (u8 *) &addr4->sin_addr,  
                                              port);  
575                  break;                  break;
576            default:
577                    goto skip;
578          }          }
579            if (type == SOCK_RAW)
580                    port = htons(sock->sk->sk_protocol);
581            error = ccs_network_entry(is_ipv6, operation, address, port);
582     skip:
583          return error;          return error;
584  }  }
585    
# Line 717  int ccs_socket_bind_permission(struct so Line 588  int ccs_socket_bind_permission(struct so
588   *   *
589   * Currently, the LSM hook for this purpose is not provided.   * Currently, the LSM hook for this purpose is not provided.
590   */   */
591  int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr)  static int __ccs_socket_accept_permission(struct socket *sock,
592                                              struct sockaddr *addr)
593  {  {
594            struct task_struct * const task = current;
595          int error = 0;          int error = 0;
596          int addr_len;          int addr_len;
597            u32 *address;
598            u16 port;
599            bool is_ipv6;
600          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
601          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
602                  return 0;                  return 0;
# Line 735  int ccs_socket_accept_permission(struct Line 611  int ccs_socket_accept_permission(struct
611          if (error)          if (error)
612                  return error;                  return error;
613          switch (addr->sa_family) {          switch (addr->sa_family) {
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
614          case AF_INET6:          case AF_INET6:
615                  addr6 = (struct sockaddr_in6 *) addr;                  is_ipv6 = true;
616                  error = ccs_network_accept_acl(true,                  address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr
617                                                 addr6->sin6_addr.s6_addr,                          .s6_addr;
618                                                 addr6->sin6_port);                  port = ((struct sockaddr_in6 *) addr)->sin6_port;
619                  break;                  break;
620          case AF_INET:          case AF_INET:
621                  addr4 = (struct sockaddr_in *) addr;                  is_ipv6 = false;
622                  error = ccs_network_accept_acl(false,                  address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
623                                                 (u8 *) &addr4->sin_addr,                  port = ((struct sockaddr_in *) addr)->sin_port;
                                                addr4->sin_port);  
624                  break;                  break;
625            default:
626                    goto skip;
627          }          }
628            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
629            error = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, address,
630                                      port);
631            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
632     skip:
633          return error;          return error;
634  }  }
635    
636  /* Check permission for sending a datagram via a UDP or RAW socket. */  /* Check permission for sending a datagram via a UDP or RAW socket. */
637  int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr,  static int __ccs_socket_sendmsg_permission(struct socket *sock,
638                                    int addr_len)                                             struct msghdr *msg, int size)
639  {  {
640            struct sockaddr *addr = (struct sockaddr *) msg->msg_name;
641            const int addr_len = msg->msg_namelen;
642          int error = 0;          int error = 0;
643          const int type = sock->type;          const int type = sock->type;
644            u32 *address;
645            u16 port;
646            bool is_ipv6;
647            u8 operation;
648            if (!addr)
649                    return 0;
650          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
651          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
652                  return 0;                  return 0;
653          if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))          switch (type) {
654            case SOCK_DGRAM:
655                    operation = CCS_NETWORK_UDP_CONNECT;
656                    break;
657            case SOCK_RAW:
658                    operation = CCS_NETWORK_RAW_CONNECT;
659                    break;
660            default:
661                  return 0;                  return 0;
662            }
663          switch (addr->sa_family) {          switch (addr->sa_family) {
                 struct sockaddr_in6 *addr6;  
                 struct sockaddr_in *addr4;  
                 u16 port;  
664          case AF_INET6:          case AF_INET6:
665                  if (addr_len < SIN6_LEN_RFC2133)                  if (addr_len < SIN6_LEN_RFC2133)
666                          break;                          goto skip;
667                  addr6 = (struct sockaddr_in6 *) addr;                  is_ipv6 = true;
668                  if (type == SOCK_DGRAM)                  address = (u32 *) ((struct sockaddr_in6 *) addr)->sin6_addr
669                          port = addr6->sin6_port;                          .s6_addr;
670                  else                  port = ((struct sockaddr_in6 *) addr)->sin6_port;
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_sendmsg_acl(true, type,  
                                                 addr6->sin6_addr.s6_addr,  
                                                 port);  
671                  break;                  break;
672          case AF_INET:          case AF_INET:
673                  if (addr_len < sizeof(struct sockaddr_in))                  if (addr_len < sizeof(struct sockaddr_in))
674                          break;                          goto skip;
675                  addr4 = (struct sockaddr_in *) addr;                  is_ipv6 = false;
676                  if (type == SOCK_DGRAM)                  address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
677                          port = addr4->sin_port;                  port = ((struct sockaddr_in *) addr)->sin_port;
                 else  
                         port = htons(sock->sk->sk_protocol);  
                 error = ccs_network_sendmsg_acl(false, type,  
                                                 (u8 *) &addr4->sin_addr,  
                                                 port);  
678                  break;                  break;
679            default:
680                    goto skip;
681          }          }
682            if (type == SOCK_RAW)
683                    port = htons(sock->sk->sk_protocol);
684            error = ccs_network_entry(is_ipv6, operation, address, port);
685     skip:
686          return error;          return error;
687  }  }
688    
689  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
690  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5  #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
691    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
692    
693  static inline struct iphdr *ip_hdr(const struct sk_buff *skb)  static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
694  {  {
# Line 816  static inline struct ipv6hdr *ipv6_hdr(c Line 707  static inline struct ipv6hdr *ipv6_hdr(c
707    
708  #endif  #endif
709  #endif  #endif
710    #endif
711    
712  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)  #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
713  static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,  static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
# Line 860  static void skb_kill_datagram(struct soc Line 752  static void skb_kill_datagram(struct soc
752   *   *
753   * Currently, the LSM hook for this purpose is not provided.   * Currently, the LSM hook for this purpose is not provided.
754   */   */
755  int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb,  static int __ccs_socket_recvmsg_permission(struct sock *sk,
756                                    const unsigned int flags)                                             struct sk_buff *skb,
757                                               const unsigned int flags)
758  {  {
759            struct task_struct * const task = current;
760          int error = 0;          int error = 0;
761          const unsigned int type = sk->sk_type;          const unsigned int type = sk->sk_type;
762          if (type != SOCK_DGRAM && type != SOCK_RAW)          u16 port;
763            bool is_ipv6;
764            u8 operation;
765            union {
766                    struct in6_addr sin6;
767                    struct in_addr sin4;
768            } address;
769            switch (type) {
770            case SOCK_DGRAM:
771                    operation = CCS_NETWORK_UDP_CONNECT;
772                    break;
773            case SOCK_RAW:
774                    operation = CCS_NETWORK_RAW_CONNECT;
775                    break;
776            default:
777                  return 0;                  return 0;
778            }
779          /* Nothing to do if I am a kernel service. */          /* Nothing to do if I am a kernel service. */
780          if (segment_eq(get_fs(), KERNEL_DS))          if (segment_eq(get_fs(), KERNEL_DS))
781                  return 0;                  return 0;
   
782          switch (sk->sk_family) {          switch (sk->sk_family) {
                 struct in6_addr sin6;  
                 struct in_addr sin4;  
                 u16 port;  
783          case PF_INET6:          case PF_INET6:
784                  if (type == SOCK_DGRAM) { /* UDP IPv6 */                  is_ipv6 = true;
785                          if (skb->protocol == htons(ETH_P_IP)) {                  if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP))
786                                  ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),                          ipv6_addr_set(&address.sin6, 0, 0, htonl(0xffff),
787                                                ip_hdr(skb)->saddr);                                        ip_hdr(skb)->saddr);
788                          } else {                  else
789                                  ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);                          ipv6_addr_copy(&address.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);  
790                  break;                  break;
791          case PF_INET:          case PF_INET:
792                  if (type == SOCK_DGRAM) { /* UDP IPv4 */                  is_ipv6 = false;
793                          sin4.s_addr = ip_hdr(skb)->saddr;                  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);  
794                  break;                  break;
795            default:
796                    goto skip;
797          }          }
798            if (type == SOCK_DGRAM)
799                    port = udp_hdr(skb)->source;
800            else
801                    port = htons(sk->sk_protocol);
802            task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
803            error = ccs_network_entry(is_ipv6, operation, (u32 *) &address, port);
804            task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
805     skip:
806          if (!error)          if (!error)
807                  return 0;                  return 0;
808          /*          /*
# Line 911  int ccs_socket_recvmsg_permission(struct Line 811  int ccs_socket_recvmsg_permission(struct
811           * prevent the caller from picking up next message from wanted source           * prevent the caller from picking up next message from wanted source
812           * when the caller is using MSG_PEEK flag for picking up.           * when the caller is using MSG_PEEK flag for picking up.
813           */           */
814  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)          {
815          if (type == SOCK_DGRAM)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
816                  lock_sock(sk);                  bool slow = false;
817                    if (type == SOCK_DGRAM)
818                            slow = lock_sock_fast(sk);
819    #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
820                    if (type == SOCK_DGRAM)
821                            lock_sock(sk);
822  #endif  #endif
823          skb_kill_datagram(sk, skb, flags);                  skb_kill_datagram(sk, skb, flags);
824  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
825          if (type == SOCK_DGRAM)                  if (type == SOCK_DGRAM)
826                  release_sock(sk);                          unlock_sock_fast(sk, slow);
827    #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
828                    if (type == SOCK_DGRAM)
829                            release_sock(sk);
830  #endif  #endif
831            }
832          /* Hope less harmful than -EPERM. */          /* Hope less harmful than -EPERM. */
833          return -ENOMEM;          return -ENOMEM;
834  }  }
835  EXPORT_SYMBOL(ccs_socket_recvmsg_permission);  
836    void __init ccs_network_init(void)
837    {
838            ccsecurity_ops.socket_create_permission =
839                    __ccs_socket_create_permission;
840            ccsecurity_ops.socket_listen_permission =
841                    __ccs_socket_listen_permission;
842            ccsecurity_ops.socket_connect_permission =
843                    __ccs_socket_connect_permission;
844            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
845            ccsecurity_ops.socket_accept_permission =
846                    __ccs_socket_accept_permission;
847            ccsecurity_ops.socket_sendmsg_permission =
848                    __ccs_socket_sendmsg_permission;
849            ccsecurity_ops.socket_recvmsg_permission =
850                    __ccs_socket_recvmsg_permission;
851    }
852    
853    #endif

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

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