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

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.5.x/ccs-patch/fs/tomoyo_network.c revision 815 by kumaneko, Tue Dec 18 07:13:08 2007 UTC trunk/1.6.x/ccs-patch/fs/tomoyo_network.c revision 1064 by kumaneko, Fri Mar 28 05:06:28 2008 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-2007  NTT DATA CORPORATION   * Copyright (C) 2005-2008  NTT DATA CORPORATION
7   *   *
8   * Version: 1.5.3-pre   2007/12/18   * Version: 1.6.0-rc   2008/03/28
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   */   */
 /***** TOMOYO Linux start. *****/  
14    
15  #include <linux/ccs_common.h>  #include <linux/ccs_common.h>
16  #include <linux/tomoyo.h>  #include <linux/tomoyo.h>
17  #include <linux/realpath.h>  #include <linux/realpath.h>
18  #include <net/ip.h>  #include <net/ip.h>
19    
20  /*************************  VARIABLES  *************************/  /**
21     * audit_network_log - Audit network log.
22  extern struct mutex domain_acl_lock;   *
23     * @is_ipv6:    True if @address is an IPv6 address.
24  /*************************  AUDIT FUNCTIONS  *************************/   * @operation:  The name of operation.
25     * @address:    An IPv4 or IPv6 address.
26  static int AuditNetworkLog(const bool is_ipv6, const char *operation, const u32 *address, const u16 port, const bool is_granted, const u8 profile, const unsigned int mode)   * @port:       Port number.
27     * @is_granted: True if this is a granted log.
28     * @profile:    Profile number used.
29     * @mode:       Access control mode used.
30     *
31     * Returns 0 on success, negative value otherwise.
32     */
33    static int audit_network_log(const bool is_ipv6, const char *operation,
34                                 const char *address, const u16 port,
35                                 const bool is_granted,
36                                 const u8 profile, const u8 mode)
37  {  {
38          char *buf;          char *buf;
39          int len = 256;          int len = 256;
40          if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM;          int len2;
41          if ((buf = InitAuditLog(&len, profile, mode)) == NULL) return -ENOMEM;          if (ccs_can_save_audit_log(is_granted) < 0)
42          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);                  return -ENOMEM;
43          if (is_ipv6) {          buf = ccs_init_audit_log(&len, profile, mode, NULL);
44                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address);          if (!buf)
45          } else {                  return -ENOMEM;
46                  u32 ip = *address;          len2 = strlen(buf);
47                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));          snprintf(buf + len2, len - len2 - 1, KEYWORD_ALLOW_NETWORK "%s %s %u\n",
48          }                   operation, address, port);
49          snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port);          return ccs_write_audit_log(buf, is_granted);
         return WriteAuditLog(buf, is_granted);  
50  }  }
51    
52  /*************************  UTILITY FUNCTIONS  *************************/  /**
53     * save_ipv6_address - Keep the given IPv6 address on the RAM.
54  /* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */   *
55  static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr)   * @addr: Pointer to "struct in6_addr".
56     *
57     * Returns pointer to "struct in6_addr" on success, NULL otherwise.
58     *
59     * The RAM is shared, so NEVER try to modify or kfree() the returned address.
60     */
61    static const struct in6_addr *save_ipv6_address(const struct in6_addr *addr)
62  {  {
63          static const int block_size = 16;          static const u8 block_size = 16;
64          struct addr_list {          struct addr_list {
65                  struct in6_addr addr[block_size];                  struct in6_addr addr[block_size];
66                  struct list1_head list;                  struct list1_head list;
# Line 55  static const struct in6_addr *SaveIPv6Ad Line 69  static const struct in6_addr *SaveIPv6Ad
69          static LIST1_HEAD(address_list);          static LIST1_HEAD(address_list);
70          struct addr_list *ptr;          struct addr_list *ptr;
71          static DEFINE_MUTEX(lock);          static DEFINE_MUTEX(lock);
72          int i = block_size;          u8 i = block_size;
73          if (!addr) return NULL;          if (!addr)
74                    return NULL;
75          mutex_lock(&lock);          mutex_lock(&lock);
76          list1_for_each_entry(ptr, &address_list, list) {          list1_for_each_entry(ptr, &address_list, list) {
77                  for (i = 0; i < ptr->in_use_count; i++) {                  for (i = 0; i < ptr->in_use_count; i++) {
78                          if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0) goto ok;                          if (!memcmp(&ptr->addr[i], addr, sizeof(*addr)))
79                                    goto ok;
80                  }                  }
81                  if (i < block_size) break;                  if (i < block_size)
82                            break;
83          }          }
84          if (i == block_size) {          if (i == block_size) {
85                  ptr = alloc_element(sizeof(*ptr));                  ptr = ccs_alloc_element(sizeof(*ptr));
86                  if (!ptr) goto ok;                  if (!ptr)
87                            goto ok;
88                  list1_add_tail_mb(&ptr->list, &address_list);                  list1_add_tail_mb(&ptr->list, &address_list);
89                  i = 0;                  i = 0;
90          }          }
91          ptr->addr[ptr->in_use_count++] = *addr;          ptr->addr[ptr->in_use_count++] = *addr;
92  ok:   ok:
93          mutex_unlock(&lock);          mutex_unlock(&lock);
94          return ptr ? &ptr->addr[i] : NULL;          return ptr ? &ptr->addr[i] : NULL;
95  }  }
96    
97  /*************************  ADDRESS GROUP HANDLER  *************************/  /* The list for "struct address_group_entry". */
   
98  static LIST1_HEAD(address_group_list);  static LIST1_HEAD(address_group_list);
99    
100  static int AddAddressGroupEntry(const char *group_name, const bool is_ipv6, const u16 *min_address, const u16 *max_address, const bool is_delete)  /**
101     * update_address_group_entry - Update "struct address_group_entry" list.
102     *
103     * @group_name:  The name of address group.
104     * @is_ipv6:     True if @min_address and @max_address are IPv6 addresses.
105     * @min_address: Start of IPv4 or IPv6 address range.
106     * @max_address: End of IPv4 or IPv6 address range.
107     * @is_delete:   True if it is a delete request.
108     *
109     * Returns 0 on success, negative value otherwise.
110     */
111    static int update_address_group_entry(const char *group_name,
112                                          const bool is_ipv6,
113                                          const u16 *min_address,
114                                          const u16 *max_address,
115                                          const bool is_delete)
116  {  {
117          static DEFINE_MUTEX(lock);          static DEFINE_MUTEX(lock);
118          struct address_group_entry *new_group, *group;          struct address_group_entry *new_group;
119          struct address_group_member *new_member, *member;          struct address_group_entry *group;
120            struct address_group_member *new_member;
121            struct address_group_member *member;
122          const struct path_info *saved_group_name;          const struct path_info *saved_group_name;
123          const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;          const struct in6_addr *saved_min_address = NULL;
124            const struct in6_addr *saved_max_address = NULL;
125          int error = -ENOMEM;          int error = -ENOMEM;
126          bool found = 0;          bool found = false;
127          if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL;          if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||
128          if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM;              !group_name[0])
129          if (is_ipv6) {                  return -EINVAL;
130                  if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL          saved_group_name = ccs_save_name(group_name);
131                      || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;          if (!saved_group_name)
132          }                  return -ENOMEM;
133            if (!is_ipv6)
134                    goto not_ipv6;
135            saved_min_address
136                    = save_ipv6_address((struct in6_addr *) min_address);
137            saved_max_address
138                    = save_ipv6_address((struct in6_addr *) max_address);
139            if (!saved_min_address || !saved_max_address)
140                    return -ENOMEM;
141     not_ipv6:
142          mutex_lock(&lock);          mutex_lock(&lock);
143          list1_for_each_entry(group, &address_group_list, list) {          list1_for_each_entry(group, &address_group_list, list) {
144                  if (saved_group_name != group->group_name) continue;                  if (saved_group_name != group->group_name)
145                  list1_for_each_entry(member, &group->address_group_member_list, list) {                          continue;
146                          if (member->is_ipv6 != is_ipv6) continue;                  list1_for_each_entry(member, &group->address_group_member_list,
147                                         list) {
148                            if (member->is_ipv6 != is_ipv6)
149                                    continue;
150                          if (is_ipv6) {                          if (is_ipv6) {
151                                  if (member->min.ipv6 != saved_min_address || member->max.ipv6 != saved_max_address) continue;                                  if (member->min.ipv6 != saved_min_address ||
152                                        member->max.ipv6 != saved_max_address)
153                                            continue;
154                          } else {                          } else {
155                                  if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue;                                  if (member->min.ipv4 != *(u32 *) min_address ||
156                                        member->max.ipv4 != *(u32 *) max_address)
157                                            continue;
158                          }                          }
159                          member->is_deleted = is_delete;                          member->is_deleted = is_delete;
160                          error = 0;                          error = 0;
161                          goto out;                          goto out;
162                  }                  }
163                  found = 1;                  found = true;
164                  break;                  break;
165          }          }
166          if (is_delete) {          if (is_delete) {
# Line 117  static int AddAddressGroupEntry(const ch Line 168  static int AddAddressGroupEntry(const ch
168                  goto out;                  goto out;
169          }          }
170          if (!found) {          if (!found) {
171                  if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out;                  new_group = ccs_alloc_element(sizeof(*new_group));
172                    if (!new_group)
173                            goto out;
174                  INIT_LIST1_HEAD(&new_group->address_group_member_list);                  INIT_LIST1_HEAD(&new_group->address_group_member_list);
175                  new_group->group_name = saved_group_name;                  new_group->group_name = saved_group_name;
176                  list1_add_tail_mb(&new_group->list, &address_group_list);                  list1_add_tail_mb(&new_group->list, &address_group_list);
177                  group = new_group;                  group = new_group;
178          }          }
179          if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out;          new_member = ccs_alloc_element(sizeof(*new_member));
180            if (!new_member)
181                    goto out;
182          new_member->is_ipv6 = is_ipv6;          new_member->is_ipv6 = is_ipv6;
183          if (is_ipv6) {          if (is_ipv6) {
184                  new_member->min.ipv6 = saved_min_address;                  new_member->min.ipv6 = saved_min_address;
185                  new_member->max.ipv6 = saved_max_address;                  new_member->max.ipv6 = saved_max_address;
186          } else {          } else {
187                  new_member->min.ipv4 = * (u32 *) min_address;                  new_member->min.ipv4 = *(u32 *) min_address;
188                  new_member->max.ipv4 = * (u32 *) max_address;                  new_member->max.ipv4 = *(u32 *) max_address;
189          }          }
190          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);
191          error = 0;          error = 0;
192   out:   out:
193          mutex_unlock(&lock);          mutex_unlock(&lock);
194            ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY);
195          return error;          return error;
196  }  }
197    
198  int AddAddressGroupPolicy(char *data, const bool is_delete)  /**
199     * ccs_write_address_group_policy - Write "struct address_group_entry" list.
200     *
201     * @data:      String to parse.
202     * @is_delete: True if it is a delete request.
203     *
204     * Returns 0 on success, negative value otherwise.
205     */
206    int ccs_write_address_group_policy(char *data, const bool is_delete)
207  {  {
208          int count, is_ipv6;          u8 count;
209          u16 min_address[8], max_address[8];          bool is_ipv6;
210            u16 min_address[8];
211            u16 max_address[8];
212          char *cp = strchr(data, ' ');          char *cp = strchr(data, ' ');
213          if (!cp) return -EINVAL;          if (!cp)
214                    return -EINVAL;
215          *cp++ = '\0';          *cp++ = '\0';
216          if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",          count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
217                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],                         "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
218                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],                         &min_address[0], &min_address[1],
219                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                         &min_address[2], &min_address[3],
220                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                         &min_address[4], &min_address[5],
221                  int i;                         &min_address[6], &min_address[7],
222                           &max_address[0], &max_address[1],
223                           &max_address[2], &max_address[3],
224                           &max_address[4], &max_address[5],
225                           &max_address[6], &max_address[7]);
226            if (count == 8 || count == 16) {
227                    u8 i;
228                  for (i = 0; i < 8; i++) {                  for (i = 0; i < 8; i++) {
229                          min_address[i] = htons(min_address[i]);                          min_address[i] = htons(min_address[i]);
230                          max_address[i] = htons(max_address[i]);                          max_address[i] = htons(max_address[i]);
231                  }                  }
232                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                  if (count == 8)
233                  is_ipv6 = 1;                          memmove(max_address, min_address, sizeof(min_address));
234          } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",                  is_ipv6 = true;
235                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],                  goto ok;
236                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {          }
237                  u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);          count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
238                  * (u32 *) min_address = ip;                         &min_address[0], &min_address[1],
239                  if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);                         &min_address[2], &min_address[3],
240                  * (u32 *) max_address = ip;                         &max_address[0], &max_address[1],
241                  is_ipv6 = 0;                         &max_address[2], &max_address[3]);
242          } else {          if (count == 4 || count == 8) {
243                  return -EINVAL;                  u32 ip = ((((u8) min_address[0]) << 24)
244                              + (((u8) min_address[1]) << 16)
245                              + (((u8) min_address[2]) << 8)
246                              + (u8) min_address[3]);
247                    *(u32 *) min_address = ip;
248                    if (count == 8)
249                            ip = ((((u8) max_address[0]) << 24)
250                                  + (((u8) max_address[1]) << 16)
251                                  + (((u8) max_address[2]) << 8)
252                                  + (u8) max_address[3]);
253                    *(u32 *) max_address = ip;
254                    is_ipv6 = false;
255                    goto ok;
256          }          }
257          return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete);          return -EINVAL;
258     ok:
259            return update_address_group_entry(data, is_ipv6,
260                                              min_address, max_address, is_delete);
261  }  }
262    
263  static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name)  /**
264     * find_or_assign_new_address_group - Create address group.
265     *
266     * @group_name: The name of address group.
267     *
268     * Returns pointer to "struct address_group_entry" on success, NULL otherwise.
269     */
270    static struct address_group_entry *
271    find_or_assign_new_address_group(const char *group_name)
272  {  {
273          int i;          u8 i;
274          struct address_group_entry *group;          struct address_group_entry *group;
275          for (i = 0; i <= 1; i++) {          for (i = 0; i <= 1; i++) {
276                  list1_for_each_entry(group, &address_group_list, list) {                  list1_for_each_entry(group, &address_group_list, list) {
277                          if (strcmp(group_name, group->group_name->name) == 0) return group;                          if (!strcmp(group_name, group->group_name->name))
278                                    return group;
279                  }                  }
280                  if (i == 0) {                  if (!i) {
281                          const u16 dummy[2] = { 0, 0 };                          const u16 dummy[2] = { 0, 0 };
282                          AddAddressGroupEntry(group_name, 0, dummy, dummy, 0);                          update_address_group_entry(group_name, false,
283                          AddAddressGroupEntry(group_name, 0, dummy, dummy, 1);                                                     dummy, dummy, false);
284                            update_address_group_entry(group_name, false,
285                                                       dummy, dummy, true);
286                  }                  }
287          }          }
288          return NULL;          return NULL;
289  }  }
290    
291  static int AddressMatchesToGroup(const bool is_ipv6, const u32 *address, const struct address_group_entry *group)  /**
292     * address_matches_to_group - Check whether the given address matches members of the given address group.
293     *
294     * @is_ipv6: True if @address is an IPv6 address.
295     * @address: An IPv4 or IPv6 address.
296     * @group:   Pointer to "struct address_group_entry".
297     *
298     * Returns true if @address matches addresses in @group group, false otherwise.
299     */
300    static bool address_matches_to_group(const bool is_ipv6, const u32 *address,
301                                         const struct address_group_entry *group)
302  {  {
303          struct address_group_member *member;          struct address_group_member *member;
304          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
305          list1_for_each_entry(member, &group->address_group_member_list, list) {          list1_for_each_entry(member, &group->address_group_member_list, list) {
306                  if (member->is_deleted) continue;                  if (member->is_deleted)
307                            continue;
308                  if (member->is_ipv6) {                  if (member->is_ipv6) {
309                          if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1;                          if (is_ipv6 &&
310                                memcmp(member->min.ipv6, address, 16) <= 0 &&
311                                memcmp(address, member->max.ipv6, 16) <= 0)
312                                    return true;
313                  } else {                  } else {
314                          if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1;                          if (!is_ipv6 &&
315                                member->min.ipv4 <= ip && ip <= member->max.ipv4)
316                                    return true;
317                  }                  }
318          }          }
319          return 0;          return false;
320  }  }
321    
322  int ReadAddressGroupPolicy(struct io_buffer *head)  /**
323     * ccs_read_address_group_policy - Read "struct address_group_entry" list.
324     *
325     * @head: Pointer to "struct ccs_io_buffer".
326     *
327     * Returns true on success, false otherwise.
328     */
329    bool ccs_read_address_group_policy(struct ccs_io_buffer *head)
330  {  {
331          struct list1_head *gpos;          struct list1_head *gpos;
332          struct list1_head *mpos;          struct list1_head *mpos;
333          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {
334                  struct address_group_entry *group;                  struct address_group_entry *group;
335                  group = list1_entry(gpos, struct address_group_entry, list);                  group = list1_entry(gpos, struct address_group_entry, list);
336                  list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) {                  list1_for_each_cookie(mpos, head->read_var2,
337                                          &group->address_group_member_list) {
338                          char buf[128];                          char buf[128];
339                          struct address_group_member *member;                          struct address_group_member *member;
340                          member = list1_entry(mpos, struct address_group_member, list);                          member = list1_entry(mpos, struct address_group_member,
341                          if (member->is_deleted) continue;                                               list);
342                            if (member->is_deleted)
343                                    continue;
344                          if (member->is_ipv6) {                          if (member->is_ipv6) {
345                                  const struct in6_addr *min_address = member->min.ipv6, *max_address = member->max.ipv6;                                  const struct in6_addr *min_address
346                                  print_ipv6(buf, sizeof(buf), min_address);                                          = member->min.ipv6;
347                                    const struct in6_addr *max_address
348                                            = member->max.ipv6;
349                                    ccs_print_ipv6(buf, sizeof(buf), min_address);
350                                  if (min_address != max_address) {                                  if (min_address != max_address) {
351                                            int len;
352                                          char *cp = strchr(buf, '\0');                                          char *cp = strchr(buf, '\0');
353                                          *cp++ = '-';                                          *cp++ = '-';
354                                          print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);                                          len = strlen(buf);
355                                            ccs_print_ipv6(cp, sizeof(buf) - len,
356                                                           max_address);
357                                  }                                  }
358                          } else {                          } else {
359                                  const u32 min_address = member->min.ipv4, max_address = member->max.ipv4;                                  const u32 min_address = member->min.ipv4;
360                                    const u32 max_address = member->max.ipv4;
361                                  memset(buf, 0, sizeof(buf));                                  memset(buf, 0, sizeof(buf));
362                                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address));                                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u",
363                                             HIPQUAD(min_address));
364                                  if (min_address != max_address) {                                  if (min_address != max_address) {
365                                          const int len = strlen(buf);                                          const int len = strlen(buf);
366                                          snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address));                                          snprintf(buf + len,
367                                                     sizeof(buf) - 1 - len,
368                                                     "-%u.%u.%u.%u",
369                                                     HIPQUAD(max_address));
370                                  }                                  }
371                          }                          }
372                          if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) return -ENOMEM;                          if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP
373                                               "%s %s\n", group->group_name->name,
374                                               buf))
375                                    goto out;
376                  }                  }
377          }          }
378          return 0;          return true;
379     out:
380            return false;
381  }  }
382    
 /*************************  NETWORK NETWORK ACL HANDLER  *************************/  
   
383  #if !defined(NIP6)  #if !defined(NIP6)
384  #define NIP6(addr) \  #define NIP6(addr)      \
385          ntohs((addr).s6_addr16[0]), \          ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \
386          ntohs((addr).s6_addr16[1]), \          ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
387          ntohs((addr).s6_addr16[2]), \          ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
388          ntohs((addr).s6_addr16[3]), \          ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7])
         ntohs((addr).s6_addr16[4]), \  
         ntohs((addr).s6_addr16[5]), \  
         ntohs((addr).s6_addr16[6]), \  
         ntohs((addr).s6_addr16[7])  
389  #endif  #endif
390    
391  char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)  /**
392     * ccs_print_ipv6 - Print an IPv6 address.
393     *
394     * @buffer:     Buffer to write to.
395     * @buffer_len: Size of @buffer.
396     * @ip:         Pointer to "struct in6_addr".
397     *
398     * Returns nothing.
399     */
400    void ccs_print_ipv6(char *buffer, const int buffer_len,
401                        const struct in6_addr *ip)
402  {  {
403          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
404          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));          snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));
         return buffer;  
405  }  }
406    
407  const char *network2keyword(const unsigned int operation)  /**
408     * ccs_net2keyword - Convert network operation index to network operation name.
409     *
410     * @operation: Type of operation.
411     *
412     * Returns the name of operation.
413     */
414    const char *ccs_net2keyword(const u8 operation)
415  {  {
416          const char *keyword = "unknown";          const char *keyword = "unknown";
417          switch (operation) {          switch (operation) {
# Line 292  const char *network2keyword(const unsign Line 443  const char *network2keyword(const unsign
443          return keyword;          return keyword;
444  }  }
445    
446  static int AddNetworkEntry(const u8 operation, const u8 record_type, const struct address_group_entry *group, const u32 *min_address, const u32 *max_address, const u16 min_port, const u16 max_port, struct domain_info *domain, const struct condition_list *condition, const bool is_delete)  /**
447     * update_network_entry - Update "struct ip_network_acl_record" list.
448     *
449     * @operation:   Type of operation.
450     * @record_type: Type of address.
451     * @group:       Pointer to "struct address_group_entry". May be NULL.
452     * @min_address: Start of IPv4 or IPv6 address range.
453     * @max_address: End of IPv4 or IPv6 address range.
454     * @min_port:    Start of port number range.
455     * @max_port:    End of port number range.
456     * @domain:      Pointer to "struct domain_info".
457     * @condition:   Pointer to "struct condition_list". May be NULL.
458     * @is_delete:   True if it is a delete request.
459     *
460     * Returns 0 on success, negative value otherwise.
461     */
462    static int update_network_entry(const u8 operation, const u8 record_type,
463                                    const struct address_group_entry *group,
464                                    const u32 *min_address, const u32 *max_address,
465                                    const u16 min_port, const u16 max_port,
466                                    struct domain_info *domain,
467                                    const struct condition_list *condition,
468                                    const bool is_delete)
469  {  {
470          struct acl_info *ptr;          struct acl_info *ptr;
471          struct ip_network_acl_record *acl;          struct ip_network_acl_record *acl;
472          int error = -ENOMEM;          int error = -ENOMEM;
473          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
474          const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;          const u32 min_ip = ntohl(*min_address);
475          if (!domain) return -EINVAL;          const u32 max_ip = ntohl(*max_address);
476          if (record_type == IP_RECORD_TYPE_IPv6) {          const struct in6_addr *saved_min_address = NULL;
477                  if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL          const struct in6_addr *saved_max_address = NULL;
478                      || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;          if (!domain)
479          }                  return -EINVAL;
480            if (record_type != IP_RECORD_TYPE_IPv6)
481                    goto not_ipv6;
482            saved_min_address = save_ipv6_address((struct in6_addr *) min_address);
483            saved_max_address = save_ipv6_address((struct in6_addr *) max_address);
484            if (!saved_min_address || !saved_max_address)
485                    return -ENOMEM;
486     not_ipv6:
487          mutex_lock(&domain_acl_lock);          mutex_lock(&domain_acl_lock);
488          if (!is_delete) {          if (is_delete)
489                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {                  goto delete;
490                          acl = container_of(ptr, struct ip_network_acl_record, head);          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
491                          if (ptr->type == TYPE_IP_NETWORK_ACL && acl->operation_type == operation && acl->record_type == record_type && ptr->cond == condition && acl->min_port == min_port && max_port == acl->max_port) {                  if (ccs_acl_type1(ptr) != TYPE_IP_NETWORK_ACL)
492                                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                          continue;
493                                          if (acl->u.group == group) {                  if (ccs_get_condition_part(ptr) != condition)
494                                                  ptr->is_deleted = 0;                          continue;
495                                                  /* Found. Nothing to do. */                  acl = container_of(ptr, struct ip_network_acl_record, head);
496                                                  error = 0;                  if (acl->operation_type != operation ||
497                                                  goto out;                      acl->record_type != record_type ||
498                                          }                      acl->min_port != min_port || max_port != acl->max_port)
499                                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                          continue;
                                         if (acl->u.ipv4.min == min_ip && max_ip == acl->u.ipv4.max) {  
                                                 ptr->is_deleted = 0;  
                                                 /* Found. Nothing to do. */  
                                                 error = 0;  
                                                 goto out;  
                                         }  
                                 } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                                         if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) {  
                                                 ptr->is_deleted = 0;  
                                                 /* Found. Nothing to do. */  
                                                 error = 0;  
                                                 goto out;  
                                         }  
                                 }  
                         }  
                 }  
                 /* Not found. Append it to the tail. */  
                 if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out;  
                 acl->head.type = TYPE_IP_NETWORK_ACL;  
                 acl->operation_type = operation;  
                 acl->record_type = record_type;  
                 acl->head.cond = condition;  
500                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
501                          acl->u.group = group;                          if (acl->u.group != group)
502                                    continue;
503                  } else if (record_type == IP_RECORD_TYPE_IPv4) {                  } else if (record_type == IP_RECORD_TYPE_IPv4) {
504                          acl->u.ipv4.min = min_ip;                          if (acl->u.ipv4.min != min_ip ||
505                          acl->u.ipv4.max = max_ip;                              max_ip != acl->u.ipv4.max)
506                  } else {                                  continue;
507                          acl->u.ipv6.min = saved_min_address;                  } else if (record_type == IP_RECORD_TYPE_IPv6) {
508                          acl->u.ipv6.max = saved_max_address;                          if (acl->u.ipv6.min != saved_min_address ||
509                                saved_max_address != acl->u.ipv6.max)
510                                    continue;
511                  }                  }
512                  acl->min_port = min_port;                  error = ccs_add_domain_acl(NULL, ptr);
513                  acl->max_port = max_port;                  goto out;
514                  error = AddDomainACL(domain, &acl->head);          }
515            /* Not found. Append it to the tail. */
516            acl = ccs_alloc_acl_element(TYPE_IP_NETWORK_ACL, condition);
517            if (!acl)
518                    goto out;
519            acl->operation_type = operation;
520            acl->record_type = record_type;
521            if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
522                    acl->u.group = group;
523            } else if (record_type == IP_RECORD_TYPE_IPv4) {
524                    acl->u.ipv4.min = min_ip;
525                    acl->u.ipv4.max = max_ip;
526          } else {          } else {
527                  error = -ENOENT;                  acl->u.ipv6.min = saved_min_address;
528                  list1_for_each_entry(ptr, &domain->acl_info_list, list) {                  acl->u.ipv6.max = saved_max_address;
529                          acl = container_of(ptr, struct ip_network_acl_record, head);          }
530                          if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || acl->record_type != record_type || ptr->cond != condition || acl->min_port != min_port || acl->max_port != max_port) continue;          acl->min_port = min_port;
531                          if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {          acl->max_port = max_port;
532                                  if (acl->u.group != group) continue;          error = ccs_add_domain_acl(domain, &acl->head);
533                          } else if (record_type == IP_RECORD_TYPE_IPv4) {          goto out;
534                                  if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue;   delete:
535                          } else if (record_type == IP_RECORD_TYPE_IPv6) {          error = -ENOENT;
536                                  if (acl->u.ipv6.min != saved_min_address || saved_max_address != acl->u.ipv6.max) continue;          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
537                          }                  if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
538                          error = DelDomainACL(ptr);                          continue;
539                          break;                  if (ccs_get_condition_part(ptr) != condition)
540                            continue;
541                    acl = container_of(ptr, struct ip_network_acl_record, head);
542                    if (acl->operation_type != operation ||
543                        acl->record_type != record_type ||
544                        acl->min_port != min_port || max_port != acl->max_port)
545                            continue;
546                    if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
547                            if (acl->u.group != group)
548                                    continue;
549                    } else if (record_type == IP_RECORD_TYPE_IPv4) {
550                            if (acl->u.ipv4.min != min_ip ||
551                                max_ip != acl->u.ipv4.max)
552                                    continue;
553                    } else if (record_type == IP_RECORD_TYPE_IPv6) {
554                            if (acl->u.ipv6.min != saved_min_address ||
555                                saved_max_address != acl->u.ipv6.max)
556                                    continue;
557                  }                  }
558                    error = ccs_del_domain_acl(ptr);
559                    break;
560          }          }
561   out: ;   out:
562          mutex_unlock(&domain_acl_lock);          mutex_unlock(&domain_acl_lock);
563          return error;          return error;
564  }  }
565    
566  static int CheckNetworkEntry(const bool is_ipv6, const int operation, const u32 *address, const u16 port)  /**
567     * check_network_entry - Check permission for network operation.
568     *
569     * @is_ipv6:   True if @address is an IPv6 address.
570     * @operation: Type of operation.
571     * @address:   An IPv4 or IPv6 address.
572     * @port:      Port number.
573     *
574     * Returns 0 on success, negative value otherwise.
575     */
576    static int check_network_entry(const bool is_ipv6, const u8 operation,
577                                   const u32 *address, const u16 port)
578  {  {
579          struct domain_info * const domain = current->domain_info;          struct domain_info * const domain = current->domain_info;
580          struct acl_info *ptr;          struct acl_info *ptr;
581          const char *keyword = network2keyword(operation);          const char *keyword = ccs_net2keyword(operation);
582          const u8 profile = current->domain_info->profile;          const u8 profile = current->domain_info->profile;
583          const unsigned int mode = CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK);          const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_NETWORK);
584          const bool is_enforce = (mode == 3);          const bool is_enforce = (mode == 3);
585          const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/          /* using host byte order to allow u32 comparison than memcmp().*/
586          bool found = 0;          const u32 ip = ntohl(*address);
587          if (!mode) return 0;          bool found = false;
588            char buf[64];
589            if (!mode)
590                    return 0;
591          list1_for_each_entry(ptr, &domain->acl_info_list, list) {          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
592                  struct ip_network_acl_record *acl;                  struct ip_network_acl_record *acl;
593                    if (ccs_acl_type2(ptr) != TYPE_IP_NETWORK_ACL)
594                            continue;
595                  acl = container_of(ptr, struct ip_network_acl_record, head);                  acl = container_of(ptr, struct ip_network_acl_record, head);
596                  if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || port < acl->min_port || acl->max_port < port || CheckCondition(ptr->cond, NULL)) continue;                  if (acl->operation_type != operation || port < acl->min_port ||
597                        acl->max_port < port || !ccs_check_condition(ptr, NULL))
598                            continue;
599                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {                  if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
600                          if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue;                          if (!address_matches_to_group(is_ipv6, address,
601                                                          acl->u.group))
602                                    continue;
603                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {                  } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {
604                          if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue;                          if (is_ipv6 ||
605                                ip < acl->u.ipv4.min || acl->u.ipv4.max < ip)
606                                    continue;
607                  } else {                  } else {
608                          if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue;                          if (!is_ipv6 ||
609                                memcmp(acl->u.ipv6.min, address, 16) > 0 ||
610                                memcmp(address, acl->u.ipv6.max, 16) > 0)
611                                    continue;
612                  }                  }
613                  found = 1;                  ccs_update_condition(ptr);
614                    found = true;
615                  break;                  break;
616          }          }
617          AuditNetworkLog(is_ipv6, keyword, address, port, found, profile, mode);          memset(buf, 0, sizeof(buf));
618          if (found) return 0;          if (is_ipv6)
619          if (TomoyoVerboseMode()) {                  ccs_print_ipv6(buf, sizeof(buf),
620                  if (is_ipv6) {                                 (const struct in6_addr *) address);
621                          char buf[64];          else
622                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip));
623                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));          audit_network_log(is_ipv6, keyword, buf, port, found, profile, mode);
624                  } else {          if (found)
625                          printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain));                  return 0;
626                  }          if (ccs_verbose_mode())
627          }                  printk(KERN_WARNING "TOMOYO-%s: %s to %s %u denied for %s\n",
628          if (is_enforce) {                         ccs_get_msg(is_enforce), keyword, buf, port,
629                  if (is_ipv6) {                         ccs_get_last_name(domain));
630                          char buf[64];          if (is_enforce)
631                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);                  return ccs_check_supervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s "
632                          return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);                                              "%s %u\n", domain->domainname->name,
633                  }                                              keyword, buf, port);
634                  return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);          if (mode == 1 && ccs_check_domain_quota(domain))
635          }                  update_network_entry(operation, is_ipv6 ?
636          if (mode == 1 && CheckDomainQuota(domain)) AddNetworkEntry(operation, is_ipv6 ? IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4, NULL, address, address, port, port, domain, NULL, 0);                                       IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4,
637                                         NULL, address, address, port, port, domain,
638                                         NULL, 0);
639          return 0;          return 0;
640  }  }
641    
642  int AddNetworkPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete)  /**
643  {   * ccs_write_network_policy - Write "struct ip_network_acl_record" list.
644          u8 sock_type, operation, record_type;   *
645          u16 min_address[8], max_address[8];   * @data:      String to parse.
646     * @domain:    Pointer to "struct domain_info".
647     * @condition: Pointer to "struct condition_list". May be NULL.
648     * @is_delete: True if it is a delete request.
649     *
650     * Returns 0 on success, negative value otherwise.
651     */
652    int ccs_write_network_policy(char *data, struct domain_info *domain,
653                                 const struct condition_list *condition,
654                                 const bool is_delete)
655    {
656            u8 sock_type;
657            u8 operation;
658            u8 record_type;
659            u16 min_address[8];
660            u16 max_address[8];
661          struct address_group_entry *group = NULL;          struct address_group_entry *group = NULL;
662          u16 min_port, max_port;          u16 min_port;
663          int count;          u16 max_port;
664          char *cp1 = NULL, *cp2 = NULL;          u8 count;
665          if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++;          char *cp1 = strchr(data, ' ');
666          if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM;          char *cp2;
667          else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM;          if (!cp1)
668          else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW;                  goto out;
669          else goto out;          cp1++;
670          if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++;          if (!strncmp(data, "TCP ", 4))
671          if (strncmp(cp1, "bind ", 5) == 0) {                  sock_type = SOCK_STREAM;
672                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND;          else if (!strncmp(data, "UDP ", 4))
673          } else if (strncmp(cp1, "connect ", 8) == 0) {                  sock_type = SOCK_DGRAM;
674                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;          else if (!strncmp(data, "RAW ", 4))
675          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) {                  sock_type = SOCK_RAW;
676            else
677                    goto out;
678            cp2 = strchr(cp1, ' ');
679            if (!cp2)
680                    goto out;
681            cp2++;
682            if (!strncmp(cp1, "bind ", 5))
683                    switch (sock_type) {
684                    case SOCK_STREAM:
685                            operation = NETWORK_ACL_TCP_BIND;
686                            break;
687                    case SOCK_DGRAM:
688                            operation = NETWORK_ACL_UDP_BIND;
689                            break;
690                    default:
691                            operation = NETWORK_ACL_RAW_BIND;
692                    }
693            else if (!strncmp(cp1, "connect ", 8))
694                    switch (sock_type) {
695                    case SOCK_STREAM:
696                            operation = NETWORK_ACL_TCP_CONNECT;
697                            break;
698                    case SOCK_DGRAM:
699                            operation = NETWORK_ACL_UDP_CONNECT;
700                            break;
701                    default:
702                            operation = NETWORK_ACL_RAW_CONNECT;
703                    }
704            else if (sock_type == SOCK_STREAM && !strncmp(cp1, "listen ", 7))
705                  operation = NETWORK_ACL_TCP_LISTEN;                  operation = NETWORK_ACL_TCP_LISTEN;
706          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) {          else if (sock_type == SOCK_STREAM && !strncmp(cp1, "accept ", 7))
707                  operation = NETWORK_ACL_TCP_ACCEPT;                  operation = NETWORK_ACL_TCP_ACCEPT;
708          } else {          else
709                  goto out;                  goto out;
710          }          cp1 = strchr(cp2, ' ');
711          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';          if (!cp1)
712          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                  goto out;
713                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],          *cp1++ = '\0';
714                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],          count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
715                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                         "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
716                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                         &min_address[0], &min_address[1],
717                  int i;                         &min_address[2], &min_address[3],
718                           &min_address[4], &min_address[5],
719                           &min_address[6], &min_address[7],
720                           &max_address[0], &max_address[1],
721                           &max_address[2], &max_address[3],
722                           &max_address[4], &max_address[5],
723                           &max_address[6], &max_address[7]);
724            if (count == 8 || count == 16) {
725                    u8 i;
726                  for (i = 0; i < 8; i++) {                  for (i = 0; i < 8; i++) {
727                          min_address[i] = htons(min_address[i]);                          min_address[i] = htons(min_address[i]);
728                          max_address[i] = htons(max_address[i]);                          max_address[i] = htons(max_address[i]);
729                  }                  }
730                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                  if (count == 8)
731                            memmove(max_address, min_address, sizeof(min_address));
732                  record_type = IP_RECORD_TYPE_IPv6;                  record_type = IP_RECORD_TYPE_IPv6;
733          } else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",                  goto ok;
734                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],          }
735                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {          count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
736                  u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);                         &min_address[0], &min_address[1],
737                  * (u32 *) min_address = ip;                         &min_address[2], &min_address[3],
738                  if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);                         &max_address[0], &max_address[1],
739                  * (u32 *) max_address = ip;                         &max_address[2], &max_address[3]);
740            if (count == 4 || count == 8) {
741                    u32 ip = htonl((((u8) min_address[0]) << 24)
742                                   + (((u8) min_address[1]) << 16)
743                                   + (((u8) min_address[2]) << 8)
744                                   + (u8) min_address[3]);
745                    *(u32 *) min_address = ip;
746                    if (count == 8)
747                            ip = htonl((((u8) max_address[0]) << 24)
748                                       + (((u8) max_address[1]) << 16)
749                                       + (((u8) max_address[2]) << 8)
750                                       + (u8) max_address[3]);
751                    *(u32 *) max_address = ip;
752                  record_type = IP_RECORD_TYPE_IPv4;                  record_type = IP_RECORD_TYPE_IPv4;
753          } else if (*cp2 == '@') {                  goto ok;
                 if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM;  
                 record_type = IP_RECORD_TYPE_ADDRESS_GROUP;  
         } else {  
                 goto out;  
754          }          }
755          if (strchr(cp1, ' ')) goto out;          if (*cp2 == '@') {
756          if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) {                  group = find_or_assign_new_address_group(cp2 + 1);
757                  if (count == 1) max_port = min_port;                  if (!group)
758                  return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, condition, is_delete);                          return -ENOMEM;
759                    record_type = IP_RECORD_TYPE_ADDRESS_GROUP;
760                    goto ok;
761          }          }
762   out: ;   out:
763          return -EINVAL;          return -EINVAL;
764     ok:
765            if (strchr(cp1, ' '))
766                    goto out;
767            count = sscanf(cp1, "%hu-%hu", &min_port, &max_port);
768            if (count != 1 && count != 2)
769                    goto out;
770            if (count == 1)
771                    max_port = min_port;
772            return update_network_entry(operation, record_type, group,
773                                        (u32 *) min_address, (u32 *) max_address,
774                                        min_port, max_port, domain, condition,
775                                        is_delete);
776  }  }
777    
778  int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
779     * ccs_check_network_listen_acl - Check permission for listen() operation.
780     *
781     * @is_ipv6: True if @address is an IPv6 address.
782     * @address: An IPv4 or IPv6 address.
783     * @port:    Port number.
784     *
785     * Returns 0 on success, negative value otherwise.
786     */
787    int ccs_check_network_listen_acl(const _Bool is_ipv6, const u8 *address,
788                                     const u16 port)
789  {  {
790          return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port));          return check_network_entry(is_ipv6, NETWORK_ACL_TCP_LISTEN,
791                                       (const u32 *) address, ntohs(port));
792  }  }
793    
794  int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
795     * ccs_check_network_connect_acl - Check permission for connect() operation.
796     *
797     * @is_ipv6:   True if @address is an IPv6 address.
798     * @sock_type: Type of socket. (TCP or UDP or RAW)
799     * @address:   An IPv4 or IPv6 address.
800     * @port:      Port number.
801     *
802     * Returns 0 on success, negative value otherwise.
803     */
804    int ccs_check_network_connect_acl(const _Bool is_ipv6, const int sock_type,
805                                      const u8 *address, const u16 port)
806  {  {
807          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT), (const u32 *) address, ntohs(port));          u8 operation;
808            switch (sock_type) {
809            case SOCK_STREAM:
810                    operation = NETWORK_ACL_TCP_CONNECT;
811                    break;
812            case SOCK_DGRAM:
813                    operation = NETWORK_ACL_UDP_CONNECT;
814                    break;
815            default:
816                    operation = NETWORK_ACL_RAW_CONNECT;
817            }
818            return check_network_entry(is_ipv6, operation, (const u32 *) address,
819                                       ntohs(port));
820  }  }
821    
822  int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
823     * ccs_check_network_bind_acl - Check permission for bind() operation.
824     *
825     * @is_ipv6:   True if @address is an IPv6 address.
826     * @sock_type: Type of socket. (TCP or UDP or RAW)
827     * @address:   An IPv4 or IPv6 address.
828     * @port:      Port number.
829     *
830     * Returns 0 on success, negative value otherwise.
831     */
832    int ccs_check_network_bind_acl(const _Bool is_ipv6, const int sock_type,
833                                   const u8 *address, const u16 port)
834  {  {
835          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND), (const u32 *) address, ntohs(port));          u8 operation;
836            switch (sock_type) {
837            case SOCK_STREAM:
838                    operation = NETWORK_ACL_TCP_BIND;
839                    break;
840            case SOCK_DGRAM:
841                    operation = NETWORK_ACL_UDP_BIND;
842                    break;
843            default:
844                    operation = NETWORK_ACL_RAW_BIND;
845            }
846            return check_network_entry(is_ipv6, operation, (const u32 *) address,
847                                       ntohs(port));
848  }  }
849    
850  int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port)  /**
851     * ccs_check_network_accept_acl - Check permission for accept() operation.
852     *
853     * @is_ipv6: True if @address is an IPv6 address.
854     * @address: An IPv4 or IPv6 address.
855     * @port:    Port number.
856     *
857     * Returns 0 on success, negative value otherwise.
858     */
859    int ccs_check_network_accept_acl(const _Bool is_ipv6, const u8 *address,
860                                     const u16 port)
861  {  {
862          int retval;          int retval;
863          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
864          retval = CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port));          retval = check_network_entry(is_ipv6, NETWORK_ACL_TCP_ACCEPT,
865                                         (const u32 *) address, ntohs(port));
866          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
867          return retval;          return retval;
868  }  }
869    
870  int CheckNetworkSendMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
871     * ccs_check_network_sendmsg_acl - Check permission for sendmsg() operation.
872     *
873     * @is_ipv6:   True if @address is an IPv6 address.
874     * @sock_type: Type of socket. (UDP or RAW)
875     * @address:   An IPv4 or IPv6 address.
876     * @port:      Port number.
877     *
878     * Returns 0 on success, negative value otherwise.
879     */
880    int ccs_check_network_sendmsg_acl(const _Bool is_ipv6, const int sock_type,
881                                      const u8 *address, const u16 port)
882  {  {
883          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          u8 operation;
884            if (sock_type == SOCK_DGRAM)
885                    operation = NETWORK_ACL_UDP_CONNECT;
886            else
887                    operation = NETWORK_ACL_RAW_CONNECT;
888            return check_network_entry(is_ipv6, operation, (const u32 *) address,
889                                       ntohs(port));
890  }  }
891    
892  int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /**
893     * ccs_check_network_recvmsg_acl - Check permission for recvmsg() operation.
894     *
895     * @is_ipv6:   True if @address is an IPv6 address.
896     * @sock_type: Type of socket. (UDP or RAW)
897     * @address:   An IPv4 or IPv6 address.
898     * @port:      Port number.
899     *
900     * Returns 0 on success, negative value otherwise.
901     */
902    int ccs_check_network_recvmsg_acl(const _Bool is_ipv6, const int sock_type,
903                                      const u8 *address, const u16 port)
904  {  {
905          int retval;          int retval;
906            const u8 operation
907                    = (sock_type == SOCK_DGRAM) ?
908                    NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;
909          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
910          retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          retval = check_network_entry(is_ipv6, operation, (const u32 *) address,
911                                         ntohs(port));
912          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
913          return retval;          return retval;
914  }  }
   
 /***** TOMOYO Linux end. *****/  

Legend:
Removed from v.815  
changed lines
  Added in v.1064

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