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

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.6.x/ccs-patch/fs/tomoyo_network.c revision 1657 by kumaneko, Tue Oct 7 02:21:32 2008 UTC branches/ccs-patch/fs/tomoyo_network.c revision 2828 by kumaneko, Mon Aug 3 05:36:36 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.5-pre   2008/10/07   * Version: 1.7.0-pre   2009/07/03
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.
12   *   *
13   */   */
14    
 #include <linux/ccs_common.h>  
 #include <linux/tomoyo.h>  
 #include <linux/realpath.h>  
15  #include <linux/net.h>  #include <linux/net.h>
16  #include <linux/inet.h>  #include <linux/inet.h>
17  #include <linux/in.h>  #include <linux/in.h>
18  #include <linux/in6.h>  #include <linux/in6.h>
19    #include <net/ip.h>
20    #include <net/ipv6.h>
21    #include <net/udp.h>
22    #include <linux/ccs_common.h>
23    #include <linux/tomoyo.h>
24    #include <linux/tomoyo_socket.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   * @r:          Pointer to "struct ccs_request_info".   * @r:          Pointer to "struct ccs_request_info".
42   * @operation:  The name of operation.   * @operation:  The name of operation.
# Line 31  Line 46 
46   *   *
47   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
48   */   */
49  static int audit_network_log(struct ccs_request_info *r, 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)
52  {  {
53            if (!is_granted && ccs_verbose_mode(r->domain))
54                    printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",
55                           ccs_get_msg(r->mode == 3), operation, address, port,
56                           ccs_get_last_name(r->domain));
57          return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK          return ccs_write_audit_log(is_granted, r, KEYWORD_ALLOW_NETWORK
58                                     "%s %s %u\n", operation, address, port);                                     "%s %s %u\n", operation, address, port);
59  }  }
60    
61    /* The list for "struct ccs_address_group_entry". */
62    LIST_HEAD(ccs_address_group_list);
63    
64  /**  /**
65   * save_ipv6_address - Keep the given IPv6 address on the RAM.   * ccs_get_address_group - Allocate memory for "struct ccs_address_group_entry".
  *  
  * @addr: Pointer to "struct in6_addr".  
66   *   *
67   * Returns pointer to "struct in6_addr" on success, NULL otherwise.   * @group_name: The name of address group.
68   *   *
69   * The RAM is shared, so NEVER try to modify or kfree() the returned address.   * Returns pointer to "struct ccs_address_group_entry" on success,
70     * NULL otherwise.
71   */   */
72  static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr)  static struct ccs_address_group_entry *ccs_get_address_group(const char *
73  {                                                               group_name)
74          static const u8 block_size = 16;  {
75          struct addr_list {          struct ccs_address_group_entry *entry = NULL;
76                  /* Workaround for gcc 4.3's bug. */          struct ccs_address_group_entry *group;
77                  struct in6_addr addr[16]; /* = block_size */          const struct ccs_path_info *saved_group_name;
78                  struct list1_head list;          int error = -ENOMEM;
79                  u32 in_use_count;          if (!ccs_is_correct_path(group_name, 0, 0, 0) ||
80          };              !group_name[0])
         static LIST1_HEAD(address_list);  
         struct addr_list *ptr;  
         static DEFINE_MUTEX(lock);  
         u8 i = block_size;  
         if (!addr)  
81                  return NULL;                  return NULL;
82          mutex_lock(&lock);          saved_group_name = ccs_get_name(group_name);
83          list1_for_each_entry(ptr, &address_list, list) {          if (!saved_group_name)
84                  for (i = 0; i < ptr->in_use_count; i++) {                  return NULL;
85                          if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))          entry = kzalloc(sizeof(*entry), GFP_KERNEL);
86                                  goto ok;          mutex_lock(&ccs_policy_lock);
87                  }          list_for_each_entry_rcu(group, &ccs_address_group_list, list) {
88                  if (i < block_size)                  if (saved_group_name != group->group_name)
89                          break;                          continue;
90          }                  atomic_inc(&group->users);
91          if (i == block_size) {                  error = 0;
92                  ptr = ccs_alloc_element(sizeof(*ptr));                  break;
                 if (!ptr)  
                         goto ok;  
                 list1_add_tail_mb(&ptr->list, &address_list);  
                 i = 0;  
93          }          }
94          ptr->addr[ptr->in_use_count++] = *addr;          if (error && ccs_memory_ok(entry, sizeof(*entry))) {
95   ok:                  INIT_LIST_HEAD(&entry->address_group_member_list);
96          mutex_unlock(&lock);                  entry->group_name = saved_group_name;
97          return ptr ? &ptr->addr[i] : NULL;                  saved_group_name = NULL;
98                    atomic_set(&entry->users, 1);
99                    list_add_tail_rcu(&entry->list, &ccs_address_group_list);
100                    group = entry;
101                    entry = NULL;
102                    error = 0;
103            }
104            mutex_unlock(&ccs_policy_lock);
105            ccs_put_name(saved_group_name);
106            kfree(entry);
107            return !error ? group : NULL;
108  }  }
109    
 /* The list for "struct address_group_entry". */  
 static LIST1_HEAD(address_group_list);  
   
110  /**  /**
111   * update_address_group_entry - Update "struct address_group_entry" list.   * ccs_update_address_group_entry - Update "struct ccs_address_group_entry" list.
112   *   *
113   * @group_name:  The name of address group.   * @group_name:  The name of address group.
114   * @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 99  static LIST1_HEAD(address_group_list); Line 118  static LIST1_HEAD(address_group_list);
118   *   *
119   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
120   */   */
121  static int update_address_group_entry(const char *group_name,  static int ccs_update_address_group_entry(const char *group_name,
122                                        const bool is_ipv6,                                            const bool is_ipv6,
123                                        const u16 *min_address,                                            const u16 *min_address,
124                                        const u16 *max_address,                                            const u16 *max_address,
125                                        const bool is_delete)                                            const bool is_delete)
126  {  {
127          static DEFINE_MUTEX(lock);          struct ccs_address_group_entry *group;
128          struct address_group_entry *new_group;          struct ccs_address_group_member *entry = NULL;
129          struct address_group_entry *group;          struct ccs_address_group_member *member;
         struct address_group_member *new_member;  
         struct address_group_member *member;  
         const struct path_info *saved_group_name;  
130          const struct in6_addr *saved_min_address = NULL;          const struct in6_addr *saved_min_address = NULL;
131          const struct in6_addr *saved_max_address = NULL;          const struct in6_addr *saved_max_address = NULL;
132          int error = -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
133          bool found = false;          const u32 min_ipv4_address = ntohl(*(u32 *) min_address);
134          if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||          const u32 max_ipv4_address = ntohl(*(u32 *) max_address);
135              !group_name[0])          group = ccs_get_address_group(group_name);
136                  return -EINVAL;          if (!group)
         saved_group_name = ccs_save_name(group_name);  
         if (!saved_group_name)  
                 return -ENOMEM;  
         if (!is_ipv6)  
                 goto not_ipv6;  
         saved_min_address  
                 = save_ipv6_address((struct in6_addr *) min_address);  
         saved_max_address  
                 = save_ipv6_address((struct in6_addr *) max_address);  
         if (!saved_min_address || !saved_max_address)  
137                  return -ENOMEM;                  return -ENOMEM;
138   not_ipv6:          if (is_ipv6) {
139          mutex_lock(&lock);                  saved_min_address
140          list1_for_each_entry(group, &address_group_list, list) {                          = ccs_get_ipv6_address((struct in6_addr *)
141                  if (saved_group_name != group->group_name)                                                 min_address);
142                          continue;                  saved_max_address
143                  list1_for_each_entry(member, &group->address_group_member_list,                          = ccs_get_ipv6_address((struct in6_addr *)
144                                       list) {                                                 max_address);
145                          if (member->is_ipv6 != is_ipv6)                  if (!saved_min_address || !saved_max_address)
                                 continue;  
                         if (is_ipv6) {  
                                 if (member->min.ipv6 != saved_min_address ||  
                                     member->max.ipv6 != saved_max_address)  
                                         continue;  
                         } else {  
                                 if (member->min.ipv4 != *(u32 *) min_address ||  
                                     member->max.ipv4 != *(u32 *) max_address)  
                                         continue;  
                         }  
                         member->is_deleted = is_delete;  
                         error = 0;  
146                          goto out;                          goto out;
147            }
148            if (!is_delete)
149                    entry = kzalloc(sizeof(*entry), GFP_KERNEL);
150            mutex_lock(&ccs_policy_lock);
151            list_for_each_entry_rcu(member, &group->address_group_member_list,
152                                    list) {
153                    if (member->is_ipv6 != is_ipv6)
154                            continue;
155                    if (is_ipv6) {
156                            if (member->min.ipv6 != saved_min_address ||
157                                member->max.ipv6 != saved_max_address)
158                                    continue;
159                    } else {
160                            if (member->min.ipv4 != min_ipv4_address ||
161                                member->max.ipv4 != max_ipv4_address)
162                                    continue;
163                  }                  }
164                  found = true;                  member->is_deleted = is_delete;
165                    error = 0;
166                  break;                  break;
167          }          }
168          if (is_delete) {          if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) {
169                  error = -ENOENT;                  entry->is_ipv6 = is_ipv6;
170                  goto out;                  if (is_ipv6) {
171          }                          entry->min.ipv6 = saved_min_address;
172          if (!found) {                          saved_min_address = NULL;
173                  new_group = ccs_alloc_element(sizeof(*new_group));                          entry->max.ipv6 = saved_max_address;
174                  if (!new_group)                          saved_max_address = NULL;
175                          goto out;                  } else {
176                  INIT_LIST1_HEAD(&new_group->address_group_member_list);                          entry->min.ipv4 = min_ipv4_address;
177                  new_group->group_name = saved_group_name;                          entry->max.ipv4 = max_ipv4_address;
178                  list1_add_tail_mb(&new_group->list, &address_group_list);                  }
179                  group = new_group;                  list_add_tail_rcu(&entry->list,
180          }                                    &group->address_group_member_list);
181          new_member = ccs_alloc_element(sizeof(*new_member));                  entry = NULL;
182          if (!new_member)                  error = 0;
                 goto out;  
         new_member->is_ipv6 = is_ipv6;  
         if (is_ipv6) {  
                 new_member->min.ipv6 = saved_min_address;  
                 new_member->max.ipv6 = saved_max_address;  
         } else {  
                 new_member->min.ipv4 = *(u32 *) min_address;  
                 new_member->max.ipv4 = *(u32 *) max_address;  
183          }          }
184          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);          mutex_unlock(&ccs_policy_lock);
         error = 0;  
185   out:   out:
186          mutex_unlock(&lock);          ccs_put_ipv6_address(saved_min_address);
187          ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);          ccs_put_ipv6_address(saved_max_address);
188            ccs_put_address_group(group);
189          return error;          return error;
190  }  }
191    
192  /**  /**
193   * parse_ip_address - Parse an IP address.   * ccs_parse_ip_address - Parse an IP address.
194   *   *
195   * @address: String to parse.   * @address: String to parse.
196   * @min:     Pointer to store min address.   * @min:     Pointer to store min address.
# Line 195  static int update_address_group_entry(co Line 198  static int update_address_group_entry(co
198   *   *
199   * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.   * Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise.
200   */   */
201  static int parse_ip_address(char *address, u16 *min, u16 *max)  static int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
202  {  {
203          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
204                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
# Line 217  static int parse_ip_address(char *addres Line 220  static int parse_ip_address(char *addres
220                         &min[0], &min[1], &min[2], &min[3],                         &min[0], &min[1], &min[2], &min[3],
221                         &max[0], &max[1], &max[2], &max[3]);                         &max[0], &max[1], &max[2], &max[3]);
222          if (count == 4 || count == 8) {          if (count == 4 || count == 8) {
223                  u8 *p = (void *) min;                  u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
224                  *p++ = min[0];                                 + (((u8) min[2]) << 8) + (u8) min[3]);
225                  *p++ = min[1];                  memmove(min, &ip, sizeof(ip));
226                  *p++ = min[2];                  if (count == 8)
227                  *p = min[3];                          ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16)
228                  if (count == 4)                                     + (((u8) max[2]) << 8) + (u8) max[3]);
229                          memmove(max, min, sizeof(u16) * 4);                  memmove(max, &ip, sizeof(ip));
                 p = (void *) max;  
                 *p++ = max[0];  
                 *p++ = max[1];  
                 *p++ = max[2];  
                 *p = max[3];  
230                  return 1;                  return 1;
231          }          }
232          return 0;          return 0;
233  }  }
234    
235  /**  /**
236   * ccs_write_address_group_policy - Write "struct address_group_entry" list.   * ccs_write_address_group_policy - Write "struct ccs_address_group_entry" list.
237   *   *
238   * @data:      String to parse.   * @data:      String to parse.
239   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
# Line 244  static int parse_ip_address(char *addres Line 242  static int parse_ip_address(char *addres
242   */   */
243  int ccs_write_address_group_policy(char *data, const bool is_delete)  int ccs_write_address_group_policy(char *data, const bool is_delete)
244  {  {
245            char *w[2];
246          bool is_ipv6;          bool is_ipv6;
247          u16 min_address[8];          u16 min_address[8];
248          u16 max_address[8];          u16 max_address[8];
249          char *cp = strchr(data, ' ');          if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0])
250          if (!cp)                  return -EINVAL;
251                  return -EINVAL;          switch (ccs_parse_ip_address(w[1], min_address, max_address)) {
         *cp++ = '\0';  
         switch (parse_ip_address(cp, min_address, max_address)) {  
252          case 2:          case 2:
253                  is_ipv6 = true;                  is_ipv6 = true;
254                  break;                  break;
# Line 261  int ccs_write_address_group_policy(char Line 258  int ccs_write_address_group_policy(char
258          default:          default:
259                  return -EINVAL;                  return -EINVAL;
260          }          }
261          return update_address_group_entry(data, is_ipv6,          return ccs_update_address_group_entry(w[0], is_ipv6, min_address,
262                                            min_address, max_address, is_delete);                                                max_address, is_delete);
 }  
   
 /**  
  * find_or_assign_new_address_group - Create address group.  
  *  
  * @group_name: The name of address group.  
  *  
  * Returns pointer to "struct address_group_entry" on success, NULL otherwise.  
  */  
 static struct address_group_entry *  
 find_or_assign_new_address_group(const char *group_name)  
 {  
         u8 i;  
         struct address_group_entry *group;  
         for (i = 0; i <= 1; i++) {  
                 list1_for_each_entry(group, &address_group_list, list) {  
                         if (!strcmp(group_name, group->group_name->name))  
                                 return group;  
                 }  
                 if (!i) {  
                         const u16 dummy[2] = { 0, 0 };  
                         update_address_group_entry(group_name, false,  
                                                    dummy, dummy, false);  
                         update_address_group_entry(group_name, false,  
                                                    dummy, dummy, true);  
                 }  
         }  
         return NULL;  
263  }  }
264    
265  /**  /**
266   * 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.
267   *   *
268   * @is_ipv6: True if @address is an IPv6 address.   * @is_ipv6: True if @address is an IPv6 address.
269   * @address: An IPv4 or IPv6 address.   * @address: An IPv4 or IPv6 address.
270   * @group:   Pointer to "struct address_group_entry".   * @group:   Pointer to "struct ccs_address_group_entry".
271   *   *
272   * Returns true if @address matches addresses in @group group, false otherwise.   * Returns true if @address matches addresses in @group group, false otherwise.
273     *
274     * Caller holds ccs_read_lock().
275   */   */
276  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,
277                                       const struct address_group_entry *group)                                        const struct ccs_address_group_entry *
278                                          group)
279  {  {
280          struct address_group_member *member;          struct ccs_address_group_member *member;
281          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
282          list1_for_each_entry(member, &group->address_group_member_list, list) {          bool matched = false;
283            ccs_check_read_lock();
284            list_for_each_entry_rcu(member, &group->address_group_member_list,
285                                    list) {
286                  if (member->is_deleted)                  if (member->is_deleted)
287                          continue;                          continue;
288                  if (member->is_ipv6) {                  if (member->is_ipv6) {
289                          if (is_ipv6 &&                          if (is_ipv6 &&
290                              memcmp(member->min.ipv6, address, 16) <= 0 &&                              memcmp(member->min.ipv6, address, 16) <= 0 &&
291                              memcmp(address, member->max.ipv6, 16) <= 0)                              memcmp(address, member->max.ipv6, 16) <= 0) {
292                                  return true;                                  matched = true;
293                                    break;
294                            }
295                  } else {                  } else {
296                          if (!is_ipv6 &&                          if (!is_ipv6 &&
297                              member->min.ipv4 <= ip && ip <= member->max.ipv4)                              member->min.ipv4 <= ip && ip <= member->max.ipv4) {
298                                  return true;                                  matched = true;
299                                    break;
300                            }
301                  }                  }
302          }          }
303          return false;          return matched;
304  }  }
305    
306  /**  /**
307   * ccs_read_address_group_policy - Read "struct address_group_entry" list.   * ccs_read_address_group_policy - Read "struct ccs_address_group_entry" list.
308   *   *
309   * @head: Pointer to "struct ccs_io_buffer".   * @head: Pointer to "struct ccs_io_buffer".
310   *   *
311   * Returns true on success, false otherwise.   * Returns true on success, false otherwise.
312     *
313     * Caller holds ccs_read_lock().
314   */   */
315  bool ccs_read_address_group_policy(struct ccs_io_buffer *head)  bool ccs_read_address_group_policy(struct ccs_io_buffer *head)
316  {  {
317          struct list1_head *gpos;          struct list_head *gpos;
318          struct list1_head *mpos;          struct list_head *mpos;
319          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {          bool done = true;
320                  struct address_group_entry *group;          ccs_check_read_lock();
321                  group = list1_entry(gpos, struct address_group_entry, list);          list_for_each_cookie(gpos, head->read_var1, &ccs_address_group_list) {
322                  list1_for_each_cookie(mpos, head->read_var2,                  struct ccs_address_group_entry *group;
323                                        &group->address_group_member_list) {                  group = list_entry(gpos, struct ccs_address_group_entry, list);
324                    list_for_each_cookie(mpos, head->read_var2,
325                                         &group->address_group_member_list) {
326                          char buf[128];                          char buf[128];
327                          struct address_group_member *member;                          struct ccs_address_group_member *member;
328                          member = list1_entry(mpos, struct address_group_member,                          member = list_entry(mpos,
329                                                 struct ccs_address_group_member,
330                                               list);                                               list);
331                          if (member->is_deleted)                          if (member->is_deleted)
332                                  continue;                                  continue;
# Line 354  bool ccs_read_address_group_policy(struc Line 338  bool ccs_read_address_group_policy(struc
338                                  ccs_print_ipv6(buf, sizeof(buf), min_address);                                  ccs_print_ipv6(buf, sizeof(buf), min_address);
339                                  if (min_address != max_address) {                                  if (min_address != max_address) {
340                                          int len;                                          int len;
341                                          char *cp = strchr(buf, '\0');                                          char *cp = buf + strlen(buf);
342                                          *cp++ = '-';                                          *cp++ = '-';
343                                          len = strlen(buf);                                          len = strlen(buf);
344                                          ccs_print_ipv6(cp, sizeof(buf) - len,                                          ccs_print_ipv6(cp, sizeof(buf) - len,
# Line 374  bool ccs_read_address_group_policy(struc Line 358  bool ccs_read_address_group_policy(struc
358                                                   HIPQUAD(max_address));                                                   HIPQUAD(max_address));
359                                  }                                  }
360                          }                          }
361                          if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP                          done = ccs_io_printf(head, KEYWORD_ADDRESS_GROUP
362                                             "%s %s\n", group->group_name->name,                                               "%s %s\n", group->group_name->name,
363                                             buf))                                               buf);
364                                  goto out;                          if (!done)
365                                    break;
366                  }                  }
367                    if (!done)
368                            break;
369          }          }
370          return true;          return done;
  out:  
         return false;  
371  }  }
372    
373  #if !defined(NIP6)  #if !defined(NIP6)
# Line 449  const char *ccs_net2keyword(const u8 ope Line 434  const char *ccs_net2keyword(const u8 ope
434  }  }
435    
436  /**  /**
437   * update_network_entry - Update "struct ip_network_acl_record" list.   * ccs_update_network_entry - Update "struct ccs_ip_network_acl_record" list.
438   *   *
439   * @operation:   Type of operation.   * @operation:   Type of operation.
440   * @record_type: Type of address.   * @record_type: Type of address.
441   * @group:       Pointer to "struct address_group_entry". May be NULL.   * @group:       Name of group. May be NULL.
442   * @min_address: Start of IPv4 or IPv6 address range.   * @min_address: Start of IPv4 or IPv6 address range.
443   * @max_address: End of IPv4 or IPv6 address range.   * @max_address: End of IPv4 or IPv6 address range.
444   * @min_port:    Start of port number range.   * @min_port:    Start of port number range.
445   * @max_port:    End of port number range.   * @max_port:    End of port number range.
446   * @domain:      Pointer to "struct domain_info".   * @domain:      Pointer to "struct ccs_domain_info".
447   * @condition:   Pointer to "struct condition_list". May be NULL.   * @condition:   Pointer to "struct ccs_condition". May be NULL.
448   * @is_delete:   True if it is a delete request.   * @is_delete:   True if it is a delete request.
449   *   *
450   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
451   */   */
452  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,
453                                  const struct address_group_entry *group,                                      const char *group_name,
454                                  const u32 *min_address, const u32 *max_address,                                      const u32 *min_address,
455                                  const u16 min_port, const u16 max_port,                                      const u32 *max_address,
456                                  struct domain_info *domain,                                      const u16 min_port, const u16 max_port,
457                                  const struct condition_list *condition,                                      struct ccs_domain_info *domain,
458                                  const bool is_delete)                                      struct ccs_condition *condition,
459                                        const bool is_delete)
460  {  {
461          struct acl_info *ptr;          struct ccs_ip_network_acl_record *entry = NULL;
462          struct ip_network_acl_record *acl;          struct ccs_acl_info *ptr;
463          int error = -ENOMEM;          int error = is_delete ? -ENOENT : -ENOMEM;
464          /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
465          const u32 min_ip = ntohl(*min_address);          const u32 min_ip = ntohl(*min_address);
466          const u32 max_ip = ntohl(*max_address);          const u32 max_ip = ntohl(*max_address);
467          const struct in6_addr *saved_min_address = NULL;          const struct in6_addr *saved_min_address = NULL;
468          const struct in6_addr *saved_max_address = NULL;          const struct in6_addr *saved_max_address = NULL;
469            struct ccs_address_group_entry *group = NULL;
470          if (!domain)          if (!domain)
471                  return -EINVAL;                  return -EINVAL;
472          if (record_type != IP_RECORD_TYPE_IPv6)          if (group_name) {
473                  goto not_ipv6;                  group = ccs_get_address_group(group_name);
474          saved_min_address = save_ipv6_address((struct in6_addr *) min_address);                  if (!group)
475          saved_max_address = save_ipv6_address((struct in6_addr *) max_address);                          return -ENOMEM;
476          if (!saved_min_address || !saved_max_address)          } else if (record_type == IP_RECORD_TYPE_IPv6) {
477                  return -ENOMEM;                  saved_min_address = ccs_get_ipv6_address((struct in6_addr *)
478   not_ipv6:                                                           min_address);
479          mutex_lock(&domain_acl_lock);                  saved_max_address = ccs_get_ipv6_address((struct in6_addr *)
480                                                             max_address);
481                    if (!saved_min_address || !saved_max_address)
482                            goto out;
483            }
484          if (is_delete)          if (is_delete)
485                  goto delete;                  goto delete;
486          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          entry = kzalloc(sizeof(*entry), GFP_KERNEL);
487            mutex_lock(&ccs_policy_lock);
488            list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
489                    struct ccs_ip_network_acl_record *acl;
490                  if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)                  if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)
491                          continue;                          continue;
492                  if (ccs_get_condition_part(ptr) != condition)                  if (ptr->cond != condition)
493                          continue;                          continue;
494                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
495                  if (acl->operation_type != operation ||                  if (acl->operation_type != operation ||
496                      acl->record_type != record_type ||                      acl->record_type != record_type ||
497                      acl->min_port != min_port || max_port != acl->max_port)                      acl->min_port != min_port || max_port != acl->max_port)
# Line 515  static int update_network_entry(const u8 Line 509  static int update_network_entry(const u8
509                                  continue;                                  continue;
510                  }                  }
511                  error = ccs_add_domain_acl(NULL, ptr);                  error = ccs_add_domain_acl(NULL, ptr);
512                  goto out;                  break;
513          }          }
514          /* Not found. Append it to the tail. */          if (error && ccs_memory_ok(entry, sizeof(*entry))) {
515          acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition);                  entry->head.type = TYPE_IP_NETWORK_ACL;
516          if (!acl)                  entry->head.cond = condition;
517                  goto out;                  entry->operation_type = operation;
518          acl->operation_type = operation;                  entry->record_type = record_type;
519          acl->record_type = record_type;                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
520          if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                          entry->u.group = group;
521                  acl->u.group = group;                          group = NULL;
522          } else if (record_type == IP_RECORD_TYPE_IPv4) {                  } else if (record_type == IP_RECORD_TYPE_IPv4) {
523                  acl->u.ipv4.min = min_ip;                          entry->u.ipv4.min = min_ip;
524                  acl->u.ipv4.max = max_ip;                          entry->u.ipv4.max = max_ip;
525          } else {                  } else {
526                  acl->u.ipv6.min = saved_min_address;                          entry->u.ipv6.min = saved_min_address;
527                  acl->u.ipv6.max = saved_max_address;                          saved_min_address = NULL;
528          }                          entry->u.ipv6.max = saved_max_address;
529          acl->min_port = min_port;                          saved_max_address = NULL;
530          acl->max_port = max_port;                  }
531          error = ccs_add_domain_acl(domain, &acl->head);                  entry->min_port = min_port;
532                    entry->max_port = max_port;
533                    error = ccs_add_domain_acl(domain, &entry->head);
534                    entry = NULL;
535            }
536            mutex_unlock(&ccs_policy_lock);
537          goto out;          goto out;
538   delete:   delete:
539          error = -ENOENT;          mutex_lock(&ccs_policy_lock);
540          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
541                    struct ccs_ip_network_acl_record *acl;
542                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
543                          continue;                          continue;
544                  if (ccs_get_condition_part(ptr) != condition)                  if (ptr->cond != condition)
545                          continue;                          continue;
546                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
547                  if (acl->operation_type != operation ||                  if (acl->operation_type != operation ||
548                      acl->record_type != record_type ||                      acl->record_type != record_type ||
549                      acl->min_port != min_port || max_port != acl->max_port)                      acl->min_port != min_port || max_port != acl->max_port)
# Line 563  static int update_network_entry(const u8 Line 563  static int update_network_entry(const u8
563                  error = ccs_del_domain_acl(ptr);                  error = ccs_del_domain_acl(ptr);
564                  break;                  break;
565          }          }
566            mutex_unlock(&ccs_policy_lock);
567   out:   out:
568          mutex_unlock(&domain_acl_lock);          ccs_put_ipv6_address(saved_min_address);
569            ccs_put_ipv6_address(saved_max_address);
570            ccs_put_address_group(group);
571            kfree(entry);
572          return error;          return error;
573  }  }
574    
575  /**  /**
576   * check_network_entry - Check permission for network operation.   * ccs_check_network_entry2 - Check permission for network operation.
577   *   *
578   * @is_ipv6:   True if @address is an IPv6 address.   * @is_ipv6:   True if @address is an IPv6 address.
579   * @operation: Type of operation.   * @operation: Type of operation.
# Line 577  static int update_network_entry(const u8 Line 581  static int update_network_entry(const u8
581   * @port:      Port number.   * @port:      Port number.
582   *   *
583   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
584     *
585     * Caller holds ccs_read_lock().
586   */   */
587  static int check_network_entry(const bool is_ipv6, const u8 operation,  static int ccs_check_network_entry2(const bool is_ipv6, const u8 operation,
588                                 const u32 *address, const u16 port)                                      const u32 *address, const u16 port)
589  {  {
590          struct ccs_request_info r;          struct ccs_request_info r;
591          struct acl_info *ptr;          struct ccs_acl_info *ptr;
592          const char *keyword = ccs_net2keyword(operation);          const char *keyword = ccs_net2keyword(operation);
593          bool is_enforce;          bool is_enforce;
594          /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
595          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
596          bool found = false;          bool found = false;
597          char buf[64];          char buf[64];
598            ccs_check_read_lock();
599          if (!ccs_can_sleep())          if (!ccs_can_sleep())
600                  return 0;                  return 0;
601          ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_NETWORK);          ccs_init_request_info(&r, NULL, CCS_MAC_FOR_NETWORK);
602          is_enforce = (r.mode == 3);          is_enforce = (r.mode == 3);
603          if (!r.mode)          if (!r.mode)
604                  return 0;                  return 0;
605  retry:   retry:
606          list1_for_each_entry(ptr, &r.domain->acl_info_list, list) {          list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) {
607                  struct ip_network_acl_record *acl;                  struct ccs_ip_network_acl_record *acl;
608                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
609                          continue;                          continue;
610                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ccs_ip_network_acl_record, head);
611                  if (acl->operation_type != operation || port < acl->min_port ||                  if (acl->operation_type != operation || port < acl->min_port ||
612                      acl->max_port < port || !ccs_check_condition(&r, ptr))                      acl->max_port < port || !ccs_check_condition(&r, ptr))
613                          continue;                          continue;
614                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
615                          if (!address_matches_to_group(is_ipv6, address,                          if (!ccs_address_matches_group(is_ipv6, address,
616                                                        acl->u.group))                                                         acl->u.group))
617                                  continue;                                  continue;
618                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {
619                          if (is_ipv6 ||                          if (is_ipv6 ||
# Line 618  retry: Line 625  retry:
625                              memcmp(address, acl->u.ipv6.max, 16) > 0)                              memcmp(address, acl->u.ipv6.max, 16) > 0)
626                                  continue;                                  continue;
627                  }                  }
628                  ccs_update_condition(ptr);                  r.cond = ptr->cond;
629                  found = true;                  found = true;
630                  break;                  break;
631          }          }
# Line 628  retry: Line 635  retry:
635                                 (const struct in6_addr *) address);                                 (const struct in6_addr *) address);
636          else          else
637                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
638          audit_network_log(&r, keyword, buf, port, found);          ccs_audit_network_log(&r, keyword, buf, port, found);
639          if (found)          if (found)
640                  return 0;                  return 0;
         if (ccs_verbose_mode(r.domain))  
                 printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",  
                        ccs_get_msg(is_enforce), keyword, buf, port,  
                        ccs_get_last_name(r.domain));  
641          if (is_enforce) {          if (is_enforce) {
642                  int error = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK                  int err = ccs_check_supervisor(&r, KEYWORD_ALLOW_NETWORK
643                                                   "%s %s %u\n", keyword, buf,                                                 "%s %s %u\n", keyword, buf,
644                                                   port);                                                 port);
645                  if (error == 1) {                  if (err == 1)
                         r.retry++;  
646                          goto retry;                          goto retry;
647                  }                  return err;
648                  return error;          } else if (ccs_domain_quota_ok(&r)) {
649                    struct ccs_condition *cond = ccs_handler_cond();
650                    ccs_update_network_entry(operation, is_ipv6 ?
651                                             IP_RECORD_TYPE_IPv6 :
652                                             IP_RECORD_TYPE_IPv4,
653                                             NULL, address, address, port, port,
654                                             r.domain, cond, false);
655                    ccs_put_condition(cond);
656          }          }
         if (r.mode == 1 && ccs_check_domain_quota(r.domain))  
                 update_network_entry(operation, is_ipv6 ?  
                                      IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4,  
                                      NULL, address, address, port, port,  
                                      r.domain, NULL, 0);  
657          return 0;          return 0;
658  }  }
659    
660  /**  /**
661   * ccs_write_network_policy - Write "struct ip_network_acl_record" list.   * ccs_check_network_entry - Check permission for network operation.
662     *
663     * @is_ipv6:   True if @address is an IPv6 address.
664     * @operation: Type of operation.
665     * @address:   An IPv4 or IPv6 address.
666     * @port:      Port number.
667     *
668     * Returns 0 on success, negative value otherwise.
669     */
670    static int ccs_check_network_entry(const bool is_ipv6, const u8 operation,
671                                       const u32 *address, const u16 port)
672    {
673            const int idx = ccs_read_lock();
674            const int error = ccs_check_network_entry2(is_ipv6, operation,
675                                                       address, port);
676            ccs_read_unlock(idx);
677            return error;
678    }
679    
680    /**
681     * ccs_write_network_policy - Write "struct ccs_ip_network_acl_record" list.
682   *   *
683   * @data:      String to parse.   * @data:      String to parse.
684   * @domain:    Pointer to "struct domain_info".   * @domain:    Pointer to "struct ccs_domain_info".
685   * @condition: Pointer to "struct condition_list". May be NULL.   * @condition: Pointer to "struct ccs_condition". May be NULL.
686   * @is_delete: True if it is a delete request.   * @is_delete: True if it is a delete request.
687   *   *
688   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
689   */   */
690  int ccs_write_network_policy(char *data, struct domain_info *domain,  int ccs_write_network_policy(char *data, struct ccs_domain_info *domain,
691                               const struct condition_list *condition,                               struct ccs_condition *condition,
692                               const bool is_delete)                               const bool is_delete)
693  {  {
694            char *w[4];
695          u8 sock_type;          u8 sock_type;
696          u8 operation;          u8 operation;
697          u8 record_type;          u8 record_type;
698          u16 min_address[8];          u16 min_address[8];
699          u16 max_address[8];          u16 max_address[8];
700          struct address_group_entry *group = NULL;          const char *group_name = NULL;
701          u16 min_port;          u16 min_port;
702          u16 max_port;          u16 max_port;
703          u8 count;          u8 count;
704          char *cp1 = strchr(data, ' ');          if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0])
705          char *cp2;                  return -EINVAL;
706          if (!cp1)          if (!strcmp(w[0], "TCP"))
                 goto out;  
         cp1++;  
         if (!strncmp(data, "TCP ", 4))  
707                  sock_type = SOCK_STREAM;                  sock_type = SOCK_STREAM;
708          else if (!strncmp(data, "UDP ", 4))          else if (!strcmp(w[0], "UDP"))
709                  sock_type = SOCK_DGRAM;                  sock_type = SOCK_DGRAM;
710          else if (!strncmp(data, "RAW ", 4))          else if (!strcmp(w[0], "RAW"))
711                  sock_type = SOCK_RAW;                  sock_type = SOCK_RAW;
712          else          else
713                  goto out;                  goto out;
714          cp2 = strchr(cp1, ' ');          if (!strcmp(w[1], "bind"))
         if (!cp2)  
                 goto out;  
         cp2++;  
         if (!strncmp(cp1, "bind ", 5))  
715                  switch (sock_type) {                  switch (sock_type) {
716                  case SOCK_STREAM:                  case SOCK_STREAM:
717                          operation = NETWORK_ACL_TCP_BIND;                          operation = NETWORK_ACL_TCP_BIND;
# Line 704  int ccs_write_network_policy(char *data, Line 722  int ccs_write_network_policy(char *data,
722                  default:                  default:
723                          operation = NETWORK_ACL_RAW_BIND;                          operation = NETWORK_ACL_RAW_BIND;
724                  }                  }
725          else if (!strncmp(cp1, "connect ", 8))          else if (!strcmp(w[1], "connect"))
726                  switch (sock_type) {                  switch (sock_type) {
727                  case SOCK_STREAM:                  case SOCK_STREAM:
728                          operation = NETWORK_ACL_TCP_CONNECT;                          operation = NETWORK_ACL_TCP_CONNECT;
# Line 715  int ccs_write_network_policy(char *data, Line 733  int ccs_write_network_policy(char *data,
733                  default:                  default:
734                          operation = NETWORK_ACL_RAW_CONNECT;                          operation = NETWORK_ACL_RAW_CONNECT;
735                  }                  }
736          else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen"))
737                  operation = NETWORK_ACL_TCP_LISTEN;                  operation = NETWORK_ACL_TCP_LISTEN;
738          else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7))          else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept"))
739                  operation = NETWORK_ACL_TCP_ACCEPT;                  operation = NETWORK_ACL_TCP_ACCEPT;
740          else          else
741                  goto out;                  goto out;
742          cp1 = strchr(cp2, ' ');          switch (ccs_parse_ip_address(w[2], min_address, max_address)) {
         if (!cp1)  
                 goto out;  
         *cp1++ = '\0';  
         switch (parse_ip_address(cp2, min_address, max_address)) {  
743          case 2:          case 2:
744                  record_type = IP_RECORD_TYPE_IPv6;                  record_type = IP_RECORD_TYPE_IPv6;
745                  break;                  break;
# Line 733  int ccs_write_network_policy(char *data, Line 747  int ccs_write_network_policy(char *data,
747                  record_type = IP_RECORD_TYPE_IPv4;                  record_type = IP_RECORD_TYPE_IPv4;
748                  break;                  break;
749          default:          default:
750                  if (*cp2 != '@')                  if (w[2][0] != '@')
751                          goto out;                          goto out;
752                  group = find_or_assign_new_address_group(cp2 + 1);                  group_name = w[2] + 1;
                 if (!group)  
                         return -ENOMEM;  
753                  record_type = IP_RECORD_TYPE_ADDRESS_GROUP;                  record_type = IP_RECORD_TYPE_ADDRESS_GROUP;
754                  break;                  break;
755          }          }
756          if (strchr(cp1, ' '))          count = sscanf(w[3], "%hu-%hu", &min_port, &max_port);
                 goto out;  
         count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);  
757          if (count != 1 && count != 2)          if (count != 1 && count != 2)
758                  goto out;                  goto out;
759          if (count == 1)          if (count == 1)
760                  max_port = min_port;                  max_port = min_port;
761          return update_network_entry(operation, record_type, group,          return ccs_update_network_entry(operation, record_type, group_name,
762                                      (u32 *) min_address, (u32 *) max_address,                                          (u32 *) min_address,
763                                      min_port, max_port, domain, condition,                                          (u32 *) max_address,
764                                      is_delete);                                          min_port, max_port, domain, condition,
765                                            is_delete);
766   out:   out:
767          return -EINVAL;          return -EINVAL;
768  }  }
# Line 765  int ccs_write_network_policy(char *data, Line 776  int ccs_write_network_policy(char *data,
776   *   *
777   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
778   */   */
779  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,
780                                   const u16 port)                                                 const u8 *address,
781                                                   const u16 port)
782  {  {
783          return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,          return ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,
784                                     (const u32 *) address, ntohs(port));                                         (const u32 *) address, ntohs(port));
785  }  }
786    
787  /**  /**
# Line 782  int ccs_check_network_listen_acl(const _ Line 794  int ccs_check_network_listen_acl(const _
794   *   *
795   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
796   */   */
797  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,
798                                    const u8 *address, const u16 port)                                                  const int sock_type,
799                                                    const u8 *address,
800                                                    const u16 port)
801  {  {
802          u8 operation;          u8 operation;
803          switch (sock_type) {          switch (sock_type) {
# Line 796  int ccs_check_network_connect_acl(const Line 810  int ccs_check_network_connect_acl(const
810          default:          default:
811                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = NETWORK_ACL_RAW_CONNECT;
812          }          }
813          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
814                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
815  }  }
816    
817  /**  /**
# Line 810  int ccs_check_network_connect_acl(const Line 824  int ccs_check_network_connect_acl(const
824   *   *
825   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
826   */   */
827  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,
828                                 const u8 *address, const u16 port)                                        const u8 *address, const u16 port)
829  {  {
830          u8 operation;          u8 operation;
831          switch (sock_type) {          switch (sock_type) {
# Line 824  int ccs_check_network_bind_acl(const _Bo Line 838  int ccs_check_network_bind_acl(const _Bo
838          default:          default:
839                  operation = NETWORK_ACL_RAW_BIND;                  operation = NETWORK_ACL_RAW_BIND;
840          }          }
841          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
842                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
843  }  }
844    
845  /**  /**
# Line 837  int ccs_check_network_bind_acl(const _Bo Line 851  int ccs_check_network_bind_acl(const _Bo
851   *   *
852   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
853   */   */
854  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,
855                                   const u16 port)                                                 const u8 *address,
856                                                   const u16 port)
857  {  {
858          int retval;          int retval;
859          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
860          retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,          retval = ccs_check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,
861                                       (const u32 *) address, ntohs(port));                                           (const u32 *) address, ntohs(port));
862          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
863          return retval;          return retval;
864  }  }
865    
# Line 858  int ccs_check_network_accept_acl(const _ Line 873  int ccs_check_network_accept_acl(const _
873   *   *
874   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
875   */   */
876  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,
877                                    const u8 *address, const u16 port)                                                  const int sock_type,
878                                                    const u8 *address,
879                                                    const u16 port)
880  {  {
881          u8 operation;          u8 operation;
882          if (sock_type == SOCK_DGRAM)          if (sock_type == SOCK_DGRAM)
883                  operation = NETWORK_ACL_UDP_CONNECT;                  operation = NETWORK_ACL_UDP_CONNECT;
884          else          else
885                  operation = NETWORK_ACL_RAW_CONNECT;                  operation = NETWORK_ACL_RAW_CONNECT;
886          return check_network_entry(is_ipv6, operation, (const u32 *) address,          return ccs_check_network_entry(is_ipv6, operation,
887                                     ntohs(port));                                         (const u32 *) address, ntohs(port));
888  }  }
889    
890  /**  /**
# Line 880  int ccs_check_network_sendmsg_acl(const Line 897  int ccs_check_network_sendmsg_acl(const
897   *   *
898   * Returns 0 on success, negative value otherwise.   * Returns 0 on success, negative value otherwise.
899   */   */
900  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,
901                                    const u8 *address, const u16 port)                                                  const int sock_type,
902                                                    const u8 *address,
903                                                    const u16 port)
904  {  {
905          int retval;          int retval;
906          const u8 operation          const u8 operation
907                  = (sock_type == SOCK_DGRAM) ?                  = (sock_type == SOCK_DGRAM) ?
908                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                  NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;
909          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
910          retval = check_network_entry(is_ipv6, operation, (const u32 *) address,          retval = ccs_check_network_entry(is_ipv6, operation,
911                                       ntohs(port));                                           (const u32 *) address, ntohs(port));
912          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
913          return retval;          return retval;
914  }  }
915    
916    #define MAX_SOCK_ADDR 128 /* net/socket.c */
917    
918    /* Check permission for creating a socket. */
919    int ccs_socket_create_permission(int family, int type, int protocol)
920    {
921            int error = 0;
922            /* Nothing to do if I am a kernel service. */
923            if (segment_eq(get_fs(), KERNEL_DS))
924                    return 0;
925            if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
926                    return -EPERM;
927            if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
928                    return -EPERM;
929            if (family != PF_INET && family != PF_INET6)
930                    return 0;
931            switch (type) {
932            case SOCK_STREAM:
933                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE))
934                            error = -EPERM;
935                    break;
936            case SOCK_DGRAM:
937                    if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET))
938                            error = -EPERM;
939                    break;
940            case SOCK_RAW:
941                    if (!ccs_capable(CCS_USE_INET_RAW_SOCKET))
942                            error = -EPERM;
943                    break;
944            }
945            return error;
946    }
947    
948    /* Check permission for listening a TCP socket. */
949    int ccs_socket_listen_permission(struct socket *sock)
950    {
951            int error = 0;
952            char addr[MAX_SOCK_ADDR];
953            int addr_len;
954            /* Nothing to do if I am a kernel service. */
955            if (segment_eq(get_fs(), KERNEL_DS))
956                    return 0;
957            if (sock->type != SOCK_STREAM)
958                    return 0;
959            switch (sock->sk->sk_family) {
960            case PF_INET:
961            case PF_INET6:
962                    break;
963            default:
964                    return 0;
965            }
966            if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN))
967                    return -EPERM;
968            if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0))
969                    return -EPERM;
970            switch (((struct sockaddr *) addr)->sa_family) {
971                    struct sockaddr_in6 *addr6;
972                    struct sockaddr_in *addr4;
973            case AF_INET6:
974                    addr6 = (struct sockaddr_in6 *) addr;
975                    error = ccs_check_network_listen_acl(true,
976                                                         addr6->sin6_addr.s6_addr,
977                                                         addr6->sin6_port);
978                    break;
979            case AF_INET:
980                    addr4 = (struct sockaddr_in *) addr;
981                    error = ccs_check_network_listen_acl(false,
982                                                         (u8 *) &addr4->sin_addr,
983                                                         addr4->sin_port);
984                    break;
985            }
986            return error;
987    }
988    
989    /* Check permission for setting the remote IP address/port pair of a socket. */
990    int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr,
991                                      int addr_len)
992    {
993            int error = 0;
994            const unsigned int type = sock->type;
995            /* Nothing to do if I am a kernel service. */
996            if (segment_eq(get_fs(), KERNEL_DS))
997                    return 0;
998            switch (type) {
999            case SOCK_STREAM:
1000            case SOCK_DGRAM:
1001            case SOCK_RAW:
1002                    break;
1003            default:
1004                    return 0;
1005            }
1006            switch (addr->sa_family) {
1007                    struct sockaddr_in6 *addr6;
1008                    struct sockaddr_in *addr4;
1009                    u16 port;
1010            case AF_INET6:
1011                    if (addr_len < SIN6_LEN_RFC2133)
1012                            break;
1013                    addr6 = (struct sockaddr_in6 *) addr;
1014                    if (type != SOCK_RAW)
1015                            port = addr6->sin6_port;
1016                    else
1017                            port = htons(sock->sk->sk_protocol);
1018                    error = ccs_check_network_connect_acl(true, type,
1019                                                          addr6->sin6_addr.s6_addr,
1020                                                          port);
1021                    break;
1022            case AF_INET:
1023                    if (addr_len < sizeof(struct sockaddr_in))
1024                            break;
1025                    addr4 = (struct sockaddr_in *) addr;
1026                    if (type != SOCK_RAW)
1027                            port = addr4->sin_port;
1028                    else
1029                            port = htons(sock->sk->sk_protocol);
1030                    error = ccs_check_network_connect_acl(false, type,
1031                                                          (u8 *) &addr4->sin_addr,
1032                                                          port);
1033                    break;
1034            }
1035            if (type != SOCK_STREAM)
1036                    return error;
1037            switch (sock->sk->sk_family) {
1038            case PF_INET:
1039            case PF_INET6:
1040                    if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT))
1041                            error = -EPERM;
1042                    break;
1043            }
1044            return error;
1045    }
1046    
1047    /* Check permission for setting the local IP address/port pair of a socket. */
1048    int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
1049                                   int addr_len)
1050    {
1051            int error = 0;
1052            const unsigned int type = sock->type;
1053            /* Nothing to do if I am a kernel service. */
1054            if (segment_eq(get_fs(), KERNEL_DS))
1055                    return 0;
1056            switch (type) {
1057            case SOCK_STREAM:
1058            case SOCK_DGRAM:
1059            case SOCK_RAW:
1060                    break;
1061            default:
1062                    return 0;
1063            }
1064            switch (addr->sa_family) {
1065                    struct sockaddr_in6 *addr6;
1066                    struct sockaddr_in *addr4;
1067                    u16 port;
1068            case AF_INET6:
1069                    if (addr_len < SIN6_LEN_RFC2133)
1070                            break;
1071                    addr6 = (struct sockaddr_in6 *) addr;
1072                    if (type != SOCK_RAW)
1073                            port = addr6->sin6_port;
1074                    else
1075                            port = htons(sock->sk->sk_protocol);
1076                    error = ccs_check_network_bind_acl(true, type,
1077                                                       addr6->sin6_addr.s6_addr,
1078                                                       port);
1079                    break;
1080            case AF_INET:
1081                    if (addr_len < sizeof(struct sockaddr_in))
1082                            break;
1083                    addr4 = (struct sockaddr_in *) addr;
1084                    if (type != SOCK_RAW)
1085                            port = addr4->sin_port;
1086                    else
1087                            port = htons(sock->sk->sk_protocol);
1088                    error = ccs_check_network_bind_acl(false, type,
1089                                                       (u8 *) &addr4->sin_addr,
1090                                                       port);
1091                    break;
1092            }
1093            return error;
1094    }
1095    
1096    /*
1097     * Check permission for accepting a TCP socket.
1098     *
1099     * Currently, the LSM hook for this purpose is not provided.
1100     */
1101    int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr)
1102    {
1103            int error = 0;
1104            int addr_len;
1105            /* Nothing to do if I am a kernel service. */
1106            if (segment_eq(get_fs(), KERNEL_DS))
1107                    return 0;
1108            switch (sock->sk->sk_family) {
1109            case PF_INET:
1110            case PF_INET6:
1111                    break;
1112            default:
1113                    return 0;
1114            }
1115            error = sock->ops->getname(sock, addr, &addr_len, 2);
1116            if (error)
1117                    return error;
1118            switch (addr->sa_family) {
1119                    struct sockaddr_in6 *addr6;
1120                    struct sockaddr_in *addr4;
1121            case AF_INET6:
1122                    addr6 = (struct sockaddr_in6 *) addr;
1123                    error = ccs_check_network_accept_acl(true,
1124                                                         addr6->sin6_addr.s6_addr,
1125                                                         addr6->sin6_port);
1126                    break;
1127            case AF_INET:
1128                    addr4 = (struct sockaddr_in *) addr;
1129                    error = ccs_check_network_accept_acl(false,
1130                                                         (u8 *) &addr4->sin_addr,
1131                                                         addr4->sin_port);
1132                    break;
1133            }
1134            return error;
1135    }
1136    
1137    /* Check permission for sending a datagram via a UDP or RAW socket. */
1138    int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr,
1139                                      int addr_len)
1140    {
1141            int error = 0;
1142            const int type = sock->type;
1143            /* Nothing to do if I am a kernel service. */
1144            if (segment_eq(get_fs(), KERNEL_DS))
1145                    return 0;
1146            if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW))
1147                    return 0;
1148            switch (addr->sa_family) {
1149                    struct sockaddr_in6 *addr6;
1150                    struct sockaddr_in *addr4;
1151                    u16 port;
1152            case AF_INET6:
1153                    if (addr_len < SIN6_LEN_RFC2133)
1154                            break;
1155                    addr6 = (struct sockaddr_in6 *) addr;
1156                    if (type == SOCK_DGRAM)
1157                            port = addr6->sin6_port;
1158                    else
1159                            port = htons(sock->sk->sk_protocol);
1160                    error = ccs_check_network_sendmsg_acl(true, type,
1161                                                          addr6->sin6_addr.s6_addr,
1162                                                          port);
1163                    break;
1164            case AF_INET:
1165                    if (addr_len < sizeof(struct sockaddr_in))
1166                            break;
1167                    addr4 = (struct sockaddr_in *) addr;
1168                    if (type == SOCK_DGRAM)
1169                            port = addr4->sin_port;
1170                    else
1171                            port = htons(sock->sk->sk_protocol);
1172                    error = ccs_check_network_sendmsg_acl(false, type,
1173                                                          (u8 *) &addr4->sin_addr,
1174                                                          port);
1175                    break;
1176            }
1177            return error;
1178    }
1179    
1180    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
1181    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
1182    
1183    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
1184    {
1185            return skb->nh.iph;
1186    }
1187    
1188    static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
1189    {
1190            return skb->h.uh;
1191    }
1192    
1193    static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
1194    {
1195            return skb->nh.ipv6h;
1196    }
1197    
1198    #endif
1199    #endif
1200    
1201    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
1202    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
1203                                  unsigned int flags)
1204    {
1205            /* Clear queue. */
1206            if (flags & MSG_PEEK) {
1207                    int clear = 0;
1208                    spin_lock_irq(&sk->sk_receive_queue.lock);
1209                    if (skb == skb_peek(&sk->sk_receive_queue)) {
1210                            __skb_unlink(skb, &sk->sk_receive_queue);
1211                            clear = 1;
1212                    }
1213                    spin_unlock_irq(&sk->sk_receive_queue.lock);
1214                    if (clear)
1215                            kfree_skb(skb);
1216            }
1217            skb_free_datagram(sk, skb);
1218    }
1219    #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
1220    static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
1221                                  unsigned int flags)
1222    {
1223            /* Clear queue. */
1224            if (flags & MSG_PEEK) {
1225                    int clear = 0;
1226                    spin_lock_bh(&sk->sk_receive_queue.lock);
1227                    if (skb == skb_peek(&sk->sk_receive_queue)) {
1228                            __skb_unlink(skb, &sk->sk_receive_queue);
1229                            clear = 1;
1230                    }
1231                    spin_unlock_bh(&sk->sk_receive_queue.lock);
1232                    if (clear)
1233                            kfree_skb(skb);
1234            }
1235            skb_free_datagram(sk, skb);
1236    }
1237    #endif
1238    
1239    /*
1240     * Check permission for receiving a datagram via a UDP or RAW socket.
1241     *
1242     * Currently, the LSM hook for this purpose is not provided.
1243     */
1244    int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb,
1245                                      const unsigned int flags)
1246    {
1247            int error = 0;
1248            const unsigned int type = sk->sk_type;
1249            if (type != SOCK_DGRAM && type != SOCK_RAW)
1250                    return 0;
1251            /* Nothing to do if I am a kernel service. */
1252            if (segment_eq(get_fs(), KERNEL_DS))
1253                    return 0;
1254    
1255            switch (sk->sk_family) {
1256                    struct in6_addr sin6;
1257                    struct in_addr sin4;
1258                    u16 port;
1259            case PF_INET6:
1260                    if (type == SOCK_DGRAM) { /* UDP IPv6 */
1261                            if (skb->protocol == htons(ETH_P_IP)) {
1262                                    ipv6_addr_set(&sin6, 0, 0, htonl(0xffff),
1263                                                  ip_hdr(skb)->saddr);
1264                            } else {
1265                                    ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
1266                            }
1267                            port = udp_hdr(skb)->source;
1268                    } else { /* RAW IPv6 */
1269                            ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr);
1270                            port = htons(sk->sk_protocol);
1271                    }
1272                    error = ccs_check_network_recvmsg_acl(true, type,
1273                                                          (u8 *) &sin6, port);
1274                    break;
1275            case PF_INET:
1276                    if (type == SOCK_DGRAM) { /* UDP IPv4 */
1277                            sin4.s_addr = ip_hdr(skb)->saddr;
1278                            port = udp_hdr(skb)->source;
1279                    } else { /* RAW IPv4 */
1280                            sin4.s_addr = ip_hdr(skb)->saddr;
1281                            port = htons(sk->sk_protocol);
1282                    }
1283                    error = ccs_check_network_recvmsg_acl(false, type,
1284                                                          (u8 *) &sin4, port);
1285                    break;
1286            }
1287            if (!error)
1288                    return 0;
1289            /*
1290             * Remove from queue if MSG_PEEK is used so that
1291             * the head message from unwanted source in receive queue will not
1292             * prevent the caller from picking up next message from wanted source
1293             * when the caller is using MSG_PEEK flag for picking up.
1294             */
1295    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
1296            if (type == SOCK_DGRAM)
1297                    lock_sock(sk);
1298    #endif
1299            skb_kill_datagram(sk, skb, flags);
1300    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
1301            if (type == SOCK_DGRAM)
1302                    release_sock(sk);
1303    #endif
1304            /* Hope less harmful than -EPERM. */
1305            return -ENOMEM;
1306    }
1307    EXPORT_SYMBOL(ccs_socket_recvmsg_permission);

Legend:
Removed from v.1657  
changed lines
  Added in v.2828

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