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

Subversion リポジトリの参照

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

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

revision 1084 by kumaneko, Thu Apr 3 09:54:46 2008 UTC revision 2039 by kumaneko, Wed Jan 7 04:42:05 2009 UTC
# Line 3  Line 3 
3   *   *
4   * Implementation of the Domain-Based Mandatory Access Control.   * Implementation of the Domain-Based Mandatory Access Control.
5   *   *
6   * Copyright (C) 2005-2008  NTT DATA CORPORATION   * Copyright (C) 2005-2009  NTT DATA CORPORATION
7   *   *
8   * Version: 1.6.0   2008/04/01   * Version: 1.6.6-pre   2009/01/05
9   *   *
10   * 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.
11   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
# Line 19  Line 19 
19  #include <linux/inet.h>  #include <linux/inet.h>
20  #include <linux/in.h>  #include <linux/in.h>
21  #include <linux/in6.h>  #include <linux/in6.h>
22    #include <net/ip.h>
23    #include <net/ipv6.h>
24    #include <net/udp.h>
25    
26    /* Index numbers for Network Controls. */
27    enum ccs_network_acl_index {
28            NETWORK_ACL_UDP_BIND,
29            NETWORK_ACL_UDP_CONNECT,
30            NETWORK_ACL_TCP_BIND,
31            NETWORK_ACL_TCP_LISTEN,
32            NETWORK_ACL_TCP_CONNECT,
33            NETWORK_ACL_TCP_ACCEPT,
34            NETWORK_ACL_RAW_BIND,
35            NETWORK_ACL_RAW_CONNECT
36    };
37    
38  /**  /**
39   * audit_network_log - Audit network log.   * ccs_audit_network_log - Audit network log.
40   *   *
41   * @is_ipv6:    True if @address is an IPv6 address.   * @r:          Pointer to "struct ccs_request_info".
42   * @operation:  The name of operation.   * @operation:  The name of operation.
43   * @address:    An IPv4 or IPv6 address.   * @address:    An IPv4 or IPv6 address.
44   * @port:       Port number.   * @port:       Port number.
45   * @is_granted: True if this is a granted log.   * @is_granted: True if this is a granted log.
  * @profile:    Profile number used.  
  * @mode:       Access control mode used.  
46   *   *
47   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
48   */   */
49  static int audit_network_log(const bool is_ipv6, const char *operation,  static int ccs_audit_network_log(struct ccs_request_info *r,
50                               const char *address, const u16 port,                                   const char *operation, const char *address,
51                               const bool is_granted,                                   const u16 port, const bool is_granted)
                              const u8 profile, const u8 mode)  
52  {  {
53          char *buf;          return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK
54          int len = 256;                                     "%s %s %u\n", operation, address, port);
         int len2;  
         if (ccs_can_save_audit_log(is_granted) < 0)  
                 return -ENOMEM;  
         buf = ccs_init_audit_log(&len, profile, mode, NULL);  
         if (!buf)  
                 return -ENOMEM;  
         len2 = strlen(buf);  
         snprintf(buf + len2, len - len2 - 1, KEYWORD_ALLOW_NETWORK "%s %s %u\n",  
                  operation, address, port);  
         return ccs_write_audit_log(buf, is_granted);  
55  }  }
56    
57  /**  /**
58   * save_ipv6_address - Keep the given IPv6 address on the RAM.   * ccs_save_ipv6_address - Keep the given IPv6 address on the RAM.
59   *   *
60   * @addr: Pointer to "struct in6_addr".   * @addr: Pointer to "struct in6_addr".
61   *   *
# Line 61  static int audit_network_log(const bool Line 63  static int audit_network_log(const bool
63   *   *
64   * The RAM is shared, so NEVER try to modify or kfree() the returned address.   * The RAM is shared, so NEVER try to modify or kfree() the returned address.
65   */   */
66  static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr)  static const struct in6_addr *ccs_save_ipv6_address(const struct in6_addr *addr)
67  {  {
68          static const u8 block_size = 16;          static const u8 ccs_block_size = 16;
69          struct addr_list {          struct ccs_addr_list {
70                  struct in6_addr addr[block_size];                  /* Workaround for gcc 4.3's bug. */
71                    struct in6_addr addr[16]; /* = ccs_block_size */
72                  struct list1_head list;                  struct list1_head list;
73                  u32 in_use_count;                  u32 in_use_count;
74          };          };
75          static LIST1_HEAD(address_list);          static LIST1_HEAD(ccs_address_list);
76          struct addr_list *ptr;          struct ccs_addr_list *ptr;
77          static DEFINE_MUTEX(lock);          static DEFINE_MUTEX(lock);
78          u8 i = block_size;          u8 i = ccs_block_size;
79          if (!addr)          if (!addr)
80                  return NULL;                  return NULL;
81          mutex_lock(&lock);          mutex_lock(&lock);
82          list1_for_each_entry(ptr, &address_list, list) {          list1_for_each_entry(ptr, &ccs_address_list, list) {
83                  for (i = 0; i < ptr->in_use_count; i++) {                  for (i = 0; i < ptr->in_use_count; i++) {
84                          if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))                          if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))
85                                  goto ok;                                  goto ok;
86                  }                  }
87                  if (i < block_size)                  if (i < ccs_block_size)
88                          break;                          break;
89          }          }
90          if (i == block_size) {          if (i == ccs_block_size) {
91                  ptr = ccs_alloc_element(sizeof(*ptr));                  ptr = ccs_alloc_element(sizeof(*ptr));
92                  if (!ptr)                  if (!ptr)
93                          goto ok;                          goto ok;
94                  list1_add_tail_mb(&ptr->list, &address_list);                  list1_add_tail_mb(&ptr->list, &ccs_address_list);
95                  i = 0;                  i = 0;
96          }          }
97          ptr->addr[ptr->in_use_count++] = *addr;          ptr->addr[ptr->in_use_count++] = *addr;
# Line 97  static const struct in6_addr *save_ipv6_ Line 100  static const struct in6_addr *save_ipv6_
100          return ptr ? &ptr->addr[i] : NULL;          return ptr ? &ptr->addr[i] : NULL;
101  }  }
102    
103  /* The list for "struct address_group_entry". */  /* The list for "struct ccs_address_group_entry". */
104  static LIST1_HEAD(address_group_list);  static LIST1_HEAD(ccs_address_group_list);
105    
106  /**  /**
107   * update_address_group_entry - Update "struct address_group_entry" list.   * ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list.
108   *   *
109   * @group_name:  The name of address group.   * @group_name:  The name of address group.
110   * @is_ipv6:     True if @min_address and @max_address are IPv6 addresses.   * @is_ipv6:     True if @min_address and @max_address are IPv6 addresses.
# Line 111  static LIST1_HEAD(address_group_list); Line 114  static LIST1_HEAD(address_group_list);
114   *   *
115   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
116   */   */
117  static int update_address_group_entry(const char *group_name,  static int ccs_update_address_group_entry(const char *group_name,
118                                        const bool is_ipv6,                                            const bool is_ipv6,
119                                        const u16 *min_address,                                            const u16 *min_address,
120                                        const u16 *max_address,                                            const u16 *max_address,
121                                        const bool is_delete)                                            const bool is_delete)
122  {  {
123          static DEFINE_MUTEX(lock);          static DEFINE_MUTEX(lock);
124          struct address_group_entry *new_group;          struct ccs_address_group_entry *new_group;
125          struct address_group_entry *group;          struct ccs_address_group_entry *group;
126          struct address_group_member *new_member;          struct ccs_address_group_member *new_member;
127          struct address_group_member *member;          struct ccs_address_group_member *member;
128          const struct path_info *saved_group_name;          const struct ccs_path_info *saved_group_name;
129          const struct in6_addr *saved_min_address = NULL;          const struct in6_addr *saved_min_address = NULL;
130          const struct in6_addr *saved_max_address = NULL;          const struct in6_addr *saved_max_address = NULL;
131          int error = -ENOMEM;          int error = -ENOMEM;
# Line 136  static int update_address_group_entry(co Line 139  static int update_address_group_entry(co
139          if (!is_ipv6)          if (!is_ipv6)
140                  goto not_ipv6;                  goto not_ipv6;
141          saved_min_address          saved_min_address
142                  = save_ipv6_address((struct in6_addr *) min_address);                  = ccs_save_ipv6_address((struct in6_addr *) min_address);
143          saved_max_address          saved_max_address
144                  = save_ipv6_address((struct in6_addr *) max_address);                  = ccs_save_ipv6_address((struct in6_addr *) max_address);
145          if (!saved_min_address || !saved_max_address)          if (!saved_min_address || !saved_max_address)
146                  return -ENOMEM;                  return -ENOMEM;
147   not_ipv6:   not_ipv6:
148          mutex_lock(&lock);          mutex_lock(&lock);
149          list1_for_each_entry(group, &address_group_list, list) {          list1_for_each_entry(group, &ccs_address_group_list, list) {
150                  if (saved_group_name != group->group_name)                  if (saved_group_name != group->group_name)
151                          continue;                          continue;
152                  list1_for_each_entry(member, &group->address_group_member_list,                  list1_for_each_entry(member,
153                                         &group->address_group_member_list,
154                                       list) {                                       list) {
155                          if (member->is_ipv6 != is_ipv6)                          if (member->is_ipv6 != is_ipv6)
156                                  continue;                                  continue;
# Line 176  static int update_address_group_entry(co Line 180  static int update_address_group_entry(co
180                          goto out;                          goto out;
181                  INIT_LIST1_HEAD(&new_group->address_group_member_list);                  INIT_LIST1_HEAD(&new_group->address_group_member_list);
182                  new_group->group_name = saved_group_name;                  new_group->group_name = saved_group_name;
183                  list1_add_tail_mb(&new_group->list, &address_group_list);                  list1_add_tail_mb(&new_group->list, &ccs_address_group_list);
184                  group = new_group;                  group = new_group;
185          }          }
186          new_member = ccs_alloc_element(sizeof(*new_member));          new_member = ccs_alloc_element(sizeof(*new_member));
# Line 190  static int update_address_group_entry(co Line 194  static int update_address_group_entry(co
194                  new_member->min.ipv4 = *(u32 *) min_address;                  new_member->min.ipv4 = *(u32 *) min_address;
195                  new_member->max.ipv4 = *(u32 *) max_address;                  new_member->max.ipv4 = *(u32 *) max_address;
196          }          }
197          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);          list1_add_tail_mb(&new_member->list,
198                              &group->address_group_member_list);
199          error = 0;          error = 0;
200   out:   out:
201          mutex_unlock(&lock);          mutex_unlock(&lock);
# Line 199  static int update_address_group_entry(co Line 204  static int update_address_group_entry(co
204  }  }
205    
206  /**  /**
207   * ccs_write_address_group_policy - Write "struct address_group_entry" list.   * ccs_parse_ip_address - Parse an IP address.
208     *
209     * @address: String to parse.
210     * @min:     Pointer to store min address.
211     * @max:     Pointer to store max address.
212     *
213     * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.
214     */
215    static int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
216    {
217            int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
218                               "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
219                               &min[0], &min[1], &min[2], &min[3],
220                               &min[4], &min[5], &min[6], &min[7],
221                               &max[0], &max[1], &max[2], &max[3],
222                               &max[4], &max[5], &max[6], &max[7]);
223            if (count == 8 || count == 16) {
224                    u8 i;
225                    if (count == 8)
226                            memmove(max, min, sizeof(u16) * 8);
227                    for (i = 0; i < 8; i++) {
228                            min[i] = htons(min[i]);
229                            max[i] = htons(max[i]);
230                    }
231                    return 2;
232            }
233            count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
234                           &min[0], &min[1], &min[2], &min[3],
235                           &max[0], &max[1], &max[2], &max[3]);
236            if (count == 4 || count == 8) {
237                    u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
238                                   + (((u8) min[2]) << 8) + (u8) min[3]);
239                    memmove(min, &ip, sizeof(ip));
240                    if (count == 8)
241                            ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)
242                                       + (((u8) max[2]) << 8) + (u8) max[3]);
243                    memmove(max, &ip, sizeof(ip));
244                    return 1;
245            }
246            return 0;
247    }
248    
249    /**
250     * ccs_write_address_group_policy - Write "struct ccs_address_group_entry" list.
251   *   *
252   * @data:      String to parse.   * @data:      String to parse.
253   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
# Line 208  static int update_address_group_entry(co Line 256  static int update_address_group_entry(co
256   */   */
257  int ccs_write_address_group_policy(char *data, const bool is_delete)  int ccs_write_address_group_policy(char *data, const bool is_delete)
258  {  {
         u8 count;  
259          bool is_ipv6;          bool is_ipv6;
260          u16 min_address[8];          u16 min_address[8];
261          u16 max_address[8];          u16 max_address[8];
# Line 216  int ccs_write_address_group_policy(char Line 263  int ccs_write_address_group_policy(char
263          if (!cp)          if (!cp)
264                  return -EINVAL;                  return -EINVAL;
265          *cp++ = '\0';          *cp++ = '\0';
266          count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"          switch (ccs_parse_ip_address(cp, min_address, max_address)) {
267                         "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",          case 2:
                        &min_address[0], &min_address[1],  
                        &min_address[2], &min_address[3],  
                        &min_address[4], &min_address[5],  
                        &min_address[6], &min_address[7],  
                        &max_address[0], &max_address[1],  
                        &max_address[2], &max_address[3],  
                        &max_address[4], &max_address[5],  
                        &max_address[6], &max_address[7]);  
         if (count == 8 || count == 16) {  
                 u8 i;  
                 for (i = 0; i < 8; i++) {  
                         min_address[i] = htons(min_address[i]);  
                         max_address[i] = htons(max_address[i]);  
                 }  
                 if (count == 8)  
                         memmove(max_address, min_address, sizeof(min_address));  
268                  is_ipv6 = true;                  is_ipv6 = true;
269                  goto ok;                  break;
270          }          case 1:
         count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",  
                        &min_address[0], &min_address[1],  
                        &min_address[2], &min_address[3],  
                        &max_address[0], &max_address[1],  
                        &max_address[2], &max_address[3]);  
         if (count == 4 || count == 8) {  
                 u32 ip = ((((u8) min_address[0]) << 24)  
                           + (((u8) min_address[1]) << 16)  
                           + (((u8) min_address[2]) << 8)  
                           + (u8) min_address[3]);  
                 *(u32 *) min_address = ip;  
                 if (count == 8)  
                         ip = ((((u8) max_address[0]) << 24)  
                               + (((u8) max_address[1]) << 16)  
                               + (((u8) max_address[2]) << 8)  
                               + (u8) max_address[3]);  
                 *(u32 *) max_address = ip;  
271                  is_ipv6 = false;                  is_ipv6 = false;
272                  goto ok;                  break;
273            default:
274                    return -EINVAL;
275          }          }
276          return -EINVAL;          return ccs_update_address_group_entry(data, is_ipv6, min_address,
277   ok:                                                max_address, is_delete);
         return update_address_group_entry(data, is_ipv6,  
                                           min_address, max_address, is_delete);  
278  }  }
279    
280  /**  /**
281   * find_or_assign_new_address_group - Create address group.   * ccs_find_or_assign_new_address_group - Create address group.
282   *   *
283   * @group_name: The name of address group.   * @group_name: The name of address group.
284   *   *
285   * Returns pointer to "struct address_group_entry" on success, NULL otherwise.   * Returns pointer to "struct ccs_address_group_entry" on success,
286     * NULL otherwise.
287   */   */
288  static struct address_group_entry *  static struct ccs_address_group_entry *
289  find_or_assign_new_address_group(const char *group_name)  ccs_find_or_assign_new_address_group(const char *group_name)
290  {  {
291          u8 i;          u8 i;
292          struct address_group_entry *group;          struct ccs_address_group_entry *group;
293          for (i = 0; i <= 1; i++) {          for (i = 0; i <= 1; i++) {
294                  list1_for_each_entry(group, &address_group_list, list) {                  list1_for_each_entry(group, &ccs_address_group_list, list) {
295                          if (!strcmp(group_name, group->group_name->name))                          if (!strcmp(group_name, group->group_name->name))
296                                  return group;                                  return group;
297                  }                  }
298                  if (!i) {                  if (!i) {
299                          const u16 dummy[2] = { 0, 0 };                          const u16 dummy[2] = { 0, 0 };
300                          update_address_group_entry(group_name, false,                          ccs_update_address_group_entry(group_name, false,
301                                                     dummy, dummy, false);                                                         dummy, dummy, false);
302                          update_address_group_entry(group_name, false,                          ccs_update_address_group_entry(group_name, false,
303                                                     dummy, dummy, true);                                                         dummy, dummy, true);
304                  }                  }
305          }          }
306          return NULL;          return NULL;
307  }  }
308    
309  /**  /**
310   * address_matches_to_group - Check whether the given address matches members of the given address group.   * ccs_address_matches_group - Check whether the given address matches members of the given address group.
311   *   *
312   * @is_ipv6: True if @address is an IPv6 address.   * @is_ipv6: True if @address is an IPv6 address.
313   * @address: An IPv4 or IPv6 address.   * @address: An IPv4 or IPv6 address.
314   * @group:   Pointer to "struct address_group_entry".   * @group:   Pointer to "struct ccs_address_group_entry".
315   *   *
316   * Returns true if @address matches addresses in @group group, false otherwise.   * Returns true if @address matches addresses in @group group, false otherwise.
317   */   */
318  static bool address_matches_to_group(const bool is_ipv6, const u32 *address,  static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address,
319                                       const struct address_group_entry *group)                                        const struct ccs_address_group_entry *
320                                          group)
321  {  {
322          struct address_group_member *member;          struct ccs_address_group_member *member;
323          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
324          list1_for_each_entry(member, &group->address_group_member_list, list) {          list1_for_each_entry(member, &group->address_group_member_list,
325                                 list) {
326                  if (member->is_deleted)                  if (member->is_deleted)
327                          continue;                          continue;
328                  if (member->is_ipv6) {                  if (member->is_ipv6) {
# Line 323  static bool address_matches_to_group(con Line 340  static bool address_matches_to_group(con
340  }  }
341    
342  /**  /**
343   * ccs_read_address_group_policy - Read "struct address_group_entry" list.   * ccs_read_address_group_policy - Read "struct ccs_address_group_entry" list.
344   *   *
345   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
346   *   *
# Line 333  bool ccs_read_address_group_policy(struc Line 350  bool ccs_read_address_group_policy(struc
350  {  {
351          struct list1_head *gpos;          struct list1_head *gpos;
352          struct list1_head *mpos;          struct list1_head *mpos;
353          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {          list1_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) {
354                  struct address_group_entry *group;                  struct ccs_address_group_entry *group;
355                  group = list1_entry(gpos, struct address_group_entry, list);                  group = list1_entry(gpos, struct ccs_address_group_entry, list);
356                  list1_for_each_cookie(mpos, head->read_var2,                  list1_for_each_cookie(mpos, head->read_var2,
357                                        &group->address_group_member_list) {                                        &group->address_group_member_list) {
358                          char buf[128];                          char buf[128];
359                          struct address_group_member *member;                          struct ccs_address_group_member *member;
360                          member = list1_entry(mpos, struct address_group_member,                          member = list1_entry(mpos,
361                                                 struct ccs_address_group_member,
362                                               list);                                               list);
363                          if (member->is_deleted)                          if (member->is_deleted)
364                                  continue;                                  continue;
# Line 352  bool ccs_read_address_group_policy(struc Line 370  bool ccs_read_address_group_policy(struc
370                                  ccs_print_ipv6(buf, sizeof(buf), min_address);                                  ccs_print_ipv6(buf, sizeof(buf), min_address);
371                                  if (min_address != max_address) {                                  if (min_address != max_address) {
372                                          int len;                                          int len;
373                                          char *cp = strchr(buf, '\0');                                          char *cp = buf + strlen(buf);
374                                          *cp++ = '-';                                          *cp++ = '-';
375                                          len = strlen(buf);                                          len = strlen(buf);
376                                          ccs_print_ipv6(cp, sizeof(buf) - len,                                          ccs_print_ipv6(cp, sizeof(buf) - len,
# Line 447  const char *ccs_net2keyword(const u8 ope Line 465  const char *ccs_net2keyword(const u8 ope
465  }  }
466    
467  /**  /**
468   * update_network_entry - Update "struct ip_network_acl_record" list.   * ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list.
469   *   *
470   * @operation:   Type of operation.   * @operation:   Type of operation.
471   * @record_type: Type of address.   * @record_type: Type of address.
472   * @group:       Pointer to "struct address_group_entry". May be NULL.   * @group:       Pointer to "struct ccs_address_group_entry". May be NULL.
473   * @min_address: Start of IPv4 or IPv6 address range.   * @min_address: Start of IPv4 or IPv6 address range.
474   * @max_address: End of IPv4 or IPv6 address range.   * @max_address: End of IPv4 or IPv6 address range.
475   * @min_port:    Start of port number range.   * @min_port:    Start of port number range.
476   * @max_port:    End of port number range.   * @max_port:    End of port number range.
477   * @domain:      Pointer to "struct domain_info".   * @domain:      Pointer to "struct domain_info".
478   * @condition:   Pointer to "struct condition_list". May be NULL.   * @condition:   Pointer to "struct ccs_condition_list". May be NULL.
479   * @is_delete:   True if it is a delete request.   * @is_delete:   True if it is a delete request.
480   *   *
481   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
482   */   */
483  static int update_network_entry(const u8 operation, const u8 record_type,  static int ccs_update_network_entry(const u8 operation, const u8 record_type,
484                                  const struct address_group_entry *group,                                      const struct ccs_address_group_entry *group,
485                                  const u32 *min_address, const u32 *max_address,                                      const u32 *min_address,
486                                  const u16 min_port, const u16 max_port,                                      const u32 *max_address,
487                                  struct domain_info *domain,                                      const u16 min_port, const u16 max_port,
488                                  const struct condition_list *condition,                                      struct domain_info *domain,
489                                  const bool is_delete)                                      const struct ccs_condition_list *condition,
490                                        const bool is_delete)
491  {  {
492          struct acl_info *ptr;          static DEFINE_MUTEX(lock);
493          struct ip_network_acl_record *acl;          struct ccs_acl_info *ptr;
494            struct ccs_ip_network_acl_record *acl;
495          int error = -ENOMEM;          int error = -ENOMEM;
496          /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
497          const u32 min_ip = ntohl(*min_address);          const u32 min_ip = ntohl(*min_address);
# Line 482  static int update_network_entry(const u8 Line 502  static int update_network_entry(const u8
502                  return -EINVAL;                  return -EINVAL;
503          if (record_type != IP_RECORD_TYPE_IPv6)          if (record_type != IP_RECORD_TYPE_IPv6)
504                  goto not_ipv6;                  goto not_ipv6;
505          saved_min_address = save_ipv6_address((struct in6_addr *) min_address);          saved_min_address = ccs_save_ipv6_address((struct in6_addr *)
506          saved_max_address = save_ipv6_address((struct in6_addr *) max_address);                                                    min_address);
507            saved_max_address = ccs_save_ipv6_address((struct in6_addr *)
508                                                      max_address);
509          if (!saved_min_address || !saved_max_address)          if (!saved_min_address || !saved_max_address)
510                  return -ENOMEM;                  return -ENOMEM;
511   not_ipv6:   not_ipv6:
512          mutex_lock(&domain_acl_lock);          mutex_lock(&lock);
513          if (is_delete)          if (is_delete)
514                  goto delete;                  goto delete;
515          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
# Line 495  static int update_network_entry(const u8 Line 517  static int update_network_entry(const u8
517                          continue;                          continue;
518                  if (ccs_get_condition_part(ptr) != condition)                  if (ccs_get_condition_part(ptr) != condition)
519                          continue;                          continue;
520                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
521                  if (acl->operation_type != operation ||                  if (acl->operation_type != operation ||
522                      acl->record_type != record_type ||                      acl->record_type != record_type ||
523                      acl->min_port != min_port || max_port != acl->max_port)                      acl->min_port != min_port || max_port != acl->max_port)
# Line 541  static int update_network_entry(const u8 Line 563  static int update_network_entry(const u8
563                          continue;                          continue;
564                  if (ccs_get_condition_part(ptr) != condition)                  if (ccs_get_condition_part(ptr) != condition)
565                          continue;                          continue;
566                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
567                  if (acl->operation_type != operation ||                  if (acl->operation_type != operation ||
568                      acl->record_type != record_type ||                      acl->record_type != record_type ||
569                      acl->min_port != min_port || max_port != acl->max_port)                      acl->min_port != min_port || max_port != acl->max_port)
# Line 562  static int update_network_entry(const u8 Line 584  static int update_network_entry(const u8
584                  break;                  break;
585          }          }
586   out:   out:
587          mutex_unlock(&domain_acl_lock);          mutex_unlock(&lock);
588          return error;          return error;
589  }  }
590    
591  /**  /**
592   * check_network_entry - Check permission for network operation.   * ccs_check_network_entry - Check permission for network operation.
593   *   *
594   * @is_ipv6:   True if @address is an IPv6 address.   * @is_ipv6:   True if @address is an IPv6 address.
595   * @operation: Type of operation.   * @operation: Type of operation.
# Line 576  static int update_network_entry(const u8 Line 598  static int update_network_entry(const u8
598   *   *
599   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
600   */   */
601  static int check_network_entry(const bool is_ipv6, const u8 operation,  static int ccs_check_network_entry(const bool is_ipv6, const u8 operation,
602                                 const u32 *address, const u16 port)                                     const u32 *address, const u16 port)
603  {  {
604          struct domain_info * const domain = current->domain_info;          struct ccs_request_info r;
605          struct acl_info *ptr;          struct ccs_acl_info *ptr;
606          const char *keyword = ccs_net2keyword(operation);          const char *keyword = ccs_net2keyword(operation);
607          const u8 profile = current->domain_info->profile;          bool is_enforce;
         const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_NETWORK);  
         const bool is_enforce = (mode == 3);  
608          /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
609          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
610          bool found = false;          bool found = false;
611          char buf[64];          char buf[64];
612          if (!mode)          if (!ccs_can_sleep())
613                  return 0;                  return 0;
614          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK);
615                  struct ip_network_acl_record *acl;          is_enforce = (r.mode == 3);
616            if (!r.mode)
617                    return 0;
618     retry:
619            list1_for_each_entry(ptr, &r.domain->acl_info_list, list) {
620                    struct ccs_ip_network_acl_record *acl;
621                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
622                          continue;                          continue;
623                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
624                  if (acl->operation_type != operation || port < acl->min_port ||                  if (acl->operation_type != operation || port < acl->min_port ||
625                      acl->max_port < port || !ccs_check_condition(ptr, NULL))                      acl->max_port < port || !ccs_check_condition(&r, ptr))
626                          continue;                          continue;
627                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
628                          if (!address_matches_to_group(is_ipv6, address,                          if (!ccs_address_matches_group(is_ipv6, address,
629                                                        acl->u.group))                                                         acl->u.group))
630                                  continue;                                  continue;
631                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {
632                          if (is_ipv6 ||                          if (is_ipv6 ||
# Line 613  static int check_network_entry(const boo Line 638  static int check_network_entry(const boo
638                              memcmp(address, acl->u.ipv6.max, 16) > 0)                              memcmp(address, acl->u.ipv6.max, 16) > 0)
639                                  continue;                                  continue;
640                  }                  }
641                  ccs_update_condition(ptr);                  r.cond = ccs_get_condition_part(ptr);
642                  found = true;                  found = true;
643                  break;                  break;
644          }          }
# Line 623  static int check_network_entry(const boo Line 648  static int check_network_entry(const boo
648                                 (const struct in6_addr *) address);                                 (const struct in6_addr *) address);
649          else          else
650                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
651          audit_network_log(is_ipv6, keyword, buf, port, found, profile, mode);          ccs_audit_network_log(&r, keyword, buf, port, found);
652          if (found)          if (found)
653                  return 0;                  return 0;
654          if (ccs_verbose_mode())          if (ccs_verbose_mode(r.domain))
655                  printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",                  printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",
656                         ccs_get_msg(is_enforce), keyword, buf, port,                         ccs_get_msg(is_enforce), keyword, buf, port,
657                         ccs_get_last_name(domain));                         ccs_get_last_name(r.domain));
658          if (is_enforce)          if (is_enforce) {
659                  return ccs_check_supervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s "                  int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK
660                                              "%s %u\n", domain->domainname->name,                                                   "%s %s %u\n", keyword, buf,
661                                              keyword, buf, port);                                                   port);
662          if (mode == 1 && ccs_check_domain_quota(domain))                  if (error == 1)
663                  update_network_entry(operation, is_ipv6 ?                          goto retry;
664                                       IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4,                  return error;
665                                       NULL, address, address, port, port, domain,          }
666                                       NULL, 0);          if (r.mode == 1 && ccs_check_domain_quota(r.domain))
667                    ccs_update_network_entry(operation, is_ipv6 ?
668                                             IP_RECORD_TYPE_IPv6 :
669                                             IP_RECORD_TYPE_IPv4,
670                                             NULL, address, address, port, port,
671                                             r.domain, ccs_handler_cond(), 0);
672          return 0;          return 0;
673  }  }
674    
675  /**  /**
676   * ccs_write_network_policy - Write "struct ip_network_acl_record" list.   * ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list.
677   *   *
678   * @data:      String to parse.   * @data:      String to parse.
679   * @domain:    Pointer to "struct domain_info".   * @domain:    Pointer to "struct domain_info".
680   * @condition: Pointer to "struct condition_list". May be NULL.   * @condition: Pointer to "struct ccs_condition_list". May be NULL.
681   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
682   *   *
683   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
684   */   */
685  int ccs_write_network_policy(char *data, struct domain_info *domain,  int ccs_write_network_policy(char *data, struct domain_info *domain,
686                               const struct condition_list *condition,                               const struct ccs_condition_list *condition,
687                               const bool is_delete)                               const bool is_delete)
688  {  {
689          u8 sock_type;          u8 sock_type;
# Line 661  int ccs_write_network_policy(char *data, Line 691  int ccs_write_network_policy(char *data,
691          u8 record_type;          u8 record_type;
692          u16 min_address[8];          u16 min_address[8];
693          u16 max_address[8];          u16 max_address[8];
694          struct address_group_entry *group = NULL;          struct ccs_address_group_entry *group = NULL;
695          u16 min_port;          u16 min_port;
696          u16 max_port;          u16 max_port;
697          u8 count;          u8 count;
# Line 714  int ccs_write_network_policy(char *data, Line 744  int ccs_write_network_policy(char *data,
744          if (!cp1)          if (!cp1)
745                  goto out;                  goto out;
746          *cp1++ = '\0';          *cp1++ = '\0';
747          count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"          switch (ccs_parse_ip_address(cp2, min_address, max_address)) {
748                         "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",          case 2:
                        &min_address[0], &min_address[1],  
                        &min_address[2], &min_address[3],  
                        &min_address[4], &min_address[5],  
                        &min_address[6], &min_address[7],  
                        &max_address[0], &max_address[1],  
                        &max_address[2], &max_address[3],  
                        &max_address[4], &max_address[5],  
                        &max_address[6], &max_address[7]);  
         if (count == 8 || count == 16) {  
                 u8 i;  
                 for (i = 0; i < 8; i++) {  
                         min_address[i] = htons(min_address[i]);  
                         max_address[i] = htons(max_address[i]);  
                 }  
                 if (count == 8)  
                         memmove(max_address, min_address, sizeof(min_address));  
749                  record_type = IP_RECORD_TYPE_IPv6;                  record_type = IP_RECORD_TYPE_IPv6;
750                  goto ok;                  break;
751          }          case 1:
         count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",  
                        &min_address[0], &min_address[1],  
                        &min_address[2], &min_address[3],  
                        &max_address[0], &max_address[1],  
                        &max_address[2], &max_address[3]);  
         if (count == 4 || count == 8) {  
                 u32 ip = htonl((((u8) min_address[0]) << 24)  
                                + (((u8) min_address[1]) << 16)  
                                + (((u8) min_address[2]) << 8)  
                                + (u8) min_address[3]);  
                 *(u32 *) min_address = ip;  
                 if (count == 8)  
                         ip = htonl((((u8) max_address[0]) << 24)  
                                    + (((u8) max_address[1]) << 16)  
                                    + (((u8) max_address[2]) << 8)  
                                    + (u8) max_address[3]);  
                 *(u32 *) max_address = ip;  
752                  record_type = IP_RECORD_TYPE_IPv4;                  record_type = IP_RECORD_TYPE_IPv4;
753                  goto ok;                  break;
754          }          default:
755          if (*cp2 == '@') {                  if (*cp2 != '@')
756                  group = find_or_assign_new_address_group(cp2 + 1);                          goto out;
757                    group = ccs_find_or_assign_new_address_group(cp2 + 1);
758                  if (!group)                  if (!group)
759                          return -ENOMEM;                          return -ENOMEM;
760                  record_type = IP_RECORD_TYPE_ADDRESS_GROUP;                  record_type = IP_RECORD_TYPE_ADDRESS_GROUP;
761                  goto ok;                  break;
762          }          }
  out:  
         return -EINVAL;  
  ok:  
763          if (strchr(cp1, ' '))          if (strchr(cp1, ' '))
764                  goto out;                  goto out;
765          count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);          count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);
# Line 772  int ccs_write_network_policy(char *data, Line 767  int ccs_write_network_policy(char *data,
767                  goto out;                  goto out;
768          if (count == 1)          if (count == 1)
769                  max_port = min_port;                  max_port = min_port;
770          return update_network_entry(operation, record_type, group,          return ccs_update_network_entry(operation, record_type, group,
771                                      (u32 *) min_address, (u32 *) max_address,                                          (u32 *) min_address,
772                                      min_port, max_port, domain, condition,                                          (u32 *) max_address,
773                                      is_delete);                                          min_port, max_port, domain, condition,
774                                            is_delete);
775     out:
776            return -EINVAL;
777  }  }
778    
779  /**  /**
# Line 787  int ccs_write_network_policy(char *data, Line 785  int ccs_write_network_policy(char *data,
785   *   *
786   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
787   */   */
788  int ccs_check_network_listen_acl(const _Bool is_ipv6, const u8 *address,  static inline int ccs_check_network_listen_acl(const bool is_ipv6,
789                                   const u16 port)                                                 const u8 *address,
790                                                   const u16 port)
791  {  {
792          return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,          return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,
793                                     (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
794  }  }
795    
796  /**  /**
# Line 804  int ccs_check_network_listen_acl(const _ Line 803  int ccs_check_network_listen_acl(const _
803   *   *
804   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
805   */   */
806  int ccs_check_network_connect_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_connect_acl(const bool is_ipv6,
807                                    const u8 *address, const u16 port)                                                  const int sock_type,
808                                                    const u8 *address,
809                                                    const u16 port)
810  {  {
811          u8 operation;          u8 operation;
812          switch (sock_type) {          switch (sock_type) {
# Line 818  int ccs_check_network_connect_acl(const Line 819  int ccs_check_network_connect_acl(const
819          default:          default:
820                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = NETWORK_ACL_RAW_CONNECT;
821          }          }
822          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
823                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
824  }  }
825    
826  /**  /**
# Line 832  int ccs_check_network_connect_acl(const Line 833  int ccs_check_network_connect_acl(const
833   *   *
834   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
835   */   */
836  int ccs_check_network_bind_acl(const _Bool is_ipv6, const int sock_type,  static int ccs_check_network_bind_acl(const bool is_ipv6, const int sock_type,
837                                 const u8 *address, const u16 port)                                        const u8 *address, const u16 port)
838  {  {
839          u8 operation;          u8 operation;
840          switch (sock_type) {          switch (sock_type) {
# Line 846  int ccs_check_network_bind_acl(const _Bo Line 847  int ccs_check_network_bind_acl(const _Bo
847          default:          default:
848                  operation = NETWORK_ACL_RAW_BIND;                  operation = NETWORK_ACL_RAW_BIND;
849          }          }
850          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
851                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
852  }  }
853    
854  /**  /**
# Line 859  int ccs_check_network_bind_acl(const _Bo Line 860  int ccs_check_network_bind_acl(const _Bo
860   *   *
861   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
862   */   */
863  int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address,  static inline int ccs_check_network_accept_acl(const bool is_ipv6,
864                                   const u16 port)                                                 const u8 *address,
865                                                   const u16 port)
866  {  {
867          int retval;          int retval;
868          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
869          retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,          retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,
870                                       (const u32 *) address, ntohs(port));                                           (const u32 *) address, ntohs(port));
871          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
872          return retval;          return retval;
873  }  }
# Line 880  int ccs_check_network_accept_acl(const _ Line 882  int ccs_check_network_accept_acl(const _
882   *   *
883   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
884   */   */
885  int ccs_check_network_sendmsg_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_sendmsg_acl(const bool is_ipv6,
886                                    const u8 *address, const u16 port)                                                  const int sock_type,
887                                                    const u8 *address,
888                                                    const u16 port)
889  {  {
890          u8 operation;          u8 operation;
891          if (sock_type == SOCK_DGRAM)          if (sock_type == SOCK_DGRAM)
892                  operation = NETWORK_ACL_UDP_CONNECT;                  operation = NETWORK_ACL_UDP_CONNECT;
893          else          else
894                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = NETWORK_ACL_RAW_CONNECT;
895          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
896                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
897  }  }
898    
899  /**  /**
# Line 902  int ccs_check_network_sendmsg_acl(const Line 906  int ccs_check_network_sendmsg_acl(const
906   *   *
907   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
908   */   */
909  int ccs_check_network_recvmsg_acl(const _Bool is_ipv6, const int sock_type,  static inline int ccs_check_network_recvmsg_acl(const bool is_ipv6,
910                                    const u8 *address, const u16 port)                                                  const int sock_type,
911                                                    const u8 *address,
912                                                    const u16 port)
913  {  {
914          int retval;          int retval;
915          const u8 operation          const u8 operation
916                  = (sock_type == SOCK_DGRAM) ?                  = (sock_type == SOCK_DGRAM) ?
917                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;
918          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
919          retval = check_network_entry(is_ipv6, operation, (const u32 *) address,          retval = ccs_check_network_entry(is_ipv6, operation,
920                                       ntohs(port));                                           (const u32 *) address, ntohs(port));
921          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
922          return retval;          return retval;
923  }  }
924    
925    #define MAX_SOCK_ADDR 128 /* net/socket.c */
926    
927    /* Check permission for creating a socket. */
928    int ccs_socket_create_permission(int family, int type, int protocol)
929    {
930            int error = 0;
931            /* Nothing to do if I am a kernel service. */
932            if (segment_eq(get_fs(), KERNEL_DS))
933                    return 0;
934            if (family == PF_PACKET && !ccs_capable(TOMOYO_USE_PACKET_SOCKET))
935                    return -EPERM;
936            if (family == PF_ROUTE && !ccs_capable(TOMOYO_USE_ROUTE_SOCKET))
937                    return -EPERM;
938            if (family != PF_INET && family != PF_INET6)
939                    return 0;
940            switch (type) {
941            case SOCK_STREAM:
942                    if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CREATE))
943                            error = -EPERM;
944                    break;
945            case SOCK_DGRAM:
946                    if (!ccs_capable(TOMOYO_USE_INET_DGRAM_SOCKET))
947                            error = -EPERM;
948                    break;
949            case SOCK_RAW:
950                    if (!ccs_capable(TOMOYO_USE_INET_RAW_SOCKET))
951                            error = -EPERM;
952                    break;
953            }
954            return error;
955    }
956    
957    /* Check permission for listening a TCP socket. */
958    int ccs_socket_listen_permission(struct socket *sock)
959    {
960            int error = 0;
961            char addr[MAX_SOCK_ADDR];
962            int addr_len;
963            /* Nothing to do if I am a kernel service. */
964            if (segment_eq(get_fs(), KERNEL_DS))
965                    return 0;
966            if (sock->type != SOCK_STREAM)
967                    return 0;
968            switch (sock->sk->sk_family) {
969            case PF_INET:
970            case PF_INET6:
971                    break;
972            default:
973                    return 0;
974            }
975            if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_LISTEN))
976                    return -EPERM;
977            if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))
978                    return -EPERM;
979            switch (((struct sockaddr *) addr)->sa_family) {
980                    struct sockaddr_in6 *addr6;
981                    struct sockaddr_in *addr4;
982            case AF_INET6:
983                    addr6 = (struct sockaddr_in6 *) addr;
984                    error = ccs_check_network_listen_acl(true,
985                                                         addr6->sin6_addr.s6_addr,
986                                                         addr6->sin6_port);
987                    break;
988            case AF_INET:
989                    addr4 = (struct sockaddr_in *) addr;
990                    error = ccs_check_network_listen_acl(false,
991                                                         (u8 *) &addr4->sin_addr,
992                                                         addr4->sin_port);
993                    break;
994            }
995            return error;
996    }
997    
998    /* Check permission for setting the remote IP address/port pair of a socket. */
999    int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr,
1000                                      int addr_len)
1001    {
1002            int error = 0;
1003            const unsigned int type = sock->type;
1004            /* Nothing to do if I am a kernel service. */
1005            if (segment_eq(get_fs(), KERNEL_DS))
1006                    return 0;
1007            switch (type) {
1008            case SOCK_STREAM:
1009            case SOCK_DGRAM:
1010            case SOCK_RAW:
1011                    break;
1012            default:
1013                    return 0;
1014            }
1015            switch (addr->sa_family) {
1016                    struct sockaddr_in6 *addr6;
1017                    struct sockaddr_in *addr4;
1018                    u16 port;
1019            case AF_INET6:
1020                    if (addr_len < SIN6_LEN_RFC2133)
1021                            break;
1022                    addr6 = (struct sockaddr_in6 *) addr;
1023                    if (type != SOCK_RAW)
1024                            port = addr6->sin6_port;
1025                    else
1026                            port = htons(sock->sk->sk_protocol);
1027                    error = ccs_check_network_connect_acl(true, type,
1028                                                          addr6->sin6_addr.s6_addr,
1029                                                          port);
1030                    break;
1031            case AF_INET:
1032                    if (addr_len < sizeof(struct sockaddr_in))
1033                            break;
1034                    addr4 = (struct sockaddr_in *) addr;
1035                    if (type != SOCK_RAW)
1036                            port = addr4->sin_port;
1037                    else
1038                            port = htons(sock->sk->sk_protocol);
1039                    error = ccs_check_network_connect_acl(false, type,
1040                                                          (u8 *) &addr4->sin_addr,
1041                                                          port);
1042                    break;
1043            }
1044            if (type != SOCK_STREAM)
1045                    return error;
1046            switch (sock->sk->sk_family) {
1047            case PF_INET:
1048            case PF_INET6:
1049                    if (!ccs_capable(TOMOYO_INET_STREAM_SOCKET_CONNECT))
1050                            error = -EPERM;
1051                    break;
1052            }
1053            return error;
1054    }
1055    
1056    /* Check permission for setting the local IP address/port pair of a socket. */
1057    int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
1058                                   int addr_len)
1059    {
1060            int error = 0;
1061            const unsigned int type = sock->type;
1062            /* Nothing to do if I am a kernel service. */
1063            if (segment_eq(get_fs(), KERNEL_DS))
1064                    return 0;
1065            switch (type) {
1066            case SOCK_STREAM:
1067            case SOCK_DGRAM:
1068            case SOCK_RAW:
1069                    break;
1070            default:
1071                    return 0;
1072            }
1073            switch (addr->sa_family) {
1074                    struct sockaddr_in6 *addr6;
1075                    struct sockaddr_in *addr4;
1076                    u16 port;
1077            case AF_INET6:
1078                    if (addr_len < SIN6_LEN_RFC2133)
1079                            break;
1080                    addr6 = (struct sockaddr_in6 *) addr;
1081                    if (type != SOCK_RAW)
1082                            port = addr6->sin6_port;
1083                    else
1084                            port = htons(sock->sk->sk_protocol);
1085                    error = ccs_check_network_bind_acl(true, type,
1086                                                       addr6->sin6_addr.s6_addr,
1087                                                       port);
1088                    break;
1089            case AF_INET:
1090                    if (addr_len < sizeof(struct sockaddr_in))
1091                            break;
1092                    addr4 = (struct sockaddr_in *) addr;
1093                    if (type != SOCK_RAW)
1094                            port = addr4->sin_port;
1095                    else
1096                            port = htons(sock->sk->sk_protocol);
1097                    error = ccs_check_network_bind_acl(false, type,
1098                                                       (u8 *) &addr4->sin_addr,
1099                                                       port);
1100                    break;
1101            }
1102            return error;
1103    }
1104    
1105    /*
1106     * Check permission for accepting a TCP socket.
1107     *
1108     * Currently, the LSM hook for this purpose is not provided.
1109     */
1110    int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr)
1111    {
1112            int error = 0;
1113            int addr_len;
1114            /* Nothing to do if I am a kernel service. */
1115            if (segment_eq(get_fs(), KERNEL_DS))
1116                    return 0;
1117            switch (sock->sk->sk_family) {
1118            case PF_INET:
1119            case PF_INET6:
1120                    break;
1121            default:
1122                    return 0;
1123            }
1124            error = sock->ops->getname(sock, addr, &addr_len, 2);
1125            if (error)
1126                    return error;
1127            switch (addr->sa_family) {
1128                    struct sockaddr_in6 *addr6;
1129                    struct sockaddr_in *addr4;
1130            case AF_INET6:
1131                    addr6 = (struct sockaddr_in6 *) addr;
1132                    error = ccs_check_network_accept_acl(true,
1133                                                         addr6->sin6_addr.s6_addr,
1134                                                         addr6->sin6_port);
1135                    break;
1136            case AF_INET:
1137                    addr4 = (struct sockaddr_in *) addr;
1138                    error = ccs_check_network_accept_acl(false,
1139                                                         (u8 *) &addr4->sin_addr,
1140                                                         addr4->sin_port);
1141                    break;
1142            }
1143            return error;
1144    }
1145    
1146    /* Check permission for sending a datagram via a UDP or RAW socket. */
1147    int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr,
1148                                      int addr_len)
1149    {
1150            int error = 0;
1151            const int type = sock->type;
1152            /* Nothing to do if I am a kernel service. */
1153            if (segment_eq(get_fs(), KERNEL_DS))
1154                    return 0;
1155            if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))
1156                    return 0;
1157            switch (addr->sa_family) {
1158                    struct sockaddr_in6 *addr6;
1159                    struct sockaddr_in *addr4;
1160                    u16 port;
1161            case AF_INET6:
1162                    if (addr_len < SIN6_LEN_RFC2133)
1163                            break;
1164                    addr6 = (struct sockaddr_in6 *) addr;
1165                    if (type == SOCK_DGRAM)
1166                            port = addr6->sin6_port;
1167                    else
1168                            port = htons(sock->sk->sk_protocol);
1169                    error = ccs_check_network_sendmsg_acl(true, type,
1170                                                          addr6->sin6_addr.s6_addr,
1171                                                          port);
1172                    break;
1173            case AF_INET:
1174                    if (addr_len < sizeof(struct sockaddr_in))
1175                            break;
1176                    addr4 = (struct sockaddr_in *) addr;
1177                    if (type == SOCK_DGRAM)
1178                            port = addr4->sin_port;
1179                    else
1180                            port = htons(sock->sk->sk_protocol);
1181                    error = ccs_check_network_sendmsg_acl(false, type,
1182                                                          (u8 *) &addr4->sin_addr,
1183                                                          port);
1184                    break;
1185            }
1186            return error;
1187    }
1188    
1189    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
1190    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
1191    
1192    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
1193    {
1194            return skb->nh.iph;
1195    }
1196    
1197    static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
1198    {
1199            return skb->h.uh;
1200    }
1201    
1202    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
1203    {
1204            return skb->nh.ipv6h;
1205    }
1206    
1207    #endif
1208    #endif
1209    
1210    /*
1211     * Check permission for receiving a datagram via a UDP or RAW socket.
1212     *
1213     * Currently, the LSM hook for this purpose is not provided.
1214     */
1215    int ccs_socket_recv_datagram_permission(struct sock *sk, struct sk_buff *skb,
1216                                            const unsigned int flags)
1217    {
1218            int error = 0;
1219            const unsigned int type = sk->sk_type;
1220            /* Nothing to do if I didn't receive a datagram. */
1221            if (!skb)
1222                    return 0;
1223            /* Nothing to do if I can't sleep. */
1224    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
1225            if (in_interrupt())
1226                    return 0;
1227    #else
1228            if (in_atomic())
1229                    return 0;
1230    #endif
1231            /* Nothing to do if I am a kernel service. */
1232            if (segment_eq(get_fs(), KERNEL_DS))
1233                    return 0;
1234            if (type != SOCK_DGRAM && type != SOCK_RAW)
1235                    return 0;
1236    
1237            switch (sk->sk_family) {
1238                    struct in6_addr sin6;
1239                    struct in_addr sin4;
1240                    u16 port;
1241            case PF_INET6:
1242                    if (type == SOCK_DGRAM) { /* UDP IPv6 */
1243                            if (skb->protocol == htons(ETH_P_IP)) {
1244                                    ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),
1245                                                  ip_hdr(skb)->saddr);
1246                            } else {
1247                                    ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
1248                            }
1249                            port = udp_hdr(skb)->source;
1250                    } else { /* RAW IPv6 */
1251                            ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
1252                            port = htons(sk->sk_protocol);
1253                    }
1254                    error = ccs_check_network_recvmsg_acl(true, type,
1255                                                          (u8 *) &sin6, port);
1256                    break;
1257            case PF_INET:
1258                    if (type == SOCK_DGRAM) { /* UDP IPv4 */
1259                            sin4.s_addr = ip_hdr(skb)->saddr;
1260                            port = udp_hdr(skb)->source;
1261                    } else { /* RAW IPv4 */
1262                            sin4.s_addr = ip_hdr(skb)->saddr;
1263                            port = htons(sk->sk_protocol);
1264                    }
1265                    error = ccs_check_network_recvmsg_acl(false, type,
1266                                                          (u8 *) &sin4, port);
1267                    break;
1268            }
1269            if (!error)
1270                    return 0;
1271    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
1272            lock_sock(sk);
1273    #endif
1274            /*
1275             * Remove from queue if MSG_PEEK is used so that
1276             * the head message from unwanted source in receive queue will not
1277             * prevent the caller from picking up next message from wanted source
1278             * when the caller is using MSG_PEEK flag for picking up.
1279             */
1280            if (flags & MSG_PEEK) {
1281                    unsigned long cpu_flags;
1282                    /***** CRITICAL SECTION START *****/
1283                    spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
1284                    if (skb == skb_peek(&sk->sk_receive_queue)) {
1285                            __skb_unlink(skb, &sk->sk_receive_queue);
1286                            atomic_dec(&skb->users);
1287                    }
1288                    spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
1289                    /***** CRITICAL SECTION END *****/
1290            }
1291            /* Drop reference count. */
1292            skb_free_datagram(sk, skb);
1293    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
1294            release_sock(sk);
1295    #endif
1296            /* Hope less harmful than -EPERM. */
1297            return -EAGAIN;
1298    }

Legend:
Removed from v.1084  
changed lines
  Added in v.2039

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