1 |
/* |
/* |
2 |
* fs/tomoyo_network.c |
* security/ccsecurity/network.c |
3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Copyright (C) 2005-2007 NTT DATA CORPORATION |
* Version: 1.7.0 2009/09/03 |
|
* |
|
|
* Version: 1.5.0 2007/09/20 |
|
7 |
* |
* |
8 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
9 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
10 |
* |
* |
11 |
*/ |
*/ |
|
/***** TOMOYO Linux start. *****/ |
|
12 |
|
|
13 |
#include <linux/ccs_common.h> |
#include <linux/net.h> |
14 |
#include <linux/tomoyo.h> |
#include <linux/inet.h> |
15 |
#include <linux/realpath.h> |
#include <linux/in.h> |
16 |
|
#include <linux/in6.h> |
17 |
#include <net/ip.h> |
#include <net/ip.h> |
18 |
|
#include <net/ipv6.h> |
19 |
|
#include <net/udp.h> |
20 |
|
#include "internal.h" |
21 |
|
|
22 |
/************************* VARIABLES *************************/ |
/** |
23 |
|
* ccs_audit_network_log - Audit network log. |
24 |
extern struct semaphore domain_acl_lock; |
* |
25 |
|
* @r: Pointer to "struct ccs_request_info". |
26 |
/************************* AUDIT FUNCTIONS *************************/ |
* @operation: The name of operation. |
27 |
|
* @address: An IPv4 or IPv6 address. |
28 |
static int AuditNetworkLog(const int is_ipv6, const char *operation, const u32 *address, const u16 port, const int is_granted) |
* @port: Port number. |
29 |
{ |
* @is_granted: True if this is a granted log. |
30 |
char *buf; |
* |
31 |
int len = 256; |
* Returns 0 on success, negative value otherwise. |
32 |
if (CanSaveAuditLog(is_granted) < 0) return -ENOMEM; |
*/ |
33 |
if ((buf = InitAuditLog(&len)) == NULL) return -ENOMEM; |
static int ccs_audit_network_log(struct ccs_request_info *r, |
34 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, KEYWORD_ALLOW_NETWORK "%s ", operation); |
const char *operation, const char *address, |
35 |
if (is_ipv6) { |
const u16 port, const bool is_granted) |
36 |
print_ipv6(buf + strlen(buf), len - strlen(buf), (const u16 *) address); |
{ |
37 |
} else { |
if (!is_granted) |
38 |
u32 ip = *address; |
ccs_warn_log(r, "%s %s %u", operation, address, port); |
39 |
snprintf(buf + strlen(buf), len - strlen(buf) - 1, "%u.%u.%u.%u", NIPQUAD(ip)); |
return ccs_write_audit_log(is_granted, r, CCS_KEYWORD_ALLOW_NETWORK |
40 |
} |
"%s %s %u\n", operation, address, port); |
|
snprintf(buf + strlen(buf), len - strlen(buf) - 1, " %u\n", port); |
|
|
return WriteAuditLog(buf, is_granted); |
|
|
} |
|
|
|
|
|
/************************* ADDRESS GROUP HANDLER *************************/ |
|
|
|
|
|
static struct address_group_entry *group_list = NULL; |
|
|
|
|
|
static int AddAddressGroupEntry(const char *group_name, const u8 is_ipv6, const u16 *min_address, const u16 *max_address, const int is_delete) |
|
|
{ |
|
|
static DECLARE_MUTEX(lock); |
|
|
struct address_group_entry *new_group, *group; |
|
|
struct address_group_member *new_member, *member; |
|
|
const struct path_info *saved_group_name; |
|
|
int error = -ENOMEM; |
|
|
if (!IsCorrectPath(group_name, 0, 0, 0, __FUNCTION__) || !group_name[0]) return -EINVAL; |
|
|
if ((saved_group_name = SaveName(group_name)) == NULL) return -ENOMEM; |
|
|
down(&lock); |
|
|
for (group = group_list; group; group = group->next) { |
|
|
if (saved_group_name != group->group_name) continue; |
|
|
for (member = group->first_member; member; member = member->next) { |
|
|
if (member->is_ipv6 != is_ipv6) continue; |
|
|
if (is_ipv6) { |
|
|
if (memcmp(member->min.ipv6, min_address, 16) || memcmp(member->max.ipv6, max_address, 16)) 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; |
|
|
} |
|
|
break; |
|
|
} |
|
|
if (is_delete) { |
|
|
error = -ENOENT; |
|
|
goto out; |
|
|
} |
|
|
if (!group) { |
|
|
if ((new_group = alloc_element(sizeof(*new_group))) == NULL) goto out; |
|
|
new_group->group_name = saved_group_name; |
|
|
mb(); /* Instead of using spinlock. */ |
|
|
if ((group = group_list) != NULL) { |
|
|
while (group->next) group = group->next; group->next = new_group; |
|
|
} else { |
|
|
group_list= new_group; |
|
|
} |
|
|
group = new_group; |
|
|
} |
|
|
if ((new_member = alloc_element(sizeof(*new_member))) == NULL) goto out; |
|
|
new_member->is_ipv6 = is_ipv6; |
|
|
if (is_ipv6) { |
|
|
memmove(new_member->min.ipv6, min_address, 16); |
|
|
memmove(new_member->max.ipv6, max_address, 16); |
|
|
} 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; |
|
|
} else { |
|
|
group->first_member = new_member; |
|
|
} |
|
|
error = 0; |
|
|
out: |
|
|
up(&lock); |
|
|
return error; |
|
41 |
} |
} |
42 |
|
|
43 |
int AddAddressGroupPolicy(char *data, const int is_delete) |
/** |
44 |
|
* ccs_parse_ip_address - Parse an IP address. |
45 |
|
* |
46 |
|
* @address: String to parse. |
47 |
|
* @min: Pointer to store min address. |
48 |
|
* @max: Pointer to store max address. |
49 |
|
* |
50 |
|
* Returns 2 if @address is an IPv6, 1 if @address is an IPv4, 0 otherwise. |
51 |
|
*/ |
52 |
|
int ccs_parse_ip_address(char *address, u16 *min, u16 *max) |
53 |
{ |
{ |
54 |
int count, is_ipv6; |
int count = sscanf(address, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx" |
55 |
u16 min_address[8], max_address[8]; |
"-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
56 |
char *cp = strchr(data, ' '); |
&min[0], &min[1], &min[2], &min[3], |
57 |
if (!cp) return -EINVAL; |
&min[4], &min[5], &min[6], &min[7], |
58 |
*cp++ = '\0'; |
&max[0], &max[1], &max[2], &max[3], |
59 |
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]); |
60 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
if (count == 8 || count == 16) { |
61 |
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
u8 i; |
62 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
if (count == 8) |
63 |
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
memmove(max, min, sizeof(u16) * 8); |
|
int i; |
|
64 |
for (i = 0; i < 8; i++) { |
for (i = 0; i < 8; i++) { |
65 |
min_address[i] = htons(min_address[i]); |
min[i] = htons(min[i]); |
66 |
max_address[i] = htons(max_address[i]); |
max[i] = htons(max[i]); |
|
} |
|
|
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
|
|
is_ipv6 = 1; |
|
|
} else if ((count = sscanf(cp, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
|
|
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
|
|
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
|
|
u32 ip = ((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
|
|
* (u32 *) min_address = ip; |
|
|
if (count == 8) ip = ((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
|
|
* (u32 *) max_address = ip; |
|
|
is_ipv6 = 0; |
|
|
} else { |
|
|
return -EINVAL; |
|
|
} |
|
|
return AddAddressGroupEntry(data, is_ipv6, min_address, max_address, is_delete); |
|
|
} |
|
|
|
|
|
static struct address_group_entry *FindOrAssignNewAddressGroup(const char *group_name) |
|
|
{ |
|
|
int i; |
|
|
struct address_group_entry *group; |
|
|
for (i = 0; i <= 1; i++) { |
|
|
for (group = group_list; group; group = group->next) { |
|
|
if (strcmp(group_name, group->group_name->name) == 0) return group; |
|
|
} |
|
|
if (i == 0) { |
|
|
const u16 dummy[2] = { 0, 0 }; |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 0); |
|
|
AddAddressGroupEntry(group_name, 0, dummy, dummy, 1); |
|
67 |
} |
} |
68 |
|
return 2; |
69 |
} |
} |
70 |
return NULL; |
count = sscanf(address, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
71 |
} |
&min[0], &min[1], &min[2], &min[3], |
72 |
|
&max[0], &max[1], &max[2], &max[3]); |
73 |
static int AddressMatchesToGroup(const u8 is_ipv6, const u32 *address, const struct address_group_entry *group) |
if (count == 4 || count == 8) { |
74 |
{ |
u32 ip = htonl((((u8) min[0]) << 24) + (((u8) min[1]) << 16) |
75 |
struct address_group_member *member; |
+ (((u8) min[2]) << 8) + (u8) min[3]); |
76 |
const u32 ip = ntohl(*address); |
memmove(min, &ip, sizeof(ip)); |
77 |
for (member = group->first_member; member; member = member->next) { |
if (count == 8) |
78 |
if (member->is_deleted) continue; |
ip = htonl((((u8) max[0]) << 24) + (((u8) max[1]) << 16) |
79 |
if (member->is_ipv6) { |
+ (((u8) max[2]) << 8) + (u8) max[3]); |
80 |
if (is_ipv6 && memcmp(member->min.ipv6, address, 16) <= 0 && memcmp(address, member->max.ipv6, 16) <= 0) return 1; |
memmove(max, &ip, sizeof(ip)); |
81 |
} else { |
return 1; |
|
if (!is_ipv6 && member->min.ipv4 <= ip && ip <= member->max.ipv4) return 1; |
|
|
} |
|
82 |
} |
} |
83 |
return 0; |
return 0; |
84 |
} |
} |
85 |
|
|
86 |
int ReadAddressGroupPolicy(struct io_buffer *head) |
#if !defined(NIP6) |
87 |
{ |
#define NIP6(addr) \ |
88 |
struct address_group_entry *group = head->read_var1; |
ntohs((addr).s6_addr16[0]), ntohs((addr).s6_addr16[1]), \ |
89 |
struct address_group_member *member = head->read_var2; |
ntohs((addr).s6_addr16[2]), ntohs((addr).s6_addr16[3]), \ |
90 |
if (!group) group = group_list; |
ntohs((addr).s6_addr16[4]), ntohs((addr).s6_addr16[5]), \ |
91 |
while (group) { |
ntohs((addr).s6_addr16[6]), ntohs((addr).s6_addr16[7]) |
92 |
head->read_var1 = group; |
#endif |
|
if (!member) member = group->first_member; |
|
|
while (member) { |
|
|
head->read_var2 = member; |
|
|
if (!member->is_deleted) { |
|
|
char buf[128]; |
|
|
if (member->is_ipv6) { |
|
|
const u16 *min_address = member->min.ipv6, *max_address = member->max.ipv6; |
|
|
print_ipv6(buf, sizeof(buf), min_address); |
|
|
if (memcmp(min_address, max_address, 16)) { |
|
|
char *cp = strchr(buf, '\0'); |
|
|
*cp++ = '-'; |
|
|
print_ipv6(cp, sizeof(buf) - strlen(buf), max_address); |
|
|
} |
|
|
} else { |
|
|
const u32 min_address = member->min.ipv4, max_address = member->max.ipv4; |
|
|
memset(buf, 0, sizeof(buf)); |
|
|
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(min_address)); |
|
|
if (min_address != max_address) { |
|
|
const int len = strlen(buf); |
|
|
snprintf(buf + len, sizeof(buf) - 1 - len, "-%u.%u.%u.%u", HIPQUAD(max_address)); |
|
|
} |
|
|
} |
|
|
if (io_printf(head, KEYWORD_ADDRESS_GROUP "%s %s\n", group->group_name->name, buf)) break; |
|
|
} |
|
|
member = member->next; |
|
|
} |
|
|
if (member) break; |
|
|
head->read_var2 = NULL; |
|
|
group = group->next; |
|
|
} |
|
|
return group ? -ENOMEM : 0; |
|
|
} |
|
|
|
|
|
/************************* NETWORK NETWORK ACL HANDLER *************************/ |
|
93 |
|
|
94 |
char *print_ipv6(char *buffer, const int buffer_len, const u16 *ip) |
/** |
95 |
|
* ccs_print_ipv6 - Print an IPv6 address. |
96 |
|
* |
97 |
|
* @buffer: Buffer to write to. |
98 |
|
* @buffer_len: Size of @buffer. |
99 |
|
* @ip: Pointer to "struct in6_addr". |
100 |
|
* |
101 |
|
* Returns nothing. |
102 |
|
*/ |
103 |
|
void ccs_print_ipv6(char *buffer, const int buffer_len, |
104 |
|
const struct in6_addr *ip) |
105 |
{ |
{ |
106 |
memset(buffer, 0, buffer_len); |
memset(buffer, 0, buffer_len); |
107 |
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; |
|
108 |
} |
} |
109 |
|
|
110 |
const char *network2keyword(const unsigned int operation) |
/** |
111 |
|
* ccs_net2keyword - Convert network operation index to network operation name. |
112 |
|
* |
113 |
|
* @operation: Type of operation. |
114 |
|
* |
115 |
|
* Returns the name of operation. |
116 |
|
*/ |
117 |
|
const char *ccs_net2keyword(const u8 operation) |
118 |
{ |
{ |
119 |
const char *keyword = "unknown"; |
const char *keyword = "unknown"; |
120 |
switch (operation) { |
switch (operation) { |
121 |
case NETWORK_ACL_UDP_BIND: |
case CCS_NETWORK_UDP_BIND: |
122 |
keyword = "UDP bind"; |
keyword = "UDP bind"; |
123 |
break; |
break; |
124 |
case NETWORK_ACL_UDP_CONNECT: |
case CCS_NETWORK_UDP_CONNECT: |
125 |
keyword = "UDP connect"; |
keyword = "UDP connect"; |
126 |
break; |
break; |
127 |
case NETWORK_ACL_TCP_BIND: |
case CCS_NETWORK_TCP_BIND: |
128 |
keyword = "TCP bind"; |
keyword = "TCP bind"; |
129 |
break; |
break; |
130 |
case NETWORK_ACL_TCP_LISTEN: |
case CCS_NETWORK_TCP_LISTEN: |
131 |
keyword = "TCP listen"; |
keyword = "TCP listen"; |
132 |
break; |
break; |
133 |
case NETWORK_ACL_TCP_CONNECT: |
case CCS_NETWORK_TCP_CONNECT: |
134 |
keyword = "TCP connect"; |
keyword = "TCP connect"; |
135 |
break; |
break; |
136 |
case NETWORK_ACL_TCP_ACCEPT: |
case CCS_NETWORK_TCP_ACCEPT: |
137 |
keyword = "TCP accept"; |
keyword = "TCP accept"; |
138 |
break; |
break; |
139 |
case NETWORK_ACL_RAW_BIND: |
case CCS_NETWORK_RAW_BIND: |
140 |
keyword = "RAW bind"; |
keyword = "RAW bind"; |
141 |
break; |
break; |
142 |
case NETWORK_ACL_RAW_CONNECT: |
case CCS_NETWORK_RAW_CONNECT: |
143 |
keyword = "RAW connect"; |
keyword = "RAW connect"; |
144 |
break; |
break; |
145 |
} |
} |
146 |
return keyword; |
return keyword; |
147 |
} |
} |
148 |
|
|
149 |
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) |
/** |
150 |
|
* ccs_network_entry2 - Check permission for network operation. |
151 |
|
* |
152 |
|
* @is_ipv6: True if @address is an IPv6 address. |
153 |
|
* @operation: Type of operation. |
154 |
|
* @address: An IPv4 or IPv6 address. |
155 |
|
* @port: Port number. |
156 |
|
* |
157 |
|
* Returns 0 on success, negative value otherwise. |
158 |
|
* |
159 |
|
* Caller holds ccs_read_lock(). |
160 |
|
*/ |
161 |
|
static int ccs_network_entry2(const bool is_ipv6, const u8 operation, |
162 |
|
const u32 *address, const u16 port) |
163 |
{ |
{ |
164 |
struct acl_info *ptr; |
struct ccs_request_info r; |
165 |
int error = -ENOMEM; |
struct ccs_acl_info *ptr; |
166 |
const u32 min_ip = ntohl(*min_address), max_ip = ntohl(*max_address); /* using host byte order to allow u32 comparison than memcmp().*/ |
const char *keyword = ccs_net2keyword(operation); |
167 |
if (!domain) return -EINVAL; |
const u16 perm = 1 << operation; |
168 |
down(&domain_acl_lock); |
/* using host byte order to allow u32 comparison than memcmp().*/ |
169 |
if (is_add) { |
const u32 ip = ntohl(*address); |
170 |
if ((ptr = domain->first_acl_ptr) == NULL) goto first_entry; |
int error; |
171 |
while (1) { |
char buf[64]; |
172 |
struct ip_network_acl_record *new_ptr = (struct ip_network_acl_record *) ptr; |
if (ccs_init_request_info(&r, NULL, |
173 |
if (ptr->type == TYPE_IP_NETWORK_ACL && new_ptr->operation_type == operation && new_ptr->record_type == record_type && ptr->cond == condition && new_ptr->min_port == min_port && max_port == new_ptr->max_port) { |
CCS_MAC_NETWORK_UDP_BIND + operation) |
174 |
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
== CCS_CONFIG_DISABLED) |
175 |
if (new_ptr->u.group == group) { |
return 0; |
176 |
ptr->is_deleted = 0; |
memset(buf, 0, sizeof(buf)); |
177 |
/* Found. Nothing to do. */ |
if (is_ipv6) |
178 |
error = 0; |
ccs_print_ipv6(buf, sizeof(buf), (const struct in6_addr *) |
179 |
break; |
address); |
180 |
} |
else |
181 |
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
snprintf(buf, sizeof(buf) - 1, "%u.%u.%u.%u", HIPQUAD(ip)); |
182 |
if (new_ptr->u.ipv4.min == min_ip && max_ip == new_ptr->u.ipv4.max) { |
do { |
183 |
ptr->is_deleted = 0; |
error = -EPERM; |
184 |
/* Found. Nothing to do. */ |
list_for_each_entry_rcu(ptr, &r.domain->acl_info_list, list) { |
185 |
error = 0; |
struct ccs_ip_network_acl *acl; |
186 |
break; |
if (ptr->is_deleted || |
187 |
} |
ptr->type != CCS_TYPE_IP_NETWORK_ACL) |
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (memcmp(new_ptr->u.ipv6.min, min_address, 16) == 0 && memcmp(max_address, new_ptr->u.ipv6.max, 16) == 0) { |
|
|
ptr->is_deleted = 0; |
|
|
/* Found. Nothing to do. */ |
|
|
error = 0; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
if (ptr->next) { |
|
|
ptr = ptr->next; |
|
188 |
continue; |
continue; |
189 |
|
acl = container_of(ptr, struct ccs_ip_network_acl, |
190 |
|
head); |
191 |
|
if (!(acl->perm & perm)) |
192 |
|
continue; |
193 |
|
if (!ccs_compare_number_union(port, &acl->port) || |
194 |
|
!ccs_condition(&r, ptr)) |
195 |
|
continue; |
196 |
|
switch (acl->address_type) { |
197 |
|
case CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP: |
198 |
|
if (!ccs_address_matches_group(is_ipv6, |
199 |
|
address, |
200 |
|
acl->address. |
201 |
|
group)) |
202 |
|
continue; |
203 |
|
break; |
204 |
|
case CCS_IP_ADDRESS_TYPE_IPv4: |
205 |
|
if (is_ipv6 || ip < acl->address.ipv4.min || |
206 |
|
acl->address.ipv4.max < ip) |
207 |
|
continue; |
208 |
|
break; |
209 |
|
default: |
210 |
|
if (!is_ipv6 || |
211 |
|
memcmp(acl->address.ipv6.min, address, 16) |
212 |
|
> 0 || |
213 |
|
memcmp(address, acl->address.ipv6.max, 16) |
214 |
|
> 0) |
215 |
|
continue; |
216 |
|
break; |
217 |
} |
} |
218 |
first_entry: ; |
r.cond = ptr->cond; |
219 |
if (is_add == 1 && TooManyDomainACL(domain)) break; |
error = 0; |
|
/* 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->operation_type = operation; |
|
|
new_ptr->record_type = 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); |
|
220 |
break; |
break; |
221 |
} |
} |
222 |
} else { |
ccs_audit_network_log(&r, keyword, buf, port, !error); |
223 |
error = -ENOENT; |
if (!error) |
|
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) { |
|
|
struct ip_network_acl_record *ptr2 = (struct ip_network_acl_record *) ptr; |
|
|
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || ptr2->operation_type != operation || ptr2->record_type != record_type || ptr->cond != condition || ptr2->min_port != min_port || ptr2->max_port != max_port) continue; |
|
|
if (record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
|
|
if (ptr2->u.group != group) continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv4) { |
|
|
if (ptr2->u.ipv4.min != min_ip || max_ip != ptr2->u.ipv4.max) continue; |
|
|
} else if (record_type == IP_RECORD_TYPE_IPv6) { |
|
|
if (memcmp(ptr2->u.ipv6.min, min_address, 16) || memcmp(max_address, ptr2->u.ipv6.max, 16)) continue; |
|
|
} |
|
|
error = DelDomainACL(ptr); |
|
224 |
break; |
break; |
225 |
} |
error = ccs_supervisor(&r, CCS_KEYWORD_ALLOW_NETWORK |
226 |
} |
"%s %s %u\n", keyword, buf, port); |
227 |
up(&domain_acl_lock); |
} while (error == 1); |
228 |
|
if (r.mode != CCS_CONFIG_ENFORCING) |
229 |
|
error = 0; |
230 |
return error; |
return error; |
231 |
} |
} |
232 |
|
|
233 |
static int CheckNetworkEntry(const int is_ipv6, const int operation, const u32 *address, const u16 port) |
/** |
234 |
|
* ccs_network_entry - Check permission for network operation. |
235 |
|
* |
236 |
|
* @is_ipv6: True if @address is an IPv6 address. |
237 |
|
* @operation: Type of operation. |
238 |
|
* @address: An IPv4 or IPv6 address. |
239 |
|
* @port: Port number. |
240 |
|
* |
241 |
|
* Returns 0 on success, negative value otherwise. |
242 |
|
*/ |
243 |
|
static int ccs_network_entry(const bool is_ipv6, const u8 operation, |
244 |
|
const u32 *address, const u16 port) |
245 |
{ |
{ |
246 |
struct domain_info * const domain = current->domain_info; |
const int idx = ccs_read_lock(); |
247 |
struct acl_info *ptr; |
const int error = ccs_network_entry2(is_ipv6, operation, |
248 |
const char *keyword = network2keyword(operation); |
address, port); |
249 |
const int is_enforce = CheckCCSEnforce(CCS_TOMOYO_MAC_FOR_NETWORK); |
ccs_read_unlock(idx); |
250 |
const u32 ip = ntohl(*address); /* using host byte order to allow u32 comparison than memcmp().*/ |
return error; |
251 |
if (!CheckCCSFlags(CCS_TOMOYO_MAC_FOR_NETWORK)) return 0; |
} |
252 |
for (ptr = domain->first_acl_ptr; ptr; ptr = ptr->next) { |
|
253 |
struct ip_network_acl_record *ptr2 = (struct ip_network_acl_record *) ptr; |
/** |
254 |
if (ptr->type != TYPE_IP_NETWORK_ACL || ptr->is_deleted || ptr2->operation_type != operation || port < ptr2->min_port || ptr2->max_port < port || CheckCondition(ptr->cond, NULL)) continue; |
* ccs_write_network_policy - Write "struct ccs_ip_network_acl" list. |
255 |
if (ptr2->record_type == IP_RECORD_TYPE_ADDRESS_GROUP) { |
* |
256 |
if (AddressMatchesToGroup(is_ipv6, address, ptr2->u.group)) break; |
* @data: String to parse. |
257 |
} else if (ptr2->record_type == IP_RECORD_TYPE_IPv4) { |
* @domain: Pointer to "struct ccs_domain_info". |
258 |
if (!is_ipv6 && ptr2->u.ipv4.min <= ip && ip <= ptr2->u.ipv4.max) break; |
* @condition: Pointer to "struct ccs_condition". May be NULL. |
259 |
} else { |
* @is_delete: True if it is a delete request. |
260 |
if (is_ipv6 && memcmp(ptr2->u.ipv6.min, address, 16) <= 0 && memcmp(address, ptr2->u.ipv6.max, 16) <= 0) break; |
* |
261 |
|
* Returns 0 on success, negative value otherwise. |
262 |
|
*/ |
263 |
|
int ccs_write_network_policy(char *data, struct ccs_domain_info *domain, |
264 |
|
struct ccs_condition *condition, |
265 |
|
const bool is_delete) |
266 |
|
{ |
267 |
|
struct ccs_ip_network_acl *entry = NULL; |
268 |
|
struct ccs_acl_info *ptr; |
269 |
|
struct ccs_ip_network_acl e = { |
270 |
|
.head.type = CCS_TYPE_IP_NETWORK_ACL, |
271 |
|
.head.cond = condition, |
272 |
|
}; |
273 |
|
u16 min_address[8]; |
274 |
|
u16 max_address[8]; |
275 |
|
int error = is_delete ? -ENOENT : -ENOMEM; |
276 |
|
u8 sock_type; |
277 |
|
char *w[4]; |
278 |
|
if (!ccs_tokenize(data, w, sizeof(w)) || !w[3][0]) |
279 |
|
return -EINVAL; |
280 |
|
if (!strcmp(w[0], "TCP")) |
281 |
|
sock_type = SOCK_STREAM; |
282 |
|
else if (!strcmp(w[0], "UDP")) |
283 |
|
sock_type = SOCK_DGRAM; |
284 |
|
else if (!strcmp(w[0], "RAW")) |
285 |
|
sock_type = SOCK_RAW; |
286 |
|
else |
287 |
|
return -EINVAL; |
288 |
|
if (!strcmp(w[1], "bind")) |
289 |
|
switch (sock_type) { |
290 |
|
case SOCK_STREAM: |
291 |
|
e.perm = 1 << CCS_NETWORK_TCP_BIND; |
292 |
|
break; |
293 |
|
case SOCK_DGRAM: |
294 |
|
e.perm = 1 << CCS_NETWORK_UDP_BIND; |
295 |
|
break; |
296 |
|
default: |
297 |
|
e.perm = 1 << CCS_NETWORK_RAW_BIND; |
298 |
|
break; |
299 |
} |
} |
300 |
|
else if (!strcmp(w[1], "connect")) |
301 |
|
switch (sock_type) { |
302 |
|
case SOCK_STREAM: |
303 |
|
e.perm = 1 << CCS_NETWORK_TCP_CONNECT; |
304 |
|
break; |
305 |
|
case SOCK_DGRAM: |
306 |
|
e.perm = 1 << CCS_NETWORK_UDP_CONNECT; |
307 |
|
break; |
308 |
|
default: |
309 |
|
e.perm = 1 << CCS_NETWORK_RAW_CONNECT; |
310 |
|
break; |
311 |
|
} |
312 |
|
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "listen")) |
313 |
|
e.perm = 1 << CCS_NETWORK_TCP_LISTEN; |
314 |
|
else if (sock_type == SOCK_STREAM && !strcmp(w[1], "accept")) |
315 |
|
e.perm = 1 << CCS_NETWORK_TCP_ACCEPT; |
316 |
|
else |
317 |
|
return -EINVAL; |
318 |
|
switch (ccs_parse_ip_address(w[2], min_address, max_address)) { |
319 |
|
case 2: |
320 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_IPv6; |
321 |
|
e.address.ipv6.min = ccs_get_ipv6_address((struct in6_addr *) |
322 |
|
min_address); |
323 |
|
e.address.ipv6.max = ccs_get_ipv6_address((struct in6_addr *) |
324 |
|
max_address); |
325 |
|
if (!e.address.ipv6.min || !e.address.ipv6.max) |
326 |
|
goto out; |
327 |
|
break; |
328 |
|
case 1: |
329 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_IPv4; |
330 |
|
/* use host byte order to allow u32 comparison.*/ |
331 |
|
e.address.ipv4.min = ntohl(*(u32 *) min_address); |
332 |
|
e.address.ipv4.max = ntohl(*(u32 *) max_address); |
333 |
|
break; |
334 |
|
default: |
335 |
|
if (w[2][0] != '@') |
336 |
|
return -EINVAL; |
337 |
|
e.address_type = CCS_IP_ADDRESS_TYPE_ADDRESS_GROUP; |
338 |
|
e.address.group = ccs_get_address_group(w[2] + 1); |
339 |
|
if (!e.address.group) |
340 |
|
return -ENOMEM; |
341 |
|
break; |
342 |
} |
} |
343 |
if (ptr) { |
if (!ccs_parse_number_union(w[3], &e.port)) |
344 |
AuditNetworkLog(is_ipv6, keyword, address, port, 1); |
goto out; |
345 |
return 0; |
if (!is_delete) |
346 |
} |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
347 |
if (TomoyoVerboseMode()) { |
mutex_lock(&ccs_policy_lock); |
348 |
if (is_ipv6) { |
list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
349 |
char buf[64]; |
struct ccs_ip_network_acl *acl = |
350 |
print_ipv6(buf, sizeof(buf), (const u16 *) address); |
container_of(ptr, struct ccs_ip_network_acl, |
351 |
printk("TOMOYO-%s: %s to %s %u denied for %s\n", GetMSG(is_enforce), keyword, buf, port, GetLastName(domain)); |
head); |
352 |
|
if (ptr->type != CCS_TYPE_IP_NETWORK_ACL || |
353 |
|
ptr->cond != condition || |
354 |
|
ccs_memcmp(acl, &e, offsetof(typeof(e), address_type), |
355 |
|
sizeof(e))) |
356 |
|
continue; |
357 |
|
if (is_delete) { |
358 |
|
acl->perm &= ~e.perm; |
359 |
|
if (!acl->perm) |
360 |
|
ptr->is_deleted = true; |
361 |
} else { |
} else { |
362 |
printk("TOMOYO-%s: %s to %u.%u.%u.%u %u denied for %s\n", GetMSG(is_enforce), keyword, HIPQUAD(ip), port, GetLastName(domain)); |
if (ptr->is_deleted) |
363 |
|
acl->perm = 0; |
364 |
|
acl->perm |= e.perm; |
365 |
|
ptr->is_deleted = false; |
366 |
} |
} |
367 |
|
error = 0; |
368 |
|
break; |
369 |
} |
} |
370 |
AuditNetworkLog(is_ipv6, keyword, address, port, 0); |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
371 |
if (is_enforce) { |
ccs_add_domain_acl(domain, &entry->head); |
372 |
if (is_ipv6) { |
entry = NULL; |
373 |
char buf[64]; |
error = 0; |
|
print_ipv6(buf, sizeof(buf), (const u16 *) address); |
|
|
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %s %u\n", domain->domainname->name, keyword, buf, port); |
|
|
} |
|
|
return CheckSupervisor("%s\n" KEYWORD_ALLOW_NETWORK "%s %u.%u.%u.%u %u\n", domain->domainname->name, keyword, HIPQUAD(ip), port); |
|
374 |
} |
} |
375 |
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); |
mutex_unlock(&ccs_policy_lock); |
376 |
return 0; |
out: |
377 |
|
if (w[2][0] == '@') |
378 |
|
ccs_put_address_group(e.address.group); |
379 |
|
else if (e.address_type == CCS_IP_ADDRESS_TYPE_IPv6) { |
380 |
|
ccs_put_ipv6_address(e.address.ipv6.min); |
381 |
|
ccs_put_ipv6_address(e.address.ipv6.max); |
382 |
|
} |
383 |
|
ccs_put_number_union(&e.port); |
384 |
|
kfree(entry); |
385 |
|
return error; |
386 |
} |
} |
387 |
|
|
388 |
int AddNetworkPolicy(char *data, struct domain_info *domain, const int is_delete) |
/** |
389 |
|
* ccs_network_listen_acl - Check permission for listen() operation. |
390 |
|
* |
391 |
|
* @is_ipv6: True if @address is an IPv6 address. |
392 |
|
* @address: An IPv4 or IPv6 address. |
393 |
|
* @port: Port number. |
394 |
|
* |
395 |
|
* Returns 0 on success, negative value otherwise. |
396 |
|
*/ |
397 |
|
static inline int ccs_network_listen_acl(const bool is_ipv6, |
398 |
|
const u8 *address, |
399 |
|
const u16 port) |
400 |
{ |
{ |
401 |
u8 sock_type, operation, record_type; |
return ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_LISTEN, |
402 |
u16 min_address[8], max_address[8]; |
(const u32 *) address, ntohs(port)); |
403 |
struct address_group_entry *group = NULL; |
} |
404 |
u16 min_port, max_port; |
|
405 |
int count; |
/** |
406 |
char *cp1 = NULL, *cp2 = NULL; |
* ccs_network_connect_acl - Check permission for connect() operation. |
407 |
const struct condition_list *condition = NULL; |
* |
408 |
cp1 = FindConditionPart(data); |
* @is_ipv6: True if @address is an IPv6 address. |
409 |
if (cp1 && (condition = FindOrAssignNewCondition(cp1)) == NULL) goto out; |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
410 |
if ((cp1 = strchr(data, ' ')) == NULL) goto out; cp1++; |
* @address: An IPv4 or IPv6 address. |
411 |
if (strncmp(data, "TCP ", 4) == 0) sock_type = SOCK_STREAM; |
* @port: Port number. |
412 |
else if (strncmp(data, "UDP ", 4) == 0) sock_type = SOCK_DGRAM; |
* |
413 |
else if (strncmp(data, "RAW ", 4) == 0) sock_type = SOCK_RAW; |
* Returns 0 on success, negative value otherwise. |
414 |
else goto out; |
*/ |
415 |
if ((cp2 = strchr(cp1, ' ')) == NULL) goto out; cp2++; |
static inline int ccs_network_connect_acl(const bool is_ipv6, |
416 |
if (strncmp(cp1, "bind ", 5) == 0) { |
const int sock_type, |
417 |
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND; |
const u8 *address, |
418 |
} else if (strncmp(cp1, "connect ", 8) == 0) { |
const u16 port) |
419 |
operation = (sock_type == SOCK_STREAM) ? NETWORK_ACL_TCP_CONNECT : (sock_type == SOCK_DGRAM) ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT; |
{ |
420 |
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "listen ", 7) == 0) { |
u8 operation; |
421 |
operation = NETWORK_ACL_TCP_LISTEN; |
switch (sock_type) { |
422 |
} else if (sock_type == SOCK_STREAM && strncmp(cp1, "accept ", 7) == 0) { |
case SOCK_STREAM: |
423 |
operation = NETWORK_ACL_TCP_ACCEPT; |
operation = CCS_NETWORK_TCP_CONNECT; |
424 |
} else { |
break; |
425 |
goto out; |
case SOCK_DGRAM: |
426 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
427 |
|
break; |
428 |
|
default: |
429 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
430 |
} |
} |
431 |
if ((cp1 = strchr(cp2, ' ')) == NULL) goto out; *cp1++ = '\0'; |
return ccs_network_entry(is_ipv6, operation, |
432 |
if ((count = sscanf(cp2, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx-%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", |
(const u32 *) address, ntohs(port)); |
433 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
} |
434 |
&min_address[4], &min_address[5], &min_address[6], &min_address[7], |
|
435 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3], |
/** |
436 |
&max_address[4], &max_address[5], &max_address[6], &max_address[7])) == 8 || count == 16) { |
* ccs_network_bind_acl - Check permission for bind() operation. |
437 |
int i; |
* |
438 |
for (i = 0; i < 8; i++) { |
* @is_ipv6: True if @address is an IPv6 address. |
439 |
min_address[i] = htons(min_address[i]); |
* @sock_type: Type of socket. (TCP or UDP or RAW) |
440 |
max_address[i] = htons(max_address[i]); |
* @address: An IPv4 or IPv6 address. |
441 |
} |
* @port: Port number. |
442 |
if (count == 8) memmove(max_address, min_address, sizeof(min_address)); |
* |
443 |
record_type = IP_RECORD_TYPE_IPv6; |
* Returns 0 on success, negative value otherwise. |
444 |
} else if ((count = sscanf(cp2, "%hu.%hu.%hu.%hu-%hu.%hu.%hu.%hu", |
*/ |
445 |
&min_address[0], &min_address[1], &min_address[2], &min_address[3], |
static int ccs_network_bind_acl(const bool is_ipv6, const int sock_type, |
446 |
&max_address[0], &max_address[1], &max_address[2], &max_address[3])) == 4 || count == 8) { |
const u8 *address, const u16 port) |
447 |
u32 ip = htonl((((u8) min_address[0]) << 24) + (((u8) min_address[1]) << 16) + (((u8) min_address[2]) << 8) + (u8) min_address[3]); |
{ |
448 |
* (u32 *) min_address = ip; |
u8 operation; |
449 |
if (count == 8) ip = htonl((((u8) max_address[0]) << 24) + (((u8) max_address[1]) << 16) + (((u8) max_address[2]) << 8) + (u8) max_address[3]); |
switch (sock_type) { |
450 |
* (u32 *) max_address = ip; |
case SOCK_STREAM: |
451 |
record_type = IP_RECORD_TYPE_IPv4; |
operation = CCS_NETWORK_TCP_BIND; |
452 |
} else if (*cp2 == '@') { |
break; |
453 |
if ((group = FindOrAssignNewAddressGroup(cp2 + 1)) == NULL) return -ENOMEM; |
case SOCK_DGRAM: |
454 |
record_type = IP_RECORD_TYPE_ADDRESS_GROUP; |
operation = CCS_NETWORK_UDP_BIND; |
455 |
} else { |
break; |
456 |
goto out; |
default: |
457 |
|
operation = CCS_NETWORK_RAW_BIND; |
458 |
} |
} |
459 |
if (strchr(cp1, ' ')) goto out; |
return ccs_network_entry(is_ipv6, operation, |
460 |
if ((count = sscanf(cp1, "%hu-%hu", &min_port, &max_port)) == 1 || count == 2) { |
(const u32 *) address, ntohs(port)); |
461 |
if (count == 1) max_port = min_port; |
} |
462 |
return AddNetworkEntry(operation, record_type, group, (u32 *) min_address, (u32 *) max_address, min_port, max_port, domain, is_delete ? 0 : -1, condition); |
|
463 |
|
/** |
464 |
|
* ccs_network_accept_acl - Check permission for accept() operation. |
465 |
|
* |
466 |
|
* @is_ipv6: True if @address is an IPv6 address. |
467 |
|
* @address: An IPv4 or IPv6 address. |
468 |
|
* @port: Port number. |
469 |
|
* |
470 |
|
* Returns 0 on success, negative value otherwise. |
471 |
|
*/ |
472 |
|
static inline int ccs_network_accept_acl(const bool is_ipv6, |
473 |
|
const u8 *address, |
474 |
|
const u16 port) |
475 |
|
{ |
476 |
|
int retval; |
477 |
|
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
478 |
|
retval = ccs_network_entry(is_ipv6, CCS_NETWORK_TCP_ACCEPT, |
479 |
|
(const u32 *) address, ntohs(port)); |
480 |
|
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
481 |
|
return retval; |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* ccs_network_sendmsg_acl - Check permission for sendmsg() operation. |
486 |
|
* |
487 |
|
* @is_ipv6: True if @address is an IPv6 address. |
488 |
|
* @sock_type: Type of socket. (UDP or RAW) |
489 |
|
* @address: An IPv4 or IPv6 address. |
490 |
|
* @port: Port number. |
491 |
|
* |
492 |
|
* Returns 0 on success, negative value otherwise. |
493 |
|
*/ |
494 |
|
static inline int ccs_network_sendmsg_acl(const bool is_ipv6, |
495 |
|
const int sock_type, |
496 |
|
const u8 *address, |
497 |
|
const u16 port) |
498 |
|
{ |
499 |
|
u8 operation; |
500 |
|
if (sock_type == SOCK_DGRAM) |
501 |
|
operation = CCS_NETWORK_UDP_CONNECT; |
502 |
|
else |
503 |
|
operation = CCS_NETWORK_RAW_CONNECT; |
504 |
|
return ccs_network_entry(is_ipv6, operation, |
505 |
|
(const u32 *) address, ntohs(port)); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
|
* ccs_network_recvmsg_acl - Check permission for recvmsg() operation. |
510 |
|
* |
511 |
|
* @is_ipv6: True if @address is an IPv6 address. |
512 |
|
* @sock_type: Type of socket. (UDP or RAW) |
513 |
|
* @address: An IPv4 or IPv6 address. |
514 |
|
* @port: Port number. |
515 |
|
* |
516 |
|
* Returns 0 on success, negative value otherwise. |
517 |
|
*/ |
518 |
|
static inline int ccs_network_recvmsg_acl(const bool is_ipv6, |
519 |
|
const int sock_type, |
520 |
|
const u8 *address, |
521 |
|
const u16 port) |
522 |
|
{ |
523 |
|
int retval; |
524 |
|
const u8 operation |
525 |
|
= (sock_type == SOCK_DGRAM) ? |
526 |
|
CCS_NETWORK_UDP_CONNECT : CCS_NETWORK_RAW_CONNECT; |
527 |
|
current->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
528 |
|
retval = ccs_network_entry(is_ipv6, operation, |
529 |
|
(const u32 *) address, ntohs(port)); |
530 |
|
current->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
531 |
|
return retval; |
532 |
|
} |
533 |
|
|
534 |
|
#define MAX_SOCK_ADDR 128 /* net/socket.c */ |
535 |
|
|
536 |
|
/* Check permission for creating a socket. */ |
537 |
|
int ccs_socket_create_permission(int family, int type, int protocol) |
538 |
|
{ |
539 |
|
int error = 0; |
540 |
|
/* Nothing to do if I am a kernel service. */ |
541 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
542 |
|
return 0; |
543 |
|
if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET)) |
544 |
|
return -EPERM; |
545 |
|
if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET)) |
546 |
|
return -EPERM; |
547 |
|
if (family != PF_INET && family != PF_INET6) |
548 |
|
return 0; |
549 |
|
switch (type) { |
550 |
|
case SOCK_STREAM: |
551 |
|
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CREATE)) |
552 |
|
error = -EPERM; |
553 |
|
break; |
554 |
|
case SOCK_DGRAM: |
555 |
|
if (!ccs_capable(CCS_USE_INET_DGRAM_SOCKET)) |
556 |
|
error = -EPERM; |
557 |
|
break; |
558 |
|
case SOCK_RAW: |
559 |
|
if (!ccs_capable(CCS_USE_INET_RAW_SOCKET)) |
560 |
|
error = -EPERM; |
561 |
|
break; |
562 |
} |
} |
563 |
out: ; |
return error; |
|
return -EINVAL; |
|
564 |
} |
} |
565 |
|
|
566 |
int CheckNetworkListenACL(const int is_ipv6, const u8 *address, const u16 port) |
/* Check permission for listening a TCP socket. */ |
567 |
|
int ccs_socket_listen_permission(struct socket *sock) |
568 |
{ |
{ |
569 |
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_LISTEN, (const u32 *) address, ntohs(port)); |
int error = 0; |
570 |
|
char addr[MAX_SOCK_ADDR]; |
571 |
|
int addr_len; |
572 |
|
/* Nothing to do if I am a kernel service. */ |
573 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
574 |
|
return 0; |
575 |
|
if (sock->type != SOCK_STREAM) |
576 |
|
return 0; |
577 |
|
switch (sock->sk->sk_family) { |
578 |
|
case PF_INET: |
579 |
|
case PF_INET6: |
580 |
|
break; |
581 |
|
default: |
582 |
|
return 0; |
583 |
|
} |
584 |
|
if (!ccs_capable(CCS_INET_STREAM_SOCKET_LISTEN)) |
585 |
|
return -EPERM; |
586 |
|
if (sock->ops->getname(sock, (struct sockaddr *) addr, &addr_len, 0)) |
587 |
|
return -EPERM; |
588 |
|
switch (((struct sockaddr *) addr)->sa_family) { |
589 |
|
struct sockaddr_in6 *addr6; |
590 |
|
struct sockaddr_in *addr4; |
591 |
|
case AF_INET6: |
592 |
|
addr6 = (struct sockaddr_in6 *) addr; |
593 |
|
error = ccs_network_listen_acl(true, |
594 |
|
addr6->sin6_addr.s6_addr, |
595 |
|
addr6->sin6_port); |
596 |
|
break; |
597 |
|
case AF_INET: |
598 |
|
addr4 = (struct sockaddr_in *) addr; |
599 |
|
error = ccs_network_listen_acl(false, |
600 |
|
(u8 *) &addr4->sin_addr, |
601 |
|
addr4->sin_port); |
602 |
|
break; |
603 |
|
} |
604 |
|
return error; |
605 |
|
} |
606 |
|
|
607 |
|
/* Check permission for setting the remote IP address/port pair of a socket. */ |
608 |
|
int ccs_socket_connect_permission(struct socket *sock, struct sockaddr *addr, |
609 |
|
int addr_len) |
610 |
|
{ |
611 |
|
int error = 0; |
612 |
|
const unsigned int type = sock->type; |
613 |
|
/* Nothing to do if I am a kernel service. */ |
614 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
615 |
|
return 0; |
616 |
|
switch (type) { |
617 |
|
case SOCK_STREAM: |
618 |
|
case SOCK_DGRAM: |
619 |
|
case SOCK_RAW: |
620 |
|
break; |
621 |
|
default: |
622 |
|
return 0; |
623 |
|
} |
624 |
|
switch (addr->sa_family) { |
625 |
|
struct sockaddr_in6 *addr6; |
626 |
|
struct sockaddr_in *addr4; |
627 |
|
u16 port; |
628 |
|
case AF_INET6: |
629 |
|
if (addr_len < SIN6_LEN_RFC2133) |
630 |
|
break; |
631 |
|
addr6 = (struct sockaddr_in6 *) addr; |
632 |
|
if (type != SOCK_RAW) |
633 |
|
port = addr6->sin6_port; |
634 |
|
else |
635 |
|
port = htons(sock->sk->sk_protocol); |
636 |
|
error = ccs_network_connect_acl(true, type, |
637 |
|
addr6->sin6_addr.s6_addr, |
638 |
|
port); |
639 |
|
break; |
640 |
|
case AF_INET: |
641 |
|
if (addr_len < sizeof(struct sockaddr_in)) |
642 |
|
break; |
643 |
|
addr4 = (struct sockaddr_in *) addr; |
644 |
|
if (type != SOCK_RAW) |
645 |
|
port = addr4->sin_port; |
646 |
|
else |
647 |
|
port = htons(sock->sk->sk_protocol); |
648 |
|
error = ccs_network_connect_acl(false, type, |
649 |
|
(u8 *) &addr4->sin_addr, |
650 |
|
port); |
651 |
|
break; |
652 |
|
} |
653 |
|
if (type != SOCK_STREAM) |
654 |
|
return error; |
655 |
|
switch (sock->sk->sk_family) { |
656 |
|
case PF_INET: |
657 |
|
case PF_INET6: |
658 |
|
if (!ccs_capable(CCS_INET_STREAM_SOCKET_CONNECT)) |
659 |
|
error = -EPERM; |
660 |
|
break; |
661 |
|
} |
662 |
|
return error; |
663 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkListenACL); |
|
664 |
|
|
665 |
int CheckNetworkConnectACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/* Check permission for setting the local IP address/port pair of a socket. */ |
666 |
|
int ccs_socket_bind_permission(struct socket *sock, struct sockaddr *addr, |
667 |
|
int addr_len) |
668 |
|
{ |
669 |
|
int error = 0; |
670 |
|
const unsigned int type = sock->type; |
671 |
|
/* Nothing to do if I am a kernel service. */ |
672 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
673 |
|
return 0; |
674 |
|
switch (type) { |
675 |
|
case SOCK_STREAM: |
676 |
|
case SOCK_DGRAM: |
677 |
|
case SOCK_RAW: |
678 |
|
break; |
679 |
|
default: |
680 |
|
return 0; |
681 |
|
} |
682 |
|
switch (addr->sa_family) { |
683 |
|
struct sockaddr_in6 *addr6; |
684 |
|
struct sockaddr_in *addr4; |
685 |
|
u16 port; |
686 |
|
case AF_INET6: |
687 |
|
if (addr_len < SIN6_LEN_RFC2133) |
688 |
|
break; |
689 |
|
addr6 = (struct sockaddr_in6 *) addr; |
690 |
|
if (type != SOCK_RAW) |
691 |
|
port = addr6->sin6_port; |
692 |
|
else |
693 |
|
port = htons(sock->sk->sk_protocol); |
694 |
|
error = ccs_network_bind_acl(true, type, |
695 |
|
addr6->sin6_addr.s6_addr, |
696 |
|
port); |
697 |
|
break; |
698 |
|
case AF_INET: |
699 |
|
if (addr_len < sizeof(struct sockaddr_in)) |
700 |
|
break; |
701 |
|
addr4 = (struct sockaddr_in *) addr; |
702 |
|
if (type != SOCK_RAW) |
703 |
|
port = addr4->sin_port; |
704 |
|
else |
705 |
|
port = htons(sock->sk->sk_protocol); |
706 |
|
error = ccs_network_bind_acl(false, type, |
707 |
|
(u8 *) &addr4->sin_addr, |
708 |
|
port); |
709 |
|
break; |
710 |
|
} |
711 |
|
return error; |
712 |
|
} |
713 |
|
|
714 |
|
/* |
715 |
|
* Check permission for accepting a TCP socket. |
716 |
|
* |
717 |
|
* Currently, the LSM hook for this purpose is not provided. |
718 |
|
*/ |
719 |
|
int ccs_socket_accept_permission(struct socket *sock, struct sockaddr *addr) |
720 |
{ |
{ |
721 |
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)); |
int error = 0; |
722 |
|
int addr_len; |
723 |
|
/* Nothing to do if I am a kernel service. */ |
724 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
725 |
|
return 0; |
726 |
|
switch (sock->sk->sk_family) { |
727 |
|
case PF_INET: |
728 |
|
case PF_INET6: |
729 |
|
break; |
730 |
|
default: |
731 |
|
return 0; |
732 |
|
} |
733 |
|
error = sock->ops->getname(sock, addr, &addr_len, 2); |
734 |
|
if (error) |
735 |
|
return error; |
736 |
|
switch (addr->sa_family) { |
737 |
|
struct sockaddr_in6 *addr6; |
738 |
|
struct sockaddr_in *addr4; |
739 |
|
case AF_INET6: |
740 |
|
addr6 = (struct sockaddr_in6 *) addr; |
741 |
|
error = ccs_network_accept_acl(true, |
742 |
|
addr6->sin6_addr.s6_addr, |
743 |
|
addr6->sin6_port); |
744 |
|
break; |
745 |
|
case AF_INET: |
746 |
|
addr4 = (struct sockaddr_in *) addr; |
747 |
|
error = ccs_network_accept_acl(false, |
748 |
|
(u8 *) &addr4->sin_addr, |
749 |
|
addr4->sin_port); |
750 |
|
break; |
751 |
|
} |
752 |
|
return error; |
753 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkConnectACL); |
|
754 |
|
|
755 |
int CheckNetworkBindACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
/* Check permission for sending a datagram via a UDP or RAW socket. */ |
756 |
|
int ccs_socket_sendmsg_permission(struct socket *sock, struct sockaddr *addr, |
757 |
|
int addr_len) |
758 |
|
{ |
759 |
|
int error = 0; |
760 |
|
const int type = sock->type; |
761 |
|
/* Nothing to do if I am a kernel service. */ |
762 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
763 |
|
return 0; |
764 |
|
if (!addr || (type != SOCK_DGRAM && type != SOCK_RAW)) |
765 |
|
return 0; |
766 |
|
switch (addr->sa_family) { |
767 |
|
struct sockaddr_in6 *addr6; |
768 |
|
struct sockaddr_in *addr4; |
769 |
|
u16 port; |
770 |
|
case AF_INET6: |
771 |
|
if (addr_len < SIN6_LEN_RFC2133) |
772 |
|
break; |
773 |
|
addr6 = (struct sockaddr_in6 *) addr; |
774 |
|
if (type == SOCK_DGRAM) |
775 |
|
port = addr6->sin6_port; |
776 |
|
else |
777 |
|
port = htons(sock->sk->sk_protocol); |
778 |
|
error = ccs_network_sendmsg_acl(true, type, |
779 |
|
addr6->sin6_addr.s6_addr, |
780 |
|
port); |
781 |
|
break; |
782 |
|
case AF_INET: |
783 |
|
if (addr_len < sizeof(struct sockaddr_in)) |
784 |
|
break; |
785 |
|
addr4 = (struct sockaddr_in *) addr; |
786 |
|
if (type == SOCK_DGRAM) |
787 |
|
port = addr4->sin_port; |
788 |
|
else |
789 |
|
port = htons(sock->sk->sk_protocol); |
790 |
|
error = ccs_network_sendmsg_acl(false, type, |
791 |
|
(u8 *) &addr4->sin_addr, |
792 |
|
port); |
793 |
|
break; |
794 |
|
} |
795 |
|
return error; |
796 |
|
} |
797 |
|
|
798 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22) |
799 |
|
#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 |
800 |
|
#if !defined(AX_MAJOR) || AX_MAJOR != 3 || !defined(AX_MINOR) || AX_MINOR < 2 |
801 |
|
|
802 |
|
static inline struct iphdr *ip_hdr(const struct sk_buff *skb) |
803 |
{ |
{ |
804 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_STREAM ? NETWORK_ACL_TCP_BIND : (sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_BIND : NETWORK_ACL_RAW_BIND), (const u32 *) address, ntohs(port)); |
return skb->nh.iph; |
805 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkBindACL); |
|
806 |
|
|
807 |
int CheckNetworkAcceptACL(const int is_ipv6, const u8 *address, const u16 port) |
static inline struct udphdr *udp_hdr(const struct sk_buff *skb) |
808 |
{ |
{ |
809 |
return CheckNetworkEntry(is_ipv6, NETWORK_ACL_TCP_ACCEPT, (const u32 *) address, ntohs(port)); |
return skb->h.uh; |
810 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkAcceptACL); |
|
811 |
|
|
812 |
int CheckNetworkSendMsgACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb) |
813 |
{ |
{ |
814 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
return skb->nh.ipv6h; |
815 |
} |
} |
|
EXPORT_SYMBOL(CheckNetworkSendMsgACL); |
|
816 |
|
|
817 |
int CheckNetworkRecvMsgACL(const int is_ipv6, const int sock_type, const u8 *address, const u16 port) |
#endif |
818 |
|
#endif |
819 |
|
#endif |
820 |
|
|
821 |
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12) |
822 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
823 |
|
unsigned int flags) |
824 |
|
{ |
825 |
|
/* Clear queue. */ |
826 |
|
if (flags & MSG_PEEK) { |
827 |
|
int clear = 0; |
828 |
|
spin_lock_irq(&sk->sk_receive_queue.lock); |
829 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
830 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
831 |
|
clear = 1; |
832 |
|
} |
833 |
|
spin_unlock_irq(&sk->sk_receive_queue.lock); |
834 |
|
if (clear) |
835 |
|
kfree_skb(skb); |
836 |
|
} |
837 |
|
skb_free_datagram(sk, skb); |
838 |
|
} |
839 |
|
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) |
840 |
|
static void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, |
841 |
|
unsigned int flags) |
842 |
{ |
{ |
843 |
return CheckNetworkEntry(is_ipv6, sock_type == SOCK_DGRAM ? NETWORK_ACL_UDP_CONNECT : NETWORK_ACL_RAW_CONNECT, (const u32 *) address, ntohs(port)); |
/* Clear queue. */ |
844 |
|
if (flags & MSG_PEEK) { |
845 |
|
int clear = 0; |
846 |
|
spin_lock_bh(&sk->sk_receive_queue.lock); |
847 |
|
if (skb == skb_peek(&sk->sk_receive_queue)) { |
848 |
|
__skb_unlink(skb, &sk->sk_receive_queue); |
849 |
|
clear = 1; |
850 |
|
} |
851 |
|
spin_unlock_bh(&sk->sk_receive_queue.lock); |
852 |
|
if (clear) |
853 |
|
kfree_skb(skb); |
854 |
|
} |
855 |
|
skb_free_datagram(sk, skb); |
856 |
} |
} |
857 |
EXPORT_SYMBOL(CheckNetworkRecvMsgACL); |
#endif |
858 |
|
|
859 |
/***** TOMOYO Linux end. *****/ |
/* |
860 |
|
* Check permission for receiving a datagram via a UDP or RAW socket. |
861 |
|
* |
862 |
|
* Currently, the LSM hook for this purpose is not provided. |
863 |
|
*/ |
864 |
|
int ccs_socket_recvmsg_permission(struct sock *sk, struct sk_buff *skb, |
865 |
|
const unsigned int flags) |
866 |
|
{ |
867 |
|
int error = 0; |
868 |
|
const unsigned int type = sk->sk_type; |
869 |
|
if (type != SOCK_DGRAM && type != SOCK_RAW) |
870 |
|
return 0; |
871 |
|
/* Nothing to do if I am a kernel service. */ |
872 |
|
if (segment_eq(get_fs(), KERNEL_DS)) |
873 |
|
return 0; |
874 |
|
|
875 |
|
switch (sk->sk_family) { |
876 |
|
struct in6_addr sin6; |
877 |
|
struct in_addr sin4; |
878 |
|
u16 port; |
879 |
|
case PF_INET6: |
880 |
|
if (type == SOCK_DGRAM) { /* UDP IPv6 */ |
881 |
|
if (skb->protocol == htons(ETH_P_IP)) { |
882 |
|
ipv6_addr_set(&sin6, 0, 0, htonl(0xffff), |
883 |
|
ip_hdr(skb)->saddr); |
884 |
|
} else { |
885 |
|
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
886 |
|
} |
887 |
|
port = udp_hdr(skb)->source; |
888 |
|
} else { /* RAW IPv6 */ |
889 |
|
ipv6_addr_copy(&sin6, &ipv6_hdr(skb)->saddr); |
890 |
|
port = htons(sk->sk_protocol); |
891 |
|
} |
892 |
|
error = ccs_network_recvmsg_acl(true, type, |
893 |
|
(u8 *) &sin6, port); |
894 |
|
break; |
895 |
|
case PF_INET: |
896 |
|
if (type == SOCK_DGRAM) { /* UDP IPv4 */ |
897 |
|
sin4.s_addr = ip_hdr(skb)->saddr; |
898 |
|
port = udp_hdr(skb)->source; |
899 |
|
} else { /* RAW IPv4 */ |
900 |
|
sin4.s_addr = ip_hdr(skb)->saddr; |
901 |
|
port = htons(sk->sk_protocol); |
902 |
|
} |
903 |
|
error = ccs_network_recvmsg_acl(false, type, |
904 |
|
(u8 *) &sin4, port); |
905 |
|
break; |
906 |
|
} |
907 |
|
if (!error) |
908 |
|
return 0; |
909 |
|
/* |
910 |
|
* Remove from queue if MSG_PEEK is used so that |
911 |
|
* the head message from unwanted source in receive queue will not |
912 |
|
* prevent the caller from picking up next message from wanted source |
913 |
|
* when the caller is using MSG_PEEK flag for picking up. |
914 |
|
*/ |
915 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
916 |
|
if (type == SOCK_DGRAM) |
917 |
|
lock_sock(sk); |
918 |
|
#endif |
919 |
|
skb_kill_datagram(sk, skb, flags); |
920 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
921 |
|
if (type == SOCK_DGRAM) |
922 |
|
release_sock(sk); |
923 |
|
#endif |
924 |
|
/* Hope less harmful than -EPERM. */ |
925 |
|
return -ENOMEM; |
926 |
|
} |
927 |
|
EXPORT_SYMBOL(ccs_socket_recvmsg_permission); |