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

Subversion リポジトリの参照

Diff of /branches/ccs-patch/security/ccsecurity/network.c

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

revision 712 by kumaneko, Mon Nov 19 12:08:17 2007 UTC revision 719 by kumaneko, Wed Nov 21 04:01:53 2007 UTC
# Line 32  static int AuditNetworkLog(const bool is Line 32  static int AuditNetworkLog(const bool is
32          if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM;          if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM;
33          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);
34          if (is_ipv6) {          if (is_ipv6) {
35                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const u16 *) address);                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address);
36          } else {          } else {
37                  u32 ip = *address;                  u32 ip = *address;
38                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));
# Line 41  static int AuditNetworkLog(const bool is Line 41  static int AuditNetworkLog(const bool is
41          return WriteAuditLog(buf, is_granted);          return WriteAuditLog(buf, is_granted);
42  }  }
43    
44    /*************************  UTILITY FUNCTIONS  *************************/
45    
46    /* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */
47    static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr)
48    {
49            static const int block_size = 16;
50            struct addr_list {
51                    struct in6_addr addr[block_size];
52                    struct addr_list *next;
53                    u32 in_use_count;
54            };
55            static struct addr_list *list = NULL;
56            struct addr_list *ptr;
57            static DEFINE_MUTEX(lock);
58            int i = block_size;
59            if (!addr) return NULL;
60            mutex_lock(&lock);
61            for (ptr = list; ptr; ptr = ptr->next) {
62                    for (i = 0; i < ptr->in_use_count; i++) {
63                            if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0) goto ok;
64                    }
65                    if (i < block_size) break;
66            }
67            if (i == block_size && (ptr = alloc_element(sizeof(*ptr))) != NULL) {
68                    struct addr_list *p = list;
69                    if (p) {
70                            while (p->next) p = p->next;
71                            p->next = ptr;
72                    } else {
73                            list = ptr;
74                    }
75                    i = 0;
76            }
77            if (ptr) ptr->addr[ptr->in_use_count++] = *addr;
78    ok:
79            mutex_unlock(&lock);
80            return ptr ? &ptr->addr[i] : NULL;
81    }
82    
83  /*************************  ADDRESS GROUP HANDLER  *************************/  /*************************  ADDRESS GROUP HANDLER  *************************/
84    
85  static LIST_HEAD(address_group_list);  static LIST_HEAD(address_group_list);
# Line 51  static int AddAddressGroupEntry(const ch Line 90  static int AddAddressGroupEntry(const ch
90          struct address_group_entry *new_group, *group;          struct address_group_entry *new_group, *group;
91          struct address_group_member *new_member, *member;          struct address_group_member *new_member, *member;
92          const struct path_info *saved_group_name;          const struct path_info *saved_group_name;
93            const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;
94          int error = -ENOMEM;          int error = -ENOMEM;
95          bool found = 0;          bool found = 0;
96          if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL;          if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL;
97          if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM;          if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM;
98            if (is_ipv6) {
99                    if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL
100                        || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address))) return -ENOMEM;
101            }
102          mutex_lock(&lock);          mutex_lock(&lock);
103          list_for_each_entry(group, &address_group_list, list) {          list_for_each_entry(group, &address_group_list, list) {
104                  if (saved_group_name != group->group_name) continue;                  if (saved_group_name != group->group_name) continue;
105                  list_for_each_entry(member, &group->address_group_member_list, list) {                  list_for_each_entry(member, &group->address_group_member_list, list) {
106                          if (member->is_ipv6 != is_ipv6) continue;                          if (member->is_ipv6 != is_ipv6) continue;
107                          if (is_ipv6) {                          if (is_ipv6) {
108                                  if (memcmp(member->min.ipv6, min_address, 16) || memcmp(member->max.ipv6, max_address, 16)) continue;                                  if (member->min.ipv6 != saved_min_address || member->max.ipv6 != saved_max_address) continue;
109                          } else {                          } else {
110                                  if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue;                                  if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue;
111                          }                          }
# Line 86  static int AddAddressGroupEntry(const ch Line 130  static int AddAddressGroupEntry(const ch
130          if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out;          if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out;
131          new_member->is_ipv6 = is_ipv6;          new_member->is_ipv6 = is_ipv6;
132          if (is_ipv6) {          if (is_ipv6) {
133                  memmove(new_member->min.ipv6, min_address, 16);                  new_member->min.ipv6 = saved_min_address;
134                  memmove(new_member->max.ipv6, max_address, 16);                  new_member->max.ipv6 = saved_max_address;
135          } else {          } else {
136                  new_member->min.ipv4 = * (u32 *) min_address;                  new_member->min.ipv4 = * (u32 *) min_address;
137                  new_member->max.ipv4 = * (u32 *) max_address;                  new_member->max.ipv4 = * (u32 *) max_address;
# Line 107  int AddAddressGroupPolicy(char *data, co Line 151  int AddAddressGroupPolicy(char *data, co
151          if (!cp) return -EINVAL;          if (!cp) return -EINVAL;
152          *cp++ = '\0';          *cp++ = '\0';
153          if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",          if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
154                                                  &min_address[0], &min_address[1], &min_address[2], &min_address[3],                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],
155                                                  &min_address[4], &min_address[5], &min_address[6], &min_address[7],                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],
156                                                  &max_address[0], &max_address[1], &max_address[2], &max_address[3],                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],
157                                                  &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {
158                  int i;                  int i;
159                  for (i = 0; i < 8; i++) {                  for (i = 0; i < 8; i++) {
160                          min_address[i] = htons(min_address[i]);                          min_address[i] = htons(min_address[i]);
# Line 119  int AddAddressGroupPolicy(char *data, co Line 163  int AddAddressGroupPolicy(char *data, co
163                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));
164                  is_ipv6 = 1;                  is_ipv6 = 1;
165          } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
166                                                             &min_address[0], &min_address[1], &min_address[2], &min_address[3],                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],
167                                                             &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {
168                  u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);                  u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);
169                  * (u32 *) min_address = ip;                  * (u32 *) min_address = ip;
170                  if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);                  if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);
# Line 177  int ReadAddressGroupPolicy(struct io_buf Line 221  int ReadAddressGroupPolicy(struct io_buf
221                          member = list_entry(mpos, struct address_group_member, list);                          member = list_entry(mpos, struct address_group_member, list);
222                          if (member->is_deleted) continue;                          if (member->is_deleted) continue;
223                          if (member->is_ipv6) {                          if (member->is_ipv6) {
224                                  const u16 *min_address = member->min.ipv6, *max_address = member->max.ipv6;                                  const struct in6_addr *min_address = member->min.ipv6, *max_address = member->max.ipv6;
225                                  print_ipv6(buf, sizeof(buf), min_address);                                  print_ipv6(buf, sizeof(buf), min_address);
226                                  if (memcmp(min_address, max_address, 16)) {                                  if (min_address != max_address) {
227                                          char *cp = strchr(buf, '\0');                                          char *cp = strchr(buf, '\0');
228                                          *cp++ = '-';                                          *cp++ = '-';
229                                          print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);                                          print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);
# Line 201  int ReadAddressGroupPolicy(struct io_buf Line 245  int ReadAddressGroupPolicy(struct io_buf
245    
246  /*************************  NETWORK NETWORK ACL HANDLER  *************************/  /*************************  NETWORK NETWORK ACL HANDLER  *************************/
247    
248  char *print_ipv6(char *buffer, const int buffer_len, const u16 *ip)  #if !defined(NIP6)
249    #define NIP6(addr) \
250            ntohs((addr).s6_addr16[0]), \
251            ntohs((addr).s6_addr16[1]), \
252            ntohs((addr).s6_addr16[2]), \
253            ntohs((addr).s6_addr16[3]), \
254            ntohs((addr).s6_addr16[4]), \
255            ntohs((addr).s6_addr16[5]), \
256            ntohs((addr).s6_addr16[6]), \
257            ntohs((addr).s6_addr16[7])
258    #endif
259    
260    char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)
261  {  {
262          memset(buffer, 0, buffer_len);          memset(buffer, 0, buffer_len);
263          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));
264          return buffer;          return buffer;
265  }  }
266    
# Line 246  static int AddNetworkEntry(const u8 oper Line 302  static int AddNetworkEntry(const u8 oper
302          struct ip_network_acl_record *acl;          struct ip_network_acl_record *acl;
303          int error = -ENOMEM;          int error = -ENOMEM;
304          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/          const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/
305            const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;
306          if (!domain) return -EINVAL;          if (!domain) return -EINVAL;
307            if (record_type == IP_RECORD_TYPE_IPv6) {
308                    if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL
309                        || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address))) return -ENOMEM;
310            }
311          mutex_lock(&domain_acl_lock);          mutex_lock(&domain_acl_lock);
312          if (!is_delete) {          if (!is_delete) {
313                  list_for_each_entry(ptr, &domain->acl_info_list, list) {                  list_for_each_entry(ptr, &domain->acl_info_list, list) {
# Line 267  static int AddNetworkEntry(const u8 oper Line 328  static int AddNetworkEntry(const u8 oper
328                                                  goto out;                                                  goto out;
329                                          }                                          }
330                                  } else if (record_type == IP_RECORD_TYPE_IPv6) {                                  } else if (record_type == IP_RECORD_TYPE_IPv6) {
331                                          if (memcmp(acl->u.ipv6.min, min_address, 16) == 0 && memcmp(max_address, acl->u.ipv6.max, 16) == 0) {                                          if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) {
332                                                  ptr->is_deleted = 0;                                                  ptr->is_deleted = 0;
333                                                  /* Found. Nothing to do. */                                                  /* Found. Nothing to do. */
334                                                  error = 0;                                                  error = 0;
# Line 288  static int AddNetworkEntry(const u8 oper Line 349  static int AddNetworkEntry(const u8 oper
349                          acl->u.ipv4.min = min_ip;                          acl->u.ipv4.min = min_ip;
350                          acl->u.ipv4.max = max_ip;                          acl->u.ipv4.max = max_ip;
351                  } else {                  } else {
352                          memmove(acl->u.ipv6.min, min_address, 16);                          acl->u.ipv6.min = saved_min_address;
353                          memmove(acl->u.ipv6.max, max_address, 16);                          acl->u.ipv6.max = saved_max_address;
354                  }                  }
355                  acl->min_port = min_port;                  acl->min_port = min_port;
356                  acl->max_port = max_port;                  acl->max_port = max_port;
# Line 304  static int AddNetworkEntry(const u8 oper Line 365  static int AddNetworkEntry(const u8 oper
365                          } else if (record_type == IP_RECORD_TYPE_IPv4) {                          } else if (record_type == IP_RECORD_TYPE_IPv4) {
366                                  if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue;                                  if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue;
367                          } else if (record_type == IP_RECORD_TYPE_IPv6) {                          } else if (record_type == IP_RECORD_TYPE_IPv6) {
368                                  if (memcmp(acl->u.ipv6.min, min_address, 16) || memcmp(max_address, acl->u.ipv6.max, 16)) continue;                                  if (acl->u.ipv6.min != saved_min_address || saved_max_address != acl->u.ipv6.max) continue;
369                          }                          }
370                          error = DelDomainACL(ptr);                          error = DelDomainACL(ptr);
371                          break;                          break;
# Line 344  static int CheckNetworkEntry(const bool Line 405  static int CheckNetworkEntry(const bool
405          if (TomoyoVerboseMode()) {          if (TomoyoVerboseMode()) {
406                  if (is_ipv6) {                  if (is_ipv6) {
407                          char buf[64];                          char buf[64];
408                          print_ipv6(buf, sizeof(buf), (const u16 *) address);                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);
409                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));
410                  } else {                  } else {
411                          printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain));                          printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain));
# Line 354  static int CheckNetworkEntry(const bool Line 415  static int CheckNetworkEntry(const bool
415          if (is_enforce) {          if (is_enforce) {
416                  if (is_ipv6) {                  if (is_ipv6) {
417                          char buf[64];                          char buf[64];
418                          print_ipv6(buf, sizeof(buf), (const u16 *) address);                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);
419                          return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);                          return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);
420                  }                  }
421                  return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);                  return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);
# Line 390  int AddNetworkPolicy(char *data, struct Line 451  int AddNetworkPolicy(char *data, struct
451          }          }
452          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';
453          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
454                                                  &min_address[0], &min_address[1], &min_address[2], &min_address[3],                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],
455                                                  &min_address[4], &min_address[5], &min_address[6], &min_address[7],                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],
456                                                  &max_address[0], &max_address[1], &max_address[2], &max_address[3],                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],
457                                                  &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {
458                  int i;                  int i;
459                  for (i = 0; i < 8; i++) {                  for (i = 0; i < 8; i++) {
460                          min_address[i] = htons(min_address[i]);                          min_address[i] = htons(min_address[i]);
# Line 402  int AddNetworkPolicy(char *data, struct Line 463  int AddNetworkPolicy(char *data, struct
463                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));
464                  record_type = IP_RECORD_TYPE_IPv6;                  record_type = IP_RECORD_TYPE_IPv6;
465          } else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",          } else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
466                                                             &min_address[0], &min_address[1], &min_address[2], &min_address[3],                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],
467                                                             &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {
468                  u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);                  u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);
469                  * (u32 *) min_address = ip;                  * (u32 *) min_address = ip;
470                  if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);                  if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);

Legend:
Removed from v.712  
changed lines
  Added in v.719

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