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