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/08/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 <net/ip.h> |
#include <net/ip.h> |
18 |
|
#include <net/ipv6.h> |
19 |
|
#include <net/udp.h> |
20 |
|
#include "internal.h" |
21 |
|
|
22 |
|
struct ccs_addr_info { |
23 |
|
u8 protocol; |
24 |
|
u8 operation; |
25 |
|
u16 port; /* In network byte order. */ |
26 |
|
const u32 *address; /* In network byte order. */ |
27 |
|
bool is_ipv6; |
28 |
|
}; |
29 |
|
|
30 |
|
const char *ccs_net_protocol_keyword[CCS_MAX_NETWORK_PROTOCOL] = { |
31 |
|
[CCS_NETWORK_TCP_PROTOCOL] = "TCP", |
32 |
|
[CCS_NETWORK_UDP_PROTOCOL] = "UDP", |
33 |
|
[CCS_NETWORK_RAW_PROTOCOL] = "RAW", |
34 |
|
}; |
35 |
|
|
36 |
|
const char *ccs_net_keyword[CCS_MAX_NETWORK_OPERATION] = { |
37 |
|
[CCS_NETWORK_BIND] = "bind", |
38 |
|
[CCS_NETWORK_LISTEN] = "listen", |
39 |
|
[CCS_NETWORK_CONNECT] = "connect", |
40 |
|
[CCS_NETWORK_ACCEPT] = "accept", |
41 |
|
[CCS_NETWORK_SEND] = "send", |
42 |
|
[CCS_NETWORK_RECV] = "recv", |
43 |
|
}; |
44 |
|
|
45 |
/************************* VARIABLES *************************/ |
/** |
46 |
|
* ccs_audit_network_log - Audit network log. |
47 |
extern struct mutex domain_acl_lock; |
* |
48 |
|
* @r: Pointer to "struct ccs_request_info". |
49 |
/************************* AUDIT FUNCTIONS *************************/ |
* |
50 |
|
* Returns 0 on success, negative value otherwise. |
51 |
static int AuditNetworkLog(const bool is_ipv6, const char *operation, const u32 *address, const u16 port, const bool is_granted) |
*/ |
52 |
|
static int ccs_audit_network_log(struct ccs_request_info *r) |
53 |
{ |
{ |
54 |
char *buf; |
char buf[128]; |
55 |
int len = 256; |
const char *protocol = |
56 |
if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM; |
ccs_net_protocol_keyword[r->param.network.protocol]; |
57 |
if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM; |
const char *operation = ccs_net_keyword[r->param.network.operation]; |
58 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation); |
const u32 *address = r->param.network.address; |
59 |
if (is_ipv6) { |
const u16 port = r->param.network.port; |
60 |
print_ipv6(buf + strlen(buf), len - strlen(buf), (const struct in6_addr *) address); |
if (r->param.network.is_ipv6) |
61 |
} else { |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
62 |
u32 ip = *address; |
address, (const struct in6_addr *) address); |
63 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip)); |
else |
64 |
} |
ccs_print_ipv4(buf, sizeof(buf), r->param.network.ip, |
65 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port); |
r->param.network.ip); |
66 |
return WriteAuditLog(buf, is_granted); |
ccs_write_log(r, "network %s %s %s %u\n", protocol, operation, buf, |
67 |
|
port); |
68 |
|
if (r->granted) |
69 |
|
return 0; |
70 |
|
ccs_warn_log(r, "network %s %s %s %u", protocol, operation, buf, port); |
71 |
|
return ccs_supervisor(r, "network %s %s %s %u\n", protocol, operation, |
72 |
|
buf, port); |
73 |
} |
} |
74 |
|
|
75 |
/************************* UTILITY FUNCTIONS *************************/ |
/** |
76 |
|
* ccs_parse_ip_address - Parse an IP address. |
77 |
/* Keep the given IPv6 address on the RAM. The RAM is shared, so NEVER try to modify or kfree() the returned address. */ |
* |
78 |
static const struct in6_addr *SaveIPv6Address(const struct in6_addr *addr) |
* @address: String to parse. |
79 |
|
* @min: Pointer to store min address. |
80 |
|
* @max: Pointer to store max address. |
81 |
|
* |
82 |
|
* Returns CCS_IP_ADDRESS_TYPE_IPv6 if @address is an IPv6, |
83 |
|
* CCS_IP_ADDRESS_TYPE_IPv4 if @address is an IPv4, |
84 |
|
* CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP otherwise. |
85 |
|
*/ |
86 |
|
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
87 |
{ |
{ |
88 |
static const int block_size = 16; |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
89 |
struct addr_list { |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
90 |
struct in6_addr addr[block_size]; |
&min[0], &min[1], &min[2], &min[3], |
91 |
struct list1_head list; |
&min[4], &min[5], &min[6], &min[7], |
92 |
u32 in_use_count; |
&max[0], &max[1], &max[2], &max[3], |
93 |
}; |
&max[4], &max[5], &max[6], &max[7]); |
94 |
static LIST1_HEAD(address_list); |
if (count == 8 || count == 16) { |
95 |
struct addr_list *ptr; |
u8 i; |
96 |
static DEFINE_MUTEX(lock); |
if (count == 8) |
97 |
int i = block_size; |
memmove(max, min, sizeof(u16) * 8); |
98 |
if (!addr) return NULL; |
for (i = 0; i < 8; i++) { |
99 |
mutex_lock(&lock); |
min[i] = htons(min[i]); |
100 |
list1_for_each_entry(ptr, &address_list, list) { |
max[i] = htons(max[i]); |
|
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; |
|
101 |
} |
} |
102 |
found = 1; |
return CCS_IP_ADDRESS_TYPE_IPv6; |
|
break; |
|
|
} |
|
|
if (is_delete) { |
|
|
error = -ENOENT; |
|
|
goto out; |
|
103 |
} |
} |
104 |
if (!found) { |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
105 |
if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out; |
&min[0], &min[1], &min[2], &min[3], |
106 |
INIT_LIST1_HEAD(&new_group->address_group_member_list); |
&max[0], &max[1], &max[2], &max[3]); |
107 |
new_group->group_name = saved_group_name; |
if (count == 4 || count == 8) { |
108 |
list1_add_tail_mb(&new_group->list, &address_group_list); |
u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16) |
109 |
group = new_group; |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
110 |
} |
memmove(min, &ip, sizeof(ip)); |
111 |
if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out; |
if (count == 8) |
112 |
new_member->is_ipv6 = is_ipv6; |
ip = htonl((((u8) max[0]) << 24) |
113 |
if (is_ipv6) { |
+ (((u8) max[1]) << 16) |
114 |
new_member->min.ipv6 = saved_min_address; |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
115 |
new_member->max.ipv6 = saved_max_address; |
memmove(max, &ip, sizeof(ip)); |
116 |
} else { |
return CCS_IP_ADDRESS_TYPE_IPv4; |
|
new_member->min.ipv4 = * (u32 *) min_address; |
|
|
new_member->max.ipv4 = * (u32 *) max_address; |
|
117 |
} |
} |
118 |
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; |
|
119 |
} |
} |
120 |
|
|
121 |
int AddAddressGroupPolicy(char *data, const bool is_delete) |
/** |
122 |
|
* ccs_print_ipv4 - Print an IPv4 address. |
123 |
|
* |
124 |
|
* @buffer: Buffer to write to. |
125 |
|
* @buffer_len: Size of @buffer. |
126 |
|
* @min_ip: Min address in host byte order. |
127 |
|
* @max_ip: Max address in host byte order. |
128 |
|
* |
129 |
|
* Returns nothing. |
130 |
|
*/ |
131 |
|
void ccs_print_ipv4(char *buffer, const int buffer_len, |
132 |
|
const u32 min_ip, const u32 max_ip) |
133 |
{ |
{ |
134 |
int count, is_ipv6; |
memset(buffer, 0, buffer_len); |
135 |
u16 min_address[8], max_address[8]; |
snprintf(buffer, buffer_len - 1, "%u.%u.%u.%u%c%u.%u.%u.%u", |
136 |
char *cp = strchr(data, ' '); |
HIPQUAD(min_ip), min_ip == max_ip ? '\0' : '-', |
137 |
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); |
|
138 |
} |
} |
139 |
|
|
140 |
static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name) |
#if !defined(NIP6) |
141 |
{ |
#define NIP6(addr) \ |
142 |
int i; |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
143 |
struct address_group_entry *group; |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
144 |
for (i = 0; i <= 1; i++) { |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
145 |
list1_for_each_entry(group, &address_group_list, list) { |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
146 |
if (strcmp(group_name, group->group_name->name) == 0) return group; |
#endif |
|
} |
|
|
if (i == 0) { |
|
|
const u16 dummy[2] = { 0, 0 }; |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 0); |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 1); |
|
|
} |
|
|
} |
|
|
return NULL; |
|
|
} |
|
147 |
|
|
148 |
static int AddressMatchesToGroup(const bool is_ipv6, const u32 *address, const struct address_group_entry *group) |
/** |
149 |
|
* ccs_print_ipv6 - Print an IPv6 address. |
150 |
|
* |
151 |
|
* @buffer: Buffer to write to. |
152 |
|
* @buffer_len: Size of @buffer. |
153 |
|
* @min_ip: Pointer to "struct in6_addr". |
154 |
|
* @max_ip: Pointer to "struct in6_addr". |
155 |
|
* |
156 |
|
* Returns nothing. |
157 |
|
*/ |
158 |
|
void ccs_print_ipv6(char *buffer, const int buffer_len, |
159 |
|
const struct in6_addr *min_ip, |
160 |
|
const struct in6_addr *max_ip) |
161 |
{ |
{ |
162 |
struct address_group_member *member; |
memset(buffer, 0, buffer_len); |
163 |
const u32 ip = ntohl(*address); |
snprintf(buffer, buffer_len - 1, |
164 |
list1_for_each_entry(member, &group->address_group_member_list, list) { |
"%x:%x:%x:%x:%x:%x:%x:%x%c%x:%x:%x:%x:%x:%x:%x:%x", |
165 |
if (member->is_deleted) continue; |
NIP6(*min_ip), min_ip == max_ip ? '\0' : '-', |
166 |
if (member->is_ipv6) { |
NIP6(*max_ip)); |
167 |
if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1; |
} |
168 |
} else { |
|
169 |
if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1; |
static bool ccs_check_network_acl(struct ccs_request_info *r, |
170 |
} |
const struct ccs_acl_info *ptr) |
171 |
|
{ |
172 |
|
const struct ccs_ip_network_acl *acl = |
173 |
|
container_of(ptr, typeof(*acl), head); |
174 |
|
bool ret; |
175 |
|
if (!(acl->perm & (1 << r->param.network.operation)) || |
176 |
|
!ccs_compare_number_union(r->param.network.port, &acl->port)) |
177 |
|
return false; |
178 |
|
switch (acl->address_type) { |
179 |
|
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
180 |
|
ret = ccs_address_matches_group(r->param.network.is_ipv6, |
181 |
|
r->param.network.address, |
182 |
|
acl->address.group); |
183 |
|
break; |
184 |
|
case CCS_IP_ADDRESS_TYPE_IPv4: |
185 |
|
ret = !r->param.network.is_ipv6 && |
186 |
|
acl->address.ipv4.min <= r->param.network.ip && |
187 |
|
r->param.network.ip <= acl->address.ipv4.max; |
188 |
|
break; |
189 |
|
default: |
190 |
|
ret = r->param.network.is_ipv6 && |
191 |
|
memcmp(acl->address.ipv6.min, r->param.network.address, |
192 |
|
16) <= 0 && |
193 |
|
memcmp(r->param.network.address, acl->address.ipv6.max, |
194 |
|
16) <= 0; |
195 |
|
break; |
196 |
} |
} |
197 |
return 0; |
return ret; |
198 |
} |
} |
199 |
|
|
200 |
int ReadAddressGroupPolicy(struct io_buffer *head) |
static const u8 |
201 |
|
ccs_net2mac[CCS_MAX_NETWORK_PROTOCOL][CCS_MAX_NETWORK_OPERATION] = { |
202 |
|
[CCS_NETWORK_TCP_PROTOCOL] = { |
203 |
|
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_TCP_BIND, |
204 |
|
[CCS_NETWORK_LISTEN] = CCS_MAC_NETWORK_TCP_LISTEN, |
205 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_TCP_CONNECT, |
206 |
|
[CCS_NETWORK_ACCEPT] = CCS_MAC_NETWORK_TCP_ACCEPT, |
207 |
|
}, |
208 |
|
[CCS_NETWORK_UDP_PROTOCOL] = { |
209 |
|
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_UDP_BIND, |
210 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UDP_SEND, |
211 |
|
[CCS_NETWORK_SEND] = CCS_MAC_NETWORK_UDP_SEND, |
212 |
|
[CCS_NETWORK_RECV] = CCS_MAC_NETWORK_UDP_RECV, |
213 |
|
}, |
214 |
|
[CCS_NETWORK_RAW_PROTOCOL] = { |
215 |
|
[CCS_NETWORK_BIND] = CCS_MAC_NETWORK_RAW_BIND, |
216 |
|
[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_RAW_SEND, |
217 |
|
[CCS_NETWORK_SEND] = CCS_MAC_NETWORK_RAW_SEND, |
218 |
|
[CCS_NETWORK_RECV] = CCS_MAC_NETWORK_RAW_RECV, |
219 |
|
}, |
220 |
|
}; |
221 |
|
|
222 |
|
/** |
223 |
|
* ccs_network_entry - Check permission for network operation. |
224 |
|
* |
225 |
|
* @address: Pointer to "struct ccs_ip_address". |
226 |
|
* |
227 |
|
* Returns 0 on success, negative value otherwise. |
228 |
|
*/ |
229 |
|
static int ccs_network_entry(const struct ccs_addr_info *address) |
230 |
{ |
{ |
231 |
struct list1_head *gpos; |
const int idx = ccs_read_lock(); |
232 |
struct list1_head *mpos; |
struct ccs_request_info r; |
233 |
list1_for_each_cookie(gpos, head->read_var1, &address_group_list) { |
int error = 0; |
234 |
struct address_group_entry *group; |
const u8 type = ccs_net2mac[address->protocol][address->operation]; |
235 |
group = list1_entry(gpos, struct address_group_entry, list); |
if (!type || ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) { |
236 |
list1_for_each_cookie(mpos, head->read_var2, &group->address_group_member_list) { |
r.param_type = CCS_TYPE_IP_NETWORK_ACL; |
237 |
char buf[128]; |
r.param.network.protocol = address->protocol; |
238 |
struct address_group_member *member; |
r.param.network.operation = address->operation; |
239 |
member = list1_entry(mpos, struct address_group_member, list); |
r.param.network.is_ipv6 = address->is_ipv6; |
240 |
if (member->is_deleted) continue; |
r.param.network.address = address->address; |
241 |
if (member->is_ipv6) { |
r.param.network.port = ntohs(address->port); |
242 |
const struct in6_addr *min_address = member->min.ipv6, *max_address = member->max.ipv6; |
/* use host byte order to allow u32 comparison than memcmp().*/ |
243 |
print_ipv6(buf, sizeof(buf), min_address); |
r.param.network.ip = ntohl(*address->address); |
244 |
if (min_address != max_address) { |
do { |
245 |
char *cp = strchr(buf, '\0'); |
ccs_check_acl(&r, ccs_check_network_acl); |
246 |
*cp++ = '-'; |
error = ccs_audit_network_log(&r); |
247 |
print_ipv6(cp, sizeof(buf) - strlen(buf), max_address); |
} while (error == CCS_RETRY_REQUEST); |
|
} |
|
|
} else { |
|
|
const u32 min_address = member->min.ipv4, max_address = member->max.ipv4; |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address)); |
|
|
if (min_address != max_address) { |
|
|
const int len = strlen(buf); |
|
|
snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address)); |
|
|
} |
|
|
} |
|
|
if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) return -ENOMEM; |
|
|
} |
|
248 |
} |
} |
249 |
return 0; |
ccs_read_unlock(idx); |
250 |
|
return error; |
251 |
} |
} |
252 |
|
|
253 |
/************************* NETWORK NETWORK ACL HANDLER *************************/ |
static bool ccs_same_ip_network_acl(const struct ccs_acl_info *a, |
254 |
|
const struct ccs_acl_info *b) |
|
#if !defined(NIP6) |
|
|
#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 |
|
|
|
|
|
char *print_ipv6(char *buffer, const int buffer_len, const struct in6_addr *ip) |
|
255 |
{ |
{ |
256 |
memset(buffer, 0, buffer_len); |
const struct ccs_ip_network_acl *p1 = container_of(a, typeof(*p1), |
257 |
snprintf(buffer, buffer_len - 1, "%x:%x:%x:%x:%x:%x:%x:%x", NIP6(*ip)); |
head); |
258 |
return buffer; |
const struct ccs_ip_network_acl *p2 = container_of(b, typeof(*p2), |
259 |
|
head); |
260 |
|
return ccs_same_acl_head(&p1->head, &p2->head) |
261 |
|
&& p1->protocol == p2->protocol |
262 |
|
&& p1->address_type == p2->address_type && |
263 |
|
p1->address.ipv4.min == p2->address.ipv4.min && |
264 |
|
p1->address.ipv6.min == p2->address.ipv6.min && |
265 |
|
p1->address.ipv4.max == p2->address.ipv4.max && |
266 |
|
p1->address.ipv6.max == p2->address.ipv6.max && |
267 |
|
p1->address.group == p2->address.group && |
268 |
|
ccs_same_number_union(&p1->port, &p2->port); |
269 |
|
} |
270 |
|
|
271 |
|
static bool ccs_merge_ip_network_acl(struct ccs_acl_info *a, |
272 |
|
struct ccs_acl_info *b, |
273 |
|
const bool is_delete) |
274 |
|
{ |
275 |
|
u8 * const a_perm = &container_of(a, struct ccs_ip_network_acl, head) |
276 |
|
->perm; |
277 |
|
u8 perm = *a_perm; |
278 |
|
const u8 b_perm = container_of(b, struct ccs_ip_network_acl, head) |
279 |
|
->perm; |
280 |
|
if (is_delete) |
281 |
|
perm &= ~b_perm; |
282 |
|
else |
283 |
|
perm |= b_perm; |
284 |
|
*a_perm = perm; |
285 |
|
return !perm; |
286 |
} |
} |
287 |
|
|
288 |
const char *network2keyword(const unsigned int operation) |
/** |
289 |
|
* ccs_write_network - Write "struct ccs_ip_network_acl" list. |
290 |
|
* |
291 |
|
* @data: String to parse. |
292 |
|
* @domain: Pointer to "struct ccs_domain_info". |
293 |
|
* @condition: Pointer to "struct ccs_condition". Maybe NULL. |
294 |
|
* @is_delete: True if it is a delete request. |
295 |
|
* |
296 |
|
* Returns 0 on success, negative value otherwise. |
297 |
|
*/ |
298 |
|
int ccs_write_network(char *data, struct ccs_domain_info *domain, |
299 |
|
struct ccs_condition *condition, const bool is_delete) |
300 |
{ |
{ |
301 |
const char *keyword = "unknown"; |
struct ccs_ip_network_acl e = { |
302 |
switch (operation) { |
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
303 |
case NETWORK_ACL_UDP_BIND: |
.head.cond = condition, |
304 |
keyword = "UDP bind"; |
}; |
305 |
break; |
u16 min_address[8]; |
306 |
case NETWORK_ACL_UDP_CONNECT: |
u16 max_address[8]; |
307 |
keyword = "UDP connect"; |
int error = is_delete ? -ENOENT : -ENOMEM; |
308 |
break; |
u8 type; |
309 |
case NETWORK_ACL_TCP_BIND: |
char *w[4]; |
310 |
keyword = "TCP bind"; |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
311 |
break; |
return -EINVAL; |
312 |
case NETWORK_ACL_TCP_LISTEN: |
for (e.protocol = 0; e.protocol < CCS_MAX_NETWORK_PROTOCOL; |
313 |
keyword = "TCP listen"; |
e.protocol++) |
314 |
break; |
if (!strcmp(w[0], ccs_net_protocol_keyword[e.protocol])) |
|
case NETWORK_ACL_TCP_CONNECT: |
|
|
keyword = "TCP connect"; |
|
|
break; |
|
|
case NETWORK_ACL_TCP_ACCEPT: |
|
|
keyword = "TCP accept"; |
|
|
break; |
|
|
case NETWORK_ACL_RAW_BIND: |
|
|
keyword = "RAW bind"; |
|
|
break; |
|
|
case NETWORK_ACL_RAW_CONNECT: |
|
|
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); |
|
315 |
break; |
break; |
316 |
} |
for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) |
317 |
|
if (ccs_permstr(w[1], ccs_net_keyword[type])) |
318 |
|
e.perm |= 1 << type; |
319 |
|
if (e.protocol == CCS_MAX_NETWORK_PROTOCOL || !e.perm) |
320 |
|
return -EINVAL; |
321 |
|
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
322 |
|
case CCS_IP_ADDRESS_TYPE_IPv6: |
323 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
324 |
|
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
325 |
|
min_address); |
326 |
|
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
327 |
|
max_address); |
328 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
329 |
|
goto out; |
330 |
|
break; |
331 |
|
case CCS_IP_ADDRESS_TYPE_IPv4: |
332 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
333 |
|
/* use host byte order to allow u32 comparison.*/ |
334 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
335 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
336 |
|
break; |
337 |
|
default: |
338 |
|
if (w[2][0] != '@') |
339 |
|
return -EINVAL; |
340 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
341 |
|
e.address.group = ccs_get_group(w[2] + 1, CCS_ADDRESS_GROUP); |
342 |
|
if (!e.address.group) |
343 |
|
return -ENOMEM; |
344 |
|
break; |
345 |
|
} |
346 |
|
if (!ccs_parse_number_union(w[3], &e.port)) |
347 |
|
goto out; |
348 |
|
error = ccs_update_domain(&e.head, sizeof(e), is_delete, domain, |
349 |
|
ccs_same_ip_network_acl, |
350 |
|
ccs_merge_ip_network_acl); |
351 |
|
out: |
352 |
|
if (e.address_type == CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP) |
353 |
|
ccs_put_group(e.address.group); |
354 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
355 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
356 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
357 |
} |
} |
358 |
out: ; |
ccs_put_number_union(&e.port); |
|
mutex_unlock(&domain_acl_lock); |
|
359 |
return error; |
return error; |
360 |
} |
} |
361 |
|
|
362 |
static int CheckNetworkEntry(const bool is_ipv6, const int operation, const u32 *address, const u16 port) |
#ifndef CONFIG_NET |
363 |
|
|
364 |
|
void __init ccs_network_init(void) |
365 |
{ |
{ |
366 |
struct domain_info * const domain = current->domain_info; |
} |
367 |
struct acl_info *ptr; |
|
368 |
const char *keyword = network2keyword(operation); |
#else |
369 |
const bool is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK); |
|
370 |
const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/ |
static bool ccs_check_address(const struct sockaddr *addr, |
371 |
bool found = 0; |
const unsigned int addr_len, |
372 |
if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0; |
struct ccs_addr_info *address) |
373 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
{ |
374 |
struct ip_network_acl_record *acl; |
switch (addr->sa_family) { |
375 |
acl = container_of(ptr, struct ip_network_acl_record, head); |
case AF_INET6: |
376 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || acl->operation_type != operation || port < acl->min_port || acl->max_port < port || CheckCondition(ptr->cond, NULL)) continue; |
if (addr_len < SIN6_LEN_RFC2133) |
377 |
if (acl->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
goto skip; |
378 |
if (!AddressMatchesToGroup(is_ipv6, address, acl->u.group)) continue; |
address->is_ipv6 = true; |
379 |
} else if (acl->record_type == IP_RECORD_TYPE_IPv4) { |
address->address = (u32 *) |
380 |
if (is_ipv6 || ip < acl->u.ipv4.min || acl->u.ipv4.max < ip) continue; |
((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr; |
381 |
} else { |
address->port = ((struct sockaddr_in6 *) addr)->sin6_port; |
|
if (!is_ipv6 || memcmp(acl->u.ipv6.min, address, 16) > 0 || memcmp(address, acl->u.ipv6.max, 16) > 0) continue; |
|
|
} |
|
|
found = 1; |
|
382 |
break; |
break; |
383 |
|
case AF_INET: |
384 |
} |
if (addr_len < sizeof(struct sockaddr_in)) |
385 |
AuditNetworkLog(is_ipv6, keyword, address, port, found); |
goto skip; |
386 |
if (found) return 0; |
address->is_ipv6 = false; |
387 |
if (TomoyoVerboseMode()) { |
address->address = (u32 *) |
388 |
if (is_ipv6) { |
&((struct sockaddr_in *) addr)->sin_addr; |
389 |
char buf[64]; |
address->port = ((struct sockaddr_in *) addr)->sin_port; |
390 |
print_ipv6(buf, sizeof(buf), (const struct in6_addr *) address); |
break; |
391 |
printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain)); |
default: |
392 |
} else { |
goto skip; |
|
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); |
|
393 |
} |
} |
394 |
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); |
return true; |
395 |
|
skip: |
396 |
|
return false; |
397 |
|
} |
398 |
|
|
399 |
|
/* Check permission for creating a socket. */ |
400 |
|
static int __ccs_socket_create_permission(int family, int type, int protocol) |
401 |
|
{ |
402 |
|
/* Nothing to do if I am a kernel service. */ |
403 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
404 |
|
return 0; |
405 |
|
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
406 |
|
return -EPERM; |
407 |
|
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
408 |
|
return -EPERM; |
409 |
return 0; |
return 0; |
410 |
} |
} |
411 |
|
|
412 |
int AddNetworkPolicy(char *data, struct domain_info *domain, const struct condition_list *condition, const bool is_delete) |
/* Check permission for listening a TCP socket. */ |
413 |
|
static int __ccs_socket_listen_permission(struct socket *sock) |
414 |
{ |
{ |
415 |
u8 sock_type, operation, record_type; |
struct sockaddr_storage addr; |
416 |
u16 min_address[8], max_address[8]; |
int error = 0; |
417 |
struct address_group_entry *group = NULL; |
int addr_len; |
418 |
u16 min_port, max_port; |
struct ccs_addr_info address; |
419 |
int count; |
/* Nothing to do if I am a kernel service. */ |
420 |
char *cp1 = NULL, *cp2 = NULL; |
if (segment_eq(get_fs(), KERNEL_DS)) |
421 |
if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++; |
return 0; |
422 |
if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM; |
if (sock->type != SOCK_STREAM) |
423 |
else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM; |
return 0; |
424 |
else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW; |
switch (sock->sk->sk_family) { |
425 |
else goto out; |
case PF_INET: |
426 |
if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++; |
case PF_INET6: |
427 |
if (strncmp(cp1, "bind ", 5) == 0) { |
break; |
428 |
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND; |
default: |
429 |
} else if (strncmp(cp1, "connect ", 8) == 0) { |
return 0; |
|
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
|
|
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) { |
|
|
operation = NETWORK_ACL_TCP_LISTEN; |
|
|
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) { |
|
|
operation = NETWORK_ACL_TCP_ACCEPT; |
|
|
} else { |
|
|
goto out; |
|
430 |
} |
} |
431 |
if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0'; |
if (sock->ops->getname(sock, (struct sockaddr *) &addr, &addr_len, 0)) |
432 |
if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
return -EPERM; |
433 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
434 |
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
address.operation = CCS_NETWORK_LISTEN; |
435 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
if (ccs_check_address((struct sockaddr *) &addr, addr_len, &address)) |
436 |
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
error = ccs_network_entry(&address); |
437 |
int i; |
return error; |
438 |
for (i = 0; i < 8; i++) { |
} |
439 |
min_address[i] = htons(min_address[i]); |
|
440 |
max_address[i] = htons(max_address[i]); |
/* Check permission for setting the remote IP address/port pair of a socket. */ |
441 |
} |
static int __ccs_socket_connect_permission(struct socket *sock, |
442 |
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
struct sockaddr *addr, int addr_len) |
443 |
record_type = IP_RECORD_TYPE_IPv6; |
{ |
444 |
} else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
int error = 0; |
445 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
const unsigned int type = sock->type; |
446 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
struct ccs_addr_info address; |
447 |
u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
/* Nothing to do if I am a kernel service. */ |
448 |
* (u32 *) min_address = ip; |
if (segment_eq(get_fs(), KERNEL_DS)) |
449 |
if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
return 0; |
450 |
* (u32 *) max_address = ip; |
switch (type) { |
451 |
record_type = IP_RECORD_TYPE_IPv4; |
case SOCK_STREAM: |
452 |
} else if (*cp2 == '@') { |
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
453 |
if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM; |
break; |
454 |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
case SOCK_DGRAM: |
455 |
} else { |
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
456 |
goto out; |
break; |
457 |
|
case SOCK_RAW: |
458 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
459 |
|
break; |
460 |
|
default: |
461 |
|
return 0; |
462 |
} |
} |
463 |
if (strchr(cp1, ' ')) goto out; |
address.operation = CCS_NETWORK_CONNECT; |
464 |
if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) { |
if (!ccs_check_address(addr, addr_len, &address)) |
465 |
if (count == 1) max_port = min_port; |
goto skip; |
466 |
return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, condition, is_delete); |
if (type == SOCK_RAW) |
467 |
|
address.port = htons(sock->sk->sk_protocol); |
468 |
|
error = ccs_network_entry(&address); |
469 |
|
skip: |
470 |
|
return error; |
471 |
|
} |
472 |
|
|
473 |
|
/* Check permission for setting the local IP address/port pair of a socket. */ |
474 |
|
static int __ccs_socket_bind_permission(struct socket *sock, |
475 |
|
struct sockaddr *addr, int addr_len) |
476 |
|
{ |
477 |
|
int error = 0; |
478 |
|
const unsigned int type = sock->type; |
479 |
|
struct ccs_addr_info address; |
480 |
|
/* Nothing to do if I am a kernel service. */ |
481 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
482 |
|
return 0; |
483 |
|
switch (type) { |
484 |
|
case SOCK_STREAM: |
485 |
|
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
486 |
|
break; |
487 |
|
case SOCK_DGRAM: |
488 |
|
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
489 |
|
break; |
490 |
|
case SOCK_RAW: |
491 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
492 |
|
break; |
493 |
|
default: |
494 |
|
return 0; |
495 |
} |
} |
496 |
out: ; |
address.operation = CCS_NETWORK_BIND; |
497 |
return -EINVAL; |
if (!ccs_check_address(addr, addr_len, &address)) |
498 |
|
goto skip; |
499 |
|
if (type == SOCK_RAW) |
500 |
|
address.port = htons(sock->sk->sk_protocol); |
501 |
|
error = ccs_network_entry(&address); |
502 |
|
skip: |
503 |
|
return error; |
504 |
} |
} |
505 |
|
|
506 |
int CheckNetworkListenACL(const _Bool is_ipv6, const u8 *address, const u16 port) |
/* Check permission for accepting a TCP socket. */ |
507 |
{ |
static int __ccs_socket_post_accept_permission(struct socket *sock, |
508 |
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port)); |
struct socket *newsock) |
509 |
|
{ |
510 |
|
struct sockaddr_storage addr; |
511 |
|
struct task_struct * const task = current; |
512 |
|
int error = 0; |
513 |
|
int addr_len; |
514 |
|
struct ccs_addr_info address; |
515 |
|
/* Nothing to do if I am a kernel service. */ |
516 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
517 |
|
return 0; |
518 |
|
switch (newsock->sk->sk_family) { |
519 |
|
case PF_INET: |
520 |
|
case PF_INET6: |
521 |
|
break; |
522 |
|
default: |
523 |
|
return 0; |
524 |
|
} |
525 |
|
error = newsock->ops->getname(newsock, (struct sockaddr *) &addr, |
526 |
|
&addr_len, 2); |
527 |
|
if (error) |
528 |
|
return error; |
529 |
|
if (!ccs_check_address((struct sockaddr *) &addr, addr_len, |
530 |
|
&address)) |
531 |
|
goto skip; |
532 |
|
address.protocol = CCS_NETWORK_TCP_PROTOCOL; |
533 |
|
address.operation = CCS_NETWORK_ACCEPT; |
534 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
535 |
|
error = ccs_network_entry(&address); |
536 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
537 |
|
skip: |
538 |
|
return error; |
539 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkListenACL); |
|
540 |
|
|
541 |
int CheckNetworkConnectACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
542 |
{ |
static int __ccs_socket_sendmsg_permission(struct socket *sock, |
543 |
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)); |
struct msghdr *msg, int size) |
544 |
|
{ |
545 |
|
int error = 0; |
546 |
|
const int type = sock->type; |
547 |
|
struct ccs_addr_info address; |
548 |
|
if (!msg->msg_name) |
549 |
|
return 0; |
550 |
|
/* Nothing to do if I am a kernel service. */ |
551 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
552 |
|
return 0; |
553 |
|
switch (type) { |
554 |
|
case SOCK_DGRAM: |
555 |
|
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
556 |
|
break; |
557 |
|
case SOCK_RAW: |
558 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
559 |
|
break; |
560 |
|
default: |
561 |
|
return 0; |
562 |
|
} |
563 |
|
address.operation = CCS_NETWORK_SEND; |
564 |
|
if (!ccs_check_address((struct sockaddr *) msg->msg_name, |
565 |
|
msg->msg_namelen, &address)) |
566 |
|
goto skip; |
567 |
|
if (type == SOCK_RAW) |
568 |
|
address.port = htons(sock->sk->sk_protocol); |
569 |
|
error = ccs_network_entry(&address); |
570 |
|
skip: |
571 |
|
return error; |
572 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkConnectACL); |
|
573 |
|
|
574 |
int CheckNetworkBindACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
575 |
|
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
576 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
577 |
|
|
578 |
|
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
579 |
{ |
{ |
580 |
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)); |
return skb->nh.iph; |
581 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkBindACL); |
|
582 |
|
|
583 |
int CheckNetworkAcceptACL(const _Bool is_ipv6, const u8 *address, const u16 port) |
static inline struct udphdr *udp_hdr(const struct sk_buff *skb) |
584 |
{ |
{ |
585 |
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; |
|
586 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkAcceptACL); |
|
587 |
|
|
588 |
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) |
589 |
{ |
{ |
590 |
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; |
591 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkSendMsgACL); |
|
592 |
|
|
593 |
int CheckNetworkRecvMsgACL(const _Bool is_ipv6, const int sock_type, const u8 *address, const u16 port) |
#endif |
594 |
|
#endif |
595 |
|
#endif |
596 |
|
|
597 |
|
/* Check permission for receiving a datagram via a UDP or RAW socket. */ |
598 |
|
static int __ccs_socket_post_recvmsg_permission(struct sock *sk, |
599 |
|
struct sk_buff *skb) |
600 |
|
{ |
601 |
|
struct task_struct * const task = current; |
602 |
|
int error = 0; |
603 |
|
const unsigned int type = sk->sk_type; |
604 |
|
struct ccs_addr_info address; |
605 |
|
union { |
606 |
|
struct in6_addr sin6; |
607 |
|
struct in_addr sin4; |
608 |
|
} ip_address; |
609 |
|
switch (type) { |
610 |
|
case SOCK_DGRAM: |
611 |
|
address.protocol = CCS_NETWORK_UDP_PROTOCOL; |
612 |
|
break; |
613 |
|
case SOCK_RAW: |
614 |
|
address.protocol = CCS_NETWORK_RAW_PROTOCOL; |
615 |
|
break; |
616 |
|
default: |
617 |
|
return 0; |
618 |
|
} |
619 |
|
/* Nothing to do if I am a kernel service. */ |
620 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
621 |
|
return 0; |
622 |
|
switch (sk->sk_family) { |
623 |
|
case PF_INET6: |
624 |
|
address.is_ipv6 = true; |
625 |
|
if (type == SOCK_DGRAM && skb->protocol == htons(ETH_P_IP)) |
626 |
|
ipv6_addr_set(&ip_address.sin6, 0, 0, htonl(0xffff), |
627 |
|
ip_hdr(skb)->saddr); |
628 |
|
else |
629 |
|
ipv6_addr_copy(&ip_address.sin6, &ipv6_hdr(skb)->saddr); |
630 |
|
break; |
631 |
|
case PF_INET: |
632 |
|
address.is_ipv6 = false; |
633 |
|
ip_address.sin4.s_addr = ip_hdr(skb)->saddr; |
634 |
|
break; |
635 |
|
default: |
636 |
|
goto skip; |
637 |
|
} |
638 |
|
address.address = (u32 *) &ip_address; |
639 |
|
if (type == SOCK_DGRAM) |
640 |
|
address.port = udp_hdr(skb)->source; |
641 |
|
else |
642 |
|
address.port = htons(sk->sk_protocol); |
643 |
|
address.operation = CCS_NETWORK_RECV; |
644 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
645 |
|
error = ccs_network_entry(&address); |
646 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
647 |
|
skip: |
648 |
|
return error; |
649 |
|
} |
650 |
|
|
651 |
|
void __init ccs_network_init(void) |
652 |
{ |
{ |
653 |
int retval; |
ccsecurity_ops.socket_create_permission = |
654 |
current->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
__ccs_socket_create_permission; |
655 |
retval = CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
ccsecurity_ops.socket_listen_permission = |
656 |
current->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
__ccs_socket_listen_permission; |
657 |
return retval; |
ccsecurity_ops.socket_connect_permission = |
658 |
|
__ccs_socket_connect_permission; |
659 |
|
ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission; |
660 |
|
ccsecurity_ops.socket_post_accept_permission = |
661 |
|
__ccs_socket_post_accept_permission; |
662 |
|
ccsecurity_ops.socket_sendmsg_permission = |
663 |
|
__ccs_socket_sendmsg_permission; |
664 |
|
ccsecurity_ops.socket_post_recvmsg_permission = |
665 |
|
__ccs_socket_post_recvmsg_permission; |
666 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkRecvMsgACL); |
|
667 |
|
|
668 |
/***** TOMOYO Linux end. *****/ |
#endif |