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

Subversion リポジトリの参照

Diff of /trunk/1.6.x/ccs-patch/fs/tomoyo_network.c

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

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

Legend:
Removed from v.1032  
changed lines
  Added in v.1052

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