1.0.36
@@ -0,0 +1,307 @@ | ||
1 | +Notes for AKARI project | |
2 | + | |
3 | +AKARI is Access Keeping And Regulating Instrument for Linux 2.6 and later | |
4 | +kernels. | |
5 | + | |
6 | +You can use AKARI for analyzing your system's behavior (i.e. reports which | |
7 | +application accesses which resources like strace command does) and optionally | |
8 | +restricting your system's behavior (i.e. controls which application can | |
9 | +access which resources like TOMOYO/AppArmor does). | |
10 | + | |
11 | +AKARI is forked from TOMOYO 1.8 and made as a LKM (loadable kernel module) | |
12 | +so that you don't need to replace your kernels installed in your system. | |
13 | + | |
14 | +This patch is released under the GPLv2. | |
15 | + | |
16 | +Project URL: http://akari.osdn.jp/ | |
17 | + | |
18 | +ChangeLog: | |
19 | + | |
20 | +Version 1.0 2010/10/10 First release. | |
21 | + | |
22 | +Version 1.0.1 2010/10/18 Minor update release. | |
23 | + | |
24 | + Synchronize with TOMOYO revision 4069. | |
25 | + | |
26 | + Fix off-by-two in ccs_check_unix_address(). | |
27 | + | |
28 | + Implement post accept() LSM hook. | |
29 | + | |
30 | +Version 1.0.2 2010/10/25 Minor update release. | |
31 | + | |
32 | + Synchronize with TOMOYO revision 4090. | |
33 | + | |
34 | + Add getattr() and readdir() checks. | |
35 | + | |
36 | + Use "YYYY/MM/DD hh:mm:ss" format for /proc/ccs/ interface. | |
37 | + | |
38 | + Do not automatically add / for umount(). | |
39 | + | |
40 | +Version 1.0.3 2010/11/01 Minor update release. | |
41 | + | |
42 | + Synchronize with TOMOYO revision 4104. | |
43 | + | |
44 | + Fix pathname handling in ccs_unix_entry(). | |
45 | + | |
46 | +Version 1.0.4 2010/11/11 Minor update release. | |
47 | + | |
48 | + Synchronize with TOMOYO 1.8.0 release. | |
49 | + | |
50 | + Add sysctl() check for 2.6.21 to 2.6.32 kernels. | |
51 | + | |
52 | + Fix double new_decode_dev() bug for mknod(). | |
53 | + | |
54 | + Fix keyword typo. | |
55 | + | |
56 | + Fix build failure on some kernels. | |
57 | + | |
58 | + Changed pathname prefix priority. | |
59 | + | |
60 | + Use hash table for faster scan. | |
61 | + | |
62 | + Updated function comments. | |
63 | + | |
64 | +Version 1.0.5 2010/11/22 Minor update release. | |
65 | + | |
66 | + Make ccs_domain_info/ccs_flags inheritable for 2.6.29 and later kernels. | |
67 | + | |
68 | +Version 1.0.6 2010/12/31 Minor update release. | |
69 | + | |
70 | + Synchronize with TOMOYO revision 4280. | |
71 | + | |
72 | + Use same interface for audit logs. | |
73 | + | |
74 | + Split ccs_null_security into ccs_default_security and ccs_oom_security. | |
75 | + | |
76 | +Version 1.0.7 2011/01/21 Minor update release. | |
77 | + | |
78 | + Synchronize with TOMOYO revision 4400. | |
79 | + | |
80 | + Use filesystem name for unnamed devices when vfsmount is missing. | |
81 | + | |
82 | +Version 1.0.8 2011/02/07 Minor update release. | |
83 | + | |
84 | + Synchronize with TOMOYO revision 4545. | |
85 | + | |
86 | + Fix infinite loop bug when reading /proc/ccs/audit or /proc/ccs/query . | |
87 | + | |
88 | +Version 1.0.9 2011/02/14 Minor update release. | |
89 | + | |
90 | + Fix missing permission check for interpreters in 2.6.30 and later kernels. | |
91 | + | |
92 | +Version 1.0.10 2011/02/15 Minor update release. | |
93 | + | |
94 | + Fix missing permission check for interpreters in 2.6.23 and earlier kernels. | |
95 | + | |
96 | + Fix wrong execute permission check and domain transition in 2.6.28 and earlier kernels. | |
97 | + | |
98 | +Version 1.0.11 2010/04/01 Minor update release. | |
99 | + | |
100 | + Synchronize with TOMOYO 1.8.1 release. | |
101 | + | |
102 | + Run garbage collector without waiting for /proc/ccs/ users. | |
103 | + | |
104 | + Support built-in policy configuration. | |
105 | + | |
106 | + Remove /proc/ccs/meminfo interface. | |
107 | + | |
108 | + Pack policy when printing via /proc/ccs/ interface. | |
109 | + | |
110 | + Fix conditional policy parsing. | |
111 | + | |
112 | + Serialize updating profile's comment line. | |
113 | + | |
114 | +Version 1.0.12 2011/04/11 Minor update release. | |
115 | + | |
116 | + Synchronize with TOMOYO revision 4874. | |
117 | + | |
118 | + Fix fcntl(F_SETFL, O_APPEND) handling. | |
119 | + | |
120 | +Version 1.0.13 2011/05/05 Minor update release. | |
121 | + | |
122 | + Synchronize with TOMOYO revision 4963. | |
123 | + | |
124 | + Fix wrong profile number in audit logs for "misc env" permission. | |
125 | + | |
126 | +Version 1.0.14 2011/05/11 Minor update release. | |
127 | + | |
128 | + Synchronize with TOMOYO revision 4978. | |
129 | + | |
130 | + Fix wrong domainname validation. | |
131 | + | |
132 | +Version 1.0.15 2011/06/20 Minor update release. | |
133 | + | |
134 | + Synchronize with TOMOYO 1.8.2 release. | |
135 | + | |
136 | + Add policy namespace support. | |
137 | + | |
138 | +Version 1.0.16 2011/07/07 Minor update release. | |
139 | + | |
140 | + Synchronize with TOMOYO revision 5235. | |
141 | + | |
142 | + Remove /proc/ccs/.domain_status interface. | |
143 | + | |
144 | +Version 1.0.17 2011/07/13 Minor update release. | |
145 | + | |
146 | + Synchronize with TOMOYO revision 5266. | |
147 | + | |
148 | + Fix /proc/ccs/stat parser. | |
149 | + | |
150 | + Accept "::" notation for IPv6 address. | |
151 | + | |
152 | +Version 1.0.18 2011/09/03 Minor update release. | |
153 | + | |
154 | + Synchronize with TOMOYO revision 5401. | |
155 | + | |
156 | + Avoid race when retrying "file execute" permission check. | |
157 | + | |
158 | + Remove unneeded daemonize(). | |
159 | + | |
160 | + Fix load failure with !CONFIG_SMP && !CONFIG_DEBUG_SPINLOCK kernels. | |
161 | + | |
162 | +Version 1.0.19 2011/09/15 Minor update release. | |
163 | + | |
164 | + Use akari/config.h for choosing build options. | |
165 | + | |
166 | + Fix build error on CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER=y case. | |
167 | + | |
168 | + Use lookup_mnt() rather than __put_namespace(). (2.6.0 to 2.6.2 kernels) | |
169 | + | |
170 | + Fix unbalanced spin_lock()/spin_unlock() pair in lsm_pin(). | |
171 | + (2.6.15 to 2.6.35 kernels) | |
172 | + | |
173 | + Fix "struct task_struct" leaks of tasks created before loading akari.ko . | |
174 | + (2.6.28 and earlier kernels) | |
175 | + | |
176 | + Use "struct task_struct"->pids[PIDTYPE_PID].pid instead of | |
177 | + "struct task_struct" for associating with per "struct task_struct" variables | |
178 | + (i.e. "struct ccs_security") in order to reduce amount of dead memory | |
179 | + waiting for garbage collection. (2.6.29 and later kernels) | |
180 | + | |
181 | + Add akari_test.ko for checking whether akari.ko seems to work or not. | |
182 | + | |
183 | + Add SH and ARM architectures support. (Needs more testing.) | |
184 | + | |
185 | +Version 1.0.20 2011/09/29 Minor update release. | |
186 | + | |
187 | + Synchronize with TOMOYO 1.8.3 release. | |
188 | + | |
189 | + Allow specifying domain transition preference. | |
190 | + | |
191 | + Simplify garbage collector. | |
192 | + | |
193 | +Version 1.0.21 2011/10/25 Minor update release. | |
194 | + | |
195 | + Synchronize with TOMOYO revision 5569. | |
196 | + | |
197 | + Fix incomplete read after seek. | |
198 | + | |
199 | + Use query id for reaching target process's domain policy. | |
200 | + | |
201 | + Fix quota counting. | |
202 | + | |
203 | +Version 1.0.22 2011/11/11 Minor update release. | |
204 | + | |
205 | + Synchronize with TOMOYO revision 5625. | |
206 | + | |
207 | + Optimize for object's size. | |
208 | + | |
209 | +Version 1.0.23 2011/11/18 Minor update release. | |
210 | + | |
211 | + Synchronize with TOMOYO revision 5646. | |
212 | + | |
213 | + Fix kernel config mapping error. | |
214 | + | |
215 | +Version 1.0.24 2011/12/13 Minor update release. | |
216 | + | |
217 | + Synchronize with TOMOYO revision 5711. | |
218 | + | |
219 | + Follow __d_path() behavior change. (Only 2.6.36 and later) | |
220 | + | |
221 | +Version 1.0.25 2012/02/29 Minor update release. | |
222 | + | |
223 | + Synchronize with TOMOYO revision 5893. | |
224 | + | |
225 | + Follow UMH_WAIT_PROC constant renumbering. | |
226 | + | |
227 | + Fix mount flags checking order. | |
228 | + | |
229 | +Version 1.0.26 2012/04/01 Minor update release. | |
230 | + | |
231 | + Synchronize with TOMOYO revision 5973. | |
232 | + | |
233 | + Return appropriate value to poll(). | |
234 | + | |
235 | +Version 1.0.27 2012/05/05 Minor update release. | |
236 | + | |
237 | + Synchronize with TOMOYO revision 6035. | |
238 | + | |
239 | + Readd RHEL_MINOR/AX_MINOR checks. | |
240 | + | |
241 | + Accept manager programs which do not start with / . | |
242 | + | |
243 | +Version 1.0.28 2012/10/20 Security update release. | |
244 | + | |
245 | + Fix kernel panic caused by double kfree() bug when "struct ccs_execve" | |
246 | + pointer was by error duplicated at __ccs_alloc_task_security(). | |
247 | + This bug affects only 2.6.28 and earlier kernels. | |
248 | + | |
249 | +Version 1.0.29 2012/11/04 Minor update release. | |
250 | + | |
251 | + Use dummy pointer as needed in order to make sure that security_bprm_free() | |
252 | + (which is used for making the caller of do_execve() return to previous | |
253 | + domain when do_execve() failed after domain transition) is always called. | |
254 | + Without this fix, domain transition history on 2.6.28 and earlier kernels | |
255 | + becomes inaccurate. | |
256 | + | |
257 | +Version 1.0.30 2013/02/14 Minor update release. | |
258 | + | |
259 | + Commit a2a8474c "exec: do not sleep in TASK_TRACED under ->cred_guard_mutex" | |
260 | + moved "current->in_execve = 1;" from before prepare_bprm_creds() to after | |
261 | + prepare_bprm_creds(). It turned out that, as an unexpected bonus, we can use | |
262 | + security_prepare_creds() as a hook for emulating security_bprm_free() hook. | |
263 | + | |
264 | + I updated the logic for security_bprm_free() emulation, and now AKARI should | |
265 | + be able to coexist with other AKARI-like LKM-based LSM implementations (e.g. | |
266 | + CaitSith) on all kernel versions other than 2.6.29 and 2.6.30. | |
267 | + | |
268 | +Version 1.0.31 2015/01/12 Minor update release. | |
269 | + | |
270 | + Synchronize with TOMOYO revision 6373. | |
271 | + | |
272 | + Fix missing chmod(-1) check in Linux 3.1 and later kernels. | |
273 | + | |
274 | + Fix potentially using bogus attributes when stat() fails. | |
275 | + | |
276 | +Version 1.0.32 2015/04/08 Minor update release. | |
277 | + | |
278 | + Synchronize with TOMOYO revision 6388. | |
279 | + | |
280 | + Fix incorrect readdir() permission check. | |
281 | + | |
282 | +Version 1.0.33 2015/04/21 Minor update release. | |
283 | + | |
284 | + Synchronize with TOMOYO revision 6407. | |
285 | + | |
286 | + Fix incorrect retry request check. | |
287 | + | |
288 | +Version 1.0.34 2015/05/05 Minor update release. | |
289 | + | |
290 | + Synchronize with TOMOYO 1.8.4 release. | |
291 | + | |
292 | + Support multiple use_group entries. | |
293 | + | |
294 | +Version 1.0.35 2015/11/11 Minor update release. | |
295 | + | |
296 | + Synchronize with TOMOYO 1.8.5 release. | |
297 | + | |
298 | + Use memory allocation flags used by TOMOYO 2.x. | |
299 | + | |
300 | + Limit wildcard recursion depth. | |
301 | + | |
302 | +Version 1.0.36 2017/02/20 Minor update release. | |
303 | + | |
304 | + Synchronize with TOMOYO revision 6553. | |
305 | + | |
306 | + The bug fixed in TOMOYO's GC does not affect AKARI because | |
307 | + AKARI always uses CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY. |
@@ -0,0 +1,1054 @@ | ||
1 | +/* | |
2 | + * security/ccsecurity/gc.c | |
3 | + * | |
4 | + * Copyright (C) 2005-2012 NTT DATA CORPORATION | |
5 | + * | |
6 | + * Version: 1.8.5+ 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | + | |
11 | +/***** SECTION1: Constants definition *****/ | |
12 | + | |
13 | +/* For compatibility with older kernels. */ | |
14 | +#ifndef for_each_process | |
15 | +#define for_each_process for_each_task | |
16 | +#endif | |
17 | + | |
18 | +/* The list for "struct ccs_io_buffer". */ | |
19 | +static LIST_HEAD(ccs_io_buffer_list); | |
20 | +/* Lock for protecting ccs_io_buffer_list. */ | |
21 | +static DEFINE_SPINLOCK(ccs_io_buffer_list_lock); | |
22 | + | |
23 | +/***** SECTION2: Structure definition *****/ | |
24 | + | |
25 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) | |
26 | + | |
27 | +/* | |
28 | + * Lock for syscall users. | |
29 | + * | |
30 | + * This lock is used for protecting single SRCU section for 2.6.18 and | |
31 | + * earlier kernels because they don't have SRCU support. | |
32 | + */ | |
33 | +struct ccs_lock_struct { | |
34 | + int counter_idx; /* Currently active index (0 or 1). */ | |
35 | + int counter[2]; /* Current users. Protected by ccs_counter_lock. */ | |
36 | +}; | |
37 | + | |
38 | +#endif | |
39 | + | |
40 | +/***** SECTION3: Prototype definition section *****/ | |
41 | + | |
42 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) | |
43 | +int ccs_lock(void); | |
44 | +#endif | |
45 | +void ccs_del_acl(struct list_head *element); | |
46 | +void ccs_del_condition(struct list_head *element); | |
47 | +void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register); | |
48 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) | |
49 | +void ccs_unlock(const int idx); | |
50 | +#endif | |
51 | + | |
52 | +static bool ccs_domain_used_by_task(struct ccs_domain_info *domain); | |
53 | +static bool ccs_name_used_by_io_buffer(const char *string, const size_t size); | |
54 | +static bool ccs_struct_used_by_io_buffer(const struct list_head *element); | |
55 | +static int ccs_gc_thread(void *unused); | |
56 | +static void ccs_collect_acl(struct list_head *list); | |
57 | +static void ccs_collect_entry(void); | |
58 | +static void ccs_collect_member(const enum ccs_policy_id id, | |
59 | + struct list_head *member_list); | |
60 | +static void ccs_memory_free(const void *ptr, const enum ccs_policy_id type); | |
61 | +static void ccs_put_name_union(struct ccs_name_union *ptr); | |
62 | +static void ccs_put_number_union(struct ccs_number_union *ptr); | |
63 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) | |
64 | +static void ccs_synchronize_counter(void); | |
65 | +#endif | |
66 | +static void ccs_try_to_gc(const enum ccs_policy_id type, | |
67 | + struct list_head *element); | |
68 | + | |
69 | +/***** SECTION4: Standalone functions section *****/ | |
70 | + | |
71 | +/***** SECTION5: Variables definition section *****/ | |
72 | + | |
73 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
74 | + | |
75 | +/* | |
76 | + * Lock for syscall users. | |
77 | + * | |
78 | + * This lock is held for only protecting single SRCU section. | |
79 | + */ | |
80 | +struct srcu_struct ccs_ss; | |
81 | + | |
82 | +#else | |
83 | + | |
84 | +static struct ccs_lock_struct ccs_counter; | |
85 | +/* Lock for protecting ccs_counter. */ | |
86 | +static DEFINE_SPINLOCK(ccs_counter_lock); | |
87 | + | |
88 | +#endif | |
89 | + | |
90 | +/***** SECTION6: Dependent functions section *****/ | |
91 | + | |
92 | +/** | |
93 | + * ccs_memory_free - Free memory for elements. | |
94 | + * | |
95 | + * @ptr: Pointer to allocated memory. | |
96 | + * @type: One of values in "enum ccs_policy_id". | |
97 | + * | |
98 | + * Returns nothing. | |
99 | + * | |
100 | + * Caller holds ccs_policy_lock mutex. | |
101 | + */ | |
102 | +static void ccs_memory_free(const void *ptr, const enum ccs_policy_id type) | |
103 | +{ | |
104 | + /* Size of an element. */ | |
105 | + static const u8 e[CCS_MAX_POLICY] = { | |
106 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
107 | + [CCS_ID_RESERVEDPORT] = sizeof(struct ccs_reserved), | |
108 | +#endif | |
109 | + [CCS_ID_GROUP] = sizeof(struct ccs_group), | |
110 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
111 | + [CCS_ID_ADDRESS_GROUP] = sizeof(struct ccs_address_group), | |
112 | +#endif | |
113 | + [CCS_ID_PATH_GROUP] = sizeof(struct ccs_path_group), | |
114 | + [CCS_ID_NUMBER_GROUP] = sizeof(struct ccs_number_group), | |
115 | + [CCS_ID_AGGREGATOR] = sizeof(struct ccs_aggregator), | |
116 | + [CCS_ID_TRANSITION_CONTROL] | |
117 | + = sizeof(struct ccs_transition_control), | |
118 | + [CCS_ID_MANAGER] = sizeof(struct ccs_manager), | |
119 | + /* [CCS_ID_CONDITION] = "struct ccs_condition"->size, */ | |
120 | + /* [CCS_ID_NAME] = "struct ccs_name"->size, */ | |
121 | + /* [CCS_ID_ACL] = a["struct ccs_acl_info"->type], */ | |
122 | + [CCS_ID_DOMAIN] = sizeof(struct ccs_domain_info), | |
123 | + }; | |
124 | + /* Size of a domain ACL element. */ | |
125 | + static const u8 a[] = { | |
126 | + [CCS_TYPE_PATH_ACL] = sizeof(struct ccs_path_acl), | |
127 | + [CCS_TYPE_PATH2_ACL] = sizeof(struct ccs_path2_acl), | |
128 | + [CCS_TYPE_PATH_NUMBER_ACL] | |
129 | + = sizeof(struct ccs_path_number_acl), | |
130 | + [CCS_TYPE_MKDEV_ACL] = sizeof(struct ccs_mkdev_acl), | |
131 | + [CCS_TYPE_MOUNT_ACL] = sizeof(struct ccs_mount_acl), | |
132 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
133 | + [CCS_TYPE_INET_ACL] = sizeof(struct ccs_inet_acl), | |
134 | + [CCS_TYPE_UNIX_ACL] = sizeof(struct ccs_unix_acl), | |
135 | +#endif | |
136 | +#ifdef CONFIG_CCSECURITY_MISC | |
137 | + [CCS_TYPE_ENV_ACL] = sizeof(struct ccs_env_acl), | |
138 | +#endif | |
139 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
140 | + [CCS_TYPE_CAPABILITY_ACL] = sizeof(struct ccs_capability_acl), | |
141 | +#endif | |
142 | +#ifdef CONFIG_CCSECURITY_IPC | |
143 | + [CCS_TYPE_SIGNAL_ACL] = sizeof(struct ccs_signal_acl), | |
144 | +#endif | |
145 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
146 | + [CCS_TYPE_AUTO_EXECUTE_HANDLER] | |
147 | + = sizeof(struct ccs_handler_acl), | |
148 | + [CCS_TYPE_DENIED_EXECUTE_HANDLER] | |
149 | + = sizeof(struct ccs_handler_acl), | |
150 | +#endif | |
151 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
152 | + [CCS_TYPE_AUTO_TASK_ACL] = sizeof(struct ccs_task_acl), | |
153 | + [CCS_TYPE_MANUAL_TASK_ACL] = sizeof(struct ccs_task_acl), | |
154 | +#endif | |
155 | + }; | |
156 | + size_t size; | |
157 | + if (type == CCS_ID_ACL) | |
158 | + size = a[container_of(ptr, typeof(struct ccs_acl_info), | |
159 | + list)->type]; | |
160 | + else if (type == CCS_ID_NAME) | |
161 | + size = container_of(ptr, typeof(struct ccs_name), | |
162 | + head.list)->size; | |
163 | + else if (type == CCS_ID_CONDITION) | |
164 | + size = container_of(ptr, typeof(struct ccs_condition), | |
165 | + head.list)->size; | |
166 | + else | |
167 | + size = e[type]; | |
168 | + ccs_memory_used[CCS_MEMORY_POLICY] -= ccs_round2(size); | |
169 | + kfree(ptr); | |
170 | +} | |
171 | + | |
172 | +/** | |
173 | + * ccs_put_name_union - Drop reference on "struct ccs_name_union". | |
174 | + * | |
175 | + * @ptr: Pointer to "struct ccs_name_union". | |
176 | + * | |
177 | + * Returns nothing. | |
178 | + */ | |
179 | +static void ccs_put_name_union(struct ccs_name_union *ptr) | |
180 | +{ | |
181 | + ccs_put_group(ptr->group); | |
182 | + ccs_put_name(ptr->filename); | |
183 | +} | |
184 | + | |
185 | +/** | |
186 | + * ccs_put_number_union - Drop reference on "struct ccs_number_union". | |
187 | + * | |
188 | + * @ptr: Pointer to "struct ccs_number_union". | |
189 | + * | |
190 | + * Returns nothing. | |
191 | + */ | |
192 | +static void ccs_put_number_union(struct ccs_number_union *ptr) | |
193 | +{ | |
194 | + ccs_put_group(ptr->group); | |
195 | +} | |
196 | + | |
197 | +/** | |
198 | + * ccs_struct_used_by_io_buffer - Check whether the list element is used by /proc/ccs/ users or not. | |
199 | + * | |
200 | + * @element: Pointer to "struct list_head". | |
201 | + * | |
202 | + * Returns true if @element is used by /proc/ccs/ users, false otherwise. | |
203 | + */ | |
204 | +static bool ccs_struct_used_by_io_buffer(const struct list_head *element) | |
205 | +{ | |
206 | + struct ccs_io_buffer *head; | |
207 | + bool in_use = false; | |
208 | + spin_lock(&ccs_io_buffer_list_lock); | |
209 | + list_for_each_entry(head, &ccs_io_buffer_list, list) { | |
210 | + head->users++; | |
211 | + spin_unlock(&ccs_io_buffer_list_lock); | |
212 | + mutex_lock(&head->io_sem); | |
213 | + if (head->r.domain == element || head->r.group == element || | |
214 | + head->r.acl == element || &head->w.domain->list == element) | |
215 | + in_use = true; | |
216 | + mutex_unlock(&head->io_sem); | |
217 | + spin_lock(&ccs_io_buffer_list_lock); | |
218 | + head->users--; | |
219 | + if (in_use) | |
220 | + break; | |
221 | + } | |
222 | + spin_unlock(&ccs_io_buffer_list_lock); | |
223 | + return in_use; | |
224 | +} | |
225 | + | |
226 | +/** | |
227 | + * ccs_name_used_by_io_buffer - Check whether the string is used by /proc/ccs/ users or not. | |
228 | + * | |
229 | + * @string: String to check. | |
230 | + * @size: Memory allocated for @string . | |
231 | + * | |
232 | + * Returns true if @string is used by /proc/ccs/ users, false otherwise. | |
233 | + */ | |
234 | +static bool ccs_name_used_by_io_buffer(const char *string, const size_t size) | |
235 | +{ | |
236 | + struct ccs_io_buffer *head; | |
237 | + bool in_use = false; | |
238 | + spin_lock(&ccs_io_buffer_list_lock); | |
239 | + list_for_each_entry(head, &ccs_io_buffer_list, list) { | |
240 | + int i; | |
241 | + head->users++; | |
242 | + spin_unlock(&ccs_io_buffer_list_lock); | |
243 | + mutex_lock(&head->io_sem); | |
244 | + for (i = 0; i < CCS_MAX_IO_READ_QUEUE; i++) { | |
245 | + const char *w = head->r.w[i]; | |
246 | + if (w < string || w > string + size) | |
247 | + continue; | |
248 | + in_use = true; | |
249 | + break; | |
250 | + } | |
251 | + mutex_unlock(&head->io_sem); | |
252 | + spin_lock(&ccs_io_buffer_list_lock); | |
253 | + head->users--; | |
254 | + if (in_use) | |
255 | + break; | |
256 | + } | |
257 | + spin_unlock(&ccs_io_buffer_list_lock); | |
258 | + return in_use; | |
259 | +} | |
260 | + | |
261 | +/** | |
262 | + * ccs_del_transition_control - Delete members in "struct ccs_transition_control". | |
263 | + * | |
264 | + * @element: Pointer to "struct list_head". | |
265 | + * | |
266 | + * Returns nothing. | |
267 | + */ | |
268 | +static inline void ccs_del_transition_control(struct list_head *element) | |
269 | +{ | |
270 | + struct ccs_transition_control *ptr = | |
271 | + container_of(element, typeof(*ptr), head.list); | |
272 | + ccs_put_name(ptr->domainname); | |
273 | + ccs_put_name(ptr->program); | |
274 | +} | |
275 | + | |
276 | +/** | |
277 | + * ccs_del_aggregator - Delete members in "struct ccs_aggregator". | |
278 | + * | |
279 | + * @element: Pointer to "struct list_head". | |
280 | + * | |
281 | + * Returns nothing. | |
282 | + */ | |
283 | +static inline void ccs_del_aggregator(struct list_head *element) | |
284 | +{ | |
285 | + struct ccs_aggregator *ptr = | |
286 | + container_of(element, typeof(*ptr), head.list); | |
287 | + ccs_put_name(ptr->original_name); | |
288 | + ccs_put_name(ptr->aggregated_name); | |
289 | +} | |
290 | + | |
291 | +/** | |
292 | + * ccs_del_manager - Delete members in "struct ccs_manager". | |
293 | + * | |
294 | + * @element: Pointer to "struct list_head". | |
295 | + * | |
296 | + * Returns nothing. | |
297 | + */ | |
298 | +static inline void ccs_del_manager(struct list_head *element) | |
299 | +{ | |
300 | + struct ccs_manager *ptr = | |
301 | + container_of(element, typeof(*ptr), head.list); | |
302 | + ccs_put_name(ptr->manager); | |
303 | +} | |
304 | + | |
305 | +/** | |
306 | + * ccs_domain_used_by_task - Check whether the given pointer is referenced by a task. | |
307 | + * | |
308 | + * @domain: Pointer to "struct ccs_domain_info". | |
309 | + * | |
310 | + * Returns true if @domain is in use, false otherwise. | |
311 | + */ | |
312 | +static bool ccs_domain_used_by_task(struct ccs_domain_info *domain) | |
313 | +{ | |
314 | + bool in_use = false; | |
315 | + /* | |
316 | + * Don't delete this domain if somebody is doing execve(). | |
317 | + * | |
318 | + * Since ccs_finish_execve() first reverts ccs_domain_info and then | |
319 | + * updates ccs_flags, we need smp_rmb() to make sure that GC first | |
320 | + * checks ccs_flags and then checks ccs_domain_info. | |
321 | + */ | |
322 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
323 | + int idx; | |
324 | + rcu_read_lock(); | |
325 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
326 | + struct ccs_security *ptr; | |
327 | + struct list_head *list = &ccs_task_security_list[idx]; | |
328 | + list_for_each_entry_rcu(ptr, list, list) { | |
329 | + if (!(ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
330 | + smp_rmb(); /* Avoid out of order execution. */ | |
331 | + if (ptr->ccs_domain_info != domain) | |
332 | + continue; | |
333 | + } | |
334 | + in_use = true; | |
335 | + goto out; | |
336 | + } | |
337 | + } | |
338 | + in_use = ccs_used_by_cred(domain); | |
339 | +out: | |
340 | + rcu_read_unlock(); | |
341 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0) || defined(for_each_process_thread) | |
342 | + struct task_struct *g; | |
343 | + struct task_struct *t; | |
344 | + rcu_read_lock(); | |
345 | + for_each_process_thread(g, t) { | |
346 | + if (!(t->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
347 | + smp_rmb(); /* Avoid out of order execution. */ | |
348 | + if (t->ccs_domain_info != domain) | |
349 | + continue; | |
350 | + } | |
351 | + in_use = true; | |
352 | + goto out; | |
353 | + } | |
354 | +out: | |
355 | + rcu_read_unlock(); | |
356 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) | |
357 | + struct task_struct *g; | |
358 | + struct task_struct *t; | |
359 | + rcu_read_lock(); | |
360 | + read_lock(&tasklist_lock); | |
361 | + do_each_thread(g, t) { | |
362 | + if (!(t->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
363 | + smp_rmb(); /* Avoid out of order execution. */ | |
364 | + if (t->ccs_domain_info != domain) | |
365 | + continue; | |
366 | + } | |
367 | + in_use = true; | |
368 | + goto out; | |
369 | + } while_each_thread(g, t); | |
370 | +out: | |
371 | + read_unlock(&tasklist_lock); | |
372 | + rcu_read_unlock(); | |
373 | +#else | |
374 | + struct task_struct *p; | |
375 | + read_lock(&tasklist_lock); | |
376 | + for_each_process(p) { | |
377 | + if (!(p->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
378 | + smp_rmb(); /* Avoid out of order execution. */ | |
379 | + if (p->ccs_domain_info != domain) | |
380 | + continue; | |
381 | + } | |
382 | + in_use = true; | |
383 | + break; | |
384 | + } | |
385 | + read_unlock(&tasklist_lock); | |
386 | +#endif | |
387 | + return in_use; | |
388 | +} | |
389 | + | |
390 | +/** | |
391 | + * ccs_del_acl - Delete members in "struct ccs_acl_info". | |
392 | + * | |
393 | + * @element: Pointer to "struct list_head". | |
394 | + * | |
395 | + * Returns nothing. | |
396 | + */ | |
397 | +void ccs_del_acl(struct list_head *element) | |
398 | +{ | |
399 | + struct ccs_acl_info *acl = container_of(element, typeof(*acl), list); | |
400 | + ccs_put_condition(acl->cond); | |
401 | + switch (acl->type) { | |
402 | + case CCS_TYPE_PATH_ACL: | |
403 | + { | |
404 | + struct ccs_path_acl *entry = | |
405 | + container_of(acl, typeof(*entry), head); | |
406 | + ccs_put_name_union(&entry->name); | |
407 | + } | |
408 | + break; | |
409 | + case CCS_TYPE_PATH2_ACL: | |
410 | + { | |
411 | + struct ccs_path2_acl *entry = | |
412 | + container_of(acl, typeof(*entry), head); | |
413 | + ccs_put_name_union(&entry->name1); | |
414 | + ccs_put_name_union(&entry->name2); | |
415 | + } | |
416 | + break; | |
417 | + case CCS_TYPE_PATH_NUMBER_ACL: | |
418 | + { | |
419 | + struct ccs_path_number_acl *entry = | |
420 | + container_of(acl, typeof(*entry), head); | |
421 | + ccs_put_name_union(&entry->name); | |
422 | + ccs_put_number_union(&entry->number); | |
423 | + } | |
424 | + break; | |
425 | + case CCS_TYPE_MKDEV_ACL: | |
426 | + { | |
427 | + struct ccs_mkdev_acl *entry = | |
428 | + container_of(acl, typeof(*entry), head); | |
429 | + ccs_put_name_union(&entry->name); | |
430 | + ccs_put_number_union(&entry->mode); | |
431 | + ccs_put_number_union(&entry->major); | |
432 | + ccs_put_number_union(&entry->minor); | |
433 | + } | |
434 | + break; | |
435 | + case CCS_TYPE_MOUNT_ACL: | |
436 | + { | |
437 | + struct ccs_mount_acl *entry = | |
438 | + container_of(acl, typeof(*entry), head); | |
439 | + ccs_put_name_union(&entry->dev_name); | |
440 | + ccs_put_name_union(&entry->dir_name); | |
441 | + ccs_put_name_union(&entry->fs_type); | |
442 | + ccs_put_number_union(&entry->flags); | |
443 | + } | |
444 | + break; | |
445 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
446 | + case CCS_TYPE_INET_ACL: | |
447 | + { | |
448 | + struct ccs_inet_acl *entry = | |
449 | + container_of(acl, typeof(*entry), head); | |
450 | + ccs_put_group(entry->address.group); | |
451 | + ccs_put_number_union(&entry->port); | |
452 | + } | |
453 | + break; | |
454 | + case CCS_TYPE_UNIX_ACL: | |
455 | + { | |
456 | + struct ccs_unix_acl *entry = | |
457 | + container_of(acl, typeof(*entry), head); | |
458 | + ccs_put_name_union(&entry->name); | |
459 | + } | |
460 | + break; | |
461 | +#endif | |
462 | +#ifdef CONFIG_CCSECURITY_MISC | |
463 | + case CCS_TYPE_ENV_ACL: | |
464 | + { | |
465 | + struct ccs_env_acl *entry = | |
466 | + container_of(acl, typeof(*entry), head); | |
467 | + ccs_put_name(entry->env); | |
468 | + } | |
469 | + break; | |
470 | +#endif | |
471 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
472 | + case CCS_TYPE_CAPABILITY_ACL: | |
473 | + { | |
474 | + /* Nothing to do. */ | |
475 | + } | |
476 | + break; | |
477 | +#endif | |
478 | +#ifdef CONFIG_CCSECURITY_IPC | |
479 | + case CCS_TYPE_SIGNAL_ACL: | |
480 | + { | |
481 | + struct ccs_signal_acl *entry = | |
482 | + container_of(acl, typeof(*entry), head); | |
483 | + ccs_put_number_union(&entry->sig); | |
484 | + ccs_put_name(entry->domainname); | |
485 | + } | |
486 | + break; | |
487 | +#endif | |
488 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
489 | + case CCS_TYPE_AUTO_EXECUTE_HANDLER: | |
490 | + case CCS_TYPE_DENIED_EXECUTE_HANDLER: | |
491 | + { | |
492 | + struct ccs_handler_acl *entry = | |
493 | + container_of(acl, typeof(*entry), head); | |
494 | + ccs_put_name(entry->handler); | |
495 | + } | |
496 | + break; | |
497 | +#endif | |
498 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
499 | + case CCS_TYPE_AUTO_TASK_ACL: | |
500 | + case CCS_TYPE_MANUAL_TASK_ACL: | |
501 | + { | |
502 | + struct ccs_task_acl *entry = | |
503 | + container_of(acl, typeof(*entry), head); | |
504 | + ccs_put_name(entry->domainname); | |
505 | + } | |
506 | + break; | |
507 | +#endif | |
508 | + } | |
509 | +} | |
510 | + | |
511 | +/** | |
512 | + * ccs_del_domain - Delete members in "struct ccs_domain_info". | |
513 | + * | |
514 | + * @element: Pointer to "struct list_head". | |
515 | + * | |
516 | + * Returns nothing. | |
517 | + * | |
518 | + * Caller holds ccs_policy_lock mutex. | |
519 | + */ | |
520 | +static inline void ccs_del_domain(struct list_head *element) | |
521 | +{ | |
522 | + struct ccs_domain_info *domain = | |
523 | + container_of(element, typeof(*domain), list); | |
524 | + struct ccs_acl_info *acl; | |
525 | + struct ccs_acl_info *tmp; | |
526 | + /* | |
527 | + * Since this domain is referenced from neither "struct ccs_io_buffer" | |
528 | + * nor "struct task_struct", we can delete elements without checking | |
529 | + * for is_deleted flag. | |
530 | + */ | |
531 | + list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) { | |
532 | + ccs_del_acl(&acl->list); | |
533 | + ccs_memory_free(acl, CCS_ID_ACL); | |
534 | + } | |
535 | + ccs_put_name(domain->domainname); | |
536 | +} | |
537 | + | |
538 | +/** | |
539 | + * ccs_del_path_group - Delete members in "struct ccs_path_group". | |
540 | + * | |
541 | + * @element: Pointer to "struct list_head". | |
542 | + * | |
543 | + * Returns nothing. | |
544 | + */ | |
545 | +static inline void ccs_del_path_group(struct list_head *element) | |
546 | +{ | |
547 | + struct ccs_path_group *member = | |
548 | + container_of(element, typeof(*member), head.list); | |
549 | + ccs_put_name(member->member_name); | |
550 | +} | |
551 | + | |
552 | +/** | |
553 | + * ccs_del_group - Delete "struct ccs_group". | |
554 | + * | |
555 | + * @element: Pointer to "struct list_head". | |
556 | + * | |
557 | + * Returns nothing. | |
558 | + */ | |
559 | +static inline void ccs_del_group(struct list_head *element) | |
560 | +{ | |
561 | + struct ccs_group *group = | |
562 | + container_of(element, typeof(*group), head.list); | |
563 | + ccs_put_name(group->group_name); | |
564 | +} | |
565 | + | |
566 | +/** | |
567 | + * ccs_del_address_group - Delete members in "struct ccs_address_group". | |
568 | + * | |
569 | + * @element: Pointer to "struct list_head". | |
570 | + * | |
571 | + * Returns nothing. | |
572 | + */ | |
573 | +static inline void ccs_del_address_group(struct list_head *element) | |
574 | +{ | |
575 | + /* Nothing to do. */ | |
576 | +} | |
577 | + | |
578 | +/** | |
579 | + * ccs_del_number_group - Delete members in "struct ccs_number_group". | |
580 | + * | |
581 | + * @element: Pointer to "struct list_head". | |
582 | + * | |
583 | + * Returns nothing. | |
584 | + */ | |
585 | +static inline void ccs_del_number_group(struct list_head *element) | |
586 | +{ | |
587 | + /* Nothing to do. */ | |
588 | +} | |
589 | + | |
590 | +/** | |
591 | + * ccs_del_reservedport - Delete members in "struct ccs_reserved". | |
592 | + * | |
593 | + * @element: Pointer to "struct list_head". | |
594 | + * | |
595 | + * Returns nothing. | |
596 | + */ | |
597 | +static inline void ccs_del_reservedport(struct list_head *element) | |
598 | +{ | |
599 | + /* Nothing to do. */ | |
600 | +} | |
601 | + | |
602 | +/** | |
603 | + * ccs_del_condition - Delete members in "struct ccs_condition". | |
604 | + * | |
605 | + * @element: Pointer to "struct list_head". | |
606 | + * | |
607 | + * Returns nothing. | |
608 | + */ | |
609 | +void ccs_del_condition(struct list_head *element) | |
610 | +{ | |
611 | + struct ccs_condition *cond = container_of(element, typeof(*cond), | |
612 | + head.list); | |
613 | + const u16 condc = cond->condc; | |
614 | + const u16 numbers_count = cond->numbers_count; | |
615 | + const u16 names_count = cond->names_count; | |
616 | + const u16 argc = cond->argc; | |
617 | + const u16 envc = cond->envc; | |
618 | + unsigned int i; | |
619 | + const struct ccs_condition_element *condp | |
620 | + = (const struct ccs_condition_element *) (cond + 1); | |
621 | + struct ccs_number_union *numbers_p | |
622 | + = (struct ccs_number_union *) (condp + condc); | |
623 | + struct ccs_name_union *names_p | |
624 | + = (struct ccs_name_union *) (numbers_p + numbers_count); | |
625 | + const struct ccs_argv *argv | |
626 | + = (const struct ccs_argv *) (names_p + names_count); | |
627 | + const struct ccs_envp *envp | |
628 | + = (const struct ccs_envp *) (argv + argc); | |
629 | + for (i = 0; i < numbers_count; i++) | |
630 | + ccs_put_number_union(numbers_p++); | |
631 | + for (i = 0; i < names_count; i++) | |
632 | + ccs_put_name_union(names_p++); | |
633 | + for (i = 0; i < argc; argv++, i++) | |
634 | + ccs_put_name(argv->value); | |
635 | + for (i = 0; i < envc; envp++, i++) { | |
636 | + ccs_put_name(envp->name); | |
637 | + ccs_put_name(envp->value); | |
638 | + } | |
639 | + ccs_put_name(cond->transit); | |
640 | +} | |
641 | + | |
642 | +/** | |
643 | + * ccs_del_name - Delete members in "struct ccs_name". | |
644 | + * | |
645 | + * @element: Pointer to "struct list_head". | |
646 | + * | |
647 | + * Returns nothing. | |
648 | + */ | |
649 | +static inline void ccs_del_name(struct list_head *element) | |
650 | +{ | |
651 | + /* Nothing to do. */ | |
652 | +} | |
653 | + | |
654 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19) | |
655 | + | |
656 | +/** | |
657 | + * ccs_lock - Alternative for srcu_read_lock(). | |
658 | + * | |
659 | + * Returns index number which has to be passed to ccs_unlock(). | |
660 | + */ | |
661 | +int ccs_lock(void) | |
662 | +{ | |
663 | + int idx; | |
664 | + spin_lock(&ccs_counter_lock); | |
665 | + idx = ccs_counter.counter_idx; | |
666 | + ccs_counter.counter[idx]++; | |
667 | + spin_unlock(&ccs_counter_lock); | |
668 | + return idx; | |
669 | +} | |
670 | + | |
671 | +/** | |
672 | + * ccs_unlock - Alternative for srcu_read_unlock(). | |
673 | + * | |
674 | + * @idx: Index number returned by ccs_lock(). | |
675 | + * | |
676 | + * Returns nothing. | |
677 | + */ | |
678 | +void ccs_unlock(const int idx) | |
679 | +{ | |
680 | + spin_lock(&ccs_counter_lock); | |
681 | + ccs_counter.counter[idx]--; | |
682 | + spin_unlock(&ccs_counter_lock); | |
683 | +} | |
684 | + | |
685 | +/** | |
686 | + * ccs_synchronize_counter - Alternative for synchronize_srcu(). | |
687 | + * | |
688 | + * Returns nothing. | |
689 | + */ | |
690 | +static void ccs_synchronize_counter(void) | |
691 | +{ | |
692 | + int idx; | |
693 | + int v; | |
694 | + /* | |
695 | + * Change currently active counter's index. Make it visible to other | |
696 | + * threads by doing it with ccs_counter_lock held. | |
697 | + * This function is called by garbage collector thread, and the garbage | |
698 | + * collector thread is exclusive. Therefore, it is guaranteed that | |
699 | + * SRCU grace period has expired when returning from this function. | |
700 | + */ | |
701 | + spin_lock(&ccs_counter_lock); | |
702 | + idx = ccs_counter.counter_idx; | |
703 | + ccs_counter.counter_idx ^= 1; | |
704 | + v = ccs_counter.counter[idx]; | |
705 | + spin_unlock(&ccs_counter_lock); | |
706 | + /* Wait for previously active counter to become 0. */ | |
707 | + while (v) { | |
708 | + ssleep(1); | |
709 | + spin_lock(&ccs_counter_lock); | |
710 | + v = ccs_counter.counter[idx]; | |
711 | + spin_unlock(&ccs_counter_lock); | |
712 | + } | |
713 | +} | |
714 | + | |
715 | +#endif | |
716 | + | |
717 | +/** | |
718 | + * ccs_try_to_gc - Try to kfree() an entry. | |
719 | + * | |
720 | + * @type: One of values in "enum ccs_policy_id". | |
721 | + * @element: Pointer to "struct list_head". | |
722 | + * | |
723 | + * Returns nothing. | |
724 | + * | |
725 | + * Caller holds ccs_policy_lock mutex. | |
726 | + */ | |
727 | +static void ccs_try_to_gc(const enum ccs_policy_id type, | |
728 | + struct list_head *element) | |
729 | +{ | |
730 | + /* | |
731 | + * __list_del_entry() guarantees that the list element became no longer | |
732 | + * reachable from the list which the element was originally on (e.g. | |
733 | + * ccs_domain_list). Also, synchronize_srcu() guarantees that the list | |
734 | + * element became no longer referenced by syscall users. | |
735 | + */ | |
736 | + __list_del_entry(element); | |
737 | + mutex_unlock(&ccs_policy_lock); | |
738 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
739 | + synchronize_srcu(&ccs_ss); | |
740 | +#else | |
741 | + ccs_synchronize_counter(); | |
742 | +#endif | |
743 | + /* | |
744 | + * However, there are two users which may still be using the list | |
745 | + * element. We need to defer until both users forget this element. | |
746 | + * | |
747 | + * Don't kfree() until "struct ccs_io_buffer"->r.{domain,group,acl} and | |
748 | + * "struct ccs_io_buffer"->w.domain forget this element. | |
749 | + */ | |
750 | + if (ccs_struct_used_by_io_buffer(element)) | |
751 | + goto reinject; | |
752 | + switch (type) { | |
753 | + case CCS_ID_TRANSITION_CONTROL: | |
754 | + ccs_del_transition_control(element); | |
755 | + break; | |
756 | + case CCS_ID_MANAGER: | |
757 | + ccs_del_manager(element); | |
758 | + break; | |
759 | + case CCS_ID_AGGREGATOR: | |
760 | + ccs_del_aggregator(element); | |
761 | + break; | |
762 | + case CCS_ID_GROUP: | |
763 | + ccs_del_group(element); | |
764 | + break; | |
765 | + case CCS_ID_PATH_GROUP: | |
766 | + ccs_del_path_group(element); | |
767 | + break; | |
768 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
769 | + case CCS_ID_ADDRESS_GROUP: | |
770 | + ccs_del_address_group(element); | |
771 | + break; | |
772 | +#endif | |
773 | + case CCS_ID_NUMBER_GROUP: | |
774 | + ccs_del_number_group(element); | |
775 | + break; | |
776 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
777 | + case CCS_ID_RESERVEDPORT: | |
778 | + ccs_del_reservedport(element); | |
779 | + break; | |
780 | +#endif | |
781 | + case CCS_ID_CONDITION: | |
782 | + ccs_del_condition(element); | |
783 | + break; | |
784 | + case CCS_ID_NAME: | |
785 | + /* | |
786 | + * Don't kfree() until all "struct ccs_io_buffer"->r.w[] forget | |
787 | + * this element. | |
788 | + */ | |
789 | + if (ccs_name_used_by_io_buffer | |
790 | + (container_of(element, typeof(struct ccs_name), | |
791 | + head.list)->entry.name, | |
792 | + container_of(element, typeof(struct ccs_name), | |
793 | + head.list)->size)) | |
794 | + goto reinject; | |
795 | + ccs_del_name(element); | |
796 | + break; | |
797 | + case CCS_ID_ACL: | |
798 | + ccs_del_acl(element); | |
799 | + break; | |
800 | + case CCS_ID_DOMAIN: | |
801 | + /* | |
802 | + * Don't kfree() until all "struct task_struct" forget this | |
803 | + * element. | |
804 | + */ | |
805 | + if (ccs_domain_used_by_task | |
806 | + (container_of(element, typeof(struct ccs_domain_info), | |
807 | + list))) | |
808 | + goto reinject; | |
809 | + break; | |
810 | + case CCS_MAX_POLICY: | |
811 | + break; | |
812 | + } | |
813 | + mutex_lock(&ccs_policy_lock); | |
814 | + if (type == CCS_ID_DOMAIN) | |
815 | + ccs_del_domain(element); | |
816 | + ccs_memory_free(element, type); | |
817 | + return; | |
818 | +reinject: | |
819 | + /* | |
820 | + * We can safely reinject this element here bacause | |
821 | + * (1) Appending list elements and removing list elements are protected | |
822 | + * by ccs_policy_lock mutex. | |
823 | + * (2) Only this function removes list elements and this function is | |
824 | + * exclusively executed by ccs_gc_mutex mutex. | |
825 | + * are true. | |
826 | + */ | |
827 | + mutex_lock(&ccs_policy_lock); | |
828 | + list_add_rcu(element, element->prev); | |
829 | +} | |
830 | + | |
831 | +/** | |
832 | + * ccs_collect_member - Delete elements with "struct ccs_acl_head". | |
833 | + * | |
834 | + * @id: One of values in "enum ccs_policy_id". | |
835 | + * @member_list: Pointer to "struct list_head". | |
836 | + * | |
837 | + * Returns nothing. | |
838 | + * | |
839 | + * Caller holds ccs_policy_lock mutex. | |
840 | + */ | |
841 | +static void ccs_collect_member(const enum ccs_policy_id id, | |
842 | + struct list_head *member_list) | |
843 | +{ | |
844 | + struct ccs_acl_head *member; | |
845 | + struct ccs_acl_head *tmp; | |
846 | + list_for_each_entry_safe(member, tmp, member_list, list) { | |
847 | + if (!member->is_deleted) | |
848 | + continue; | |
849 | + member->is_deleted = CCS_GC_IN_PROGRESS; | |
850 | + ccs_try_to_gc(id, &member->list); | |
851 | + } | |
852 | +} | |
853 | + | |
854 | +/** | |
855 | + * ccs_collect_acl - Delete elements in "struct ccs_domain_info". | |
856 | + * | |
857 | + * @list: Pointer to "struct list_head". | |
858 | + * | |
859 | + * Returns nothing. | |
860 | + * | |
861 | + * Caller holds ccs_policy_lock mutex. | |
862 | + */ | |
863 | +static void ccs_collect_acl(struct list_head *list) | |
864 | +{ | |
865 | + struct ccs_acl_info *acl; | |
866 | + struct ccs_acl_info *tmp; | |
867 | + list_for_each_entry_safe(acl, tmp, list, list) { | |
868 | + if (!acl->is_deleted) | |
869 | + continue; | |
870 | + acl->is_deleted = CCS_GC_IN_PROGRESS; | |
871 | + ccs_try_to_gc(CCS_ID_ACL, &acl->list); | |
872 | + } | |
873 | +} | |
874 | + | |
875 | +/** | |
876 | + * ccs_collect_entry - Try to kfree() deleted elements. | |
877 | + * | |
878 | + * Returns nothing. | |
879 | + */ | |
880 | +static void ccs_collect_entry(void) | |
881 | +{ | |
882 | + int i; | |
883 | + enum ccs_policy_id id; | |
884 | + struct ccs_policy_namespace *ns; | |
885 | + mutex_lock(&ccs_policy_lock); | |
886 | + { | |
887 | + struct ccs_domain_info *domain; | |
888 | + struct ccs_domain_info *tmp; | |
889 | + list_for_each_entry_safe(domain, tmp, &ccs_domain_list, list) { | |
890 | + ccs_collect_acl(&domain->acl_info_list); | |
891 | + if (!domain->is_deleted || | |
892 | + ccs_domain_used_by_task(domain)) | |
893 | + continue; | |
894 | + ccs_try_to_gc(CCS_ID_DOMAIN, &domain->list); | |
895 | + } | |
896 | + } | |
897 | + list_for_each_entry(ns, &ccs_namespace_list, namespace_list) { | |
898 | + for (id = 0; id < CCS_MAX_POLICY; id++) | |
899 | + ccs_collect_member(id, &ns->policy_list[id]); | |
900 | + for (i = 0; i < CCS_MAX_ACL_GROUPS; i++) | |
901 | + ccs_collect_acl(&ns->acl_group[i]); | |
902 | + } | |
903 | + { | |
904 | + struct ccs_shared_acl_head *ptr; | |
905 | + struct ccs_shared_acl_head *tmp; | |
906 | + list_for_each_entry_safe(ptr, tmp, &ccs_condition_list, list) { | |
907 | + if (atomic_read(&ptr->users) > 0) | |
908 | + continue; | |
909 | + atomic_set(&ptr->users, CCS_GC_IN_PROGRESS); | |
910 | + ccs_try_to_gc(CCS_ID_CONDITION, &ptr->list); | |
911 | + } | |
912 | + } | |
913 | + list_for_each_entry(ns, &ccs_namespace_list, namespace_list) { | |
914 | + for (i = 0; i < CCS_MAX_GROUP; i++) { | |
915 | + struct list_head *list = &ns->group_list[i]; | |
916 | + struct ccs_group *group; | |
917 | + struct ccs_group *tmp; | |
918 | + switch (i) { | |
919 | + case 0: | |
920 | + id = CCS_ID_PATH_GROUP; | |
921 | + break; | |
922 | + case 1: | |
923 | + id = CCS_ID_NUMBER_GROUP; | |
924 | + break; | |
925 | + default: | |
926 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
927 | + id = CCS_ID_ADDRESS_GROUP; | |
928 | +#else | |
929 | + continue; | |
930 | +#endif | |
931 | + break; | |
932 | + } | |
933 | + list_for_each_entry_safe(group, tmp, list, head.list) { | |
934 | + ccs_collect_member(id, &group->member_list); | |
935 | + if (!list_empty(&group->member_list) || | |
936 | + atomic_read(&group->head.users) > 0) | |
937 | + continue; | |
938 | + atomic_set(&group->head.users, | |
939 | + CCS_GC_IN_PROGRESS); | |
940 | + ccs_try_to_gc(CCS_ID_GROUP, &group->head.list); | |
941 | + } | |
942 | + } | |
943 | + } | |
944 | + for (i = 0; i < CCS_MAX_HASH; i++) { | |
945 | + struct list_head *list = &ccs_name_list[i]; | |
946 | + struct ccs_shared_acl_head *ptr; | |
947 | + struct ccs_shared_acl_head *tmp; | |
948 | + list_for_each_entry_safe(ptr, tmp, list, list) { | |
949 | + if (atomic_read(&ptr->users) > 0) | |
950 | + continue; | |
951 | + atomic_set(&ptr->users, CCS_GC_IN_PROGRESS); | |
952 | + ccs_try_to_gc(CCS_ID_NAME, &ptr->list); | |
953 | + } | |
954 | + } | |
955 | + mutex_unlock(&ccs_policy_lock); | |
956 | +} | |
957 | + | |
958 | +/** | |
959 | + * ccs_gc_thread - Garbage collector thread function. | |
960 | + * | |
961 | + * @unused: Unused. | |
962 | + * | |
963 | + * Returns 0. | |
964 | + */ | |
965 | +static int ccs_gc_thread(void *unused) | |
966 | +{ | |
967 | + /* Garbage collector thread is exclusive. */ | |
968 | + static DEFINE_MUTEX(ccs_gc_mutex); | |
969 | + if (!mutex_trylock(&ccs_gc_mutex)) | |
970 | + goto out; | |
971 | +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6) | |
972 | + /* daemonize() not needed. */ | |
973 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) | |
974 | + daemonize("GC for CCS"); | |
975 | +#else | |
976 | + daemonize(); | |
977 | + reparent_to_init(); | |
978 | +#if defined(TASK_DEAD) | |
979 | + { | |
980 | + struct task_struct *task = current; | |
981 | + spin_lock_irq(&task->sighand->siglock); | |
982 | + siginitsetinv(&task->blocked, 0); | |
983 | + recalc_sigpending(); | |
984 | + spin_unlock_irq(&task->sighand->siglock); | |
985 | + } | |
986 | +#else | |
987 | + { | |
988 | + struct task_struct *task = current; | |
989 | + spin_lock_irq(&task->sigmask_lock); | |
990 | + siginitsetinv(&task->blocked, 0); | |
991 | + recalc_sigpending(task); | |
992 | + spin_unlock_irq(&task->sigmask_lock); | |
993 | + } | |
994 | +#endif | |
995 | + snprintf(current->comm, sizeof(current->comm) - 1, "GC for CCS"); | |
996 | +#endif | |
997 | + ccs_collect_entry(); | |
998 | + { | |
999 | + struct ccs_io_buffer *head; | |
1000 | + struct ccs_io_buffer *tmp; | |
1001 | + spin_lock(&ccs_io_buffer_list_lock); | |
1002 | + list_for_each_entry_safe(head, tmp, &ccs_io_buffer_list, | |
1003 | + list) { | |
1004 | + if (head->users) | |
1005 | + continue; | |
1006 | + list_del(&head->list); | |
1007 | + kfree(head->read_buf); | |
1008 | + kfree(head->write_buf); | |
1009 | + kfree(head); | |
1010 | + } | |
1011 | + spin_unlock(&ccs_io_buffer_list_lock); | |
1012 | + } | |
1013 | + mutex_unlock(&ccs_gc_mutex); | |
1014 | +out: | |
1015 | + /* This acts as do_exit(0). */ | |
1016 | + return 0; | |
1017 | +} | |
1018 | + | |
1019 | +/** | |
1020 | + * ccs_notify_gc - Register/unregister /proc/ccs/ users. | |
1021 | + * | |
1022 | + * @head: Pointer to "struct ccs_io_buffer". | |
1023 | + * @is_register: True if register, false if unregister. | |
1024 | + * | |
1025 | + * Returns nothing. | |
1026 | + */ | |
1027 | +void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register) | |
1028 | +{ | |
1029 | + bool is_write = false; | |
1030 | + spin_lock(&ccs_io_buffer_list_lock); | |
1031 | + if (is_register) { | |
1032 | + head->users = 1; | |
1033 | + list_add(&head->list, &ccs_io_buffer_list); | |
1034 | + } else { | |
1035 | + is_write = head->write_buf != NULL; | |
1036 | + if (!--head->users) { | |
1037 | + list_del(&head->list); | |
1038 | + kfree(head->read_buf); | |
1039 | + kfree(head->write_buf); | |
1040 | + kfree(head); | |
1041 | + } | |
1042 | + } | |
1043 | + spin_unlock(&ccs_io_buffer_list_lock); | |
1044 | + if (is_write) { | |
1045 | +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6) | |
1046 | + struct task_struct *task = kthread_create(ccs_gc_thread, NULL, | |
1047 | + "GC for CCS"); | |
1048 | + if (!IS_ERR(task)) | |
1049 | + wake_up_process(task); | |
1050 | +#else | |
1051 | + kernel_thread(ccs_gc_thread, NULL, 0); | |
1052 | +#endif | |
1053 | + } | |
1054 | +} |
@@ -0,0 +1,1393 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | + | |
14 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
15 | +extern struct ccs_security ccs_oom_security; | |
16 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
17 | +extern struct ccs_security ccs_default_security; | |
18 | +/* Dummy marker for calling security_bprm_free(). */ | |
19 | +static const unsigned long ccs_bprm_security; | |
20 | + | |
21 | +/* For exporting variables and functions. */ | |
22 | +struct ccsecurity_exports ccsecurity_exports; | |
23 | +/* Members are updated by loadable kernel module. */ | |
24 | +struct ccsecurity_operations ccsecurity_ops; | |
25 | + | |
26 | +/* Function pointers originally registered by register_security(). */ | |
27 | +static struct security_operations original_security_ops /* = *security_ops; */; | |
28 | + | |
29 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
30 | + | |
31 | +/** | |
32 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
33 | + * | |
34 | + * @count: Count to increment or decrement. | |
35 | + * | |
36 | + * Returns updated counter. | |
37 | + */ | |
38 | +static unsigned int ccs_update_ee_counter(int count) | |
39 | +{ | |
40 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10) || defined(atomic_add_return) | |
41 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
42 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
43 | + return atomic_add_return(count, &ccs_ee_counter); | |
44 | +#else | |
45 | + static DEFINE_SPINLOCK(ccs_ee_lock); | |
46 | + static unsigned int ccs_ee_counter; | |
47 | + unsigned long flags; | |
48 | + spin_lock_irqsave(&ccs_ee_lock, flags); | |
49 | + ccs_ee_counter += count; | |
50 | + count = ccs_ee_counter; | |
51 | + spin_unlock_irqrestore(&ccs_ee_lock, flags); | |
52 | + return count; | |
53 | +#endif | |
54 | +} | |
55 | + | |
56 | +/** | |
57 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
58 | + * | |
59 | + * @ee: Pointer to "struct ccs_execve". | |
60 | + * | |
61 | + * Returns nothing. | |
62 | + */ | |
63 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
64 | +{ | |
65 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
66 | + current->pid, ccs_update_ee_counter(1) - 1); | |
67 | +} | |
68 | + | |
69 | +/** | |
70 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
71 | + * | |
72 | + * @ee: Pointer to "struct ccs_execve". | |
73 | + * @task: True if released by current task, false otherwise. | |
74 | + * | |
75 | + * Returns nothing. | |
76 | + */ | |
77 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
78 | + const bool is_current) | |
79 | +{ | |
80 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
81 | + if (is_current) | |
82 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
83 | + ee, current->pid, tmp); | |
84 | + else | |
85 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
86 | + ee, tmp); | |
87 | +} | |
88 | + | |
89 | +#endif | |
90 | + | |
91 | +#if !defined(CONFIG_AKARI_DEBUG) | |
92 | +#define ccs_debug_trace(pos) do { } while (0) | |
93 | +#else | |
94 | +#define ccs_debug_trace(pos) \ | |
95 | + do { \ | |
96 | + static bool done; \ | |
97 | + if (!done) { \ | |
98 | + printk(KERN_INFO \ | |
99 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
100 | + done = true; \ | |
101 | + } \ | |
102 | + } while (0) | |
103 | +#endif | |
104 | + | |
105 | +/** | |
106 | + * ccs_clear_execve - Release memory used by do_execve(). | |
107 | + * | |
108 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
109 | + * @security: Pointer to "struct ccs_security". | |
110 | + * | |
111 | + * Returns nothing. | |
112 | + */ | |
113 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
114 | +{ | |
115 | + struct ccs_execve *ee; | |
116 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
117 | + return; | |
118 | + ee = security->ee; | |
119 | + security->ee = NULL; | |
120 | + if (!ee) | |
121 | + return; | |
122 | + ccs_finish_execve(ret, ee); | |
123 | +} | |
124 | + | |
125 | +/** | |
126 | + * ccs_task_alloc_security - Allocate memory for new tasks. | |
127 | + * | |
128 | + * @p: Pointer to "struct task_struct". | |
129 | + * | |
130 | + * Returns 0 on success, negative value otherwise. | |
131 | + */ | |
132 | +static int ccs_task_alloc_security(struct task_struct *p) | |
133 | +{ | |
134 | + int rc = ccs_alloc_task_security(p); | |
135 | + if (rc) | |
136 | + return rc; | |
137 | + while (!original_security_ops.task_alloc_security); | |
138 | + rc = original_security_ops.task_alloc_security(p); | |
139 | + if (rc) | |
140 | + ccs_free_task_security(p); | |
141 | + return rc; | |
142 | +} | |
143 | + | |
144 | +/** | |
145 | + * ccs_task_free_security - Release memory for "struct task_struct". | |
146 | + * | |
147 | + * @p: Pointer to "struct task_struct". | |
148 | + * | |
149 | + * Returns nothing. | |
150 | + */ | |
151 | +static void ccs_task_free_security(struct task_struct *p) | |
152 | +{ | |
153 | + while (!original_security_ops.task_free_security); | |
154 | + original_security_ops.task_free_security(p); | |
155 | + ccs_free_task_security(p); | |
156 | +} | |
157 | + | |
158 | +/** | |
159 | + * ccs_bprm_alloc_security - Allocate memory for "struct linux_binprm". | |
160 | + * | |
161 | + * @bprm: Pointer to "struct linux_binprm". | |
162 | + * | |
163 | + * Returns 0 on success, negative value otherwise. | |
164 | + */ | |
165 | +static int ccs_bprm_alloc_security(struct linux_binprm *bprm) | |
166 | +{ | |
167 | + int rc; | |
168 | + while (!original_security_ops.bprm_alloc_security); | |
169 | + rc = original_security_ops.bprm_alloc_security(bprm); | |
170 | + if (bprm->security || rc) | |
171 | + return rc; | |
172 | + /* | |
173 | + * Update bprm->security to &ccs_bprm_security so that | |
174 | + * security_bprm_free() is called even if do_execve() failed at | |
175 | + * search_binary_handler() without allocating memory at | |
176 | + * security_bprm_alloc(). This trick assumes that active LSM module | |
177 | + * does not access bprm->security if that module did not allocate | |
178 | + * memory at security_bprm_alloc(). | |
179 | + */ | |
180 | + bprm->security = (void *) &ccs_bprm_security; | |
181 | + return 0; | |
182 | +} | |
183 | + | |
184 | +/** | |
185 | + * ccs_bprm_free_security - Release memory for "struct linux_binprm". | |
186 | + * | |
187 | + * @bprm: Pointer to "struct linux_binprm". | |
188 | + * | |
189 | + * Returns nothing. | |
190 | + */ | |
191 | +static void ccs_bprm_free_security(struct linux_binprm *bprm) | |
192 | +{ | |
193 | + /* | |
194 | + * If do_execve() succeeded, bprm->security will be updated to NULL at | |
195 | + * security_bprm_compute_creds()/security_bprm_apply_creds() if | |
196 | + * bprm->security was set to &ccs_bprm_security at | |
197 | + * security_bprm_alloc(). | |
198 | + * | |
199 | + * If do_execve() failed, bprm->security remains at &ccs_bprm_security | |
200 | + * if bprm->security was set to &ccs_bprm_security at | |
201 | + * security_bprm_alloc(). | |
202 | + * | |
203 | + * And do_execve() does not call security_bprm_free() if do_execve() | |
204 | + * failed and bprm->security == NULL. Therefore, do not call | |
205 | + * original_security_ops.bprm_free_security() if bprm->security remains | |
206 | + * at &ccs_bprm_security . | |
207 | + */ | |
208 | + if (bprm->security != &ccs_bprm_security) { | |
209 | + while (!original_security_ops.bprm_free_security); | |
210 | + original_security_ops.bprm_free_security(bprm); | |
211 | + } | |
212 | + /* | |
213 | + * If do_execve() succeeded, | |
214 | + * ccs_clear_execve(0, ccs_current_security()); | |
215 | + * is called before calling below one. | |
216 | + * Thus, below call becomes no-op if do_execve() succeeded. | |
217 | + */ | |
218 | + ccs_clear_execve(-1, ccs_current_security()); | |
219 | +} | |
220 | + | |
221 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
222 | + | |
223 | +/** | |
224 | + * ccs_bprm_compute_creds - A hook which is called when do_execve() succeeded. | |
225 | + * | |
226 | + * @bprm: Pointer to "struct linux_binprm". | |
227 | + * | |
228 | + * Returns nothing. | |
229 | + */ | |
230 | +static void ccs_bprm_compute_creds(struct linux_binprm *bprm) | |
231 | +{ | |
232 | + if (bprm->security == &ccs_bprm_security) | |
233 | + bprm->security = NULL; | |
234 | + while (!original_security_ops.bprm_compute_creds); | |
235 | + original_security_ops.bprm_compute_creds(bprm); | |
236 | + ccs_clear_execve(0, ccs_current_security()); | |
237 | +} | |
238 | + | |
239 | +#else | |
240 | + | |
241 | +/** | |
242 | + * ccs_bprm_apply_creds - A hook which is called when do_execve() succeeded. | |
243 | + * | |
244 | + * @bprm: Pointer to "struct linux_binprm". | |
245 | + * @unsafe: Unsafe flag. | |
246 | + * | |
247 | + * Returns nothing. | |
248 | + */ | |
249 | +static void ccs_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) | |
250 | +{ | |
251 | + if (bprm->security == &ccs_bprm_security) | |
252 | + bprm->security = NULL; | |
253 | + while (!original_security_ops.bprm_apply_creds); | |
254 | + original_security_ops.bprm_apply_creds(bprm, unsafe); | |
255 | + ccs_clear_execve(0, ccs_current_security()); | |
256 | +} | |
257 | + | |
258 | +#endif | |
259 | + | |
260 | +/** | |
261 | + * ccs_bprm_check_security - Check permission for execve(). | |
262 | + * | |
263 | + * @bprm: Pointer to "struct linux_binprm". | |
264 | + * | |
265 | + * Returns 0 on success, negative value otherwise. | |
266 | + */ | |
267 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
268 | +{ | |
269 | + struct ccs_security *security = ccs_current_security(); | |
270 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
271 | + return -ENOMEM; | |
272 | + if (!security->ee) { | |
273 | + int rc; | |
274 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
275 | + if (!ccs_policy_loaded) | |
276 | + ccs_load_policy(bprm->filename); | |
277 | +#endif | |
278 | + rc = ccs_start_execve(bprm, &security->ee); | |
279 | + if (rc) | |
280 | + return rc; | |
281 | + } | |
282 | + while (!original_security_ops.bprm_check_security); | |
283 | + return original_security_ops.bprm_check_security(bprm); | |
284 | +} | |
285 | + | |
286 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
287 | + | |
288 | +/** | |
289 | + * ccs_open - Check permission for open(). | |
290 | + * | |
291 | + * @f: Pointer to "struct file". | |
292 | + * | |
293 | + * Returns 0 on success, negative value otherwise. | |
294 | + */ | |
295 | +static int ccs_open(struct file *f) | |
296 | +{ | |
297 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
298 | + f->f_flags + 1); | |
299 | +} | |
300 | + | |
301 | +#endif | |
302 | + | |
303 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
304 | + | |
305 | +/** | |
306 | + * ccs_dentry_open - Check permission for open(). | |
307 | + * | |
308 | + * @f: Pointer to "struct file". | |
309 | + * | |
310 | + * Returns 0 on success, negative value otherwise. | |
311 | + */ | |
312 | +static int ccs_dentry_open(struct file *f) | |
313 | +{ | |
314 | + int rc = ccs_open(f); | |
315 | + if (rc) | |
316 | + return rc; | |
317 | + while (!original_security_ops.dentry_open); | |
318 | + return original_security_ops.dentry_open(f); | |
319 | +} | |
320 | + | |
321 | +#else | |
322 | + | |
323 | +/** | |
324 | + * ccs_open - Check permission for open(). | |
325 | + * | |
326 | + * @inode: Pointer to "struct inode". | |
327 | + * @mask: Open mode. | |
328 | + * @nd: Pointer to "struct nameidata". | |
329 | + * | |
330 | + * Returns 0 on success, negative value otherwise. | |
331 | + */ | |
332 | +static int ccs_open(struct inode *inode, int mask, struct nameidata *nd) | |
333 | +{ | |
334 | + int flags; | |
335 | + if (!nd || !nd->dentry) | |
336 | + return 0; | |
337 | + /* open_exec() passes MAY_EXEC . */ | |
338 | + if (mask == MAY_EXEC && inode && S_ISREG(inode->i_mode) && | |
339 | + (ccs_current_flags() & CCS_TASK_IS_IN_EXECVE)) | |
340 | + mask = MAY_READ; | |
341 | + /* | |
342 | + * This flags value is passed to ACC_MODE(). | |
343 | + * ccs_open_permission() for older versions uses old ACC_MODE(). | |
344 | + */ | |
345 | + switch (mask & (MAY_READ | MAY_WRITE)) { | |
346 | + case MAY_READ: | |
347 | + flags = 01; | |
348 | + break; | |
349 | + case MAY_WRITE: | |
350 | + flags = 02; | |
351 | + break; | |
352 | + case MAY_READ | MAY_WRITE: | |
353 | + flags = 03; | |
354 | + break; | |
355 | + default: | |
356 | + return 0; | |
357 | + } | |
358 | + return ccs_open_permission(nd->dentry, nd->mnt, flags); | |
359 | +} | |
360 | + | |
361 | +/** | |
362 | + * ccs_inode_permission - Check permission for open(). | |
363 | + * | |
364 | + * @inode: Pointer to "struct inode". | |
365 | + * @mask: Open mode. | |
366 | + * @nd: Pointer to "struct nameidata". | |
367 | + * | |
368 | + * Returns 0 on success, negative value otherwise. | |
369 | + * | |
370 | + * Note that this hook is called from permission(), and may not be called for | |
371 | + * open(). Maybe it is better to use security_file_permission(). | |
372 | + */ | |
373 | +static int ccs_inode_permission(struct inode *inode, int mask, | |
374 | + struct nameidata *nd) | |
375 | +{ | |
376 | + int rc = ccs_open(inode, mask, nd); | |
377 | + if (rc) | |
378 | + return rc; | |
379 | + while (!original_security_ops.inode_permission); | |
380 | + return original_security_ops.inode_permission(inode, mask, nd); | |
381 | +} | |
382 | + | |
383 | +#endif | |
384 | + | |
385 | +/** | |
386 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
387 | + * | |
388 | + * @dentry: Pointer to "struct dentry". | |
389 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
390 | + * @attr: Pointer to "struct iattr". | |
391 | + * | |
392 | + * Returns 0 on success, negative value otherwise. | |
393 | + */ | |
394 | +static int ccs_inode_setattr(struct dentry *dentry, struct vfsmount *mnt, | |
395 | + struct iattr *attr) | |
396 | +{ | |
397 | + int rc = 0; | |
398 | + if (attr->ia_valid & ATTR_UID) | |
399 | + rc = ccs_chown_permission(dentry, mnt, attr->ia_uid, -1); | |
400 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
401 | + rc = ccs_chown_permission(dentry, mnt, -1, attr->ia_gid); | |
402 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
403 | + rc = ccs_chmod_permission(dentry, mnt, attr->ia_mode); | |
404 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
405 | + rc = ccs_truncate_permission(dentry, mnt); | |
406 | + if (rc) | |
407 | + return rc; | |
408 | + while (!original_security_ops.inode_setattr); | |
409 | + return original_security_ops.inode_setattr(dentry, mnt, attr); | |
410 | +} | |
411 | + | |
412 | +/** | |
413 | + * ccs_inode_getattr - Check permission for stat(). | |
414 | + * | |
415 | + * @mnt: Pointer to "struct vfsmount". | |
416 | + * @dentry: Pointer to "struct dentry". | |
417 | + * | |
418 | + * Returns 0 on success, negative value otherwise. | |
419 | + */ | |
420 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
421 | +{ | |
422 | + int rc = ccs_getattr_permission(mnt, dentry); | |
423 | + if (rc) | |
424 | + return rc; | |
425 | + while (!original_security_ops.inode_getattr); | |
426 | + return original_security_ops.inode_getattr(mnt, dentry); | |
427 | +} | |
428 | + | |
429 | +/** | |
430 | + * ccs_inode_mknod - Check permission for mknod(). | |
431 | + * | |
432 | + * @dir: Pointer to "struct inode". | |
433 | + * @dentry: Pointer to "struct dentry". | |
434 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
435 | + * @mode: Create mode. | |
436 | + * @dev: Device major/minor number. | |
437 | + * | |
438 | + * Returns 0 on success, negative value otherwise. | |
439 | + */ | |
440 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
441 | + struct vfsmount *mnt, int mode, dev_t dev) | |
442 | +{ | |
443 | + int rc = ccs_mknod_permission(dentry, mnt, mode, dev); | |
444 | + if (rc) | |
445 | + return rc; | |
446 | + while (!original_security_ops.inode_mknod); | |
447 | + return original_security_ops.inode_mknod(dir, dentry, mnt, mode, dev); | |
448 | +} | |
449 | + | |
450 | +/** | |
451 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
452 | + * | |
453 | + * @dir: Pointer to "struct inode". | |
454 | + * @dentry: Pointer to "struct dentry". | |
455 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
456 | + * @mode: Create mode. | |
457 | + * | |
458 | + * Returns 0 on success, negative value otherwise. | |
459 | + */ | |
460 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
461 | + struct vfsmount *mnt, int mode) | |
462 | +{ | |
463 | + int rc = ccs_mkdir_permission(dentry, mnt, mode); | |
464 | + if (rc) | |
465 | + return rc; | |
466 | + while (!original_security_ops.inode_mkdir); | |
467 | + return original_security_ops.inode_mkdir(dir, dentry, mnt, mode); | |
468 | +} | |
469 | + | |
470 | +/** | |
471 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
472 | + * | |
473 | + * @dir: Pointer to "struct inode". | |
474 | + * @dentry: Pointer to "struct dentry". | |
475 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
476 | + * | |
477 | + * Returns 0 on success, negative value otherwise. | |
478 | + */ | |
479 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry, | |
480 | + struct vfsmount *mnt) | |
481 | +{ | |
482 | + int rc = ccs_rmdir_permission(dentry, mnt); | |
483 | + if (rc) | |
484 | + return rc; | |
485 | + while (!original_security_ops.inode_rmdir); | |
486 | + return original_security_ops.inode_rmdir(dir, dentry, mnt); | |
487 | +} | |
488 | + | |
489 | +/** | |
490 | + * ccs_inode_unlink - Check permission for unlink(). | |
491 | + * | |
492 | + * @dir: Pointer to "struct inode". | |
493 | + * @dentry: Pointer to "struct dentry". | |
494 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
495 | + * | |
496 | + * Returns 0 on success, negative value otherwise. | |
497 | + */ | |
498 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry, | |
499 | + struct vfsmount *mnt) | |
500 | +{ | |
501 | + int rc = ccs_unlink_permission(dentry, mnt); | |
502 | + if (rc) | |
503 | + return rc; | |
504 | + while (!original_security_ops.inode_unlink); | |
505 | + return original_security_ops.inode_unlink(dir, dentry, mnt); | |
506 | +} | |
507 | + | |
508 | +/** | |
509 | + * ccs_inode_symlink - Check permission for symlink(). | |
510 | + * | |
511 | + * @dir: Pointer to "struct inode". | |
512 | + * @dentry: Pointer to "struct dentry". | |
513 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
514 | + * @old_name: Content of symbolic link. | |
515 | + * | |
516 | + * Returns 0 on success, negative value otherwise. | |
517 | + */ | |
518 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
519 | + struct vfsmount *mnt, const char *old_name) | |
520 | +{ | |
521 | + int rc = ccs_symlink_permission(dentry, mnt, old_name); | |
522 | + if (rc) | |
523 | + return rc; | |
524 | + while (!original_security_ops.inode_symlink); | |
525 | + return original_security_ops.inode_symlink(dir, dentry, mnt, old_name); | |
526 | +} | |
527 | + | |
528 | +/** | |
529 | + * ccs_inode_rename - Check permission for rename(). | |
530 | + * | |
531 | + * @old_dir: Pointer to "struct inode". | |
532 | + * @old_dentry: Pointer to "struct dentry". | |
533 | + * @old_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
534 | + * @new_dir: Pointer to "struct inode". | |
535 | + * @new_dentry: Pointer to "struct dentry". | |
536 | + * @new_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
537 | + * | |
538 | + * Returns 0 on success, negative value otherwise. | |
539 | + */ | |
540 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
541 | + struct vfsmount *old_mnt, struct inode *new_dir, | |
542 | + struct dentry *new_dentry, | |
543 | + struct vfsmount *new_mnt) | |
544 | +{ | |
545 | + int rc = ccs_rename_permission(old_dentry, new_dentry, new_mnt); | |
546 | + if (rc) | |
547 | + return rc; | |
548 | + while (!original_security_ops.inode_rename); | |
549 | + return original_security_ops.inode_rename(old_dir, old_dentry, old_mnt, | |
550 | + new_dir, new_dentry, | |
551 | + new_mnt); | |
552 | +} | |
553 | + | |
554 | +/** | |
555 | + * ccs_inode_link - Check permission for link(). | |
556 | + * | |
557 | + * @old_dentry: Pointer to "struct dentry". | |
558 | + * @old_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
559 | + * @dir: Pointer to "struct inode". | |
560 | + * @new_dentry: Pointer to "struct dentry". | |
561 | + * @new_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
562 | + * | |
563 | + * Returns 0 on success, negative value otherwise. | |
564 | + */ | |
565 | +static int ccs_inode_link(struct dentry *old_dentry, struct vfsmount *old_mnt, | |
566 | + struct inode *dir, struct dentry *new_dentry, | |
567 | + struct vfsmount *new_mnt) | |
568 | +{ | |
569 | + int rc = ccs_link_permission(old_dentry, new_dentry, new_mnt); | |
570 | + if (rc) | |
571 | + return rc; | |
572 | + while (!original_security_ops.inode_link); | |
573 | + return original_security_ops.inode_link(old_dentry, old_mnt, dir, | |
574 | + new_dentry, new_mnt); | |
575 | +} | |
576 | + | |
577 | +/** | |
578 | + * ccs_inode_create - Check permission for creat(). | |
579 | + * | |
580 | + * @dir: Pointer to "struct inode". | |
581 | + * @dentry: Pointer to "struct dentry". | |
582 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
583 | + * @mode: Create mode. | |
584 | + * | |
585 | + * Returns 0 on success, negative value otherwise. | |
586 | + */ | |
587 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
588 | + struct vfsmount *mnt, int mode) | |
589 | +{ | |
590 | + int rc = ccs_mknod_permission(dentry, mnt, mode, 0); | |
591 | + if (rc) | |
592 | + return rc; | |
593 | + while (!original_security_ops.inode_create); | |
594 | + return original_security_ops.inode_create(dir, dentry, mnt, mode); | |
595 | +} | |
596 | + | |
597 | +#ifdef CONFIG_SECURITY_NETWORK | |
598 | + | |
599 | +#include <net/sock.h> | |
600 | + | |
601 | +/* Structure for remembering an accept()ed socket's status. */ | |
602 | +struct ccs_socket_tag { | |
603 | + struct list_head list; | |
604 | + struct inode *inode; | |
605 | + int status; | |
606 | + struct rcu_head rcu; | |
607 | +}; | |
608 | + | |
609 | +/* | |
610 | + * List for managing accept()ed sockets. | |
611 | + * Since we don't need to keep an accept()ed socket into this list after | |
612 | + * once the permission was granted, the number of entries in this list is | |
613 | + * likely small. Therefore, we don't use hash tables. | |
614 | + */ | |
615 | +static LIST_HEAD(ccs_accepted_socket_list); | |
616 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
617 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
618 | + | |
619 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
620 | + | |
621 | +/** | |
622 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
623 | + * | |
624 | + * @rcu: Pointer to "struct rcu_head". | |
625 | + * | |
626 | + * Returns nothing. | |
627 | + */ | |
628 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
629 | +{ | |
630 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
631 | + kfree(ptr); | |
632 | +} | |
633 | + | |
634 | +#else | |
635 | + | |
636 | +/** | |
637 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
638 | + * | |
639 | + * @arg: Pointer to "void". | |
640 | + * | |
641 | + * Returns nothing. | |
642 | + */ | |
643 | +static void ccs_socket_rcu_free(void *arg) | |
644 | +{ | |
645 | + struct ccs_socket_tag *ptr = arg; | |
646 | + kfree(ptr); | |
647 | +} | |
648 | + | |
649 | +#endif | |
650 | + | |
651 | +/** | |
652 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
653 | + * | |
654 | + * @inode: Pointer to "struct inode". | |
655 | + * @status: New status. | |
656 | + * | |
657 | + * Returns nothing. | |
658 | + * | |
659 | + * If @status == 0, memory for that socket will be released after RCU grace | |
660 | + * period. | |
661 | + */ | |
662 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
663 | +{ | |
664 | + struct ccs_socket_tag *ptr; | |
665 | + /* | |
666 | + * Protect whole section because multiple threads may call this | |
667 | + * function with same "sock" via ccs_validate_socket(). | |
668 | + */ | |
669 | + spin_lock(&ccs_accepted_socket_list_lock); | |
670 | + rcu_read_lock(); | |
671 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
672 | + if (ptr->inode != inode) | |
673 | + continue; | |
674 | + ptr->status = status; | |
675 | + if (status) | |
676 | + break; | |
677 | + list_del_rcu(&ptr->list); | |
678 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
679 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
680 | +#else | |
681 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free, ptr); | |
682 | +#endif | |
683 | + break; | |
684 | + } | |
685 | + rcu_read_unlock(); | |
686 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
687 | +} | |
688 | + | |
689 | +/** | |
690 | + * ccs_validate_socket - Check post accept() permission if needed. | |
691 | + * | |
692 | + * @sock: Pointer to "struct socket". | |
693 | + * | |
694 | + * Returns 0 on success, negative value otherwise. | |
695 | + */ | |
696 | +static int ccs_validate_socket(struct socket *sock) | |
697 | +{ | |
698 | + struct inode *inode = SOCK_INODE(sock); | |
699 | + struct ccs_socket_tag *ptr; | |
700 | + int ret = 0; | |
701 | + rcu_read_lock(); | |
702 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
703 | + if (ptr->inode != inode) | |
704 | + continue; | |
705 | + ret = ptr->status; | |
706 | + break; | |
707 | + } | |
708 | + rcu_read_unlock(); | |
709 | + if (ret <= 0) | |
710 | + /* | |
711 | + * This socket is not an accept()ed socket or this socket is | |
712 | + * an accept()ed socket and post accept() permission is done. | |
713 | + */ | |
714 | + return ret; | |
715 | + /* | |
716 | + * Check post accept() permission now. | |
717 | + * | |
718 | + * Strictly speaking, we need to pass both listen()ing socket and | |
719 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
720 | + * But since socket's family and type are same for both sockets, | |
721 | + * passing the accept()ed socket in place for the listen()ing socket | |
722 | + * will work. | |
723 | + */ | |
724 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
725 | + /* | |
726 | + * If permission was granted, we forget that this is an accept()ed | |
727 | + * socket. Otherwise, we remember that this socket needs to return | |
728 | + * error for subsequent socketcalls. | |
729 | + */ | |
730 | + ccs_update_socket_tag(inode, ret); | |
731 | + return ret; | |
732 | +} | |
733 | + | |
734 | +/** | |
735 | + * ccs_socket_accept - Check permission for accept(). | |
736 | + * | |
737 | + * @sock: Pointer to "struct socket". | |
738 | + * @newsock: Pointer to "struct socket". | |
739 | + * | |
740 | + * Returns 0 on success, negative value otherwise. | |
741 | + * | |
742 | + * This hook is used for setting up environment for doing post accept() | |
743 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
744 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
745 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
746 | + * in order to do post accept() permission check before returning to userspace. | |
747 | + * If we make the copy in security_socket_post_create(), it would be possible | |
748 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
749 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
750 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
751 | + * rather than between sock->ops->accept() and returning to userspace. | |
752 | + * This means that if a socket was close()d before calling some socket | |
753 | + * syscalls, post accept() permission check will not be done. | |
754 | + */ | |
755 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
756 | +{ | |
757 | + struct ccs_socket_tag *ptr; | |
758 | + int rc = ccs_validate_socket(sock); | |
759 | + if (rc < 0) | |
760 | + return rc; | |
761 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
762 | + if (!ptr) | |
763 | + return -ENOMEM; | |
764 | + while (!original_security_ops.socket_accept); | |
765 | + rc = original_security_ops.socket_accept(sock, newsock); | |
766 | + if (rc) { | |
767 | + kfree(ptr); | |
768 | + return rc; | |
769 | + } | |
770 | + /* | |
771 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
772 | + * "newsock" as "an accept()ed socket but post accept() permission | |
773 | + * check is not done yet" by allocating memory using inode of the | |
774 | + * "newsock" as a search key. | |
775 | + */ | |
776 | + ptr->inode = SOCK_INODE(newsock); | |
777 | + ptr->status = 1; /* Check post accept() permission later. */ | |
778 | + spin_lock(&ccs_accepted_socket_list_lock); | |
779 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
780 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
781 | + return 0; | |
782 | +} | |
783 | + | |
784 | +/** | |
785 | + * ccs_socket_listen - Check permission for listen(). | |
786 | + * | |
787 | + * @sock: Pointer to "struct socket". | |
788 | + * @backlog: Backlog parameter. | |
789 | + * | |
790 | + * Returns 0 on success, negative value otherwise. | |
791 | + */ | |
792 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
793 | +{ | |
794 | + int rc = ccs_validate_socket(sock); | |
795 | + if (rc < 0) | |
796 | + return rc; | |
797 | + rc = ccs_socket_listen_permission(sock); | |
798 | + if (rc) | |
799 | + return rc; | |
800 | + while (!original_security_ops.socket_listen); | |
801 | + return original_security_ops.socket_listen(sock, backlog); | |
802 | +} | |
803 | + | |
804 | +/** | |
805 | + * ccs_socket_connect - Check permission for connect(). | |
806 | + * | |
807 | + * @sock: Pointer to "struct socket". | |
808 | + * @addr: Pointer to "struct sockaddr". | |
809 | + * @addr_len: Size of @addr. | |
810 | + * | |
811 | + * Returns 0 on success, negative value otherwise. | |
812 | + */ | |
813 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
814 | + int addr_len) | |
815 | +{ | |
816 | + int rc = ccs_validate_socket(sock); | |
817 | + if (rc < 0) | |
818 | + return rc; | |
819 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
820 | + if (rc) | |
821 | + return rc; | |
822 | + while (!original_security_ops.socket_connect); | |
823 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
824 | +} | |
825 | + | |
826 | +/** | |
827 | + * ccs_socket_bind - Check permission for bind(). | |
828 | + * | |
829 | + * @sock: Pointer to "struct socket". | |
830 | + * @addr: Pointer to "struct sockaddr". | |
831 | + * @addr_len: Size of @addr. | |
832 | + * | |
833 | + * Returns 0 on success, negative value otherwise. | |
834 | + */ | |
835 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
836 | + int addr_len) | |
837 | +{ | |
838 | + int rc = ccs_validate_socket(sock); | |
839 | + if (rc < 0) | |
840 | + return rc; | |
841 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
842 | + if (rc) | |
843 | + return rc; | |
844 | + while (!original_security_ops.socket_bind); | |
845 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
846 | +} | |
847 | + | |
848 | +/** | |
849 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
850 | + * | |
851 | + * @sock: Pointer to "struct socket". | |
852 | + * @msg: Pointer to "struct msghdr". | |
853 | + * @size: Size of message. | |
854 | + * | |
855 | + * Returns 0 on success, negative value otherwise. | |
856 | + */ | |
857 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
858 | + int size) | |
859 | +{ | |
860 | + int rc = ccs_validate_socket(sock); | |
861 | + if (rc < 0) | |
862 | + return rc; | |
863 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
864 | + if (rc) | |
865 | + return rc; | |
866 | + while (!original_security_ops.socket_sendmsg); | |
867 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
868 | +} | |
869 | + | |
870 | +/** | |
871 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
872 | + * | |
873 | + * @sock: Pointer to "struct socket". | |
874 | + * @msg: Pointer to "struct msghdr". | |
875 | + * @size: Size of message. | |
876 | + * @flags: Flags. | |
877 | + * | |
878 | + * Returns 0 on success, negative value otherwise. | |
879 | + */ | |
880 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
881 | + int size, int flags) | |
882 | +{ | |
883 | + int rc = ccs_validate_socket(sock); | |
884 | + if (rc < 0) | |
885 | + return rc; | |
886 | + while (!original_security_ops.socket_recvmsg); | |
887 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
888 | +} | |
889 | + | |
890 | +/** | |
891 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
892 | + * | |
893 | + * @sock: Pointer to "struct socket". | |
894 | + * | |
895 | + * Returns 0 on success, negative value otherwise. | |
896 | + */ | |
897 | +static int ccs_socket_getsockname(struct socket *sock) | |
898 | +{ | |
899 | + int rc = ccs_validate_socket(sock); | |
900 | + if (rc < 0) | |
901 | + return rc; | |
902 | + while (!original_security_ops.socket_getsockname); | |
903 | + return original_security_ops.socket_getsockname(sock); | |
904 | +} | |
905 | + | |
906 | +/** | |
907 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
908 | + * | |
909 | + * @sock: Pointer to "struct socket". | |
910 | + * | |
911 | + * Returns 0 on success, negative value otherwise. | |
912 | + */ | |
913 | +static int ccs_socket_getpeername(struct socket *sock) | |
914 | +{ | |
915 | + int rc = ccs_validate_socket(sock); | |
916 | + if (rc < 0) | |
917 | + return rc; | |
918 | + while (!original_security_ops.socket_getpeername); | |
919 | + return original_security_ops.socket_getpeername(sock); | |
920 | +} | |
921 | + | |
922 | +/** | |
923 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
924 | + * | |
925 | + * @sock: Pointer to "struct socket". | |
926 | + * @level: Level. | |
927 | + * @optname: Option's name, | |
928 | + * | |
929 | + * Returns 0 on success, negative value otherwise. | |
930 | + */ | |
931 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
932 | +{ | |
933 | + int rc = ccs_validate_socket(sock); | |
934 | + if (rc < 0) | |
935 | + return rc; | |
936 | + while (!original_security_ops.socket_getsockopt); | |
937 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
938 | +} | |
939 | + | |
940 | +/** | |
941 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
942 | + * | |
943 | + * @sock: Pointer to "struct socket". | |
944 | + * @level: Level. | |
945 | + * @optname: Option's name, | |
946 | + * | |
947 | + * Returns 0 on success, negative value otherwise. | |
948 | + */ | |
949 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
950 | +{ | |
951 | + int rc = ccs_validate_socket(sock); | |
952 | + if (rc < 0) | |
953 | + return rc; | |
954 | + while (!original_security_ops.socket_setsockopt); | |
955 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
956 | +} | |
957 | + | |
958 | +/** | |
959 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
960 | + * | |
961 | + * @sock: Pointer to "struct socket". | |
962 | + * @how: Shutdown mode. | |
963 | + * | |
964 | + * Returns 0 on success, negative value otherwise. | |
965 | + */ | |
966 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
967 | +{ | |
968 | + int rc = ccs_validate_socket(sock); | |
969 | + if (rc < 0) | |
970 | + return rc; | |
971 | + while (!original_security_ops.socket_shutdown); | |
972 | + return original_security_ops.socket_shutdown(sock, how); | |
973 | +} | |
974 | + | |
975 | +#define SOCKFS_MAGIC 0x534F434B | |
976 | + | |
977 | +/** | |
978 | + * ccs_inode_free_security - Release memory associated with an inode. | |
979 | + * | |
980 | + * @inode: Pointer to "struct inode". | |
981 | + * | |
982 | + * Returns nothing. | |
983 | + * | |
984 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
985 | + */ | |
986 | +static void ccs_inode_free_security(struct inode *inode) | |
987 | +{ | |
988 | + while (!original_security_ops.inode_free_security); | |
989 | + original_security_ops.inode_free_security(inode); | |
990 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
991 | + ccs_update_socket_tag(inode, 0); | |
992 | +} | |
993 | + | |
994 | +#endif | |
995 | + | |
996 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24) | |
997 | + | |
998 | +/** | |
999 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1000 | + * | |
1001 | + * @old_nd: Pointer to "struct nameidata". | |
1002 | + * @new_nd: Pointer to "struct nameidata". | |
1003 | + * | |
1004 | + * Returns 0 on success, negative value otherwise. | |
1005 | + */ | |
1006 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1007 | +{ | |
1008 | + int rc = ccs_pivot_root_permission(old_nd, new_nd); | |
1009 | + if (rc) | |
1010 | + return rc; | |
1011 | + while (!original_security_ops.sb_pivotroot); | |
1012 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1013 | +} | |
1014 | + | |
1015 | +/** | |
1016 | + * ccs_sb_mount - Check permission for mount(). | |
1017 | + * | |
1018 | + * @dev_name: Name of device file. | |
1019 | + * @nd: Pointer to "struct nameidata". | |
1020 | + * @type: Name of filesystem type. Maybe NULL. | |
1021 | + * @flags: Mount options. | |
1022 | + * @data_page: Optional data. Maybe NULL. | |
1023 | + * | |
1024 | + * Returns 0 on success, negative value otherwise. | |
1025 | + */ | |
1026 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1027 | + unsigned long flags, void *data_page) | |
1028 | +{ | |
1029 | + int rc = ccs_mount_permission(dev_name, nd, type, flags, data_page); | |
1030 | + if (rc) | |
1031 | + return rc; | |
1032 | + while (!original_security_ops.sb_mount); | |
1033 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1034 | + data_page); | |
1035 | +} | |
1036 | + | |
1037 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) | |
1038 | + | |
1039 | +/** | |
1040 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1041 | + * | |
1042 | + * @old_nd: Pointer to "struct nameidata". | |
1043 | + * @new_nd: Pointer to "struct nameidata". | |
1044 | + * | |
1045 | + * Returns 0 on success, negative value otherwise. | |
1046 | + */ | |
1047 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1048 | +{ | |
1049 | + int rc = ccs_pivot_root_permission(&old_nd->path, &new_nd->path); | |
1050 | + if (rc) | |
1051 | + return rc; | |
1052 | + while (!original_security_ops.sb_pivotroot); | |
1053 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1054 | +} | |
1055 | + | |
1056 | +/** | |
1057 | + * ccs_sb_mount - Check permission for mount(). | |
1058 | + * | |
1059 | + * @dev_name: Name of device file. | |
1060 | + * @nd: Pointer to "struct nameidata". | |
1061 | + * @type: Name of filesystem type. Maybe NULL. | |
1062 | + * @flags: Mount options. | |
1063 | + * @data_page: Optional data. Maybe NULL. | |
1064 | + * | |
1065 | + * Returns 0 on success, negative value otherwise. | |
1066 | + */ | |
1067 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1068 | + unsigned long flags, void *data_page) | |
1069 | +{ | |
1070 | + int rc = ccs_mount_permission(dev_name, &nd->path, type, flags, | |
1071 | + data_page); | |
1072 | + if (rc) | |
1073 | + return rc; | |
1074 | + while (!original_security_ops.sb_mount); | |
1075 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1076 | + data_page); | |
1077 | +} | |
1078 | + | |
1079 | +#else | |
1080 | + | |
1081 | +/** | |
1082 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1083 | + * | |
1084 | + * @old_path: Pointer to "struct path". | |
1085 | + * @new_path: Pointer to "struct path". | |
1086 | + * | |
1087 | + * Returns 0 on success, negative value otherwise. | |
1088 | + */ | |
1089 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1090 | +{ | |
1091 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1092 | + if (rc) | |
1093 | + return rc; | |
1094 | + while (!original_security_ops.sb_pivotroot); | |
1095 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1096 | +} | |
1097 | + | |
1098 | +/** | |
1099 | + * ccs_sb_mount - Check permission for mount(). | |
1100 | + * | |
1101 | + * @dev_name: Name of device file. | |
1102 | + * @path: Pointer to "struct path". | |
1103 | + * @type: Name of filesystem type. Maybe NULL. | |
1104 | + * @flags: Mount options. | |
1105 | + * @data_page: Optional data. Maybe NULL. | |
1106 | + * | |
1107 | + * Returns 0 on success, negative value otherwise. | |
1108 | + */ | |
1109 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1110 | + unsigned long flags, void *data_page) | |
1111 | +{ | |
1112 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1113 | + if (rc) | |
1114 | + return rc; | |
1115 | + while (!original_security_ops.sb_mount); | |
1116 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1117 | + data_page); | |
1118 | +} | |
1119 | + | |
1120 | +#endif | |
1121 | + | |
1122 | +/** | |
1123 | + * ccs_sb_umount - Check permission for umount(). | |
1124 | + * | |
1125 | + * @mnt: Pointer to "struct vfsmount". | |
1126 | + * @flags: Unmount flags. | |
1127 | + * | |
1128 | + * Returns 0 on success, negative value otherwise. | |
1129 | + */ | |
1130 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1131 | +{ | |
1132 | + int rc = ccs_umount_permission(mnt, flags); | |
1133 | + if (rc) | |
1134 | + return rc; | |
1135 | + while (!original_security_ops.sb_umount); | |
1136 | + return original_security_ops.sb_umount(mnt, flags); | |
1137 | +} | |
1138 | + | |
1139 | +/** | |
1140 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1141 | + * | |
1142 | + * @file: Pointer to "struct file". | |
1143 | + * @cmd: Command number. | |
1144 | + * @arg: Value for @cmd. | |
1145 | + * | |
1146 | + * Returns 0 on success, negative value otherwise. | |
1147 | + */ | |
1148 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1149 | + unsigned long arg) | |
1150 | +{ | |
1151 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1152 | + if (rc) | |
1153 | + return rc; | |
1154 | + while (!original_security_ops.file_fcntl); | |
1155 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1156 | +} | |
1157 | + | |
1158 | +/** | |
1159 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1160 | + * | |
1161 | + * @filp: Pointer to "struct file". | |
1162 | + * @cmd: Command number. | |
1163 | + * @arg: Value for @cmd. | |
1164 | + * | |
1165 | + * Returns 0 on success, negative value otherwise. | |
1166 | + */ | |
1167 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1168 | + unsigned long arg) | |
1169 | +{ | |
1170 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1171 | + if (rc) | |
1172 | + return rc; | |
1173 | + while (!original_security_ops.file_ioctl); | |
1174 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1175 | +} | |
1176 | + | |
1177 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1178 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1179 | + const struct ccs_path_info *filename); | |
1180 | + | |
1181 | +/** | |
1182 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1183 | + * | |
1184 | + * @buffer: Pointer to "struct char *". | |
1185 | + * @buflen: Pointer to int which holds size of @buffer. | |
1186 | + * @str: String to copy. | |
1187 | + * | |
1188 | + * Returns 0 on success, negative value otherwise. | |
1189 | + * | |
1190 | + * @buffer and @buflen are updated upon success. | |
1191 | + */ | |
1192 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1193 | +{ | |
1194 | + int namelen = strlen(str); | |
1195 | + if (*buflen < namelen) | |
1196 | + return -ENOMEM; | |
1197 | + *buflen -= namelen; | |
1198 | + *buffer -= namelen; | |
1199 | + memcpy(*buffer, str, namelen); | |
1200 | + return 0; | |
1201 | +} | |
1202 | + | |
1203 | +/** | |
1204 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1205 | + * | |
1206 | + * @table: Pointer to "struct ctl_table". | |
1207 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1208 | + * | |
1209 | + * Returns 0 on success, negative value otherwise. | |
1210 | + */ | |
1211 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1212 | +{ | |
1213 | + int error; | |
1214 | + struct ccs_path_info buf; | |
1215 | + struct ccs_request_info r; | |
1216 | + int buflen; | |
1217 | + char *buffer; | |
1218 | + int idx; | |
1219 | + while (!original_security_ops.sysctl); | |
1220 | + error = original_security_ops.sysctl(table, op); | |
1221 | + if (error) | |
1222 | + return error; | |
1223 | + op &= MAY_READ | MAY_WRITE; | |
1224 | + if (!op) | |
1225 | + return 0; | |
1226 | + buffer = NULL; | |
1227 | + buf.name = NULL; | |
1228 | + idx = ccs_read_lock(); | |
1229 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1230 | + == CCS_CONFIG_DISABLED) | |
1231 | + goto out; | |
1232 | + error = -ENOMEM; | |
1233 | + buflen = 4096; | |
1234 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1235 | + if (buffer) { | |
1236 | + char *end = buffer + buflen; | |
1237 | + *--end = '\0'; | |
1238 | + buflen--; | |
1239 | + while (table) { | |
1240 | + char num[32]; | |
1241 | + const char *sp = table->procname; | |
1242 | + if (!sp) { | |
1243 | + memset(num, 0, sizeof(num)); | |
1244 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1245 | + table->ctl_name); | |
1246 | + sp = num; | |
1247 | + } | |
1248 | + if (ccs_prepend(&end, &buflen, sp) || | |
1249 | + ccs_prepend(&end, &buflen, "/")) | |
1250 | + goto out; | |
1251 | + table = table->parent; | |
1252 | + } | |
1253 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1254 | + goto out; | |
1255 | + buf.name = ccs_encode(end); | |
1256 | + } | |
1257 | + if (buf.name) { | |
1258 | + ccs_fill_path_info(&buf); | |
1259 | + if (op & MAY_READ) | |
1260 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1261 | + else | |
1262 | + error = 0; | |
1263 | + if (!error && (op & MAY_WRITE)) | |
1264 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1265 | + } | |
1266 | +out: | |
1267 | + ccs_read_unlock(idx); | |
1268 | + kfree(buf.name); | |
1269 | + kfree(buffer); | |
1270 | + return error; | |
1271 | +} | |
1272 | + | |
1273 | +#endif | |
1274 | + | |
1275 | +/* | |
1276 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1277 | + * Because copying byte array is not atomic. Reader checks | |
1278 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1279 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1280 | + */ | |
1281 | +#define swap_security_ops(op) \ | |
1282 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1283 | + | |
1284 | +/** | |
1285 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1286 | + * | |
1287 | + * @ops: Pointer to "struct security_operations". | |
1288 | + * | |
1289 | + * Returns nothing. | |
1290 | + */ | |
1291 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1292 | +{ | |
1293 | + /* Security context allocator. */ | |
1294 | + swap_security_ops(task_alloc_security); | |
1295 | + swap_security_ops(task_free_security); | |
1296 | + swap_security_ops(bprm_alloc_security); | |
1297 | + swap_security_ops(bprm_free_security); | |
1298 | + /* Security context updater for successful execve(). */ | |
1299 | + swap_security_ops(bprm_check_security); | |
1300 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
1301 | + swap_security_ops(bprm_compute_creds); | |
1302 | +#else | |
1303 | + swap_security_ops(bprm_apply_creds); | |
1304 | +#endif | |
1305 | + /* Various permission checker. */ | |
1306 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1307 | + swap_security_ops(dentry_open); | |
1308 | +#else | |
1309 | + swap_security_ops(inode_permission); | |
1310 | +#endif | |
1311 | + swap_security_ops(file_fcntl); | |
1312 | + swap_security_ops(file_ioctl); | |
1313 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1314 | + swap_security_ops(sysctl); | |
1315 | +#endif | |
1316 | + swap_security_ops(sb_pivotroot); | |
1317 | + swap_security_ops(sb_mount); | |
1318 | + swap_security_ops(sb_umount); | |
1319 | + swap_security_ops(inode_mknod); | |
1320 | + swap_security_ops(inode_mkdir); | |
1321 | + swap_security_ops(inode_rmdir); | |
1322 | + swap_security_ops(inode_unlink); | |
1323 | + swap_security_ops(inode_symlink); | |
1324 | + swap_security_ops(inode_rename); | |
1325 | + swap_security_ops(inode_link); | |
1326 | + swap_security_ops(inode_create); | |
1327 | + swap_security_ops(inode_setattr); | |
1328 | + swap_security_ops(inode_getattr); | |
1329 | +#ifdef CONFIG_SECURITY_NETWORK | |
1330 | + swap_security_ops(socket_bind); | |
1331 | + swap_security_ops(socket_connect); | |
1332 | + swap_security_ops(socket_listen); | |
1333 | + swap_security_ops(socket_sendmsg); | |
1334 | + swap_security_ops(socket_recvmsg); | |
1335 | + swap_security_ops(socket_getsockname); | |
1336 | + swap_security_ops(socket_getpeername); | |
1337 | + swap_security_ops(socket_getsockopt); | |
1338 | + swap_security_ops(socket_setsockopt); | |
1339 | + swap_security_ops(socket_shutdown); | |
1340 | + swap_security_ops(socket_accept); | |
1341 | + swap_security_ops(inode_free_security); | |
1342 | +#endif | |
1343 | +} | |
1344 | + | |
1345 | +#undef swap_security_ops | |
1346 | + | |
1347 | +/** | |
1348 | + * ccs_init - Initialize this module. | |
1349 | + * | |
1350 | + * Returns 0 on success, negative value otherwise. | |
1351 | + */ | |
1352 | +static int __init ccs_init(void) | |
1353 | +{ | |
1354 | + struct security_operations *ops = probe_security_ops(); | |
1355 | + if (!ops) | |
1356 | + goto out; | |
1357 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1358 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1359 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1360 | + goto out; | |
1361 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1362 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1363 | + goto out; | |
1364 | +#endif | |
1365 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1366 | + if (!ccsecurity_exports.vfsmount_lock) | |
1367 | + goto out; | |
1368 | + ccs_main_init(); | |
1369 | + ccs_update_security_ops(ops); | |
1370 | + printk(KERN_INFO "AKARI: 1.0.36 2017/02/20\n"); | |
1371 | + printk(KERN_INFO | |
1372 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1373 | + return 0; | |
1374 | +out: | |
1375 | + return -EINVAL; | |
1376 | +} | |
1377 | + | |
1378 | +module_init(ccs_init); | |
1379 | +MODULE_LICENSE("GPL"); | |
1380 | + | |
1381 | +/** | |
1382 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1383 | + * | |
1384 | + * @domain: Pointer to "struct ccs_domain_info". | |
1385 | + * | |
1386 | + * Returns true if @domain is in use, false otherwise. | |
1387 | + * | |
1388 | + * Caller holds rcu_read_lock(). | |
1389 | + */ | |
1390 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1391 | +{ | |
1392 | + return false; | |
1393 | +} |
@@ -0,0 +1,1373 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | + | |
14 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
15 | +extern struct ccs_security ccs_oom_security; | |
16 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
17 | +extern struct ccs_security ccs_default_security; | |
18 | +/* Dummy marker for calling security_bprm_free(). */ | |
19 | +static const unsigned long ccs_bprm_security; | |
20 | + | |
21 | +/* For exporting variables and functions. */ | |
22 | +struct ccsecurity_exports ccsecurity_exports; | |
23 | +/* Members are updated by loadable kernel module. */ | |
24 | +struct ccsecurity_operations ccsecurity_ops; | |
25 | + | |
26 | +/* Function pointers originally registered by register_security(). */ | |
27 | +static struct security_operations original_security_ops /* = *security_ops; */; | |
28 | + | |
29 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
30 | + | |
31 | +/** | |
32 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
33 | + * | |
34 | + * @count: Count to increment or decrement. | |
35 | + * | |
36 | + * Returns updated counter. | |
37 | + */ | |
38 | +static unsigned int ccs_update_ee_counter(int count) | |
39 | +{ | |
40 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10) || defined(atomic_add_return) | |
41 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
42 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
43 | + return atomic_add_return(count, &ccs_ee_counter); | |
44 | +#else | |
45 | + static DEFINE_SPINLOCK(ccs_ee_lock); | |
46 | + static unsigned int ccs_ee_counter; | |
47 | + unsigned long flags; | |
48 | + spin_lock_irqsave(&ccs_ee_lock, flags); | |
49 | + ccs_ee_counter += count; | |
50 | + count = ccs_ee_counter; | |
51 | + spin_unlock_irqrestore(&ccs_ee_lock, flags); | |
52 | + return count; | |
53 | +#endif | |
54 | +} | |
55 | + | |
56 | +/** | |
57 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
58 | + * | |
59 | + * @ee: Pointer to "struct ccs_execve". | |
60 | + * | |
61 | + * Returns nothing. | |
62 | + */ | |
63 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
64 | +{ | |
65 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
66 | + current->pid, ccs_update_ee_counter(1) - 1); | |
67 | +} | |
68 | + | |
69 | +/** | |
70 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
71 | + * | |
72 | + * @ee: Pointer to "struct ccs_execve". | |
73 | + * @task: True if released by current task, false otherwise. | |
74 | + * | |
75 | + * Returns nothing. | |
76 | + */ | |
77 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
78 | + const bool is_current) | |
79 | +{ | |
80 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
81 | + if (is_current) | |
82 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
83 | + ee, current->pid, tmp); | |
84 | + else | |
85 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
86 | + ee, tmp); | |
87 | +} | |
88 | + | |
89 | +#endif | |
90 | + | |
91 | +#if !defined(CONFIG_AKARI_DEBUG) | |
92 | +#define ccs_debug_trace(pos) do { } while (0) | |
93 | +#else | |
94 | +#define ccs_debug_trace(pos) \ | |
95 | + do { \ | |
96 | + static bool done; \ | |
97 | + if (!done) { \ | |
98 | + printk(KERN_INFO \ | |
99 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
100 | + done = true; \ | |
101 | + } \ | |
102 | + } while (0) | |
103 | +#endif | |
104 | + | |
105 | +/** | |
106 | + * ccs_clear_execve - Release memory used by do_execve(). | |
107 | + * | |
108 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
109 | + * @security: Pointer to "struct ccs_security". | |
110 | + * | |
111 | + * Returns nothing. | |
112 | + */ | |
113 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
114 | +{ | |
115 | + struct ccs_execve *ee; | |
116 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
117 | + return; | |
118 | + ee = security->ee; | |
119 | + security->ee = NULL; | |
120 | + if (!ee) | |
121 | + return; | |
122 | + ccs_finish_execve(ret, ee); | |
123 | +} | |
124 | + | |
125 | +/** | |
126 | + * ccs_task_alloc_security - Allocate memory for new tasks. | |
127 | + * | |
128 | + * @p: Pointer to "struct task_struct". | |
129 | + * | |
130 | + * Returns 0 on success, negative value otherwise. | |
131 | + */ | |
132 | +static int ccs_task_alloc_security(struct task_struct *p) | |
133 | +{ | |
134 | + int rc = ccs_alloc_task_security(p); | |
135 | + if (rc) | |
136 | + return rc; | |
137 | + while (!original_security_ops.task_alloc_security); | |
138 | + rc = original_security_ops.task_alloc_security(p); | |
139 | + if (rc) | |
140 | + ccs_free_task_security(p); | |
141 | + return rc; | |
142 | +} | |
143 | + | |
144 | +/** | |
145 | + * ccs_task_free_security - Release memory for "struct task_struct". | |
146 | + * | |
147 | + * @p: Pointer to "struct task_struct". | |
148 | + * | |
149 | + * Returns nothing. | |
150 | + */ | |
151 | +static void ccs_task_free_security(struct task_struct *p) | |
152 | +{ | |
153 | + while (!original_security_ops.task_free_security); | |
154 | + original_security_ops.task_free_security(p); | |
155 | + ccs_free_task_security(p); | |
156 | +} | |
157 | + | |
158 | +/** | |
159 | + * ccs_bprm_alloc_security - Allocate memory for "struct linux_binprm". | |
160 | + * | |
161 | + * @bprm: Pointer to "struct linux_binprm". | |
162 | + * | |
163 | + * Returns 0 on success, negative value otherwise. | |
164 | + */ | |
165 | +static int ccs_bprm_alloc_security(struct linux_binprm *bprm) | |
166 | +{ | |
167 | + int rc; | |
168 | + while (!original_security_ops.bprm_alloc_security); | |
169 | + rc = original_security_ops.bprm_alloc_security(bprm); | |
170 | + if (bprm->security || rc) | |
171 | + return rc; | |
172 | + /* | |
173 | + * Update bprm->security to &ccs_bprm_security so that | |
174 | + * security_bprm_free() is called even if do_execve() failed at | |
175 | + * search_binary_handler() without allocating memory at | |
176 | + * security_bprm_alloc(). This trick assumes that active LSM module | |
177 | + * does not access bprm->security if that module did not allocate | |
178 | + * memory at security_bprm_alloc(). | |
179 | + */ | |
180 | + bprm->security = (void *) &ccs_bprm_security; | |
181 | + return 0; | |
182 | +} | |
183 | + | |
184 | +/** | |
185 | + * ccs_bprm_free_security - Release memory for "struct linux_binprm". | |
186 | + * | |
187 | + * @bprm: Pointer to "struct linux_binprm". | |
188 | + * | |
189 | + * Returns nothing. | |
190 | + */ | |
191 | +static void ccs_bprm_free_security(struct linux_binprm *bprm) | |
192 | +{ | |
193 | + /* | |
194 | + * If do_execve() succeeded, bprm->security will be updated to NULL at | |
195 | + * security_bprm_compute_creds()/security_bprm_apply_creds() if | |
196 | + * bprm->security was set to &ccs_bprm_security at | |
197 | + * security_bprm_alloc(). | |
198 | + * | |
199 | + * If do_execve() failed, bprm->security remains at &ccs_bprm_security | |
200 | + * if bprm->security was set to &ccs_bprm_security at | |
201 | + * security_bprm_alloc(). | |
202 | + * | |
203 | + * And do_execve() does not call security_bprm_free() if do_execve() | |
204 | + * failed and bprm->security == NULL. Therefore, do not call | |
205 | + * original_security_ops.bprm_free_security() if bprm->security remains | |
206 | + * at &ccs_bprm_security . | |
207 | + */ | |
208 | + if (bprm->security != &ccs_bprm_security) { | |
209 | + while (!original_security_ops.bprm_free_security); | |
210 | + original_security_ops.bprm_free_security(bprm); | |
211 | + } | |
212 | + /* | |
213 | + * If do_execve() succeeded, | |
214 | + * ccs_clear_execve(0, ccs_current_security()); | |
215 | + * is called before calling below one. | |
216 | + * Thus, below call becomes no-op if do_execve() succeeded. | |
217 | + */ | |
218 | + ccs_clear_execve(-1, ccs_current_security()); | |
219 | +} | |
220 | + | |
221 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
222 | + | |
223 | +/** | |
224 | + * ccs_bprm_compute_creds - A hook which is called when do_execve() succeeded. | |
225 | + * | |
226 | + * @bprm: Pointer to "struct linux_binprm". | |
227 | + * | |
228 | + * Returns nothing. | |
229 | + */ | |
230 | +static void ccs_bprm_compute_creds(struct linux_binprm *bprm) | |
231 | +{ | |
232 | + if (bprm->security == &ccs_bprm_security) | |
233 | + bprm->security = NULL; | |
234 | + while (!original_security_ops.bprm_compute_creds); | |
235 | + original_security_ops.bprm_compute_creds(bprm); | |
236 | + ccs_clear_execve(0, ccs_current_security()); | |
237 | +} | |
238 | + | |
239 | +#else | |
240 | + | |
241 | +/** | |
242 | + * ccs_bprm_apply_creds - A hook which is called when do_execve() succeeded. | |
243 | + * | |
244 | + * @bprm: Pointer to "struct linux_binprm". | |
245 | + * @unsafe: Unsafe flag. | |
246 | + * | |
247 | + * Returns nothing. | |
248 | + */ | |
249 | +static void ccs_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) | |
250 | +{ | |
251 | + if (bprm->security == &ccs_bprm_security) | |
252 | + bprm->security = NULL; | |
253 | + while (!original_security_ops.bprm_apply_creds); | |
254 | + original_security_ops.bprm_apply_creds(bprm, unsafe); | |
255 | + ccs_clear_execve(0, ccs_current_security()); | |
256 | +} | |
257 | + | |
258 | +#endif | |
259 | + | |
260 | +/** | |
261 | + * ccs_bprm_check_security - Check permission for execve(). | |
262 | + * | |
263 | + * @bprm: Pointer to "struct linux_binprm". | |
264 | + * | |
265 | + * Returns 0 on success, negative value otherwise. | |
266 | + */ | |
267 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
268 | +{ | |
269 | + struct ccs_security *security = ccs_current_security(); | |
270 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
271 | + return -ENOMEM; | |
272 | + if (!security->ee) { | |
273 | + int rc; | |
274 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
275 | + if (!ccs_policy_loaded) | |
276 | + ccs_load_policy(bprm->filename); | |
277 | +#endif | |
278 | + rc = ccs_start_execve(bprm, &security->ee); | |
279 | + if (rc) | |
280 | + return rc; | |
281 | + } | |
282 | + while (!original_security_ops.bprm_check_security); | |
283 | + return original_security_ops.bprm_check_security(bprm); | |
284 | +} | |
285 | + | |
286 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
287 | + | |
288 | +/** | |
289 | + * ccs_open - Check permission for open(). | |
290 | + * | |
291 | + * @f: Pointer to "struct file". | |
292 | + * | |
293 | + * Returns 0 on success, negative value otherwise. | |
294 | + */ | |
295 | +static int ccs_open(struct file *f) | |
296 | +{ | |
297 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
298 | + f->f_flags + 1); | |
299 | +} | |
300 | + | |
301 | +#endif | |
302 | + | |
303 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
304 | + | |
305 | +/** | |
306 | + * ccs_dentry_open - Check permission for open(). | |
307 | + * | |
308 | + * @f: Pointer to "struct file". | |
309 | + * | |
310 | + * Returns 0 on success, negative value otherwise. | |
311 | + */ | |
312 | +static int ccs_dentry_open(struct file *f) | |
313 | +{ | |
314 | + int rc = ccs_open(f); | |
315 | + if (rc) | |
316 | + return rc; | |
317 | + while (!original_security_ops.dentry_open); | |
318 | + return original_security_ops.dentry_open(f); | |
319 | +} | |
320 | + | |
321 | +#else | |
322 | + | |
323 | +/** | |
324 | + * ccs_open - Check permission for open(). | |
325 | + * | |
326 | + * @inode: Pointer to "struct inode". | |
327 | + * @mask: Open mode. | |
328 | + * @nd: Pointer to "struct nameidata". | |
329 | + * | |
330 | + * Returns 0 on success, negative value otherwise. | |
331 | + */ | |
332 | +static int ccs_open(struct inode *inode, int mask, struct nameidata *nd) | |
333 | +{ | |
334 | + int flags; | |
335 | + if (!nd || !nd->dentry) | |
336 | + return 0; | |
337 | + /* open_exec() passes MAY_EXEC . */ | |
338 | + if (mask == MAY_EXEC && inode && S_ISREG(inode->i_mode) && | |
339 | + (ccs_current_flags() & CCS_TASK_IS_IN_EXECVE)) | |
340 | + mask = MAY_READ; | |
341 | + /* | |
342 | + * This flags value is passed to ACC_MODE(). | |
343 | + * ccs_open_permission() for older versions uses old ACC_MODE(). | |
344 | + */ | |
345 | + switch (mask & (MAY_READ | MAY_WRITE)) { | |
346 | + case MAY_READ: | |
347 | + flags = 01; | |
348 | + break; | |
349 | + case MAY_WRITE: | |
350 | + flags = 02; | |
351 | + break; | |
352 | + case MAY_READ | MAY_WRITE: | |
353 | + flags = 03; | |
354 | + break; | |
355 | + default: | |
356 | + return 0; | |
357 | + } | |
358 | + return ccs_open_permission(nd->dentry, nd->mnt, flags); | |
359 | +} | |
360 | + | |
361 | +/** | |
362 | + * ccs_inode_permission - Check permission for open(). | |
363 | + * | |
364 | + * @inode: Pointer to "struct inode". | |
365 | + * @mask: Open mode. | |
366 | + * @nd: Pointer to "struct nameidata". | |
367 | + * | |
368 | + * Returns 0 on success, negative value otherwise. | |
369 | + * | |
370 | + * Note that this hook is called from permission(), and may not be called for | |
371 | + * open(). Maybe it is better to use security_file_permission(). | |
372 | + */ | |
373 | +static int ccs_inode_permission(struct inode *inode, int mask, | |
374 | + struct nameidata *nd) | |
375 | +{ | |
376 | + int rc = ccs_open(inode, mask, nd); | |
377 | + if (rc) | |
378 | + return rc; | |
379 | + while (!original_security_ops.inode_permission); | |
380 | + return original_security_ops.inode_permission(inode, mask, nd); | |
381 | +} | |
382 | + | |
383 | +#endif | |
384 | + | |
385 | +/** | |
386 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
387 | + * | |
388 | + * @dentry: Pointer to "struct dentry". | |
389 | + * @attr: Pointer to "struct iattr". | |
390 | + * | |
391 | + * Returns 0 on success, negative value otherwise. | |
392 | + */ | |
393 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
394 | +{ | |
395 | + int rc = 0; | |
396 | + if (attr->ia_valid & ATTR_UID) | |
397 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, -1); | |
398 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
399 | + rc = ccs_chown_permission(dentry, NULL, -1, attr->ia_gid); | |
400 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
401 | + rc = ccs_chmod_permission(dentry, NULL, attr->ia_mode); | |
402 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
403 | + rc = ccs_truncate_permission(dentry, NULL); | |
404 | + if (rc) | |
405 | + return rc; | |
406 | + while (!original_security_ops.inode_setattr); | |
407 | + return original_security_ops.inode_setattr(dentry, attr); | |
408 | +} | |
409 | + | |
410 | +/** | |
411 | + * ccs_inode_getattr - Check permission for stat(). | |
412 | + * | |
413 | + * @mnt: Pointer to "struct vfsmount". | |
414 | + * @dentry: Pointer to "struct dentry". | |
415 | + * | |
416 | + * Returns 0 on success, negative value otherwise. | |
417 | + */ | |
418 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
419 | +{ | |
420 | + int rc = ccs_getattr_permission(mnt, dentry); | |
421 | + if (rc) | |
422 | + return rc; | |
423 | + while (!original_security_ops.inode_getattr); | |
424 | + return original_security_ops.inode_getattr(mnt, dentry); | |
425 | +} | |
426 | + | |
427 | +/** | |
428 | + * ccs_inode_mknod - Check permission for mknod(). | |
429 | + * | |
430 | + * @dir: Pointer to "struct inode". | |
431 | + * @dentry: Pointer to "struct dentry". | |
432 | + * @mode: Create mode. | |
433 | + * @dev: Device major/minor number. | |
434 | + * | |
435 | + * Returns 0 on success, negative value otherwise. | |
436 | + */ | |
437 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
438 | + dev_t dev) | |
439 | +{ | |
440 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
441 | + if (rc) | |
442 | + return rc; | |
443 | + while (!original_security_ops.inode_mknod); | |
444 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
445 | +} | |
446 | + | |
447 | +/** | |
448 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
449 | + * | |
450 | + * @dir: Pointer to "struct inode". | |
451 | + * @dentry: Pointer to "struct dentry". | |
452 | + * @mode: Create mode. | |
453 | + * | |
454 | + * Returns 0 on success, negative value otherwise. | |
455 | + */ | |
456 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
457 | +{ | |
458 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
459 | + if (rc) | |
460 | + return rc; | |
461 | + while (!original_security_ops.inode_mkdir); | |
462 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
463 | +} | |
464 | + | |
465 | +/** | |
466 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
467 | + * | |
468 | + * @dir: Pointer to "struct inode". | |
469 | + * @dentry: Pointer to "struct dentry". | |
470 | + * | |
471 | + * Returns 0 on success, negative value otherwise. | |
472 | + */ | |
473 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
474 | +{ | |
475 | + int rc = ccs_rmdir_permission(dentry, NULL); | |
476 | + if (rc) | |
477 | + return rc; | |
478 | + while (!original_security_ops.inode_rmdir); | |
479 | + return original_security_ops.inode_rmdir(dir, dentry); | |
480 | +} | |
481 | + | |
482 | +/** | |
483 | + * ccs_inode_unlink - Check permission for unlink(). | |
484 | + * | |
485 | + * @dir: Pointer to "struct inode". | |
486 | + * @dentry: Pointer to "struct dentry". | |
487 | + * | |
488 | + * Returns 0 on success, negative value otherwise. | |
489 | + */ | |
490 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
491 | +{ | |
492 | + int rc = ccs_unlink_permission(dentry, NULL); | |
493 | + if (rc) | |
494 | + return rc; | |
495 | + while (!original_security_ops.inode_unlink); | |
496 | + return original_security_ops.inode_unlink(dir, dentry); | |
497 | +} | |
498 | + | |
499 | +/** | |
500 | + * ccs_inode_symlink - Check permission for symlink(). | |
501 | + * | |
502 | + * @dir: Pointer to "struct inode". | |
503 | + * @dentry: Pointer to "struct dentry". | |
504 | + * @old_name: Content of symbolic link. | |
505 | + * | |
506 | + * Returns 0 on success, negative value otherwise. | |
507 | + */ | |
508 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
509 | + const char *old_name) | |
510 | +{ | |
511 | + int rc = ccs_symlink_permission(dentry, NULL, old_name); | |
512 | + if (rc) | |
513 | + return rc; | |
514 | + while (!original_security_ops.inode_symlink); | |
515 | + return original_security_ops.inode_symlink(dir, dentry, old_name); | |
516 | +} | |
517 | + | |
518 | +/** | |
519 | + * ccs_inode_rename - Check permission for rename(). | |
520 | + * | |
521 | + * @old_dir: Pointer to "struct inode". | |
522 | + * @old_dentry: Pointer to "struct dentry". | |
523 | + * @new_dir: Pointer to "struct inode". | |
524 | + * @new_dentry: Pointer to "struct dentry". | |
525 | + * | |
526 | + * Returns 0 on success, negative value otherwise. | |
527 | + */ | |
528 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
529 | + struct inode *new_dir, struct dentry *new_dentry) | |
530 | +{ | |
531 | + int rc = ccs_rename_permission(old_dentry, new_dentry, NULL); | |
532 | + if (rc) | |
533 | + return rc; | |
534 | + while (!original_security_ops.inode_rename); | |
535 | + return original_security_ops.inode_rename(old_dir, old_dentry, new_dir, | |
536 | + new_dentry); | |
537 | +} | |
538 | + | |
539 | +/** | |
540 | + * ccs_inode_link - Check permission for link(). | |
541 | + * | |
542 | + * @old_dentry: Pointer to "struct dentry". | |
543 | + * @dir: Pointer to "struct inode". | |
544 | + * @new_dentry: Pointer to "struct dentry". | |
545 | + * | |
546 | + * Returns 0 on success, negative value otherwise. | |
547 | + */ | |
548 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
549 | + struct dentry *new_dentry) | |
550 | +{ | |
551 | + int rc = ccs_link_permission(old_dentry, new_dentry, NULL); | |
552 | + if (rc) | |
553 | + return rc; | |
554 | + while (!original_security_ops.inode_link); | |
555 | + return original_security_ops.inode_link(old_dentry, dir, new_dentry); | |
556 | +} | |
557 | + | |
558 | +/** | |
559 | + * ccs_inode_create - Check permission for creat(). | |
560 | + * | |
561 | + * @dir: Pointer to "struct inode". | |
562 | + * @dentry: Pointer to "struct dentry". | |
563 | + * @mode: Create mode. | |
564 | + * | |
565 | + * Returns 0 on success, negative value otherwise. | |
566 | + */ | |
567 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
568 | + int mode) | |
569 | +{ | |
570 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
571 | + if (rc) | |
572 | + return rc; | |
573 | + while (!original_security_ops.inode_create); | |
574 | + return original_security_ops.inode_create(dir, dentry, mode); | |
575 | +} | |
576 | + | |
577 | +#ifdef CONFIG_SECURITY_NETWORK | |
578 | + | |
579 | +#include <net/sock.h> | |
580 | + | |
581 | +/* Structure for remembering an accept()ed socket's status. */ | |
582 | +struct ccs_socket_tag { | |
583 | + struct list_head list; | |
584 | + struct inode *inode; | |
585 | + int status; | |
586 | + struct rcu_head rcu; | |
587 | +}; | |
588 | + | |
589 | +/* | |
590 | + * List for managing accept()ed sockets. | |
591 | + * Since we don't need to keep an accept()ed socket into this list after | |
592 | + * once the permission was granted, the number of entries in this list is | |
593 | + * likely small. Therefore, we don't use hash tables. | |
594 | + */ | |
595 | +static LIST_HEAD(ccs_accepted_socket_list); | |
596 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
597 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
598 | + | |
599 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
600 | + | |
601 | +/** | |
602 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
603 | + * | |
604 | + * @rcu: Pointer to "struct rcu_head". | |
605 | + * | |
606 | + * Returns nothing. | |
607 | + */ | |
608 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
609 | +{ | |
610 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
611 | + kfree(ptr); | |
612 | +} | |
613 | + | |
614 | +#else | |
615 | + | |
616 | +/** | |
617 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
618 | + * | |
619 | + * @arg: Pointer to "void". | |
620 | + * | |
621 | + * Returns nothing. | |
622 | + */ | |
623 | +static void ccs_socket_rcu_free(void *arg) | |
624 | +{ | |
625 | + struct ccs_socket_tag *ptr = arg; | |
626 | + kfree(ptr); | |
627 | +} | |
628 | + | |
629 | +#endif | |
630 | + | |
631 | +/** | |
632 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
633 | + * | |
634 | + * @inode: Pointer to "struct inode". | |
635 | + * @status: New status. | |
636 | + * | |
637 | + * Returns nothing. | |
638 | + * | |
639 | + * If @status == 0, memory for that socket will be released after RCU grace | |
640 | + * period. | |
641 | + */ | |
642 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
643 | +{ | |
644 | + struct ccs_socket_tag *ptr; | |
645 | + /* | |
646 | + * Protect whole section because multiple threads may call this | |
647 | + * function with same "sock" via ccs_validate_socket(). | |
648 | + */ | |
649 | + spin_lock(&ccs_accepted_socket_list_lock); | |
650 | + rcu_read_lock(); | |
651 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
652 | + if (ptr->inode != inode) | |
653 | + continue; | |
654 | + ptr->status = status; | |
655 | + if (status) | |
656 | + break; | |
657 | + list_del_rcu(&ptr->list); | |
658 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
659 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
660 | +#else | |
661 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free, ptr); | |
662 | +#endif | |
663 | + break; | |
664 | + } | |
665 | + rcu_read_unlock(); | |
666 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
667 | +} | |
668 | + | |
669 | +/** | |
670 | + * ccs_validate_socket - Check post accept() permission if needed. | |
671 | + * | |
672 | + * @sock: Pointer to "struct socket". | |
673 | + * | |
674 | + * Returns 0 on success, negative value otherwise. | |
675 | + */ | |
676 | +static int ccs_validate_socket(struct socket *sock) | |
677 | +{ | |
678 | + struct inode *inode = SOCK_INODE(sock); | |
679 | + struct ccs_socket_tag *ptr; | |
680 | + int ret = 0; | |
681 | + rcu_read_lock(); | |
682 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
683 | + if (ptr->inode != inode) | |
684 | + continue; | |
685 | + ret = ptr->status; | |
686 | + break; | |
687 | + } | |
688 | + rcu_read_unlock(); | |
689 | + if (ret <= 0) | |
690 | + /* | |
691 | + * This socket is not an accept()ed socket or this socket is | |
692 | + * an accept()ed socket and post accept() permission is done. | |
693 | + */ | |
694 | + return ret; | |
695 | + /* | |
696 | + * Check post accept() permission now. | |
697 | + * | |
698 | + * Strictly speaking, we need to pass both listen()ing socket and | |
699 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
700 | + * But since socket's family and type are same for both sockets, | |
701 | + * passing the accept()ed socket in place for the listen()ing socket | |
702 | + * will work. | |
703 | + */ | |
704 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
705 | + /* | |
706 | + * If permission was granted, we forget that this is an accept()ed | |
707 | + * socket. Otherwise, we remember that this socket needs to return | |
708 | + * error for subsequent socketcalls. | |
709 | + */ | |
710 | + ccs_update_socket_tag(inode, ret); | |
711 | + return ret; | |
712 | +} | |
713 | + | |
714 | +/** | |
715 | + * ccs_socket_accept - Check permission for accept(). | |
716 | + * | |
717 | + * @sock: Pointer to "struct socket". | |
718 | + * @newsock: Pointer to "struct socket". | |
719 | + * | |
720 | + * Returns 0 on success, negative value otherwise. | |
721 | + * | |
722 | + * This hook is used for setting up environment for doing post accept() | |
723 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
724 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
725 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
726 | + * in order to do post accept() permission check before returning to userspace. | |
727 | + * If we make the copy in security_socket_post_create(), it would be possible | |
728 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
729 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
730 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
731 | + * rather than between sock->ops->accept() and returning to userspace. | |
732 | + * This means that if a socket was close()d before calling some socket | |
733 | + * syscalls, post accept() permission check will not be done. | |
734 | + */ | |
735 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
736 | +{ | |
737 | + struct ccs_socket_tag *ptr; | |
738 | + int rc = ccs_validate_socket(sock); | |
739 | + if (rc < 0) | |
740 | + return rc; | |
741 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
742 | + if (!ptr) | |
743 | + return -ENOMEM; | |
744 | + while (!original_security_ops.socket_accept); | |
745 | + rc = original_security_ops.socket_accept(sock, newsock); | |
746 | + if (rc) { | |
747 | + kfree(ptr); | |
748 | + return rc; | |
749 | + } | |
750 | + /* | |
751 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
752 | + * "newsock" as "an accept()ed socket but post accept() permission | |
753 | + * check is not done yet" by allocating memory using inode of the | |
754 | + * "newsock" as a search key. | |
755 | + */ | |
756 | + ptr->inode = SOCK_INODE(newsock); | |
757 | + ptr->status = 1; /* Check post accept() permission later. */ | |
758 | + spin_lock(&ccs_accepted_socket_list_lock); | |
759 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
760 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
761 | + return 0; | |
762 | +} | |
763 | + | |
764 | +/** | |
765 | + * ccs_socket_listen - Check permission for listen(). | |
766 | + * | |
767 | + * @sock: Pointer to "struct socket". | |
768 | + * @backlog: Backlog parameter. | |
769 | + * | |
770 | + * Returns 0 on success, negative value otherwise. | |
771 | + */ | |
772 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
773 | +{ | |
774 | + int rc = ccs_validate_socket(sock); | |
775 | + if (rc < 0) | |
776 | + return rc; | |
777 | + rc = ccs_socket_listen_permission(sock); | |
778 | + if (rc) | |
779 | + return rc; | |
780 | + while (!original_security_ops.socket_listen); | |
781 | + return original_security_ops.socket_listen(sock, backlog); | |
782 | +} | |
783 | + | |
784 | +/** | |
785 | + * ccs_socket_connect - Check permission for connect(). | |
786 | + * | |
787 | + * @sock: Pointer to "struct socket". | |
788 | + * @addr: Pointer to "struct sockaddr". | |
789 | + * @addr_len: Size of @addr. | |
790 | + * | |
791 | + * Returns 0 on success, negative value otherwise. | |
792 | + */ | |
793 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
794 | + int addr_len) | |
795 | +{ | |
796 | + int rc = ccs_validate_socket(sock); | |
797 | + if (rc < 0) | |
798 | + return rc; | |
799 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
800 | + if (rc) | |
801 | + return rc; | |
802 | + while (!original_security_ops.socket_connect); | |
803 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
804 | +} | |
805 | + | |
806 | +/** | |
807 | + * ccs_socket_bind - Check permission for bind(). | |
808 | + * | |
809 | + * @sock: Pointer to "struct socket". | |
810 | + * @addr: Pointer to "struct sockaddr". | |
811 | + * @addr_len: Size of @addr. | |
812 | + * | |
813 | + * Returns 0 on success, negative value otherwise. | |
814 | + */ | |
815 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
816 | + int addr_len) | |
817 | +{ | |
818 | + int rc = ccs_validate_socket(sock); | |
819 | + if (rc < 0) | |
820 | + return rc; | |
821 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
822 | + if (rc) | |
823 | + return rc; | |
824 | + while (!original_security_ops.socket_bind); | |
825 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
826 | +} | |
827 | + | |
828 | +/** | |
829 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
830 | + * | |
831 | + * @sock: Pointer to "struct socket". | |
832 | + * @msg: Pointer to "struct msghdr". | |
833 | + * @size: Size of message. | |
834 | + * | |
835 | + * Returns 0 on success, negative value otherwise. | |
836 | + */ | |
837 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
838 | + int size) | |
839 | +{ | |
840 | + int rc = ccs_validate_socket(sock); | |
841 | + if (rc < 0) | |
842 | + return rc; | |
843 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
844 | + if (rc) | |
845 | + return rc; | |
846 | + while (!original_security_ops.socket_sendmsg); | |
847 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
848 | +} | |
849 | + | |
850 | +/** | |
851 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
852 | + * | |
853 | + * @sock: Pointer to "struct socket". | |
854 | + * @msg: Pointer to "struct msghdr". | |
855 | + * @size: Size of message. | |
856 | + * @flags: Flags. | |
857 | + * | |
858 | + * Returns 0 on success, negative value otherwise. | |
859 | + */ | |
860 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
861 | + int size, int flags) | |
862 | +{ | |
863 | + int rc = ccs_validate_socket(sock); | |
864 | + if (rc < 0) | |
865 | + return rc; | |
866 | + while (!original_security_ops.socket_recvmsg); | |
867 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
868 | +} | |
869 | + | |
870 | +/** | |
871 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
872 | + * | |
873 | + * @sock: Pointer to "struct socket". | |
874 | + * | |
875 | + * Returns 0 on success, negative value otherwise. | |
876 | + */ | |
877 | +static int ccs_socket_getsockname(struct socket *sock) | |
878 | +{ | |
879 | + int rc = ccs_validate_socket(sock); | |
880 | + if (rc < 0) | |
881 | + return rc; | |
882 | + while (!original_security_ops.socket_getsockname); | |
883 | + return original_security_ops.socket_getsockname(sock); | |
884 | +} | |
885 | + | |
886 | +/** | |
887 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
888 | + * | |
889 | + * @sock: Pointer to "struct socket". | |
890 | + * | |
891 | + * Returns 0 on success, negative value otherwise. | |
892 | + */ | |
893 | +static int ccs_socket_getpeername(struct socket *sock) | |
894 | +{ | |
895 | + int rc = ccs_validate_socket(sock); | |
896 | + if (rc < 0) | |
897 | + return rc; | |
898 | + while (!original_security_ops.socket_getpeername); | |
899 | + return original_security_ops.socket_getpeername(sock); | |
900 | +} | |
901 | + | |
902 | +/** | |
903 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
904 | + * | |
905 | + * @sock: Pointer to "struct socket". | |
906 | + * @level: Level. | |
907 | + * @optname: Option's name, | |
908 | + * | |
909 | + * Returns 0 on success, negative value otherwise. | |
910 | + */ | |
911 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
912 | +{ | |
913 | + int rc = ccs_validate_socket(sock); | |
914 | + if (rc < 0) | |
915 | + return rc; | |
916 | + while (!original_security_ops.socket_getsockopt); | |
917 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
918 | +} | |
919 | + | |
920 | +/** | |
921 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
922 | + * | |
923 | + * @sock: Pointer to "struct socket". | |
924 | + * @level: Level. | |
925 | + * @optname: Option's name, | |
926 | + * | |
927 | + * Returns 0 on success, negative value otherwise. | |
928 | + */ | |
929 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
930 | +{ | |
931 | + int rc = ccs_validate_socket(sock); | |
932 | + if (rc < 0) | |
933 | + return rc; | |
934 | + while (!original_security_ops.socket_setsockopt); | |
935 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
936 | +} | |
937 | + | |
938 | +/** | |
939 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
940 | + * | |
941 | + * @sock: Pointer to "struct socket". | |
942 | + * @how: Shutdown mode. | |
943 | + * | |
944 | + * Returns 0 on success, negative value otherwise. | |
945 | + */ | |
946 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
947 | +{ | |
948 | + int rc = ccs_validate_socket(sock); | |
949 | + if (rc < 0) | |
950 | + return rc; | |
951 | + while (!original_security_ops.socket_shutdown); | |
952 | + return original_security_ops.socket_shutdown(sock, how); | |
953 | +} | |
954 | + | |
955 | +#define SOCKFS_MAGIC 0x534F434B | |
956 | + | |
957 | +/** | |
958 | + * ccs_inode_free_security - Release memory associated with an inode. | |
959 | + * | |
960 | + * @inode: Pointer to "struct inode". | |
961 | + * | |
962 | + * Returns nothing. | |
963 | + * | |
964 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
965 | + */ | |
966 | +static void ccs_inode_free_security(struct inode *inode) | |
967 | +{ | |
968 | + while (!original_security_ops.inode_free_security); | |
969 | + original_security_ops.inode_free_security(inode); | |
970 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
971 | + ccs_update_socket_tag(inode, 0); | |
972 | +} | |
973 | + | |
974 | +#endif | |
975 | + | |
976 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24) | |
977 | + | |
978 | +/** | |
979 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
980 | + * | |
981 | + * @old_nd: Pointer to "struct nameidata". | |
982 | + * @new_nd: Pointer to "struct nameidata". | |
983 | + * | |
984 | + * Returns 0 on success, negative value otherwise. | |
985 | + */ | |
986 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
987 | +{ | |
988 | + int rc = ccs_pivot_root_permission(old_nd, new_nd); | |
989 | + if (rc) | |
990 | + return rc; | |
991 | + while (!original_security_ops.sb_pivotroot); | |
992 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
993 | +} | |
994 | + | |
995 | +/** | |
996 | + * ccs_sb_mount - Check permission for mount(). | |
997 | + * | |
998 | + * @dev_name: Name of device file. | |
999 | + * @nd: Pointer to "struct nameidata". | |
1000 | + * @type: Name of filesystem type. Maybe NULL. | |
1001 | + * @flags: Mount options. | |
1002 | + * @data_page: Optional data. Maybe NULL. | |
1003 | + * | |
1004 | + * Returns 0 on success, negative value otherwise. | |
1005 | + */ | |
1006 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1007 | + unsigned long flags, void *data_page) | |
1008 | +{ | |
1009 | + int rc = ccs_mount_permission(dev_name, nd, type, flags, data_page); | |
1010 | + if (rc) | |
1011 | + return rc; | |
1012 | + while (!original_security_ops.sb_mount); | |
1013 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1014 | + data_page); | |
1015 | +} | |
1016 | + | |
1017 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) | |
1018 | + | |
1019 | +/** | |
1020 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1021 | + * | |
1022 | + * @old_nd: Pointer to "struct nameidata". | |
1023 | + * @new_nd: Pointer to "struct nameidata". | |
1024 | + * | |
1025 | + * Returns 0 on success, negative value otherwise. | |
1026 | + */ | |
1027 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1028 | +{ | |
1029 | + int rc = ccs_pivot_root_permission(&old_nd->path, &new_nd->path); | |
1030 | + if (rc) | |
1031 | + return rc; | |
1032 | + while (!original_security_ops.sb_pivotroot); | |
1033 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1034 | +} | |
1035 | + | |
1036 | +/** | |
1037 | + * ccs_sb_mount - Check permission for mount(). | |
1038 | + * | |
1039 | + * @dev_name: Name of device file. | |
1040 | + * @nd: Pointer to "struct nameidata". | |
1041 | + * @type: Name of filesystem type. Maybe NULL. | |
1042 | + * @flags: Mount options. | |
1043 | + * @data_page: Optional data. Maybe NULL. | |
1044 | + * | |
1045 | + * Returns 0 on success, negative value otherwise. | |
1046 | + */ | |
1047 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1048 | + unsigned long flags, void *data_page) | |
1049 | +{ | |
1050 | + int rc = ccs_mount_permission(dev_name, &nd->path, type, flags, | |
1051 | + data_page); | |
1052 | + if (rc) | |
1053 | + return rc; | |
1054 | + while (!original_security_ops.sb_mount); | |
1055 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1056 | + data_page); | |
1057 | +} | |
1058 | + | |
1059 | +#else | |
1060 | + | |
1061 | +/** | |
1062 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1063 | + * | |
1064 | + * @old_path: Pointer to "struct path". | |
1065 | + * @new_path: Pointer to "struct path". | |
1066 | + * | |
1067 | + * Returns 0 on success, negative value otherwise. | |
1068 | + */ | |
1069 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1070 | +{ | |
1071 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1072 | + if (rc) | |
1073 | + return rc; | |
1074 | + while (!original_security_ops.sb_pivotroot); | |
1075 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1076 | +} | |
1077 | + | |
1078 | +/** | |
1079 | + * ccs_sb_mount - Check permission for mount(). | |
1080 | + * | |
1081 | + * @dev_name: Name of device file. | |
1082 | + * @path: Pointer to "struct path". | |
1083 | + * @type: Name of filesystem type. Maybe NULL. | |
1084 | + * @flags: Mount options. | |
1085 | + * @data_page: Optional data. Maybe NULL. | |
1086 | + * | |
1087 | + * Returns 0 on success, negative value otherwise. | |
1088 | + */ | |
1089 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1090 | + unsigned long flags, void *data_page) | |
1091 | +{ | |
1092 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1093 | + if (rc) | |
1094 | + return rc; | |
1095 | + while (!original_security_ops.sb_mount); | |
1096 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1097 | + data_page); | |
1098 | +} | |
1099 | + | |
1100 | +#endif | |
1101 | + | |
1102 | +/** | |
1103 | + * ccs_sb_umount - Check permission for umount(). | |
1104 | + * | |
1105 | + * @mnt: Pointer to "struct vfsmount". | |
1106 | + * @flags: Unmount flags. | |
1107 | + * | |
1108 | + * Returns 0 on success, negative value otherwise. | |
1109 | + */ | |
1110 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1111 | +{ | |
1112 | + int rc = ccs_umount_permission(mnt, flags); | |
1113 | + if (rc) | |
1114 | + return rc; | |
1115 | + while (!original_security_ops.sb_umount); | |
1116 | + return original_security_ops.sb_umount(mnt, flags); | |
1117 | +} | |
1118 | + | |
1119 | +/** | |
1120 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1121 | + * | |
1122 | + * @file: Pointer to "struct file". | |
1123 | + * @cmd: Command number. | |
1124 | + * @arg: Value for @cmd. | |
1125 | + * | |
1126 | + * Returns 0 on success, negative value otherwise. | |
1127 | + */ | |
1128 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1129 | + unsigned long arg) | |
1130 | +{ | |
1131 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1132 | + if (rc) | |
1133 | + return rc; | |
1134 | + while (!original_security_ops.file_fcntl); | |
1135 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1136 | +} | |
1137 | + | |
1138 | +/** | |
1139 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1140 | + * | |
1141 | + * @filp: Pointer to "struct file". | |
1142 | + * @cmd: Command number. | |
1143 | + * @arg: Value for @cmd. | |
1144 | + * | |
1145 | + * Returns 0 on success, negative value otherwise. | |
1146 | + */ | |
1147 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1148 | + unsigned long arg) | |
1149 | +{ | |
1150 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1151 | + if (rc) | |
1152 | + return rc; | |
1153 | + while (!original_security_ops.file_ioctl); | |
1154 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1155 | +} | |
1156 | + | |
1157 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1158 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1159 | + const struct ccs_path_info *filename); | |
1160 | + | |
1161 | +/** | |
1162 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1163 | + * | |
1164 | + * @buffer: Pointer to "struct char *". | |
1165 | + * @buflen: Pointer to int which holds size of @buffer. | |
1166 | + * @str: String to copy. | |
1167 | + * | |
1168 | + * Returns 0 on success, negative value otherwise. | |
1169 | + * | |
1170 | + * @buffer and @buflen are updated upon success. | |
1171 | + */ | |
1172 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1173 | +{ | |
1174 | + int namelen = strlen(str); | |
1175 | + if (*buflen < namelen) | |
1176 | + return -ENOMEM; | |
1177 | + *buflen -= namelen; | |
1178 | + *buffer -= namelen; | |
1179 | + memcpy(*buffer, str, namelen); | |
1180 | + return 0; | |
1181 | +} | |
1182 | + | |
1183 | +/** | |
1184 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1185 | + * | |
1186 | + * @table: Pointer to "struct ctl_table". | |
1187 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1188 | + * | |
1189 | + * Returns 0 on success, negative value otherwise. | |
1190 | + */ | |
1191 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1192 | +{ | |
1193 | + int error; | |
1194 | + struct ccs_path_info buf; | |
1195 | + struct ccs_request_info r; | |
1196 | + int buflen; | |
1197 | + char *buffer; | |
1198 | + int idx; | |
1199 | + while (!original_security_ops.sysctl); | |
1200 | + error = original_security_ops.sysctl(table, op); | |
1201 | + if (error) | |
1202 | + return error; | |
1203 | + op &= MAY_READ | MAY_WRITE; | |
1204 | + if (!op) | |
1205 | + return 0; | |
1206 | + buffer = NULL; | |
1207 | + buf.name = NULL; | |
1208 | + idx = ccs_read_lock(); | |
1209 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1210 | + == CCS_CONFIG_DISABLED) | |
1211 | + goto out; | |
1212 | + error = -ENOMEM; | |
1213 | + buflen = 4096; | |
1214 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1215 | + if (buffer) { | |
1216 | + char *end = buffer + buflen; | |
1217 | + *--end = '\0'; | |
1218 | + buflen--; | |
1219 | + while (table) { | |
1220 | + char num[32]; | |
1221 | + const char *sp = table->procname; | |
1222 | + if (!sp) { | |
1223 | + memset(num, 0, sizeof(num)); | |
1224 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1225 | + table->ctl_name); | |
1226 | + sp = num; | |
1227 | + } | |
1228 | + if (ccs_prepend(&end, &buflen, sp) || | |
1229 | + ccs_prepend(&end, &buflen, "/")) | |
1230 | + goto out; | |
1231 | + table = table->parent; | |
1232 | + } | |
1233 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1234 | + goto out; | |
1235 | + buf.name = ccs_encode(end); | |
1236 | + } | |
1237 | + if (buf.name) { | |
1238 | + ccs_fill_path_info(&buf); | |
1239 | + if (op & MAY_READ) | |
1240 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1241 | + else | |
1242 | + error = 0; | |
1243 | + if (!error && (op & MAY_WRITE)) | |
1244 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1245 | + } | |
1246 | +out: | |
1247 | + ccs_read_unlock(idx); | |
1248 | + kfree(buf.name); | |
1249 | + kfree(buffer); | |
1250 | + return error; | |
1251 | +} | |
1252 | + | |
1253 | +#endif | |
1254 | + | |
1255 | +/* | |
1256 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1257 | + * Because copying byte array is not atomic. Reader checks | |
1258 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1259 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1260 | + */ | |
1261 | +#define swap_security_ops(op) \ | |
1262 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1263 | + | |
1264 | +/** | |
1265 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1266 | + * | |
1267 | + * @ops: Pointer to "struct security_operations". | |
1268 | + * | |
1269 | + * Returns nothing. | |
1270 | + */ | |
1271 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1272 | +{ | |
1273 | + /* Security context allocator. */ | |
1274 | + swap_security_ops(task_alloc_security); | |
1275 | + swap_security_ops(task_free_security); | |
1276 | + swap_security_ops(bprm_alloc_security); | |
1277 | + swap_security_ops(bprm_free_security); | |
1278 | + /* Security context updater for successful execve(). */ | |
1279 | + swap_security_ops(bprm_check_security); | |
1280 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
1281 | + swap_security_ops(bprm_compute_creds); | |
1282 | +#else | |
1283 | + swap_security_ops(bprm_apply_creds); | |
1284 | +#endif | |
1285 | + /* Various permission checker. */ | |
1286 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1287 | + swap_security_ops(dentry_open); | |
1288 | +#else | |
1289 | + swap_security_ops(inode_permission); | |
1290 | +#endif | |
1291 | + swap_security_ops(file_fcntl); | |
1292 | + swap_security_ops(file_ioctl); | |
1293 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1294 | + swap_security_ops(sysctl); | |
1295 | +#endif | |
1296 | + swap_security_ops(sb_pivotroot); | |
1297 | + swap_security_ops(sb_mount); | |
1298 | + swap_security_ops(sb_umount); | |
1299 | + swap_security_ops(inode_mknod); | |
1300 | + swap_security_ops(inode_mkdir); | |
1301 | + swap_security_ops(inode_rmdir); | |
1302 | + swap_security_ops(inode_unlink); | |
1303 | + swap_security_ops(inode_symlink); | |
1304 | + swap_security_ops(inode_rename); | |
1305 | + swap_security_ops(inode_link); | |
1306 | + swap_security_ops(inode_create); | |
1307 | + swap_security_ops(inode_setattr); | |
1308 | + swap_security_ops(inode_getattr); | |
1309 | +#ifdef CONFIG_SECURITY_NETWORK | |
1310 | + swap_security_ops(socket_bind); | |
1311 | + swap_security_ops(socket_connect); | |
1312 | + swap_security_ops(socket_listen); | |
1313 | + swap_security_ops(socket_sendmsg); | |
1314 | + swap_security_ops(socket_recvmsg); | |
1315 | + swap_security_ops(socket_getsockname); | |
1316 | + swap_security_ops(socket_getpeername); | |
1317 | + swap_security_ops(socket_getsockopt); | |
1318 | + swap_security_ops(socket_setsockopt); | |
1319 | + swap_security_ops(socket_shutdown); | |
1320 | + swap_security_ops(socket_accept); | |
1321 | + swap_security_ops(inode_free_security); | |
1322 | +#endif | |
1323 | +} | |
1324 | + | |
1325 | +#undef swap_security_ops | |
1326 | + | |
1327 | +/** | |
1328 | + * ccs_init - Initialize this module. | |
1329 | + * | |
1330 | + * Returns 0 on success, negative value otherwise. | |
1331 | + */ | |
1332 | +static int __init ccs_init(void) | |
1333 | +{ | |
1334 | + struct security_operations *ops = probe_security_ops(); | |
1335 | + if (!ops) | |
1336 | + goto out; | |
1337 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1338 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1339 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1340 | + goto out; | |
1341 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1342 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1343 | + goto out; | |
1344 | +#endif | |
1345 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1346 | + if (!ccsecurity_exports.vfsmount_lock) | |
1347 | + goto out; | |
1348 | + ccs_main_init(); | |
1349 | + ccs_update_security_ops(ops); | |
1350 | + printk(KERN_INFO "AKARI: 1.0.36 2017/02/20\n"); | |
1351 | + printk(KERN_INFO | |
1352 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1353 | + return 0; | |
1354 | +out: | |
1355 | + return -EINVAL; | |
1356 | +} | |
1357 | + | |
1358 | +module_init(ccs_init); | |
1359 | +MODULE_LICENSE("GPL"); | |
1360 | + | |
1361 | +/** | |
1362 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1363 | + * | |
1364 | + * @domain: Pointer to "struct ccs_domain_info". | |
1365 | + * | |
1366 | + * Returns true if @domain is in use, false otherwise. | |
1367 | + * | |
1368 | + * Caller holds rcu_read_lock(). | |
1369 | + */ | |
1370 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1371 | +{ | |
1372 | + return false; | |
1373 | +} |
@@ -0,0 +1,2184 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) | |
12 | +#define USE_UMODE_T | |
13 | +#else | |
14 | +#include "check_umode_t.h" | |
15 | +#endif | |
16 | + | |
17 | +/* Prototype definition. */ | |
18 | +static void ccs_task_security_gc(void); | |
19 | +static int ccs_copy_cred_security(const struct cred *new, | |
20 | + const struct cred *old, gfp_t gfp); | |
21 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred); | |
22 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
23 | +static atomic_t ccs_in_execve_tasks = ATOMIC_INIT(0); | |
24 | +/* | |
25 | + * List of "struct ccs_security" for "struct pid". | |
26 | + * | |
27 | + * All instances on this list is guaranteed that "struct ccs_security"->pid != | |
28 | + * NULL. Also, instances on this list that are in execve() are guaranteed that | |
29 | + * "struct ccs_security"->cred remembers "struct linux_binprm"->cred with a | |
30 | + * refcount on "struct linux_binprm"->cred. | |
31 | + */ | |
32 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
33 | +/* | |
34 | + * List of "struct ccs_security" for "struct cred". | |
35 | + * | |
36 | + * Since the number of "struct cred" is nearly equals to the number of | |
37 | + * "struct pid", we allocate hash tables like ccs_task_security_list. | |
38 | + * | |
39 | + * All instances on this list are guaranteed that "struct ccs_security"->pid == | |
40 | + * NULL and "struct ccs_security"->cred != NULL. | |
41 | + */ | |
42 | +static struct list_head ccs_cred_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
43 | + | |
44 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
45 | +static struct ccs_security ccs_oom_security = { | |
46 | + .ccs_domain_info = &ccs_kernel_domain | |
47 | +}; | |
48 | + | |
49 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
50 | +static struct ccs_security ccs_default_security = { | |
51 | + .ccs_domain_info = &ccs_kernel_domain | |
52 | +}; | |
53 | + | |
54 | +/* For exporting variables and functions. */ | |
55 | +struct ccsecurity_exports ccsecurity_exports; | |
56 | +/* Members are updated by loadable kernel module. */ | |
57 | +struct ccsecurity_operations ccsecurity_ops; | |
58 | + | |
59 | +/* Function pointers originally registered by register_security(). */ | |
60 | +static struct security_operations original_security_ops /* = *security_ops; */; | |
61 | + | |
62 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
63 | + | |
64 | +/** | |
65 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
66 | + * | |
67 | + * @count: Count to increment or decrement. | |
68 | + * | |
69 | + * Returns updated counter. | |
70 | + */ | |
71 | +static unsigned int ccs_update_ee_counter(int count) | |
72 | +{ | |
73 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
74 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
75 | + return atomic_add_return(count, &ccs_ee_counter); | |
76 | +} | |
77 | + | |
78 | +/** | |
79 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
80 | + * | |
81 | + * @ee: Pointer to "struct ccs_execve". | |
82 | + * | |
83 | + * Returns nothing. | |
84 | + */ | |
85 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
86 | +{ | |
87 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
88 | + current->pid, ccs_update_ee_counter(1) - 1); | |
89 | +} | |
90 | + | |
91 | +/** | |
92 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
93 | + * | |
94 | + * @ee: Pointer to "struct ccs_execve". | |
95 | + * @task: True if released by current task, false otherwise. | |
96 | + * | |
97 | + * Returns nothing. | |
98 | + */ | |
99 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
100 | + const bool is_current) | |
101 | +{ | |
102 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
103 | + if (is_current) | |
104 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
105 | + ee, current->pid, tmp); | |
106 | + else | |
107 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
108 | + ee, tmp); | |
109 | +} | |
110 | + | |
111 | +#endif | |
112 | + | |
113 | +#if !defined(CONFIG_AKARI_DEBUG) | |
114 | +#define ccs_debug_trace(pos) do { } while (0) | |
115 | +#else | |
116 | +#define ccs_debug_trace(pos) \ | |
117 | + do { \ | |
118 | + static bool done; \ | |
119 | + if (!done) { \ | |
120 | + printk(KERN_INFO \ | |
121 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
122 | + done = true; \ | |
123 | + } \ | |
124 | + } while (0) | |
125 | +#endif | |
126 | + | |
127 | +/** | |
128 | + * ccs_clear_execve - Release memory used by do_execve(). | |
129 | + * | |
130 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
131 | + * @security: Pointer to "struct ccs_security". | |
132 | + * | |
133 | + * Returns nothing. | |
134 | + */ | |
135 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
136 | +{ | |
137 | + struct ccs_execve *ee; | |
138 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
139 | + return; | |
140 | + ee = security->ee; | |
141 | + security->ee = NULL; | |
142 | + if (!ee) | |
143 | + return; | |
144 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) | |
145 | + /* | |
146 | + * Drop refcount on "struct cred" in "struct linux_binprm" and forget | |
147 | + * it. | |
148 | + */ | |
149 | + put_cred(security->cred); | |
150 | + security->cred = NULL; | |
151 | +#endif | |
152 | + atomic_dec(&ccs_in_execve_tasks); | |
153 | + ccs_finish_execve(ret, ee); | |
154 | +} | |
155 | + | |
156 | +/** | |
157 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
158 | + * | |
159 | + * @rcu: Pointer to "struct rcu_head". | |
160 | + * | |
161 | + * Returns nothing. | |
162 | + */ | |
163 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
164 | +{ | |
165 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
166 | + struct ccs_execve *ee = ptr->ee; | |
167 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
168 | + /* | |
169 | + * If this security context was associated with "struct pid" and | |
170 | + * ptr->ccs_flags has CCS_TASK_IS_IN_EXECVE set, it indicates that a | |
171 | + * "struct task_struct" associated with this security context exited | |
172 | + * immediately after do_execve() has failed. | |
173 | + */ | |
174 | + if (ptr->pid && (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
175 | + ccs_debug_trace("1"); | |
176 | + atomic_dec(&ccs_in_execve_tasks); | |
177 | + } | |
178 | +#else | |
179 | + /* | |
180 | + * If this security context was associated with "struct pid" and | |
181 | + * remembers "struct cred" in "struct linux_binprm", it indicates that | |
182 | + * a "struct task_struct" associated with this security context exited | |
183 | + * immediately after do_execve() has failed. | |
184 | + */ | |
185 | + if (ptr->pid && ptr->cred) { | |
186 | + ccs_debug_trace("1"); | |
187 | + put_cred(ptr->cred); | |
188 | + atomic_dec(&ccs_in_execve_tasks); | |
189 | + } | |
190 | +#endif | |
191 | + /* | |
192 | + * If this security context was associated with "struct pid", | |
193 | + * drop refcount obtained by get_pid() in ccs_find_task_security(). | |
194 | + */ | |
195 | + if (ptr->pid) { | |
196 | + ccs_debug_trace("2"); | |
197 | + put_pid(ptr->pid); | |
198 | + } | |
199 | + if (ee) { | |
200 | + ccs_debug_trace("3"); | |
201 | + ccs_audit_free_execve(ee, false); | |
202 | + kfree(ee->handler_path); | |
203 | + kfree(ee); | |
204 | + } | |
205 | + kfree(ptr); | |
206 | +} | |
207 | + | |
208 | +/** | |
209 | + * ccs_del_security - Release "struct ccs_security". | |
210 | + * | |
211 | + * @ptr: Pointer to "struct ccs_security". | |
212 | + * | |
213 | + * Returns nothing. | |
214 | + */ | |
215 | +static void ccs_del_security(struct ccs_security *ptr) | |
216 | +{ | |
217 | + unsigned long flags; | |
218 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
219 | + return; | |
220 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
221 | + list_del_rcu(&ptr->list); | |
222 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
223 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
224 | +} | |
225 | + | |
226 | +/** | |
227 | + * ccs_add_cred_security - Add "struct ccs_security" to list. | |
228 | + * | |
229 | + * @ptr: Pointer to "struct ccs_security". | |
230 | + * | |
231 | + * Returns nothing. | |
232 | + */ | |
233 | +static void ccs_add_cred_security(struct ccs_security *ptr) | |
234 | +{ | |
235 | + unsigned long flags; | |
236 | + struct list_head *list = &ccs_cred_security_list | |
237 | + [hash_ptr((void *) ptr->cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
238 | +#ifdef CONFIG_AKARI_DEBUG | |
239 | + if (ptr->pid) | |
240 | + printk(KERN_INFO "AKARI: \"struct ccs_security\"->pid != NULL" | |
241 | + "\n"); | |
242 | +#endif | |
243 | + ptr->pid = NULL; | |
244 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
245 | + list_add_rcu(&ptr->list, list); | |
246 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
247 | +} | |
248 | + | |
249 | +/** | |
250 | + * ccs_task_create - Make snapshot of security context for new task. | |
251 | + * | |
252 | + * @clone_flags: Flags passed to clone(). | |
253 | + * | |
254 | + * Returns 0 on success, negative value otherwise. | |
255 | + */ | |
256 | +static int ccs_task_create(unsigned long clone_flags) | |
257 | +{ | |
258 | + int rc; | |
259 | + struct ccs_security *old_security; | |
260 | + struct ccs_security *new_security; | |
261 | + struct cred *cred = prepare_creds(); | |
262 | + if (!cred) | |
263 | + return -ENOMEM; | |
264 | + while (!original_security_ops.task_create); | |
265 | + rc = original_security_ops.task_create(clone_flags); | |
266 | + if (rc) { | |
267 | + abort_creds(cred); | |
268 | + return rc; | |
269 | + } | |
270 | + old_security = ccs_find_task_security(current); | |
271 | + new_security = ccs_find_cred_security(cred); | |
272 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
273 | + new_security->ccs_flags = old_security->ccs_flags; | |
274 | + return commit_creds(cred); | |
275 | +} | |
276 | + | |
277 | +/** | |
278 | + * ccs_cred_prepare - Allocate memory for new credentials. | |
279 | + * | |
280 | + * @new: Pointer to "struct cred". | |
281 | + * @old: Pointer to "struct cred". | |
282 | + * @gfp: Memory allocation flags. | |
283 | + * | |
284 | + * Returns 0 on success, negative value otherwise. | |
285 | + */ | |
286 | +static int ccs_cred_prepare(struct cred *new, const struct cred *old, | |
287 | + gfp_t gfp) | |
288 | +{ | |
289 | + int rc; | |
290 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
291 | + /* | |
292 | + * For checking whether reverting domain transition is needed or not. | |
293 | + * | |
294 | + * See ccs_find_task_security() for reason. | |
295 | + */ | |
296 | + if (gfp == GFP_KERNEL) | |
297 | + ccs_find_task_security(current); | |
298 | +#endif | |
299 | + rc = ccs_copy_cred_security(new, old, gfp); | |
300 | + if (rc) | |
301 | + return rc; | |
302 | + if (gfp == GFP_KERNEL) | |
303 | + ccs_task_security_gc(); | |
304 | + while (!original_security_ops.cred_prepare); | |
305 | + rc = original_security_ops.cred_prepare(new, old, gfp); | |
306 | + if (rc) | |
307 | + ccs_del_security(ccs_find_cred_security(new)); | |
308 | + return rc; | |
309 | +} | |
310 | + | |
311 | +/** | |
312 | + * ccs_cred_free - Release memory used by credentials. | |
313 | + * | |
314 | + * @cred: Pointer to "struct cred". | |
315 | + * | |
316 | + * Returns nothing. | |
317 | + */ | |
318 | +static void ccs_cred_free(struct cred *cred) | |
319 | +{ | |
320 | + while (!original_security_ops.cred_free); | |
321 | + original_security_ops.cred_free(cred); | |
322 | + ccs_del_security(ccs_find_cred_security(cred)); | |
323 | +} | |
324 | + | |
325 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
326 | + | |
327 | +/** | |
328 | + * ccs_alloc_cred_security - Allocate memory for new credentials. | |
329 | + * | |
330 | + * @cred: Pointer to "struct cred". | |
331 | + * @gfp: Memory allocation flags. | |
332 | + * | |
333 | + * Returns 0 on success, negative value otherwise. | |
334 | + */ | |
335 | +static int ccs_alloc_cred_security(const struct cred *cred, gfp_t gfp) | |
336 | +{ | |
337 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
338 | + gfp); | |
339 | + if (!new_security) | |
340 | + return -ENOMEM; | |
341 | + new_security->cred = cred; | |
342 | + ccs_add_cred_security(new_security); | |
343 | + return 0; | |
344 | +} | |
345 | + | |
346 | +/** | |
347 | + * ccs_cred_alloc_blank - Allocate memory for new credentials. | |
348 | + * | |
349 | + * @new: Pointer to "struct cred". | |
350 | + * @gfp: Memory allocation flags. | |
351 | + * | |
352 | + * Returns 0 on success, negative value otherwise. | |
353 | + */ | |
354 | +static int ccs_cred_alloc_blank(struct cred *new, gfp_t gfp) | |
355 | +{ | |
356 | + int rc = ccs_alloc_cred_security(new, gfp); | |
357 | + if (rc) | |
358 | + return rc; | |
359 | + while (!original_security_ops.cred_alloc_blank); | |
360 | + rc = original_security_ops.cred_alloc_blank(new, gfp); | |
361 | + if (rc) | |
362 | + ccs_del_security(ccs_find_cred_security(new)); | |
363 | + return rc; | |
364 | +} | |
365 | + | |
366 | +/** | |
367 | + * ccs_cred_transfer - Transfer "struct ccs_security" between credentials. | |
368 | + * | |
369 | + * @new: Pointer to "struct cred". | |
370 | + * @old: Pointer to "struct cred". | |
371 | + * | |
372 | + * Returns nothing. | |
373 | + */ | |
374 | +static void ccs_cred_transfer(struct cred *new, const struct cred *old) | |
375 | +{ | |
376 | + struct ccs_security *new_security; | |
377 | + struct ccs_security *old_security; | |
378 | + while (!original_security_ops.cred_transfer); | |
379 | + original_security_ops.cred_transfer(new, old); | |
380 | + new_security = ccs_find_cred_security(new); | |
381 | + old_security = ccs_find_cred_security(old); | |
382 | + if (new_security == &ccs_default_security || | |
383 | + new_security == &ccs_oom_security || | |
384 | + old_security == &ccs_default_security || | |
385 | + old_security == &ccs_oom_security) | |
386 | + return; | |
387 | + new_security->ccs_flags = old_security->ccs_flags; | |
388 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
389 | +} | |
390 | + | |
391 | +#endif | |
392 | + | |
393 | +/** | |
394 | + * ccs_bprm_committing_creds - A hook which is called when do_execve() succeeded. | |
395 | + * | |
396 | + * @bprm: Pointer to "struct linux_binprm". | |
397 | + * | |
398 | + * Returns nothing. | |
399 | + */ | |
400 | +static void ccs_bprm_committing_creds(struct linux_binprm *bprm) | |
401 | +{ | |
402 | + struct ccs_security *old_security; | |
403 | + struct ccs_security *new_security; | |
404 | + while (!original_security_ops.bprm_committing_creds); | |
405 | + original_security_ops.bprm_committing_creds(bprm); | |
406 | + old_security = ccs_current_security(); | |
407 | + if (old_security == &ccs_default_security || | |
408 | + old_security == &ccs_oom_security) | |
409 | + return; | |
410 | + ccs_clear_execve(0, old_security); | |
411 | + /* Update current task's cred's domain for future fork(). */ | |
412 | + new_security = ccs_find_cred_security(bprm->cred); | |
413 | + new_security->ccs_flags = old_security->ccs_flags; | |
414 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
415 | +} | |
416 | + | |
417 | +/** | |
418 | + * ccs_bprm_check_security - Check permission for execve(). | |
419 | + * | |
420 | + * @bprm: Pointer to "struct linux_binprm". | |
421 | + * | |
422 | + * Returns 0 on success, negative value otherwise. | |
423 | + */ | |
424 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
425 | +{ | |
426 | + struct ccs_security *security = ccs_current_security(); | |
427 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
428 | + return -ENOMEM; | |
429 | + if (!security->ee) { | |
430 | + int rc; | |
431 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
432 | + if (!ccs_policy_loaded) | |
433 | + ccs_load_policy(bprm->filename); | |
434 | +#endif | |
435 | + rc = ccs_start_execve(bprm, &security->ee); | |
436 | + if (security->ee) { | |
437 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) | |
438 | + /* | |
439 | + * Get refcount on "struct cred" in | |
440 | + * "struct linux_binprm" and remember it. | |
441 | + */ | |
442 | + get_cred(bprm->cred); | |
443 | + security->cred = bprm->cred; | |
444 | +#endif | |
445 | + atomic_inc(&ccs_in_execve_tasks); | |
446 | + } | |
447 | + if (rc) | |
448 | + return rc; | |
449 | + } | |
450 | + while (!original_security_ops.bprm_check_security); | |
451 | + return original_security_ops.bprm_check_security(bprm); | |
452 | +} | |
453 | + | |
454 | +/** | |
455 | + * ccs_open - Check permission for open(). | |
456 | + * | |
457 | + * @f: Pointer to "struct file". | |
458 | + * | |
459 | + * Returns 0 on success, negative value otherwise. | |
460 | + */ | |
461 | +static int ccs_open(struct file *f) | |
462 | +{ | |
463 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
464 | + return ccs_open_permission(f); | |
465 | +#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 6 | |
466 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
467 | + f->f_flags); | |
468 | +#else | |
469 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
470 | + f->f_flags + 1); | |
471 | +#endif | |
472 | +} | |
473 | + | |
474 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
475 | + | |
476 | +/** | |
477 | + * ccs_file_open - Check permission for open(). | |
478 | + * | |
479 | + * @f: Pointer to "struct file". | |
480 | + * @cred: Pointer to "struct cred". | |
481 | + * | |
482 | + * Returns 0 on success, negative value otherwise. | |
483 | + */ | |
484 | +static int ccs_file_open(struct file *f, const struct cred *cred) | |
485 | +{ | |
486 | + int rc = ccs_open(f); | |
487 | + if (rc) | |
488 | + return rc; | |
489 | + while (!original_security_ops.file_open); | |
490 | + return original_security_ops.file_open(f, cred); | |
491 | +} | |
492 | + | |
493 | +#else | |
494 | + | |
495 | +/** | |
496 | + * ccs_dentry_open - Check permission for open(). | |
497 | + * | |
498 | + * @f: Pointer to "struct file". | |
499 | + * @cred: Pointer to "struct cred". | |
500 | + * | |
501 | + * Returns 0 on success, negative value otherwise. | |
502 | + */ | |
503 | +static int ccs_dentry_open(struct file *f, const struct cred *cred) | |
504 | +{ | |
505 | + int rc = ccs_open(f); | |
506 | + if (rc) | |
507 | + return rc; | |
508 | + while (!original_security_ops.dentry_open); | |
509 | + return original_security_ops.dentry_open(f, cred); | |
510 | +} | |
511 | + | |
512 | +#endif | |
513 | + | |
514 | +#if defined(CONFIG_SECURITY_PATH) | |
515 | + | |
516 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
517 | + | |
518 | +/** | |
519 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
520 | + * | |
521 | + * @path: Pointer to "struct path". | |
522 | + * @user: User ID. | |
523 | + * @group: Group ID. | |
524 | + * | |
525 | + * Returns 0 on success, negative value otherwise. | |
526 | + */ | |
527 | +static int ccs_path_chown(struct path *path, kuid_t user, kgid_t group) | |
528 | +{ | |
529 | + int rc = ccs_chown_permission(path->dentry, path->mnt, user, group); | |
530 | + if (rc) | |
531 | + return rc; | |
532 | + while (!original_security_ops.path_chown); | |
533 | + return original_security_ops.path_chown(path, user, group); | |
534 | +} | |
535 | + | |
536 | +/** | |
537 | + * ccs_path_chmod - Check permission for chmod(). | |
538 | + * | |
539 | + * @path: Pointer to "struct path". | |
540 | + * @mode: Mode. | |
541 | + * | |
542 | + * Returns 0 on success, negative value otherwise. | |
543 | + */ | |
544 | +static int ccs_path_chmod(struct path *path, umode_t mode) | |
545 | +{ | |
546 | + int rc = ccs_chmod_permission(path->dentry, path->mnt, mode); | |
547 | + if (rc) | |
548 | + return rc; | |
549 | + while (!original_security_ops.path_chmod); | |
550 | + return original_security_ops.path_chmod(path, mode); | |
551 | +} | |
552 | + | |
553 | +/** | |
554 | + * ccs_path_chroot - Check permission for chroot(). | |
555 | + * | |
556 | + * @path: Pointer to "struct path". | |
557 | + * | |
558 | + * Returns 0 on success, negative value otherwise. | |
559 | + */ | |
560 | +static int ccs_path_chroot(struct path *path) | |
561 | +{ | |
562 | + int rc = ccs_chroot_permission(path); | |
563 | + if (rc) | |
564 | + return rc; | |
565 | + while (!original_security_ops.path_chroot); | |
566 | + return original_security_ops.path_chroot(path); | |
567 | +} | |
568 | + | |
569 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
570 | + | |
571 | +/** | |
572 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
573 | + * | |
574 | + * @path: Pointer to "struct path". | |
575 | + * @user: User ID. | |
576 | + * @group: Group ID. | |
577 | + * | |
578 | + * Returns 0 on success, negative value otherwise. | |
579 | + */ | |
580 | +static int ccs_path_chown(struct path *path, uid_t user, gid_t group) | |
581 | +{ | |
582 | + int rc = ccs_chown_permission(path->dentry, path->mnt, user, group); | |
583 | + if (rc) | |
584 | + return rc; | |
585 | + while (!original_security_ops.path_chown); | |
586 | + return original_security_ops.path_chown(path, user, group); | |
587 | +} | |
588 | + | |
589 | +#if defined(USE_UMODE_T) | |
590 | + | |
591 | +/** | |
592 | + * ccs_path_chmod - Check permission for chmod(). | |
593 | + * | |
594 | + * @path: Pointer to "struct path". | |
595 | + * @mode: Mode. | |
596 | + * | |
597 | + * Returns 0 on success, negative value otherwise. | |
598 | + */ | |
599 | +static int ccs_path_chmod(struct path *path, umode_t mode) | |
600 | +{ | |
601 | + int rc = ccs_chmod_permission(path->dentry, path->mnt, mode); | |
602 | + if (rc) | |
603 | + return rc; | |
604 | + while (!original_security_ops.path_chmod); | |
605 | + return original_security_ops.path_chmod(path, mode); | |
606 | +} | |
607 | + | |
608 | +#else | |
609 | + | |
610 | +/** | |
611 | + * ccs_path_chmod - Check permission for chmod(). | |
612 | + * | |
613 | + * @dentry: Pointer to "struct dentry". | |
614 | + * @vfsmnt: Pointer to "struct vfsmount". | |
615 | + * @mode: Mode. | |
616 | + * | |
617 | + * Returns 0 on success, negative value otherwise. | |
618 | + */ | |
619 | +static int ccs_path_chmod(struct dentry *dentry, struct vfsmount *vfsmnt, | |
620 | + mode_t mode) | |
621 | +{ | |
622 | + int rc = ccs_chmod_permission(dentry, vfsmnt, mode); | |
623 | + if (rc) | |
624 | + return rc; | |
625 | + while (!original_security_ops.path_chmod); | |
626 | + return original_security_ops.path_chmod(dentry, vfsmnt, mode); | |
627 | +} | |
628 | + | |
629 | +#endif | |
630 | + | |
631 | +/** | |
632 | + * ccs_path_chroot - Check permission for chroot(). | |
633 | + * | |
634 | + * @path: Pointer to "struct path". | |
635 | + * | |
636 | + * Returns 0 on success, negative value otherwise. | |
637 | + */ | |
638 | +static int ccs_path_chroot(struct path *path) | |
639 | +{ | |
640 | + int rc = ccs_chroot_permission(path); | |
641 | + if (rc) | |
642 | + return rc; | |
643 | + while (!original_security_ops.path_chroot); | |
644 | + return original_security_ops.path_chroot(path); | |
645 | +} | |
646 | + | |
647 | +#endif | |
648 | + | |
649 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) | |
650 | + | |
651 | +/** | |
652 | + * ccs_path_truncate - Check permission for truncate(). | |
653 | + * | |
654 | + * @path: Pointer to "struct path". | |
655 | + * | |
656 | + * Returns 0 on success, negative value otherwise. | |
657 | + */ | |
658 | +static int ccs_path_truncate(struct path *path) | |
659 | +{ | |
660 | + int rc = ccs_truncate_permission(path->dentry, path->mnt); | |
661 | + if (rc) | |
662 | + return rc; | |
663 | + while (!original_security_ops.path_truncate); | |
664 | + return original_security_ops.path_truncate(path); | |
665 | +} | |
666 | + | |
667 | +#else | |
668 | + | |
669 | +/** | |
670 | + * ccs_path_truncate - Check permission for truncate(). | |
671 | + * | |
672 | + * @path: Pointer to "struct path". | |
673 | + * @length: New length. | |
674 | + * @time_attrs: New time attributes. | |
675 | + * | |
676 | + * Returns 0 on success, negative value otherwise. | |
677 | + */ | |
678 | +static int ccs_path_truncate(struct path *path, loff_t length, | |
679 | + unsigned int time_attrs) | |
680 | +{ | |
681 | + int rc = ccs_truncate_permission(path->dentry, path->mnt); | |
682 | + if (rc) | |
683 | + return rc; | |
684 | + while (!original_security_ops.path_truncate); | |
685 | + return original_security_ops.path_truncate(path, length, time_attrs); | |
686 | +} | |
687 | + | |
688 | +#endif | |
689 | + | |
690 | +#endif | |
691 | + | |
692 | +/** | |
693 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
694 | + * | |
695 | + * @dentry: Pointer to "struct dentry". | |
696 | + * @attr: Pointer to "struct iattr". | |
697 | + * | |
698 | + * Returns 0 on success, negative value otherwise. | |
699 | + */ | |
700 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
701 | +{ | |
702 | + int rc = 0; | |
703 | +#if !defined(CONFIG_SECURITY_PATH) || LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) | |
704 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
705 | + if (attr->ia_valid & ATTR_UID) | |
706 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, | |
707 | + INVALID_GID); | |
708 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
709 | + rc = ccs_chown_permission(dentry, NULL, INVALID_UID, | |
710 | + attr->ia_gid); | |
711 | +#else | |
712 | + if (attr->ia_valid & ATTR_UID) | |
713 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, -1); | |
714 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
715 | + rc = ccs_chown_permission(dentry, NULL, -1, attr->ia_gid); | |
716 | +#endif | |
717 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
718 | + rc = ccs_chmod_permission(dentry, NULL, attr->ia_mode); | |
719 | +#endif | |
720 | +#if !defined(CONFIG_SECURITY_PATH) | |
721 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
722 | + rc = ccs_truncate_permission(dentry, NULL); | |
723 | +#endif | |
724 | + if (rc) | |
725 | + return rc; | |
726 | + while (!original_security_ops.inode_setattr); | |
727 | + return original_security_ops.inode_setattr(dentry, attr); | |
728 | +} | |
729 | + | |
730 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) | |
731 | + | |
732 | +/** | |
733 | + * ccs_inode_getattr - Check permission for stat(). | |
734 | + * | |
735 | + * @path: Pointer to "struct path". | |
736 | + * | |
737 | + * Returns 0 on success, negative value otherwise. | |
738 | + */ | |
739 | +static int ccs_inode_getattr(const struct path *path) | |
740 | +{ | |
741 | + int rc = ccs_getattr_permission(path->mnt, path->dentry); | |
742 | + if (rc) | |
743 | + return rc; | |
744 | + while (!original_security_ops.inode_getattr); | |
745 | + return original_security_ops.inode_getattr(path); | |
746 | +} | |
747 | + | |
748 | +#else | |
749 | + | |
750 | +/** | |
751 | + * ccs_inode_getattr - Check permission for stat(). | |
752 | + * | |
753 | + * @mnt: Pointer to "struct vfsmount". | |
754 | + * @dentry: Pointer to "struct dentry". | |
755 | + * | |
756 | + * Returns 0 on success, negative value otherwise. | |
757 | + */ | |
758 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
759 | +{ | |
760 | + int rc = ccs_getattr_permission(mnt, dentry); | |
761 | + if (rc) | |
762 | + return rc; | |
763 | + while (!original_security_ops.inode_getattr); | |
764 | + return original_security_ops.inode_getattr(mnt, dentry); | |
765 | +} | |
766 | + | |
767 | +#endif | |
768 | + | |
769 | +#if defined(CONFIG_SECURITY_PATH) | |
770 | + | |
771 | +#if defined(USE_UMODE_T) | |
772 | + | |
773 | +/** | |
774 | + * ccs_path_mknod - Check permission for mknod(). | |
775 | + * | |
776 | + * @dir: Pointer to "struct path". | |
777 | + * @dentry: Pointer to "struct dentry". | |
778 | + * @mode: Create mode. | |
779 | + * @dev: Device major/minor number. | |
780 | + * | |
781 | + * Returns 0 on success, negative value otherwise. | |
782 | + */ | |
783 | +static int ccs_path_mknod(struct path *dir, struct dentry *dentry, | |
784 | + umode_t mode, unsigned int dev) | |
785 | +{ | |
786 | + int rc = ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
787 | + if (rc) | |
788 | + return rc; | |
789 | + while (!original_security_ops.path_mknod); | |
790 | + return original_security_ops.path_mknod(dir, dentry, mode, dev); | |
791 | +} | |
792 | + | |
793 | +/** | |
794 | + * ccs_path_mkdir - Check permission for mkdir(). | |
795 | + * | |
796 | + * @dir: Pointer to "struct path". | |
797 | + * @dentry: Pointer to "struct dentry". | |
798 | + * @mode: Create mode. | |
799 | + * | |
800 | + * Returns 0 on success, negative value otherwise. | |
801 | + */ | |
802 | +static int ccs_path_mkdir(struct path *dir, struct dentry *dentry, | |
803 | + umode_t mode) | |
804 | +{ | |
805 | + int rc = ccs_mkdir_permission(dentry, dir->mnt, mode); | |
806 | + if (rc) | |
807 | + return rc; | |
808 | + while (!original_security_ops.path_mkdir); | |
809 | + return original_security_ops.path_mkdir(dir, dentry, mode); | |
810 | +} | |
811 | + | |
812 | +#else | |
813 | + | |
814 | +/** | |
815 | + * ccs_path_mknod - Check permission for mknod(). | |
816 | + * | |
817 | + * @dir: Pointer to "struct path". | |
818 | + * @dentry: Pointer to "struct dentry". | |
819 | + * @mode: Create mode. | |
820 | + * @dev: Device major/minor number. | |
821 | + * | |
822 | + * Returns 0 on success, negative value otherwise. | |
823 | + */ | |
824 | +static int ccs_path_mknod(struct path *dir, struct dentry *dentry, int mode, | |
825 | + unsigned int dev) | |
826 | +{ | |
827 | + int rc = ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
828 | + if (rc) | |
829 | + return rc; | |
830 | + while (!original_security_ops.path_mknod); | |
831 | + return original_security_ops.path_mknod(dir, dentry, mode, dev); | |
832 | +} | |
833 | + | |
834 | +/** | |
835 | + * ccs_path_mkdir - Check permission for mkdir(). | |
836 | + * | |
837 | + * @dir: Pointer to "struct path". | |
838 | + * @dentry: Pointer to "struct dentry". | |
839 | + * @mode: Create mode. | |
840 | + * | |
841 | + * Returns 0 on success, negative value otherwise. | |
842 | + */ | |
843 | +static int ccs_path_mkdir(struct path *dir, struct dentry *dentry, int mode) | |
844 | +{ | |
845 | + int rc = ccs_mkdir_permission(dentry, dir->mnt, mode); | |
846 | + if (rc) | |
847 | + return rc; | |
848 | + while (!original_security_ops.path_mkdir); | |
849 | + return original_security_ops.path_mkdir(dir, dentry, mode); | |
850 | +} | |
851 | + | |
852 | +#endif | |
853 | + | |
854 | +/** | |
855 | + * ccs_path_rmdir - Check permission for rmdir(). | |
856 | + * | |
857 | + * @dir: Pointer to "struct path". | |
858 | + * @dentry: Pointer to "struct dentry". | |
859 | + * | |
860 | + * Returns 0 on success, negative value otherwise. | |
861 | + */ | |
862 | +static int ccs_path_rmdir(struct path *dir, struct dentry *dentry) | |
863 | +{ | |
864 | + int rc = ccs_rmdir_permission(dentry, dir->mnt); | |
865 | + if (rc) | |
866 | + return rc; | |
867 | + while (!original_security_ops.path_rmdir); | |
868 | + return original_security_ops.path_rmdir(dir, dentry); | |
869 | +} | |
870 | + | |
871 | +/** | |
872 | + * ccs_path_unlink - Check permission for unlink(). | |
873 | + * | |
874 | + * @dir: Pointer to "struct path". | |
875 | + * @dentry: Pointer to "struct dentry". | |
876 | + * | |
877 | + * Returns 0 on success, negative value otherwise. | |
878 | + */ | |
879 | +static int ccs_path_unlink(struct path *dir, struct dentry *dentry) | |
880 | +{ | |
881 | + int rc = ccs_unlink_permission(dentry, dir->mnt); | |
882 | + if (rc) | |
883 | + return rc; | |
884 | + while (!original_security_ops.path_unlink); | |
885 | + return original_security_ops.path_unlink(dir, dentry); | |
886 | +} | |
887 | + | |
888 | +/** | |
889 | + * ccs_path_symlink - Check permission for symlink(). | |
890 | + * | |
891 | + * @dir: Pointer to "struct path". | |
892 | + * @dentry: Pointer to "struct dentry". | |
893 | + * @old_name: Content of symbolic link. | |
894 | + * | |
895 | + * Returns 0 on success, negative value otherwise. | |
896 | + */ | |
897 | +static int ccs_path_symlink(struct path *dir, struct dentry *dentry, | |
898 | + const char *old_name) | |
899 | +{ | |
900 | + int rc = ccs_symlink_permission(dentry, dir->mnt, old_name); | |
901 | + if (rc) | |
902 | + return rc; | |
903 | + while (!original_security_ops.path_symlink); | |
904 | + return original_security_ops.path_symlink(dir, dentry, old_name); | |
905 | +} | |
906 | + | |
907 | +/** | |
908 | + * ccs_path_rename - Check permission for rename(). | |
909 | + * | |
910 | + * @old_dir: Pointer to "struct path". | |
911 | + * @old_dentry: Pointer to "struct dentry". | |
912 | + * @new_dir: Pointer to "struct path". | |
913 | + * @new_dentry: Pointer to "struct dentry". | |
914 | + * | |
915 | + * Returns 0 on success, negative value otherwise. | |
916 | + */ | |
917 | +static int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry, | |
918 | + struct path *new_dir, struct dentry *new_dentry) | |
919 | +{ | |
920 | + int rc = ccs_rename_permission(old_dentry, new_dentry, old_dir->mnt); | |
921 | + if (rc) | |
922 | + return rc; | |
923 | + while (!original_security_ops.path_rename); | |
924 | + return original_security_ops.path_rename(old_dir, old_dentry, new_dir, | |
925 | + new_dentry); | |
926 | +} | |
927 | + | |
928 | +/** | |
929 | + * ccs_path_link - Check permission for link(). | |
930 | + * | |
931 | + * @old_dentry: Pointer to "struct dentry". | |
932 | + * @new_dir: Pointer to "struct path". | |
933 | + * @new_dentry: Pointer to "struct dentry". | |
934 | + * | |
935 | + * Returns 0 on success, negative value otherwise. | |
936 | + */ | |
937 | +static int ccs_path_link(struct dentry *old_dentry, struct path *new_dir, | |
938 | + struct dentry *new_dentry) | |
939 | +{ | |
940 | + int rc = ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); | |
941 | + if (rc) | |
942 | + return rc; | |
943 | + while (!original_security_ops.path_link); | |
944 | + return original_security_ops.path_link(old_dentry, new_dir, | |
945 | + new_dentry); | |
946 | +} | |
947 | + | |
948 | +#else | |
949 | + | |
950 | +#if defined(USE_UMODE_T) | |
951 | + | |
952 | +/** | |
953 | + * ccs_inode_mknod - Check permission for mknod(). | |
954 | + * | |
955 | + * @dir: Pointer to "struct inode". | |
956 | + * @dentry: Pointer to "struct dentry". | |
957 | + * @mode: Create mode. | |
958 | + * @dev: Device major/minor number. | |
959 | + * | |
960 | + * Returns 0 on success, negative value otherwise. | |
961 | + */ | |
962 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
963 | + umode_t mode, dev_t dev) | |
964 | +{ | |
965 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
966 | + if (rc) | |
967 | + return rc; | |
968 | + while (!original_security_ops.inode_mknod); | |
969 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
970 | +} | |
971 | + | |
972 | +/** | |
973 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
974 | + * | |
975 | + * @dir: Pointer to "struct inode". | |
976 | + * @dentry: Pointer to "struct dentry". | |
977 | + * @mode: Create mode. | |
978 | + * | |
979 | + * Returns 0 on success, negative value otherwise. | |
980 | + */ | |
981 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
982 | + umode_t mode) | |
983 | +{ | |
984 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
985 | + if (rc) | |
986 | + return rc; | |
987 | + while (!original_security_ops.inode_mkdir); | |
988 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
989 | +} | |
990 | + | |
991 | +#else | |
992 | + | |
993 | +/** | |
994 | + * ccs_inode_mknod - Check permission for mknod(). | |
995 | + * | |
996 | + * @dir: Pointer to "struct inode". | |
997 | + * @dentry: Pointer to "struct dentry". | |
998 | + * @mode: Create mode. | |
999 | + * @dev: Device major/minor number. | |
1000 | + * | |
1001 | + * Returns 0 on success, negative value otherwise. | |
1002 | + */ | |
1003 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
1004 | + dev_t dev) | |
1005 | +{ | |
1006 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
1007 | + if (rc) | |
1008 | + return rc; | |
1009 | + while (!original_security_ops.inode_mknod); | |
1010 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
1011 | +} | |
1012 | + | |
1013 | +/** | |
1014 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
1015 | + * | |
1016 | + * @dir: Pointer to "struct inode". | |
1017 | + * @dentry: Pointer to "struct dentry". | |
1018 | + * @mode: Create mode. | |
1019 | + * | |
1020 | + * Returns 0 on success, negative value otherwise. | |
1021 | + */ | |
1022 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1023 | +{ | |
1024 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
1025 | + if (rc) | |
1026 | + return rc; | |
1027 | + while (!original_security_ops.inode_mkdir); | |
1028 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
1029 | +} | |
1030 | + | |
1031 | +#endif | |
1032 | + | |
1033 | +/** | |
1034 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
1035 | + * | |
1036 | + * @dir: Pointer to "struct inode". | |
1037 | + * @dentry: Pointer to "struct dentry". | |
1038 | + * | |
1039 | + * Returns 0 on success, negative value otherwise. | |
1040 | + */ | |
1041 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
1042 | +{ | |
1043 | + int rc = ccs_rmdir_permission(dentry, NULL); | |
1044 | + if (rc) | |
1045 | + return rc; | |
1046 | + while (!original_security_ops.inode_rmdir); | |
1047 | + return original_security_ops.inode_rmdir(dir, dentry); | |
1048 | +} | |
1049 | + | |
1050 | +/** | |
1051 | + * ccs_inode_unlink - Check permission for unlink(). | |
1052 | + * | |
1053 | + * @dir: Pointer to "struct inode". | |
1054 | + * @dentry: Pointer to "struct dentry". | |
1055 | + * | |
1056 | + * Returns 0 on success, negative value otherwise. | |
1057 | + */ | |
1058 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
1059 | +{ | |
1060 | + int rc = ccs_unlink_permission(dentry, NULL); | |
1061 | + if (rc) | |
1062 | + return rc; | |
1063 | + while (!original_security_ops.inode_unlink); | |
1064 | + return original_security_ops.inode_unlink(dir, dentry); | |
1065 | +} | |
1066 | + | |
1067 | +/** | |
1068 | + * ccs_inode_symlink - Check permission for symlink(). | |
1069 | + * | |
1070 | + * @dir: Pointer to "struct inode". | |
1071 | + * @dentry: Pointer to "struct dentry". | |
1072 | + * @old_name: Content of symbolic link. | |
1073 | + * | |
1074 | + * Returns 0 on success, negative value otherwise. | |
1075 | + */ | |
1076 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
1077 | + const char *old_name) | |
1078 | +{ | |
1079 | + int rc = ccs_symlink_permission(dentry, NULL, old_name); | |
1080 | + if (rc) | |
1081 | + return rc; | |
1082 | + while (!original_security_ops.inode_symlink); | |
1083 | + return original_security_ops.inode_symlink(dir, dentry, old_name); | |
1084 | +} | |
1085 | + | |
1086 | +/** | |
1087 | + * ccs_inode_rename - Check permission for rename(). | |
1088 | + * | |
1089 | + * @old_dir: Pointer to "struct inode". | |
1090 | + * @old_dentry: Pointer to "struct dentry". | |
1091 | + * @new_dir: Pointer to "struct inode". | |
1092 | + * @new_dentry: Pointer to "struct dentry". | |
1093 | + * | |
1094 | + * Returns 0 on success, negative value otherwise. | |
1095 | + */ | |
1096 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
1097 | + struct inode *new_dir, struct dentry *new_dentry) | |
1098 | +{ | |
1099 | + int rc = ccs_rename_permission(old_dentry, new_dentry, NULL); | |
1100 | + if (rc) | |
1101 | + return rc; | |
1102 | + while (!original_security_ops.inode_rename); | |
1103 | + return original_security_ops.inode_rename(old_dir, old_dentry, new_dir, | |
1104 | + new_dentry); | |
1105 | +} | |
1106 | + | |
1107 | +/** | |
1108 | + * ccs_inode_link - Check permission for link(). | |
1109 | + * | |
1110 | + * @old_dentry: Pointer to "struct dentry". | |
1111 | + * @dir: Pointer to "struct inode". | |
1112 | + * @new_dentry: Pointer to "struct dentry". | |
1113 | + * | |
1114 | + * Returns 0 on success, negative value otherwise. | |
1115 | + */ | |
1116 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
1117 | + struct dentry *new_dentry) | |
1118 | +{ | |
1119 | + int rc = ccs_link_permission(old_dentry, new_dentry, NULL); | |
1120 | + if (rc) | |
1121 | + return rc; | |
1122 | + while (!original_security_ops.inode_link); | |
1123 | + return original_security_ops.inode_link(old_dentry, dir, new_dentry); | |
1124 | +} | |
1125 | + | |
1126 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) | |
1127 | + | |
1128 | +/** | |
1129 | + * ccs_inode_create - Check permission for creat(). | |
1130 | + * | |
1131 | + * @dir: Pointer to "struct inode". | |
1132 | + * @dentry: Pointer to "struct dentry". | |
1133 | + * @mode: Create mode. | |
1134 | + * | |
1135 | + * Returns 0 on success, negative value otherwise. | |
1136 | + */ | |
1137 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
1138 | + umode_t mode) | |
1139 | +{ | |
1140 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
1141 | + if (rc) | |
1142 | + return rc; | |
1143 | + while (!original_security_ops.inode_create); | |
1144 | + return original_security_ops.inode_create(dir, dentry, mode); | |
1145 | +} | |
1146 | + | |
1147 | +#else | |
1148 | + | |
1149 | +/** | |
1150 | + * ccs_inode_create - Check permission for creat(). | |
1151 | + * | |
1152 | + * @dir: Pointer to "struct inode". | |
1153 | + * @dentry: Pointer to "struct dentry". | |
1154 | + * @mode: Create mode. | |
1155 | + * | |
1156 | + * Returns 0 on success, negative value otherwise. | |
1157 | + */ | |
1158 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
1159 | + int mode) | |
1160 | +{ | |
1161 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
1162 | + if (rc) | |
1163 | + return rc; | |
1164 | + while (!original_security_ops.inode_create); | |
1165 | + return original_security_ops.inode_create(dir, dentry, mode); | |
1166 | +} | |
1167 | + | |
1168 | +#endif | |
1169 | + | |
1170 | +#endif | |
1171 | + | |
1172 | +#ifdef CONFIG_SECURITY_NETWORK | |
1173 | + | |
1174 | +#include <net/sock.h> | |
1175 | + | |
1176 | +/* Structure for remembering an accept()ed socket's status. */ | |
1177 | +struct ccs_socket_tag { | |
1178 | + struct list_head list; | |
1179 | + struct inode *inode; | |
1180 | + int status; | |
1181 | + struct rcu_head rcu; | |
1182 | +}; | |
1183 | + | |
1184 | +/* | |
1185 | + * List for managing accept()ed sockets. | |
1186 | + * Since we don't need to keep an accept()ed socket into this list after | |
1187 | + * once the permission was granted, the number of entries in this list is | |
1188 | + * likely small. Therefore, we don't use hash tables. | |
1189 | + */ | |
1190 | +static LIST_HEAD(ccs_accepted_socket_list); | |
1191 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
1192 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
1193 | + | |
1194 | +/** | |
1195 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
1196 | + * | |
1197 | + * @rcu: Pointer to "struct rcu_head". | |
1198 | + * | |
1199 | + * Returns nothing. | |
1200 | + */ | |
1201 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
1202 | +{ | |
1203 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
1204 | + kfree(ptr); | |
1205 | +} | |
1206 | + | |
1207 | +/** | |
1208 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
1209 | + * | |
1210 | + * @inode: Pointer to "struct inode". | |
1211 | + * @status: New status. | |
1212 | + * | |
1213 | + * Returns nothing. | |
1214 | + * | |
1215 | + * If @status == 0, memory for that socket will be released after RCU grace | |
1216 | + * period. | |
1217 | + */ | |
1218 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
1219 | +{ | |
1220 | + struct ccs_socket_tag *ptr; | |
1221 | + /* | |
1222 | + * Protect whole section because multiple threads may call this | |
1223 | + * function with same "sock" via ccs_validate_socket(). | |
1224 | + */ | |
1225 | + spin_lock(&ccs_accepted_socket_list_lock); | |
1226 | + rcu_read_lock(); | |
1227 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
1228 | + if (ptr->inode != inode) | |
1229 | + continue; | |
1230 | + ptr->status = status; | |
1231 | + if (status) | |
1232 | + break; | |
1233 | + list_del_rcu(&ptr->list); | |
1234 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
1235 | + break; | |
1236 | + } | |
1237 | + rcu_read_unlock(); | |
1238 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
1239 | +} | |
1240 | + | |
1241 | +/** | |
1242 | + * ccs_validate_socket - Check post accept() permission if needed. | |
1243 | + * | |
1244 | + * @sock: Pointer to "struct socket". | |
1245 | + * | |
1246 | + * Returns 0 on success, negative value otherwise. | |
1247 | + */ | |
1248 | +static int ccs_validate_socket(struct socket *sock) | |
1249 | +{ | |
1250 | + struct inode *inode = SOCK_INODE(sock); | |
1251 | + struct ccs_socket_tag *ptr; | |
1252 | + int ret = 0; | |
1253 | + rcu_read_lock(); | |
1254 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
1255 | + if (ptr->inode != inode) | |
1256 | + continue; | |
1257 | + ret = ptr->status; | |
1258 | + break; | |
1259 | + } | |
1260 | + rcu_read_unlock(); | |
1261 | + if (ret <= 0) | |
1262 | + /* | |
1263 | + * This socket is not an accept()ed socket or this socket is | |
1264 | + * an accept()ed socket and post accept() permission is done. | |
1265 | + */ | |
1266 | + return ret; | |
1267 | + /* | |
1268 | + * Check post accept() permission now. | |
1269 | + * | |
1270 | + * Strictly speaking, we need to pass both listen()ing socket and | |
1271 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
1272 | + * But since socket's family and type are same for both sockets, | |
1273 | + * passing the accept()ed socket in place for the listen()ing socket | |
1274 | + * will work. | |
1275 | + */ | |
1276 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
1277 | + /* | |
1278 | + * If permission was granted, we forget that this is an accept()ed | |
1279 | + * socket. Otherwise, we remember that this socket needs to return | |
1280 | + * error for subsequent socketcalls. | |
1281 | + */ | |
1282 | + ccs_update_socket_tag(inode, ret); | |
1283 | + return ret; | |
1284 | +} | |
1285 | + | |
1286 | +/** | |
1287 | + * ccs_socket_accept - Check permission for accept(). | |
1288 | + * | |
1289 | + * @sock: Pointer to "struct socket". | |
1290 | + * @newsock: Pointer to "struct socket". | |
1291 | + * | |
1292 | + * Returns 0 on success, negative value otherwise. | |
1293 | + * | |
1294 | + * This hook is used for setting up environment for doing post accept() | |
1295 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
1296 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
1297 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
1298 | + * in order to do post accept() permission check before returning to userspace. | |
1299 | + * If we make the copy in security_socket_post_create(), it would be possible | |
1300 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
1301 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
1302 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
1303 | + * rather than between sock->ops->accept() and returning to userspace. | |
1304 | + * This means that if a socket was close()d before calling some socket | |
1305 | + * syscalls, post accept() permission check will not be done. | |
1306 | + */ | |
1307 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
1308 | +{ | |
1309 | + struct ccs_socket_tag *ptr; | |
1310 | + int rc = ccs_validate_socket(sock); | |
1311 | + if (rc < 0) | |
1312 | + return rc; | |
1313 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
1314 | + if (!ptr) | |
1315 | + return -ENOMEM; | |
1316 | + while (!original_security_ops.socket_accept); | |
1317 | + rc = original_security_ops.socket_accept(sock, newsock); | |
1318 | + if (rc) { | |
1319 | + kfree(ptr); | |
1320 | + return rc; | |
1321 | + } | |
1322 | + /* | |
1323 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
1324 | + * "newsock" as "an accept()ed socket but post accept() permission | |
1325 | + * check is not done yet" by allocating memory using inode of the | |
1326 | + * "newsock" as a search key. | |
1327 | + */ | |
1328 | + ptr->inode = SOCK_INODE(newsock); | |
1329 | + ptr->status = 1; /* Check post accept() permission later. */ | |
1330 | + spin_lock(&ccs_accepted_socket_list_lock); | |
1331 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
1332 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
1333 | + return 0; | |
1334 | +} | |
1335 | + | |
1336 | +/** | |
1337 | + * ccs_socket_listen - Check permission for listen(). | |
1338 | + * | |
1339 | + * @sock: Pointer to "struct socket". | |
1340 | + * @backlog: Backlog parameter. | |
1341 | + * | |
1342 | + * Returns 0 on success, negative value otherwise. | |
1343 | + */ | |
1344 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
1345 | +{ | |
1346 | + int rc = ccs_validate_socket(sock); | |
1347 | + if (rc < 0) | |
1348 | + return rc; | |
1349 | + rc = ccs_socket_listen_permission(sock); | |
1350 | + if (rc) | |
1351 | + return rc; | |
1352 | + while (!original_security_ops.socket_listen); | |
1353 | + return original_security_ops.socket_listen(sock, backlog); | |
1354 | +} | |
1355 | + | |
1356 | +/** | |
1357 | + * ccs_socket_connect - Check permission for connect(). | |
1358 | + * | |
1359 | + * @sock: Pointer to "struct socket". | |
1360 | + * @addr: Pointer to "struct sockaddr". | |
1361 | + * @addr_len: Size of @addr. | |
1362 | + * | |
1363 | + * Returns 0 on success, negative value otherwise. | |
1364 | + */ | |
1365 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
1366 | + int addr_len) | |
1367 | +{ | |
1368 | + int rc = ccs_validate_socket(sock); | |
1369 | + if (rc < 0) | |
1370 | + return rc; | |
1371 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
1372 | + if (rc) | |
1373 | + return rc; | |
1374 | + while (!original_security_ops.socket_connect); | |
1375 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
1376 | +} | |
1377 | + | |
1378 | +/** | |
1379 | + * ccs_socket_bind - Check permission for bind(). | |
1380 | + * | |
1381 | + * @sock: Pointer to "struct socket". | |
1382 | + * @addr: Pointer to "struct sockaddr". | |
1383 | + * @addr_len: Size of @addr. | |
1384 | + * | |
1385 | + * Returns 0 on success, negative value otherwise. | |
1386 | + */ | |
1387 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
1388 | + int addr_len) | |
1389 | +{ | |
1390 | + int rc = ccs_validate_socket(sock); | |
1391 | + if (rc < 0) | |
1392 | + return rc; | |
1393 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
1394 | + if (rc) | |
1395 | + return rc; | |
1396 | + while (!original_security_ops.socket_bind); | |
1397 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
1398 | +} | |
1399 | + | |
1400 | +/** | |
1401 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
1402 | + * | |
1403 | + * @sock: Pointer to "struct socket". | |
1404 | + * @msg: Pointer to "struct msghdr". | |
1405 | + * @size: Size of message. | |
1406 | + * | |
1407 | + * Returns 0 on success, negative value otherwise. | |
1408 | + */ | |
1409 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
1410 | + int size) | |
1411 | +{ | |
1412 | + int rc = ccs_validate_socket(sock); | |
1413 | + if (rc < 0) | |
1414 | + return rc; | |
1415 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
1416 | + if (rc) | |
1417 | + return rc; | |
1418 | + while (!original_security_ops.socket_sendmsg); | |
1419 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
1420 | +} | |
1421 | + | |
1422 | +/** | |
1423 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
1424 | + * | |
1425 | + * @sock: Pointer to "struct socket". | |
1426 | + * @msg: Pointer to "struct msghdr". | |
1427 | + * @size: Size of message. | |
1428 | + * @flags: Flags. | |
1429 | + * | |
1430 | + * Returns 0 on success, negative value otherwise. | |
1431 | + */ | |
1432 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
1433 | + int size, int flags) | |
1434 | +{ | |
1435 | + int rc = ccs_validate_socket(sock); | |
1436 | + if (rc < 0) | |
1437 | + return rc; | |
1438 | + while (!original_security_ops.socket_recvmsg); | |
1439 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
1440 | +} | |
1441 | + | |
1442 | +/** | |
1443 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
1444 | + * | |
1445 | + * @sock: Pointer to "struct socket". | |
1446 | + * | |
1447 | + * Returns 0 on success, negative value otherwise. | |
1448 | + */ | |
1449 | +static int ccs_socket_getsockname(struct socket *sock) | |
1450 | +{ | |
1451 | + int rc = ccs_validate_socket(sock); | |
1452 | + if (rc < 0) | |
1453 | + return rc; | |
1454 | + while (!original_security_ops.socket_getsockname); | |
1455 | + return original_security_ops.socket_getsockname(sock); | |
1456 | +} | |
1457 | + | |
1458 | +/** | |
1459 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
1460 | + * | |
1461 | + * @sock: Pointer to "struct socket". | |
1462 | + * | |
1463 | + * Returns 0 on success, negative value otherwise. | |
1464 | + */ | |
1465 | +static int ccs_socket_getpeername(struct socket *sock) | |
1466 | +{ | |
1467 | + int rc = ccs_validate_socket(sock); | |
1468 | + if (rc < 0) | |
1469 | + return rc; | |
1470 | + while (!original_security_ops.socket_getpeername); | |
1471 | + return original_security_ops.socket_getpeername(sock); | |
1472 | +} | |
1473 | + | |
1474 | +/** | |
1475 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
1476 | + * | |
1477 | + * @sock: Pointer to "struct socket". | |
1478 | + * @level: Level. | |
1479 | + * @optname: Option's name, | |
1480 | + * | |
1481 | + * Returns 0 on success, negative value otherwise. | |
1482 | + */ | |
1483 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1484 | +{ | |
1485 | + int rc = ccs_validate_socket(sock); | |
1486 | + if (rc < 0) | |
1487 | + return rc; | |
1488 | + while (!original_security_ops.socket_getsockopt); | |
1489 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
1490 | +} | |
1491 | + | |
1492 | +/** | |
1493 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1494 | + * | |
1495 | + * @sock: Pointer to "struct socket". | |
1496 | + * @level: Level. | |
1497 | + * @optname: Option's name, | |
1498 | + * | |
1499 | + * Returns 0 on success, negative value otherwise. | |
1500 | + */ | |
1501 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1502 | +{ | |
1503 | + int rc = ccs_validate_socket(sock); | |
1504 | + if (rc < 0) | |
1505 | + return rc; | |
1506 | + while (!original_security_ops.socket_setsockopt); | |
1507 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
1508 | +} | |
1509 | + | |
1510 | +/** | |
1511 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1512 | + * | |
1513 | + * @sock: Pointer to "struct socket". | |
1514 | + * @how: Shutdown mode. | |
1515 | + * | |
1516 | + * Returns 0 on success, negative value otherwise. | |
1517 | + */ | |
1518 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1519 | +{ | |
1520 | + int rc = ccs_validate_socket(sock); | |
1521 | + if (rc < 0) | |
1522 | + return rc; | |
1523 | + while (!original_security_ops.socket_shutdown); | |
1524 | + return original_security_ops.socket_shutdown(sock, how); | |
1525 | +} | |
1526 | + | |
1527 | +#define SOCKFS_MAGIC 0x534F434B | |
1528 | + | |
1529 | +/** | |
1530 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1531 | + * | |
1532 | + * @inode: Pointer to "struct inode". | |
1533 | + * | |
1534 | + * Returns nothing. | |
1535 | + * | |
1536 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1537 | + */ | |
1538 | +static void ccs_inode_free_security(struct inode *inode) | |
1539 | +{ | |
1540 | + while (!original_security_ops.inode_free_security); | |
1541 | + original_security_ops.inode_free_security(inode); | |
1542 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1543 | + ccs_update_socket_tag(inode, 0); | |
1544 | +} | |
1545 | + | |
1546 | +#endif | |
1547 | + | |
1548 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) | |
1549 | + | |
1550 | +/** | |
1551 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1552 | + * | |
1553 | + * @old_path: Pointer to "struct path". | |
1554 | + * @new_path: Pointer to "struct path". | |
1555 | + * | |
1556 | + * Returns 0 on success, negative value otherwise. | |
1557 | + */ | |
1558 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1559 | +{ | |
1560 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1561 | + if (rc) | |
1562 | + return rc; | |
1563 | + while (!original_security_ops.sb_pivotroot); | |
1564 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1565 | +} | |
1566 | + | |
1567 | +/** | |
1568 | + * ccs_sb_mount - Check permission for mount(). | |
1569 | + * | |
1570 | + * @dev_name: Name of device file. | |
1571 | + * @path: Pointer to "struct path". | |
1572 | + * @type: Name of filesystem type. Maybe NULL. | |
1573 | + * @flags: Mount options. | |
1574 | + * @data_page: Optional data. Maybe NULL. | |
1575 | + * | |
1576 | + * Returns 0 on success, negative value otherwise. | |
1577 | + */ | |
1578 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1579 | + unsigned long flags, void *data_page) | |
1580 | +{ | |
1581 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1582 | + if (rc) | |
1583 | + return rc; | |
1584 | + while (!original_security_ops.sb_mount); | |
1585 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1586 | + data_page); | |
1587 | +} | |
1588 | + | |
1589 | +#else | |
1590 | + | |
1591 | +/** | |
1592 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1593 | + * | |
1594 | + * @old_path: Pointer to "struct path". | |
1595 | + * @new_path: Pointer to "struct path". | |
1596 | + * | |
1597 | + * Returns 0 on success, negative value otherwise. | |
1598 | + */ | |
1599 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1600 | +{ | |
1601 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1602 | + if (rc) | |
1603 | + return rc; | |
1604 | + while (!original_security_ops.sb_pivotroot); | |
1605 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1606 | +} | |
1607 | + | |
1608 | +/** | |
1609 | + * ccs_sb_mount - Check permission for mount(). | |
1610 | + * | |
1611 | + * @dev_name: Name of device file. | |
1612 | + * @path: Pointer to "struct path". | |
1613 | + * @type: Name of filesystem type. Maybe NULL. | |
1614 | + * @flags: Mount options. | |
1615 | + * @data_page: Optional data. Maybe NULL. | |
1616 | + * | |
1617 | + * Returns 0 on success, negative value otherwise. | |
1618 | + */ | |
1619 | +static int ccs_sb_mount(const char *dev_name, struct path *path, | |
1620 | + const char *type, unsigned long flags, void *data_page) | |
1621 | +{ | |
1622 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1623 | + if (rc) | |
1624 | + return rc; | |
1625 | + while (!original_security_ops.sb_mount); | |
1626 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1627 | + data_page); | |
1628 | +} | |
1629 | + | |
1630 | +#endif | |
1631 | + | |
1632 | +/** | |
1633 | + * ccs_sb_umount - Check permission for umount(). | |
1634 | + * | |
1635 | + * @mnt: Pointer to "struct vfsmount". | |
1636 | + * @flags: Unmount flags. | |
1637 | + * | |
1638 | + * Returns 0 on success, negative value otherwise. | |
1639 | + */ | |
1640 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1641 | +{ | |
1642 | + int rc = ccs_umount_permission(mnt, flags); | |
1643 | + if (rc) | |
1644 | + return rc; | |
1645 | + while (!original_security_ops.sb_umount); | |
1646 | + return original_security_ops.sb_umount(mnt, flags); | |
1647 | +} | |
1648 | + | |
1649 | +/** | |
1650 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1651 | + * | |
1652 | + * @file: Pointer to "struct file". | |
1653 | + * @cmd: Command number. | |
1654 | + * @arg: Value for @cmd. | |
1655 | + * | |
1656 | + * Returns 0 on success, negative value otherwise. | |
1657 | + */ | |
1658 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1659 | + unsigned long arg) | |
1660 | +{ | |
1661 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1662 | + if (rc) | |
1663 | + return rc; | |
1664 | + while (!original_security_ops.file_fcntl); | |
1665 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1666 | +} | |
1667 | + | |
1668 | +/** | |
1669 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1670 | + * | |
1671 | + * @filp: Pointer to "struct file". | |
1672 | + * @cmd: Command number. | |
1673 | + * @arg: Value for @cmd. | |
1674 | + * | |
1675 | + * Returns 0 on success, negative value otherwise. | |
1676 | + */ | |
1677 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1678 | + unsigned long arg) | |
1679 | +{ | |
1680 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1681 | + if (rc) | |
1682 | + return rc; | |
1683 | + while (!original_security_ops.file_ioctl); | |
1684 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1685 | +} | |
1686 | + | |
1687 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL) | |
1688 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1689 | + const struct ccs_path_info *filename); | |
1690 | + | |
1691 | +/** | |
1692 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1693 | + * | |
1694 | + * @buffer: Pointer to "struct char *". | |
1695 | + * @buflen: Pointer to int which holds size of @buffer. | |
1696 | + * @str: String to copy. | |
1697 | + * | |
1698 | + * Returns 0 on success, negative value otherwise. | |
1699 | + * | |
1700 | + * @buffer and @buflen are updated upon success. | |
1701 | + */ | |
1702 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1703 | +{ | |
1704 | + int namelen = strlen(str); | |
1705 | + if (*buflen < namelen) | |
1706 | + return -ENOMEM; | |
1707 | + *buflen -= namelen; | |
1708 | + *buffer -= namelen; | |
1709 | + memcpy(*buffer, str, namelen); | |
1710 | + return 0; | |
1711 | +} | |
1712 | + | |
1713 | +/** | |
1714 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1715 | + * | |
1716 | + * @table: Pointer to "struct ctl_table". | |
1717 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1718 | + * | |
1719 | + * Returns 0 on success, negative value otherwise. | |
1720 | + */ | |
1721 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1722 | +{ | |
1723 | + int error; | |
1724 | + struct ccs_path_info buf; | |
1725 | + struct ccs_request_info r; | |
1726 | + int buflen; | |
1727 | + char *buffer; | |
1728 | + int idx; | |
1729 | + while (!original_security_ops.sysctl); | |
1730 | + error = original_security_ops.sysctl(table, op); | |
1731 | + if (error) | |
1732 | + return error; | |
1733 | + op &= MAY_READ | MAY_WRITE; | |
1734 | + if (!op) | |
1735 | + return 0; | |
1736 | + buffer = NULL; | |
1737 | + buf.name = NULL; | |
1738 | + idx = ccs_read_lock(); | |
1739 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1740 | + == CCS_CONFIG_DISABLED) | |
1741 | + goto out; | |
1742 | + error = -ENOMEM; | |
1743 | + buflen = 4096; | |
1744 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1745 | + if (buffer) { | |
1746 | + char *end = buffer + buflen; | |
1747 | + *--end = '\0'; | |
1748 | + buflen--; | |
1749 | + while (table) { | |
1750 | + char num[32]; | |
1751 | + const char *sp = table->procname; | |
1752 | + if (!sp) { | |
1753 | + memset(num, 0, sizeof(num)); | |
1754 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1755 | + table->ctl_name); | |
1756 | + sp = num; | |
1757 | + } | |
1758 | + if (ccs_prepend(&end, &buflen, sp) || | |
1759 | + ccs_prepend(&end, &buflen, "/")) | |
1760 | + goto out; | |
1761 | + table = table->parent; | |
1762 | + } | |
1763 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1764 | + goto out; | |
1765 | + buf.name = ccs_encode(end); | |
1766 | + } | |
1767 | + if (buf.name) { | |
1768 | + ccs_fill_path_info(&buf); | |
1769 | + if (op & MAY_READ) | |
1770 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1771 | + else | |
1772 | + error = 0; | |
1773 | + if (!error && (op & MAY_WRITE)) | |
1774 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1775 | + } | |
1776 | +out: | |
1777 | + ccs_read_unlock(idx); | |
1778 | + kfree(buf.name); | |
1779 | + kfree(buffer); | |
1780 | + return error; | |
1781 | +} | |
1782 | + | |
1783 | +#endif | |
1784 | + | |
1785 | +/* | |
1786 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1787 | + * Because copying byte array is not atomic. Reader checks | |
1788 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1789 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1790 | + */ | |
1791 | +#define swap_security_ops(op) \ | |
1792 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1793 | + | |
1794 | +/** | |
1795 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1796 | + * | |
1797 | + * @ops: Pointer to "struct security_operations". | |
1798 | + * | |
1799 | + * Returns nothing. | |
1800 | + */ | |
1801 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1802 | +{ | |
1803 | + /* Security context allocator. */ | |
1804 | + swap_security_ops(task_create); | |
1805 | + swap_security_ops(cred_prepare); | |
1806 | + swap_security_ops(cred_free); | |
1807 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
1808 | + swap_security_ops(cred_alloc_blank); | |
1809 | + swap_security_ops(cred_transfer); | |
1810 | +#endif | |
1811 | + /* Security context updater for successful execve(). */ | |
1812 | + swap_security_ops(bprm_check_security); | |
1813 | + swap_security_ops(bprm_committing_creds); | |
1814 | + /* Various permission checker. */ | |
1815 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
1816 | + swap_security_ops(file_open); | |
1817 | +#else | |
1818 | + swap_security_ops(dentry_open); | |
1819 | +#endif | |
1820 | + swap_security_ops(file_fcntl); | |
1821 | + swap_security_ops(file_ioctl); | |
1822 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL) | |
1823 | + swap_security_ops(sysctl); | |
1824 | +#endif | |
1825 | + swap_security_ops(sb_pivotroot); | |
1826 | + swap_security_ops(sb_mount); | |
1827 | + swap_security_ops(sb_umount); | |
1828 | +#if defined(CONFIG_SECURITY_PATH) | |
1829 | + swap_security_ops(path_mknod); | |
1830 | + swap_security_ops(path_mkdir); | |
1831 | + swap_security_ops(path_rmdir); | |
1832 | + swap_security_ops(path_unlink); | |
1833 | + swap_security_ops(path_symlink); | |
1834 | + swap_security_ops(path_rename); | |
1835 | + swap_security_ops(path_link); | |
1836 | + swap_security_ops(path_truncate); | |
1837 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
1838 | + swap_security_ops(path_chmod); | |
1839 | + swap_security_ops(path_chown); | |
1840 | + swap_security_ops(path_chroot); | |
1841 | +#endif | |
1842 | +#else | |
1843 | + swap_security_ops(inode_mknod); | |
1844 | + swap_security_ops(inode_mkdir); | |
1845 | + swap_security_ops(inode_rmdir); | |
1846 | + swap_security_ops(inode_unlink); | |
1847 | + swap_security_ops(inode_symlink); | |
1848 | + swap_security_ops(inode_rename); | |
1849 | + swap_security_ops(inode_link); | |
1850 | + swap_security_ops(inode_create); | |
1851 | +#endif | |
1852 | + swap_security_ops(inode_setattr); | |
1853 | + swap_security_ops(inode_getattr); | |
1854 | +#ifdef CONFIG_SECURITY_NETWORK | |
1855 | + swap_security_ops(socket_bind); | |
1856 | + swap_security_ops(socket_connect); | |
1857 | + swap_security_ops(socket_listen); | |
1858 | + swap_security_ops(socket_sendmsg); | |
1859 | + swap_security_ops(socket_recvmsg); | |
1860 | + swap_security_ops(socket_getsockname); | |
1861 | + swap_security_ops(socket_getpeername); | |
1862 | + swap_security_ops(socket_getsockopt); | |
1863 | + swap_security_ops(socket_setsockopt); | |
1864 | + swap_security_ops(socket_shutdown); | |
1865 | + swap_security_ops(socket_accept); | |
1866 | + swap_security_ops(inode_free_security); | |
1867 | +#endif | |
1868 | +} | |
1869 | + | |
1870 | +#undef swap_security_ops | |
1871 | + | |
1872 | +/** | |
1873 | + * ccs_init - Initialize this module. | |
1874 | + * | |
1875 | + * Returns 0 on success, negative value otherwise. | |
1876 | + */ | |
1877 | +static int __init ccs_init(void) | |
1878 | +{ | |
1879 | + struct security_operations *ops = probe_security_ops(); | |
1880 | + if (!ops) | |
1881 | + goto out; | |
1882 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1883 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1884 | + goto out; | |
1885 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1886 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1887 | + goto out; | |
1888 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
1889 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1890 | + if (!ccsecurity_exports.vfsmount_lock) | |
1891 | + goto out; | |
1892 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0) | |
1893 | + ccsecurity_exports.__d_path = probe___d_path(); | |
1894 | + if (!ccsecurity_exports.__d_path) | |
1895 | + goto out; | |
1896 | +#else | |
1897 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1898 | + if (!ccsecurity_exports.d_absolute_path) | |
1899 | + goto out; | |
1900 | +#endif | |
1901 | + { | |
1902 | + int idx; | |
1903 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1904 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1905 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1906 | + } | |
1907 | + } | |
1908 | + ccs_main_init(); | |
1909 | + ccs_update_security_ops(ops); | |
1910 | + printk(KERN_INFO "AKARI: 1.0.36 2017/02/20\n"); | |
1911 | + printk(KERN_INFO | |
1912 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1913 | + return 0; | |
1914 | +out: | |
1915 | + return -EINVAL; | |
1916 | +} | |
1917 | + | |
1918 | +module_init(ccs_init); | |
1919 | +MODULE_LICENSE("GPL"); | |
1920 | + | |
1921 | +/** | |
1922 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1923 | + * | |
1924 | + * @domain: Pointer to "struct ccs_domain_info". | |
1925 | + * | |
1926 | + * Returns true if @domain is in use, false otherwise. | |
1927 | + * | |
1928 | + * Caller holds rcu_read_lock(). | |
1929 | + */ | |
1930 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1931 | +{ | |
1932 | + int idx; | |
1933 | + struct ccs_security *ptr; | |
1934 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1935 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1936 | + list_for_each_entry_rcu(ptr, list, list) { | |
1937 | + struct ccs_execve *ee = ptr->ee; | |
1938 | + if (ptr->ccs_domain_info == domain || | |
1939 | + (ee && ee->previous_domain == domain)) { | |
1940 | + return true; | |
1941 | + } | |
1942 | + } | |
1943 | + } | |
1944 | + return false; | |
1945 | +} | |
1946 | + | |
1947 | +/** | |
1948 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1949 | + * | |
1950 | + * @ptr: Pointer to "struct ccs_security". | |
1951 | + * @list: Pointer to "struct list_head". | |
1952 | + * | |
1953 | + * Returns nothing. | |
1954 | + */ | |
1955 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1956 | + struct list_head *list) | |
1957 | +{ | |
1958 | + unsigned long flags; | |
1959 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1960 | + list_add_rcu(&ptr->list, list); | |
1961 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1962 | +} | |
1963 | + | |
1964 | +/** | |
1965 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1966 | + * | |
1967 | + * @task: Pointer to "struct task_struct". | |
1968 | + * | |
1969 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1970 | + * out of memory, &ccs_default_security otherwise. | |
1971 | + * | |
1972 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1973 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1974 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1975 | + * won't work. | |
1976 | + */ | |
1977 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1978 | +{ | |
1979 | + struct ccs_security *ptr; | |
1980 | + struct list_head *list = &ccs_task_security_list | |
1981 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1982 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1983 | + while (!list->next); | |
1984 | + rcu_read_lock(); | |
1985 | + list_for_each_entry_rcu(ptr, list, list) { | |
1986 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
1987 | + continue; | |
1988 | + rcu_read_unlock(); | |
1989 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
1990 | + /* | |
1991 | + * Current thread needs to transit from old domain to new | |
1992 | + * domain before do_execve() succeeds in order to check | |
1993 | + * permission for interpreters and environment variables using | |
1994 | + * new domain's ACL rules. The domain transition has to be | |
1995 | + * visible from other CPU in order to allow interactive | |
1996 | + * enforcing mode. Also, the domain transition has to be | |
1997 | + * reverted if do_execve() failed. However, an LSM hook for | |
1998 | + * reverting domain transition is missing. | |
1999 | + * | |
2000 | + * security_prepare_creds() is called from prepare_creds() from | |
2001 | + * prepare_bprm_creds() from do_execve() before setting | |
2002 | + * current->in_execve flag, and current->in_execve flag is | |
2003 | + * cleared by the time next do_execve() request starts. | |
2004 | + * This means that we can emulate the missing LSM hook for | |
2005 | + * reverting domain transition, by calling this function from | |
2006 | + * security_prepare_creds(). | |
2007 | + * | |
2008 | + * If current->in_execve is not set but ptr->ccs_flags has | |
2009 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
2010 | + * has failed and reverting domain transition is needed. | |
2011 | + */ | |
2012 | + if (task == current && | |
2013 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
2014 | + !current->in_execve) { | |
2015 | + ccs_debug_trace("4"); | |
2016 | + ccs_clear_execve(-1, ptr); | |
2017 | + } | |
2018 | +#else | |
2019 | + /* | |
2020 | + * Current thread needs to transit from old domain to new | |
2021 | + * domain before do_execve() succeeds in order to check | |
2022 | + * permission for interpreters and environment variables using | |
2023 | + * new domain's ACL rules. The domain transition has to be | |
2024 | + * visible from other CPU in order to allow interactive | |
2025 | + * enforcing mode. Also, the domain transition has to be | |
2026 | + * reverted if do_execve() failed. However, an LSM hook for | |
2027 | + * reverting domain transition is missing. | |
2028 | + * | |
2029 | + * When do_execve() failed, "struct cred" in | |
2030 | + * "struct linux_binprm" is scheduled for destruction. | |
2031 | + * But current thread returns to userspace without waiting for | |
2032 | + * destruction. The security_cred_free() LSM hook is called | |
2033 | + * after an RCU grace period has elapsed. Since some CPU may be | |
2034 | + * doing long long RCU read side critical section, there is | |
2035 | + * no guarantee that security_cred_free() is called before | |
2036 | + * current thread again calls do_execve(). | |
2037 | + * | |
2038 | + * To be able to revert domain transition before processing | |
2039 | + * next do_execve() request, current thread gets a refcount on | |
2040 | + * "struct cred" in "struct linux_binprm" and memorizes it. | |
2041 | + * Current thread drops the refcount and forgets it when | |
2042 | + * do_execve() succeeded. | |
2043 | + * | |
2044 | + * Therefore, if current thread hasn't forgotten it and | |
2045 | + * current thread is the last one using that "struct cred", | |
2046 | + * it indicates that do_execve() has failed and reverting | |
2047 | + * domain transition is needed. | |
2048 | + */ | |
2049 | + if (task == current && ptr->cred && | |
2050 | + atomic_read(&ptr->cred->usage) == 1) { | |
2051 | + ccs_debug_trace("4"); | |
2052 | + ccs_clear_execve(-1, ptr); | |
2053 | + } | |
2054 | +#endif | |
2055 | + return ptr; | |
2056 | + } | |
2057 | + rcu_read_unlock(); | |
2058 | + if (task != current) { | |
2059 | + /* | |
2060 | + * If a thread does nothing after fork(), caller will reach | |
2061 | + * here because "struct ccs_security" for that thread is not | |
2062 | + * yet allocated. But that thread is keeping a snapshot of | |
2063 | + * "struct ccs_security" taken as of ccs_task_create() | |
2064 | + * associated with that thread's "struct cred". | |
2065 | + * | |
2066 | + * Since that snapshot will be used as initial data when that | |
2067 | + * thread allocates "struct ccs_security" for that thread, we | |
2068 | + * can return that snapshot rather than &ccs_default_security. | |
2069 | + * | |
2070 | + * Since this function is called by only ccs_select_one() and | |
2071 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
2072 | + * it is guaranteed that caller has called rcu_read_lock() | |
2073 | + * (via ccs_tasklist_lock()) before finding this thread and | |
2074 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
2075 | + * like get_robust_list() does. | |
2076 | + */ | |
2077 | + return ccs_find_cred_security(__task_cred(task)); | |
2078 | + } | |
2079 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
2080 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
2081 | + if (!ptr) { | |
2082 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
2083 | + task->pid); | |
2084 | + send_sig(SIGKILL, current, 0); | |
2085 | + return &ccs_oom_security; | |
2086 | + } | |
2087 | + *ptr = *ccs_find_cred_security(task->cred); | |
2088 | + /* We can shortcut because task == current. */ | |
2089 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
2090 | + pids[PIDTYPE_PID].pid); | |
2091 | + ptr->cred = NULL; | |
2092 | + ccs_add_task_security(ptr, list); | |
2093 | + return ptr; | |
2094 | +} | |
2095 | + | |
2096 | +/** | |
2097 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
2098 | + * | |
2099 | + * @new: Pointer to "struct cred". | |
2100 | + * @old: Pointer to "struct cred". | |
2101 | + * @gfp: Memory allocation flags. | |
2102 | + * | |
2103 | + * Returns 0 on success, negative value otherwise. | |
2104 | + */ | |
2105 | +static int ccs_copy_cred_security(const struct cred *new, | |
2106 | + const struct cred *old, gfp_t gfp) | |
2107 | +{ | |
2108 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
2109 | + struct ccs_security *new_security = | |
2110 | + kzalloc(sizeof(*new_security), gfp); | |
2111 | + if (!new_security) | |
2112 | + return -ENOMEM; | |
2113 | + *new_security = *old_security; | |
2114 | + new_security->cred = new; | |
2115 | + ccs_add_cred_security(new_security); | |
2116 | + return 0; | |
2117 | +} | |
2118 | + | |
2119 | +/** | |
2120 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
2121 | + * | |
2122 | + * @cred: Pointer to "struct cred". | |
2123 | + * | |
2124 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
2125 | + * otherwise. | |
2126 | + */ | |
2127 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
2128 | +{ | |
2129 | + struct ccs_security *ptr; | |
2130 | + struct list_head *list = &ccs_cred_security_list | |
2131 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
2132 | + rcu_read_lock(); | |
2133 | + list_for_each_entry_rcu(ptr, list, list) { | |
2134 | + if (ptr->cred != cred) | |
2135 | + continue; | |
2136 | + rcu_read_unlock(); | |
2137 | + return ptr; | |
2138 | + } | |
2139 | + rcu_read_unlock(); | |
2140 | + return &ccs_default_security; | |
2141 | +} | |
2142 | + | |
2143 | +/** | |
2144 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
2145 | + * | |
2146 | + * Returns nothing. | |
2147 | + * | |
2148 | + * Since security_task_free() is missing, I can't release memory associated | |
2149 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
2150 | + * "struct pid" and runs garbage collection when associated | |
2151 | + * "struct task_struct" has gone. | |
2152 | + */ | |
2153 | +static void ccs_task_security_gc(void) | |
2154 | +{ | |
2155 | + static DEFINE_SPINLOCK(lock); | |
2156 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
2157 | + unsigned int idx; | |
2158 | + /* | |
2159 | + * If some process is doing execve(), try to garbage collection now. | |
2160 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
2161 | + * as soon as execve() has completed in order to compensate for lack of | |
2162 | + * security_bprm_free() and security_task_free() hooks. | |
2163 | + * | |
2164 | + * Otherwise, reduce frequency for performance reason. | |
2165 | + */ | |
2166 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
2167 | + atomic_inc_return(&gc_counter) < 1024) | |
2168 | + return; | |
2169 | + if (!spin_trylock(&lock)) | |
2170 | + return; | |
2171 | + atomic_set(&gc_counter, 0); | |
2172 | + rcu_read_lock(); | |
2173 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
2174 | + struct ccs_security *ptr; | |
2175 | + struct list_head *list = &ccs_task_security_list[idx]; | |
2176 | + list_for_each_entry_rcu(ptr, list, list) { | |
2177 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
2178 | + continue; | |
2179 | + ccs_del_security(ptr); | |
2180 | + } | |
2181 | + } | |
2182 | + rcu_read_unlock(); | |
2183 | + spin_unlock(&lock); | |
2184 | +} |
@@ -0,0 +1,1491 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | +static void ccs_task_security_gc(void); | |
14 | +static int ccs_copy_cred_security(const struct cred *new, | |
15 | + const struct cred *old, gfp_t gfp); | |
16 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred); | |
17 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
18 | +static atomic_t ccs_in_execve_tasks = ATOMIC_INIT(0); | |
19 | +/* | |
20 | + * List of "struct ccs_security" for "struct pid". | |
21 | + * | |
22 | + * All instances on this list is guaranteed that "struct ccs_security"->pid != | |
23 | + * NULL. Also, instances on this list that are in execve() are guaranteed that | |
24 | + * "struct ccs_security"->cred remembers "struct linux_binprm"->cred with a | |
25 | + * refcount on "struct linux_binprm"->cred. | |
26 | + */ | |
27 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
28 | +/* | |
29 | + * List of "struct ccs_security" for "struct cred". | |
30 | + * | |
31 | + * Since the number of "struct cred" is nearly equals to the number of | |
32 | + * "struct pid", we allocate hash tables like ccs_task_security_list. | |
33 | + * | |
34 | + * All instances on this list are guaranteed that "struct ccs_security"->pid == | |
35 | + * NULL and "struct ccs_security"->cred != NULL. | |
36 | + */ | |
37 | +static struct list_head ccs_cred_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
38 | + | |
39 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
40 | +static struct ccs_security ccs_oom_security = { | |
41 | + .ccs_domain_info = &ccs_kernel_domain | |
42 | +}; | |
43 | + | |
44 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
45 | +static struct ccs_security ccs_default_security = { | |
46 | + .ccs_domain_info = &ccs_kernel_domain | |
47 | +}; | |
48 | + | |
49 | +/* For exporting variables and functions. */ | |
50 | +struct ccsecurity_exports ccsecurity_exports; | |
51 | +/* Members are updated by loadable kernel module. */ | |
52 | +struct ccsecurity_operations ccsecurity_ops; | |
53 | + | |
54 | +/* Original hooks. */ | |
55 | +static union security_list_options original_cred_prepare; | |
56 | +static union security_list_options original_cred_free; | |
57 | +static union security_list_options original_cred_alloc_blank; | |
58 | + | |
59 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
60 | + | |
61 | +/** | |
62 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
63 | + * | |
64 | + * @count: Count to increment or decrement. | |
65 | + * | |
66 | + * Returns updated counter. | |
67 | + */ | |
68 | +static unsigned int ccs_update_ee_counter(int count) | |
69 | +{ | |
70 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
71 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
72 | + return atomic_add_return(count, &ccs_ee_counter); | |
73 | +} | |
74 | + | |
75 | +/** | |
76 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
77 | + * | |
78 | + * @ee: Pointer to "struct ccs_execve". | |
79 | + * | |
80 | + * Returns nothing. | |
81 | + */ | |
82 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
83 | +{ | |
84 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
85 | + current->pid, ccs_update_ee_counter(1) - 1); | |
86 | +} | |
87 | + | |
88 | +/** | |
89 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
90 | + * | |
91 | + * @ee: Pointer to "struct ccs_execve". | |
92 | + * @task: True if released by current task, false otherwise. | |
93 | + * | |
94 | + * Returns nothing. | |
95 | + */ | |
96 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
97 | + const bool is_current) | |
98 | +{ | |
99 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
100 | + if (is_current) | |
101 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
102 | + ee, current->pid, tmp); | |
103 | + else | |
104 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
105 | + ee, tmp); | |
106 | +} | |
107 | + | |
108 | +#endif | |
109 | + | |
110 | +#if !defined(CONFIG_AKARI_DEBUG) | |
111 | +#define ccs_debug_trace(pos) do { } while (0) | |
112 | +#else | |
113 | +#define ccs_debug_trace(pos) \ | |
114 | + do { \ | |
115 | + static bool done; \ | |
116 | + if (!done) { \ | |
117 | + printk(KERN_INFO \ | |
118 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
119 | + done = true; \ | |
120 | + } \ | |
121 | + } while (0) | |
122 | +#endif | |
123 | + | |
124 | +/** | |
125 | + * ccs_clear_execve - Release memory used by do_execve(). | |
126 | + * | |
127 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
128 | + * @security: Pointer to "struct ccs_security". | |
129 | + * | |
130 | + * Returns nothing. | |
131 | + */ | |
132 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
133 | +{ | |
134 | + struct ccs_execve *ee; | |
135 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
136 | + return; | |
137 | + ee = security->ee; | |
138 | + security->ee = NULL; | |
139 | + if (!ee) | |
140 | + return; | |
141 | + atomic_dec(&ccs_in_execve_tasks); | |
142 | + ccs_finish_execve(ret, ee); | |
143 | +} | |
144 | + | |
145 | +/** | |
146 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
147 | + * | |
148 | + * @rcu: Pointer to "struct rcu_head". | |
149 | + * | |
150 | + * Returns nothing. | |
151 | + */ | |
152 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
153 | +{ | |
154 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
155 | + struct ccs_execve *ee = ptr->ee; | |
156 | + /* | |
157 | + * If this security context was associated with "struct pid" and | |
158 | + * ptr->ccs_flags has CCS_TASK_IS_IN_EXECVE set, it indicates that a | |
159 | + * "struct task_struct" associated with this security context exited | |
160 | + * immediately after do_execve() has failed. | |
161 | + */ | |
162 | + if (ptr->pid && (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
163 | + ccs_debug_trace("1"); | |
164 | + atomic_dec(&ccs_in_execve_tasks); | |
165 | + } | |
166 | + /* | |
167 | + * If this security context was associated with "struct pid", | |
168 | + * drop refcount obtained by get_pid() in ccs_find_task_security(). | |
169 | + */ | |
170 | + if (ptr->pid) { | |
171 | + ccs_debug_trace("2"); | |
172 | + put_pid(ptr->pid); | |
173 | + } | |
174 | + if (ee) { | |
175 | + ccs_debug_trace("3"); | |
176 | + ccs_audit_free_execve(ee, false); | |
177 | + kfree(ee->handler_path); | |
178 | + kfree(ee); | |
179 | + } | |
180 | + kfree(ptr); | |
181 | +} | |
182 | + | |
183 | +/** | |
184 | + * ccs_del_security - Release "struct ccs_security". | |
185 | + * | |
186 | + * @ptr: Pointer to "struct ccs_security". | |
187 | + * | |
188 | + * Returns nothing. | |
189 | + */ | |
190 | +static void ccs_del_security(struct ccs_security *ptr) | |
191 | +{ | |
192 | + unsigned long flags; | |
193 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
194 | + return; | |
195 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
196 | + list_del_rcu(&ptr->list); | |
197 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
198 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
199 | +} | |
200 | + | |
201 | +/** | |
202 | + * ccs_add_cred_security - Add "struct ccs_security" to list. | |
203 | + * | |
204 | + * @ptr: Pointer to "struct ccs_security". | |
205 | + * | |
206 | + * Returns nothing. | |
207 | + */ | |
208 | +static void ccs_add_cred_security(struct ccs_security *ptr) | |
209 | +{ | |
210 | + unsigned long flags; | |
211 | + struct list_head *list = &ccs_cred_security_list | |
212 | + [hash_ptr((void *) ptr->cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
213 | +#ifdef CONFIG_AKARI_DEBUG | |
214 | + if (ptr->pid) | |
215 | + printk(KERN_INFO "AKARI: \"struct ccs_security\"->pid != NULL" | |
216 | + "\n"); | |
217 | +#endif | |
218 | + ptr->pid = NULL; | |
219 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
220 | + list_add_rcu(&ptr->list, list); | |
221 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
222 | +} | |
223 | + | |
224 | +/** | |
225 | + * ccs_task_create - Make snapshot of security context for new task. | |
226 | + * | |
227 | + * @clone_flags: Flags passed to clone(). | |
228 | + * | |
229 | + * Returns 0 on success, negative value otherwise. | |
230 | + */ | |
231 | +static int ccs_task_create(unsigned long clone_flags) | |
232 | +{ | |
233 | + struct ccs_security *old_security; | |
234 | + struct ccs_security *new_security; | |
235 | + struct cred *cred = prepare_creds(); | |
236 | + if (!cred) | |
237 | + return -ENOMEM; | |
238 | + old_security = ccs_find_task_security(current); | |
239 | + new_security = ccs_find_cred_security(cred); | |
240 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
241 | + new_security->ccs_flags = old_security->ccs_flags; | |
242 | + return commit_creds(cred); | |
243 | +} | |
244 | + | |
245 | +/** | |
246 | + * ccs_cred_prepare - Allocate memory for new credentials. | |
247 | + * | |
248 | + * @new: Pointer to "struct cred". | |
249 | + * @old: Pointer to "struct cred". | |
250 | + * @gfp: Memory allocation flags. | |
251 | + * | |
252 | + * Returns 0 on success, negative value otherwise. | |
253 | + */ | |
254 | +static int ccs_cred_prepare(struct cred *new, const struct cred *old, | |
255 | + gfp_t gfp) | |
256 | +{ | |
257 | + int rc1; | |
258 | + /* | |
259 | + * For checking whether reverting domain transition is needed or not. | |
260 | + * | |
261 | + * See ccs_find_task_security() for reason. | |
262 | + */ | |
263 | + if (gfp == GFP_KERNEL) | |
264 | + ccs_find_task_security(current); | |
265 | + rc1 = ccs_copy_cred_security(new, old, gfp); | |
266 | + if (gfp == GFP_KERNEL) | |
267 | + ccs_task_security_gc(); | |
268 | + if (original_cred_prepare.cred_prepare) { | |
269 | + const int rc2 = original_cred_prepare.cred_prepare(new, old, | |
270 | + gfp); | |
271 | + if (rc2) { | |
272 | + ccs_del_security(ccs_find_cred_security(new)); | |
273 | + return rc2; | |
274 | + } | |
275 | + } | |
276 | + return rc1; | |
277 | +} | |
278 | + | |
279 | +/** | |
280 | + * ccs_cred_free - Release memory used by credentials. | |
281 | + * | |
282 | + * @cred: Pointer to "struct cred". | |
283 | + * | |
284 | + * Returns nothing. | |
285 | + */ | |
286 | +static void ccs_cred_free(struct cred *cred) | |
287 | +{ | |
288 | + if (original_cred_free.cred_free) | |
289 | + original_cred_free.cred_free(cred); | |
290 | + ccs_del_security(ccs_find_cred_security(cred)); | |
291 | +} | |
292 | + | |
293 | +/** | |
294 | + * ccs_alloc_cred_security - Allocate memory for new credentials. | |
295 | + * | |
296 | + * @cred: Pointer to "struct cred". | |
297 | + * @gfp: Memory allocation flags. | |
298 | + * | |
299 | + * Returns 0 on success, negative value otherwise. | |
300 | + */ | |
301 | +static int ccs_alloc_cred_security(const struct cred *cred, gfp_t gfp) | |
302 | +{ | |
303 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
304 | + gfp); | |
305 | + if (!new_security) | |
306 | + return -ENOMEM; | |
307 | + new_security->cred = cred; | |
308 | + ccs_add_cred_security(new_security); | |
309 | + return 0; | |
310 | +} | |
311 | + | |
312 | +/** | |
313 | + * ccs_cred_alloc_blank - Allocate memory for new credentials. | |
314 | + * | |
315 | + * @new: Pointer to "struct cred". | |
316 | + * @gfp: Memory allocation flags. | |
317 | + * | |
318 | + * Returns 0 on success, negative value otherwise. | |
319 | + */ | |
320 | +static int ccs_cred_alloc_blank(struct cred *new, gfp_t gfp) | |
321 | +{ | |
322 | + const int rc1 = ccs_alloc_cred_security(new, gfp); | |
323 | + if (original_cred_alloc_blank.cred_alloc_blank) { | |
324 | + const int rc2 = original_cred_alloc_blank. | |
325 | + cred_alloc_blank(new, gfp); | |
326 | + if (rc2) { | |
327 | + ccs_del_security(ccs_find_cred_security(new)); | |
328 | + return rc2; | |
329 | + } | |
330 | + } | |
331 | + return rc1; | |
332 | +} | |
333 | + | |
334 | +/** | |
335 | + * ccs_cred_transfer - Transfer "struct ccs_security" between credentials. | |
336 | + * | |
337 | + * @new: Pointer to "struct cred". | |
338 | + * @old: Pointer to "struct cred". | |
339 | + * | |
340 | + * Returns nothing. | |
341 | + */ | |
342 | +static void ccs_cred_transfer(struct cred *new, const struct cred *old) | |
343 | +{ | |
344 | + struct ccs_security *new_security = ccs_find_cred_security(new); | |
345 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
346 | + if (new_security == &ccs_default_security || | |
347 | + new_security == &ccs_oom_security || | |
348 | + old_security == &ccs_default_security || | |
349 | + old_security == &ccs_oom_security) | |
350 | + return; | |
351 | + new_security->ccs_flags = old_security->ccs_flags; | |
352 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
353 | +} | |
354 | + | |
355 | +/** | |
356 | + * ccs_bprm_committing_creds - A hook which is called when do_execve() succeeded. | |
357 | + * | |
358 | + * @bprm: Pointer to "struct linux_binprm". | |
359 | + * | |
360 | + * Returns nothing. | |
361 | + */ | |
362 | +static void ccs_bprm_committing_creds(struct linux_binprm *bprm) | |
363 | +{ | |
364 | + struct ccs_security *old_security = ccs_current_security(); | |
365 | + struct ccs_security *new_security; | |
366 | + if (old_security == &ccs_default_security || | |
367 | + old_security == &ccs_oom_security) | |
368 | + return; | |
369 | + ccs_clear_execve(0, old_security); | |
370 | + /* Update current task's cred's domain for future fork(). */ | |
371 | + new_security = ccs_find_cred_security(bprm->cred); | |
372 | + new_security->ccs_flags = old_security->ccs_flags; | |
373 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
374 | +} | |
375 | + | |
376 | +/** | |
377 | + * ccs_bprm_check_security - Check permission for execve(). | |
378 | + * | |
379 | + * @bprm: Pointer to "struct linux_binprm". | |
380 | + * | |
381 | + * Returns 0 on success, negative value otherwise. | |
382 | + */ | |
383 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
384 | +{ | |
385 | + struct ccs_security *security = ccs_current_security(); | |
386 | + int rc; | |
387 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
388 | + return -ENOMEM; | |
389 | + if (security->ee) | |
390 | + return 0; | |
391 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
392 | + if (!ccs_policy_loaded) | |
393 | + ccs_load_policy(bprm->filename); | |
394 | +#endif | |
395 | + rc = ccs_start_execve(bprm, &security->ee); | |
396 | + if (security->ee) | |
397 | + atomic_inc(&ccs_in_execve_tasks); | |
398 | + return rc; | |
399 | +} | |
400 | + | |
401 | +/** | |
402 | + * ccs_file_open - Check permission for open(). | |
403 | + * | |
404 | + * @f: Pointer to "struct file". | |
405 | + * @cred: Pointer to "struct cred". | |
406 | + * | |
407 | + * Returns 0 on success, negative value otherwise. | |
408 | + */ | |
409 | +static int ccs_file_open(struct file *f, const struct cred *cred) | |
410 | +{ | |
411 | + return ccs_open_permission(f); | |
412 | +} | |
413 | + | |
414 | +#ifdef CONFIG_SECURITY_PATH | |
415 | + | |
416 | +/** | |
417 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
418 | + * | |
419 | + * @path: Pointer to "struct path". | |
420 | + * @user: User ID. | |
421 | + * @group: Group ID. | |
422 | + * | |
423 | + * Returns 0 on success, negative value otherwise. | |
424 | + */ | |
425 | +static int ccs_path_chown(struct path *path, kuid_t user, kgid_t group) | |
426 | +{ | |
427 | + return ccs_chown_permission(path->dentry, path->mnt, user, group); | |
428 | +} | |
429 | + | |
430 | +/** | |
431 | + * ccs_path_chmod - Check permission for chmod(). | |
432 | + * | |
433 | + * @path: Pointer to "struct path". | |
434 | + * @mode: Mode. | |
435 | + * | |
436 | + * Returns 0 on success, negative value otherwise. | |
437 | + */ | |
438 | +static int ccs_path_chmod(struct path *path, umode_t mode) | |
439 | +{ | |
440 | + return ccs_chmod_permission(path->dentry, path->mnt, mode); | |
441 | +} | |
442 | + | |
443 | +/** | |
444 | + * ccs_path_chroot - Check permission for chroot(). | |
445 | + * | |
446 | + * @path: Pointer to "struct path". | |
447 | + * | |
448 | + * Returns 0 on success, negative value otherwise. | |
449 | + */ | |
450 | +static int ccs_path_chroot(struct path *path) | |
451 | +{ | |
452 | + return ccs_chroot_permission(path); | |
453 | +} | |
454 | + | |
455 | +/** | |
456 | + * ccs_path_truncate - Check permission for truncate(). | |
457 | + * | |
458 | + * @path: Pointer to "struct path". | |
459 | + * | |
460 | + * Returns 0 on success, negative value otherwise. | |
461 | + */ | |
462 | +static int ccs_path_truncate(struct path *path) | |
463 | +{ | |
464 | + return ccs_truncate_permission(path->dentry, path->mnt); | |
465 | +} | |
466 | + | |
467 | +#else | |
468 | + | |
469 | +/** | |
470 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
471 | + * | |
472 | + * @dentry: Pointer to "struct dentry". | |
473 | + * @attr: Pointer to "struct iattr". | |
474 | + * | |
475 | + * Returns 0 on success, negative value otherwise. | |
476 | + */ | |
477 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
478 | +{ | |
479 | + const int rc1 = (attr->ia_valid & ATTR_UID) ? | |
480 | + ccs_chown_permission(dentry, NULL, attr->ia_uid, INVALID_GID) : | |
481 | + 0; | |
482 | + const int rc2 = (attr->ia_valid & ATTR_GID) ? | |
483 | + ccs_chown_permission(dentry, NULL, INVALID_UID, attr->ia_gid) : | |
484 | + 0; | |
485 | + const int rc3 = (attr->ia_valid & ATTR_MODE) ? | |
486 | + ccs_chmod_permission(dentry, NULL, attr->ia_mode) : 0; | |
487 | + const int rc4 = (attr->ia_valid & ATTR_SIZE) ? | |
488 | + ccs_truncate_permission(dentry, NULL) : 0; | |
489 | + if (rc4) | |
490 | + return rc4; | |
491 | + if (rc3) | |
492 | + return rc3; | |
493 | + if (rc2) | |
494 | + return rc2; | |
495 | + return rc1; | |
496 | +} | |
497 | + | |
498 | +#endif | |
499 | + | |
500 | +/** | |
501 | + * ccs_inode_getattr - Check permission for stat(). | |
502 | + * | |
503 | + * @path: Pointer to "struct path". | |
504 | + * | |
505 | + * Returns 0 on success, negative value otherwise. | |
506 | + */ | |
507 | +static int ccs_inode_getattr(const struct path *path) | |
508 | +{ | |
509 | + return ccs_getattr_permission(path->mnt, path->dentry); | |
510 | +} | |
511 | + | |
512 | +#ifdef CONFIG_SECURITY_PATH | |
513 | + | |
514 | +/** | |
515 | + * ccs_path_mknod - Check permission for mknod(). | |
516 | + * | |
517 | + * @dir: Pointer to "struct path". | |
518 | + * @dentry: Pointer to "struct dentry". | |
519 | + * @mode: Create mode. | |
520 | + * @dev: Device major/minor number. | |
521 | + * | |
522 | + * Returns 0 on success, negative value otherwise. | |
523 | + */ | |
524 | +static int ccs_path_mknod(struct path *dir, struct dentry *dentry, | |
525 | + umode_t mode, unsigned int dev) | |
526 | +{ | |
527 | + return ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
528 | +} | |
529 | + | |
530 | +/** | |
531 | + * ccs_path_mkdir - Check permission for mkdir(). | |
532 | + * | |
533 | + * @dir: Pointer to "struct path". | |
534 | + * @dentry: Pointer to "struct dentry". | |
535 | + * @mode: Create mode. | |
536 | + * | |
537 | + * Returns 0 on success, negative value otherwise. | |
538 | + */ | |
539 | +static int ccs_path_mkdir(struct path *dir, struct dentry *dentry, | |
540 | + umode_t mode) | |
541 | +{ | |
542 | + return ccs_mkdir_permission(dentry, dir->mnt, mode); | |
543 | +} | |
544 | + | |
545 | +/** | |
546 | + * ccs_path_rmdir - Check permission for rmdir(). | |
547 | + * | |
548 | + * @dir: Pointer to "struct path". | |
549 | + * @dentry: Pointer to "struct dentry". | |
550 | + * | |
551 | + * Returns 0 on success, negative value otherwise. | |
552 | + */ | |
553 | +static int ccs_path_rmdir(struct path *dir, struct dentry *dentry) | |
554 | +{ | |
555 | + return ccs_rmdir_permission(dentry, dir->mnt); | |
556 | +} | |
557 | + | |
558 | +/** | |
559 | + * ccs_path_unlink - Check permission for unlink(). | |
560 | + * | |
561 | + * @dir: Pointer to "struct path". | |
562 | + * @dentry: Pointer to "struct dentry". | |
563 | + * | |
564 | + * Returns 0 on success, negative value otherwise. | |
565 | + */ | |
566 | +static int ccs_path_unlink(struct path *dir, struct dentry *dentry) | |
567 | +{ | |
568 | + return ccs_unlink_permission(dentry, dir->mnt); | |
569 | +} | |
570 | + | |
571 | +/** | |
572 | + * ccs_path_symlink - Check permission for symlink(). | |
573 | + * | |
574 | + * @dir: Pointer to "struct path". | |
575 | + * @dentry: Pointer to "struct dentry". | |
576 | + * @old_name: Content of symbolic link. | |
577 | + * | |
578 | + * Returns 0 on success, negative value otherwise. | |
579 | + */ | |
580 | +static int ccs_path_symlink(struct path *dir, struct dentry *dentry, | |
581 | + const char *old_name) | |
582 | +{ | |
583 | + return ccs_symlink_permission(dentry, dir->mnt, old_name); | |
584 | +} | |
585 | + | |
586 | +/** | |
587 | + * ccs_path_rename - Check permission for rename(). | |
588 | + * | |
589 | + * @old_dir: Pointer to "struct path". | |
590 | + * @old_dentry: Pointer to "struct dentry". | |
591 | + * @new_dir: Pointer to "struct path". | |
592 | + * @new_dentry: Pointer to "struct dentry". | |
593 | + * | |
594 | + * Returns 0 on success, negative value otherwise. | |
595 | + */ | |
596 | +static int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry, | |
597 | + struct path *new_dir, struct dentry *new_dentry) | |
598 | +{ | |
599 | + return ccs_rename_permission(old_dentry, new_dentry, old_dir->mnt); | |
600 | +} | |
601 | + | |
602 | +/** | |
603 | + * ccs_path_link - Check permission for link(). | |
604 | + * | |
605 | + * @old_dentry: Pointer to "struct dentry". | |
606 | + * @new_dir: Pointer to "struct path". | |
607 | + * @new_dentry: Pointer to "struct dentry". | |
608 | + * | |
609 | + * Returns 0 on success, negative value otherwise. | |
610 | + */ | |
611 | +static int ccs_path_link(struct dentry *old_dentry, struct path *new_dir, | |
612 | + struct dentry *new_dentry) | |
613 | +{ | |
614 | + return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); | |
615 | +} | |
616 | + | |
617 | +#else | |
618 | + | |
619 | +/** | |
620 | + * ccs_inode_mknod - Check permission for mknod(). | |
621 | + * | |
622 | + * @dir: Pointer to "struct inode". | |
623 | + * @dentry: Pointer to "struct dentry". | |
624 | + * @mode: Create mode. | |
625 | + * @dev: Device major/minor number. | |
626 | + * | |
627 | + * Returns 0 on success, negative value otherwise. | |
628 | + */ | |
629 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
630 | + umode_t mode, dev_t dev) | |
631 | +{ | |
632 | + return ccs_mknod_permission(dentry, NULL, mode, dev); | |
633 | +} | |
634 | + | |
635 | +/** | |
636 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
637 | + * | |
638 | + * @dir: Pointer to "struct inode". | |
639 | + * @dentry: Pointer to "struct dentry". | |
640 | + * @mode: Create mode. | |
641 | + * | |
642 | + * Returns 0 on success, negative value otherwise. | |
643 | + */ | |
644 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
645 | + umode_t mode) | |
646 | +{ | |
647 | + return ccs_mkdir_permission(dentry, NULL, mode); | |
648 | +} | |
649 | + | |
650 | +/** | |
651 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
652 | + * | |
653 | + * @dir: Pointer to "struct inode". | |
654 | + * @dentry: Pointer to "struct dentry". | |
655 | + * | |
656 | + * Returns 0 on success, negative value otherwise. | |
657 | + */ | |
658 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
659 | +{ | |
660 | + return ccs_rmdir_permission(dentry, NULL); | |
661 | +} | |
662 | + | |
663 | +/** | |
664 | + * ccs_inode_unlink - Check permission for unlink(). | |
665 | + * | |
666 | + * @dir: Pointer to "struct inode". | |
667 | + * @dentry: Pointer to "struct dentry". | |
668 | + * | |
669 | + * Returns 0 on success, negative value otherwise. | |
670 | + */ | |
671 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
672 | +{ | |
673 | + return ccs_unlink_permission(dentry, NULL); | |
674 | +} | |
675 | + | |
676 | +/** | |
677 | + * ccs_inode_symlink - Check permission for symlink(). | |
678 | + * | |
679 | + * @dir: Pointer to "struct inode". | |
680 | + * @dentry: Pointer to "struct dentry". | |
681 | + * @old_name: Content of symbolic link. | |
682 | + * | |
683 | + * Returns 0 on success, negative value otherwise. | |
684 | + */ | |
685 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
686 | + const char *old_name) | |
687 | +{ | |
688 | + return ccs_symlink_permission(dentry, NULL, old_name); | |
689 | +} | |
690 | + | |
691 | +/** | |
692 | + * ccs_inode_rename - Check permission for rename(). | |
693 | + * | |
694 | + * @old_dir: Pointer to "struct inode". | |
695 | + * @old_dentry: Pointer to "struct dentry". | |
696 | + * @new_dir: Pointer to "struct inode". | |
697 | + * @new_dentry: Pointer to "struct dentry". | |
698 | + * | |
699 | + * Returns 0 on success, negative value otherwise. | |
700 | + */ | |
701 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
702 | + struct inode *new_dir, struct dentry *new_dentry) | |
703 | +{ | |
704 | + return ccs_rename_permission(old_dentry, new_dentry, NULL); | |
705 | +} | |
706 | + | |
707 | +/** | |
708 | + * ccs_inode_link - Check permission for link(). | |
709 | + * | |
710 | + * @old_dentry: Pointer to "struct dentry". | |
711 | + * @dir: Pointer to "struct inode". | |
712 | + * @new_dentry: Pointer to "struct dentry". | |
713 | + * | |
714 | + * Returns 0 on success, negative value otherwise. | |
715 | + */ | |
716 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
717 | + struct dentry *new_dentry) | |
718 | +{ | |
719 | + return ccs_link_permission(old_dentry, new_dentry, NULL); | |
720 | +} | |
721 | + | |
722 | +/** | |
723 | + * ccs_inode_create - Check permission for creat(). | |
724 | + * | |
725 | + * @dir: Pointer to "struct inode". | |
726 | + * @dentry: Pointer to "struct dentry". | |
727 | + * @mode: Create mode. | |
728 | + * | |
729 | + * Returns 0 on success, negative value otherwise. | |
730 | + */ | |
731 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
732 | + umode_t mode) | |
733 | +{ | |
734 | + return ccs_mknod_permission(dentry, NULL, mode, 0); | |
735 | +} | |
736 | + | |
737 | +#endif | |
738 | + | |
739 | +#ifdef CONFIG_SECURITY_NETWORK | |
740 | + | |
741 | +#include <net/sock.h> | |
742 | + | |
743 | +/* Structure for remembering an accept()ed socket's status. */ | |
744 | +struct ccs_socket_tag { | |
745 | + struct list_head list; | |
746 | + struct inode *inode; | |
747 | + int status; | |
748 | + struct rcu_head rcu; | |
749 | +}; | |
750 | + | |
751 | +/* | |
752 | + * List for managing accept()ed sockets. | |
753 | + * Since we don't need to keep an accept()ed socket into this list after | |
754 | + * once the permission was granted, the number of entries in this list is | |
755 | + * likely small. Therefore, we don't use hash tables. | |
756 | + */ | |
757 | +static LIST_HEAD(ccs_accepted_socket_list); | |
758 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
759 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
760 | + | |
761 | +/** | |
762 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
763 | + * | |
764 | + * @rcu: Pointer to "struct rcu_head". | |
765 | + * | |
766 | + * Returns nothing. | |
767 | + */ | |
768 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
769 | +{ | |
770 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
771 | + kfree(ptr); | |
772 | +} | |
773 | + | |
774 | +/** | |
775 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
776 | + * | |
777 | + * @inode: Pointer to "struct inode". | |
778 | + * @status: New status. | |
779 | + * | |
780 | + * Returns nothing. | |
781 | + * | |
782 | + * If @status == 0, memory for that socket will be released after RCU grace | |
783 | + * period. | |
784 | + */ | |
785 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
786 | +{ | |
787 | + struct ccs_socket_tag *ptr; | |
788 | + /* | |
789 | + * Protect whole section because multiple threads may call this | |
790 | + * function with same "sock" via ccs_validate_socket(). | |
791 | + */ | |
792 | + spin_lock(&ccs_accepted_socket_list_lock); | |
793 | + rcu_read_lock(); | |
794 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
795 | + if (ptr->inode != inode) | |
796 | + continue; | |
797 | + ptr->status = status; | |
798 | + if (status) | |
799 | + break; | |
800 | + list_del_rcu(&ptr->list); | |
801 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
802 | + break; | |
803 | + } | |
804 | + rcu_read_unlock(); | |
805 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
806 | +} | |
807 | + | |
808 | +/** | |
809 | + * ccs_validate_socket - Check post accept() permission if needed. | |
810 | + * | |
811 | + * @sock: Pointer to "struct socket". | |
812 | + * | |
813 | + * Returns 0 on success, negative value otherwise. | |
814 | + */ | |
815 | +static int ccs_validate_socket(struct socket *sock) | |
816 | +{ | |
817 | + struct inode *inode = SOCK_INODE(sock); | |
818 | + struct ccs_socket_tag *ptr; | |
819 | + int ret = 0; | |
820 | + rcu_read_lock(); | |
821 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
822 | + if (ptr->inode != inode) | |
823 | + continue; | |
824 | + ret = ptr->status; | |
825 | + break; | |
826 | + } | |
827 | + rcu_read_unlock(); | |
828 | + if (ret <= 0) | |
829 | + /* | |
830 | + * This socket is not an accept()ed socket or this socket is | |
831 | + * an accept()ed socket and post accept() permission is done. | |
832 | + */ | |
833 | + return ret; | |
834 | + /* | |
835 | + * Check post accept() permission now. | |
836 | + * | |
837 | + * Strictly speaking, we need to pass both listen()ing socket and | |
838 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
839 | + * But since socket's family and type are same for both sockets, | |
840 | + * passing the accept()ed socket in place for the listen()ing socket | |
841 | + * will work. | |
842 | + */ | |
843 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
844 | + /* | |
845 | + * If permission was granted, we forget that this is an accept()ed | |
846 | + * socket. Otherwise, we remember that this socket needs to return | |
847 | + * error for subsequent socketcalls. | |
848 | + */ | |
849 | + ccs_update_socket_tag(inode, ret); | |
850 | + return ret; | |
851 | +} | |
852 | + | |
853 | +/** | |
854 | + * ccs_socket_accept - Check permission for accept(). | |
855 | + * | |
856 | + * @sock: Pointer to "struct socket". | |
857 | + * @newsock: Pointer to "struct socket". | |
858 | + * | |
859 | + * Returns 0 on success, negative value otherwise. | |
860 | + * | |
861 | + * This hook is used for setting up environment for doing post accept() | |
862 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
863 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
864 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
865 | + * in order to do post accept() permission check before returning to userspace. | |
866 | + * If we make the copy in security_socket_post_create(), it would be possible | |
867 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
868 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
869 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
870 | + * rather than between sock->ops->accept() and returning to userspace. | |
871 | + * This means that if a socket was close()d before calling some socket | |
872 | + * syscalls, post accept() permission check will not be done. | |
873 | + */ | |
874 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
875 | +{ | |
876 | + struct ccs_socket_tag *ptr; | |
877 | + const int rc = ccs_validate_socket(sock); | |
878 | + if (rc < 0) | |
879 | + return rc; | |
880 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
881 | + if (!ptr) | |
882 | + return -ENOMEM; | |
883 | + /* | |
884 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
885 | + * "newsock" as "an accept()ed socket but post accept() permission | |
886 | + * check is not done yet" by allocating memory using inode of the | |
887 | + * "newsock" as a search key. | |
888 | + */ | |
889 | + ptr->inode = SOCK_INODE(newsock); | |
890 | + ptr->status = 1; /* Check post accept() permission later. */ | |
891 | + spin_lock(&ccs_accepted_socket_list_lock); | |
892 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
893 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
894 | + return 0; | |
895 | +} | |
896 | + | |
897 | +/** | |
898 | + * ccs_socket_listen - Check permission for listen(). | |
899 | + * | |
900 | + * @sock: Pointer to "struct socket". | |
901 | + * @backlog: Backlog parameter. | |
902 | + * | |
903 | + * Returns 0 on success, negative value otherwise. | |
904 | + */ | |
905 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
906 | +{ | |
907 | + const int rc = ccs_validate_socket(sock); | |
908 | + if (rc < 0) | |
909 | + return rc; | |
910 | + return ccs_socket_listen_permission(sock); | |
911 | +} | |
912 | + | |
913 | +/** | |
914 | + * ccs_socket_connect - Check permission for connect(). | |
915 | + * | |
916 | + * @sock: Pointer to "struct socket". | |
917 | + * @addr: Pointer to "struct sockaddr". | |
918 | + * @addr_len: Size of @addr. | |
919 | + * | |
920 | + * Returns 0 on success, negative value otherwise. | |
921 | + */ | |
922 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
923 | + int addr_len) | |
924 | +{ | |
925 | + const int rc = ccs_validate_socket(sock); | |
926 | + if (rc < 0) | |
927 | + return rc; | |
928 | + return ccs_socket_connect_permission(sock, addr, addr_len); | |
929 | +} | |
930 | + | |
931 | +/** | |
932 | + * ccs_socket_bind - Check permission for bind(). | |
933 | + * | |
934 | + * @sock: Pointer to "struct socket". | |
935 | + * @addr: Pointer to "struct sockaddr". | |
936 | + * @addr_len: Size of @addr. | |
937 | + * | |
938 | + * Returns 0 on success, negative value otherwise. | |
939 | + */ | |
940 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
941 | + int addr_len) | |
942 | +{ | |
943 | + const int rc = ccs_validate_socket(sock); | |
944 | + if (rc < 0) | |
945 | + return rc; | |
946 | + return ccs_socket_bind_permission(sock, addr, addr_len); | |
947 | +} | |
948 | + | |
949 | +/** | |
950 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
951 | + * | |
952 | + * @sock: Pointer to "struct socket". | |
953 | + * @msg: Pointer to "struct msghdr". | |
954 | + * @size: Size of message. | |
955 | + * | |
956 | + * Returns 0 on success, negative value otherwise. | |
957 | + */ | |
958 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
959 | + int size) | |
960 | +{ | |
961 | + const int rc = ccs_validate_socket(sock); | |
962 | + if (rc < 0) | |
963 | + return rc; | |
964 | + return ccs_socket_sendmsg_permission(sock, msg, size); | |
965 | +} | |
966 | + | |
967 | +/** | |
968 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
969 | + * | |
970 | + * @sock: Pointer to "struct socket". | |
971 | + * @msg: Pointer to "struct msghdr". | |
972 | + * @size: Size of message. | |
973 | + * @flags: Flags. | |
974 | + * | |
975 | + * Returns 0 on success, negative value otherwise. | |
976 | + */ | |
977 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
978 | + int size, int flags) | |
979 | +{ | |
980 | + return ccs_validate_socket(sock); | |
981 | +} | |
982 | + | |
983 | +/** | |
984 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
985 | + * | |
986 | + * @sock: Pointer to "struct socket". | |
987 | + * | |
988 | + * Returns 0 on success, negative value otherwise. | |
989 | + */ | |
990 | +static int ccs_socket_getsockname(struct socket *sock) | |
991 | +{ | |
992 | + return ccs_validate_socket(sock); | |
993 | +} | |
994 | + | |
995 | +/** | |
996 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
997 | + * | |
998 | + * @sock: Pointer to "struct socket". | |
999 | + * | |
1000 | + * Returns 0 on success, negative value otherwise. | |
1001 | + */ | |
1002 | +static int ccs_socket_getpeername(struct socket *sock) | |
1003 | +{ | |
1004 | + return ccs_validate_socket(sock); | |
1005 | +} | |
1006 | + | |
1007 | +/** | |
1008 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
1009 | + * | |
1010 | + * @sock: Pointer to "struct socket". | |
1011 | + * @level: Level. | |
1012 | + * @optname: Option's name, | |
1013 | + * | |
1014 | + * Returns 0 on success, negative value otherwise. | |
1015 | + */ | |
1016 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1017 | +{ | |
1018 | + return ccs_validate_socket(sock); | |
1019 | +} | |
1020 | + | |
1021 | +/** | |
1022 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1023 | + * | |
1024 | + * @sock: Pointer to "struct socket". | |
1025 | + * @level: Level. | |
1026 | + * @optname: Option's name, | |
1027 | + * | |
1028 | + * Returns 0 on success, negative value otherwise. | |
1029 | + */ | |
1030 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1031 | +{ | |
1032 | + return ccs_validate_socket(sock); | |
1033 | +} | |
1034 | + | |
1035 | +/** | |
1036 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1037 | + * | |
1038 | + * @sock: Pointer to "struct socket". | |
1039 | + * @how: Shutdown mode. | |
1040 | + * | |
1041 | + * Returns 0 on success, negative value otherwise. | |
1042 | + */ | |
1043 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1044 | +{ | |
1045 | + return ccs_validate_socket(sock); | |
1046 | +} | |
1047 | + | |
1048 | +#define SOCKFS_MAGIC 0x534F434B | |
1049 | + | |
1050 | +/** | |
1051 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1052 | + * | |
1053 | + * @inode: Pointer to "struct inode". | |
1054 | + * | |
1055 | + * Returns nothing. | |
1056 | + * | |
1057 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1058 | + */ | |
1059 | +static void ccs_inode_free_security(struct inode *inode) | |
1060 | +{ | |
1061 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1062 | + ccs_update_socket_tag(inode, 0); | |
1063 | +} | |
1064 | + | |
1065 | +#endif | |
1066 | + | |
1067 | +/** | |
1068 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1069 | + * | |
1070 | + * @old_path: Pointer to "struct path". | |
1071 | + * @new_path: Pointer to "struct path". | |
1072 | + * | |
1073 | + * Returns 0 on success, negative value otherwise. | |
1074 | + */ | |
1075 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1076 | +{ | |
1077 | + return ccs_pivot_root_permission(old_path, new_path); | |
1078 | +} | |
1079 | + | |
1080 | +/** | |
1081 | + * ccs_sb_mount - Check permission for mount(). | |
1082 | + * | |
1083 | + * @dev_name: Name of device file. | |
1084 | + * @path: Pointer to "struct path". | |
1085 | + * @type: Name of filesystem type. Maybe NULL. | |
1086 | + * @flags: Mount options. | |
1087 | + * @data_page: Optional data. Maybe NULL. | |
1088 | + * | |
1089 | + * Returns 0 on success, negative value otherwise. | |
1090 | + */ | |
1091 | +static int ccs_sb_mount(const char *dev_name, struct path *path, | |
1092 | + const char *type, unsigned long flags, void *data_page) | |
1093 | +{ | |
1094 | + return ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1095 | +} | |
1096 | + | |
1097 | +/** | |
1098 | + * ccs_sb_umount - Check permission for umount(). | |
1099 | + * | |
1100 | + * @mnt: Pointer to "struct vfsmount". | |
1101 | + * @flags: Unmount flags. | |
1102 | + * | |
1103 | + * Returns 0 on success, negative value otherwise. | |
1104 | + */ | |
1105 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1106 | +{ | |
1107 | + return ccs_umount_permission(mnt, flags); | |
1108 | +} | |
1109 | + | |
1110 | +/** | |
1111 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1112 | + * | |
1113 | + * @file: Pointer to "struct file". | |
1114 | + * @cmd: Command number. | |
1115 | + * @arg: Value for @cmd. | |
1116 | + * | |
1117 | + * Returns 0 on success, negative value otherwise. | |
1118 | + */ | |
1119 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1120 | + unsigned long arg) | |
1121 | +{ | |
1122 | + return ccs_fcntl_permission(file, cmd, arg); | |
1123 | +} | |
1124 | + | |
1125 | +/** | |
1126 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1127 | + * | |
1128 | + * @filp: Pointer to "struct file". | |
1129 | + * @cmd: Command number. | |
1130 | + * @arg: Value for @cmd. | |
1131 | + * | |
1132 | + * Returns 0 on success, negative value otherwise. | |
1133 | + */ | |
1134 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1135 | + unsigned long arg) | |
1136 | +{ | |
1137 | + return ccs_ioctl_permission(filp, cmd, arg); | |
1138 | +} | |
1139 | + | |
1140 | +#define MY_HOOK_INIT(HEAD, HOOK) \ | |
1141 | + { .head = &probe_dummy_security_hook_heads.HEAD, \ | |
1142 | + .hook = { .HEAD = HOOK } } | |
1143 | + | |
1144 | +static struct security_hook_list akari_hooks[] = { | |
1145 | + /* Security context allocator. */ | |
1146 | + MY_HOOK_INIT(cred_free, ccs_cred_free), | |
1147 | + MY_HOOK_INIT(cred_prepare, ccs_cred_prepare), | |
1148 | + MY_HOOK_INIT(cred_alloc_blank, ccs_cred_alloc_blank), | |
1149 | + MY_HOOK_INIT(cred_transfer, ccs_cred_transfer), | |
1150 | + MY_HOOK_INIT(task_create, ccs_task_create), | |
1151 | + /* Security context updater for successful execve(). */ | |
1152 | + MY_HOOK_INIT(bprm_check_security, ccs_bprm_check_security), | |
1153 | + MY_HOOK_INIT(bprm_committing_creds, ccs_bprm_committing_creds), | |
1154 | + /* Various permission checker. */ | |
1155 | + MY_HOOK_INIT(file_open, ccs_file_open), | |
1156 | + MY_HOOK_INIT(file_fcntl, ccs_file_fcntl), | |
1157 | + MY_HOOK_INIT(file_ioctl, ccs_file_ioctl), | |
1158 | + MY_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), | |
1159 | + MY_HOOK_INIT(sb_mount, ccs_sb_mount), | |
1160 | + MY_HOOK_INIT(sb_umount, ccs_sb_umount), | |
1161 | +#ifdef CONFIG_SECURITY_PATH | |
1162 | + MY_HOOK_INIT(path_mknod, ccs_path_mknod), | |
1163 | + MY_HOOK_INIT(path_mkdir, ccs_path_mkdir), | |
1164 | + MY_HOOK_INIT(path_rmdir, ccs_path_rmdir), | |
1165 | + MY_HOOK_INIT(path_unlink, ccs_path_unlink), | |
1166 | + MY_HOOK_INIT(path_symlink, ccs_path_symlink), | |
1167 | + MY_HOOK_INIT(path_rename, ccs_path_rename), | |
1168 | + MY_HOOK_INIT(path_link, ccs_path_link), | |
1169 | + MY_HOOK_INIT(path_truncate, ccs_path_truncate), | |
1170 | + MY_HOOK_INIT(path_chmod, ccs_path_chmod), | |
1171 | + MY_HOOK_INIT(path_chown, ccs_path_chown), | |
1172 | + MY_HOOK_INIT(path_chroot, ccs_path_chroot), | |
1173 | +#else | |
1174 | + MY_HOOK_INIT(inode_mknod, ccs_inode_mknod), | |
1175 | + MY_HOOK_INIT(inode_mkdir, ccs_inode_mkdir), | |
1176 | + MY_HOOK_INIT(inode_rmdir, ccs_inode_rmdir), | |
1177 | + MY_HOOK_INIT(inode_unlink, ccs_inode_unlink), | |
1178 | + MY_HOOK_INIT(inode_symlink, ccs_inode_symlink), | |
1179 | + MY_HOOK_INIT(inode_rename, ccs_inode_rename), | |
1180 | + MY_HOOK_INIT(inode_link, ccs_inode_link), | |
1181 | + MY_HOOK_INIT(inode_create, ccs_inode_create), | |
1182 | + MY_HOOK_INIT(inode_setattr, ccs_inode_setattr), | |
1183 | +#endif | |
1184 | + MY_HOOK_INIT(inode_getattr, ccs_inode_getattr), | |
1185 | +#ifdef CONFIG_SECURITY_NETWORK | |
1186 | + MY_HOOK_INIT(socket_bind, ccs_socket_bind), | |
1187 | + MY_HOOK_INIT(socket_connect, ccs_socket_connect), | |
1188 | + MY_HOOK_INIT(socket_listen, ccs_socket_listen), | |
1189 | + MY_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), | |
1190 | + MY_HOOK_INIT(socket_recvmsg, ccs_socket_recvmsg), | |
1191 | + MY_HOOK_INIT(socket_getsockname, ccs_socket_getsockname), | |
1192 | + MY_HOOK_INIT(socket_getpeername, ccs_socket_getpeername), | |
1193 | + MY_HOOK_INIT(socket_getsockopt, ccs_socket_getsockopt), | |
1194 | + MY_HOOK_INIT(socket_setsockopt, ccs_socket_setsockopt), | |
1195 | + MY_HOOK_INIT(socket_shutdown, ccs_socket_shutdown), | |
1196 | + MY_HOOK_INIT(socket_accept, ccs_socket_accept), | |
1197 | + MY_HOOK_INIT(inode_free_security, ccs_inode_free_security), | |
1198 | +#endif | |
1199 | +}; | |
1200 | + | |
1201 | +static inline void add_hook(struct security_hook_list *hook) | |
1202 | +{ | |
1203 | + list_add_tail_rcu(&hook->list, hook->head); | |
1204 | +} | |
1205 | + | |
1206 | +static void __init swap_hook(struct security_hook_list *hook, | |
1207 | + union security_list_options *original) | |
1208 | +{ | |
1209 | + struct list_head *list = hook->head; | |
1210 | + if (list_empty(list)) { | |
1211 | + add_hook(hook); | |
1212 | + } else { | |
1213 | + struct security_hook_list *shp = | |
1214 | + list_last_entry(list, struct security_hook_list, list); | |
1215 | + *original = shp->hook; | |
1216 | + smp_wmb(); | |
1217 | + shp->hook = hook->hook; | |
1218 | + } | |
1219 | +} | |
1220 | + | |
1221 | +/** | |
1222 | + * ccs_init - Initialize this module. | |
1223 | + * | |
1224 | + * Returns 0 on success, negative value otherwise. | |
1225 | + */ | |
1226 | +static int __init ccs_init(void) | |
1227 | +{ | |
1228 | + int idx; | |
1229 | + struct security_hook_heads *hooks = probe_security_hook_heads(); | |
1230 | + if (!hooks) | |
1231 | + goto out; | |
1232 | + for (idx = 0; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1233 | + akari_hooks[idx].head = ((void *) hooks) | |
1234 | + + ((unsigned long) akari_hooks[idx].head) | |
1235 | + - ((unsigned long) &probe_dummy_security_hook_heads); | |
1236 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1237 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1238 | + goto out; | |
1239 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1240 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1241 | + goto out; | |
1242 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1243 | + if (!ccsecurity_exports.d_absolute_path) | |
1244 | + goto out; | |
1245 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1246 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1247 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1248 | + } | |
1249 | + ccs_main_init(); | |
1250 | + swap_hook(&akari_hooks[0], &original_cred_free); | |
1251 | + swap_hook(&akari_hooks[1], &original_cred_prepare); | |
1252 | + swap_hook(&akari_hooks[2], &original_cred_alloc_blank); | |
1253 | + for (idx = 3; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1254 | + add_hook(&akari_hooks[idx]); | |
1255 | + printk(KERN_INFO "AKARI: 1.0.36 2017/02/20\n"); | |
1256 | + printk(KERN_INFO | |
1257 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1258 | + return 0; | |
1259 | +out: | |
1260 | + return -EINVAL; | |
1261 | +} | |
1262 | + | |
1263 | +module_init(ccs_init); | |
1264 | +MODULE_LICENSE("GPL"); | |
1265 | + | |
1266 | +/** | |
1267 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1268 | + * | |
1269 | + * @domain: Pointer to "struct ccs_domain_info". | |
1270 | + * | |
1271 | + * Returns true if @domain is in use, false otherwise. | |
1272 | + * | |
1273 | + * Caller holds rcu_read_lock(). | |
1274 | + */ | |
1275 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1276 | +{ | |
1277 | + int idx; | |
1278 | + struct ccs_security *ptr; | |
1279 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1280 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1281 | + list_for_each_entry_rcu(ptr, list, list) { | |
1282 | + struct ccs_execve *ee = ptr->ee; | |
1283 | + if (ptr->ccs_domain_info == domain || | |
1284 | + (ee && ee->previous_domain == domain)) { | |
1285 | + return true; | |
1286 | + } | |
1287 | + } | |
1288 | + } | |
1289 | + return false; | |
1290 | +} | |
1291 | + | |
1292 | +/** | |
1293 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1294 | + * | |
1295 | + * @ptr: Pointer to "struct ccs_security". | |
1296 | + * @list: Pointer to "struct list_head". | |
1297 | + * | |
1298 | + * Returns nothing. | |
1299 | + */ | |
1300 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1301 | + struct list_head *list) | |
1302 | +{ | |
1303 | + unsigned long flags; | |
1304 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1305 | + list_add_rcu(&ptr->list, list); | |
1306 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1307 | +} | |
1308 | + | |
1309 | +/** | |
1310 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1311 | + * | |
1312 | + * @task: Pointer to "struct task_struct". | |
1313 | + * | |
1314 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1315 | + * out of memory, &ccs_default_security otherwise. | |
1316 | + * | |
1317 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1318 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1319 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1320 | + * won't work. | |
1321 | + */ | |
1322 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1323 | +{ | |
1324 | + struct ccs_security *ptr; | |
1325 | + struct list_head *list = &ccs_task_security_list | |
1326 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1327 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1328 | + while (!list->next); | |
1329 | + rcu_read_lock(); | |
1330 | + list_for_each_entry_rcu(ptr, list, list) { | |
1331 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
1332 | + continue; | |
1333 | + rcu_read_unlock(); | |
1334 | + /* | |
1335 | + * Current thread needs to transit from old domain to new | |
1336 | + * domain before do_execve() succeeds in order to check | |
1337 | + * permission for interpreters and environment variables using | |
1338 | + * new domain's ACL rules. The domain transition has to be | |
1339 | + * visible from other CPU in order to allow interactive | |
1340 | + * enforcing mode. Also, the domain transition has to be | |
1341 | + * reverted if do_execve() failed. However, an LSM hook for | |
1342 | + * reverting domain transition is missing. | |
1343 | + * | |
1344 | + * security_prepare_creds() is called from prepare_creds() from | |
1345 | + * prepare_bprm_creds() from do_execve() before setting | |
1346 | + * current->in_execve flag, and current->in_execve flag is | |
1347 | + * cleared by the time next do_execve() request starts. | |
1348 | + * This means that we can emulate the missing LSM hook for | |
1349 | + * reverting domain transition, by calling this function from | |
1350 | + * security_prepare_creds(). | |
1351 | + * | |
1352 | + * If current->in_execve is not set but ptr->ccs_flags has | |
1353 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
1354 | + * has failed and reverting domain transition is needed. | |
1355 | + */ | |
1356 | + if (task == current && | |
1357 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
1358 | + !current->in_execve) { | |
1359 | + ccs_debug_trace("4"); | |
1360 | + ccs_clear_execve(-1, ptr); | |
1361 | + } | |
1362 | + return ptr; | |
1363 | + } | |
1364 | + rcu_read_unlock(); | |
1365 | + if (task != current) { | |
1366 | + /* | |
1367 | + * If a thread does nothing after fork(), caller will reach | |
1368 | + * here because "struct ccs_security" for that thread is not | |
1369 | + * yet allocated. But that thread is keeping a snapshot of | |
1370 | + * "struct ccs_security" taken as of ccs_task_create() | |
1371 | + * associated with that thread's "struct cred". | |
1372 | + * | |
1373 | + * Since that snapshot will be used as initial data when that | |
1374 | + * thread allocates "struct ccs_security" for that thread, we | |
1375 | + * can return that snapshot rather than &ccs_default_security. | |
1376 | + * | |
1377 | + * Since this function is called by only ccs_select_one() and | |
1378 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
1379 | + * it is guaranteed that caller has called rcu_read_lock() | |
1380 | + * (via ccs_tasklist_lock()) before finding this thread and | |
1381 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
1382 | + * like get_robust_list() does. | |
1383 | + */ | |
1384 | + return ccs_find_cred_security(__task_cred(task)); | |
1385 | + } | |
1386 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1387 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1388 | + if (!ptr) { | |
1389 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1390 | + task->pid); | |
1391 | + send_sig(SIGKILL, current, 0); | |
1392 | + return &ccs_oom_security; | |
1393 | + } | |
1394 | + *ptr = *ccs_find_cred_security(task->cred); | |
1395 | + /* We can shortcut because task == current. */ | |
1396 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
1397 | + pids[PIDTYPE_PID].pid); | |
1398 | + ptr->cred = NULL; | |
1399 | + ccs_add_task_security(ptr, list); | |
1400 | + return ptr; | |
1401 | +} | |
1402 | + | |
1403 | +/** | |
1404 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
1405 | + * | |
1406 | + * @new: Pointer to "struct cred". | |
1407 | + * @old: Pointer to "struct cred". | |
1408 | + * @gfp: Memory allocation flags. | |
1409 | + * | |
1410 | + * Returns 0 on success, negative value otherwise. | |
1411 | + */ | |
1412 | +static int ccs_copy_cred_security(const struct cred *new, | |
1413 | + const struct cred *old, gfp_t gfp) | |
1414 | +{ | |
1415 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
1416 | + struct ccs_security *new_security = | |
1417 | + kzalloc(sizeof(*new_security), gfp); | |
1418 | + if (!new_security) | |
1419 | + return -ENOMEM; | |
1420 | + *new_security = *old_security; | |
1421 | + new_security->cred = new; | |
1422 | + ccs_add_cred_security(new_security); | |
1423 | + return 0; | |
1424 | +} | |
1425 | + | |
1426 | +/** | |
1427 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
1428 | + * | |
1429 | + * @cred: Pointer to "struct cred". | |
1430 | + * | |
1431 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
1432 | + * otherwise. | |
1433 | + */ | |
1434 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
1435 | +{ | |
1436 | + struct ccs_security *ptr; | |
1437 | + struct list_head *list = &ccs_cred_security_list | |
1438 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
1439 | + rcu_read_lock(); | |
1440 | + list_for_each_entry_rcu(ptr, list, list) { | |
1441 | + if (ptr->cred != cred) | |
1442 | + continue; | |
1443 | + rcu_read_unlock(); | |
1444 | + return ptr; | |
1445 | + } | |
1446 | + rcu_read_unlock(); | |
1447 | + return &ccs_default_security; | |
1448 | +} | |
1449 | + | |
1450 | +/** | |
1451 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
1452 | + * | |
1453 | + * Returns nothing. | |
1454 | + * | |
1455 | + * Since security_task_free() is missing, I can't release memory associated | |
1456 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
1457 | + * "struct pid" and runs garbage collection when associated | |
1458 | + * "struct task_struct" has gone. | |
1459 | + */ | |
1460 | +static void ccs_task_security_gc(void) | |
1461 | +{ | |
1462 | + static DEFINE_SPINLOCK(lock); | |
1463 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
1464 | + unsigned int idx; | |
1465 | + /* | |
1466 | + * If some process is doing execve(), try to garbage collection now. | |
1467 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
1468 | + * as soon as execve() has completed in order to compensate for lack of | |
1469 | + * security_bprm_free() and security_task_free() hooks. | |
1470 | + * | |
1471 | + * Otherwise, reduce frequency for performance reason. | |
1472 | + */ | |
1473 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
1474 | + atomic_inc_return(&gc_counter) < 1024) | |
1475 | + return; | |
1476 | + if (!spin_trylock(&lock)) | |
1477 | + return; | |
1478 | + atomic_set(&gc_counter, 0); | |
1479 | + rcu_read_lock(); | |
1480 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1481 | + struct ccs_security *ptr; | |
1482 | + struct list_head *list = &ccs_task_security_list[idx]; | |
1483 | + list_for_each_entry_rcu(ptr, list, list) { | |
1484 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
1485 | + continue; | |
1486 | + ccs_del_security(ptr); | |
1487 | + } | |
1488 | + } | |
1489 | + rcu_read_unlock(); | |
1490 | + spin_unlock(&lock); | |
1491 | +} |
@@ -0,0 +1,1494 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | +static void ccs_task_security_gc(void); | |
14 | +static int ccs_copy_cred_security(const struct cred *new, | |
15 | + const struct cred *old, gfp_t gfp); | |
16 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred); | |
17 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
18 | +static atomic_t ccs_in_execve_tasks = ATOMIC_INIT(0); | |
19 | +/* | |
20 | + * List of "struct ccs_security" for "struct pid". | |
21 | + * | |
22 | + * All instances on this list is guaranteed that "struct ccs_security"->pid != | |
23 | + * NULL. Also, instances on this list that are in execve() are guaranteed that | |
24 | + * "struct ccs_security"->cred remembers "struct linux_binprm"->cred with a | |
25 | + * refcount on "struct linux_binprm"->cred. | |
26 | + */ | |
27 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
28 | +/* | |
29 | + * List of "struct ccs_security" for "struct cred". | |
30 | + * | |
31 | + * Since the number of "struct cred" is nearly equals to the number of | |
32 | + * "struct pid", we allocate hash tables like ccs_task_security_list. | |
33 | + * | |
34 | + * All instances on this list are guaranteed that "struct ccs_security"->pid == | |
35 | + * NULL and "struct ccs_security"->cred != NULL. | |
36 | + */ | |
37 | +static struct list_head ccs_cred_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
38 | + | |
39 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
40 | +static struct ccs_security ccs_oom_security = { | |
41 | + .ccs_domain_info = &ccs_kernel_domain | |
42 | +}; | |
43 | + | |
44 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
45 | +static struct ccs_security ccs_default_security = { | |
46 | + .ccs_domain_info = &ccs_kernel_domain | |
47 | +}; | |
48 | + | |
49 | +/* For exporting variables and functions. */ | |
50 | +struct ccsecurity_exports ccsecurity_exports; | |
51 | +/* Members are updated by loadable kernel module. */ | |
52 | +struct ccsecurity_operations ccsecurity_ops; | |
53 | + | |
54 | +/* Original hooks. */ | |
55 | +static union security_list_options original_cred_prepare; | |
56 | +static union security_list_options original_cred_free; | |
57 | +static union security_list_options original_cred_alloc_blank; | |
58 | + | |
59 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
60 | + | |
61 | +/** | |
62 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
63 | + * | |
64 | + * @count: Count to increment or decrement. | |
65 | + * | |
66 | + * Returns updated counter. | |
67 | + */ | |
68 | +static unsigned int ccs_update_ee_counter(int count) | |
69 | +{ | |
70 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
71 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
72 | + return atomic_add_return(count, &ccs_ee_counter); | |
73 | +} | |
74 | + | |
75 | +/** | |
76 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
77 | + * | |
78 | + * @ee: Pointer to "struct ccs_execve". | |
79 | + * | |
80 | + * Returns nothing. | |
81 | + */ | |
82 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
83 | +{ | |
84 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
85 | + current->pid, ccs_update_ee_counter(1) - 1); | |
86 | +} | |
87 | + | |
88 | +/** | |
89 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
90 | + * | |
91 | + * @ee: Pointer to "struct ccs_execve". | |
92 | + * @task: True if released by current task, false otherwise. | |
93 | + * | |
94 | + * Returns nothing. | |
95 | + */ | |
96 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
97 | + const bool is_current) | |
98 | +{ | |
99 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
100 | + if (is_current) | |
101 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
102 | + ee, current->pid, tmp); | |
103 | + else | |
104 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
105 | + ee, tmp); | |
106 | +} | |
107 | + | |
108 | +#endif | |
109 | + | |
110 | +#if !defined(CONFIG_AKARI_DEBUG) | |
111 | +#define ccs_debug_trace(pos) do { } while (0) | |
112 | +#else | |
113 | +#define ccs_debug_trace(pos) \ | |
114 | + do { \ | |
115 | + static bool done; \ | |
116 | + if (!done) { \ | |
117 | + printk(KERN_INFO \ | |
118 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
119 | + done = true; \ | |
120 | + } \ | |
121 | + } while (0) | |
122 | +#endif | |
123 | + | |
124 | +/** | |
125 | + * ccs_clear_execve - Release memory used by do_execve(). | |
126 | + * | |
127 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
128 | + * @security: Pointer to "struct ccs_security". | |
129 | + * | |
130 | + * Returns nothing. | |
131 | + */ | |
132 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
133 | +{ | |
134 | + struct ccs_execve *ee; | |
135 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
136 | + return; | |
137 | + ee = security->ee; | |
138 | + security->ee = NULL; | |
139 | + if (!ee) | |
140 | + return; | |
141 | + atomic_dec(&ccs_in_execve_tasks); | |
142 | + ccs_finish_execve(ret, ee); | |
143 | +} | |
144 | + | |
145 | +/** | |
146 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
147 | + * | |
148 | + * @rcu: Pointer to "struct rcu_head". | |
149 | + * | |
150 | + * Returns nothing. | |
151 | + */ | |
152 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
153 | +{ | |
154 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
155 | + struct ccs_execve *ee = ptr->ee; | |
156 | + /* | |
157 | + * If this security context was associated with "struct pid" and | |
158 | + * ptr->ccs_flags has CCS_TASK_IS_IN_EXECVE set, it indicates that a | |
159 | + * "struct task_struct" associated with this security context exited | |
160 | + * immediately after do_execve() has failed. | |
161 | + */ | |
162 | + if (ptr->pid && (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE)) { | |
163 | + ccs_debug_trace("1"); | |
164 | + atomic_dec(&ccs_in_execve_tasks); | |
165 | + } | |
166 | + /* | |
167 | + * If this security context was associated with "struct pid", | |
168 | + * drop refcount obtained by get_pid() in ccs_find_task_security(). | |
169 | + */ | |
170 | + if (ptr->pid) { | |
171 | + ccs_debug_trace("2"); | |
172 | + put_pid(ptr->pid); | |
173 | + } | |
174 | + if (ee) { | |
175 | + ccs_debug_trace("3"); | |
176 | + ccs_audit_free_execve(ee, false); | |
177 | + kfree(ee->handler_path); | |
178 | + kfree(ee); | |
179 | + } | |
180 | + kfree(ptr); | |
181 | +} | |
182 | + | |
183 | +/** | |
184 | + * ccs_del_security - Release "struct ccs_security". | |
185 | + * | |
186 | + * @ptr: Pointer to "struct ccs_security". | |
187 | + * | |
188 | + * Returns nothing. | |
189 | + */ | |
190 | +static void ccs_del_security(struct ccs_security *ptr) | |
191 | +{ | |
192 | + unsigned long flags; | |
193 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
194 | + return; | |
195 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
196 | + list_del_rcu(&ptr->list); | |
197 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
198 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
199 | +} | |
200 | + | |
201 | +/** | |
202 | + * ccs_add_cred_security - Add "struct ccs_security" to list. | |
203 | + * | |
204 | + * @ptr: Pointer to "struct ccs_security". | |
205 | + * | |
206 | + * Returns nothing. | |
207 | + */ | |
208 | +static void ccs_add_cred_security(struct ccs_security *ptr) | |
209 | +{ | |
210 | + unsigned long flags; | |
211 | + struct list_head *list = &ccs_cred_security_list | |
212 | + [hash_ptr((void *) ptr->cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
213 | +#ifdef CONFIG_AKARI_DEBUG | |
214 | + if (ptr->pid) | |
215 | + printk(KERN_INFO "AKARI: \"struct ccs_security\"->pid != NULL" | |
216 | + "\n"); | |
217 | +#endif | |
218 | + ptr->pid = NULL; | |
219 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
220 | + list_add_rcu(&ptr->list, list); | |
221 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
222 | +} | |
223 | + | |
224 | +/** | |
225 | + * ccs_task_create - Make snapshot of security context for new task. | |
226 | + * | |
227 | + * @clone_flags: Flags passed to clone(). | |
228 | + * | |
229 | + * Returns 0 on success, negative value otherwise. | |
230 | + */ | |
231 | +static int ccs_task_create(unsigned long clone_flags) | |
232 | +{ | |
233 | + struct ccs_security *old_security; | |
234 | + struct ccs_security *new_security; | |
235 | + struct cred *cred = prepare_creds(); | |
236 | + if (!cred) | |
237 | + return -ENOMEM; | |
238 | + old_security = ccs_find_task_security(current); | |
239 | + new_security = ccs_find_cred_security(cred); | |
240 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
241 | + new_security->ccs_flags = old_security->ccs_flags; | |
242 | + return commit_creds(cred); | |
243 | +} | |
244 | + | |
245 | +/** | |
246 | + * ccs_cred_prepare - Allocate memory for new credentials. | |
247 | + * | |
248 | + * @new: Pointer to "struct cred". | |
249 | + * @old: Pointer to "struct cred". | |
250 | + * @gfp: Memory allocation flags. | |
251 | + * | |
252 | + * Returns 0 on success, negative value otherwise. | |
253 | + */ | |
254 | +static int ccs_cred_prepare(struct cred *new, const struct cred *old, | |
255 | + gfp_t gfp) | |
256 | +{ | |
257 | + int rc1; | |
258 | + /* | |
259 | + * For checking whether reverting domain transition is needed or not. | |
260 | + * | |
261 | + * See ccs_find_task_security() for reason. | |
262 | + */ | |
263 | + if (gfp == GFP_KERNEL) | |
264 | + ccs_find_task_security(current); | |
265 | + rc1 = ccs_copy_cred_security(new, old, gfp); | |
266 | + if (gfp == GFP_KERNEL) | |
267 | + ccs_task_security_gc(); | |
268 | + if (original_cred_prepare.cred_prepare) { | |
269 | + const int rc2 = original_cred_prepare.cred_prepare(new, old, | |
270 | + gfp); | |
271 | + if (rc2) { | |
272 | + ccs_del_security(ccs_find_cred_security(new)); | |
273 | + return rc2; | |
274 | + } | |
275 | + } | |
276 | + return rc1; | |
277 | +} | |
278 | + | |
279 | +/** | |
280 | + * ccs_cred_free - Release memory used by credentials. | |
281 | + * | |
282 | + * @cred: Pointer to "struct cred". | |
283 | + * | |
284 | + * Returns nothing. | |
285 | + */ | |
286 | +static void ccs_cred_free(struct cred *cred) | |
287 | +{ | |
288 | + if (original_cred_free.cred_free) | |
289 | + original_cred_free.cred_free(cred); | |
290 | + ccs_del_security(ccs_find_cred_security(cred)); | |
291 | +} | |
292 | + | |
293 | +/** | |
294 | + * ccs_alloc_cred_security - Allocate memory for new credentials. | |
295 | + * | |
296 | + * @cred: Pointer to "struct cred". | |
297 | + * @gfp: Memory allocation flags. | |
298 | + * | |
299 | + * Returns 0 on success, negative value otherwise. | |
300 | + */ | |
301 | +static int ccs_alloc_cred_security(const struct cred *cred, gfp_t gfp) | |
302 | +{ | |
303 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
304 | + gfp); | |
305 | + if (!new_security) | |
306 | + return -ENOMEM; | |
307 | + new_security->cred = cred; | |
308 | + ccs_add_cred_security(new_security); | |
309 | + return 0; | |
310 | +} | |
311 | + | |
312 | +/** | |
313 | + * ccs_cred_alloc_blank - Allocate memory for new credentials. | |
314 | + * | |
315 | + * @new: Pointer to "struct cred". | |
316 | + * @gfp: Memory allocation flags. | |
317 | + * | |
318 | + * Returns 0 on success, negative value otherwise. | |
319 | + */ | |
320 | +static int ccs_cred_alloc_blank(struct cred *new, gfp_t gfp) | |
321 | +{ | |
322 | + const int rc1 = ccs_alloc_cred_security(new, gfp); | |
323 | + if (original_cred_alloc_blank.cred_alloc_blank) { | |
324 | + const int rc2 = original_cred_alloc_blank. | |
325 | + cred_alloc_blank(new, gfp); | |
326 | + if (rc2) { | |
327 | + ccs_del_security(ccs_find_cred_security(new)); | |
328 | + return rc2; | |
329 | + } | |
330 | + } | |
331 | + return rc1; | |
332 | +} | |
333 | + | |
334 | +/** | |
335 | + * ccs_cred_transfer - Transfer "struct ccs_security" between credentials. | |
336 | + * | |
337 | + * @new: Pointer to "struct cred". | |
338 | + * @old: Pointer to "struct cred". | |
339 | + * | |
340 | + * Returns nothing. | |
341 | + */ | |
342 | +static void ccs_cred_transfer(struct cred *new, const struct cred *old) | |
343 | +{ | |
344 | + struct ccs_security *new_security = ccs_find_cred_security(new); | |
345 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
346 | + if (new_security == &ccs_default_security || | |
347 | + new_security == &ccs_oom_security || | |
348 | + old_security == &ccs_default_security || | |
349 | + old_security == &ccs_oom_security) | |
350 | + return; | |
351 | + new_security->ccs_flags = old_security->ccs_flags; | |
352 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
353 | +} | |
354 | + | |
355 | +/** | |
356 | + * ccs_bprm_committing_creds - A hook which is called when do_execve() succeeded. | |
357 | + * | |
358 | + * @bprm: Pointer to "struct linux_binprm". | |
359 | + * | |
360 | + * Returns nothing. | |
361 | + */ | |
362 | +static void ccs_bprm_committing_creds(struct linux_binprm *bprm) | |
363 | +{ | |
364 | + struct ccs_security *old_security = ccs_current_security(); | |
365 | + struct ccs_security *new_security; | |
366 | + if (old_security == &ccs_default_security || | |
367 | + old_security == &ccs_oom_security) | |
368 | + return; | |
369 | + ccs_clear_execve(0, old_security); | |
370 | + /* Update current task's cred's domain for future fork(). */ | |
371 | + new_security = ccs_find_cred_security(bprm->cred); | |
372 | + new_security->ccs_flags = old_security->ccs_flags; | |
373 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
374 | +} | |
375 | + | |
376 | +/** | |
377 | + * ccs_bprm_check_security - Check permission for execve(). | |
378 | + * | |
379 | + * @bprm: Pointer to "struct linux_binprm". | |
380 | + * | |
381 | + * Returns 0 on success, negative value otherwise. | |
382 | + */ | |
383 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
384 | +{ | |
385 | + struct ccs_security *security = ccs_current_security(); | |
386 | + int rc; | |
387 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
388 | + return -ENOMEM; | |
389 | + if (security->ee) | |
390 | + return 0; | |
391 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
392 | + if (!ccs_policy_loaded) | |
393 | + ccs_load_policy(bprm->filename); | |
394 | +#endif | |
395 | + rc = ccs_start_execve(bprm, &security->ee); | |
396 | + if (security->ee) | |
397 | + atomic_inc(&ccs_in_execve_tasks); | |
398 | + return rc; | |
399 | +} | |
400 | + | |
401 | +/** | |
402 | + * ccs_file_open - Check permission for open(). | |
403 | + * | |
404 | + * @f: Pointer to "struct file". | |
405 | + * @cred: Pointer to "struct cred". | |
406 | + * | |
407 | + * Returns 0 on success, negative value otherwise. | |
408 | + */ | |
409 | +static int ccs_file_open(struct file *f, const struct cred *cred) | |
410 | +{ | |
411 | + return ccs_open_permission(f); | |
412 | +} | |
413 | + | |
414 | +#ifdef CONFIG_SECURITY_PATH | |
415 | + | |
416 | +/** | |
417 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
418 | + * | |
419 | + * @path: Pointer to "struct path". | |
420 | + * @user: User ID. | |
421 | + * @group: Group ID. | |
422 | + * | |
423 | + * Returns 0 on success, negative value otherwise. | |
424 | + */ | |
425 | +static int ccs_path_chown(const struct path *path, kuid_t user, kgid_t group) | |
426 | +{ | |
427 | + return ccs_chown_permission(path->dentry, path->mnt, user, group); | |
428 | +} | |
429 | + | |
430 | +/** | |
431 | + * ccs_path_chmod - Check permission for chmod(). | |
432 | + * | |
433 | + * @path: Pointer to "struct path". | |
434 | + * @mode: Mode. | |
435 | + * | |
436 | + * Returns 0 on success, negative value otherwise. | |
437 | + */ | |
438 | +static int ccs_path_chmod(const struct path *path, umode_t mode) | |
439 | +{ | |
440 | + return ccs_chmod_permission(path->dentry, path->mnt, mode); | |
441 | +} | |
442 | + | |
443 | +/** | |
444 | + * ccs_path_chroot - Check permission for chroot(). | |
445 | + * | |
446 | + * @path: Pointer to "struct path". | |
447 | + * | |
448 | + * Returns 0 on success, negative value otherwise. | |
449 | + */ | |
450 | +static int ccs_path_chroot(const struct path *path) | |
451 | +{ | |
452 | + return ccs_chroot_permission(path); | |
453 | +} | |
454 | + | |
455 | +/** | |
456 | + * ccs_path_truncate - Check permission for truncate(). | |
457 | + * | |
458 | + * @path: Pointer to "struct path". | |
459 | + * | |
460 | + * Returns 0 on success, negative value otherwise. | |
461 | + */ | |
462 | +static int ccs_path_truncate(const struct path *path) | |
463 | +{ | |
464 | + return ccs_truncate_permission(path->dentry, path->mnt); | |
465 | +} | |
466 | + | |
467 | +#else | |
468 | + | |
469 | +/** | |
470 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
471 | + * | |
472 | + * @dentry: Pointer to "struct dentry". | |
473 | + * @attr: Pointer to "struct iattr". | |
474 | + * | |
475 | + * Returns 0 on success, negative value otherwise. | |
476 | + */ | |
477 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
478 | +{ | |
479 | + const int rc1 = (attr->ia_valid & ATTR_UID) ? | |
480 | + ccs_chown_permission(dentry, NULL, attr->ia_uid, INVALID_GID) : | |
481 | + 0; | |
482 | + const int rc2 = (attr->ia_valid & ATTR_GID) ? | |
483 | + ccs_chown_permission(dentry, NULL, INVALID_UID, attr->ia_gid) : | |
484 | + 0; | |
485 | + const int rc3 = (attr->ia_valid & ATTR_MODE) ? | |
486 | + ccs_chmod_permission(dentry, NULL, attr->ia_mode) : 0; | |
487 | + const int rc4 = (attr->ia_valid & ATTR_SIZE) ? | |
488 | + ccs_truncate_permission(dentry, NULL) : 0; | |
489 | + if (rc4) | |
490 | + return rc4; | |
491 | + if (rc3) | |
492 | + return rc3; | |
493 | + if (rc2) | |
494 | + return rc2; | |
495 | + return rc1; | |
496 | +} | |
497 | + | |
498 | +#endif | |
499 | + | |
500 | +/** | |
501 | + * ccs_inode_getattr - Check permission for stat(). | |
502 | + * | |
503 | + * @path: Pointer to "struct path". | |
504 | + * | |
505 | + * Returns 0 on success, negative value otherwise. | |
506 | + */ | |
507 | +static int ccs_inode_getattr(const struct path *path) | |
508 | +{ | |
509 | + return ccs_getattr_permission(path->mnt, path->dentry); | |
510 | +} | |
511 | + | |
512 | +#ifdef CONFIG_SECURITY_PATH | |
513 | + | |
514 | +/** | |
515 | + * ccs_path_mknod - Check permission for mknod(). | |
516 | + * | |
517 | + * @dir: Pointer to "struct path". | |
518 | + * @dentry: Pointer to "struct dentry". | |
519 | + * @mode: Create mode. | |
520 | + * @dev: Device major/minor number. | |
521 | + * | |
522 | + * Returns 0 on success, negative value otherwise. | |
523 | + */ | |
524 | +static int ccs_path_mknod(const struct path *dir, struct dentry *dentry, | |
525 | + umode_t mode, unsigned int dev) | |
526 | +{ | |
527 | + return ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
528 | +} | |
529 | + | |
530 | +/** | |
531 | + * ccs_path_mkdir - Check permission for mkdir(). | |
532 | + * | |
533 | + * @dir: Pointer to "struct path". | |
534 | + * @dentry: Pointer to "struct dentry". | |
535 | + * @mode: Create mode. | |
536 | + * | |
537 | + * Returns 0 on success, negative value otherwise. | |
538 | + */ | |
539 | +static int ccs_path_mkdir(const struct path *dir, struct dentry *dentry, | |
540 | + umode_t mode) | |
541 | +{ | |
542 | + return ccs_mkdir_permission(dentry, dir->mnt, mode); | |
543 | +} | |
544 | + | |
545 | +/** | |
546 | + * ccs_path_rmdir - Check permission for rmdir(). | |
547 | + * | |
548 | + * @dir: Pointer to "struct path". | |
549 | + * @dentry: Pointer to "struct dentry". | |
550 | + * | |
551 | + * Returns 0 on success, negative value otherwise. | |
552 | + */ | |
553 | +static int ccs_path_rmdir(const struct path *dir, struct dentry *dentry) | |
554 | +{ | |
555 | + return ccs_rmdir_permission(dentry, dir->mnt); | |
556 | +} | |
557 | + | |
558 | +/** | |
559 | + * ccs_path_unlink - Check permission for unlink(). | |
560 | + * | |
561 | + * @dir: Pointer to "struct path". | |
562 | + * @dentry: Pointer to "struct dentry". | |
563 | + * | |
564 | + * Returns 0 on success, negative value otherwise. | |
565 | + */ | |
566 | +static int ccs_path_unlink(const struct path *dir, struct dentry *dentry) | |
567 | +{ | |
568 | + return ccs_unlink_permission(dentry, dir->mnt); | |
569 | +} | |
570 | + | |
571 | +/** | |
572 | + * ccs_path_symlink - Check permission for symlink(). | |
573 | + * | |
574 | + * @dir: Pointer to "struct path". | |
575 | + * @dentry: Pointer to "struct dentry". | |
576 | + * @old_name: Content of symbolic link. | |
577 | + * | |
578 | + * Returns 0 on success, negative value otherwise. | |
579 | + */ | |
580 | +static int ccs_path_symlink(const struct path *dir, struct dentry *dentry, | |
581 | + const char *old_name) | |
582 | +{ | |
583 | + return ccs_symlink_permission(dentry, dir->mnt, old_name); | |
584 | +} | |
585 | + | |
586 | +/** | |
587 | + * ccs_path_rename - Check permission for rename(). | |
588 | + * | |
589 | + * @old_dir: Pointer to "struct path". | |
590 | + * @old_dentry: Pointer to "struct dentry". | |
591 | + * @new_dir: Pointer to "struct path". | |
592 | + * @new_dentry: Pointer to "struct dentry". | |
593 | + * | |
594 | + * Returns 0 on success, negative value otherwise. | |
595 | + */ | |
596 | +static int ccs_path_rename(const struct path *old_dir, | |
597 | + struct dentry *old_dentry, | |
598 | + const struct path *new_dir, | |
599 | + struct dentry *new_dentry) | |
600 | +{ | |
601 | + return ccs_rename_permission(old_dentry, new_dentry, old_dir->mnt); | |
602 | +} | |
603 | + | |
604 | +/** | |
605 | + * ccs_path_link - Check permission for link(). | |
606 | + * | |
607 | + * @old_dentry: Pointer to "struct dentry". | |
608 | + * @new_dir: Pointer to "struct path". | |
609 | + * @new_dentry: Pointer to "struct dentry". | |
610 | + * | |
611 | + * Returns 0 on success, negative value otherwise. | |
612 | + */ | |
613 | +static int ccs_path_link(struct dentry *old_dentry, const struct path *new_dir, | |
614 | + struct dentry *new_dentry) | |
615 | +{ | |
616 | + return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); | |
617 | +} | |
618 | + | |
619 | +#else | |
620 | + | |
621 | +/** | |
622 | + * ccs_inode_mknod - Check permission for mknod(). | |
623 | + * | |
624 | + * @dir: Pointer to "struct inode". | |
625 | + * @dentry: Pointer to "struct dentry". | |
626 | + * @mode: Create mode. | |
627 | + * @dev: Device major/minor number. | |
628 | + * | |
629 | + * Returns 0 on success, negative value otherwise. | |
630 | + */ | |
631 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
632 | + umode_t mode, dev_t dev) | |
633 | +{ | |
634 | + return ccs_mknod_permission(dentry, NULL, mode, dev); | |
635 | +} | |
636 | + | |
637 | +/** | |
638 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
639 | + * | |
640 | + * @dir: Pointer to "struct inode". | |
641 | + * @dentry: Pointer to "struct dentry". | |
642 | + * @mode: Create mode. | |
643 | + * | |
644 | + * Returns 0 on success, negative value otherwise. | |
645 | + */ | |
646 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
647 | + umode_t mode) | |
648 | +{ | |
649 | + return ccs_mkdir_permission(dentry, NULL, mode); | |
650 | +} | |
651 | + | |
652 | +/** | |
653 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
654 | + * | |
655 | + * @dir: Pointer to "struct inode". | |
656 | + * @dentry: Pointer to "struct dentry". | |
657 | + * | |
658 | + * Returns 0 on success, negative value otherwise. | |
659 | + */ | |
660 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
661 | +{ | |
662 | + return ccs_rmdir_permission(dentry, NULL); | |
663 | +} | |
664 | + | |
665 | +/** | |
666 | + * ccs_inode_unlink - Check permission for unlink(). | |
667 | + * | |
668 | + * @dir: Pointer to "struct inode". | |
669 | + * @dentry: Pointer to "struct dentry". | |
670 | + * | |
671 | + * Returns 0 on success, negative value otherwise. | |
672 | + */ | |
673 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
674 | +{ | |
675 | + return ccs_unlink_permission(dentry, NULL); | |
676 | +} | |
677 | + | |
678 | +/** | |
679 | + * ccs_inode_symlink - Check permission for symlink(). | |
680 | + * | |
681 | + * @dir: Pointer to "struct inode". | |
682 | + * @dentry: Pointer to "struct dentry". | |
683 | + * @old_name: Content of symbolic link. | |
684 | + * | |
685 | + * Returns 0 on success, negative value otherwise. | |
686 | + */ | |
687 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
688 | + const char *old_name) | |
689 | +{ | |
690 | + return ccs_symlink_permission(dentry, NULL, old_name); | |
691 | +} | |
692 | + | |
693 | +/** | |
694 | + * ccs_inode_rename - Check permission for rename(). | |
695 | + * | |
696 | + * @old_dir: Pointer to "struct inode". | |
697 | + * @old_dentry: Pointer to "struct dentry". | |
698 | + * @new_dir: Pointer to "struct inode". | |
699 | + * @new_dentry: Pointer to "struct dentry". | |
700 | + * | |
701 | + * Returns 0 on success, negative value otherwise. | |
702 | + */ | |
703 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
704 | + struct inode *new_dir, struct dentry *new_dentry) | |
705 | +{ | |
706 | + return ccs_rename_permission(old_dentry, new_dentry, NULL); | |
707 | +} | |
708 | + | |
709 | +/** | |
710 | + * ccs_inode_link - Check permission for link(). | |
711 | + * | |
712 | + * @old_dentry: Pointer to "struct dentry". | |
713 | + * @dir: Pointer to "struct inode". | |
714 | + * @new_dentry: Pointer to "struct dentry". | |
715 | + * | |
716 | + * Returns 0 on success, negative value otherwise. | |
717 | + */ | |
718 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
719 | + struct dentry *new_dentry) | |
720 | +{ | |
721 | + return ccs_link_permission(old_dentry, new_dentry, NULL); | |
722 | +} | |
723 | + | |
724 | +/** | |
725 | + * ccs_inode_create - Check permission for creat(). | |
726 | + * | |
727 | + * @dir: Pointer to "struct inode". | |
728 | + * @dentry: Pointer to "struct dentry". | |
729 | + * @mode: Create mode. | |
730 | + * | |
731 | + * Returns 0 on success, negative value otherwise. | |
732 | + */ | |
733 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
734 | + umode_t mode) | |
735 | +{ | |
736 | + return ccs_mknod_permission(dentry, NULL, mode, 0); | |
737 | +} | |
738 | + | |
739 | +#endif | |
740 | + | |
741 | +#ifdef CONFIG_SECURITY_NETWORK | |
742 | + | |
743 | +#include <net/sock.h> | |
744 | + | |
745 | +/* Structure for remembering an accept()ed socket's status. */ | |
746 | +struct ccs_socket_tag { | |
747 | + struct list_head list; | |
748 | + struct inode *inode; | |
749 | + int status; | |
750 | + struct rcu_head rcu; | |
751 | +}; | |
752 | + | |
753 | +/* | |
754 | + * List for managing accept()ed sockets. | |
755 | + * Since we don't need to keep an accept()ed socket into this list after | |
756 | + * once the permission was granted, the number of entries in this list is | |
757 | + * likely small. Therefore, we don't use hash tables. | |
758 | + */ | |
759 | +static LIST_HEAD(ccs_accepted_socket_list); | |
760 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
761 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
762 | + | |
763 | +/** | |
764 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
765 | + * | |
766 | + * @rcu: Pointer to "struct rcu_head". | |
767 | + * | |
768 | + * Returns nothing. | |
769 | + */ | |
770 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
771 | +{ | |
772 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
773 | + kfree(ptr); | |
774 | +} | |
775 | + | |
776 | +/** | |
777 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
778 | + * | |
779 | + * @inode: Pointer to "struct inode". | |
780 | + * @status: New status. | |
781 | + * | |
782 | + * Returns nothing. | |
783 | + * | |
784 | + * If @status == 0, memory for that socket will be released after RCU grace | |
785 | + * period. | |
786 | + */ | |
787 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
788 | +{ | |
789 | + struct ccs_socket_tag *ptr; | |
790 | + /* | |
791 | + * Protect whole section because multiple threads may call this | |
792 | + * function with same "sock" via ccs_validate_socket(). | |
793 | + */ | |
794 | + spin_lock(&ccs_accepted_socket_list_lock); | |
795 | + rcu_read_lock(); | |
796 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
797 | + if (ptr->inode != inode) | |
798 | + continue; | |
799 | + ptr->status = status; | |
800 | + if (status) | |
801 | + break; | |
802 | + list_del_rcu(&ptr->list); | |
803 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
804 | + break; | |
805 | + } | |
806 | + rcu_read_unlock(); | |
807 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
808 | +} | |
809 | + | |
810 | +/** | |
811 | + * ccs_validate_socket - Check post accept() permission if needed. | |
812 | + * | |
813 | + * @sock: Pointer to "struct socket". | |
814 | + * | |
815 | + * Returns 0 on success, negative value otherwise. | |
816 | + */ | |
817 | +static int ccs_validate_socket(struct socket *sock) | |
818 | +{ | |
819 | + struct inode *inode = SOCK_INODE(sock); | |
820 | + struct ccs_socket_tag *ptr; | |
821 | + int ret = 0; | |
822 | + rcu_read_lock(); | |
823 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
824 | + if (ptr->inode != inode) | |
825 | + continue; | |
826 | + ret = ptr->status; | |
827 | + break; | |
828 | + } | |
829 | + rcu_read_unlock(); | |
830 | + if (ret <= 0) | |
831 | + /* | |
832 | + * This socket is not an accept()ed socket or this socket is | |
833 | + * an accept()ed socket and post accept() permission is done. | |
834 | + */ | |
835 | + return ret; | |
836 | + /* | |
837 | + * Check post accept() permission now. | |
838 | + * | |
839 | + * Strictly speaking, we need to pass both listen()ing socket and | |
840 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
841 | + * But since socket's family and type are same for both sockets, | |
842 | + * passing the accept()ed socket in place for the listen()ing socket | |
843 | + * will work. | |
844 | + */ | |
845 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
846 | + /* | |
847 | + * If permission was granted, we forget that this is an accept()ed | |
848 | + * socket. Otherwise, we remember that this socket needs to return | |
849 | + * error for subsequent socketcalls. | |
850 | + */ | |
851 | + ccs_update_socket_tag(inode, ret); | |
852 | + return ret; | |
853 | +} | |
854 | + | |
855 | +/** | |
856 | + * ccs_socket_accept - Check permission for accept(). | |
857 | + * | |
858 | + * @sock: Pointer to "struct socket". | |
859 | + * @newsock: Pointer to "struct socket". | |
860 | + * | |
861 | + * Returns 0 on success, negative value otherwise. | |
862 | + * | |
863 | + * This hook is used for setting up environment for doing post accept() | |
864 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
865 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
866 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
867 | + * in order to do post accept() permission check before returning to userspace. | |
868 | + * If we make the copy in security_socket_post_create(), it would be possible | |
869 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
870 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
871 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
872 | + * rather than between sock->ops->accept() and returning to userspace. | |
873 | + * This means that if a socket was close()d before calling some socket | |
874 | + * syscalls, post accept() permission check will not be done. | |
875 | + */ | |
876 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
877 | +{ | |
878 | + struct ccs_socket_tag *ptr; | |
879 | + const int rc = ccs_validate_socket(sock); | |
880 | + if (rc < 0) | |
881 | + return rc; | |
882 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
883 | + if (!ptr) | |
884 | + return -ENOMEM; | |
885 | + /* | |
886 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
887 | + * "newsock" as "an accept()ed socket but post accept() permission | |
888 | + * check is not done yet" by allocating memory using inode of the | |
889 | + * "newsock" as a search key. | |
890 | + */ | |
891 | + ptr->inode = SOCK_INODE(newsock); | |
892 | + ptr->status = 1; /* Check post accept() permission later. */ | |
893 | + spin_lock(&ccs_accepted_socket_list_lock); | |
894 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
895 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
896 | + return 0; | |
897 | +} | |
898 | + | |
899 | +/** | |
900 | + * ccs_socket_listen - Check permission for listen(). | |
901 | + * | |
902 | + * @sock: Pointer to "struct socket". | |
903 | + * @backlog: Backlog parameter. | |
904 | + * | |
905 | + * Returns 0 on success, negative value otherwise. | |
906 | + */ | |
907 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
908 | +{ | |
909 | + const int rc = ccs_validate_socket(sock); | |
910 | + if (rc < 0) | |
911 | + return rc; | |
912 | + return ccs_socket_listen_permission(sock); | |
913 | +} | |
914 | + | |
915 | +/** | |
916 | + * ccs_socket_connect - Check permission for connect(). | |
917 | + * | |
918 | + * @sock: Pointer to "struct socket". | |
919 | + * @addr: Pointer to "struct sockaddr". | |
920 | + * @addr_len: Size of @addr. | |
921 | + * | |
922 | + * Returns 0 on success, negative value otherwise. | |
923 | + */ | |
924 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
925 | + int addr_len) | |
926 | +{ | |
927 | + const int rc = ccs_validate_socket(sock); | |
928 | + if (rc < 0) | |
929 | + return rc; | |
930 | + return ccs_socket_connect_permission(sock, addr, addr_len); | |
931 | +} | |
932 | + | |
933 | +/** | |
934 | + * ccs_socket_bind - Check permission for bind(). | |
935 | + * | |
936 | + * @sock: Pointer to "struct socket". | |
937 | + * @addr: Pointer to "struct sockaddr". | |
938 | + * @addr_len: Size of @addr. | |
939 | + * | |
940 | + * Returns 0 on success, negative value otherwise. | |
941 | + */ | |
942 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
943 | + int addr_len) | |
944 | +{ | |
945 | + const int rc = ccs_validate_socket(sock); | |
946 | + if (rc < 0) | |
947 | + return rc; | |
948 | + return ccs_socket_bind_permission(sock, addr, addr_len); | |
949 | +} | |
950 | + | |
951 | +/** | |
952 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
953 | + * | |
954 | + * @sock: Pointer to "struct socket". | |
955 | + * @msg: Pointer to "struct msghdr". | |
956 | + * @size: Size of message. | |
957 | + * | |
958 | + * Returns 0 on success, negative value otherwise. | |
959 | + */ | |
960 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
961 | + int size) | |
962 | +{ | |
963 | + const int rc = ccs_validate_socket(sock); | |
964 | + if (rc < 0) | |
965 | + return rc; | |
966 | + return ccs_socket_sendmsg_permission(sock, msg, size); | |
967 | +} | |
968 | + | |
969 | +/** | |
970 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
971 | + * | |
972 | + * @sock: Pointer to "struct socket". | |
973 | + * @msg: Pointer to "struct msghdr". | |
974 | + * @size: Size of message. | |
975 | + * @flags: Flags. | |
976 | + * | |
977 | + * Returns 0 on success, negative value otherwise. | |
978 | + */ | |
979 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
980 | + int size, int flags) | |
981 | +{ | |
982 | + return ccs_validate_socket(sock); | |
983 | +} | |
984 | + | |
985 | +/** | |
986 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
987 | + * | |
988 | + * @sock: Pointer to "struct socket". | |
989 | + * | |
990 | + * Returns 0 on success, negative value otherwise. | |
991 | + */ | |
992 | +static int ccs_socket_getsockname(struct socket *sock) | |
993 | +{ | |
994 | + return ccs_validate_socket(sock); | |
995 | +} | |
996 | + | |
997 | +/** | |
998 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
999 | + * | |
1000 | + * @sock: Pointer to "struct socket". | |
1001 | + * | |
1002 | + * Returns 0 on success, negative value otherwise. | |
1003 | + */ | |
1004 | +static int ccs_socket_getpeername(struct socket *sock) | |
1005 | +{ | |
1006 | + return ccs_validate_socket(sock); | |
1007 | +} | |
1008 | + | |
1009 | +/** | |
1010 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
1011 | + * | |
1012 | + * @sock: Pointer to "struct socket". | |
1013 | + * @level: Level. | |
1014 | + * @optname: Option's name, | |
1015 | + * | |
1016 | + * Returns 0 on success, negative value otherwise. | |
1017 | + */ | |
1018 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1019 | +{ | |
1020 | + return ccs_validate_socket(sock); | |
1021 | +} | |
1022 | + | |
1023 | +/** | |
1024 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1025 | + * | |
1026 | + * @sock: Pointer to "struct socket". | |
1027 | + * @level: Level. | |
1028 | + * @optname: Option's name, | |
1029 | + * | |
1030 | + * Returns 0 on success, negative value otherwise. | |
1031 | + */ | |
1032 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1033 | +{ | |
1034 | + return ccs_validate_socket(sock); | |
1035 | +} | |
1036 | + | |
1037 | +/** | |
1038 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1039 | + * | |
1040 | + * @sock: Pointer to "struct socket". | |
1041 | + * @how: Shutdown mode. | |
1042 | + * | |
1043 | + * Returns 0 on success, negative value otherwise. | |
1044 | + */ | |
1045 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1046 | +{ | |
1047 | + return ccs_validate_socket(sock); | |
1048 | +} | |
1049 | + | |
1050 | +#define SOCKFS_MAGIC 0x534F434B | |
1051 | + | |
1052 | +/** | |
1053 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1054 | + * | |
1055 | + * @inode: Pointer to "struct inode". | |
1056 | + * | |
1057 | + * Returns nothing. | |
1058 | + * | |
1059 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1060 | + */ | |
1061 | +static void ccs_inode_free_security(struct inode *inode) | |
1062 | +{ | |
1063 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1064 | + ccs_update_socket_tag(inode, 0); | |
1065 | +} | |
1066 | + | |
1067 | +#endif | |
1068 | + | |
1069 | +/** | |
1070 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1071 | + * | |
1072 | + * @old_path: Pointer to "struct path". | |
1073 | + * @new_path: Pointer to "struct path". | |
1074 | + * | |
1075 | + * Returns 0 on success, negative value otherwise. | |
1076 | + */ | |
1077 | +static int ccs_sb_pivotroot(const struct path *old_path, | |
1078 | + const struct path *new_path) | |
1079 | +{ | |
1080 | + return ccs_pivot_root_permission(old_path, new_path); | |
1081 | +} | |
1082 | + | |
1083 | +/** | |
1084 | + * ccs_sb_mount - Check permission for mount(). | |
1085 | + * | |
1086 | + * @dev_name: Name of device file. | |
1087 | + * @path: Pointer to "struct path". | |
1088 | + * @type: Name of filesystem type. Maybe NULL. | |
1089 | + * @flags: Mount options. | |
1090 | + * @data_page: Optional data. Maybe NULL. | |
1091 | + * | |
1092 | + * Returns 0 on success, negative value otherwise. | |
1093 | + */ | |
1094 | +static int ccs_sb_mount(const char *dev_name, const struct path *path, | |
1095 | + const char *type, unsigned long flags, void *data_page) | |
1096 | +{ | |
1097 | + return ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1098 | +} | |
1099 | + | |
1100 | +/** | |
1101 | + * ccs_sb_umount - Check permission for umount(). | |
1102 | + * | |
1103 | + * @mnt: Pointer to "struct vfsmount". | |
1104 | + * @flags: Unmount flags. | |
1105 | + * | |
1106 | + * Returns 0 on success, negative value otherwise. | |
1107 | + */ | |
1108 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1109 | +{ | |
1110 | + return ccs_umount_permission(mnt, flags); | |
1111 | +} | |
1112 | + | |
1113 | +/** | |
1114 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1115 | + * | |
1116 | + * @file: Pointer to "struct file". | |
1117 | + * @cmd: Command number. | |
1118 | + * @arg: Value for @cmd. | |
1119 | + * | |
1120 | + * Returns 0 on success, negative value otherwise. | |
1121 | + */ | |
1122 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1123 | + unsigned long arg) | |
1124 | +{ | |
1125 | + return ccs_fcntl_permission(file, cmd, arg); | |
1126 | +} | |
1127 | + | |
1128 | +/** | |
1129 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1130 | + * | |
1131 | + * @filp: Pointer to "struct file". | |
1132 | + * @cmd: Command number. | |
1133 | + * @arg: Value for @cmd. | |
1134 | + * | |
1135 | + * Returns 0 on success, negative value otherwise. | |
1136 | + */ | |
1137 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1138 | + unsigned long arg) | |
1139 | +{ | |
1140 | + return ccs_ioctl_permission(filp, cmd, arg); | |
1141 | +} | |
1142 | + | |
1143 | +#define MY_HOOK_INIT(HEAD, HOOK) \ | |
1144 | + { .head = &probe_dummy_security_hook_heads.HEAD, \ | |
1145 | + .hook = { .HEAD = HOOK } } | |
1146 | + | |
1147 | +static struct security_hook_list akari_hooks[] = { | |
1148 | + /* Security context allocator. */ | |
1149 | + MY_HOOK_INIT(cred_free, ccs_cred_free), | |
1150 | + MY_HOOK_INIT(cred_prepare, ccs_cred_prepare), | |
1151 | + MY_HOOK_INIT(cred_alloc_blank, ccs_cred_alloc_blank), | |
1152 | + MY_HOOK_INIT(cred_transfer, ccs_cred_transfer), | |
1153 | + MY_HOOK_INIT(task_create, ccs_task_create), | |
1154 | + /* Security context updater for successful execve(). */ | |
1155 | + MY_HOOK_INIT(bprm_check_security, ccs_bprm_check_security), | |
1156 | + MY_HOOK_INIT(bprm_committing_creds, ccs_bprm_committing_creds), | |
1157 | + /* Various permission checker. */ | |
1158 | + MY_HOOK_INIT(file_open, ccs_file_open), | |
1159 | + MY_HOOK_INIT(file_fcntl, ccs_file_fcntl), | |
1160 | + MY_HOOK_INIT(file_ioctl, ccs_file_ioctl), | |
1161 | + MY_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), | |
1162 | + MY_HOOK_INIT(sb_mount, ccs_sb_mount), | |
1163 | + MY_HOOK_INIT(sb_umount, ccs_sb_umount), | |
1164 | +#ifdef CONFIG_SECURITY_PATH | |
1165 | + MY_HOOK_INIT(path_mknod, ccs_path_mknod), | |
1166 | + MY_HOOK_INIT(path_mkdir, ccs_path_mkdir), | |
1167 | + MY_HOOK_INIT(path_rmdir, ccs_path_rmdir), | |
1168 | + MY_HOOK_INIT(path_unlink, ccs_path_unlink), | |
1169 | + MY_HOOK_INIT(path_symlink, ccs_path_symlink), | |
1170 | + MY_HOOK_INIT(path_rename, ccs_path_rename), | |
1171 | + MY_HOOK_INIT(path_link, ccs_path_link), | |
1172 | + MY_HOOK_INIT(path_truncate, ccs_path_truncate), | |
1173 | + MY_HOOK_INIT(path_chmod, ccs_path_chmod), | |
1174 | + MY_HOOK_INIT(path_chown, ccs_path_chown), | |
1175 | + MY_HOOK_INIT(path_chroot, ccs_path_chroot), | |
1176 | +#else | |
1177 | + MY_HOOK_INIT(inode_mknod, ccs_inode_mknod), | |
1178 | + MY_HOOK_INIT(inode_mkdir, ccs_inode_mkdir), | |
1179 | + MY_HOOK_INIT(inode_rmdir, ccs_inode_rmdir), | |
1180 | + MY_HOOK_INIT(inode_unlink, ccs_inode_unlink), | |
1181 | + MY_HOOK_INIT(inode_symlink, ccs_inode_symlink), | |
1182 | + MY_HOOK_INIT(inode_rename, ccs_inode_rename), | |
1183 | + MY_HOOK_INIT(inode_link, ccs_inode_link), | |
1184 | + MY_HOOK_INIT(inode_create, ccs_inode_create), | |
1185 | + MY_HOOK_INIT(inode_setattr, ccs_inode_setattr), | |
1186 | +#endif | |
1187 | + MY_HOOK_INIT(inode_getattr, ccs_inode_getattr), | |
1188 | +#ifdef CONFIG_SECURITY_NETWORK | |
1189 | + MY_HOOK_INIT(socket_bind, ccs_socket_bind), | |
1190 | + MY_HOOK_INIT(socket_connect, ccs_socket_connect), | |
1191 | + MY_HOOK_INIT(socket_listen, ccs_socket_listen), | |
1192 | + MY_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), | |
1193 | + MY_HOOK_INIT(socket_recvmsg, ccs_socket_recvmsg), | |
1194 | + MY_HOOK_INIT(socket_getsockname, ccs_socket_getsockname), | |
1195 | + MY_HOOK_INIT(socket_getpeername, ccs_socket_getpeername), | |
1196 | + MY_HOOK_INIT(socket_getsockopt, ccs_socket_getsockopt), | |
1197 | + MY_HOOK_INIT(socket_setsockopt, ccs_socket_setsockopt), | |
1198 | + MY_HOOK_INIT(socket_shutdown, ccs_socket_shutdown), | |
1199 | + MY_HOOK_INIT(socket_accept, ccs_socket_accept), | |
1200 | + MY_HOOK_INIT(inode_free_security, ccs_inode_free_security), | |
1201 | +#endif | |
1202 | +}; | |
1203 | + | |
1204 | +static inline void add_hook(struct security_hook_list *hook) | |
1205 | +{ | |
1206 | + list_add_tail_rcu(&hook->list, hook->head); | |
1207 | +} | |
1208 | + | |
1209 | +static void __init swap_hook(struct security_hook_list *hook, | |
1210 | + union security_list_options *original) | |
1211 | +{ | |
1212 | + struct list_head *list = hook->head; | |
1213 | + if (list_empty(list)) { | |
1214 | + add_hook(hook); | |
1215 | + } else { | |
1216 | + struct security_hook_list *shp = | |
1217 | + list_last_entry(list, struct security_hook_list, list); | |
1218 | + *original = shp->hook; | |
1219 | + smp_wmb(); | |
1220 | + shp->hook = hook->hook; | |
1221 | + } | |
1222 | +} | |
1223 | + | |
1224 | +/** | |
1225 | + * ccs_init - Initialize this module. | |
1226 | + * | |
1227 | + * Returns 0 on success, negative value otherwise. | |
1228 | + */ | |
1229 | +static int __init ccs_init(void) | |
1230 | +{ | |
1231 | + int idx; | |
1232 | + struct security_hook_heads *hooks = probe_security_hook_heads(); | |
1233 | + if (!hooks) | |
1234 | + goto out; | |
1235 | + for (idx = 0; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1236 | + akari_hooks[idx].head = ((void *) hooks) | |
1237 | + + ((unsigned long) akari_hooks[idx].head) | |
1238 | + - ((unsigned long) &probe_dummy_security_hook_heads); | |
1239 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1240 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1241 | + goto out; | |
1242 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1243 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1244 | + goto out; | |
1245 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1246 | + if (!ccsecurity_exports.d_absolute_path) | |
1247 | + goto out; | |
1248 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1249 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1250 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1251 | + } | |
1252 | + ccs_main_init(); | |
1253 | + swap_hook(&akari_hooks[0], &original_cred_free); | |
1254 | + swap_hook(&akari_hooks[1], &original_cred_prepare); | |
1255 | + swap_hook(&akari_hooks[2], &original_cred_alloc_blank); | |
1256 | + for (idx = 3; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1257 | + add_hook(&akari_hooks[idx]); | |
1258 | + printk(KERN_INFO "AKARI: 1.0.36 2017/02/20\n"); | |
1259 | + printk(KERN_INFO | |
1260 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1261 | + return 0; | |
1262 | +out: | |
1263 | + return -EINVAL; | |
1264 | +} | |
1265 | + | |
1266 | +module_init(ccs_init); | |
1267 | +MODULE_LICENSE("GPL"); | |
1268 | + | |
1269 | +/** | |
1270 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1271 | + * | |
1272 | + * @domain: Pointer to "struct ccs_domain_info". | |
1273 | + * | |
1274 | + * Returns true if @domain is in use, false otherwise. | |
1275 | + * | |
1276 | + * Caller holds rcu_read_lock(). | |
1277 | + */ | |
1278 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1279 | +{ | |
1280 | + int idx; | |
1281 | + struct ccs_security *ptr; | |
1282 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1283 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1284 | + list_for_each_entry_rcu(ptr, list, list) { | |
1285 | + struct ccs_execve *ee = ptr->ee; | |
1286 | + if (ptr->ccs_domain_info == domain || | |
1287 | + (ee && ee->previous_domain == domain)) { | |
1288 | + return true; | |
1289 | + } | |
1290 | + } | |
1291 | + } | |
1292 | + return false; | |
1293 | +} | |
1294 | + | |
1295 | +/** | |
1296 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1297 | + * | |
1298 | + * @ptr: Pointer to "struct ccs_security". | |
1299 | + * @list: Pointer to "struct list_head". | |
1300 | + * | |
1301 | + * Returns nothing. | |
1302 | + */ | |
1303 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1304 | + struct list_head *list) | |
1305 | +{ | |
1306 | + unsigned long flags; | |
1307 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1308 | + list_add_rcu(&ptr->list, list); | |
1309 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1310 | +} | |
1311 | + | |
1312 | +/** | |
1313 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1314 | + * | |
1315 | + * @task: Pointer to "struct task_struct". | |
1316 | + * | |
1317 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1318 | + * out of memory, &ccs_default_security otherwise. | |
1319 | + * | |
1320 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1321 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1322 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1323 | + * won't work. | |
1324 | + */ | |
1325 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1326 | +{ | |
1327 | + struct ccs_security *ptr; | |
1328 | + struct list_head *list = &ccs_task_security_list | |
1329 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1330 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1331 | + while (!list->next); | |
1332 | + rcu_read_lock(); | |
1333 | + list_for_each_entry_rcu(ptr, list, list) { | |
1334 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
1335 | + continue; | |
1336 | + rcu_read_unlock(); | |
1337 | + /* | |
1338 | + * Current thread needs to transit from old domain to new | |
1339 | + * domain before do_execve() succeeds in order to check | |
1340 | + * permission for interpreters and environment variables using | |
1341 | + * new domain's ACL rules. The domain transition has to be | |
1342 | + * visible from other CPU in order to allow interactive | |
1343 | + * enforcing mode. Also, the domain transition has to be | |
1344 | + * reverted if do_execve() failed. However, an LSM hook for | |
1345 | + * reverting domain transition is missing. | |
1346 | + * | |
1347 | + * security_prepare_creds() is called from prepare_creds() from | |
1348 | + * prepare_bprm_creds() from do_execve() before setting | |
1349 | + * current->in_execve flag, and current->in_execve flag is | |
1350 | + * cleared by the time next do_execve() request starts. | |
1351 | + * This means that we can emulate the missing LSM hook for | |
1352 | + * reverting domain transition, by calling this function from | |
1353 | + * security_prepare_creds(). | |
1354 | + * | |
1355 | + * If current->in_execve is not set but ptr->ccs_flags has | |
1356 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
1357 | + * has failed and reverting domain transition is needed. | |
1358 | + */ | |
1359 | + if (task == current && | |
1360 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
1361 | + !current->in_execve) { | |
1362 | + ccs_debug_trace("4"); | |
1363 | + ccs_clear_execve(-1, ptr); | |
1364 | + } | |
1365 | + return ptr; | |
1366 | + } | |
1367 | + rcu_read_unlock(); | |
1368 | + if (task != current) { | |
1369 | + /* | |
1370 | + * If a thread does nothing after fork(), caller will reach | |
1371 | + * here because "struct ccs_security" for that thread is not | |
1372 | + * yet allocated. But that thread is keeping a snapshot of | |
1373 | + * "struct ccs_security" taken as of ccs_task_create() | |
1374 | + * associated with that thread's "struct cred". | |
1375 | + * | |
1376 | + * Since that snapshot will be used as initial data when that | |
1377 | + * thread allocates "struct ccs_security" for that thread, we | |
1378 | + * can return that snapshot rather than &ccs_default_security. | |
1379 | + * | |
1380 | + * Since this function is called by only ccs_select_one() and | |
1381 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
1382 | + * it is guaranteed that caller has called rcu_read_lock() | |
1383 | + * (via ccs_tasklist_lock()) before finding this thread and | |
1384 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
1385 | + * like get_robust_list() does. | |
1386 | + */ | |
1387 | + return ccs_find_cred_security(__task_cred(task)); | |
1388 | + } | |
1389 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1390 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1391 | + if (!ptr) { | |
1392 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1393 | + task->pid); | |
1394 | + send_sig(SIGKILL, current, 0); | |
1395 | + return &ccs_oom_security; | |
1396 | + } | |
1397 | + *ptr = *ccs_find_cred_security(task->cred); | |
1398 | + /* We can shortcut because task == current. */ | |
1399 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
1400 | + pids[PIDTYPE_PID].pid); | |
1401 | + ptr->cred = NULL; | |
1402 | + ccs_add_task_security(ptr, list); | |
1403 | + return ptr; | |
1404 | +} | |
1405 | + | |
1406 | +/** | |
1407 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
1408 | + * | |
1409 | + * @new: Pointer to "struct cred". | |
1410 | + * @old: Pointer to "struct cred". | |
1411 | + * @gfp: Memory allocation flags. | |
1412 | + * | |
1413 | + * Returns 0 on success, negative value otherwise. | |
1414 | + */ | |
1415 | +static int ccs_copy_cred_security(const struct cred *new, | |
1416 | + const struct cred *old, gfp_t gfp) | |
1417 | +{ | |
1418 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
1419 | + struct ccs_security *new_security = | |
1420 | + kzalloc(sizeof(*new_security), gfp); | |
1421 | + if (!new_security) | |
1422 | + return -ENOMEM; | |
1423 | + *new_security = *old_security; | |
1424 | + new_security->cred = new; | |
1425 | + ccs_add_cred_security(new_security); | |
1426 | + return 0; | |
1427 | +} | |
1428 | + | |
1429 | +/** | |
1430 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
1431 | + * | |
1432 | + * @cred: Pointer to "struct cred". | |
1433 | + * | |
1434 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
1435 | + * otherwise. | |
1436 | + */ | |
1437 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
1438 | +{ | |
1439 | + struct ccs_security *ptr; | |
1440 | + struct list_head *list = &ccs_cred_security_list | |
1441 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
1442 | + rcu_read_lock(); | |
1443 | + list_for_each_entry_rcu(ptr, list, list) { | |
1444 | + if (ptr->cred != cred) | |
1445 | + continue; | |
1446 | + rcu_read_unlock(); | |
1447 | + return ptr; | |
1448 | + } | |
1449 | + rcu_read_unlock(); | |
1450 | + return &ccs_default_security; | |
1451 | +} | |
1452 | + | |
1453 | +/** | |
1454 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
1455 | + * | |
1456 | + * Returns nothing. | |
1457 | + * | |
1458 | + * Since security_task_free() is missing, I can't release memory associated | |
1459 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
1460 | + * "struct pid" and runs garbage collection when associated | |
1461 | + * "struct task_struct" has gone. | |
1462 | + */ | |
1463 | +static void ccs_task_security_gc(void) | |
1464 | +{ | |
1465 | + static DEFINE_SPINLOCK(lock); | |
1466 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
1467 | + unsigned int idx; | |
1468 | + /* | |
1469 | + * If some process is doing execve(), try to garbage collection now. | |
1470 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
1471 | + * as soon as execve() has completed in order to compensate for lack of | |
1472 | + * security_bprm_free() and security_task_free() hooks. | |
1473 | + * | |
1474 | + * Otherwise, reduce frequency for performance reason. | |
1475 | + */ | |
1476 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
1477 | + atomic_inc_return(&gc_counter) < 1024) | |
1478 | + return; | |
1479 | + if (!spin_trylock(&lock)) | |
1480 | + return; | |
1481 | + atomic_set(&gc_counter, 0); | |
1482 | + rcu_read_lock(); | |
1483 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1484 | + struct ccs_security *ptr; | |
1485 | + struct list_head *list = &ccs_task_security_list[idx]; | |
1486 | + list_for_each_entry_rcu(ptr, list, list) { | |
1487 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
1488 | + continue; | |
1489 | + ccs_del_security(ptr); | |
1490 | + } | |
1491 | + } | |
1492 | + rcu_read_unlock(); | |
1493 | + spin_unlock(&lock); | |
1494 | +} |
@@ -0,0 +1,32 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.36 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include <linux/version.h> | |
10 | +#include <linux/security.h> | |
11 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) | |
12 | +#include "lsm-4.7.c" | |
13 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) | |
14 | +#include "lsm-4.2.c" | |
15 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) | |
16 | +#include "lsm-2.6.29.c" | |
17 | +/* | |
18 | + * AppArmor patch added "struct vfsmount *" to security_inode_\*() hooks. | |
19 | + * Detect it by checking whether D_PATH_DISCONNECT is defined or not. | |
20 | + * Also, there may be other kernels with "struct vfsmount *" added. | |
21 | + * If you got build failure, check security_inode_\*() hooks in | |
22 | + * include/linux/security.h. | |
23 | + */ | |
24 | +#elif defined(D_PATH_DISCONNECT) | |
25 | +#include "lsm-2.6.0-vfs.c" | |
26 | +#elif defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 25) | |
27 | +#include "lsm-2.6.0-vfs.c" | |
28 | +#elif defined(CONFIG_SECURITY_APPARMOR) && LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 24) | |
29 | +#include "lsm-2.6.0-vfs.c" | |
30 | +#else | |
31 | +#include "lsm-2.6.0.c" | |
32 | +#endif |
@@ -0,0 +1,6499 @@ | ||
1 | +/* | |
2 | + * security/ccsecurity/policy_io.c | |
3 | + * | |
4 | + * Copyright (C) 2005-2012 NTT DATA CORPORATION | |
5 | + * | |
6 | + * Version: 1.8.5+ 2017/02/20 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | + | |
11 | +/***** SECTION1: Constants definition *****/ | |
12 | + | |
13 | +/* Define this to enable debug mode. */ | |
14 | +/* #define DEBUG_CONDITION */ | |
15 | + | |
16 | +#ifdef DEBUG_CONDITION | |
17 | +#define dprintk printk | |
18 | +#else | |
19 | +#define dprintk(...) do { } while (0) | |
20 | +#endif | |
21 | + | |
22 | +/* Mapping table from "enum ccs_mac_index" to "enum ccs_mac_category_index". */ | |
23 | +static const u8 ccs_index2category[CCS_MAX_MAC_INDEX] = { | |
24 | + /* CONFIG::file group */ | |
25 | + [CCS_MAC_FILE_EXECUTE] = CCS_MAC_CATEGORY_FILE, | |
26 | + [CCS_MAC_FILE_OPEN] = CCS_MAC_CATEGORY_FILE, | |
27 | + [CCS_MAC_FILE_CREATE] = CCS_MAC_CATEGORY_FILE, | |
28 | + [CCS_MAC_FILE_UNLINK] = CCS_MAC_CATEGORY_FILE, | |
29 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
30 | + [CCS_MAC_FILE_GETATTR] = CCS_MAC_CATEGORY_FILE, | |
31 | +#endif | |
32 | + [CCS_MAC_FILE_MKDIR] = CCS_MAC_CATEGORY_FILE, | |
33 | + [CCS_MAC_FILE_RMDIR] = CCS_MAC_CATEGORY_FILE, | |
34 | + [CCS_MAC_FILE_MKFIFO] = CCS_MAC_CATEGORY_FILE, | |
35 | + [CCS_MAC_FILE_MKSOCK] = CCS_MAC_CATEGORY_FILE, | |
36 | + [CCS_MAC_FILE_TRUNCATE] = CCS_MAC_CATEGORY_FILE, | |
37 | + [CCS_MAC_FILE_SYMLINK] = CCS_MAC_CATEGORY_FILE, | |
38 | + [CCS_MAC_FILE_MKBLOCK] = CCS_MAC_CATEGORY_FILE, | |
39 | + [CCS_MAC_FILE_MKCHAR] = CCS_MAC_CATEGORY_FILE, | |
40 | + [CCS_MAC_FILE_LINK] = CCS_MAC_CATEGORY_FILE, | |
41 | + [CCS_MAC_FILE_RENAME] = CCS_MAC_CATEGORY_FILE, | |
42 | + [CCS_MAC_FILE_CHMOD] = CCS_MAC_CATEGORY_FILE, | |
43 | + [CCS_MAC_FILE_CHOWN] = CCS_MAC_CATEGORY_FILE, | |
44 | + [CCS_MAC_FILE_CHGRP] = CCS_MAC_CATEGORY_FILE, | |
45 | + [CCS_MAC_FILE_IOCTL] = CCS_MAC_CATEGORY_FILE, | |
46 | + [CCS_MAC_FILE_CHROOT] = CCS_MAC_CATEGORY_FILE, | |
47 | + [CCS_MAC_FILE_MOUNT] = CCS_MAC_CATEGORY_FILE, | |
48 | + [CCS_MAC_FILE_UMOUNT] = CCS_MAC_CATEGORY_FILE, | |
49 | + [CCS_MAC_FILE_PIVOT_ROOT] = CCS_MAC_CATEGORY_FILE, | |
50 | +#ifdef CONFIG_CCSECURITY_MISC | |
51 | + /* CONFIG::misc group */ | |
52 | + [CCS_MAC_ENVIRON] = CCS_MAC_CATEGORY_MISC, | |
53 | +#endif | |
54 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
55 | + /* CONFIG::network group */ | |
56 | + [CCS_MAC_NETWORK_INET_STREAM_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
57 | + [CCS_MAC_NETWORK_INET_STREAM_LISTEN] = CCS_MAC_CATEGORY_NETWORK, | |
58 | + [CCS_MAC_NETWORK_INET_STREAM_CONNECT] = CCS_MAC_CATEGORY_NETWORK, | |
59 | + [CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, | |
60 | + [CCS_MAC_NETWORK_INET_DGRAM_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
61 | + [CCS_MAC_NETWORK_INET_DGRAM_SEND] = CCS_MAC_CATEGORY_NETWORK, | |
62 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
63 | + [CCS_MAC_NETWORK_INET_DGRAM_RECV] = CCS_MAC_CATEGORY_NETWORK, | |
64 | +#endif | |
65 | + [CCS_MAC_NETWORK_INET_RAW_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
66 | + [CCS_MAC_NETWORK_INET_RAW_SEND] = CCS_MAC_CATEGORY_NETWORK, | |
67 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
68 | + [CCS_MAC_NETWORK_INET_RAW_RECV] = CCS_MAC_CATEGORY_NETWORK, | |
69 | +#endif | |
70 | + [CCS_MAC_NETWORK_UNIX_STREAM_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
71 | + [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = CCS_MAC_CATEGORY_NETWORK, | |
72 | + [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = CCS_MAC_CATEGORY_NETWORK, | |
73 | + [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, | |
74 | + [CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
75 | + [CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = CCS_MAC_CATEGORY_NETWORK, | |
76 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
77 | + [CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = CCS_MAC_CATEGORY_NETWORK, | |
78 | +#endif | |
79 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = CCS_MAC_CATEGORY_NETWORK, | |
80 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = CCS_MAC_CATEGORY_NETWORK, | |
81 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = CCS_MAC_CATEGORY_NETWORK, | |
82 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = CCS_MAC_CATEGORY_NETWORK, | |
83 | +#endif | |
84 | +#ifdef CONFIG_CCSECURITY_IPC | |
85 | + /* CONFIG::ipc group */ | |
86 | + [CCS_MAC_SIGNAL] = CCS_MAC_CATEGORY_IPC, | |
87 | +#endif | |
88 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
89 | + /* CONFIG::capability group */ | |
90 | + [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = CCS_MAC_CATEGORY_CAPABILITY, | |
91 | + [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = CCS_MAC_CATEGORY_CAPABILITY, | |
92 | + [CCS_MAC_CAPABILITY_SYS_REBOOT] = CCS_MAC_CATEGORY_CAPABILITY, | |
93 | + [CCS_MAC_CAPABILITY_SYS_VHANGUP] = CCS_MAC_CATEGORY_CAPABILITY, | |
94 | + [CCS_MAC_CAPABILITY_SYS_SETTIME] = CCS_MAC_CATEGORY_CAPABILITY, | |
95 | + [CCS_MAC_CAPABILITY_SYS_NICE] = CCS_MAC_CATEGORY_CAPABILITY, | |
96 | + [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME] = CCS_MAC_CATEGORY_CAPABILITY, | |
97 | + [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = CCS_MAC_CATEGORY_CAPABILITY, | |
98 | + [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = CCS_MAC_CATEGORY_CAPABILITY, | |
99 | + [CCS_MAC_CAPABILITY_SYS_PTRACE] = CCS_MAC_CATEGORY_CAPABILITY, | |
100 | +#endif | |
101 | +}; | |
102 | + | |
103 | +/* String table for operation mode. */ | |
104 | +static const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = { | |
105 | + [CCS_CONFIG_DISABLED] = "disabled", | |
106 | + [CCS_CONFIG_LEARNING] = "learning", | |
107 | + [CCS_CONFIG_PERMISSIVE] = "permissive", | |
108 | + [CCS_CONFIG_ENFORCING] = "enforcing" | |
109 | +}; | |
110 | + | |
111 | +/* String table for /proc/ccs/profile interface. */ | |
112 | +static const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX | |
113 | + + CCS_MAX_MAC_CATEGORY_INDEX] = { | |
114 | + /* CONFIG::file group */ | |
115 | + [CCS_MAC_FILE_EXECUTE] = "execute", | |
116 | + [CCS_MAC_FILE_OPEN] = "open", | |
117 | + [CCS_MAC_FILE_CREATE] = "create", | |
118 | + [CCS_MAC_FILE_UNLINK] = "unlink", | |
119 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
120 | + [CCS_MAC_FILE_GETATTR] = "getattr", | |
121 | +#endif | |
122 | + [CCS_MAC_FILE_MKDIR] = "mkdir", | |
123 | + [CCS_MAC_FILE_RMDIR] = "rmdir", | |
124 | + [CCS_MAC_FILE_MKFIFO] = "mkfifo", | |
125 | + [CCS_MAC_FILE_MKSOCK] = "mksock", | |
126 | + [CCS_MAC_FILE_TRUNCATE] = "truncate", | |
127 | + [CCS_MAC_FILE_SYMLINK] = "symlink", | |
128 | + [CCS_MAC_FILE_MKBLOCK] = "mkblock", | |
129 | + [CCS_MAC_FILE_MKCHAR] = "mkchar", | |
130 | + [CCS_MAC_FILE_LINK] = "link", | |
131 | + [CCS_MAC_FILE_RENAME] = "rename", | |
132 | + [CCS_MAC_FILE_CHMOD] = "chmod", | |
133 | + [CCS_MAC_FILE_CHOWN] = "chown", | |
134 | + [CCS_MAC_FILE_CHGRP] = "chgrp", | |
135 | + [CCS_MAC_FILE_IOCTL] = "ioctl", | |
136 | + [CCS_MAC_FILE_CHROOT] = "chroot", | |
137 | + [CCS_MAC_FILE_MOUNT] = "mount", | |
138 | + [CCS_MAC_FILE_UMOUNT] = "unmount", | |
139 | + [CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root", | |
140 | +#ifdef CONFIG_CCSECURITY_MISC | |
141 | + /* CONFIG::misc group */ | |
142 | + [CCS_MAC_ENVIRON] = "env", | |
143 | +#endif | |
144 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
145 | + /* CONFIG::network group */ | |
146 | + [CCS_MAC_NETWORK_INET_STREAM_BIND] = "inet_stream_bind", | |
147 | + [CCS_MAC_NETWORK_INET_STREAM_LISTEN] = "inet_stream_listen", | |
148 | + [CCS_MAC_NETWORK_INET_STREAM_CONNECT] = "inet_stream_connect", | |
149 | + [CCS_MAC_NETWORK_INET_STREAM_ACCEPT] = "inet_stream_accept", | |
150 | + [CCS_MAC_NETWORK_INET_DGRAM_BIND] = "inet_dgram_bind", | |
151 | + [CCS_MAC_NETWORK_INET_DGRAM_SEND] = "inet_dgram_send", | |
152 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
153 | + [CCS_MAC_NETWORK_INET_DGRAM_RECV] = "inet_dgram_recv", | |
154 | +#endif | |
155 | + [CCS_MAC_NETWORK_INET_RAW_BIND] = "inet_raw_bind", | |
156 | + [CCS_MAC_NETWORK_INET_RAW_SEND] = "inet_raw_send", | |
157 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
158 | + [CCS_MAC_NETWORK_INET_RAW_RECV] = "inet_raw_recv", | |
159 | +#endif | |
160 | + [CCS_MAC_NETWORK_UNIX_STREAM_BIND] = "unix_stream_bind", | |
161 | + [CCS_MAC_NETWORK_UNIX_STREAM_LISTEN] = "unix_stream_listen", | |
162 | + [CCS_MAC_NETWORK_UNIX_STREAM_CONNECT] = "unix_stream_connect", | |
163 | + [CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT] = "unix_stream_accept", | |
164 | + [CCS_MAC_NETWORK_UNIX_DGRAM_BIND] = "unix_dgram_bind", | |
165 | + [CCS_MAC_NETWORK_UNIX_DGRAM_SEND] = "unix_dgram_send", | |
166 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
167 | + [CCS_MAC_NETWORK_UNIX_DGRAM_RECV] = "unix_dgram_recv", | |
168 | +#endif | |
169 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND] = "unix_seqpacket_bind", | |
170 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN] = "unix_seqpacket_listen", | |
171 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect", | |
172 | + [CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT] = "unix_seqpacket_accept", | |
173 | +#endif | |
174 | +#ifdef CONFIG_CCSECURITY_IPC | |
175 | + /* CONFIG::ipc group */ | |
176 | + [CCS_MAC_SIGNAL] = "signal", | |
177 | +#endif | |
178 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
179 | + /* CONFIG::capability group */ | |
180 | + [CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET] = "use_route", | |
181 | + [CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet", | |
182 | + [CCS_MAC_CAPABILITY_SYS_REBOOT] = "SYS_REBOOT", | |
183 | + [CCS_MAC_CAPABILITY_SYS_VHANGUP] = "SYS_VHANGUP", | |
184 | + [CCS_MAC_CAPABILITY_SYS_SETTIME] = "SYS_TIME", | |
185 | + [CCS_MAC_CAPABILITY_SYS_NICE] = "SYS_NICE", | |
186 | + [CCS_MAC_CAPABILITY_SYS_SETHOSTNAME] = "SYS_SETHOSTNAME", | |
187 | + [CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module", | |
188 | + [CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD] = "SYS_KEXEC_LOAD", | |
189 | + [CCS_MAC_CAPABILITY_SYS_PTRACE] = "SYS_PTRACE", | |
190 | +#endif | |
191 | + /* CONFIG group */ | |
192 | + [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE] = "file", | |
193 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
194 | + [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK] = "network", | |
195 | +#endif | |
196 | +#ifdef CONFIG_CCSECURITY_MISC | |
197 | + [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC] = "misc", | |
198 | +#endif | |
199 | +#ifdef CONFIG_CCSECURITY_IPC | |
200 | + [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC] = "ipc", | |
201 | +#endif | |
202 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
203 | + [CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability", | |
204 | +#endif | |
205 | +}; | |
206 | + | |
207 | +/* String table for path operation. */ | |
208 | +static const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = { | |
209 | + [CCS_TYPE_EXECUTE] = "execute", | |
210 | + [CCS_TYPE_READ] = "read", | |
211 | + [CCS_TYPE_WRITE] = "write", | |
212 | + [CCS_TYPE_APPEND] = "append", | |
213 | + [CCS_TYPE_UNLINK] = "unlink", | |
214 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
215 | + [CCS_TYPE_GETATTR] = "getattr", | |
216 | +#endif | |
217 | + [CCS_TYPE_RMDIR] = "rmdir", | |
218 | + [CCS_TYPE_TRUNCATE] = "truncate", | |
219 | + [CCS_TYPE_SYMLINK] = "symlink", | |
220 | + [CCS_TYPE_CHROOT] = "chroot", | |
221 | + [CCS_TYPE_UMOUNT] = "unmount", | |
222 | +}; | |
223 | + | |
224 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
225 | + | |
226 | +/* String table for socket's operation. */ | |
227 | +static const char * const ccs_socket_keyword[CCS_MAX_NETWORK_OPERATION] = { | |
228 | + [CCS_NETWORK_BIND] = "bind", | |
229 | + [CCS_NETWORK_LISTEN] = "listen", | |
230 | + [CCS_NETWORK_CONNECT] = "connect", | |
231 | + [CCS_NETWORK_ACCEPT] = "accept", | |
232 | + [CCS_NETWORK_SEND] = "send", | |
233 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
234 | + [CCS_NETWORK_RECV] = "recv", | |
235 | +#endif | |
236 | +}; | |
237 | + | |
238 | +/* String table for socket's protocols. */ | |
239 | +static const char * const ccs_proto_keyword[CCS_SOCK_MAX] = { | |
240 | + [SOCK_STREAM] = "stream", | |
241 | + [SOCK_DGRAM] = "dgram", | |
242 | + [SOCK_RAW] = "raw", | |
243 | + [SOCK_SEQPACKET] = "seqpacket", | |
244 | + [0] = " ", /* Dummy for avoiding NULL pointer dereference. */ | |
245 | + [4] = " ", /* Dummy for avoiding NULL pointer dereference. */ | |
246 | +}; | |
247 | + | |
248 | +#endif | |
249 | + | |
250 | +/* String table for categories. */ | |
251 | +static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = { | |
252 | + [CCS_MAC_CATEGORY_FILE] = "file", | |
253 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
254 | + [CCS_MAC_CATEGORY_NETWORK] = "network", | |
255 | +#endif | |
256 | +#ifdef CONFIG_CCSECURITY_MISC | |
257 | + [CCS_MAC_CATEGORY_MISC] = "misc", | |
258 | +#endif | |
259 | +#ifdef CONFIG_CCSECURITY_IPC | |
260 | + [CCS_MAC_CATEGORY_IPC] = "ipc", | |
261 | +#endif | |
262 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
263 | + [CCS_MAC_CATEGORY_CAPABILITY] = "capability", | |
264 | +#endif | |
265 | +}; | |
266 | + | |
267 | +/* String table for conditions. */ | |
268 | +static const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = { | |
269 | + [CCS_TASK_UID] = "task.uid", | |
270 | + [CCS_TASK_EUID] = "task.euid", | |
271 | + [CCS_TASK_SUID] = "task.suid", | |
272 | + [CCS_TASK_FSUID] = "task.fsuid", | |
273 | + [CCS_TASK_GID] = "task.gid", | |
274 | + [CCS_TASK_EGID] = "task.egid", | |
275 | + [CCS_TASK_SGID] = "task.sgid", | |
276 | + [CCS_TASK_FSGID] = "task.fsgid", | |
277 | + [CCS_TASK_PID] = "task.pid", | |
278 | + [CCS_TASK_PPID] = "task.ppid", | |
279 | + [CCS_EXEC_ARGC] = "exec.argc", | |
280 | + [CCS_EXEC_ENVC] = "exec.envc", | |
281 | + [CCS_TYPE_IS_SOCKET] = "socket", | |
282 | + [CCS_TYPE_IS_SYMLINK] = "symlink", | |
283 | + [CCS_TYPE_IS_FILE] = "file", | |
284 | + [CCS_TYPE_IS_BLOCK_DEV] = "block", | |
285 | + [CCS_TYPE_IS_DIRECTORY] = "directory", | |
286 | + [CCS_TYPE_IS_CHAR_DEV] = "char", | |
287 | + [CCS_TYPE_IS_FIFO] = "fifo", | |
288 | + [CCS_MODE_SETUID] = "setuid", | |
289 | + [CCS_MODE_SETGID] = "setgid", | |
290 | + [CCS_MODE_STICKY] = "sticky", | |
291 | + [CCS_MODE_OWNER_READ] = "owner_read", | |
292 | + [CCS_MODE_OWNER_WRITE] = "owner_write", | |
293 | + [CCS_MODE_OWNER_EXECUTE] = "owner_execute", | |
294 | + [CCS_MODE_GROUP_READ] = "group_read", | |
295 | + [CCS_MODE_GROUP_WRITE] = "group_write", | |
296 | + [CCS_MODE_GROUP_EXECUTE] = "group_execute", | |
297 | + [CCS_MODE_OTHERS_READ] = "others_read", | |
298 | + [CCS_MODE_OTHERS_WRITE] = "others_write", | |
299 | + [CCS_MODE_OTHERS_EXECUTE] = "others_execute", | |
300 | + [CCS_TASK_TYPE] = "task.type", | |
301 | + [CCS_TASK_EXECUTE_HANDLER] = "execute_handler", | |
302 | + [CCS_EXEC_REALPATH] = "exec.realpath", | |
303 | + [CCS_SYMLINK_TARGET] = "symlink.target", | |
304 | + [CCS_PATH1_UID] = "path1.uid", | |
305 | + [CCS_PATH1_GID] = "path1.gid", | |
306 | + [CCS_PATH1_INO] = "path1.ino", | |
307 | + [CCS_PATH1_MAJOR] = "path1.major", | |
308 | + [CCS_PATH1_MINOR] = "path1.minor", | |
309 | + [CCS_PATH1_PERM] = "path1.perm", | |
310 | + [CCS_PATH1_TYPE] = "path1.type", | |
311 | + [CCS_PATH1_DEV_MAJOR] = "path1.dev_major", | |
312 | + [CCS_PATH1_DEV_MINOR] = "path1.dev_minor", | |
313 | + [CCS_PATH2_UID] = "path2.uid", | |
314 | + [CCS_PATH2_GID] = "path2.gid", | |
315 | + [CCS_PATH2_INO] = "path2.ino", | |
316 | + [CCS_PATH2_MAJOR] = "path2.major", | |
317 | + [CCS_PATH2_MINOR] = "path2.minor", | |
318 | + [CCS_PATH2_PERM] = "path2.perm", | |
319 | + [CCS_PATH2_TYPE] = "path2.type", | |
320 | + [CCS_PATH2_DEV_MAJOR] = "path2.dev_major", | |
321 | + [CCS_PATH2_DEV_MINOR] = "path2.dev_minor", | |
322 | + [CCS_PATH1_PARENT_UID] = "path1.parent.uid", | |
323 | + [CCS_PATH1_PARENT_GID] = "path1.parent.gid", | |
324 | + [CCS_PATH1_PARENT_INO] = "path1.parent.ino", | |
325 | + [CCS_PATH1_PARENT_PERM] = "path1.parent.perm", | |
326 | + [CCS_PATH2_PARENT_UID] = "path2.parent.uid", | |
327 | + [CCS_PATH2_PARENT_GID] = "path2.parent.gid", | |
328 | + [CCS_PATH2_PARENT_INO] = "path2.parent.ino", | |
329 | + [CCS_PATH2_PARENT_PERM] = "path2.parent.perm", | |
330 | +}; | |
331 | + | |
332 | +/* String table for PREFERENCE keyword. */ | |
333 | +static const char * const ccs_pref_keywords[CCS_MAX_PREF] = { | |
334 | + [CCS_PREF_MAX_AUDIT_LOG] = "max_audit_log", | |
335 | + [CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry", | |
336 | + [CCS_PREF_ENFORCING_PENALTY] = "enforcing_penalty", | |
337 | +}; | |
338 | + | |
339 | +/* String table for domain flags. */ | |
340 | +const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = { | |
341 | + [CCS_DIF_QUOTA_WARNED] = "quota_exceeded\n", | |
342 | + [CCS_DIF_TRANSITION_FAILED] = "transition_failed\n", | |
343 | +}; | |
344 | + | |
345 | +/* String table for domain transition control keywords. */ | |
346 | +static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = { | |
347 | + [CCS_TRANSITION_CONTROL_NO_RESET] = "no_reset_domain ", | |
348 | + [CCS_TRANSITION_CONTROL_RESET] = "reset_domain ", | |
349 | + [CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ", | |
350 | + [CCS_TRANSITION_CONTROL_INITIALIZE] = "initialize_domain ", | |
351 | + [CCS_TRANSITION_CONTROL_NO_KEEP] = "no_keep_domain ", | |
352 | + [CCS_TRANSITION_CONTROL_KEEP] = "keep_domain ", | |
353 | +}; | |
354 | + | |
355 | +/* String table for grouping keywords. */ | |
356 | +static const char * const ccs_group_name[CCS_MAX_GROUP] = { | |
357 | + [CCS_PATH_GROUP] = "path_group ", | |
358 | + [CCS_NUMBER_GROUP] = "number_group ", | |
359 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
360 | + [CCS_ADDRESS_GROUP] = "address_group ", | |
361 | +#endif | |
362 | +}; | |
363 | + | |
364 | +/* String table for /proc/ccs/stat interface. */ | |
365 | +static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = { | |
366 | + [CCS_STAT_POLICY_UPDATES] = "update:", | |
367 | + [CCS_STAT_POLICY_LEARNING] = "violation in learning mode:", | |
368 | + [CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:", | |
369 | + [CCS_STAT_POLICY_ENFORCING] = "violation in enforcing mode:", | |
370 | +}; | |
371 | + | |
372 | +/* String table for /proc/ccs/stat interface. */ | |
373 | +static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = { | |
374 | + [CCS_MEMORY_POLICY] = "policy:", | |
375 | + [CCS_MEMORY_AUDIT] = "audit log:", | |
376 | + [CCS_MEMORY_QUERY] = "query message:", | |
377 | +}; | |
378 | + | |
379 | +/***** SECTION2: Structure definition *****/ | |
380 | + | |
381 | +struct iattr; | |
382 | + | |
383 | +/* Structure for query. */ | |
384 | +struct ccs_query { | |
385 | + struct list_head list; | |
386 | + struct ccs_domain_info *domain; | |
387 | + char *query; | |
388 | + size_t query_len; | |
389 | + unsigned int serial; | |
390 | + u8 timer; | |
391 | + u8 answer; | |
392 | + u8 retry; | |
393 | +}; | |
394 | + | |
395 | +/* Structure for audit log. */ | |
396 | +struct ccs_log { | |
397 | + struct list_head list; | |
398 | + char *log; | |
399 | + int size; | |
400 | +}; | |
401 | + | |
402 | +/***** SECTION3: Prototype definition section *****/ | |
403 | + | |
404 | +int ccs_audit_log(struct ccs_request_info *r); | |
405 | +struct ccs_domain_info *ccs_assign_domain(const char *domainname, | |
406 | + const bool transit); | |
407 | +u8 ccs_get_config(const u8 profile, const u8 index); | |
408 | +void ccs_transition_failed(const char *domainname); | |
409 | +void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...); | |
410 | + | |
411 | +static bool ccs_correct_domain(const unsigned char *domainname); | |
412 | +static bool ccs_correct_path(const char *filename); | |
413 | +static bool ccs_correct_word(const char *string); | |
414 | +static bool ccs_correct_word2(const char *string, size_t len); | |
415 | +static bool ccs_domain_def(const unsigned char *buffer); | |
416 | +static bool ccs_domain_quota_ok(struct ccs_request_info *r); | |
417 | +static bool ccs_flush(struct ccs_io_buffer *head); | |
418 | +static bool ccs_get_audit(const struct ccs_request_info *r); | |
419 | +static bool ccs_has_more_namespace(struct ccs_io_buffer *head); | |
420 | +static bool ccs_manager(void); | |
421 | +static bool ccs_namespace_jump(const char *domainname); | |
422 | +static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv); | |
423 | +static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp); | |
424 | +static bool ccs_parse_name_union(struct ccs_acl_param *param, | |
425 | + struct ccs_name_union *ptr); | |
426 | +static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param, | |
427 | + struct ccs_name_union *ptr); | |
428 | +static bool ccs_parse_number_union(struct ccs_acl_param *param, | |
429 | + struct ccs_number_union *ptr); | |
430 | +static bool ccs_permstr(const char *string, const char *keyword); | |
431 | +static bool ccs_print_condition(struct ccs_io_buffer *head, | |
432 | + const struct ccs_condition *cond); | |
433 | +static bool ccs_print_entry(struct ccs_io_buffer *head, | |
434 | + const struct ccs_acl_info *acl); | |
435 | +static bool ccs_print_group(struct ccs_io_buffer *head, | |
436 | + const struct ccs_group *group); | |
437 | +static bool ccs_read_acl(struct ccs_io_buffer *head, struct list_head *list); | |
438 | +static bool ccs_read_group(struct ccs_io_buffer *head, const int idx); | |
439 | +static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx); | |
440 | +static bool ccs_same_condition(const struct ccs_condition *a, | |
441 | + const struct ccs_condition *b); | |
442 | +static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data); | |
443 | +static bool ccs_set_lf(struct ccs_io_buffer *head); | |
444 | +static bool ccs_str_starts(char **src, const char *find); | |
445 | +static char *ccs_get_transit_preference(struct ccs_acl_param *param, | |
446 | + struct ccs_condition *e); | |
447 | +static char *ccs_init_log(struct ccs_request_info *r, int len, const char *fmt, | |
448 | + va_list args); | |
449 | +static char *ccs_print_bprm(struct linux_binprm *bprm, | |
450 | + struct ccs_page_dump *dump); | |
451 | +static char *ccs_print_header(struct ccs_request_info *r); | |
452 | +static char *ccs_read_token(struct ccs_acl_param *param); | |
453 | +static const char *ccs_yesno(const unsigned int value); | |
454 | +static const struct ccs_path_info *ccs_get_domainname | |
455 | +(struct ccs_acl_param *param); | |
456 | +static const struct ccs_path_info *ccs_get_dqword(char *start); | |
457 | +static int __init ccs_init_module(void); | |
458 | +static int ccs_delete_domain(char *domainname); | |
459 | +static int ccs_open(struct inode *inode, struct file *file); | |
460 | +static int ccs_parse_policy(struct ccs_io_buffer *head, char *line); | |
461 | +static int ccs_release(struct inode *inode, struct file *file); | |
462 | +static int ccs_set_mode(char *name, const char *value, | |
463 | + struct ccs_profile *profile); | |
464 | +static int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...) | |
465 | + __printf(2, 3); | |
466 | +static int ccs_truncate(char *str); | |
467 | +static int ccs_update_acl(const int size, struct ccs_acl_param *param); | |
468 | +static int ccs_update_manager_entry(const char *manager, const bool is_delete); | |
469 | +static int ccs_update_policy(const int size, struct ccs_acl_param *param); | |
470 | +static int ccs_write_acl(struct ccs_policy_namespace *ns, | |
471 | + struct list_head *list, char *data, | |
472 | + const bool is_delete); | |
473 | +static int ccs_write_aggregator(struct ccs_acl_param *param); | |
474 | +static int ccs_write_answer(struct ccs_io_buffer *head); | |
475 | +static int ccs_write_domain(struct ccs_io_buffer *head); | |
476 | +static int ccs_write_exception(struct ccs_io_buffer *head); | |
477 | +static int ccs_write_file(struct ccs_acl_param *param); | |
478 | +static int ccs_write_group(struct ccs_acl_param *param, const u8 type); | |
479 | +static int ccs_write_manager(struct ccs_io_buffer *head); | |
480 | +static int ccs_write_pid(struct ccs_io_buffer *head); | |
481 | +static int ccs_write_profile(struct ccs_io_buffer *head); | |
482 | +static int ccs_write_stat(struct ccs_io_buffer *head); | |
483 | +static int ccs_write_task(struct ccs_acl_param *param); | |
484 | +static int ccs_write_transition_control(struct ccs_acl_param *param, | |
485 | + const u8 type); | |
486 | +static s8 ccs_find_yesno(const char *string, const char *find); | |
487 | +static ssize_t ccs_read(struct file *file, char __user *buf, size_t count, | |
488 | + loff_t *ppos); | |
489 | +static ssize_t ccs_read_self(struct file *file, char __user *buf, size_t count, | |
490 | + loff_t *ppos); | |
491 | +static ssize_t ccs_write(struct file *file, const char __user *buf, | |
492 | + size_t count, loff_t *ppos); | |
493 | +static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry); | |
494 | +static struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param); | |
495 | +static struct ccs_domain_info *ccs_find_domain(const char *domainname); | |
496 | +static struct ccs_domain_info *ccs_find_domain_by_qid(unsigned int serial); | |
497 | +static struct ccs_group *ccs_get_group(struct ccs_acl_param *param, | |
498 | + const u8 idx); | |
499 | +static struct ccs_policy_namespace *ccs_assign_namespace | |
500 | +(const char *domainname); | |
501 | +static struct ccs_policy_namespace *ccs_find_namespace(const char *name, | |
502 | + const unsigned int len); | |
503 | +static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns, | |
504 | + const unsigned int profile); | |
505 | +static struct ccs_profile *ccs_profile(const u8 profile); | |
506 | +static u8 ccs_condition_type(const char *word); | |
507 | +static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3); | |
508 | +static u8 ccs_parse_ulong(unsigned long *result, char **str); | |
509 | +static unsigned int ccs_poll(struct file *file, poll_table *wait); | |
510 | +static void __init ccs_create_entry(const char *name, const umode_t mode, | |
511 | + struct proc_dir_entry *parent, | |
512 | + const u8 key); | |
513 | +static void __init ccs_load_builtin_policy(void); | |
514 | +static void __init ccs_policy_io_init(void); | |
515 | +static void __init ccs_proc_init(void); | |
516 | +static void ccs_add_entry(char *header); | |
517 | +static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) | |
518 | + __printf(3, 4); | |
519 | +static void ccs_addprintf(char *buffer, int len, const char *fmt, ...); | |
520 | +static void ccs_check_profile(void); | |
521 | +static void ccs_convert_time(time_t time, struct ccs_time *stamp); | |
522 | +static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns); | |
523 | +static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) | |
524 | + __printf(2, 3); | |
525 | +static void ccs_normalize_line(unsigned char *buffer); | |
526 | +static void ccs_print_config(struct ccs_io_buffer *head, const u8 config); | |
527 | +static void ccs_print_name_union(struct ccs_io_buffer *head, | |
528 | + const struct ccs_name_union *ptr); | |
529 | +static void ccs_print_name_union_quoted(struct ccs_io_buffer *head, | |
530 | + const struct ccs_name_union *ptr); | |
531 | +static void ccs_print_namespace(struct ccs_io_buffer *head); | |
532 | +static void ccs_print_number_union(struct ccs_io_buffer *head, | |
533 | + const struct ccs_number_union *ptr); | |
534 | +static void ccs_print_number_union_nospace(struct ccs_io_buffer *head, | |
535 | + const struct ccs_number_union *ptr); | |
536 | +static void ccs_read_domain(struct ccs_io_buffer *head); | |
537 | +static void ccs_read_exception(struct ccs_io_buffer *head); | |
538 | +static void ccs_read_log(struct ccs_io_buffer *head); | |
539 | +static void ccs_read_manager(struct ccs_io_buffer *head); | |
540 | +static void ccs_read_pid(struct ccs_io_buffer *head); | |
541 | +static void ccs_read_profile(struct ccs_io_buffer *head); | |
542 | +static void ccs_read_query(struct ccs_io_buffer *head); | |
543 | +static void ccs_read_stat(struct ccs_io_buffer *head); | |
544 | +static void ccs_read_version(struct ccs_io_buffer *head); | |
545 | +static void ccs_set_group(struct ccs_io_buffer *head, const char *category); | |
546 | +static void ccs_set_namespace_cursor(struct ccs_io_buffer *head); | |
547 | +static void ccs_set_slash(struct ccs_io_buffer *head); | |
548 | +static void ccs_set_space(struct ccs_io_buffer *head); | |
549 | +static void ccs_set_string(struct ccs_io_buffer *head, const char *string); | |
550 | +static void ccs_set_uint(unsigned int *i, const char *string, | |
551 | + const char *find); | |
552 | +static void ccs_update_stat(const u8 index); | |
553 | +static void ccs_update_task_domain(struct ccs_request_info *r); | |
554 | +static void ccs_write_log2(struct ccs_request_info *r, int len, | |
555 | + const char *fmt, va_list args); | |
556 | + | |
557 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
558 | +static bool __ccs_lport_reserved(const u16 port); | |
559 | +static int ccs_write_reserved_port(struct ccs_acl_param *param); | |
560 | +#endif | |
561 | + | |
562 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
563 | +static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param, | |
564 | + struct ccs_ipaddr_union *ptr); | |
565 | +static int ccs_print_ipv4(char *buffer, const unsigned int buffer_len, | |
566 | + const u32 *ip); | |
567 | +static int ccs_print_ipv6(char *buffer, const unsigned int buffer_len, | |
568 | + const struct in6_addr *ip); | |
569 | +static int ccs_write_inet_network(struct ccs_acl_param *param); | |
570 | +static int ccs_write_unix_network(struct ccs_acl_param *param); | |
571 | +static void ccs_print_ip(char *buf, const unsigned int size, | |
572 | + const struct ccs_ipaddr_union *ptr); | |
573 | +#endif | |
574 | + | |
575 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
576 | +static int ccs_write_capability(struct ccs_acl_param *param); | |
577 | +#endif | |
578 | + | |
579 | +#ifdef CONFIG_CCSECURITY_MISC | |
580 | +static int ccs_write_misc(struct ccs_acl_param *param); | |
581 | +#endif | |
582 | + | |
583 | +#ifdef CONFIG_CCSECURITY_IPC | |
584 | +static int ccs_write_ipc(struct ccs_acl_param *param); | |
585 | +#endif | |
586 | + | |
587 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
588 | +static ssize_t ccs_write_self(struct file *file, const char __user *buf, | |
589 | + size_t count, loff_t *ppos); | |
590 | +#endif | |
591 | + | |
592 | +/***** SECTION4: Standalone functions section *****/ | |
593 | + | |
594 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) | |
595 | + | |
596 | +/** | |
597 | + * fatal_signal_pending - Check whether SIGKILL is pending or not. | |
598 | + * | |
599 | + * @p: Pointer to "struct task_struct". | |
600 | + * | |
601 | + * Returns true if SIGKILL is pending on @p, false otherwise. | |
602 | + * | |
603 | + * This is for compatibility with older kernels. | |
604 | + */ | |
605 | +#define fatal_signal_pending(p) (signal_pending(p) && \ | |
606 | + sigismember(&p->pending.signal, SIGKILL)) | |
607 | + | |
608 | +#endif | |
609 | + | |
610 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
611 | + | |
612 | +/** | |
613 | + * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. | |
614 | + * | |
615 | + * @wq: The waitqueue to wait on. | |
616 | + * @condition: A C expression for the event to wait for. | |
617 | + * @ret: Timeout, in jiffies. | |
618 | + * | |
619 | + * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a | |
620 | + * signal, and the remaining jiffies otherwise if the condition evaluated to | |
621 | + * true before the timeout elapsed. | |
622 | + * | |
623 | + * This is for compatibility with older kernels. | |
624 | + */ | |
625 | +#define __wait_event_interruptible_timeout(wq, condition, ret) \ | |
626 | +do { \ | |
627 | + wait_queue_t __wait; \ | |
628 | + init_waitqueue_entry(&__wait, current); \ | |
629 | + \ | |
630 | + add_wait_queue(&wq, &__wait); \ | |
631 | + for (;;) { \ | |
632 | + set_current_state(TASK_INTERRUPTIBLE); \ | |
633 | + if (condition) \ | |
634 | + break; \ | |
635 | + if (!signal_pending(current)) { \ | |
636 | + ret = schedule_timeout(ret); \ | |
637 | + if (!ret) \ | |
638 | + break; \ | |
639 | + continue; \ | |
640 | + } \ | |
641 | + ret = -ERESTARTSYS; \ | |
642 | + break; \ | |
643 | + } \ | |
644 | + current->state = TASK_RUNNING; \ | |
645 | + remove_wait_queue(&wq, &__wait); \ | |
646 | +} while (0) | |
647 | + | |
648 | +/** | |
649 | + * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses. | |
650 | + * | |
651 | + * @wq: The waitqueue to wait on. | |
652 | + * @condition: A C expression for the event to wait for. | |
653 | + * @timeout: Timeout, in jiffies. | |
654 | + * | |
655 | + * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a | |
656 | + * signal, and the remaining jiffies otherwise if the condition evaluated to | |
657 | + * true before the timeout elapsed. | |
658 | + * | |
659 | + * This is for compatibility with older kernels. | |
660 | + */ | |
661 | +#define wait_event_interruptible_timeout(wq, condition, timeout) \ | |
662 | +({ \ | |
663 | + long __ret = timeout; \ | |
664 | + if (!(condition)) \ | |
665 | + __wait_event_interruptible_timeout(wq, condition, __ret); \ | |
666 | + __ret; \ | |
667 | +}) | |
668 | + | |
669 | +#endif | |
670 | + | |
671 | +/** | |
672 | + * ccs_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss. | |
673 | + * | |
674 | + * @time: Seconds since 1970/01/01 00:00:00. | |
675 | + * @stamp: Pointer to "struct ccs_time". | |
676 | + * | |
677 | + * Returns nothing. | |
678 | + * | |
679 | + * This function does not handle Y2038 problem. | |
680 | + */ | |
681 | +static void ccs_convert_time(time_t time, struct ccs_time *stamp) | |
682 | +{ | |
683 | + static const u16 ccs_eom[2][12] = { | |
684 | + { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | |
685 | + { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | |
686 | + }; | |
687 | + u16 y; | |
688 | + u8 m; | |
689 | + bool r; | |
690 | + stamp->sec = time % 60; | |
691 | + time /= 60; | |
692 | + stamp->min = time % 60; | |
693 | + time /= 60; | |
694 | + stamp->hour = time % 24; | |
695 | + time /= 24; | |
696 | + for (y = 1970; ; y++) { | |
697 | + const unsigned short days = (y & 3) ? 365 : 366; | |
698 | + if (time < days) | |
699 | + break; | |
700 | + time -= days; | |
701 | + } | |
702 | + r = (y & 3) == 0; | |
703 | + for (m = 0; m < 11 && time >= ccs_eom[r][m]; m++); | |
704 | + if (m) | |
705 | + time -= ccs_eom[r][m - 1]; | |
706 | + stamp->year = y; | |
707 | + stamp->month = ++m; | |
708 | + stamp->day = ++time; | |
709 | +} | |
710 | + | |
711 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 23) | |
712 | +#if !defined(RHEL_VERSION) || RHEL_VERSION != 3 | |
713 | + | |
714 | +/** | |
715 | + * PDE - Get "struct proc_dir_entry". | |
716 | + * | |
717 | + * @inode: Pointer to "struct inode". | |
718 | + * | |
719 | + * Returns pointer to "struct proc_dir_entry". | |
720 | + * | |
721 | + * This is for compatibility with older kernels. | |
722 | + */ | |
723 | +static inline struct proc_dir_entry *PDE(const struct inode *inode) | |
724 | +{ | |
725 | + return (struct proc_dir_entry *) inode->u.generic_ip; | |
726 | +} | |
727 | + | |
728 | +#endif | |
729 | +#endif | |
730 | + | |
731 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
732 | + | |
733 | +/** | |
734 | + * proc_notify_change - Update inode's attributes and reflect to the dentry. | |
735 | + * | |
736 | + * @dentry: Pointer to "struct dentry". | |
737 | + * @iattr: Pointer to "struct iattr". | |
738 | + * | |
739 | + * Returns 0 on success, negative value otherwise. | |
740 | + * | |
741 | + * The 2.4 kernels don't allow chmod()/chown() for files in /proc, | |
742 | + * while the 2.6 kernels allow. | |
743 | + * To permit management of /proc/ccs/ interface by non-root user, | |
744 | + * I modified to allow chmod()/chown() of /proc/ccs/ interface like 2.6 kernels | |
745 | + * by adding "struct inode_operations"->setattr hook. | |
746 | + */ | |
747 | +static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) | |
748 | +{ | |
749 | + struct inode *inode = dentry->d_inode; | |
750 | + struct proc_dir_entry *de = PDE(inode); | |
751 | + int error; | |
752 | + | |
753 | + error = inode_change_ok(inode, iattr); | |
754 | + if (error) | |
755 | + goto out; | |
756 | + | |
757 | + error = inode_setattr(inode, iattr); | |
758 | + if (error) | |
759 | + goto out; | |
760 | + | |
761 | + de->uid = inode->i_uid; | |
762 | + de->gid = inode->i_gid; | |
763 | + de->mode = inode->i_mode; | |
764 | +out: | |
765 | + return error; | |
766 | +} | |
767 | + | |
768 | +#endif | |
769 | + | |
770 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
771 | + | |
772 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) && defined(CONFIG_NET) | |
773 | +#define ccs_in4_pton in4_pton | |
774 | +#define ccs_in6_pton in6_pton | |
775 | +#else | |
776 | +/* | |
777 | + * Routines for parsing IPv4 or IPv6 address. | |
778 | + * These are copied from lib/hexdump.c net/core/utils.c . | |
779 | + */ | |
780 | +#include <linux/ctype.h> | |
781 | + | |
782 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35) | |
783 | +static int hex_to_bin(char ch) | |
784 | +{ | |
785 | + if ((ch >= '0') && (ch <= '9')) | |
786 | + return ch - '0'; | |
787 | + ch = tolower(ch); | |
788 | + if ((ch >= 'a') && (ch <= 'f')) | |
789 | + return ch - 'a' + 10; | |
790 | + return -1; | |
791 | +} | |
792 | +#endif | |
793 | + | |
794 | +#define IN6PTON_XDIGIT 0x00010000 | |
795 | +#define IN6PTON_DIGIT 0x00020000 | |
796 | +#define IN6PTON_COLON_MASK 0x00700000 | |
797 | +#define IN6PTON_COLON_1 0x00100000 /* single : requested */ | |
798 | +#define IN6PTON_COLON_2 0x00200000 /* second : requested */ | |
799 | +#define IN6PTON_COLON_1_2 0x00400000 /* :: requested */ | |
800 | +#define IN6PTON_DOT 0x00800000 /* . */ | |
801 | +#define IN6PTON_DELIM 0x10000000 | |
802 | +#define IN6PTON_NULL 0x20000000 /* first/tail */ | |
803 | +#define IN6PTON_UNKNOWN 0x40000000 | |
804 | + | |
805 | +static inline int xdigit2bin(char c, int delim) | |
806 | +{ | |
807 | + int val; | |
808 | + | |
809 | + if (c == delim || c == '\0') | |
810 | + return IN6PTON_DELIM; | |
811 | + if (c == ':') | |
812 | + return IN6PTON_COLON_MASK; | |
813 | + if (c == '.') | |
814 | + return IN6PTON_DOT; | |
815 | + | |
816 | + val = hex_to_bin(c); | |
817 | + if (val >= 0) | |
818 | + return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0); | |
819 | + | |
820 | + if (delim == -1) | |
821 | + return IN6PTON_DELIM; | |
822 | + return IN6PTON_UNKNOWN; | |
823 | +} | |
824 | + | |
825 | +static int ccs_in4_pton(const char *src, int srclen, u8 *dst, int delim, | |
826 | + const char **end) | |
827 | +{ | |
828 | + const char *s; | |
829 | + u8 *d; | |
830 | + u8 dbuf[4]; | |
831 | + int ret = 0; | |
832 | + int i; | |
833 | + int w = 0; | |
834 | + | |
835 | + if (srclen < 0) | |
836 | + srclen = strlen(src); | |
837 | + s = src; | |
838 | + d = dbuf; | |
839 | + i = 0; | |
840 | + while (1) { | |
841 | + int c; | |
842 | + c = xdigit2bin(srclen > 0 ? *s : '\0', delim); | |
843 | + if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | | |
844 | + IN6PTON_COLON_MASK))) | |
845 | + goto out; | |
846 | + if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) { | |
847 | + if (w == 0) | |
848 | + goto out; | |
849 | + *d++ = w & 0xff; | |
850 | + w = 0; | |
851 | + i++; | |
852 | + if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) { | |
853 | + if (i != 4) | |
854 | + goto out; | |
855 | + break; | |
856 | + } | |
857 | + goto cont; | |
858 | + } | |
859 | + w = (w * 10) + c; | |
860 | + if ((w & 0xffff) > 255) | |
861 | + goto out; | |
862 | +cont: | |
863 | + if (i >= 4) | |
864 | + goto out; | |
865 | + s++; | |
866 | + srclen--; | |
867 | + } | |
868 | + ret = 1; | |
869 | + memcpy(dst, dbuf, sizeof(dbuf)); | |
870 | +out: | |
871 | + if (end) | |
872 | + *end = s; | |
873 | + return ret; | |
874 | +} | |
875 | + | |
876 | +static int ccs_in6_pton(const char *src, int srclen, u8 *dst, int delim, | |
877 | + const char **end) | |
878 | +{ | |
879 | + const char *s, *tok = NULL; | |
880 | + u8 *d, *dc = NULL; | |
881 | + u8 dbuf[16]; | |
882 | + int ret = 0; | |
883 | + int i; | |
884 | + int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL; | |
885 | + int w = 0; | |
886 | + | |
887 | + memset(dbuf, 0, sizeof(dbuf)); | |
888 | + | |
889 | + s = src; | |
890 | + d = dbuf; | |
891 | + if (srclen < 0) | |
892 | + srclen = strlen(src); | |
893 | + | |
894 | + while (1) { | |
895 | + int c; | |
896 | + | |
897 | + c = xdigit2bin(srclen > 0 ? *s : '\0', delim); | |
898 | + if (!(c & state)) | |
899 | + goto out; | |
900 | + if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) { | |
901 | + /* process one 16-bit word */ | |
902 | + if (!(state & IN6PTON_NULL)) { | |
903 | + *d++ = (w >> 8) & 0xff; | |
904 | + *d++ = w & 0xff; | |
905 | + } | |
906 | + w = 0; | |
907 | + if (c & IN6PTON_DELIM) { | |
908 | + /* We've processed last word */ | |
909 | + break; | |
910 | + } | |
911 | + /* | |
912 | + * COLON_1 => XDIGIT | |
913 | + * COLON_2 => XDIGIT|DELIM | |
914 | + * COLON_1_2 => COLON_2 | |
915 | + */ | |
916 | + switch (state & IN6PTON_COLON_MASK) { | |
917 | + case IN6PTON_COLON_2: | |
918 | + dc = d; | |
919 | + state = IN6PTON_XDIGIT | IN6PTON_DELIM; | |
920 | + if (dc - dbuf >= sizeof(dbuf)) | |
921 | + state |= IN6PTON_NULL; | |
922 | + break; | |
923 | + case IN6PTON_COLON_1|IN6PTON_COLON_1_2: | |
924 | + state = IN6PTON_XDIGIT | IN6PTON_COLON_2; | |
925 | + break; | |
926 | + case IN6PTON_COLON_1: | |
927 | + state = IN6PTON_XDIGIT; | |
928 | + break; | |
929 | + case IN6PTON_COLON_1_2: | |
930 | + state = IN6PTON_COLON_2; | |
931 | + break; | |
932 | + default: | |
933 | + state = 0; | |
934 | + } | |
935 | + tok = s + 1; | |
936 | + goto cont; | |
937 | + } | |
938 | + | |
939 | + if (c & IN6PTON_DOT) { | |
940 | + ret = ccs_in4_pton(tok ? tok : s, srclen + | |
941 | + (int)(s - tok), d, delim, &s); | |
942 | + if (ret > 0) { | |
943 | + d += 4; | |
944 | + break; | |
945 | + } | |
946 | + goto out; | |
947 | + } | |
948 | + | |
949 | + w = (w << 4) | (0xff & c); | |
950 | + state = IN6PTON_COLON_1 | IN6PTON_DELIM; | |
951 | + if (!(w & 0xf000)) | |
952 | + state |= IN6PTON_XDIGIT; | |
953 | + if (!dc && d + 2 < dbuf + sizeof(dbuf)) { | |
954 | + state |= IN6PTON_COLON_1_2; | |
955 | + state &= ~IN6PTON_DELIM; | |
956 | + } | |
957 | + if (d + 2 >= dbuf + sizeof(dbuf)) | |
958 | + state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2); | |
959 | +cont: | |
960 | + if ((dc && d + 4 < dbuf + sizeof(dbuf)) || | |
961 | + d + 4 == dbuf + sizeof(dbuf)) | |
962 | + state |= IN6PTON_DOT; | |
963 | + if (d >= dbuf + sizeof(dbuf)) | |
964 | + state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK); | |
965 | + s++; | |
966 | + srclen--; | |
967 | + } | |
968 | + | |
969 | + i = 15; d--; | |
970 | + | |
971 | + if (dc) { | |
972 | + while (d >= dc) | |
973 | + dst[i--] = *d--; | |
974 | + while (i >= dc - dbuf) | |
975 | + dst[i--] = 0; | |
976 | + while (i >= 0) | |
977 | + dst[i--] = *d--; | |
978 | + } else | |
979 | + memcpy(dst, dbuf, sizeof(dbuf)); | |
980 | + | |
981 | + ret = 1; | |
982 | +out: | |
983 | + if (end) | |
984 | + *end = s; | |
985 | + return ret; | |
986 | +} | |
987 | +#endif | |
988 | + | |
989 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32) | |
990 | + | |
991 | +/* | |
992 | + * Routines for printing IPv4 or IPv6 address. | |
993 | + * These are copied from include/linux/kernel.h include/net/ipv6.h | |
994 | + * include/net/addrconf.h lib/hexdump.c lib/vsprintf.c and simplified. | |
995 | + */ | |
996 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) | |
997 | +#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 || !defined(RHEL_MINOR) || RHEL_MINOR < 9 | |
998 | +static const char hex_asc[] = "0123456789abcdef"; | |
999 | +#define hex_asc_lo(x) hex_asc[((x) & 0x0f)] | |
1000 | +#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] | |
1001 | + | |
1002 | +static inline char *pack_hex_byte(char *buf, u8 byte) | |
1003 | +{ | |
1004 | + *buf++ = hex_asc_hi(byte); | |
1005 | + *buf++ = hex_asc_lo(byte); | |
1006 | + return buf; | |
1007 | +} | |
1008 | +#endif | |
1009 | +#endif | |
1010 | + | |
1011 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) | |
1012 | +static inline int ipv6_addr_v4mapped(const struct in6_addr *a) | |
1013 | +{ | |
1014 | + return (a->s6_addr32[0] | a->s6_addr32[1] | | |
1015 | + (a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0; | |
1016 | +} | |
1017 | +#endif | |
1018 | + | |
1019 | +static inline int ipv6_addr_is_isatap(const struct in6_addr *addr) | |
1020 | +{ | |
1021 | + return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE); | |
1022 | +} | |
1023 | + | |
1024 | +static char *ip4_string(char *p, const u8 *addr) | |
1025 | +{ | |
1026 | + /* | |
1027 | + * Since this function is called outside vsnprintf(), I can use | |
1028 | + * sprintf() here. | |
1029 | + */ | |
1030 | + return p + | |
1031 | + sprintf(p, "%u.%u.%u.%u", addr[0], addr[1], addr[2], addr[3]); | |
1032 | +} | |
1033 | + | |
1034 | +static char *ip6_compressed_string(char *p, const char *addr) | |
1035 | +{ | |
1036 | + int i, j, range; | |
1037 | + unsigned char zerolength[8]; | |
1038 | + int longest = 1; | |
1039 | + int colonpos = -1; | |
1040 | + u16 word; | |
1041 | + u8 hi, lo; | |
1042 | + bool needcolon = false; | |
1043 | + bool useIPv4; | |
1044 | + struct in6_addr in6; | |
1045 | + | |
1046 | + memcpy(&in6, addr, sizeof(struct in6_addr)); | |
1047 | + | |
1048 | + useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); | |
1049 | + | |
1050 | + memset(zerolength, 0, sizeof(zerolength)); | |
1051 | + | |
1052 | + if (useIPv4) | |
1053 | + range = 6; | |
1054 | + else | |
1055 | + range = 8; | |
1056 | + | |
1057 | + /* find position of longest 0 run */ | |
1058 | + for (i = 0; i < range; i++) { | |
1059 | + for (j = i; j < range; j++) { | |
1060 | + if (in6.s6_addr16[j] != 0) | |
1061 | + break; | |
1062 | + zerolength[i]++; | |
1063 | + } | |
1064 | + } | |
1065 | + for (i = 0; i < range; i++) { | |
1066 | + if (zerolength[i] > longest) { | |
1067 | + longest = zerolength[i]; | |
1068 | + colonpos = i; | |
1069 | + } | |
1070 | + } | |
1071 | + if (longest == 1) /* don't compress a single 0 */ | |
1072 | + colonpos = -1; | |
1073 | + | |
1074 | + /* emit address */ | |
1075 | + for (i = 0; i < range; i++) { | |
1076 | + if (i == colonpos) { | |
1077 | + if (needcolon || i == 0) | |
1078 | + *p++ = ':'; | |
1079 | + *p++ = ':'; | |
1080 | + needcolon = false; | |
1081 | + i += longest - 1; | |
1082 | + continue; | |
1083 | + } | |
1084 | + if (needcolon) { | |
1085 | + *p++ = ':'; | |
1086 | + needcolon = false; | |
1087 | + } | |
1088 | + /* hex u16 without leading 0s */ | |
1089 | + word = ntohs(in6.s6_addr16[i]); | |
1090 | + hi = word >> 8; | |
1091 | + lo = word & 0xff; | |
1092 | + if (hi) { | |
1093 | + if (hi > 0x0f) | |
1094 | + p = pack_hex_byte(p, hi); | |
1095 | + else | |
1096 | + *p++ = hex_asc_lo(hi); | |
1097 | + p = pack_hex_byte(p, lo); | |
1098 | + } else if (lo > 0x0f) | |
1099 | + p = pack_hex_byte(p, lo); | |
1100 | + else | |
1101 | + *p++ = hex_asc_lo(lo); | |
1102 | + needcolon = true; | |
1103 | + } | |
1104 | + | |
1105 | + if (useIPv4) { | |
1106 | + if (needcolon) | |
1107 | + *p++ = ':'; | |
1108 | + p = ip4_string(p, &in6.s6_addr[12]); | |
1109 | + } | |
1110 | + *p = '\0'; | |
1111 | + | |
1112 | + return p; | |
1113 | +} | |
1114 | +#endif | |
1115 | + | |
1116 | +/** | |
1117 | + * ccs_print_ipv4 - Print an IPv4 address. | |
1118 | + * | |
1119 | + * @buffer: Buffer to write to. | |
1120 | + * @buffer_len: Size of @buffer. | |
1121 | + * @ip: Pointer to "u32 in network byte order". | |
1122 | + * | |
1123 | + * Returns written length. | |
1124 | + */ | |
1125 | +static int ccs_print_ipv4(char *buffer, const unsigned int buffer_len, | |
1126 | + const u32 *ip) | |
1127 | +{ | |
1128 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
1129 | + return snprintf(buffer, buffer_len, "%pI4", ip); | |
1130 | +#else | |
1131 | + char addr[sizeof("255.255.255.255")]; | |
1132 | + ip4_string(addr, (const u8 *) ip); | |
1133 | + return snprintf(buffer, buffer_len, "%s", addr); | |
1134 | +#endif | |
1135 | +} | |
1136 | + | |
1137 | +/** | |
1138 | + * ccs_print_ipv6 - Print an IPv6 address. | |
1139 | + * | |
1140 | + * @buffer: Buffer to write to. | |
1141 | + * @buffer_len: Size of @buffer. | |
1142 | + * @ip: Pointer to "struct in6_addr". | |
1143 | + * | |
1144 | + * Returns written length. | |
1145 | + */ | |
1146 | +static int ccs_print_ipv6(char *buffer, const unsigned int buffer_len, | |
1147 | + const struct in6_addr *ip) | |
1148 | +{ | |
1149 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
1150 | + return snprintf(buffer, buffer_len, "%pI6c", ip); | |
1151 | +#else | |
1152 | + char addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | |
1153 | + ip6_compressed_string(addr, (const u8 *) ip); | |
1154 | + return snprintf(buffer, buffer_len, "%s", addr); | |
1155 | +#endif | |
1156 | +} | |
1157 | + | |
1158 | +/** | |
1159 | + * ccs_print_ip - Print an IP address. | |
1160 | + * | |
1161 | + * @buf: Buffer to write to. | |
1162 | + * @size: Size of @buf. | |
1163 | + * @ptr: Pointer to "struct ipaddr_union". | |
1164 | + * | |
1165 | + * Returns nothing. | |
1166 | + */ | |
1167 | +static void ccs_print_ip(char *buf, const unsigned int size, | |
1168 | + const struct ccs_ipaddr_union *ptr) | |
1169 | +{ | |
1170 | + int len; | |
1171 | + if (ptr->is_ipv6) | |
1172 | + len = ccs_print_ipv6(buf, size, &ptr->ip[0]); | |
1173 | + else | |
1174 | + len = ccs_print_ipv4(buf, size, &ptr->ip[0].s6_addr32[0]); | |
1175 | + if (!memcmp(&ptr->ip[0], &ptr->ip[1], 16) || len >= size / 2) | |
1176 | + return; | |
1177 | + buf[len++] = '-'; | |
1178 | + if (ptr->is_ipv6) | |
1179 | + ccs_print_ipv6(buf + len, size - len, &ptr->ip[1]); | |
1180 | + else | |
1181 | + ccs_print_ipv4(buf + len, size - len, | |
1182 | + &ptr->ip[1].s6_addr32[0]); | |
1183 | +} | |
1184 | + | |
1185 | +#endif | |
1186 | + | |
1187 | +/***** SECTION5: Variables definition section *****/ | |
1188 | + | |
1189 | +/* Permit policy management by non-root user? */ | |
1190 | +static bool ccs_manage_by_non_root; | |
1191 | + | |
1192 | +/* Lock for protecting policy. */ | |
1193 | +DEFINE_MUTEX(ccs_policy_lock); | |
1194 | + | |
1195 | +/* Has /sbin/init started? */ | |
1196 | +bool ccs_policy_loaded; | |
1197 | + | |
1198 | +/* List of namespaces. */ | |
1199 | +LIST_HEAD(ccs_namespace_list); | |
1200 | +/* True if namespace other than ccs_kernel_namespace is defined. */ | |
1201 | +static bool ccs_namespace_enabled; | |
1202 | + | |
1203 | +/* Initial namespace.*/ | |
1204 | +static struct ccs_policy_namespace ccs_kernel_namespace; | |
1205 | + | |
1206 | +/* List of "struct ccs_condition". */ | |
1207 | +LIST_HEAD(ccs_condition_list); | |
1208 | + | |
1209 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
1210 | +/* Bitmap for reserved local port numbers.*/ | |
1211 | +static u8 ccs_reserved_port_map[8192]; | |
1212 | +#endif | |
1213 | + | |
1214 | +/* Wait queue for kernel -> userspace notification. */ | |
1215 | +static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait); | |
1216 | +/* Wait queue for userspace -> kernel notification. */ | |
1217 | +static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait); | |
1218 | + | |
1219 | +/* The list for "struct ccs_query". */ | |
1220 | +static LIST_HEAD(ccs_query_list); | |
1221 | + | |
1222 | +/* Lock for manipulating ccs_query_list. */ | |
1223 | +static DEFINE_SPINLOCK(ccs_query_list_lock); | |
1224 | + | |
1225 | +/* Number of "struct file" referring /proc/ccs/query interface. */ | |
1226 | +static atomic_t ccs_query_observers = ATOMIC_INIT(0); | |
1227 | + | |
1228 | +/* Wait queue for /proc/ccs/audit. */ | |
1229 | +static DECLARE_WAIT_QUEUE_HEAD(ccs_log_wait); | |
1230 | + | |
1231 | +/* The list for "struct ccs_log". */ | |
1232 | +static LIST_HEAD(ccs_log); | |
1233 | + | |
1234 | +/* Lock for "struct list_head ccs_log". */ | |
1235 | +static DEFINE_SPINLOCK(ccs_log_lock); | |
1236 | + | |
1237 | +/* Length of "stuct list_head ccs_log". */ | |
1238 | +static unsigned int ccs_log_count; | |
1239 | + | |
1240 | +/* Timestamp counter for last updated. */ | |
1241 | +static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT]; | |
1242 | + | |
1243 | +/* Counter for number of updates. */ | |
1244 | +static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT]; | |
1245 | + | |
1246 | +/* Operations for /proc/ccs/self_domain interface. */ | |
1247 | +static | |
1248 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17) | |
1249 | +const | |
1250 | +#endif | |
1251 | +struct file_operations ccs_self_operations = { | |
1252 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
1253 | + .write = ccs_write_self, | |
1254 | +#endif | |
1255 | + .read = ccs_read_self, | |
1256 | +}; | |
1257 | + | |
1258 | +/* Operations for /proc/ccs/ interface. */ | |
1259 | +static | |
1260 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17) | |
1261 | +const | |
1262 | +#endif | |
1263 | +struct file_operations ccs_operations = { | |
1264 | + .open = ccs_open, | |
1265 | + .release = ccs_release, | |
1266 | + .poll = ccs_poll, | |
1267 | + .read = ccs_read, | |
1268 | + .write = ccs_write, | |
1269 | +}; | |
1270 | + | |
1271 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
1272 | + | |
1273 | +/* The inode operations for /proc/ccs/ directory. */ | |
1274 | +static struct inode_operations ccs_dir_inode_operations; | |
1275 | + | |
1276 | +/* The inode operations for files under /proc/ccs/ directory. */ | |
1277 | +static struct inode_operations ccs_file_inode_operations; | |
1278 | + | |
1279 | +#endif | |
1280 | + | |
1281 | +/***** SECTION6: Dependent functions section *****/ | |
1282 | + | |
1283 | +/** | |
1284 | + * list_for_each_cookie - iterate over a list with cookie. | |
1285 | + * | |
1286 | + * @pos: Pointer to "struct list_head". | |
1287 | + * @head: Pointer to "struct list_head". | |
1288 | + */ | |
1289 | +#define list_for_each_cookie(pos, head) \ | |
1290 | + for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \ | |
1291 | + pos != (head); pos = srcu_dereference(pos->next, &ccs_ss)) | |
1292 | + | |
1293 | +/** | |
1294 | + * ccs_read_token - Read a word from a line. | |
1295 | + * | |
1296 | + * @param: Pointer to "struct ccs_acl_param". | |
1297 | + * | |
1298 | + * Returns a word on success, "" otherwise. | |
1299 | + * | |
1300 | + * To allow the caller to skip NULL check, this function returns "" rather than | |
1301 | + * NULL if there is no more words to read. | |
1302 | + */ | |
1303 | +static char *ccs_read_token(struct ccs_acl_param *param) | |
1304 | +{ | |
1305 | + char *pos = param->data; | |
1306 | + char *del = strchr(pos, ' '); | |
1307 | + if (del) | |
1308 | + *del++ = '\0'; | |
1309 | + else | |
1310 | + del = pos + strlen(pos); | |
1311 | + param->data = del; | |
1312 | + return pos; | |
1313 | +} | |
1314 | + | |
1315 | +/** | |
1316 | + * ccs_make_byte - Make byte value from three octal characters. | |
1317 | + * | |
1318 | + * @c1: The first character. | |
1319 | + * @c2: The second character. | |
1320 | + * @c3: The third character. | |
1321 | + * | |
1322 | + * Returns byte value. | |
1323 | + */ | |
1324 | +static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3) | |
1325 | +{ | |
1326 | + return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0'); | |
1327 | +} | |
1328 | + | |
1329 | +/** | |
1330 | + * ccs_correct_word2 - Check whether the given string follows the naming rules. | |
1331 | + * | |
1332 | + * @string: The byte sequence to check. Not '\0'-terminated. | |
1333 | + * @len: Length of @string. | |
1334 | + * | |
1335 | + * Returns true if @string follows the naming rules, false otherwise. | |
1336 | + */ | |
1337 | +static bool ccs_correct_word2(const char *string, size_t len) | |
1338 | +{ | |
1339 | + u8 recursion = 20; | |
1340 | + const char *const start = string; | |
1341 | + bool in_repetition = false; | |
1342 | + if (!len) | |
1343 | + goto out; | |
1344 | + while (len--) { | |
1345 | + unsigned char c = *string++; | |
1346 | + if (c == '\\') { | |
1347 | + if (!len--) | |
1348 | + goto out; | |
1349 | + c = *string++; | |
1350 | + if (c >= '0' && c <= '3') { | |
1351 | + unsigned char d; | |
1352 | + unsigned char e; | |
1353 | + if (!len-- || !len--) | |
1354 | + goto out; | |
1355 | + d = *string++; | |
1356 | + e = *string++; | |
1357 | + if (d < '0' || d > '7' || e < '0' || e > '7') | |
1358 | + goto out; | |
1359 | + c = ccs_make_byte(c, d, e); | |
1360 | + if (c <= ' ' || c >= 127) | |
1361 | + continue; | |
1362 | + goto out; | |
1363 | + } | |
1364 | + switch (c) { | |
1365 | + case '\\': /* "\\" */ | |
1366 | + case '+': /* "\+" */ | |
1367 | + case '?': /* "\?" */ | |
1368 | + case 'x': /* "\x" */ | |
1369 | + case 'a': /* "\a" */ | |
1370 | + case '-': /* "\-" */ | |
1371 | + continue; | |
1372 | + } | |
1373 | + if (!recursion--) | |
1374 | + goto out; | |
1375 | + switch (c) { | |
1376 | + case '*': /* "\*" */ | |
1377 | + case '@': /* "\@" */ | |
1378 | + case '$': /* "\$" */ | |
1379 | + case 'X': /* "\X" */ | |
1380 | + case 'A': /* "\A" */ | |
1381 | + continue; | |
1382 | + case '{': /* "/\{" */ | |
1383 | + if (string - 3 < start || *(string - 3) != '/') | |
1384 | + goto out; | |
1385 | + in_repetition = true; | |
1386 | + continue; | |
1387 | + case '}': /* "\}/" */ | |
1388 | + if (*string != '/') | |
1389 | + goto out; | |
1390 | + if (!in_repetition) | |
1391 | + goto out; | |
1392 | + in_repetition = false; | |
1393 | + continue; | |
1394 | + } | |
1395 | + goto out; | |
1396 | + } else if (in_repetition && c == '/') { | |
1397 | + goto out; | |
1398 | + } else if (c <= ' ' || c >= 127) { | |
1399 | + goto out; | |
1400 | + } | |
1401 | + } | |
1402 | + if (in_repetition) | |
1403 | + goto out; | |
1404 | + return true; | |
1405 | +out: | |
1406 | + return false; | |
1407 | +} | |
1408 | + | |
1409 | +/** | |
1410 | + * ccs_correct_word - Check whether the given string follows the naming rules. | |
1411 | + * | |
1412 | + * @string: The string to check. | |
1413 | + * | |
1414 | + * Returns true if @string follows the naming rules, false otherwise. | |
1415 | + */ | |
1416 | +static bool ccs_correct_word(const char *string) | |
1417 | +{ | |
1418 | + return ccs_correct_word2(string, strlen(string)); | |
1419 | +} | |
1420 | + | |
1421 | +/** | |
1422 | + * ccs_get_group - Allocate memory for "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group". | |
1423 | + * | |
1424 | + * @param: Pointer to "struct ccs_acl_param". | |
1425 | + * @idx: Index number. | |
1426 | + * | |
1427 | + * Returns pointer to "struct ccs_group" on success, NULL otherwise. | |
1428 | + */ | |
1429 | +static struct ccs_group *ccs_get_group(struct ccs_acl_param *param, | |
1430 | + const u8 idx) | |
1431 | +{ | |
1432 | + struct ccs_group e = { }; | |
1433 | + struct ccs_group *group = NULL; | |
1434 | + struct list_head *list; | |
1435 | + const char *group_name = ccs_read_token(param); | |
1436 | + bool found = false; | |
1437 | + if (!ccs_correct_word(group_name) || idx >= CCS_MAX_GROUP) | |
1438 | + return NULL; | |
1439 | + e.group_name = ccs_get_name(group_name); | |
1440 | + if (!e.group_name) | |
1441 | + return NULL; | |
1442 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
1443 | + goto out; | |
1444 | + list = ¶m->ns->group_list[idx]; | |
1445 | + list_for_each_entry(group, list, head.list) { | |
1446 | + if (e.group_name != group->group_name || | |
1447 | + atomic_read(&group->head.users) == CCS_GC_IN_PROGRESS) | |
1448 | + continue; | |
1449 | + atomic_inc(&group->head.users); | |
1450 | + found = true; | |
1451 | + break; | |
1452 | + } | |
1453 | + if (!found) { | |
1454 | + struct ccs_group *entry = ccs_commit_ok(&e, sizeof(e)); | |
1455 | + if (entry) { | |
1456 | + INIT_LIST_HEAD(&entry->member_list); | |
1457 | + atomic_set(&entry->head.users, 1); | |
1458 | + list_add_tail_rcu(&entry->head.list, list); | |
1459 | + group = entry; | |
1460 | + found = true; | |
1461 | + } | |
1462 | + } | |
1463 | + mutex_unlock(&ccs_policy_lock); | |
1464 | +out: | |
1465 | + ccs_put_name(e.group_name); | |
1466 | + return found ? group : NULL; | |
1467 | +} | |
1468 | + | |
1469 | +/** | |
1470 | + * ccs_parse_name_union - Parse a ccs_name_union. | |
1471 | + * | |
1472 | + * @param: Pointer to "struct ccs_acl_param". | |
1473 | + * @ptr: Pointer to "struct ccs_name_union". | |
1474 | + * | |
1475 | + * Returns true on success, false otherwise. | |
1476 | + */ | |
1477 | +static bool ccs_parse_name_union(struct ccs_acl_param *param, | |
1478 | + struct ccs_name_union *ptr) | |
1479 | +{ | |
1480 | + char *filename; | |
1481 | + if (param->data[0] == '@') { | |
1482 | + param->data++; | |
1483 | + ptr->group = ccs_get_group(param, CCS_PATH_GROUP); | |
1484 | + return ptr->group != NULL; | |
1485 | + } | |
1486 | + filename = ccs_read_token(param); | |
1487 | + if (!ccs_correct_word(filename)) | |
1488 | + return false; | |
1489 | + ptr->filename = ccs_get_name(filename); | |
1490 | + return ptr->filename != NULL; | |
1491 | +} | |
1492 | + | |
1493 | +/** | |
1494 | + * ccs_parse_ulong - Parse an "unsigned long" value. | |
1495 | + * | |
1496 | + * @result: Pointer to "unsigned long". | |
1497 | + * @str: Pointer to string to parse. | |
1498 | + * | |
1499 | + * Returns one of values in "enum ccs_value_type". | |
1500 | + * | |
1501 | + * The @src is updated to point the first character after the value | |
1502 | + * on success. | |
1503 | + */ | |
1504 | +static u8 ccs_parse_ulong(unsigned long *result, char **str) | |
1505 | +{ | |
1506 | + const char *cp = *str; | |
1507 | + char *ep; | |
1508 | + int base = 10; | |
1509 | + if (*cp == '0') { | |
1510 | + char c = *(cp + 1); | |
1511 | + if (c == 'x' || c == 'X') { | |
1512 | + base = 16; | |
1513 | + cp += 2; | |
1514 | + } else if (c >= '0' && c <= '7') { | |
1515 | + base = 8; | |
1516 | + cp++; | |
1517 | + } | |
1518 | + } | |
1519 | + *result = simple_strtoul(cp, &ep, base); | |
1520 | + if (cp == ep) | |
1521 | + return CCS_VALUE_TYPE_INVALID; | |
1522 | + *str = ep; | |
1523 | + switch (base) { | |
1524 | + case 16: | |
1525 | + return CCS_VALUE_TYPE_HEXADECIMAL; | |
1526 | + case 8: | |
1527 | + return CCS_VALUE_TYPE_OCTAL; | |
1528 | + default: | |
1529 | + return CCS_VALUE_TYPE_DECIMAL; | |
1530 | + } | |
1531 | +} | |
1532 | + | |
1533 | +/** | |
1534 | + * ccs_parse_number_union - Parse a ccs_number_union. | |
1535 | + * | |
1536 | + * @param: Pointer to "struct ccs_acl_param". | |
1537 | + * @ptr: Pointer to "struct ccs_number_union". | |
1538 | + * | |
1539 | + * Returns true on success, false otherwise. | |
1540 | + */ | |
1541 | +static bool ccs_parse_number_union(struct ccs_acl_param *param, | |
1542 | + struct ccs_number_union *ptr) | |
1543 | +{ | |
1544 | + char *data; | |
1545 | + u8 type; | |
1546 | + unsigned long v; | |
1547 | + memset(ptr, 0, sizeof(*ptr)); | |
1548 | + if (param->data[0] == '@') { | |
1549 | + param->data++; | |
1550 | + ptr->group = ccs_get_group(param, CCS_NUMBER_GROUP); | |
1551 | + return ptr->group != NULL; | |
1552 | + } | |
1553 | + data = ccs_read_token(param); | |
1554 | + type = ccs_parse_ulong(&v, &data); | |
1555 | + if (type == CCS_VALUE_TYPE_INVALID) | |
1556 | + return false; | |
1557 | + ptr->values[0] = v; | |
1558 | + ptr->value_type[0] = type; | |
1559 | + if (!*data) { | |
1560 | + ptr->values[1] = v; | |
1561 | + ptr->value_type[1] = type; | |
1562 | + return true; | |
1563 | + } | |
1564 | + if (*data++ != '-') | |
1565 | + return false; | |
1566 | + type = ccs_parse_ulong(&v, &data); | |
1567 | + if (type == CCS_VALUE_TYPE_INVALID || *data || ptr->values[0] > v) | |
1568 | + return false; | |
1569 | + ptr->values[1] = v; | |
1570 | + ptr->value_type[1] = type; | |
1571 | + return true; | |
1572 | +} | |
1573 | + | |
1574 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
1575 | + | |
1576 | +/** | |
1577 | + * ccs_parse_ipaddr_union - Parse an IP address. | |
1578 | + * | |
1579 | + * @param: Pointer to "struct ccs_acl_param". | |
1580 | + * @ptr: Pointer to "struct ccs_ipaddr_union". | |
1581 | + * | |
1582 | + * Returns true on success, false otherwise. | |
1583 | + */ | |
1584 | +static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param, | |
1585 | + struct ccs_ipaddr_union *ptr) | |
1586 | +{ | |
1587 | + u8 * const min = ptr->ip[0].in6_u.u6_addr8; | |
1588 | + u8 * const max = ptr->ip[1].in6_u.u6_addr8; | |
1589 | + char *address = ccs_read_token(param); | |
1590 | + const char *end; | |
1591 | + if (!strchr(address, ':') && | |
1592 | + ccs_in4_pton(address, -1, min, '-', &end) > 0) { | |
1593 | + ptr->is_ipv6 = false; | |
1594 | + if (!*end) | |
1595 | + ptr->ip[1].s6_addr32[0] = ptr->ip[0].s6_addr32[0]; | |
1596 | + else if (*end++ != '-' || | |
1597 | + ccs_in4_pton(end, -1, max, '\0', &end) <= 0 || *end) | |
1598 | + return false; | |
1599 | + return true; | |
1600 | + } | |
1601 | + if (ccs_in6_pton(address, -1, min, '-', &end) > 0) { | |
1602 | + ptr->is_ipv6 = true; | |
1603 | + if (!*end) | |
1604 | + memmove(max, min, sizeof(u16) * 8); | |
1605 | + else if (*end++ != '-' || | |
1606 | + ccs_in6_pton(end, -1, max, '\0', &end) <= 0 || *end) | |
1607 | + return false; | |
1608 | + return true; | |
1609 | + } | |
1610 | + return false; | |
1611 | +} | |
1612 | + | |
1613 | +#endif | |
1614 | + | |
1615 | +/** | |
1616 | + * ccs_get_dqword - ccs_get_name() for a quoted string. | |
1617 | + * | |
1618 | + * @start: String to save. | |
1619 | + * | |
1620 | + * Returns pointer to "struct ccs_path_info" on success, NULL otherwise. | |
1621 | + */ | |
1622 | +static const struct ccs_path_info *ccs_get_dqword(char *start) | |
1623 | +{ | |
1624 | + char *cp = start + strlen(start) - 1; | |
1625 | + if (cp == start || *start++ != '"' || *cp != '"') | |
1626 | + return NULL; | |
1627 | + *cp = '\0'; | |
1628 | + if (*start && !ccs_correct_word(start)) | |
1629 | + return NULL; | |
1630 | + return ccs_get_name(start); | |
1631 | +} | |
1632 | + | |
1633 | +/** | |
1634 | + * ccs_parse_name_union_quoted - Parse a quoted word. | |
1635 | + * | |
1636 | + * @param: Pointer to "struct ccs_acl_param". | |
1637 | + * @ptr: Pointer to "struct ccs_name_union". | |
1638 | + * | |
1639 | + * Returns true on success, false otherwise. | |
1640 | + */ | |
1641 | +static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param, | |
1642 | + struct ccs_name_union *ptr) | |
1643 | +{ | |
1644 | + char *filename = param->data; | |
1645 | + if (*filename == '@') | |
1646 | + return ccs_parse_name_union(param, ptr); | |
1647 | + ptr->filename = ccs_get_dqword(filename); | |
1648 | + return ptr->filename != NULL; | |
1649 | +} | |
1650 | + | |
1651 | +/** | |
1652 | + * ccs_parse_argv - Parse an argv[] condition part. | |
1653 | + * | |
1654 | + * @left: Lefthand value. | |
1655 | + * @right: Righthand value. | |
1656 | + * @argv: Pointer to "struct ccs_argv". | |
1657 | + * | |
1658 | + * Returns true on success, false otherwise. | |
1659 | + */ | |
1660 | +static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv) | |
1661 | +{ | |
1662 | + if (ccs_parse_ulong(&argv->index, &left) != CCS_VALUE_TYPE_DECIMAL || | |
1663 | + *left++ != ']' || *left) | |
1664 | + return false; | |
1665 | + argv->value = ccs_get_dqword(right); | |
1666 | + return argv->value != NULL; | |
1667 | +} | |
1668 | + | |
1669 | +/** | |
1670 | + * ccs_parse_envp - Parse an envp[] condition part. | |
1671 | + * | |
1672 | + * @left: Lefthand value. | |
1673 | + * @right: Righthand value. | |
1674 | + * @envp: Pointer to "struct ccs_envp". | |
1675 | + * | |
1676 | + * Returns true on success, false otherwise. | |
1677 | + */ | |
1678 | +static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp) | |
1679 | +{ | |
1680 | + const struct ccs_path_info *name; | |
1681 | + const struct ccs_path_info *value; | |
1682 | + char *cp = left + strlen(left) - 1; | |
1683 | + if (*cp-- != ']' || *cp != '"') | |
1684 | + goto out; | |
1685 | + *cp = '\0'; | |
1686 | + if (!ccs_correct_word(left)) | |
1687 | + goto out; | |
1688 | + name = ccs_get_name(left); | |
1689 | + if (!name) | |
1690 | + goto out; | |
1691 | + if (!strcmp(right, "NULL")) { | |
1692 | + value = NULL; | |
1693 | + } else { | |
1694 | + value = ccs_get_dqword(right); | |
1695 | + if (!value) { | |
1696 | + ccs_put_name(name); | |
1697 | + goto out; | |
1698 | + } | |
1699 | + } | |
1700 | + envp->name = name; | |
1701 | + envp->value = value; | |
1702 | + return true; | |
1703 | +out: | |
1704 | + return false; | |
1705 | +} | |
1706 | + | |
1707 | +/** | |
1708 | + * ccs_same_condition - Check for duplicated "struct ccs_condition" entry. | |
1709 | + * | |
1710 | + * @a: Pointer to "struct ccs_condition". | |
1711 | + * @b: Pointer to "struct ccs_condition". | |
1712 | + * | |
1713 | + * Returns true if @a == @b, false otherwise. | |
1714 | + */ | |
1715 | +static bool ccs_same_condition(const struct ccs_condition *a, | |
1716 | + const struct ccs_condition *b) | |
1717 | +{ | |
1718 | + return a->size == b->size && a->condc == b->condc && | |
1719 | + a->numbers_count == b->numbers_count && | |
1720 | + a->names_count == b->names_count && | |
1721 | + a->argc == b->argc && a->envc == b->envc && | |
1722 | + a->grant_log == b->grant_log && | |
1723 | + a->exec_transit == b->exec_transit && a->transit == b->transit | |
1724 | + && !memcmp(a + 1, b + 1, a->size - sizeof(*a)); | |
1725 | +} | |
1726 | + | |
1727 | +/** | |
1728 | + * ccs_condition_type - Get condition type. | |
1729 | + * | |
1730 | + * @word: Keyword string. | |
1731 | + * | |
1732 | + * Returns one of values in "enum ccs_conditions_index" on success, | |
1733 | + * CCS_MAX_CONDITION_KEYWORD otherwise. | |
1734 | + */ | |
1735 | +static u8 ccs_condition_type(const char *word) | |
1736 | +{ | |
1737 | + u8 i; | |
1738 | + for (i = 0; i < CCS_MAX_CONDITION_KEYWORD; i++) { | |
1739 | + if (!strcmp(word, ccs_condition_keyword[i])) | |
1740 | + break; | |
1741 | + } | |
1742 | + return i; | |
1743 | +} | |
1744 | + | |
1745 | +/** | |
1746 | + * ccs_commit_condition - Commit "struct ccs_condition". | |
1747 | + * | |
1748 | + * @entry: Pointer to "struct ccs_condition". | |
1749 | + * | |
1750 | + * Returns pointer to "struct ccs_condition" on success, NULL otherwise. | |
1751 | + * | |
1752 | + * This function merges duplicated entries. This function returns NULL if | |
1753 | + * @entry is not duplicated but memory quota for policy has exceeded. | |
1754 | + */ | |
1755 | +static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry) | |
1756 | +{ | |
1757 | + struct ccs_condition *ptr; | |
1758 | + bool found = false; | |
1759 | + if (mutex_lock_interruptible(&ccs_policy_lock)) { | |
1760 | + dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__); | |
1761 | + ptr = NULL; | |
1762 | + found = true; | |
1763 | + goto out; | |
1764 | + } | |
1765 | + list_for_each_entry(ptr, &ccs_condition_list, head.list) { | |
1766 | + if (!ccs_same_condition(ptr, entry) || | |
1767 | + atomic_read(&ptr->head.users) == CCS_GC_IN_PROGRESS) | |
1768 | + continue; | |
1769 | + /* Same entry found. Share this entry. */ | |
1770 | + atomic_inc(&ptr->head.users); | |
1771 | + found = true; | |
1772 | + break; | |
1773 | + } | |
1774 | + if (!found) { | |
1775 | + if (ccs_memory_ok(entry, entry->size)) { | |
1776 | + atomic_set(&entry->head.users, 1); | |
1777 | + list_add(&entry->head.list, &ccs_condition_list); | |
1778 | + } else { | |
1779 | + found = true; | |
1780 | + ptr = NULL; | |
1781 | + } | |
1782 | + } | |
1783 | + mutex_unlock(&ccs_policy_lock); | |
1784 | +out: | |
1785 | + if (found) { | |
1786 | + ccs_del_condition(&entry->head.list); | |
1787 | + kfree(entry); | |
1788 | + entry = ptr; | |
1789 | + } | |
1790 | + return entry; | |
1791 | +} | |
1792 | + | |
1793 | +/** | |
1794 | + * ccs_correct_path - Check whether the given pathname follows the naming rules. | |
1795 | + * | |
1796 | + * @filename: The pathname to check. | |
1797 | + * | |
1798 | + * Returns true if @filename follows the naming rules, false otherwise. | |
1799 | + */ | |
1800 | +static bool ccs_correct_path(const char *filename) | |
1801 | +{ | |
1802 | + return *filename == '/' && ccs_correct_word(filename); | |
1803 | +} | |
1804 | + | |
1805 | +/** | |
1806 | + * ccs_domain_def - Check whether the given token can be a domainname. | |
1807 | + * | |
1808 | + * @buffer: The token to check. | |
1809 | + * | |
1810 | + * Returns true if @buffer possibly be a domainname, false otherwise. | |
1811 | + */ | |
1812 | +static bool ccs_domain_def(const unsigned char *buffer) | |
1813 | +{ | |
1814 | + const unsigned char *cp; | |
1815 | + int len; | |
1816 | + if (*buffer != '<') | |
1817 | + return false; | |
1818 | + cp = strchr(buffer, ' '); | |
1819 | + if (!cp) | |
1820 | + len = strlen(buffer); | |
1821 | + else | |
1822 | + len = cp - buffer; | |
1823 | + if (buffer[len - 1] != '>' || !ccs_correct_word2(buffer + 1, len - 2)) | |
1824 | + return false; | |
1825 | + return true; | |
1826 | +} | |
1827 | + | |
1828 | +/** | |
1829 | + * ccs_correct_domain - Check whether the given domainname follows the naming rules. | |
1830 | + * | |
1831 | + * @domainname: The domainname to check. | |
1832 | + * | |
1833 | + * Returns true if @domainname follows the naming rules, false otherwise. | |
1834 | + */ | |
1835 | +static bool ccs_correct_domain(const unsigned char *domainname) | |
1836 | +{ | |
1837 | + if (!domainname || !ccs_domain_def(domainname)) | |
1838 | + return false; | |
1839 | + domainname = strchr(domainname, ' '); | |
1840 | + if (!domainname++) | |
1841 | + return true; | |
1842 | + while (1) { | |
1843 | + const unsigned char *cp = strchr(domainname, ' '); | |
1844 | + if (!cp) | |
1845 | + break; | |
1846 | + if (*domainname != '/' || | |
1847 | + !ccs_correct_word2(domainname, cp - domainname)) | |
1848 | + return false; | |
1849 | + domainname = cp + 1; | |
1850 | + } | |
1851 | + return ccs_correct_path(domainname); | |
1852 | +} | |
1853 | + | |
1854 | +/** | |
1855 | + * ccs_normalize_line - Format string. | |
1856 | + * | |
1857 | + * @buffer: The line to normalize. | |
1858 | + * | |
1859 | + * Returns nothing. | |
1860 | + * | |
1861 | + * Leading and trailing whitespaces are removed. | |
1862 | + * Multiple whitespaces are packed into single space. | |
1863 | + */ | |
1864 | +static void ccs_normalize_line(unsigned char *buffer) | |
1865 | +{ | |
1866 | + unsigned char *sp = buffer; | |
1867 | + unsigned char *dp = buffer; | |
1868 | + bool first = true; | |
1869 | + while (*sp && (*sp <= ' ' || *sp >= 127)) | |
1870 | + sp++; | |
1871 | + while (*sp) { | |
1872 | + if (!first) | |
1873 | + *dp++ = ' '; | |
1874 | + first = false; | |
1875 | + while (*sp > ' ' && *sp < 127) | |
1876 | + *dp++ = *sp++; | |
1877 | + while (*sp && (*sp <= ' ' || *sp >= 127)) | |
1878 | + sp++; | |
1879 | + } | |
1880 | + *dp = '\0'; | |
1881 | +} | |
1882 | + | |
1883 | +/** | |
1884 | + * ccs_get_domainname - Read a domainname from a line. | |
1885 | + * | |
1886 | + * @param: Pointer to "struct ccs_acl_param". | |
1887 | + * | |
1888 | + * Returns a domainname on success, NULL otherwise. | |
1889 | + */ | |
1890 | +static const struct ccs_path_info *ccs_get_domainname | |
1891 | +(struct ccs_acl_param *param) | |
1892 | +{ | |
1893 | + char *start = param->data; | |
1894 | + char *pos = start; | |
1895 | + while (*pos) { | |
1896 | + if (*pos++ != ' ' || *pos++ == '/') | |
1897 | + continue; | |
1898 | + pos -= 2; | |
1899 | + *pos++ = '\0'; | |
1900 | + break; | |
1901 | + } | |
1902 | + param->data = pos; | |
1903 | + if (ccs_correct_domain(start)) | |
1904 | + return ccs_get_name(start); | |
1905 | + return NULL; | |
1906 | +} | |
1907 | + | |
1908 | +/** | |
1909 | + * ccs_get_transit_preference - Parse domain transition preference for execve(). | |
1910 | + * | |
1911 | + * @param: Pointer to "struct ccs_acl_param". | |
1912 | + * @e: Pointer to "struct ccs_condition". | |
1913 | + * | |
1914 | + * Returns the condition string part. | |
1915 | + */ | |
1916 | +static char *ccs_get_transit_preference(struct ccs_acl_param *param, | |
1917 | + struct ccs_condition *e) | |
1918 | +{ | |
1919 | + char * const pos = param->data; | |
1920 | + bool flag; | |
1921 | + if (*pos == '<') { | |
1922 | + e->transit = ccs_get_domainname(param); | |
1923 | + goto done; | |
1924 | + } | |
1925 | + { | |
1926 | + char *cp = strchr(pos, ' '); | |
1927 | + if (cp) | |
1928 | + *cp = '\0'; | |
1929 | + flag = ccs_correct_path(pos) || !strcmp(pos, "keep") || | |
1930 | + !strcmp(pos, "initialize") || !strcmp(pos, "reset") || | |
1931 | + !strcmp(pos, "child") || !strcmp(pos, "parent"); | |
1932 | + if (cp) | |
1933 | + *cp = ' '; | |
1934 | + } | |
1935 | + if (!flag) | |
1936 | + return pos; | |
1937 | + e->transit = ccs_get_name(ccs_read_token(param)); | |
1938 | +done: | |
1939 | + if (e->transit) { | |
1940 | + e->exec_transit = true; | |
1941 | + return param->data; | |
1942 | + } | |
1943 | + /* | |
1944 | + * Return a bad read-only condition string that will let | |
1945 | + * ccs_get_condition() return NULL. | |
1946 | + */ | |
1947 | + return "/"; | |
1948 | +} | |
1949 | + | |
1950 | +/** | |
1951 | + * ccs_get_condition - Parse condition part. | |
1952 | + * | |
1953 | + * @param: Pointer to "struct ccs_acl_param". | |
1954 | + * | |
1955 | + * Returns pointer to "struct ccs_condition" on success, NULL otherwise. | |
1956 | + */ | |
1957 | +static struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param) | |
1958 | +{ | |
1959 | + struct ccs_condition *entry = NULL; | |
1960 | + struct ccs_condition_element *condp = NULL; | |
1961 | + struct ccs_number_union *numbers_p = NULL; | |
1962 | + struct ccs_name_union *names_p = NULL; | |
1963 | + struct ccs_argv *argv = NULL; | |
1964 | + struct ccs_envp *envp = NULL; | |
1965 | + struct ccs_condition e = { }; | |
1966 | + char * const start_of_string = ccs_get_transit_preference(param, &e); | |
1967 | + char * const end_of_string = start_of_string + strlen(start_of_string); | |
1968 | + char *pos; | |
1969 | +rerun: | |
1970 | + pos = start_of_string; | |
1971 | + while (1) { | |
1972 | + u8 left = -1; | |
1973 | + u8 right = -1; | |
1974 | + char *left_word = pos; | |
1975 | + char *cp; | |
1976 | + char *right_word; | |
1977 | + bool is_not; | |
1978 | + if (!*left_word) | |
1979 | + break; | |
1980 | + /* | |
1981 | + * Since left-hand condition does not allow use of "path_group" | |
1982 | + * or "number_group" and environment variable's names do not | |
1983 | + * accept '=', it is guaranteed that the original line consists | |
1984 | + * of one or more repetition of $left$operator$right blocks | |
1985 | + * where "$left is free from '=' and ' '" and "$operator is | |
1986 | + * either '=' or '!='" and "$right is free from ' '". | |
1987 | + * Therefore, we can reconstruct the original line at the end | |
1988 | + * of dry run even if we overwrite $operator with '\0'. | |
1989 | + */ | |
1990 | + cp = strchr(pos, ' '); | |
1991 | + if (cp) { | |
1992 | + *cp = '\0'; /* Will restore later. */ | |
1993 | + pos = cp + 1; | |
1994 | + } else { | |
1995 | + pos = ""; | |
1996 | + } | |
1997 | + right_word = strchr(left_word, '='); | |
1998 | + if (!right_word || right_word == left_word) | |
1999 | + goto out; | |
2000 | + is_not = *(right_word - 1) == '!'; | |
2001 | + if (is_not) | |
2002 | + *(right_word++ - 1) = '\0'; /* Will restore later. */ | |
2003 | + else if (*(right_word + 1) != '=') | |
2004 | + *right_word++ = '\0'; /* Will restore later. */ | |
2005 | + else | |
2006 | + goto out; | |
2007 | + dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word, | |
2008 | + is_not ? "!" : "", right_word); | |
2009 | + if (!strcmp(left_word, "grant_log")) { | |
2010 | + if (entry) { | |
2011 | + if (is_not || | |
2012 | + entry->grant_log != CCS_GRANTLOG_AUTO) | |
2013 | + goto out; | |
2014 | + else if (!strcmp(right_word, "yes")) | |
2015 | + entry->grant_log = CCS_GRANTLOG_YES; | |
2016 | + else if (!strcmp(right_word, "no")) | |
2017 | + entry->grant_log = CCS_GRANTLOG_NO; | |
2018 | + else | |
2019 | + goto out; | |
2020 | + } | |
2021 | + continue; | |
2022 | + } | |
2023 | + if (!strcmp(left_word, "auto_domain_transition")) { | |
2024 | + if (entry) { | |
2025 | + if (is_not || entry->transit) | |
2026 | + goto out; | |
2027 | + entry->transit = ccs_get_dqword(right_word); | |
2028 | + if (!entry->transit || | |
2029 | + (entry->transit->name[0] != '/' && | |
2030 | + !ccs_domain_def(entry->transit->name))) | |
2031 | + goto out; | |
2032 | + } | |
2033 | + continue; | |
2034 | + } | |
2035 | + if (!strncmp(left_word, "exec.argv[", 10)) { | |
2036 | + if (!argv) { | |
2037 | + e.argc++; | |
2038 | + e.condc++; | |
2039 | + } else { | |
2040 | + e.argc--; | |
2041 | + e.condc--; | |
2042 | + left = CCS_ARGV_ENTRY; | |
2043 | + argv->is_not = is_not; | |
2044 | + if (!ccs_parse_argv(left_word + 10, | |
2045 | + right_word, argv++)) | |
2046 | + goto out; | |
2047 | + } | |
2048 | + goto store_value; | |
2049 | + } | |
2050 | + if (!strncmp(left_word, "exec.envp[\"", 11)) { | |
2051 | + if (!envp) { | |
2052 | + e.envc++; | |
2053 | + e.condc++; | |
2054 | + } else { | |
2055 | + e.envc--; | |
2056 | + e.condc--; | |
2057 | + left = CCS_ENVP_ENTRY; | |
2058 | + envp->is_not = is_not; | |
2059 | + if (!ccs_parse_envp(left_word + 11, | |
2060 | + right_word, envp++)) | |
2061 | + goto out; | |
2062 | + } | |
2063 | + goto store_value; | |
2064 | + } | |
2065 | + left = ccs_condition_type(left_word); | |
2066 | + dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word, | |
2067 | + left); | |
2068 | + if (left == CCS_MAX_CONDITION_KEYWORD) { | |
2069 | + if (!numbers_p) { | |
2070 | + e.numbers_count++; | |
2071 | + } else { | |
2072 | + e.numbers_count--; | |
2073 | + left = CCS_NUMBER_UNION; | |
2074 | + param->data = left_word; | |
2075 | + if (*left_word == '@' || | |
2076 | + !ccs_parse_number_union(param, | |
2077 | + numbers_p++)) | |
2078 | + goto out; | |
2079 | + } | |
2080 | + } | |
2081 | + if (!condp) | |
2082 | + e.condc++; | |
2083 | + else | |
2084 | + e.condc--; | |
2085 | + if (left == CCS_EXEC_REALPATH || left == CCS_SYMLINK_TARGET) { | |
2086 | + if (!names_p) { | |
2087 | + e.names_count++; | |
2088 | + } else { | |
2089 | + e.names_count--; | |
2090 | + right = CCS_NAME_UNION; | |
2091 | + param->data = right_word; | |
2092 | + if (!ccs_parse_name_union_quoted(param, | |
2093 | + names_p++)) | |
2094 | + goto out; | |
2095 | + } | |
2096 | + goto store_value; | |
2097 | + } | |
2098 | + right = ccs_condition_type(right_word); | |
2099 | + if (right == CCS_MAX_CONDITION_KEYWORD) { | |
2100 | + if (!numbers_p) { | |
2101 | + e.numbers_count++; | |
2102 | + } else { | |
2103 | + e.numbers_count--; | |
2104 | + right = CCS_NUMBER_UNION; | |
2105 | + param->data = right_word; | |
2106 | + if (!ccs_parse_number_union(param, | |
2107 | + numbers_p++)) | |
2108 | + goto out; | |
2109 | + } | |
2110 | + } | |
2111 | +store_value: | |
2112 | + if (!condp) { | |
2113 | + dprintk(KERN_WARNING "%u: dry_run left=%u right=%u " | |
2114 | + "match=%u\n", __LINE__, left, right, !is_not); | |
2115 | + continue; | |
2116 | + } | |
2117 | + condp->left = left; | |
2118 | + condp->right = right; | |
2119 | + condp->equals = !is_not; | |
2120 | + dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n", | |
2121 | + __LINE__, condp->left, condp->right, | |
2122 | + condp->equals); | |
2123 | + condp++; | |
2124 | + } | |
2125 | + dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u\n", | |
2126 | + __LINE__, e.condc, e.numbers_count, e.names_count, e.argc, | |
2127 | + e.envc); | |
2128 | + if (entry) { | |
2129 | + BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc | | |
2130 | + e.condc); | |
2131 | + return ccs_commit_condition(entry); | |
2132 | + } | |
2133 | + e.size = sizeof(*entry) | |
2134 | + + e.condc * sizeof(struct ccs_condition_element) | |
2135 | + + e.numbers_count * sizeof(struct ccs_number_union) | |
2136 | + + e.names_count * sizeof(struct ccs_name_union) | |
2137 | + + e.argc * sizeof(struct ccs_argv) | |
2138 | + + e.envc * sizeof(struct ccs_envp); | |
2139 | + entry = kzalloc(e.size, CCS_GFP_FLAGS); | |
2140 | + if (!entry) | |
2141 | + goto out2; | |
2142 | + *entry = e; | |
2143 | + e.transit = NULL; | |
2144 | + condp = (struct ccs_condition_element *) (entry + 1); | |
2145 | + numbers_p = (struct ccs_number_union *) (condp + e.condc); | |
2146 | + names_p = (struct ccs_name_union *) (numbers_p + e.numbers_count); | |
2147 | + argv = (struct ccs_argv *) (names_p + e.names_count); | |
2148 | + envp = (struct ccs_envp *) (argv + e.argc); | |
2149 | + { | |
2150 | + bool flag = false; | |
2151 | + for (pos = start_of_string; pos < end_of_string; pos++) { | |
2152 | + if (*pos) | |
2153 | + continue; | |
2154 | + if (flag) /* Restore " ". */ | |
2155 | + *pos = ' '; | |
2156 | + else if (*(pos + 1) == '=') /* Restore "!=". */ | |
2157 | + *pos = '!'; | |
2158 | + else /* Restore "=". */ | |
2159 | + *pos = '='; | |
2160 | + flag = !flag; | |
2161 | + } | |
2162 | + } | |
2163 | + goto rerun; | |
2164 | +out: | |
2165 | + dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__); | |
2166 | + if (entry) { | |
2167 | + ccs_del_condition(&entry->head.list); | |
2168 | + kfree(entry); | |
2169 | + } | |
2170 | +out2: | |
2171 | + ccs_put_name(e.transit); | |
2172 | + return NULL; | |
2173 | +} | |
2174 | + | |
2175 | +/** | |
2176 | + * ccs_yesno - Return "yes" or "no". | |
2177 | + * | |
2178 | + * @value: Bool value. | |
2179 | + * | |
2180 | + * Returns "yes" if @value is not 0, "no" otherwise. | |
2181 | + */ | |
2182 | +static const char *ccs_yesno(const unsigned int value) | |
2183 | +{ | |
2184 | + return value ? "yes" : "no"; | |
2185 | +} | |
2186 | + | |
2187 | +/** | |
2188 | + * ccs_addprintf - strncat()-like-snprintf(). | |
2189 | + * | |
2190 | + * @buffer: Buffer to write to. Must be '\0'-terminated. | |
2191 | + * @len: Size of @buffer. | |
2192 | + * @fmt: The printf()'s format string, followed by parameters. | |
2193 | + * | |
2194 | + * Returns nothing. | |
2195 | + */ | |
2196 | +static void ccs_addprintf(char *buffer, int len, const char *fmt, ...) | |
2197 | +{ | |
2198 | + va_list args; | |
2199 | + const int pos = strlen(buffer); | |
2200 | + va_start(args, fmt); | |
2201 | + vsnprintf(buffer + pos, len - pos - 1, fmt, args); | |
2202 | + va_end(args); | |
2203 | +} | |
2204 | + | |
2205 | +/** | |
2206 | + * ccs_flush - Flush queued string to userspace's buffer. | |
2207 | + * | |
2208 | + * @head: Pointer to "struct ccs_io_buffer". | |
2209 | + * | |
2210 | + * Returns true if all data was flushed, false otherwise. | |
2211 | + */ | |
2212 | +static bool ccs_flush(struct ccs_io_buffer *head) | |
2213 | +{ | |
2214 | + while (head->r.w_pos) { | |
2215 | + const char *w = head->r.w[0]; | |
2216 | + size_t len = strlen(w); | |
2217 | + if (len) { | |
2218 | + if (len > head->read_user_buf_avail) | |
2219 | + len = head->read_user_buf_avail; | |
2220 | + if (!len) | |
2221 | + return false; | |
2222 | + if (copy_to_user(head->read_user_buf, w, len)) | |
2223 | + return false; | |
2224 | + head->read_user_buf_avail -= len; | |
2225 | + head->read_user_buf += len; | |
2226 | + w += len; | |
2227 | + } | |
2228 | + head->r.w[0] = w; | |
2229 | + if (*w) | |
2230 | + return false; | |
2231 | + /* Add '\0' for audit logs and query. */ | |
2232 | + if (head->type == CCS_AUDIT || head->type == CCS_QUERY) { | |
2233 | + if (!head->read_user_buf_avail || | |
2234 | + copy_to_user(head->read_user_buf, "", 1)) | |
2235 | + return false; | |
2236 | + head->read_user_buf_avail--; | |
2237 | + head->read_user_buf++; | |
2238 | + } | |
2239 | + head->r.w_pos--; | |
2240 | + for (len = 0; len < head->r.w_pos; len++) | |
2241 | + head->r.w[len] = head->r.w[len + 1]; | |
2242 | + } | |
2243 | + head->r.avail = 0; | |
2244 | + return true; | |
2245 | +} | |
2246 | + | |
2247 | +/** | |
2248 | + * ccs_set_string - Queue string to "struct ccs_io_buffer" structure. | |
2249 | + * | |
2250 | + * @head: Pointer to "struct ccs_io_buffer". | |
2251 | + * @string: String to print. | |
2252 | + * | |
2253 | + * Returns nothing. | |
2254 | + * | |
2255 | + * Note that @string has to be kept valid until @head is kfree()d. | |
2256 | + * This means that char[] allocated on stack memory cannot be passed to | |
2257 | + * this function. Use ccs_io_printf() for char[] allocated on stack memory. | |
2258 | + */ | |
2259 | +static void ccs_set_string(struct ccs_io_buffer *head, const char *string) | |
2260 | +{ | |
2261 | + if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) { | |
2262 | + head->r.w[head->r.w_pos++] = string; | |
2263 | + ccs_flush(head); | |
2264 | + } else | |
2265 | + printk(KERN_WARNING "Too many words in a line.\n"); | |
2266 | +} | |
2267 | + | |
2268 | +/** | |
2269 | + * ccs_io_printf - printf() to "struct ccs_io_buffer" structure. | |
2270 | + * | |
2271 | + * @head: Pointer to "struct ccs_io_buffer". | |
2272 | + * @fmt: The printf()'s format string, followed by parameters. | |
2273 | + * | |
2274 | + * Returns nothing. | |
2275 | + */ | |
2276 | +static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...) | |
2277 | +{ | |
2278 | + va_list args; | |
2279 | + size_t len; | |
2280 | + size_t pos = head->r.avail; | |
2281 | + int size = head->readbuf_size - pos; | |
2282 | + if (size <= 0) | |
2283 | + return; | |
2284 | + va_start(args, fmt); | |
2285 | + len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1; | |
2286 | + va_end(args); | |
2287 | + if (pos + len >= head->readbuf_size) { | |
2288 | + printk(KERN_WARNING "Too many words in a line.\n"); | |
2289 | + return; | |
2290 | + } | |
2291 | + head->r.avail += len; | |
2292 | + ccs_set_string(head, head->read_buf + pos); | |
2293 | +} | |
2294 | + | |
2295 | +/** | |
2296 | + * ccs_set_space - Put a space to "struct ccs_io_buffer" structure. | |
2297 | + * | |
2298 | + * @head: Pointer to "struct ccs_io_buffer". | |
2299 | + * | |
2300 | + * Returns nothing. | |
2301 | + */ | |
2302 | +static void ccs_set_space(struct ccs_io_buffer *head) | |
2303 | +{ | |
2304 | + ccs_set_string(head, " "); | |
2305 | +} | |
2306 | + | |
2307 | +/** | |
2308 | + * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure. | |
2309 | + * | |
2310 | + * @head: Pointer to "struct ccs_io_buffer". | |
2311 | + * | |
2312 | + * Returns true if all data was flushed, false otherwise. | |
2313 | + */ | |
2314 | +static bool ccs_set_lf(struct ccs_io_buffer *head) | |
2315 | +{ | |
2316 | + ccs_set_string(head, "\n"); | |
2317 | + return !head->r.w_pos; | |
2318 | +} | |
2319 | + | |
2320 | +/** | |
2321 | + * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure. | |
2322 | + * | |
2323 | + * @head: Pointer to "struct ccs_io_buffer". | |
2324 | + * | |
2325 | + * Returns nothing. | |
2326 | + */ | |
2327 | +static void ccs_set_slash(struct ccs_io_buffer *head) | |
2328 | +{ | |
2329 | + ccs_set_string(head, "/"); | |
2330 | +} | |
2331 | + | |
2332 | +/** | |
2333 | + * ccs_init_policy_namespace - Initialize namespace. | |
2334 | + * | |
2335 | + * @ns: Pointer to "struct ccs_policy_namespace". | |
2336 | + * | |
2337 | + * Returns nothing. | |
2338 | + */ | |
2339 | +static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns) | |
2340 | +{ | |
2341 | + unsigned int idx; | |
2342 | + for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++) | |
2343 | + INIT_LIST_HEAD(&ns->acl_group[idx]); | |
2344 | + for (idx = 0; idx < CCS_MAX_GROUP; idx++) | |
2345 | + INIT_LIST_HEAD(&ns->group_list[idx]); | |
2346 | + for (idx = 0; idx < CCS_MAX_POLICY; idx++) | |
2347 | + INIT_LIST_HEAD(&ns->policy_list[idx]); | |
2348 | + ns->profile_version = 20150505; | |
2349 | + ccs_namespace_enabled = !list_empty(&ccs_namespace_list); | |
2350 | + list_add_tail_rcu(&ns->namespace_list, &ccs_namespace_list); | |
2351 | +} | |
2352 | + | |
2353 | +/** | |
2354 | + * ccs_print_namespace - Print namespace header. | |
2355 | + * | |
2356 | + * @head: Pointer to "struct ccs_io_buffer". | |
2357 | + * | |
2358 | + * Returns nothing. | |
2359 | + */ | |
2360 | +static void ccs_print_namespace(struct ccs_io_buffer *head) | |
2361 | +{ | |
2362 | + if (!ccs_namespace_enabled) | |
2363 | + return; | |
2364 | + ccs_set_string(head, | |
2365 | + container_of(head->r.ns, struct ccs_policy_namespace, | |
2366 | + namespace_list)->name); | |
2367 | + ccs_set_space(head); | |
2368 | +} | |
2369 | + | |
2370 | +/** | |
2371 | + * ccs_assign_profile - Create a new profile. | |
2372 | + * | |
2373 | + * @ns: Pointer to "struct ccs_policy_namespace". | |
2374 | + * @profile: Profile number to create. | |
2375 | + * | |
2376 | + * Returns pointer to "struct ccs_profile" on success, NULL otherwise. | |
2377 | + */ | |
2378 | +static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns, | |
2379 | + const unsigned int profile) | |
2380 | +{ | |
2381 | + struct ccs_profile *ptr; | |
2382 | + struct ccs_profile *entry; | |
2383 | + if (profile >= CCS_MAX_PROFILES) | |
2384 | + return NULL; | |
2385 | + ptr = ns->profile_ptr[profile]; | |
2386 | + if (ptr) | |
2387 | + return ptr; | |
2388 | + entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS); | |
2389 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
2390 | + goto out; | |
2391 | + ptr = ns->profile_ptr[profile]; | |
2392 | + if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) { | |
2393 | + ptr = entry; | |
2394 | + ptr->default_config = CCS_CONFIG_DISABLED | | |
2395 | + CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG; | |
2396 | + memset(ptr->config, CCS_CONFIG_USE_DEFAULT, | |
2397 | + sizeof(ptr->config)); | |
2398 | + ptr->pref[CCS_PREF_MAX_AUDIT_LOG] = | |
2399 | + CONFIG_CCSECURITY_MAX_AUDIT_LOG; | |
2400 | + ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] = | |
2401 | + CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY; | |
2402 | + mb(); /* Avoid out-of-order execution. */ | |
2403 | + ns->profile_ptr[profile] = ptr; | |
2404 | + entry = NULL; | |
2405 | + } | |
2406 | + mutex_unlock(&ccs_policy_lock); | |
2407 | +out: | |
2408 | + kfree(entry); | |
2409 | + return ptr; | |
2410 | +} | |
2411 | + | |
2412 | +/** | |
2413 | + * ccs_check_profile - Check all profiles currently assigned to domains are defined. | |
2414 | + * | |
2415 | + * Returns nothing. | |
2416 | + */ | |
2417 | +static void ccs_check_profile(void) | |
2418 | +{ | |
2419 | + struct ccs_domain_info *domain; | |
2420 | + const int idx = ccs_read_lock(); | |
2421 | + ccs_policy_loaded = true; | |
2422 | + printk(KERN_INFO "CCSecurity: 1.8.5+ 2017/02/20\n"); | |
2423 | + list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { | |
2424 | + const u8 profile = domain->profile; | |
2425 | + struct ccs_policy_namespace *ns = domain->ns; | |
2426 | + if (ns->profile_version == 20100903) { | |
2427 | + static bool done; | |
2428 | + if (!done) | |
2429 | + printk(KERN_INFO "Converting profile version " | |
2430 | + "from %u to %u.\n", 20100903, 20150505); | |
2431 | + done = true; | |
2432 | + ns->profile_version = 20150505; | |
2433 | + } | |
2434 | + if (ns->profile_version != 20150505) | |
2435 | + printk(KERN_ERR | |
2436 | + "Profile version %u is not supported.\n", | |
2437 | + ns->profile_version); | |
2438 | + else if (!ns->profile_ptr[profile]) | |
2439 | + printk(KERN_ERR | |
2440 | + "Profile %u (used by '%s') is not defined.\n", | |
2441 | + profile, domain->domainname->name); | |
2442 | + else | |
2443 | + continue; | |
2444 | + printk(KERN_ERR | |
2445 | + "Userland tools for TOMOYO 1.8 must be installed and " | |
2446 | + "policy must be initialized.\n"); | |
2447 | + printk(KERN_ERR "Please see http://tomoyo.osdn.jp/1.8/ " | |
2448 | + "for more information.\n"); | |
2449 | + panic("STOP!"); | |
2450 | + } | |
2451 | + ccs_read_unlock(idx); | |
2452 | + printk(KERN_INFO "Mandatory Access Control activated.\n"); | |
2453 | +} | |
2454 | + | |
2455 | +/** | |
2456 | + * ccs_profile - Find a profile. | |
2457 | + * | |
2458 | + * @profile: Profile number to find. | |
2459 | + * | |
2460 | + * Returns pointer to "struct ccs_profile". | |
2461 | + */ | |
2462 | +static struct ccs_profile *ccs_profile(const u8 profile) | |
2463 | +{ | |
2464 | + static struct ccs_profile ccs_null_profile; | |
2465 | + struct ccs_profile *ptr = ccs_current_namespace()-> | |
2466 | + profile_ptr[profile]; | |
2467 | + if (!ptr) | |
2468 | + ptr = &ccs_null_profile; | |
2469 | + return ptr; | |
2470 | +} | |
2471 | + | |
2472 | +/** | |
2473 | + * ccs_get_config - Get config for specified profile's specified functionality. | |
2474 | + * | |
2475 | + * @profile: Profile number. | |
2476 | + * @index: Index number of functionality. | |
2477 | + * | |
2478 | + * Returns config. | |
2479 | + * | |
2480 | + * First, check for CONFIG::category::functionality. | |
2481 | + * If CONFIG::category::functionality is set to use default, then check | |
2482 | + * CONFIG::category. If CONFIG::category is set to use default, then use | |
2483 | + * CONFIG. CONFIG cannot be set to use default. | |
2484 | + */ | |
2485 | +u8 ccs_get_config(const u8 profile, const u8 index) | |
2486 | +{ | |
2487 | + u8 config; | |
2488 | + const struct ccs_profile *p; | |
2489 | + if (!ccs_policy_loaded) | |
2490 | + return CCS_CONFIG_DISABLED; | |
2491 | + p = ccs_profile(profile); | |
2492 | + config = p->config[index]; | |
2493 | + if (config == CCS_CONFIG_USE_DEFAULT) | |
2494 | + config = p->config[ccs_index2category[index] | |
2495 | + + CCS_MAX_MAC_INDEX]; | |
2496 | + if (config == CCS_CONFIG_USE_DEFAULT) | |
2497 | + config = p->default_config; | |
2498 | + return config; | |
2499 | +} | |
2500 | + | |
2501 | +/** | |
2502 | + * ccs_find_yesno - Find values for specified keyword. | |
2503 | + * | |
2504 | + * @string: String to check. | |
2505 | + * @find: Name of keyword. | |
2506 | + * | |
2507 | + * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise. | |
2508 | + */ | |
2509 | +static s8 ccs_find_yesno(const char *string, const char *find) | |
2510 | +{ | |
2511 | + const char *cp = strstr(string, find); | |
2512 | + if (cp) { | |
2513 | + cp += strlen(find); | |
2514 | + if (!strncmp(cp, "=yes", 4)) | |
2515 | + return 1; | |
2516 | + else if (!strncmp(cp, "=no", 3)) | |
2517 | + return 0; | |
2518 | + } | |
2519 | + return -1; | |
2520 | +} | |
2521 | + | |
2522 | +/** | |
2523 | + * ccs_set_uint - Set value for specified preference. | |
2524 | + * | |
2525 | + * @i: Pointer to "unsigned int". | |
2526 | + * @string: String to check. | |
2527 | + * @find: Name of keyword. | |
2528 | + * | |
2529 | + * Returns nothing. | |
2530 | + */ | |
2531 | +static void ccs_set_uint(unsigned int *i, const char *string, const char *find) | |
2532 | +{ | |
2533 | + const char *cp = strstr(string, find); | |
2534 | + if (cp) | |
2535 | + sscanf(cp + strlen(find), "=%u", i); | |
2536 | +} | |
2537 | + | |
2538 | +/** | |
2539 | + * ccs_str_starts - Check whether the given string starts with the given keyword. | |
2540 | + * | |
2541 | + * @src: Pointer to pointer to the string. | |
2542 | + * @find: Pointer to the keyword. | |
2543 | + * | |
2544 | + * Returns true if @src starts with @find, false otherwise. | |
2545 | + * | |
2546 | + * The @src is updated to point the first character after the @find | |
2547 | + * if @src starts with @find. | |
2548 | + */ | |
2549 | +static bool ccs_str_starts(char **src, const char *find) | |
2550 | +{ | |
2551 | + const int len = strlen(find); | |
2552 | + char *tmp = *src; | |
2553 | + if (strncmp(tmp, find, len)) | |
2554 | + return false; | |
2555 | + tmp += len; | |
2556 | + *src = tmp; | |
2557 | + return true; | |
2558 | +} | |
2559 | + | |
2560 | +/** | |
2561 | + * ccs_print_group - Print group's name. | |
2562 | + * | |
2563 | + * @head: Pointer to "struct ccs_io_buffer". | |
2564 | + * @group: Pointer to "struct ccsgroup". Maybe NULL. | |
2565 | + * | |
2566 | + * Returns true if @group is not NULL. false otherwise. | |
2567 | + */ | |
2568 | +static bool ccs_print_group(struct ccs_io_buffer *head, | |
2569 | + const struct ccs_group *group) | |
2570 | +{ | |
2571 | + if (group) { | |
2572 | + ccs_set_string(head, "@"); | |
2573 | + ccs_set_string(head, group->group_name->name); | |
2574 | + return true; | |
2575 | + } | |
2576 | + return false; | |
2577 | +} | |
2578 | + | |
2579 | +/** | |
2580 | + * ccs_set_mode - Set mode for specified profile. | |
2581 | + * | |
2582 | + * @name: Name of functionality. | |
2583 | + * @value: Mode for @name. | |
2584 | + * @profile: Pointer to "struct ccs_profile". | |
2585 | + * | |
2586 | + * Returns 0 on success, negative value otherwise. | |
2587 | + */ | |
2588 | +static int ccs_set_mode(char *name, const char *value, | |
2589 | + struct ccs_profile *profile) | |
2590 | +{ | |
2591 | + u8 i; | |
2592 | + u8 config; | |
2593 | + if (!strcmp(name, "CONFIG")) { | |
2594 | + i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX; | |
2595 | + config = profile->default_config; | |
2596 | + } else if (ccs_str_starts(&name, "CONFIG::")) { | |
2597 | + config = 0; | |
2598 | + for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX; | |
2599 | + i++) { | |
2600 | + int len = 0; | |
2601 | + if (i < CCS_MAX_MAC_INDEX) { | |
2602 | + const u8 c = ccs_index2category[i]; | |
2603 | + const char *category = | |
2604 | + ccs_category_keywords[c]; | |
2605 | + len = strlen(category); | |
2606 | + if (strncmp(name, category, len) || | |
2607 | + name[len++] != ':' || name[len++] != ':') | |
2608 | + continue; | |
2609 | + } | |
2610 | + if (strcmp(name + len, ccs_mac_keywords[i])) | |
2611 | + continue; | |
2612 | + config = profile->config[i]; | |
2613 | + break; | |
2614 | + } | |
2615 | + if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX) | |
2616 | + return -EINVAL; | |
2617 | + } else { | |
2618 | + return -EINVAL; | |
2619 | + } | |
2620 | + if (strstr(value, "use_default")) { | |
2621 | + config = CCS_CONFIG_USE_DEFAULT; | |
2622 | + } else { | |
2623 | + u8 mode; | |
2624 | + for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++) | |
2625 | + if (strstr(value, ccs_mode[mode])) | |
2626 | + /* | |
2627 | + * Update lower 3 bits in order to distinguish | |
2628 | + * 'config' from 'CCS_CONFIG_USE_DEAFULT'. | |
2629 | + */ | |
2630 | + config = (config & ~7) | mode; | |
2631 | + if (config != CCS_CONFIG_USE_DEFAULT) { | |
2632 | + switch (ccs_find_yesno(value, "grant_log")) { | |
2633 | + case 1: | |
2634 | + config |= CCS_CONFIG_WANT_GRANT_LOG; | |
2635 | + break; | |
2636 | + case 0: | |
2637 | + config &= ~CCS_CONFIG_WANT_GRANT_LOG; | |
2638 | + break; | |
2639 | + } | |
2640 | + switch (ccs_find_yesno(value, "reject_log")) { | |
2641 | + case 1: | |
2642 | + config |= CCS_CONFIG_WANT_REJECT_LOG; | |
2643 | + break; | |
2644 | + case 0: | |
2645 | + config &= ~CCS_CONFIG_WANT_REJECT_LOG; | |
2646 | + break; | |
2647 | + } | |
2648 | + } | |
2649 | + } | |
2650 | + if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX) | |
2651 | + profile->config[i] = config; | |
2652 | + else if (config != CCS_CONFIG_USE_DEFAULT) | |
2653 | + profile->default_config = config; | |
2654 | + return 0; | |
2655 | +} | |
2656 | + | |
2657 | +/** | |
2658 | + * ccs_write_profile - Write profile table. | |
2659 | + * | |
2660 | + * @head: Pointer to "struct ccs_io_buffer". | |
2661 | + * | |
2662 | + * Returns 0 on success, negative value otherwise. | |
2663 | + */ | |
2664 | +static int ccs_write_profile(struct ccs_io_buffer *head) | |
2665 | +{ | |
2666 | + char *data = head->write_buf; | |
2667 | + unsigned int i; | |
2668 | + char *cp; | |
2669 | + struct ccs_profile *profile; | |
2670 | + if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version) | |
2671 | + == 1) | |
2672 | + return 0; | |
2673 | + i = simple_strtoul(data, &cp, 10); | |
2674 | + if (*cp != '-') | |
2675 | + return -EINVAL; | |
2676 | + data = cp + 1; | |
2677 | + profile = ccs_assign_profile(head->w.ns, i); | |
2678 | + if (!profile) | |
2679 | + return -EINVAL; | |
2680 | + cp = strchr(data, '='); | |
2681 | + if (!cp) | |
2682 | + return -EINVAL; | |
2683 | + *cp++ = '\0'; | |
2684 | + if (!strcmp(data, "COMMENT")) { | |
2685 | + static DEFINE_SPINLOCK(lock); | |
2686 | + const struct ccs_path_info *new_comment = ccs_get_name(cp); | |
2687 | + const struct ccs_path_info *old_comment; | |
2688 | + if (!new_comment) | |
2689 | + return -ENOMEM; | |
2690 | + spin_lock(&lock); | |
2691 | + old_comment = profile->comment; | |
2692 | + profile->comment = new_comment; | |
2693 | + spin_unlock(&lock); | |
2694 | + ccs_put_name(old_comment); | |
2695 | + return 0; | |
2696 | + } | |
2697 | + if (!strcmp(data, "PREFERENCE")) { | |
2698 | + for (i = 0; i < CCS_MAX_PREF; i++) | |
2699 | + ccs_set_uint(&profile->pref[i], cp, | |
2700 | + ccs_pref_keywords[i]); | |
2701 | + return 0; | |
2702 | + } | |
2703 | + return ccs_set_mode(data, cp, profile); | |
2704 | +} | |
2705 | + | |
2706 | +/** | |
2707 | + * ccs_print_config - Print mode for specified functionality. | |
2708 | + * | |
2709 | + * @head: Pointer to "struct ccs_io_buffer". | |
2710 | + * @config: Mode for that functionality. | |
2711 | + * | |
2712 | + * Returns nothing. | |
2713 | + * | |
2714 | + * Caller prints functionality's name. | |
2715 | + */ | |
2716 | +static void ccs_print_config(struct ccs_io_buffer *head, const u8 config) | |
2717 | +{ | |
2718 | + ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n", | |
2719 | + ccs_mode[config & 3], | |
2720 | + ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG), | |
2721 | + ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG)); | |
2722 | +} | |
2723 | + | |
2724 | +/** | |
2725 | + * ccs_read_profile - Read profile table. | |
2726 | + * | |
2727 | + * @head: Pointer to "struct ccs_io_buffer". | |
2728 | + * | |
2729 | + * Returns nothing. | |
2730 | + */ | |
2731 | +static void ccs_read_profile(struct ccs_io_buffer *head) | |
2732 | +{ | |
2733 | + u8 index; | |
2734 | + struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns), | |
2735 | + namespace_list); | |
2736 | + const struct ccs_profile *profile; | |
2737 | + if (head->r.eof) | |
2738 | + return; | |
2739 | +next: | |
2740 | + index = head->r.index; | |
2741 | + profile = ns->profile_ptr[index]; | |
2742 | + switch (head->r.step) { | |
2743 | + case 0: | |
2744 | + ccs_print_namespace(head); | |
2745 | + ccs_io_printf(head, "PROFILE_VERSION=%u\n", | |
2746 | + ns->profile_version); | |
2747 | + head->r.step++; | |
2748 | + break; | |
2749 | + case 1: | |
2750 | + for ( ; head->r.index < CCS_MAX_PROFILES; head->r.index++) | |
2751 | + if (ns->profile_ptr[head->r.index]) | |
2752 | + break; | |
2753 | + if (head->r.index == CCS_MAX_PROFILES) { | |
2754 | + head->r.eof = true; | |
2755 | + return; | |
2756 | + } | |
2757 | + head->r.step++; | |
2758 | + break; | |
2759 | + case 2: | |
2760 | + { | |
2761 | + u8 i; | |
2762 | + const struct ccs_path_info *comment = profile->comment; | |
2763 | + ccs_print_namespace(head); | |
2764 | + ccs_io_printf(head, "%u-COMMENT=", index); | |
2765 | + ccs_set_string(head, comment ? comment->name : ""); | |
2766 | + ccs_set_lf(head); | |
2767 | + ccs_print_namespace(head); | |
2768 | + ccs_io_printf(head, "%u-PREFERENCE={ ", index); | |
2769 | + for (i = 0; i < CCS_MAX_PREF; i++) | |
2770 | + ccs_io_printf(head, "%s=%u ", | |
2771 | + ccs_pref_keywords[i], | |
2772 | + profile->pref[i]); | |
2773 | + ccs_set_string(head, "}\n"); | |
2774 | + head->r.step++; | |
2775 | + } | |
2776 | + break; | |
2777 | + case 3: | |
2778 | + { | |
2779 | + ccs_print_namespace(head); | |
2780 | + ccs_io_printf(head, "%u-%s", index, "CONFIG"); | |
2781 | + ccs_print_config(head, profile->default_config); | |
2782 | + head->r.bit = 0; | |
2783 | + head->r.step++; | |
2784 | + } | |
2785 | + break; | |
2786 | + case 4: | |
2787 | + for ( ; head->r.bit < CCS_MAX_MAC_INDEX | |
2788 | + + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) { | |
2789 | + const u8 i = head->r.bit; | |
2790 | + const u8 config = profile->config[i]; | |
2791 | + if (config == CCS_CONFIG_USE_DEFAULT) | |
2792 | + continue; | |
2793 | + ccs_print_namespace(head); | |
2794 | + if (i < CCS_MAX_MAC_INDEX) | |
2795 | + ccs_io_printf(head, "%u-CONFIG::%s::%s", index, | |
2796 | + ccs_category_keywords | |
2797 | + [ccs_index2category[i]], | |
2798 | + ccs_mac_keywords[i]); | |
2799 | + else | |
2800 | + ccs_io_printf(head, "%u-CONFIG::%s", index, | |
2801 | + ccs_mac_keywords[i]); | |
2802 | + ccs_print_config(head, config); | |
2803 | + head->r.bit++; | |
2804 | + break; | |
2805 | + } | |
2806 | + if (head->r.bit == CCS_MAX_MAC_INDEX | |
2807 | + + CCS_MAX_MAC_CATEGORY_INDEX) { | |
2808 | + head->r.index++; | |
2809 | + head->r.step = 1; | |
2810 | + } | |
2811 | + break; | |
2812 | + } | |
2813 | + if (ccs_flush(head)) | |
2814 | + goto next; | |
2815 | +} | |
2816 | + | |
2817 | +/** | |
2818 | + * ccs_update_policy - Update an entry for exception policy. | |
2819 | + * | |
2820 | + * @size: Size of new entry in bytes. | |
2821 | + * @param: Pointer to "struct ccs_acl_param". | |
2822 | + * | |
2823 | + * Returns 0 on success, negative value otherwise. | |
2824 | + * | |
2825 | + * Caller holds ccs_read_lock(). | |
2826 | + */ | |
2827 | +static int ccs_update_policy(const int size, struct ccs_acl_param *param) | |
2828 | +{ | |
2829 | + struct ccs_acl_head *new_entry = ¶m->e.acl_head; | |
2830 | + int error = param->is_delete ? -ENOENT : -ENOMEM; | |
2831 | + struct ccs_acl_head *entry; | |
2832 | + struct list_head *list = param->list; | |
2833 | + BUG_ON(size < sizeof(*entry)); | |
2834 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
2835 | + return -ENOMEM; | |
2836 | + list_for_each_entry_srcu(entry, list, list, &ccs_ss) { | |
2837 | + if (entry->is_deleted == CCS_GC_IN_PROGRESS) | |
2838 | + continue; | |
2839 | + if (memcmp(entry + 1, new_entry + 1, size - sizeof(*entry))) | |
2840 | + continue; | |
2841 | + entry->is_deleted = param->is_delete; | |
2842 | + error = 0; | |
2843 | + break; | |
2844 | + } | |
2845 | + if (error && !param->is_delete) { | |
2846 | + entry = ccs_commit_ok(new_entry, size); | |
2847 | + if (entry) { | |
2848 | + list_add_tail_rcu(&entry->list, list); | |
2849 | + error = 0; | |
2850 | + } | |
2851 | + } | |
2852 | + mutex_unlock(&ccs_policy_lock); | |
2853 | + return error; | |
2854 | +} | |
2855 | + | |
2856 | +/** | |
2857 | + * ccs_update_manager_entry - Add a manager entry. | |
2858 | + * | |
2859 | + * @manager: The path to manager or the domainnamme. | |
2860 | + * @is_delete: True if it is a delete request. | |
2861 | + * | |
2862 | + * Returns 0 on success, negative value otherwise. | |
2863 | + */ | |
2864 | +static int ccs_update_manager_entry(const char *manager, | |
2865 | + const bool is_delete) | |
2866 | +{ | |
2867 | + struct ccs_acl_param param = { | |
2868 | + /* .ns = &ccs_kernel_namespace, */ | |
2869 | + .is_delete = is_delete, | |
2870 | + .list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER], | |
2871 | + }; | |
2872 | + struct ccs_manager *e = ¶m.e.manager; | |
2873 | + int error = is_delete ? -ENOENT : -ENOMEM; | |
2874 | + /* Forced zero clear for using memcmp() at ccs_update_policy(). */ | |
2875 | + memset(¶m.e, 0, sizeof(param.e)); | |
2876 | + if (!ccs_correct_domain(manager) && !ccs_correct_word(manager)) | |
2877 | + return -EINVAL; | |
2878 | + e->manager = ccs_get_name(manager); | |
2879 | + if (e->manager) { | |
2880 | + error = ccs_update_policy(sizeof(*e), ¶m); | |
2881 | + ccs_put_name(e->manager); | |
2882 | + } | |
2883 | + return error; | |
2884 | +} | |
2885 | + | |
2886 | +/** | |
2887 | + * ccs_write_manager - Write manager policy. | |
2888 | + * | |
2889 | + * @head: Pointer to "struct ccs_io_buffer". | |
2890 | + * | |
2891 | + * Returns 0 on success, negative value otherwise. | |
2892 | + */ | |
2893 | +static int ccs_write_manager(struct ccs_io_buffer *head) | |
2894 | +{ | |
2895 | + const char *data = head->write_buf; | |
2896 | + if (!strcmp(data, "manage_by_non_root")) { | |
2897 | + ccs_manage_by_non_root = !head->w.is_delete; | |
2898 | + return 0; | |
2899 | + } | |
2900 | + return ccs_update_manager_entry(data, head->w.is_delete); | |
2901 | +} | |
2902 | + | |
2903 | +/** | |
2904 | + * ccs_read_manager - Read manager policy. | |
2905 | + * | |
2906 | + * @head: Pointer to "struct ccs_io_buffer". | |
2907 | + * | |
2908 | + * Returns nothing. | |
2909 | + * | |
2910 | + * Caller holds ccs_read_lock(). | |
2911 | + */ | |
2912 | +static void ccs_read_manager(struct ccs_io_buffer *head) | |
2913 | +{ | |
2914 | + if (head->r.eof) | |
2915 | + return; | |
2916 | + list_for_each_cookie(head->r.acl, &ccs_kernel_namespace. | |
2917 | + policy_list[CCS_ID_MANAGER]) { | |
2918 | + struct ccs_manager *ptr = | |
2919 | + list_entry(head->r.acl, typeof(*ptr), head.list); | |
2920 | + if (ptr->head.is_deleted) | |
2921 | + continue; | |
2922 | + if (!ccs_flush(head)) | |
2923 | + return; | |
2924 | + ccs_set_string(head, ptr->manager->name); | |
2925 | + ccs_set_lf(head); | |
2926 | + } | |
2927 | + head->r.eof = true; | |
2928 | +} | |
2929 | + | |
2930 | +/** | |
2931 | + * ccs_manager - Check whether the current process is a policy manager. | |
2932 | + * | |
2933 | + * Returns true if the current process is permitted to modify policy | |
2934 | + * via /proc/ccs/ interface. | |
2935 | + * | |
2936 | + * Caller holds ccs_read_lock(). | |
2937 | + */ | |
2938 | +static bool ccs_manager(void) | |
2939 | +{ | |
2940 | + struct ccs_manager *ptr; | |
2941 | + struct ccs_path_info exe; | |
2942 | + struct ccs_security *task = ccs_current_security(); | |
2943 | + const struct ccs_path_info *domainname | |
2944 | + = ccs_current_domain()->domainname; | |
2945 | + bool found = false; | |
2946 | + if (!ccs_policy_loaded) | |
2947 | + return true; | |
2948 | + if (task->ccs_flags & CCS_TASK_IS_MANAGER) | |
2949 | + return true; | |
2950 | + if (!ccs_manage_by_non_root && | |
2951 | + (!uid_eq(current_uid(), GLOBAL_ROOT_UID) || | |
2952 | + !uid_eq(current_euid(), GLOBAL_ROOT_UID))) | |
2953 | + return false; | |
2954 | + exe.name = ccs_get_exe(); | |
2955 | + if (!exe.name) | |
2956 | + return false; | |
2957 | + ccs_fill_path_info(&exe); | |
2958 | + list_for_each_entry_srcu(ptr, &ccs_kernel_namespace. | |
2959 | + policy_list[CCS_ID_MANAGER], head.list, | |
2960 | + &ccs_ss) { | |
2961 | + if (ptr->head.is_deleted) | |
2962 | + continue; | |
2963 | + if (ccs_pathcmp(domainname, ptr->manager) && | |
2964 | + ccs_pathcmp(&exe, ptr->manager)) | |
2965 | + continue; | |
2966 | + /* Set manager flag. */ | |
2967 | + task->ccs_flags |= CCS_TASK_IS_MANAGER; | |
2968 | + found = true; | |
2969 | + break; | |
2970 | + } | |
2971 | + if (!found) { /* Reduce error messages. */ | |
2972 | + static pid_t ccs_last_pid; | |
2973 | + const pid_t pid = current->pid; | |
2974 | + if (ccs_last_pid != pid) { | |
2975 | + printk(KERN_WARNING "%s ( %s ) is not permitted to " | |
2976 | + "update policies.\n", domainname->name, | |
2977 | + exe.name); | |
2978 | + ccs_last_pid = pid; | |
2979 | + } | |
2980 | + } | |
2981 | + kfree(exe.name); | |
2982 | + return found; | |
2983 | +} | |
2984 | + | |
2985 | +/** | |
2986 | + * ccs_find_domain - Find a domain by the given name. | |
2987 | + * | |
2988 | + * @domainname: The domainname to find. | |
2989 | + * | |
2990 | + * Returns pointer to "struct ccs_domain_info" if found, NULL otherwise. | |
2991 | + * | |
2992 | + * Caller holds ccs_read_lock(). | |
2993 | + */ | |
2994 | +static struct ccs_domain_info *ccs_find_domain(const char *domainname) | |
2995 | +{ | |
2996 | + struct ccs_domain_info *domain; | |
2997 | + struct ccs_path_info name; | |
2998 | + name.name = domainname; | |
2999 | + ccs_fill_path_info(&name); | |
3000 | + list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { | |
3001 | + if (!domain->is_deleted && | |
3002 | + !ccs_pathcmp(&name, domain->domainname)) | |
3003 | + return domain; | |
3004 | + } | |
3005 | + return NULL; | |
3006 | +} | |
3007 | + | |
3008 | +/** | |
3009 | + * ccs_select_domain - Parse select command. | |
3010 | + * | |
3011 | + * @head: Pointer to "struct ccs_io_buffer". | |
3012 | + * @data: String to parse. | |
3013 | + * | |
3014 | + * Returns true on success, false otherwise. | |
3015 | + * | |
3016 | + * Caller holds ccs_read_lock(). | |
3017 | + */ | |
3018 | +static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data) | |
3019 | +{ | |
3020 | + unsigned int pid; | |
3021 | + struct ccs_domain_info *domain = NULL; | |
3022 | + bool global_pid = false; | |
3023 | + if (strncmp(data, "select ", 7)) | |
3024 | + return false; | |
3025 | + data += 7; | |
3026 | + if (sscanf(data, "pid=%u", &pid) == 1 || | |
3027 | + (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) { | |
3028 | + struct task_struct *p; | |
3029 | + ccs_tasklist_lock(); | |
3030 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
3031 | + if (global_pid) | |
3032 | + p = ccsecurity_exports.find_task_by_pid_ns(pid, | |
3033 | + &init_pid_ns); | |
3034 | + else | |
3035 | + p = ccsecurity_exports.find_task_by_vpid(pid); | |
3036 | +#else | |
3037 | + p = find_task_by_pid(pid); | |
3038 | +#endif | |
3039 | + if (p) | |
3040 | + domain = ccs_task_domain(p); | |
3041 | + ccs_tasklist_unlock(); | |
3042 | + } else if (!strncmp(data, "domain=", 7)) { | |
3043 | + if (*(data + 7) == '<') | |
3044 | + domain = ccs_find_domain(data + 7); | |
3045 | + } else if (sscanf(data, "Q=%u", &pid) == 1) { | |
3046 | + domain = ccs_find_domain_by_qid(pid); | |
3047 | + } else | |
3048 | + return false; | |
3049 | + head->w.domain = domain; | |
3050 | + /* Accessing read_buf is safe because head->io_sem is held. */ | |
3051 | + if (!head->read_buf) | |
3052 | + return true; /* Do nothing if open(O_WRONLY). */ | |
3053 | + memset(&head->r, 0, sizeof(head->r)); | |
3054 | + head->r.print_this_domain_only = true; | |
3055 | + if (domain) | |
3056 | + head->r.domain = &domain->list; | |
3057 | + else | |
3058 | + head->r.eof = true; | |
3059 | + ccs_io_printf(head, "# select %s\n", data); | |
3060 | + if (domain && domain->is_deleted) | |
3061 | + ccs_set_string(head, "# This is a deleted domain.\n"); | |
3062 | + return true; | |
3063 | +} | |
3064 | + | |
3065 | +/** | |
3066 | + * ccs_update_acl - Update "struct ccs_acl_info" entry. | |
3067 | + * | |
3068 | + * @size: Size of new entry in bytes. | |
3069 | + * @param: Pointer to "struct ccs_acl_param". | |
3070 | + * | |
3071 | + * Returns 0 on success, negative value otherwise. | |
3072 | + * | |
3073 | + * Caller holds ccs_read_lock(). | |
3074 | + */ | |
3075 | +static int ccs_update_acl(const int size, struct ccs_acl_param *param) | |
3076 | +{ | |
3077 | + struct ccs_acl_info *new_entry = ¶m->e.acl_info; | |
3078 | + const bool is_delete = param->is_delete; | |
3079 | + int error = is_delete ? -ENOENT : -ENOMEM; | |
3080 | + struct ccs_acl_info *entry; | |
3081 | + struct list_head * const list = param->list; | |
3082 | + BUG_ON(size < sizeof(*entry)); | |
3083 | + if (param->data[0]) { | |
3084 | + new_entry->cond = ccs_get_condition(param); | |
3085 | + if (!new_entry->cond) | |
3086 | + return -EINVAL; | |
3087 | + /* | |
3088 | + * Domain transition preference is allowed for only | |
3089 | + * "file execute"/"task auto_execute_handler"/ | |
3090 | + * "task denied_auto_execute_handler" entries. | |
3091 | + */ | |
3092 | + if (new_entry->cond->exec_transit && | |
3093 | + !(new_entry->type == CCS_TYPE_PATH_ACL && | |
3094 | + new_entry->perm == 1 << CCS_TYPE_EXECUTE) | |
3095 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
3096 | + && new_entry->type != CCS_TYPE_AUTO_EXECUTE_HANDLER && | |
3097 | + new_entry->type != CCS_TYPE_DENIED_EXECUTE_HANDLER | |
3098 | +#endif | |
3099 | + ) | |
3100 | + return -EINVAL; | |
3101 | + } | |
3102 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
3103 | + return -ENOMEM; | |
3104 | + list_for_each_entry_srcu(entry, list, list, &ccs_ss) { | |
3105 | + if (entry->is_deleted == CCS_GC_IN_PROGRESS) | |
3106 | + continue; | |
3107 | + if (entry->type != new_entry->type || | |
3108 | + entry->cond != new_entry->cond || | |
3109 | + memcmp(entry + 1, new_entry + 1, size - sizeof(*entry))) | |
3110 | + continue; | |
3111 | + if (is_delete) | |
3112 | + entry->perm &= ~new_entry->perm; | |
3113 | + else | |
3114 | + entry->perm |= new_entry->perm; | |
3115 | + entry->is_deleted = !entry->perm; | |
3116 | + error = 0; | |
3117 | + break; | |
3118 | + } | |
3119 | + if (error && !is_delete) { | |
3120 | + entry = ccs_commit_ok(new_entry, size); | |
3121 | + if (entry) { | |
3122 | + list_add_tail_rcu(&entry->list, list); | |
3123 | + error = 0; | |
3124 | + } | |
3125 | + } | |
3126 | + mutex_unlock(&ccs_policy_lock); | |
3127 | + return error; | |
3128 | +} | |
3129 | + | |
3130 | +/** | |
3131 | + * ccs_permstr - Find permission keywords. | |
3132 | + * | |
3133 | + * @string: String representation for permissions in foo/bar/buz format. | |
3134 | + * @keyword: Keyword to find from @string/ | |
3135 | + * | |
3136 | + * Returns ture if @keyword was found in @string, false otherwise. | |
3137 | + * | |
3138 | + * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2. | |
3139 | + */ | |
3140 | +static bool ccs_permstr(const char *string, const char *keyword) | |
3141 | +{ | |
3142 | + const char *cp = strstr(string, keyword); | |
3143 | + if (cp) | |
3144 | + return cp == string || *(cp - 1) == '/'; | |
3145 | + return false; | |
3146 | +} | |
3147 | + | |
3148 | +/** | |
3149 | + * ccs_write_task - Update task related list. | |
3150 | + * | |
3151 | + * @param: Pointer to "struct ccs_acl_param". | |
3152 | + * | |
3153 | + * Returns 0 on success, negative value otherwise. | |
3154 | + * | |
3155 | + * Caller holds ccs_read_lock(). | |
3156 | + */ | |
3157 | +static int ccs_write_task(struct ccs_acl_param *param) | |
3158 | +{ | |
3159 | + int error; | |
3160 | + const bool is_auto = ccs_str_starts(¶m->data, | |
3161 | + "auto_domain_transition "); | |
3162 | + if (!is_auto && !ccs_str_starts(¶m->data, | |
3163 | + "manual_domain_transition ")) { | |
3164 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
3165 | + struct ccs_handler_acl *e = ¶m->e.handler_acl; | |
3166 | + char *handler; | |
3167 | + if (ccs_str_starts(¶m->data, "auto_execute_handler ")) | |
3168 | + e->head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER; | |
3169 | + else if (ccs_str_starts(¶m->data, | |
3170 | + "denied_execute_handler ")) | |
3171 | + e->head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER; | |
3172 | + else | |
3173 | + return -EINVAL; | |
3174 | + handler = ccs_read_token(param); | |
3175 | + if (!ccs_correct_path(handler)) | |
3176 | + return -EINVAL; | |
3177 | + e->handler = ccs_get_name(handler); | |
3178 | + if (!e->handler) | |
3179 | + return -ENOMEM; | |
3180 | + if (e->handler->is_patterned) | |
3181 | + return -EINVAL; /* No patterns allowed. */ | |
3182 | + return ccs_update_acl(sizeof(*e), param); | |
3183 | +#else | |
3184 | + error = -EINVAL; | |
3185 | +#endif | |
3186 | + } else { | |
3187 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
3188 | + struct ccs_task_acl *e = ¶m->e.task_acl; | |
3189 | + e->head.type = is_auto ? | |
3190 | + CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL; | |
3191 | + e->domainname = ccs_get_domainname(param); | |
3192 | + if (!e->domainname) | |
3193 | + return -EINVAL; | |
3194 | + return ccs_update_acl(sizeof(*e), param); | |
3195 | +#else | |
3196 | + error = -EINVAL; | |
3197 | +#endif | |
3198 | + } | |
3199 | + return error; | |
3200 | +} | |
3201 | + | |
3202 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
3203 | + | |
3204 | +/** | |
3205 | + * ccs_write_inet_network - Write "struct ccs_inet_acl" list. | |
3206 | + * | |
3207 | + * @param: Pointer to "struct ccs_acl_param". | |
3208 | + * | |
3209 | + * Returns 0 on success, negative value otherwise. | |
3210 | + * | |
3211 | + * Caller holds ccs_read_lock(). | |
3212 | + */ | |
3213 | +static int ccs_write_inet_network(struct ccs_acl_param *param) | |
3214 | +{ | |
3215 | + struct ccs_inet_acl *e = ¶m->e.inet_acl; | |
3216 | + u8 type; | |
3217 | + const char *protocol = ccs_read_token(param); | |
3218 | + const char *operation = ccs_read_token(param); | |
3219 | + e->head.type = CCS_TYPE_INET_ACL; | |
3220 | + for (type = 0; type < CCS_SOCK_MAX; type++) | |
3221 | + if (!strcmp(protocol, ccs_proto_keyword[type])) | |
3222 | + break; | |
3223 | + if (type == CCS_SOCK_MAX) | |
3224 | + return -EINVAL; | |
3225 | + e->protocol = type; | |
3226 | + e->head.perm = 0; | |
3227 | + for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) | |
3228 | + if (ccs_permstr(operation, ccs_socket_keyword[type])) | |
3229 | + e->head.perm |= 1 << type; | |
3230 | + if (!e->head.perm) | |
3231 | + return -EINVAL; | |
3232 | + if (param->data[0] == '@') { | |
3233 | + param->data++; | |
3234 | + e->address.group = ccs_get_group(param, CCS_ADDRESS_GROUP); | |
3235 | + if (!e->address.group) | |
3236 | + return -ENOMEM; | |
3237 | + } else { | |
3238 | + if (!ccs_parse_ipaddr_union(param, &e->address)) | |
3239 | + return -EINVAL; | |
3240 | + } | |
3241 | + if (!ccs_parse_number_union(param, &e->port) || | |
3242 | + e->port.values[1] > 65535) | |
3243 | + return -EINVAL; | |
3244 | + return ccs_update_acl(sizeof(*e), param); | |
3245 | +} | |
3246 | + | |
3247 | +/** | |
3248 | + * ccs_write_unix_network - Write "struct ccs_unix_acl" list. | |
3249 | + * | |
3250 | + * @param: Pointer to "struct ccs_acl_param". | |
3251 | + * | |
3252 | + * Returns 0 on success, negative value otherwise. | |
3253 | + */ | |
3254 | +static int ccs_write_unix_network(struct ccs_acl_param *param) | |
3255 | +{ | |
3256 | + struct ccs_unix_acl *e = ¶m->e.unix_acl; | |
3257 | + u8 type; | |
3258 | + const char *protocol = ccs_read_token(param); | |
3259 | + const char *operation = ccs_read_token(param); | |
3260 | + e->head.type = CCS_TYPE_UNIX_ACL; | |
3261 | + for (type = 0; type < CCS_SOCK_MAX; type++) | |
3262 | + if (!strcmp(protocol, ccs_proto_keyword[type])) | |
3263 | + break; | |
3264 | + if (type == CCS_SOCK_MAX) | |
3265 | + return -EINVAL; | |
3266 | + e->protocol = type; | |
3267 | + e->head.perm = 0; | |
3268 | + for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++) | |
3269 | + if (ccs_permstr(operation, ccs_socket_keyword[type])) | |
3270 | + e->head.perm |= 1 << type; | |
3271 | + if (!e->head.perm) | |
3272 | + return -EINVAL; | |
3273 | + if (!ccs_parse_name_union(param, &e->name)) | |
3274 | + return -EINVAL; | |
3275 | + return ccs_update_acl(sizeof(*e), param); | |
3276 | +} | |
3277 | + | |
3278 | +#endif | |
3279 | + | |
3280 | +/** | |
3281 | + * ccs_write_file - Update file related list. | |
3282 | + * | |
3283 | + * @param: Pointer to "struct ccs_acl_param". | |
3284 | + * | |
3285 | + * Returns 0 on success, negative value otherwise. | |
3286 | + * | |
3287 | + * Caller holds ccs_read_lock(). | |
3288 | + */ | |
3289 | +static int ccs_write_file(struct ccs_acl_param *param) | |
3290 | +{ | |
3291 | + u16 perm = 0; | |
3292 | + u8 type; | |
3293 | + const char *operation = ccs_read_token(param); | |
3294 | + for (type = 0; type < CCS_MAX_PATH_OPERATION; type++) | |
3295 | + if (ccs_permstr(operation, ccs_path_keyword[type])) | |
3296 | + perm |= 1 << type; | |
3297 | + if (perm) { | |
3298 | + struct ccs_path_acl *e = ¶m->e.path_acl; | |
3299 | + e->head.type = CCS_TYPE_PATH_ACL; | |
3300 | + e->head.perm = perm; | |
3301 | + if (!ccs_parse_name_union(param, &e->name)) | |
3302 | + return -EINVAL; | |
3303 | + return ccs_update_acl(sizeof(*e), param); | |
3304 | + } | |
3305 | + for (type = 0; type < CCS_MAX_PATH2_OPERATION; type++) | |
3306 | + if (ccs_permstr(operation, ccs_mac_keywords[ccs_pp2mac[type]])) | |
3307 | + perm |= 1 << type; | |
3308 | + if (perm) { | |
3309 | + struct ccs_path2_acl *e = ¶m->e.path2_acl; | |
3310 | + e->head.type = CCS_TYPE_PATH2_ACL; | |
3311 | + e->head.perm = perm; | |
3312 | + if (!ccs_parse_name_union(param, &e->name1) || | |
3313 | + !ccs_parse_name_union(param, &e->name2)) | |
3314 | + return -EINVAL; | |
3315 | + return ccs_update_acl(sizeof(*e), param); | |
3316 | + } | |
3317 | + for (type = 0; type < CCS_MAX_PATH_NUMBER_OPERATION; type++) | |
3318 | + if (ccs_permstr(operation, ccs_mac_keywords[ccs_pn2mac[type]])) | |
3319 | + perm |= 1 << type; | |
3320 | + if (perm) { | |
3321 | + struct ccs_path_number_acl *e = ¶m->e.path_number_acl; | |
3322 | + e->head.type = CCS_TYPE_PATH_NUMBER_ACL; | |
3323 | + e->head.perm = perm; | |
3324 | + if (!ccs_parse_name_union(param, &e->name) || | |
3325 | + !ccs_parse_number_union(param, &e->number)) | |
3326 | + return -EINVAL; | |
3327 | + return ccs_update_acl(sizeof(*e), param); | |
3328 | + } | |
3329 | + for (type = 0; type < CCS_MAX_MKDEV_OPERATION; type++) | |
3330 | + if (ccs_permstr(operation, | |
3331 | + ccs_mac_keywords[ccs_pnnn2mac[type]])) | |
3332 | + perm |= 1 << type; | |
3333 | + if (perm) { | |
3334 | + struct ccs_mkdev_acl *e = ¶m->e.mkdev_acl; | |
3335 | + e->head.type = CCS_TYPE_MKDEV_ACL; | |
3336 | + e->head.perm = perm; | |
3337 | + if (!ccs_parse_name_union(param, &e->name) || | |
3338 | + !ccs_parse_number_union(param, &e->mode) || | |
3339 | + !ccs_parse_number_union(param, &e->major) || | |
3340 | + !ccs_parse_number_union(param, &e->minor)) | |
3341 | + return -EINVAL; | |
3342 | + return ccs_update_acl(sizeof(*e), param); | |
3343 | + } | |
3344 | + if (ccs_permstr(operation, ccs_mac_keywords[CCS_MAC_FILE_MOUNT])) { | |
3345 | + struct ccs_mount_acl *e = ¶m->e.mount_acl; | |
3346 | + e->head.type = CCS_TYPE_MOUNT_ACL; | |
3347 | + if (!ccs_parse_name_union(param, &e->dev_name) || | |
3348 | + !ccs_parse_name_union(param, &e->dir_name) || | |
3349 | + !ccs_parse_name_union(param, &e->fs_type) || | |
3350 | + !ccs_parse_number_union(param, &e->flags)) | |
3351 | + return -EINVAL; | |
3352 | + return ccs_update_acl(sizeof(*e), param); | |
3353 | + } | |
3354 | + return -EINVAL; | |
3355 | +} | |
3356 | + | |
3357 | +#ifdef CONFIG_CCSECURITY_MISC | |
3358 | + | |
3359 | +/** | |
3360 | + * ccs_write_misc - Update environment variable list. | |
3361 | + * | |
3362 | + * @param: Pointer to "struct ccs_acl_param". | |
3363 | + * | |
3364 | + * Returns 0 on success, negative value otherwise. | |
3365 | + */ | |
3366 | +static int ccs_write_misc(struct ccs_acl_param *param) | |
3367 | +{ | |
3368 | + if (ccs_str_starts(¶m->data, "env ")) { | |
3369 | + struct ccs_env_acl *e = ¶m->e.env_acl; | |
3370 | + const char *data = ccs_read_token(param); | |
3371 | + e->head.type = CCS_TYPE_ENV_ACL; | |
3372 | + if (!ccs_correct_word(data) || strchr(data, '=')) | |
3373 | + return -EINVAL; | |
3374 | + e->env = ccs_get_name(data); | |
3375 | + if (!e->env) | |
3376 | + return -ENOMEM; | |
3377 | + return ccs_update_acl(sizeof(*e), param); | |
3378 | + } | |
3379 | + return -EINVAL; | |
3380 | +} | |
3381 | + | |
3382 | +#endif | |
3383 | + | |
3384 | +#ifdef CONFIG_CCSECURITY_IPC | |
3385 | + | |
3386 | +/** | |
3387 | + * ccs_write_ipc - Update "struct ccs_signal_acl" list. | |
3388 | + * | |
3389 | + * @param: Pointer to "struct ccs_acl_param". | |
3390 | + * | |
3391 | + * Returns 0 on success, negative value otherwise. | |
3392 | + */ | |
3393 | +static int ccs_write_ipc(struct ccs_acl_param *param) | |
3394 | +{ | |
3395 | + struct ccs_signal_acl *e = ¶m->e.signal_acl; | |
3396 | + e->head.type = CCS_TYPE_SIGNAL_ACL; | |
3397 | + if (!ccs_parse_number_union(param, &e->sig)) | |
3398 | + return -EINVAL; | |
3399 | + e->domainname = ccs_get_domainname(param); | |
3400 | + if (!e->domainname) | |
3401 | + return -EINVAL; | |
3402 | + return ccs_update_acl(sizeof(*e), param); | |
3403 | +} | |
3404 | + | |
3405 | +#endif | |
3406 | + | |
3407 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
3408 | + | |
3409 | +/** | |
3410 | + * ccs_write_capability - Write "struct ccs_capability_acl" list. | |
3411 | + * | |
3412 | + * @param: Pointer to "struct ccs_acl_param". | |
3413 | + * | |
3414 | + * Returns 0 on success, negative value otherwise. | |
3415 | + * | |
3416 | + * Caller holds ccs_read_lock(). | |
3417 | + */ | |
3418 | +static int ccs_write_capability(struct ccs_acl_param *param) | |
3419 | +{ | |
3420 | + struct ccs_capability_acl *e = ¶m->e.capability_acl; | |
3421 | + const char *operation = ccs_read_token(param); | |
3422 | + u8 type; | |
3423 | + e->head.type = CCS_TYPE_CAPABILITY_ACL; | |
3424 | + for (type = 0; type < CCS_MAX_CAPABILITY_INDEX; type++) { | |
3425 | + if (strcmp(operation, ccs_mac_keywords[ccs_c2mac[type]])) | |
3426 | + continue; | |
3427 | + e->operation = type; | |
3428 | + return ccs_update_acl(sizeof(*e), param); | |
3429 | + } | |
3430 | + return -EINVAL; | |
3431 | +} | |
3432 | + | |
3433 | +#endif | |
3434 | + | |
3435 | +/** | |
3436 | + * ccs_write_acl - Write "struct ccs_acl_info" list. | |
3437 | + * | |
3438 | + * @ns: Pointer to "struct ccs_policy_namespace". | |
3439 | + * @list: Pointer to "struct list_head". | |
3440 | + * @data: Policy to be interpreted. | |
3441 | + * @is_delete: True if it is a delete request. | |
3442 | + * | |
3443 | + * Returns 0 on success, negative value otherwise. | |
3444 | + * | |
3445 | + * Caller holds ccs_read_lock(). | |
3446 | + */ | |
3447 | +static int ccs_write_acl(struct ccs_policy_namespace *ns, | |
3448 | + struct list_head *list, char *data, | |
3449 | + const bool is_delete) | |
3450 | +{ | |
3451 | + struct ccs_acl_param param = { | |
3452 | + .ns = ns, | |
3453 | + .list = list, | |
3454 | + .data = data, | |
3455 | + .is_delete = is_delete, | |
3456 | + }; | |
3457 | + static const struct { | |
3458 | + const char *keyword; | |
3459 | + int (*write) (struct ccs_acl_param *); | |
3460 | + } ccs_callback[] = { | |
3461 | + { "file ", ccs_write_file }, | |
3462 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
3463 | + { "network inet ", ccs_write_inet_network }, | |
3464 | + { "network unix ", ccs_write_unix_network }, | |
3465 | +#endif | |
3466 | +#ifdef CONFIG_CCSECURITY_MISC | |
3467 | + { "misc ", ccs_write_misc }, | |
3468 | +#endif | |
3469 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
3470 | + { "capability ", ccs_write_capability }, | |
3471 | +#endif | |
3472 | +#ifdef CONFIG_CCSECURITY_IPC | |
3473 | + { "ipc signal ", ccs_write_ipc }, | |
3474 | +#endif | |
3475 | + { "task ", ccs_write_task }, | |
3476 | + }; | |
3477 | + u8 i; | |
3478 | + /* Forced zero clear for using memcmp() at ccs_update_acl(). */ | |
3479 | + memset(¶m.e, 0, sizeof(param.e)); | |
3480 | + param.e.acl_info.perm = 1; | |
3481 | + for (i = 0; i < ARRAY_SIZE(ccs_callback); i++) { | |
3482 | + int error; | |
3483 | + if (!ccs_str_starts(¶m.data, ccs_callback[i].keyword)) | |
3484 | + continue; | |
3485 | + error = ccs_callback[i].write(¶m); | |
3486 | + ccs_del_acl(¶m.e.acl_info.list); | |
3487 | + return error; | |
3488 | + } | |
3489 | + return -EINVAL; | |
3490 | +} | |
3491 | + | |
3492 | +/** | |
3493 | + * ccs_delete_domain - Delete a domain. | |
3494 | + * | |
3495 | + * @domainname: The name of domain. | |
3496 | + * | |
3497 | + * Returns 0. | |
3498 | + */ | |
3499 | +static int ccs_delete_domain(char *domainname) | |
3500 | +{ | |
3501 | + struct ccs_domain_info *domain; | |
3502 | + struct ccs_path_info name; | |
3503 | + name.name = domainname; | |
3504 | + ccs_fill_path_info(&name); | |
3505 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
3506 | + return 0; | |
3507 | + /* Is there an active domain? */ | |
3508 | + list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) { | |
3509 | + /* Never delete ccs_kernel_domain. */ | |
3510 | + if (domain == &ccs_kernel_domain) | |
3511 | + continue; | |
3512 | + if (domain->is_deleted || | |
3513 | + ccs_pathcmp(domain->domainname, &name)) | |
3514 | + continue; | |
3515 | + domain->is_deleted = true; | |
3516 | + break; | |
3517 | + } | |
3518 | + mutex_unlock(&ccs_policy_lock); | |
3519 | + return 0; | |
3520 | +} | |
3521 | + | |
3522 | +/** | |
3523 | + * ccs_write_domain - Write domain policy. | |
3524 | + * | |
3525 | + * @head: Pointer to "struct ccs_io_buffer". | |
3526 | + * | |
3527 | + * Returns 0 on success, negative value otherwise. | |
3528 | + * | |
3529 | + * Caller holds ccs_read_lock(). | |
3530 | + */ | |
3531 | +static int ccs_write_domain(struct ccs_io_buffer *head) | |
3532 | +{ | |
3533 | + char *data = head->write_buf; | |
3534 | + struct ccs_policy_namespace *ns; | |
3535 | + struct ccs_domain_info *domain = head->w.domain; | |
3536 | + const bool is_delete = head->w.is_delete; | |
3537 | + const bool is_select = !is_delete && ccs_str_starts(&data, "select "); | |
3538 | + unsigned int idx; | |
3539 | + if (*data == '<') { | |
3540 | + domain = NULL; | |
3541 | + if (is_delete) | |
3542 | + ccs_delete_domain(data); | |
3543 | + else if (is_select) | |
3544 | + domain = ccs_find_domain(data); | |
3545 | + else | |
3546 | + domain = ccs_assign_domain(data, false); | |
3547 | + head->w.domain = domain; | |
3548 | + return 0; | |
3549 | + } | |
3550 | + if (!domain) | |
3551 | + return -EINVAL; | |
3552 | + ns = domain->ns; | |
3553 | + if (sscanf(data, "use_profile %u\n", &idx) == 1 && | |
3554 | + idx < CCS_MAX_PROFILES) { | |
3555 | + if (!ccs_policy_loaded || ns->profile_ptr[(u8) idx]) | |
3556 | + if (!is_delete) | |
3557 | + domain->profile = (u8) idx; | |
3558 | + return 0; | |
3559 | + } | |
3560 | + if (sscanf(data, "use_group %u\n", &idx) == 1 && | |
3561 | + idx < CCS_MAX_ACL_GROUPS) { | |
3562 | + if (!is_delete) | |
3563 | + set_bit(idx, domain->group); | |
3564 | + else | |
3565 | + clear_bit(idx, domain->group); | |
3566 | + return 0; | |
3567 | + } | |
3568 | + for (idx = 0; idx < CCS_MAX_DOMAIN_INFO_FLAGS; idx++) { | |
3569 | + const char *cp = ccs_dif[idx]; | |
3570 | + if (strncmp(data, cp, strlen(cp) - 1)) | |
3571 | + continue; | |
3572 | + domain->flags[idx] = !is_delete; | |
3573 | + return 0; | |
3574 | + } | |
3575 | + return ccs_write_acl(ns, &domain->acl_info_list, data, is_delete); | |
3576 | +} | |
3577 | + | |
3578 | +/** | |
3579 | + * ccs_print_name_union - Print a ccs_name_union. | |
3580 | + * | |
3581 | + * @head: Pointer to "struct ccs_io_buffer". | |
3582 | + * @ptr: Pointer to "struct ccs_name_union". | |
3583 | + * | |
3584 | + * Returns nothing. | |
3585 | + */ | |
3586 | +static void ccs_print_name_union(struct ccs_io_buffer *head, | |
3587 | + const struct ccs_name_union *ptr) | |
3588 | +{ | |
3589 | + ccs_set_space(head); | |
3590 | + if (!ccs_print_group(head, ptr->group)) | |
3591 | + ccs_set_string(head, ptr->filename->name); | |
3592 | +} | |
3593 | + | |
3594 | +/** | |
3595 | + * ccs_print_name_union_quoted - Print a ccs_name_union with a quote. | |
3596 | + * | |
3597 | + * @head: Pointer to "struct ccs_io_buffer". | |
3598 | + * @ptr: Pointer to "struct ccs_name_union". | |
3599 | + * | |
3600 | + * Returns nothing. | |
3601 | + */ | |
3602 | +static void ccs_print_name_union_quoted(struct ccs_io_buffer *head, | |
3603 | + const struct ccs_name_union *ptr) | |
3604 | +{ | |
3605 | + if (!ccs_print_group(head, ptr->group)) { | |
3606 | + ccs_set_string(head, "\""); | |
3607 | + ccs_set_string(head, ptr->filename->name); | |
3608 | + ccs_set_string(head, "\""); | |
3609 | + } | |
3610 | +} | |
3611 | + | |
3612 | +/** | |
3613 | + * ccs_print_number_union_nospace - Print a ccs_number_union without a space. | |
3614 | + * | |
3615 | + * @head: Pointer to "struct ccs_io_buffer". | |
3616 | + * @ptr: Pointer to "struct ccs_number_union". | |
3617 | + * | |
3618 | + * Returns nothing. | |
3619 | + */ | |
3620 | +static void ccs_print_number_union_nospace(struct ccs_io_buffer *head, | |
3621 | + const struct ccs_number_union *ptr) | |
3622 | +{ | |
3623 | + if (!ccs_print_group(head, ptr->group)) { | |
3624 | + int i; | |
3625 | + unsigned long min = ptr->values[0]; | |
3626 | + const unsigned long max = ptr->values[1]; | |
3627 | + u8 min_type = ptr->value_type[0]; | |
3628 | + const u8 max_type = ptr->value_type[1]; | |
3629 | + char buffer[128]; | |
3630 | + buffer[0] = '\0'; | |
3631 | + for (i = 0; i < 2; i++) { | |
3632 | + switch (min_type) { | |
3633 | + case CCS_VALUE_TYPE_HEXADECIMAL: | |
3634 | + ccs_addprintf(buffer, sizeof(buffer), "0x%lX", | |
3635 | + min); | |
3636 | + break; | |
3637 | + case CCS_VALUE_TYPE_OCTAL: | |
3638 | + ccs_addprintf(buffer, sizeof(buffer), "0%lo", | |
3639 | + min); | |
3640 | + break; | |
3641 | + default: | |
3642 | + ccs_addprintf(buffer, sizeof(buffer), "%lu", | |
3643 | + min); | |
3644 | + break; | |
3645 | + } | |
3646 | + if (min == max && min_type == max_type) | |
3647 | + break; | |
3648 | + ccs_addprintf(buffer, sizeof(buffer), "-"); | |
3649 | + min_type = max_type; | |
3650 | + min = max; | |
3651 | + } | |
3652 | + ccs_io_printf(head, "%s", buffer); | |
3653 | + } | |
3654 | +} | |
3655 | + | |
3656 | +/** | |
3657 | + * ccs_print_number_union - Print a ccs_number_union. | |
3658 | + * | |
3659 | + * @head: Pointer to "struct ccs_io_buffer". | |
3660 | + * @ptr: Pointer to "struct ccs_number_union". | |
3661 | + * | |
3662 | + * Returns nothing. | |
3663 | + */ | |
3664 | +static void ccs_print_number_union(struct ccs_io_buffer *head, | |
3665 | + const struct ccs_number_union *ptr) | |
3666 | +{ | |
3667 | + ccs_set_space(head); | |
3668 | + ccs_print_number_union_nospace(head, ptr); | |
3669 | +} | |
3670 | + | |
3671 | +/** | |
3672 | + * ccs_print_condition - Print condition part. | |
3673 | + * | |
3674 | + * @head: Pointer to "struct ccs_io_buffer". | |
3675 | + * @cond: Pointer to "struct ccs_condition". | |
3676 | + * | |
3677 | + * Returns true on success, false otherwise. | |
3678 | + */ | |
3679 | +static bool ccs_print_condition(struct ccs_io_buffer *head, | |
3680 | + const struct ccs_condition *cond) | |
3681 | +{ | |
3682 | + switch (head->r.cond_step) { | |
3683 | + case 0: | |
3684 | + head->r.cond_index = 0; | |
3685 | + head->r.cond_step++; | |
3686 | + if (cond->transit && cond->exec_transit) { | |
3687 | + ccs_set_space(head); | |
3688 | + ccs_set_string(head, cond->transit->name); | |
3689 | + } | |
3690 | + /* fall through */ | |
3691 | + case 1: | |
3692 | + { | |
3693 | + const u16 condc = cond->condc; | |
3694 | + const struct ccs_condition_element *condp = | |
3695 | + (typeof(condp)) (cond + 1); | |
3696 | + const struct ccs_number_union *numbers_p = | |
3697 | + (typeof(numbers_p)) (condp + condc); | |
3698 | + const struct ccs_name_union *names_p = | |
3699 | + (typeof(names_p)) | |
3700 | + (numbers_p + cond->numbers_count); | |
3701 | + const struct ccs_argv *argv = | |
3702 | + (typeof(argv)) (names_p + cond->names_count); | |
3703 | + const struct ccs_envp *envp = | |
3704 | + (typeof(envp)) (argv + cond->argc); | |
3705 | + u16 skip; | |
3706 | + for (skip = 0; skip < head->r.cond_index; skip++) { | |
3707 | + const u8 left = condp->left; | |
3708 | + const u8 right = condp->right; | |
3709 | + condp++; | |
3710 | + switch (left) { | |
3711 | + case CCS_ARGV_ENTRY: | |
3712 | + argv++; | |
3713 | + continue; | |
3714 | + case CCS_ENVP_ENTRY: | |
3715 | + envp++; | |
3716 | + continue; | |
3717 | + case CCS_NUMBER_UNION: | |
3718 | + numbers_p++; | |
3719 | + break; | |
3720 | + } | |
3721 | + switch (right) { | |
3722 | + case CCS_NAME_UNION: | |
3723 | + names_p++; | |
3724 | + break; | |
3725 | + case CCS_NUMBER_UNION: | |
3726 | + numbers_p++; | |
3727 | + break; | |
3728 | + } | |
3729 | + } | |
3730 | + while (head->r.cond_index < condc) { | |
3731 | + const u8 match = condp->equals; | |
3732 | + const u8 left = condp->left; | |
3733 | + const u8 right = condp->right; | |
3734 | + if (!ccs_flush(head)) | |
3735 | + return false; | |
3736 | + condp++; | |
3737 | + head->r.cond_index++; | |
3738 | + ccs_set_space(head); | |
3739 | + switch (left) { | |
3740 | + case CCS_ARGV_ENTRY: | |
3741 | + ccs_io_printf(head, | |
3742 | + "exec.argv[%lu]%s=\"", | |
3743 | + argv->index, | |
3744 | + argv->is_not ? "!" : ""); | |
3745 | + ccs_set_string(head, | |
3746 | + argv->value->name); | |
3747 | + ccs_set_string(head, "\""); | |
3748 | + argv++; | |
3749 | + continue; | |
3750 | + case CCS_ENVP_ENTRY: | |
3751 | + ccs_set_string(head, "exec.envp[\""); | |
3752 | + ccs_set_string(head, envp->name->name); | |
3753 | + ccs_io_printf(head, "\"]%s=", | |
3754 | + envp->is_not ? "!" : ""); | |
3755 | + if (envp->value) { | |
3756 | + ccs_set_string(head, "\""); | |
3757 | + ccs_set_string(head, envp-> | |
3758 | + value->name); | |
3759 | + ccs_set_string(head, "\""); | |
3760 | + } else { | |
3761 | + ccs_set_string(head, "NULL"); | |
3762 | + } | |
3763 | + envp++; | |
3764 | + continue; | |
3765 | + case CCS_NUMBER_UNION: | |
3766 | + ccs_print_number_union_nospace | |
3767 | + (head, numbers_p++); | |
3768 | + break; | |
3769 | + default: | |
3770 | + ccs_set_string(head, | |
3771 | + ccs_condition_keyword[left]); | |
3772 | + break; | |
3773 | + } | |
3774 | + ccs_set_string(head, match ? "=" : "!="); | |
3775 | + switch (right) { | |
3776 | + case CCS_NAME_UNION: | |
3777 | + ccs_print_name_union_quoted | |
3778 | + (head, names_p++); | |
3779 | + break; | |
3780 | + case CCS_NUMBER_UNION: | |
3781 | + ccs_print_number_union_nospace | |
3782 | + (head, numbers_p++); | |
3783 | + break; | |
3784 | + default: | |
3785 | + ccs_set_string(head, | |
3786 | + ccs_condition_keyword[right]); | |
3787 | + break; | |
3788 | + } | |
3789 | + } | |
3790 | + } | |
3791 | + head->r.cond_step++; | |
3792 | + /* fall through */ | |
3793 | + case 2: | |
3794 | + if (!ccs_flush(head)) | |
3795 | + break; | |
3796 | + head->r.cond_step++; | |
3797 | + /* fall through */ | |
3798 | + case 3: | |
3799 | + if (cond->grant_log != CCS_GRANTLOG_AUTO) | |
3800 | + ccs_io_printf(head, " grant_log=%s", | |
3801 | + ccs_yesno(cond->grant_log == | |
3802 | + CCS_GRANTLOG_YES)); | |
3803 | + if (cond->transit && !cond->exec_transit) { | |
3804 | + const char *name = cond->transit->name; | |
3805 | + ccs_set_string(head, " auto_domain_transition=\""); | |
3806 | + ccs_set_string(head, name); | |
3807 | + ccs_set_string(head, "\""); | |
3808 | + } | |
3809 | + ccs_set_lf(head); | |
3810 | + return true; | |
3811 | + } | |
3812 | + return false; | |
3813 | +} | |
3814 | + | |
3815 | +/** | |
3816 | + * ccs_set_group - Print "acl_group " header keyword and category name. | |
3817 | + * | |
3818 | + * @head: Pointer to "struct ccs_io_buffer". | |
3819 | + * @category: Category name. | |
3820 | + * | |
3821 | + * Returns nothing. | |
3822 | + */ | |
3823 | +static void ccs_set_group(struct ccs_io_buffer *head, const char *category) | |
3824 | +{ | |
3825 | + if (head->type == CCS_EXCEPTION_POLICY) { | |
3826 | + ccs_print_namespace(head); | |
3827 | + ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index); | |
3828 | + } | |
3829 | + ccs_set_string(head, category); | |
3830 | +} | |
3831 | + | |
3832 | +/** | |
3833 | + * ccs_print_entry - Print an ACL entry. | |
3834 | + * | |
3835 | + * @head: Pointer to "struct ccs_io_buffer". | |
3836 | + * @acl: Pointer to an ACL entry. | |
3837 | + * | |
3838 | + * Returns true on success, false otherwise. | |
3839 | + */ | |
3840 | +static bool ccs_print_entry(struct ccs_io_buffer *head, | |
3841 | + const struct ccs_acl_info *acl) | |
3842 | +{ | |
3843 | + const u8 acl_type = acl->type; | |
3844 | + const bool may_trigger_transition = acl->cond && acl->cond->transit; | |
3845 | + bool first = true; | |
3846 | + u8 bit; | |
3847 | + if (head->r.print_cond_part) | |
3848 | + goto print_cond_part; | |
3849 | + if (acl->is_deleted) | |
3850 | + return true; | |
3851 | + if (!ccs_flush(head)) | |
3852 | + return false; | |
3853 | + else if (acl_type == CCS_TYPE_PATH_ACL) { | |
3854 | + struct ccs_path_acl *ptr | |
3855 | + = container_of(acl, typeof(*ptr), head); | |
3856 | + for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) { | |
3857 | + if (!(acl->perm & (1 << bit))) | |
3858 | + continue; | |
3859 | + if (head->r.print_transition_related_only && | |
3860 | + bit != CCS_TYPE_EXECUTE && !may_trigger_transition) | |
3861 | + continue; | |
3862 | + if (first) { | |
3863 | + ccs_set_group(head, "file "); | |
3864 | + first = false; | |
3865 | + } else { | |
3866 | + ccs_set_slash(head); | |
3867 | + } | |
3868 | + ccs_set_string(head, ccs_path_keyword[bit]); | |
3869 | + } | |
3870 | + if (first) | |
3871 | + return true; | |
3872 | + ccs_print_name_union(head, &ptr->name); | |
3873 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
3874 | + } else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER || | |
3875 | + acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) { | |
3876 | + struct ccs_handler_acl *ptr | |
3877 | + = container_of(acl, typeof(*ptr), head); | |
3878 | + ccs_set_group(head, "task "); | |
3879 | + ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER | |
3880 | + ? "auto_execute_handler " : | |
3881 | + "denied_execute_handler "); | |
3882 | + ccs_set_string(head, ptr->handler->name); | |
3883 | +#endif | |
3884 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
3885 | + } else if (acl_type == CCS_TYPE_AUTO_TASK_ACL || | |
3886 | + acl_type == CCS_TYPE_MANUAL_TASK_ACL) { | |
3887 | + struct ccs_task_acl *ptr = | |
3888 | + container_of(acl, typeof(*ptr), head); | |
3889 | + ccs_set_group(head, "task "); | |
3890 | + ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ? | |
3891 | + "auto_domain_transition " : | |
3892 | + "manual_domain_transition "); | |
3893 | + ccs_set_string(head, ptr->domainname->name); | |
3894 | +#endif | |
3895 | + } else if (head->r.print_transition_related_only && | |
3896 | + !may_trigger_transition) { | |
3897 | + return true; | |
3898 | + } else if (acl_type == CCS_TYPE_MKDEV_ACL) { | |
3899 | + struct ccs_mkdev_acl *ptr = | |
3900 | + container_of(acl, typeof(*ptr), head); | |
3901 | + for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) { | |
3902 | + if (!(acl->perm & (1 << bit))) | |
3903 | + continue; | |
3904 | + if (first) { | |
3905 | + ccs_set_group(head, "file "); | |
3906 | + first = false; | |
3907 | + } else { | |
3908 | + ccs_set_slash(head); | |
3909 | + } | |
3910 | + ccs_set_string(head, ccs_mac_keywords | |
3911 | + [ccs_pnnn2mac[bit]]); | |
3912 | + } | |
3913 | + if (first) | |
3914 | + return true; | |
3915 | + ccs_print_name_union(head, &ptr->name); | |
3916 | + ccs_print_number_union(head, &ptr->mode); | |
3917 | + ccs_print_number_union(head, &ptr->major); | |
3918 | + ccs_print_number_union(head, &ptr->minor); | |
3919 | + } else if (acl_type == CCS_TYPE_PATH2_ACL) { | |
3920 | + struct ccs_path2_acl *ptr = | |
3921 | + container_of(acl, typeof(*ptr), head); | |
3922 | + for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) { | |
3923 | + if (!(acl->perm & (1 << bit))) | |
3924 | + continue; | |
3925 | + if (first) { | |
3926 | + ccs_set_group(head, "file "); | |
3927 | + first = false; | |
3928 | + } else { | |
3929 | + ccs_set_slash(head); | |
3930 | + } | |
3931 | + ccs_set_string(head, ccs_mac_keywords | |
3932 | + [ccs_pp2mac[bit]]); | |
3933 | + } | |
3934 | + if (first) | |
3935 | + return true; | |
3936 | + ccs_print_name_union(head, &ptr->name1); | |
3937 | + ccs_print_name_union(head, &ptr->name2); | |
3938 | + } else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) { | |
3939 | + struct ccs_path_number_acl *ptr = | |
3940 | + container_of(acl, typeof(*ptr), head); | |
3941 | + for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) { | |
3942 | + if (!(acl->perm & (1 << bit))) | |
3943 | + continue; | |
3944 | + if (first) { | |
3945 | + ccs_set_group(head, "file "); | |
3946 | + first = false; | |
3947 | + } else { | |
3948 | + ccs_set_slash(head); | |
3949 | + } | |
3950 | + ccs_set_string(head, ccs_mac_keywords | |
3951 | + [ccs_pn2mac[bit]]); | |
3952 | + } | |
3953 | + if (first) | |
3954 | + return true; | |
3955 | + ccs_print_name_union(head, &ptr->name); | |
3956 | + ccs_print_number_union(head, &ptr->number); | |
3957 | +#ifdef CONFIG_CCSECURITY_MISC | |
3958 | + } else if (acl_type == CCS_TYPE_ENV_ACL) { | |
3959 | + struct ccs_env_acl *ptr = | |
3960 | + container_of(acl, typeof(*ptr), head); | |
3961 | + ccs_set_group(head, "misc env "); | |
3962 | + ccs_set_string(head, ptr->env->name); | |
3963 | +#endif | |
3964 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
3965 | + } else if (acl_type == CCS_TYPE_CAPABILITY_ACL) { | |
3966 | + struct ccs_capability_acl *ptr = | |
3967 | + container_of(acl, typeof(*ptr), head); | |
3968 | + ccs_set_group(head, "capability "); | |
3969 | + ccs_set_string(head, ccs_mac_keywords | |
3970 | + [ccs_c2mac[ptr->operation]]); | |
3971 | +#endif | |
3972 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
3973 | + } else if (acl_type == CCS_TYPE_INET_ACL) { | |
3974 | + struct ccs_inet_acl *ptr = | |
3975 | + container_of(acl, typeof(*ptr), head); | |
3976 | + for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) { | |
3977 | + if (!(acl->perm & (1 << bit))) | |
3978 | + continue; | |
3979 | + if (first) { | |
3980 | + ccs_set_group(head, "network inet "); | |
3981 | + ccs_set_string(head, ccs_proto_keyword | |
3982 | + [ptr->protocol]); | |
3983 | + ccs_set_space(head); | |
3984 | + first = false; | |
3985 | + } else { | |
3986 | + ccs_set_slash(head); | |
3987 | + } | |
3988 | + ccs_set_string(head, ccs_socket_keyword[bit]); | |
3989 | + } | |
3990 | + if (first) | |
3991 | + return true; | |
3992 | + ccs_set_space(head); | |
3993 | + if (!ccs_print_group(head, ptr->address.group)) { | |
3994 | + char buf[128]; | |
3995 | + ccs_print_ip(buf, sizeof(buf), &ptr->address); | |
3996 | + ccs_io_printf(head, "%s", buf); | |
3997 | + } | |
3998 | + ccs_print_number_union(head, &ptr->port); | |
3999 | + } else if (acl_type == CCS_TYPE_UNIX_ACL) { | |
4000 | + struct ccs_unix_acl *ptr = | |
4001 | + container_of(acl, typeof(*ptr), head); | |
4002 | + for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) { | |
4003 | + if (!(acl->perm & (1 << bit))) | |
4004 | + continue; | |
4005 | + if (first) { | |
4006 | + ccs_set_group(head, "network unix "); | |
4007 | + ccs_set_string(head, ccs_proto_keyword | |
4008 | + [ptr->protocol]); | |
4009 | + ccs_set_space(head); | |
4010 | + first = false; | |
4011 | + } else { | |
4012 | + ccs_set_slash(head); | |
4013 | + } | |
4014 | + ccs_set_string(head, ccs_socket_keyword[bit]); | |
4015 | + } | |
4016 | + if (first) | |
4017 | + return true; | |
4018 | + ccs_print_name_union(head, &ptr->name); | |
4019 | +#endif | |
4020 | +#ifdef CONFIG_CCSECURITY_IPC | |
4021 | + } else if (acl_type == CCS_TYPE_SIGNAL_ACL) { | |
4022 | + struct ccs_signal_acl *ptr = | |
4023 | + container_of(acl, typeof(*ptr), head); | |
4024 | + ccs_set_group(head, "ipc signal "); | |
4025 | + ccs_print_number_union_nospace(head, &ptr->sig); | |
4026 | + ccs_set_space(head); | |
4027 | + ccs_set_string(head, ptr->domainname->name); | |
4028 | +#endif | |
4029 | + } else if (acl_type == CCS_TYPE_MOUNT_ACL) { | |
4030 | + struct ccs_mount_acl *ptr = | |
4031 | + container_of(acl, typeof(*ptr), head); | |
4032 | + ccs_set_group(head, "file mount"); | |
4033 | + ccs_print_name_union(head, &ptr->dev_name); | |
4034 | + ccs_print_name_union(head, &ptr->dir_name); | |
4035 | + ccs_print_name_union(head, &ptr->fs_type); | |
4036 | + ccs_print_number_union(head, &ptr->flags); | |
4037 | + } | |
4038 | + if (acl->cond) { | |
4039 | + head->r.print_cond_part = true; | |
4040 | + head->r.cond_step = 0; | |
4041 | + if (!ccs_flush(head)) | |
4042 | + return false; | |
4043 | +print_cond_part: | |
4044 | + if (!ccs_print_condition(head, acl->cond)) | |
4045 | + return false; | |
4046 | + head->r.print_cond_part = false; | |
4047 | + } else { | |
4048 | + ccs_set_lf(head); | |
4049 | + } | |
4050 | + return true; | |
4051 | +} | |
4052 | + | |
4053 | +/** | |
4054 | + * ccs_read_acl - Read "struct ccs_acl_info" list. | |
4055 | + * | |
4056 | + * @head: Pointer to "struct ccs_io_buffer". | |
4057 | + * @list: Pointer to "struct list_head". | |
4058 | + * | |
4059 | + * Returns true on success, false otherwise. | |
4060 | + * | |
4061 | + * Caller holds ccs_read_lock(). | |
4062 | + */ | |
4063 | +static bool ccs_read_acl(struct ccs_io_buffer *head, struct list_head *list) | |
4064 | +{ | |
4065 | + list_for_each_cookie(head->r.acl, list) { | |
4066 | + struct ccs_acl_info *ptr = | |
4067 | + list_entry(head->r.acl, typeof(*ptr), list); | |
4068 | + if (!ccs_print_entry(head, ptr)) | |
4069 | + return false; | |
4070 | + } | |
4071 | + head->r.acl = NULL; | |
4072 | + return true; | |
4073 | +} | |
4074 | + | |
4075 | +/** | |
4076 | + * ccs_read_domain - Read domain policy. | |
4077 | + * | |
4078 | + * @head: Pointer to "struct ccs_io_buffer". | |
4079 | + * | |
4080 | + * Returns nothing. | |
4081 | + * | |
4082 | + * Caller holds ccs_read_lock(). | |
4083 | + */ | |
4084 | +static void ccs_read_domain(struct ccs_io_buffer *head) | |
4085 | +{ | |
4086 | + if (head->r.eof) | |
4087 | + return; | |
4088 | + list_for_each_cookie(head->r.domain, &ccs_domain_list) { | |
4089 | + struct ccs_domain_info *domain = | |
4090 | + list_entry(head->r.domain, typeof(*domain), list); | |
4091 | + switch (head->r.step) { | |
4092 | + u8 i; | |
4093 | + case 0: | |
4094 | + if (domain->is_deleted && | |
4095 | + !head->r.print_this_domain_only) | |
4096 | + continue; | |
4097 | + /* Print domainname and flags. */ | |
4098 | + ccs_set_string(head, domain->domainname->name); | |
4099 | + ccs_set_lf(head); | |
4100 | + ccs_io_printf(head, "use_profile %u\n", | |
4101 | + domain->profile); | |
4102 | + for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++) | |
4103 | + if (domain->flags[i]) | |
4104 | + ccs_set_string(head, ccs_dif[i]); | |
4105 | + head->r.index = 0; | |
4106 | + head->r.step++; | |
4107 | + /* fall through */ | |
4108 | + case 1: | |
4109 | + while (head->r.index < CCS_MAX_ACL_GROUPS) { | |
4110 | + i = head->r.index++; | |
4111 | + if (!test_bit(i, domain->group)) | |
4112 | + continue; | |
4113 | + ccs_io_printf(head, "use_group %u\n", i); | |
4114 | + if (!ccs_flush(head)) | |
4115 | + return; | |
4116 | + } | |
4117 | + head->r.index = 0; | |
4118 | + head->r.step++; | |
4119 | + ccs_set_lf(head); | |
4120 | + /* fall through */ | |
4121 | + case 2: | |
4122 | + if (!ccs_read_acl(head, &domain->acl_info_list)) | |
4123 | + return; | |
4124 | + head->r.step++; | |
4125 | + if (!ccs_set_lf(head)) | |
4126 | + return; | |
4127 | + /* fall through */ | |
4128 | + case 3: | |
4129 | + head->r.step = 0; | |
4130 | + if (head->r.print_this_domain_only) | |
4131 | + goto done; | |
4132 | + } | |
4133 | + } | |
4134 | +done: | |
4135 | + head->r.eof = true; | |
4136 | +} | |
4137 | + | |
4138 | +/** | |
4139 | + * ccs_write_pid - Specify PID to obtain domainname. | |
4140 | + * | |
4141 | + * @head: Pointer to "struct ccs_io_buffer". | |
4142 | + * | |
4143 | + * Returns 0. | |
4144 | + */ | |
4145 | +static int ccs_write_pid(struct ccs_io_buffer *head) | |
4146 | +{ | |
4147 | + head->r.eof = false; | |
4148 | + return 0; | |
4149 | +} | |
4150 | + | |
4151 | +/** | |
4152 | + * ccs_read_pid - Read information of a process. | |
4153 | + * | |
4154 | + * @head: Pointer to "struct ccs_io_buffer". | |
4155 | + * | |
4156 | + * Returns the domainname which the specified PID is in or | |
4157 | + * process information of the specified PID on success, | |
4158 | + * empty string otherwise. | |
4159 | + * | |
4160 | + * Caller holds ccs_read_lock(). | |
4161 | + */ | |
4162 | +static void ccs_read_pid(struct ccs_io_buffer *head) | |
4163 | +{ | |
4164 | + char *buf = head->write_buf; | |
4165 | + bool task_info = false; | |
4166 | + bool global_pid = false; | |
4167 | + unsigned int pid; | |
4168 | + struct task_struct *p; | |
4169 | + struct ccs_domain_info *domain = NULL; | |
4170 | + u32 ccs_flags = 0; | |
4171 | + /* Accessing write_buf is safe because head->io_sem is held. */ | |
4172 | + if (!buf) { | |
4173 | + head->r.eof = true; | |
4174 | + return; /* Do nothing if open(O_RDONLY). */ | |
4175 | + } | |
4176 | + if (head->r.w_pos || head->r.eof) | |
4177 | + return; | |
4178 | + head->r.eof = true; | |
4179 | + if (ccs_str_starts(&buf, "info ")) | |
4180 | + task_info = true; | |
4181 | + if (ccs_str_starts(&buf, "global-pid ")) | |
4182 | + global_pid = true; | |
4183 | + pid = (unsigned int) simple_strtoul(buf, NULL, 10); | |
4184 | + ccs_tasklist_lock(); | |
4185 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
4186 | + if (global_pid) | |
4187 | + p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns); | |
4188 | + else | |
4189 | + p = ccsecurity_exports.find_task_by_vpid(pid); | |
4190 | +#else | |
4191 | + p = find_task_by_pid(pid); | |
4192 | +#endif | |
4193 | + if (p) { | |
4194 | + domain = ccs_task_domain(p); | |
4195 | + ccs_flags = ccs_task_flags(p); | |
4196 | + } | |
4197 | + ccs_tasklist_unlock(); | |
4198 | + if (!domain) | |
4199 | + return; | |
4200 | + if (!task_info) { | |
4201 | + ccs_io_printf(head, "%u %u ", pid, domain->profile); | |
4202 | + ccs_set_string(head, domain->domainname->name); | |
4203 | + } else { | |
4204 | + ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid, | |
4205 | + ccs_yesno(ccs_flags & | |
4206 | + CCS_TASK_IS_MANAGER), | |
4207 | + ccs_yesno(ccs_flags & | |
4208 | + CCS_TASK_IS_EXECUTE_HANDLER)); | |
4209 | + } | |
4210 | +} | |
4211 | + | |
4212 | +/** | |
4213 | + * ccs_write_group - Write "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list. | |
4214 | + * | |
4215 | + * @param: Pointer to "struct ccs_acl_param". | |
4216 | + * @type: Type of this group. | |
4217 | + * | |
4218 | + * Returns 0 on success, negative value otherwise. | |
4219 | + */ | |
4220 | +static int ccs_write_group(struct ccs_acl_param *param, const u8 type) | |
4221 | +{ | |
4222 | + struct ccs_group *group = ccs_get_group(param, type); | |
4223 | + int error = -EINVAL; | |
4224 | + if (!group) | |
4225 | + return -ENOMEM; | |
4226 | + param->list = &group->member_list; | |
4227 | + if (type == CCS_PATH_GROUP) { | |
4228 | + struct ccs_path_group *e = ¶m->e.path_group; | |
4229 | + e->member_name = ccs_get_name(ccs_read_token(param)); | |
4230 | + if (!e->member_name) { | |
4231 | + error = -ENOMEM; | |
4232 | + goto out; | |
4233 | + } | |
4234 | + error = ccs_update_policy(sizeof(*e), param); | |
4235 | + ccs_put_name(e->member_name); | |
4236 | + } else if (type == CCS_NUMBER_GROUP) { | |
4237 | + struct ccs_number_group *e = ¶m->e.number_group; | |
4238 | + if (param->data[0] == '@' || | |
4239 | + !ccs_parse_number_union(param, &e->number)) | |
4240 | + goto out; | |
4241 | + error = ccs_update_policy(sizeof(*e), param); | |
4242 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
4243 | + } else { | |
4244 | + struct ccs_address_group *e = ¶m->e.address_group; | |
4245 | + if (param->data[0] == '@' || | |
4246 | + !ccs_parse_ipaddr_union(param, &e->address)) | |
4247 | + goto out; | |
4248 | + error = ccs_update_policy(sizeof(*e), param); | |
4249 | +#endif | |
4250 | + } | |
4251 | +out: | |
4252 | + ccs_put_group(group); | |
4253 | + return error; | |
4254 | +} | |
4255 | + | |
4256 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
4257 | +/** | |
4258 | + * ccs_lport_reserved - Check whether local port is reserved or not. | |
4259 | + * | |
4260 | + * @port: Port number. | |
4261 | + * | |
4262 | + * Returns true if local port is reserved, false otherwise. | |
4263 | + */ | |
4264 | +static bool __ccs_lport_reserved(const u16 port) | |
4265 | +{ | |
4266 | + return ccs_reserved_port_map[port >> 3] & (1 << (port & 7)) | |
4267 | + ? true : false; | |
4268 | +} | |
4269 | + | |
4270 | +/** | |
4271 | + * ccs_write_reserved_port - Update "struct ccs_reserved" list. | |
4272 | + * | |
4273 | + * @param: Pointer to "struct ccs_acl_param". | |
4274 | + * | |
4275 | + * Returns 0 on success, negative value otherwise. | |
4276 | + * | |
4277 | + * Caller holds ccs_read_lock(). | |
4278 | + */ | |
4279 | +static int ccs_write_reserved_port(struct ccs_acl_param *param) | |
4280 | +{ | |
4281 | + struct ccs_reserved *e = ¶m->e.reserved; | |
4282 | + struct ccs_policy_namespace *ns = param->ns; | |
4283 | + int error; | |
4284 | + u8 *tmp; | |
4285 | + if (param->data[0] == '@' || | |
4286 | + !ccs_parse_number_union(param, &e->port) || | |
4287 | + e->port.values[1] > 65535 || param->data[0]) | |
4288 | + return -EINVAL; | |
4289 | + param->list = &ns->policy_list[CCS_ID_RESERVEDPORT]; | |
4290 | + error = ccs_update_policy(sizeof(*e), param); | |
4291 | + if (error) | |
4292 | + return error; | |
4293 | + tmp = kzalloc(sizeof(ccs_reserved_port_map), CCS_GFP_FLAGS); | |
4294 | + if (!tmp) | |
4295 | + return -ENOMEM; | |
4296 | + list_for_each_entry_srcu(ns, &ccs_namespace_list, namespace_list, | |
4297 | + &ccs_ss) { | |
4298 | + struct ccs_reserved *ptr; | |
4299 | + struct list_head *list = &ns->policy_list[CCS_ID_RESERVEDPORT]; | |
4300 | + list_for_each_entry_srcu(ptr, list, head.list, &ccs_ss) { | |
4301 | + unsigned int port; | |
4302 | + if (ptr->head.is_deleted) | |
4303 | + continue; | |
4304 | + for (port = ptr->port.values[0]; | |
4305 | + port <= ptr->port.values[1]; port++) | |
4306 | + tmp[port >> 3] |= 1 << (port & 7); | |
4307 | + } | |
4308 | + } | |
4309 | + memmove(ccs_reserved_port_map, tmp, sizeof(ccs_reserved_port_map)); | |
4310 | + kfree(tmp); | |
4311 | + /* | |
4312 | + * Since this feature is no-op by default, we don't need to register | |
4313 | + * this callback hook unless the first entry is added. | |
4314 | + */ | |
4315 | + ccsecurity_ops.lport_reserved = __ccs_lport_reserved; | |
4316 | + return 0; | |
4317 | +} | |
4318 | +#endif | |
4319 | + | |
4320 | +/** | |
4321 | + * ccs_write_aggregator - Write "struct ccs_aggregator" list. | |
4322 | + * | |
4323 | + * @param: Pointer to "struct ccs_acl_param". | |
4324 | + * | |
4325 | + * Returns 0 on success, negative value otherwise. | |
4326 | + */ | |
4327 | +static int ccs_write_aggregator(struct ccs_acl_param *param) | |
4328 | +{ | |
4329 | + struct ccs_aggregator *e = ¶m->e.aggregator; | |
4330 | + int error = param->is_delete ? -ENOENT : -ENOMEM; | |
4331 | + const char *original_name = ccs_read_token(param); | |
4332 | + const char *aggregated_name = ccs_read_token(param); | |
4333 | + if (!ccs_correct_word(original_name) || | |
4334 | + !ccs_correct_path(aggregated_name)) | |
4335 | + return -EINVAL; | |
4336 | + e->original_name = ccs_get_name(original_name); | |
4337 | + e->aggregated_name = ccs_get_name(aggregated_name); | |
4338 | + if (!e->original_name || !e->aggregated_name || | |
4339 | + e->aggregated_name->is_patterned) /* No patterns allowed. */ | |
4340 | + goto out; | |
4341 | + param->list = ¶m->ns->policy_list[CCS_ID_AGGREGATOR]; | |
4342 | + error = ccs_update_policy(sizeof(*e), param); | |
4343 | +out: | |
4344 | + ccs_put_name(e->original_name); | |
4345 | + ccs_put_name(e->aggregated_name); | |
4346 | + return error; | |
4347 | +} | |
4348 | + | |
4349 | +/** | |
4350 | + * ccs_write_transition_control - Write "struct ccs_transition_control" list. | |
4351 | + * | |
4352 | + * @param: Pointer to "struct ccs_acl_param". | |
4353 | + * @type: Type of this entry. | |
4354 | + * | |
4355 | + * Returns 0 on success, negative value otherwise. | |
4356 | + */ | |
4357 | +static int ccs_write_transition_control(struct ccs_acl_param *param, | |
4358 | + const u8 type) | |
4359 | +{ | |
4360 | + struct ccs_transition_control *e = ¶m->e.transition_control; | |
4361 | + int error = param->is_delete ? -ENOENT : -ENOMEM; | |
4362 | + char *program = param->data; | |
4363 | + char *domainname = strstr(program, " from "); | |
4364 | + e->type = type; | |
4365 | + if (domainname) { | |
4366 | + *domainname = '\0'; | |
4367 | + domainname += 6; | |
4368 | + } else if (type == CCS_TRANSITION_CONTROL_NO_KEEP || | |
4369 | + type == CCS_TRANSITION_CONTROL_KEEP) { | |
4370 | + domainname = program; | |
4371 | + program = NULL; | |
4372 | + } | |
4373 | + if (program && strcmp(program, "any")) { | |
4374 | + if (!ccs_correct_path(program)) | |
4375 | + return -EINVAL; | |
4376 | + e->program = ccs_get_name(program); | |
4377 | + if (!e->program) | |
4378 | + goto out; | |
4379 | + } | |
4380 | + if (domainname && strcmp(domainname, "any")) { | |
4381 | + if (!ccs_correct_domain(domainname)) { | |
4382 | + if (!ccs_correct_path(domainname)) | |
4383 | + goto out; | |
4384 | + e->is_last_name = true; | |
4385 | + } | |
4386 | + e->domainname = ccs_get_name(domainname); | |
4387 | + if (!e->domainname) | |
4388 | + goto out; | |
4389 | + } | |
4390 | + param->list = ¶m->ns->policy_list[CCS_ID_TRANSITION_CONTROL]; | |
4391 | + error = ccs_update_policy(sizeof(*e), param); | |
4392 | +out: | |
4393 | + ccs_put_name(e->domainname); | |
4394 | + ccs_put_name(e->program); | |
4395 | + return error; | |
4396 | +} | |
4397 | + | |
4398 | +/** | |
4399 | + * ccs_write_exception - Write exception policy. | |
4400 | + * | |
4401 | + * @head: Pointer to "struct ccs_io_buffer". | |
4402 | + * | |
4403 | + * Returns 0 on success, negative value otherwise. | |
4404 | + */ | |
4405 | +static int ccs_write_exception(struct ccs_io_buffer *head) | |
4406 | +{ | |
4407 | + const bool is_delete = head->w.is_delete; | |
4408 | + struct ccs_acl_param param = { | |
4409 | + .ns = head->w.ns, | |
4410 | + .is_delete = is_delete, | |
4411 | + .data = head->write_buf, | |
4412 | + }; | |
4413 | + u8 i; | |
4414 | + /* Forced zero clear for using memcmp() at ccs_update_policy(). */ | |
4415 | + memset(¶m.e, 0, sizeof(param.e)); | |
4416 | + if (ccs_str_starts(¶m.data, "aggregator ")) | |
4417 | + return ccs_write_aggregator(¶m); | |
4418 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
4419 | + if (ccs_str_starts(¶m.data, "deny_autobind ")) | |
4420 | + return ccs_write_reserved_port(¶m); | |
4421 | +#endif | |
4422 | + for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++) | |
4423 | + if (ccs_str_starts(¶m.data, ccs_transition_type[i])) | |
4424 | + return ccs_write_transition_control(¶m, i); | |
4425 | + for (i = 0; i < CCS_MAX_GROUP; i++) | |
4426 | + if (ccs_str_starts(¶m.data, ccs_group_name[i])) | |
4427 | + return ccs_write_group(¶m, i); | |
4428 | + if (ccs_str_starts(¶m.data, "acl_group ")) { | |
4429 | + unsigned int group; | |
4430 | + char *data; | |
4431 | + group = simple_strtoul(param.data, &data, 10); | |
4432 | + if (group < CCS_MAX_ACL_GROUPS && *data++ == ' ') | |
4433 | + return ccs_write_acl(head->w.ns, | |
4434 | + &head->w.ns->acl_group[group], | |
4435 | + data, is_delete); | |
4436 | + } | |
4437 | + return -EINVAL; | |
4438 | +} | |
4439 | + | |
4440 | +/** | |
4441 | + * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list. | |
4442 | + * | |
4443 | + * @head: Pointer to "struct ccs_io_buffer". | |
4444 | + * @idx: Index number. | |
4445 | + * | |
4446 | + * Returns true on success, false otherwise. | |
4447 | + * | |
4448 | + * Caller holds ccs_read_lock(). | |
4449 | + */ | |
4450 | +static bool ccs_read_group(struct ccs_io_buffer *head, const int idx) | |
4451 | +{ | |
4452 | + struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns), | |
4453 | + namespace_list); | |
4454 | + struct list_head *list = &ns->group_list[idx]; | |
4455 | + list_for_each_cookie(head->r.group, list) { | |
4456 | + struct ccs_group *group = | |
4457 | + list_entry(head->r.group, typeof(*group), head.list); | |
4458 | + list_for_each_cookie(head->r.acl, &group->member_list) { | |
4459 | + struct ccs_acl_head *ptr = | |
4460 | + list_entry(head->r.acl, typeof(*ptr), list); | |
4461 | + if (ptr->is_deleted) | |
4462 | + continue; | |
4463 | + if (!ccs_flush(head)) | |
4464 | + return false; | |
4465 | + ccs_print_namespace(head); | |
4466 | + ccs_set_string(head, ccs_group_name[idx]); | |
4467 | + ccs_set_string(head, group->group_name->name); | |
4468 | + if (idx == CCS_PATH_GROUP) { | |
4469 | + ccs_set_space(head); | |
4470 | + ccs_set_string(head, container_of | |
4471 | + (ptr, struct ccs_path_group, | |
4472 | + head)->member_name->name); | |
4473 | + } else if (idx == CCS_NUMBER_GROUP) { | |
4474 | + ccs_print_number_union(head, &container_of | |
4475 | + (ptr, struct ccs_number_group, | |
4476 | + head)->number); | |
4477 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
4478 | + } else if (idx == CCS_ADDRESS_GROUP) { | |
4479 | + char buffer[128]; | |
4480 | + struct ccs_address_group *member = | |
4481 | + container_of(ptr, typeof(*member), | |
4482 | + head); | |
4483 | + ccs_print_ip(buffer, sizeof(buffer), | |
4484 | + &member->address); | |
4485 | + ccs_io_printf(head, " %s", buffer); | |
4486 | +#endif | |
4487 | + } | |
4488 | + ccs_set_lf(head); | |
4489 | + } | |
4490 | + head->r.acl = NULL; | |
4491 | + } | |
4492 | + head->r.group = NULL; | |
4493 | + return true; | |
4494 | +} | |
4495 | + | |
4496 | +/** | |
4497 | + * ccs_read_policy - Read "struct ccs_..._entry" list. | |
4498 | + * | |
4499 | + * @head: Pointer to "struct ccs_io_buffer". | |
4500 | + * @idx: Index number. | |
4501 | + * | |
4502 | + * Returns true on success, false otherwise. | |
4503 | + * | |
4504 | + * Caller holds ccs_read_lock(). | |
4505 | + */ | |
4506 | +static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx) | |
4507 | +{ | |
4508 | + struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns), | |
4509 | + namespace_list); | |
4510 | + struct list_head *list = &ns->policy_list[idx]; | |
4511 | + list_for_each_cookie(head->r.acl, list) { | |
4512 | + struct ccs_acl_head *acl = | |
4513 | + container_of(head->r.acl, typeof(*acl), list); | |
4514 | + if (acl->is_deleted) | |
4515 | + continue; | |
4516 | + if (head->r.print_transition_related_only && | |
4517 | + idx != CCS_ID_TRANSITION_CONTROL) | |
4518 | + continue; | |
4519 | + if (!ccs_flush(head)) | |
4520 | + return false; | |
4521 | + switch (idx) { | |
4522 | + case CCS_ID_TRANSITION_CONTROL: | |
4523 | + { | |
4524 | + struct ccs_transition_control *ptr = | |
4525 | + container_of(acl, typeof(*ptr), head); | |
4526 | + ccs_print_namespace(head); | |
4527 | + ccs_set_string(head, | |
4528 | + ccs_transition_type[ptr->type]); | |
4529 | + ccs_set_string(head, ptr->program ? | |
4530 | + ptr->program->name : "any"); | |
4531 | + ccs_set_string(head, " from "); | |
4532 | + ccs_set_string(head, ptr->domainname ? | |
4533 | + ptr->domainname->name : "any"); | |
4534 | + } | |
4535 | + break; | |
4536 | + case CCS_ID_AGGREGATOR: | |
4537 | + { | |
4538 | + struct ccs_aggregator *ptr = | |
4539 | + container_of(acl, typeof(*ptr), head); | |
4540 | + ccs_print_namespace(head); | |
4541 | + ccs_set_string(head, "aggregator "); | |
4542 | + ccs_set_string(head, ptr->original_name->name); | |
4543 | + ccs_set_space(head); | |
4544 | + ccs_set_string(head, | |
4545 | + ptr->aggregated_name->name); | |
4546 | + } | |
4547 | + break; | |
4548 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
4549 | + case CCS_ID_RESERVEDPORT: | |
4550 | + { | |
4551 | + struct ccs_reserved *ptr = | |
4552 | + container_of(acl, typeof(*ptr), head); | |
4553 | + ccs_print_namespace(head); | |
4554 | + ccs_set_string(head, "deny_autobind "); | |
4555 | + ccs_print_number_union_nospace(head, | |
4556 | + &ptr->port); | |
4557 | + } | |
4558 | + break; | |
4559 | +#endif | |
4560 | + default: | |
4561 | + continue; | |
4562 | + } | |
4563 | + ccs_set_lf(head); | |
4564 | + } | |
4565 | + head->r.acl = NULL; | |
4566 | + return true; | |
4567 | +} | |
4568 | + | |
4569 | +/** | |
4570 | + * ccs_read_exception - Read exception policy. | |
4571 | + * | |
4572 | + * @head: Pointer to "struct ccs_io_buffer". | |
4573 | + * | |
4574 | + * Returns nothing. | |
4575 | + * | |
4576 | + * Caller holds ccs_read_lock(). | |
4577 | + */ | |
4578 | +static void ccs_read_exception(struct ccs_io_buffer *head) | |
4579 | +{ | |
4580 | + struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns), | |
4581 | + namespace_list); | |
4582 | + if (head->r.eof) | |
4583 | + return; | |
4584 | + while (head->r.step < CCS_MAX_POLICY && | |
4585 | + ccs_read_policy(head, head->r.step)) | |
4586 | + head->r.step++; | |
4587 | + if (head->r.step < CCS_MAX_POLICY) | |
4588 | + return; | |
4589 | + while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP && | |
4590 | + ccs_read_group(head, head->r.step - CCS_MAX_POLICY)) | |
4591 | + head->r.step++; | |
4592 | + if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP) | |
4593 | + return; | |
4594 | + while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP | |
4595 | + + CCS_MAX_ACL_GROUPS) { | |
4596 | + head->r.acl_group_index = | |
4597 | + head->r.step - CCS_MAX_POLICY - CCS_MAX_GROUP; | |
4598 | + if (!ccs_read_acl(head, &ns->acl_group | |
4599 | + [head->r.acl_group_index])) | |
4600 | + return; | |
4601 | + head->r.step++; | |
4602 | + } | |
4603 | + head->r.eof = true; | |
4604 | +} | |
4605 | + | |
4606 | +/** | |
4607 | + * ccs_truncate - Truncate a line. | |
4608 | + * | |
4609 | + * @str: String to truncate. | |
4610 | + * | |
4611 | + * Returns length of truncated @str. | |
4612 | + */ | |
4613 | +static int ccs_truncate(char *str) | |
4614 | +{ | |
4615 | + char *start = str; | |
4616 | + while (*(unsigned char *) str > (unsigned char) ' ') | |
4617 | + str++; | |
4618 | + *str = '\0'; | |
4619 | + return strlen(start) + 1; | |
4620 | +} | |
4621 | + | |
4622 | +/** | |
4623 | + * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode. | |
4624 | + * | |
4625 | + * @header: Lines containing ACL. | |
4626 | + * | |
4627 | + * Returns nothing. | |
4628 | + */ | |
4629 | +static void ccs_add_entry(char *header) | |
4630 | +{ | |
4631 | + char *buffer; | |
4632 | + char *realpath = NULL; | |
4633 | + char *argv0 = NULL; | |
4634 | + char *symlink = NULL; | |
4635 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
4636 | + char *handler; | |
4637 | +#endif | |
4638 | + char *cp = strchr(header, '\n'); | |
4639 | + int len; | |
4640 | + if (!cp) | |
4641 | + return; | |
4642 | + cp = strchr(cp + 1, '\n'); | |
4643 | + if (!cp) | |
4644 | + return; | |
4645 | + *cp++ = '\0'; | |
4646 | + len = strlen(cp) + 1; | |
4647 | + /* strstr() will return NULL if ordering is wrong. */ | |
4648 | + if (*cp == 'f') { | |
4649 | + argv0 = strstr(header, " argv[]={ \""); | |
4650 | + if (argv0) { | |
4651 | + argv0 += 10; | |
4652 | + len += ccs_truncate(argv0) + 14; | |
4653 | + } | |
4654 | + realpath = strstr(header, " exec={ realpath=\""); | |
4655 | + if (realpath) { | |
4656 | + realpath += 8; | |
4657 | + len += ccs_truncate(realpath) + 6; | |
4658 | + } | |
4659 | + symlink = strstr(header, " symlink.target=\""); | |
4660 | + if (symlink) | |
4661 | + len += ccs_truncate(symlink + 1) + 1; | |
4662 | + } | |
4663 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
4664 | + handler = strstr(header, "type=execute_handler"); | |
4665 | + if (handler) | |
4666 | + len += ccs_truncate(handler) + 6; | |
4667 | +#endif | |
4668 | + buffer = kmalloc(len, CCS_GFP_FLAGS); | |
4669 | + if (!buffer) | |
4670 | + return; | |
4671 | + snprintf(buffer, len - 1, "%s", cp); | |
4672 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
4673 | + if (handler) | |
4674 | + ccs_addprintf(buffer, len, " task.%s", handler); | |
4675 | +#endif | |
4676 | + if (realpath) | |
4677 | + ccs_addprintf(buffer, len, " exec.%s", realpath); | |
4678 | + if (argv0) | |
4679 | + ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0); | |
4680 | + if (symlink) | |
4681 | + ccs_addprintf(buffer, len, "%s", symlink); | |
4682 | + ccs_normalize_line(buffer); | |
4683 | + { | |
4684 | + struct ccs_domain_info *domain = ccs_current_domain(); | |
4685 | + if (!ccs_write_acl(domain->ns, &domain->acl_info_list, | |
4686 | + buffer, false)) | |
4687 | + ccs_update_stat(CCS_STAT_POLICY_UPDATES); | |
4688 | + } | |
4689 | + kfree(buffer); | |
4690 | +} | |
4691 | + | |
4692 | +/** | |
4693 | + * ccs_domain_quota_ok - Check for domain's quota. | |
4694 | + * | |
4695 | + * @r: Pointer to "struct ccs_request_info". | |
4696 | + * | |
4697 | + * Returns true if the domain is not exceeded quota, false otherwise. | |
4698 | + * | |
4699 | + * Caller holds ccs_read_lock(). | |
4700 | + */ | |
4701 | +static bool ccs_domain_quota_ok(struct ccs_request_info *r) | |
4702 | +{ | |
4703 | + unsigned int count = 0; | |
4704 | + struct ccs_domain_info * const domain = ccs_current_domain(); | |
4705 | + struct ccs_acl_info *ptr; | |
4706 | + if (r->mode != CCS_CONFIG_LEARNING) | |
4707 | + return false; | |
4708 | + if (!domain) | |
4709 | + return true; | |
4710 | + list_for_each_entry_srcu(ptr, &domain->acl_info_list, list, &ccs_ss) { | |
4711 | + u16 perm; | |
4712 | + u8 i; | |
4713 | + if (ptr->is_deleted) | |
4714 | + continue; | |
4715 | + switch (ptr->type) { | |
4716 | + case CCS_TYPE_PATH_ACL: | |
4717 | + case CCS_TYPE_PATH2_ACL: | |
4718 | + case CCS_TYPE_PATH_NUMBER_ACL: | |
4719 | + case CCS_TYPE_MKDEV_ACL: | |
4720 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
4721 | + case CCS_TYPE_INET_ACL: | |
4722 | + case CCS_TYPE_UNIX_ACL: | |
4723 | +#endif | |
4724 | + perm = ptr->perm; | |
4725 | + break; | |
4726 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
4727 | + case CCS_TYPE_AUTO_EXECUTE_HANDLER: | |
4728 | + case CCS_TYPE_DENIED_EXECUTE_HANDLER: | |
4729 | +#endif | |
4730 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
4731 | + case CCS_TYPE_AUTO_TASK_ACL: | |
4732 | + case CCS_TYPE_MANUAL_TASK_ACL: | |
4733 | +#endif | |
4734 | + perm = 0; | |
4735 | + break; | |
4736 | + default: | |
4737 | + perm = 1; | |
4738 | + } | |
4739 | + for (i = 0; i < 16; i++) | |
4740 | + if (perm & (1 << i)) | |
4741 | + count++; | |
4742 | + } | |
4743 | + if (count < ccs_profile(r->profile)->pref[CCS_PREF_MAX_LEARNING_ENTRY]) | |
4744 | + return true; | |
4745 | + if (!domain->flags[CCS_DIF_QUOTA_WARNED]) { | |
4746 | + domain->flags[CCS_DIF_QUOTA_WARNED] = true; | |
4747 | + /* r->granted = false; */ | |
4748 | + ccs_write_log(r, "%s", ccs_dif[CCS_DIF_QUOTA_WARNED]); | |
4749 | + printk(KERN_WARNING "WARNING: " | |
4750 | + "Domain '%s' has too many ACLs to hold. " | |
4751 | + "Stopped learning mode.\n", domain->domainname->name); | |
4752 | + } | |
4753 | + return false; | |
4754 | +} | |
4755 | + | |
4756 | +/** | |
4757 | + * ccs_supervisor - Ask for the supervisor's decision. | |
4758 | + * | |
4759 | + * @r: Pointer to "struct ccs_request_info". | |
4760 | + * @fmt: The printf()'s format string, followed by parameters. | |
4761 | + * | |
4762 | + * Returns 0 if the supervisor decided to permit the access request which | |
4763 | + * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor | |
4764 | + * decided to retry the access request which violated the policy in enforcing | |
4765 | + * mode, 0 if it is not in enforcing mode, -EPERM otherwise. | |
4766 | + */ | |
4767 | +static int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...) | |
4768 | +{ | |
4769 | + va_list args; | |
4770 | + int error; | |
4771 | + int len; | |
4772 | + static unsigned int ccs_serial; | |
4773 | + struct ccs_query entry = { }; | |
4774 | + bool quota_exceeded = false; | |
4775 | + va_start(args, fmt); | |
4776 | + len = vsnprintf((char *) &len, 1, fmt, args) + 1; | |
4777 | + va_end(args); | |
4778 | + /* Write /proc/ccs/audit. */ | |
4779 | + va_start(args, fmt); | |
4780 | + ccs_write_log2(r, len, fmt, args); | |
4781 | + va_end(args); | |
4782 | + /* Nothing more to do if granted. */ | |
4783 | + if (r->granted) | |
4784 | + return 0; | |
4785 | + if (r->mode) | |
4786 | + ccs_update_stat(r->mode); | |
4787 | + switch (r->mode) { | |
4788 | + int i; | |
4789 | + struct ccs_profile *p; | |
4790 | + case CCS_CONFIG_ENFORCING: | |
4791 | + error = -EPERM; | |
4792 | + if (atomic_read(&ccs_query_observers)) | |
4793 | + break; | |
4794 | + if (r->dont_sleep_on_enforce_error) | |
4795 | + goto out; | |
4796 | + p = ccs_profile(r->profile); | |
4797 | + /* Check enforcing_penalty parameter. */ | |
4798 | + for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) { | |
4799 | + set_current_state(TASK_INTERRUPTIBLE); | |
4800 | + schedule_timeout(HZ / 10); | |
4801 | + } | |
4802 | + goto out; | |
4803 | + case CCS_CONFIG_LEARNING: | |
4804 | + error = 0; | |
4805 | + /* Check max_learning_entry parameter. */ | |
4806 | + if (ccs_domain_quota_ok(r)) | |
4807 | + break; | |
4808 | + /* fall through */ | |
4809 | + default: | |
4810 | + return 0; | |
4811 | + } | |
4812 | + /* Get message. */ | |
4813 | + va_start(args, fmt); | |
4814 | + entry.query = ccs_init_log(r, len, fmt, args); | |
4815 | + va_end(args); | |
4816 | + if (!entry.query) | |
4817 | + goto out; | |
4818 | + entry.query_len = strlen(entry.query) + 1; | |
4819 | + if (!error) { | |
4820 | + ccs_add_entry(entry.query); | |
4821 | + goto out; | |
4822 | + } | |
4823 | + len = ccs_round2(entry.query_len); | |
4824 | + entry.domain = ccs_current_domain(); | |
4825 | + spin_lock(&ccs_query_list_lock); | |
4826 | + if (ccs_memory_quota[CCS_MEMORY_QUERY] && | |
4827 | + ccs_memory_used[CCS_MEMORY_QUERY] + len | |
4828 | + >= ccs_memory_quota[CCS_MEMORY_QUERY]) { | |
4829 | + quota_exceeded = true; | |
4830 | + } else { | |
4831 | + entry.serial = ccs_serial++; | |
4832 | + entry.retry = r->retry; | |
4833 | + ccs_memory_used[CCS_MEMORY_QUERY] += len; | |
4834 | + list_add_tail(&entry.list, &ccs_query_list); | |
4835 | + } | |
4836 | + spin_unlock(&ccs_query_list_lock); | |
4837 | + if (quota_exceeded) | |
4838 | + goto out; | |
4839 | + /* Give 10 seconds for supervisor's opinion. */ | |
4840 | + while (entry.timer < 10) { | |
4841 | + wake_up_all(&ccs_query_wait); | |
4842 | + if (wait_event_interruptible_timeout | |
4843 | + (ccs_answer_wait, entry.answer || | |
4844 | + !atomic_read(&ccs_query_observers), HZ)) | |
4845 | + break; | |
4846 | + else | |
4847 | + entry.timer++; | |
4848 | + } | |
4849 | + spin_lock(&ccs_query_list_lock); | |
4850 | + list_del(&entry.list); | |
4851 | + ccs_memory_used[CCS_MEMORY_QUERY] -= len; | |
4852 | + spin_unlock(&ccs_query_list_lock); | |
4853 | + switch (entry.answer) { | |
4854 | + case 3: /* Asked to retry by administrator. */ | |
4855 | + error = CCS_RETRY_REQUEST; | |
4856 | + r->retry++; | |
4857 | + break; | |
4858 | + case 1: | |
4859 | + /* Granted by administrator. */ | |
4860 | + error = 0; | |
4861 | + break; | |
4862 | + default: | |
4863 | + /* Timed out or rejected by administrator. */ | |
4864 | + break; | |
4865 | + } | |
4866 | +out: | |
4867 | + kfree(entry.query); | |
4868 | + return error; | |
4869 | +} | |
4870 | + | |
4871 | +/** | |
4872 | + * ccs_audit_log - Audit permission check log. | |
4873 | + * | |
4874 | + * @r: Pointer to "struct ccs_request_info". | |
4875 | + * | |
4876 | + * Returns return value of ccs_supervisor(). | |
4877 | + */ | |
4878 | +int ccs_audit_log(struct ccs_request_info *r) | |
4879 | +{ | |
4880 | + switch (r->param_type) { | |
4881 | + u8 type; | |
4882 | + char buf[48]; | |
4883 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
4884 | + const u32 *address; | |
4885 | +#endif | |
4886 | + case CCS_TYPE_PATH_ACL: | |
4887 | + return ccs_supervisor(r, "file %s %s\n", ccs_path_keyword | |
4888 | + [r->param.path.operation], | |
4889 | + r->param.path.filename->name); | |
4890 | + case CCS_TYPE_PATH2_ACL: | |
4891 | + return ccs_supervisor(r, "file %s %s %s\n", ccs_mac_keywords | |
4892 | + [ccs_pp2mac[r->param.path2.operation]], | |
4893 | + r->param.path2.filename1->name, | |
4894 | + r->param.path2.filename2->name); | |
4895 | + case CCS_TYPE_PATH_NUMBER_ACL: | |
4896 | + type = r->param.path_number.operation; | |
4897 | + switch (type) { | |
4898 | + case CCS_TYPE_CREATE: | |
4899 | + case CCS_TYPE_MKDIR: | |
4900 | + case CCS_TYPE_MKFIFO: | |
4901 | + case CCS_TYPE_MKSOCK: | |
4902 | + case CCS_TYPE_CHMOD: | |
4903 | + snprintf(buf, sizeof(buf), "0%lo", | |
4904 | + r->param.path_number.number); | |
4905 | + break; | |
4906 | + case CCS_TYPE_IOCTL: | |
4907 | + snprintf(buf, sizeof(buf), "0x%lX", | |
4908 | + r->param.path_number.number); | |
4909 | + break; | |
4910 | + default: | |
4911 | + snprintf(buf, sizeof(buf), "%lu", | |
4912 | + r->param.path_number.number); | |
4913 | + break; | |
4914 | + } | |
4915 | + return ccs_supervisor(r, "file %s %s %s\n", ccs_mac_keywords | |
4916 | + [ccs_pn2mac[type]], | |
4917 | + r->param.path_number.filename->name, | |
4918 | + buf); | |
4919 | + case CCS_TYPE_MKDEV_ACL: | |
4920 | + return ccs_supervisor(r, "file %s %s 0%o %u %u\n", | |
4921 | + ccs_mac_keywords | |
4922 | + [ccs_pnnn2mac[r->param.mkdev.operation]], | |
4923 | + r->param.mkdev.filename->name, | |
4924 | + r->param.mkdev.mode, | |
4925 | + r->param.mkdev.major, | |
4926 | + r->param.mkdev.minor); | |
4927 | + case CCS_TYPE_MOUNT_ACL: | |
4928 | + return ccs_supervisor(r, "file mount %s %s %s 0x%lX\n", | |
4929 | + r->param.mount.dev->name, | |
4930 | + r->param.mount.dir->name, | |
4931 | + r->param.mount.type->name, | |
4932 | + r->param.mount.flags); | |
4933 | +#ifdef CONFIG_CCSECURITY_MISC | |
4934 | + case CCS_TYPE_ENV_ACL: | |
4935 | + return ccs_supervisor(r, "misc env %s\n", | |
4936 | + r->param.environ.name->name); | |
4937 | +#endif | |
4938 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
4939 | + case CCS_TYPE_CAPABILITY_ACL: | |
4940 | + return ccs_supervisor(r, "capability %s\n", ccs_mac_keywords | |
4941 | + [ccs_c2mac[r->param.capability. | |
4942 | + operation]]); | |
4943 | +#endif | |
4944 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
4945 | + case CCS_TYPE_INET_ACL: | |
4946 | + address = r->param.inet_network.address; | |
4947 | + if (r->param.inet_network.is_ipv6) | |
4948 | + ccs_print_ipv6(buf, sizeof(buf), | |
4949 | + (const struct in6_addr *) address); | |
4950 | + else | |
4951 | + ccs_print_ipv4(buf, sizeof(buf), address); | |
4952 | + return ccs_supervisor(r, "network inet %s %s %s %u\n", | |
4953 | + ccs_proto_keyword[r->param.inet_network. | |
4954 | + protocol], | |
4955 | + ccs_socket_keyword[r->param.inet_network. | |
4956 | + operation], | |
4957 | + buf, r->param.inet_network.port); | |
4958 | + case CCS_TYPE_UNIX_ACL: | |
4959 | + return ccs_supervisor(r, "network unix %s %s %s\n", | |
4960 | + ccs_proto_keyword[r->param. | |
4961 | + unix_network.protocol], | |
4962 | + ccs_socket_keyword[r->param.unix_network. | |
4963 | + operation], | |
4964 | + r->param.unix_network.address->name); | |
4965 | +#endif | |
4966 | +#ifdef CONFIG_CCSECURITY_IPC | |
4967 | + case CCS_TYPE_SIGNAL_ACL: | |
4968 | + return ccs_supervisor(r, "ipc signal %d %s\n", | |
4969 | + r->param.signal.sig, | |
4970 | + r->param.signal.dest_pattern); | |
4971 | +#endif | |
4972 | + } | |
4973 | + return 0; | |
4974 | +} | |
4975 | + | |
4976 | +/** | |
4977 | + * ccs_find_domain_by_qid - Get domain by query id. | |
4978 | + * | |
4979 | + * @serial: Query ID assigned by ccs_supervisor(). | |
4980 | + * | |
4981 | + * Returns pointer to "struct ccs_domain_info" if found, NULL otherwise. | |
4982 | + */ | |
4983 | +static struct ccs_domain_info *ccs_find_domain_by_qid(unsigned int serial) | |
4984 | +{ | |
4985 | + struct ccs_query *ptr; | |
4986 | + struct ccs_domain_info *domain = NULL; | |
4987 | + spin_lock(&ccs_query_list_lock); | |
4988 | + list_for_each_entry(ptr, &ccs_query_list, list) { | |
4989 | + if (ptr->serial != serial) | |
4990 | + continue; | |
4991 | + domain = ptr->domain; | |
4992 | + break; | |
4993 | + } | |
4994 | + spin_unlock(&ccs_query_list_lock); | |
4995 | + return domain; | |
4996 | +} | |
4997 | + | |
4998 | +/** | |
4999 | + * ccs_read_query - Read access requests which violated policy in enforcing mode. | |
5000 | + * | |
5001 | + * @head: Pointer to "struct ccs_io_buffer". | |
5002 | + * | |
5003 | + * Returns nothing. | |
5004 | + */ | |
5005 | +static void ccs_read_query(struct ccs_io_buffer *head) | |
5006 | +{ | |
5007 | + struct list_head *tmp; | |
5008 | + unsigned int pos = 0; | |
5009 | + size_t len = 0; | |
5010 | + char *buf; | |
5011 | + if (head->r.w_pos) | |
5012 | + return; | |
5013 | + kfree(head->read_buf); | |
5014 | + head->read_buf = NULL; | |
5015 | + spin_lock(&ccs_query_list_lock); | |
5016 | + list_for_each(tmp, &ccs_query_list) { | |
5017 | + struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); | |
5018 | + if (pos++ != head->r.query_index) | |
5019 | + continue; | |
5020 | + len = ptr->query_len; | |
5021 | + break; | |
5022 | + } | |
5023 | + spin_unlock(&ccs_query_list_lock); | |
5024 | + if (!len) { | |
5025 | + head->r.query_index = 0; | |
5026 | + return; | |
5027 | + } | |
5028 | + buf = kzalloc(len + 32, CCS_GFP_FLAGS); | |
5029 | + if (!buf) | |
5030 | + return; | |
5031 | + pos = 0; | |
5032 | + spin_lock(&ccs_query_list_lock); | |
5033 | + list_for_each(tmp, &ccs_query_list) { | |
5034 | + struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); | |
5035 | + if (pos++ != head->r.query_index) | |
5036 | + continue; | |
5037 | + /* | |
5038 | + * Some query can be skipped because ccs_query_list | |
5039 | + * can change, but I don't care. | |
5040 | + */ | |
5041 | + if (len == ptr->query_len) | |
5042 | + snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial, | |
5043 | + ptr->retry, ptr->query); | |
5044 | + break; | |
5045 | + } | |
5046 | + spin_unlock(&ccs_query_list_lock); | |
5047 | + if (buf[0]) { | |
5048 | + head->read_buf = buf; | |
5049 | + head->r.w[head->r.w_pos++] = buf; | |
5050 | + head->r.query_index++; | |
5051 | + } else { | |
5052 | + kfree(buf); | |
5053 | + } | |
5054 | +} | |
5055 | + | |
5056 | +/** | |
5057 | + * ccs_write_answer - Write the supervisor's decision. | |
5058 | + * | |
5059 | + * @head: Pointer to "struct ccs_io_buffer". | |
5060 | + * | |
5061 | + * Returns 0 on success, -EINVAL otherwise. | |
5062 | + */ | |
5063 | +static int ccs_write_answer(struct ccs_io_buffer *head) | |
5064 | +{ | |
5065 | + char *data = head->write_buf; | |
5066 | + struct list_head *tmp; | |
5067 | + unsigned int serial; | |
5068 | + unsigned int answer; | |
5069 | + spin_lock(&ccs_query_list_lock); | |
5070 | + list_for_each(tmp, &ccs_query_list) { | |
5071 | + struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); | |
5072 | + ptr->timer = 0; | |
5073 | + } | |
5074 | + spin_unlock(&ccs_query_list_lock); | |
5075 | + if (sscanf(data, "A%u=%u", &serial, &answer) != 2) | |
5076 | + return -EINVAL; | |
5077 | + spin_lock(&ccs_query_list_lock); | |
5078 | + list_for_each(tmp, &ccs_query_list) { | |
5079 | + struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list); | |
5080 | + if (ptr->serial != serial) | |
5081 | + continue; | |
5082 | + ptr->answer = (u8) answer; | |
5083 | + /* Remove from ccs_query_list. */ | |
5084 | + if (ptr->answer) { | |
5085 | + list_del(&ptr->list); | |
5086 | + INIT_LIST_HEAD(&ptr->list); | |
5087 | + } | |
5088 | + break; | |
5089 | + } | |
5090 | + spin_unlock(&ccs_query_list_lock); | |
5091 | + wake_up_all(&ccs_answer_wait); | |
5092 | + return 0; | |
5093 | +} | |
5094 | + | |
5095 | +/** | |
5096 | + * ccs_read_version - Get version. | |
5097 | + * | |
5098 | + * @head: Pointer to "struct ccs_io_buffer". | |
5099 | + * | |
5100 | + * Returns nothing. | |
5101 | + */ | |
5102 | +static void ccs_read_version(struct ccs_io_buffer *head) | |
5103 | +{ | |
5104 | + if (head->r.eof) | |
5105 | + return; | |
5106 | + ccs_set_string(head, "1.8.5"); | |
5107 | + head->r.eof = true; | |
5108 | +} | |
5109 | + | |
5110 | +/** | |
5111 | + * ccs_update_stat - Update statistic counters. | |
5112 | + * | |
5113 | + * @index: Index for policy type. | |
5114 | + * | |
5115 | + * Returns nothing. | |
5116 | + */ | |
5117 | +static void ccs_update_stat(const u8 index) | |
5118 | +{ | |
5119 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
5120 | + struct timeval tv; | |
5121 | + do_gettimeofday(&tv); | |
5122 | + /* | |
5123 | + * I don't use atomic operations because race condition is not fatal. | |
5124 | + */ | |
5125 | + ccs_stat_updated[index]++; | |
5126 | + ccs_stat_modified[index] = tv.tv_sec; | |
5127 | +#else | |
5128 | + /* | |
5129 | + * I don't use atomic operations because race condition is not fatal. | |
5130 | + */ | |
5131 | + ccs_stat_updated[index]++; | |
5132 | + ccs_stat_modified[index] = get_seconds(); | |
5133 | +#endif | |
5134 | +} | |
5135 | + | |
5136 | +/** | |
5137 | + * ccs_read_stat - Read statistic data. | |
5138 | + * | |
5139 | + * @head: Pointer to "struct ccs_io_buffer". | |
5140 | + * | |
5141 | + * Returns nothing. | |
5142 | + */ | |
5143 | +static void ccs_read_stat(struct ccs_io_buffer *head) | |
5144 | +{ | |
5145 | + u8 i; | |
5146 | + unsigned int total = 0; | |
5147 | + if (head->r.eof) | |
5148 | + return; | |
5149 | + for (i = 0; i < CCS_MAX_POLICY_STAT; i++) { | |
5150 | + ccs_io_printf(head, "Policy %-30s %10u", ccs_policy_headers[i], | |
5151 | + ccs_stat_updated[i]); | |
5152 | + if (ccs_stat_modified[i]) { | |
5153 | + struct ccs_time stamp; | |
5154 | + ccs_convert_time(ccs_stat_modified[i], &stamp); | |
5155 | + ccs_io_printf(head, " (Last: %04u/%02u/%02u " | |
5156 | + "%02u:%02u:%02u)", | |
5157 | + stamp.year, stamp.month, stamp.day, | |
5158 | + stamp.hour, stamp.min, stamp.sec); | |
5159 | + } | |
5160 | + ccs_set_lf(head); | |
5161 | + } | |
5162 | + for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) { | |
5163 | + unsigned int used = ccs_memory_used[i]; | |
5164 | + total += used; | |
5165 | + ccs_io_printf(head, "Memory used by %-22s %10u", | |
5166 | + ccs_memory_headers[i], used); | |
5167 | + used = ccs_memory_quota[i]; | |
5168 | + if (used) | |
5169 | + ccs_io_printf(head, " (Quota: %10u)", used); | |
5170 | + ccs_set_lf(head); | |
5171 | + } | |
5172 | + ccs_io_printf(head, "Total memory used: %10u\n", | |
5173 | + total); | |
5174 | + head->r.eof = true; | |
5175 | +} | |
5176 | + | |
5177 | +/** | |
5178 | + * ccs_write_stat - Set memory quota. | |
5179 | + * | |
5180 | + * @head: Pointer to "struct ccs_io_buffer". | |
5181 | + * | |
5182 | + * Returns 0. | |
5183 | + */ | |
5184 | +static int ccs_write_stat(struct ccs_io_buffer *head) | |
5185 | +{ | |
5186 | + char *data = head->write_buf; | |
5187 | + u8 i; | |
5188 | + if (ccs_str_starts(&data, "Memory used by ")) | |
5189 | + for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) | |
5190 | + if (ccs_str_starts(&data, ccs_memory_headers[i])) { | |
5191 | + if (*data == ' ') | |
5192 | + data++; | |
5193 | + ccs_memory_quota[i] = | |
5194 | + simple_strtoul(data, NULL, 10); | |
5195 | + } | |
5196 | + return 0; | |
5197 | +} | |
5198 | + | |
5199 | +/** | |
5200 | + * ccs_print_bprm - Print "struct linux_binprm" for auditing. | |
5201 | + * | |
5202 | + * @bprm: Pointer to "struct linux_binprm". | |
5203 | + * @dump: Pointer to "struct ccs_page_dump". | |
5204 | + * | |
5205 | + * Returns the contents of @bprm on success, NULL otherwise. | |
5206 | + * | |
5207 | + * This function uses kzalloc(), so caller must kfree() if this function | |
5208 | + * didn't return NULL. | |
5209 | + */ | |
5210 | +static char *ccs_print_bprm(struct linux_binprm *bprm, | |
5211 | + struct ccs_page_dump *dump) | |
5212 | +{ | |
5213 | + static const int ccs_buffer_len = 4096 * 2; | |
5214 | + char *buffer = kzalloc(ccs_buffer_len, CCS_GFP_FLAGS); | |
5215 | + char *cp; | |
5216 | + char *last_start; | |
5217 | + int len; | |
5218 | + unsigned long pos = bprm->p; | |
5219 | + int offset = pos % PAGE_SIZE; | |
5220 | + int argv_count = bprm->argc; | |
5221 | + int envp_count = bprm->envc; | |
5222 | + bool truncated = false; | |
5223 | + if (!buffer) | |
5224 | + return NULL; | |
5225 | + len = snprintf(buffer, ccs_buffer_len - 1, "argv[]={ "); | |
5226 | + cp = buffer + len; | |
5227 | + if (!argv_count) { | |
5228 | + memmove(cp, "} envp[]={ ", 11); | |
5229 | + cp += 11; | |
5230 | + } | |
5231 | + last_start = cp; | |
5232 | + while (argv_count || envp_count) { | |
5233 | + if (!ccs_dump_page(bprm, pos, dump)) | |
5234 | + goto out; | |
5235 | + pos += PAGE_SIZE - offset; | |
5236 | + /* Read. */ | |
5237 | + while (offset < PAGE_SIZE) { | |
5238 | + const char *kaddr = dump->data; | |
5239 | + const unsigned char c = kaddr[offset++]; | |
5240 | + if (cp == last_start) | |
5241 | + *cp++ = '"'; | |
5242 | + if (cp >= buffer + ccs_buffer_len - 32) { | |
5243 | + /* Reserve some room for "..." string. */ | |
5244 | + truncated = true; | |
5245 | + } else if (c == '\\') { | |
5246 | + *cp++ = '\\'; | |
5247 | + *cp++ = '\\'; | |
5248 | + } else if (c > ' ' && c < 127) { | |
5249 | + *cp++ = c; | |
5250 | + } else if (!c) { | |
5251 | + *cp++ = '"'; | |
5252 | + *cp++ = ' '; | |
5253 | + last_start = cp; | |
5254 | + } else { | |
5255 | + *cp++ = '\\'; | |
5256 | + *cp++ = (c >> 6) + '0'; | |
5257 | + *cp++ = ((c >> 3) & 7) + '0'; | |
5258 | + *cp++ = (c & 7) + '0'; | |
5259 | + } | |
5260 | + if (c) | |
5261 | + continue; | |
5262 | + if (argv_count) { | |
5263 | + if (--argv_count == 0) { | |
5264 | + if (truncated) { | |
5265 | + cp = last_start; | |
5266 | + memmove(cp, "... ", 4); | |
5267 | + cp += 4; | |
5268 | + } | |
5269 | + memmove(cp, "} envp[]={ ", 11); | |
5270 | + cp += 11; | |
5271 | + last_start = cp; | |
5272 | + truncated = false; | |
5273 | + } | |
5274 | + } else if (envp_count) { | |
5275 | + if (--envp_count == 0) { | |
5276 | + if (truncated) { | |
5277 | + cp = last_start; | |
5278 | + memmove(cp, "... ", 4); | |
5279 | + cp += 4; | |
5280 | + } | |
5281 | + } | |
5282 | + } | |
5283 | + if (!argv_count && !envp_count) | |
5284 | + break; | |
5285 | + } | |
5286 | + offset = 0; | |
5287 | + } | |
5288 | + *cp++ = '}'; | |
5289 | + *cp = '\0'; | |
5290 | + return buffer; | |
5291 | +out: | |
5292 | + snprintf(buffer, ccs_buffer_len - 1, "argv[]={ ... } envp[]= { ... }"); | |
5293 | + return buffer; | |
5294 | +} | |
5295 | + | |
5296 | +/** | |
5297 | + * ccs_filetype - Get string representation of file type. | |
5298 | + * | |
5299 | + * @mode: Mode value for stat(). | |
5300 | + * | |
5301 | + * Returns file type string. | |
5302 | + */ | |
5303 | +static inline const char *ccs_filetype(const umode_t mode) | |
5304 | +{ | |
5305 | + switch (mode & S_IFMT) { | |
5306 | + case S_IFREG: | |
5307 | + case 0: | |
5308 | + return ccs_condition_keyword[CCS_TYPE_IS_FILE]; | |
5309 | + case S_IFDIR: | |
5310 | + return ccs_condition_keyword[CCS_TYPE_IS_DIRECTORY]; | |
5311 | + case S_IFLNK: | |
5312 | + return ccs_condition_keyword[CCS_TYPE_IS_SYMLINK]; | |
5313 | + case S_IFIFO: | |
5314 | + return ccs_condition_keyword[CCS_TYPE_IS_FIFO]; | |
5315 | + case S_IFSOCK: | |
5316 | + return ccs_condition_keyword[CCS_TYPE_IS_SOCKET]; | |
5317 | + case S_IFBLK: | |
5318 | + return ccs_condition_keyword[CCS_TYPE_IS_BLOCK_DEV]; | |
5319 | + case S_IFCHR: | |
5320 | + return ccs_condition_keyword[CCS_TYPE_IS_CHAR_DEV]; | |
5321 | + } | |
5322 | + return "unknown"; /* This should not happen. */ | |
5323 | +} | |
5324 | + | |
5325 | +/** | |
5326 | + * ccs_print_header - Get header line of audit log. | |
5327 | + * | |
5328 | + * @r: Pointer to "struct ccs_request_info". | |
5329 | + * | |
5330 | + * Returns string representation. | |
5331 | + * | |
5332 | + * This function uses kmalloc(), so caller must kfree() if this function | |
5333 | + * didn't return NULL. | |
5334 | + */ | |
5335 | +static char *ccs_print_header(struct ccs_request_info *r) | |
5336 | +{ | |
5337 | + struct ccs_time stamp; | |
5338 | + struct ccs_obj_info *obj = r->obj; | |
5339 | + const u32 ccs_flags = ccs_current_flags(); | |
5340 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) | |
5341 | + const pid_t gpid = ccs_sys_getpid(); | |
5342 | +#else | |
5343 | + const pid_t gpid = task_pid_nr(current); | |
5344 | +#endif | |
5345 | + static const int ccs_buffer_len = 4096; | |
5346 | + char *buffer = kmalloc(ccs_buffer_len, CCS_GFP_FLAGS); | |
5347 | + int pos; | |
5348 | + u8 i; | |
5349 | + if (!buffer) | |
5350 | + return NULL; | |
5351 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
5352 | + { | |
5353 | + struct timeval tv; | |
5354 | + do_gettimeofday(&tv); | |
5355 | + ccs_convert_time(tv.tv_sec, &stamp); | |
5356 | + } | |
5357 | +#else | |
5358 | + ccs_convert_time(get_seconds(), &stamp); | |
5359 | +#endif | |
5360 | + pos = snprintf(buffer, ccs_buffer_len - 1, | |
5361 | + "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s " | |
5362 | + "granted=%s (global-pid=%u) task={ pid=%u ppid=%u " | |
5363 | + "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u " | |
5364 | + "fsuid=%u fsgid=%u type%s=execute_handler }", | |
5365 | + stamp.year, stamp.month, stamp.day, stamp.hour, | |
5366 | + stamp.min, stamp.sec, r->profile, ccs_mode[r->mode], | |
5367 | + ccs_yesno(r->granted), gpid, ccs_sys_getpid(), | |
5368 | + ccs_sys_getppid(), | |
5369 | + from_kuid(&init_user_ns, current_uid()), | |
5370 | + from_kgid(&init_user_ns, current_gid()), | |
5371 | + from_kuid(&init_user_ns, current_euid()), | |
5372 | + from_kgid(&init_user_ns, current_egid()), | |
5373 | + from_kuid(&init_user_ns, current_suid()), | |
5374 | + from_kgid(&init_user_ns, current_sgid()), | |
5375 | + from_kuid(&init_user_ns, current_fsuid()), | |
5376 | + from_kgid(&init_user_ns, current_fsgid()), | |
5377 | + ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ? "" : "!"); | |
5378 | + if (!obj) | |
5379 | + goto no_obj_info; | |
5380 | + if (!obj->validate_done) { | |
5381 | + ccs_get_attributes(obj); | |
5382 | + obj->validate_done = true; | |
5383 | + } | |
5384 | + for (i = 0; i < CCS_MAX_PATH_STAT; i++) { | |
5385 | + struct ccs_mini_stat *stat; | |
5386 | + unsigned int dev; | |
5387 | + umode_t mode; | |
5388 | + if (!obj->stat_valid[i]) | |
5389 | + continue; | |
5390 | + stat = &obj->stat[i]; | |
5391 | + dev = stat->dev; | |
5392 | + mode = stat->mode; | |
5393 | + if (i & 1) { | |
5394 | + pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos, | |
5395 | + " path%u.parent={ uid=%u gid=%u " | |
5396 | + "ino=%lu perm=0%o }", (i >> 1) + 1, | |
5397 | + from_kuid(&init_user_ns, stat->uid), | |
5398 | + from_kgid(&init_user_ns, stat->gid), | |
5399 | + (unsigned long) stat->ino, | |
5400 | + stat->mode & S_IALLUGO); | |
5401 | + continue; | |
5402 | + } | |
5403 | + pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos, | |
5404 | + " path%u={ uid=%u gid=%u ino=%lu major=%u" | |
5405 | + " minor=%u perm=0%o type=%s", (i >> 1) + 1, | |
5406 | + from_kuid(&init_user_ns, stat->uid), | |
5407 | + from_kgid(&init_user_ns, stat->gid), | |
5408 | + (unsigned long) stat->ino, MAJOR(dev), | |
5409 | + MINOR(dev), mode & S_IALLUGO, | |
5410 | + ccs_filetype(mode)); | |
5411 | + if (S_ISCHR(mode) || S_ISBLK(mode)) { | |
5412 | + dev = stat->rdev; | |
5413 | + pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos, | |
5414 | + " dev_major=%u dev_minor=%u", | |
5415 | + MAJOR(dev), MINOR(dev)); | |
5416 | + } | |
5417 | + pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos, " }"); | |
5418 | + } | |
5419 | +no_obj_info: | |
5420 | + if (pos < ccs_buffer_len - 1) | |
5421 | + return buffer; | |
5422 | + kfree(buffer); | |
5423 | + return NULL; | |
5424 | +} | |
5425 | + | |
5426 | +/** | |
5427 | + * ccs_init_log - Allocate buffer for audit logs. | |
5428 | + * | |
5429 | + * @r: Pointer to "struct ccs_request_info". | |
5430 | + * @len: Buffer size needed for @fmt and @args. | |
5431 | + * @fmt: The printf()'s format string. | |
5432 | + * @args: va_list structure for @fmt. | |
5433 | + * | |
5434 | + * Returns pointer to allocated memory. | |
5435 | + * | |
5436 | + * This function uses kzalloc(), so caller must kfree() if this function | |
5437 | + * didn't return NULL. | |
5438 | + */ | |
5439 | +static char *ccs_init_log(struct ccs_request_info *r, int len, const char *fmt, | |
5440 | + va_list args) | |
5441 | +{ | |
5442 | + char *buf = NULL; | |
5443 | + char *bprm_info = NULL; | |
5444 | + char *realpath = NULL; | |
5445 | + const char *symlink = NULL; | |
5446 | + const char *header = NULL; | |
5447 | + int pos; | |
5448 | + const char *domainname = ccs_current_domain()->domainname->name; | |
5449 | + header = ccs_print_header(r); | |
5450 | + if (!header) | |
5451 | + return NULL; | |
5452 | + /* +10 is for '\n' etc. and '\0'. */ | |
5453 | + len += strlen(domainname) + strlen(header) + 10; | |
5454 | + if (r->ee) { | |
5455 | + struct file *file = r->ee->bprm->file; | |
5456 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) | |
5457 | + struct path path = { file->f_vfsmnt, file->f_dentry }; | |
5458 | + realpath = ccs_realpath(&path); | |
5459 | +#else | |
5460 | + realpath = ccs_realpath(&file->f_path); | |
5461 | +#endif | |
5462 | + bprm_info = ccs_print_bprm(r->ee->bprm, &r->ee->dump); | |
5463 | + if (!realpath || !bprm_info) | |
5464 | + goto out; | |
5465 | + /* +80 is for " exec={ realpath=\"%s\" argc=%d envc=%d %s }" */ | |
5466 | + len += strlen(realpath) + 80 + strlen(bprm_info); | |
5467 | + } else if (r->obj && r->obj->symlink_target) { | |
5468 | + symlink = r->obj->symlink_target->name; | |
5469 | + /* +18 is for " symlink.target=\"%s\"" */ | |
5470 | + len += 18 + strlen(symlink); | |
5471 | + } | |
5472 | + len = ccs_round2(len); | |
5473 | + buf = kzalloc(len, CCS_GFP_FLAGS); | |
5474 | + if (!buf) | |
5475 | + goto out; | |
5476 | + len--; | |
5477 | + pos = snprintf(buf, len, "%s", header); | |
5478 | + if (realpath) { | |
5479 | + struct linux_binprm *bprm = r->ee->bprm; | |
5480 | + pos += snprintf(buf + pos, len - pos, | |
5481 | + " exec={ realpath=\"%s\" argc=%d envc=%d %s }", | |
5482 | + realpath, bprm->argc, bprm->envc, bprm_info); | |
5483 | + } else if (symlink) | |
5484 | + pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"", | |
5485 | + symlink); | |
5486 | + pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname); | |
5487 | + vsnprintf(buf + pos, len - pos, fmt, args); | |
5488 | +out: | |
5489 | + kfree(realpath); | |
5490 | + kfree(bprm_info); | |
5491 | + kfree(header); | |
5492 | + return buf; | |
5493 | +} | |
5494 | + | |
5495 | +/** | |
5496 | + * ccs_transition_failed - Print waning message and send signal when domain transition failed. | |
5497 | + * | |
5498 | + * @domainname: Name of domain to transit. | |
5499 | + * | |
5500 | + * Returns nothing. | |
5501 | + * | |
5502 | + * Note that if current->pid == 1, sending SIGKILL won't work. | |
5503 | + */ | |
5504 | +void ccs_transition_failed(const char *domainname) | |
5505 | +{ | |
5506 | + printk(KERN_WARNING | |
5507 | + "ERROR: Unable to transit to '%s' domain.\n", domainname); | |
5508 | + force_sig(SIGKILL, current); | |
5509 | +} | |
5510 | + | |
5511 | +/** | |
5512 | + * ccs_update_task_domain - Update task's domain. | |
5513 | + * | |
5514 | + * @r: Pointer to "struct ccs_request_info". | |
5515 | + * | |
5516 | + * Returns nothing. | |
5517 | + * | |
5518 | + * The task will retry as hard as possible. But if domain transition failed, | |
5519 | + * the task will be killed by SIGKILL. | |
5520 | + */ | |
5521 | +static void ccs_update_task_domain(struct ccs_request_info *r) | |
5522 | +{ | |
5523 | + char *buf; | |
5524 | + const char *cp; | |
5525 | + const struct ccs_acl_info *acl = r->matched_acl; | |
5526 | + r->matched_acl = NULL; | |
5527 | + if (!acl || !acl->cond || !acl->cond->transit || | |
5528 | + acl->cond->exec_transit) | |
5529 | + return; | |
5530 | + while (1) { | |
5531 | + buf = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS); | |
5532 | + if (buf) | |
5533 | + break; | |
5534 | + ssleep(1); | |
5535 | + if (fatal_signal_pending(current)) | |
5536 | + return; | |
5537 | + } | |
5538 | + cp = acl->cond->transit->name; | |
5539 | + if (*cp == '/') | |
5540 | + snprintf(buf, CCS_EXEC_TMPSIZE - 1, "%s %s", | |
5541 | + ccs_current_domain()->domainname->name, cp); | |
5542 | + else | |
5543 | + strncpy(buf, cp, CCS_EXEC_TMPSIZE - 1); | |
5544 | + if (!ccs_assign_domain(buf, true)) | |
5545 | + ccs_transition_failed(buf); | |
5546 | + kfree(buf); | |
5547 | +} | |
5548 | + | |
5549 | +/** | |
5550 | + * ccs_get_audit - Get audit mode. | |
5551 | + * | |
5552 | + * @r: Pointer to "struct ccs_request_info". | |
5553 | + * | |
5554 | + * Returns true if this request should be audited, false otherwise. | |
5555 | + */ | |
5556 | +static bool ccs_get_audit(const struct ccs_request_info *r) | |
5557 | +{ | |
5558 | + const struct ccs_acl_info *matched_acl = r->matched_acl; | |
5559 | + const u8 profile = r->profile; | |
5560 | + const u8 index = r->type; | |
5561 | + const bool is_granted = r->granted; | |
5562 | + u8 mode; | |
5563 | + struct ccs_profile *p; | |
5564 | + if (!ccs_policy_loaded) | |
5565 | + return false; | |
5566 | + p = ccs_profile(profile); | |
5567 | + if (ccs_log_count >= p->pref[CCS_PREF_MAX_AUDIT_LOG]) | |
5568 | + return false; | |
5569 | + if (is_granted && matched_acl && matched_acl->cond && | |
5570 | + matched_acl->cond->grant_log != CCS_GRANTLOG_AUTO) | |
5571 | + return matched_acl->cond->grant_log == CCS_GRANTLOG_YES; | |
5572 | + mode = p->config[index]; | |
5573 | + if (mode == CCS_CONFIG_USE_DEFAULT) | |
5574 | + mode = p->config | |
5575 | + [ccs_index2category[index] + CCS_MAX_MAC_INDEX]; | |
5576 | + if (mode == CCS_CONFIG_USE_DEFAULT) | |
5577 | + mode = p->default_config; | |
5578 | + if (is_granted) | |
5579 | + return mode & CCS_CONFIG_WANT_GRANT_LOG; | |
5580 | + return mode & CCS_CONFIG_WANT_REJECT_LOG; | |
5581 | +} | |
5582 | + | |
5583 | +/** | |
5584 | + * ccs_write_log2 - Write an audit log. | |
5585 | + * | |
5586 | + * @r: Pointer to "struct ccs_request_info". | |
5587 | + * @len: Buffer size needed for @fmt and @args. | |
5588 | + * @fmt: The printf()'s format string. | |
5589 | + * @args: va_list structure for @fmt. | |
5590 | + * | |
5591 | + * Returns nothing. | |
5592 | + */ | |
5593 | +static void ccs_write_log2(struct ccs_request_info *r, int len, | |
5594 | + const char *fmt, va_list args) | |
5595 | +{ | |
5596 | + char *buf; | |
5597 | + struct ccs_log *entry; | |
5598 | + bool quota_exceeded = false; | |
5599 | + if (!ccs_get_audit(r)) | |
5600 | + goto out; | |
5601 | + buf = ccs_init_log(r, len, fmt, args); | |
5602 | + if (!buf) | |
5603 | + goto out; | |
5604 | + entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS); | |
5605 | + if (!entry) { | |
5606 | + kfree(buf); | |
5607 | + goto out; | |
5608 | + } | |
5609 | + entry->log = buf; | |
5610 | + len = ccs_round2(strlen(buf) + 1); | |
5611 | + /* | |
5612 | + * The entry->size is used for memory quota checks. | |
5613 | + * Don't go beyond strlen(entry->log). | |
5614 | + */ | |
5615 | + entry->size = len + ccs_round2(sizeof(*entry)); | |
5616 | + spin_lock(&ccs_log_lock); | |
5617 | + if (ccs_memory_quota[CCS_MEMORY_AUDIT] && | |
5618 | + ccs_memory_used[CCS_MEMORY_AUDIT] + entry->size >= | |
5619 | + ccs_memory_quota[CCS_MEMORY_AUDIT]) { | |
5620 | + quota_exceeded = true; | |
5621 | + } else { | |
5622 | + ccs_memory_used[CCS_MEMORY_AUDIT] += entry->size; | |
5623 | + list_add_tail(&entry->list, &ccs_log); | |
5624 | + ccs_log_count++; | |
5625 | + } | |
5626 | + spin_unlock(&ccs_log_lock); | |
5627 | + if (quota_exceeded) { | |
5628 | + kfree(buf); | |
5629 | + kfree(entry); | |
5630 | + goto out; | |
5631 | + } | |
5632 | + wake_up(&ccs_log_wait); | |
5633 | +out: | |
5634 | + ccs_update_task_domain(r); | |
5635 | +} | |
5636 | + | |
5637 | +/** | |
5638 | + * ccs_write_log - Write an audit log. | |
5639 | + * | |
5640 | + * @r: Pointer to "struct ccs_request_info". | |
5641 | + * @fmt: The printf()'s format string, followed by parameters. | |
5642 | + * | |
5643 | + * Returns nothing. | |
5644 | + */ | |
5645 | +void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...) | |
5646 | +{ | |
5647 | + va_list args; | |
5648 | + int len; | |
5649 | + va_start(args, fmt); | |
5650 | + len = vsnprintf((char *) &len, 1, fmt, args) + 1; | |
5651 | + va_end(args); | |
5652 | + va_start(args, fmt); | |
5653 | + ccs_write_log2(r, len, fmt, args); | |
5654 | + va_end(args); | |
5655 | +} | |
5656 | + | |
5657 | +/** | |
5658 | + * ccs_read_log - Read an audit log. | |
5659 | + * | |
5660 | + * @head: Pointer to "struct ccs_io_buffer". | |
5661 | + * | |
5662 | + * Returns nothing. | |
5663 | + */ | |
5664 | +static void ccs_read_log(struct ccs_io_buffer *head) | |
5665 | +{ | |
5666 | + struct ccs_log *ptr = NULL; | |
5667 | + if (head->r.w_pos) | |
5668 | + return; | |
5669 | + kfree(head->read_buf); | |
5670 | + head->read_buf = NULL; | |
5671 | + spin_lock(&ccs_log_lock); | |
5672 | + if (!list_empty(&ccs_log)) { | |
5673 | + ptr = list_entry(ccs_log.next, typeof(*ptr), list); | |
5674 | + list_del(&ptr->list); | |
5675 | + ccs_log_count--; | |
5676 | + ccs_memory_used[CCS_MEMORY_AUDIT] -= ptr->size; | |
5677 | + } | |
5678 | + spin_unlock(&ccs_log_lock); | |
5679 | + if (ptr) { | |
5680 | + head->read_buf = ptr->log; | |
5681 | + head->r.w[head->r.w_pos++] = head->read_buf; | |
5682 | + kfree(ptr); | |
5683 | + } | |
5684 | +} | |
5685 | + | |
5686 | +/** | |
5687 | + * ccs_set_namespace_cursor - Set namespace to read. | |
5688 | + * | |
5689 | + * @head: Pointer to "struct ccs_io_buffer". | |
5690 | + * | |
5691 | + * Returns nothing. | |
5692 | + */ | |
5693 | +static void ccs_set_namespace_cursor(struct ccs_io_buffer *head) | |
5694 | +{ | |
5695 | + struct list_head *ns; | |
5696 | + if (head->type != CCS_EXCEPTION_POLICY && head->type != CCS_PROFILE) | |
5697 | + return; | |
5698 | + /* | |
5699 | + * If this is the first read, or reading previous namespace finished | |
5700 | + * and has more namespaces to read, update the namespace cursor. | |
5701 | + */ | |
5702 | + ns = head->r.ns; | |
5703 | + if (!ns || (head->r.eof && ns->next != &ccs_namespace_list)) { | |
5704 | + /* Clearing is OK because ccs_flush() returned true. */ | |
5705 | + memset(&head->r, 0, sizeof(head->r)); | |
5706 | + head->r.ns = ns ? ns->next : ccs_namespace_list.next; | |
5707 | + } | |
5708 | +} | |
5709 | + | |
5710 | +/** | |
5711 | + * ccs_has_more_namespace - Check for unread namespaces. | |
5712 | + * | |
5713 | + * @head: Pointer to "struct ccs_io_buffer". | |
5714 | + * | |
5715 | + * Returns true if we have more entries to print, false otherwise. | |
5716 | + */ | |
5717 | +static bool ccs_has_more_namespace(struct ccs_io_buffer *head) | |
5718 | +{ | |
5719 | + return (head->type == CCS_EXCEPTION_POLICY || | |
5720 | + head->type == CCS_PROFILE) && head->r.eof && | |
5721 | + head->r.ns->next != &ccs_namespace_list; | |
5722 | +} | |
5723 | + | |
5724 | +/** | |
5725 | + * ccs_find_namespace - Find specified namespace. | |
5726 | + * | |
5727 | + * @name: Name of namespace to find. | |
5728 | + * @len: Length of @name. | |
5729 | + * | |
5730 | + * Returns pointer to "struct ccs_policy_namespace" if found, NULL otherwise. | |
5731 | + * | |
5732 | + * Caller holds ccs_read_lock(). | |
5733 | + */ | |
5734 | +static struct ccs_policy_namespace *ccs_find_namespace(const char *name, | |
5735 | + const unsigned int len) | |
5736 | +{ | |
5737 | + struct ccs_policy_namespace *ns; | |
5738 | + list_for_each_entry_srcu(ns, &ccs_namespace_list, namespace_list, | |
5739 | + &ccs_ss) { | |
5740 | + if (strncmp(name, ns->name, len) || | |
5741 | + (name[len] && name[len] != ' ')) | |
5742 | + continue; | |
5743 | + return ns; | |
5744 | + } | |
5745 | + return NULL; | |
5746 | +} | |
5747 | + | |
5748 | +/** | |
5749 | + * ccs_assign_namespace - Create a new namespace. | |
5750 | + * | |
5751 | + * @domainname: Name of namespace to create. | |
5752 | + * | |
5753 | + * Returns pointer to "struct ccs_policy_namespace" on success, NULL otherwise. | |
5754 | + * | |
5755 | + * Caller holds ccs_read_lock(). | |
5756 | + */ | |
5757 | +static struct ccs_policy_namespace *ccs_assign_namespace | |
5758 | +(const char *domainname) | |
5759 | +{ | |
5760 | + struct ccs_policy_namespace *ptr; | |
5761 | + struct ccs_policy_namespace *entry; | |
5762 | + const char *cp = domainname; | |
5763 | + unsigned int len = 0; | |
5764 | + while (*cp && *cp++ != ' ') | |
5765 | + len++; | |
5766 | + ptr = ccs_find_namespace(domainname, len); | |
5767 | + if (ptr) | |
5768 | + return ptr; | |
5769 | + if (len >= CCS_EXEC_TMPSIZE - 10 || !ccs_domain_def(domainname)) | |
5770 | + return NULL; | |
5771 | + entry = kzalloc(sizeof(*entry) + len + 1, CCS_GFP_FLAGS); | |
5772 | + if (!entry) | |
5773 | + return NULL; | |
5774 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
5775 | + goto out; | |
5776 | + ptr = ccs_find_namespace(domainname, len); | |
5777 | + if (!ptr && ccs_memory_ok(entry, sizeof(*entry) + len + 1)) { | |
5778 | + char *name = (char *) (entry + 1); | |
5779 | + ptr = entry; | |
5780 | + memmove(name, domainname, len); | |
5781 | + name[len] = '\0'; | |
5782 | + entry->name = name; | |
5783 | + ccs_init_policy_namespace(entry); | |
5784 | + entry = NULL; | |
5785 | + } | |
5786 | + mutex_unlock(&ccs_policy_lock); | |
5787 | +out: | |
5788 | + kfree(entry); | |
5789 | + return ptr; | |
5790 | +} | |
5791 | + | |
5792 | +/** | |
5793 | + * ccs_namespace_jump - Check for namespace jump. | |
5794 | + * | |
5795 | + * @domainname: Name of domain. | |
5796 | + * | |
5797 | + * Returns true if namespace differs, false otherwise. | |
5798 | + */ | |
5799 | +static bool ccs_namespace_jump(const char *domainname) | |
5800 | +{ | |
5801 | + const char *namespace = ccs_current_namespace()->name; | |
5802 | + const int len = strlen(namespace); | |
5803 | + return strncmp(domainname, namespace, len) || | |
5804 | + (domainname[len] && domainname[len] != ' '); | |
5805 | +} | |
5806 | + | |
5807 | +/** | |
5808 | + * ccs_assign_domain - Create a domain or a namespace. | |
5809 | + * | |
5810 | + * @domainname: The name of domain. | |
5811 | + * @transit: True if transit to domain found or created. | |
5812 | + * | |
5813 | + * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise. | |
5814 | + * | |
5815 | + * Caller holds ccs_read_lock(). | |
5816 | + */ | |
5817 | +struct ccs_domain_info *ccs_assign_domain(const char *domainname, | |
5818 | + const bool transit) | |
5819 | +{ | |
5820 | + struct ccs_security *security = ccs_current_security(); | |
5821 | + struct ccs_domain_info e = { }; | |
5822 | + struct ccs_domain_info *entry = ccs_find_domain(domainname); | |
5823 | + bool created = false; | |
5824 | + if (entry) { | |
5825 | + if (transit) { | |
5826 | + /* | |
5827 | + * Since namespace is created at runtime, profiles may | |
5828 | + * not be created by the moment the process transits to | |
5829 | + * that domain. Do not perform domain transition if | |
5830 | + * profile for that domain is not yet created. | |
5831 | + */ | |
5832 | + if (ccs_policy_loaded && | |
5833 | + !entry->ns->profile_ptr[entry->profile]) | |
5834 | + return NULL; | |
5835 | + security->ccs_domain_info = entry; | |
5836 | + } | |
5837 | + return entry; | |
5838 | + } | |
5839 | + /* Requested domain does not exist. */ | |
5840 | + /* Don't create requested domain if domainname is invalid. */ | |
5841 | + if (strlen(domainname) >= CCS_EXEC_TMPSIZE - 10 || | |
5842 | + !ccs_correct_domain(domainname)) | |
5843 | + return NULL; | |
5844 | + /* | |
5845 | + * Since definition of profiles and acl_groups may differ across | |
5846 | + * namespaces, do not inherit "use_profile" and "use_group" settings | |
5847 | + * by automatically creating requested domain upon domain transition. | |
5848 | + */ | |
5849 | + if (transit && ccs_namespace_jump(domainname)) | |
5850 | + return NULL; | |
5851 | + e.ns = ccs_assign_namespace(domainname); | |
5852 | + if (!e.ns) | |
5853 | + return NULL; | |
5854 | + /* | |
5855 | + * "use_profile" and "use_group" settings for automatically created | |
5856 | + * domains are inherited from current domain. These are 0 for manually | |
5857 | + * created domains. | |
5858 | + */ | |
5859 | + if (transit) { | |
5860 | + const struct ccs_domain_info *domain = | |
5861 | + security->ccs_domain_info; | |
5862 | + e.profile = domain->profile; | |
5863 | + memcpy(e.group, domain->group, sizeof(e.group)); | |
5864 | + } | |
5865 | + e.domainname = ccs_get_name(domainname); | |
5866 | + if (!e.domainname) | |
5867 | + return NULL; | |
5868 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
5869 | + goto out; | |
5870 | + entry = ccs_find_domain(domainname); | |
5871 | + if (!entry) { | |
5872 | + entry = ccs_commit_ok(&e, sizeof(e)); | |
5873 | + if (entry) { | |
5874 | + INIT_LIST_HEAD(&entry->acl_info_list); | |
5875 | + list_add_tail_rcu(&entry->list, &ccs_domain_list); | |
5876 | + created = true; | |
5877 | + } | |
5878 | + } | |
5879 | + mutex_unlock(&ccs_policy_lock); | |
5880 | +out: | |
5881 | + ccs_put_name(e.domainname); | |
5882 | + if (entry && transit) { | |
5883 | + security->ccs_domain_info = entry; | |
5884 | + if (created) { | |
5885 | + struct ccs_request_info r; | |
5886 | + int i; | |
5887 | + ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE); | |
5888 | + r.granted = false; | |
5889 | + ccs_write_log(&r, "use_profile %u\n", entry->profile); | |
5890 | + for (i = 0; i < CCS_MAX_ACL_GROUPS; i++) | |
5891 | + if (test_bit(i, entry->group)) | |
5892 | + ccs_write_log(&r, "use_group %u\n", i); | |
5893 | + ccs_update_stat(CCS_STAT_POLICY_UPDATES); | |
5894 | + } | |
5895 | + } | |
5896 | + return entry; | |
5897 | +} | |
5898 | + | |
5899 | +/** | |
5900 | + * ccs_parse_policy - Parse a policy line. | |
5901 | + * | |
5902 | + * @head: Pointer to "struct ccs_io_buffer". | |
5903 | + * @line: Line to parse. | |
5904 | + * | |
5905 | + * Returns 0 on success, negative value otherwise. | |
5906 | + * | |
5907 | + * Caller holds ccs_read_lock(). | |
5908 | + */ | |
5909 | +static int ccs_parse_policy(struct ccs_io_buffer *head, char *line) | |
5910 | +{ | |
5911 | + /* Delete request? */ | |
5912 | + head->w.is_delete = !strncmp(line, "delete ", 7); | |
5913 | + if (head->w.is_delete) | |
5914 | + memmove(line, line + 7, strlen(line + 7) + 1); | |
5915 | + /* Selecting namespace to update. */ | |
5916 | + if (head->type == CCS_EXCEPTION_POLICY || head->type == CCS_PROFILE) { | |
5917 | + if (*line == '<') { | |
5918 | + char *cp = strchr(line, ' '); | |
5919 | + if (cp) { | |
5920 | + *cp++ = '\0'; | |
5921 | + head->w.ns = ccs_assign_namespace(line); | |
5922 | + memmove(line, cp, strlen(cp) + 1); | |
5923 | + } else | |
5924 | + head->w.ns = NULL; | |
5925 | + } else | |
5926 | + head->w.ns = &ccs_kernel_namespace; | |
5927 | + /* Don't allow updating if namespace is invalid. */ | |
5928 | + if (!head->w.ns) | |
5929 | + return -ENOENT; | |
5930 | + } | |
5931 | + /* Do the update. */ | |
5932 | + switch (head->type) { | |
5933 | + case CCS_DOMAIN_POLICY: | |
5934 | + return ccs_write_domain(head); | |
5935 | + case CCS_EXCEPTION_POLICY: | |
5936 | + return ccs_write_exception(head); | |
5937 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
5938 | + case CCS_EXECUTE_HANDLER: | |
5939 | +#endif | |
5940 | + case CCS_PROCESS_STATUS: | |
5941 | + return ccs_write_pid(head); | |
5942 | + case CCS_STAT: | |
5943 | + return ccs_write_stat(head); | |
5944 | + case CCS_PROFILE: | |
5945 | + return ccs_write_profile(head); | |
5946 | + case CCS_QUERY: | |
5947 | + return ccs_write_answer(head); | |
5948 | + case CCS_MANAGER: | |
5949 | + return ccs_write_manager(head); | |
5950 | + default: | |
5951 | + return -ENOSYS; | |
5952 | + } | |
5953 | +} | |
5954 | + | |
5955 | +/** | |
5956 | + * ccs_policy_io_init - Register hooks for policy I/O. | |
5957 | + * | |
5958 | + * Returns nothing. | |
5959 | + */ | |
5960 | +static void __init ccs_policy_io_init(void) | |
5961 | +{ | |
5962 | + ccsecurity_ops.check_profile = ccs_check_profile; | |
5963 | +} | |
5964 | + | |
5965 | +/** | |
5966 | + * ccs_load_builtin_policy - Load built-in policy. | |
5967 | + * | |
5968 | + * Returns nothing. | |
5969 | + */ | |
5970 | +static void __init ccs_load_builtin_policy(void) | |
5971 | +{ | |
5972 | + /* | |
5973 | + * This include file is manually created and contains built-in policy | |
5974 | + * named "ccs_builtin_profile", "ccs_builtin_exception_policy", | |
5975 | + * "ccs_builtin_domain_policy", "ccs_builtin_manager", | |
5976 | + * "ccs_builtin_stat" in the form of "static char [] __initdata". | |
5977 | + */ | |
5978 | +#include "builtin-policy.h" | |
5979 | + u8 i; | |
5980 | + const int idx = ccs_read_lock(); | |
5981 | + for (i = 0; i < 5; i++) { | |
5982 | + struct ccs_io_buffer head = { }; | |
5983 | + char *start = ""; | |
5984 | + switch (i) { | |
5985 | + case 0: | |
5986 | + start = ccs_builtin_profile; | |
5987 | + head.type = CCS_PROFILE; | |
5988 | + break; | |
5989 | + case 1: | |
5990 | + start = ccs_builtin_exception_policy; | |
5991 | + head.type = CCS_EXCEPTION_POLICY; | |
5992 | + break; | |
5993 | + case 2: | |
5994 | + start = ccs_builtin_domain_policy; | |
5995 | + head.type = CCS_DOMAIN_POLICY; | |
5996 | + break; | |
5997 | + case 3: | |
5998 | + start = ccs_builtin_manager; | |
5999 | + head.type = CCS_MANAGER; | |
6000 | + break; | |
6001 | + case 4: | |
6002 | + start = ccs_builtin_stat; | |
6003 | + head.type = CCS_STAT; | |
6004 | + break; | |
6005 | + } | |
6006 | + while (1) { | |
6007 | + char *end = strchr(start, '\n'); | |
6008 | + if (!end) | |
6009 | + break; | |
6010 | + *end = '\0'; | |
6011 | + ccs_normalize_line(start); | |
6012 | + head.write_buf = start; | |
6013 | + ccs_parse_policy(&head, start); | |
6014 | + start = end + 1; | |
6015 | + } | |
6016 | + } | |
6017 | + ccs_read_unlock(idx); | |
6018 | +#ifdef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
6019 | + ccs_check_profile(); | |
6020 | +#endif | |
6021 | +} | |
6022 | + | |
6023 | +/** | |
6024 | + * ccs_read_self - read() for /proc/ccs/self_domain interface. | |
6025 | + * | |
6026 | + * @file: Pointer to "struct file". | |
6027 | + * @buf: Domainname which current thread belongs to. | |
6028 | + * @count: Size of @buf. | |
6029 | + * @ppos: Bytes read by now. | |
6030 | + * | |
6031 | + * Returns read size on success, negative value otherwise. | |
6032 | + */ | |
6033 | +static ssize_t ccs_read_self(struct file *file, char __user *buf, size_t count, | |
6034 | + loff_t *ppos) | |
6035 | +{ | |
6036 | + const char *domain = ccs_current_domain()->domainname->name; | |
6037 | + loff_t len = strlen(domain); | |
6038 | + loff_t pos = *ppos; | |
6039 | + if (pos >= len || !count) | |
6040 | + return 0; | |
6041 | + len -= pos; | |
6042 | + if (count < len) | |
6043 | + len = count; | |
6044 | + if (copy_to_user(buf, domain + pos, len)) | |
6045 | + return -EFAULT; | |
6046 | + *ppos += len; | |
6047 | + return len; | |
6048 | +} | |
6049 | + | |
6050 | +/** | |
6051 | + * ccs_open - open() for /proc/ccs/ interface. | |
6052 | + * | |
6053 | + * @inode: Pointer to "struct inode". | |
6054 | + * @file: Pointer to "struct file". | |
6055 | + * | |
6056 | + * Returns 0 on success, negative value otherwise. | |
6057 | + */ | |
6058 | +static int ccs_open(struct inode *inode, struct file *file) | |
6059 | +{ | |
6060 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0) | |
6061 | + const u8 type = (unsigned long) PDE_DATA(inode); | |
6062 | +#else | |
6063 | + const u8 type = (unsigned long) PDE(inode)->data; | |
6064 | +#endif | |
6065 | + struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS); | |
6066 | + if (!head) | |
6067 | + return -ENOMEM; | |
6068 | + mutex_init(&head->io_sem); | |
6069 | + head->type = type; | |
6070 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
6071 | + if (type == CCS_EXECUTE_HANDLER) { | |
6072 | + /* Allow execute_handler to read process's status. */ | |
6073 | + if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) { | |
6074 | + kfree(head); | |
6075 | + return -EPERM; | |
6076 | + } | |
6077 | + } | |
6078 | +#endif | |
6079 | + if ((file->f_mode & FMODE_READ) && type != CCS_AUDIT && | |
6080 | + type != CCS_QUERY) { | |
6081 | + /* Don't allocate read_buf for poll() access. */ | |
6082 | + head->readbuf_size = 4096; | |
6083 | + head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS); | |
6084 | + if (!head->read_buf) { | |
6085 | + kfree(head); | |
6086 | + return -ENOMEM; | |
6087 | + } | |
6088 | + } | |
6089 | + if (file->f_mode & FMODE_WRITE) { | |
6090 | + head->writebuf_size = 4096; | |
6091 | + head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS); | |
6092 | + if (!head->write_buf) { | |
6093 | + kfree(head->read_buf); | |
6094 | + kfree(head); | |
6095 | + return -ENOMEM; | |
6096 | + } | |
6097 | + } | |
6098 | + /* | |
6099 | + * If the file is /proc/ccs/query, increment the observer counter. | |
6100 | + * The obserber counter is used by ccs_supervisor() to see if | |
6101 | + * there is some process monitoring /proc/ccs/query. | |
6102 | + */ | |
6103 | + if (type == CCS_QUERY) | |
6104 | + atomic_inc(&ccs_query_observers); | |
6105 | + file->private_data = head; | |
6106 | + ccs_notify_gc(head, true); | |
6107 | + return 0; | |
6108 | +} | |
6109 | + | |
6110 | +/** | |
6111 | + * ccs_release - close() for /proc/ccs/ interface. | |
6112 | + * | |
6113 | + * @inode: Pointer to "struct inode". | |
6114 | + * @file: Pointer to "struct file". | |
6115 | + * | |
6116 | + * Returns 0. | |
6117 | + */ | |
6118 | +static int ccs_release(struct inode *inode, struct file *file) | |
6119 | +{ | |
6120 | + struct ccs_io_buffer *head = file->private_data; | |
6121 | + /* | |
6122 | + * If the file is /proc/ccs/query, decrement the observer counter. | |
6123 | + */ | |
6124 | + if (head->type == CCS_QUERY && | |
6125 | + atomic_dec_and_test(&ccs_query_observers)) | |
6126 | + wake_up_all(&ccs_answer_wait); | |
6127 | + ccs_notify_gc(head, false); | |
6128 | + return 0; | |
6129 | +} | |
6130 | + | |
6131 | +/** | |
6132 | + * ccs_poll - poll() for /proc/ccs/ interface. | |
6133 | + * | |
6134 | + * @file: Pointer to "struct file". | |
6135 | + * @wait: Pointer to "poll_table". Maybe NULL. | |
6136 | + * | |
6137 | + * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write, | |
6138 | + * POLLOUT | POLLWRNORM otherwise. | |
6139 | + */ | |
6140 | +static unsigned int ccs_poll(struct file *file, poll_table *wait) | |
6141 | +{ | |
6142 | + struct ccs_io_buffer *head = file->private_data; | |
6143 | + if (head->type == CCS_AUDIT) { | |
6144 | + if (!ccs_memory_used[CCS_MEMORY_AUDIT]) { | |
6145 | + poll_wait(file, &ccs_log_wait, wait); | |
6146 | + if (!ccs_memory_used[CCS_MEMORY_AUDIT]) | |
6147 | + return POLLOUT | POLLWRNORM; | |
6148 | + } | |
6149 | + } else if (head->type == CCS_QUERY) { | |
6150 | + if (list_empty(&ccs_query_list)) { | |
6151 | + poll_wait(file, &ccs_query_wait, wait); | |
6152 | + if (list_empty(&ccs_query_list)) | |
6153 | + return POLLOUT | POLLWRNORM; | |
6154 | + } | |
6155 | + } | |
6156 | + return POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM; | |
6157 | +} | |
6158 | + | |
6159 | +/** | |
6160 | + * ccs_read - read() for /proc/ccs/ interface. | |
6161 | + * | |
6162 | + * @file: Pointer to "struct file". | |
6163 | + * @buf: Pointer to buffer. | |
6164 | + * @count: Size of @buf. | |
6165 | + * @ppos: Unused. | |
6166 | + * | |
6167 | + * Returns bytes read on success, negative value otherwise. | |
6168 | + */ | |
6169 | +static ssize_t ccs_read(struct file *file, char __user *buf, size_t count, | |
6170 | + loff_t *ppos) | |
6171 | +{ | |
6172 | + struct ccs_io_buffer *head = file->private_data; | |
6173 | + int len; | |
6174 | + int idx; | |
6175 | + if (mutex_lock_interruptible(&head->io_sem)) | |
6176 | + return -EINTR; | |
6177 | + head->read_user_buf = buf; | |
6178 | + head->read_user_buf_avail = count; | |
6179 | + idx = ccs_read_lock(); | |
6180 | + if (ccs_flush(head)) | |
6181 | + /* Call the policy handler. */ | |
6182 | + do { | |
6183 | + ccs_set_namespace_cursor(head); | |
6184 | + switch (head->type) { | |
6185 | + case CCS_DOMAIN_POLICY: | |
6186 | + ccs_read_domain(head); | |
6187 | + break; | |
6188 | + case CCS_EXCEPTION_POLICY: | |
6189 | + ccs_read_exception(head); | |
6190 | + break; | |
6191 | + case CCS_AUDIT: | |
6192 | + ccs_read_log(head); | |
6193 | + break; | |
6194 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
6195 | + case CCS_EXECUTE_HANDLER: | |
6196 | +#endif | |
6197 | + case CCS_PROCESS_STATUS: | |
6198 | + ccs_read_pid(head); | |
6199 | + break; | |
6200 | + case CCS_VERSION: | |
6201 | + ccs_read_version(head); | |
6202 | + break; | |
6203 | + case CCS_STAT: | |
6204 | + ccs_read_stat(head); | |
6205 | + break; | |
6206 | + case CCS_PROFILE: | |
6207 | + ccs_read_profile(head); | |
6208 | + break; | |
6209 | + case CCS_QUERY: | |
6210 | + ccs_read_query(head); | |
6211 | + break; | |
6212 | + case CCS_MANAGER: | |
6213 | + ccs_read_manager(head); | |
6214 | + break; | |
6215 | + } | |
6216 | + } while (ccs_flush(head) && ccs_has_more_namespace(head)); | |
6217 | + ccs_read_unlock(idx); | |
6218 | + len = head->read_user_buf - buf; | |
6219 | + mutex_unlock(&head->io_sem); | |
6220 | + return len; | |
6221 | +} | |
6222 | + | |
6223 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
6224 | + | |
6225 | +/** | |
6226 | + * ccs_write_self - write() for /proc/ccs/self_domain interface. | |
6227 | + * | |
6228 | + * @file: Pointer to "struct file". | |
6229 | + * @buf: Domainname to transit to. | |
6230 | + * @count: Size of @buf. | |
6231 | + * @ppos: Unused. | |
6232 | + * | |
6233 | + * Returns @count on success, negative value otherwise. | |
6234 | + * | |
6235 | + * If domain transition was permitted but the domain transition failed, this | |
6236 | + * function returns error rather than terminating current thread with SIGKILL. | |
6237 | + */ | |
6238 | +static ssize_t ccs_write_self(struct file *file, const char __user *buf, | |
6239 | + size_t count, loff_t *ppos) | |
6240 | +{ | |
6241 | + char *data; | |
6242 | + int error; | |
6243 | + if (!count || count >= CCS_EXEC_TMPSIZE - 10) | |
6244 | + return -ENOMEM; | |
6245 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0) | |
6246 | + data = kzalloc(count + 1, CCS_GFP_FLAGS); | |
6247 | + if (!data) | |
6248 | + return -ENOMEM; | |
6249 | + if (copy_from_user(data, buf, count)) { | |
6250 | + error = -EFAULT; | |
6251 | + goto out; | |
6252 | + } | |
6253 | +#else | |
6254 | + data = memdup_user_nul(buf, count); | |
6255 | + if (IS_ERR(data)) | |
6256 | + return PTR_ERR(data); | |
6257 | +#endif | |
6258 | + ccs_normalize_line(data); | |
6259 | + if (ccs_correct_domain(data)) { | |
6260 | + const int idx = ccs_read_lock(); | |
6261 | + struct ccs_path_info name; | |
6262 | + struct ccs_request_info r; | |
6263 | + name.name = data; | |
6264 | + ccs_fill_path_info(&name); | |
6265 | + /* Check "task manual_domain_transition" permission. */ | |
6266 | + ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE); | |
6267 | + r.param_type = CCS_TYPE_MANUAL_TASK_ACL; | |
6268 | + r.param.task.domainname = &name; | |
6269 | + ccs_check_acl(&r); | |
6270 | + if (!r.granted) | |
6271 | + error = -EPERM; | |
6272 | + else | |
6273 | + error = ccs_assign_domain(data, true) ? 0 : -ENOENT; | |
6274 | + ccs_read_unlock(idx); | |
6275 | + } else | |
6276 | + error = -EINVAL; | |
6277 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0) | |
6278 | +out: | |
6279 | +#endif | |
6280 | + kfree(data); | |
6281 | + return error ? error : count; | |
6282 | +} | |
6283 | + | |
6284 | +#endif | |
6285 | + | |
6286 | +/** | |
6287 | + * ccs_write - write() for /proc/ccs/ interface. | |
6288 | + * | |
6289 | + * @file: Pointer to "struct file". | |
6290 | + * @buf: Pointer to buffer. | |
6291 | + * @count: Size of @buf. | |
6292 | + * @ppos: Unused. | |
6293 | + * | |
6294 | + * Returns @count on success, negative value otherwise. | |
6295 | + */ | |
6296 | +static ssize_t ccs_write(struct file *file, const char __user *buf, | |
6297 | + size_t count, loff_t *ppos) | |
6298 | +{ | |
6299 | + struct ccs_io_buffer *head = file->private_data; | |
6300 | + int error = count; | |
6301 | + char *cp0 = head->write_buf; | |
6302 | + int idx; | |
6303 | + if (mutex_lock_interruptible(&head->io_sem)) | |
6304 | + return -EINTR; | |
6305 | + head->read_user_buf_avail = 0; | |
6306 | + idx = ccs_read_lock(); | |
6307 | + /* Read a line and dispatch it to the policy handler. */ | |
6308 | + while (count) { | |
6309 | + char c; | |
6310 | + if (head->w.avail >= head->writebuf_size - 1) { | |
6311 | + const int len = head->writebuf_size * 2; | |
6312 | + char *cp = kzalloc(len, CCS_GFP_FLAGS); | |
6313 | + if (!cp) { | |
6314 | + error = -ENOMEM; | |
6315 | + break; | |
6316 | + } | |
6317 | + memmove(cp, cp0, head->w.avail); | |
6318 | + kfree(cp0); | |
6319 | + head->write_buf = cp; | |
6320 | + cp0 = cp; | |
6321 | + head->writebuf_size = len; | |
6322 | + } | |
6323 | + if (get_user(c, buf)) { | |
6324 | + error = -EFAULT; | |
6325 | + break; | |
6326 | + } | |
6327 | + buf++; | |
6328 | + count--; | |
6329 | + cp0[head->w.avail++] = c; | |
6330 | + if (c != '\n') | |
6331 | + continue; | |
6332 | + cp0[head->w.avail - 1] = '\0'; | |
6333 | + head->w.avail = 0; | |
6334 | + ccs_normalize_line(cp0); | |
6335 | + if (!strcmp(cp0, "reset")) { | |
6336 | + head->w.ns = &ccs_kernel_namespace; | |
6337 | + head->w.domain = NULL; | |
6338 | + memset(&head->r, 0, sizeof(head->r)); | |
6339 | + continue; | |
6340 | + } | |
6341 | + /* Don't allow updating policies by non manager programs. */ | |
6342 | + switch (head->type) { | |
6343 | + case CCS_PROCESS_STATUS: | |
6344 | + /* This does not write anything. */ | |
6345 | + break; | |
6346 | + case CCS_DOMAIN_POLICY: | |
6347 | + if (ccs_select_domain(head, cp0)) | |
6348 | + continue; | |
6349 | + /* fall through */ | |
6350 | + case CCS_EXCEPTION_POLICY: | |
6351 | + if (!strcmp(cp0, "select transition_only")) { | |
6352 | + head->r.print_transition_related_only = true; | |
6353 | + continue; | |
6354 | + } | |
6355 | + /* fall through */ | |
6356 | + default: | |
6357 | + if (!ccs_manager()) { | |
6358 | + error = -EPERM; | |
6359 | + goto out; | |
6360 | + } | |
6361 | + } | |
6362 | + switch (ccs_parse_policy(head, cp0)) { | |
6363 | + case -EPERM: | |
6364 | + error = -EPERM; | |
6365 | + goto out; | |
6366 | + case 0: | |
6367 | + /* Update statistics. */ | |
6368 | + switch (head->type) { | |
6369 | + case CCS_DOMAIN_POLICY: | |
6370 | + case CCS_EXCEPTION_POLICY: | |
6371 | + case CCS_STAT: | |
6372 | + case CCS_PROFILE: | |
6373 | + case CCS_MANAGER: | |
6374 | + ccs_update_stat(CCS_STAT_POLICY_UPDATES); | |
6375 | + break; | |
6376 | + default: | |
6377 | + break; | |
6378 | + } | |
6379 | + break; | |
6380 | + } | |
6381 | + } | |
6382 | +out: | |
6383 | + ccs_read_unlock(idx); | |
6384 | + mutex_unlock(&head->io_sem); | |
6385 | + return error; | |
6386 | +} | |
6387 | + | |
6388 | +/** | |
6389 | + * ccs_create_entry - Create interface files under /proc/ccs/ directory. | |
6390 | + * | |
6391 | + * @name: The name of the interface file. | |
6392 | + * @mode: The permission of the interface file. | |
6393 | + * @parent: The parent directory. | |
6394 | + * @key: Type of interface. | |
6395 | + * | |
6396 | + * Returns nothing. | |
6397 | + */ | |
6398 | +static void __init ccs_create_entry(const char *name, const umode_t mode, | |
6399 | + struct proc_dir_entry *parent, | |
6400 | + const u8 key) | |
6401 | +{ | |
6402 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26) | |
6403 | + proc_create_data(name, mode, parent, &ccs_operations, | |
6404 | + ((u8 *) NULL) + key); | |
6405 | +#else | |
6406 | + struct proc_dir_entry *entry = create_proc_entry(name, mode, parent); | |
6407 | + if (entry) { | |
6408 | + entry->proc_fops = &ccs_operations; | |
6409 | + entry->data = ((u8 *) NULL) + key; | |
6410 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
6411 | + if (entry->proc_iops) | |
6412 | + ccs_file_inode_operations = *entry->proc_iops; | |
6413 | + if (!ccs_file_inode_operations.setattr) | |
6414 | + ccs_file_inode_operations.setattr = proc_notify_change; | |
6415 | + entry->proc_iops = &ccs_file_inode_operations; | |
6416 | +#endif | |
6417 | + } | |
6418 | +#endif | |
6419 | +} | |
6420 | + | |
6421 | +/** | |
6422 | + * ccs_proc_init - Initialize /proc/ccs/ interface. | |
6423 | + * | |
6424 | + * Returns nothing. | |
6425 | + */ | |
6426 | +static void __init ccs_proc_init(void) | |
6427 | +{ | |
6428 | + struct proc_dir_entry *ccs_dir = proc_mkdir("ccs", NULL); | |
6429 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
6430 | + if (ccs_dir->proc_iops) | |
6431 | + ccs_dir_inode_operations = *ccs_dir->proc_iops; | |
6432 | + if (!ccs_dir_inode_operations.setattr) | |
6433 | + ccs_dir_inode_operations.setattr = proc_notify_change; | |
6434 | + ccs_dir->proc_iops = &ccs_dir_inode_operations; | |
6435 | +#endif | |
6436 | + ccs_create_entry("query", 0600, ccs_dir, CCS_QUERY); | |
6437 | + ccs_create_entry("domain_policy", 0600, ccs_dir, CCS_DOMAIN_POLICY); | |
6438 | + ccs_create_entry("exception_policy", 0600, ccs_dir, | |
6439 | + CCS_EXCEPTION_POLICY); | |
6440 | + ccs_create_entry("audit", 0400, ccs_dir, CCS_AUDIT); | |
6441 | + ccs_create_entry(".process_status", 0600, ccs_dir, | |
6442 | + CCS_PROCESS_STATUS); | |
6443 | + ccs_create_entry("stat", 0644, ccs_dir, CCS_STAT); | |
6444 | + ccs_create_entry("profile", 0600, ccs_dir, CCS_PROFILE); | |
6445 | + ccs_create_entry("manager", 0600, ccs_dir, CCS_MANAGER); | |
6446 | + ccs_create_entry("version", 0400, ccs_dir, CCS_VERSION); | |
6447 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
6448 | + ccs_create_entry(".execute_handler", 0666, ccs_dir, | |
6449 | + CCS_EXECUTE_HANDLER); | |
6450 | +#endif | |
6451 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26) | |
6452 | + proc_create("self_domain", 0666, ccs_dir, &ccs_self_operations); | |
6453 | +#else | |
6454 | + { | |
6455 | + struct proc_dir_entry *e = create_proc_entry("self_domain", | |
6456 | + 0666, ccs_dir); | |
6457 | + if (e) | |
6458 | + e->proc_fops = &ccs_self_operations; | |
6459 | + } | |
6460 | +#endif | |
6461 | +} | |
6462 | + | |
6463 | +/** | |
6464 | + * ccs_init_module - Initialize this module. | |
6465 | + * | |
6466 | + * Returns 0 on success, negative value otherwise. | |
6467 | + */ | |
6468 | +static int __init ccs_init_module(void) | |
6469 | +{ | |
6470 | + if (ccsecurity_ops.disabled) | |
6471 | + return -EINVAL; | |
6472 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
6473 | + MOD_INC_USE_COUNT; | |
6474 | +#endif | |
6475 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
6476 | + if (init_srcu_struct(&ccs_ss)) | |
6477 | + panic("Out of memory."); | |
6478 | +#endif | |
6479 | + ccs_kernel_namespace.name = "<kernel>"; | |
6480 | + ccs_init_policy_namespace(&ccs_kernel_namespace); | |
6481 | + ccs_kernel_domain.ns = &ccs_kernel_namespace; | |
6482 | + INIT_LIST_HEAD(&ccs_kernel_domain.acl_info_list); | |
6483 | + ccs_mm_init(); | |
6484 | + ccs_policy_io_init(); | |
6485 | + ccs_permission_init(); | |
6486 | + ccs_proc_init(); | |
6487 | + ccs_load_builtin_policy(); | |
6488 | + return 0; | |
6489 | +} | |
6490 | + | |
6491 | +/** | |
6492 | + * ccs_main_init - Initialize this module. | |
6493 | + * | |
6494 | + * Returns nothing. | |
6495 | + */ | |
6496 | +void __init ccs_main_init(void) | |
6497 | +{ | |
6498 | + ccs_init_module(); | |
6499 | +} |
@@ -0,0 +1,2162 @@ | ||
1 | +/* | |
2 | + * security/ccsecurity/internal.h | |
3 | + * | |
4 | + * Copyright (C) 2005-2012 NTT DATA CORPORATION | |
5 | + * | |
6 | + * Version: 1.8.5 2015/11/11 | |
7 | + */ | |
8 | + | |
9 | +#ifndef _SECURITY_CCSECURITY_INTERNAL_H | |
10 | +#define _SECURITY_CCSECURITY_INTERNAL_H | |
11 | + | |
12 | +#include <linux/version.h> | |
13 | +#include <linux/types.h> | |
14 | +#include <linux/kernel.h> | |
15 | +#include <linux/string.h> | |
16 | +#include <linux/mm.h> | |
17 | +#include <linux/utime.h> | |
18 | +#include <linux/file.h> | |
19 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38) | |
20 | +#include <linux/smp_lock.h> | |
21 | +#endif | |
22 | +#include <linux/module.h> | |
23 | +#include <linux/init.h> | |
24 | +#include <linux/slab.h> | |
25 | +#include <linux/highmem.h> | |
26 | +#include <linux/poll.h> | |
27 | +#include <linux/binfmts.h> | |
28 | +#include <linux/delay.h> | |
29 | +#include <linux/sched.h> | |
30 | +#include <linux/dcache.h> | |
31 | +#include <linux/mount.h> | |
32 | +#include <linux/net.h> | |
33 | +#include <linux/inet.h> | |
34 | +#include <linux/in.h> | |
35 | +#include <linux/in6.h> | |
36 | +#include <linux/un.h> | |
37 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
38 | +#include <linux/fs.h> | |
39 | +#endif | |
40 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) | |
41 | +#include <linux/namei.h> | |
42 | +#endif | |
43 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) | |
44 | +#include <linux/fs_struct.h> | |
45 | +#endif | |
46 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) | |
47 | +#include <linux/namespace.h> | |
48 | +#endif | |
49 | +#include <linux/proc_fs.h> | |
50 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(RHEL_MAJOR) | |
51 | +#include <linux/hash.h> | |
52 | +#endif | |
53 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL)) | |
54 | +#include <linux/sysctl.h> | |
55 | +#endif | |
56 | +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6) | |
57 | +#include <linux/kthread.h> | |
58 | +#endif | |
59 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0) | |
60 | +#include <linux/magic.h> | |
61 | +#endif | |
62 | +#include <stdarg.h> | |
63 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) | |
64 | +#include <linux/uaccess.h> | |
65 | +#else | |
66 | +#include <asm/uaccess.h> | |
67 | +#endif | |
68 | +#include <net/sock.h> | |
69 | +#include <net/af_unix.h> | |
70 | +#include <net/ip.h> | |
71 | +#include <net/ipv6.h> | |
72 | +#include <net/udp.h> | |
73 | + | |
74 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
75 | +#define sk_family family | |
76 | +#define sk_protocol protocol | |
77 | +#define sk_type type | |
78 | +#endif | |
79 | + | |
80 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) | |
81 | + | |
82 | +/* Structure for holding "struct vfsmount *" and "struct dentry *". */ | |
83 | +struct path { | |
84 | + struct vfsmount *mnt; | |
85 | + struct dentry *dentry; | |
86 | +}; | |
87 | + | |
88 | +#endif | |
89 | + | |
90 | +#ifndef __printf | |
91 | +#define __printf(a,b) __attribute__((format(printf,a,b))) | |
92 | +#endif | |
93 | +#ifndef __packed | |
94 | +#define __packed __attribute__((__packed__)) | |
95 | +#endif | |
96 | +#ifndef bool | |
97 | +#define bool _Bool | |
98 | +#endif | |
99 | +#ifndef false | |
100 | +#define false 0 | |
101 | +#endif | |
102 | +#ifndef true | |
103 | +#define true 1 | |
104 | +#endif | |
105 | + | |
106 | +#ifndef __user | |
107 | +#define __user | |
108 | +#endif | |
109 | + | |
110 | +#ifndef current_uid | |
111 | +#define current_uid() (current->uid) | |
112 | +#endif | |
113 | +#ifndef current_gid | |
114 | +#define current_gid() (current->gid) | |
115 | +#endif | |
116 | +#ifndef current_euid | |
117 | +#define current_euid() (current->euid) | |
118 | +#endif | |
119 | +#ifndef current_egid | |
120 | +#define current_egid() (current->egid) | |
121 | +#endif | |
122 | +#ifndef current_suid | |
123 | +#define current_suid() (current->suid) | |
124 | +#endif | |
125 | +#ifndef current_sgid | |
126 | +#define current_sgid() (current->sgid) | |
127 | +#endif | |
128 | +#ifndef current_fsuid | |
129 | +#define current_fsuid() (current->fsuid) | |
130 | +#endif | |
131 | +#ifndef current_fsgid | |
132 | +#define current_fsgid() (current->fsgid) | |
133 | +#endif | |
134 | + | |
135 | +#ifndef DEFINE_SPINLOCK | |
136 | +#define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED | |
137 | +#endif | |
138 | + | |
139 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) | |
140 | +#define mutex semaphore | |
141 | +#define mutex_init(mutex) init_MUTEX(mutex) | |
142 | +#define mutex_unlock(mutex) up(mutex) | |
143 | +#define mutex_lock(mutex) down(mutex) | |
144 | +#define mutex_lock_interruptible(mutex) down_interruptible(mutex) | |
145 | +#define mutex_trylock(mutex) (!down_trylock(mutex)) | |
146 | +#define DEFINE_MUTEX(mutexname) DECLARE_MUTEX(mutexname) | |
147 | +#endif | |
148 | + | |
149 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15) | |
150 | +#define MS_UNBINDABLE (1<<17) /* change to unbindable */ | |
151 | +#define MS_PRIVATE (1<<18) /* change to private */ | |
152 | +#define MS_SLAVE (1<<19) /* change to slave */ | |
153 | +#define MS_SHARED (1<<20) /* change to shared */ | |
154 | +#endif | |
155 | + | |
156 | +#ifndef container_of | |
157 | +#define container_of(ptr, type, member) ({ \ | |
158 | + const typeof(((type *)0)->member) *__mptr = (ptr); \ | |
159 | + (type *)((char *)__mptr - offsetof(type, member)); }) | |
160 | +#endif | |
161 | + | |
162 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
163 | +#define smp_read_barrier_depends smp_rmb | |
164 | +#endif | |
165 | + | |
166 | +#ifndef ACCESS_ONCE | |
167 | +#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) | |
168 | +#endif | |
169 | + | |
170 | +#ifndef rcu_dereference | |
171 | +#define rcu_dereference(p) ({ \ | |
172 | + typeof(p) _________p1 = ACCESS_ONCE(p); \ | |
173 | + smp_read_barrier_depends(); /* see RCU */ \ | |
174 | + (_________p1); \ | |
175 | + }) | |
176 | +#endif | |
177 | + | |
178 | +#ifndef rcu_assign_pointer | |
179 | +#define rcu_assign_pointer(p, v) \ | |
180 | + ({ \ | |
181 | + if (!__builtin_constant_p(v) || \ | |
182 | + ((v) != NULL)) \ | |
183 | + smp_wmb(); /* see RCU */ \ | |
184 | + (p) = (v); \ | |
185 | + }) | |
186 | +#endif | |
187 | + | |
188 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) | |
189 | +#define f_vfsmnt f_path.mnt | |
190 | +#endif | |
191 | + | |
192 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14) | |
193 | + | |
194 | +/** | |
195 | + * kzalloc() - Allocate memory. The memory is set to zero. | |
196 | + * | |
197 | + * @size: Size to allocate. | |
198 | + * @flags: GFP flags. | |
199 | + * | |
200 | + * Returns pointer to allocated memory on success, NULL otherwise. | |
201 | + * | |
202 | + * This is for compatibility with older kernels. | |
203 | + * | |
204 | + * Since several distributions backported kzalloc(), I define it as a macro | |
205 | + * rather than an inlined function in order to avoid multiple definition error. | |
206 | + */ | |
207 | +#define kzalloc(size, flags) ({ \ | |
208 | + void *ret = kmalloc((size), (flags)); \ | |
209 | + if (ret) \ | |
210 | + memset(ret, 0, (size)); \ | |
211 | + ret; }) | |
212 | + | |
213 | +#endif | |
214 | + | |
215 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) | |
216 | + | |
217 | +/** | |
218 | + * path_put - Drop reference on "struct path". | |
219 | + * | |
220 | + * @path: Pointer to "struct path". | |
221 | + * | |
222 | + * Returns nothing. | |
223 | + * | |
224 | + * This is for compatibility with older kernels. | |
225 | + */ | |
226 | +static inline void path_put(struct path *path) | |
227 | +{ | |
228 | + dput(path->dentry); | |
229 | + mntput(path->mnt); | |
230 | +} | |
231 | + | |
232 | +#endif | |
233 | + | |
234 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
235 | + | |
236 | +/** | |
237 | + * __list_add_rcu - Insert a new entry between two known consecutive entries. | |
238 | + * | |
239 | + * @new: Pointer to "struct list_head". | |
240 | + * @prev: Pointer to "struct list_head". | |
241 | + * @next: Pointer to "struct list_head". | |
242 | + * | |
243 | + * Returns nothing. | |
244 | + * | |
245 | + * This is for compatibility with older kernels. | |
246 | + */ | |
247 | +static inline void __list_add_rcu(struct list_head *new, | |
248 | + struct list_head *prev, | |
249 | + struct list_head *next) | |
250 | +{ | |
251 | + new->next = next; | |
252 | + new->prev = prev; | |
253 | + rcu_assign_pointer(prev->next, new); | |
254 | + next->prev = new; | |
255 | +} | |
256 | + | |
257 | +/** | |
258 | + * list_add_tail_rcu - Add a new entry to rcu-protected list. | |
259 | + * | |
260 | + * @new: Pointer to "struct list_head". | |
261 | + * @head: Pointer to "struct list_head". | |
262 | + * | |
263 | + * Returns nothing. | |
264 | + * | |
265 | + * This is for compatibility with older kernels. | |
266 | + */ | |
267 | +static inline void list_add_tail_rcu(struct list_head *new, | |
268 | + struct list_head *head) | |
269 | +{ | |
270 | + __list_add_rcu(new, head->prev, head); | |
271 | +} | |
272 | + | |
273 | +/** | |
274 | + * list_add_rcu - Add a new entry to rcu-protected list. | |
275 | + * | |
276 | + * @new: Pointer to "struct list_head". | |
277 | + * @head: Pointer to "struct list_head". | |
278 | + * | |
279 | + * Returns nothing. | |
280 | + * | |
281 | + * This is for compatibility with older kernels. | |
282 | + */ | |
283 | +static inline void list_add_rcu(struct list_head *new, struct list_head *head) | |
284 | +{ | |
285 | + __list_add_rcu(new, head, head->next); | |
286 | +} | |
287 | + | |
288 | +#endif | |
289 | + | |
290 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38) | |
291 | + | |
292 | +/** | |
293 | + * __list_del_entry - Deletes entry from list without re-initialization. | |
294 | + * | |
295 | + * @entry: Pointer to "struct list_head". | |
296 | + * | |
297 | + * Returns nothing. | |
298 | + * | |
299 | + * This is for compatibility with older kernels. | |
300 | + */ | |
301 | +static inline void __list_del_entry(struct list_head *entry) | |
302 | +{ | |
303 | + __list_del(entry->prev, entry->next); | |
304 | +} | |
305 | + | |
306 | +#endif | |
307 | + | |
308 | +#ifndef list_for_each_entry_safe | |
309 | + | |
310 | +/** | |
311 | + * list_for_each_entry_safe - Iterate over list of given type safe against removal of list entry. | |
312 | + * | |
313 | + * @pos: The "type *" to use as a loop cursor. | |
314 | + * @n: Another "type *" to use as temporary storage. | |
315 | + * @head: Pointer to "struct list_head". | |
316 | + * @member: The name of the list_struct within the struct. | |
317 | + * | |
318 | + * This is for compatibility with older kernels. | |
319 | + */ | |
320 | +#define list_for_each_entry_safe(pos, n, head, member) \ | |
321 | + for (pos = list_entry((head)->next, typeof(*pos), member), \ | |
322 | + n = list_entry(pos->member.next, typeof(*pos), member); \ | |
323 | + &pos->member != (head); \ | |
324 | + pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
325 | + | |
326 | +#endif | |
327 | + | |
328 | +#ifndef srcu_dereference | |
329 | + | |
330 | +/** | |
331 | + * srcu_dereference - Fetch SRCU-protected pointer with checking. | |
332 | + * | |
333 | + * @p: The pointer to read, prior to dereferencing. | |
334 | + * @ss: Pointer to "struct srcu_struct". | |
335 | + * | |
336 | + * Returns @p. | |
337 | + * | |
338 | + * This is for compatibility with older kernels. | |
339 | + */ | |
340 | +#define srcu_dereference(p, ss) rcu_dereference(p) | |
341 | + | |
342 | +#endif | |
343 | + | |
344 | +#ifndef list_for_each_entry_srcu | |
345 | + | |
346 | +/** | |
347 | + * list_for_each_entry_srcu - Iterate over rcu list of given type. | |
348 | + * | |
349 | + * @pos: The type * to use as a loop cursor. | |
350 | + * @head: The head for your list. | |
351 | + * @member: The name of the list_struct within the struct. | |
352 | + * @ss: Pointer to "struct srcu_struct". | |
353 | + * | |
354 | + * As of 2.6.36, this macro is not provided because only TOMOYO wants it. | |
355 | + */ | |
356 | +#define list_for_each_entry_srcu(pos, head, member, ss) \ | |
357 | + for (pos = list_entry(srcu_dereference((head)->next, ss), \ | |
358 | + typeof(*pos), member); \ | |
359 | + prefetch(pos->member.next), &pos->member != (head); \ | |
360 | + pos = list_entry(srcu_dereference(pos->member.next, ss), \ | |
361 | + typeof(*pos), member)) | |
362 | + | |
363 | +#endif | |
364 | + | |
365 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 30) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9)) | |
366 | + | |
367 | +#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 4, 21) | |
368 | +#undef ssleep | |
369 | +#endif | |
370 | + | |
371 | +#ifndef ssleep | |
372 | + | |
373 | +/** | |
374 | + * ssleep - Sleep for specified seconds. | |
375 | + * | |
376 | + * @secs: Seconds to sleep. | |
377 | + * | |
378 | + * Returns nothing. | |
379 | + * | |
380 | + * This is for compatibility with older kernels. | |
381 | + * | |
382 | + * Since several distributions backported ssleep(), I define it as a macro | |
383 | + * rather than an inlined function in order to avoid multiple definition error. | |
384 | + */ | |
385 | +#define ssleep(secs) { \ | |
386 | + set_current_state(TASK_UNINTERRUPTIBLE); \ | |
387 | + schedule_timeout((HZ * secs) + 1); \ | |
388 | + } | |
389 | + | |
390 | +#endif | |
391 | + | |
392 | +#endif | |
393 | + | |
394 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) | |
395 | + | |
396 | +/** | |
397 | + * from_kuid - Convert kuid_t to uid_t. | |
398 | + * | |
399 | + * @ns: Unused. | |
400 | + * @uid: kuid_t value. | |
401 | + * | |
402 | + * Returns uid seen from init's user namespace. | |
403 | + */ | |
404 | +#define from_kuid(ns, uid) (uid) | |
405 | + | |
406 | +/** | |
407 | + * from_kgid - Convert kgid_t to gid_t. | |
408 | + * | |
409 | + * @ns: Unused. | |
410 | + * @gid: kgid_t value. | |
411 | + * | |
412 | + * Returns gid seen from init's user namespace. | |
413 | + */ | |
414 | +#define from_kgid(ns, gid) (gid) | |
415 | + | |
416 | +/** | |
417 | + * uid_eq - Check whether the uids are equals or not. | |
418 | + * | |
419 | + * @left: Uid seen from current user namespace. | |
420 | + * @right: Uid seen from current user namespace. | |
421 | + * | |
422 | + * Returns true if uid is root in init's user namespace, false otherwise. | |
423 | + */ | |
424 | +#define uid_eq(left, right) ((left) == (right)) | |
425 | +#define GLOBAL_ROOT_UID 0 | |
426 | + | |
427 | +#endif | |
428 | + | |
429 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) | |
430 | +#define d_backing_inode(upper) (upper)->d_inode | |
431 | +#endif | |
432 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) | |
433 | +#define d_is_dir(dentry) ({ struct inode *inode = d_backing_inode(dentry); \ | |
434 | + inode && S_ISDIR(inode->i_mode); }) | |
435 | +#endif | |
436 | + | |
437 | +/* | |
438 | + * TOMOYO specific part start. | |
439 | + */ | |
440 | + | |
441 | +/* Clear TOMOYO 1.8 config. */ | |
442 | +#undef CONFIG_CCSECURITY | |
443 | +#undef CONFIG_CCSECURITY_LKM | |
444 | +#undef CONFIG_CCSECURITY_DISABLE_BY_DEFAULT | |
445 | +#undef CONFIG_CCSECURITY_MAX_AUDIT_LOG | |
446 | +#undef CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY | |
447 | +#undef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
448 | +#undef CONFIG_CCSECURITY_POLICY_LOADER | |
449 | +#undef CONFIG_CCSECURITY_ACTIVATION_TRIGGER | |
450 | +#undef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
451 | +#undef CONFIG_CCSECURITY_FILE_READDIR | |
452 | +#undef CONFIG_CCSECURITY_FILE_GETATTR | |
453 | +#undef CONFIG_CCSECURITY_NETWORK | |
454 | +#undef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
455 | +#undef CONFIG_CCSECURITY_CAPABILITY | |
456 | +#undef CONFIG_CCSECURITY_IPC | |
457 | +#undef CONFIG_CCSECURITY_MISC | |
458 | +#undef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
459 | +#undef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
460 | +#undef CONFIG_CCSECURITY_PORTRESERVE | |
461 | +/* Define AKARI 1.0 config. */ | |
462 | +#include "config.h" | |
463 | +#ifndef CONFIG_CCSECURITY | |
464 | +#define CONFIG_CCSECURITY | |
465 | +#endif | |
466 | +#ifndef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
467 | +#define CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
468 | +#endif | |
469 | +#ifndef CONFIG_CCSECURITY_MAX_AUDIT_LOG | |
470 | +#define CONFIG_CCSECURITY_MAX_AUDIT_LOG 1024 | |
471 | +#endif | |
472 | +#ifndef CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY | |
473 | +#define CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY 2048 | |
474 | +#endif | |
475 | +#ifndef CONFIG_CCSECURITY_POLICY_LOADER | |
476 | +#define CONFIG_CCSECURITY_POLICY_LOADER "/sbin/ccs-init" | |
477 | +#endif | |
478 | +#ifndef CONFIG_CCSECURITY_ACTIVATION_TRIGGER | |
479 | +#define CONFIG_CCSECURITY_ACTIVATION_TRIGGER "/sbin/init" | |
480 | +#endif | |
481 | +#include "ccsecurity.h" | |
482 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
483 | +#error This module supports only 2.6.0 and later kernels. | |
484 | +#endif | |
485 | +#ifndef CONFIG_SECURITY | |
486 | +#error You must choose CONFIG_SECURITY=y for building this module. | |
487 | +#endif | |
488 | +#ifndef CONFIG_KALLSYMS | |
489 | +#error You must choose CONFIG_KALLSYMS=y for building this module. | |
490 | +#endif | |
491 | +#ifndef CONFIG_PROC_FS | |
492 | +#error You must choose CONFIG_PROC_FS=y for building this module. | |
493 | +#endif | |
494 | +#ifndef CONFIG_MODULES | |
495 | +#error You must choose CONFIG_MODULES=y for building this module. | |
496 | +#endif | |
497 | + | |
498 | +/* Enumeration definition for internal use. */ | |
499 | + | |
500 | +/* Index numbers for Access Controls. */ | |
501 | +enum ccs_acl_entry_type_index { | |
502 | + CCS_TYPE_PATH_ACL, | |
503 | + CCS_TYPE_PATH2_ACL, | |
504 | + CCS_TYPE_PATH_NUMBER_ACL, | |
505 | + CCS_TYPE_MKDEV_ACL, | |
506 | + CCS_TYPE_MOUNT_ACL, | |
507 | +#ifdef CONFIG_CCSECURITY_MISC | |
508 | + CCS_TYPE_ENV_ACL, | |
509 | +#endif | |
510 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
511 | + CCS_TYPE_CAPABILITY_ACL, | |
512 | +#endif | |
513 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
514 | + CCS_TYPE_INET_ACL, | |
515 | + CCS_TYPE_UNIX_ACL, | |
516 | +#endif | |
517 | +#ifdef CONFIG_CCSECURITY_IPC | |
518 | + CCS_TYPE_SIGNAL_ACL, | |
519 | +#endif | |
520 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
521 | + CCS_TYPE_AUTO_EXECUTE_HANDLER, | |
522 | + CCS_TYPE_DENIED_EXECUTE_HANDLER, | |
523 | +#endif | |
524 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
525 | + CCS_TYPE_AUTO_TASK_ACL, | |
526 | + CCS_TYPE_MANUAL_TASK_ACL, | |
527 | +#endif | |
528 | +}; | |
529 | + | |
530 | +/* Index numbers for "struct ccs_condition". */ | |
531 | +enum ccs_conditions_index { | |
532 | + CCS_TASK_UID, /* current_uid() */ | |
533 | + CCS_TASK_EUID, /* current_euid() */ | |
534 | + CCS_TASK_SUID, /* current_suid() */ | |
535 | + CCS_TASK_FSUID, /* current_fsuid() */ | |
536 | + CCS_TASK_GID, /* current_gid() */ | |
537 | + CCS_TASK_EGID, /* current_egid() */ | |
538 | + CCS_TASK_SGID, /* current_sgid() */ | |
539 | + CCS_TASK_FSGID, /* current_fsgid() */ | |
540 | + CCS_TASK_PID, /* sys_getpid() */ | |
541 | + CCS_TASK_PPID, /* sys_getppid() */ | |
542 | + CCS_EXEC_ARGC, /* "struct linux_binprm *"->argc */ | |
543 | + CCS_EXEC_ENVC, /* "struct linux_binprm *"->envc */ | |
544 | + CCS_TYPE_IS_SOCKET, /* S_IFSOCK */ | |
545 | + CCS_TYPE_IS_SYMLINK, /* S_IFLNK */ | |
546 | + CCS_TYPE_IS_FILE, /* S_IFREG */ | |
547 | + CCS_TYPE_IS_BLOCK_DEV, /* S_IFBLK */ | |
548 | + CCS_TYPE_IS_DIRECTORY, /* S_IFDIR */ | |
549 | + CCS_TYPE_IS_CHAR_DEV, /* S_IFCHR */ | |
550 | + CCS_TYPE_IS_FIFO, /* S_IFIFO */ | |
551 | + CCS_MODE_SETUID, /* S_ISUID */ | |
552 | + CCS_MODE_SETGID, /* S_ISGID */ | |
553 | + CCS_MODE_STICKY, /* S_ISVTX */ | |
554 | + CCS_MODE_OWNER_READ, /* S_IRUSR */ | |
555 | + CCS_MODE_OWNER_WRITE, /* S_IWUSR */ | |
556 | + CCS_MODE_OWNER_EXECUTE, /* S_IXUSR */ | |
557 | + CCS_MODE_GROUP_READ, /* S_IRGRP */ | |
558 | + CCS_MODE_GROUP_WRITE, /* S_IWGRP */ | |
559 | + CCS_MODE_GROUP_EXECUTE, /* S_IXGRP */ | |
560 | + CCS_MODE_OTHERS_READ, /* S_IROTH */ | |
561 | + CCS_MODE_OTHERS_WRITE, /* S_IWOTH */ | |
562 | + CCS_MODE_OTHERS_EXECUTE, /* S_IXOTH */ | |
563 | + CCS_TASK_TYPE, /* ((u8) task->ccs_flags) & | |
564 | + CCS_TASK_IS_EXECUTE_HANDLER */ | |
565 | + CCS_TASK_EXECUTE_HANDLER, /* CCS_TASK_IS_EXECUTE_HANDLER */ | |
566 | + CCS_EXEC_REALPATH, | |
567 | + CCS_SYMLINK_TARGET, | |
568 | + CCS_PATH1_UID, | |
569 | + CCS_PATH1_GID, | |
570 | + CCS_PATH1_INO, | |
571 | + CCS_PATH1_MAJOR, | |
572 | + CCS_PATH1_MINOR, | |
573 | + CCS_PATH1_PERM, | |
574 | + CCS_PATH1_TYPE, | |
575 | + CCS_PATH1_DEV_MAJOR, | |
576 | + CCS_PATH1_DEV_MINOR, | |
577 | + CCS_PATH2_UID, | |
578 | + CCS_PATH2_GID, | |
579 | + CCS_PATH2_INO, | |
580 | + CCS_PATH2_MAJOR, | |
581 | + CCS_PATH2_MINOR, | |
582 | + CCS_PATH2_PERM, | |
583 | + CCS_PATH2_TYPE, | |
584 | + CCS_PATH2_DEV_MAJOR, | |
585 | + CCS_PATH2_DEV_MINOR, | |
586 | + CCS_PATH1_PARENT_UID, | |
587 | + CCS_PATH1_PARENT_GID, | |
588 | + CCS_PATH1_PARENT_INO, | |
589 | + CCS_PATH1_PARENT_PERM, | |
590 | + CCS_PATH2_PARENT_UID, | |
591 | + CCS_PATH2_PARENT_GID, | |
592 | + CCS_PATH2_PARENT_INO, | |
593 | + CCS_PATH2_PARENT_PERM, | |
594 | + CCS_MAX_CONDITION_KEYWORD, | |
595 | + CCS_NUMBER_UNION, | |
596 | + CCS_NAME_UNION, | |
597 | + CCS_ARGV_ENTRY, | |
598 | + CCS_ENVP_ENTRY, | |
599 | +}; | |
600 | + | |
601 | +/* Index numbers for domain's attributes. */ | |
602 | +enum ccs_domain_info_flags_index { | |
603 | + /* Quota warnning flag. */ | |
604 | + CCS_DIF_QUOTA_WARNED, | |
605 | + /* | |
606 | + * This domain was unable to create a new domain at | |
607 | + * ccs_find_next_domain() because the name of the domain to be created | |
608 | + * was too long or it could not allocate memory. | |
609 | + * More than one process continued execve() without domain transition. | |
610 | + */ | |
611 | + CCS_DIF_TRANSITION_FAILED, | |
612 | + CCS_MAX_DOMAIN_INFO_FLAGS | |
613 | +}; | |
614 | + | |
615 | +/* Index numbers for audit type. */ | |
616 | +enum ccs_grant_log { | |
617 | + /* Follow profile's configuration. */ | |
618 | + CCS_GRANTLOG_AUTO, | |
619 | + /* Do not generate grant log. */ | |
620 | + CCS_GRANTLOG_NO, | |
621 | + /* Generate grant_log. */ | |
622 | + CCS_GRANTLOG_YES, | |
623 | +}; | |
624 | + | |
625 | +/* Index numbers for group entries. */ | |
626 | +enum ccs_group_id { | |
627 | + CCS_PATH_GROUP, | |
628 | + CCS_NUMBER_GROUP, | |
629 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
630 | + CCS_ADDRESS_GROUP, | |
631 | +#endif | |
632 | + CCS_MAX_GROUP | |
633 | +}; | |
634 | + | |
635 | +/* Index numbers for category of functionality. */ | |
636 | +enum ccs_mac_category_index { | |
637 | + CCS_MAC_CATEGORY_FILE, | |
638 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
639 | + CCS_MAC_CATEGORY_NETWORK, | |
640 | +#endif | |
641 | +#ifdef CONFIG_CCSECURITY_MISC | |
642 | + CCS_MAC_CATEGORY_MISC, | |
643 | +#endif | |
644 | +#ifdef CONFIG_CCSECURITY_IPC | |
645 | + CCS_MAC_CATEGORY_IPC, | |
646 | +#endif | |
647 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
648 | + CCS_MAC_CATEGORY_CAPABILITY, | |
649 | +#endif | |
650 | + CCS_MAX_MAC_CATEGORY_INDEX | |
651 | +}; | |
652 | + | |
653 | +/* Index numbers for functionality. */ | |
654 | +enum ccs_mac_index { | |
655 | + CCS_MAC_FILE_EXECUTE, | |
656 | + CCS_MAC_FILE_OPEN, | |
657 | + CCS_MAC_FILE_CREATE, | |
658 | + CCS_MAC_FILE_UNLINK, | |
659 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
660 | + CCS_MAC_FILE_GETATTR, | |
661 | +#endif | |
662 | + CCS_MAC_FILE_MKDIR, | |
663 | + CCS_MAC_FILE_RMDIR, | |
664 | + CCS_MAC_FILE_MKFIFO, | |
665 | + CCS_MAC_FILE_MKSOCK, | |
666 | + CCS_MAC_FILE_TRUNCATE, | |
667 | + CCS_MAC_FILE_SYMLINK, | |
668 | + CCS_MAC_FILE_MKBLOCK, | |
669 | + CCS_MAC_FILE_MKCHAR, | |
670 | + CCS_MAC_FILE_LINK, | |
671 | + CCS_MAC_FILE_RENAME, | |
672 | + CCS_MAC_FILE_CHMOD, | |
673 | + CCS_MAC_FILE_CHOWN, | |
674 | + CCS_MAC_FILE_CHGRP, | |
675 | + CCS_MAC_FILE_IOCTL, | |
676 | + CCS_MAC_FILE_CHROOT, | |
677 | + CCS_MAC_FILE_MOUNT, | |
678 | + CCS_MAC_FILE_UMOUNT, | |
679 | + CCS_MAC_FILE_PIVOT_ROOT, | |
680 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
681 | + CCS_MAC_NETWORK_INET_STREAM_BIND, | |
682 | + CCS_MAC_NETWORK_INET_STREAM_LISTEN, | |
683 | + CCS_MAC_NETWORK_INET_STREAM_CONNECT, | |
684 | + CCS_MAC_NETWORK_INET_STREAM_ACCEPT, | |
685 | + CCS_MAC_NETWORK_INET_DGRAM_BIND, | |
686 | + CCS_MAC_NETWORK_INET_DGRAM_SEND, | |
687 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
688 | + CCS_MAC_NETWORK_INET_DGRAM_RECV, | |
689 | +#endif | |
690 | + CCS_MAC_NETWORK_INET_RAW_BIND, | |
691 | + CCS_MAC_NETWORK_INET_RAW_SEND, | |
692 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
693 | + CCS_MAC_NETWORK_INET_RAW_RECV, | |
694 | +#endif | |
695 | + CCS_MAC_NETWORK_UNIX_STREAM_BIND, | |
696 | + CCS_MAC_NETWORK_UNIX_STREAM_LISTEN, | |
697 | + CCS_MAC_NETWORK_UNIX_STREAM_CONNECT, | |
698 | + CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT, | |
699 | + CCS_MAC_NETWORK_UNIX_DGRAM_BIND, | |
700 | + CCS_MAC_NETWORK_UNIX_DGRAM_SEND, | |
701 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
702 | + CCS_MAC_NETWORK_UNIX_DGRAM_RECV, | |
703 | +#endif | |
704 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND, | |
705 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN, | |
706 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT, | |
707 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT, | |
708 | +#endif | |
709 | +#ifdef CONFIG_CCSECURITY_MISC | |
710 | + CCS_MAC_ENVIRON, | |
711 | +#endif | |
712 | +#ifdef CONFIG_CCSECURITY_IPC | |
713 | + CCS_MAC_SIGNAL, | |
714 | +#endif | |
715 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
716 | + CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET, | |
717 | + CCS_MAC_CAPABILITY_USE_PACKET_SOCKET, | |
718 | + CCS_MAC_CAPABILITY_SYS_REBOOT, | |
719 | + CCS_MAC_CAPABILITY_SYS_VHANGUP, | |
720 | + CCS_MAC_CAPABILITY_SYS_SETTIME, | |
721 | + CCS_MAC_CAPABILITY_SYS_NICE, | |
722 | + CCS_MAC_CAPABILITY_SYS_SETHOSTNAME, | |
723 | + CCS_MAC_CAPABILITY_USE_KERNEL_MODULE, | |
724 | + CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD, | |
725 | + CCS_MAC_CAPABILITY_SYS_PTRACE, | |
726 | +#endif | |
727 | + CCS_MAX_MAC_INDEX | |
728 | +}; | |
729 | + | |
730 | +/* Index numbers for /proc/ccs/stat interface. */ | |
731 | +enum ccs_memory_stat_type { | |
732 | + CCS_MEMORY_POLICY, | |
733 | + CCS_MEMORY_AUDIT, | |
734 | + CCS_MEMORY_QUERY, | |
735 | + CCS_MAX_MEMORY_STAT | |
736 | +}; | |
737 | + | |
738 | +/* Index numbers for access controls with one pathname and three numbers. */ | |
739 | +enum ccs_mkdev_acl_index { | |
740 | + CCS_TYPE_MKBLOCK, | |
741 | + CCS_TYPE_MKCHAR, | |
742 | + CCS_MAX_MKDEV_OPERATION | |
743 | +}; | |
744 | + | |
745 | +/* Index numbers for operation mode. */ | |
746 | +enum ccs_mode_value { | |
747 | + CCS_CONFIG_DISABLED, | |
748 | + CCS_CONFIG_LEARNING, | |
749 | + CCS_CONFIG_PERMISSIVE, | |
750 | + CCS_CONFIG_ENFORCING, | |
751 | + CCS_CONFIG_MAX_MODE, | |
752 | + CCS_CONFIG_WANT_REJECT_LOG = 64, | |
753 | + CCS_CONFIG_WANT_GRANT_LOG = 128, | |
754 | + CCS_CONFIG_USE_DEFAULT = 255, | |
755 | +}; | |
756 | + | |
757 | +/* Index numbers for socket operations. */ | |
758 | +enum ccs_network_acl_index { | |
759 | + CCS_NETWORK_BIND, /* bind() operation. */ | |
760 | + CCS_NETWORK_LISTEN, /* listen() operation. */ | |
761 | + CCS_NETWORK_CONNECT, /* connect() operation. */ | |
762 | + CCS_NETWORK_ACCEPT, /* accept() operation. */ | |
763 | + CCS_NETWORK_SEND, /* send() operation. */ | |
764 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
765 | + CCS_NETWORK_RECV, /* recv() operation. */ | |
766 | +#endif | |
767 | + CCS_MAX_NETWORK_OPERATION | |
768 | +}; | |
769 | + | |
770 | +/* Index numbers for access controls with two pathnames. */ | |
771 | +enum ccs_path2_acl_index { | |
772 | + CCS_TYPE_LINK, | |
773 | + CCS_TYPE_RENAME, | |
774 | + CCS_TYPE_PIVOT_ROOT, | |
775 | + CCS_MAX_PATH2_OPERATION | |
776 | +}; | |
777 | + | |
778 | +/* Index numbers for access controls with one pathname. */ | |
779 | +enum ccs_path_acl_index { | |
780 | + CCS_TYPE_EXECUTE, | |
781 | + CCS_TYPE_READ, | |
782 | + CCS_TYPE_WRITE, | |
783 | + CCS_TYPE_APPEND, | |
784 | + CCS_TYPE_UNLINK, | |
785 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
786 | + CCS_TYPE_GETATTR, | |
787 | +#endif | |
788 | + CCS_TYPE_RMDIR, | |
789 | + CCS_TYPE_TRUNCATE, | |
790 | + CCS_TYPE_SYMLINK, | |
791 | + CCS_TYPE_CHROOT, | |
792 | + CCS_TYPE_UMOUNT, | |
793 | + CCS_MAX_PATH_OPERATION | |
794 | +}; | |
795 | + | |
796 | +/* Index numbers for access controls with one pathname and one number. */ | |
797 | +enum ccs_path_number_acl_index { | |
798 | + CCS_TYPE_CREATE, | |
799 | + CCS_TYPE_MKDIR, | |
800 | + CCS_TYPE_MKFIFO, | |
801 | + CCS_TYPE_MKSOCK, | |
802 | + CCS_TYPE_IOCTL, | |
803 | + CCS_TYPE_CHMOD, | |
804 | + CCS_TYPE_CHOWN, | |
805 | + CCS_TYPE_CHGRP, | |
806 | + CCS_MAX_PATH_NUMBER_OPERATION | |
807 | +}; | |
808 | + | |
809 | +/* Index numbers for stat(). */ | |
810 | +enum ccs_path_stat_index { | |
811 | + /* Do not change this order. */ | |
812 | + CCS_PATH1, | |
813 | + CCS_PATH1_PARENT, | |
814 | + CCS_PATH2, | |
815 | + CCS_PATH2_PARENT, | |
816 | + CCS_MAX_PATH_STAT | |
817 | +}; | |
818 | + | |
819 | +/* Index numbers for entry type. */ | |
820 | +enum ccs_policy_id { | |
821 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
822 | + CCS_ID_RESERVEDPORT, | |
823 | +#endif | |
824 | + CCS_ID_GROUP, | |
825 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
826 | + CCS_ID_ADDRESS_GROUP, | |
827 | +#endif | |
828 | + CCS_ID_PATH_GROUP, | |
829 | + CCS_ID_NUMBER_GROUP, | |
830 | + CCS_ID_AGGREGATOR, | |
831 | + CCS_ID_TRANSITION_CONTROL, | |
832 | + CCS_ID_MANAGER, | |
833 | + CCS_ID_CONDITION, | |
834 | + CCS_ID_NAME, | |
835 | + CCS_ID_ACL, | |
836 | + CCS_ID_DOMAIN, | |
837 | + CCS_MAX_POLICY | |
838 | +}; | |
839 | + | |
840 | +/* Index numbers for /proc/ccs/stat interface. */ | |
841 | +enum ccs_policy_stat_type { | |
842 | + /* Do not change this order. */ | |
843 | + CCS_STAT_POLICY_UPDATES, | |
844 | + CCS_STAT_POLICY_LEARNING, /* == CCS_CONFIG_LEARNING */ | |
845 | + CCS_STAT_POLICY_PERMISSIVE, /* == CCS_CONFIG_PERMISSIVE */ | |
846 | + CCS_STAT_POLICY_ENFORCING, /* == CCS_CONFIG_ENFORCING */ | |
847 | + CCS_MAX_POLICY_STAT | |
848 | +}; | |
849 | + | |
850 | +/* Index numbers for profile's PREFERENCE values. */ | |
851 | +enum ccs_pref_index { | |
852 | + CCS_PREF_MAX_AUDIT_LOG, | |
853 | + CCS_PREF_MAX_LEARNING_ENTRY, | |
854 | + CCS_PREF_ENFORCING_PENALTY, | |
855 | + CCS_MAX_PREF | |
856 | +}; | |
857 | + | |
858 | +/* Index numbers for /proc/ccs/ interfaces. */ | |
859 | +enum ccs_proc_interface_index { | |
860 | + CCS_DOMAIN_POLICY, | |
861 | + CCS_EXCEPTION_POLICY, | |
862 | + CCS_PROCESS_STATUS, | |
863 | + CCS_STAT, | |
864 | + CCS_AUDIT, | |
865 | + CCS_VERSION, | |
866 | + CCS_PROFILE, | |
867 | + CCS_QUERY, | |
868 | + CCS_MANAGER, | |
869 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
870 | + CCS_EXECUTE_HANDLER, | |
871 | +#endif | |
872 | +}; | |
873 | + | |
874 | +/* Index numbers for special mount operations. */ | |
875 | +enum ccs_special_mount { | |
876 | + CCS_MOUNT_BIND, /* mount --bind /source /dest */ | |
877 | + CCS_MOUNT_MOVE, /* mount --move /old /new */ | |
878 | + CCS_MOUNT_REMOUNT, /* mount -o remount /dir */ | |
879 | + CCS_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */ | |
880 | + CCS_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */ | |
881 | + CCS_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */ | |
882 | + CCS_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */ | |
883 | + CCS_MAX_SPECIAL_MOUNT | |
884 | +}; | |
885 | + | |
886 | +/* Index numbers for domain transition control keywords. */ | |
887 | +enum ccs_transition_type { | |
888 | + /* Do not change this order, */ | |
889 | + CCS_TRANSITION_CONTROL_NO_RESET, | |
890 | + CCS_TRANSITION_CONTROL_RESET, | |
891 | + CCS_TRANSITION_CONTROL_NO_INITIALIZE, | |
892 | + CCS_TRANSITION_CONTROL_INITIALIZE, | |
893 | + CCS_TRANSITION_CONTROL_NO_KEEP, | |
894 | + CCS_TRANSITION_CONTROL_KEEP, | |
895 | + CCS_MAX_TRANSITION_TYPE | |
896 | +}; | |
897 | + | |
898 | +/* Index numbers for type of numeric values. */ | |
899 | +enum ccs_value_type { | |
900 | + CCS_VALUE_TYPE_INVALID, | |
901 | + CCS_VALUE_TYPE_DECIMAL, | |
902 | + CCS_VALUE_TYPE_OCTAL, | |
903 | + CCS_VALUE_TYPE_HEXADECIMAL, | |
904 | +}; | |
905 | + | |
906 | +/* Constants definition for internal use. */ | |
907 | + | |
908 | +/* | |
909 | + * TOMOYO uses this hash only when appending a string into the string table. | |
910 | + * Frequency of appending strings is very low. So we don't need large (e.g. | |
911 | + * 64k) hash size. 256 will be sufficient. | |
912 | + */ | |
913 | +#define CCS_HASH_BITS 8 | |
914 | +#define CCS_MAX_HASH (1u << CCS_HASH_BITS) | |
915 | + | |
916 | +/* | |
917 | + * TOMOYO checks only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET. | |
918 | + * Therefore, we don't need SOCK_MAX. | |
919 | + */ | |
920 | +#define CCS_SOCK_MAX 6 | |
921 | + | |
922 | +/* Size of temporary buffer for execve() operation. */ | |
923 | +#define CCS_EXEC_TMPSIZE 4096 | |
924 | + | |
925 | +/* Garbage collector is trying to kfree() this element. */ | |
926 | +#define CCS_GC_IN_PROGRESS -1 | |
927 | + | |
928 | +/* Profile number is an integer between 0 and 255. */ | |
929 | +#define CCS_MAX_PROFILES 256 | |
930 | + | |
931 | +/* Group number is an integer between 0 and 255. */ | |
932 | +#define CCS_MAX_ACL_GROUPS 256 | |
933 | + | |
934 | +/* Current thread is doing open(O_RDONLY | O_TRUNC) ? */ | |
935 | +#define CCS_OPEN_FOR_READ_TRUNCATE 1 | |
936 | +/* Current thread is doing open(3) ? */ | |
937 | +#define CCS_OPEN_FOR_IOCTL_ONLY 2 | |
938 | +/* Current thread is doing do_execve() ? */ | |
939 | +#define CCS_TASK_IS_IN_EXECVE 4 | |
940 | +/* Current thread is running as an execute handler program? */ | |
941 | +#define CCS_TASK_IS_EXECUTE_HANDLER 8 | |
942 | +/* Current thread is allowed to modify policy via /proc/ccs/ interface? */ | |
943 | +#define CCS_TASK_IS_MANAGER 16 | |
944 | + | |
945 | +/* | |
946 | + * Retry this request. Returned by ccs_supervisor() if policy violation has | |
947 | + * occurred in enforcing mode and the userspace daemon decided to retry. | |
948 | + * | |
949 | + * We must choose a positive value in order to distinguish "granted" (which is | |
950 | + * 0) and "rejected" (which is a negative value) and "retry". | |
951 | + */ | |
952 | +#define CCS_RETRY_REQUEST 1 | |
953 | + | |
954 | +/* The gfp flags used by TOMOYO. */ | |
955 | +#define CCS_GFP_FLAGS GFP_NOFS | |
956 | + | |
957 | +/* Size of read buffer for /proc/ccs/ interface. */ | |
958 | +#define CCS_MAX_IO_READ_QUEUE 64 | |
959 | + | |
960 | +/* Structure definition for internal use. */ | |
961 | + | |
962 | +/* Common header for holding ACL entries. */ | |
963 | +struct ccs_acl_head { | |
964 | + struct list_head list; | |
965 | + s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */ | |
966 | +} __packed; | |
967 | + | |
968 | +/* Common header for shared entries. */ | |
969 | +struct ccs_shared_acl_head { | |
970 | + struct list_head list; | |
971 | + atomic_t users; | |
972 | +} __packed; | |
973 | + | |
974 | +/* Common header for individual entries. */ | |
975 | +struct ccs_acl_info { | |
976 | + struct list_head list; | |
977 | + struct ccs_condition *cond; /* Maybe NULL. */ | |
978 | + s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */ | |
979 | + u8 type; /* One of values in "enum ccs_acl_entry_type_index". */ | |
980 | + u16 perm; | |
981 | +} __packed; | |
982 | + | |
983 | +/* Structure for holding a word. */ | |
984 | +struct ccs_name_union { | |
985 | + /* Either @filename or @group is NULL. */ | |
986 | + const struct ccs_path_info *filename; | |
987 | + struct ccs_group *group; | |
988 | +}; | |
989 | + | |
990 | +/* Structure for holding a number. */ | |
991 | +struct ccs_number_union { | |
992 | + unsigned long values[2]; | |
993 | + struct ccs_group *group; /* Maybe NULL. */ | |
994 | + /* One of values in "enum ccs_value_type". */ | |
995 | + u8 value_type[2]; | |
996 | +}; | |
997 | + | |
998 | +/* Structure for holding an IP address. */ | |
999 | +struct ccs_ipaddr_union { | |
1000 | + struct in6_addr ip[2]; /* Big endian. */ | |
1001 | + struct ccs_group *group; /* Pointer to address group. */ | |
1002 | + bool is_ipv6; /* Valid only if @group == NULL. */ | |
1003 | +}; | |
1004 | + | |
1005 | +/* Structure for "path_group"/"number_group"/"address_group" directive. */ | |
1006 | +struct ccs_group { | |
1007 | + struct ccs_shared_acl_head head; | |
1008 | + /* Name of group (without leading '@'). */ | |
1009 | + const struct ccs_path_info *group_name; | |
1010 | + /* | |
1011 | + * List of "struct ccs_path_group" or "struct ccs_number_group" or | |
1012 | + * "struct ccs_address_group". | |
1013 | + */ | |
1014 | + struct list_head member_list; | |
1015 | +}; | |
1016 | + | |
1017 | +/* Structure for "path_group" directive. */ | |
1018 | +struct ccs_path_group { | |
1019 | + struct ccs_acl_head head; | |
1020 | + const struct ccs_path_info *member_name; | |
1021 | +}; | |
1022 | + | |
1023 | +/* Structure for "number_group" directive. */ | |
1024 | +struct ccs_number_group { | |
1025 | + struct ccs_acl_head head; | |
1026 | + struct ccs_number_union number; | |
1027 | +}; | |
1028 | + | |
1029 | +/* Structure for "address_group" directive. */ | |
1030 | +struct ccs_address_group { | |
1031 | + struct ccs_acl_head head; | |
1032 | + /* Structure for holding an IP address. */ | |
1033 | + struct ccs_ipaddr_union address; | |
1034 | +}; | |
1035 | + | |
1036 | +/* Subset of "struct stat". Used by conditional ACL and audit logs. */ | |
1037 | +struct ccs_mini_stat { | |
1038 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
1039 | + kuid_t uid; | |
1040 | + kgid_t gid; | |
1041 | +#else | |
1042 | + uid_t uid; | |
1043 | + gid_t gid; | |
1044 | +#endif | |
1045 | + ino_t ino; | |
1046 | + umode_t mode; | |
1047 | + dev_t dev; | |
1048 | + dev_t rdev; | |
1049 | +}; | |
1050 | + | |
1051 | +/* Structure for dumping argv[] and envp[] of "struct linux_binprm". */ | |
1052 | +struct ccs_page_dump { | |
1053 | + struct page *page; /* Previously dumped page. */ | |
1054 | + char *data; /* Contents of "page". Size is PAGE_SIZE. */ | |
1055 | +}; | |
1056 | + | |
1057 | +/* Structure for attribute checks in addition to pathname checks. */ | |
1058 | +struct ccs_obj_info { | |
1059 | + /* True if ccs_get_attributes() was already called, false otherwise. */ | |
1060 | + bool validate_done; | |
1061 | + /* True if @stat[] is valid. */ | |
1062 | + bool stat_valid[CCS_MAX_PATH_STAT]; | |
1063 | + /* First pathname. Initialized with { NULL, NULL } if no path. */ | |
1064 | + struct path path1; | |
1065 | + /* Second pathname. Initialized with { NULL, NULL } if no path. */ | |
1066 | + struct path path2; | |
1067 | + /* | |
1068 | + * Information on @path1, @path1's parent directory, @path2, @path2's | |
1069 | + * parent directory. | |
1070 | + */ | |
1071 | + struct ccs_mini_stat stat[CCS_MAX_PATH_STAT]; | |
1072 | + /* | |
1073 | + * Content of symbolic link to be created. NULL for operations other | |
1074 | + * than symlink(). | |
1075 | + */ | |
1076 | + struct ccs_path_info *symlink_target; | |
1077 | +}; | |
1078 | + | |
1079 | +/* Structure for entries which follows "struct ccs_condition". */ | |
1080 | +struct ccs_condition_element { | |
1081 | + /* | |
1082 | + * Left hand operand. A "struct ccs_argv" for CCS_ARGV_ENTRY, a | |
1083 | + * "struct ccs_envp" for CCS_ENVP_ENTRY is attached to the tail | |
1084 | + * of the array of this struct. | |
1085 | + */ | |
1086 | + u8 left; | |
1087 | + /* | |
1088 | + * Right hand operand. A "struct ccs_number_union" for | |
1089 | + * CCS_NUMBER_UNION, a "struct ccs_name_union" for CCS_NAME_UNION is | |
1090 | + * attached to the tail of the array of this struct. | |
1091 | + */ | |
1092 | + u8 right; | |
1093 | + /* Equation operator. True if equals or overlaps, false otherwise. */ | |
1094 | + bool equals; | |
1095 | +}; | |
1096 | + | |
1097 | +/* Structure for optional arguments. */ | |
1098 | +struct ccs_condition { | |
1099 | + struct ccs_shared_acl_head head; | |
1100 | + u32 size; /* Memory size allocated for this entry. */ | |
1101 | + u16 condc; /* Number of conditions in this struct. */ | |
1102 | + u16 numbers_count; /* Number of "struct ccs_number_union values". */ | |
1103 | + u16 names_count; /* Number of "struct ccs_name_union names". */ | |
1104 | + u16 argc; /* Number of "struct ccs_argv". */ | |
1105 | + u16 envc; /* Number of "struct ccs_envp". */ | |
1106 | + u8 grant_log; /* One of values in "enum ccs_grant_log". */ | |
1107 | + bool exec_transit; /* True if transit is for "file execute". */ | |
1108 | + const struct ccs_path_info *transit; /* Maybe NULL. */ | |
1109 | + /* | |
1110 | + * struct ccs_condition_element condition[condc]; | |
1111 | + * struct ccs_number_union values[numbers_count]; | |
1112 | + * struct ccs_name_union names[names_count]; | |
1113 | + * struct ccs_argv argv[argc]; | |
1114 | + * struct ccs_envp envp[envc]; | |
1115 | + */ | |
1116 | +}; | |
1117 | + | |
1118 | +struct ccs_execve; | |
1119 | +struct ccs_policy_namespace; | |
1120 | + | |
1121 | +/* Structure for request info. */ | |
1122 | +struct ccs_request_info { | |
1123 | + /* | |
1124 | + * For holding parameters specific to operations which deal files. | |
1125 | + * NULL if not dealing files. | |
1126 | + */ | |
1127 | + struct ccs_obj_info *obj; | |
1128 | + /* | |
1129 | + * For holding parameters specific to execve() request. | |
1130 | + * NULL if not dealing do_execve(). | |
1131 | + */ | |
1132 | + struct ccs_execve *ee; | |
1133 | + /* | |
1134 | + * For holding parameters. | |
1135 | + * Pointers in this union are not NULL except path->matched_path. | |
1136 | + */ | |
1137 | + union { | |
1138 | + struct { | |
1139 | + const struct ccs_path_info *filename; | |
1140 | + /* | |
1141 | + * For using wildcards at ccs_find_next_domain(). | |
1142 | + * | |
1143 | + * The matched_acl cannot be used because it may refer | |
1144 | + * a "struct ccs_path_acl" with ->is_group == true. | |
1145 | + * We want to use exact "struct ccs_path_info" rather | |
1146 | + * than "struct ccs_path_acl". | |
1147 | + */ | |
1148 | + const struct ccs_path_info *matched_path; | |
1149 | + /* One of values in "enum ccs_path_acl_index". */ | |
1150 | + u8 operation; | |
1151 | + } path; | |
1152 | + struct { | |
1153 | + const struct ccs_path_info *filename1; | |
1154 | + const struct ccs_path_info *filename2; | |
1155 | + /* One of values in "enum ccs_path2_acl_index". */ | |
1156 | + u8 operation; | |
1157 | + } path2; | |
1158 | + struct { | |
1159 | + const struct ccs_path_info *filename; | |
1160 | + unsigned int mode; | |
1161 | + unsigned int major; | |
1162 | + unsigned int minor; | |
1163 | + /* One of values in "enum ccs_mkdev_acl_index". */ | |
1164 | + u8 operation; | |
1165 | + } mkdev; | |
1166 | + struct { | |
1167 | + const struct ccs_path_info *filename; | |
1168 | + unsigned long number; | |
1169 | + /* | |
1170 | + * One of values in "enum ccs_path_number_acl_index". | |
1171 | + */ | |
1172 | + u8 operation; | |
1173 | + } path_number; | |
1174 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
1175 | + struct { | |
1176 | + const u32 *address; /* Big endian. */ | |
1177 | + u16 port; /* Host endian. */ | |
1178 | + /* One of values smaller than CCS_SOCK_MAX. */ | |
1179 | + u8 protocol; | |
1180 | + /* One of values in "enum ccs_network_acl_index". */ | |
1181 | + u8 operation; | |
1182 | + bool is_ipv6; | |
1183 | + } inet_network; | |
1184 | + struct { | |
1185 | + const struct ccs_path_info *address; | |
1186 | + /* One of values smaller than CCS_SOCK_MAX. */ | |
1187 | + u8 protocol; | |
1188 | + /* One of values in "enum ccs_network_acl_index". */ | |
1189 | + u8 operation; | |
1190 | + } unix_network; | |
1191 | +#endif | |
1192 | +#ifdef CONFIG_CCSECURITY_MISC | |
1193 | + struct { | |
1194 | + const struct ccs_path_info *name; | |
1195 | + } environ; | |
1196 | +#endif | |
1197 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
1198 | + struct { | |
1199 | + /* One of values in "enum ccs_capability_acl_index". */ | |
1200 | + u8 operation; | |
1201 | + } capability; | |
1202 | +#endif | |
1203 | +#ifdef CONFIG_CCSECURITY_IPC | |
1204 | + struct { | |
1205 | + const char *dest_pattern; | |
1206 | + int sig; | |
1207 | + } signal; | |
1208 | +#endif | |
1209 | + struct { | |
1210 | + const struct ccs_path_info *type; | |
1211 | + const struct ccs_path_info *dir; | |
1212 | + const struct ccs_path_info *dev; | |
1213 | + unsigned long flags; | |
1214 | + int need_dev; | |
1215 | + } mount; | |
1216 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
1217 | + struct { | |
1218 | + const struct ccs_path_info *domainname; | |
1219 | + } task; | |
1220 | +#endif | |
1221 | + } param; | |
1222 | + /* | |
1223 | + * For updating current->ccs_domain_info at ccs_update_task_domain(). | |
1224 | + * Initialized to NULL at ccs_init_request_info(). | |
1225 | + * Matching "struct ccs_acl_info" is copied if access request was | |
1226 | + * granted. Re-initialized to NULL at ccs_update_task_domain(). | |
1227 | + */ | |
1228 | + struct ccs_acl_info *matched_acl; | |
1229 | + u8 param_type; /* One of values in "enum ccs_acl_entry_type_index". */ | |
1230 | + bool granted; /* True if granted, false otherwise. */ | |
1231 | + /* True if current thread should not be carried sleep penalty. */ | |
1232 | + bool dont_sleep_on_enforce_error; | |
1233 | + /* | |
1234 | + * For counting number of retries made for this request. | |
1235 | + * This counter is incremented whenever ccs_supervisor() returned | |
1236 | + * CCS_RETRY_REQUEST. | |
1237 | + */ | |
1238 | + u8 retry; | |
1239 | + /* | |
1240 | + * For holding profile number used for this request. | |
1241 | + * One of values between 0 and CCS_MAX_PROFILES - 1. | |
1242 | + */ | |
1243 | + u8 profile; | |
1244 | + /* | |
1245 | + * For holding operation mode used for this request. | |
1246 | + * One of CCS_CONFIG_DISABLED, CCS_CONFIG_LEARNING, | |
1247 | + * CCS_CONFIG_PERMISSIVE, CCS_CONFIG_ENFORCING. | |
1248 | + */ | |
1249 | + u8 mode; | |
1250 | + /* | |
1251 | + * For holding operation index used for this request. | |
1252 | + * Used by ccs_init_request_info() / ccs_get_mode() / | |
1253 | + * ccs_write_log(). One of values in "enum ccs_mac_index". | |
1254 | + */ | |
1255 | + u8 type; | |
1256 | +}; | |
1257 | + | |
1258 | +/* Structure for holding a token. */ | |
1259 | +struct ccs_path_info { | |
1260 | + const char *name; | |
1261 | + u32 hash; /* = full_name_hash(name, strlen(name)) */ | |
1262 | + u16 total_len; /* = strlen(name) */ | |
1263 | + u16 const_len; /* = ccs_const_part_length(name) */ | |
1264 | + bool is_dir; /* = ccs_strendswith(name, "/") */ | |
1265 | + bool is_patterned; /* = const_len < total_len */ | |
1266 | +}; | |
1267 | + | |
1268 | +/* Structure for execve() operation. */ | |
1269 | +struct ccs_execve { | |
1270 | + struct ccs_request_info r; | |
1271 | + struct ccs_obj_info obj; | |
1272 | + struct linux_binprm *bprm; | |
1273 | + struct ccs_domain_info *previous_domain; | |
1274 | + const struct ccs_path_info *transition; | |
1275 | + /* For execute_handler */ | |
1276 | + const struct ccs_path_info *handler; | |
1277 | + char *handler_path; /* = kstrdup(handler->name, CCS_GFP_FLAGS) */ | |
1278 | + /* For dumping argv[] and envp[]. */ | |
1279 | + struct ccs_page_dump dump; | |
1280 | + /* For temporary use. */ | |
1281 | + char *tmp; /* Size is CCS_EXEC_TMPSIZE bytes */ | |
1282 | +}; | |
1283 | + | |
1284 | +/* Structure for domain information. */ | |
1285 | +struct ccs_domain_info { | |
1286 | + struct list_head list; | |
1287 | + struct list_head acl_info_list; | |
1288 | + /* Name of this domain. Never NULL. */ | |
1289 | + const struct ccs_path_info *domainname; | |
1290 | + /* Namespace for this domain. Never NULL. */ | |
1291 | + struct ccs_policy_namespace *ns; | |
1292 | + /* Group numbers to use. */ | |
1293 | + unsigned long group[CCS_MAX_ACL_GROUPS / BITS_PER_LONG]; | |
1294 | + u8 profile; /* Profile number to use. */ | |
1295 | + bool is_deleted; /* Delete flag. */ | |
1296 | + bool flags[CCS_MAX_DOMAIN_INFO_FLAGS]; | |
1297 | +}; | |
1298 | + | |
1299 | +/* | |
1300 | + * Structure for "reset_domain"/"no_reset_domain"/"initialize_domain"/ | |
1301 | + * "no_initialize_domain"/"keep_domain"/"no_keep_domain" keyword. | |
1302 | + */ | |
1303 | +struct ccs_transition_control { | |
1304 | + struct ccs_acl_head head; | |
1305 | + u8 type; /* One of values in "enum ccs_transition_type" */ | |
1306 | + bool is_last_name; /* True if the domainname is ccs_last_word(). */ | |
1307 | + const struct ccs_path_info *domainname; /* Maybe NULL */ | |
1308 | + const struct ccs_path_info *program; /* Maybe NULL */ | |
1309 | +}; | |
1310 | + | |
1311 | +/* Structure for "aggregator" keyword. */ | |
1312 | +struct ccs_aggregator { | |
1313 | + struct ccs_acl_head head; | |
1314 | + const struct ccs_path_info *original_name; | |
1315 | + const struct ccs_path_info *aggregated_name; | |
1316 | +}; | |
1317 | + | |
1318 | +/* Structure for "deny_autobind" keyword. */ | |
1319 | +struct ccs_reserved { | |
1320 | + struct ccs_acl_head head; | |
1321 | + struct ccs_number_union port; | |
1322 | +}; | |
1323 | + | |
1324 | +/* Structure for policy manager. */ | |
1325 | +struct ccs_manager { | |
1326 | + struct ccs_acl_head head; | |
1327 | + /* A path to program or a domainname. */ | |
1328 | + const struct ccs_path_info *manager; | |
1329 | +}; | |
1330 | + | |
1331 | +/* Structure for argv[]. */ | |
1332 | +struct ccs_argv { | |
1333 | + unsigned long index; | |
1334 | + const struct ccs_path_info *value; | |
1335 | + bool is_not; | |
1336 | +}; | |
1337 | + | |
1338 | +/* Structure for envp[]. */ | |
1339 | +struct ccs_envp { | |
1340 | + const struct ccs_path_info *name; | |
1341 | + const struct ccs_path_info *value; | |
1342 | + bool is_not; | |
1343 | +}; | |
1344 | + | |
1345 | +/* | |
1346 | + * Structure for "task auto_execute_handler" and "task denied_execute_handler" | |
1347 | + * directive. | |
1348 | + * | |
1349 | + * If "task auto_execute_handler" directive exists and the current process is | |
1350 | + * not an execute handler, all execve() requests are replaced by execve() | |
1351 | + * requests of a program specified by "task auto_execute_handler" directive. | |
1352 | + * If the current process is an execute handler, "task auto_execute_handler" | |
1353 | + * and "task denied_execute_handler" directives are ignored. | |
1354 | + * The program specified by "task execute_handler" validates execve() | |
1355 | + * parameters and executes the original execve() requests if appropriate. | |
1356 | + * | |
1357 | + * "task denied_execute_handler" directive is used only when execve() request | |
1358 | + * was rejected in enforcing mode (i.e. CONFIG::file::execute={ mode=enforcing | |
1359 | + * }). The program specified by "task denied_execute_handler" does whatever it | |
1360 | + * wants to do (e.g. silently terminate, change firewall settings, redirect the | |
1361 | + * user to honey pot etc.). | |
1362 | + */ | |
1363 | +struct ccs_handler_acl { | |
1364 | + struct ccs_acl_info head; /* type = CCS_TYPE_*_EXECUTE_HANDLER */ | |
1365 | + const struct ccs_path_info *handler; /* Pointer to single pathname. */ | |
1366 | +}; | |
1367 | + | |
1368 | +/* | |
1369 | + * Structure for "task auto_domain_transition" and | |
1370 | + * "task manual_domain_transition" directive. | |
1371 | + */ | |
1372 | +struct ccs_task_acl { | |
1373 | + struct ccs_acl_info head; /* type = CCS_TYPE_*_TASK_ACL */ | |
1374 | + /* Pointer to domainname. */ | |
1375 | + const struct ccs_path_info *domainname; | |
1376 | +}; | |
1377 | + | |
1378 | +/* | |
1379 | + * Structure for "file execute", "file read", "file write", "file append", | |
1380 | + * "file unlink", "file getattr", "file rmdir", "file truncate", | |
1381 | + * "file symlink", "file chroot" and "file unmount" directive. | |
1382 | + */ | |
1383 | +struct ccs_path_acl { | |
1384 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH_ACL */ | |
1385 | + struct ccs_name_union name; | |
1386 | +}; | |
1387 | + | |
1388 | +/* | |
1389 | + * Structure for "file rename", "file link" and "file pivot_root" directive. | |
1390 | + */ | |
1391 | +struct ccs_path2_acl { | |
1392 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH2_ACL */ | |
1393 | + struct ccs_name_union name1; | |
1394 | + struct ccs_name_union name2; | |
1395 | +}; | |
1396 | + | |
1397 | +/* | |
1398 | + * Structure for "file create", "file mkdir", "file mkfifo", "file mksock", | |
1399 | + * "file ioctl", "file chmod", "file chown" and "file chgrp" directive. | |
1400 | + */ | |
1401 | +struct ccs_path_number_acl { | |
1402 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH_NUMBER_ACL */ | |
1403 | + struct ccs_name_union name; | |
1404 | + struct ccs_number_union number; | |
1405 | +}; | |
1406 | + | |
1407 | +/* Structure for "file mkblock" and "file mkchar" directive. */ | |
1408 | +struct ccs_mkdev_acl { | |
1409 | + struct ccs_acl_info head; /* type = CCS_TYPE_MKDEV_ACL */ | |
1410 | + struct ccs_name_union name; | |
1411 | + struct ccs_number_union mode; | |
1412 | + struct ccs_number_union major; | |
1413 | + struct ccs_number_union minor; | |
1414 | +}; | |
1415 | + | |
1416 | +/* Structure for "file mount" directive. */ | |
1417 | +struct ccs_mount_acl { | |
1418 | + struct ccs_acl_info head; /* type = CCS_TYPE_MOUNT_ACL */ | |
1419 | + struct ccs_name_union dev_name; | |
1420 | + struct ccs_name_union dir_name; | |
1421 | + struct ccs_name_union fs_type; | |
1422 | + struct ccs_number_union flags; | |
1423 | +}; | |
1424 | + | |
1425 | +/* Structure for "misc env" directive in domain policy. */ | |
1426 | +struct ccs_env_acl { | |
1427 | + struct ccs_acl_info head; /* type = CCS_TYPE_ENV_ACL */ | |
1428 | + const struct ccs_path_info *env; /* environment variable */ | |
1429 | +}; | |
1430 | + | |
1431 | +/* Structure for "capability" directive. */ | |
1432 | +struct ccs_capability_acl { | |
1433 | + struct ccs_acl_info head; /* type = CCS_TYPE_CAPABILITY_ACL */ | |
1434 | + u8 operation; /* One of values in "enum ccs_capability_acl_index". */ | |
1435 | +}; | |
1436 | + | |
1437 | +/* Structure for "ipc signal" directive. */ | |
1438 | +struct ccs_signal_acl { | |
1439 | + struct ccs_acl_info head; /* type = CCS_TYPE_SIGNAL_ACL */ | |
1440 | + struct ccs_number_union sig; | |
1441 | + /* Pointer to destination pattern. */ | |
1442 | + const struct ccs_path_info *domainname; | |
1443 | +}; | |
1444 | + | |
1445 | +/* Structure for "network inet" directive. */ | |
1446 | +struct ccs_inet_acl { | |
1447 | + struct ccs_acl_info head; /* type = CCS_TYPE_INET_ACL */ | |
1448 | + u8 protocol; | |
1449 | + struct ccs_ipaddr_union address; | |
1450 | + struct ccs_number_union port; | |
1451 | +}; | |
1452 | + | |
1453 | +/* Structure for "network unix" directive. */ | |
1454 | +struct ccs_unix_acl { | |
1455 | + struct ccs_acl_info head; /* type = CCS_TYPE_UNIX_ACL */ | |
1456 | + u8 protocol; | |
1457 | + struct ccs_name_union name; | |
1458 | +}; | |
1459 | + | |
1460 | +/* Structure for holding string data. */ | |
1461 | +struct ccs_name { | |
1462 | + struct ccs_shared_acl_head head; | |
1463 | + int size; /* Memory size allocated for this entry. */ | |
1464 | + struct ccs_path_info entry; | |
1465 | +}; | |
1466 | + | |
1467 | +/* Structure for holding a line from /proc/ccs/ interface. */ | |
1468 | +struct ccs_acl_param { | |
1469 | + char *data; /* Unprocessed data. */ | |
1470 | + struct list_head *list; /* List to add or remove. */ | |
1471 | + struct ccs_policy_namespace *ns; /* Namespace to use. */ | |
1472 | + bool is_delete; /* True if it is a delete request. */ | |
1473 | + union ccs_acl_union { | |
1474 | + struct ccs_acl_info acl_info; | |
1475 | + struct ccs_handler_acl handler_acl; | |
1476 | + struct ccs_task_acl task_acl; | |
1477 | + struct ccs_path_acl path_acl; | |
1478 | + struct ccs_path2_acl path2_acl; | |
1479 | + struct ccs_path_number_acl path_number_acl; | |
1480 | + struct ccs_mkdev_acl mkdev_acl; | |
1481 | + struct ccs_mount_acl mount_acl; | |
1482 | + struct ccs_env_acl env_acl; | |
1483 | + struct ccs_capability_acl capability_acl; | |
1484 | + struct ccs_signal_acl signal_acl; | |
1485 | + struct ccs_inet_acl inet_acl; | |
1486 | + struct ccs_unix_acl unix_acl; | |
1487 | + /**/ | |
1488 | + struct ccs_acl_head acl_head; | |
1489 | + struct ccs_transition_control transition_control; | |
1490 | + struct ccs_aggregator aggregator; | |
1491 | + struct ccs_reserved reserved; | |
1492 | + struct ccs_manager manager; | |
1493 | + struct ccs_path_group path_group; | |
1494 | + struct ccs_number_group number_group; | |
1495 | + struct ccs_address_group address_group; | |
1496 | + } e; | |
1497 | +}; | |
1498 | + | |
1499 | +/* Structure for reading/writing policy via /proc/ccs/ interfaces. */ | |
1500 | +struct ccs_io_buffer { | |
1501 | + /* Exclusive lock for this structure. */ | |
1502 | + struct mutex io_sem; | |
1503 | + char __user *read_user_buf; | |
1504 | + size_t read_user_buf_avail; | |
1505 | + struct { | |
1506 | + struct list_head *ns; | |
1507 | + struct list_head *domain; | |
1508 | + struct list_head *group; | |
1509 | + struct list_head *acl; | |
1510 | + size_t avail; | |
1511 | + unsigned int step; | |
1512 | + unsigned int query_index; | |
1513 | + u16 index; | |
1514 | + u16 cond_index; | |
1515 | + u8 acl_group_index; | |
1516 | + u8 cond_step; | |
1517 | + u8 bit; | |
1518 | + u8 w_pos; | |
1519 | + bool eof; | |
1520 | + bool print_this_domain_only; | |
1521 | + bool print_transition_related_only; | |
1522 | + bool print_cond_part; | |
1523 | + const char *w[CCS_MAX_IO_READ_QUEUE]; | |
1524 | + } r; | |
1525 | + struct { | |
1526 | + struct ccs_policy_namespace *ns; | |
1527 | + struct ccs_domain_info *domain; | |
1528 | + size_t avail; | |
1529 | + bool is_delete; | |
1530 | + } w; | |
1531 | + /* Buffer for reading. */ | |
1532 | + char *read_buf; | |
1533 | + /* Size of read buffer. */ | |
1534 | + size_t readbuf_size; | |
1535 | + /* Buffer for writing. */ | |
1536 | + char *write_buf; | |
1537 | + /* Size of write buffer. */ | |
1538 | + size_t writebuf_size; | |
1539 | + /* Type of interface. */ | |
1540 | + enum ccs_proc_interface_index type; | |
1541 | + /* Users counter protected by ccs_io_buffer_list_lock. */ | |
1542 | + u8 users; | |
1543 | + /* List for telling GC not to kfree() elements. */ | |
1544 | + struct list_head list; | |
1545 | +}; | |
1546 | + | |
1547 | +/* Structure for /proc/ccs/profile interface. */ | |
1548 | +struct ccs_profile { | |
1549 | + const struct ccs_path_info *comment; | |
1550 | + u8 default_config; | |
1551 | + u8 config[CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX]; | |
1552 | + unsigned int pref[CCS_MAX_PREF]; | |
1553 | +}; | |
1554 | + | |
1555 | +/* Structure for representing YYYY/MM/DD hh/mm/ss. */ | |
1556 | +struct ccs_time { | |
1557 | + u16 year; | |
1558 | + u8 month; | |
1559 | + u8 day; | |
1560 | + u8 hour; | |
1561 | + u8 min; | |
1562 | + u8 sec; | |
1563 | +}; | |
1564 | + | |
1565 | +/* Structure for policy namespace. */ | |
1566 | +struct ccs_policy_namespace { | |
1567 | + /* Profile table. Memory is allocated as needed. */ | |
1568 | + struct ccs_profile *profile_ptr[CCS_MAX_PROFILES]; | |
1569 | + /* List of "struct ccs_group". */ | |
1570 | + struct list_head group_list[CCS_MAX_GROUP]; | |
1571 | + /* List of policy. */ | |
1572 | + struct list_head policy_list[CCS_MAX_POLICY]; | |
1573 | + /* The global ACL referred by "use_group" keyword. */ | |
1574 | + struct list_head acl_group[CCS_MAX_ACL_GROUPS]; | |
1575 | + /* List for connecting to ccs_namespace_list list. */ | |
1576 | + struct list_head namespace_list; | |
1577 | + /* Profile version. Currently only 20150505 is supported. */ | |
1578 | + unsigned int profile_version; | |
1579 | + /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */ | |
1580 | + const char *name; | |
1581 | +}; | |
1582 | + | |
1583 | +/* Prototype definition for "struct ccsecurity_operations". */ | |
1584 | + | |
1585 | +void __init ccs_permission_init(void); | |
1586 | +void __init ccs_mm_init(void); | |
1587 | + | |
1588 | +/* Prototype definition for internal use. */ | |
1589 | + | |
1590 | +bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, | |
1591 | + struct ccs_page_dump *dump); | |
1592 | +bool ccs_memory_ok(const void *ptr, const unsigned int size); | |
1593 | +char *ccs_encode(const char *str); | |
1594 | +char *ccs_encode2(const char *str, int str_len); | |
1595 | +char *ccs_realpath(const struct path *path); | |
1596 | +const char *ccs_get_exe(void); | |
1597 | +const struct ccs_path_info *ccs_get_name(const char *name); | |
1598 | +int ccs_audit_log(struct ccs_request_info *r); | |
1599 | +int ccs_check_acl(struct ccs_request_info *r); | |
1600 | +int ccs_init_request_info(struct ccs_request_info *r, const u8 index); | |
1601 | +struct ccs_domain_info *ccs_assign_domain(const char *domainname, | |
1602 | + const bool transit); | |
1603 | +u8 ccs_get_config(const u8 profile, const u8 index); | |
1604 | +void *ccs_commit_ok(void *data, const unsigned int size); | |
1605 | +void ccs_del_acl(struct list_head *element); | |
1606 | +void ccs_del_condition(struct list_head *element); | |
1607 | +void ccs_fill_path_info(struct ccs_path_info *ptr); | |
1608 | +void ccs_get_attributes(struct ccs_obj_info *obj); | |
1609 | +void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register); | |
1610 | +void ccs_transition_failed(const char *domainname); | |
1611 | +void ccs_warn_oom(const char *function); | |
1612 | +void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...) | |
1613 | + __printf(2, 3); | |
1614 | + | |
1615 | +/* Variable definition for internal use. */ | |
1616 | + | |
1617 | +extern bool ccs_policy_loaded; | |
1618 | +extern const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS]; | |
1619 | +extern const u8 ccs_c2mac[CCS_MAX_CAPABILITY_INDEX]; | |
1620 | +extern const u8 ccs_pn2mac[CCS_MAX_PATH_NUMBER_OPERATION]; | |
1621 | +extern const u8 ccs_pnnn2mac[CCS_MAX_MKDEV_OPERATION]; | |
1622 | +extern const u8 ccs_pp2mac[CCS_MAX_PATH2_OPERATION]; | |
1623 | +extern struct ccs_domain_info ccs_kernel_domain; | |
1624 | +extern struct list_head ccs_condition_list; | |
1625 | +extern struct list_head ccs_domain_list; | |
1626 | +extern struct list_head ccs_name_list[CCS_MAX_HASH]; | |
1627 | +extern struct list_head ccs_namespace_list; | |
1628 | +extern struct mutex ccs_policy_lock; | |
1629 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
1630 | +extern struct srcu_struct ccs_ss; | |
1631 | +#endif | |
1632 | +extern unsigned int ccs_memory_quota[CCS_MAX_MEMORY_STAT]; | |
1633 | +extern unsigned int ccs_memory_used[CCS_MAX_MEMORY_STAT]; | |
1634 | + | |
1635 | +/* Inlined functions for internal use. */ | |
1636 | + | |
1637 | +/** | |
1638 | + * ccs_pathcmp - strcmp() for "struct ccs_path_info" structure. | |
1639 | + * | |
1640 | + * @a: Pointer to "struct ccs_path_info". | |
1641 | + * @b: Pointer to "struct ccs_path_info". | |
1642 | + * | |
1643 | + * Returns true if @a != @b, false otherwise. | |
1644 | + */ | |
1645 | +static inline bool ccs_pathcmp(const struct ccs_path_info *a, | |
1646 | + const struct ccs_path_info *b) | |
1647 | +{ | |
1648 | + return a->hash != b->hash || strcmp(a->name, b->name); | |
1649 | +} | |
1650 | + | |
1651 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
1652 | + | |
1653 | +/** | |
1654 | + * ccs_read_lock - Take lock for protecting policy. | |
1655 | + * | |
1656 | + * Returns index number for ccs_read_unlock(). | |
1657 | + */ | |
1658 | +static inline int ccs_read_lock(void) | |
1659 | +{ | |
1660 | + return srcu_read_lock(&ccs_ss); | |
1661 | +} | |
1662 | + | |
1663 | +/** | |
1664 | + * ccs_read_unlock - Release lock for protecting policy. | |
1665 | + * | |
1666 | + * @idx: Index number returned by ccs_read_lock(). | |
1667 | + * | |
1668 | + * Returns nothing. | |
1669 | + */ | |
1670 | +static inline void ccs_read_unlock(const int idx) | |
1671 | +{ | |
1672 | + srcu_read_unlock(&ccs_ss, idx); | |
1673 | +} | |
1674 | + | |
1675 | +#else | |
1676 | + | |
1677 | +int ccs_lock(void); | |
1678 | +void ccs_unlock(const int idx); | |
1679 | + | |
1680 | +/** | |
1681 | + * ccs_read_lock - Take lock for protecting policy. | |
1682 | + * | |
1683 | + * Returns index number for ccs_read_unlock(). | |
1684 | + */ | |
1685 | +static inline int ccs_read_lock(void) | |
1686 | +{ | |
1687 | + return ccs_lock(); | |
1688 | +} | |
1689 | + | |
1690 | +/** | |
1691 | + * ccs_read_unlock - Release lock for protecting policy. | |
1692 | + * | |
1693 | + * @idx: Index number returned by ccs_read_lock(). | |
1694 | + * | |
1695 | + * Returns nothing. | |
1696 | + */ | |
1697 | +static inline void ccs_read_unlock(const int idx) | |
1698 | +{ | |
1699 | + ccs_unlock(idx); | |
1700 | +} | |
1701 | + | |
1702 | +#endif | |
1703 | + | |
1704 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) | |
1705 | + | |
1706 | +/** | |
1707 | + * ccs_tasklist_lock - Take lock for reading list of "struct task_struct". | |
1708 | + * | |
1709 | + * Returns nothing. | |
1710 | + */ | |
1711 | +static inline void ccs_tasklist_lock(void) | |
1712 | +{ | |
1713 | + rcu_read_lock(); | |
1714 | +} | |
1715 | + | |
1716 | +/** | |
1717 | + * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct". | |
1718 | + * | |
1719 | + * Returns nothing. | |
1720 | + */ | |
1721 | +static inline void ccs_tasklist_unlock(void) | |
1722 | +{ | |
1723 | + rcu_read_unlock(); | |
1724 | +} | |
1725 | + | |
1726 | +#else | |
1727 | + | |
1728 | +/** | |
1729 | + * ccs_tasklist_lock - Take lock for reading list of "struct task_struct". | |
1730 | + * | |
1731 | + * Returns nothing. | |
1732 | + */ | |
1733 | +static inline void ccs_tasklist_lock(void) | |
1734 | +{ | |
1735 | + read_lock(&tasklist_lock); | |
1736 | +} | |
1737 | + | |
1738 | +/** | |
1739 | + * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct". | |
1740 | + * | |
1741 | + * Returns nothing. | |
1742 | + */ | |
1743 | +static inline void ccs_tasklist_unlock(void) | |
1744 | +{ | |
1745 | + read_unlock(&tasklist_lock); | |
1746 | +} | |
1747 | + | |
1748 | +#endif | |
1749 | + | |
1750 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1751 | + | |
1752 | +/** | |
1753 | + * ccs_sys_getppid - Copy of getppid(). | |
1754 | + * | |
1755 | + * Returns parent process's PID. | |
1756 | + * | |
1757 | + * Alpha does not have getppid() defined. To be able to build this module on | |
1758 | + * Alpha, I have to copy getppid() from kernel/timer.c. | |
1759 | + */ | |
1760 | +static inline pid_t ccs_sys_getppid(void) | |
1761 | +{ | |
1762 | + pid_t pid; | |
1763 | + rcu_read_lock(); | |
1764 | + pid = task_tgid_vnr(rcu_dereference(current->real_parent)); | |
1765 | + rcu_read_unlock(); | |
1766 | + return pid; | |
1767 | +} | |
1768 | + | |
1769 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) | |
1770 | + | |
1771 | +/** | |
1772 | + * ccs_sys_getppid - Copy of getppid(). | |
1773 | + * | |
1774 | + * Returns parent process's PID. | |
1775 | + * | |
1776 | + * This function was rewritten to use RCU in 2.6.16.34. However, distributors | |
1777 | + * which use earlier kernels (e.g. 2.6.8/2.6.9) did not backport the bugfix. | |
1778 | + * Therefore, I'm using code for 2.6.16.34 for earlier kernels. | |
1779 | + */ | |
1780 | +static inline pid_t ccs_sys_getppid(void) | |
1781 | +{ | |
1782 | + pid_t pid; | |
1783 | + rcu_read_lock(); | |
1784 | +#if (defined(RHEL_MAJOR) && RHEL_MAJOR == 5) || (defined(AX_MAJOR) && AX_MAJOR == 3) | |
1785 | + pid = rcu_dereference(current->parent)->tgid; | |
1786 | +#elif defined(CONFIG_UTRACE) | |
1787 | + /* | |
1788 | + * RHEL 5.0 kernel does not have RHEL_MAJOR/RHEL_MINOR defined. | |
1789 | + * Assume RHEL 5.0 if CONFIG_UTRACE is defined. | |
1790 | + */ | |
1791 | + pid = rcu_dereference(current->parent)->tgid; | |
1792 | +#else | |
1793 | + pid = rcu_dereference(current->real_parent)->tgid; | |
1794 | +#endif | |
1795 | + rcu_read_unlock(); | |
1796 | + return pid; | |
1797 | +} | |
1798 | + | |
1799 | +#else | |
1800 | + | |
1801 | +/** | |
1802 | + * ccs_sys_getppid - Copy of getppid(). | |
1803 | + * | |
1804 | + * Returns parent process's PID. | |
1805 | + * | |
1806 | + * I can't use code for 2.6.16.34 for 2.4 kernels because 2.4 kernels does not | |
1807 | + * have RCU. Therefore, I'm using pessimistic lock (i.e. tasklist_lock | |
1808 | + * spinlock). | |
1809 | + */ | |
1810 | +static inline pid_t ccs_sys_getppid(void) | |
1811 | +{ | |
1812 | + pid_t pid; | |
1813 | + read_lock(&tasklist_lock); | |
1814 | +#ifdef TASK_DEAD | |
1815 | + pid = current->group_leader->real_parent->tgid; | |
1816 | +#else | |
1817 | + pid = current->p_opptr->pid; | |
1818 | +#endif | |
1819 | + read_unlock(&tasklist_lock); | |
1820 | + return pid; | |
1821 | +} | |
1822 | + | |
1823 | +#endif | |
1824 | + | |
1825 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1826 | + | |
1827 | +/** | |
1828 | + * ccs_sys_getpid - Copy of getpid(). | |
1829 | + * | |
1830 | + * Returns current thread's PID. | |
1831 | + * | |
1832 | + * Alpha does not have getpid() defined. To be able to build this module on | |
1833 | + * Alpha, I have to copy getpid() from kernel/timer.c. | |
1834 | + */ | |
1835 | +static inline pid_t ccs_sys_getpid(void) | |
1836 | +{ | |
1837 | + return task_tgid_vnr(current); | |
1838 | +} | |
1839 | + | |
1840 | +#else | |
1841 | + | |
1842 | +/** | |
1843 | + * ccs_sys_getpid - Copy of getpid(). | |
1844 | + * | |
1845 | + * Returns current thread's PID. | |
1846 | + */ | |
1847 | +static inline pid_t ccs_sys_getpid(void) | |
1848 | +{ | |
1849 | + return current->tgid; | |
1850 | +} | |
1851 | + | |
1852 | +#endif | |
1853 | + | |
1854 | +/** | |
1855 | + * ccs_get_mode - Get mode for specified functionality. | |
1856 | + * | |
1857 | + * @profile: Profile number. | |
1858 | + * @index: Functionality number. | |
1859 | + * | |
1860 | + * Returns mode. | |
1861 | + */ | |
1862 | +static inline u8 ccs_get_mode(const u8 profile, const u8 index) | |
1863 | +{ | |
1864 | + return ccs_get_config(profile, index) & (CCS_CONFIG_MAX_MODE - 1); | |
1865 | +} | |
1866 | + | |
1867 | +#if defined(CONFIG_SLOB) | |
1868 | + | |
1869 | +/** | |
1870 | + * ccs_round2 - Round up to power of 2 for calculating memory usage. | |
1871 | + * | |
1872 | + * @size: Size to be rounded up. | |
1873 | + * | |
1874 | + * Returns @size. | |
1875 | + * | |
1876 | + * Since SLOB does not round up, this function simply returns @size. | |
1877 | + */ | |
1878 | +static inline int ccs_round2(size_t size) | |
1879 | +{ | |
1880 | + return size; | |
1881 | +} | |
1882 | + | |
1883 | +#else | |
1884 | + | |
1885 | +/** | |
1886 | + * ccs_round2 - Round up to power of 2 for calculating memory usage. | |
1887 | + * | |
1888 | + * @size: Size to be rounded up. | |
1889 | + * | |
1890 | + * Returns rounded size. | |
1891 | + * | |
1892 | + * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of | |
1893 | + * (e.g.) 128 bytes. | |
1894 | + */ | |
1895 | +static inline int ccs_round2(size_t size) | |
1896 | +{ | |
1897 | +#if PAGE_SIZE == 4096 | |
1898 | + size_t bsize = 32; | |
1899 | +#else | |
1900 | + size_t bsize = 64; | |
1901 | +#endif | |
1902 | + if (!size) | |
1903 | + return 0; | |
1904 | + while (size > bsize) | |
1905 | + bsize <<= 1; | |
1906 | + return bsize; | |
1907 | +} | |
1908 | + | |
1909 | +#endif | |
1910 | + | |
1911 | +/** | |
1912 | + * ccs_put_condition - Drop reference on "struct ccs_condition". | |
1913 | + * | |
1914 | + * @cond: Pointer to "struct ccs_condition". Maybe NULL. | |
1915 | + * | |
1916 | + * Returns nothing. | |
1917 | + */ | |
1918 | +static inline void ccs_put_condition(struct ccs_condition *cond) | |
1919 | +{ | |
1920 | + if (cond) | |
1921 | + atomic_dec(&cond->head.users); | |
1922 | +} | |
1923 | + | |
1924 | +/** | |
1925 | + * ccs_put_group - Drop reference on "struct ccs_group". | |
1926 | + * | |
1927 | + * @group: Pointer to "struct ccs_group". Maybe NULL. | |
1928 | + * | |
1929 | + * Returns nothing. | |
1930 | + */ | |
1931 | +static inline void ccs_put_group(struct ccs_group *group) | |
1932 | +{ | |
1933 | + if (group) | |
1934 | + atomic_dec(&group->head.users); | |
1935 | +} | |
1936 | + | |
1937 | +/** | |
1938 | + * ccs_put_name - Drop reference on "struct ccs_name". | |
1939 | + * | |
1940 | + * @name: Pointer to "struct ccs_path_info". Maybe NULL. | |
1941 | + * | |
1942 | + * Returns nothing. | |
1943 | + */ | |
1944 | +static inline void ccs_put_name(const struct ccs_path_info *name) | |
1945 | +{ | |
1946 | + if (name) | |
1947 | + atomic_dec(&container_of(name, struct ccs_name, entry)-> | |
1948 | + head.users); | |
1949 | +} | |
1950 | + | |
1951 | +/* For importing variables and functions. */ | |
1952 | +extern struct ccsecurity_exports ccsecurity_exports; | |
1953 | + | |
1954 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
1955 | + | |
1956 | +/* | |
1957 | + * Structure for holding "struct ccs_domain_info *" and "struct ccs_execve *" | |
1958 | + * and "u32 ccs_flags" for each "struct task_struct". | |
1959 | + * | |
1960 | + * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct" | |
1961 | + * are maintained outside that "struct task_struct". Therefore, ccs_security | |
1962 | + * != task_struct . This keeps KABI for distributor's prebuilt kernels but | |
1963 | + * entails slow access. | |
1964 | + * | |
1965 | + * Memory for this structure is allocated when current thread tries to access | |
1966 | + * it. Therefore, if memory allocation failed, current thread will be killed by | |
1967 | + * SIGKILL. Note that if current->pid == 1, sending SIGKILL won't work. | |
1968 | + */ | |
1969 | +struct ccs_security { | |
1970 | + struct list_head list; | |
1971 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) | |
1972 | + const struct task_struct *task; | |
1973 | +#else | |
1974 | + struct pid *pid; /* Maybe NULL. */ | |
1975 | + const struct cred *cred; /* Maybe NULL. */ | |
1976 | +#endif | |
1977 | + struct ccs_domain_info *ccs_domain_info; | |
1978 | + u32 ccs_flags; | |
1979 | + struct ccs_execve *ee; /* Maybe NULL. */ | |
1980 | + struct rcu_head rcu; | |
1981 | +}; | |
1982 | + | |
1983 | +void __init ccs_main_init(void); | |
1984 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain); | |
1985 | +int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve **eep); | |
1986 | +void ccs_finish_execve(int retval, struct ccs_execve *ee); | |
1987 | +void ccs_load_policy(const char *filename); | |
1988 | +#ifndef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
1989 | +#define ccs_audit_alloc_execve(ee) do { } while (0) | |
1990 | +#define ccs_audit_free_execve(ee, is_current) do { } while (0) | |
1991 | +#else | |
1992 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee); | |
1993 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
1994 | + const bool is_current); | |
1995 | +#endif | |
1996 | + | |
1997 | +#define CCS_TASK_SECURITY_HASH_BITS 12 | |
1998 | +#define CCS_MAX_TASK_SECURITY_HASH (1u << CCS_TASK_SECURITY_HASH_BITS) | |
1999 | +extern struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
2000 | + | |
2001 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task); | |
2002 | + | |
2003 | +/** | |
2004 | + * ccs_current_security - Get "struct ccs_security" for current thread. | |
2005 | + * | |
2006 | + * Returns pointer to "struct ccs_security" for current thread. | |
2007 | + */ | |
2008 | +static inline struct ccs_security *ccs_current_security(void) | |
2009 | +{ | |
2010 | + return ccs_find_task_security(current); | |
2011 | +} | |
2012 | + | |
2013 | +/** | |
2014 | + * ccs_task_domain - Get "struct ccs_domain_info" for specified thread. | |
2015 | + * | |
2016 | + * @task: Pointer to "struct task_struct". | |
2017 | + * | |
2018 | + * Returns pointer to "struct ccs_security" for specified thread. | |
2019 | + */ | |
2020 | +static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task) | |
2021 | +{ | |
2022 | + struct ccs_domain_info *domain; | |
2023 | + rcu_read_lock(); | |
2024 | + domain = ccs_find_task_security(task)->ccs_domain_info; | |
2025 | + rcu_read_unlock(); | |
2026 | + return domain; | |
2027 | +} | |
2028 | + | |
2029 | +/** | |
2030 | + * ccs_current_domain - Get "struct ccs_domain_info" for current thread. | |
2031 | + * | |
2032 | + * Returns pointer to "struct ccs_domain_info" for current thread. | |
2033 | + */ | |
2034 | +static inline struct ccs_domain_info *ccs_current_domain(void) | |
2035 | +{ | |
2036 | + return ccs_find_task_security(current)->ccs_domain_info; | |
2037 | +} | |
2038 | + | |
2039 | +/** | |
2040 | + * ccs_task_flags - Get flags for specified thread. | |
2041 | + * | |
2042 | + * @task: Pointer to "struct task_struct". | |
2043 | + * | |
2044 | + * Returns flags for specified thread. | |
2045 | + */ | |
2046 | +static inline u32 ccs_task_flags(struct task_struct *task) | |
2047 | +{ | |
2048 | + u32 ccs_flags; | |
2049 | + rcu_read_lock(); | |
2050 | + ccs_flags = ccs_find_task_security(task)->ccs_flags; | |
2051 | + rcu_read_unlock(); | |
2052 | + return ccs_flags; | |
2053 | +} | |
2054 | + | |
2055 | +/** | |
2056 | + * ccs_current_flags - Get flags for current thread. | |
2057 | + * | |
2058 | + * Returns flags for current thread. | |
2059 | + */ | |
2060 | +static inline u32 ccs_current_flags(void) | |
2061 | +{ | |
2062 | + return ccs_find_task_security(current)->ccs_flags; | |
2063 | +} | |
2064 | + | |
2065 | +#else | |
2066 | + | |
2067 | +/* | |
2068 | + * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct" | |
2069 | + * are maintained inside that "struct task_struct". Therefore, ccs_security == | |
2070 | + * task_struct . This allows fast access but breaks KABI checks for | |
2071 | + * distributor's prebuilt kernels due to changes in "struct task_struct". | |
2072 | + */ | |
2073 | +#define ccs_security task_struct | |
2074 | + | |
2075 | +/** | |
2076 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
2077 | + * | |
2078 | + * @task: Pointer to "struct task_struct". | |
2079 | + * | |
2080 | + * Returns pointer to "struct ccs_security". | |
2081 | + */ | |
2082 | +static inline struct ccs_security *ccs_find_task_security(struct task_struct * | |
2083 | + task) | |
2084 | +{ | |
2085 | + return task; | |
2086 | +} | |
2087 | + | |
2088 | +/** | |
2089 | + * ccs_current_security - Get "struct ccs_security" for current thread. | |
2090 | + * | |
2091 | + * Returns pointer to "struct ccs_security" for current thread. | |
2092 | + */ | |
2093 | +static inline struct ccs_security *ccs_current_security(void) | |
2094 | +{ | |
2095 | + return ccs_find_task_security(current); | |
2096 | +} | |
2097 | + | |
2098 | +/** | |
2099 | + * ccs_task_domain - Get "struct ccs_domain_info" for specified thread. | |
2100 | + * | |
2101 | + * @task: Pointer to "struct task_struct". | |
2102 | + * | |
2103 | + * Returns pointer to "struct ccs_security" for specified thread. | |
2104 | + */ | |
2105 | +static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task) | |
2106 | +{ | |
2107 | + struct ccs_domain_info *domain = task->ccs_domain_info; | |
2108 | + return domain ? domain : &ccs_kernel_domain; | |
2109 | +} | |
2110 | + | |
2111 | +/** | |
2112 | + * ccs_current_domain - Get "struct ccs_domain_info" for current thread. | |
2113 | + * | |
2114 | + * Returns pointer to "struct ccs_domain_info" for current thread. | |
2115 | + * | |
2116 | + * If current thread does not belong to a domain (which is true for initial | |
2117 | + * init_task in order to hide ccs_kernel_domain from this module), | |
2118 | + * current thread enters into ccs_kernel_domain. | |
2119 | + */ | |
2120 | +static inline struct ccs_domain_info *ccs_current_domain(void) | |
2121 | +{ | |
2122 | + struct task_struct *task = current; | |
2123 | + if (!task->ccs_domain_info) | |
2124 | + task->ccs_domain_info = &ccs_kernel_domain; | |
2125 | + return task->ccs_domain_info; | |
2126 | +} | |
2127 | + | |
2128 | +/** | |
2129 | + * ccs_task_flags - Get flags for specified thread. | |
2130 | + * | |
2131 | + * @task: Pointer to "struct task_struct". | |
2132 | + * | |
2133 | + * Returns flags for specified thread. | |
2134 | + */ | |
2135 | +static inline u32 ccs_task_flags(struct task_struct *task) | |
2136 | +{ | |
2137 | + return ccs_find_task_security(task)->ccs_flags; | |
2138 | +} | |
2139 | + | |
2140 | +/** | |
2141 | + * ccs_current_flags - Get flags for current thread. | |
2142 | + * | |
2143 | + * Returns flags for current thread. | |
2144 | + */ | |
2145 | +static inline u32 ccs_current_flags(void) | |
2146 | +{ | |
2147 | + return ccs_find_task_security(current)->ccs_flags; | |
2148 | +} | |
2149 | + | |
2150 | +#endif | |
2151 | + | |
2152 | +/** | |
2153 | + * ccs_current_namespace - Get "struct ccs_policy_namespace" for current thread. | |
2154 | + * | |
2155 | + * Returns pointer to "struct ccs_policy_namespace" for current thread. | |
2156 | + */ | |
2157 | +static inline struct ccs_policy_namespace *ccs_current_namespace(void) | |
2158 | +{ | |
2159 | + return ccs_current_domain()->ns; | |
2160 | +} | |
2161 | + | |
2162 | +#endif |
@@ -0,0 +1,5033 @@ | ||
1 | +/* | |
2 | + * security/ccsecurity/permission.c | |
3 | + * | |
4 | + * Copyright (C) 2005-2012 NTT DATA CORPORATION | |
5 | + * | |
6 | + * Version: 1.8.5 2015/11/11 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | + | |
11 | +/***** SECTION1: Constants definition *****/ | |
12 | + | |
13 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32) | |
14 | + | |
15 | +/* | |
16 | + * may_open() receives open flags modified by open_to_namei_flags() until | |
17 | + * 2.6.32. We stop here in case some distributions backported ACC_MODE changes, | |
18 | + * for we can't determine whether may_open() receives open flags modified by | |
19 | + * open_to_namei_flags() or not. | |
20 | + */ | |
21 | +#ifdef ACC_MODE | |
22 | +#error ACC_MODE already defined. | |
23 | +#endif | |
24 | +#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) | |
25 | + | |
26 | +#if defined(RHEL_MAJOR) && RHEL_MAJOR == 6 | |
27 | +/* RHEL6 passes unmodified flags since 2.6.32-71.14.1.el6 . */ | |
28 | +#undef ACC_MODE | |
29 | +#define ACC_MODE(x) ("\004\002\006"[(x)&O_ACCMODE]) | |
30 | +#endif | |
31 | + | |
32 | +#endif | |
33 | + | |
34 | +/* String table for special mount operations. */ | |
35 | +static const char * const ccs_mounts[CCS_MAX_SPECIAL_MOUNT] = { | |
36 | + [CCS_MOUNT_BIND] = "--bind", | |
37 | + [CCS_MOUNT_MOVE] = "--move", | |
38 | + [CCS_MOUNT_REMOUNT] = "--remount", | |
39 | + [CCS_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable", | |
40 | + [CCS_MOUNT_MAKE_PRIVATE] = "--make-private", | |
41 | + [CCS_MOUNT_MAKE_SLAVE] = "--make-slave", | |
42 | + [CCS_MOUNT_MAKE_SHARED] = "--make-shared", | |
43 | +}; | |
44 | + | |
45 | +/* Mapping table from "enum ccs_path_acl_index" to "enum ccs_mac_index". */ | |
46 | +static const u8 ccs_p2mac[CCS_MAX_PATH_OPERATION] = { | |
47 | + [CCS_TYPE_EXECUTE] = CCS_MAC_FILE_EXECUTE, | |
48 | + [CCS_TYPE_READ] = CCS_MAC_FILE_OPEN, | |
49 | + [CCS_TYPE_WRITE] = CCS_MAC_FILE_OPEN, | |
50 | + [CCS_TYPE_APPEND] = CCS_MAC_FILE_OPEN, | |
51 | + [CCS_TYPE_UNLINK] = CCS_MAC_FILE_UNLINK, | |
52 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
53 | + [CCS_TYPE_GETATTR] = CCS_MAC_FILE_GETATTR, | |
54 | +#endif | |
55 | + [CCS_TYPE_RMDIR] = CCS_MAC_FILE_RMDIR, | |
56 | + [CCS_TYPE_TRUNCATE] = CCS_MAC_FILE_TRUNCATE, | |
57 | + [CCS_TYPE_SYMLINK] = CCS_MAC_FILE_SYMLINK, | |
58 | + [CCS_TYPE_CHROOT] = CCS_MAC_FILE_CHROOT, | |
59 | + [CCS_TYPE_UMOUNT] = CCS_MAC_FILE_UMOUNT, | |
60 | +}; | |
61 | + | |
62 | +/* Mapping table from "enum ccs_mkdev_acl_index" to "enum ccs_mac_index". */ | |
63 | +const u8 ccs_pnnn2mac[CCS_MAX_MKDEV_OPERATION] = { | |
64 | + [CCS_TYPE_MKBLOCK] = CCS_MAC_FILE_MKBLOCK, | |
65 | + [CCS_TYPE_MKCHAR] = CCS_MAC_FILE_MKCHAR, | |
66 | +}; | |
67 | + | |
68 | +/* Mapping table from "enum ccs_path2_acl_index" to "enum ccs_mac_index". */ | |
69 | +const u8 ccs_pp2mac[CCS_MAX_PATH2_OPERATION] = { | |
70 | + [CCS_TYPE_LINK] = CCS_MAC_FILE_LINK, | |
71 | + [CCS_TYPE_RENAME] = CCS_MAC_FILE_RENAME, | |
72 | + [CCS_TYPE_PIVOT_ROOT] = CCS_MAC_FILE_PIVOT_ROOT, | |
73 | +}; | |
74 | + | |
75 | +/* | |
76 | + * Mapping table from "enum ccs_path_number_acl_index" to "enum ccs_mac_index". | |
77 | + */ | |
78 | +const u8 ccs_pn2mac[CCS_MAX_PATH_NUMBER_OPERATION] = { | |
79 | + [CCS_TYPE_CREATE] = CCS_MAC_FILE_CREATE, | |
80 | + [CCS_TYPE_MKDIR] = CCS_MAC_FILE_MKDIR, | |
81 | + [CCS_TYPE_MKFIFO] = CCS_MAC_FILE_MKFIFO, | |
82 | + [CCS_TYPE_MKSOCK] = CCS_MAC_FILE_MKSOCK, | |
83 | + [CCS_TYPE_IOCTL] = CCS_MAC_FILE_IOCTL, | |
84 | + [CCS_TYPE_CHMOD] = CCS_MAC_FILE_CHMOD, | |
85 | + [CCS_TYPE_CHOWN] = CCS_MAC_FILE_CHOWN, | |
86 | + [CCS_TYPE_CHGRP] = CCS_MAC_FILE_CHGRP, | |
87 | +}; | |
88 | + | |
89 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
90 | + | |
91 | +/* | |
92 | + * Mapping table from "enum ccs_network_acl_index" to "enum ccs_mac_index" for | |
93 | + * inet domain socket. | |
94 | + */ | |
95 | +static const u8 ccs_inet2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = { | |
96 | + [SOCK_STREAM] = { | |
97 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_INET_STREAM_BIND, | |
98 | + [CCS_NETWORK_LISTEN] = CCS_MAC_NETWORK_INET_STREAM_LISTEN, | |
99 | + [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_INET_STREAM_CONNECT, | |
100 | + [CCS_NETWORK_ACCEPT] = CCS_MAC_NETWORK_INET_STREAM_ACCEPT, | |
101 | + }, | |
102 | + [SOCK_DGRAM] = { | |
103 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_INET_DGRAM_BIND, | |
104 | + [CCS_NETWORK_SEND] = CCS_MAC_NETWORK_INET_DGRAM_SEND, | |
105 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
106 | + [CCS_NETWORK_RECV] = CCS_MAC_NETWORK_INET_DGRAM_RECV, | |
107 | +#endif | |
108 | + }, | |
109 | + [SOCK_RAW] = { | |
110 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_INET_RAW_BIND, | |
111 | + [CCS_NETWORK_SEND] = CCS_MAC_NETWORK_INET_RAW_SEND, | |
112 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
113 | + [CCS_NETWORK_RECV] = CCS_MAC_NETWORK_INET_RAW_RECV, | |
114 | +#endif | |
115 | + }, | |
116 | +}; | |
117 | + | |
118 | +/* | |
119 | + * Mapping table from "enum ccs_network_acl_index" to "enum ccs_mac_index" for | |
120 | + * unix domain socket. | |
121 | + */ | |
122 | +static const u8 ccs_unix2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = { | |
123 | + [SOCK_STREAM] = { | |
124 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_UNIX_STREAM_BIND, | |
125 | + [CCS_NETWORK_LISTEN] = CCS_MAC_NETWORK_UNIX_STREAM_LISTEN, | |
126 | + [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_STREAM_CONNECT, | |
127 | + [CCS_NETWORK_ACCEPT] = CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT, | |
128 | + }, | |
129 | + [SOCK_DGRAM] = { | |
130 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_UNIX_DGRAM_BIND, | |
131 | + [CCS_NETWORK_SEND] = CCS_MAC_NETWORK_UNIX_DGRAM_SEND, | |
132 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
133 | + [CCS_NETWORK_RECV] = CCS_MAC_NETWORK_UNIX_DGRAM_RECV, | |
134 | +#endif | |
135 | + }, | |
136 | + [SOCK_SEQPACKET] = { | |
137 | + [CCS_NETWORK_BIND] = CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND, | |
138 | + [CCS_NETWORK_LISTEN] = CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN, | |
139 | + [CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT, | |
140 | + [CCS_NETWORK_ACCEPT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT, | |
141 | + }, | |
142 | +}; | |
143 | + | |
144 | +#endif | |
145 | + | |
146 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
147 | + | |
148 | +/* | |
149 | + * Mapping table from "enum ccs_capability_acl_index" to "enum ccs_mac_index". | |
150 | + */ | |
151 | +const u8 ccs_c2mac[CCS_MAX_CAPABILITY_INDEX] = { | |
152 | + [CCS_USE_ROUTE_SOCKET] = CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET, | |
153 | + [CCS_USE_PACKET_SOCKET] = CCS_MAC_CAPABILITY_USE_PACKET_SOCKET, | |
154 | + [CCS_SYS_REBOOT] = CCS_MAC_CAPABILITY_SYS_REBOOT, | |
155 | + [CCS_SYS_VHANGUP] = CCS_MAC_CAPABILITY_SYS_VHANGUP, | |
156 | + [CCS_SYS_SETTIME] = CCS_MAC_CAPABILITY_SYS_SETTIME, | |
157 | + [CCS_SYS_NICE] = CCS_MAC_CAPABILITY_SYS_NICE, | |
158 | + [CCS_SYS_SETHOSTNAME] = CCS_MAC_CAPABILITY_SYS_SETHOSTNAME, | |
159 | + [CCS_USE_KERNEL_MODULE] = CCS_MAC_CAPABILITY_USE_KERNEL_MODULE, | |
160 | + [CCS_SYS_KEXEC_LOAD] = CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD, | |
161 | + [CCS_SYS_PTRACE] = CCS_MAC_CAPABILITY_SYS_PTRACE, | |
162 | +}; | |
163 | + | |
164 | +#endif | |
165 | + | |
166 | +/***** SECTION2: Structure definition *****/ | |
167 | + | |
168 | +/* Structure for holding inet domain socket's address. */ | |
169 | +struct ccs_inet_addr_info { | |
170 | + u16 port; /* In network byte order. */ | |
171 | + const u32 *address; /* In network byte order. */ | |
172 | + bool is_ipv6; | |
173 | +}; | |
174 | + | |
175 | +/* Structure for holding unix domain socket's address. */ | |
176 | +struct ccs_unix_addr_info { | |
177 | + u8 *addr; /* This may not be '\0' terminated string. */ | |
178 | + unsigned int addr_len; | |
179 | +}; | |
180 | + | |
181 | +/* Structure for holding socket address. */ | |
182 | +struct ccs_addr_info { | |
183 | + u8 protocol; | |
184 | + u8 operation; | |
185 | + struct ccs_inet_addr_info inet; | |
186 | + struct ccs_unix_addr_info unix0; | |
187 | +}; | |
188 | + | |
189 | +/***** SECTION3: Prototype definition section *****/ | |
190 | + | |
191 | +bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, | |
192 | + struct ccs_page_dump *dump); | |
193 | +void ccs_get_attributes(struct ccs_obj_info *obj); | |
194 | + | |
195 | +static bool ccs_alphabet_char(const char c); | |
196 | +static bool ccs_argv(const unsigned int index, const char *arg_ptr, | |
197 | + const int argc, const struct ccs_argv *argv, u8 *checked); | |
198 | +static bool ccs_byte_range(const char *str); | |
199 | +static bool ccs_check_entry(struct ccs_request_info *r, | |
200 | + struct ccs_acl_info *ptr); | |
201 | +static bool ccs_check_mkdev_acl(struct ccs_request_info *r, | |
202 | + const struct ccs_acl_info *ptr); | |
203 | +static bool ccs_check_mount_acl(struct ccs_request_info *r, | |
204 | + const struct ccs_acl_info *ptr); | |
205 | +static bool ccs_check_path2_acl(struct ccs_request_info *r, | |
206 | + const struct ccs_acl_info *ptr); | |
207 | +static bool ccs_check_path_acl(struct ccs_request_info *r, | |
208 | + const struct ccs_acl_info *ptr); | |
209 | +static bool ccs_check_path_number_acl(struct ccs_request_info *r, | |
210 | + const struct ccs_acl_info *ptr); | |
211 | +static bool ccs_compare_number_union(const unsigned long value, | |
212 | + const struct ccs_number_union *ptr); | |
213 | +static bool ccs_condition(struct ccs_request_info *r, | |
214 | + const struct ccs_condition *cond); | |
215 | +static bool ccs_decimal(const char c); | |
216 | +static bool ccs_envp(const char *env_name, const char *env_value, | |
217 | + const int envc, const struct ccs_envp *envp, u8 *checked); | |
218 | +static bool ccs_file_matches_pattern(const char *filename, | |
219 | + const char *filename_end, | |
220 | + const char *pattern, | |
221 | + const char *pattern_end); | |
222 | +static bool ccs_file_matches_pattern2(const char *filename, | |
223 | + const char *filename_end, | |
224 | + const char *pattern, | |
225 | + const char *pattern_end); | |
226 | +static bool ccs_get_realpath(struct ccs_path_info *buf, struct path *path); | |
227 | +static bool ccs_hexadecimal(const char c); | |
228 | +static bool ccs_number_matches_group(const unsigned long min, | |
229 | + const unsigned long max, | |
230 | + const struct ccs_group *group); | |
231 | +static bool ccs_path_matches_pattern(const struct ccs_path_info *filename, | |
232 | + const struct ccs_path_info *pattern); | |
233 | +static bool ccs_path_matches_pattern2(const char *f, const char *p); | |
234 | +static bool ccs_scan_bprm(struct ccs_execve *ee, const u16 argc, | |
235 | + const struct ccs_argv *argv, const u16 envc, | |
236 | + const struct ccs_envp *envp); | |
237 | +static bool ccs_scan_exec_realpath(struct file *file, | |
238 | + const struct ccs_name_union *ptr, | |
239 | + const bool match); | |
240 | +static bool ccs_scan_transition(const struct list_head *list, | |
241 | + const struct ccs_path_info *domainname, | |
242 | + const struct ccs_path_info *program, | |
243 | + const char *last_name, | |
244 | + const enum ccs_transition_type type); | |
245 | +static const char *ccs_last_word(const char *name); | |
246 | +static const struct ccs_path_info *ccs_compare_name_union | |
247 | +(const struct ccs_path_info *name, const struct ccs_name_union *ptr); | |
248 | +static const struct ccs_path_info *ccs_path_matches_group | |
249 | +(const struct ccs_path_info *pathname, const struct ccs_group *group); | |
250 | +static enum ccs_transition_type ccs_transition_type | |
251 | +(const struct ccs_policy_namespace *ns, const struct ccs_path_info *domainname, | |
252 | + const struct ccs_path_info *program); | |
253 | +static int __ccs_chmod_permission(struct dentry *dentry, | |
254 | + struct vfsmount *vfsmnt, mode_t mode); | |
255 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
256 | +static int __ccs_chown_permission(struct dentry *dentry, | |
257 | + struct vfsmount *vfsmnt, kuid_t user, | |
258 | + kgid_t group); | |
259 | +#else | |
260 | +static int __ccs_chown_permission(struct dentry *dentry, | |
261 | + struct vfsmount *vfsmnt, uid_t user, | |
262 | + gid_t group); | |
263 | +#endif | |
264 | +static int __ccs_chroot_permission(const struct path *path); | |
265 | +static int __ccs_fcntl_permission(struct file *file, unsigned int cmd, | |
266 | + unsigned long arg); | |
267 | +static int __ccs_ioctl_permission(struct file *filp, unsigned int cmd, | |
268 | + unsigned long arg); | |
269 | +static int __ccs_link_permission(struct dentry *old_dentry, | |
270 | + struct dentry *new_dentry, | |
271 | + struct vfsmount *mnt); | |
272 | +static int __ccs_mkdir_permission(struct dentry *dentry, struct vfsmount *mnt, | |
273 | + unsigned int mode); | |
274 | +static int __ccs_mknod_permission(struct dentry *dentry, struct vfsmount *mnt, | |
275 | + const unsigned int mode, unsigned int dev); | |
276 | +static int __ccs_mount_permission(const char *dev_name, | |
277 | + const struct path *path, const char *type, | |
278 | + unsigned long flags, void *data_page); | |
279 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) | |
280 | +static int __ccs_open_exec_permission(struct dentry *dentry, | |
281 | + struct vfsmount *mnt); | |
282 | +#endif | |
283 | +static int __ccs_open_permission(struct dentry *dentry, struct vfsmount *mnt, | |
284 | + const int flag); | |
285 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33 |
Part of diff was cut off due to size limit. Use your local client to view the full diff.