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

Subversion リポジトリの参照

Diff of /trunk/1.8.x/ccs-patch/security/ccsecurity/network.c

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

trunk/1.5.x/ccs-patch/fs/tomoyo_network.c revision 776 by kumaneko, Wed Dec 5 05:29:11 2007 UTC trunk/1.8.x/ccs-patch/security/ccsecurity/network.c revision 3994 by kumaneko, Sat Sep 18 13:23:06 2010 UTC
# Line 1  Line 1 
1  /*  /*
2   * fs/tomoyo_network.c   * security/ccsecurity/network.c
3   *   *
4   * Implementation of the Domain-Based Mandatory Access Control.   * Copyright (C) 2005-2010  NTT DATA CORPORATION
5   *   *
6   * Copyright (C) 2005-2007  NTT DATA CORPORATION   * Version: 1.8.0-pre   2010/09/01
  *  
  * Version: 1.5.3-pre   2007/12/03  
7   *   *
8   * 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.
9   * See README.ccs for ChangeLog.   * See README.ccs for ChangeLog.
10   *   *
11   */   */
 /***** TOMOYO Linux start. *****/  
12    
13  #include <linux/ccs_common.h>  #include <linux/net.h>
14  #include <linux/tomoyo.h>  #include <linux/inet.h>
15  #include <linux/realpath.h>  #include <linux/in.h>
16    #include <linux/in6.h>
17    #include <linux/un.h>
18    #include <net/sock.h>
19    #include <net/af_unix.h>
20  #include <net/ip.h>  #include <net/ip.h>
21    #include <net/ipv6.h>
22    #include <net/udp.h>
23    #include "internal.h"
24    
25    struct ccs_inet_addr_info {
26            u16 port;           /* In network byte order. */
27            const u32 *address; /* In network byte order. */
28            bool is_ipv6;
29    };
30    
31    struct ccs_unix_addr_info {
32            u8 *addr;
33            unsigned int addr_len;
34    };
35    
36    struct ccs_addr_info {
37            u8 protocol;
38            u8 operation;
39            struct ccs_inet_addr_info inet;
40            struct ccs_unix_addr_info unix0;
41    };
42    
43    const char * const ccs_proto_keyword[CCS_SOCK_MAX] = {
44            [SOCK_STREAM]    = "stream",
45            [SOCK_DGRAM]     = "dgram",
46            [SOCK_RAW]       = "raw",
47            [SOCK_SEQPACKET] = "seqpacket",
48            [0] = " ",
49            [4] = " ",
50    };
51    
52    const char * const ccs_socket_keyword[CCS_MAX_NETWORK_OPERATION] = {
53            [CCS_NETWORK_BIND]    = "bind",
54            [CCS_NETWORK_LISTEN]  = "listen",
55            [CCS_NETWORK_CONNECT] = "connect",
56            [CCS_NETWORK_ACCEPT]  = "accept",
57            [CCS_NETWORK_SEND]    = "send",
58            [CCS_NETWORK_RECV]    = "recv",
59    };
60    
61    static int ccs_audit_net_log(struct ccs_request_info *r, const char *family,
62                                 const u8 proto, const u8 ope, const char *address)
63    {
64            const char *protocol = ccs_proto_keyword[proto];
65            const char *operation = ccs_socket_keyword[ope];
66            ccs_write_log(r, "network %s %s %s %s\n", family,
67                          protocol, operation, address);
68            if (r->granted)
69                    return 0;
70            ccs_warn_log(r, "network %s %s %s %s\n", family, protocol, operation,
71                         address);
72            return ccs_supervisor(r, "network %s %s %s %s\n", family, protocol,
73                                  operation, address);
74    }
75    
76  /*************************  VARIABLES  *************************/  /**
77     * ccs_audit_inet_log - Audit INET network log.
78  extern struct mutex domain_acl_lock;   *
79     * @r: Pointer to "struct ccs_request_info".
80     *
81     * Returns 0 on success, negative value otherwise.
82     */
83    static int ccs_audit_inet_log(struct ccs_request_info *r)
84    {
85            char buf[128];
86            int len;
87            const u32 *address = r->param.inet_network.address;
88            if (r->param.inet_network.is_ipv6)
89                    ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *)
90                                   address, (const struct in6_addr *) address);
91            else
92                    ccs_print_ipv4(buf, sizeof(buf), r->param.inet_network.ip,
93                                   r->param.inet_network.ip);
94            len = strlen(buf);
95            snprintf(buf + len, sizeof(buf) - len, " %u",
96                     r->param.inet_network.port);
97            return ccs_audit_net_log(r, "inet", r->param.inet_network.protocol,
98                                     r->param.inet_network.operation, buf);
99    }
100    
101  /*************************  AUDIT FUNCTIONS  *************************/  /**
102     * ccs_audit_unix_log - Audit UNIX network log.
103     *
104     * @r: Pointer to "struct ccs_request_info".
105     *
106     * Returns 0 on success, negative value otherwise.
107     */
108    static int ccs_audit_unix_log(struct ccs_request_info *r)
109    {
110            return ccs_audit_net_log(r, "unix", r->param.unix_network.protocol,
111                                     r->param.unix_network.operation,
112                                     r->param.unix_network.address->name);
113    }
114    
115  static int AuditNetworkLog(const bool is_ipv6, const char *operation, const u32 *address, const u16 port, const bool is_granted)  /**
116     * ccs_parse_ip_address - Parse an IP address.
117     *
118     * @address: String to parse.
119     * @min:     Pointer to store min address.
120     * @max:     Pointer to store max address.
121     *
122     * Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6,
123     * CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4,
124     * CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise.
125     */
126    int ccs_parse_ip_address(char *address, u16 *min, u16 *max)
127  {  {
128          char *buf;          int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"
129          int len = 256;                             "-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
130          if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM;                             &min[0], &min[1], &min[2], &min[3],
131          if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM;                             &min[4], &min[5], &min[6], &min[7],
132          snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation);                             &max[0], &max[1], &max[2], &max[3],
133          if (is_ipv6) {                             &max[4], &max[5], &max[6], &max[7]);
134                  print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address);          if (count == 8 || count == 16) {
135          } else {                  u8 i;
136                  u32 ip = *address;                  if (count == 8)
137                  snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip));                          memmove(max, min, sizeof(u16) * 8);
138          }                  for (i = 0; i < 8; i++) {
139          snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port);                          min[i] = htons(min[i]);
140          return WriteAuditLog(buf, is_granted);                          max[i] = htons(max[i]);
 }  
   
 /*************************  UTILITY FUNCTIONS  *************************/  
   
 /* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */  
 static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr)  
 {  
         static const int block_size = 16;  
         struct addr_list {  
                 struct in6_addr addr[block_size];  
                 struct list1_head list;  
                 u32 in_use_count;  
         };  
         static LIST1_HEAD(address_list);  
         struct addr_list *ptr;  
         static DEFINE_MUTEX(lock);  
         int i = block_size;  
         if (!addr) return NULL;  
         mutex_lock(&lock);  
         list1_for_each_entry(ptr, &address_list, list) {  
                 for (i = 0; i < ptr->in_use_count; i++) {  
                         if (memcmp(&ptr->addr[i], addr, sizeof(*addr)) == 0) goto ok;  
                 }  
                 if (i < block_size) break;  
         }  
         if (i == block_size) {  
                 ptr = alloc_element(sizeof(*ptr));  
                 if (!ptr) goto ok;  
                 list1_add_tail_mb(&ptr->list, &address_list);  
                 i = 0;  
         }  
         ptr->addr[ptr->in_use_count++] = *addr;  
 ok:  
         mutex_unlock(&lock);  
         return ptr ? &ptr->addr[i] : NULL;  
 }  
   
 /*************************  ADDRESS GROUP HANDLER  *************************/  
   
 static LIST1_HEAD(address_group_list);  
   
 static int AddAddressGroupEntry(const char *group_name, const bool is_ipv6, const u16 *min_address, const u16 *max_address, const bool is_delete)  
 {  
         static DEFINE_MUTEX(lock);  
         struct address_group_entry *new_group, *group;  
         struct address_group_member *new_member, *member;  
         const struct path_info *saved_group_name;  
         const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;  
         int error = -ENOMEM;  
         bool found = 0;  
         if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL;  
         if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM;  
         if (is_ipv6) {  
                 if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL  
                     || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;  
         }  
         mutex_lock(&lock);  
         list1_for_each_entry(group, &address_group_list, list) {  
                 if (saved_group_name != group->group_name) continue;  
                 list1_for_each_entry(member, &group->address_group_member_list, list) {  
                         if (member->is_ipv6 != is_ipv6) continue;  
                         if (is_ipv6) {  
                                 if (member->min.ipv6 != saved_min_address || member->max.ipv6 != saved_max_address) continue;  
                         } else {  
                                 if (member->min.ipv4 != * (u32 *) min_address || member->max.ipv4 != * (u32 *) max_address) continue;  
                         }  
                         member->is_deleted = is_delete;  
                         error = 0;  
                         goto out;  
141                  }                  }
142                  found = 1;                  return CCS_IP_ADDRESS_TYPE_IPv6;
                 break;  
         }  
         if (is_delete) {  
                 error = -ENOENT;  
                 goto out;  
143          }          }
144          if (!found) {          count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",
145                  if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out;                         &min[0], &min[1], &min[2], &min[3],
146                  INIT_LIST1_HEAD(&new_group->address_group_member_list);                         &max[0], &max[1], &max[2], &max[3]);
147                  new_group->group_name = saved_group_name;          if (count == 4 || count == 8) {
148                  list1_add_tail_mb(&new_group->list, &address_group_list);                  u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16)
149                  group = new_group;                                 + (((u8) min[2]) << 8) + (u8) min[3]);
150          }                  memmove(min, &ip, sizeof(ip));
151          if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out;                  if (count == 8)
152          new_member->is_ipv6 = is_ipv6;                          ip = htonl((((u8) max[0]) << 24)
153          if (is_ipv6) {                                     + (((u8) max[1]) << 16)
154                  new_member->min.ipv6 = saved_min_address;                                     + (((u8) max[2]) << 8) + (u8) max[3]);
155                  new_member->max.ipv6 = saved_max_address;                  memmove(max, &ip, sizeof(ip));
156          } else {                  return CCS_IP_ADDRESS_TYPE_IPv4;
                 new_member->min.ipv4 = * (u32 *) min_address;  
                 new_member->max.ipv4 = * (u32 *) max_address;  
157          }          }
158          list1_add_tail_mb(&new_member->list, &group->address_group_member_list);          return CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
         error = 0;  
  out:  
         mutex_unlock(&lock);  
         return error;  
159  }  }
160    
161  int AddAddressGroupPolicy(char *data, const bool is_delete)  /**
162     * ccs_print_ipv4 - Print an IPv4 address.
163     *
164     * @buffer:     Buffer to write to.
165     * @buffer_len: Size of @buffer.
166     * @min_ip:     Min address in host byte order.
167     * @max_ip:     Max address in host byte order.
168     *
169     * Returns nothing.
170     */
171    void ccs_print_ipv4(char *buffer, const int buffer_len,
172                        const u32 min_ip, const u32 max_ip)
173  {  {
174          int count, is_ipv6;          memset(buffer, 0, buffer_len);
175          u16 min_address[8], max_address[8];          snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u",
176          char *cp = strchr(data, ' ');                   HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-',
177          if (!cp) return -EINVAL;                   HIPQUAD(max_ip));
         *cp++ = '\0';  
         if ((count = sscanf(cp, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",  
                             &min_address[0], &min_address[1], &min_address[2], &min_address[3],  
                             &min_address[4], &min_address[5], &min_address[6], &min_address[7],  
                             &max_address[0], &max_address[1], &max_address[2], &max_address[3],  
                             &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {  
                 int i;  
                 for (i = 0; i < 8; i++) {  
                         min_address[i] = htons(min_address[i]);  
                         max_address[i] = htons(max_address[i]);  
                 }  
                 if (count == 8) memmove(max_address, min_address, sizeof(min_address));  
                 is_ipv6 = 1;  
         } else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",  
                                    &min_address[0], &min_address[1], &min_address[2], &min_address[3],  
                                    &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {  
                 u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);  
                 * (u32 *) min_address = ip;  
                 if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);  
                 * (u32 *) max_address = ip;  
                 is_ipv6 = 0;  
         } else {  
                 return -EINVAL;  
         }  
         return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete);  
178  }  }
179    
180  static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name)  #if !defined(NIP6)
181    #define NIP6(addr)                                                      \
182            ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]),         \
183                    ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \
184                    ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \
185                    ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7])
186    #endif
187    
188    /**
189     * ccs_print_ipv6 - Print an IPv6 address.
190     *
191     * @buffer:     Buffer to write to.
192     * @buffer_len: Size of @buffer.
193     * @min_ip:     Pointer to "struct in6_addr".
194     * @max_ip:     Pointer to "struct in6_addr".
195     *
196     * Returns nothing.
197     */
198    void ccs_print_ipv6(char *buffer, const int buffer_len,
199                        const struct in6_addr *min_ip,
200                        const struct in6_addr *max_ip)
201  {  {
202          int i;          memset(buffer, 0, buffer_len);
203          struct address_group_entry *group;          snprintf(buffer, buffer_len - 1,
204          for (i = 0; i <= 1; i++) {                   "%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x",
205                  list1_for_each_entry(group, &address_group_list, list) {                   NIP6(*min_ip), min_ip == max_ip ? '\0' : '-',
206                          if (strcmp(group_name, group->group_name->name) == 0) return group;                   NIP6(*max_ip));
207                  }  }
208                  if (i == 0) {  
209                          const u16 dummy[2] = { 0, 0 };  static bool ccs_check_inet_acl(struct ccs_request_info *r,
210                          AddAddressGroupEntry(group_name, 0, dummy, dummy, 0);                                 const struct ccs_acl_info *ptr)
211                          AddAddressGroupEntry(group_name, 0, dummy, dummy, 1);  {
212                  }          const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
213            bool ret;
214            if (!(acl->perm & (1 << r->param.inet_network.operation)) ||
215                !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
216                    return false;
217            switch (acl->address_type) {
218            case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP:
219                    ret = ccs_address_matches_group(r->param.inet_network.is_ipv6,
220                                                    r->param.inet_network.address,
221                                                    acl->address.group);
222                    break;
223            case CCS_IP_ADDRESS_TYPE_IPv4:
224                    ret = !r->param.inet_network.is_ipv6 &&
225                            acl->address.ipv4.min <= r->param.inet_network.ip &&
226                            r->param.inet_network.ip <= acl->address.ipv4.max;
227                    break;
228            default:
229                    ret = r->param.inet_network.is_ipv6 &&
230                            memcmp(acl->address.ipv6.min,
231                                   r->param.inet_network.address, 16) <= 0 &&
232                            memcmp(r->param.inet_network.address,
233                                   acl->address.ipv6.max, 16) <= 0;
234                    break;
235          }          }
236          return NULL;          return ret;
237  }  }
238    
239  static int AddressMatchesToGroup(const bool is_ipv6, const u32 *address, const struct address_group_entry *group)  static bool ccs_check_unix_acl(struct ccs_request_info *r,
240                                   const struct ccs_acl_info *ptr)
241  {  {
242          struct address_group_member *member;          const struct ccs_unix_acl *acl = container_of(ptr, typeof(*acl), head);
243          const u32 ip = ntohl(*address);          return (acl->perm & (1 << r->param.unix_network.operation)) &&
244          list1_for_each_entry(member, &group->address_group_member_list, list) {                  ccs_compare_name_union(r->param.unix_network.address,
245                  if (member->is_deleted) continue;                                         &acl->name);
246                  if (member->is_ipv6) {  }
247                          if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1;  
248                  } else {  static const u8
249                          if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1;  ccs_inet2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = {
250                  }          [SOCK_STREAM] = {
251          }                  [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_STREAM_BIND,
252          return 0;                  [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_INET_STREAM_LISTEN,
253                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_INET_STREAM_CONNECT,
254                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_INET_STREAM_ACCEPT,
255            },
256            [SOCK_DGRAM] = {
257                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_DGRAM_BIND,
258                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_DGRAM_SEND,
259                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_DGRAM_RECV,
260            },
261            [SOCK_RAW]    = {
262                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_RAW_BIND,
263                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_RAW_SEND,
264                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_RAW_RECV,
265            },
266    };
267    
268    static const u8
269    ccs_unix2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = {
270            [SOCK_STREAM] = {
271                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_STREAM_BIND,
272                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_STREAM_LISTEN,
273                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_STREAM_CONNECT,
274                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT,
275            },
276            [SOCK_DGRAM] = {
277                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_DGRAM_BIND,
278                    [CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_UNIX_DGRAM_SEND,
279                    [CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_UNIX_DGRAM_RECV,
280            },
281            [SOCK_SEQPACKET] = {
282                    [CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND,
283                    [CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
284                    [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
285                    [CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT,
286            },
287    };
288    
289    static bool ccs_same_inet_acl(const struct ccs_acl_info *a,
290                                          const struct ccs_acl_info *b)
291    {
292            const struct ccs_inet_acl *p1 = container_of(a, typeof(*p1), head);
293            const struct ccs_inet_acl *p2 = container_of(b, typeof(*p2), head);
294            return p1->protocol == p2->protocol &&
295                    p1->address_type == p2->address_type &&
296                    p1->address.ipv4.min == p2->address.ipv4.min &&
297                    p1->address.ipv6.min == p2->address.ipv6.min &&
298                    p1->address.ipv4.max == p2->address.ipv4.max &&
299                    p1->address.ipv6.max == p2->address.ipv6.max &&
300                    p1->address.group == p2->address.group &&
301                    ccs_same_number_union(&p1->port, &p2->port);
302    }
303    
304    static bool ccs_same_unix_acl(const struct ccs_acl_info *a,
305                                          const struct ccs_acl_info *b)
306    {
307            const struct ccs_unix_acl *p1 = container_of(a, typeof(*p1), head);
308            const struct ccs_unix_acl *p2 = container_of(b, typeof(*p2), head);
309            return p1->protocol == p2->protocol &&
310                    ccs_same_name_union(&p1->name, &p2->name);
311    }
312    
313    static bool ccs_merge_inet_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
314                                   const bool is_delete)
315    {
316            u8 * const a_perm = &container_of(a, struct ccs_inet_acl, head)->perm;
317            u8 perm = *a_perm;
318            const u8 b_perm = container_of(b, struct ccs_inet_acl, head)->perm;
319            if (is_delete)
320                    perm &= ~b_perm;
321            else
322                    perm |= b_perm;
323            *a_perm = perm;
324            return !perm;
325    }
326    
327    static bool ccs_merge_unix_acl(struct ccs_acl_info *a, struct ccs_acl_info *b,
328                                   const bool is_delete)
329    {
330            u8 * const a_perm = &container_of(a, struct ccs_unix_acl, head)->perm;
331            u8 perm = *a_perm;
332            const u8 b_perm = container_of(b, struct ccs_unix_acl, head)->perm;
333            if (is_delete)
334                    perm &= ~b_perm;
335            else
336                    perm |= b_perm;
337            *a_perm = perm;
338            return !perm;
339  }  }
340    
341  int ReadAddressGroupPolicy(struct io_buffer *head)  /**
342     * ccs_write_inet_network - Write "struct ccs_inet_acl" list.
343     *
344     * @param: Pointer to "struct ccs_acl_param".
345     *
346     * Returns 0 on success, negative value otherwise.
347     */
348    int ccs_write_inet_network(struct ccs_acl_param *param)
349  {  {
350          struct list1_head *gpos;          struct ccs_inet_acl e = { .head.type = CCS_TYPE_INET_ACL };
351          struct list1_head *mpos;          u16 min_address[8];
352          list1_for_each_cookie(gpos, head->read_var1, &address_group_list) {          u16 max_address[8];
353                  struct address_group_entry *group;          int error = -EINVAL;
354                  group = list1_entry(gpos, struct address_group_entry, list);          u8 type;
355                  list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) {          const char *protocol = ccs_read_token(param);
356                          char buf[128];          const char *operation = ccs_read_token(param);
357                          struct address_group_member *member;          char *address = ccs_read_token(param);
358                          member = list1_entry(mpos, struct address_group_member, list);          for (e.protocol = 0; e.protocol < CCS_SOCK_MAX; e.protocol++)
359                          if (member->is_deleted) continue;                  if (!strcmp(protocol, ccs_proto_keyword[e.protocol]))
360                          if (member->is_ipv6) {                          break;
361                                  const struct in6_addr *min_address = member->min.ipv6, *max_address = member->max.ipv6;          for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
362                                  print_ipv6(buf, sizeof(buf), min_address);                  if (ccs_permstr(operation, ccs_socket_keyword[type]))
363                                  if (min_address != max_address) {                          e.perm |= 1 << type;
364                                          char *cp = strchr(buf, '\0');          if (e.protocol == CCS_SOCK_MAX || !e.perm)
365                                          *cp++ = '-';                  return -EINVAL;
366                                          print_ipv6(cp, sizeof(buf) - strlen(buf), max_address);          switch (ccs_parse_ip_address(address, min_address, max_address)) {
367                                  }          case CCS_IP_ADDRESS_TYPE_IPv6:
368                          } else {                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv6;
369                                  const u32 min_address = member->min.ipv4, max_address = member->max.ipv4;                  e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *)
370                                  memset(buf, 0, sizeof(buf));                                                              min_address);
371                                  snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address));                  e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *)
372                                  if (min_address != max_address) {                                                              max_address);
373                                          const int len = strlen(buf);                  if (!e.address.ipv6.min || !e.address.ipv6.max)
374                                          snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address));                          goto out;
375                                  }                  break;
376                          }          case CCS_IP_ADDRESS_TYPE_IPv4:
377                          if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) return -ENOMEM;                  e.address_type = CCS_IP_ADDRESS_TYPE_IPv4;
378                  }                  /* use host byte order to allow u32 comparison.*/
379                    e.address.ipv4.min = ntohl(*(u32 *) min_address);
380                    e.address.ipv4.max = ntohl(*(u32 *) max_address);
381                    break;
382            default:
383                    if (address[0] != '@')
384                            return -EINVAL;
385                    e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP;
386                    e.address.group = ccs_get_group(address + 1,
387                                                    CCS_ADDRESS_GROUP);
388                    if (!e.address.group)
389                            return -ENOMEM;
390                    break;
391          }          }
392          return 0;          if (!ccs_parse_number_union(ccs_read_token(param), &e.port))
393                    goto out;
394            error = ccs_update_domain(&e.head, sizeof(e), param, ccs_same_inet_acl,
395                                      ccs_merge_inet_acl);
396     out:
397            if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP)
398                    ccs_put_group(e.address.group);
399            else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) {
400                    ccs_put_ipv6_address(e.address.ipv6.min);
401                    ccs_put_ipv6_address(e.address.ipv6.max);
402            }
403            ccs_put_number_union(&e.port);
404            return error;
405  }  }
406    
407  /*************************  NETWORK NETWORK ACL HANDLER  *************************/  /**
408     * ccs_write_unix_network - Write "struct ccs_unix_acl" list.
409     *
410     * @param: Pointer to "struct ccs_acl_param".
411     *
412     * Returns 0 on success, negative value otherwise.
413     */
414    int ccs_write_unix_network(struct ccs_acl_param *param)
415    {
416            struct ccs_unix_acl e = { .head.type = CCS_TYPE_UNIX_ACL };
417            int error;
418            u8 type;
419            const char *protocol = ccs_read_token(param);
420            const char *operation = ccs_read_token(param);
421            for (e.protocol = 0; e.protocol < CCS_SOCK_MAX; e.protocol++)
422                    if (!strcmp(protocol, ccs_proto_keyword[e.protocol]))
423                            break;
424            for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
425                    if (ccs_permstr(operation, ccs_socket_keyword[type]))
426                            e.perm |= 1 << type;
427            if (e.protocol == CCS_SOCK_MAX || !e.perm)
428                    return -EINVAL;
429            if (!ccs_parse_name_union(ccs_read_token(param), &e.name))
430                    return -EINVAL;
431            error = ccs_update_domain(&e.head, sizeof(e), param, ccs_same_unix_acl,
432                                      ccs_merge_unix_acl);
433            ccs_put_name_union(&e.name);
434            return error;
435    }
436    
437  #if !defined(NIP6)  #ifndef CONFIG_NET
 #define NIP6(addr) \  
         ntohs((addr).s6_addr16[0]), \  
         ntohs((addr).s6_addr16[1]), \  
         ntohs((addr).s6_addr16[2]), \  
         ntohs((addr).s6_addr16[3]), \  
         ntohs((addr).s6_addr16[4]), \  
         ntohs((addr).s6_addr16[5]), \  
         ntohs((addr).s6_addr16[6]), \  
         ntohs((addr).s6_addr16[7])  
 #endif  
438    
439  char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip)  void __init ccs_network_init(void)
440  {  {
         memset(buffer, 0, buffer_len);  
         snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip));  
         return buffer;  
441  }  }
442    
443  const char *network2keyword(const unsigned int operation)  #else
444    
445    /**
446     * ccs_inet_entry - Check permission for INET network operation.
447     *
448     * @address: Pointer to "struct ccs_addr_info".
449     *
450     * Returns 0 on success, negative value otherwise.
451     */
452    static int ccs_inet_entry(const struct ccs_addr_info *address)
453  {  {
454          const char *keyword = "unknown";          const int idx = ccs_read_lock();
455          switch (operation) {          struct ccs_request_info r;
456          case NETWORK_ACL_UDP_BIND:          int error = 0;
457                  keyword = "UDP bind";          const u8 type = ccs_inet2mac[address->protocol][address->operation];
458                  break;          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
459          case NETWORK_ACL_UDP_CONNECT:                  struct task_struct * const task = current;
460                  keyword = "UDP connect";                  const bool no_sleep = address->operation == CCS_NETWORK_ACCEPT
461                  break;                          || address->operation == CCS_NETWORK_RECV;
462          case NETWORK_ACL_TCP_BIND:                  r.param_type = CCS_TYPE_INET_ACL;
463                  keyword = "TCP bind";                  r.param.inet_network.protocol = address->protocol;
464                  break;                  r.param.inet_network.operation = address->operation;
465          case NETWORK_ACL_TCP_LISTEN:                  r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
466                  keyword = "TCP listen";                  r.param.inet_network.address = address->inet.address;
467                  break;                  r.param.inet_network.port = ntohs(address->inet.port);
468          case NETWORK_ACL_TCP_CONNECT:                  /* use host byte order to allow u32 comparison than memcmp().*/
469                  keyword = "TCP connect";                  r.param.inet_network.ip = ntohl(*address->inet.address);
470                  break;                  if (no_sleep)
471          case NETWORK_ACL_TCP_ACCEPT:                          task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
472                  keyword = "TCP accept";                  do {
473                  break;                          ccs_check_acl(&r, ccs_check_inet_acl);
474          case NETWORK_ACL_RAW_BIND:                          error = ccs_audit_inet_log(&r);
475                  keyword = "RAW bind";                  } while (error == CCS_RETRY_REQUEST);
476                  break;                  if (no_sleep)
477          case NETWORK_ACL_RAW_CONNECT:                          task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
                 keyword = "RAW connect";  
                 break;  
         }  
         return keyword;  
 }  
   
 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)  
 {  
         struct acl_info *ptr;  
         struct ip_network_acl_record *acl;  
         int error = -ENOMEM;  
         const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/  
         const struct in6_addr *saved_min_address = NULL, *saved_max_address = NULL;  
         if (!domain) return -EINVAL;  
         if (record_type == IP_RECORD_TYPE_IPv6) {  
                 if ((saved_min_address = SaveIPv6Address((struct in6_addr *) min_address)) == NULL  
                     || (saved_max_address = SaveIPv6Address((struct in6_addr *) max_address)) == NULL) return -ENOMEM;  
         }  
         mutex_lock(&domain_acl_lock);  
         if (!is_delete) {  
                 list1_for_each_entry(ptr, &domain->acl_info_list, list) {  
                         acl = container_of(ptr, struct ip_network_acl_record, head);  
                         if (ptr->type == TYPE_IP_NETWORK_ACL && acl->operation_type == operation && acl->record_type == record_type && ptr->cond == condition && acl->min_port == min_port && max_port == acl->max_port) {  
                                 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                                         if (acl->u.group == group) {  
                                                 ptr->is_deleted = 0;  
                                                 /* Found. Nothing to do. */  
                                                 error = 0;  
                                                 goto out;  
                                         }  
                                 } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                                         if (acl->u.ipv4.min == min_ip && max_ip == acl->u.ipv4.max) {  
                                                 ptr->is_deleted = 0;  
                                                 /* Found. Nothing to do. */  
                                                 error = 0;  
                                                 goto out;  
                                         }  
                                 } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                                         if (acl->u.ipv6.min == saved_min_address && saved_max_address == acl->u.ipv6.max) {  
                                                 ptr->is_deleted = 0;  
                                                 /* Found. Nothing to do. */  
                                                 error = 0;  
                                                 goto out;  
                                         }  
                                 }  
                         }  
                 }  
                 /* Not found. Append it to the tail. */  
                 if ((acl = alloc_element(sizeof(*acl))) == NULL) goto out;  
                 acl->head.type = TYPE_IP_NETWORK_ACL;  
                 acl->operation_type = operation;  
                 acl->record_type = record_type;  
                 acl->head.cond = condition;  
                 if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                         acl->u.group = group;  
                 } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                         acl->u.ipv4.min = min_ip;  
                         acl->u.ipv4.max = max_ip;  
                 } else {  
                         acl->u.ipv6.min = saved_min_address;  
                         acl->u.ipv6.max = saved_max_address;  
                 }  
                 acl->min_port = min_port;  
                 acl->max_port = max_port;  
                 error = AddDomainACL(domain, &acl->head);  
         } else {  
                 error = -ENOENT;  
                 list1_for_each_entry(ptr, &domain->acl_info_list, list) {  
                         acl = container_of(ptr, struct ip_network_acl_record, head);  
                         if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || acl->record_type != record_type || ptr->cond != condition || acl->min_port != min_port || acl->max_port != max_port) continue;  
                         if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                                 if (acl->u.group != group) continue;  
                         } else if (record_type == IP_RECORD_TYPE_IPv4) {  
                                 if (acl->u.ipv4.min != min_ip || max_ip != acl->u.ipv4.max) continue;  
                         } else if (record_type == IP_RECORD_TYPE_IPv6) {  
                                 if (acl->u.ipv6.min != saved_min_address || saved_max_address != acl->u.ipv6.max) continue;  
                         }  
                         error = DelDomainACL(ptr);  
                         break;  
                 }  
478          }          }
479   out: ;          ccs_read_unlock(idx);
         mutex_unlock(&domain_acl_lock);  
480          return error;          return error;
481  }  }
482    
483  static int CheckNetworkEntry(const bool is_ipv6, const int operation, const u32 *address, const u16 port)  static int ccs_check_inet_address(const struct sockaddr *addr,
484  {                                    const unsigned int addr_len, const u16 port,
485          struct domain_info * const domain = current->domain_info;                                    struct ccs_addr_info *address)
486          struct acl_info *ptr;  {
487          const char *keyword = network2keyword(operation);          struct ccs_inet_addr_info *i = &address->inet;
488          const bool is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK);          switch (addr->sa_family) {
489          const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/          case AF_INET6:
490          bool found = 0;                  if (addr_len < SIN6_LEN_RFC2133)
491          if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0;                          goto skip;
492          list1_for_each_entry(ptr, &domain->acl_info_list, list) {                  i->is_ipv6 = true;
493                  struct ip_network_acl_record *acl;                  i->address = (u32 *)
494                  acl = container_of(ptr, struct ip_network_acl_record, head);                          ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
495                  if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || port < acl->min_port || acl->max_port < port || CheckCondition(ptr->cond, NULL)) continue;                  i->port = ((struct sockaddr_in6 *) addr)->sin6_port;
                 if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) {  
                         if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue;  
                 } else if (acl->record_type == IP_RECORD_TYPE_IPv4) {  
                         if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue;  
                 } else {  
                         if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue;  
                 }  
                 found = 1;  
496                  break;                  break;
497                                    case AF_INET:
498          }                  if (addr_len < sizeof(struct sockaddr_in))
499          AuditNetworkLog(is_ipv6, keyword, address, port, found);                          goto skip;
500          if (found) return 0;                  i->is_ipv6 = false;
501          if (TomoyoVerboseMode()) {                  i->address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
502                  if (is_ipv6) {                  i->port = ((struct sockaddr_in *) addr)->sin_port;
503                          char buf[64];                  break;
504                          print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);          default:
505                          printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain));                  goto skip;
                 } else {  
                         printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain));  
                 }  
         }  
         AuditNetworkLog(is_ipv6, keyword, address, port, 0);  
         if (is_enforce) {  
                 if (is_ipv6) {  
                         char buf[64];  
                         print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address);  
                         return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port);  
                 }  
                 return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port);  
506          }          }
507          if (CheckCCSAccept(CCS_TOMOYO_MAC_FOR_NETWORK, domain)) AddNetworkEntry(operation, is_ipv6 ? IP_RECORD_TYPE_IPv6 : IP_RECORD_TYPE_IPv4, NULL, address, address, port, port, domain, NULL, 0);          if (address->protocol == SOCK_RAW)
508                    i->port = htons(port);
509            return ccs_inet_entry(address);
510     skip:
511          return 0;          return 0;
512  }  }
513    
514  int AddNetworkPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete)  /**
515     * ccs_unix_entry - Check permission for UNIX network operation.
516     *
517     * @address: Pointer to "struct ccs_addr_info".
518     *
519     * Returns 0 on success, negative value otherwise.
520     */
521    static int ccs_unix_entry(const struct ccs_addr_info *address)
522  {  {
523          u8 sock_type, operation, record_type;          const int idx = ccs_read_lock();
524          u16 min_address[8], max_address[8];          struct ccs_request_info r;
525          struct address_group_entry *group = NULL;          int error = 0;
526          u16 min_port, max_port;          const u8 type = ccs_unix2mac[address->protocol][address->operation];
527          int count;          if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
528          char *cp1 = NULL, *cp2 = NULL;                  char *buf;
529          if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++;                  if (address->unix0.addr_len <= sizeof(sa_family_t))
530          if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM;                          buf = ccs_encode2("anonymous", 9);
531          else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM;                  else if (address->unix0.addr[0])
532          else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW;                          buf = ccs_encode(address->unix0.addr);
533          else goto out;                  else
534          if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++;                          buf = ccs_encode2(address->unix0.addr,
535          if (strncmp(cp1, "bind ", 5) == 0) {                                            address->unix0.addr_len
536                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND;                                            - sizeof(sa_family_t));
537          } else if (strncmp(cp1, "connect ", 8) == 0) {                  if (buf) {
538                  operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT;                          struct task_struct * const task = current;
539          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) {                          const bool no_sleep =
540                  operation = NETWORK_ACL_TCP_LISTEN;                                  address->operation == CCS_NETWORK_ACCEPT ||
541          } else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) {                                  address->operation == CCS_NETWORK_RECV;
542                  operation = NETWORK_ACL_TCP_ACCEPT;                          struct ccs_path_info addr;
543          } else {                          addr.name = buf;
544                  goto out;                          ccs_fill_path_info(&addr);
545          }                          r.param_type = CCS_TYPE_UNIX_ACL;
546          if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0';                          r.param.unix_network.protocol = address->protocol;
547          if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",                          r.param.unix_network.operation = address->operation;
548                              &min_address[0], &min_address[1], &min_address[2], &min_address[3],                          r.param.unix_network.address = &addr;
549                              &min_address[4], &min_address[5], &min_address[6], &min_address[7],                          if (no_sleep)
550                              &max_address[0], &max_address[1], &max_address[2], &max_address[3],                                  task->ccs_flags |=
551                              &max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) {                                          CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
552                  int i;                  
553                  for (i = 0; i < 8; i++) {                          do {
554                          min_address[i] = htons(min_address[i]);                                  ccs_check_acl(&r, ccs_check_unix_acl);
555                          max_address[i] = htons(max_address[i]);                                  error = ccs_audit_unix_log(&r);
556                  }                          } while (error == CCS_RETRY_REQUEST);
557                  if (count == 8) memmove(max_address, min_address, sizeof(min_address));                          if (no_sleep)
558                  record_type = IP_RECORD_TYPE_IPv6;                                  task->ccs_flags &=
559          } else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu",                                          ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;
560                                     &min_address[0], &min_address[1], &min_address[2], &min_address[3],                          kfree(buf);
561                                     &max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) {                  } else
562                  u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]);                          error = -ENOMEM;
                 * (u32 *) min_address = ip;  
                 if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]);  
                 * (u32 *) max_address = ip;  
                 record_type = IP_RECORD_TYPE_IPv4;  
         } else if (*cp2 == '@') {  
                 if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM;  
                 record_type = IP_RECORD_TYPE_ADDRESS_GROUP;  
         } else {  
                 goto out;  
563          }          }
564          if (strchr(cp1, ' ')) goto out;          ccs_read_unlock(idx);
565          if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) {          return error;
                 if (count == 1) max_port = min_port;  
                 return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, condition, is_delete);  
         }  
  out: ;  
         return -EINVAL;  
566  }  }
567    
568  int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port)  static int ccs_check_unix_address(struct sockaddr *addr,
569  {                                    const unsigned int addr_len,
570          return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port));                                    struct ccs_addr_info *address)
571    {
572            struct ccs_unix_addr_info *u = &address->unix0;
573            if (addr->sa_family != AF_UNIX)
574                    return 0;
575            u->addr = ((struct sockaddr_un *) addr)->sun_path;
576            u->addr_len = addr_len;
577            /*
578             * Terminate pathname with '\0' like unix_mkname() does.
579             * This is needed because pathname was copied by move_addr_to_kernel()
580             * but not yet terminated by unix_mkname().
581             */
582            if (u->addr[0] && addr_len > sizeof(short) &&
583                addr_len <= sizeof(struct sockaddr_un))
584                    ((char *) u->addr)[addr_len] = '\0';
585            return ccs_unix_entry(address);
586    }
587    
588    static bool ccs_kernel_service(void)
589    {
590            /* Nothing to do if I am a kernel service. */
591            return segment_eq(get_fs(), KERNEL_DS);
592    }
593    
594    static u8 ccs_sock_family(struct sock *sk)
595    {
596            u8 family;
597            if (ccs_kernel_service())
598                    return 0;
599            family = sk->sk_family;
600            switch (family) {
601            case PF_INET:
602            case PF_INET6:
603            case PF_UNIX:
604                    return family;
605            default:
606                    return 0;
607            }
608  }  }
 EXPORT_SYMBOL(CheckNetworkListenACL);  
609    
610  int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /* Check permission for creating a socket. */
611  {  static int __ccs_socket_create_permission(int family, int type, int protocol)
612          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));  {
613            if (ccs_kernel_service())
614                    return 0;
615            if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
616                    return -EPERM;
617            if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
618                    return -EPERM;
619            return 0;
620  }  }
 EXPORT_SYMBOL(CheckNetworkConnectACL);  
621    
622  int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  /* Check permission for listening a socket. */
623    static int __ccs_socket_listen_permission(struct socket *sock)
624  {  {
625          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));          struct ccs_addr_info address;
626            const u8 family = ccs_sock_family(sock->sk);
627            const unsigned int type = sock->type;
628            struct sockaddr_storage addr;
629            int addr_len;
630            if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
631                    return 0;
632            {
633                    const int error = sock->ops->getname(sock, (struct sockaddr *)
634                                                         &addr, &addr_len, 0);
635                    if (error)
636                            return error;
637            }
638            address.protocol = type;
639            address.operation = CCS_NETWORK_LISTEN;
640            if (family == PF_UNIX)
641                    return ccs_check_unix_address((struct sockaddr *) &addr,
642                                                  addr_len, &address);
643            return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
644                                          &address);
645    }
646    
647    /* Check permission for setting the remote address of a socket. */
648    static int __ccs_socket_connect_permission(struct socket *sock,
649                                               struct sockaddr *addr, int addr_len)
650    {
651            struct ccs_addr_info address;
652            const u8 family = ccs_sock_family(sock->sk);
653            const unsigned int type = sock->type;
654            if (!family)
655                    return 0;
656            address.protocol = type;
657            switch (type) {
658            case SOCK_DGRAM:
659            case SOCK_RAW:
660                    address.operation = CCS_NETWORK_SEND;
661                    break;
662            case SOCK_STREAM:
663            case SOCK_SEQPACKET:
664                    address.operation = CCS_NETWORK_CONNECT;
665                    break;
666            default:
667                    return 0;
668            }
669            if (family == PF_UNIX)
670                    return ccs_check_unix_address(addr, addr_len, &address);
671            return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
672                                          &address);
673    }
674    
675    /* Check permission for setting the local address of a socket. */
676    static int __ccs_socket_bind_permission(struct socket *sock,
677                                            struct sockaddr *addr, int addr_len)
678    {
679            struct ccs_addr_info address;
680            const u8 family = ccs_sock_family(sock->sk);
681            const unsigned int type = sock->type;
682            if (!family)
683                    return 0;
684            switch (type) {
685            case SOCK_STREAM:
686            case SOCK_DGRAM:
687            case SOCK_RAW:
688            case SOCK_SEQPACKET:
689                    address.protocol = type;
690                    address.operation = CCS_NETWORK_BIND;
691                    break;
692            default:
693                    return 0;
694            }
695            if (family == PF_UNIX)
696                    return ccs_check_unix_address(addr, addr_len, &address);
697            return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
698                                          &address);
699    }
700    
701    /* Check permission for sending a datagram. */
702    static int __ccs_socket_sendmsg_permission(struct socket *sock,
703                                               struct msghdr *msg, int size)
704    {
705            struct ccs_addr_info address;
706            const u8 family = ccs_sock_family(sock->sk);
707            const unsigned int type = sock->type;
708            if (!msg->msg_name || !family ||
709                (type != SOCK_DGRAM && type != SOCK_RAW))
710                    return 0;
711            address.protocol = type;
712            address.operation = CCS_NETWORK_SEND;
713            if (family == PF_UNIX)
714                    return ccs_check_unix_address((struct sockaddr *)
715                                                  msg->msg_name, msg->msg_namelen,
716                                                  &address);
717            return ccs_check_inet_address((struct sockaddr *) msg->msg_name,
718                                          msg->msg_namelen, sock->sk->sk_protocol,
719                                          &address);
720    }
721    
722    /* Check permission for accepting a socket. */
723    static int __ccs_socket_post_accept_permission(struct socket *sock,
724                                                   struct socket *newsock)
725    {
726            struct ccs_addr_info address;
727            const u8 family = ccs_sock_family(sock->sk);
728            const unsigned int type = sock->type;
729            struct sockaddr_storage addr;
730            int addr_len;
731            if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
732                    return 0;
733            {
734                    const int error = newsock->ops->getname(newsock,
735                                                            (struct sockaddr *)
736                                                            &addr, &addr_len, 2);
737                    if (error)
738                            return error;
739            }
740            address.protocol = type;
741            address.operation = CCS_NETWORK_ACCEPT;
742            if (family == PF_UNIX)
743                    return ccs_check_unix_address((struct sockaddr *) &addr,
744                                                  addr_len, &address);
745            return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
746                                          &address);
747    }
748    
749    #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
750    #if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
751    #if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2
752    
753    static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
754    {
755            return skb->nh.iph;
756  }  }
 EXPORT_SYMBOL(CheckNetworkBindACL);  
757    
758  int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port)  static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
759  {  {
760          int retval;          return skb->h.uh;
         current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         retval = CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port));  
         current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
         return retval;  
761  }  }
 EXPORT_SYMBOL(CheckNetworkAcceptACL);  
762    
763  int CheckNetworkSendMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
764  {  {
765          return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));          return skb->nh.ipv6h;
766  }  }
 EXPORT_SYMBOL(CheckNetworkSendMsgACL);  
767    
768  int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port)  #endif
769  {  #endif
770          int retval;  #endif
771          current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  
772          retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port));  /* Check permission for receiving a datagram. */
773          current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR;  static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
774          return retval;                                                  struct sk_buff *skb)
775    {
776            struct ccs_addr_info address;
777            const u8 family = ccs_sock_family(sk);
778            const unsigned int type = sk->sk_type;
779            struct sockaddr_storage addr;
780            if (!family)
781                    return 0;
782            switch (type) {
783            case SOCK_DGRAM:
784            case SOCK_RAW:
785                    address.protocol = type;
786                    break;
787            default:
788                    return 0;
789            }
790            address.operation = CCS_NETWORK_RECV;
791            switch (family) {
792            case PF_INET6:
793                    {
794                            struct in6_addr *sin6 = (struct in6_addr *) &addr;
795                            address.inet.is_ipv6 = true;
796                            if (type == SOCK_DGRAM &&
797                                skb->protocol == htons(ETH_P_IP))
798                                    ipv6_addr_set(sin6, 0, 0, htonl(0xffff),
799                                                  ip_hdr(skb)->saddr);
800                            else
801                                    ipv6_addr_copy(sin6, &ipv6_hdr(skb)->saddr);
802                            break;
803                    }
804            case PF_INET:
805                    {
806                            struct in_addr *sin4 = (struct in_addr *) &addr;
807                            address.inet.is_ipv6 = false;
808                            sin4->s_addr = ip_hdr(skb)->saddr;
809                            break;
810                    }
811            default: /* == PF_UNIX */
812                    {
813    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
814                            struct unix_address *u = unix_sk(skb->sk)->addr;
815    #else
816                            struct unix_address *u = skb->sk->protinfo.af_unix.addr;
817    #endif
818                            unsigned int addr_len;
819                            if (!u)
820                                    return 0;
821                            addr_len = u->len;
822                            if (addr_len >= sizeof(addr))
823                                    return 0;
824                            memcpy(&addr, u->name, addr_len);
825                            return ccs_check_unix_address((struct sockaddr *) &addr,
826                                                          addr_len, &address);
827                    }
828            }
829            address.inet.address = (u32 *) &addr;
830            if (type == SOCK_DGRAM)
831                    address.inet.port = udp_hdr(skb)->source;
832            else
833                    address.inet.port = htons(sk->sk_protocol);
834            return ccs_inet_entry(&address);
835    }
836    
837    void __init ccs_network_init(void)
838    {
839            ccsecurity_ops.socket_create_permission =
840                    __ccs_socket_create_permission;
841            ccsecurity_ops.socket_listen_permission =
842                    __ccs_socket_listen_permission;
843            ccsecurity_ops.socket_connect_permission =
844                    __ccs_socket_connect_permission;
845            ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
846            ccsecurity_ops.socket_post_accept_permission =
847                    __ccs_socket_post_accept_permission;
848            ccsecurity_ops.socket_sendmsg_permission =
849                    __ccs_socket_sendmsg_permission;
850            ccsecurity_ops.socket_post_recvmsg_permission =
851                    __ccs_socket_post_recvmsg_permission;
852  }  }
 EXPORT_SYMBOL(CheckNetworkRecvMsgACL);  
853    
854  /***** TOMOYO Linux end. *****/  #endif

Legend:
Removed from v.776  
changed lines
  Added in v.3994

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