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

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

Legend:
Removed from v.1379  
changed lines
  Added in v.2570

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