1 |
/* |
/* |
2 |
* fs/tomoyo_domain.c |
* security/ccsecurity/domain.c |
3 |
* |
* |
4 |
* Implementation of the Domain-Based Mandatory Access Control. |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
5 |
* |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
* Version: 1.7.0 2009/10/01 |
|
* |
|
|
* Version: 1.6.5-pre 2008/10/11 |
|
7 |
* |
* |
8 |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
* This file is applicable to both 2.4.30 and 2.6.11 and later. |
9 |
* See README.ccs for ChangeLog. |
* See README.ccs for ChangeLog. |
10 |
* |
* |
11 |
*/ |
*/ |
12 |
|
|
13 |
#include <linux/ccs_common.h> |
#include <linux/slab.h> |
|
#include <linux/tomoyo.h> |
|
|
#include <linux/realpath.h> |
|
14 |
#include <linux/highmem.h> |
#include <linux/highmem.h> |
15 |
#include <linux/binfmts.h> |
#include <linux/version.h> |
16 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
17 |
#include <linux/namei.h> |
#include <linux/namei.h> |
18 |
#include <linux/mount.h> |
#include <linux/mount.h> |
19 |
#endif |
#endif |
20 |
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) |
21 |
/* For compatibility with older kernels. */ |
#include <linux/fs_struct.h> |
|
#ifndef for_each_process |
|
|
#define for_each_process for_each_task |
|
22 |
#endif |
#endif |
23 |
|
#include "internal.h" |
24 |
|
|
25 |
/* Variables definitions.*/ |
/* Variables definitions.*/ |
26 |
|
|
27 |
/* The initial domain. */ |
/* The initial domain. */ |
28 |
struct domain_info KERNEL_DOMAIN; |
struct ccs_domain_info ccs_kernel_domain; |
|
|
|
|
/* The list for "struct domain_info". */ |
|
|
LIST1_HEAD(domain_list); |
|
|
|
|
|
#ifdef CONFIG_TOMOYO |
|
|
|
|
|
/* Domain creation lock. */ |
|
|
static DEFINE_MUTEX(domain_list_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) |
|
|
{ |
|
|
/* We need to serialize because this is bitfield operation. */ |
|
|
static DEFINE_SPINLOCK(lock); |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
spin_lock(&lock); |
|
|
if (!is_delete) |
|
|
domain->flags |= flags; |
|
|
else |
|
|
domain->flags &= ~flags; |
|
|
spin_unlock(&lock); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_get_last_name - Get last component of a domainname. |
|
|
* |
|
|
* @domain: Pointer to "struct domain_info". |
|
|
* |
|
|
* Returns the last component of the domainname. |
|
|
*/ |
|
|
const char *ccs_get_last_name(const struct domain_info *domain) |
|
|
{ |
|
|
const char *cp0 = domain->domainname->name; |
|
|
const char *cp1 = strrchr(cp0, ' '); |
|
|
if (cp1) |
|
|
return cp1 + 1; |
|
|
return cp0; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_add_domain_acl - Add the given ACL to the given domain. |
|
|
* |
|
|
* @domain: Pointer to "struct domain_info". May be NULL. |
|
|
* @acl: Pointer to "struct acl_info". |
|
|
* |
|
|
* Returns 0. |
|
|
*/ |
|
|
int ccs_add_domain_acl(struct domain_info *domain, struct acl_info *acl) |
|
|
{ |
|
|
if (domain) { |
|
|
/* |
|
|
* We need to serialize because this function is called by |
|
|
* various update functions. |
|
|
*/ |
|
|
static DEFINE_SPINLOCK(lock); |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
spin_lock(&lock); |
|
|
list1_add_tail_mb(&acl->list, &domain->acl_info_list); |
|
|
spin_unlock(&lock); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
} else { |
|
|
acl->type &= ~ACL_DELETED; |
|
|
} |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
|
|
return 0; |
|
|
} |
|
29 |
|
|
30 |
/** |
/* The list for "struct ccs_domain_info". */ |
31 |
* ccs_del_domain_acl - Delete the given ACL from the domain. |
LIST_HEAD(ccs_domain_list); |
|
* |
|
|
* @acl: Pointer to "struct acl_info". May be NULL. |
|
|
* |
|
|
* Returns 0. |
|
|
*/ |
|
|
int ccs_del_domain_acl(struct acl_info *acl) |
|
|
{ |
|
|
if (acl) |
|
|
acl->type |= ACL_DELETED; |
|
|
ccs_update_counter(CCS_UPDATES_COUNTER_DOMAIN_POLICY); |
|
|
return 0; |
|
|
} |
|
32 |
|
|
33 |
/** |
/** |
34 |
* audit_execute_handler_log - Audit execute_handler log. |
* ccs_audit_execute_handler_log - Audit execute_handler log. |
35 |
* |
* |
36 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
37 |
* @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". |
|
38 |
* |
* |
39 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
40 |
*/ |
*/ |
41 |
static int audit_execute_handler_log(const bool is_default, |
static int ccs_audit_execute_handler_log(struct ccs_execve_entry *ee, |
42 |
const char *handler, |
const bool is_default) |
|
struct linux_binprm *bprm) |
|
43 |
{ |
{ |
44 |
struct ccs_request_info r; |
struct ccs_request_info *r = &ee->r; |
45 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_FILE); |
const char *handler = ee->handler->name; |
46 |
r.bprm = bprm; |
r->mode = ccs_get_mode(r->profile, CCS_MAC_FILE_EXECUTE); |
47 |
return ccs_write_audit_log(true, &r, "%s %s\n", |
return ccs_write_audit_log(true, r, "%s %s\n", |
48 |
is_default ? KEYWORD_EXECUTE_HANDLER : |
is_default ? CCS_KEYWORD_EXECUTE_HANDLER : |
49 |
KEYWORD_DENIED_EXECUTE_HANDLER, handler); |
CCS_KEYWORD_DENIED_EXECUTE_HANDLER, handler); |
50 |
} |
} |
51 |
|
|
52 |
/** |
/** |
53 |
* audit_domain_creation_log - Audit domain creation log. |
* ccs_audit_domain_creation_log - Audit domain creation log. |
54 |
* |
* |
55 |
* @domain: Pointer to "struct domain_info". |
* @domain: Pointer to "struct ccs_domain_info". |
56 |
* |
* |
57 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
58 |
*/ |
*/ |
59 |
static int audit_domain_creation_log(struct domain_info *domain) |
static int ccs_audit_domain_creation_log(struct ccs_domain_info *domain) |
60 |
{ |
{ |
61 |
|
int error; |
62 |
struct ccs_request_info r; |
struct ccs_request_info r; |
63 |
ccs_init_request_info(&r, domain, CCS_TOMOYO_MAC_FOR_FILE); |
ccs_init_request_info(&r, domain, CCS_MAC_FILE_EXECUTE); |
64 |
return ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile); |
error = ccs_write_audit_log(false, &r, "use_profile %u\n", r.profile); |
65 |
|
return error; |
66 |
} |
} |
67 |
|
|
68 |
/* The list for "struct domain_initializer_entry". */ |
/* The list for "struct ccs_domain_initializer_entry". */ |
69 |
static LIST1_HEAD(domain_initializer_list); |
LIST_HEAD(ccs_domain_initializer_list); |
70 |
|
|
71 |
/** |
/** |
72 |
* update_domain_initializer_entry - Update "struct domain_initializer_entry" list. |
* ccs_update_domain_initializer_entry - Update "struct ccs_domain_initializer_entry" list. |
73 |
* |
* |
74 |
* @domainname: The name of domain. May be NULL. |
* @domainname: The name of domain. May be NULL. |
75 |
* @program: The name of program. |
* @program: The name of program. |
78 |
* |
* |
79 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
80 |
*/ |
*/ |
81 |
static int update_domain_initializer_entry(const char *domainname, |
static int ccs_update_domain_initializer_entry(const char *domainname, |
82 |
const char *program, |
const char *program, |
83 |
const bool is_not, |
const bool is_not, |
84 |
const bool is_delete) |
const bool is_delete) |
85 |
{ |
{ |
86 |
struct domain_initializer_entry *new_entry; |
struct ccs_domain_initializer_entry *entry = NULL; |
87 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
88 |
static DEFINE_MUTEX(lock); |
struct ccs_domain_initializer_entry e = { .is_not = is_not }; |
89 |
const struct path_info *saved_program; |
int error = is_delete ? -ENOENT : -ENOMEM; |
90 |
const struct path_info *saved_domainname = NULL; |
if (!ccs_is_correct_path(program, 1, -1, -1)) |
|
int error = -ENOMEM; |
|
|
bool is_last_name = false; |
|
|
if (!ccs_is_correct_path(program, 1, -1, -1, __func__)) |
|
91 |
return -EINVAL; /* No patterns allowed. */ |
return -EINVAL; /* No patterns allowed. */ |
92 |
if (domainname) { |
if (domainname) { |
93 |
if (!ccs_is_domain_def(domainname) && |
if (!ccs_is_domain_def(domainname) && |
94 |
ccs_is_correct_path(domainname, 1, -1, -1, __func__)) |
ccs_is_correct_path(domainname, 1, -1, -1)) |
95 |
is_last_name = true; |
e.is_last_name = true; |
96 |
else if (!ccs_is_correct_domain(domainname, __func__)) |
else if (!ccs_is_correct_domain(domainname)) |
97 |
return -EINVAL; |
return -EINVAL; |
98 |
saved_domainname = ccs_save_name(domainname); |
e.domainname = ccs_get_name(domainname); |
99 |
if (!saved_domainname) |
if (!e.domainname) |
100 |
return -ENOMEM; |
goto out; |
101 |
} |
} |
102 |
saved_program = ccs_save_name(program); |
e.program = ccs_get_name(program); |
103 |
if (!saved_program) |
if (!e.program) |
104 |
return -ENOMEM; |
goto out; |
105 |
mutex_lock(&lock); |
if (!is_delete) |
106 |
list1_for_each_entry(ptr, &domain_initializer_list, list) { |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
107 |
if (ptr->is_not != is_not || |
mutex_lock(&ccs_policy_lock); |
108 |
ptr->domainname != saved_domainname || |
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
109 |
ptr->program != saved_program) |
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not), |
110 |
|
sizeof(e))) |
111 |
continue; |
continue; |
112 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
113 |
error = 0; |
error = 0; |
114 |
goto out; |
break; |
115 |
} |
} |
116 |
if (is_delete) { |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
117 |
error = -ENOENT; |
list_add_tail_rcu(&entry->list, &ccs_domain_initializer_list); |
118 |
goto out; |
entry = NULL; |
119 |
|
error = 0; |
120 |
} |
} |
121 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
|
if (!new_entry) |
|
|
goto out; |
|
|
new_entry->domainname = saved_domainname; |
|
|
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; |
|
122 |
out: |
out: |
123 |
mutex_unlock(&lock); |
ccs_put_name(e.domainname); |
124 |
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
ccs_put_name(e.program); |
125 |
|
kfree(entry); |
126 |
return error; |
return error; |
127 |
} |
} |
128 |
|
|
129 |
/** |
/** |
130 |
* ccs_read_domain_initializer_policy - Read "struct domain_initializer_entry" list. |
* ccs_read_domain_initializer_policy - Read "struct ccs_domain_initializer_entry" list. |
131 |
* |
* |
132 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
133 |
* |
* |
134 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
135 |
|
* |
136 |
|
* Caller holds ccs_read_lock(). |
137 |
*/ |
*/ |
138 |
bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head) |
bool ccs_read_domain_initializer_policy(struct ccs_io_buffer *head) |
139 |
{ |
{ |
140 |
struct list1_head *pos; |
struct list_head *pos; |
141 |
list1_for_each_cookie(pos, head->read_var2, &domain_initializer_list) { |
bool done = true; |
142 |
|
list_for_each_cookie(pos, head->read_var2, |
143 |
|
&ccs_domain_initializer_list) { |
144 |
const char *no; |
const char *no; |
145 |
const char *from = ""; |
const char *from = ""; |
146 |
const char *domain = ""; |
const char *domain = ""; |
147 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
148 |
ptr = list1_entry(pos, struct domain_initializer_entry, list); |
ptr = list_entry(pos, struct ccs_domain_initializer_entry, |
149 |
|
list); |
150 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
151 |
continue; |
continue; |
152 |
no = ptr->is_not ? "no_" : ""; |
no = ptr->is_not ? "no_" : ""; |
154 |
from = " from "; |
from = " from "; |
155 |
domain = ptr->domainname->name; |
domain = ptr->domainname->name; |
156 |
} |
} |
157 |
if (!ccs_io_printf(head, |
done = ccs_io_printf(head, "%s" CCS_KEYWORD_INITIALIZE_DOMAIN |
158 |
"%s" KEYWORD_INITIALIZE_DOMAIN "%s%s%s\n", |
"%s%s%s\n", no, ptr->program->name, from, |
159 |
no, ptr->program->name, from, domain)) |
domain); |
160 |
goto out; |
if (!done) |
161 |
|
break; |
162 |
} |
} |
163 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
164 |
} |
} |
165 |
|
|
166 |
/** |
/** |
167 |
* ccs_write_domain_initializer_policy - Write "struct domain_initializer_entry" list. |
* ccs_write_domain_initializer_policy - Write "struct ccs_domain_initializer_entry" list. |
168 |
* |
* |
169 |
* @data: String to parse. |
* @data: String to parse. |
170 |
* @is_not: True if it is "no_initialize_domain" entry. |
* @is_not: True if it is "no_initialize_domain" entry. |
178 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
179 |
if (cp) { |
if (cp) { |
180 |
*cp = '\0'; |
*cp = '\0'; |
181 |
return update_domain_initializer_entry(cp + 6, data, is_not, |
return ccs_update_domain_initializer_entry(cp + 6, data, |
182 |
is_delete); |
is_not, is_delete); |
183 |
} |
} |
184 |
return update_domain_initializer_entry(NULL, data, is_not, is_delete); |
return ccs_update_domain_initializer_entry(NULL, data, is_not, |
185 |
|
is_delete); |
186 |
} |
} |
187 |
|
|
188 |
/** |
/** |
189 |
* is_domain_initializer - Check whether the given program causes domainname reinitialization. |
* ccs_is_domain_initializer - Check whether the given program causes domainname reinitialization. |
190 |
* |
* |
191 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
192 |
* @program: The name of program. |
* @program: The name of program. |
194 |
* |
* |
195 |
* Returns true if executing @program reinitializes domain transition, |
* Returns true if executing @program reinitializes domain transition, |
196 |
* false otherwise. |
* false otherwise. |
197 |
|
* |
198 |
|
* Caller holds ccs_read_lock(). |
199 |
*/ |
*/ |
200 |
static bool is_domain_initializer(const struct path_info *domainname, |
static bool ccs_is_domain_initializer(const struct ccs_path_info *domainname, |
201 |
const struct path_info *program, |
const struct ccs_path_info *program, |
202 |
const struct path_info *last_name) |
const struct ccs_path_info *last_name) |
203 |
{ |
{ |
204 |
struct domain_initializer_entry *ptr; |
struct ccs_domain_initializer_entry *ptr; |
205 |
bool flag = false; |
bool flag = false; |
206 |
list1_for_each_entry(ptr, &domain_initializer_list, list) { |
list_for_each_entry_rcu(ptr, &ccs_domain_initializer_list, list) { |
207 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
208 |
continue; |
continue; |
209 |
if (ptr->domainname) { |
if (ptr->domainname) { |
217 |
} |
} |
218 |
if (ccs_pathcmp(ptr->program, program)) |
if (ccs_pathcmp(ptr->program, program)) |
219 |
continue; |
continue; |
220 |
if (ptr->is_not) |
if (ptr->is_not) { |
221 |
return false; |
flag = false; |
222 |
|
break; |
223 |
|
} |
224 |
flag = true; |
flag = true; |
225 |
} |
} |
226 |
return flag; |
return flag; |
227 |
} |
} |
228 |
|
|
229 |
/* The list for "struct domain_keeper_entry". */ |
/* The list for "struct ccs_domain_keeper_entry". */ |
230 |
static LIST1_HEAD(domain_keeper_list); |
LIST_HEAD(ccs_domain_keeper_list); |
231 |
|
|
232 |
/** |
/** |
233 |
* update_domain_keeper_entry - Update "struct domain_keeper_entry" list. |
* ccs_update_domain_keeper_entry - Update "struct ccs_domain_keeper_entry" list. |
234 |
* |
* |
235 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
236 |
* @program: The name of program. May be NULL. |
* @program: The name of program. May be NULL. |
239 |
* |
* |
240 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
241 |
*/ |
*/ |
242 |
static int update_domain_keeper_entry(const char *domainname, |
static int ccs_update_domain_keeper_entry(const char *domainname, |
243 |
const char *program, |
const char *program, |
244 |
const bool is_not, const bool is_delete) |
const bool is_not, |
245 |
{ |
const bool is_delete) |
246 |
struct domain_keeper_entry *new_entry; |
{ |
247 |
struct domain_keeper_entry *ptr; |
struct ccs_domain_keeper_entry *entry = NULL; |
248 |
const struct path_info *saved_domainname; |
struct ccs_domain_keeper_entry *ptr; |
249 |
const struct path_info *saved_program = NULL; |
struct ccs_domain_keeper_entry e = { .is_not = is_not }; |
250 |
static DEFINE_MUTEX(lock); |
int error = is_delete ? -ENOENT : -ENOMEM; |
|
int error = -ENOMEM; |
|
|
bool is_last_name = false; |
|
251 |
if (!ccs_is_domain_def(domainname) && |
if (!ccs_is_domain_def(domainname) && |
252 |
ccs_is_correct_path(domainname, 1, -1, -1, __func__)) |
ccs_is_correct_path(domainname, 1, -1, -1)) |
253 |
is_last_name = true; |
e.is_last_name = true; |
254 |
else if (!ccs_is_correct_domain(domainname, __func__)) |
else if (!ccs_is_correct_domain(domainname)) |
255 |
return -EINVAL; |
return -EINVAL; |
256 |
if (program) { |
if (program) { |
257 |
if (!ccs_is_correct_path(program, 1, -1, -1, __func__)) |
if (!ccs_is_correct_path(program, 1, -1, -1)) |
258 |
return -EINVAL; |
return -EINVAL; |
259 |
saved_program = ccs_save_name(program); |
e.program = ccs_get_name(program); |
260 |
if (!saved_program) |
if (!e.program) |
261 |
return -ENOMEM; |
goto out; |
262 |
} |
} |
263 |
saved_domainname = ccs_save_name(domainname); |
e.domainname = ccs_get_name(domainname); |
264 |
if (!saved_domainname) |
if (!e.domainname) |
265 |
return -ENOMEM; |
goto out; |
266 |
mutex_lock(&lock); |
if (!is_delete) |
267 |
list1_for_each_entry(ptr, &domain_keeper_list, list) { |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
268 |
if (ptr->is_not != is_not || |
mutex_lock(&ccs_policy_lock); |
269 |
ptr->domainname != saved_domainname || |
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
270 |
ptr->program != saved_program) |
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), is_not), |
271 |
|
sizeof(e))) |
272 |
continue; |
continue; |
273 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
274 |
error = 0; |
error = 0; |
275 |
goto out; |
break; |
276 |
} |
} |
277 |
if (is_delete) { |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
278 |
error = -ENOENT; |
list_add_tail_rcu(&entry->list, &ccs_domain_keeper_list); |
279 |
goto out; |
entry = NULL; |
280 |
|
error = 0; |
281 |
} |
} |
282 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
|
if (!new_entry) |
|
|
goto out; |
|
|
new_entry->domainname = saved_domainname; |
|
|
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; |
|
283 |
out: |
out: |
284 |
mutex_unlock(&lock); |
ccs_put_name(e.domainname); |
285 |
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
ccs_put_name(e.program); |
286 |
|
kfree(entry); |
287 |
return error; |
return error; |
288 |
} |
} |
289 |
|
|
290 |
/** |
/** |
291 |
* ccs_write_domain_keeper_policy - Write "struct domain_keeper_entry" list. |
* ccs_write_domain_keeper_policy - Write "struct ccs_domain_keeper_entry" list. |
292 |
* |
* |
293 |
* @data: String to parse. |
* @data: String to parse. |
294 |
* @is_not: True if it is "no_keep_domain" entry. |
* @is_not: True if it is "no_keep_domain" entry. |
301 |
char *cp = strstr(data, " from "); |
char *cp = strstr(data, " from "); |
302 |
if (cp) { |
if (cp) { |
303 |
*cp = '\0'; |
*cp = '\0'; |
304 |
return update_domain_keeper_entry(cp + 6, data, |
return ccs_update_domain_keeper_entry(cp + 6, data, |
305 |
is_not, is_delete); |
is_not, is_delete); |
306 |
} |
} |
307 |
return update_domain_keeper_entry(data, NULL, is_not, is_delete); |
return ccs_update_domain_keeper_entry(data, NULL, is_not, is_delete); |
308 |
} |
} |
309 |
|
|
310 |
/** |
/** |
311 |
* ccs_read_domain_keeper_policy - Read "struct domain_keeper_entry" list. |
* ccs_read_domain_keeper_policy - Read "struct ccs_domain_keeper_entry" list. |
312 |
* |
* |
313 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
314 |
* |
* |
315 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
316 |
|
* |
317 |
|
* Caller holds ccs_read_lock(). |
318 |
*/ |
*/ |
319 |
bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head) |
bool ccs_read_domain_keeper_policy(struct ccs_io_buffer *head) |
320 |
{ |
{ |
321 |
struct list1_head *pos; |
struct list_head *pos; |
322 |
list1_for_each_cookie(pos, head->read_var2, &domain_keeper_list) { |
bool done = true; |
323 |
struct domain_keeper_entry *ptr; |
list_for_each_cookie(pos, head->read_var2, |
324 |
|
&ccs_domain_keeper_list) { |
325 |
|
struct ccs_domain_keeper_entry *ptr; |
326 |
const char *no; |
const char *no; |
327 |
const char *from = ""; |
const char *from = ""; |
328 |
const char *program = ""; |
const char *program = ""; |
329 |
ptr = list1_entry(pos, struct domain_keeper_entry, list); |
ptr = list_entry(pos, struct ccs_domain_keeper_entry, list); |
330 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
331 |
continue; |
continue; |
332 |
no = ptr->is_not ? "no_" : ""; |
no = ptr->is_not ? "no_" : ""; |
334 |
from = " from "; |
from = " from "; |
335 |
program = ptr->program->name; |
program = ptr->program->name; |
336 |
} |
} |
337 |
if (!ccs_io_printf(head, |
done = ccs_io_printf(head, "%s" CCS_KEYWORD_KEEP_DOMAIN |
338 |
"%s" KEYWORD_KEEP_DOMAIN "%s%s%s\n", no, |
"%s%s%s\n", no, program, from, |
339 |
program, from, ptr->domainname->name)) |
ptr->domainname->name); |
340 |
goto out; |
if (!done) |
341 |
|
break; |
342 |
} |
} |
343 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
344 |
} |
} |
345 |
|
|
346 |
/** |
/** |
347 |
* 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. |
348 |
* |
* |
349 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
350 |
* @program: The name of program. |
* @program: The name of program. |
352 |
* |
* |
353 |
* Returns true if executing @program supresses domain transition, |
* Returns true if executing @program supresses domain transition, |
354 |
* false otherwise. |
* false otherwise. |
355 |
|
* |
356 |
|
* Caller holds ccs_read_lock(). |
357 |
*/ |
*/ |
358 |
static bool is_domain_keeper(const struct path_info *domainname, |
static bool ccs_is_domain_keeper(const struct ccs_path_info *domainname, |
359 |
const struct path_info *program, |
const struct ccs_path_info *program, |
360 |
const struct path_info *last_name) |
const struct ccs_path_info *last_name) |
361 |
{ |
{ |
362 |
struct domain_keeper_entry *ptr; |
struct ccs_domain_keeper_entry *ptr; |
363 |
bool flag = false; |
bool flag = false; |
364 |
list1_for_each_entry(ptr, &domain_keeper_list, list) { |
list_for_each_entry_rcu(ptr, &ccs_domain_keeper_list, list) { |
365 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
366 |
continue; |
continue; |
367 |
if (!ptr->is_last_name) { |
if (!ptr->is_last_name) { |
373 |
} |
} |
374 |
if (ptr->program && ccs_pathcmp(ptr->program, program)) |
if (ptr->program && ccs_pathcmp(ptr->program, program)) |
375 |
continue; |
continue; |
376 |
if (ptr->is_not) |
if (ptr->is_not) { |
377 |
return false; |
flag = false; |
378 |
|
break; |
379 |
|
} |
380 |
flag = true; |
flag = true; |
381 |
} |
} |
382 |
return flag; |
return flag; |
383 |
} |
} |
384 |
|
|
385 |
/* The list for "struct alias_entry". */ |
/* The list for "struct ccs_aggregator_entry". */ |
386 |
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; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_write_alias_policy - Write "struct alias_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); |
|
387 |
|
|
388 |
/** |
/** |
389 |
* update_aggregator_entry - Update "struct aggregator_entry" list. |
* ccs_update_aggregator_entry - Update "struct ccs_aggregator_entry" list. |
390 |
* |
* |
391 |
* @original_name: The original program's name. |
* @original_name: The original program's name. |
392 |
* @aggregated_name: The aggregated program's name. |
* @aggregated_name: The aggregated program's name. |
394 |
* |
* |
395 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
396 |
*/ |
*/ |
397 |
static int update_aggregator_entry(const char *original_name, |
static int ccs_update_aggregator_entry(const char *original_name, |
398 |
const char *aggregated_name, |
const char *aggregated_name, |
399 |
const bool is_delete) |
const bool is_delete) |
400 |
{ |
{ |
401 |
struct aggregator_entry *new_entry; |
struct ccs_aggregator_entry *entry = NULL; |
402 |
struct aggregator_entry *ptr; |
struct ccs_aggregator_entry *ptr; |
403 |
static DEFINE_MUTEX(lock); |
struct ccs_aggregator_entry e = { }; |
404 |
const struct path_info *saved_original_name; |
int error = is_delete ? -ENOENT : -ENOMEM; |
405 |
const struct path_info *saved_aggregated_name; |
if (!ccs_is_correct_path(original_name, 1, 0, -1) || |
406 |
int error = -ENOMEM; |
!ccs_is_correct_path(aggregated_name, 1, -1, -1)) |
|
if (!ccs_is_correct_path(original_name, 1, 0, -1, __func__) || |
|
|
!ccs_is_correct_path(aggregated_name, 1, -1, -1, __func__)) |
|
407 |
return -EINVAL; |
return -EINVAL; |
408 |
saved_original_name = ccs_save_name(original_name); |
e.original_name = ccs_get_name(original_name); |
409 |
saved_aggregated_name = ccs_save_name(aggregated_name); |
e.aggregated_name = ccs_get_name(aggregated_name); |
410 |
if (!saved_original_name || !saved_aggregated_name) |
if (!e.original_name || !e.aggregated_name) |
411 |
return -ENOMEM; |
goto out; |
412 |
mutex_lock(&lock); |
if (!is_delete) |
413 |
list1_for_each_entry(ptr, &aggregator_list, list) { |
entry = kmalloc(sizeof(e), GFP_KERNEL); |
414 |
if (ptr->original_name != saved_original_name || |
mutex_lock(&ccs_policy_lock); |
415 |
ptr->aggregated_name != saved_aggregated_name) |
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
416 |
|
if (ccs_memcmp(ptr, &e, offsetof(typeof(e), original_name), |
417 |
|
sizeof(e))) |
418 |
continue; |
continue; |
419 |
ptr->is_deleted = is_delete; |
ptr->is_deleted = is_delete; |
420 |
error = 0; |
error = 0; |
421 |
goto out; |
break; |
422 |
} |
} |
423 |
if (is_delete) { |
if (!is_delete && error && ccs_commit_ok(entry, &e, sizeof(e))) { |
424 |
error = -ENOENT; |
list_add_tail_rcu(&entry->list, &ccs_aggregator_list); |
425 |
goto out; |
entry = NULL; |
426 |
|
error = 0; |
427 |
} |
} |
428 |
new_entry = ccs_alloc_element(sizeof(*new_entry)); |
mutex_unlock(&ccs_policy_lock); |
|
if (!new_entry) |
|
|
goto out; |
|
|
new_entry->original_name = saved_original_name; |
|
|
new_entry->aggregated_name = saved_aggregated_name; |
|
|
list1_add_tail_mb(&new_entry->list, &aggregator_list); |
|
|
error = 0; |
|
429 |
out: |
out: |
430 |
mutex_unlock(&lock); |
ccs_put_name(e.original_name); |
431 |
ccs_update_counter(CCS_UPDATES_COUNTER_EXCEPTION_POLICY); |
ccs_put_name(e.aggregated_name); |
432 |
|
kfree(entry); |
433 |
return error; |
return error; |
434 |
} |
} |
435 |
|
|
436 |
/** |
/** |
437 |
* ccs_read_aggregator_policy - Read "struct aggregator_entry" list. |
* ccs_read_aggregator_policy - Read "struct ccs_aggregator_entry" list. |
438 |
* |
* |
439 |
* @head: Pointer to "struct ccs_io_buffer". |
* @head: Pointer to "struct ccs_io_buffer". |
440 |
* |
* |
441 |
* Returns true on success, false otherwise. |
* Returns true on success, false otherwise. |
442 |
|
* |
443 |
|
* Caller holds ccs_read_lock(). |
444 |
*/ |
*/ |
445 |
bool ccs_read_aggregator_policy(struct ccs_io_buffer *head) |
bool ccs_read_aggregator_policy(struct ccs_io_buffer *head) |
446 |
{ |
{ |
447 |
struct list1_head *pos; |
struct list_head *pos; |
448 |
list1_for_each_cookie(pos, head->read_var2, &aggregator_list) { |
bool done = true; |
449 |
struct aggregator_entry *ptr; |
list_for_each_cookie(pos, head->read_var2, &ccs_aggregator_list) { |
450 |
ptr = list1_entry(pos, struct aggregator_entry, list); |
struct ccs_aggregator_entry *ptr; |
451 |
|
ptr = list_entry(pos, struct ccs_aggregator_entry, list); |
452 |
if (ptr->is_deleted) |
if (ptr->is_deleted) |
453 |
continue; |
continue; |
454 |
if (!ccs_io_printf(head, KEYWORD_AGGREGATOR "%s %s\n", |
done = ccs_io_printf(head, CCS_KEYWORD_AGGREGATOR "%s %s\n", |
455 |
ptr->original_name->name, |
ptr->original_name->name, |
456 |
ptr->aggregated_name->name)) |
ptr->aggregated_name->name); |
457 |
goto out; |
if (!done) |
458 |
|
break; |
459 |
} |
} |
460 |
return true; |
return done; |
|
out: |
|
|
return false; |
|
461 |
} |
} |
462 |
|
|
463 |
/** |
/** |
464 |
* ccs_write_aggregator_policy - Write "struct aggregator_entry" list. |
* ccs_write_aggregator_policy - Write "struct ccs_aggregator_entry" list. |
465 |
* |
* |
466 |
* @data: String to parse. |
* @data: String to parse. |
467 |
* @is_delete: True if it is a delete request. |
* @is_delete: True if it is a delete request. |
470 |
*/ |
*/ |
471 |
int ccs_write_aggregator_policy(char *data, const bool is_delete) |
int ccs_write_aggregator_policy(char *data, const bool is_delete) |
472 |
{ |
{ |
473 |
char *cp = strchr(data, ' '); |
char *w[2]; |
474 |
if (!cp) |
if (!ccs_tokenize(data, w, sizeof(w)) || !w[1][0]) |
475 |
return -EINVAL; |
return -EINVAL; |
476 |
*cp++ = '\0'; |
return ccs_update_aggregator_entry(w[0], w[1], is_delete); |
|
return update_aggregator_entry(data, cp, is_delete); |
|
477 |
} |
} |
478 |
|
|
479 |
/* Domain create/delete/undelete handler. */ |
/* Domain create/delete handler. */ |
|
|
|
|
/* #define DEBUG_DOMAIN_UNDELETE */ |
|
480 |
|
|
481 |
/** |
/** |
482 |
* ccs_delete_domain - Delete a domain. |
* ccs_delete_domain - Delete a domain. |
487 |
*/ |
*/ |
488 |
int ccs_delete_domain(char *domainname) |
int ccs_delete_domain(char *domainname) |
489 |
{ |
{ |
490 |
struct domain_info *domain; |
struct ccs_domain_info *domain; |
491 |
struct path_info name; |
struct ccs_path_info name; |
492 |
name.name = domainname; |
name.name = domainname; |
493 |
ccs_fill_path_info(&name); |
ccs_fill_path_info(&name); |
494 |
mutex_lock(&domain_list_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 |
|
495 |
/* Is there an active domain? */ |
/* Is there an active domain? */ |
496 |
list1_for_each_entry(domain, &domain_list, list) { |
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
497 |
struct domain_info *domain2; |
/* Never delete ccs_kernel_domain */ |
498 |
/* Never delete KERNEL_DOMAIN */ |
if (domain == &ccs_kernel_domain) |
|
if (domain == &KERNEL_DOMAIN) |
|
499 |
continue; |
continue; |
500 |
if (domain->is_deleted || |
if (domain->is_deleted || |
501 |
ccs_pathcmp(domain->domainname, &name)) |
ccs_pathcmp(domain->domainname, &name)) |
502 |
continue; |
continue; |
503 |
/* 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 |
|
504 |
break; |
break; |
505 |
} |
} |
506 |
mutex_unlock(&domain_list_lock); |
mutex_unlock(&ccs_policy_lock); |
507 |
return 0; |
return 0; |
508 |
} |
} |
509 |
|
|
510 |
/** |
/** |
|
* 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(&domain_list_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(&domain_list_lock); |
|
|
return candidate_domain; |
|
|
} |
|
|
|
|
|
/** |
|
511 |
* ccs_find_or_assign_new_domain - Create a domain. |
* ccs_find_or_assign_new_domain - Create a domain. |
512 |
* |
* |
513 |
* @domainname: The name of domain. |
* @domainname: The name of domain. |
514 |
* @profile: Profile number to assign if the domain was newly created. |
* @profile: Profile number to assign if the domain was newly created. |
515 |
* |
* |
516 |
* Returns pointer to "struct domain_info" on success, NULL otherwise. |
* Returns pointer to "struct ccs_domain_info" on success, NULL otherwise. |
517 |
*/ |
*/ |
518 |
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, |
519 |
const u8 profile) |
const u8 profile) |
520 |
{ |
{ |
521 |
struct domain_info *domain = NULL; |
struct ccs_domain_info *entry; |
522 |
const struct path_info *saved_domainname; |
struct ccs_domain_info *domain; |
523 |
mutex_lock(&domain_list_lock); |
const struct ccs_path_info *saved_domainname; |
524 |
domain = ccs_find_domain(domainname); |
bool found = false; |
525 |
if (domain) |
|
526 |
goto out; |
if (!ccs_is_correct_domain(domainname)) |
527 |
if (!ccs_is_correct_domain(domainname, __func__)) |
return NULL; |
528 |
goto out; |
saved_domainname = ccs_get_name(domainname); |
|
saved_domainname = ccs_save_name(domainname); |
|
529 |
if (!saved_domainname) |
if (!saved_domainname) |
530 |
goto out; |
return NULL; |
531 |
/* Can I reuse memory of deleted domain? */ |
entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
532 |
list1_for_each_entry(domain, &domain_list, list) { |
mutex_lock(&ccs_policy_lock); |
533 |
struct task_struct *p; |
list_for_each_entry_rcu(domain, &ccs_domain_list, list) { |
534 |
struct acl_info *ptr; |
if (domain->is_deleted || |
535 |
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) |
|
536 |
continue; |
continue; |
537 |
#ifdef DEBUG_DOMAIN_UNDELETE |
found = true; |
538 |
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; |
|
|
} |
|
|
ccs_set_domain_flag(domain, true, domain->flags); |
|
|
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); |
|
|
} |
|
|
out: |
|
|
mutex_unlock(&domain_list_lock); |
|
|
return domain; |
|
|
} |
|
|
|
|
|
/** |
|
|
* get_argv0 - Get argv[0]. |
|
|
* |
|
|
* @bprm: Pointer to "struct linux_binprm". |
|
|
* @tmp: Buffer for temporal use. |
|
|
* |
|
|
* Returns true on success, false otherwise. |
|
|
*/ |
|
|
static bool get_argv0(struct linux_binprm *bprm, struct ccs_page_buffer *tmp) |
|
|
{ |
|
|
char *arg_ptr = tmp->buffer; |
|
|
int arg_len = 0; |
|
|
unsigned long pos = bprm->p; |
|
|
int i = pos / PAGE_SIZE; |
|
|
int offset = pos % PAGE_SIZE; |
|
|
bool done = false; |
|
|
if (!bprm->argc) |
|
|
goto out; |
|
|
while (1) { |
|
|
struct page *page; |
|
|
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) |
|
|
goto out; |
|
|
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; |
|
|
} |
|
|
/* Read. */ |
|
|
while (offset < PAGE_SIZE) { |
|
|
const unsigned char c = kaddr[offset++]; |
|
|
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
|
|
if (c == '\\') { |
|
|
arg_ptr[arg_len++] = '\\'; |
|
|
arg_ptr[arg_len++] = '\\'; |
|
|
} else if (c == '/') { |
|
|
arg_len = 0; |
|
|
} else if (c > ' ' && c < 127) { |
|
|
arg_ptr[arg_len++] = c; |
|
|
} else { |
|
|
arg_ptr[arg_len++] = '\\'; |
|
|
arg_ptr[arg_len++] = (c >> 6) + '0'; |
|
|
arg_ptr[arg_len++] |
|
|
= ((c >> 3) & 7) + '0'; |
|
|
arg_ptr[arg_len++] = (c & 7) + '0'; |
|
|
} |
|
|
} else { |
|
|
arg_ptr[arg_len] = '\0'; |
|
|
done = true; |
|
|
break; |
|
|
} |
|
|
} |
|
|
/* Unmap. */ |
|
|
kunmap(page); |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
i++; |
|
|
offset = 0; |
|
|
if (done) |
|
|
break; |
|
539 |
} |
} |
540 |
return true; |
if (!found && ccs_memory_ok(entry, sizeof(*entry))) { |
541 |
out: |
INIT_LIST_HEAD(&entry->acl_info_list); |
542 |
return false; |
entry->domainname = saved_domainname; |
543 |
|
saved_domainname = NULL; |
544 |
|
entry->profile = profile; |
545 |
|
list_add_tail_rcu(&entry->list, &ccs_domain_list); |
546 |
|
domain = entry; |
547 |
|
entry = NULL; |
548 |
|
found = true; |
549 |
|
} |
550 |
|
mutex_unlock(&ccs_policy_lock); |
551 |
|
ccs_put_name(saved_domainname); |
552 |
|
kfree(entry); |
553 |
|
return found ? domain : NULL; |
554 |
} |
} |
555 |
|
|
556 |
/** |
/** |
557 |
* find_next_domain - Find a domain. |
* ccs_find_next_domain - Find a domain. |
558 |
* |
* |
559 |
* @r: Pointer to "struct ccs_request_info". |
* @ee: Pointer to "struct ccs_execve_entry". |
|
* @handler: Pathname to verify. May be NULL. |
|
560 |
* |
* |
561 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
562 |
|
* |
563 |
|
* Caller holds ccs_read_lock(). |
564 |
*/ |
*/ |
565 |
static int find_next_domain(struct ccs_request_info *r, |
static int ccs_find_next_domain(struct ccs_execve_entry *ee) |
|
const struct path_info *handler) |
|
566 |
{ |
{ |
567 |
/* |
struct ccs_request_info *r = &ee->r; |
568 |
* This function assumes that the size of buffer returned by |
const struct ccs_path_info *handler = ee->handler; |
569 |
* ccs_realpath() = CCS_MAX_PATHNAME_LEN. |
struct ccs_domain_info *domain = NULL; |
|
*/ |
|
|
struct domain_info *domain = NULL; |
|
570 |
const char *old_domain_name = r->domain->domainname->name; |
const char *old_domain_name = r->domain->domainname->name; |
571 |
struct linux_binprm *bprm = r->bprm; |
struct linux_binprm *bprm = ee->bprm; |
572 |
struct ccs_page_buffer *tmp = r->obj->tmp; |
const u32 ccs_flags = current->ccs_flags; |
573 |
const char *original_name = bprm->filename; |
struct ccs_path_info rn = { }; /* real name */ |
574 |
const u8 mode = r->mode; |
struct ccs_path_info ln; /* last name */ |
|
const bool is_enforce = (mode == 3); |
|
|
const u32 tomoyo_flags = r->tomoyo_flags; |
|
|
char *new_domain_name = NULL; |
|
|
char *real_program_name = NULL; |
|
|
char *symlink_program_name = NULL; |
|
|
struct path_info rn; /* real name */ |
|
|
struct path_info sn; /* symlink name */ |
|
|
struct path_info ln; /* last name */ |
|
575 |
int retval; |
int retval; |
576 |
|
bool need_kfree = false; |
577 |
{ |
ln.name = ccs_last_word(old_domain_name); |
578 |
/* |
ccs_fill_path_info(&ln); |
579 |
* Built-in initializers. This is needed because policies are |
retry: |
580 |
* not loaded until starting /sbin/init. |
current->ccs_flags = ccs_flags; |
581 |
*/ |
r->cond = NULL; |
582 |
static bool first = true; |
if (need_kfree) { |
583 |
if (first) { |
kfree(rn.name); |
584 |
update_domain_initializer_entry(NULL, "/sbin/hotplug", |
need_kfree = false; |
|
false, false); |
|
|
update_domain_initializer_entry(NULL, "/sbin/modprobe", |
|
|
false, false); |
|
|
first = false; |
|
|
} |
|
585 |
} |
} |
586 |
|
|
587 |
retry: |
/* Get symlink's pathname of program. */ |
588 |
current->tomoyo_flags = tomoyo_flags; |
retval = ccs_symlink_path(bprm->filename, &rn); |
589 |
r->tomoyo_flags = tomoyo_flags; |
if (retval < 0) |
|
/* Get ccs_realpath of program. */ |
|
|
retval = -ENOENT; /* I hope ccs_realpath() won't fail with -ENOMEM. */ |
|
|
ccs_free(real_program_name); |
|
|
real_program_name = ccs_realpath(original_name); |
|
|
if (!real_program_name) |
|
|
goto out; |
|
|
/* Get ccs_realpath of symbolic link. */ |
|
|
ccs_free(symlink_program_name); |
|
|
symlink_program_name = ccs_realpath_nofollow(original_name); |
|
|
if (!symlink_program_name) |
|
590 |
goto out; |
goto out; |
591 |
|
need_kfree = true; |
|
rn.name = real_program_name; |
|
|
ccs_fill_path_info(&rn); |
|
|
sn.name = symlink_program_name; |
|
|
ccs_fill_path_info(&sn); |
|
|
ln.name = ccs_get_last_name(r->domain); |
|
|
ccs_fill_path_info(&ln); |
|
592 |
|
|
593 |
if (handler) { |
if (handler) { |
594 |
if (ccs_pathcmp(&rn, handler)) { |
if (ccs_pathcmp(&rn, handler)) { |
601 |
} |
} |
602 |
goto out; |
goto out; |
603 |
} |
} |
604 |
goto calculate_domain; |
} else { |
605 |
} |
struct ccs_aggregator_entry *ptr; |
606 |
|
/* Check 'aggregator' directive. */ |
607 |
/* Check 'alias' directive. */ |
list_for_each_entry_rcu(ptr, &ccs_aggregator_list, list) { |
|
if (ccs_pathcmp(&rn, &sn)) { |
|
|
struct alias_entry *ptr; |
|
|
/* Is this program allowed to be called via symbolic links? */ |
|
|
list1_for_each_entry(ptr, &alias_list, list) { |
|
|
if (ptr->is_deleted || |
|
|
ccs_pathcmp(&rn, ptr->original_name) || |
|
|
ccs_pathcmp(&sn, 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(&rn); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Compare basename of real_program_name and argv[0] */ |
|
|
r->mode = ccs_check_flags(r->domain, CCS_TOMOYO_MAC_FOR_ARGV0); |
|
|
if (bprm->argc > 0 && r->mode) { |
|
|
char *base_argv0 = tmp->buffer; |
|
|
const char *base_filename; |
|
|
retval = -ENOMEM; |
|
|
if (!get_argv0(bprm, tmp)) |
|
|
goto out; |
|
|
base_filename = strrchr(real_program_name, '/'); |
|
|
if (!base_filename) |
|
|
base_filename = real_program_name; |
|
|
else |
|
|
base_filename++; |
|
|
if (strcmp(base_argv0, base_filename)) { |
|
|
retval = ccs_check_argv0_perm(r, &rn, base_argv0); |
|
|
if (retval == 1) { |
|
|
r->retry++; |
|
|
goto retry; |
|
|
} |
|
|
r->retry = 0; |
|
|
r->tomoyo_flags = current->tomoyo_flags; |
|
|
if (retval < 0) |
|
|
goto out; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Check 'aggregator' directive. */ |
|
|
{ |
|
|
struct aggregator_entry *ptr; |
|
|
/* Is this program allowed to be aggregated? */ |
|
|
list1_for_each_entry(ptr, &aggregator_list, list) { |
|
608 |
if (ptr->is_deleted || |
if (ptr->is_deleted || |
609 |
!ccs_path_matches_pattern(&rn, ptr->original_name)) |
!ccs_path_matches_pattern(&rn, ptr->original_name)) |
610 |
continue; |
continue; |
611 |
memset(real_program_name, 0, CCS_MAX_PATHNAME_LEN); |
kfree(rn.name); |
612 |
strncpy(real_program_name, ptr->aggregated_name->name, |
need_kfree = false; |
613 |
CCS_MAX_PATHNAME_LEN - 1); |
/* This is OK because it is read only. */ |
614 |
ccs_fill_path_info(&rn); |
rn = *ptr->aggregated_name; |
615 |
break; |
break; |
616 |
} |
} |
|
} |
|
617 |
|
|
618 |
/* Check execute permission. */ |
/* Check execute permission. */ |
619 |
r->mode = mode; |
retval = ccs_exec_perm(r, &rn); |
620 |
retval = ccs_check_exec_perm(r, &rn); |
if (retval == 1) |
621 |
if (retval == 1) { |
goto retry; |
622 |
r->retry++; |
if (retval < 0) |
623 |
goto retry; |
goto out; |
624 |
} |
} |
|
r->retry = 0; |
|
|
r->tomoyo_flags = current->tomoyo_flags; |
|
|
if (retval < 0) |
|
|
goto out; |
|
625 |
|
|
626 |
calculate_domain: |
/* Calculate domain to transit to. */ |
627 |
new_domain_name = tmp->buffer; |
if (ccs_is_domain_initializer(r->domain->domainname, &rn, &ln)) { |
628 |
if (is_domain_initializer(r->domain->domainname, &rn, &ln)) { |
/* Transit to the child of ccs_kernel_domain domain. */ |
629 |
/* Transit to the child of KERNEL_DOMAIN domain. */ |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, ROOT_NAME " " "%s", |
630 |
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, |
rn.name); |
631 |
ROOT_NAME " " "%s", real_program_name); |
} else if (r->domain == &ccs_kernel_domain && !ccs_policy_loaded) { |
|
} else if (r->domain == &KERNEL_DOMAIN && !sbin_init_started) { |
|
632 |
/* |
/* |
633 |
* Needn't to transit from kernel domain before starting |
* Needn't to transit from kernel domain before starting |
634 |
* /sbin/init. But transit from kernel domain if executing |
* /sbin/init. But transit from kernel domain if executing |
635 |
* initializers because they might start before /sbin/init. |
* initializers because they might start before /sbin/init. |
636 |
*/ |
*/ |
637 |
domain = r->domain; |
domain = r->domain; |
638 |
} else if (is_domain_keeper(r->domain->domainname, &rn, &ln)) { |
} else if (ccs_is_domain_keeper(r->domain->domainname, &rn, &ln)) { |
639 |
/* Keep current domain. */ |
/* Keep current domain. */ |
640 |
domain = r->domain; |
domain = r->domain; |
641 |
} else { |
} else { |
642 |
/* Normal domain transition. */ |
/* Normal domain transition. */ |
643 |
snprintf(new_domain_name, CCS_MAX_PATHNAME_LEN + 1, |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s", |
644 |
"%s %s", old_domain_name, real_program_name); |
old_domain_name, rn.name); |
645 |
} |
} |
646 |
if (domain || strlen(new_domain_name) >= CCS_MAX_PATHNAME_LEN) |
if (domain || strlen(ee->tmp) >= CCS_EXEC_TMPSIZE - 10) |
647 |
goto done; |
goto done; |
648 |
domain = ccs_find_domain(new_domain_name); |
domain = ccs_find_domain(ee->tmp); |
649 |
if (domain) |
if (domain) |
650 |
goto done; |
goto done; |
651 |
if (is_enforce) { |
if (r->mode == CCS_CONFIG_ENFORCING) { |
652 |
int error = ccs_check_supervisor(r, |
int error = ccs_supervisor(r, "# wants to create domain\n" |
653 |
"# wants to create domain\n" |
"%s\n", ee->tmp); |
654 |
"%s\n", new_domain_name); |
if (error == 1) |
|
if (error == 1) { |
|
|
r->retry++; |
|
655 |
goto retry; |
goto retry; |
|
} |
|
|
r->retry = 0; |
|
656 |
if (error < 0) |
if (error < 0) |
657 |
goto done; |
goto done; |
658 |
} |
} |
659 |
domain = ccs_find_or_assign_new_domain(new_domain_name, r->profile); |
domain = ccs_find_or_assign_new_domain(ee->tmp, r->profile); |
660 |
if (domain) |
if (domain) |
661 |
audit_domain_creation_log(domain); |
ccs_audit_domain_creation_log(r->domain); |
662 |
done: |
done: |
663 |
if (!domain) { |
if (!domain) { |
664 |
printk(KERN_WARNING "TOMOYO-ERROR: Domain '%s' not defined.\n", |
printk(KERN_WARNING "ERROR: Domain '%s' not defined.\n", |
665 |
new_domain_name); |
ee->tmp); |
666 |
if (is_enforce) |
if (r->mode == CCS_CONFIG_ENFORCING) |
667 |
retval = -EPERM; |
retval = -EPERM; |
668 |
else { |
else { |
669 |
retval = 0; |
retval = 0; |
670 |
ccs_set_domain_flag(r->domain, false, |
r->domain->domain_transition_failed = true; |
|
DOMAIN_FLAGS_TRANSITION_FAILED); |
|
671 |
} |
} |
672 |
} else { |
} else { |
673 |
retval = 0; |
retval = 0; |
674 |
} |
} |
675 |
out: |
out: |
|
ccs_free(real_program_name); |
|
|
ccs_free(symlink_program_name); |
|
676 |
if (domain) |
if (domain) |
677 |
r->domain = domain; |
r->domain = domain; |
678 |
|
if (need_kfree) |
679 |
|
kfree(rn.name); |
680 |
return retval; |
return retval; |
681 |
} |
} |
682 |
|
|
683 |
/** |
/** |
684 |
* check_environ - Check permission for environment variable names. |
* ccs_environ - Check permission for environment variable names. |
685 |
* |
* |
686 |
* @r: Pointer to "struct ccs_request_info". |
* @ee: Pointer to "struct ccs_execve_entry". |
687 |
* |
* |
688 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
689 |
*/ |
*/ |
690 |
static int check_environ(struct ccs_request_info *r) |
static int ccs_environ(struct ccs_execve_entry *ee) |
691 |
{ |
{ |
692 |
struct linux_binprm *bprm = r->bprm; |
struct ccs_request_info *r = &ee->r; |
693 |
struct ccs_page_buffer *tmp = r->obj->tmp; |
struct linux_binprm *bprm = ee->bprm; |
694 |
char *arg_ptr = tmp->buffer; |
char *arg_ptr = ee->tmp; |
695 |
int arg_len = 0; |
int arg_len = 0; |
696 |
unsigned long pos = bprm->p; |
unsigned long pos = bprm->p; |
|
int i = pos / PAGE_SIZE; |
|
697 |
int offset = pos % PAGE_SIZE; |
int offset = pos % PAGE_SIZE; |
698 |
int argv_count = bprm->argc; |
int argv_count = bprm->argc; |
699 |
int envp_count = bprm->envc; |
int envp_count = bprm->envc; |
702 |
if (!r->mode || !envp_count) |
if (!r->mode || !envp_count) |
703 |
return 0; |
return 0; |
704 |
while (error == -ENOMEM) { |
while (error == -ENOMEM) { |
705 |
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) |
|
706 |
goto out; |
goto out; |
707 |
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; |
|
|
} |
|
708 |
/* Read. */ |
/* Read. */ |
709 |
while (argv_count && offset < PAGE_SIZE) { |
while (argv_count && offset < PAGE_SIZE) { |
710 |
|
const char *kaddr = ee->dump.data; |
711 |
if (!kaddr[offset++]) |
if (!kaddr[offset++]) |
712 |
argv_count--; |
argv_count--; |
713 |
} |
} |
714 |
if (argv_count) |
if (argv_count) { |
715 |
goto unmap_page; |
offset = 0; |
716 |
|
continue; |
717 |
|
} |
718 |
while (offset < PAGE_SIZE) { |
while (offset < PAGE_SIZE) { |
719 |
|
const char *kaddr = ee->dump.data; |
720 |
const unsigned char c = kaddr[offset++]; |
const unsigned char c = kaddr[offset++]; |
721 |
if (c && arg_len < CCS_MAX_PATHNAME_LEN - 10) { |
if (c && arg_len < CCS_EXEC_TMPSIZE - 10) { |
722 |
if (c == '=') { |
if (c == '=') { |
723 |
arg_ptr[arg_len++] = '\0'; |
arg_ptr[arg_len++] = '\0'; |
724 |
} else if (c == '\\') { |
} else if (c == '\\') { |
738 |
} |
} |
739 |
if (c) |
if (c) |
740 |
continue; |
continue; |
741 |
if (ccs_check_env_perm(r, arg_ptr)) { |
if (ccs_env_perm(r, arg_ptr)) { |
742 |
error = -EPERM; |
error = -EPERM; |
743 |
break; |
break; |
744 |
} |
} |
748 |
} |
} |
749 |
arg_len = 0; |
arg_len = 0; |
750 |
} |
} |
|
unmap_page: |
|
|
/* Unmap. */ |
|
|
kunmap(page); |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
|
|
put_page(page); |
|
|
#endif |
|
|
i++; |
|
751 |
offset = 0; |
offset = 0; |
752 |
} |
} |
753 |
out: |
out: |
757 |
} |
} |
758 |
|
|
759 |
/** |
/** |
760 |
* unescape - Unescape escaped string. |
* ccs_unescape - Unescape escaped string. |
761 |
* |
* |
762 |
* @dest: String to unescape. |
* @dest: String to unescape. |
763 |
* |
* |
764 |
* Returns nothing. |
* Returns nothing. |
765 |
*/ |
*/ |
766 |
static void unescape(unsigned char *dest) |
static void ccs_unescape(unsigned char *dest) |
767 |
{ |
{ |
768 |
unsigned char *src = dest; |
unsigned char *src = dest; |
769 |
unsigned char c; |
unsigned char c; |
770 |
unsigned char d; |
unsigned char d; |
771 |
unsigned char e; |
unsigned char e; |
772 |
while ((c = *src++) != '\0') { |
while (1) { |
773 |
|
c = *src++; |
774 |
|
if (!c) |
775 |
|
break; |
776 |
if (c != '\\') { |
if (c != '\\') { |
777 |
*dest++ = c; |
*dest++ = c; |
778 |
continue; |
continue; |
796 |
} |
} |
797 |
|
|
798 |
/** |
/** |
799 |
* root_depth - Get number of directories to strip. |
* ccs_root_depth - Get number of directories to strip. |
800 |
* |
* |
801 |
* @dentry: Pointer to "struct dentry". |
* @dentry: Pointer to "struct dentry". |
802 |
* @vfsmnt: Pointer to "struct vfsmount". |
* @vfsmnt: Pointer to "struct vfsmount". |
803 |
* |
* |
804 |
* Returns number of directories to strip. |
* Returns number of directories to strip. |
805 |
*/ |
*/ |
806 |
static inline int root_depth(struct dentry *dentry, struct vfsmount *vfsmnt) |
static inline int ccs_root_depth(struct dentry *dentry, struct vfsmount *vfsmnt) |
807 |
{ |
{ |
808 |
int depth = 0; |
int depth = 0; |
|
/***** CRITICAL SECTION START *****/ |
|
809 |
ccs_realpath_lock(); |
ccs_realpath_lock(); |
810 |
for (;;) { |
for (;;) { |
811 |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
820 |
depth++; |
depth++; |
821 |
} |
} |
822 |
ccs_realpath_unlock(); |
ccs_realpath_unlock(); |
|
/***** CRITICAL SECTION END *****/ |
|
823 |
return depth; |
return depth; |
824 |
} |
} |
825 |
|
|
826 |
/** |
/** |
827 |
* get_root_depth - return the depth of root directory. |
* ccs_get_root_depth - return the depth of root directory. |
828 |
* |
* |
829 |
* Returns number of directories to strip. |
* Returns number of directories to strip. |
830 |
*/ |
*/ |
831 |
static int get_root_depth(void) |
static int ccs_get_root_depth(void) |
832 |
{ |
{ |
833 |
int depth; |
int depth; |
834 |
struct dentry *dentry; |
struct dentry *dentry; |
836 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
837 |
struct path root; |
struct path root; |
838 |
#endif |
#endif |
|
/***** CRITICAL SECTION START *****/ |
|
839 |
read_lock(¤t->fs->lock); |
read_lock(¤t->fs->lock); |
840 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
841 |
root = current->fs->root; |
root = current->fs->root; |
847 |
vfsmnt = mntget(current->fs->rootmnt); |
vfsmnt = mntget(current->fs->rootmnt); |
848 |
#endif |
#endif |
849 |
read_unlock(¤t->fs->lock); |
read_unlock(¤t->fs->lock); |
850 |
/***** CRITICAL SECTION END *****/ |
depth = ccs_root_depth(dentry, vfsmnt); |
|
depth = root_depth(dentry, vfsmnt); |
|
851 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) |
852 |
path_put(&root); |
path_put(&root); |
853 |
#else |
#else |
858 |
} |
} |
859 |
|
|
860 |
/** |
/** |
861 |
* try_alt_exec - Try to start execute handler. |
* ccs_try_alt_exec - Try to start execute handler. |
862 |
* |
* |
863 |
* @r: Pointer to "struct ccs_request_info". |
* @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. |
|
864 |
* |
* |
865 |
* Returns 0 on success, negative value otherwise. |
* Returns 0 on success, negative value otherwise. |
866 |
*/ |
*/ |
867 |
static int try_alt_exec(struct ccs_request_info *r, |
static int ccs_try_alt_exec(struct ccs_execve_entry *ee) |
|
const struct path_info *handler, char **eh_path) |
|
868 |
{ |
{ |
869 |
/* |
/* |
870 |
* Contents of modified bprm. |
* Contents of modified bprm. |
880 |
* modified bprm->argv[0] |
* modified bprm->argv[0] |
881 |
* = the program's name specified by execute_handler |
* = the program's name specified by execute_handler |
882 |
* modified bprm->argv[1] |
* modified bprm->argv[1] |
883 |
* = current->domain_info->domainname->name |
* = ccs_current_domain()->domainname->name |
884 |
* modified bprm->argv[2] |
* modified bprm->argv[2] |
885 |
* = the current process's name |
* = the current process's name |
886 |
* modified bprm->argv[3] |
* modified bprm->argv[3] |
902 |
* modified bprm->argv[bprm->envc + bprm->argc + 6] |
* modified bprm->argv[bprm->envc + bprm->argc + 6] |
903 |
* = original bprm->envp[bprm->envc - 1] |
* = original bprm->envp[bprm->envc - 1] |
904 |
*/ |
*/ |
905 |
struct linux_binprm *bprm = r->bprm; |
struct linux_binprm *bprm = ee->bprm; |
906 |
struct file *filp; |
struct file *filp; |
907 |
int retval; |
int retval; |
908 |
const int original_argc = bprm->argc; |
const int original_argc = bprm->argc; |
909 |
const int original_envc = bprm->envc; |
const int original_envc = bprm->envc; |
910 |
struct task_struct *task = current; |
struct task_struct *task = current; |
|
char *buffer = r->obj->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); |
|
911 |
|
|
912 |
/* Close the requested program's dentry. */ |
/* Close the requested program's dentry. */ |
913 |
|
ee->obj.path1.dentry = NULL; |
914 |
|
ee->obj.path1.mnt = NULL; |
915 |
|
ee->obj.validate_done = false; |
916 |
allow_write_access(bprm->file); |
allow_write_access(bprm->file); |
917 |
fput(bprm->file); |
fput(bprm->file); |
918 |
bprm->file = NULL; |
bprm->file = NULL; |
919 |
|
|
920 |
{ /* Adjust root directory for open_exec(). */ |
/* Invalidate page dump cache. */ |
921 |
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); |
|
|
} |
|
922 |
|
|
923 |
/* Move envp[] to argv[] */ |
/* Move envp[] to argv[] */ |
924 |
bprm->argc += bprm->envc; |
bprm->argc += bprm->envc; |
926 |
|
|
927 |
/* Set argv[6] */ |
/* Set argv[6] */ |
928 |
{ |
{ |
929 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d", |
char *cp = ee->tmp; |
930 |
original_envc); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc); |
931 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &cp, bprm); |
932 |
if (retval < 0) |
if (retval < 0) |
933 |
goto out; |
goto out; |
934 |
bprm->argc++; |
bprm->argc++; |
936 |
|
|
937 |
/* Set argv[5] */ |
/* Set argv[5] */ |
938 |
{ |
{ |
939 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, "%d", |
char *cp = ee->tmp; |
940 |
original_argc); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc); |
941 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &cp, bprm); |
942 |
if (retval < 0) |
if (retval < 0) |
943 |
goto out; |
goto out; |
944 |
bprm->argc++; |
bprm->argc++; |
954 |
|
|
955 |
/* Set argv[3] */ |
/* Set argv[3] */ |
956 |
{ |
{ |
957 |
const u32 tomoyo_flags = task->tomoyo_flags; |
char *cp = ee->tmp; |
958 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, |
const u32 ccs_flags = task->ccs_flags; |
959 |
|
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, |
960 |
"pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d " |
"pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d " |
961 |
"sgid=%d fsuid=%d fsgid=%d state[0]=%u " |
"sgid=%d fsuid=%d fsgid=%d state[0]=%u " |
962 |
"state[1]=%u state[2]=%u", |
"state[1]=%u state[2]=%u", |
963 |
task->pid, task->uid, task->gid, task->euid, |
(pid_t) sys_getpid(), current_uid(), current_gid(), |
964 |
task->egid, task->suid, task->sgid, task->fsuid, |
current_euid(), current_egid(), current_suid(), |
965 |
task->fsgid, (u8) (tomoyo_flags >> 24), |
current_sgid(), current_fsuid(), current_fsgid(), |
966 |
(u8) (tomoyo_flags >> 16), (u8) (tomoyo_flags >> 8)); |
(u8) (ccs_flags >> 24), (u8) (ccs_flags >> 16), |
967 |
retval = copy_strings_kernel(1, &buffer, bprm); |
(u8) (ccs_flags >> 8)); |
968 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
969 |
if (retval < 0) |
if (retval < 0) |
970 |
goto out; |
goto out; |
971 |
bprm->argc++; |
bprm->argc++; |
976 |
char *exe = (char *) ccs_get_exe(); |
char *exe = (char *) ccs_get_exe(); |
977 |
if (exe) { |
if (exe) { |
978 |
retval = copy_strings_kernel(1, &exe, bprm); |
retval = copy_strings_kernel(1, &exe, bprm); |
979 |
ccs_free(exe); |
kfree(exe); |
980 |
} else { |
} else { |
981 |
snprintf(buffer, sizeof(struct ccs_page_buffer) - 1, |
exe = ee->tmp; |
982 |
"<unknown>"); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>"); |
983 |
retval = copy_strings_kernel(1, &buffer, bprm); |
retval = copy_strings_kernel(1, &exe, bprm); |
984 |
} |
} |
985 |
if (retval < 0) |
if (retval < 0) |
986 |
goto out; |
goto out; |
989 |
|
|
990 |
/* Set argv[1] */ |
/* Set argv[1] */ |
991 |
{ |
{ |
992 |
strncpy(buffer, task->domain_info->domainname->name, |
char *cp = ee->tmp; |
993 |
sizeof(struct ccs_page_buffer) - 1); |
snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s", |
994 |
retval = copy_strings_kernel(1, &buffer, bprm); |
ccs_current_domain()->domainname->name); |
995 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
996 |
if (retval < 0) |
if (retval < 0) |
997 |
goto out; |
goto out; |
998 |
bprm->argc++; |
bprm->argc++; |
1000 |
|
|
1001 |
/* Set argv[0] */ |
/* Set argv[0] */ |
1002 |
{ |
{ |
1003 |
retval = copy_strings_kernel(1, &execute_handler, bprm); |
int depth = ccs_get_root_depth(); |
1004 |
|
int len = ee->handler->total_len + 1; |
1005 |
|
char *cp = kmalloc(len, GFP_KERNEL); |
1006 |
|
if (!cp) { |
1007 |
|
retval = -ENOMEM; |
1008 |
|
goto out; |
1009 |
|
} |
1010 |
|
ee->handler_path = cp; |
1011 |
|
memmove(cp, ee->handler->name, len); |
1012 |
|
ccs_unescape(cp); |
1013 |
|
retval = -ENOENT; |
1014 |
|
if (!*cp || *cp != '/') |
1015 |
|
goto out; |
1016 |
|
/* Adjust root directory for open_exec(). */ |
1017 |
|
while (depth) { |
1018 |
|
cp = strchr(cp + 1, '/'); |
1019 |
|
if (!cp) |
1020 |
|
goto out; |
1021 |
|
depth--; |
1022 |
|
} |
1023 |
|
memmove(ee->handler_path, cp, strlen(cp) + 1); |
1024 |
|
cp = ee->handler_path; |
1025 |
|
retval = copy_strings_kernel(1, &cp, bprm); |
1026 |
if (retval < 0) |
if (retval < 0) |
1027 |
goto out; |
goto out; |
1028 |
bprm->argc++; |
bprm->argc++; |
1034 |
#endif |
#endif |
1035 |
|
|
1036 |
/* OK, now restart the process with execute handler program's dentry. */ |
/* OK, now restart the process with execute handler program's dentry. */ |
1037 |
filp = open_exec(execute_handler); |
filp = open_exec(ee->handler_path); |
1038 |
if (IS_ERR(filp)) { |
if (IS_ERR(filp)) { |
1039 |
retval = PTR_ERR(filp); |
retval = PTR_ERR(filp); |
1040 |
goto out; |
goto out; |
1041 |
} |
} |
1042 |
|
ee->obj.path1.dentry = filp->f_dentry; |
1043 |
|
ee->obj.path1.mnt = filp->f_vfsmnt; |
1044 |
bprm->file = filp; |
bprm->file = filp; |
1045 |
bprm->filename = execute_handler; |
bprm->filename = ee->handler_path; |
1046 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) |
1047 |
bprm->interp = execute_handler; |
bprm->interp = bprm->filename; |
1048 |
#endif |
#endif |
1049 |
retval = prepare_binprm(bprm); |
retval = prepare_binprm(bprm); |
1050 |
if (retval < 0) |
if (retval < 0) |
1051 |
goto out; |
goto out; |
1052 |
task->tomoyo_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
task->ccs_flags |= CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1053 |
retval = find_next_domain(r, handler); |
retval = ccs_find_next_domain(ee); |
1054 |
task->tomoyo_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
task->ccs_flags &= ~CCS_DONT_SLEEP_ON_ENFORCE_ERROR; |
1055 |
out: |
out: |
1056 |
return retval; |
return retval; |
1057 |
} |
} |
1058 |
|
|
1059 |
/** |
/** |
1060 |
* find_execute_handler - Find an execute handler. |
* ccs_find_execute_handler - Find an execute handler. |
1061 |
* |
* |
1062 |
|
* @ee: Pointer to "struct ccs_execve_entry". |
1063 |
* @type: Type of execute handler. |
* @type: Type of execute handler. |
1064 |
* |
* |
1065 |
* Returns pointer to "struct path_info" if found, NULL otherwise. |
* Returns true if found, false otherwise. |
1066 |
|
* |
1067 |
|
* Caller holds ccs_read_lock(). |
1068 |
*/ |
*/ |
1069 |
static const struct path_info *find_execute_handler(const u8 type) |
static bool ccs_find_execute_handler(struct ccs_execve_entry *ee, |
1070 |
|
const u8 type) |
1071 |
{ |
{ |
1072 |
struct task_struct *task = current; |
struct task_struct *task = current; |
1073 |
const struct domain_info *domain = task->domain_info; |
const struct ccs_domain_info *domain = ccs_current_domain(); |
1074 |
struct acl_info *ptr; |
struct ccs_acl_info *ptr; |
1075 |
|
bool found = false; |
1076 |
/* |
/* |
1077 |
* Don't use execute handler if the current process is |
* Don't use execute handler if the current process is |
1078 |
* marked as execute handler to avoid infinite execute handler loop. |
* marked as execute handler to avoid infinite execute handler loop. |
1079 |
*/ |
*/ |
1080 |
if (task->tomoyo_flags & TOMOYO_TASK_IS_EXECUTE_HANDLER) |
if (task->ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER) |
1081 |
return NULL; |
return false; |
1082 |
list1_for_each_entry(ptr, &domain->acl_info_list, list) { |
list_for_each_entry(ptr, &domain->acl_info_list, list) { |
1083 |
struct execute_handler_record *acl; |
struct ccs_execute_handler_record *acl; |
1084 |
if (ptr->type != type) |
if (ptr->type != type) |
1085 |
continue; |
continue; |
1086 |
acl = container_of(ptr, struct execute_handler_record, head); |
acl = container_of(ptr, struct ccs_execute_handler_record, |
1087 |
return acl->handler; |
head); |
1088 |
|
ee->handler = acl->handler; |
1089 |
|
found = true; |
1090 |
|
break; |
1091 |
} |
} |
1092 |
return NULL; |
return found; |
1093 |
} |
} |
1094 |
|
|
|
/* List of next_domain which is used for checking interpreter's permissions. */ |
|
|
struct execve_entry { |
|
|
struct list_head list; |
|
|
struct task_struct *task; |
|
|
struct domain_info *next_domain; |
|
|
}; |
|
|
|
|
|
static LIST_HEAD(execve_list); |
|
|
static DEFINE_SPINLOCK(execve_list_lock); |
|
|
|
|
1095 |
/** |
/** |
1096 |
* ccs_register_next_domain - Remember next_domain. |
* ccs_dump_page - Dump a page to buffer. |
1097 |
* |
* |
1098 |
* @next_domain: Pointer to "struct domain_info". |
* @bprm: Pointer to "struct linux_binprm". |
1099 |
* |
* @pos: Location to dump. |
1100 |
* Returns 0 on success, -ENOMEM otherwise. |
* @dump: Poiner to "struct ccs_page_dump". |
|
*/ |
|
|
static int ccs_register_next_domain(struct domain_info *next_domain) |
|
|
{ |
|
|
struct execve_entry *ee = kmalloc(sizeof(*ee), GFP_KERNEL); |
|
|
if (!ee) |
|
|
return -ENOMEM; |
|
|
ee->task = current; |
|
|
ee->next_domain = next_domain; |
|
|
/***** CRITICAL SECTION START *****/ |
|
|
spin_lock(&execve_list_lock); |
|
|
list_add(&ee->list, &execve_list); |
|
|
spin_unlock(&execve_list_lock); |
|
|
/***** CRITICAL SECTION END *****/ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
/** |
|
|
* ccs_fetch_next_domain - Fetch next_domain from the list. |
|
1101 |
* |
* |
1102 |
* Returns pointer to "struct domain_info" which will be used if execve() |
* Returns true on success, false otherwise. |
|
* succeeds. This function does not return NULL. |
|
1103 |
*/ |
*/ |
1104 |
struct domain_info *ccs_fetch_next_domain(void) |
bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, |
1105 |
|
struct ccs_page_dump *dump) |
1106 |
{ |
{ |
1107 |
struct task_struct *task = current; |
struct page *page; |
1108 |
struct domain_info *next_domain = task->domain_info; |
/* dump->data is released by ccs_finish_execve(). */ |
1109 |
struct execve_entry *p; |
if (!dump->data) { |
1110 |
/***** CRITICAL SECTION START *****/ |
dump->data = kzalloc(PAGE_SIZE, GFP_KERNEL); |
1111 |
spin_lock(&execve_list_lock); |
if (!dump->data) |
1112 |
list_for_each_entry(p, &execve_list, list) { |
return false; |
|
if (p->task != task) |
|
|
continue; |
|
|
next_domain = p->next_domain; |
|
|
break; |
|
1113 |
} |
} |
1114 |
spin_unlock(&execve_list_lock); |
/* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */ |
1115 |
/***** CRITICAL SECTION END *****/ |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1116 |
return next_domain; |
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1117 |
} |
return false; |
1118 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU) |
1119 |
/** |
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1120 |
* ccs_unregister_next_domain - Forget next_domain. |
return false; |
1121 |
*/ |
#elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU) |
1122 |
static void ccs_unregister_next_domain(void) |
if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0) |
1123 |
{ |
return false; |
1124 |
struct task_struct *task = current; |
#else |
1125 |
struct execve_entry *p; |
page = bprm->page[pos / PAGE_SIZE]; |
1126 |
struct execve_entry *ee = NULL; |
#endif |
1127 |
/***** CRITICAL SECTION START *****/ |
if (page != dump->page) { |
1128 |
spin_lock(&execve_list_lock); |
const unsigned int offset = pos % PAGE_SIZE; |
1129 |
list_for_each_entry(p, &execve_list, list) { |
/* |
1130 |
if (p->task != task) |
* Maybe kmap()/kunmap() should be used here. |
1131 |
continue; |
* But remove_arg_zero() uses kmap_atomic()/kunmap_atomic(). |
1132 |
list_del(&p->list); |
* So do I. |
1133 |
ee = p; |
*/ |
1134 |
break; |
char *kaddr = kmap_atomic(page, KM_USER0); |
1135 |
|
dump->page = page; |
1136 |
|
memcpy(dump->data + offset, kaddr + offset, PAGE_SIZE - offset); |
1137 |
|
kunmap_atomic(kaddr, KM_USER0); |
1138 |
} |
} |
1139 |
spin_unlock(&execve_list_lock); |
/* Same with put_arg_page(page) in fs/exec.c */ |
1140 |
/***** CRITICAL SECTION END *****/ |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) && defined(CONFIG_MMU) |
1141 |
kfree(ee); |
put_page(page); |
1142 |
|
#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3 && defined(CONFIG_MMU) |
1143 |
|
put_page(page); |
1144 |
|
#elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2 && defined(CONFIG_MMU) |
1145 |
|
put_page(page); |
1146 |
|
#endif |
1147 |
|
return true; |
1148 |
} |
} |
1149 |
|
|
1150 |
/** |
/** |
1151 |
* search_binary_handler_with_transition - Perform domain transition. |
* ccs_start_execve - Prepare for execve() operation. |
1152 |
* |
* |
1153 |
* @bprm: Pointer to "struct linux_binprm". |
* @bprm: Pointer to "struct linux_binprm". |
1154 |
* @regs: Pointer to "struct pt_regs". |
* @eep: Pointer to "struct ccs_execve_entry *". |
1155 |
* |
* |
1156 |
* Returns result of search_binary_handler() on success, |
* Returns 0 on success, negative value otherwise. |
|
* negative value otherwise. |
|
1157 |
*/ |
*/ |
1158 |
int search_binary_handler_with_transition(struct linux_binprm *bprm, |
int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve_entry **eep) |
|
struct pt_regs *regs) |
|
1159 |
{ |
{ |
1160 |
int retval; |
int retval; |
1161 |
struct task_struct *task = current; |
struct task_struct *task = current; |
1162 |
const struct path_info *handler; |
struct ccs_execve_entry *ee; |
1163 |
struct ccs_request_info r; |
*eep = NULL; |
1164 |
struct obj_info obj; |
if (!ccs_policy_loaded) |
|
/* |
|
|
* "eh_path" holds path to execute handler program. |
|
|
* Thus, keep valid until search_binary_handler() finishes. |
|
|
*/ |
|
|
char *eh_path = NULL; |
|
|
struct ccs_page_buffer *tmp = ccs_alloc(sizeof(struct ccs_page_buffer)); |
|
|
memset(&obj, 0, sizeof(obj)); |
|
|
if (!sbin_init_started) |
|
1165 |
ccs_load_policy(bprm->filename); |
ccs_load_policy(bprm->filename); |
1166 |
if (!tmp) |
ee = kzalloc(sizeof(*ee), GFP_KERNEL); |
1167 |
|
if (!ee) |
1168 |
return -ENOMEM; |
return -ENOMEM; |
1169 |
|
ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, GFP_KERNEL); |
1170 |
ccs_init_request_info(&r, NULL, CCS_TOMOYO_MAC_FOR_FILE); |
if (!ee->tmp) { |
1171 |
r.bprm = bprm; |
kfree(ee); |
1172 |
r.obj = &obj; |
return -ENOMEM; |
1173 |
obj.path1_dentry = bprm->file->f_dentry; |
} |
1174 |
obj.path1_vfsmnt = bprm->file->f_vfsmnt; |
ee->reader_idx = ccs_read_lock(); |
1175 |
obj.tmp = tmp; |
/* ee->dump->data is allocated by ccs_dump_page(). */ |
1176 |
|
ee->previous_domain = task->ccs_domain_info; |
1177 |
/* Clear manager flag. */ |
/* Clear manager flag. */ |
1178 |
task->tomoyo_flags &= ~CCS_TASK_IS_POLICY_MANAGER; |
task->ccs_flags &= ~CCS_TASK_IS_POLICY_MANAGER; |
1179 |
handler = find_execute_handler(TYPE_EXECUTE_HANDLER); |
/* Tell GC that I started execve(). */ |
1180 |
if (handler) { |
task->ccs_flags |= CCS_TASK_IS_IN_EXECVE; |
1181 |
retval = try_alt_exec(&r, handler, &eh_path); |
/* |
1182 |
|
* Make task->ccs_flags visible to GC before changing |
1183 |
|
* task->ccs_domain_info . |
1184 |
|
*/ |
1185 |
|
smp_mb(); |
1186 |
|
*eep = ee; |
1187 |
|
ccs_init_request_info(&ee->r, NULL, CCS_MAC_FILE_EXECUTE); |
1188 |
|
ee->r.ee = ee; |
1189 |
|
ee->bprm = bprm; |
1190 |
|
ee->r.obj = &ee->obj; |
1191 |
|
ee->obj.path1.dentry = bprm->file->f_dentry; |
1192 |
|
ee->obj.path1.mnt = bprm->file->f_vfsmnt; |
1193 |
|
if (ccs_find_execute_handler(ee, CCS_TYPE_EXECUTE_HANDLER)) { |
1194 |
|
retval = ccs_try_alt_exec(ee); |
1195 |
if (!retval) |
if (!retval) |
1196 |
audit_execute_handler_log(true, handler->name, bprm); |
ccs_audit_execute_handler_log(ee, true); |
1197 |
goto ok; |
goto ok; |
1198 |
} |
} |
1199 |
retval = find_next_domain(&r, NULL); |
retval = ccs_find_next_domain(ee); |
1200 |
if (retval != -EPERM) |
if (retval != -EPERM) |
1201 |
goto ok; |
goto ok; |
1202 |
handler = find_execute_handler(TYPE_DENIED_EXECUTE_HANDLER); |
if (ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) { |
1203 |
if (handler) { |
retval = ccs_try_alt_exec(ee); |
|
retval = try_alt_exec(&r, handler, &eh_path); |
|
1204 |
if (!retval) |
if (!retval) |
1205 |
audit_execute_handler_log(false, handler->name, bprm); |
ccs_audit_execute_handler_log(ee, false); |
1206 |
} |
} |
1207 |
ok: |
ok: |
1208 |
if (retval < 0) |
if (retval < 0) |
1209 |
goto out; |
goto out; |
1210 |
r.mode = ccs_check_flags(r.domain, CCS_TOMOYO_MAC_FOR_ENV); |
/* |
1211 |
retval = check_environ(&r); |
* Proceed to the next domain in order to allow reaching via PID. |
1212 |
if (retval < 0) |
* It will be reverted if execve() failed. Reverting is not good. |
1213 |
goto out; |
* But it is better than being unable to reach via PID in interactive |
1214 |
retval = ccs_register_next_domain(r.domain); |
* enforcing mode. |
1215 |
if (retval < 0) |
*/ |
1216 |
goto out; |
task->ccs_domain_info = ee->r.domain; |
1217 |
task->tomoyo_flags |= TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
ee->r.mode = ccs_get_mode(ee->r.domain->profile, CCS_MAC_ENVIRON); |
1218 |
retval = search_binary_handler(bprm, regs); |
retval = ccs_environ(ee); |
|
task->tomoyo_flags &= ~TOMOYO_CHECK_READ_FOR_OPEN_EXEC; |
|
1219 |
if (retval < 0) |
if (retval < 0) |
1220 |
goto out; |
goto out; |
1221 |
/* Proceed to next domain if execution suceeded. */ |
retval = 0; |
|
task->domain_info = r.domain; |
|
|
mb(); /* Make domain transition visible to other CPUs. */ |
|
|
/* Mark the current process as execute handler. */ |
|
|
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; |
|
1222 |
out: |
out: |
|
ccs_unregister_next_domain(); |
|
|
ccs_free(eh_path); |
|
|
ccs_free(tmp); |
|
1223 |
return retval; |
return retval; |
1224 |
} |
} |
1225 |
|
|
|
#else |
|
|
|
|
1226 |
/** |
/** |
1227 |
* search_binary_handler_with_transition - Wrapper for search_binary_handler(). |
* ccs_finish_execve - Clean up execve() operation. |
1228 |
* |
* |
1229 |
* @bprm: Pointer to "struct linux_binprm". |
* @retval: Return code of an execve() operation. |
1230 |
* @regs: Pointer to "struct pt_regs". |
* @ee: Pointer to "struct ccs_execve_entry". |
1231 |
* |
* |
1232 |
* Returns the result of search_binary_handler(). |
* Caller holds ccs_read_lock(). |
1233 |
*/ |
*/ |
1234 |
int search_binary_handler_with_transition(struct linux_binprm *bprm, |
void ccs_finish_execve(int retval, struct ccs_execve_entry *ee) |
|
struct pt_regs *regs) |
|
1235 |
{ |
{ |
1236 |
#ifdef CONFIG_SAKURA |
struct task_struct *task = current; |
1237 |
/* Clear manager flag. */ |
if (!ee) |
1238 |
current->tomoyo_flags &= ~CCS_TASK_IS_POLICY_MANAGER; |
return; |
1239 |
ccs_load_policy(bprm->filename); |
if (retval < 0) { |
1240 |
#endif |
task->ccs_domain_info = ee->previous_domain; |
1241 |
return search_binary_handler(bprm, regs); |
/* |
1242 |
|
* Make task->ccs_domain_info visible to GC before changing |
1243 |
|
* task->ccs_flags . |
1244 |
|
*/ |
1245 |
|
smp_mb(); |
1246 |
|
} else { |
1247 |
|
/* Mark the current process as execute handler. */ |
1248 |
|
if (ee->handler) |
1249 |
|
task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER; |
1250 |
|
/* Mark the current process as normal process. */ |
1251 |
|
else |
1252 |
|
task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER; |
1253 |
|
} |
1254 |
|
/* Tell GC that I finished execve(). */ |
1255 |
|
task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE; |
1256 |
|
ccs_read_unlock(ee->reader_idx); |
1257 |
|
kfree(ee->handler_path); |
1258 |
|
kfree(ee->tmp); |
1259 |
|
kfree(ee->dump.data); |
1260 |
|
kfree(ee); |
1261 |
} |
} |
|
|
|
|
#endif |
|