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