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

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

Legend:
Removed from v.808  
changed lines
  Added in v.1191

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