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

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/ccs-patch/fs/tomoyo_network.c revision 111 by kumaneko, Wed Feb 28 11:45:08 2007 UTC trunk/1.6.x/ccs-patch/fs/tomoyo_network.c revision 1052 by kumaneko, Mon Mar 24 03:50: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.3.2   2007/02/14   * 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  /*************************  VARIABLES  *************************/  /**
21     * audit_network_log - Audit network log.
22  extern struct semaphore domain_acl_lock;   *
23     * @is_ipv6:    True if @address is an IPv6 address.
24  /*************************  AUDIT FUNCTIONS  *************************/   * @operation:  The name of operation.
25     * @address:    An IPv4 or IPv6 address.
26  #ifdef CONFIG_TOMOYO_AUDIT   * @port:       Port number.
27  static int AuditNetworkLog(const int is_ipv6, const char *operation, const u32 *address, const u16 port, const int is_granted)   * @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)) == 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 u16 *) 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  }  }
 #else  
 static inline void AuditNetworkLog(const int is_ipv6, const char *operation, const u32 *address, const u16 port, const int is_granted) {}  
 #endif  
61    
62  /*************************  ADDRESS GROUP HANDLER  *************************/  /**
63     * save_ipv6_address - Keep the given IPv6 address on the RAM.
64     *
65     * @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;
74            struct addr_list {
75                    struct in6_addr addr[block_size];
76                    struct list1_head list;
77                    u32 in_use_count;
78            };
79            static LIST1_HEAD(address_list);
80            struct addr_list *ptr;
81            static DEFINE_MUTEX(lock);
82            u8 i = block_size;
83            if (!addr)
84                    return NULL;
85            mutex_lock(&lock);
86            list1_for_each_entry(ptr, &address_list, list) {
87                    for (i = 0; i < ptr->in_use_count; i++) {
88                            if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0)
89                                    goto ok;
90                    }
91                    if (i < block_size)
92                            break;
93            }
94            if (i == block_size) {
95                    ptr = ccs_alloc_element(sizeof(*ptr));
96                    if (!ptr)
97                            goto ok;
98                    list1_add_tail_mb(&ptr->list, &address_list);
99                    i = 0;
100            }
101            ptr->addr[ptr->in_use_count++] = *addr;
102     ok:
103            mutex_unlock(&lock);
104            return ptr ? &ptr->addr[i] : NULL;
105    }
106    
107  static ADDRESS_GROUP_ENTRY *group_list = NULL;  /* The list for "struct address_group_entry". */
108    static LIST1_HEAD(address_group_list);
109    
110  static int AddAddressGroupEntry(const char *group_name, const u8 is_ipv6, const u16 *min_address, const u16 *max_address, const int is_delete)  /**
111  {   * update_address_group_entry - Update "struct address_group_entry" list.
112          static DECLARE_MUTEX(lock);   *
113          ADDRESS_GROUP_ENTRY *new_group, *group;   * @group_name:  The name of group.
114          ADDRESS_GROUP_MEMBER *new_member, *member;   * @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);
128            struct address_group_entry *new_group, *group;
129            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;
132            const struct in6_addr *saved_max_address = NULL;
133          int error = -ENOMEM;          int error = -ENOMEM;
134          if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL;          bool found = false;
135          if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM;          if (!ccs_is_correct_path(group_name, 0, 0, 0, __func__) ||
136          down(&lock);              !group_name[0])
137          for (group = group_list; group; group = group->next) {                  return -EINVAL;
138                  if (saved_group_name != group->group_name) continue;          saved_group_name = ccs_save_name(group_name);
139                  for (member = group->first_member; member; member = member->next) {          if (!saved_group_name)
140                          if (member->is_ipv6 != is_ipv6) continue;                  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);
151            list1_for_each_entry(group, &address_group_list, list) {
152                    if (saved_group_name != group->group_name)
153                            continue;
154                    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 (memcmp(member->min.ipv6, min_address, 16) || memcmp(member->max.ipv6, max_address, 16)) 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;
169                          goto out;                          goto out;
170                  }                  }
171                    found = true;
172                  break;                  break;
173          }          }
174          if (is_delete) {          if (is_delete) {
175                  error = -ENOENT;                  error = -ENOENT;
176                  goto out;                  goto out;
177          }          }
178          if (!group) {          if (!found) {
179                  if ((new_group = (ADDRESS_GROUP_ENTRY *) alloc_element(sizeof(ADDRESS_GROUP_ENTRY))) == 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);
183                  new_group->group_name = saved_group_name;                  new_group->group_name = saved_group_name;
184                  mb(); /* Instead of using spinlock. */                  list1_add_tail_mb(&new_group->list, &address_group_list);
                 if ((group = group_list) != NULL) {  
                         while (group->next) group = group->next; group->next = new_group;  
                 } else {  
                         group_list= new_group;  
                 }  
185                  group = new_group;                  group = new_group;
186          }          }
187          if ((new_member = (ADDRESS_GROUP_MEMBER *) alloc_element(sizeof(ADDRESS_GROUP_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                  memmove(new_member->min.ipv6, min_address, 16);                  new_member->min.ipv6 = saved_min_address;
193                  memmove(new_member->max.ipv6, max_address, 16);                  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;
         }  
         mb(); /* Instead of using spinlock. */  
         if ((member = group->first_member) != NULL) {  
                 while (member->next) member = member->next; member->next = new_member;  
         } else {  
                 group->first_member = new_member;  
197          }          }
198            list1_add_tail_mb(&new_member->list, &group->address_group_member_list);
199          error = 0;          error = 0;
200   out:   out:
201          up(&lock);          mutex_unlock(&lock);
202          return error;          return error;
203  }  }
204    
205  int AddAddressGroupPolicy(char *data, const int 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          int count, is_ipv6;          u8 count;
216            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                  int i;                         &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;
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                  is_ipv6 = 1;                          memmove(max_address, min_address, sizeof(min_address));
240          } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",                  is_ipv6 = true;
241                                                             &min_address[0], &min_address[1], &min_address[2], &min_address[3],                  goto ok;
242                                                             &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {          }
243                  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",
244                  * (u32 *) min_address = ip;                         &min_address[0], &min_address[1],
245                  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],
246                  * (u32 *) max_address = ip;                         &max_address[0], &max_address[1],
247                  is_ipv6 = 0;                         &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;
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 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          int i;          u8 i;
280          ADDRESS_GROUP_ENTRY *group;          struct address_group_entry *group;
281          for (i = 0; i <= 1; i++) {          for (i = 0; i <= 1; i++) {
282                  for (group = group_list; group; group = group->next) {                  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 int AddressMatchesToGroup(const u8 is_ipv6, const u32 *address, const 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          ADDRESS_GROUP_MEMBER *member;          struct address_group_member *member;
311          const u32 ip = ntohl(*address);          const u32 ip = ntohl(*address);
312          for (member = group->first_member; member; member = member->next) {          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 (memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1;                          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 (member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1;                          if (!is_ipv6 &&
322                                member->min.ipv4 <= ip && ip <= member->max.ipv4)
323                                    return true;
324                  }                  }
325          }          }
326          return 0;          return false;
327  }  }
328    
329  int ReadAddressGroupPolicy(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          ADDRESS_GROUP_ENTRY *group = (ADDRESS_GROUP_ENTRY *) head->read_var1;          struct list1_head *gpos;
339          ADDRESS_GROUP_MEMBER *member = (ADDRESS_GROUP_MEMBER *) head->read_var2;          struct list1_head *mpos;
340          if (!group) group = group_list;          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {
341          while (group) {                  struct address_group_entry *group;
342                  head->read_var1 = (struct domain_info *) group;                  group = list1_entry(gpos, struct address_group_entry, list);
343                  if (!member) member = group->first_member;                  list1_for_each_cookie(mpos, head->read_var2,
344                  while (member) {                                        &group->address_group_member_list) {
345                          head->read_var2 = (void *) member;                          char buf[128];
346                          if (!member->is_deleted) {                          struct address_group_member *member;
347                                  char buf[128];                          member = list1_entry(mpos, struct address_group_member,
348                                  if (member->is_ipv6) {                                               list);
349                                          const u16 *min_address = member->min.ipv6, *max_address = member->max.ipv6;                          if (member->is_deleted)
350                                          print_ipv6(buf, sizeof(buf), min_address);                                  continue;
351                                          if (memcmp(min_address, max_address, 16)) {                          if (!member->is_ipv6) {
352                                                  char *cp = strchr(buf, '\0');                                  const struct in6_addr *min_address
353                                                  *cp++ = '-';                                          = member->min.ipv6;
354                                                  print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);                                  const struct in6_addr *max_address
355                                          }                                          = member->max.ipv6;
356                                  } else {                                  ccs_print_ipv6(buf, sizeof(buf), min_address);
357                                          const u32 min_address = member->min.ipv4, max_address = member->max.ipv4;                                  if (min_address != max_address) {
358                                          memset(buf, 0, sizeof(buf));                                          int len;
359                                          snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address));                                          char *cp = strchr(buf, '\0');
360                                          if (min_address != max_address) {                                          *cp++ = '-';
361                                                  const int len = strlen(buf);                                          len = strlen(buf);
362                                                  snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address));                                          ccs_print_ipv6(cp, sizeof(buf) - len,
363                                          }                                                         max_address);
364                                    }
365                            } else {
366                                    const u32 min_address = member->min.ipv4;
367                                    const u32 max_address = member->max.ipv4;
368                                    memset(buf, 0, sizeof(buf));
369                                    snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u",
370                                             HIPQUAD(min_address));
371                                    if (min_address != max_address) {
372                                            const int len = strlen(buf);
373                                            snprintf(buf + len,
374                                                     sizeof(buf) - 1 - len,
375                                                     "-%u.%u.%u.%u",
376                                                     HIPQUAD(max_address));
377                                  }                                  }
                                 if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) break;  
378                          }                          }
379                          member = member->next;                          if (!ccs_io_printf(head, KEYWORD_ADDRESS_GROUP
380                                               "%s %s\n", group->group_name->name,
381                                               buf))
382                                    goto out;
383                  }                  }
                 if (member) break;  
                 head->read_var2 = NULL;  
                 group = group->next;  
384          }          }
385          return group ? -ENOMEM : 0;          return true;
386     out:
387            return false;
388  }  }
389    
390  /*************************  NETWORK NETWORK ACL HANDLER  *************************/  #if !defined(NIP6)
391    #define NIP6(addr)      \
392            ntohs((addr).s6_addr16[0]),ntohs((addr).s6_addr16[1]),\
393            ntohs((addr).s6_addr16[2]),ntohs((addr).s6_addr16[3]),\
394            ntohs((addr).s6_addr16[4]),ntohs((addr).s6_addr16[5]),\
395            ntohs((addr).s6_addr16[6]),ntohs((addr).s6_addr16[7])
396    #endif
397    
398  char *print_ipv6(char *buffer, const int buffer_len, const u16 *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", ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]), ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]));          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 *network2keyword(const unsigned int 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 256  const char *network2keyword(const unsign Line 451  const char *network2keyword(const unsign
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 u8 is_delete, const struct condition_list *condition)  /**
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;
480          int error = -ENOMEM;          int error = -ENOMEM;
481          const u8 type = TYPE_IP_NETWORK_ACL;          /* using host byte order to allow u32 comparison than memcmp().*/
482          const u8 hash = operation;          const u32 min_ip = ntohl(*min_address);
483          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/          const u32 max_ip = ntohl(*max_address);
484          if (!domain) return -EINVAL;          const struct in6_addr *saved_min_address = NULL;
485          down(&domain_acl_lock);          const struct in6_addr *saved_max_address = NULL;
486          if (!is_delete) {          if (!domain)
487                  if ((ptr = domain->first_acl_ptr) == NULL) goto first_entry;                  return -EINVAL;
488                  while (1) {          if (record_type != IP_RECORD_TYPE_IPv6)
489                          IP_NETWORK_ACL_RECORD *new_ptr;                  goto not_ipv6;
490                          if (ptr->type == type && ptr->u.b[0] == hash && ptr->cond == condition && ((IP_NETWORK_ACL_RECORD *) ptr)->min_port == min_port && max_port == ((IP_NETWORK_ACL_RECORD *) ptr)->max_port) {          saved_min_address = save_ipv6_address((struct in6_addr *) min_address);
491                                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {          saved_max_address = save_ipv6_address((struct in6_addr *) max_address);
492                                          if (((IP_NETWORK_ACL_RECORD *) ptr)->u.group == group) {          if (!saved_min_address || !saved_max_address)
493                                                  ptr->is_deleted = 0;                  return -ENOMEM;
494                                                  /* Found. Nothing to do. */   not_ipv6:
495                                                  error = 0;          mutex_lock(&domain_acl_lock);
496                                                  break;          if (is_delete)
497                                          }                  goto delete;
498                                  } else if (record_type == IP_RECORD_TYPE_IPv4) {          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
499                                          if (((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.min == min_ip && max_ip == ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.max) {                  if ((ptr->type & ~(ACL_DELETED | ACL_WITH_CONDITION))
500                                                  ptr->is_deleted = 0;                      != TYPE_IP_NETWORK_ACL)
501                                                  /* Found. Nothing to do. */                          continue;
502                                                  error = 0;                  if (ccs_get_condition_part(ptr) != condition)
503                                                  break;                          continue;
504                                          }                  acl = container_of(ptr, struct ip_network_acl_record, head);
505                                  } else if (record_type == IP_RECORD_TYPE_IPv6) {                  if (acl->operation_type != operation ||
506                                          if (memcmp(((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.min, min_address, 16) == 0 && memcmp(max_address, ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.max, 16) == 0) {                      acl->record_type != record_type ||
507                                                  ptr->is_deleted = 0;                      acl->min_port != min_port || max_port != acl->max_port)
508                                                  /* Found. Nothing to do. */                          continue;
509                                                  error = 0;                  if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {
510                                                  break;                          if (acl->u.group != group)
511                                          }                                  continue;
512                                  }                  } else if (record_type == IP_RECORD_TYPE_IPv4) {
513                          }                          if (acl->u.ipv4.min != min_ip ||
514                          if (ptr->next) {                              max_ip != acl->u.ipv4.max)
515                                  ptr = ptr->next;                                  continue;
516                    } else if (record_type == IP_RECORD_TYPE_IPv6) {
517                            if (acl->u.ipv6.min != saved_min_address ||
518                                saved_max_address != acl->u.ipv6.max)
519                                  continue;                                  continue;
                         }  
                 first_entry: ;  
                         /* Not found. Append it to the tail. */  
                         if ((new_ptr = (IP_NETWORK_ACL_RECORD *) alloc_element(sizeof(IP_NETWORK_ACL_RECORD))) == NULL) break;  
                         new_ptr->head.type = type;  
                         new_ptr->head.u.b[0] = hash;  
                         new_ptr->head.u.b[1] = record_type;  
                         new_ptr->head.cond = condition;  
                         if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                                 new_ptr->u.group = group;  
                         } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                                 new_ptr->u.ipv4.min = min_ip;  
                                 new_ptr->u.ipv4.max = max_ip;  
                         } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                                 memmove(new_ptr->u.ipv6.min, min_address, 16);  
                                 memmove(new_ptr->u.ipv6.max, max_address, 16);  
                         }  
                         new_ptr->min_port = min_port;  
                         new_ptr->max_port = max_port;  
                         error = AddDomainACL(ptr, domain, (struct acl_info *) new_ptr);  
                         break;  
520                  }                  }
521                    error = ccs_add_domain_acl(NULL, ptr);
522                    goto out;
523            }
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                  for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) {                  acl->u.ipv6.max = saved_max_address;
538                          if (ptr->type != type || ptr->is_deleted || ptr->u.b[0] != hash || ptr->u.b[1] != record_type || ptr->cond != condition || ((IP_NETWORK_ACL_RECORD *) ptr)->min_port != min_port || ((IP_NETWORK_ACL_RECORD *) ptr)->max_port != max_port) continue;          }
539                          if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {          acl->min_port = min_port;
540                                  if (((IP_NETWORK_ACL_RECORD *) ptr)->u.group != group) continue;          acl->max_port = max_port;
541                          } else if (record_type == IP_RECORD_TYPE_IPv4) {          error = ccs_add_domain_acl(domain, &acl->head);
542                                  if (((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.min != min_ip || max_ip != ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.max) continue;          goto out;
543                          } else if (record_type == IP_RECORD_TYPE_IPv6) {   delete:
544                                  if (memcmp(((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.min, min_address, 16) || memcmp(max_address, ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.max, 16)) continue;          error = -ENOENT;
545                          }          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
546                          error = DelDomainACL(ptr);                  if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_IP_NETWORK_ACL)
547                          break;                          continue;
548                    if (ccs_get_condition_part(ptr) != condition)
549                            continue;
550                    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          up(&domain_acl_lock);   out:
571            mutex_unlock(&domain_acl_lock);
572          return error;          return error;
573  }  }
574    
575  static int CheckNetworkEntry(const int is_ipv6, const int 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 = network2keyword(operation);          const char *keyword = ccs_net2keyword(operation);
591          const u8 type = TYPE_IP_NETWORK_ACL;          const u8 profile = current->domain_info->profile;
592          const u8 hash = operation;          const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_NETWORK);
593          const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK);          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          if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0;          const u32 ip = ntohl(*address);
596          for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) {          bool found = false;
597                  if (ptr->type != type || ptr->is_deleted || ptr->u.b[0] != hash || port < ((IP_NETWORK_ACL_RECORD *) ptr)->min_port || ((IP_NETWORK_ACL_RECORD *) ptr)->max_port < port || CheckCondition(ptr->cond, NULL)) continue;          if (!mode)
598                  if (ptr->u.b[1] == IP_RECORD_TYPE_ADDRESS_GROUP) {                  return 0;
599                          if (AddressMatchesToGroup(is_ipv6, address, ((IP_NETWORK_ACL_RECORD *) ptr)->u.group)) break;          list1_for_each_entry(ptr, &domain->acl_info_list, list) {
600                  } else if (ptr->u.b[1] == IP_RECORD_TYPE_IPv4) {                  struct ip_network_acl_record *acl;
601                          if (!is_ipv6 && ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.min <= ip && ip <= ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv4.max) break;                  if ((ptr->type & ~ACL_WITH_CONDITION) != TYPE_IP_NETWORK_ACL)
602                            continue;
603                    acl = container_of(ptr, struct ip_network_acl_record, head);
604                    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) {
608                            if (!address_matches_to_group(is_ipv6, address,
609                                                          acl->u.group))
610                                    continue;
611                    } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {
612                            if (is_ipv6 ||
613                                ip < acl->u.ipv4.min || acl->u.ipv4.max < ip)
614                                    continue;
615                  } else {                  } else {
616                          if (is_ipv6 && memcmp(((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.min, address, 16) <= 0 && memcmp(address, ((IP_NETWORK_ACL_RECORD *) ptr)->u.ipv6.max, 16) <= 0) break;                          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                    ccs_update_condition(ptr);
622                    found = true;
623                    break;
624          }          }
625          if (ptr) {          audit_network_log(is_ipv6, keyword, address, port, found, profile,
626                  AuditNetworkLog(is_ipv6, keyword, address, port, 1);                            mode);
627            if (found)
628                  return 0;                  return 0;
629          }          if (ccs_verbose_mode()) {
         if (TomoyoVerboseMode()) {  
630                  if (is_ipv6) {                  if (is_ipv6) {
631                          char buf[64];                          char buf[64];
632                          print_ipv6(buf, sizeof(buf), (const u16 *) 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          }          }
         AuditNetworkLog(is_ipv6, keyword, address, port, 0);  
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 u16 *) 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          if (CheckCCSAccept(CCS_TOMOYO_MAC_FOR_NETWORK)) AddNetworkEntry(operation, is_ipv6 ? IP_RECORD_TYPE_IPv6: IP_RECORD_TYPE_IPv4, NULL, address, address, port, port, domain, 0, NULL);                                                      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 int 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];
683          struct address_group_entry *group = NULL;          struct address_group_entry *group = NULL;
684          u16 min_port, max_port;          u16 min_port, max_port;
685          int count;          u8 count;
686          char *cp1 = NULL, *cp2 = NULL;          char *cp1 = NULL, *cp2 = NULL;
687          const struct condition_list *condition = NULL;          cp1 = strchr(data, ' ');
688          cp1 = FindConditionPart(data);          if (!cp1)
689          if (cp1 && (condition = FindOrAssignNewCondition(cp1)) == NULL) goto out;                  goto out;
690          if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++;          cp1++;
691          if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM;          if (!strncmp(data, "TCP ", 4))
692          else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM;                  sock_type = SOCK_STREAM;
693          else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW;          else if (!strncmp(data, "UDP ", 4))
694          else goto out;                  sock_type = SOCK_DGRAM;
695          if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++;          else if (!strncmp(data, "RAW ", 4))
696          if (strncmp(cp1, "bind ", 5) == 0) {                  sock_type = SOCK_RAW;
697                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND;          else
698          } else if (strncmp(cp1, "connect ", 8) == 0) {                  goto out;
699                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;          cp2 = strchr(cp1, ' ');
700          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) {          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                  int i;                         &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;
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, is_delete, condition);          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 int 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 int 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 int 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 int 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          return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port));          int retval;
883            current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
884            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;
887            return retval;
888  }  }
889    
890  int CheckNetworkSendMsgACL(const int 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 int 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          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          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;
930            retval = check_network_entry(is_ipv6, operation, (const u32 *) address,
931                                         ntohs(port));
932            current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
933            return retval;
934  }  }
   
 EXPORT_SYMBOL(CheckNetworkListenACL);  
 EXPORT_SYMBOL(CheckNetworkConnectACL);  
 EXPORT_SYMBOL(CheckNetworkBindACL);  
 EXPORT_SYMBOL(CheckNetworkAcceptACL);  
 EXPORT_SYMBOL(CheckNetworkSendMsgACL);  
 EXPORT_SYMBOL(CheckNetworkRecvMsgACL);  
   
 /***** TOMOYO Linux end. *****/  

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

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