3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Implementation of the Domain-Based Mandatory Access Control. |
5 |
* |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
7 |
* |
* |
8 |
* Version: 1.6.2 2008/06/25 |
* Version: 1.7.0-pre 2009/07/03 |
9 |
* |
* |
10 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
11 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
12 |
* |
* |
13 |
*/ |
*/ |
14 |
|
|
15 |
#include <linux/ccs_common.h> |
#include <linux/slab.h> |
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
16 |
#include <linux/highmem.h> |
#include <linux/highmem.h> |
17 |
#include <linux/binfmts.h> |
#include <linux/version.h> |
18 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
19 |
#include <linux/namei.h> |
#include <linux/namei.h> |
20 |
#include <linux/mount.h> |
#include <linux/mount.h> |
21 |
#endif |
#endif |
22 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) |
23 |
|
#include <linux/fs_struct.h> |
24 |
|
#endif |
25 |
|
#include <linux/ccs_common.h> |
26 |
|
#include <linux/tomoyo.h> |
27 |
|
|
28 |
/* For compatibility with older kernels. */ |
/* For compatibility with older kernels. */ |
29 |
#ifndef for_each_process |
#ifndef for_each_process |
33 |
/* Variables definitions.*/ |
/* Variables definitions.*/ |
34 |
|
|
35 |
/* The initial domain. */ |
/* The initial domain. */ |
36 |
struct domain_info KERNEL_DOMAIN; |
struct ccs_domain_info ccs_kernel_domain; |
|
|
|
|
/* The list for "struct domain_info". */ |
|
|
LIST1_HEAD(domain_list); |
|
37 |
|
|
38 |
#ifdef CONFIG_TOMOYO |
/* The list for "struct ccs_domain_info". */ |
39 |
|
LIST_HEAD(ccs_domain_list); |
|
/* Lock for appending domain's ACL. */ |
|
|
DEFINE_MUTEX(domain_acl_lock); |
|
|
|
|
|
/* Domain creation lock. */ |
|
|
static DEFINE_MUTEX(new_domain_assign_lock); |
|
|
|
|
|
/* Structure for "initialize_domain" and "no_initialize_domain" keyword. */ |
|
|
struct domain_initializer_entry { |
|
|
struct list1_head list; |
|
|
const struct path_info *domainname; /* This may be NULL */ |
|
|
const struct path_info *program; |
|
|
bool is_deleted; |
|
|
bool is_not; /* True if this entry is "no_initialize_domain". */ |
|
|
bool is_last_name; /* True if the domainname is ccs_get_last_name(). */ |
|
|
}; |
|
|
|
|
|
/* Structure for "keep_domain" and "no_keep_domain" keyword. */ |
|
|
struct domain_keeper_entry { |
|
|
struct list1_head list; |
|
|
const struct path_info *domainname; |
|
|
const struct path_info *program; /* This may be NULL */ |
|
|
bool is_deleted; |
|
|
bool is_not; /* True if this entry is "no_keep_domain". */ |
|
|
bool is_last_name; /* True if the domainname is ccs_get_last_name(). */ |
|
|
}; |
|
|
|
|
|
/* Structure for "aggregator" keyword. */ |
|
|
struct aggregator_entry { |
|
|
struct list1_head list; |
|
|
const struct path_info *original_name; |
|
|
const struct path_info *aggregated_name; |
|
|
bool is_deleted; |
|
|
}; |
|
|
|
|
|
/* Structure for "alias" keyword. */ |
|
|
struct alias_entry { |
|
|
struct list1_head list; |
|
|
const struct path_info *original_name; |
|
|
const struct path_info *aliased_name; |
|
|
bool is_deleted; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* ccs_set_domain_flag - Set or clear domain's attribute flags. |
|
|
* |
|
|
* @domain: Pointer to "struct domain_info". |
|
|
* @is_delete: True if it is a delete request. |
|
|
* @flags: Flags to set or clear. |
|
|
* |
|
|
* Returns nothing. |
|
|
*/ |
|
|
void ccs_set_domain_flag(struct domain_info *domain, const bool is_delete, |
|
|
const u8 flags) |
|
|
{ |
|
|
mutex_lock(&new_domain_assign_lock); |
|
|
if (!is_delete) |
|
|
domain->flags |= flags; |
|
|
else |
|
|
domain->flags &= ~flags; |
|
|
mutex_unlock(&new_domain_assign_lock); |
|
|
} |
|
40 |
|
|
41 |
/** |
/** |
42 |
* ccs_get_last_name - Get last component of a domainname. |
* ccs_get_last_name - Get last component of a domainname. |
43 |
* |
* |
44 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
45 |
* |
* |
46 |
* Returns the last component of the domainname. |
* Returns the last component of the domainname. |
47 |
*/ |
*/ |
48 |
const char *ccs_get_last_name(const struct domain_info *domain) |
const char *ccs_get_last_name(const struct ccs_domain_info *domain) |
49 |
{ |
{ |
50 |
const char *cp0 = domain->domainname->name, *cp1 = strrchr(cp0, ' '); |
const char *cp0 = domain->domainname->name; |
51 |
|
const char *cp1 = strrchr(cp0, ' '); |
52 |
if (cp1) |
if (cp1) |
53 |
return cp1 + 1; |
return cp1 + 1; |
54 |
return cp0; |
return cp0; |
57 |
/** |
/** |
58 |
* ccs_add_domain_acl - Add the given ACL to the given domain. |
* ccs_add_domain_acl - Add the given ACL to the given domain. |
59 |
* |
* |
60 |
* @domain: Pointer to "struct domain_info". May be NULL. |
* @domain: Pointer to "struct ccs_domain_info". May be NULL. |
61 |
* @acl: Pointer to "struct acl_info". |
* @acl: Pointer to "struct ccs_acl_info". |
62 |
* |
* |
63 |
* Returns 0. |
* Returns 0. |
64 |
*/ |
*/ |
65 |
int ccs_add_domain_acl(struct domain_info *domain, struct acl_info *acl) |
int ccs_add_domain_acl(struct ccs_domain_info *domain, struct ccs_acl_info *acl) |
66 |
{ |
{ |
67 |
if (domain) |
if (domain) { |
68 |
list1_add_tail_mb(&acl->list, &domain->acl_info_list); |
if (acl->cond) |
69 |
else |
atomic_inc(&acl->cond->users); |
70 |
|
list_add_tail_rcu(&acl->list, &domain->acl_info_list); |
71 |
|
} else { |
72 |
acl->type &= ~ACL_DELETED; |
acl->type &= ~ACL_DELETED; |
73 |
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
} |
74 |
return 0; |
return 0; |
75 |
} |
} |
76 |
|
|
77 |
/** |
/** |
78 |
* ccs_del_domain_acl - Delete the given ACL from the domain. |
* ccs_del_domain_acl - Delete the given ACL from the domain. |
79 |
* |
* |
80 |
* @acl: Pointer to "struct acl_info". May be NULL. |
* @acl: Pointer to "struct ccs_acl_info". May be NULL. |
81 |
* |
* |
82 |
* Returns 0. |
* Returns 0. |
83 |
*/ |
*/ |
84 |
int ccs_del_domain_acl(struct acl_info *acl) |
int ccs_del_domain_acl(struct ccs_acl_info *acl) |
85 |
{ |
{ |
86 |
if (acl) |
if (acl) |
87 |
acl->type |= ACL_DELETED; |
acl->type |= ACL_DELETED; |
|
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
|
88 |
return 0; |
return 0; |
89 |
} |
} |
90 |
|
|
91 |
/** |
/** |
92 |
* audit_execute_handler_log - Audit execute_handler log. |
* ccs_audit_execute_handler_log - Audit execute_handler log. |
93 |
* |
* |
94 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
95 |
* @is_default: True if it is "execute_handler" log. |
* @is_default: True if it is "execute_handler" log. |
|
* @handler: The realpath of the handler. |
|
|
* @bprm: Pointer to "struct linux_binprm". |
|
96 |
* |
* |
97 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
98 |
*/ |
*/ |
99 |
static int audit_execute_handler_log(const bool is_default, |
static int ccs_audit_execute_handler_log(struct ccs_execve_entry *ee, |
100 |
const char *handler, |
const bool is_default) |
101 |
struct linux_binprm *bprm) |
{ |
102 |
{ |
struct ccs_request_info *r = &ee->r; |
103 |
char *buf; |
const char *handler = ee->handler->name; |
104 |
int len; |
r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_FILE); |
105 |
int len2; |
return ccs_write_audit_log(true, r, "%s %s\n", |
106 |
u8 profile; |
is_default ? KEYWORD_EXECUTE_HANDLER : |
107 |
u8 mode; |
KEYWORD_DENIED_EXECUTE_HANDLER, handler); |
|
if (ccs_can_save_audit_log(true) < 0) |
|
|
return -ENOMEM; |
|
|
len = strlen(handler) + 32; |
|
|
profile = current->domain_info->profile; |
|
|
mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_FILE); |
|
|
buf = ccs_init_audit_log(&len, profile, mode, bprm); |
|
|
if (!buf) |
|
|
return -ENOMEM; |
|
|
len2 = strlen(buf); |
|
|
snprintf(buf + len2, len - len2 - 1, "%s %s\n", |
|
|
is_default ? KEYWORD_EXECUTE_HANDLER : |
|
|
KEYWORD_DENIED_EXECUTE_HANDLER, handler); |
|
|
return ccs_write_audit_log(buf, true); |
|
108 |
} |
} |
109 |
|
|
110 |
/** |
/** |
111 |
* audit_domain_creation_log - Audit domain creation log. |
* ccs_audit_domain_creation_log - Audit domain creation log. |
112 |
* |
* |
113 |
* @domainname: The name of newly created domain. |
* @domain: Pointer to "struct ccs_domain_info". |
|
* @mode: Access control mode used. |
|
|
* @profile: Profile number used. |
|
114 |
* |
* |
115 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
116 |
*/ |
*/ |
117 |
static int audit_domain_creation_log(const char *domainname, const u8 mode, |
static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain) |
|
const u8 profile) |
|
118 |
{ |
{ |
119 |
char *buf; |
int error; |
120 |
char *cp; |
struct ccs_request_info r; |
121 |
int len; |
ccs_init_request_info(&r, domain, CCS_MAC_FOR_FILE); |
122 |
int len2; |
error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile); |
123 |
if (ccs_can_save_audit_log(false) < 0) |
return error; |
|
return -ENOMEM; |
|
|
len = strlen(domainname) + 32; |
|
|
buf = ccs_init_audit_log(&len, profile, mode, NULL); |
|
|
if (!buf) |
|
|
return -ENOMEM; |
|
|
cp = strchr(buf, '\n'); |
|
|
if (!cp) { |
|
|
ccs_free(buf); |
|
|
return -ENOMEM; |
|
|
} |
|
|
*++cp = '\0'; |
|
|
len2 = strlen(buf); |
|
|
snprintf(buf + len2, len - len2 - 1, "%s\nuse_profile %u\n", |
|
|
domainname, profile); |
|
|
return ccs_write_audit_log(buf, false); |
|
124 |
} |
} |
125 |
|
|
126 |
/* The list for "struct domain_initializer_entry". */ |
/* The list for "struct ccs_domain_initializer_entry". */ |
127 |
static LIST1_HEAD(domain_initializer_list); |
LIST_HEAD(ccs_domain_initializer_list); |
128 |
|
|
129 |
/** |
/** |
130 |
* update_domain_initializer_entry - Update "struct domain_initializer_entry" list. |
* ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list. |
131 |
* |
* |
132 |
* @domainname: The name of domain. May be NULL. |
* @domainname: The name of domain. May be NULL. |
133 |
* @program: The name of program. |
* @program: The name of program. |
136 |
* |
* |
137 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
138 |
*/ |
*/ |
139 |
static int update_domain_initializer_entry(const char *domainname, |
static int ccs_update_domain_initializer_entry(const char *domainname, |
140 |
const char *program, |
const char *program, |
141 |
const bool is_not, |
const bool is_not, |
142 |
const bool is_delete) |
const bool is_delete) |
143 |
{ |
{ |
144 |
struct domain_initializer_entry *new_entry; |
struct ccs_domain_initializer_entry *entry = NULL; |
145 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
146 |
static DEFINE_MUTEX(lock); |
const struct ccs_path_info *saved_program; |
147 |
const struct path_info *saved_program; |
const struct ccs_path_info *saved_domainname = NULL; |
148 |
const struct path_info *saved_domainname = NULL; |
int error = is_delete ? -ENOENT : -ENOMEM; |
|
int error = -ENOMEM; |
|
149 |
bool is_last_name = false; |
bool is_last_name = false; |
150 |
if (!ccs_is_correct_path(program, 1, -1, -1, __func__)) |
if (!ccs_is_correct_path(program, 1, -1, -1)) |
151 |
return -EINVAL; /* No patterns allowed. */ |
return -EINVAL; /* No patterns allowed. */ |
152 |
if (domainname) { |
if (domainname) { |
153 |
if (!ccs_is_domain_def(domainname) && |
if (!ccs_is_domain_def(domainname) && |
154 |
ccs_is_correct_path(domainname, 1, -1, -1, __func__)) |
ccs_is_correct_path(domainname, 1, -1, -1)) |
155 |
is_last_name = true; |
is_last_name = true; |
156 |
else if (!ccs_is_correct_domain(domainname, __func__)) |
else if (!ccs_is_correct_domain(domainname)) |
157 |
return -EINVAL; |
return -EINVAL; |
158 |
saved_domainname = ccs_save_name(domainname); |
saved_domainname = ccs_get_name(domainname); |
159 |
if (!saved_domainname) |
if (!saved_domainname) |
160 |
return -ENOMEM; |
return -ENOMEM; |
161 |
} |
} |
162 |
saved_program = ccs_save_name(program); |
saved_program = ccs_get_name(program); |
163 |
if (!saved_program) |
if (!saved_program) { |
164 |
|
ccs_put_name(saved_domainname); |
165 |
return -ENOMEM; |
return -ENOMEM; |
166 |
mutex_lock(&lock); |
} |
167 |
list1_for_each_entry(ptr, &domain_initializer_list, list) { |
if (!is_delete) |
168 |
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
169 |
|
mutex_lock(&ccs_policy_lock); |
170 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
171 |
if (ptr->is_not != is_not || |
if (ptr->is_not != is_not || |
172 |
ptr->domainname != saved_domainname || |
ptr->domainname != saved_domainname || |
173 |
ptr->program != saved_program) |
ptr->program != saved_program) |
174 |
continue; |
continue; |
175 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
176 |
error = 0; |
error = 0; |
177 |
goto out; |
break; |
178 |
} |
} |
179 |
if (is_delete) { |
if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) { |
180 |
error = -ENOENT; |
entry->domainname = saved_domainname; |
181 |
goto out; |
saved_domainname = NULL; |
182 |
|
entry->program = saved_program; |
183 |
|
saved_program = NULL; |
184 |
|
entry->is_not = is_not; |
185 |
|
entry->is_last_name = is_last_name; |
186 |
|
list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list); |
187 |
|
entry = NULL; |
188 |
|
error = 0; |
189 |
} |
} |
190 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
191 |
if (!new_entry) |
ccs_put_name(saved_domainname); |
192 |
goto out; |
ccs_put_name(saved_program); |
193 |
new_entry->domainname = saved_domainname; |
kfree(entry); |
|
new_entry->program = saved_program; |
|
|
new_entry->is_not = is_not; |
|
|
new_entry->is_last_name = is_last_name; |
|
|
list1_add_tail_mb(&new_entry->list, &domain_initializer_list); |
|
|
error = 0; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
194 |
return error; |
return error; |
195 |
} |
} |
196 |
|
|
197 |
/** |
/** |
198 |
* ccs_read_domain_initializer_policy - Read "struct domain_initializer_entry" list. |
* ccs_read_domain_initializer_policy - Read "struct ccs_domain_initializer_entry" list. |
199 |
* |
* |
200 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
201 |
* |
* |
202 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
203 |
|
* |
204 |
|
* Caller holds ccs_read_lock(). |
205 |
*/ |
*/ |
206 |
bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head) |
bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head) |
207 |
{ |
{ |
208 |
struct list1_head *pos; |
struct list_head *pos; |
209 |
list1_for_each_cookie(pos, head->read_var2, &domain_initializer_list) { |
bool done = true; |
210 |
|
ccs_check_read_lock(); |
211 |
|
list_for_each_cookie(pos, head->read_var2, |
212 |
|
&ccs_domain_initializer_list) { |
213 |
const char *no; |
const char *no; |
214 |
const char *from = ""; |
const char *from = ""; |
215 |
const char *domain = ""; |
const char *domain = ""; |
216 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
217 |
ptr = list1_entry(pos, struct domain_initializer_entry, list); |
ptr = list_entry(pos, struct ccs_domain_initializer_entry, |
218 |
|
list); |
219 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
220 |
continue; |
continue; |
221 |
no = ptr->is_not ? "no_" : ""; |
no = ptr->is_not ? "no_" : ""; |
223 |
from = " from "; |
from = " from "; |
224 |
domain = ptr->domainname->name; |
domain = ptr->domainname->name; |
225 |
} |
} |
226 |
if (!ccs_io_printf(head, |
done = ccs_io_printf(head, |
227 |
"%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n", |
"%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n", |
228 |
no, ptr->program->name, from, domain)) |
no, ptr->program->name, from, domain); |
229 |
goto out; |
if (!done) |
230 |
|
break; |
231 |
} |
} |
232 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
233 |
} |
} |
234 |
|
|
235 |
/** |
/** |
236 |
* ccs_write_domain_initializer_policy - Write "struct domain_initializer_entry" list. |
* ccs_write_domain_initializer_policy - Write "struct ccs_domain_initializer_entry" list. |
237 |
* |
* |
238 |
* @data: String to parse. |
* @data: String to parse. |
239 |
* @is_not: True if it is "no_initialize_domain" entry. |
* @is_not: True if it is "no_initialize_domain" entry. |
247 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
248 |
if (cp) { |
if (cp) { |
249 |
*cp = '\0'; |
*cp = '\0'; |
250 |
return update_domain_initializer_entry(cp + 6, data, is_not, |
return ccs_update_domain_initializer_entry(cp + 6, data, |
251 |
is_delete); |
is_not, is_delete); |
252 |
} |
} |
253 |
return update_domain_initializer_entry(NULL, data, is_not, is_delete); |
return ccs_update_domain_initializer_entry(NULL, data, is_not, |
254 |
|
is_delete); |
255 |
} |
} |
256 |
|
|
257 |
/** |
/** |
258 |
* is_domain_initializer - Check whether the given program causes domainname reinitialization. |
* ccs_is_domain_initializer - Check whether the given program causes domainname reinitialization. |
259 |
* |
* |
260 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
261 |
* @program: The name of program. |
* @program: The name of program. |
263 |
* |
* |
264 |
* Returns true if executing @program reinitializes domain transition, |
* Returns true if executing @program reinitializes domain transition, |
265 |
* false otherwise. |
* false otherwise. |
266 |
|
* |
267 |
|
* Caller holds ccs_read_lock(). |
268 |
*/ |
*/ |
269 |
static bool is_domain_initializer(const struct path_info *domainname, |
static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname, |
270 |
const struct path_info *program, |
const struct ccs_path_info *program, |
271 |
const struct path_info *last_name) |
const struct ccs_path_info *last_name) |
272 |
{ |
{ |
273 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
274 |
bool flag = false; |
bool flag = false; |
275 |
list1_for_each_entry(ptr, &domain_initializer_list, list) { |
ccs_check_read_lock(); |
276 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
277 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
278 |
continue; |
continue; |
279 |
if (ptr->domainname) { |
if (ptr->domainname) { |
287 |
} |
} |
288 |
if (ccs_pathcmp(ptr->program, program)) |
if (ccs_pathcmp(ptr->program, program)) |
289 |
continue; |
continue; |
290 |
if (ptr->is_not) |
if (ptr->is_not) { |
291 |
return false; |
flag = false; |
292 |
|
break; |
293 |
|
} |
294 |
flag = true; |
flag = true; |
295 |
} |
} |
296 |
return flag; |
return flag; |
297 |
} |
} |
298 |
|
|
299 |
/* The list for "struct domain_keeper_entry". */ |
/* The list for "struct ccs_domain_keeper_entry". */ |
300 |
static LIST1_HEAD(domain_keeper_list); |
LIST_HEAD(ccs_domain_keeper_list); |
301 |
|
|
302 |
/** |
/** |
303 |
* update_domain_keeper_entry - Update "struct domain_keeper_entry" list. |
* ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list. |
304 |
* |
* |
305 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
306 |
* @program: The name of program. May be NULL. |
* @program: The name of program. May be NULL. |
309 |
* |
* |
310 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
311 |
*/ |
*/ |
312 |
static int update_domain_keeper_entry(const char *domainname, |
static int ccs_update_domain_keeper_entry(const char *domainname, |
313 |
const char *program, |
const char *program, |
314 |
const bool is_not, const bool is_delete) |
const bool is_not, |
315 |
{ |
const bool is_delete) |
316 |
struct domain_keeper_entry *new_entry; |
{ |
317 |
struct domain_keeper_entry *ptr; |
struct ccs_domain_keeper_entry *entry = NULL; |
318 |
const struct path_info *saved_domainname; |
struct ccs_domain_keeper_entry *ptr; |
319 |
const struct path_info *saved_program = NULL; |
const struct ccs_path_info *saved_domainname; |
320 |
static DEFINE_MUTEX(lock); |
const struct ccs_path_info *saved_program = NULL; |
321 |
int error = -ENOMEM; |
int error = is_delete ? -ENOENT : -ENOMEM; |
322 |
bool is_last_name = false; |
bool is_last_name = false; |
323 |
if (!ccs_is_domain_def(domainname) && |
if (!ccs_is_domain_def(domainname) && |
324 |
ccs_is_correct_path(domainname, 1, -1, -1, __func__)) |
ccs_is_correct_path(domainname, 1, -1, -1)) |
325 |
is_last_name = true; |
is_last_name = true; |
326 |
else if (!ccs_is_correct_domain(domainname, __func__)) |
else if (!ccs_is_correct_domain(domainname)) |
327 |
return -EINVAL; |
return -EINVAL; |
328 |
if (program) { |
if (program) { |
329 |
if (!ccs_is_correct_path(program, 1, -1, -1, __func__)) |
if (!ccs_is_correct_path(program, 1, -1, -1)) |
330 |
return -EINVAL; |
return -EINVAL; |
331 |
saved_program = ccs_save_name(program); |
saved_program = ccs_get_name(program); |
332 |
if (!saved_program) |
if (!saved_program) |
333 |
return -ENOMEM; |
return -ENOMEM; |
334 |
} |
} |
335 |
saved_domainname = ccs_save_name(domainname); |
saved_domainname = ccs_get_name(domainname); |
336 |
if (!saved_domainname) |
if (!saved_domainname) { |
337 |
|
ccs_put_name(saved_program); |
338 |
return -ENOMEM; |
return -ENOMEM; |
339 |
mutex_lock(&lock); |
} |
340 |
list1_for_each_entry(ptr, &domain_keeper_list, list) { |
if (!is_delete) |
341 |
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
342 |
|
mutex_lock(&ccs_policy_lock); |
343 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
344 |
if (ptr->is_not != is_not || |
if (ptr->is_not != is_not || |
345 |
ptr->domainname != saved_domainname || |
ptr->domainname != saved_domainname || |
346 |
ptr->program != saved_program) |
ptr->program != saved_program) |
347 |
continue; |
continue; |
348 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
349 |
error = 0; |
error = 0; |
350 |
goto out; |
break; |
351 |
} |
} |
352 |
if (is_delete) { |
if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) { |
353 |
error = -ENOENT; |
entry->domainname = saved_domainname; |
354 |
goto out; |
saved_domainname = NULL; |
355 |
|
entry->program = saved_program; |
356 |
|
saved_program = NULL; |
357 |
|
entry->is_not = is_not; |
358 |
|
entry->is_last_name = is_last_name; |
359 |
|
list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list); |
360 |
|
entry = NULL; |
361 |
|
error = 0; |
362 |
} |
} |
363 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
364 |
if (!new_entry) |
ccs_put_name(saved_domainname); |
365 |
goto out; |
ccs_put_name(saved_program); |
366 |
new_entry->domainname = saved_domainname; |
kfree(entry); |
|
new_entry->program = saved_program; |
|
|
new_entry->is_not = is_not; |
|
|
new_entry->is_last_name = is_last_name; |
|
|
list1_add_tail_mb(&new_entry->list, &domain_keeper_list); |
|
|
error = 0; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
367 |
return error; |
return error; |
368 |
} |
} |
369 |
|
|
370 |
/** |
/** |
371 |
* ccs_write_domain_keeper_policy - Write "struct domain_keeper_entry" list. |
* ccs_write_domain_keeper_policy - Write "struct ccs_domain_keeper_entry" list. |
372 |
* |
* |
373 |
* @data: String to parse. |
* @data: String to parse. |
374 |
* @is_not: True if it is "no_keep_domain" entry. |
* @is_not: True if it is "no_keep_domain" entry. |
381 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
382 |
if (cp) { |
if (cp) { |
383 |
*cp = '\0'; |
*cp = '\0'; |
384 |
return update_domain_keeper_entry(cp + 6, data, |
return ccs_update_domain_keeper_entry(cp + 6, data, |
385 |
is_not, is_delete); |
is_not, is_delete); |
386 |
} |
} |
387 |
return update_domain_keeper_entry(data, NULL, is_not, is_delete); |
return ccs_update_domain_keeper_entry(data, NULL, is_not, is_delete); |
388 |
} |
} |
389 |
|
|
390 |
/** |
/** |
391 |
* ccs_read_domain_keeper_policy - Read "struct domain_keeper_entry" list. |
* ccs_read_domain_keeper_policy - Read "struct ccs_domain_keeper_entry" list. |
392 |
* |
* |
393 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
394 |
* |
* |
395 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
396 |
|
* |
397 |
|
* Caller holds ccs_read_lock(). |
398 |
*/ |
*/ |
399 |
bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head) |
bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head) |
400 |
{ |
{ |
401 |
struct list1_head *pos; |
struct list_head *pos; |
402 |
list1_for_each_cookie(pos, head->read_var2, &domain_keeper_list) { |
bool done = true; |
403 |
struct domain_keeper_entry *ptr; |
ccs_check_read_lock(); |
404 |
|
list_for_each_cookie(pos, head->read_var2, |
405 |
|
&ccs_domain_keeper_list) { |
406 |
|
struct ccs_domain_keeper_entry *ptr; |
407 |
const char *no; |
const char *no; |
408 |
const char *from = ""; |
const char *from = ""; |
409 |
const char *program = ""; |
const char *program = ""; |
410 |
ptr = list1_entry(pos, struct domain_keeper_entry, list); |
ptr = list_entry(pos, struct ccs_domain_keeper_entry, list); |
411 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
412 |
continue; |
continue; |
413 |
no = ptr->is_not ? "no_" : ""; |
no = ptr->is_not ? "no_" : ""; |
415 |
from = " from "; |
from = " from "; |
416 |
program = ptr->program->name; |
program = ptr->program->name; |
417 |
} |
} |
418 |
if (!ccs_io_printf(head, |
done = ccs_io_printf(head, |
419 |
"%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no, |
"%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no, |
420 |
program, from, ptr->domainname->name)) |
program, from, ptr->domainname->name); |
421 |
goto out; |
if (!done) |
422 |
|
break; |
423 |
} |
} |
424 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
425 |
} |
} |
426 |
|
|
427 |
/** |
/** |
428 |
* is_domain_keeper - Check whether the given program causes domain transition suppression. |
* ccs_is_domain_keeper - Check whether the given program causes domain transition suppression. |
429 |
* |
* |
430 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
431 |
* @program: The name of program. |
* @program: The name of program. |
433 |
* |
* |
434 |
* Returns true if executing @program supresses domain transition, |
* Returns true if executing @program supresses domain transition, |
435 |
* false otherwise. |
* false otherwise. |
436 |
|
* |
437 |
|
* Caller holds ccs_read_lock(). |
438 |
*/ |
*/ |
439 |
static bool is_domain_keeper(const struct path_info *domainname, |
static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname, |
440 |
const struct path_info *program, |
const struct ccs_path_info *program, |
441 |
const struct path_info *last_name) |
const struct ccs_path_info *last_name) |
442 |
{ |
{ |
443 |
struct domain_keeper_entry *ptr; |
struct ccs_domain_keeper_entry *ptr; |
444 |
bool flag = false; |
bool flag = false; |
445 |
list1_for_each_entry(ptr, &domain_keeper_list, list) { |
ccs_check_read_lock(); |
446 |
|
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
447 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
448 |
continue; |
continue; |
449 |
if (!ptr->is_last_name) { |
if (!ptr->is_last_name) { |
455 |
} |
} |
456 |
if (ptr->program && ccs_pathcmp(ptr->program, program)) |
if (ptr->program && ccs_pathcmp(ptr->program, program)) |
457 |
continue; |
continue; |
458 |
if (ptr->is_not) |
if (ptr->is_not) { |
459 |
return false; |
flag = false; |
460 |
|
break; |
461 |
|
} |
462 |
flag = true; |
flag = true; |
463 |
} |
} |
464 |
return flag; |
return flag; |
465 |
} |
} |
466 |
|
|
467 |
/* The list for "struct alias_entry". */ |
/* The list for "struct ccs_aggregator_entry". */ |
468 |
static LIST1_HEAD(alias_list); |
LIST_HEAD(ccs_aggregator_list); |
|
|
|
|
/** |
|
|
* update_alias_entry - Update "struct alias_entry" list. |
|
|
* |
|
|
* @original_name: The original program's real name. |
|
|
* @aliased_name: The symbolic program's symbolic link's name. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
static int update_alias_entry(const char *original_name, |
|
|
const char *aliased_name, |
|
|
const bool is_delete) |
|
|
{ |
|
|
struct alias_entry *new_entry; |
|
|
struct alias_entry *ptr; |
|
|
static DEFINE_MUTEX(lock); |
|
|
const struct path_info *saved_original_name; |
|
|
const struct path_info *saved_aliased_name; |
|
|
int error = -ENOMEM; |
|
|
if (!ccs_is_correct_path(original_name, 1, -1, -1, __func__) || |
|
|
!ccs_is_correct_path(aliased_name, 1, -1, -1, __func__)) |
|
|
return -EINVAL; /* No patterns allowed. */ |
|
|
saved_original_name = ccs_save_name(original_name); |
|
|
saved_aliased_name = ccs_save_name(aliased_name); |
|
|
if (!saved_original_name || !saved_aliased_name) |
|
|
return -ENOMEM; |
|
|
mutex_lock(&lock); |
|
|
list1_for_each_entry(ptr, &alias_list, list) { |
|
|
if (ptr->original_name != saved_original_name || |
|
|
ptr->aliased_name != saved_aliased_name) |
|
|
continue; |
|
|
ptr->is_deleted = is_delete; |
|
|
error = 0; |
|
|
goto out; |
|
|
} |
|
|
if (is_delete) { |
|
|
error = -ENOENT; |
|
|
goto out; |
|
|
} |
|
|
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
|
|
if (!new_entry) |
|
|
goto out; |
|
|
new_entry->original_name = saved_original_name; |
|
|
new_entry->aliased_name = saved_aliased_name; |
|
|
list1_add_tail_mb(&new_entry->list, &alias_list); |
|
|
error = 0; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
|
return error; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_read_alias_policy - Read "struct alias_entry" list. |
|
|
* |
|
|
* @head: Pointer to "struct ccs_io_buffer". |
|
|
* |
|
|
* Returns true on success, false otherwise. |
|
|
*/ |
|
|
bool ccs_read_alias_policy(struct ccs_io_buffer *head) |
|
|
{ |
|
|
struct list1_head *pos; |
|
|
list1_for_each_cookie(pos, head->read_var2, &alias_list) { |
|
|
struct alias_entry *ptr; |
|
|
ptr = list1_entry(pos, struct alias_entry, list); |
|
|
if (ptr->is_deleted) |
|
|
continue; |
|
|
if (!ccs_io_printf(head, KEYWORD_ALIAS "%s %s\n", |
|
|
ptr->original_name->name, |
|
|
ptr->aliased_name->name)) |
|
|
goto out; |
|
|
} |
|
|
return true; |
|
|
out: |
|
|
return false; |
|
|
} |
|
469 |
|
|
470 |
/** |
/** |
471 |
* ccs_write_alias_policy - Write "struct alias_entry" list. |
* ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list. |
|
* |
|
|
* @data: String to parse. |
|
|
* @is_delete: True if it is a delete request. |
|
|
* |
|
|
* Returns 0 on success, negative value otherwise. |
|
|
*/ |
|
|
int ccs_write_alias_policy(char *data, const bool is_delete) |
|
|
{ |
|
|
char *cp = strchr(data, ' '); |
|
|
if (!cp) |
|
|
return -EINVAL; |
|
|
*cp++ = '\0'; |
|
|
return update_alias_entry(data, cp, is_delete); |
|
|
} |
|
|
|
|
|
/* The list for "struct aggregator_entry". */ |
|
|
static LIST1_HEAD(aggregator_list); |
|
|
|
|
|
/** |
|
|
* update_aggregator_entry - Update "struct aggregator_entry" list. |
|
472 |
* |
* |
473 |
* @original_name: The original program's name. |
* @original_name: The original program's name. |
474 |
* @aggregated_name: The aggregated program's name. |
* @aggregated_name: The aggregated program's name. |
476 |
* |
* |
477 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
478 |
*/ |
*/ |
479 |
static int update_aggregator_entry(const char *original_name, |
static int ccs_update_aggregator_entry(const char *original_name, |
480 |
const char *aggregated_name, |
const char *aggregated_name, |
481 |
const bool is_delete) |
const bool is_delete) |
482 |
{ |
{ |
483 |
struct aggregator_entry *new_entry; |
struct ccs_aggregator_entry *entry = NULL; |
484 |
struct aggregator_entry *ptr; |
struct ccs_aggregator_entry *ptr; |
485 |
static DEFINE_MUTEX(lock); |
const struct ccs_path_info *saved_original_name; |
486 |
const struct path_info *saved_original_name; |
const struct ccs_path_info *saved_aggregated_name; |
487 |
const struct path_info *saved_aggregated_name; |
int error = is_delete ? -ENOENT : -ENOMEM; |
488 |
int error = -ENOMEM; |
if (!ccs_is_correct_path(original_name, 1, 0, -1) || |
489 |
if (!ccs_is_correct_path(original_name, 1, 0, -1, __func__) || |
!ccs_is_correct_path(aggregated_name, 1, -1, -1)) |
|
!ccs_is_correct_path(aggregated_name, 1, -1, -1, __func__)) |
|
490 |
return -EINVAL; |
return -EINVAL; |
491 |
saved_original_name = ccs_save_name(original_name); |
saved_original_name = ccs_get_name(original_name); |
492 |
saved_aggregated_name = ccs_save_name(aggregated_name); |
saved_aggregated_name = ccs_get_name(aggregated_name); |
493 |
if (!saved_original_name || !saved_aggregated_name) |
if (!saved_original_name || !saved_aggregated_name) { |
494 |
|
ccs_put_name(saved_original_name); |
495 |
|
ccs_put_name(saved_aggregated_name); |
496 |
return -ENOMEM; |
return -ENOMEM; |
497 |
mutex_lock(&lock); |
} |
498 |
list1_for_each_entry(ptr, &aggregator_list, list) { |
if (!is_delete) |
499 |
|
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
500 |
|
mutex_lock(&ccs_policy_lock); |
501 |
|
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
502 |
if (ptr->original_name != saved_original_name || |
if (ptr->original_name != saved_original_name || |
503 |
ptr->aggregated_name != saved_aggregated_name) |
ptr->aggregated_name != saved_aggregated_name) |
504 |
continue; |
continue; |
505 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
506 |
error = 0; |
error = 0; |
507 |
goto out; |
break; |
508 |
} |
} |
509 |
if (is_delete) { |
if (!is_delete && error && ccs_memory_ok(entry, sizeof(*entry))) { |
510 |
error = -ENOENT; |
entry->original_name = saved_original_name; |
511 |
goto out; |
saved_original_name = NULL; |
512 |
|
entry->aggregated_name = saved_aggregated_name; |
513 |
|
saved_aggregated_name = NULL; |
514 |
|
list_add_tail_rcu(&entry->list, &ccs_aggregator_list); |
515 |
|
entry = NULL; |
516 |
|
error = 0; |
517 |
} |
} |
518 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
519 |
if (!new_entry) |
ccs_put_name(saved_original_name); |
520 |
goto out; |
ccs_put_name(saved_aggregated_name); |
521 |
new_entry->original_name = saved_original_name; |
kfree(entry); |
|
new_entry->aggregated_name = saved_aggregated_name; |
|
|
list1_add_tail_mb(&new_entry->list, &aggregator_list); |
|
|
error = 0; |
|
|
out: |
|
|
mutex_unlock(&lock); |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
|
522 |
return error; |
return error; |
523 |
} |
} |
524 |
|
|
525 |
/** |
/** |
526 |
* ccs_read_aggregator_policy - Read "struct aggregator_entry" list. |
* ccs_read_aggregator_policy - Read "struct ccs_aggregator_entry" list. |
527 |
* |
* |
528 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
529 |
* |
* |
530 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
531 |
|
* |
532 |
|
* Caller holds ccs_read_lock(). |
533 |
*/ |
*/ |
534 |
bool ccs_read_aggregator_policy(struct ccs_io_buffer *head) |
bool ccs_read_aggregator_policy(struct ccs_io_buffer *head) |
535 |
{ |
{ |
536 |
struct list1_head *pos; |
struct list_head *pos; |
537 |
list1_for_each_cookie(pos, head->read_var2, &aggregator_list) { |
bool done = true; |
538 |
struct aggregator_entry *ptr; |
ccs_check_read_lock(); |
539 |
ptr = list1_entry(pos, struct aggregator_entry, list); |
list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) { |
540 |
|
struct ccs_aggregator_entry *ptr; |
541 |
|
ptr = list_entry(pos, struct ccs_aggregator_entry, list); |
542 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
543 |
continue; |
continue; |
544 |
if (!ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", |
done = ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", |
545 |
ptr->original_name->name, |
ptr->original_name->name, |
546 |
ptr->aggregated_name->name)) |
ptr->aggregated_name->name); |
547 |
goto out; |
if (!done) |
548 |
|
break; |
549 |
} |
} |
550 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
551 |
} |
} |
552 |
|
|
553 |
/** |
/** |
554 |
* ccs_write_aggregator_policy - Write "struct aggregator_entry" list. |
* ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list. |
555 |
* |
* |
556 |
* @data: String to parse. |
* @data: String to parse. |
557 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
560 |
*/ |
*/ |
561 |
int ccs_write_aggregator_policy(char *data, const bool is_delete) |
int ccs_write_aggregator_policy(char *data, const bool is_delete) |
562 |
{ |
{ |
563 |
char *cp = strchr(data, ' '); |
char *w[2]; |
564 |
if (!cp) |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0]) |
565 |
return -EINVAL; |
return -EINVAL; |
566 |
*cp++ = '\0'; |
return ccs_update_aggregator_entry(w[0], w[1], is_delete); |
|
return update_aggregator_entry(data, cp, is_delete); |
|
567 |
} |
} |
568 |
|
|
569 |
/* Domain create/delete/undelete handler. */ |
/* Domain create/delete handler. */ |
|
|
|
|
/* #define DEBUG_DOMAIN_UNDELETE */ |
|
570 |
|
|
571 |
/** |
/** |
572 |
* ccs_delete_domain - Delete a domain. |
* ccs_delete_domain - Delete a domain. |
577 |
*/ |
*/ |
578 |
int ccs_delete_domain(char *domainname) |
int ccs_delete_domain(char *domainname) |
579 |
{ |
{ |
580 |
struct domain_info *domain; |
struct ccs_domain_info *domain; |
581 |
struct path_info name; |
struct ccs_path_info name; |
582 |
name.name = domainname; |
name.name = domainname; |
583 |
ccs_fill_path_info(&name); |
ccs_fill_path_info(&name); |
584 |
mutex_lock(&new_domain_assign_lock); |
mutex_lock(&ccs_policy_lock); |
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk(KERN_DEBUG "ccs_delete_domain %s\n", domainname); |
|
|
list1_for_each_entry(domain, &domain_list, list) { |
|
|
if (ccs_pathcmp(domain->domainname, &name)) |
|
|
continue; |
|
|
printk(KERN_DEBUG "List: %p %u\n", domain, domain->is_deleted); |
|
|
} |
|
|
#endif |
|
585 |
/* Is there an active domain? */ |
/* Is there an active domain? */ |
586 |
list1_for_each_entry(domain, &domain_list, list) { |
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
587 |
struct domain_info *domain2; |
/* Never delete ccs_kernel_domain */ |
588 |
/* Never delete KERNEL_DOMAIN */ |
if (domain == &ccs_kernel_domain) |
|
if (domain == &KERNEL_DOMAIN) |
|
589 |
continue; |
continue; |
590 |
if (domain->is_deleted || |
if (domain->is_deleted || |
591 |
ccs_pathcmp(domain->domainname, &name)) |
ccs_pathcmp(domain->domainname, &name)) |
592 |
continue; |
continue; |
593 |
/* Mark already deleted domains as non undeletable. */ |
domain->is_deleted = true; |
|
list1_for_each_entry(domain2, &domain_list, list) { |
|
|
if (!domain2->is_deleted || |
|
|
ccs_pathcmp(domain2->domainname, &name)) |
|
|
continue; |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
if (domain2->is_deleted != 255) |
|
|
printk(KERN_DEBUG |
|
|
"Marked %p as non undeletable\n", |
|
|
domain2); |
|
|
#endif |
|
|
domain2->is_deleted = 255; |
|
|
} |
|
|
/* Delete and mark active domain as undeletable. */ |
|
|
domain->is_deleted = 1; |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk(KERN_DEBUG "Marked %p as undeletable\n", domain); |
|
|
#endif |
|
594 |
break; |
break; |
595 |
} |
} |
596 |
mutex_unlock(&new_domain_assign_lock); |
mutex_unlock(&ccs_policy_lock); |
597 |
return 0; |
return 0; |
598 |
} |
} |
599 |
|
|
600 |
/** |
/** |
|
* ccs_undelete_domain - Undelete a domain. |
|
|
* |
|
|
* @domainname: The name of domain. |
|
|
* |
|
|
* Returns pointer to "struct domain_info" on success, NULL otherwise. |
|
|
*/ |
|
|
struct domain_info *ccs_undelete_domain(const char *domainname) |
|
|
{ |
|
|
struct domain_info *domain; |
|
|
struct domain_info *candidate_domain = NULL; |
|
|
struct path_info name; |
|
|
name.name = domainname; |
|
|
ccs_fill_path_info(&name); |
|
|
mutex_lock(&new_domain_assign_lock); |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk(KERN_DEBUG "ccs_undelete_domain %s\n", domainname); |
|
|
list1_for_each_entry(domain, &domain_list, list) { |
|
|
if (ccs_pathcmp(domain->domainname, &name)) |
|
|
continue; |
|
|
printk(KERN_DEBUG "List: %p %u\n", domain, domain->is_deleted); |
|
|
} |
|
|
#endif |
|
|
list1_for_each_entry(domain, &domain_list, list) { |
|
|
if (ccs_pathcmp(&name, domain->domainname)) |
|
|
continue; |
|
|
if (!domain->is_deleted) { |
|
|
/* This domain is active. I can't undelete. */ |
|
|
candidate_domain = NULL; |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk(KERN_DEBUG "%p is active. I can't undelete.\n", |
|
|
domain); |
|
|
#endif |
|
|
break; |
|
|
} |
|
|
/* Is this domain undeletable? */ |
|
|
if (domain->is_deleted == 1) |
|
|
candidate_domain = domain; |
|
|
} |
|
|
if (candidate_domain) { |
|
|
candidate_domain->is_deleted = 0; |
|
|
#ifdef DEBUG_DOMAIN_UNDELETE |
|
|
printk(KERN_DEBUG "%p was undeleted.\n", candidate_domain); |
|
|
#endif |
|
|
} |
|
|
mutex_unlock(&new_domain_assign_lock); |
|
|
return candidate_domain; |
|
|
} |
|
|
|
|
|
/** |
|
601 |
* ccs_find_or_assign_new_domain - Create a domain. |
* ccs_find_or_assign_new_domain - Create a domain. |
602 |
* |
* |
603 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
604 |
* @profile: Profile number to assign if the domain was newly created. |
* @profile: Profile number to assign if the domain was newly created. |
605 |
* |
* |
606 |
* Returns pointer to "struct domain_info" on success, NULL otherwise. |
* Returns pointer to "struct ccs_domain_info" on success, NULL otherwise. |
607 |
*/ |
*/ |
608 |
struct domain_info *ccs_find_or_assign_new_domain(const char *domainname, |
struct ccs_domain_info *ccs_find_or_assign_new_domain(const char *domainname, |
609 |
const u8 profile) |
const u8 profile) |
610 |
{ |
{ |
611 |
struct domain_info *domain = NULL; |
struct ccs_domain_info *entry; |
612 |
const struct path_info *saved_domainname; |
struct ccs_domain_info *domain; |
613 |
mutex_lock(&new_domain_assign_lock); |
const struct ccs_path_info *saved_domainname; |
614 |
domain = ccs_find_domain(domainname); |
bool found = false; |
615 |
if (domain) |
|
616 |
goto out; |
if (!ccs_is_correct_domain(domainname)) |
617 |
if (!ccs_is_correct_domain(domainname, __func__)) |
return NULL; |
618 |
goto out; |
saved_domainname = ccs_get_name(domainname); |
|
saved_domainname = ccs_save_name(domainname); |
|
619 |
if (!saved_domainname) |
if (!saved_domainname) |
620 |
goto out; |
return NULL; |
621 |
/* Can I reuse memory of deleted domain? */ |
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
622 |
list1_for_each_entry(domain, &domain_list, list) { |
mutex_lock(&ccs_policy_lock); |
623 |
struct task_struct *p; |
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
624 |
struct acl_info *ptr; |
if (domain->is_deleted || |
625 |
bool flag; |
ccs_pathcmp(saved_domainname, domain->domainname)) |
|
if (!domain->is_deleted || |
|
|
domain->domainname != saved_domainname) |
|
|
continue; |
|
|
flag = false; |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
read_lock(&tasklist_lock); |
|
|
for_each_process(p) { |
|
|
if (p->domain_info != domain) |
|
|
continue; |
|
|
flag = true; |
|
|
break; |
|
|
} |
|
|
read_unlock(&tasklist_lock); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
if (flag) |
|
626 |
continue; |
continue; |
627 |
#ifdef DEBUG_DOMAIN_UNDELETE |
found = true; |
628 |
printk(KERN_DEBUG "Reusing %p %s\n", domain, |
break; |
|
domain->domainname->name); |
|
|
#endif |
|
|
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
|
|
ptr->type |= ACL_DELETED; |
|
|
} |
|
|
/* |
|
|
* Don't use ccs_set_domain_flag() because |
|
|
* new_domain_assign_lock is held. |
|
|
*/ |
|
|
domain->flags = 0; |
|
|
domain->profile = profile; |
|
|
domain->quota_warned = false; |
|
|
mb(); /* Avoid out-of-order execution. */ |
|
|
domain->is_deleted = 0; |
|
|
goto out; |
|
|
} |
|
|
/* No memory reusable. Create using new memory. */ |
|
|
domain = ccs_alloc_element(sizeof(*domain)); |
|
|
if (domain) { |
|
|
INIT_LIST1_HEAD(&domain->acl_info_list); |
|
|
domain->domainname = saved_domainname; |
|
|
domain->profile = profile; |
|
|
list1_add_tail_mb(&domain->list, &domain_list); |
|
629 |
} |
} |
630 |
out: |
if (!found && ccs_memory_ok(entry, sizeof(*entry))) { |
631 |
mutex_unlock(&new_domain_assign_lock); |
INIT_LIST_HEAD(&entry->acl_info_list); |
632 |
return domain; |
entry->domainname = saved_domainname; |
633 |
|
saved_domainname = NULL; |
634 |
|
entry->profile = profile; |
635 |
|
list_add_tail_rcu(&entry->list, &ccs_domain_list); |
636 |
|
domain = entry; |
637 |
|
entry = NULL; |
638 |
|
found = true; |
639 |
|
} |
640 |
|
mutex_unlock(&ccs_policy_lock); |
641 |
|
ccs_put_name(saved_domainname); |
642 |
|
kfree(entry); |
643 |
|
return found ? domain : NULL; |
644 |
} |
} |
645 |
|
|
646 |
/** |
/** |
647 |
* get_argv0 - Get argv[0]. |
* ccs_get_argv0 - Get argv[0]. |
648 |
* |
* |
649 |
* @bprm: Pointer to "struct linux_binprm". |
* @ee: Pointer to "struct ccs_execve_entry". |
|
* @tmp: Buffer for temporal use. |
|
650 |
* |
* |
651 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
652 |
*/ |
*/ |
653 |
static bool get_argv0(struct linux_binprm *bprm, struct ccs_page_buffer *tmp) |
static bool ccs_get_argv0(struct ccs_execve_entry *ee) |
654 |
{ |
{ |
655 |
char *arg_ptr = tmp->buffer; |
struct linux_binprm *bprm = ee->bprm; |
656 |
|
char *arg_ptr = ee->tmp; |
657 |
int arg_len = 0; |
int arg_len = 0; |
658 |
unsigned long pos = bprm->p; |
unsigned long pos = bprm->p; |
|
int i = pos / PAGE_SIZE; |
|
659 |
int offset = pos % PAGE_SIZE; |
int offset = pos % PAGE_SIZE; |
660 |
bool done = false; |
bool done = false; |
661 |
if (!bprm->argc) |
if (!bprm->argc) |
662 |
goto out; |
goto out; |
663 |
while (1) { |
while (1) { |
664 |
struct page *page; |
if (!ccs_dump_page(bprm, pos, &ee->dump)) |
|
const char *kaddr; |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, |
|
|
NULL) <= 0) |
|
665 |
goto out; |
goto out; |
666 |
pos += PAGE_SIZE - offset; |
pos += PAGE_SIZE - offset; |
|
#else |
|
|
page = bprm->page[i]; |
|
|
#endif |
|
|
/* Map. */ |
|
|
kaddr = kmap(page); |
|
|
if (!kaddr) { /* Mapping failed. */ |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
goto out; |
|
|
} |
|
667 |
/* Read. */ |
/* Read. */ |
668 |
while (offset < PAGE_SIZE) { |
while (offset < PAGE_SIZE) { |
669 |
|
const char *kaddr = ee->dump.data; |
670 |
const unsigned char c = kaddr[offset++]; |
const unsigned char c = kaddr[offset++]; |
671 |
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
672 |
if (c == '\\') { |
if (c == '\\') { |
689 |
break; |
break; |
690 |
} |
} |
691 |
} |
} |
|
/* Unmap. */ |
|
|
kunmap(page); |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
i++; |
|
692 |
offset = 0; |
offset = 0; |
693 |
if (done) |
if (done) |
694 |
break; |
break; |
699 |
} |
} |
700 |
|
|
701 |
/** |
/** |
702 |
* find_next_domain - Find a domain. |
* ccs_find_next_domain - Find a domain. |
703 |
* |
* |
704 |
* @bprm: Pointer to "struct linux_binprm". |
* @ee: Pointer to "struct ccs_execve_entry". |
|
* @next_domain: Pointer to pointer to "struct domain_info". |
|
|
* @path_to_verify: Pathname to verify. May be NULL. |
|
|
* @tmp: Buffer for temporal use. |
|
705 |
* |
* |
706 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
707 |
|
* |
708 |
|
* Caller holds ccs_read_lock(). |
709 |
*/ |
*/ |
710 |
static int find_next_domain(struct linux_binprm *bprm, |
static int ccs_find_next_domain(struct ccs_execve_entry *ee) |
|
struct domain_info **next_domain, |
|
|
const struct path_info *path_to_verify, |
|
|
struct ccs_page_buffer *tmp) |
|
711 |
{ |
{ |
712 |
/* |
struct ccs_request_info *r = &ee->r; |
713 |
* This function assumes that the size of buffer returned by |
const struct ccs_path_info *handler = ee->handler; |
714 |
* ccs_realpath() = CCS_MAX_PATHNAME_LEN. |
struct ccs_domain_info *domain = NULL; |
715 |
*/ |
const char *old_domain_name = r->domain->domainname->name; |
716 |
struct domain_info *old_domain = current->domain_info; |
struct linux_binprm *bprm = ee->bprm; |
717 |
struct domain_info *domain = NULL; |
const u8 mode = r->mode; |
|
const char *old_domain_name = old_domain->domainname->name; |
|
|
const char *original_name = bprm->filename; |
|
|
char *new_domain_name = NULL; |
|
|
char *real_program_name = NULL; |
|
|
char *symlink_program_name = NULL; |
|
|
const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_FILE); |
|
718 |
const bool is_enforce = (mode == 3); |
const bool is_enforce = (mode == 3); |
719 |
|
const u32 ccs_flags = current->ccs_flags; |
720 |
|
char *new_domain_name = NULL; |
721 |
|
struct ccs_path_info rn; /* real name */ |
722 |
|
struct ccs_path_info ln; /* last name */ |
723 |
int retval; |
int retval; |
724 |
struct path_info r; /* real name */ |
ccs_check_read_lock(); |
725 |
struct path_info s; /* symlink name */ |
retry: |
726 |
struct path_info l; /* last name */ |
current->ccs_flags = ccs_flags; |
727 |
|
r->cond = NULL; |
728 |
{ |
/* Get symlink's pathname of program. */ |
729 |
/* |
retval = ccs_symlink_path(bprm->filename, ee); |
730 |
* Built-in initializers. This is needed because policies are |
if (retval < 0) |
|
* not loaded until starting /sbin/init. |
|
|
*/ |
|
|
static bool first = true; |
|
|
if (first) { |
|
|
update_domain_initializer_entry(NULL, "/sbin/hotplug", |
|
|
false, false); |
|
|
update_domain_initializer_entry(NULL, "/sbin/modprobe", |
|
|
false, false); |
|
|
first = false; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Get ccs_realpath of program. */ |
|
|
retval = -ENOENT; /* I hope ccs_realpath() won't fail with -ENOMEM. */ |
|
|
real_program_name = ccs_realpath(original_name); |
|
|
if (!real_program_name) |
|
|
goto out; |
|
|
/* Get ccs_realpath of symbolic link. */ |
|
|
symlink_program_name = ccs_realpath_nofollow(original_name); |
|
|
if (!symlink_program_name) |
|
731 |
goto out; |
goto out; |
732 |
|
|
733 |
r.name = real_program_name; |
rn.name = ee->program_path; |
734 |
ccs_fill_path_info(&r); |
ccs_fill_path_info(&rn); |
735 |
s.name = symlink_program_name; |
ln.name = ccs_get_last_name(r->domain); |
736 |
ccs_fill_path_info(&s); |
ccs_fill_path_info(&ln); |
|
l.name = ccs_get_last_name(old_domain); |
|
|
ccs_fill_path_info(&l); |
|
737 |
|
|
738 |
if (path_to_verify) { |
if (handler) { |
739 |
if (ccs_pathcmp(&r, path_to_verify)) { |
if (ccs_pathcmp(&rn, handler)) { |
740 |
/* Failed to verify execute handler. */ |
/* Failed to verify execute handler. */ |
741 |
static u8 counter = 20; |
static u8 counter = 20; |
742 |
if (counter) { |
if (counter) { |
743 |
counter--; |
counter--; |
744 |
printk(KERN_WARNING "Failed to verify: %s\n", |
printk(KERN_WARNING "Failed to verify: %s\n", |
745 |
path_to_verify->name); |
handler->name); |
746 |
} |
} |
747 |
goto out; |
goto out; |
748 |
} |
} |
749 |
goto calculate_domain; |
goto calculate_domain; |
750 |
} |
} |
751 |
|
|
752 |
/* Check 'alias' directive. */ |
/* Compare basename of program_path and argv[0] */ |
753 |
if (ccs_pathcmp(&r, &s)) { |
r->mode = ccs_check_flags(r->domain, CCS_MAC_FOR_ARGV0); |
754 |
struct alias_entry *ptr; |
if (bprm->argc > 0 && r->mode) { |
755 |
/* Is this program allowed to be called via symbolic links? */ |
char *base_argv0 = ee->tmp; |
|
list1_for_each_entry(ptr, &alias_list, list) { |
|
|
if (ptr->is_deleted || |
|
|
ccs_pathcmp(&r, ptr->original_name) || |
|
|
ccs_pathcmp(&s, ptr->aliased_name)) |
|
|
continue; |
|
|
memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN); |
|
|
strncpy(real_program_name, ptr->aliased_name->name, |
|
|
CCS_MAX_PATHNAME_LEN - 1); |
|
|
ccs_fill_path_info(&r); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Compare basename of real_program_name and argv[0] */ |
|
|
if (bprm->argc > 0 && ccs_check_flags(CCS_TOMOYO_MAC_FOR_ARGV0)) { |
|
|
char *base_argv0 = tmp->buffer; |
|
756 |
const char *base_filename; |
const char *base_filename; |
757 |
retval = -ENOMEM; |
retval = -ENOMEM; |
758 |
if (!get_argv0(bprm, tmp)) |
if (!ccs_get_argv0(ee)) |
759 |
goto out; |
goto out; |
760 |
base_filename = strrchr(real_program_name, '/'); |
base_filename = strrchr(ee->program_path, '/'); |
761 |
if (!base_filename) |
if (!base_filename) |
762 |
base_filename = real_program_name; |
base_filename = ee->program_path; |
763 |
else |
else |
764 |
base_filename++; |
base_filename++; |
765 |
if (strcmp(base_argv0, base_filename)) { |
if (strcmp(base_argv0, base_filename)) { |
766 |
retval = ccs_check_argv0_perm(&r, base_argv0); |
retval = ccs_check_argv0_perm(r, &rn, base_argv0); |
767 |
if (retval) |
if (retval == 1) |
768 |
|
goto retry; |
769 |
|
if (retval < 0) |
770 |
goto out; |
goto out; |
771 |
} |
} |
772 |
} |
} |
773 |
|
|
774 |
/* Check 'aggregator' directive. */ |
/* Check 'aggregator' directive. */ |
775 |
{ |
{ |
776 |
struct aggregator_entry *ptr; |
struct ccs_aggregator_entry *ptr; |
777 |
/* Is this program allowed to be aggregated? */ |
/* Is this program allowed to be aggregated? */ |
778 |
list1_for_each_entry(ptr, &aggregator_list, list) { |
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
779 |
if (ptr->is_deleted || |
if (ptr->is_deleted || |
780 |
!ccs_path_matches_pattern(&r, ptr->original_name)) |
!ccs_path_matches_pattern(&rn, ptr->original_name)) |
781 |
continue; |
continue; |
782 |
memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN); |
strncpy(ee->program_path, ptr->aggregated_name->name, |
|
strncpy(real_program_name, ptr->aggregated_name->name, |
|
783 |
CCS_MAX_PATHNAME_LEN - 1); |
CCS_MAX_PATHNAME_LEN - 1); |
784 |
ccs_fill_path_info(&r); |
ccs_fill_path_info(&rn); |
785 |
break; |
break; |
786 |
} |
} |
787 |
} |
} |
788 |
|
|
789 |
/* Check execute permission. */ |
/* Check execute permission. */ |
790 |
retval = ccs_check_exec_perm(&r, bprm, tmp); |
r->mode = mode; |
791 |
|
retval = ccs_check_exec_perm(r, &rn); |
792 |
|
if (retval == 1) |
793 |
|
goto retry; |
794 |
if (retval < 0) |
if (retval < 0) |
795 |
goto out; |
goto out; |
796 |
|
|
797 |
calculate_domain: |
calculate_domain: |
798 |
new_domain_name = tmp->buffer; |
new_domain_name = ee->tmp; |
799 |
if (is_domain_initializer(old_domain->domainname, &r, &l)) { |
if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) { |
800 |
/* Transit to the child of KERNEL_DOMAIN domain. */ |
/* Transit to the child of ccs_kernel_domain domain. */ |
801 |
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, |
snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1, |
802 |
ROOT_NAME " " "%s", real_program_name); |
ROOT_NAME " " "%s", ee->program_path); |
803 |
} else if (old_domain == &KERNEL_DOMAIN && !sbin_init_started) { |
} else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) { |
804 |
/* |
/* |
805 |
* Needn't to transit from kernel domain before starting |
* Needn't to transit from kernel domain before starting |
806 |
* /sbin/init. But transit from kernel domain if executing |
* /sbin/init. But transit from kernel domain if executing |
807 |
* initializers because they might start before /sbin/init. |
* initializers because they might start before /sbin/init. |
808 |
*/ |
*/ |
809 |
domain = old_domain; |
domain = r->domain; |
810 |
} else if (is_domain_keeper(old_domain->domainname, &r, &l)) { |
} else if (ccs_is_domain_keeper(r->domain->domainname, &rn, &ln)) { |
811 |
/* Keep current domain. */ |
/* Keep current domain. */ |
812 |
domain = old_domain; |
domain = r->domain; |
813 |
} else { |
} else { |
814 |
/* Normal domain transition. */ |
/* Normal domain transition. */ |
815 |
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, |
snprintf(new_domain_name, CCS_EXEC_TMPSIZE - 1, |
816 |
"%s %s", old_domain_name, real_program_name); |
"%s %s", old_domain_name, ee->program_path); |
817 |
} |
} |
818 |
if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN) |
if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN) |
819 |
goto done; |
goto done; |
820 |
domain = ccs_find_domain(new_domain_name); |
domain = ccs_find_domain(new_domain_name); |
821 |
if (domain) |
if (domain) |
822 |
goto done; |
goto done; |
823 |
if (is_enforce && ccs_check_supervisor(NULL, |
if (is_enforce) { |
824 |
"# wants to create domain\n%s\n", |
int error = ccs_check_supervisor(r, |
825 |
new_domain_name)) |
"# wants to create domain\n" |
826 |
|
"%s\n", new_domain_name); |
827 |
|
if (error == 1) |
828 |
|
goto retry; |
829 |
|
if (error < 0) |
830 |
goto done; |
goto done; |
831 |
domain = ccs_find_or_assign_new_domain(new_domain_name, |
} |
832 |
old_domain->profile); |
domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile); |
833 |
if (domain) |
if (domain) |
834 |
audit_domain_creation_log(new_domain_name, mode, |
ccs_audit_domain_creation_log(r->domain); |
|
domain->profile); |
|
835 |
done: |
done: |
836 |
if (!domain) { |
if (!domain) { |
837 |
printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", |
printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", |
838 |
new_domain_name); |
new_domain_name); |
839 |
if (is_enforce) |
if (is_enforce) |
840 |
retval = -EPERM; |
retval = -EPERM; |
841 |
else |
else { |
842 |
ccs_set_domain_flag(old_domain, false, |
retval = 0; |
843 |
DOMAIN_FLAGS_TRANSITION_FAILED); |
r->domain->domain_transition_failed = true; |
844 |
|
} |
845 |
} else { |
} else { |
846 |
retval = 0; |
retval = 0; |
847 |
} |
} |
848 |
out: |
out: |
849 |
ccs_free(real_program_name); |
if (domain) |
850 |
ccs_free(symlink_program_name); |
r->domain = domain; |
|
*next_domain = domain ? domain : old_domain; |
|
851 |
return retval; |
return retval; |
852 |
} |
} |
853 |
|
|
854 |
/** |
/** |
855 |
* check_environ - Check permission for environment variable names. |
* ccs_check_environ - Check permission for environment variable names. |
856 |
* |
* |
857 |
* @bprm: Pointer to "struct linux_binprm". |
* @ee: Pointer to "struct ccs_execve_entry". |
|
* @tmp: Buffer for temporal use. |
|
858 |
* |
* |
859 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
860 |
*/ |
*/ |
861 |
static int check_environ(struct linux_binprm *bprm, struct ccs_page_buffer *tmp) |
static int ccs_check_environ(struct ccs_execve_entry *ee) |
862 |
{ |
{ |
863 |
const u8 profile = current->domain_info->profile; |
struct ccs_request_info *r = &ee->r; |
864 |
const u8 mode = ccs_check_flags(CCS_TOMOYO_MAC_FOR_ENV); |
struct linux_binprm *bprm = ee->bprm; |
865 |
char *arg_ptr = tmp->buffer; |
char *arg_ptr = ee->tmp; |
866 |
int arg_len = 0; |
int arg_len = 0; |
867 |
unsigned long pos = bprm->p; |
unsigned long pos = bprm->p; |
|
int i = pos / PAGE_SIZE; |
|
868 |
int offset = pos % PAGE_SIZE; |
int offset = pos % PAGE_SIZE; |
869 |
int argv_count = bprm->argc; |
int argv_count = bprm->argc; |
870 |
int envp_count = bprm->envc; |
int envp_count = bprm->envc; |
871 |
/* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */ |
/* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */ |
872 |
int error = -ENOMEM; |
int error = -ENOMEM; |
873 |
if (!mode || !envp_count) |
if (!r->mode || !envp_count) |
874 |
return 0; |
return 0; |
875 |
while (error == -ENOMEM) { |
while (error == -ENOMEM) { |
876 |
struct page *page; |
if (!ccs_dump_page(bprm, pos, &ee->dump)) |
|
const char *kaddr; |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, |
|
|
NULL) <= 0) |
|
877 |
goto out; |
goto out; |
878 |
pos += PAGE_SIZE - offset; |
pos += PAGE_SIZE - offset; |
|
#else |
|
|
page = bprm->page[i]; |
|
|
#endif |
|
|
/* Map. */ |
|
|
kaddr = kmap(page); |
|
|
if (!kaddr) { /* Mapping failed. */ |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
goto out; |
|
|
} |
|
879 |
/* Read. */ |
/* Read. */ |
880 |
while (argv_count && offset < PAGE_SIZE) { |
while (argv_count && offset < PAGE_SIZE) { |
881 |
|
const char *kaddr = ee->dump.data; |
882 |
if (!kaddr[offset++]) |
if (!kaddr[offset++]) |
883 |
argv_count--; |
argv_count--; |
884 |
} |
} |
885 |
if (argv_count) |
if (argv_count) { |
886 |
goto unmap_page; |
offset = 0; |
887 |
|
continue; |
888 |
|
} |
889 |
while (offset < PAGE_SIZE) { |
while (offset < PAGE_SIZE) { |
890 |
|
const char *kaddr = ee->dump.data; |
891 |
const unsigned char c = kaddr[offset++]; |
const unsigned char c = kaddr[offset++]; |
892 |
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
893 |
if (c == '=') { |
if (c == '=') { |
909 |
} |
} |
910 |
if (c) |
if (c) |
911 |
continue; |
continue; |
912 |
if (ccs_check_env_perm(arg_ptr, profile, mode)) { |
if (ccs_check_env_perm(r, arg_ptr)) { |
913 |
error = -EPERM; |
error = -EPERM; |
914 |
break; |
break; |
915 |
} |
} |
919 |
} |
} |
920 |
arg_len = 0; |
arg_len = 0; |
921 |
} |
} |
|
unmap_page: |
|
|
/* Unmap. */ |
|
|
kunmap(page); |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
i++; |
|
922 |
offset = 0; |
offset = 0; |
923 |
} |
} |
924 |
out: |
out: |
925 |
if (error && mode != 3) |
if (r->mode != 3) |
926 |
error = 0; |
error = 0; |
927 |
return error; |
return error; |
928 |
} |
} |
929 |
|
|
930 |
/** |
/** |
931 |
* unescape - Unescape escaped string. |
* ccs_unescape - Unescape escaped string. |
932 |
* |
* |
933 |
* @dest: String to unescape. |
* @dest: String to unescape. |
934 |
* |
* |
935 |
* Returns nothing. |
* Returns nothing. |
936 |
*/ |
*/ |
937 |
static void unescape(unsigned char *dest) |
static void ccs_unescape(unsigned char *dest) |
938 |
{ |
{ |
939 |
unsigned char *src = dest; |
unsigned char *src = dest; |
940 |
unsigned char c; |
unsigned char c; |
941 |
unsigned char d; |
unsigned char d; |
942 |
unsigned char e; |
unsigned char e; |
943 |
while ((c = *src++) != '\0') { |
while (1) { |
944 |
|
c = *src++; |
945 |
|
if (!c) |
946 |
|
break; |
947 |
if (c != '\\') { |
if (c != '\\') { |
948 |
*dest++ = c; |
*dest++ = c; |
949 |
continue; |
continue; |
967 |
} |
} |
968 |
|
|
969 |
/** |
/** |
970 |
* root_depth - Get number of directories to strip. |
* ccs_root_depth - Get number of directories to strip. |
971 |
* |
* |
972 |
* @dentry: Pointer to "struct dentry". |
* @dentry: Pointer to "struct dentry". |
973 |
* @vfsmnt: Pointer to "struct vfsmount". |
* @vfsmnt: Pointer to "struct vfsmount". |
974 |
* |
* |
975 |
* Returns number of directories to strip. |
* Returns number of directories to strip. |
976 |
*/ |
*/ |
977 |
static inline int root_depth(struct dentry *dentry, struct vfsmount *vfsmnt) |
static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt) |
978 |
{ |
{ |
979 |
int depth = 0; |
int depth = 0; |
980 |
/***** CRITICAL SECTION START *****/ |
/***** CRITICAL SECTION START *****/ |
981 |
spin_lock(&dcache_lock); |
ccs_realpath_lock(); |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
|
|
spin_lock(&vfsmount_lock); |
|
|
#endif |
|
982 |
for (;;) { |
for (;;) { |
983 |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
984 |
/* Global root? */ |
/* Global root? */ |
991 |
dentry = dentry->d_parent; |
dentry = dentry->d_parent; |
992 |
depth++; |
depth++; |
993 |
} |
} |
994 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
ccs_realpath_unlock(); |
|
spin_unlock(&vfsmount_lock); |
|
|
#endif |
|
|
spin_unlock(&dcache_lock); |
|
995 |
/***** CRITICAL SECTION END *****/ |
/***** CRITICAL SECTION END *****/ |
996 |
return depth; |
return depth; |
997 |
} |
} |
998 |
|
|
999 |
/** |
/** |
1000 |
* get_root_depth - return the depth of root directory. |
* ccs_get_root_depth - return the depth of root directory. |
1001 |
* |
* |
1002 |
* Returns number of directories to strip. |
* Returns number of directories to strip. |
1003 |
*/ |
*/ |
1004 |
static int get_root_depth(void) |
static int ccs_get_root_depth(void) |
1005 |
{ |
{ |
1006 |
int depth; |
int depth; |
1007 |
struct dentry *dentry; |
struct dentry *dentry; |
1022 |
#endif |
#endif |
1023 |
read_unlock(¤t->fs->lock); |
read_unlock(¤t->fs->lock); |
1024 |
/***** CRITICAL SECTION END *****/ |
/***** CRITICAL SECTION END *****/ |
1025 |
depth = root_depth(dentry, vfsmnt); |
depth = ccs_root_depth(dentry, vfsmnt); |
1026 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
1027 |
path_put(&root); |
path_put(&root); |
1028 |
#else |
#else |
1032 |
return depth; |
return depth; |
1033 |
} |
} |
1034 |
|
|
1035 |
|
static LIST_HEAD(ccs_execve_list); |
1036 |
|
static DEFINE_SPINLOCK(ccs_execve_list_lock); |
1037 |
|
|
1038 |
|
/** |
1039 |
|
* ccs_allocate_execve_entry - Allocate memory for execve(). |
1040 |
|
* |
1041 |
|
* Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise. |
1042 |
|
*/ |
1043 |
|
static struct ccs_execve_entry *ccs_allocate_execve_entry(void) |
1044 |
|
{ |
1045 |
|
struct ccs_execve_entry *ee = kzalloc(sizeof(*ee), GFP_KERNEL); |
1046 |
|
if (!ee) |
1047 |
|
return NULL; |
1048 |
|
ee->program_path = kzalloc(CCS_MAX_PATHNAME_LEN, GFP_KERNEL); |
1049 |
|
ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL); |
1050 |
|
if (!ee->program_path || !ee->tmp) { |
1051 |
|
kfree(ee->program_path); |
1052 |
|
kfree(ee->tmp); |
1053 |
|
kfree(ee); |
1054 |
|
return NULL; |
1055 |
|
} |
1056 |
|
ee->reader_idx = ccs_read_lock(); |
1057 |
|
/* ee->dump->data is allocated by ccs_dump_page(). */ |
1058 |
|
ee->task = current; |
1059 |
|
/***** CRITICAL SECTION START *****/ |
1060 |
|
spin_lock(&ccs_execve_list_lock); |
1061 |
|
list_add(&ee->list, &ccs_execve_list); |
1062 |
|
spin_unlock(&ccs_execve_list_lock); |
1063 |
|
/***** CRITICAL SECTION END *****/ |
1064 |
|
return ee; |
1065 |
|
} |
1066 |
|
|
1067 |
|
/** |
1068 |
|
* ccs_find_execve_entry - Find ccs_execve_entry of current process. |
1069 |
|
* |
1070 |
|
* Returns pointer to "struct ccs_execve_entry" on success, NULL otherwise. |
1071 |
|
*/ |
1072 |
|
static struct ccs_execve_entry *ccs_find_execve_entry(void) |
1073 |
|
{ |
1074 |
|
struct task_struct *task = current; |
1075 |
|
struct ccs_execve_entry *ee = NULL; |
1076 |
|
struct ccs_execve_entry *p; |
1077 |
|
/***** CRITICAL SECTION START *****/ |
1078 |
|
spin_lock(&ccs_execve_list_lock); |
1079 |
|
list_for_each_entry(p, &ccs_execve_list, list) { |
1080 |
|
if (p->task != task) |
1081 |
|
continue; |
1082 |
|
ee = p; |
1083 |
|
break; |
1084 |
|
} |
1085 |
|
spin_unlock(&ccs_execve_list_lock); |
1086 |
|
/***** CRITICAL SECTION END *****/ |
1087 |
|
return ee; |
1088 |
|
} |
1089 |
|
|
1090 |
|
/** |
1091 |
|
* ccs_free_execve_entry - Free memory for execve(). |
1092 |
|
* |
1093 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
1094 |
|
*/ |
1095 |
|
static void ccs_free_execve_entry(struct ccs_execve_entry *ee) |
1096 |
|
{ |
1097 |
|
if (!ee) |
1098 |
|
return; |
1099 |
|
/***** CRITICAL SECTION START *****/ |
1100 |
|
spin_lock(&ccs_execve_list_lock); |
1101 |
|
list_del(&ee->list); |
1102 |
|
spin_unlock(&ccs_execve_list_lock); |
1103 |
|
/***** CRITICAL SECTION END *****/ |
1104 |
|
kfree(ee->program_path); |
1105 |
|
kfree(ee->tmp); |
1106 |
|
kfree(ee->dump.data); |
1107 |
|
ccs_read_unlock(ee->reader_idx); |
1108 |
|
kfree(ee); |
1109 |
|
} |
1110 |
|
|
1111 |
/** |
/** |
1112 |
* try_alt_exec - Try to start execute handler. |
* ccs_try_alt_exec - Try to start execute handler. |
1113 |
* |
* |
1114 |
* @bprm: Pointer to "struct linux_binprm". |
* @ee: Pointer to "struct ccs_execve_entry". |
|
* @handler: Pointer to the name of execute handler. |
|
|
* @eh_path: Pointer to pointer to the name of execute handler. |
|
|
* @next_domain: Pointer to pointer to "struct domain_info". |
|
|
* @tmp: Buffer for temporal use. |
|
1115 |
* |
* |
1116 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
1117 |
*/ |
*/ |
1118 |
static int try_alt_exec(struct linux_binprm *bprm, |
static int ccs_try_alt_exec(struct ccs_execve_entry *ee) |
|
const struct path_info *handler, char **eh_path, |
|
|
struct domain_info **next_domain, |
|
|
struct ccs_page_buffer *tmp) |
|
1119 |
{ |
{ |
1120 |
/* |
/* |
1121 |
* Contents of modified bprm. |
* Contents of modified bprm. |
1131 |
* modified bprm->argv[0] |
* modified bprm->argv[0] |
1132 |
* = the program's name specified by execute_handler |
* = the program's name specified by execute_handler |
1133 |
* modified bprm->argv[1] |
* modified bprm->argv[1] |
1134 |
* = current->domain_info->domainname->name |
* = ccs_current_domain()->domainname->name |
1135 |
* modified bprm->argv[2] |
* modified bprm->argv[2] |
1136 |
* = the current process's name |
* = the current process's name |
1137 |
* modified bprm->argv[3] |
* modified bprm->argv[3] |
1153 |
* modified bprm->argv[bprm->envc + bprm->argc + 6] |
* modified bprm->argv[bprm->envc + bprm->argc + 6] |
1154 |
* = original bprm->envp[bprm->envc - 1] |
* = original bprm->envp[bprm->envc - 1] |
1155 |
*/ |
*/ |
1156 |
|
struct linux_binprm *bprm = ee->bprm; |
1157 |
struct file *filp; |
struct file *filp; |
1158 |
int retval; |
int retval; |
1159 |
const int original_argc = bprm->argc; |
const int original_argc = bprm->argc; |
1160 |
const int original_envc = bprm->envc; |
const int original_envc = bprm->envc; |
1161 |
struct task_struct *task = current; |
struct task_struct *task = current; |
|
char *buffer = tmp->buffer; |
|
|
/* Allocate memory for execute handler's pathname. */ |
|
|
char *execute_handler = ccs_alloc(sizeof(struct ccs_page_buffer)); |
|
|
*eh_path = execute_handler; |
|
|
if (!execute_handler) |
|
|
return -ENOMEM; |
|
|
strncpy(execute_handler, handler->name, |
|
|
sizeof(struct ccs_page_buffer) - 1); |
|
|
unescape(execute_handler); |
|
1162 |
|
|
1163 |
/* Close the requested program's dentry. */ |
/* Close the requested program's dentry. */ |
1164 |
allow_write_access(bprm->file); |
allow_write_access(bprm->file); |
1165 |
fput(bprm->file); |
fput(bprm->file); |
1166 |
bprm->file = NULL; |
bprm->file = NULL; |
1167 |
|
|
1168 |
{ /* Adjust root directory for open_exec(). */ |
/* Invalidate page dump cache. */ |
1169 |
int depth = get_root_depth(); |
ee->dump.page = NULL; |
|
char *cp = execute_handler; |
|
|
if (!*cp || *cp != '/') |
|
|
return -ENOENT; |
|
|
while (depth) { |
|
|
cp = strchr(cp + 1, '/'); |
|
|
if (!cp) |
|
|
return -ENOENT; |
|
|
depth--; |
|
|
} |
|
|
memmove(execute_handler, cp, strlen(cp) + 1); |
|
|
} |
|
1170 |
|
|
1171 |
/* Move envp[] to argv[] */ |
/* Move envp[] to argv[] */ |
1172 |
bprm->argc += bprm->envc; |
bprm->argc += bprm->envc; |
1174 |
|
|
1175 |
/* Set argv[6] */ |
/* Set argv[6] */ |
1176 |
{ |
{ |
1177 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d", |
char *cp = ee->tmp; |
1178 |
original_envc); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc); |
1179 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &cp, bprm); |
1180 |
if (retval < 0) |
if (retval < 0) |
1181 |
goto out; |
goto out; |
1182 |
bprm->argc++; |
bprm->argc++; |
1184 |
|
|
1185 |
/* Set argv[5] */ |
/* Set argv[5] */ |
1186 |
{ |
{ |
1187 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d", |
char *cp = ee->tmp; |
1188 |
original_argc); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc); |
1189 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &cp, bprm); |
1190 |
if (retval < 0) |
if (retval < 0) |
1191 |
goto out; |
goto out; |
1192 |
bprm->argc++; |
bprm->argc++; |
1202 |
|
|
1203 |
/* Set argv[3] */ |
/* Set argv[3] */ |
1204 |
{ |
{ |
1205 |
const u32 tomoyo_flags = task->tomoyo_flags; |
char *cp = ee->tmp; |
1206 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, |
const u32 ccs_flags = task->ccs_flags; |
1207 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, |
1208 |
"pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d " |
"pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d " |
1209 |
"sgid=%d fsuid=%d fsgid=%d state[0]=%u " |
"sgid=%d fsuid=%d fsgid=%d state[0]=%u " |
1210 |
"state[1]=%u state[2]=%u", |
"state[1]=%u state[2]=%u", |
1211 |
task->pid, task->uid, task->gid, task->euid, |
(pid_t) sys_getpid(), current_uid(), current_gid(), |
1212 |
task->egid, task->suid, task->sgid, task->fsuid, |
current_euid(), current_egid(), current_suid(), |
1213 |
task->fsgid, (u8) (tomoyo_flags >> 24), |
current_sgid(), current_fsuid(), current_fsgid(), |
1214 |
(u8) (tomoyo_flags >> 16), (u8) (tomoyo_flags >> 8)); |
(u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16), |
1215 |
retval = copy_strings_kernel(1, &buffer, bprm); |
(u8) (ccs_flags >> 8)); |
1216 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1217 |
if (retval < 0) |
if (retval < 0) |
1218 |
goto out; |
goto out; |
1219 |
bprm->argc++; |
bprm->argc++; |
1224 |
char *exe = (char *) ccs_get_exe(); |
char *exe = (char *) ccs_get_exe(); |
1225 |
if (exe) { |
if (exe) { |
1226 |
retval = copy_strings_kernel(1, &exe, bprm); |
retval = copy_strings_kernel(1, &exe, bprm); |
1227 |
ccs_free(exe); |
kfree(exe); |
1228 |
} else { |
} else { |
1229 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, |
exe = ee->tmp; |
1230 |
"<unknown>"); |
strncpy(ee->tmp, "<unknown>", CCS_EXEC_TMPSIZE - 1); |
1231 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &exe, bprm); |
1232 |
} |
} |
1233 |
if (retval < 0) |
if (retval < 0) |
1234 |
goto out; |
goto out; |
1237 |
|
|
1238 |
/* Set argv[1] */ |
/* Set argv[1] */ |
1239 |
{ |
{ |
1240 |
strncpy(buffer, task->domain_info->domainname->name, |
char *cp = ee->tmp; |
1241 |
sizeof(struct ccs_page_buffer) - 1); |
strncpy(ee->tmp, ccs_current_domain()->domainname->name, |
1242 |
retval = copy_strings_kernel(1, &buffer, bprm); |
CCS_EXEC_TMPSIZE - 1); |
1243 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1244 |
if (retval < 0) |
if (retval < 0) |
1245 |
goto out; |
goto out; |
1246 |
bprm->argc++; |
bprm->argc++; |
1248 |
|
|
1249 |
/* Set argv[0] */ |
/* Set argv[0] */ |
1250 |
{ |
{ |
1251 |
retval = copy_strings_kernel(1, &execute_handler, bprm); |
int depth = ccs_get_root_depth(); |
1252 |
|
char *cp = ee->program_path; |
1253 |
|
strncpy(cp, ee->handler->name, CCS_MAX_PATHNAME_LEN - 1); |
1254 |
|
ccs_unescape(cp); |
1255 |
|
retval = -ENOENT; |
1256 |
|
if (!*cp || *cp != '/') |
1257 |
|
goto out; |
1258 |
|
/* Adjust root directory for open_exec(). */ |
1259 |
|
while (depth) { |
1260 |
|
cp = strchr(cp + 1, '/'); |
1261 |
|
if (!cp) |
1262 |
|
goto out; |
1263 |
|
depth--; |
1264 |
|
} |
1265 |
|
memmove(ee->program_path, cp, strlen(cp) + 1); |
1266 |
|
cp = ee->program_path; |
1267 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1268 |
if (retval < 0) |
if (retval < 0) |
1269 |
goto out; |
goto out; |
1270 |
bprm->argc++; |
bprm->argc++; |
1276 |
#endif |
#endif |
1277 |
|
|
1278 |
/* OK, now restart the process with execute handler program's dentry. */ |
/* OK, now restart the process with execute handler program's dentry. */ |
1279 |
filp = open_exec(execute_handler); |
filp = open_exec(ee->program_path); |
1280 |
if (IS_ERR(filp)) { |
if (IS_ERR(filp)) { |
1281 |
retval = PTR_ERR(filp); |
retval = PTR_ERR(filp); |
1282 |
goto out; |
goto out; |
1283 |
} |
} |
1284 |
bprm->file = filp; |
bprm->file = filp; |
1285 |
bprm->filename = execute_handler; |
bprm->filename = ee->program_path; |
1286 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
1287 |
bprm->interp = execute_handler; |
bprm->interp = bprm->filename; |
1288 |
#endif |
#endif |
1289 |
retval = prepare_binprm(bprm); |
retval = prepare_binprm(bprm); |
1290 |
if (retval < 0) |
if (retval < 0) |
1291 |
goto out; |
goto out; |
1292 |
task->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
{ |
1293 |
retval = find_next_domain(bprm, next_domain, handler, tmp); |
/* |
1294 |
task->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
* Backup ee->program_path because ccs_find_next_domain() will |
1295 |
|
* overwrite ee->program_path and ee->tmp. |
1296 |
|
*/ |
1297 |
|
const int len = strlen(ee->program_path) + 1; |
1298 |
|
char *cp = kzalloc(len, GFP_KERNEL); |
1299 |
|
if (!cp) { |
1300 |
|
retval = -ENOMEM; |
1301 |
|
goto out; |
1302 |
|
} |
1303 |
|
memmove(cp, ee->program_path, len); |
1304 |
|
bprm->filename = cp; |
1305 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
1306 |
|
bprm->interp = bprm->filename; |
1307 |
|
#endif |
1308 |
|
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1309 |
|
retval = ccs_find_next_domain(ee); |
1310 |
|
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1311 |
|
/* Restore ee->program_path for search_binary_handler(). */ |
1312 |
|
memmove(ee->program_path, cp, len); |
1313 |
|
bprm->filename = ee->program_path; |
1314 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
1315 |
|
bprm->interp = bprm->filename; |
1316 |
|
#endif |
1317 |
|
kfree(cp); |
1318 |
|
} |
1319 |
out: |
out: |
1320 |
return retval; |
return retval; |
1321 |
} |
} |
1322 |
|
|
1323 |
/** |
/** |
1324 |
* find_execute_handler - Find an execute handler. |
* ccs_find_execute_handler - Find an execute handler. |
1325 |
* |
* |
1326 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
1327 |
* @type: Type of execute handler. |
* @type: Type of execute handler. |
1328 |
* |
* |
1329 |
* Returns pointer to "struct path_info" if found, NULL otherwise. |
* Returns true if found, false otherwise. |
1330 |
|
* |
1331 |
|
* Caller holds ccs_read_lock(). |
1332 |
*/ |
*/ |
1333 |
static const struct path_info *find_execute_handler(const u8 type) |
static bool ccs_find_execute_handler(struct ccs_execve_entry *ee, |
1334 |
|
const u8 type) |
1335 |
{ |
{ |
1336 |
struct task_struct *task = current; |
struct task_struct *task = current; |
1337 |
const struct domain_info *domain = task->domain_info; |
const struct ccs_domain_info *domain = ccs_current_domain(); |
1338 |
struct acl_info *ptr; |
struct ccs_acl_info *ptr; |
1339 |
|
bool found = false; |
1340 |
|
ccs_check_read_lock(); |
1341 |
/* |
/* |
1342 |
* Don't use execute handler if the current process is |
* Don't use execute handler if the current process is |
1343 |
* marked as execute handler to avoid infinite execute handler loop. |
* marked as execute handler to avoid infinite execute handler loop. |
1344 |
*/ |
*/ |
1345 |
if (task->tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER) |
if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) |
1346 |
return NULL; |
return false; |
1347 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
list_for_each_entry(ptr, &domain->acl_info_list, list) { |
1348 |
struct execute_handler_record *acl; |
struct ccs_execute_handler_record *acl; |
1349 |
if (ptr->type != type) |
if (ptr->type != type) |
1350 |
continue; |
continue; |
1351 |
acl = container_of(ptr, struct execute_handler_record, head); |
acl = container_of(ptr, struct ccs_execute_handler_record, |
1352 |
return acl->handler; |
head); |
1353 |
|
ee->handler = acl->handler; |
1354 |
|
found = true; |
1355 |
|
break; |
1356 |
} |
} |
1357 |
return NULL; |
return found; |
1358 |
} |
} |
1359 |
|
|
1360 |
/** |
/** |
1361 |
* search_binary_handler_with_transition - Perform domain transition. |
* ccs_dump_page - Dump a page to buffer. |
1362 |
* |
* |
1363 |
* @bprm: Pointer to "struct linux_binprm". |
* @bprm: Pointer to "struct linux_binprm". |
1364 |
* @regs: Pointer to "struct pt_regs". |
* @pos: Location to dump. |
1365 |
|
* @dump: Poiner to "struct ccs_page_dump". |
1366 |
* |
* |
1367 |
* Returns result of search_binary_handler() on success, |
* Returns true on success, false otherwise. |
|
* negative value otherwise. |
|
1368 |
*/ |
*/ |
1369 |
int search_binary_handler_with_transition(struct linux_binprm *bprm, |
bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, |
1370 |
struct pt_regs *regs) |
struct ccs_page_dump *dump) |
1371 |
|
{ |
1372 |
|
struct page *page; |
1373 |
|
/* dump->data is released by ccs_free_execve_entry(). */ |
1374 |
|
if (!dump->data) { |
1375 |
|
dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL); |
1376 |
|
if (!dump->data) |
1377 |
|
return false; |
1378 |
|
} |
1379 |
|
/* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */ |
1380 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1381 |
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1382 |
|
return false; |
1383 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU) |
1384 |
|
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1385 |
|
return false; |
1386 |
|
#else |
1387 |
|
page = bprm->page[pos / PAGE_SIZE]; |
1388 |
|
#endif |
1389 |
|
if (page != dump->page) { |
1390 |
|
const unsigned int offset = pos % PAGE_SIZE; |
1391 |
|
/* |
1392 |
|
* Maybe kmap()/kunmap() should be used here. |
1393 |
|
* But remove_arg_zero() uses kmap_atomic()/kunmap_atomic(). |
1394 |
|
* So do I. |
1395 |
|
*/ |
1396 |
|
char *kaddr = kmap_atomic(page, KM_USER0); |
1397 |
|
dump->page = page; |
1398 |
|
memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset); |
1399 |
|
kunmap_atomic(kaddr, KM_USER0); |
1400 |
|
} |
1401 |
|
/* Same with put_arg_page(page) in fs/exec.c */ |
1402 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1403 |
|
put_page(page); |
1404 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR == 3 && defined(CONFIG_MMU) |
1405 |
|
put_page(page); |
1406 |
|
#endif |
1407 |
|
return true; |
1408 |
|
} |
1409 |
|
|
1410 |
|
/** |
1411 |
|
* ccs_fetch_next_domain - Fetch next_domain from the list. |
1412 |
|
* |
1413 |
|
* Returns pointer to "struct ccs_domain_info" which will be used if execve() |
1414 |
|
* succeeds. This function does not return NULL. |
1415 |
|
*/ |
1416 |
|
struct ccs_domain_info *ccs_fetch_next_domain(void) |
1417 |
|
{ |
1418 |
|
struct ccs_execve_entry *ee = ccs_find_execve_entry(); |
1419 |
|
struct ccs_domain_info *next_domain = NULL; |
1420 |
|
if (ee) |
1421 |
|
next_domain = ee->r.domain; |
1422 |
|
if (!next_domain) |
1423 |
|
next_domain = ccs_current_domain(); |
1424 |
|
return next_domain; |
1425 |
|
} |
1426 |
|
|
1427 |
|
/** |
1428 |
|
* ccs_start_execve - Prepare for execve() operation. |
1429 |
|
* |
1430 |
|
* @bprm: Pointer to "struct linux_binprm". |
1431 |
|
* |
1432 |
|
* Returns 0 on success, negative value otherwise. |
1433 |
|
*/ |
1434 |
|
int ccs_start_execve(struct linux_binprm *bprm) |
1435 |
{ |
{ |
|
struct task_struct *task = current; |
|
|
struct domain_info *next_domain = NULL; |
|
|
struct domain_info *prev_domain = task->domain_info; |
|
|
const struct path_info *handler; |
|
1436 |
int retval; |
int retval; |
1437 |
/* |
struct task_struct *task = current; |
1438 |
* "eh_path" holds path to execute handler program. |
struct ccs_execve_entry *ee = ccs_allocate_execve_entry(); |
1439 |
* Thus, keep valid until search_binary_handler() finishes. |
if (!ccs_policy_loaded) |
1440 |
*/ |
ccs_load_policy(bprm->filename); |
1441 |
char *eh_path = NULL; |
if (!ee) |
|
struct ccs_page_buffer *tmp = ccs_alloc(sizeof(struct ccs_page_buffer)); |
|
|
ccs_load_policy(bprm->filename); |
|
|
if (!tmp) |
|
1442 |
return -ENOMEM; |
return -ENOMEM; |
1443 |
/* printk(KERN_DEBUG "rootdepth=%d\n", get_root_depth()); */ |
ccs_init_request_info(&ee->r, NULL, CCS_MAC_FOR_FILE); |
1444 |
handler = find_execute_handler(TYPE_EXECUTE_HANDLER); |
ee->r.ee = ee; |
1445 |
if (handler) { |
ee->bprm = bprm; |
1446 |
retval = try_alt_exec(bprm, handler, &eh_path, &next_domain, |
ee->r.obj = &ee->obj; |
1447 |
tmp); |
ee->obj.path1_dentry = bprm->file->f_dentry; |
1448 |
|
ee->obj.path1_vfsmnt = bprm->file->f_vfsmnt; |
1449 |
|
/* Clear manager flag. */ |
1450 |
|
task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER; |
1451 |
|
if (ccs_find_execute_handler(ee, TYPE_EXECUTE_HANDLER)) { |
1452 |
|
retval = ccs_try_alt_exec(ee); |
1453 |
if (!retval) |
if (!retval) |
1454 |
audit_execute_handler_log(true, handler->name, bprm); |
ccs_audit_execute_handler_log(ee, true); |
1455 |
goto ok; |
goto ok; |
1456 |
} |
} |
1457 |
retval = find_next_domain(bprm, &next_domain, NULL, tmp); |
retval = ccs_find_next_domain(ee); |
1458 |
if (retval != -EPERM) |
if (retval != -EPERM) |
1459 |
goto ok; |
goto ok; |
1460 |
handler = find_execute_handler(TYPE_DENIED_EXECUTE_HANDLER); |
if (ccs_find_execute_handler(ee, TYPE_DENIED_EXECUTE_HANDLER)) { |
1461 |
if (handler) { |
retval = ccs_try_alt_exec(ee); |
|
retval = try_alt_exec(bprm, handler, &eh_path, &next_domain, |
|
|
tmp); |
|
1462 |
if (!retval) |
if (!retval) |
1463 |
audit_execute_handler_log(false, handler->name, bprm); |
ccs_audit_execute_handler_log(ee, false); |
1464 |
} |
} |
1465 |
ok: |
ok: |
1466 |
if (retval) |
if (retval < 0) |
1467 |
goto out; |
goto out; |
1468 |
task->domain_info = next_domain; |
ee->r.mode = ccs_check_flags(ee->r.domain, CCS_MAC_FOR_ENV); |
1469 |
retval = check_environ(bprm, tmp); |
retval = ccs_check_environ(ee); |
1470 |
if (retval) |
if (retval < 0) |
1471 |
goto out; |
goto out; |
1472 |
task->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
task->ccs_flags |= CCS_CHECK_READ_FOR_OPEN_EXEC; |
1473 |
retval = search_binary_handler(bprm, regs); |
retval = 0; |
|
task->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
|
1474 |
out: |
out: |
1475 |
/* Return to previous domain if execution failed. */ |
if (retval) |
1476 |
if (retval < 0) |
ccs_finish_execve(retval); |
|
task->domain_info = prev_domain; |
|
|
/* Mark the current process as execute handler. */ |
|
|
else if (handler) |
|
|
task->tomoyo_flags |= TOMOYO_TASK_IS_EXECUTE_HANDLER; |
|
|
/* Mark the current process as normal process. */ |
|
|
else |
|
|
task->tomoyo_flags &= ~TOMOYO_TASK_IS_EXECUTE_HANDLER; |
|
|
ccs_free(eh_path); |
|
|
ccs_free(tmp); |
|
1477 |
return retval; |
return retval; |
1478 |
} |
} |
1479 |
|
|
|
#else |
|
|
|
|
1480 |
/** |
/** |
1481 |
* search_binary_handler_with_transition - Wrapper for search_binary_handler(). |
* ccs_finish_execve - Clean up execve() operation. |
1482 |
* |
* |
1483 |
* @bprm: Pointer to "struct linux_binprm". |
* @retval: Return code of an execve() operation. |
|
* @regs: Pointer to "struct pt_regs". |
|
1484 |
* |
* |
1485 |
* Returns the result of search_binary_handler(). |
* Caller holds ccs_read_lock(). |
1486 |
*/ |
*/ |
1487 |
int search_binary_handler_with_transition(struct linux_binprm *bprm, |
void ccs_finish_execve(int retval) |
|
struct pt_regs *regs) |
|
1488 |
{ |
{ |
1489 |
#ifdef CONFIG_SAKURA |
struct task_struct *task = current; |
1490 |
ccs_load_policy(bprm->filename); |
struct ccs_execve_entry *ee = ccs_find_execve_entry(); |
1491 |
#endif |
ccs_check_read_lock(); |
1492 |
return search_binary_handler(bprm, regs); |
task->ccs_flags &= ~CCS_CHECK_READ_FOR_OPEN_EXEC; |
1493 |
|
if (!ee) |
1494 |
|
return; |
1495 |
|
if (retval < 0) |
1496 |
|
goto out; |
1497 |
|
/* Proceed to next domain if execution suceeded. */ |
1498 |
|
task->ccs_domain_info = ee->r.domain; |
1499 |
|
/* Mark the current process as execute handler. */ |
1500 |
|
if (ee->handler) |
1501 |
|
task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER; |
1502 |
|
/* Mark the current process as normal process. */ |
1503 |
|
else |
1504 |
|
task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER; |
1505 |
|
out: |
1506 |
|
ccs_free_execve_entry(ee); |
1507 |
} |
} |
|
|
|
|
#endif |
|