1.0.37
@@ -0,0 +1,311 @@ | ||
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. | |
308 | + | |
309 | +Version 1.0.37 2017/09/17 Minor update release. | |
310 | + | |
311 | + Use smp_rmb() when waiting for hook readiness. |
@@ -0,0 +1,1587 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | + | |
14 | +static int __ccs_alloc_task_security(const struct task_struct *task); | |
15 | +static void __ccs_free_task_security(const struct task_struct *task); | |
16 | + | |
17 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
18 | +static struct ccs_security ccs_oom_security = { | |
19 | + .ccs_domain_info = &ccs_kernel_domain | |
20 | +}; | |
21 | + | |
22 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
23 | +static struct ccs_security ccs_default_security = { | |
24 | + .ccs_domain_info = &ccs_kernel_domain | |
25 | +}; | |
26 | + | |
27 | +/* List of "struct ccs_security". */ | |
28 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
29 | +/* Lock for protecting ccs_task_security_list[]. */ | |
30 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
31 | + | |
32 | +/* Dummy marker for calling security_bprm_free(). */ | |
33 | +static const unsigned long ccs_bprm_security; | |
34 | + | |
35 | +/* For exporting variables and functions. */ | |
36 | +struct ccsecurity_exports ccsecurity_exports; | |
37 | +/* Members are updated by loadable kernel module. */ | |
38 | +struct ccsecurity_operations ccsecurity_ops; | |
39 | + | |
40 | +/* Function pointers originally registered by register_security(). */ | |
41 | +static struct security_operations original_security_ops /* = *security_ops; */; | |
42 | + | |
43 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
44 | + | |
45 | +/** | |
46 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
47 | + * | |
48 | + * @count: Count to increment or decrement. | |
49 | + * | |
50 | + * Returns updated counter. | |
51 | + */ | |
52 | +static unsigned int ccs_update_ee_counter(int count) | |
53 | +{ | |
54 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10) || defined(atomic_add_return) | |
55 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
56 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
57 | + return atomic_add_return(count, &ccs_ee_counter); | |
58 | +#else | |
59 | + static DEFINE_SPINLOCK(ccs_ee_lock); | |
60 | + static unsigned int ccs_ee_counter; | |
61 | + unsigned long flags; | |
62 | + spin_lock_irqsave(&ccs_ee_lock, flags); | |
63 | + ccs_ee_counter += count; | |
64 | + count = ccs_ee_counter; | |
65 | + spin_unlock_irqrestore(&ccs_ee_lock, flags); | |
66 | + return count; | |
67 | +#endif | |
68 | +} | |
69 | + | |
70 | +/** | |
71 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
72 | + * | |
73 | + * @ee: Pointer to "struct ccs_execve". | |
74 | + * | |
75 | + * Returns nothing. | |
76 | + */ | |
77 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
78 | +{ | |
79 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
80 | + current->pid, ccs_update_ee_counter(1) - 1); | |
81 | +} | |
82 | + | |
83 | +/** | |
84 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
85 | + * | |
86 | + * @ee: Pointer to "struct ccs_execve". | |
87 | + * @task: True if released by current task, false otherwise. | |
88 | + * | |
89 | + * Returns nothing. | |
90 | + */ | |
91 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
92 | + const bool is_current) | |
93 | +{ | |
94 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
95 | + if (is_current) | |
96 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
97 | + ee, current->pid, tmp); | |
98 | + else | |
99 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
100 | + ee, tmp); | |
101 | +} | |
102 | + | |
103 | +#endif | |
104 | + | |
105 | +#if !defined(CONFIG_AKARI_DEBUG) | |
106 | +#define ccs_debug_trace(pos) do { } while (0) | |
107 | +#else | |
108 | +#define ccs_debug_trace(pos) \ | |
109 | + do { \ | |
110 | + static bool done; \ | |
111 | + if (!done) { \ | |
112 | + printk(KERN_INFO \ | |
113 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
114 | + done = true; \ | |
115 | + } \ | |
116 | + } while (0) | |
117 | +#endif | |
118 | + | |
119 | +/** | |
120 | + * ccs_clear_execve - Release memory used by do_execve(). | |
121 | + * | |
122 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
123 | + * @security: Pointer to "struct ccs_security". | |
124 | + * | |
125 | + * Returns nothing. | |
126 | + */ | |
127 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
128 | +{ | |
129 | + struct ccs_execve *ee; | |
130 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
131 | + return; | |
132 | + ee = security->ee; | |
133 | + security->ee = NULL; | |
134 | + if (!ee) | |
135 | + return; | |
136 | + ccs_finish_execve(ret, ee); | |
137 | +} | |
138 | + | |
139 | +/** | |
140 | + * ccs_task_alloc_security - Allocate memory for new tasks. | |
141 | + * | |
142 | + * @p: Pointer to "struct task_struct". | |
143 | + * | |
144 | + * Returns 0 on success, negative value otherwise. | |
145 | + */ | |
146 | +static int ccs_task_alloc_security(struct task_struct *p) | |
147 | +{ | |
148 | + int rc = __ccs_alloc_task_security(p); | |
149 | + if (rc) | |
150 | + return rc; | |
151 | + while (!original_security_ops.task_alloc_security) | |
152 | + smp_rmb(); | |
153 | + rc = original_security_ops.task_alloc_security(p); | |
154 | + if (rc) | |
155 | + __ccs_free_task_security(p); | |
156 | + return rc; | |
157 | +} | |
158 | + | |
159 | +/** | |
160 | + * ccs_task_free_security - Release memory for "struct task_struct". | |
161 | + * | |
162 | + * @p: Pointer to "struct task_struct". | |
163 | + * | |
164 | + * Returns nothing. | |
165 | + */ | |
166 | +static void ccs_task_free_security(struct task_struct *p) | |
167 | +{ | |
168 | + while (!original_security_ops.task_free_security) | |
169 | + smp_rmb(); | |
170 | + original_security_ops.task_free_security(p); | |
171 | + __ccs_free_task_security(p); | |
172 | +} | |
173 | + | |
174 | +/** | |
175 | + * ccs_bprm_alloc_security - Allocate memory for "struct linux_binprm". | |
176 | + * | |
177 | + * @bprm: Pointer to "struct linux_binprm". | |
178 | + * | |
179 | + * Returns 0 on success, negative value otherwise. | |
180 | + */ | |
181 | +static int ccs_bprm_alloc_security(struct linux_binprm *bprm) | |
182 | +{ | |
183 | + int rc; | |
184 | + while (!original_security_ops.bprm_alloc_security) | |
185 | + smp_rmb(); | |
186 | + rc = original_security_ops.bprm_alloc_security(bprm); | |
187 | + if (bprm->security || rc) | |
188 | + return rc; | |
189 | + /* | |
190 | + * Update bprm->security to &ccs_bprm_security so that | |
191 | + * security_bprm_free() is called even if do_execve() failed at | |
192 | + * search_binary_handler() without allocating memory at | |
193 | + * security_bprm_alloc(). This trick assumes that active LSM module | |
194 | + * does not access bprm->security if that module did not allocate | |
195 | + * memory at security_bprm_alloc(). | |
196 | + */ | |
197 | + bprm->security = (void *) &ccs_bprm_security; | |
198 | + return 0; | |
199 | +} | |
200 | + | |
201 | +/** | |
202 | + * ccs_bprm_free_security - Release memory for "struct linux_binprm". | |
203 | + * | |
204 | + * @bprm: Pointer to "struct linux_binprm". | |
205 | + * | |
206 | + * Returns nothing. | |
207 | + */ | |
208 | +static void ccs_bprm_free_security(struct linux_binprm *bprm) | |
209 | +{ | |
210 | + /* | |
211 | + * If do_execve() succeeded, bprm->security will be updated to NULL at | |
212 | + * security_bprm_compute_creds()/security_bprm_apply_creds() if | |
213 | + * bprm->security was set to &ccs_bprm_security at | |
214 | + * security_bprm_alloc(). | |
215 | + * | |
216 | + * If do_execve() failed, bprm->security remains at &ccs_bprm_security | |
217 | + * if bprm->security was set to &ccs_bprm_security at | |
218 | + * security_bprm_alloc(). | |
219 | + * | |
220 | + * And do_execve() does not call security_bprm_free() if do_execve() | |
221 | + * failed and bprm->security == NULL. Therefore, do not call | |
222 | + * original_security_ops.bprm_free_security() if bprm->security remains | |
223 | + * at &ccs_bprm_security . | |
224 | + */ | |
225 | + if (bprm->security != &ccs_bprm_security) { | |
226 | + while (!original_security_ops.bprm_free_security) | |
227 | + smp_rmb(); | |
228 | + original_security_ops.bprm_free_security(bprm); | |
229 | + } | |
230 | + /* | |
231 | + * If do_execve() succeeded, | |
232 | + * ccs_clear_execve(0, ccs_current_security()); | |
233 | + * is called before calling below one. | |
234 | + * Thus, below call becomes no-op if do_execve() succeeded. | |
235 | + */ | |
236 | + ccs_clear_execve(-1, ccs_current_security()); | |
237 | +} | |
238 | + | |
239 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
240 | + | |
241 | +/** | |
242 | + * ccs_bprm_compute_creds - A hook which is called when do_execve() succeeded. | |
243 | + * | |
244 | + * @bprm: Pointer to "struct linux_binprm". | |
245 | + * | |
246 | + * Returns nothing. | |
247 | + */ | |
248 | +static void ccs_bprm_compute_creds(struct linux_binprm *bprm) | |
249 | +{ | |
250 | + if (bprm->security == &ccs_bprm_security) | |
251 | + bprm->security = NULL; | |
252 | + while (!original_security_ops.bprm_compute_creds) | |
253 | + smp_rmb(); | |
254 | + original_security_ops.bprm_compute_creds(bprm); | |
255 | + ccs_clear_execve(0, ccs_current_security()); | |
256 | +} | |
257 | + | |
258 | +#else | |
259 | + | |
260 | +/** | |
261 | + * ccs_bprm_apply_creds - A hook which is called when do_execve() succeeded. | |
262 | + * | |
263 | + * @bprm: Pointer to "struct linux_binprm". | |
264 | + * @unsafe: Unsafe flag. | |
265 | + * | |
266 | + * Returns nothing. | |
267 | + */ | |
268 | +static void ccs_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) | |
269 | +{ | |
270 | + if (bprm->security == &ccs_bprm_security) | |
271 | + bprm->security = NULL; | |
272 | + while (!original_security_ops.bprm_apply_creds) | |
273 | + smp_rmb(); | |
274 | + original_security_ops.bprm_apply_creds(bprm, unsafe); | |
275 | + ccs_clear_execve(0, ccs_current_security()); | |
276 | +} | |
277 | + | |
278 | +#endif | |
279 | + | |
280 | +/** | |
281 | + * ccs_bprm_check_security - Check permission for execve(). | |
282 | + * | |
283 | + * @bprm: Pointer to "struct linux_binprm". | |
284 | + * | |
285 | + * Returns 0 on success, negative value otherwise. | |
286 | + */ | |
287 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
288 | +{ | |
289 | + struct ccs_security *security = ccs_current_security(); | |
290 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
291 | + return -ENOMEM; | |
292 | + if (!security->ee) { | |
293 | + int rc; | |
294 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
295 | + if (!ccs_policy_loaded) | |
296 | + ccs_load_policy(bprm->filename); | |
297 | +#endif | |
298 | + rc = ccs_start_execve(bprm, &security->ee); | |
299 | + if (rc) | |
300 | + return rc; | |
301 | + } | |
302 | + while (!original_security_ops.bprm_check_security) | |
303 | + smp_rmb(); | |
304 | + return original_security_ops.bprm_check_security(bprm); | |
305 | +} | |
306 | + | |
307 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
308 | + | |
309 | +/** | |
310 | + * ccs_open - Check permission for open(). | |
311 | + * | |
312 | + * @f: Pointer to "struct file". | |
313 | + * | |
314 | + * Returns 0 on success, negative value otherwise. | |
315 | + */ | |
316 | +static int ccs_open(struct file *f) | |
317 | +{ | |
318 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
319 | + f->f_flags + 1); | |
320 | +} | |
321 | + | |
322 | +#endif | |
323 | + | |
324 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
325 | + | |
326 | +/** | |
327 | + * ccs_dentry_open - Check permission for open(). | |
328 | + * | |
329 | + * @f: Pointer to "struct file". | |
330 | + * | |
331 | + * Returns 0 on success, negative value otherwise. | |
332 | + */ | |
333 | +static int ccs_dentry_open(struct file *f) | |
334 | +{ | |
335 | + int rc = ccs_open(f); | |
336 | + if (rc) | |
337 | + return rc; | |
338 | + while (!original_security_ops.dentry_open) | |
339 | + smp_rmb(); | |
340 | + return original_security_ops.dentry_open(f); | |
341 | +} | |
342 | + | |
343 | +#else | |
344 | + | |
345 | +/** | |
346 | + * ccs_open - Check permission for open(). | |
347 | + * | |
348 | + * @inode: Pointer to "struct inode". | |
349 | + * @mask: Open mode. | |
350 | + * @nd: Pointer to "struct nameidata". | |
351 | + * | |
352 | + * Returns 0 on success, negative value otherwise. | |
353 | + */ | |
354 | +static int ccs_open(struct inode *inode, int mask, struct nameidata *nd) | |
355 | +{ | |
356 | + int flags; | |
357 | + if (!nd || !nd->dentry) | |
358 | + return 0; | |
359 | + /* open_exec() passes MAY_EXEC . */ | |
360 | + if (mask == MAY_EXEC && inode && S_ISREG(inode->i_mode) && | |
361 | + (ccs_current_flags() & CCS_TASK_IS_IN_EXECVE)) | |
362 | + mask = MAY_READ; | |
363 | + /* | |
364 | + * This flags value is passed to ACC_MODE(). | |
365 | + * ccs_open_permission() for older versions uses old ACC_MODE(). | |
366 | + */ | |
367 | + switch (mask & (MAY_READ | MAY_WRITE)) { | |
368 | + case MAY_READ: | |
369 | + flags = 01; | |
370 | + break; | |
371 | + case MAY_WRITE: | |
372 | + flags = 02; | |
373 | + break; | |
374 | + case MAY_READ | MAY_WRITE: | |
375 | + flags = 03; | |
376 | + break; | |
377 | + default: | |
378 | + return 0; | |
379 | + } | |
380 | + return ccs_open_permission(nd->dentry, nd->mnt, flags); | |
381 | +} | |
382 | + | |
383 | +/** | |
384 | + * ccs_inode_permission - Check permission for open(). | |
385 | + * | |
386 | + * @inode: Pointer to "struct inode". | |
387 | + * @mask: Open mode. | |
388 | + * @nd: Pointer to "struct nameidata". | |
389 | + * | |
390 | + * Returns 0 on success, negative value otherwise. | |
391 | + * | |
392 | + * Note that this hook is called from permission(), and may not be called for | |
393 | + * open(). Maybe it is better to use security_file_permission(). | |
394 | + */ | |
395 | +static int ccs_inode_permission(struct inode *inode, int mask, | |
396 | + struct nameidata *nd) | |
397 | +{ | |
398 | + int rc = ccs_open(inode, mask, nd); | |
399 | + if (rc) | |
400 | + return rc; | |
401 | + while (!original_security_ops.inode_permission) | |
402 | + smp_rmb(); | |
403 | + return original_security_ops.inode_permission(inode, mask, nd); | |
404 | +} | |
405 | + | |
406 | +#endif | |
407 | + | |
408 | +/** | |
409 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
410 | + * | |
411 | + * @dentry: Pointer to "struct dentry". | |
412 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
413 | + * @attr: Pointer to "struct iattr". | |
414 | + * | |
415 | + * Returns 0 on success, negative value otherwise. | |
416 | + */ | |
417 | +static int ccs_inode_setattr(struct dentry *dentry, struct vfsmount *mnt, | |
418 | + struct iattr *attr) | |
419 | +{ | |
420 | + int rc = 0; | |
421 | + if (attr->ia_valid & ATTR_UID) | |
422 | + rc = ccs_chown_permission(dentry, mnt, attr->ia_uid, -1); | |
423 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
424 | + rc = ccs_chown_permission(dentry, mnt, -1, attr->ia_gid); | |
425 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
426 | + rc = ccs_chmod_permission(dentry, mnt, attr->ia_mode); | |
427 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
428 | + rc = ccs_truncate_permission(dentry, mnt); | |
429 | + if (rc) | |
430 | + return rc; | |
431 | + while (!original_security_ops.inode_setattr) | |
432 | + smp_rmb(); | |
433 | + return original_security_ops.inode_setattr(dentry, mnt, attr); | |
434 | +} | |
435 | + | |
436 | +/** | |
437 | + * ccs_inode_getattr - Check permission for stat(). | |
438 | + * | |
439 | + * @mnt: Pointer to "struct vfsmount". | |
440 | + * @dentry: Pointer to "struct dentry". | |
441 | + * | |
442 | + * Returns 0 on success, negative value otherwise. | |
443 | + */ | |
444 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
445 | +{ | |
446 | + int rc = ccs_getattr_permission(mnt, dentry); | |
447 | + if (rc) | |
448 | + return rc; | |
449 | + while (!original_security_ops.inode_getattr) | |
450 | + smp_rmb(); | |
451 | + return original_security_ops.inode_getattr(mnt, dentry); | |
452 | +} | |
453 | + | |
454 | +/** | |
455 | + * ccs_inode_mknod - Check permission for mknod(). | |
456 | + * | |
457 | + * @dir: Pointer to "struct inode". | |
458 | + * @dentry: Pointer to "struct dentry". | |
459 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
460 | + * @mode: Create mode. | |
461 | + * @dev: Device major/minor number. | |
462 | + * | |
463 | + * Returns 0 on success, negative value otherwise. | |
464 | + */ | |
465 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
466 | + struct vfsmount *mnt, int mode, dev_t dev) | |
467 | +{ | |
468 | + int rc = ccs_mknod_permission(dentry, mnt, mode, dev); | |
469 | + if (rc) | |
470 | + return rc; | |
471 | + while (!original_security_ops.inode_mknod) | |
472 | + smp_rmb(); | |
473 | + return original_security_ops.inode_mknod(dir, dentry, mnt, mode, dev); | |
474 | +} | |
475 | + | |
476 | +/** | |
477 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
478 | + * | |
479 | + * @dir: Pointer to "struct inode". | |
480 | + * @dentry: Pointer to "struct dentry". | |
481 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
482 | + * @mode: Create mode. | |
483 | + * | |
484 | + * Returns 0 on success, negative value otherwise. | |
485 | + */ | |
486 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
487 | + struct vfsmount *mnt, int mode) | |
488 | +{ | |
489 | + int rc = ccs_mkdir_permission(dentry, mnt, mode); | |
490 | + if (rc) | |
491 | + return rc; | |
492 | + while (!original_security_ops.inode_mkdir) | |
493 | + smp_rmb(); | |
494 | + return original_security_ops.inode_mkdir(dir, dentry, mnt, mode); | |
495 | +} | |
496 | + | |
497 | +/** | |
498 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
499 | + * | |
500 | + * @dir: Pointer to "struct inode". | |
501 | + * @dentry: Pointer to "struct dentry". | |
502 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
503 | + * | |
504 | + * Returns 0 on success, negative value otherwise. | |
505 | + */ | |
506 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry, | |
507 | + struct vfsmount *mnt) | |
508 | +{ | |
509 | + int rc = ccs_rmdir_permission(dentry, mnt); | |
510 | + if (rc) | |
511 | + return rc; | |
512 | + while (!original_security_ops.inode_rmdir) | |
513 | + smp_rmb(); | |
514 | + return original_security_ops.inode_rmdir(dir, dentry, mnt); | |
515 | +} | |
516 | + | |
517 | +/** | |
518 | + * ccs_inode_unlink - Check permission for unlink(). | |
519 | + * | |
520 | + * @dir: Pointer to "struct inode". | |
521 | + * @dentry: Pointer to "struct dentry". | |
522 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
523 | + * | |
524 | + * Returns 0 on success, negative value otherwise. | |
525 | + */ | |
526 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry, | |
527 | + struct vfsmount *mnt) | |
528 | +{ | |
529 | + int rc = ccs_unlink_permission(dentry, mnt); | |
530 | + if (rc) | |
531 | + return rc; | |
532 | + while (!original_security_ops.inode_unlink) | |
533 | + smp_rmb(); | |
534 | + return original_security_ops.inode_unlink(dir, dentry, mnt); | |
535 | +} | |
536 | + | |
537 | +/** | |
538 | + * ccs_inode_symlink - Check permission for symlink(). | |
539 | + * | |
540 | + * @dir: Pointer to "struct inode". | |
541 | + * @dentry: Pointer to "struct dentry". | |
542 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
543 | + * @old_name: Content of symbolic link. | |
544 | + * | |
545 | + * Returns 0 on success, negative value otherwise. | |
546 | + */ | |
547 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
548 | + struct vfsmount *mnt, const char *old_name) | |
549 | +{ | |
550 | + int rc = ccs_symlink_permission(dentry, mnt, old_name); | |
551 | + if (rc) | |
552 | + return rc; | |
553 | + while (!original_security_ops.inode_symlink) | |
554 | + smp_rmb(); | |
555 | + return original_security_ops.inode_symlink(dir, dentry, mnt, old_name); | |
556 | +} | |
557 | + | |
558 | +/** | |
559 | + * ccs_inode_rename - Check permission for rename(). | |
560 | + * | |
561 | + * @old_dir: Pointer to "struct inode". | |
562 | + * @old_dentry: Pointer to "struct dentry". | |
563 | + * @old_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
564 | + * @new_dir: Pointer to "struct inode". | |
565 | + * @new_dentry: Pointer to "struct dentry". | |
566 | + * @new_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
567 | + * | |
568 | + * Returns 0 on success, negative value otherwise. | |
569 | + */ | |
570 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
571 | + struct vfsmount *old_mnt, struct inode *new_dir, | |
572 | + struct dentry *new_dentry, | |
573 | + struct vfsmount *new_mnt) | |
574 | +{ | |
575 | + int rc = ccs_rename_permission(old_dentry, new_dentry, new_mnt); | |
576 | + if (rc) | |
577 | + return rc; | |
578 | + while (!original_security_ops.inode_rename) | |
579 | + smp_rmb(); | |
580 | + return original_security_ops.inode_rename(old_dir, old_dentry, old_mnt, | |
581 | + new_dir, new_dentry, | |
582 | + new_mnt); | |
583 | +} | |
584 | + | |
585 | +/** | |
586 | + * ccs_inode_link - Check permission for link(). | |
587 | + * | |
588 | + * @old_dentry: Pointer to "struct dentry". | |
589 | + * @old_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
590 | + * @dir: Pointer to "struct inode". | |
591 | + * @new_dentry: Pointer to "struct dentry". | |
592 | + * @new_mnt: Pointer to "struct vfsmount". Maybe NULL. | |
593 | + * | |
594 | + * Returns 0 on success, negative value otherwise. | |
595 | + */ | |
596 | +static int ccs_inode_link(struct dentry *old_dentry, struct vfsmount *old_mnt, | |
597 | + struct inode *dir, struct dentry *new_dentry, | |
598 | + struct vfsmount *new_mnt) | |
599 | +{ | |
600 | + int rc = ccs_link_permission(old_dentry, new_dentry, new_mnt); | |
601 | + if (rc) | |
602 | + return rc; | |
603 | + while (!original_security_ops.inode_link) | |
604 | + smp_rmb(); | |
605 | + return original_security_ops.inode_link(old_dentry, old_mnt, dir, | |
606 | + new_dentry, new_mnt); | |
607 | +} | |
608 | + | |
609 | +/** | |
610 | + * ccs_inode_create - Check permission for creat(). | |
611 | + * | |
612 | + * @dir: Pointer to "struct inode". | |
613 | + * @dentry: Pointer to "struct dentry". | |
614 | + * @mnt: Pointer to "struct vfsmount". Maybe NULL. | |
615 | + * @mode: Create mode. | |
616 | + * | |
617 | + * Returns 0 on success, negative value otherwise. | |
618 | + */ | |
619 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
620 | + struct vfsmount *mnt, int mode) | |
621 | +{ | |
622 | + int rc = ccs_mknod_permission(dentry, mnt, mode, 0); | |
623 | + if (rc) | |
624 | + return rc; | |
625 | + while (!original_security_ops.inode_create) | |
626 | + smp_rmb(); | |
627 | + return original_security_ops.inode_create(dir, dentry, mnt, mode); | |
628 | +} | |
629 | + | |
630 | +#ifdef CONFIG_SECURITY_NETWORK | |
631 | + | |
632 | +#include <net/sock.h> | |
633 | + | |
634 | +/* Structure for remembering an accept()ed socket's status. */ | |
635 | +struct ccs_socket_tag { | |
636 | + struct list_head list; | |
637 | + struct inode *inode; | |
638 | + int status; | |
639 | + struct rcu_head rcu; | |
640 | +}; | |
641 | + | |
642 | +/* | |
643 | + * List for managing accept()ed sockets. | |
644 | + * Since we don't need to keep an accept()ed socket into this list after | |
645 | + * once the permission was granted, the number of entries in this list is | |
646 | + * likely small. Therefore, we don't use hash tables. | |
647 | + */ | |
648 | +static LIST_HEAD(ccs_accepted_socket_list); | |
649 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
650 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
651 | + | |
652 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
653 | + | |
654 | +/** | |
655 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
656 | + * | |
657 | + * @rcu: Pointer to "struct rcu_head". | |
658 | + * | |
659 | + * Returns nothing. | |
660 | + */ | |
661 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
662 | +{ | |
663 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
664 | + kfree(ptr); | |
665 | +} | |
666 | + | |
667 | +#else | |
668 | + | |
669 | +/** | |
670 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
671 | + * | |
672 | + * @arg: Pointer to "void". | |
673 | + * | |
674 | + * Returns nothing. | |
675 | + */ | |
676 | +static void ccs_socket_rcu_free(void *arg) | |
677 | +{ | |
678 | + struct ccs_socket_tag *ptr = arg; | |
679 | + kfree(ptr); | |
680 | +} | |
681 | + | |
682 | +#endif | |
683 | + | |
684 | +/** | |
685 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
686 | + * | |
687 | + * @inode: Pointer to "struct inode". | |
688 | + * @status: New status. | |
689 | + * | |
690 | + * Returns nothing. | |
691 | + * | |
692 | + * If @status == 0, memory for that socket will be released after RCU grace | |
693 | + * period. | |
694 | + */ | |
695 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
696 | +{ | |
697 | + struct ccs_socket_tag *ptr; | |
698 | + /* | |
699 | + * Protect whole section because multiple threads may call this | |
700 | + * function with same "sock" via ccs_validate_socket(). | |
701 | + */ | |
702 | + spin_lock(&ccs_accepted_socket_list_lock); | |
703 | + rcu_read_lock(); | |
704 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
705 | + if (ptr->inode != inode) | |
706 | + continue; | |
707 | + ptr->status = status; | |
708 | + if (status) | |
709 | + break; | |
710 | + list_del_rcu(&ptr->list); | |
711 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
712 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
713 | +#else | |
714 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free, ptr); | |
715 | +#endif | |
716 | + break; | |
717 | + } | |
718 | + rcu_read_unlock(); | |
719 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
720 | +} | |
721 | + | |
722 | +/** | |
723 | + * ccs_validate_socket - Check post accept() permission if needed. | |
724 | + * | |
725 | + * @sock: Pointer to "struct socket". | |
726 | + * | |
727 | + * Returns 0 on success, negative value otherwise. | |
728 | + */ | |
729 | +static int ccs_validate_socket(struct socket *sock) | |
730 | +{ | |
731 | + struct inode *inode = SOCK_INODE(sock); | |
732 | + struct ccs_socket_tag *ptr; | |
733 | + int ret = 0; | |
734 | + rcu_read_lock(); | |
735 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
736 | + if (ptr->inode != inode) | |
737 | + continue; | |
738 | + ret = ptr->status; | |
739 | + break; | |
740 | + } | |
741 | + rcu_read_unlock(); | |
742 | + if (ret <= 0) | |
743 | + /* | |
744 | + * This socket is not an accept()ed socket or this socket is | |
745 | + * an accept()ed socket and post accept() permission is done. | |
746 | + */ | |
747 | + return ret; | |
748 | + /* | |
749 | + * Check post accept() permission now. | |
750 | + * | |
751 | + * Strictly speaking, we need to pass both listen()ing socket and | |
752 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
753 | + * But since socket's family and type are same for both sockets, | |
754 | + * passing the accept()ed socket in place for the listen()ing socket | |
755 | + * will work. | |
756 | + */ | |
757 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
758 | + /* | |
759 | + * If permission was granted, we forget that this is an accept()ed | |
760 | + * socket. Otherwise, we remember that this socket needs to return | |
761 | + * error for subsequent socketcalls. | |
762 | + */ | |
763 | + ccs_update_socket_tag(inode, ret); | |
764 | + return ret; | |
765 | +} | |
766 | + | |
767 | +/** | |
768 | + * ccs_socket_accept - Check permission for accept(). | |
769 | + * | |
770 | + * @sock: Pointer to "struct socket". | |
771 | + * @newsock: Pointer to "struct socket". | |
772 | + * | |
773 | + * Returns 0 on success, negative value otherwise. | |
774 | + * | |
775 | + * This hook is used for setting up environment for doing post accept() | |
776 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
777 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
778 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
779 | + * in order to do post accept() permission check before returning to userspace. | |
780 | + * If we make the copy in security_socket_post_create(), it would be possible | |
781 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
782 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
783 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
784 | + * rather than between sock->ops->accept() and returning to userspace. | |
785 | + * This means that if a socket was close()d before calling some socket | |
786 | + * syscalls, post accept() permission check will not be done. | |
787 | + */ | |
788 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
789 | +{ | |
790 | + struct ccs_socket_tag *ptr; | |
791 | + int rc = ccs_validate_socket(sock); | |
792 | + if (rc < 0) | |
793 | + return rc; | |
794 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
795 | + if (!ptr) | |
796 | + return -ENOMEM; | |
797 | + while (!original_security_ops.socket_accept) | |
798 | + smp_rmb(); | |
799 | + rc = original_security_ops.socket_accept(sock, newsock); | |
800 | + if (rc) { | |
801 | + kfree(ptr); | |
802 | + return rc; | |
803 | + } | |
804 | + /* | |
805 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
806 | + * "newsock" as "an accept()ed socket but post accept() permission | |
807 | + * check is not done yet" by allocating memory using inode of the | |
808 | + * "newsock" as a search key. | |
809 | + */ | |
810 | + ptr->inode = SOCK_INODE(newsock); | |
811 | + ptr->status = 1; /* Check post accept() permission later. */ | |
812 | + spin_lock(&ccs_accepted_socket_list_lock); | |
813 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
814 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
815 | + return 0; | |
816 | +} | |
817 | + | |
818 | +/** | |
819 | + * ccs_socket_listen - Check permission for listen(). | |
820 | + * | |
821 | + * @sock: Pointer to "struct socket". | |
822 | + * @backlog: Backlog parameter. | |
823 | + * | |
824 | + * Returns 0 on success, negative value otherwise. | |
825 | + */ | |
826 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
827 | +{ | |
828 | + int rc = ccs_validate_socket(sock); | |
829 | + if (rc < 0) | |
830 | + return rc; | |
831 | + rc = ccs_socket_listen_permission(sock); | |
832 | + if (rc) | |
833 | + return rc; | |
834 | + while (!original_security_ops.socket_listen) | |
835 | + smp_rmb(); | |
836 | + return original_security_ops.socket_listen(sock, backlog); | |
837 | +} | |
838 | + | |
839 | +/** | |
840 | + * ccs_socket_connect - Check permission for connect(). | |
841 | + * | |
842 | + * @sock: Pointer to "struct socket". | |
843 | + * @addr: Pointer to "struct sockaddr". | |
844 | + * @addr_len: Size of @addr. | |
845 | + * | |
846 | + * Returns 0 on success, negative value otherwise. | |
847 | + */ | |
848 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
849 | + int addr_len) | |
850 | +{ | |
851 | + int rc = ccs_validate_socket(sock); | |
852 | + if (rc < 0) | |
853 | + return rc; | |
854 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
855 | + if (rc) | |
856 | + return rc; | |
857 | + while (!original_security_ops.socket_connect) | |
858 | + smp_rmb(); | |
859 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
860 | +} | |
861 | + | |
862 | +/** | |
863 | + * ccs_socket_bind - Check permission for bind(). | |
864 | + * | |
865 | + * @sock: Pointer to "struct socket". | |
866 | + * @addr: Pointer to "struct sockaddr". | |
867 | + * @addr_len: Size of @addr. | |
868 | + * | |
869 | + * Returns 0 on success, negative value otherwise. | |
870 | + */ | |
871 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
872 | + int addr_len) | |
873 | +{ | |
874 | + int rc = ccs_validate_socket(sock); | |
875 | + if (rc < 0) | |
876 | + return rc; | |
877 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
878 | + if (rc) | |
879 | + return rc; | |
880 | + while (!original_security_ops.socket_bind) | |
881 | + smp_rmb(); | |
882 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
883 | +} | |
884 | + | |
885 | +/** | |
886 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
887 | + * | |
888 | + * @sock: Pointer to "struct socket". | |
889 | + * @msg: Pointer to "struct msghdr". | |
890 | + * @size: Size of message. | |
891 | + * | |
892 | + * Returns 0 on success, negative value otherwise. | |
893 | + */ | |
894 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
895 | + int size) | |
896 | +{ | |
897 | + int rc = ccs_validate_socket(sock); | |
898 | + if (rc < 0) | |
899 | + return rc; | |
900 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
901 | + if (rc) | |
902 | + return rc; | |
903 | + while (!original_security_ops.socket_sendmsg) | |
904 | + smp_rmb(); | |
905 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
906 | +} | |
907 | + | |
908 | +/** | |
909 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
910 | + * | |
911 | + * @sock: Pointer to "struct socket". | |
912 | + * @msg: Pointer to "struct msghdr". | |
913 | + * @size: Size of message. | |
914 | + * @flags: Flags. | |
915 | + * | |
916 | + * Returns 0 on success, negative value otherwise. | |
917 | + */ | |
918 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
919 | + int size, int flags) | |
920 | +{ | |
921 | + int rc = ccs_validate_socket(sock); | |
922 | + if (rc < 0) | |
923 | + return rc; | |
924 | + while (!original_security_ops.socket_recvmsg) | |
925 | + smp_rmb(); | |
926 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
927 | +} | |
928 | + | |
929 | +/** | |
930 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
931 | + * | |
932 | + * @sock: Pointer to "struct socket". | |
933 | + * | |
934 | + * Returns 0 on success, negative value otherwise. | |
935 | + */ | |
936 | +static int ccs_socket_getsockname(struct socket *sock) | |
937 | +{ | |
938 | + int rc = ccs_validate_socket(sock); | |
939 | + if (rc < 0) | |
940 | + return rc; | |
941 | + while (!original_security_ops.socket_getsockname) | |
942 | + smp_rmb(); | |
943 | + return original_security_ops.socket_getsockname(sock); | |
944 | +} | |
945 | + | |
946 | +/** | |
947 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
948 | + * | |
949 | + * @sock: Pointer to "struct socket". | |
950 | + * | |
951 | + * Returns 0 on success, negative value otherwise. | |
952 | + */ | |
953 | +static int ccs_socket_getpeername(struct socket *sock) | |
954 | +{ | |
955 | + int rc = ccs_validate_socket(sock); | |
956 | + if (rc < 0) | |
957 | + return rc; | |
958 | + while (!original_security_ops.socket_getpeername) | |
959 | + smp_rmb(); | |
960 | + return original_security_ops.socket_getpeername(sock); | |
961 | +} | |
962 | + | |
963 | +/** | |
964 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
965 | + * | |
966 | + * @sock: Pointer to "struct socket". | |
967 | + * @level: Level. | |
968 | + * @optname: Option's name, | |
969 | + * | |
970 | + * Returns 0 on success, negative value otherwise. | |
971 | + */ | |
972 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
973 | +{ | |
974 | + int rc = ccs_validate_socket(sock); | |
975 | + if (rc < 0) | |
976 | + return rc; | |
977 | + while (!original_security_ops.socket_getsockopt) | |
978 | + smp_rmb(); | |
979 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
980 | +} | |
981 | + | |
982 | +/** | |
983 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
984 | + * | |
985 | + * @sock: Pointer to "struct socket". | |
986 | + * @level: Level. | |
987 | + * @optname: Option's name, | |
988 | + * | |
989 | + * Returns 0 on success, negative value otherwise. | |
990 | + */ | |
991 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
992 | +{ | |
993 | + int rc = ccs_validate_socket(sock); | |
994 | + if (rc < 0) | |
995 | + return rc; | |
996 | + while (!original_security_ops.socket_setsockopt) | |
997 | + smp_rmb(); | |
998 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
999 | +} | |
1000 | + | |
1001 | +/** | |
1002 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1003 | + * | |
1004 | + * @sock: Pointer to "struct socket". | |
1005 | + * @how: Shutdown mode. | |
1006 | + * | |
1007 | + * Returns 0 on success, negative value otherwise. | |
1008 | + */ | |
1009 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1010 | +{ | |
1011 | + int rc = ccs_validate_socket(sock); | |
1012 | + if (rc < 0) | |
1013 | + return rc; | |
1014 | + while (!original_security_ops.socket_shutdown) | |
1015 | + smp_rmb(); | |
1016 | + return original_security_ops.socket_shutdown(sock, how); | |
1017 | +} | |
1018 | + | |
1019 | +#define SOCKFS_MAGIC 0x534F434B | |
1020 | + | |
1021 | +/** | |
1022 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1023 | + * | |
1024 | + * @inode: Pointer to "struct inode". | |
1025 | + * | |
1026 | + * Returns nothing. | |
1027 | + * | |
1028 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1029 | + */ | |
1030 | +static void ccs_inode_free_security(struct inode *inode) | |
1031 | +{ | |
1032 | + while (!original_security_ops.inode_free_security) | |
1033 | + smp_rmb(); | |
1034 | + original_security_ops.inode_free_security(inode); | |
1035 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1036 | + ccs_update_socket_tag(inode, 0); | |
1037 | +} | |
1038 | + | |
1039 | +#endif | |
1040 | + | |
1041 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24) | |
1042 | + | |
1043 | +/** | |
1044 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1045 | + * | |
1046 | + * @old_nd: Pointer to "struct nameidata". | |
1047 | + * @new_nd: Pointer to "struct nameidata". | |
1048 | + * | |
1049 | + * Returns 0 on success, negative value otherwise. | |
1050 | + */ | |
1051 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1052 | +{ | |
1053 | + int rc = ccs_pivot_root_permission(old_nd, new_nd); | |
1054 | + if (rc) | |
1055 | + return rc; | |
1056 | + while (!original_security_ops.sb_pivotroot) | |
1057 | + smp_rmb(); | |
1058 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1059 | +} | |
1060 | + | |
1061 | +/** | |
1062 | + * ccs_sb_mount - Check permission for mount(). | |
1063 | + * | |
1064 | + * @dev_name: Name of device file. | |
1065 | + * @nd: Pointer to "struct nameidata". | |
1066 | + * @type: Name of filesystem type. Maybe NULL. | |
1067 | + * @flags: Mount options. | |
1068 | + * @data_page: Optional data. Maybe NULL. | |
1069 | + * | |
1070 | + * Returns 0 on success, negative value otherwise. | |
1071 | + */ | |
1072 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1073 | + unsigned long flags, void *data_page) | |
1074 | +{ | |
1075 | + int rc = ccs_mount_permission(dev_name, nd, type, flags, data_page); | |
1076 | + if (rc) | |
1077 | + return rc; | |
1078 | + while (!original_security_ops.sb_mount) | |
1079 | + smp_rmb(); | |
1080 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1081 | + data_page); | |
1082 | +} | |
1083 | + | |
1084 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) | |
1085 | + | |
1086 | +/** | |
1087 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1088 | + * | |
1089 | + * @old_nd: Pointer to "struct nameidata". | |
1090 | + * @new_nd: Pointer to "struct nameidata". | |
1091 | + * | |
1092 | + * Returns 0 on success, negative value otherwise. | |
1093 | + */ | |
1094 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1095 | +{ | |
1096 | + int rc = ccs_pivot_root_permission(&old_nd->path, &new_nd->path); | |
1097 | + if (rc) | |
1098 | + return rc; | |
1099 | + while (!original_security_ops.sb_pivotroot) | |
1100 | + smp_rmb(); | |
1101 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1102 | +} | |
1103 | + | |
1104 | +/** | |
1105 | + * ccs_sb_mount - Check permission for mount(). | |
1106 | + * | |
1107 | + * @dev_name: Name of device file. | |
1108 | + * @nd: Pointer to "struct nameidata". | |
1109 | + * @type: Name of filesystem type. Maybe NULL. | |
1110 | + * @flags: Mount options. | |
1111 | + * @data_page: Optional data. Maybe NULL. | |
1112 | + * | |
1113 | + * Returns 0 on success, negative value otherwise. | |
1114 | + */ | |
1115 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1116 | + unsigned long flags, void *data_page) | |
1117 | +{ | |
1118 | + int rc = ccs_mount_permission(dev_name, &nd->path, type, flags, | |
1119 | + data_page); | |
1120 | + if (rc) | |
1121 | + return rc; | |
1122 | + while (!original_security_ops.sb_mount) | |
1123 | + smp_rmb(); | |
1124 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1125 | + data_page); | |
1126 | +} | |
1127 | + | |
1128 | +#else | |
1129 | + | |
1130 | +/** | |
1131 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1132 | + * | |
1133 | + * @old_path: Pointer to "struct path". | |
1134 | + * @new_path: Pointer to "struct path". | |
1135 | + * | |
1136 | + * Returns 0 on success, negative value otherwise. | |
1137 | + */ | |
1138 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1139 | +{ | |
1140 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1141 | + if (rc) | |
1142 | + return rc; | |
1143 | + while (!original_security_ops.sb_pivotroot) | |
1144 | + smp_rmb(); | |
1145 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1146 | +} | |
1147 | + | |
1148 | +/** | |
1149 | + * ccs_sb_mount - Check permission for mount(). | |
1150 | + * | |
1151 | + * @dev_name: Name of device file. | |
1152 | + * @path: Pointer to "struct path". | |
1153 | + * @type: Name of filesystem type. Maybe NULL. | |
1154 | + * @flags: Mount options. | |
1155 | + * @data_page: Optional data. Maybe NULL. | |
1156 | + * | |
1157 | + * Returns 0 on success, negative value otherwise. | |
1158 | + */ | |
1159 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1160 | + unsigned long flags, void *data_page) | |
1161 | +{ | |
1162 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1163 | + if (rc) | |
1164 | + return rc; | |
1165 | + while (!original_security_ops.sb_mount) | |
1166 | + smp_rmb(); | |
1167 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1168 | + data_page); | |
1169 | +} | |
1170 | + | |
1171 | +#endif | |
1172 | + | |
1173 | +/** | |
1174 | + * ccs_sb_umount - Check permission for umount(). | |
1175 | + * | |
1176 | + * @mnt: Pointer to "struct vfsmount". | |
1177 | + * @flags: Unmount flags. | |
1178 | + * | |
1179 | + * Returns 0 on success, negative value otherwise. | |
1180 | + */ | |
1181 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1182 | +{ | |
1183 | + int rc = ccs_umount_permission(mnt, flags); | |
1184 | + if (rc) | |
1185 | + return rc; | |
1186 | + while (!original_security_ops.sb_umount) | |
1187 | + smp_rmb(); | |
1188 | + return original_security_ops.sb_umount(mnt, flags); | |
1189 | +} | |
1190 | + | |
1191 | +/** | |
1192 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1193 | + * | |
1194 | + * @file: Pointer to "struct file". | |
1195 | + * @cmd: Command number. | |
1196 | + * @arg: Value for @cmd. | |
1197 | + * | |
1198 | + * Returns 0 on success, negative value otherwise. | |
1199 | + */ | |
1200 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1201 | + unsigned long arg) | |
1202 | +{ | |
1203 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1204 | + if (rc) | |
1205 | + return rc; | |
1206 | + while (!original_security_ops.file_fcntl) | |
1207 | + smp_rmb(); | |
1208 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1209 | +} | |
1210 | + | |
1211 | +/** | |
1212 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1213 | + * | |
1214 | + * @filp: Pointer to "struct file". | |
1215 | + * @cmd: Command number. | |
1216 | + * @arg: Value for @cmd. | |
1217 | + * | |
1218 | + * Returns 0 on success, negative value otherwise. | |
1219 | + */ | |
1220 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1221 | + unsigned long arg) | |
1222 | +{ | |
1223 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1224 | + if (rc) | |
1225 | + return rc; | |
1226 | + while (!original_security_ops.file_ioctl) | |
1227 | + smp_rmb(); | |
1228 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1229 | +} | |
1230 | + | |
1231 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1232 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1233 | + const struct ccs_path_info *filename); | |
1234 | + | |
1235 | +/** | |
1236 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1237 | + * | |
1238 | + * @buffer: Pointer to "struct char *". | |
1239 | + * @buflen: Pointer to int which holds size of @buffer. | |
1240 | + * @str: String to copy. | |
1241 | + * | |
1242 | + * Returns 0 on success, negative value otherwise. | |
1243 | + * | |
1244 | + * @buffer and @buflen are updated upon success. | |
1245 | + */ | |
1246 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1247 | +{ | |
1248 | + int namelen = strlen(str); | |
1249 | + if (*buflen < namelen) | |
1250 | + return -ENOMEM; | |
1251 | + *buflen -= namelen; | |
1252 | + *buffer -= namelen; | |
1253 | + memcpy(*buffer, str, namelen); | |
1254 | + return 0; | |
1255 | +} | |
1256 | + | |
1257 | +/** | |
1258 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1259 | + * | |
1260 | + * @table: Pointer to "struct ctl_table". | |
1261 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1262 | + * | |
1263 | + * Returns 0 on success, negative value otherwise. | |
1264 | + */ | |
1265 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1266 | +{ | |
1267 | + int error; | |
1268 | + struct ccs_path_info buf; | |
1269 | + struct ccs_request_info r; | |
1270 | + int buflen; | |
1271 | + char *buffer; | |
1272 | + int idx; | |
1273 | + while (!original_security_ops.sysctl) | |
1274 | + smp_rmb(); | |
1275 | + error = original_security_ops.sysctl(table, op); | |
1276 | + if (error) | |
1277 | + return error; | |
1278 | + op &= MAY_READ | MAY_WRITE; | |
1279 | + if (!op) | |
1280 | + return 0; | |
1281 | + buffer = NULL; | |
1282 | + buf.name = NULL; | |
1283 | + idx = ccs_read_lock(); | |
1284 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1285 | + == CCS_CONFIG_DISABLED) | |
1286 | + goto out; | |
1287 | + error = -ENOMEM; | |
1288 | + buflen = 4096; | |
1289 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1290 | + if (buffer) { | |
1291 | + char *end = buffer + buflen; | |
1292 | + *--end = '\0'; | |
1293 | + buflen--; | |
1294 | + while (table) { | |
1295 | + char num[32]; | |
1296 | + const char *sp = table->procname; | |
1297 | + if (!sp) { | |
1298 | + memset(num, 0, sizeof(num)); | |
1299 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1300 | + table->ctl_name); | |
1301 | + sp = num; | |
1302 | + } | |
1303 | + if (ccs_prepend(&end, &buflen, sp) || | |
1304 | + ccs_prepend(&end, &buflen, "/")) | |
1305 | + goto out; | |
1306 | + table = table->parent; | |
1307 | + } | |
1308 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1309 | + goto out; | |
1310 | + buf.name = ccs_encode(end); | |
1311 | + } | |
1312 | + if (buf.name) { | |
1313 | + ccs_fill_path_info(&buf); | |
1314 | + if (op & MAY_READ) | |
1315 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1316 | + else | |
1317 | + error = 0; | |
1318 | + if (!error && (op & MAY_WRITE)) | |
1319 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1320 | + } | |
1321 | +out: | |
1322 | + ccs_read_unlock(idx); | |
1323 | + kfree(buf.name); | |
1324 | + kfree(buffer); | |
1325 | + return error; | |
1326 | +} | |
1327 | + | |
1328 | +#endif | |
1329 | + | |
1330 | +/* | |
1331 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1332 | + * Because copying byte array is not atomic. Reader checks | |
1333 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1334 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1335 | + */ | |
1336 | +#define swap_security_ops(op) \ | |
1337 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1338 | + | |
1339 | +/** | |
1340 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1341 | + * | |
1342 | + * @ops: Pointer to "struct security_operations". | |
1343 | + * | |
1344 | + * Returns nothing. | |
1345 | + */ | |
1346 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1347 | +{ | |
1348 | + /* Security context allocator. */ | |
1349 | + swap_security_ops(task_alloc_security); | |
1350 | + swap_security_ops(task_free_security); | |
1351 | + swap_security_ops(bprm_alloc_security); | |
1352 | + swap_security_ops(bprm_free_security); | |
1353 | + /* Security context updater for successful execve(). */ | |
1354 | + swap_security_ops(bprm_check_security); | |
1355 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
1356 | + swap_security_ops(bprm_compute_creds); | |
1357 | +#else | |
1358 | + swap_security_ops(bprm_apply_creds); | |
1359 | +#endif | |
1360 | + /* Various permission checker. */ | |
1361 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1362 | + swap_security_ops(dentry_open); | |
1363 | +#else | |
1364 | + swap_security_ops(inode_permission); | |
1365 | +#endif | |
1366 | + swap_security_ops(file_fcntl); | |
1367 | + swap_security_ops(file_ioctl); | |
1368 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1369 | + swap_security_ops(sysctl); | |
1370 | +#endif | |
1371 | + swap_security_ops(sb_pivotroot); | |
1372 | + swap_security_ops(sb_mount); | |
1373 | + swap_security_ops(sb_umount); | |
1374 | + swap_security_ops(inode_mknod); | |
1375 | + swap_security_ops(inode_mkdir); | |
1376 | + swap_security_ops(inode_rmdir); | |
1377 | + swap_security_ops(inode_unlink); | |
1378 | + swap_security_ops(inode_symlink); | |
1379 | + swap_security_ops(inode_rename); | |
1380 | + swap_security_ops(inode_link); | |
1381 | + swap_security_ops(inode_create); | |
1382 | + swap_security_ops(inode_setattr); | |
1383 | + swap_security_ops(inode_getattr); | |
1384 | +#ifdef CONFIG_SECURITY_NETWORK | |
1385 | + swap_security_ops(socket_bind); | |
1386 | + swap_security_ops(socket_connect); | |
1387 | + swap_security_ops(socket_listen); | |
1388 | + swap_security_ops(socket_sendmsg); | |
1389 | + swap_security_ops(socket_recvmsg); | |
1390 | + swap_security_ops(socket_getsockname); | |
1391 | + swap_security_ops(socket_getpeername); | |
1392 | + swap_security_ops(socket_getsockopt); | |
1393 | + swap_security_ops(socket_setsockopt); | |
1394 | + swap_security_ops(socket_shutdown); | |
1395 | + swap_security_ops(socket_accept); | |
1396 | + swap_security_ops(inode_free_security); | |
1397 | +#endif | |
1398 | +} | |
1399 | + | |
1400 | +#undef swap_security_ops | |
1401 | + | |
1402 | +/** | |
1403 | + * ccs_init - Initialize this module. | |
1404 | + * | |
1405 | + * Returns 0 on success, negative value otherwise. | |
1406 | + */ | |
1407 | +static int __init ccs_init(void) | |
1408 | +{ | |
1409 | + struct security_operations *ops = probe_security_ops(); | |
1410 | + if (!ops) | |
1411 | + goto out; | |
1412 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1413 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1414 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1415 | + goto out; | |
1416 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1417 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1418 | + goto out; | |
1419 | +#endif | |
1420 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1421 | + if (!ccsecurity_exports.vfsmount_lock) | |
1422 | + goto out; | |
1423 | + ccs_main_init(); | |
1424 | + ccs_update_security_ops(ops); | |
1425 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1426 | + printk(KERN_INFO | |
1427 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1428 | + return 0; | |
1429 | +out: | |
1430 | + return -EINVAL; | |
1431 | +} | |
1432 | + | |
1433 | +module_init(ccs_init); | |
1434 | +MODULE_LICENSE("GPL"); | |
1435 | + | |
1436 | +/** | |
1437 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1438 | + * | |
1439 | + * @domain: Pointer to "struct ccs_domain_info". | |
1440 | + * | |
1441 | + * Returns true if @domain is in use, false otherwise. | |
1442 | + * | |
1443 | + * Caller holds rcu_read_lock(). | |
1444 | + */ | |
1445 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1446 | +{ | |
1447 | + return false; | |
1448 | +} | |
1449 | + | |
1450 | +/** | |
1451 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1452 | + * | |
1453 | + * @ptr: Pointer to "struct ccs_security". | |
1454 | + * @list: Pointer to "struct list_head". | |
1455 | + * | |
1456 | + * Returns nothing. | |
1457 | + */ | |
1458 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1459 | + struct list_head *list) | |
1460 | +{ | |
1461 | + unsigned long flags; | |
1462 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1463 | + list_add_rcu(&ptr->list, list); | |
1464 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1465 | +} | |
1466 | + | |
1467 | +/** | |
1468 | + * __ccs_alloc_task_security - Allocate memory for new tasks. | |
1469 | + * | |
1470 | + * @task: Pointer to "struct task_struct". | |
1471 | + * | |
1472 | + * Returns 0 on success, negative value otherwise. | |
1473 | + */ | |
1474 | +static int __ccs_alloc_task_security(const struct task_struct *task) | |
1475 | +{ | |
1476 | + struct ccs_security *old_security = ccs_current_security(); | |
1477 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
1478 | + GFP_KERNEL); | |
1479 | + struct list_head *list = &ccs_task_security_list | |
1480 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1481 | + if (!new_security) | |
1482 | + return -ENOMEM; | |
1483 | + new_security->task = task; | |
1484 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
1485 | + new_security->ccs_flags = old_security->ccs_flags; | |
1486 | + ccs_add_task_security(new_security, list); | |
1487 | + return 0; | |
1488 | +} | |
1489 | + | |
1490 | +/** | |
1491 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1492 | + * | |
1493 | + * @task: Pointer to "struct task_struct". | |
1494 | + * | |
1495 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1496 | + * out of memory, &ccs_default_security otherwise. | |
1497 | + * | |
1498 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1499 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1500 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1501 | + * won't work. | |
1502 | + */ | |
1503 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1504 | +{ | |
1505 | + struct ccs_security *ptr; | |
1506 | + struct list_head *list = &ccs_task_security_list | |
1507 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1508 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1509 | + while (!list->next); | |
1510 | + rcu_read_lock(); | |
1511 | + list_for_each_entry_rcu(ptr, list, list) { | |
1512 | + if (ptr->task != task) | |
1513 | + continue; | |
1514 | + rcu_read_unlock(); | |
1515 | + return ptr; | |
1516 | + } | |
1517 | + rcu_read_unlock(); | |
1518 | + if (task != current) | |
1519 | + return &ccs_default_security; | |
1520 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1521 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1522 | + if (!ptr) { | |
1523 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1524 | + task->pid); | |
1525 | + send_sig(SIGKILL, current, 0); | |
1526 | + return &ccs_oom_security; | |
1527 | + } | |
1528 | + *ptr = ccs_default_security; | |
1529 | + ptr->task = task; | |
1530 | + ccs_add_task_security(ptr, list); | |
1531 | + return ptr; | |
1532 | +} | |
1533 | + | |
1534 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
1535 | + | |
1536 | +/** | |
1537 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
1538 | + * | |
1539 | + * @rcu: Pointer to "struct rcu_head". | |
1540 | + * | |
1541 | + * Returns nothing. | |
1542 | + */ | |
1543 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
1544 | +{ | |
1545 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
1546 | + kfree(ptr); | |
1547 | +} | |
1548 | + | |
1549 | +#else | |
1550 | + | |
1551 | +/** | |
1552 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
1553 | + * | |
1554 | + * @arg: Pointer to "void". | |
1555 | + * | |
1556 | + * Returns nothing. | |
1557 | + */ | |
1558 | +static void ccs_rcu_free(void *arg) | |
1559 | +{ | |
1560 | + struct ccs_security *ptr = arg; | |
1561 | + kfree(ptr); | |
1562 | +} | |
1563 | + | |
1564 | +#endif | |
1565 | + | |
1566 | +/** | |
1567 | + * __ccs_free_task_security - Release memory associated with "struct task_struct". | |
1568 | + * | |
1569 | + * @task: Pointer to "struct task_struct". | |
1570 | + * | |
1571 | + * Returns nothing. | |
1572 | + */ | |
1573 | +static void __ccs_free_task_security(const struct task_struct *task) | |
1574 | +{ | |
1575 | + unsigned long flags; | |
1576 | + struct ccs_security *ptr = ccs_find_task_security(task); | |
1577 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
1578 | + return; | |
1579 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1580 | + list_del_rcu(&ptr->list); | |
1581 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1582 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
1583 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
1584 | +#else | |
1585 | + call_rcu(&ptr->rcu, ccs_rcu_free, ptr); | |
1586 | +#endif | |
1587 | +} |
@@ -0,0 +1,1567 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | + | |
14 | +static int __ccs_alloc_task_security(const struct task_struct *task); | |
15 | +static void __ccs_free_task_security(const struct task_struct *task); | |
16 | + | |
17 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
18 | +static struct ccs_security ccs_oom_security = { | |
19 | + .ccs_domain_info = &ccs_kernel_domain | |
20 | +}; | |
21 | + | |
22 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
23 | +static struct ccs_security ccs_default_security = { | |
24 | + .ccs_domain_info = &ccs_kernel_domain | |
25 | +}; | |
26 | + | |
27 | +/* List of "struct ccs_security". */ | |
28 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
29 | +/* Lock for protecting ccs_task_security_list[]. */ | |
30 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
31 | + | |
32 | +/* Dummy marker for calling security_bprm_free(). */ | |
33 | +static const unsigned long ccs_bprm_security; | |
34 | + | |
35 | +/* For exporting variables and functions. */ | |
36 | +struct ccsecurity_exports ccsecurity_exports; | |
37 | +/* Members are updated by loadable kernel module. */ | |
38 | +struct ccsecurity_operations ccsecurity_ops; | |
39 | + | |
40 | +/* Function pointers originally registered by register_security(). */ | |
41 | +static struct security_operations original_security_ops /* = *security_ops; */; | |
42 | + | |
43 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
44 | + | |
45 | +/** | |
46 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
47 | + * | |
48 | + * @count: Count to increment or decrement. | |
49 | + * | |
50 | + * Returns updated counter. | |
51 | + */ | |
52 | +static unsigned int ccs_update_ee_counter(int count) | |
53 | +{ | |
54 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10) || defined(atomic_add_return) | |
55 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
56 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
57 | + return atomic_add_return(count, &ccs_ee_counter); | |
58 | +#else | |
59 | + static DEFINE_SPINLOCK(ccs_ee_lock); | |
60 | + static unsigned int ccs_ee_counter; | |
61 | + unsigned long flags; | |
62 | + spin_lock_irqsave(&ccs_ee_lock, flags); | |
63 | + ccs_ee_counter += count; | |
64 | + count = ccs_ee_counter; | |
65 | + spin_unlock_irqrestore(&ccs_ee_lock, flags); | |
66 | + return count; | |
67 | +#endif | |
68 | +} | |
69 | + | |
70 | +/** | |
71 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
72 | + * | |
73 | + * @ee: Pointer to "struct ccs_execve". | |
74 | + * | |
75 | + * Returns nothing. | |
76 | + */ | |
77 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
78 | +{ | |
79 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
80 | + current->pid, ccs_update_ee_counter(1) - 1); | |
81 | +} | |
82 | + | |
83 | +/** | |
84 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
85 | + * | |
86 | + * @ee: Pointer to "struct ccs_execve". | |
87 | + * @task: True if released by current task, false otherwise. | |
88 | + * | |
89 | + * Returns nothing. | |
90 | + */ | |
91 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
92 | + const bool is_current) | |
93 | +{ | |
94 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
95 | + if (is_current) | |
96 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
97 | + ee, current->pid, tmp); | |
98 | + else | |
99 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
100 | + ee, tmp); | |
101 | +} | |
102 | + | |
103 | +#endif | |
104 | + | |
105 | +#if !defined(CONFIG_AKARI_DEBUG) | |
106 | +#define ccs_debug_trace(pos) do { } while (0) | |
107 | +#else | |
108 | +#define ccs_debug_trace(pos) \ | |
109 | + do { \ | |
110 | + static bool done; \ | |
111 | + if (!done) { \ | |
112 | + printk(KERN_INFO \ | |
113 | + "AKARI: Debug trace: " pos " of 4\n"); \ | |
114 | + done = true; \ | |
115 | + } \ | |
116 | + } while (0) | |
117 | +#endif | |
118 | + | |
119 | +/** | |
120 | + * ccs_clear_execve - Release memory used by do_execve(). | |
121 | + * | |
122 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
123 | + * @security: Pointer to "struct ccs_security". | |
124 | + * | |
125 | + * Returns nothing. | |
126 | + */ | |
127 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
128 | +{ | |
129 | + struct ccs_execve *ee; | |
130 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
131 | + return; | |
132 | + ee = security->ee; | |
133 | + security->ee = NULL; | |
134 | + if (!ee) | |
135 | + return; | |
136 | + ccs_finish_execve(ret, ee); | |
137 | +} | |
138 | + | |
139 | +/** | |
140 | + * ccs_task_alloc_security - Allocate memory for new tasks. | |
141 | + * | |
142 | + * @p: Pointer to "struct task_struct". | |
143 | + * | |
144 | + * Returns 0 on success, negative value otherwise. | |
145 | + */ | |
146 | +static int ccs_task_alloc_security(struct task_struct *p) | |
147 | +{ | |
148 | + int rc = __ccs_alloc_task_security(p); | |
149 | + if (rc) | |
150 | + return rc; | |
151 | + while (!original_security_ops.task_alloc_security) | |
152 | + smp_rmb(); | |
153 | + rc = original_security_ops.task_alloc_security(p); | |
154 | + if (rc) | |
155 | + __ccs_free_task_security(p); | |
156 | + return rc; | |
157 | +} | |
158 | + | |
159 | +/** | |
160 | + * ccs_task_free_security - Release memory for "struct task_struct". | |
161 | + * | |
162 | + * @p: Pointer to "struct task_struct". | |
163 | + * | |
164 | + * Returns nothing. | |
165 | + */ | |
166 | +static void ccs_task_free_security(struct task_struct *p) | |
167 | +{ | |
168 | + while (!original_security_ops.task_free_security) | |
169 | + smp_rmb(); | |
170 | + original_security_ops.task_free_security(p); | |
171 | + __ccs_free_task_security(p); | |
172 | +} | |
173 | + | |
174 | +/** | |
175 | + * ccs_bprm_alloc_security - Allocate memory for "struct linux_binprm". | |
176 | + * | |
177 | + * @bprm: Pointer to "struct linux_binprm". | |
178 | + * | |
179 | + * Returns 0 on success, negative value otherwise. | |
180 | + */ | |
181 | +static int ccs_bprm_alloc_security(struct linux_binprm *bprm) | |
182 | +{ | |
183 | + int rc; | |
184 | + while (!original_security_ops.bprm_alloc_security) | |
185 | + smp_rmb(); | |
186 | + rc = original_security_ops.bprm_alloc_security(bprm); | |
187 | + if (bprm->security || rc) | |
188 | + return rc; | |
189 | + /* | |
190 | + * Update bprm->security to &ccs_bprm_security so that | |
191 | + * security_bprm_free() is called even if do_execve() failed at | |
192 | + * search_binary_handler() without allocating memory at | |
193 | + * security_bprm_alloc(). This trick assumes that active LSM module | |
194 | + * does not access bprm->security if that module did not allocate | |
195 | + * memory at security_bprm_alloc(). | |
196 | + */ | |
197 | + bprm->security = (void *) &ccs_bprm_security; | |
198 | + return 0; | |
199 | +} | |
200 | + | |
201 | +/** | |
202 | + * ccs_bprm_free_security - Release memory for "struct linux_binprm". | |
203 | + * | |
204 | + * @bprm: Pointer to "struct linux_binprm". | |
205 | + * | |
206 | + * Returns nothing. | |
207 | + */ | |
208 | +static void ccs_bprm_free_security(struct linux_binprm *bprm) | |
209 | +{ | |
210 | + /* | |
211 | + * If do_execve() succeeded, bprm->security will be updated to NULL at | |
212 | + * security_bprm_compute_creds()/security_bprm_apply_creds() if | |
213 | + * bprm->security was set to &ccs_bprm_security at | |
214 | + * security_bprm_alloc(). | |
215 | + * | |
216 | + * If do_execve() failed, bprm->security remains at &ccs_bprm_security | |
217 | + * if bprm->security was set to &ccs_bprm_security at | |
218 | + * security_bprm_alloc(). | |
219 | + * | |
220 | + * And do_execve() does not call security_bprm_free() if do_execve() | |
221 | + * failed and bprm->security == NULL. Therefore, do not call | |
222 | + * original_security_ops.bprm_free_security() if bprm->security remains | |
223 | + * at &ccs_bprm_security . | |
224 | + */ | |
225 | + if (bprm->security != &ccs_bprm_security) { | |
226 | + while (!original_security_ops.bprm_free_security) | |
227 | + smp_rmb(); | |
228 | + original_security_ops.bprm_free_security(bprm); | |
229 | + } | |
230 | + /* | |
231 | + * If do_execve() succeeded, | |
232 | + * ccs_clear_execve(0, ccs_current_security()); | |
233 | + * is called before calling below one. | |
234 | + * Thus, below call becomes no-op if do_execve() succeeded. | |
235 | + */ | |
236 | + ccs_clear_execve(-1, ccs_current_security()); | |
237 | +} | |
238 | + | |
239 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
240 | + | |
241 | +/** | |
242 | + * ccs_bprm_compute_creds - A hook which is called when do_execve() succeeded. | |
243 | + * | |
244 | + * @bprm: Pointer to "struct linux_binprm". | |
245 | + * | |
246 | + * Returns nothing. | |
247 | + */ | |
248 | +static void ccs_bprm_compute_creds(struct linux_binprm *bprm) | |
249 | +{ | |
250 | + if (bprm->security == &ccs_bprm_security) | |
251 | + bprm->security = NULL; | |
252 | + while (!original_security_ops.bprm_compute_creds) | |
253 | + smp_rmb(); | |
254 | + original_security_ops.bprm_compute_creds(bprm); | |
255 | + ccs_clear_execve(0, ccs_current_security()); | |
256 | +} | |
257 | + | |
258 | +#else | |
259 | + | |
260 | +/** | |
261 | + * ccs_bprm_apply_creds - A hook which is called when do_execve() succeeded. | |
262 | + * | |
263 | + * @bprm: Pointer to "struct linux_binprm". | |
264 | + * @unsafe: Unsafe flag. | |
265 | + * | |
266 | + * Returns nothing. | |
267 | + */ | |
268 | +static void ccs_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) | |
269 | +{ | |
270 | + if (bprm->security == &ccs_bprm_security) | |
271 | + bprm->security = NULL; | |
272 | + while (!original_security_ops.bprm_apply_creds) | |
273 | + smp_rmb(); | |
274 | + original_security_ops.bprm_apply_creds(bprm, unsafe); | |
275 | + ccs_clear_execve(0, ccs_current_security()); | |
276 | +} | |
277 | + | |
278 | +#endif | |
279 | + | |
280 | +/** | |
281 | + * ccs_bprm_check_security - Check permission for execve(). | |
282 | + * | |
283 | + * @bprm: Pointer to "struct linux_binprm". | |
284 | + * | |
285 | + * Returns 0 on success, negative value otherwise. | |
286 | + */ | |
287 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
288 | +{ | |
289 | + struct ccs_security *security = ccs_current_security(); | |
290 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
291 | + return -ENOMEM; | |
292 | + if (!security->ee) { | |
293 | + int rc; | |
294 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
295 | + if (!ccs_policy_loaded) | |
296 | + ccs_load_policy(bprm->filename); | |
297 | +#endif | |
298 | + rc = ccs_start_execve(bprm, &security->ee); | |
299 | + if (rc) | |
300 | + return rc; | |
301 | + } | |
302 | + while (!original_security_ops.bprm_check_security) | |
303 | + smp_rmb(); | |
304 | + return original_security_ops.bprm_check_security(bprm); | |
305 | +} | |
306 | + | |
307 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
308 | + | |
309 | +/** | |
310 | + * ccs_open - Check permission for open(). | |
311 | + * | |
312 | + * @f: Pointer to "struct file". | |
313 | + * | |
314 | + * Returns 0 on success, negative value otherwise. | |
315 | + */ | |
316 | +static int ccs_open(struct file *f) | |
317 | +{ | |
318 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
319 | + f->f_flags + 1); | |
320 | +} | |
321 | + | |
322 | +#endif | |
323 | + | |
324 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
325 | + | |
326 | +/** | |
327 | + * ccs_dentry_open - Check permission for open(). | |
328 | + * | |
329 | + * @f: Pointer to "struct file". | |
330 | + * | |
331 | + * Returns 0 on success, negative value otherwise. | |
332 | + */ | |
333 | +static int ccs_dentry_open(struct file *f) | |
334 | +{ | |
335 | + int rc = ccs_open(f); | |
336 | + if (rc) | |
337 | + return rc; | |
338 | + while (!original_security_ops.dentry_open) | |
339 | + smp_rmb(); | |
340 | + return original_security_ops.dentry_open(f); | |
341 | +} | |
342 | + | |
343 | +#else | |
344 | + | |
345 | +/** | |
346 | + * ccs_open - Check permission for open(). | |
347 | + * | |
348 | + * @inode: Pointer to "struct inode". | |
349 | + * @mask: Open mode. | |
350 | + * @nd: Pointer to "struct nameidata". | |
351 | + * | |
352 | + * Returns 0 on success, negative value otherwise. | |
353 | + */ | |
354 | +static int ccs_open(struct inode *inode, int mask, struct nameidata *nd) | |
355 | +{ | |
356 | + int flags; | |
357 | + if (!nd || !nd->dentry) | |
358 | + return 0; | |
359 | + /* open_exec() passes MAY_EXEC . */ | |
360 | + if (mask == MAY_EXEC && inode && S_ISREG(inode->i_mode) && | |
361 | + (ccs_current_flags() & CCS_TASK_IS_IN_EXECVE)) | |
362 | + mask = MAY_READ; | |
363 | + /* | |
364 | + * This flags value is passed to ACC_MODE(). | |
365 | + * ccs_open_permission() for older versions uses old ACC_MODE(). | |
366 | + */ | |
367 | + switch (mask & (MAY_READ | MAY_WRITE)) { | |
368 | + case MAY_READ: | |
369 | + flags = 01; | |
370 | + break; | |
371 | + case MAY_WRITE: | |
372 | + flags = 02; | |
373 | + break; | |
374 | + case MAY_READ | MAY_WRITE: | |
375 | + flags = 03; | |
376 | + break; | |
377 | + default: | |
378 | + return 0; | |
379 | + } | |
380 | + return ccs_open_permission(nd->dentry, nd->mnt, flags); | |
381 | +} | |
382 | + | |
383 | +/** | |
384 | + * ccs_inode_permission - Check permission for open(). | |
385 | + * | |
386 | + * @inode: Pointer to "struct inode". | |
387 | + * @mask: Open mode. | |
388 | + * @nd: Pointer to "struct nameidata". | |
389 | + * | |
390 | + * Returns 0 on success, negative value otherwise. | |
391 | + * | |
392 | + * Note that this hook is called from permission(), and may not be called for | |
393 | + * open(). Maybe it is better to use security_file_permission(). | |
394 | + */ | |
395 | +static int ccs_inode_permission(struct inode *inode, int mask, | |
396 | + struct nameidata *nd) | |
397 | +{ | |
398 | + int rc = ccs_open(inode, mask, nd); | |
399 | + if (rc) | |
400 | + return rc; | |
401 | + while (!original_security_ops.inode_permission) | |
402 | + smp_rmb(); | |
403 | + return original_security_ops.inode_permission(inode, mask, nd); | |
404 | +} | |
405 | + | |
406 | +#endif | |
407 | + | |
408 | +/** | |
409 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
410 | + * | |
411 | + * @dentry: Pointer to "struct dentry". | |
412 | + * @attr: Pointer to "struct iattr". | |
413 | + * | |
414 | + * Returns 0 on success, negative value otherwise. | |
415 | + */ | |
416 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
417 | +{ | |
418 | + int rc = 0; | |
419 | + if (attr->ia_valid & ATTR_UID) | |
420 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, -1); | |
421 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
422 | + rc = ccs_chown_permission(dentry, NULL, -1, attr->ia_gid); | |
423 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
424 | + rc = ccs_chmod_permission(dentry, NULL, attr->ia_mode); | |
425 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
426 | + rc = ccs_truncate_permission(dentry, NULL); | |
427 | + if (rc) | |
428 | + return rc; | |
429 | + while (!original_security_ops.inode_setattr) | |
430 | + smp_rmb(); | |
431 | + return original_security_ops.inode_setattr(dentry, attr); | |
432 | +} | |
433 | + | |
434 | +/** | |
435 | + * ccs_inode_getattr - Check permission for stat(). | |
436 | + * | |
437 | + * @mnt: Pointer to "struct vfsmount". | |
438 | + * @dentry: Pointer to "struct dentry". | |
439 | + * | |
440 | + * Returns 0 on success, negative value otherwise. | |
441 | + */ | |
442 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
443 | +{ | |
444 | + int rc = ccs_getattr_permission(mnt, dentry); | |
445 | + if (rc) | |
446 | + return rc; | |
447 | + while (!original_security_ops.inode_getattr) | |
448 | + smp_rmb(); | |
449 | + return original_security_ops.inode_getattr(mnt, dentry); | |
450 | +} | |
451 | + | |
452 | +/** | |
453 | + * ccs_inode_mknod - Check permission for mknod(). | |
454 | + * | |
455 | + * @dir: Pointer to "struct inode". | |
456 | + * @dentry: Pointer to "struct dentry". | |
457 | + * @mode: Create mode. | |
458 | + * @dev: Device major/minor number. | |
459 | + * | |
460 | + * Returns 0 on success, negative value otherwise. | |
461 | + */ | |
462 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
463 | + dev_t dev) | |
464 | +{ | |
465 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
466 | + if (rc) | |
467 | + return rc; | |
468 | + while (!original_security_ops.inode_mknod) | |
469 | + smp_rmb(); | |
470 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
471 | +} | |
472 | + | |
473 | +/** | |
474 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
475 | + * | |
476 | + * @dir: Pointer to "struct inode". | |
477 | + * @dentry: Pointer to "struct dentry". | |
478 | + * @mode: Create mode. | |
479 | + * | |
480 | + * Returns 0 on success, negative value otherwise. | |
481 | + */ | |
482 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
483 | +{ | |
484 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
485 | + if (rc) | |
486 | + return rc; | |
487 | + while (!original_security_ops.inode_mkdir) | |
488 | + smp_rmb(); | |
489 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
490 | +} | |
491 | + | |
492 | +/** | |
493 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
494 | + * | |
495 | + * @dir: Pointer to "struct inode". | |
496 | + * @dentry: Pointer to "struct dentry". | |
497 | + * | |
498 | + * Returns 0 on success, negative value otherwise. | |
499 | + */ | |
500 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
501 | +{ | |
502 | + int rc = ccs_rmdir_permission(dentry, NULL); | |
503 | + if (rc) | |
504 | + return rc; | |
505 | + while (!original_security_ops.inode_rmdir) | |
506 | + smp_rmb(); | |
507 | + return original_security_ops.inode_rmdir(dir, dentry); | |
508 | +} | |
509 | + | |
510 | +/** | |
511 | + * ccs_inode_unlink - Check permission for unlink(). | |
512 | + * | |
513 | + * @dir: Pointer to "struct inode". | |
514 | + * @dentry: Pointer to "struct dentry". | |
515 | + * | |
516 | + * Returns 0 on success, negative value otherwise. | |
517 | + */ | |
518 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
519 | +{ | |
520 | + int rc = ccs_unlink_permission(dentry, NULL); | |
521 | + if (rc) | |
522 | + return rc; | |
523 | + while (!original_security_ops.inode_unlink) | |
524 | + smp_rmb(); | |
525 | + return original_security_ops.inode_unlink(dir, dentry); | |
526 | +} | |
527 | + | |
528 | +/** | |
529 | + * ccs_inode_symlink - Check permission for symlink(). | |
530 | + * | |
531 | + * @dir: Pointer to "struct inode". | |
532 | + * @dentry: Pointer to "struct dentry". | |
533 | + * @old_name: Content of symbolic link. | |
534 | + * | |
535 | + * Returns 0 on success, negative value otherwise. | |
536 | + */ | |
537 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
538 | + const char *old_name) | |
539 | +{ | |
540 | + int rc = ccs_symlink_permission(dentry, NULL, old_name); | |
541 | + if (rc) | |
542 | + return rc; | |
543 | + while (!original_security_ops.inode_symlink) | |
544 | + smp_rmb(); | |
545 | + return original_security_ops.inode_symlink(dir, dentry, old_name); | |
546 | +} | |
547 | + | |
548 | +/** | |
549 | + * ccs_inode_rename - Check permission for rename(). | |
550 | + * | |
551 | + * @old_dir: Pointer to "struct inode". | |
552 | + * @old_dentry: Pointer to "struct dentry". | |
553 | + * @new_dir: Pointer to "struct inode". | |
554 | + * @new_dentry: Pointer to "struct dentry". | |
555 | + * | |
556 | + * Returns 0 on success, negative value otherwise. | |
557 | + */ | |
558 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
559 | + struct inode *new_dir, struct dentry *new_dentry) | |
560 | +{ | |
561 | + int rc = ccs_rename_permission(old_dentry, new_dentry, NULL); | |
562 | + if (rc) | |
563 | + return rc; | |
564 | + while (!original_security_ops.inode_rename) | |
565 | + smp_rmb(); | |
566 | + return original_security_ops.inode_rename(old_dir, old_dentry, new_dir, | |
567 | + new_dentry); | |
568 | +} | |
569 | + | |
570 | +/** | |
571 | + * ccs_inode_link - Check permission for link(). | |
572 | + * | |
573 | + * @old_dentry: Pointer to "struct dentry". | |
574 | + * @dir: Pointer to "struct inode". | |
575 | + * @new_dentry: Pointer to "struct dentry". | |
576 | + * | |
577 | + * Returns 0 on success, negative value otherwise. | |
578 | + */ | |
579 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
580 | + struct dentry *new_dentry) | |
581 | +{ | |
582 | + int rc = ccs_link_permission(old_dentry, new_dentry, NULL); | |
583 | + if (rc) | |
584 | + return rc; | |
585 | + while (!original_security_ops.inode_link) | |
586 | + smp_rmb(); | |
587 | + return original_security_ops.inode_link(old_dentry, dir, new_dentry); | |
588 | +} | |
589 | + | |
590 | +/** | |
591 | + * ccs_inode_create - Check permission for creat(). | |
592 | + * | |
593 | + * @dir: Pointer to "struct inode". | |
594 | + * @dentry: Pointer to "struct dentry". | |
595 | + * @mode: Create mode. | |
596 | + * | |
597 | + * Returns 0 on success, negative value otherwise. | |
598 | + */ | |
599 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
600 | + int mode) | |
601 | +{ | |
602 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
603 | + if (rc) | |
604 | + return rc; | |
605 | + while (!original_security_ops.inode_create) | |
606 | + smp_rmb(); | |
607 | + return original_security_ops.inode_create(dir, dentry, mode); | |
608 | +} | |
609 | + | |
610 | +#ifdef CONFIG_SECURITY_NETWORK | |
611 | + | |
612 | +#include <net/sock.h> | |
613 | + | |
614 | +/* Structure for remembering an accept()ed socket's status. */ | |
615 | +struct ccs_socket_tag { | |
616 | + struct list_head list; | |
617 | + struct inode *inode; | |
618 | + int status; | |
619 | + struct rcu_head rcu; | |
620 | +}; | |
621 | + | |
622 | +/* | |
623 | + * List for managing accept()ed sockets. | |
624 | + * Since we don't need to keep an accept()ed socket into this list after | |
625 | + * once the permission was granted, the number of entries in this list is | |
626 | + * likely small. Therefore, we don't use hash tables. | |
627 | + */ | |
628 | +static LIST_HEAD(ccs_accepted_socket_list); | |
629 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
630 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
631 | + | |
632 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
633 | + | |
634 | +/** | |
635 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
636 | + * | |
637 | + * @rcu: Pointer to "struct rcu_head". | |
638 | + * | |
639 | + * Returns nothing. | |
640 | + */ | |
641 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
642 | +{ | |
643 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
644 | + kfree(ptr); | |
645 | +} | |
646 | + | |
647 | +#else | |
648 | + | |
649 | +/** | |
650 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
651 | + * | |
652 | + * @arg: Pointer to "void". | |
653 | + * | |
654 | + * Returns nothing. | |
655 | + */ | |
656 | +static void ccs_socket_rcu_free(void *arg) | |
657 | +{ | |
658 | + struct ccs_socket_tag *ptr = arg; | |
659 | + kfree(ptr); | |
660 | +} | |
661 | + | |
662 | +#endif | |
663 | + | |
664 | +/** | |
665 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
666 | + * | |
667 | + * @inode: Pointer to "struct inode". | |
668 | + * @status: New status. | |
669 | + * | |
670 | + * Returns nothing. | |
671 | + * | |
672 | + * If @status == 0, memory for that socket will be released after RCU grace | |
673 | + * period. | |
674 | + */ | |
675 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
676 | +{ | |
677 | + struct ccs_socket_tag *ptr; | |
678 | + /* | |
679 | + * Protect whole section because multiple threads may call this | |
680 | + * function with same "sock" via ccs_validate_socket(). | |
681 | + */ | |
682 | + spin_lock(&ccs_accepted_socket_list_lock); | |
683 | + rcu_read_lock(); | |
684 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
685 | + if (ptr->inode != inode) | |
686 | + continue; | |
687 | + ptr->status = status; | |
688 | + if (status) | |
689 | + break; | |
690 | + list_del_rcu(&ptr->list); | |
691 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
692 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
693 | +#else | |
694 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free, ptr); | |
695 | +#endif | |
696 | + break; | |
697 | + } | |
698 | + rcu_read_unlock(); | |
699 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
700 | +} | |
701 | + | |
702 | +/** | |
703 | + * ccs_validate_socket - Check post accept() permission if needed. | |
704 | + * | |
705 | + * @sock: Pointer to "struct socket". | |
706 | + * | |
707 | + * Returns 0 on success, negative value otherwise. | |
708 | + */ | |
709 | +static int ccs_validate_socket(struct socket *sock) | |
710 | +{ | |
711 | + struct inode *inode = SOCK_INODE(sock); | |
712 | + struct ccs_socket_tag *ptr; | |
713 | + int ret = 0; | |
714 | + rcu_read_lock(); | |
715 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
716 | + if (ptr->inode != inode) | |
717 | + continue; | |
718 | + ret = ptr->status; | |
719 | + break; | |
720 | + } | |
721 | + rcu_read_unlock(); | |
722 | + if (ret <= 0) | |
723 | + /* | |
724 | + * This socket is not an accept()ed socket or this socket is | |
725 | + * an accept()ed socket and post accept() permission is done. | |
726 | + */ | |
727 | + return ret; | |
728 | + /* | |
729 | + * Check post accept() permission now. | |
730 | + * | |
731 | + * Strictly speaking, we need to pass both listen()ing socket and | |
732 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
733 | + * But since socket's family and type are same for both sockets, | |
734 | + * passing the accept()ed socket in place for the listen()ing socket | |
735 | + * will work. | |
736 | + */ | |
737 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
738 | + /* | |
739 | + * If permission was granted, we forget that this is an accept()ed | |
740 | + * socket. Otherwise, we remember that this socket needs to return | |
741 | + * error for subsequent socketcalls. | |
742 | + */ | |
743 | + ccs_update_socket_tag(inode, ret); | |
744 | + return ret; | |
745 | +} | |
746 | + | |
747 | +/** | |
748 | + * ccs_socket_accept - Check permission for accept(). | |
749 | + * | |
750 | + * @sock: Pointer to "struct socket". | |
751 | + * @newsock: Pointer to "struct socket". | |
752 | + * | |
753 | + * Returns 0 on success, negative value otherwise. | |
754 | + * | |
755 | + * This hook is used for setting up environment for doing post accept() | |
756 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
757 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
758 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
759 | + * in order to do post accept() permission check before returning to userspace. | |
760 | + * If we make the copy in security_socket_post_create(), it would be possible | |
761 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
762 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
763 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
764 | + * rather than between sock->ops->accept() and returning to userspace. | |
765 | + * This means that if a socket was close()d before calling some socket | |
766 | + * syscalls, post accept() permission check will not be done. | |
767 | + */ | |
768 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
769 | +{ | |
770 | + struct ccs_socket_tag *ptr; | |
771 | + int rc = ccs_validate_socket(sock); | |
772 | + if (rc < 0) | |
773 | + return rc; | |
774 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
775 | + if (!ptr) | |
776 | + return -ENOMEM; | |
777 | + while (!original_security_ops.socket_accept) | |
778 | + smp_rmb(); | |
779 | + rc = original_security_ops.socket_accept(sock, newsock); | |
780 | + if (rc) { | |
781 | + kfree(ptr); | |
782 | + return rc; | |
783 | + } | |
784 | + /* | |
785 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
786 | + * "newsock" as "an accept()ed socket but post accept() permission | |
787 | + * check is not done yet" by allocating memory using inode of the | |
788 | + * "newsock" as a search key. | |
789 | + */ | |
790 | + ptr->inode = SOCK_INODE(newsock); | |
791 | + ptr->status = 1; /* Check post accept() permission later. */ | |
792 | + spin_lock(&ccs_accepted_socket_list_lock); | |
793 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
794 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
795 | + return 0; | |
796 | +} | |
797 | + | |
798 | +/** | |
799 | + * ccs_socket_listen - Check permission for listen(). | |
800 | + * | |
801 | + * @sock: Pointer to "struct socket". | |
802 | + * @backlog: Backlog parameter. | |
803 | + * | |
804 | + * Returns 0 on success, negative value otherwise. | |
805 | + */ | |
806 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
807 | +{ | |
808 | + int rc = ccs_validate_socket(sock); | |
809 | + if (rc < 0) | |
810 | + return rc; | |
811 | + rc = ccs_socket_listen_permission(sock); | |
812 | + if (rc) | |
813 | + return rc; | |
814 | + while (!original_security_ops.socket_listen) | |
815 | + smp_rmb(); | |
816 | + return original_security_ops.socket_listen(sock, backlog); | |
817 | +} | |
818 | + | |
819 | +/** | |
820 | + * ccs_socket_connect - Check permission for connect(). | |
821 | + * | |
822 | + * @sock: Pointer to "struct socket". | |
823 | + * @addr: Pointer to "struct sockaddr". | |
824 | + * @addr_len: Size of @addr. | |
825 | + * | |
826 | + * Returns 0 on success, negative value otherwise. | |
827 | + */ | |
828 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
829 | + int addr_len) | |
830 | +{ | |
831 | + int rc = ccs_validate_socket(sock); | |
832 | + if (rc < 0) | |
833 | + return rc; | |
834 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
835 | + if (rc) | |
836 | + return rc; | |
837 | + while (!original_security_ops.socket_connect) | |
838 | + smp_rmb(); | |
839 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
840 | +} | |
841 | + | |
842 | +/** | |
843 | + * ccs_socket_bind - Check permission for bind(). | |
844 | + * | |
845 | + * @sock: Pointer to "struct socket". | |
846 | + * @addr: Pointer to "struct sockaddr". | |
847 | + * @addr_len: Size of @addr. | |
848 | + * | |
849 | + * Returns 0 on success, negative value otherwise. | |
850 | + */ | |
851 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
852 | + int addr_len) | |
853 | +{ | |
854 | + int rc = ccs_validate_socket(sock); | |
855 | + if (rc < 0) | |
856 | + return rc; | |
857 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
858 | + if (rc) | |
859 | + return rc; | |
860 | + while (!original_security_ops.socket_bind) | |
861 | + smp_rmb(); | |
862 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
863 | +} | |
864 | + | |
865 | +/** | |
866 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
867 | + * | |
868 | + * @sock: Pointer to "struct socket". | |
869 | + * @msg: Pointer to "struct msghdr". | |
870 | + * @size: Size of message. | |
871 | + * | |
872 | + * Returns 0 on success, negative value otherwise. | |
873 | + */ | |
874 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
875 | + int size) | |
876 | +{ | |
877 | + int rc = ccs_validate_socket(sock); | |
878 | + if (rc < 0) | |
879 | + return rc; | |
880 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
881 | + if (rc) | |
882 | + return rc; | |
883 | + while (!original_security_ops.socket_sendmsg) | |
884 | + smp_rmb(); | |
885 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
886 | +} | |
887 | + | |
888 | +/** | |
889 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
890 | + * | |
891 | + * @sock: Pointer to "struct socket". | |
892 | + * @msg: Pointer to "struct msghdr". | |
893 | + * @size: Size of message. | |
894 | + * @flags: Flags. | |
895 | + * | |
896 | + * Returns 0 on success, negative value otherwise. | |
897 | + */ | |
898 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
899 | + int size, int flags) | |
900 | +{ | |
901 | + int rc = ccs_validate_socket(sock); | |
902 | + if (rc < 0) | |
903 | + return rc; | |
904 | + while (!original_security_ops.socket_recvmsg) | |
905 | + smp_rmb(); | |
906 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
907 | +} | |
908 | + | |
909 | +/** | |
910 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
911 | + * | |
912 | + * @sock: Pointer to "struct socket". | |
913 | + * | |
914 | + * Returns 0 on success, negative value otherwise. | |
915 | + */ | |
916 | +static int ccs_socket_getsockname(struct socket *sock) | |
917 | +{ | |
918 | + int rc = ccs_validate_socket(sock); | |
919 | + if (rc < 0) | |
920 | + return rc; | |
921 | + while (!original_security_ops.socket_getsockname) | |
922 | + smp_rmb(); | |
923 | + return original_security_ops.socket_getsockname(sock); | |
924 | +} | |
925 | + | |
926 | +/** | |
927 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
928 | + * | |
929 | + * @sock: Pointer to "struct socket". | |
930 | + * | |
931 | + * Returns 0 on success, negative value otherwise. | |
932 | + */ | |
933 | +static int ccs_socket_getpeername(struct socket *sock) | |
934 | +{ | |
935 | + int rc = ccs_validate_socket(sock); | |
936 | + if (rc < 0) | |
937 | + return rc; | |
938 | + while (!original_security_ops.socket_getpeername) | |
939 | + smp_rmb(); | |
940 | + return original_security_ops.socket_getpeername(sock); | |
941 | +} | |
942 | + | |
943 | +/** | |
944 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
945 | + * | |
946 | + * @sock: Pointer to "struct socket". | |
947 | + * @level: Level. | |
948 | + * @optname: Option's name, | |
949 | + * | |
950 | + * Returns 0 on success, negative value otherwise. | |
951 | + */ | |
952 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
953 | +{ | |
954 | + int rc = ccs_validate_socket(sock); | |
955 | + if (rc < 0) | |
956 | + return rc; | |
957 | + while (!original_security_ops.socket_getsockopt) | |
958 | + smp_rmb(); | |
959 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
960 | +} | |
961 | + | |
962 | +/** | |
963 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
964 | + * | |
965 | + * @sock: Pointer to "struct socket". | |
966 | + * @level: Level. | |
967 | + * @optname: Option's name, | |
968 | + * | |
969 | + * Returns 0 on success, negative value otherwise. | |
970 | + */ | |
971 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
972 | +{ | |
973 | + int rc = ccs_validate_socket(sock); | |
974 | + if (rc < 0) | |
975 | + return rc; | |
976 | + while (!original_security_ops.socket_setsockopt) | |
977 | + smp_rmb(); | |
978 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
979 | +} | |
980 | + | |
981 | +/** | |
982 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
983 | + * | |
984 | + * @sock: Pointer to "struct socket". | |
985 | + * @how: Shutdown mode. | |
986 | + * | |
987 | + * Returns 0 on success, negative value otherwise. | |
988 | + */ | |
989 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
990 | +{ | |
991 | + int rc = ccs_validate_socket(sock); | |
992 | + if (rc < 0) | |
993 | + return rc; | |
994 | + while (!original_security_ops.socket_shutdown) | |
995 | + smp_rmb(); | |
996 | + return original_security_ops.socket_shutdown(sock, how); | |
997 | +} | |
998 | + | |
999 | +#define SOCKFS_MAGIC 0x534F434B | |
1000 | + | |
1001 | +/** | |
1002 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1003 | + * | |
1004 | + * @inode: Pointer to "struct inode". | |
1005 | + * | |
1006 | + * Returns nothing. | |
1007 | + * | |
1008 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1009 | + */ | |
1010 | +static void ccs_inode_free_security(struct inode *inode) | |
1011 | +{ | |
1012 | + while (!original_security_ops.inode_free_security) | |
1013 | + smp_rmb(); | |
1014 | + original_security_ops.inode_free_security(inode); | |
1015 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1016 | + ccs_update_socket_tag(inode, 0); | |
1017 | +} | |
1018 | + | |
1019 | +#endif | |
1020 | + | |
1021 | +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24) | |
1022 | + | |
1023 | +/** | |
1024 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1025 | + * | |
1026 | + * @old_nd: Pointer to "struct nameidata". | |
1027 | + * @new_nd: Pointer to "struct nameidata". | |
1028 | + * | |
1029 | + * Returns 0 on success, negative value otherwise. | |
1030 | + */ | |
1031 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1032 | +{ | |
1033 | + int rc = ccs_pivot_root_permission(old_nd, new_nd); | |
1034 | + if (rc) | |
1035 | + return rc; | |
1036 | + while (!original_security_ops.sb_pivotroot) | |
1037 | + smp_rmb(); | |
1038 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1039 | +} | |
1040 | + | |
1041 | +/** | |
1042 | + * ccs_sb_mount - Check permission for mount(). | |
1043 | + * | |
1044 | + * @dev_name: Name of device file. | |
1045 | + * @nd: Pointer to "struct nameidata". | |
1046 | + * @type: Name of filesystem type. Maybe NULL. | |
1047 | + * @flags: Mount options. | |
1048 | + * @data_page: Optional data. Maybe NULL. | |
1049 | + * | |
1050 | + * Returns 0 on success, negative value otherwise. | |
1051 | + */ | |
1052 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1053 | + unsigned long flags, void *data_page) | |
1054 | +{ | |
1055 | + int rc = ccs_mount_permission(dev_name, nd, type, flags, data_page); | |
1056 | + if (rc) | |
1057 | + return rc; | |
1058 | + while (!original_security_ops.sb_mount) | |
1059 | + smp_rmb(); | |
1060 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1061 | + data_page); | |
1062 | +} | |
1063 | + | |
1064 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26) | |
1065 | + | |
1066 | +/** | |
1067 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1068 | + * | |
1069 | + * @old_nd: Pointer to "struct nameidata". | |
1070 | + * @new_nd: Pointer to "struct nameidata". | |
1071 | + * | |
1072 | + * Returns 0 on success, negative value otherwise. | |
1073 | + */ | |
1074 | +static int ccs_sb_pivotroot(struct nameidata *old_nd, struct nameidata *new_nd) | |
1075 | +{ | |
1076 | + int rc = ccs_pivot_root_permission(&old_nd->path, &new_nd->path); | |
1077 | + if (rc) | |
1078 | + return rc; | |
1079 | + while (!original_security_ops.sb_pivotroot) | |
1080 | + smp_rmb(); | |
1081 | + return original_security_ops.sb_pivotroot(old_nd, new_nd); | |
1082 | +} | |
1083 | + | |
1084 | +/** | |
1085 | + * ccs_sb_mount - Check permission for mount(). | |
1086 | + * | |
1087 | + * @dev_name: Name of device file. | |
1088 | + * @nd: Pointer to "struct nameidata". | |
1089 | + * @type: Name of filesystem type. Maybe NULL. | |
1090 | + * @flags: Mount options. | |
1091 | + * @data_page: Optional data. Maybe NULL. | |
1092 | + * | |
1093 | + * Returns 0 on success, negative value otherwise. | |
1094 | + */ | |
1095 | +static int ccs_sb_mount(char *dev_name, struct nameidata *nd, char *type, | |
1096 | + unsigned long flags, void *data_page) | |
1097 | +{ | |
1098 | + int rc = ccs_mount_permission(dev_name, &nd->path, type, flags, | |
1099 | + data_page); | |
1100 | + if (rc) | |
1101 | + return rc; | |
1102 | + while (!original_security_ops.sb_mount) | |
1103 | + smp_rmb(); | |
1104 | + return original_security_ops.sb_mount(dev_name, nd, type, flags, | |
1105 | + data_page); | |
1106 | +} | |
1107 | + | |
1108 | +#else | |
1109 | + | |
1110 | +/** | |
1111 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1112 | + * | |
1113 | + * @old_path: Pointer to "struct path". | |
1114 | + * @new_path: Pointer to "struct path". | |
1115 | + * | |
1116 | + * Returns 0 on success, negative value otherwise. | |
1117 | + */ | |
1118 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1119 | +{ | |
1120 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1121 | + if (rc) | |
1122 | + return rc; | |
1123 | + while (!original_security_ops.sb_pivotroot) | |
1124 | + smp_rmb(); | |
1125 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1126 | +} | |
1127 | + | |
1128 | +/** | |
1129 | + * ccs_sb_mount - Check permission for mount(). | |
1130 | + * | |
1131 | + * @dev_name: Name of device file. | |
1132 | + * @path: Pointer to "struct path". | |
1133 | + * @type: Name of filesystem type. Maybe NULL. | |
1134 | + * @flags: Mount options. | |
1135 | + * @data_page: Optional data. Maybe NULL. | |
1136 | + * | |
1137 | + * Returns 0 on success, negative value otherwise. | |
1138 | + */ | |
1139 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1140 | + unsigned long flags, void *data_page) | |
1141 | +{ | |
1142 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1143 | + if (rc) | |
1144 | + return rc; | |
1145 | + while (!original_security_ops.sb_mount) | |
1146 | + smp_rmb(); | |
1147 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1148 | + data_page); | |
1149 | +} | |
1150 | + | |
1151 | +#endif | |
1152 | + | |
1153 | +/** | |
1154 | + * ccs_sb_umount - Check permission for umount(). | |
1155 | + * | |
1156 | + * @mnt: Pointer to "struct vfsmount". | |
1157 | + * @flags: Unmount flags. | |
1158 | + * | |
1159 | + * Returns 0 on success, negative value otherwise. | |
1160 | + */ | |
1161 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1162 | +{ | |
1163 | + int rc = ccs_umount_permission(mnt, flags); | |
1164 | + if (rc) | |
1165 | + return rc; | |
1166 | + while (!original_security_ops.sb_umount) | |
1167 | + smp_rmb(); | |
1168 | + return original_security_ops.sb_umount(mnt, flags); | |
1169 | +} | |
1170 | + | |
1171 | +/** | |
1172 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1173 | + * | |
1174 | + * @file: Pointer to "struct file". | |
1175 | + * @cmd: Command number. | |
1176 | + * @arg: Value for @cmd. | |
1177 | + * | |
1178 | + * Returns 0 on success, negative value otherwise. | |
1179 | + */ | |
1180 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1181 | + unsigned long arg) | |
1182 | +{ | |
1183 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1184 | + if (rc) | |
1185 | + return rc; | |
1186 | + while (!original_security_ops.file_fcntl) | |
1187 | + smp_rmb(); | |
1188 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1189 | +} | |
1190 | + | |
1191 | +/** | |
1192 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1193 | + * | |
1194 | + * @filp: Pointer to "struct file". | |
1195 | + * @cmd: Command number. | |
1196 | + * @arg: Value for @cmd. | |
1197 | + * | |
1198 | + * Returns 0 on success, negative value otherwise. | |
1199 | + */ | |
1200 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1201 | + unsigned long arg) | |
1202 | +{ | |
1203 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1204 | + if (rc) | |
1205 | + return rc; | |
1206 | + while (!original_security_ops.file_ioctl) | |
1207 | + smp_rmb(); | |
1208 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1209 | +} | |
1210 | + | |
1211 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1212 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1213 | + const struct ccs_path_info *filename); | |
1214 | + | |
1215 | +/** | |
1216 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1217 | + * | |
1218 | + * @buffer: Pointer to "struct char *". | |
1219 | + * @buflen: Pointer to int which holds size of @buffer. | |
1220 | + * @str: String to copy. | |
1221 | + * | |
1222 | + * Returns 0 on success, negative value otherwise. | |
1223 | + * | |
1224 | + * @buffer and @buflen are updated upon success. | |
1225 | + */ | |
1226 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1227 | +{ | |
1228 | + int namelen = strlen(str); | |
1229 | + if (*buflen < namelen) | |
1230 | + return -ENOMEM; | |
1231 | + *buflen -= namelen; | |
1232 | + *buffer -= namelen; | |
1233 | + memcpy(*buffer, str, namelen); | |
1234 | + return 0; | |
1235 | +} | |
1236 | + | |
1237 | +/** | |
1238 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1239 | + * | |
1240 | + * @table: Pointer to "struct ctl_table". | |
1241 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1242 | + * | |
1243 | + * Returns 0 on success, negative value otherwise. | |
1244 | + */ | |
1245 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1246 | +{ | |
1247 | + int error; | |
1248 | + struct ccs_path_info buf; | |
1249 | + struct ccs_request_info r; | |
1250 | + int buflen; | |
1251 | + char *buffer; | |
1252 | + int idx; | |
1253 | + while (!original_security_ops.sysctl) | |
1254 | + smp_rmb(); | |
1255 | + error = original_security_ops.sysctl(table, op); | |
1256 | + if (error) | |
1257 | + return error; | |
1258 | + op &= MAY_READ | MAY_WRITE; | |
1259 | + if (!op) | |
1260 | + return 0; | |
1261 | + buffer = NULL; | |
1262 | + buf.name = NULL; | |
1263 | + idx = ccs_read_lock(); | |
1264 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1265 | + == CCS_CONFIG_DISABLED) | |
1266 | + goto out; | |
1267 | + error = -ENOMEM; | |
1268 | + buflen = 4096; | |
1269 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1270 | + if (buffer) { | |
1271 | + char *end = buffer + buflen; | |
1272 | + *--end = '\0'; | |
1273 | + buflen--; | |
1274 | + while (table) { | |
1275 | + char num[32]; | |
1276 | + const char *sp = table->procname; | |
1277 | + if (!sp) { | |
1278 | + memset(num, 0, sizeof(num)); | |
1279 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1280 | + table->ctl_name); | |
1281 | + sp = num; | |
1282 | + } | |
1283 | + if (ccs_prepend(&end, &buflen, sp) || | |
1284 | + ccs_prepend(&end, &buflen, "/")) | |
1285 | + goto out; | |
1286 | + table = table->parent; | |
1287 | + } | |
1288 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1289 | + goto out; | |
1290 | + buf.name = ccs_encode(end); | |
1291 | + } | |
1292 | + if (buf.name) { | |
1293 | + ccs_fill_path_info(&buf); | |
1294 | + if (op & MAY_READ) | |
1295 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1296 | + else | |
1297 | + error = 0; | |
1298 | + if (!error && (op & MAY_WRITE)) | |
1299 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1300 | + } | |
1301 | +out: | |
1302 | + ccs_read_unlock(idx); | |
1303 | + kfree(buf.name); | |
1304 | + kfree(buffer); | |
1305 | + return error; | |
1306 | +} | |
1307 | + | |
1308 | +#endif | |
1309 | + | |
1310 | +/* | |
1311 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1312 | + * Because copying byte array is not atomic. Reader checks | |
1313 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1314 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1315 | + */ | |
1316 | +#define swap_security_ops(op) \ | |
1317 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1318 | + | |
1319 | +/** | |
1320 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1321 | + * | |
1322 | + * @ops: Pointer to "struct security_operations". | |
1323 | + * | |
1324 | + * Returns nothing. | |
1325 | + */ | |
1326 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1327 | +{ | |
1328 | + /* Security context allocator. */ | |
1329 | + swap_security_ops(task_alloc_security); | |
1330 | + swap_security_ops(task_free_security); | |
1331 | + swap_security_ops(bprm_alloc_security); | |
1332 | + swap_security_ops(bprm_free_security); | |
1333 | + /* Security context updater for successful execve(). */ | |
1334 | + swap_security_ops(bprm_check_security); | |
1335 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 6) | |
1336 | + swap_security_ops(bprm_compute_creds); | |
1337 | +#else | |
1338 | + swap_security_ops(bprm_apply_creds); | |
1339 | +#endif | |
1340 | + /* Various permission checker. */ | |
1341 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1342 | + swap_security_ops(dentry_open); | |
1343 | +#else | |
1344 | + swap_security_ops(inode_permission); | |
1345 | +#endif | |
1346 | + swap_security_ops(file_fcntl); | |
1347 | + swap_security_ops(file_ioctl); | |
1348 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21) && defined(CONFIG_SYSCTL_SYSCALL) | |
1349 | + swap_security_ops(sysctl); | |
1350 | +#endif | |
1351 | + swap_security_ops(sb_pivotroot); | |
1352 | + swap_security_ops(sb_mount); | |
1353 | + swap_security_ops(sb_umount); | |
1354 | + swap_security_ops(inode_mknod); | |
1355 | + swap_security_ops(inode_mkdir); | |
1356 | + swap_security_ops(inode_rmdir); | |
1357 | + swap_security_ops(inode_unlink); | |
1358 | + swap_security_ops(inode_symlink); | |
1359 | + swap_security_ops(inode_rename); | |
1360 | + swap_security_ops(inode_link); | |
1361 | + swap_security_ops(inode_create); | |
1362 | + swap_security_ops(inode_setattr); | |
1363 | + swap_security_ops(inode_getattr); | |
1364 | +#ifdef CONFIG_SECURITY_NETWORK | |
1365 | + swap_security_ops(socket_bind); | |
1366 | + swap_security_ops(socket_connect); | |
1367 | + swap_security_ops(socket_listen); | |
1368 | + swap_security_ops(socket_sendmsg); | |
1369 | + swap_security_ops(socket_recvmsg); | |
1370 | + swap_security_ops(socket_getsockname); | |
1371 | + swap_security_ops(socket_getpeername); | |
1372 | + swap_security_ops(socket_getsockopt); | |
1373 | + swap_security_ops(socket_setsockopt); | |
1374 | + swap_security_ops(socket_shutdown); | |
1375 | + swap_security_ops(socket_accept); | |
1376 | + swap_security_ops(inode_free_security); | |
1377 | +#endif | |
1378 | +} | |
1379 | + | |
1380 | +#undef swap_security_ops | |
1381 | + | |
1382 | +/** | |
1383 | + * ccs_init - Initialize this module. | |
1384 | + * | |
1385 | + * Returns 0 on success, negative value otherwise. | |
1386 | + */ | |
1387 | +static int __init ccs_init(void) | |
1388 | +{ | |
1389 | + struct security_operations *ops = probe_security_ops(); | |
1390 | + if (!ops) | |
1391 | + goto out; | |
1392 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1393 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1394 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1395 | + goto out; | |
1396 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1397 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1398 | + goto out; | |
1399 | +#endif | |
1400 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1401 | + if (!ccsecurity_exports.vfsmount_lock) | |
1402 | + goto out; | |
1403 | + ccs_main_init(); | |
1404 | + ccs_update_security_ops(ops); | |
1405 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1406 | + printk(KERN_INFO | |
1407 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1408 | + return 0; | |
1409 | +out: | |
1410 | + return -EINVAL; | |
1411 | +} | |
1412 | + | |
1413 | +module_init(ccs_init); | |
1414 | +MODULE_LICENSE("GPL"); | |
1415 | + | |
1416 | +/** | |
1417 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1418 | + * | |
1419 | + * @domain: Pointer to "struct ccs_domain_info". | |
1420 | + * | |
1421 | + * Returns true if @domain is in use, false otherwise. | |
1422 | + * | |
1423 | + * Caller holds rcu_read_lock(). | |
1424 | + */ | |
1425 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1426 | +{ | |
1427 | + return false; | |
1428 | +} | |
1429 | + | |
1430 | +/** | |
1431 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1432 | + * | |
1433 | + * @ptr: Pointer to "struct ccs_security". | |
1434 | + * @list: Pointer to "struct list_head". | |
1435 | + * | |
1436 | + * Returns nothing. | |
1437 | + */ | |
1438 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1439 | + struct list_head *list) | |
1440 | +{ | |
1441 | + unsigned long flags; | |
1442 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1443 | + list_add_rcu(&ptr->list, list); | |
1444 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1445 | +} | |
1446 | + | |
1447 | +/** | |
1448 | + * __ccs_alloc_task_security - Allocate memory for new tasks. | |
1449 | + * | |
1450 | + * @task: Pointer to "struct task_struct". | |
1451 | + * | |
1452 | + * Returns 0 on success, negative value otherwise. | |
1453 | + */ | |
1454 | +static int __ccs_alloc_task_security(const struct task_struct *task) | |
1455 | +{ | |
1456 | + struct ccs_security *old_security = ccs_current_security(); | |
1457 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
1458 | + GFP_KERNEL); | |
1459 | + struct list_head *list = &ccs_task_security_list | |
1460 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1461 | + if (!new_security) | |
1462 | + return -ENOMEM; | |
1463 | + new_security->task = task; | |
1464 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
1465 | + new_security->ccs_flags = old_security->ccs_flags; | |
1466 | + ccs_add_task_security(new_security, list); | |
1467 | + return 0; | |
1468 | +} | |
1469 | + | |
1470 | +/** | |
1471 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1472 | + * | |
1473 | + * @task: Pointer to "struct task_struct". | |
1474 | + * | |
1475 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1476 | + * out of memory, &ccs_default_security otherwise. | |
1477 | + * | |
1478 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1479 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1480 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1481 | + * won't work. | |
1482 | + */ | |
1483 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1484 | +{ | |
1485 | + struct ccs_security *ptr; | |
1486 | + struct list_head *list = &ccs_task_security_list | |
1487 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1488 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1489 | + while (!list->next); | |
1490 | + rcu_read_lock(); | |
1491 | + list_for_each_entry_rcu(ptr, list, list) { | |
1492 | + if (ptr->task != task) | |
1493 | + continue; | |
1494 | + rcu_read_unlock(); | |
1495 | + return ptr; | |
1496 | + } | |
1497 | + rcu_read_unlock(); | |
1498 | + if (task != current) | |
1499 | + return &ccs_default_security; | |
1500 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1501 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1502 | + if (!ptr) { | |
1503 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1504 | + task->pid); | |
1505 | + send_sig(SIGKILL, current, 0); | |
1506 | + return &ccs_oom_security; | |
1507 | + } | |
1508 | + *ptr = ccs_default_security; | |
1509 | + ptr->task = task; | |
1510 | + ccs_add_task_security(ptr, list); | |
1511 | + return ptr; | |
1512 | +} | |
1513 | + | |
1514 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
1515 | + | |
1516 | +/** | |
1517 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
1518 | + * | |
1519 | + * @rcu: Pointer to "struct rcu_head". | |
1520 | + * | |
1521 | + * Returns nothing. | |
1522 | + */ | |
1523 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
1524 | +{ | |
1525 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
1526 | + kfree(ptr); | |
1527 | +} | |
1528 | + | |
1529 | +#else | |
1530 | + | |
1531 | +/** | |
1532 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
1533 | + * | |
1534 | + * @arg: Pointer to "void". | |
1535 | + * | |
1536 | + * Returns nothing. | |
1537 | + */ | |
1538 | +static void ccs_rcu_free(void *arg) | |
1539 | +{ | |
1540 | + struct ccs_security *ptr = arg; | |
1541 | + kfree(ptr); | |
1542 | +} | |
1543 | + | |
1544 | +#endif | |
1545 | + | |
1546 | +/** | |
1547 | + * __ccs_free_task_security - Release memory associated with "struct task_struct". | |
1548 | + * | |
1549 | + * @task: Pointer to "struct task_struct". | |
1550 | + * | |
1551 | + * Returns nothing. | |
1552 | + */ | |
1553 | +static void __ccs_free_task_security(const struct task_struct *task) | |
1554 | +{ | |
1555 | + unsigned long flags; | |
1556 | + struct ccs_security *ptr = ccs_find_task_security(task); | |
1557 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
1558 | + return; | |
1559 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1560 | + list_del_rcu(&ptr->list); | |
1561 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1562 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
1563 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
1564 | +#else | |
1565 | + call_rcu(&ptr->rcu, ccs_rcu_free, ptr); | |
1566 | +#endif | |
1567 | +} |
@@ -0,0 +1,2245 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
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 | + smp_rmb(); | |
266 | + rc = original_security_ops.task_create(clone_flags); | |
267 | + if (rc) { | |
268 | + abort_creds(cred); | |
269 | + return rc; | |
270 | + } | |
271 | + old_security = ccs_find_task_security(current); | |
272 | + new_security = ccs_find_cred_security(cred); | |
273 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
274 | + new_security->ccs_flags = old_security->ccs_flags; | |
275 | + return commit_creds(cred); | |
276 | +} | |
277 | + | |
278 | +/** | |
279 | + * ccs_cred_prepare - Allocate memory for new credentials. | |
280 | + * | |
281 | + * @new: Pointer to "struct cred". | |
282 | + * @old: Pointer to "struct cred". | |
283 | + * @gfp: Memory allocation flags. | |
284 | + * | |
285 | + * Returns 0 on success, negative value otherwise. | |
286 | + */ | |
287 | +static int ccs_cred_prepare(struct cred *new, const struct cred *old, | |
288 | + gfp_t gfp) | |
289 | +{ | |
290 | + int rc; | |
291 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
292 | + /* | |
293 | + * For checking whether reverting domain transition is needed or not. | |
294 | + * | |
295 | + * See ccs_find_task_security() for reason. | |
296 | + */ | |
297 | + if (gfp == GFP_KERNEL) | |
298 | + ccs_find_task_security(current); | |
299 | +#endif | |
300 | + rc = ccs_copy_cred_security(new, old, gfp); | |
301 | + if (rc) | |
302 | + return rc; | |
303 | + if (gfp == GFP_KERNEL) | |
304 | + ccs_task_security_gc(); | |
305 | + while (!original_security_ops.cred_prepare) | |
306 | + smp_rmb(); | |
307 | + rc = original_security_ops.cred_prepare(new, old, gfp); | |
308 | + if (rc) | |
309 | + ccs_del_security(ccs_find_cred_security(new)); | |
310 | + return rc; | |
311 | +} | |
312 | + | |
313 | +/** | |
314 | + * ccs_cred_free - Release memory used by credentials. | |
315 | + * | |
316 | + * @cred: Pointer to "struct cred". | |
317 | + * | |
318 | + * Returns nothing. | |
319 | + */ | |
320 | +static void ccs_cred_free(struct cred *cred) | |
321 | +{ | |
322 | + while (!original_security_ops.cred_free) | |
323 | + smp_rmb(); | |
324 | + original_security_ops.cred_free(cred); | |
325 | + ccs_del_security(ccs_find_cred_security(cred)); | |
326 | +} | |
327 | + | |
328 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
329 | + | |
330 | +/** | |
331 | + * ccs_alloc_cred_security - Allocate memory for new credentials. | |
332 | + * | |
333 | + * @cred: Pointer to "struct cred". | |
334 | + * @gfp: Memory allocation flags. | |
335 | + * | |
336 | + * Returns 0 on success, negative value otherwise. | |
337 | + */ | |
338 | +static int ccs_alloc_cred_security(const struct cred *cred, gfp_t gfp) | |
339 | +{ | |
340 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
341 | + gfp); | |
342 | + if (!new_security) | |
343 | + return -ENOMEM; | |
344 | + new_security->cred = cred; | |
345 | + ccs_add_cred_security(new_security); | |
346 | + return 0; | |
347 | +} | |
348 | + | |
349 | +/** | |
350 | + * ccs_cred_alloc_blank - Allocate memory for new credentials. | |
351 | + * | |
352 | + * @new: Pointer to "struct cred". | |
353 | + * @gfp: Memory allocation flags. | |
354 | + * | |
355 | + * Returns 0 on success, negative value otherwise. | |
356 | + */ | |
357 | +static int ccs_cred_alloc_blank(struct cred *new, gfp_t gfp) | |
358 | +{ | |
359 | + int rc = ccs_alloc_cred_security(new, gfp); | |
360 | + if (rc) | |
361 | + return rc; | |
362 | + while (!original_security_ops.cred_alloc_blank) | |
363 | + smp_rmb(); | |
364 | + rc = original_security_ops.cred_alloc_blank(new, gfp); | |
365 | + if (rc) | |
366 | + ccs_del_security(ccs_find_cred_security(new)); | |
367 | + return rc; | |
368 | +} | |
369 | + | |
370 | +/** | |
371 | + * ccs_cred_transfer - Transfer "struct ccs_security" between credentials. | |
372 | + * | |
373 | + * @new: Pointer to "struct cred". | |
374 | + * @old: Pointer to "struct cred". | |
375 | + * | |
376 | + * Returns nothing. | |
377 | + */ | |
378 | +static void ccs_cred_transfer(struct cred *new, const struct cred *old) | |
379 | +{ | |
380 | + struct ccs_security *new_security; | |
381 | + struct ccs_security *old_security; | |
382 | + while (!original_security_ops.cred_transfer) | |
383 | + smp_rmb(); | |
384 | + original_security_ops.cred_transfer(new, old); | |
385 | + new_security = ccs_find_cred_security(new); | |
386 | + old_security = ccs_find_cred_security(old); | |
387 | + if (new_security == &ccs_default_security || | |
388 | + new_security == &ccs_oom_security || | |
389 | + old_security == &ccs_default_security || | |
390 | + old_security == &ccs_oom_security) | |
391 | + return; | |
392 | + new_security->ccs_flags = old_security->ccs_flags; | |
393 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
394 | +} | |
395 | + | |
396 | +#endif | |
397 | + | |
398 | +/** | |
399 | + * ccs_bprm_committing_creds - A hook which is called when do_execve() succeeded. | |
400 | + * | |
401 | + * @bprm: Pointer to "struct linux_binprm". | |
402 | + * | |
403 | + * Returns nothing. | |
404 | + */ | |
405 | +static void ccs_bprm_committing_creds(struct linux_binprm *bprm) | |
406 | +{ | |
407 | + struct ccs_security *old_security; | |
408 | + struct ccs_security *new_security; | |
409 | + while (!original_security_ops.bprm_committing_creds) | |
410 | + smp_rmb(); | |
411 | + original_security_ops.bprm_committing_creds(bprm); | |
412 | + old_security = ccs_current_security(); | |
413 | + if (old_security == &ccs_default_security || | |
414 | + old_security == &ccs_oom_security) | |
415 | + return; | |
416 | + ccs_clear_execve(0, old_security); | |
417 | + /* Update current task's cred's domain for future fork(). */ | |
418 | + new_security = ccs_find_cred_security(bprm->cred); | |
419 | + new_security->ccs_flags = old_security->ccs_flags; | |
420 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
421 | +} | |
422 | + | |
423 | +/** | |
424 | + * ccs_bprm_check_security - Check permission for execve(). | |
425 | + * | |
426 | + * @bprm: Pointer to "struct linux_binprm". | |
427 | + * | |
428 | + * Returns 0 on success, negative value otherwise. | |
429 | + */ | |
430 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
431 | +{ | |
432 | + struct ccs_security *security = ccs_current_security(); | |
433 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
434 | + return -ENOMEM; | |
435 | + if (!security->ee) { | |
436 | + int rc; | |
437 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
438 | + if (!ccs_policy_loaded) | |
439 | + ccs_load_policy(bprm->filename); | |
440 | +#endif | |
441 | + rc = ccs_start_execve(bprm, &security->ee); | |
442 | + if (security->ee) { | |
443 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) | |
444 | + /* | |
445 | + * Get refcount on "struct cred" in | |
446 | + * "struct linux_binprm" and remember it. | |
447 | + */ | |
448 | + get_cred(bprm->cred); | |
449 | + security->cred = bprm->cred; | |
450 | +#endif | |
451 | + atomic_inc(&ccs_in_execve_tasks); | |
452 | + } | |
453 | + if (rc) | |
454 | + return rc; | |
455 | + } | |
456 | + while (!original_security_ops.bprm_check_security) | |
457 | + smp_rmb(); | |
458 | + return original_security_ops.bprm_check_security(bprm); | |
459 | +} | |
460 | + | |
461 | +/** | |
462 | + * ccs_open - Check permission for open(). | |
463 | + * | |
464 | + * @f: Pointer to "struct file". | |
465 | + * | |
466 | + * Returns 0 on success, negative value otherwise. | |
467 | + */ | |
468 | +static int ccs_open(struct file *f) | |
469 | +{ | |
470 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
471 | + return ccs_open_permission(f); | |
472 | +#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 6 | |
473 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
474 | + f->f_flags); | |
475 | +#else | |
476 | + return ccs_open_permission(f->f_path.dentry, f->f_path.mnt, | |
477 | + f->f_flags + 1); | |
478 | +#endif | |
479 | +} | |
480 | + | |
481 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
482 | + | |
483 | +/** | |
484 | + * ccs_file_open - Check permission for open(). | |
485 | + * | |
486 | + * @f: Pointer to "struct file". | |
487 | + * @cred: Pointer to "struct cred". | |
488 | + * | |
489 | + * Returns 0 on success, negative value otherwise. | |
490 | + */ | |
491 | +static int ccs_file_open(struct file *f, const struct cred *cred) | |
492 | +{ | |
493 | + int rc = ccs_open(f); | |
494 | + if (rc) | |
495 | + return rc; | |
496 | + while (!original_security_ops.file_open) | |
497 | + smp_rmb(); | |
498 | + return original_security_ops.file_open(f, cred); | |
499 | +} | |
500 | + | |
501 | +#else | |
502 | + | |
503 | +/** | |
504 | + * ccs_dentry_open - Check permission for open(). | |
505 | + * | |
506 | + * @f: Pointer to "struct file". | |
507 | + * @cred: Pointer to "struct cred". | |
508 | + * | |
509 | + * Returns 0 on success, negative value otherwise. | |
510 | + */ | |
511 | +static int ccs_dentry_open(struct file *f, const struct cred *cred) | |
512 | +{ | |
513 | + int rc = ccs_open(f); | |
514 | + if (rc) | |
515 | + return rc; | |
516 | + while (!original_security_ops.dentry_open) | |
517 | + smp_rmb(); | |
518 | + return original_security_ops.dentry_open(f, cred); | |
519 | +} | |
520 | + | |
521 | +#endif | |
522 | + | |
523 | +#if defined(CONFIG_SECURITY_PATH) | |
524 | + | |
525 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
526 | + | |
527 | +/** | |
528 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
529 | + * | |
530 | + * @path: Pointer to "struct path". | |
531 | + * @user: User ID. | |
532 | + * @group: Group ID. | |
533 | + * | |
534 | + * Returns 0 on success, negative value otherwise. | |
535 | + */ | |
536 | +static int ccs_path_chown(struct path *path, kuid_t user, kgid_t group) | |
537 | +{ | |
538 | + int rc = ccs_chown_permission(path->dentry, path->mnt, user, group); | |
539 | + if (rc) | |
540 | + return rc; | |
541 | + while (!original_security_ops.path_chown) | |
542 | + smp_rmb(); | |
543 | + return original_security_ops.path_chown(path, user, group); | |
544 | +} | |
545 | + | |
546 | +/** | |
547 | + * ccs_path_chmod - Check permission for chmod(). | |
548 | + * | |
549 | + * @path: Pointer to "struct path". | |
550 | + * @mode: Mode. | |
551 | + * | |
552 | + * Returns 0 on success, negative value otherwise. | |
553 | + */ | |
554 | +static int ccs_path_chmod(struct path *path, umode_t mode) | |
555 | +{ | |
556 | + int rc = ccs_chmod_permission(path->dentry, path->mnt, mode); | |
557 | + if (rc) | |
558 | + return rc; | |
559 | + while (!original_security_ops.path_chmod) | |
560 | + smp_rmb(); | |
561 | + return original_security_ops.path_chmod(path, mode); | |
562 | +} | |
563 | + | |
564 | +/** | |
565 | + * ccs_path_chroot - Check permission for chroot(). | |
566 | + * | |
567 | + * @path: Pointer to "struct path". | |
568 | + * | |
569 | + * Returns 0 on success, negative value otherwise. | |
570 | + */ | |
571 | +static int ccs_path_chroot(struct path *path) | |
572 | +{ | |
573 | + int rc = ccs_chroot_permission(path); | |
574 | + if (rc) | |
575 | + return rc; | |
576 | + while (!original_security_ops.path_chroot) | |
577 | + smp_rmb(); | |
578 | + return original_security_ops.path_chroot(path); | |
579 | +} | |
580 | + | |
581 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
582 | + | |
583 | +/** | |
584 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
585 | + * | |
586 | + * @path: Pointer to "struct path". | |
587 | + * @user: User ID. | |
588 | + * @group: Group ID. | |
589 | + * | |
590 | + * Returns 0 on success, negative value otherwise. | |
591 | + */ | |
592 | +static int ccs_path_chown(struct path *path, uid_t user, gid_t group) | |
593 | +{ | |
594 | + int rc = ccs_chown_permission(path->dentry, path->mnt, user, group); | |
595 | + if (rc) | |
596 | + return rc; | |
597 | + while (!original_security_ops.path_chown) | |
598 | + smp_rmb(); | |
599 | + return original_security_ops.path_chown(path, user, group); | |
600 | +} | |
601 | + | |
602 | +#if defined(USE_UMODE_T) | |
603 | + | |
604 | +/** | |
605 | + * ccs_path_chmod - Check permission for chmod(). | |
606 | + * | |
607 | + * @path: Pointer to "struct path". | |
608 | + * @mode: Mode. | |
609 | + * | |
610 | + * Returns 0 on success, negative value otherwise. | |
611 | + */ | |
612 | +static int ccs_path_chmod(struct path *path, umode_t mode) | |
613 | +{ | |
614 | + int rc = ccs_chmod_permission(path->dentry, path->mnt, mode); | |
615 | + if (rc) | |
616 | + return rc; | |
617 | + while (!original_security_ops.path_chmod) | |
618 | + smp_rmb(); | |
619 | + return original_security_ops.path_chmod(path, mode); | |
620 | +} | |
621 | + | |
622 | +#else | |
623 | + | |
624 | +/** | |
625 | + * ccs_path_chmod - Check permission for chmod(). | |
626 | + * | |
627 | + * @dentry: Pointer to "struct dentry". | |
628 | + * @vfsmnt: Pointer to "struct vfsmount". | |
629 | + * @mode: Mode. | |
630 | + * | |
631 | + * Returns 0 on success, negative value otherwise. | |
632 | + */ | |
633 | +static int ccs_path_chmod(struct dentry *dentry, struct vfsmount *vfsmnt, | |
634 | + mode_t mode) | |
635 | +{ | |
636 | + int rc = ccs_chmod_permission(dentry, vfsmnt, mode); | |
637 | + if (rc) | |
638 | + return rc; | |
639 | + while (!original_security_ops.path_chmod) | |
640 | + smp_rmb(); | |
641 | + return original_security_ops.path_chmod(dentry, vfsmnt, mode); | |
642 | +} | |
643 | + | |
644 | +#endif | |
645 | + | |
646 | +/** | |
647 | + * ccs_path_chroot - Check permission for chroot(). | |
648 | + * | |
649 | + * @path: Pointer to "struct path". | |
650 | + * | |
651 | + * Returns 0 on success, negative value otherwise. | |
652 | + */ | |
653 | +static int ccs_path_chroot(struct path *path) | |
654 | +{ | |
655 | + int rc = ccs_chroot_permission(path); | |
656 | + if (rc) | |
657 | + return rc; | |
658 | + while (!original_security_ops.path_chroot) | |
659 | + smp_rmb(); | |
660 | + return original_security_ops.path_chroot(path); | |
661 | +} | |
662 | + | |
663 | +#endif | |
664 | + | |
665 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) | |
666 | + | |
667 | +/** | |
668 | + * ccs_path_truncate - Check permission for truncate(). | |
669 | + * | |
670 | + * @path: Pointer to "struct path". | |
671 | + * | |
672 | + * Returns 0 on success, negative value otherwise. | |
673 | + */ | |
674 | +static int ccs_path_truncate(struct path *path) | |
675 | +{ | |
676 | + int rc = ccs_truncate_permission(path->dentry, path->mnt); | |
677 | + if (rc) | |
678 | + return rc; | |
679 | + while (!original_security_ops.path_truncate) | |
680 | + smp_rmb(); | |
681 | + return original_security_ops.path_truncate(path); | |
682 | +} | |
683 | + | |
684 | +#else | |
685 | + | |
686 | +/** | |
687 | + * ccs_path_truncate - Check permission for truncate(). | |
688 | + * | |
689 | + * @path: Pointer to "struct path". | |
690 | + * @length: New length. | |
691 | + * @time_attrs: New time attributes. | |
692 | + * | |
693 | + * Returns 0 on success, negative value otherwise. | |
694 | + */ | |
695 | +static int ccs_path_truncate(struct path *path, loff_t length, | |
696 | + unsigned int time_attrs) | |
697 | +{ | |
698 | + int rc = ccs_truncate_permission(path->dentry, path->mnt); | |
699 | + if (rc) | |
700 | + return rc; | |
701 | + while (!original_security_ops.path_truncate) | |
702 | + smp_rmb(); | |
703 | + return original_security_ops.path_truncate(path, length, time_attrs); | |
704 | +} | |
705 | + | |
706 | +#endif | |
707 | + | |
708 | +#endif | |
709 | + | |
710 | +/** | |
711 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
712 | + * | |
713 | + * @dentry: Pointer to "struct dentry". | |
714 | + * @attr: Pointer to "struct iattr". | |
715 | + * | |
716 | + * Returns 0 on success, negative value otherwise. | |
717 | + */ | |
718 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
719 | +{ | |
720 | + int rc = 0; | |
721 | +#if !defined(CONFIG_SECURITY_PATH) || LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) | |
722 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
723 | + if (attr->ia_valid & ATTR_UID) | |
724 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, | |
725 | + INVALID_GID); | |
726 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
727 | + rc = ccs_chown_permission(dentry, NULL, INVALID_UID, | |
728 | + attr->ia_gid); | |
729 | +#else | |
730 | + if (attr->ia_valid & ATTR_UID) | |
731 | + rc = ccs_chown_permission(dentry, NULL, attr->ia_uid, -1); | |
732 | + if (!rc && (attr->ia_valid & ATTR_GID)) | |
733 | + rc = ccs_chown_permission(dentry, NULL, -1, attr->ia_gid); | |
734 | +#endif | |
735 | + if (!rc && (attr->ia_valid & ATTR_MODE)) | |
736 | + rc = ccs_chmod_permission(dentry, NULL, attr->ia_mode); | |
737 | +#endif | |
738 | +#if !defined(CONFIG_SECURITY_PATH) | |
739 | + if (!rc && (attr->ia_valid & ATTR_SIZE)) | |
740 | + rc = ccs_truncate_permission(dentry, NULL); | |
741 | +#endif | |
742 | + if (rc) | |
743 | + return rc; | |
744 | + while (!original_security_ops.inode_setattr) | |
745 | + smp_rmb(); | |
746 | + return original_security_ops.inode_setattr(dentry, attr); | |
747 | +} | |
748 | + | |
749 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) | |
750 | + | |
751 | +/** | |
752 | + * ccs_inode_getattr - Check permission for stat(). | |
753 | + * | |
754 | + * @path: Pointer to "struct path". | |
755 | + * | |
756 | + * Returns 0 on success, negative value otherwise. | |
757 | + */ | |
758 | +static int ccs_inode_getattr(const struct path *path) | |
759 | +{ | |
760 | + int rc = ccs_getattr_permission(path->mnt, path->dentry); | |
761 | + if (rc) | |
762 | + return rc; | |
763 | + while (!original_security_ops.inode_getattr) | |
764 | + smp_rmb(); | |
765 | + return original_security_ops.inode_getattr(path); | |
766 | +} | |
767 | + | |
768 | +#else | |
769 | + | |
770 | +/** | |
771 | + * ccs_inode_getattr - Check permission for stat(). | |
772 | + * | |
773 | + * @mnt: Pointer to "struct vfsmount". | |
774 | + * @dentry: Pointer to "struct dentry". | |
775 | + * | |
776 | + * Returns 0 on success, negative value otherwise. | |
777 | + */ | |
778 | +static int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
779 | +{ | |
780 | + int rc = ccs_getattr_permission(mnt, dentry); | |
781 | + if (rc) | |
782 | + return rc; | |
783 | + while (!original_security_ops.inode_getattr) | |
784 | + smp_rmb(); | |
785 | + return original_security_ops.inode_getattr(mnt, dentry); | |
786 | +} | |
787 | + | |
788 | +#endif | |
789 | + | |
790 | +#if defined(CONFIG_SECURITY_PATH) | |
791 | + | |
792 | +#if defined(USE_UMODE_T) | |
793 | + | |
794 | +/** | |
795 | + * ccs_path_mknod - Check permission for mknod(). | |
796 | + * | |
797 | + * @dir: Pointer to "struct path". | |
798 | + * @dentry: Pointer to "struct dentry". | |
799 | + * @mode: Create mode. | |
800 | + * @dev: Device major/minor number. | |
801 | + * | |
802 | + * Returns 0 on success, negative value otherwise. | |
803 | + */ | |
804 | +static int ccs_path_mknod(struct path *dir, struct dentry *dentry, | |
805 | + umode_t mode, unsigned int dev) | |
806 | +{ | |
807 | + int rc = ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
808 | + if (rc) | |
809 | + return rc; | |
810 | + while (!original_security_ops.path_mknod) | |
811 | + smp_rmb(); | |
812 | + return original_security_ops.path_mknod(dir, dentry, mode, dev); | |
813 | +} | |
814 | + | |
815 | +/** | |
816 | + * ccs_path_mkdir - Check permission for mkdir(). | |
817 | + * | |
818 | + * @dir: Pointer to "struct path". | |
819 | + * @dentry: Pointer to "struct dentry". | |
820 | + * @mode: Create mode. | |
821 | + * | |
822 | + * Returns 0 on success, negative value otherwise. | |
823 | + */ | |
824 | +static int ccs_path_mkdir(struct path *dir, struct dentry *dentry, | |
825 | + umode_t mode) | |
826 | +{ | |
827 | + int rc = ccs_mkdir_permission(dentry, dir->mnt, mode); | |
828 | + if (rc) | |
829 | + return rc; | |
830 | + while (!original_security_ops.path_mkdir) | |
831 | + smp_rmb(); | |
832 | + return original_security_ops.path_mkdir(dir, dentry, mode); | |
833 | +} | |
834 | + | |
835 | +#else | |
836 | + | |
837 | +/** | |
838 | + * ccs_path_mknod - Check permission for mknod(). | |
839 | + * | |
840 | + * @dir: Pointer to "struct path". | |
841 | + * @dentry: Pointer to "struct dentry". | |
842 | + * @mode: Create mode. | |
843 | + * @dev: Device major/minor number. | |
844 | + * | |
845 | + * Returns 0 on success, negative value otherwise. | |
846 | + */ | |
847 | +static int ccs_path_mknod(struct path *dir, struct dentry *dentry, int mode, | |
848 | + unsigned int dev) | |
849 | +{ | |
850 | + int rc = ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
851 | + if (rc) | |
852 | + return rc; | |
853 | + while (!original_security_ops.path_mknod) | |
854 | + smp_rmb(); | |
855 | + return original_security_ops.path_mknod(dir, dentry, mode, dev); | |
856 | +} | |
857 | + | |
858 | +/** | |
859 | + * ccs_path_mkdir - Check permission for mkdir(). | |
860 | + * | |
861 | + * @dir: Pointer to "struct path". | |
862 | + * @dentry: Pointer to "struct dentry". | |
863 | + * @mode: Create mode. | |
864 | + * | |
865 | + * Returns 0 on success, negative value otherwise. | |
866 | + */ | |
867 | +static int ccs_path_mkdir(struct path *dir, struct dentry *dentry, int mode) | |
868 | +{ | |
869 | + int rc = ccs_mkdir_permission(dentry, dir->mnt, mode); | |
870 | + if (rc) | |
871 | + return rc; | |
872 | + while (!original_security_ops.path_mkdir) | |
873 | + smp_rmb(); | |
874 | + return original_security_ops.path_mkdir(dir, dentry, mode); | |
875 | +} | |
876 | + | |
877 | +#endif | |
878 | + | |
879 | +/** | |
880 | + * ccs_path_rmdir - Check permission for rmdir(). | |
881 | + * | |
882 | + * @dir: Pointer to "struct path". | |
883 | + * @dentry: Pointer to "struct dentry". | |
884 | + * | |
885 | + * Returns 0 on success, negative value otherwise. | |
886 | + */ | |
887 | +static int ccs_path_rmdir(struct path *dir, struct dentry *dentry) | |
888 | +{ | |
889 | + int rc = ccs_rmdir_permission(dentry, dir->mnt); | |
890 | + if (rc) | |
891 | + return rc; | |
892 | + while (!original_security_ops.path_rmdir) | |
893 | + smp_rmb(); | |
894 | + return original_security_ops.path_rmdir(dir, dentry); | |
895 | +} | |
896 | + | |
897 | +/** | |
898 | + * ccs_path_unlink - Check permission for unlink(). | |
899 | + * | |
900 | + * @dir: Pointer to "struct path". | |
901 | + * @dentry: Pointer to "struct dentry". | |
902 | + * | |
903 | + * Returns 0 on success, negative value otherwise. | |
904 | + */ | |
905 | +static int ccs_path_unlink(struct path *dir, struct dentry *dentry) | |
906 | +{ | |
907 | + int rc = ccs_unlink_permission(dentry, dir->mnt); | |
908 | + if (rc) | |
909 | + return rc; | |
910 | + while (!original_security_ops.path_unlink) | |
911 | + smp_rmb(); | |
912 | + return original_security_ops.path_unlink(dir, dentry); | |
913 | +} | |
914 | + | |
915 | +/** | |
916 | + * ccs_path_symlink - Check permission for symlink(). | |
917 | + * | |
918 | + * @dir: Pointer to "struct path". | |
919 | + * @dentry: Pointer to "struct dentry". | |
920 | + * @old_name: Content of symbolic link. | |
921 | + * | |
922 | + * Returns 0 on success, negative value otherwise. | |
923 | + */ | |
924 | +static int ccs_path_symlink(struct path *dir, struct dentry *dentry, | |
925 | + const char *old_name) | |
926 | +{ | |
927 | + int rc = ccs_symlink_permission(dentry, dir->mnt, old_name); | |
928 | + if (rc) | |
929 | + return rc; | |
930 | + while (!original_security_ops.path_symlink) | |
931 | + smp_rmb(); | |
932 | + return original_security_ops.path_symlink(dir, dentry, old_name); | |
933 | +} | |
934 | + | |
935 | +/** | |
936 | + * ccs_path_rename - Check permission for rename(). | |
937 | + * | |
938 | + * @old_dir: Pointer to "struct path". | |
939 | + * @old_dentry: Pointer to "struct dentry". | |
940 | + * @new_dir: Pointer to "struct path". | |
941 | + * @new_dentry: Pointer to "struct dentry". | |
942 | + * | |
943 | + * Returns 0 on success, negative value otherwise. | |
944 | + */ | |
945 | +static int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry, | |
946 | + struct path *new_dir, struct dentry *new_dentry) | |
947 | +{ | |
948 | + int rc = ccs_rename_permission(old_dentry, new_dentry, old_dir->mnt); | |
949 | + if (rc) | |
950 | + return rc; | |
951 | + while (!original_security_ops.path_rename) | |
952 | + smp_rmb(); | |
953 | + return original_security_ops.path_rename(old_dir, old_dentry, new_dir, | |
954 | + new_dentry); | |
955 | +} | |
956 | + | |
957 | +/** | |
958 | + * ccs_path_link - Check permission for link(). | |
959 | + * | |
960 | + * @old_dentry: Pointer to "struct dentry". | |
961 | + * @new_dir: Pointer to "struct path". | |
962 | + * @new_dentry: Pointer to "struct dentry". | |
963 | + * | |
964 | + * Returns 0 on success, negative value otherwise. | |
965 | + */ | |
966 | +static int ccs_path_link(struct dentry *old_dentry, struct path *new_dir, | |
967 | + struct dentry *new_dentry) | |
968 | +{ | |
969 | + int rc = ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); | |
970 | + if (rc) | |
971 | + return rc; | |
972 | + while (!original_security_ops.path_link) | |
973 | + smp_rmb(); | |
974 | + return original_security_ops.path_link(old_dentry, new_dir, | |
975 | + new_dentry); | |
976 | +} | |
977 | + | |
978 | +#else | |
979 | + | |
980 | +#if defined(USE_UMODE_T) | |
981 | + | |
982 | +/** | |
983 | + * ccs_inode_mknod - Check permission for mknod(). | |
984 | + * | |
985 | + * @dir: Pointer to "struct inode". | |
986 | + * @dentry: Pointer to "struct dentry". | |
987 | + * @mode: Create mode. | |
988 | + * @dev: Device major/minor number. | |
989 | + * | |
990 | + * Returns 0 on success, negative value otherwise. | |
991 | + */ | |
992 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
993 | + umode_t mode, dev_t dev) | |
994 | +{ | |
995 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
996 | + if (rc) | |
997 | + return rc; | |
998 | + while (!original_security_ops.inode_mknod) | |
999 | + smp_rmb(); | |
1000 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
1001 | +} | |
1002 | + | |
1003 | +/** | |
1004 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
1005 | + * | |
1006 | + * @dir: Pointer to "struct inode". | |
1007 | + * @dentry: Pointer to "struct dentry". | |
1008 | + * @mode: Create mode. | |
1009 | + * | |
1010 | + * Returns 0 on success, negative value otherwise. | |
1011 | + */ | |
1012 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
1013 | + umode_t mode) | |
1014 | +{ | |
1015 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
1016 | + if (rc) | |
1017 | + return rc; | |
1018 | + while (!original_security_ops.inode_mkdir) | |
1019 | + smp_rmb(); | |
1020 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
1021 | +} | |
1022 | + | |
1023 | +#else | |
1024 | + | |
1025 | +/** | |
1026 | + * ccs_inode_mknod - Check permission for mknod(). | |
1027 | + * | |
1028 | + * @dir: Pointer to "struct inode". | |
1029 | + * @dentry: Pointer to "struct dentry". | |
1030 | + * @mode: Create mode. | |
1031 | + * @dev: Device major/minor number. | |
1032 | + * | |
1033 | + * Returns 0 on success, negative value otherwise. | |
1034 | + */ | |
1035 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
1036 | + dev_t dev) | |
1037 | +{ | |
1038 | + int rc = ccs_mknod_permission(dentry, NULL, mode, dev); | |
1039 | + if (rc) | |
1040 | + return rc; | |
1041 | + while (!original_security_ops.inode_mknod) | |
1042 | + smp_rmb(); | |
1043 | + return original_security_ops.inode_mknod(dir, dentry, mode, dev); | |
1044 | +} | |
1045 | + | |
1046 | +/** | |
1047 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
1048 | + * | |
1049 | + * @dir: Pointer to "struct inode". | |
1050 | + * @dentry: Pointer to "struct dentry". | |
1051 | + * @mode: Create mode. | |
1052 | + * | |
1053 | + * Returns 0 on success, negative value otherwise. | |
1054 | + */ | |
1055 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1056 | +{ | |
1057 | + int rc = ccs_mkdir_permission(dentry, NULL, mode); | |
1058 | + if (rc) | |
1059 | + return rc; | |
1060 | + while (!original_security_ops.inode_mkdir) | |
1061 | + smp_rmb(); | |
1062 | + return original_security_ops.inode_mkdir(dir, dentry, mode); | |
1063 | +} | |
1064 | + | |
1065 | +#endif | |
1066 | + | |
1067 | +/** | |
1068 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
1069 | + * | |
1070 | + * @dir: Pointer to "struct inode". | |
1071 | + * @dentry: Pointer to "struct dentry". | |
1072 | + * | |
1073 | + * Returns 0 on success, negative value otherwise. | |
1074 | + */ | |
1075 | +static int ccs_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
1076 | +{ | |
1077 | + int rc = ccs_rmdir_permission(dentry, NULL); | |
1078 | + if (rc) | |
1079 | + return rc; | |
1080 | + while (!original_security_ops.inode_rmdir) | |
1081 | + smp_rmb(); | |
1082 | + return original_security_ops.inode_rmdir(dir, dentry); | |
1083 | +} | |
1084 | + | |
1085 | +/** | |
1086 | + * ccs_inode_unlink - Check permission for unlink(). | |
1087 | + * | |
1088 | + * @dir: Pointer to "struct inode". | |
1089 | + * @dentry: Pointer to "struct dentry". | |
1090 | + * | |
1091 | + * Returns 0 on success, negative value otherwise. | |
1092 | + */ | |
1093 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
1094 | +{ | |
1095 | + int rc = ccs_unlink_permission(dentry, NULL); | |
1096 | + if (rc) | |
1097 | + return rc; | |
1098 | + while (!original_security_ops.inode_unlink) | |
1099 | + smp_rmb(); | |
1100 | + return original_security_ops.inode_unlink(dir, dentry); | |
1101 | +} | |
1102 | + | |
1103 | +/** | |
1104 | + * ccs_inode_symlink - Check permission for symlink(). | |
1105 | + * | |
1106 | + * @dir: Pointer to "struct inode". | |
1107 | + * @dentry: Pointer to "struct dentry". | |
1108 | + * @old_name: Content of symbolic link. | |
1109 | + * | |
1110 | + * Returns 0 on success, negative value otherwise. | |
1111 | + */ | |
1112 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
1113 | + const char *old_name) | |
1114 | +{ | |
1115 | + int rc = ccs_symlink_permission(dentry, NULL, old_name); | |
1116 | + if (rc) | |
1117 | + return rc; | |
1118 | + while (!original_security_ops.inode_symlink) | |
1119 | + smp_rmb(); | |
1120 | + return original_security_ops.inode_symlink(dir, dentry, old_name); | |
1121 | +} | |
1122 | + | |
1123 | +/** | |
1124 | + * ccs_inode_rename - Check permission for rename(). | |
1125 | + * | |
1126 | + * @old_dir: Pointer to "struct inode". | |
1127 | + * @old_dentry: Pointer to "struct dentry". | |
1128 | + * @new_dir: Pointer to "struct inode". | |
1129 | + * @new_dentry: Pointer to "struct dentry". | |
1130 | + * | |
1131 | + * Returns 0 on success, negative value otherwise. | |
1132 | + */ | |
1133 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
1134 | + struct inode *new_dir, struct dentry *new_dentry) | |
1135 | +{ | |
1136 | + int rc = ccs_rename_permission(old_dentry, new_dentry, NULL); | |
1137 | + if (rc) | |
1138 | + return rc; | |
1139 | + while (!original_security_ops.inode_rename) | |
1140 | + smp_rmb(); | |
1141 | + return original_security_ops.inode_rename(old_dir, old_dentry, new_dir, | |
1142 | + new_dentry); | |
1143 | +} | |
1144 | + | |
1145 | +/** | |
1146 | + * ccs_inode_link - Check permission for link(). | |
1147 | + * | |
1148 | + * @old_dentry: Pointer to "struct dentry". | |
1149 | + * @dir: Pointer to "struct inode". | |
1150 | + * @new_dentry: Pointer to "struct dentry". | |
1151 | + * | |
1152 | + * Returns 0 on success, negative value otherwise. | |
1153 | + */ | |
1154 | +static int ccs_inode_link(struct dentry *old_dentry, struct inode *dir, | |
1155 | + struct dentry *new_dentry) | |
1156 | +{ | |
1157 | + int rc = ccs_link_permission(old_dentry, new_dentry, NULL); | |
1158 | + if (rc) | |
1159 | + return rc; | |
1160 | + while (!original_security_ops.inode_link) | |
1161 | + smp_rmb(); | |
1162 | + return original_security_ops.inode_link(old_dentry, dir, new_dentry); | |
1163 | +} | |
1164 | + | |
1165 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0) | |
1166 | + | |
1167 | +/** | |
1168 | + * ccs_inode_create - Check permission for creat(). | |
1169 | + * | |
1170 | + * @dir: Pointer to "struct inode". | |
1171 | + * @dentry: Pointer to "struct dentry". | |
1172 | + * @mode: Create mode. | |
1173 | + * | |
1174 | + * Returns 0 on success, negative value otherwise. | |
1175 | + */ | |
1176 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
1177 | + umode_t mode) | |
1178 | +{ | |
1179 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
1180 | + if (rc) | |
1181 | + return rc; | |
1182 | + while (!original_security_ops.inode_create) | |
1183 | + smp_rmb(); | |
1184 | + return original_security_ops.inode_create(dir, dentry, mode); | |
1185 | +} | |
1186 | + | |
1187 | +#else | |
1188 | + | |
1189 | +/** | |
1190 | + * ccs_inode_create - Check permission for creat(). | |
1191 | + * | |
1192 | + * @dir: Pointer to "struct inode". | |
1193 | + * @dentry: Pointer to "struct dentry". | |
1194 | + * @mode: Create mode. | |
1195 | + * | |
1196 | + * Returns 0 on success, negative value otherwise. | |
1197 | + */ | |
1198 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
1199 | + int mode) | |
1200 | +{ | |
1201 | + int rc = ccs_mknod_permission(dentry, NULL, mode, 0); | |
1202 | + if (rc) | |
1203 | + return rc; | |
1204 | + while (!original_security_ops.inode_create) | |
1205 | + smp_rmb(); | |
1206 | + return original_security_ops.inode_create(dir, dentry, mode); | |
1207 | +} | |
1208 | + | |
1209 | +#endif | |
1210 | + | |
1211 | +#endif | |
1212 | + | |
1213 | +#ifdef CONFIG_SECURITY_NETWORK | |
1214 | + | |
1215 | +#include <net/sock.h> | |
1216 | + | |
1217 | +/* Structure for remembering an accept()ed socket's status. */ | |
1218 | +struct ccs_socket_tag { | |
1219 | + struct list_head list; | |
1220 | + struct inode *inode; | |
1221 | + int status; | |
1222 | + struct rcu_head rcu; | |
1223 | +}; | |
1224 | + | |
1225 | +/* | |
1226 | + * List for managing accept()ed sockets. | |
1227 | + * Since we don't need to keep an accept()ed socket into this list after | |
1228 | + * once the permission was granted, the number of entries in this list is | |
1229 | + * likely small. Therefore, we don't use hash tables. | |
1230 | + */ | |
1231 | +static LIST_HEAD(ccs_accepted_socket_list); | |
1232 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
1233 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
1234 | + | |
1235 | +/** | |
1236 | + * ccs_socket_rcu_free - RCU callback for releasing "struct ccs_socket_tag". | |
1237 | + * | |
1238 | + * @rcu: Pointer to "struct rcu_head". | |
1239 | + * | |
1240 | + * Returns nothing. | |
1241 | + */ | |
1242 | +static void ccs_socket_rcu_free(struct rcu_head *rcu) | |
1243 | +{ | |
1244 | + struct ccs_socket_tag *ptr = container_of(rcu, typeof(*ptr), rcu); | |
1245 | + kfree(ptr); | |
1246 | +} | |
1247 | + | |
1248 | +/** | |
1249 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
1250 | + * | |
1251 | + * @inode: Pointer to "struct inode". | |
1252 | + * @status: New status. | |
1253 | + * | |
1254 | + * Returns nothing. | |
1255 | + * | |
1256 | + * If @status == 0, memory for that socket will be released after RCU grace | |
1257 | + * period. | |
1258 | + */ | |
1259 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
1260 | +{ | |
1261 | + struct ccs_socket_tag *ptr; | |
1262 | + /* | |
1263 | + * Protect whole section because multiple threads may call this | |
1264 | + * function with same "sock" via ccs_validate_socket(). | |
1265 | + */ | |
1266 | + spin_lock(&ccs_accepted_socket_list_lock); | |
1267 | + rcu_read_lock(); | |
1268 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
1269 | + if (ptr->inode != inode) | |
1270 | + continue; | |
1271 | + ptr->status = status; | |
1272 | + if (status) | |
1273 | + break; | |
1274 | + list_del_rcu(&ptr->list); | |
1275 | + call_rcu(&ptr->rcu, ccs_socket_rcu_free); | |
1276 | + break; | |
1277 | + } | |
1278 | + rcu_read_unlock(); | |
1279 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
1280 | +} | |
1281 | + | |
1282 | +/** | |
1283 | + * ccs_validate_socket - Check post accept() permission if needed. | |
1284 | + * | |
1285 | + * @sock: Pointer to "struct socket". | |
1286 | + * | |
1287 | + * Returns 0 on success, negative value otherwise. | |
1288 | + */ | |
1289 | +static int ccs_validate_socket(struct socket *sock) | |
1290 | +{ | |
1291 | + struct inode *inode = SOCK_INODE(sock); | |
1292 | + struct ccs_socket_tag *ptr; | |
1293 | + int ret = 0; | |
1294 | + rcu_read_lock(); | |
1295 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
1296 | + if (ptr->inode != inode) | |
1297 | + continue; | |
1298 | + ret = ptr->status; | |
1299 | + break; | |
1300 | + } | |
1301 | + rcu_read_unlock(); | |
1302 | + if (ret <= 0) | |
1303 | + /* | |
1304 | + * This socket is not an accept()ed socket or this socket is | |
1305 | + * an accept()ed socket and post accept() permission is done. | |
1306 | + */ | |
1307 | + return ret; | |
1308 | + /* | |
1309 | + * Check post accept() permission now. | |
1310 | + * | |
1311 | + * Strictly speaking, we need to pass both listen()ing socket and | |
1312 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
1313 | + * But since socket's family and type are same for both sockets, | |
1314 | + * passing the accept()ed socket in place for the listen()ing socket | |
1315 | + * will work. | |
1316 | + */ | |
1317 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
1318 | + /* | |
1319 | + * If permission was granted, we forget that this is an accept()ed | |
1320 | + * socket. Otherwise, we remember that this socket needs to return | |
1321 | + * error for subsequent socketcalls. | |
1322 | + */ | |
1323 | + ccs_update_socket_tag(inode, ret); | |
1324 | + return ret; | |
1325 | +} | |
1326 | + | |
1327 | +/** | |
1328 | + * ccs_socket_accept - Check permission for accept(). | |
1329 | + * | |
1330 | + * @sock: Pointer to "struct socket". | |
1331 | + * @newsock: Pointer to "struct socket". | |
1332 | + * | |
1333 | + * Returns 0 on success, negative value otherwise. | |
1334 | + * | |
1335 | + * This hook is used for setting up environment for doing post accept() | |
1336 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
1337 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
1338 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
1339 | + * in order to do post accept() permission check before returning to userspace. | |
1340 | + * If we make the copy in security_socket_post_create(), it would be possible | |
1341 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
1342 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
1343 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
1344 | + * rather than between sock->ops->accept() and returning to userspace. | |
1345 | + * This means that if a socket was close()d before calling some socket | |
1346 | + * syscalls, post accept() permission check will not be done. | |
1347 | + */ | |
1348 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
1349 | +{ | |
1350 | + struct ccs_socket_tag *ptr; | |
1351 | + int rc = ccs_validate_socket(sock); | |
1352 | + if (rc < 0) | |
1353 | + return rc; | |
1354 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
1355 | + if (!ptr) | |
1356 | + return -ENOMEM; | |
1357 | + while (!original_security_ops.socket_accept) | |
1358 | + smp_rmb(); | |
1359 | + rc = original_security_ops.socket_accept(sock, newsock); | |
1360 | + if (rc) { | |
1361 | + kfree(ptr); | |
1362 | + return rc; | |
1363 | + } | |
1364 | + /* | |
1365 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
1366 | + * "newsock" as "an accept()ed socket but post accept() permission | |
1367 | + * check is not done yet" by allocating memory using inode of the | |
1368 | + * "newsock" as a search key. | |
1369 | + */ | |
1370 | + ptr->inode = SOCK_INODE(newsock); | |
1371 | + ptr->status = 1; /* Check post accept() permission later. */ | |
1372 | + spin_lock(&ccs_accepted_socket_list_lock); | |
1373 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
1374 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
1375 | + return 0; | |
1376 | +} | |
1377 | + | |
1378 | +/** | |
1379 | + * ccs_socket_listen - Check permission for listen(). | |
1380 | + * | |
1381 | + * @sock: Pointer to "struct socket". | |
1382 | + * @backlog: Backlog parameter. | |
1383 | + * | |
1384 | + * Returns 0 on success, negative value otherwise. | |
1385 | + */ | |
1386 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
1387 | +{ | |
1388 | + int rc = ccs_validate_socket(sock); | |
1389 | + if (rc < 0) | |
1390 | + return rc; | |
1391 | + rc = ccs_socket_listen_permission(sock); | |
1392 | + if (rc) | |
1393 | + return rc; | |
1394 | + while (!original_security_ops.socket_listen) | |
1395 | + smp_rmb(); | |
1396 | + return original_security_ops.socket_listen(sock, backlog); | |
1397 | +} | |
1398 | + | |
1399 | +/** | |
1400 | + * ccs_socket_connect - Check permission for connect(). | |
1401 | + * | |
1402 | + * @sock: Pointer to "struct socket". | |
1403 | + * @addr: Pointer to "struct sockaddr". | |
1404 | + * @addr_len: Size of @addr. | |
1405 | + * | |
1406 | + * Returns 0 on success, negative value otherwise. | |
1407 | + */ | |
1408 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
1409 | + int addr_len) | |
1410 | +{ | |
1411 | + int rc = ccs_validate_socket(sock); | |
1412 | + if (rc < 0) | |
1413 | + return rc; | |
1414 | + rc = ccs_socket_connect_permission(sock, addr, addr_len); | |
1415 | + if (rc) | |
1416 | + return rc; | |
1417 | + while (!original_security_ops.socket_connect) | |
1418 | + smp_rmb(); | |
1419 | + return original_security_ops.socket_connect(sock, addr, addr_len); | |
1420 | +} | |
1421 | + | |
1422 | +/** | |
1423 | + * ccs_socket_bind - Check permission for bind(). | |
1424 | + * | |
1425 | + * @sock: Pointer to "struct socket". | |
1426 | + * @addr: Pointer to "struct sockaddr". | |
1427 | + * @addr_len: Size of @addr. | |
1428 | + * | |
1429 | + * Returns 0 on success, negative value otherwise. | |
1430 | + */ | |
1431 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
1432 | + int addr_len) | |
1433 | +{ | |
1434 | + int rc = ccs_validate_socket(sock); | |
1435 | + if (rc < 0) | |
1436 | + return rc; | |
1437 | + rc = ccs_socket_bind_permission(sock, addr, addr_len); | |
1438 | + if (rc) | |
1439 | + return rc; | |
1440 | + while (!original_security_ops.socket_bind) | |
1441 | + smp_rmb(); | |
1442 | + return original_security_ops.socket_bind(sock, addr, addr_len); | |
1443 | +} | |
1444 | + | |
1445 | +/** | |
1446 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
1447 | + * | |
1448 | + * @sock: Pointer to "struct socket". | |
1449 | + * @msg: Pointer to "struct msghdr". | |
1450 | + * @size: Size of message. | |
1451 | + * | |
1452 | + * Returns 0 on success, negative value otherwise. | |
1453 | + */ | |
1454 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
1455 | + int size) | |
1456 | +{ | |
1457 | + int rc = ccs_validate_socket(sock); | |
1458 | + if (rc < 0) | |
1459 | + return rc; | |
1460 | + rc = ccs_socket_sendmsg_permission(sock, msg, size); | |
1461 | + if (rc) | |
1462 | + return rc; | |
1463 | + while (!original_security_ops.socket_sendmsg) | |
1464 | + smp_rmb(); | |
1465 | + return original_security_ops.socket_sendmsg(sock, msg, size); | |
1466 | +} | |
1467 | + | |
1468 | +/** | |
1469 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
1470 | + * | |
1471 | + * @sock: Pointer to "struct socket". | |
1472 | + * @msg: Pointer to "struct msghdr". | |
1473 | + * @size: Size of message. | |
1474 | + * @flags: Flags. | |
1475 | + * | |
1476 | + * Returns 0 on success, negative value otherwise. | |
1477 | + */ | |
1478 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
1479 | + int size, int flags) | |
1480 | +{ | |
1481 | + int rc = ccs_validate_socket(sock); | |
1482 | + if (rc < 0) | |
1483 | + return rc; | |
1484 | + while (!original_security_ops.socket_recvmsg) | |
1485 | + smp_rmb(); | |
1486 | + return original_security_ops.socket_recvmsg(sock, msg, size, flags); | |
1487 | +} | |
1488 | + | |
1489 | +/** | |
1490 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
1491 | + * | |
1492 | + * @sock: Pointer to "struct socket". | |
1493 | + * | |
1494 | + * Returns 0 on success, negative value otherwise. | |
1495 | + */ | |
1496 | +static int ccs_socket_getsockname(struct socket *sock) | |
1497 | +{ | |
1498 | + int rc = ccs_validate_socket(sock); | |
1499 | + if (rc < 0) | |
1500 | + return rc; | |
1501 | + while (!original_security_ops.socket_getsockname) | |
1502 | + smp_rmb(); | |
1503 | + return original_security_ops.socket_getsockname(sock); | |
1504 | +} | |
1505 | + | |
1506 | +/** | |
1507 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
1508 | + * | |
1509 | + * @sock: Pointer to "struct socket". | |
1510 | + * | |
1511 | + * Returns 0 on success, negative value otherwise. | |
1512 | + */ | |
1513 | +static int ccs_socket_getpeername(struct socket *sock) | |
1514 | +{ | |
1515 | + int rc = ccs_validate_socket(sock); | |
1516 | + if (rc < 0) | |
1517 | + return rc; | |
1518 | + while (!original_security_ops.socket_getpeername) | |
1519 | + smp_rmb(); | |
1520 | + return original_security_ops.socket_getpeername(sock); | |
1521 | +} | |
1522 | + | |
1523 | +/** | |
1524 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
1525 | + * | |
1526 | + * @sock: Pointer to "struct socket". | |
1527 | + * @level: Level. | |
1528 | + * @optname: Option's name, | |
1529 | + * | |
1530 | + * Returns 0 on success, negative value otherwise. | |
1531 | + */ | |
1532 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1533 | +{ | |
1534 | + int rc = ccs_validate_socket(sock); | |
1535 | + if (rc < 0) | |
1536 | + return rc; | |
1537 | + while (!original_security_ops.socket_getsockopt) | |
1538 | + smp_rmb(); | |
1539 | + return original_security_ops.socket_getsockopt(sock, level, optname); | |
1540 | +} | |
1541 | + | |
1542 | +/** | |
1543 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1544 | + * | |
1545 | + * @sock: Pointer to "struct socket". | |
1546 | + * @level: Level. | |
1547 | + * @optname: Option's name, | |
1548 | + * | |
1549 | + * Returns 0 on success, negative value otherwise. | |
1550 | + */ | |
1551 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1552 | +{ | |
1553 | + int rc = ccs_validate_socket(sock); | |
1554 | + if (rc < 0) | |
1555 | + return rc; | |
1556 | + while (!original_security_ops.socket_setsockopt) | |
1557 | + smp_rmb(); | |
1558 | + return original_security_ops.socket_setsockopt(sock, level, optname); | |
1559 | +} | |
1560 | + | |
1561 | +/** | |
1562 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1563 | + * | |
1564 | + * @sock: Pointer to "struct socket". | |
1565 | + * @how: Shutdown mode. | |
1566 | + * | |
1567 | + * Returns 0 on success, negative value otherwise. | |
1568 | + */ | |
1569 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1570 | +{ | |
1571 | + int rc = ccs_validate_socket(sock); | |
1572 | + if (rc < 0) | |
1573 | + return rc; | |
1574 | + while (!original_security_ops.socket_shutdown) | |
1575 | + smp_rmb(); | |
1576 | + return original_security_ops.socket_shutdown(sock, how); | |
1577 | +} | |
1578 | + | |
1579 | +#define SOCKFS_MAGIC 0x534F434B | |
1580 | + | |
1581 | +/** | |
1582 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1583 | + * | |
1584 | + * @inode: Pointer to "struct inode". | |
1585 | + * | |
1586 | + * Returns nothing. | |
1587 | + * | |
1588 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1589 | + */ | |
1590 | +static void ccs_inode_free_security(struct inode *inode) | |
1591 | +{ | |
1592 | + while (!original_security_ops.inode_free_security) | |
1593 | + smp_rmb(); | |
1594 | + original_security_ops.inode_free_security(inode); | |
1595 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1596 | + ccs_update_socket_tag(inode, 0); | |
1597 | +} | |
1598 | + | |
1599 | +#endif | |
1600 | + | |
1601 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) | |
1602 | + | |
1603 | +/** | |
1604 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1605 | + * | |
1606 | + * @old_path: Pointer to "struct path". | |
1607 | + * @new_path: Pointer to "struct path". | |
1608 | + * | |
1609 | + * Returns 0 on success, negative value otherwise. | |
1610 | + */ | |
1611 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1612 | +{ | |
1613 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1614 | + if (rc) | |
1615 | + return rc; | |
1616 | + while (!original_security_ops.sb_pivotroot) | |
1617 | + smp_rmb(); | |
1618 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1619 | +} | |
1620 | + | |
1621 | +/** | |
1622 | + * ccs_sb_mount - Check permission for mount(). | |
1623 | + * | |
1624 | + * @dev_name: Name of device file. | |
1625 | + * @path: Pointer to "struct path". | |
1626 | + * @type: Name of filesystem type. Maybe NULL. | |
1627 | + * @flags: Mount options. | |
1628 | + * @data_page: Optional data. Maybe NULL. | |
1629 | + * | |
1630 | + * Returns 0 on success, negative value otherwise. | |
1631 | + */ | |
1632 | +static int ccs_sb_mount(char *dev_name, struct path *path, char *type, | |
1633 | + unsigned long flags, void *data_page) | |
1634 | +{ | |
1635 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1636 | + if (rc) | |
1637 | + return rc; | |
1638 | + while (!original_security_ops.sb_mount) | |
1639 | + smp_rmb(); | |
1640 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1641 | + data_page); | |
1642 | +} | |
1643 | + | |
1644 | +#else | |
1645 | + | |
1646 | +/** | |
1647 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1648 | + * | |
1649 | + * @old_path: Pointer to "struct path". | |
1650 | + * @new_path: Pointer to "struct path". | |
1651 | + * | |
1652 | + * Returns 0 on success, negative value otherwise. | |
1653 | + */ | |
1654 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1655 | +{ | |
1656 | + int rc = ccs_pivot_root_permission(old_path, new_path); | |
1657 | + if (rc) | |
1658 | + return rc; | |
1659 | + while (!original_security_ops.sb_pivotroot) | |
1660 | + smp_rmb(); | |
1661 | + return original_security_ops.sb_pivotroot(old_path, new_path); | |
1662 | +} | |
1663 | + | |
1664 | +/** | |
1665 | + * ccs_sb_mount - Check permission for mount(). | |
1666 | + * | |
1667 | + * @dev_name: Name of device file. | |
1668 | + * @path: Pointer to "struct path". | |
1669 | + * @type: Name of filesystem type. Maybe NULL. | |
1670 | + * @flags: Mount options. | |
1671 | + * @data_page: Optional data. Maybe NULL. | |
1672 | + * | |
1673 | + * Returns 0 on success, negative value otherwise. | |
1674 | + */ | |
1675 | +static int ccs_sb_mount(const char *dev_name, struct path *path, | |
1676 | + const char *type, unsigned long flags, void *data_page) | |
1677 | +{ | |
1678 | + int rc = ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1679 | + if (rc) | |
1680 | + return rc; | |
1681 | + while (!original_security_ops.sb_mount) | |
1682 | + smp_rmb(); | |
1683 | + return original_security_ops.sb_mount(dev_name, path, type, flags, | |
1684 | + data_page); | |
1685 | +} | |
1686 | + | |
1687 | +#endif | |
1688 | + | |
1689 | +/** | |
1690 | + * ccs_sb_umount - Check permission for umount(). | |
1691 | + * | |
1692 | + * @mnt: Pointer to "struct vfsmount". | |
1693 | + * @flags: Unmount flags. | |
1694 | + * | |
1695 | + * Returns 0 on success, negative value otherwise. | |
1696 | + */ | |
1697 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1698 | +{ | |
1699 | + int rc = ccs_umount_permission(mnt, flags); | |
1700 | + if (rc) | |
1701 | + return rc; | |
1702 | + while (!original_security_ops.sb_umount) | |
1703 | + smp_rmb(); | |
1704 | + return original_security_ops.sb_umount(mnt, flags); | |
1705 | +} | |
1706 | + | |
1707 | +/** | |
1708 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1709 | + * | |
1710 | + * @file: Pointer to "struct file". | |
1711 | + * @cmd: Command number. | |
1712 | + * @arg: Value for @cmd. | |
1713 | + * | |
1714 | + * Returns 0 on success, negative value otherwise. | |
1715 | + */ | |
1716 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1717 | + unsigned long arg) | |
1718 | +{ | |
1719 | + int rc = ccs_fcntl_permission(file, cmd, arg); | |
1720 | + if (rc) | |
1721 | + return rc; | |
1722 | + while (!original_security_ops.file_fcntl) | |
1723 | + smp_rmb(); | |
1724 | + return original_security_ops.file_fcntl(file, cmd, arg); | |
1725 | +} | |
1726 | + | |
1727 | +/** | |
1728 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1729 | + * | |
1730 | + * @filp: Pointer to "struct file". | |
1731 | + * @cmd: Command number. | |
1732 | + * @arg: Value for @cmd. | |
1733 | + * | |
1734 | + * Returns 0 on success, negative value otherwise. | |
1735 | + */ | |
1736 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1737 | + unsigned long arg) | |
1738 | +{ | |
1739 | + int rc = ccs_ioctl_permission(filp, cmd, arg); | |
1740 | + if (rc) | |
1741 | + return rc; | |
1742 | + while (!original_security_ops.file_ioctl) | |
1743 | + smp_rmb(); | |
1744 | + return original_security_ops.file_ioctl(filp, cmd, arg); | |
1745 | +} | |
1746 | + | |
1747 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL) | |
1748 | +int ccs_path_permission(struct ccs_request_info *r, u8 operation, | |
1749 | + const struct ccs_path_info *filename); | |
1750 | + | |
1751 | +/** | |
1752 | + * ccs_prepend - Copy of prepend() in fs/dcache.c. | |
1753 | + * | |
1754 | + * @buffer: Pointer to "struct char *". | |
1755 | + * @buflen: Pointer to int which holds size of @buffer. | |
1756 | + * @str: String to copy. | |
1757 | + * | |
1758 | + * Returns 0 on success, negative value otherwise. | |
1759 | + * | |
1760 | + * @buffer and @buflen are updated upon success. | |
1761 | + */ | |
1762 | +static int ccs_prepend(char **buffer, int *buflen, const char *str) | |
1763 | +{ | |
1764 | + int namelen = strlen(str); | |
1765 | + if (*buflen < namelen) | |
1766 | + return -ENOMEM; | |
1767 | + *buflen -= namelen; | |
1768 | + *buffer -= namelen; | |
1769 | + memcpy(*buffer, str, namelen); | |
1770 | + return 0; | |
1771 | +} | |
1772 | + | |
1773 | +/** | |
1774 | + * ccs_sysctl_permission - Check permission for sysctl(). | |
1775 | + * | |
1776 | + * @table: Pointer to "struct ctl_table". | |
1777 | + * @op: Operation. (MAY_READ and/or MAY_WRITE) | |
1778 | + * | |
1779 | + * Returns 0 on success, negative value otherwise. | |
1780 | + */ | |
1781 | +static int ccs_sysctl(struct ctl_table *table, int op) | |
1782 | +{ | |
1783 | + int error; | |
1784 | + struct ccs_path_info buf; | |
1785 | + struct ccs_request_info r; | |
1786 | + int buflen; | |
1787 | + char *buffer; | |
1788 | + int idx; | |
1789 | + while (!original_security_ops.sysctl) | |
1790 | + smp_rmb(); | |
1791 | + error = original_security_ops.sysctl(table, op); | |
1792 | + if (error) | |
1793 | + return error; | |
1794 | + op &= MAY_READ | MAY_WRITE; | |
1795 | + if (!op) | |
1796 | + return 0; | |
1797 | + buffer = NULL; | |
1798 | + buf.name = NULL; | |
1799 | + idx = ccs_read_lock(); | |
1800 | + if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN) | |
1801 | + == CCS_CONFIG_DISABLED) | |
1802 | + goto out; | |
1803 | + error = -ENOMEM; | |
1804 | + buflen = 4096; | |
1805 | + buffer = kmalloc(buflen, CCS_GFP_FLAGS); | |
1806 | + if (buffer) { | |
1807 | + char *end = buffer + buflen; | |
1808 | + *--end = '\0'; | |
1809 | + buflen--; | |
1810 | + while (table) { | |
1811 | + char num[32]; | |
1812 | + const char *sp = table->procname; | |
1813 | + if (!sp) { | |
1814 | + memset(num, 0, sizeof(num)); | |
1815 | + snprintf(num, sizeof(num) - 1, "=%d=", | |
1816 | + table->ctl_name); | |
1817 | + sp = num; | |
1818 | + } | |
1819 | + if (ccs_prepend(&end, &buflen, sp) || | |
1820 | + ccs_prepend(&end, &buflen, "/")) | |
1821 | + goto out; | |
1822 | + table = table->parent; | |
1823 | + } | |
1824 | + if (ccs_prepend(&end, &buflen, "proc:/sys")) | |
1825 | + goto out; | |
1826 | + buf.name = ccs_encode(end); | |
1827 | + } | |
1828 | + if (buf.name) { | |
1829 | + ccs_fill_path_info(&buf); | |
1830 | + if (op & MAY_READ) | |
1831 | + error = ccs_path_permission(&r, CCS_TYPE_READ, &buf); | |
1832 | + else | |
1833 | + error = 0; | |
1834 | + if (!error && (op & MAY_WRITE)) | |
1835 | + error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf); | |
1836 | + } | |
1837 | +out: | |
1838 | + ccs_read_unlock(idx); | |
1839 | + kfree(buf.name); | |
1840 | + kfree(buffer); | |
1841 | + return error; | |
1842 | +} | |
1843 | + | |
1844 | +#endif | |
1845 | + | |
1846 | +/* | |
1847 | + * Why not to copy all operations by "original_security_ops = *ops" ? | |
1848 | + * Because copying byte array is not atomic. Reader checks | |
1849 | + * original_security_ops.op != NULL before doing original_security_ops.op(). | |
1850 | + * Thus, modifying original_security_ops.op has to be atomic. | |
1851 | + */ | |
1852 | +#define swap_security_ops(op) \ | |
1853 | + original_security_ops.op = ops->op; smp_wmb(); ops->op = ccs_##op; | |
1854 | + | |
1855 | +/** | |
1856 | + * ccs_update_security_ops - Overwrite original "struct security_operations". | |
1857 | + * | |
1858 | + * @ops: Pointer to "struct security_operations". | |
1859 | + * | |
1860 | + * Returns nothing. | |
1861 | + */ | |
1862 | +static void __init ccs_update_security_ops(struct security_operations *ops) | |
1863 | +{ | |
1864 | + /* Security context allocator. */ | |
1865 | + swap_security_ops(task_create); | |
1866 | + swap_security_ops(cred_prepare); | |
1867 | + swap_security_ops(cred_free); | |
1868 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32) | |
1869 | + swap_security_ops(cred_alloc_blank); | |
1870 | + swap_security_ops(cred_transfer); | |
1871 | +#endif | |
1872 | + /* Security context updater for successful execve(). */ | |
1873 | + swap_security_ops(bprm_check_security); | |
1874 | + swap_security_ops(bprm_committing_creds); | |
1875 | + /* Various permission checker. */ | |
1876 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
1877 | + swap_security_ops(file_open); | |
1878 | +#else | |
1879 | + swap_security_ops(dentry_open); | |
1880 | +#endif | |
1881 | + swap_security_ops(file_fcntl); | |
1882 | + swap_security_ops(file_ioctl); | |
1883 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL) | |
1884 | + swap_security_ops(sysctl); | |
1885 | +#endif | |
1886 | + swap_security_ops(sb_pivotroot); | |
1887 | + swap_security_ops(sb_mount); | |
1888 | + swap_security_ops(sb_umount); | |
1889 | +#if defined(CONFIG_SECURITY_PATH) | |
1890 | + swap_security_ops(path_mknod); | |
1891 | + swap_security_ops(path_mkdir); | |
1892 | + swap_security_ops(path_rmdir); | |
1893 | + swap_security_ops(path_unlink); | |
1894 | + swap_security_ops(path_symlink); | |
1895 | + swap_security_ops(path_rename); | |
1896 | + swap_security_ops(path_link); | |
1897 | + swap_security_ops(path_truncate); | |
1898 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33) | |
1899 | + swap_security_ops(path_chmod); | |
1900 | + swap_security_ops(path_chown); | |
1901 | + swap_security_ops(path_chroot); | |
1902 | +#endif | |
1903 | +#else | |
1904 | + swap_security_ops(inode_mknod); | |
1905 | + swap_security_ops(inode_mkdir); | |
1906 | + swap_security_ops(inode_rmdir); | |
1907 | + swap_security_ops(inode_unlink); | |
1908 | + swap_security_ops(inode_symlink); | |
1909 | + swap_security_ops(inode_rename); | |
1910 | + swap_security_ops(inode_link); | |
1911 | + swap_security_ops(inode_create); | |
1912 | +#endif | |
1913 | + swap_security_ops(inode_setattr); | |
1914 | + swap_security_ops(inode_getattr); | |
1915 | +#ifdef CONFIG_SECURITY_NETWORK | |
1916 | + swap_security_ops(socket_bind); | |
1917 | + swap_security_ops(socket_connect); | |
1918 | + swap_security_ops(socket_listen); | |
1919 | + swap_security_ops(socket_sendmsg); | |
1920 | + swap_security_ops(socket_recvmsg); | |
1921 | + swap_security_ops(socket_getsockname); | |
1922 | + swap_security_ops(socket_getpeername); | |
1923 | + swap_security_ops(socket_getsockopt); | |
1924 | + swap_security_ops(socket_setsockopt); | |
1925 | + swap_security_ops(socket_shutdown); | |
1926 | + swap_security_ops(socket_accept); | |
1927 | + swap_security_ops(inode_free_security); | |
1928 | +#endif | |
1929 | +} | |
1930 | + | |
1931 | +#undef swap_security_ops | |
1932 | + | |
1933 | +/** | |
1934 | + * ccs_init - Initialize this module. | |
1935 | + * | |
1936 | + * Returns 0 on success, negative value otherwise. | |
1937 | + */ | |
1938 | +static int __init ccs_init(void) | |
1939 | +{ | |
1940 | + struct security_operations *ops = probe_security_ops(); | |
1941 | + if (!ops) | |
1942 | + goto out; | |
1943 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1944 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1945 | + goto out; | |
1946 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1947 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1948 | + goto out; | |
1949 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
1950 | + ccsecurity_exports.vfsmount_lock = probe_vfsmount_lock(); | |
1951 | + if (!ccsecurity_exports.vfsmount_lock) | |
1952 | + goto out; | |
1953 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0) | |
1954 | + ccsecurity_exports.__d_path = probe___d_path(); | |
1955 | + if (!ccsecurity_exports.__d_path) | |
1956 | + goto out; | |
1957 | +#else | |
1958 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1959 | + if (!ccsecurity_exports.d_absolute_path) | |
1960 | + goto out; | |
1961 | +#endif | |
1962 | + { | |
1963 | + int idx; | |
1964 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1965 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1966 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1967 | + } | |
1968 | + } | |
1969 | + ccs_main_init(); | |
1970 | + ccs_update_security_ops(ops); | |
1971 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1972 | + printk(KERN_INFO | |
1973 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1974 | + return 0; | |
1975 | +out: | |
1976 | + return -EINVAL; | |
1977 | +} | |
1978 | + | |
1979 | +module_init(ccs_init); | |
1980 | +MODULE_LICENSE("GPL"); | |
1981 | + | |
1982 | +/** | |
1983 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1984 | + * | |
1985 | + * @domain: Pointer to "struct ccs_domain_info". | |
1986 | + * | |
1987 | + * Returns true if @domain is in use, false otherwise. | |
1988 | + * | |
1989 | + * Caller holds rcu_read_lock(). | |
1990 | + */ | |
1991 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1992 | +{ | |
1993 | + int idx; | |
1994 | + struct ccs_security *ptr; | |
1995 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1996 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1997 | + list_for_each_entry_rcu(ptr, list, list) { | |
1998 | + struct ccs_execve *ee = ptr->ee; | |
1999 | + if (ptr->ccs_domain_info == domain || | |
2000 | + (ee && ee->previous_domain == domain)) { | |
2001 | + return true; | |
2002 | + } | |
2003 | + } | |
2004 | + } | |
2005 | + return false; | |
2006 | +} | |
2007 | + | |
2008 | +/** | |
2009 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
2010 | + * | |
2011 | + * @ptr: Pointer to "struct ccs_security". | |
2012 | + * @list: Pointer to "struct list_head". | |
2013 | + * | |
2014 | + * Returns nothing. | |
2015 | + */ | |
2016 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
2017 | + struct list_head *list) | |
2018 | +{ | |
2019 | + unsigned long flags; | |
2020 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
2021 | + list_add_rcu(&ptr->list, list); | |
2022 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
2023 | +} | |
2024 | + | |
2025 | +/** | |
2026 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
2027 | + * | |
2028 | + * @task: Pointer to "struct task_struct". | |
2029 | + * | |
2030 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
2031 | + * out of memory, &ccs_default_security otherwise. | |
2032 | + * | |
2033 | + * If @task is current thread and "struct ccs_security" for current thread was | |
2034 | + * not found, I try to allocate it. But if allocation failed, current thread | |
2035 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
2036 | + * won't work. | |
2037 | + */ | |
2038 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
2039 | +{ | |
2040 | + struct ccs_security *ptr; | |
2041 | + struct list_head *list = &ccs_task_security_list | |
2042 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
2043 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
2044 | + while (!list->next); | |
2045 | + rcu_read_lock(); | |
2046 | + list_for_each_entry_rcu(ptr, list, list) { | |
2047 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
2048 | + continue; | |
2049 | + rcu_read_unlock(); | |
2050 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
2051 | + /* | |
2052 | + * Current thread needs to transit from old domain to new | |
2053 | + * domain before do_execve() succeeds in order to check | |
2054 | + * permission for interpreters and environment variables using | |
2055 | + * new domain's ACL rules. The domain transition has to be | |
2056 | + * visible from other CPU in order to allow interactive | |
2057 | + * enforcing mode. Also, the domain transition has to be | |
2058 | + * reverted if do_execve() failed. However, an LSM hook for | |
2059 | + * reverting domain transition is missing. | |
2060 | + * | |
2061 | + * security_prepare_creds() is called from prepare_creds() from | |
2062 | + * prepare_bprm_creds() from do_execve() before setting | |
2063 | + * current->in_execve flag, and current->in_execve flag is | |
2064 | + * cleared by the time next do_execve() request starts. | |
2065 | + * This means that we can emulate the missing LSM hook for | |
2066 | + * reverting domain transition, by calling this function from | |
2067 | + * security_prepare_creds(). | |
2068 | + * | |
2069 | + * If current->in_execve is not set but ptr->ccs_flags has | |
2070 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
2071 | + * has failed and reverting domain transition is needed. | |
2072 | + */ | |
2073 | + if (task == current && | |
2074 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
2075 | + !current->in_execve) { | |
2076 | + ccs_debug_trace("4"); | |
2077 | + ccs_clear_execve(-1, ptr); | |
2078 | + } | |
2079 | +#else | |
2080 | + /* | |
2081 | + * Current thread needs to transit from old domain to new | |
2082 | + * domain before do_execve() succeeds in order to check | |
2083 | + * permission for interpreters and environment variables using | |
2084 | + * new domain's ACL rules. The domain transition has to be | |
2085 | + * visible from other CPU in order to allow interactive | |
2086 | + * enforcing mode. Also, the domain transition has to be | |
2087 | + * reverted if do_execve() failed. However, an LSM hook for | |
2088 | + * reverting domain transition is missing. | |
2089 | + * | |
2090 | + * When do_execve() failed, "struct cred" in | |
2091 | + * "struct linux_binprm" is scheduled for destruction. | |
2092 | + * But current thread returns to userspace without waiting for | |
2093 | + * destruction. The security_cred_free() LSM hook is called | |
2094 | + * after an RCU grace period has elapsed. Since some CPU may be | |
2095 | + * doing long long RCU read side critical section, there is | |
2096 | + * no guarantee that security_cred_free() is called before | |
2097 | + * current thread again calls do_execve(). | |
2098 | + * | |
2099 | + * To be able to revert domain transition before processing | |
2100 | + * next do_execve() request, current thread gets a refcount on | |
2101 | + * "struct cred" in "struct linux_binprm" and memorizes it. | |
2102 | + * Current thread drops the refcount and forgets it when | |
2103 | + * do_execve() succeeded. | |
2104 | + * | |
2105 | + * Therefore, if current thread hasn't forgotten it and | |
2106 | + * current thread is the last one using that "struct cred", | |
2107 | + * it indicates that do_execve() has failed and reverting | |
2108 | + * domain transition is needed. | |
2109 | + */ | |
2110 | + if (task == current && ptr->cred && | |
2111 | + atomic_read(&ptr->cred->usage) == 1) { | |
2112 | + ccs_debug_trace("4"); | |
2113 | + ccs_clear_execve(-1, ptr); | |
2114 | + } | |
2115 | +#endif | |
2116 | + return ptr; | |
2117 | + } | |
2118 | + rcu_read_unlock(); | |
2119 | + if (task != current) { | |
2120 | + /* | |
2121 | + * If a thread does nothing after fork(), caller will reach | |
2122 | + * here because "struct ccs_security" for that thread is not | |
2123 | + * yet allocated. But that thread is keeping a snapshot of | |
2124 | + * "struct ccs_security" taken as of ccs_task_create() | |
2125 | + * associated with that thread's "struct cred". | |
2126 | + * | |
2127 | + * Since that snapshot will be used as initial data when that | |
2128 | + * thread allocates "struct ccs_security" for that thread, we | |
2129 | + * can return that snapshot rather than &ccs_default_security. | |
2130 | + * | |
2131 | + * Since this function is called by only ccs_select_one() and | |
2132 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
2133 | + * it is guaranteed that caller has called rcu_read_lock() | |
2134 | + * (via ccs_tasklist_lock()) before finding this thread and | |
2135 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
2136 | + * like get_robust_list() does. | |
2137 | + */ | |
2138 | + return ccs_find_cred_security(__task_cred(task)); | |
2139 | + } | |
2140 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
2141 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
2142 | + if (!ptr) { | |
2143 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
2144 | + task->pid); | |
2145 | + send_sig(SIGKILL, current, 0); | |
2146 | + return &ccs_oom_security; | |
2147 | + } | |
2148 | + *ptr = *ccs_find_cred_security(task->cred); | |
2149 | + /* We can shortcut because task == current. */ | |
2150 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
2151 | + pids[PIDTYPE_PID].pid); | |
2152 | + ptr->cred = NULL; | |
2153 | + ccs_add_task_security(ptr, list); | |
2154 | + return ptr; | |
2155 | +} | |
2156 | + | |
2157 | +/** | |
2158 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
2159 | + * | |
2160 | + * @new: Pointer to "struct cred". | |
2161 | + * @old: Pointer to "struct cred". | |
2162 | + * @gfp: Memory allocation flags. | |
2163 | + * | |
2164 | + * Returns 0 on success, negative value otherwise. | |
2165 | + */ | |
2166 | +static int ccs_copy_cred_security(const struct cred *new, | |
2167 | + const struct cred *old, gfp_t gfp) | |
2168 | +{ | |
2169 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
2170 | + struct ccs_security *new_security = | |
2171 | + kzalloc(sizeof(*new_security), gfp); | |
2172 | + if (!new_security) | |
2173 | + return -ENOMEM; | |
2174 | + *new_security = *old_security; | |
2175 | + new_security->cred = new; | |
2176 | + ccs_add_cred_security(new_security); | |
2177 | + return 0; | |
2178 | +} | |
2179 | + | |
2180 | +/** | |
2181 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
2182 | + * | |
2183 | + * @cred: Pointer to "struct cred". | |
2184 | + * | |
2185 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
2186 | + * otherwise. | |
2187 | + */ | |
2188 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
2189 | +{ | |
2190 | + struct ccs_security *ptr; | |
2191 | + struct list_head *list = &ccs_cred_security_list | |
2192 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
2193 | + rcu_read_lock(); | |
2194 | + list_for_each_entry_rcu(ptr, list, list) { | |
2195 | + if (ptr->cred != cred) | |
2196 | + continue; | |
2197 | + rcu_read_unlock(); | |
2198 | + return ptr; | |
2199 | + } | |
2200 | + rcu_read_unlock(); | |
2201 | + return &ccs_default_security; | |
2202 | +} | |
2203 | + | |
2204 | +/** | |
2205 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
2206 | + * | |
2207 | + * Returns nothing. | |
2208 | + * | |
2209 | + * Since security_task_free() is missing, I can't release memory associated | |
2210 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
2211 | + * "struct pid" and runs garbage collection when associated | |
2212 | + * "struct task_struct" has gone. | |
2213 | + */ | |
2214 | +static void ccs_task_security_gc(void) | |
2215 | +{ | |
2216 | + static DEFINE_SPINLOCK(lock); | |
2217 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
2218 | + unsigned int idx; | |
2219 | + /* | |
2220 | + * If some process is doing execve(), try to garbage collection now. | |
2221 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
2222 | + * as soon as execve() has completed in order to compensate for lack of | |
2223 | + * security_bprm_free() and security_task_free() hooks. | |
2224 | + * | |
2225 | + * Otherwise, reduce frequency for performance reason. | |
2226 | + */ | |
2227 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
2228 | + atomic_inc_return(&gc_counter) < 1024) | |
2229 | + return; | |
2230 | + if (!spin_trylock(&lock)) | |
2231 | + return; | |
2232 | + atomic_set(&gc_counter, 0); | |
2233 | + rcu_read_lock(); | |
2234 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
2235 | + struct ccs_security *ptr; | |
2236 | + struct list_head *list = &ccs_task_security_list[idx]; | |
2237 | + list_for_each_entry_rcu(ptr, list, list) { | |
2238 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
2239 | + continue; | |
2240 | + ccs_del_security(ptr); | |
2241 | + } | |
2242 | + } | |
2243 | + rcu_read_unlock(); | |
2244 | + spin_unlock(&lock); | |
2245 | +} |
@@ -0,0 +1,1304 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
7 | + */ | |
8 | + | |
9 | +#include "internal.h" | |
10 | +#include "probe.h" | |
11 | + | |
12 | +/* Prototype definition. */ | |
13 | +static int __ccs_alloc_task_security(const struct task_struct *task); | |
14 | +static void __ccs_free_task_security(const struct task_struct *task); | |
15 | + | |
16 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
17 | +static struct ccs_security ccs_oom_security = { | |
18 | + .ccs_domain_info = &ccs_kernel_domain | |
19 | +}; | |
20 | + | |
21 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
22 | +static struct ccs_security ccs_default_security = { | |
23 | + .ccs_domain_info = &ccs_kernel_domain | |
24 | +}; | |
25 | + | |
26 | +/* List of "struct ccs_security". */ | |
27 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
28 | +/* Lock for protecting ccs_task_security_list[]. */ | |
29 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
30 | + | |
31 | +/* For exporting variables and functions. */ | |
32 | +struct ccsecurity_exports ccsecurity_exports; | |
33 | +/* Members are updated by loadable kernel module. */ | |
34 | +struct ccsecurity_operations ccsecurity_ops; | |
35 | + | |
36 | +/* Original hooks. */ | |
37 | +static union security_list_options original_cred_prepare; | |
38 | +static union security_list_options original_task_alloc; | |
39 | +static union security_list_options original_task_free; | |
40 | + | |
41 | +#ifdef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
42 | + | |
43 | +/** | |
44 | + * ccs_update_ee_counter - Update "struct ccs_execve" counter. | |
45 | + * | |
46 | + * @count: Count to increment or decrement. | |
47 | + * | |
48 | + * Returns updated counter. | |
49 | + */ | |
50 | +static unsigned int ccs_update_ee_counter(int count) | |
51 | +{ | |
52 | + /* Debug counter for detecting "struct ccs_execve" memory leak. */ | |
53 | + static atomic_t ccs_ee_counter = ATOMIC_INIT(0); | |
54 | + return atomic_add_return(count, &ccs_ee_counter); | |
55 | +} | |
56 | + | |
57 | +/** | |
58 | + * ccs_audit_alloc_execve - Audit allocation of "struct ccs_execve". | |
59 | + * | |
60 | + * @ee: Pointer to "struct ccs_execve". | |
61 | + * | |
62 | + * Returns nothing. | |
63 | + */ | |
64 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee) | |
65 | +{ | |
66 | + printk(KERN_INFO "AKARI: Allocated %p by pid=%u (count=%u)\n", ee, | |
67 | + current->pid, ccs_update_ee_counter(1) - 1); | |
68 | +} | |
69 | + | |
70 | +/** | |
71 | + * ccs_audit_free_execve - Audit release of "struct ccs_execve". | |
72 | + * | |
73 | + * @ee: Pointer to "struct ccs_execve". | |
74 | + * @task: True if released by current task, false otherwise. | |
75 | + * | |
76 | + * Returns nothing. | |
77 | + */ | |
78 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
79 | + const bool is_current) | |
80 | +{ | |
81 | + const unsigned int tmp = ccs_update_ee_counter(-1); | |
82 | + if (is_current) | |
83 | + printk(KERN_INFO "AKARI: Releasing %p by pid=%u (count=%u)\n", | |
84 | + ee, current->pid, tmp); | |
85 | + else | |
86 | + printk(KERN_INFO "AKARI: Releasing %p by kernel (count=%u)\n", | |
87 | + ee, tmp); | |
88 | +} | |
89 | + | |
90 | +#endif | |
91 | + | |
92 | +#if !defined(CONFIG_AKARI_DEBUG) | |
93 | +#define ccs_debug_trace(pos) do { } while (0) | |
94 | +#else | |
95 | +#define ccs_debug_trace(pos) \ | |
96 | + do { \ | |
97 | + static bool done; \ | |
98 | + if (!done) { \ | |
99 | + printk(KERN_INFO \ | |
100 | + "AKARI: Debug trace: " pos " of 2\n"); \ | |
101 | + done = true; \ | |
102 | + } \ | |
103 | + } while (0) | |
104 | +#endif | |
105 | + | |
106 | +/** | |
107 | + * ccs_clear_execve - Release memory used by do_execve(). | |
108 | + * | |
109 | + * @ret: 0 if do_execve() succeeded, negative value otherwise. | |
110 | + * @security: Pointer to "struct ccs_security". | |
111 | + * | |
112 | + * Returns nothing. | |
113 | + */ | |
114 | +static void ccs_clear_execve(int ret, struct ccs_security *security) | |
115 | +{ | |
116 | + struct ccs_execve *ee = security->ee; | |
117 | + if (security == &ccs_default_security || security == &ccs_oom_security | |
118 | + || !ee) | |
119 | + return; | |
120 | + security->ee = NULL; | |
121 | + ccs_finish_execve(ret, ee); | |
122 | +} | |
123 | + | |
124 | +/** | |
125 | + * ccs_task_alloc_security - Allocate memory for new tasks. | |
126 | + * | |
127 | + * @p: Pointer to "struct task_struct". | |
128 | + * @clone_flags: Flags passed to clone(). | |
129 | + * | |
130 | + * Returns 0 on success, negative value otherwise. | |
131 | + */ | |
132 | +static int ccs_task_alloc_security(struct task_struct *p, | |
133 | + unsigned long clone_flags) | |
134 | +{ | |
135 | + int rc = __ccs_alloc_task_security(p); | |
136 | + if (rc) | |
137 | + return rc; | |
138 | + if (original_task_alloc.task_alloc) { | |
139 | + rc = original_task_alloc.task_alloc(p, clone_flags); | |
140 | + if (rc) | |
141 | + __ccs_free_task_security(p); | |
142 | + } | |
143 | + return rc; | |
144 | +} | |
145 | + | |
146 | +/** | |
147 | + * ccs_task_free_security - Release memory for "struct task_struct". | |
148 | + * | |
149 | + * @p: Pointer to "struct task_struct". | |
150 | + * | |
151 | + * Returns nothing. | |
152 | + */ | |
153 | +static void ccs_task_free_security(struct task_struct *p) | |
154 | +{ | |
155 | + struct ccs_security *ptr = ccs_find_task_security(p); | |
156 | + struct ccs_execve *ee = ptr->ee; | |
157 | + if (original_task_free.task_free) | |
158 | + original_task_free.task_free(p); | |
159 | + /* | |
160 | + * Since an LSM hook for reverting domain transition is missing, | |
161 | + * ccs_finish_execve() is not called if exited immediately after | |
162 | + * execve() failed. | |
163 | + */ | |
164 | + if (ee) { | |
165 | + ccs_debug_trace("2"); | |
166 | + ccs_audit_free_execve(ee, false); | |
167 | + kfree(ee->handler_path); | |
168 | + kfree(ee); | |
169 | + ptr->ee = NULL; | |
170 | + } | |
171 | + __ccs_free_task_security(p); | |
172 | +} | |
173 | + | |
174 | +/** | |
175 | + * ccs_bprm_committing_creds - A hook which is called when do_execve() succeeded. | |
176 | + * | |
177 | + * @bprm: Pointer to "struct linux_binprm". | |
178 | + * | |
179 | + * Returns nothing. | |
180 | + */ | |
181 | +static void ccs_bprm_committing_creds(struct linux_binprm *bprm) | |
182 | +{ | |
183 | + ccs_clear_execve(0, ccs_current_security()); | |
184 | +} | |
185 | + | |
186 | +/** | |
187 | + * ccs_cred_prepare - Allocate memory for new credentials. | |
188 | + * | |
189 | + * @new: Pointer to "struct cred". | |
190 | + * @old: Pointer to "struct cred". | |
191 | + * @gfp: Memory allocation flags. | |
192 | + * | |
193 | + * Returns 0 on success, negative value otherwise. | |
194 | + */ | |
195 | +static int ccs_cred_prepare(struct cred *new, const struct cred *old, | |
196 | + gfp_t gfp) | |
197 | +{ | |
198 | + /* | |
199 | + * For checking whether reverting domain transition is needed or not. | |
200 | + * | |
201 | + * See ccs_find_task_security() for reason. | |
202 | + */ | |
203 | + if (gfp == GFP_KERNEL) | |
204 | + ccs_find_task_security(current); | |
205 | + if (original_cred_prepare.cred_prepare) | |
206 | + return original_cred_prepare.cred_prepare(new, old, gfp); | |
207 | + return 0; | |
208 | +} | |
209 | + | |
210 | +/** | |
211 | + * ccs_bprm_check_security - Check permission for execve(). | |
212 | + * | |
213 | + * @bprm: Pointer to "struct linux_binprm". | |
214 | + * | |
215 | + * Returns 0 on success, negative value otherwise. | |
216 | + */ | |
217 | +static int ccs_bprm_check_security(struct linux_binprm *bprm) | |
218 | +{ | |
219 | + struct ccs_security *security = ccs_current_security(); | |
220 | + if (security == &ccs_default_security || security == &ccs_oom_security) | |
221 | + return -ENOMEM; | |
222 | + if (security->ee) | |
223 | + return 0; | |
224 | +#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
225 | + if (!ccs_policy_loaded) | |
226 | + ccs_load_policy(bprm->filename); | |
227 | +#endif | |
228 | + return ccs_start_execve(bprm, &security->ee); | |
229 | +} | |
230 | + | |
231 | +/** | |
232 | + * ccs_file_open - Check permission for open(). | |
233 | + * | |
234 | + * @f: Pointer to "struct file". | |
235 | + * @cred: Pointer to "struct cred". | |
236 | + * | |
237 | + * Returns 0 on success, negative value otherwise. | |
238 | + */ | |
239 | +static int ccs_file_open(struct file *f, const struct cred *cred) | |
240 | +{ | |
241 | + return ccs_open_permission(f); | |
242 | +} | |
243 | + | |
244 | +#ifdef CONFIG_SECURITY_PATH | |
245 | + | |
246 | +/** | |
247 | + * ccs_path_chown - Check permission for chown()/chgrp(). | |
248 | + * | |
249 | + * @path: Pointer to "struct path". | |
250 | + * @user: User ID. | |
251 | + * @group: Group ID. | |
252 | + * | |
253 | + * Returns 0 on success, negative value otherwise. | |
254 | + */ | |
255 | +static int ccs_path_chown(const struct path *path, kuid_t user, kgid_t group) | |
256 | +{ | |
257 | + return ccs_chown_permission(path->dentry, path->mnt, user, group); | |
258 | +} | |
259 | + | |
260 | +/** | |
261 | + * ccs_path_chmod - Check permission for chmod(). | |
262 | + * | |
263 | + * @path: Pointer to "struct path". | |
264 | + * @mode: Mode. | |
265 | + * | |
266 | + * Returns 0 on success, negative value otherwise. | |
267 | + */ | |
268 | +static int ccs_path_chmod(const struct path *path, umode_t mode) | |
269 | +{ | |
270 | + return ccs_chmod_permission(path->dentry, path->mnt, mode); | |
271 | +} | |
272 | + | |
273 | +/** | |
274 | + * ccs_path_chroot - Check permission for chroot(). | |
275 | + * | |
276 | + * @path: Pointer to "struct path". | |
277 | + * | |
278 | + * Returns 0 on success, negative value otherwise. | |
279 | + */ | |
280 | +static int ccs_path_chroot(const struct path *path) | |
281 | +{ | |
282 | + return ccs_chroot_permission(path); | |
283 | +} | |
284 | + | |
285 | +/** | |
286 | + * ccs_path_truncate - Check permission for truncate(). | |
287 | + * | |
288 | + * @path: Pointer to "struct path". | |
289 | + * | |
290 | + * Returns 0 on success, negative value otherwise. | |
291 | + */ | |
292 | +static int ccs_path_truncate(const struct path *path) | |
293 | +{ | |
294 | + return ccs_truncate_permission(path->dentry, path->mnt); | |
295 | +} | |
296 | + | |
297 | +#else | |
298 | + | |
299 | +/** | |
300 | + * ccs_inode_setattr - Check permission for chown()/chgrp()/chmod()/truncate(). | |
301 | + * | |
302 | + * @dentry: Pointer to "struct dentry". | |
303 | + * @attr: Pointer to "struct iattr". | |
304 | + * | |
305 | + * Returns 0 on success, negative value otherwise. | |
306 | + */ | |
307 | +static int ccs_inode_setattr(struct dentry *dentry, struct iattr *attr) | |
308 | +{ | |
309 | + const int rc1 = (attr->ia_valid & ATTR_UID) ? | |
310 | + ccs_chown_permission(dentry, NULL, attr->ia_uid, INVALID_GID) : | |
311 | + 0; | |
312 | + const int rc2 = (attr->ia_valid & ATTR_GID) ? | |
313 | + ccs_chown_permission(dentry, NULL, INVALID_UID, attr->ia_gid) : | |
314 | + 0; | |
315 | + const int rc3 = (attr->ia_valid & ATTR_MODE) ? | |
316 | + ccs_chmod_permission(dentry, NULL, attr->ia_mode) : 0; | |
317 | + const int rc4 = (attr->ia_valid & ATTR_SIZE) ? | |
318 | + ccs_truncate_permission(dentry, NULL) : 0; | |
319 | + if (rc4) | |
320 | + return rc4; | |
321 | + if (rc3) | |
322 | + return rc3; | |
323 | + if (rc2) | |
324 | + return rc2; | |
325 | + return rc1; | |
326 | +} | |
327 | + | |
328 | +#endif | |
329 | + | |
330 | +/** | |
331 | + * ccs_inode_getattr - Check permission for stat(). | |
332 | + * | |
333 | + * @path: Pointer to "struct path". | |
334 | + * | |
335 | + * Returns 0 on success, negative value otherwise. | |
336 | + */ | |
337 | +static int ccs_inode_getattr(const struct path *path) | |
338 | +{ | |
339 | + return ccs_getattr_permission(path->mnt, path->dentry); | |
340 | +} | |
341 | + | |
342 | +#ifdef CONFIG_SECURITY_PATH | |
343 | + | |
344 | +/** | |
345 | + * ccs_path_mknod - Check permission for mknod(). | |
346 | + * | |
347 | + * @dir: Pointer to "struct path". | |
348 | + * @dentry: Pointer to "struct dentry". | |
349 | + * @mode: Create mode. | |
350 | + * @dev: Device major/minor number. | |
351 | + * | |
352 | + * Returns 0 on success, negative value otherwise. | |
353 | + */ | |
354 | +static int ccs_path_mknod(const struct path *dir, struct dentry *dentry, | |
355 | + umode_t mode, unsigned int dev) | |
356 | +{ | |
357 | + return ccs_mknod_permission(dentry, dir->mnt, mode, dev); | |
358 | +} | |
359 | + | |
360 | +/** | |
361 | + * ccs_path_mkdir - Check permission for mkdir(). | |
362 | + * | |
363 | + * @dir: Pointer to "struct path". | |
364 | + * @dentry: Pointer to "struct dentry". | |
365 | + * @mode: Create mode. | |
366 | + * | |
367 | + * Returns 0 on success, negative value otherwise. | |
368 | + */ | |
369 | +static int ccs_path_mkdir(const struct path *dir, struct dentry *dentry, | |
370 | + umode_t mode) | |
371 | +{ | |
372 | + return ccs_mkdir_permission(dentry, dir->mnt, mode); | |
373 | +} | |
374 | + | |
375 | +/** | |
376 | + * ccs_path_rmdir - Check permission for rmdir(). | |
377 | + * | |
378 | + * @dir: Pointer to "struct path". | |
379 | + * @dentry: Pointer to "struct dentry". | |
380 | + * | |
381 | + * Returns 0 on success, negative value otherwise. | |
382 | + */ | |
383 | +static int ccs_path_rmdir(const struct path *dir, struct dentry *dentry) | |
384 | +{ | |
385 | + return ccs_rmdir_permission(dentry, dir->mnt); | |
386 | +} | |
387 | + | |
388 | +/** | |
389 | + * ccs_path_unlink - Check permission for unlink(). | |
390 | + * | |
391 | + * @dir: Pointer to "struct path". | |
392 | + * @dentry: Pointer to "struct dentry". | |
393 | + * | |
394 | + * Returns 0 on success, negative value otherwise. | |
395 | + */ | |
396 | +static int ccs_path_unlink(const struct path *dir, struct dentry *dentry) | |
397 | +{ | |
398 | + return ccs_unlink_permission(dentry, dir->mnt); | |
399 | +} | |
400 | + | |
401 | +/** | |
402 | + * ccs_path_symlink - Check permission for symlink(). | |
403 | + * | |
404 | + * @dir: Pointer to "struct path". | |
405 | + * @dentry: Pointer to "struct dentry". | |
406 | + * @old_name: Content of symbolic link. | |
407 | + * | |
408 | + * Returns 0 on success, negative value otherwise. | |
409 | + */ | |
410 | +static int ccs_path_symlink(const struct path *dir, struct dentry *dentry, | |
411 | + const char *old_name) | |
412 | +{ | |
413 | + return ccs_symlink_permission(dentry, dir->mnt, old_name); | |
414 | +} | |
415 | + | |
416 | +/** | |
417 | + * ccs_path_rename - Check permission for rename(). | |
418 | + * | |
419 | + * @old_dir: Pointer to "struct path". | |
420 | + * @old_dentry: Pointer to "struct dentry". | |
421 | + * @new_dir: Pointer to "struct path". | |
422 | + * @new_dentry: Pointer to "struct dentry". | |
423 | + * | |
424 | + * Returns 0 on success, negative value otherwise. | |
425 | + */ | |
426 | +static int ccs_path_rename(const struct path *old_dir, | |
427 | + struct dentry *old_dentry, | |
428 | + const struct path *new_dir, | |
429 | + struct dentry *new_dentry) | |
430 | +{ | |
431 | + return ccs_rename_permission(old_dentry, new_dentry, old_dir->mnt); | |
432 | +} | |
433 | + | |
434 | +/** | |
435 | + * ccs_path_link - Check permission for link(). | |
436 | + * | |
437 | + * @old_dentry: Pointer to "struct dentry". | |
438 | + * @new_dir: Pointer to "struct path". | |
439 | + * @new_dentry: Pointer to "struct dentry". | |
440 | + * | |
441 | + * Returns 0 on success, negative value otherwise. | |
442 | + */ | |
443 | +static int ccs_path_link(struct dentry *old_dentry, const struct path *new_dir, | |
444 | + struct dentry *new_dentry) | |
445 | +{ | |
446 | + return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt); | |
447 | +} | |
448 | + | |
449 | +#else | |
450 | + | |
451 | +/** | |
452 | + * ccs_inode_mknod - Check permission for mknod(). | |
453 | + * | |
454 | + * @dir: Pointer to "struct inode". | |
455 | + * @dentry: Pointer to "struct dentry". | |
456 | + * @mode: Create mode. | |
457 | + * @dev: Device major/minor number. | |
458 | + * | |
459 | + * Returns 0 on success, negative value otherwise. | |
460 | + */ | |
461 | +static int ccs_inode_mknod(struct inode *dir, struct dentry *dentry, | |
462 | + umode_t mode, dev_t dev) | |
463 | +{ | |
464 | + return ccs_mknod_permission(dentry, NULL, mode, dev); | |
465 | +} | |
466 | + | |
467 | +/** | |
468 | + * ccs_inode_mkdir - Check permission for mkdir(). | |
469 | + * | |
470 | + * @dir: Pointer to "struct inode". | |
471 | + * @dentry: Pointer to "struct dentry". | |
472 | + * @mode: Create mode. | |
473 | + * | |
474 | + * Returns 0 on success, negative value otherwise. | |
475 | + */ | |
476 | +static int ccs_inode_mkdir(struct inode *dir, struct dentry *dentry, | |
477 | + umode_t mode) | |
478 | +{ | |
479 | + return ccs_mkdir_permission(dentry, NULL, mode); | |
480 | +} | |
481 | + | |
482 | +/** | |
483 | + * ccs_inode_rmdir - Check permission for rmdir(). | |
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_rmdir(struct inode *dir, struct dentry *dentry) | |
491 | +{ | |
492 | + return ccs_rmdir_permission(dentry, NULL); | |
493 | +} | |
494 | + | |
495 | +/** | |
496 | + * ccs_inode_unlink - Check permission for unlink(). | |
497 | + * | |
498 | + * @dir: Pointer to "struct inode". | |
499 | + * @dentry: Pointer to "struct dentry". | |
500 | + * | |
501 | + * Returns 0 on success, negative value otherwise. | |
502 | + */ | |
503 | +static int ccs_inode_unlink(struct inode *dir, struct dentry *dentry) | |
504 | +{ | |
505 | + return ccs_unlink_permission(dentry, NULL); | |
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 | + * @old_name: Content of symbolic link. | |
514 | + * | |
515 | + * Returns 0 on success, negative value otherwise. | |
516 | + */ | |
517 | +static int ccs_inode_symlink(struct inode *dir, struct dentry *dentry, | |
518 | + const char *old_name) | |
519 | +{ | |
520 | + return ccs_symlink_permission(dentry, NULL, old_name); | |
521 | +} | |
522 | + | |
523 | +/** | |
524 | + * ccs_inode_rename - Check permission for rename(). | |
525 | + * | |
526 | + * @old_dir: Pointer to "struct inode". | |
527 | + * @old_dentry: Pointer to "struct dentry". | |
528 | + * @new_dir: Pointer to "struct inode". | |
529 | + * @new_dentry: Pointer to "struct dentry". | |
530 | + * | |
531 | + * Returns 0 on success, negative value otherwise. | |
532 | + */ | |
533 | +static int ccs_inode_rename(struct inode *old_dir, struct dentry *old_dentry, | |
534 | + struct inode *new_dir, struct dentry *new_dentry) | |
535 | +{ | |
536 | + return ccs_rename_permission(old_dentry, new_dentry, NULL); | |
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 | + return ccs_link_permission(old_dentry, new_dentry, NULL); | |
552 | +} | |
553 | + | |
554 | +/** | |
555 | + * ccs_inode_create - Check permission for creat(). | |
556 | + * | |
557 | + * @dir: Pointer to "struct inode". | |
558 | + * @dentry: Pointer to "struct dentry". | |
559 | + * @mode: Create mode. | |
560 | + * | |
561 | + * Returns 0 on success, negative value otherwise. | |
562 | + */ | |
563 | +static int ccs_inode_create(struct inode *dir, struct dentry *dentry, | |
564 | + umode_t mode) | |
565 | +{ | |
566 | + return ccs_mknod_permission(dentry, NULL, mode, 0); | |
567 | +} | |
568 | + | |
569 | +#endif | |
570 | + | |
571 | +#ifdef CONFIG_SECURITY_NETWORK | |
572 | + | |
573 | +#include <net/sock.h> | |
574 | + | |
575 | +/* Structure for remembering an accept()ed socket's status. */ | |
576 | +struct ccs_socket_tag { | |
577 | + struct list_head list; | |
578 | + struct inode *inode; | |
579 | + int status; | |
580 | + struct rcu_head rcu; | |
581 | +}; | |
582 | + | |
583 | +/* | |
584 | + * List for managing accept()ed sockets. | |
585 | + * Since we don't need to keep an accept()ed socket into this list after | |
586 | + * once the permission was granted, the number of entries in this list is | |
587 | + * likely small. Therefore, we don't use hash tables. | |
588 | + */ | |
589 | +static LIST_HEAD(ccs_accepted_socket_list); | |
590 | +/* Lock for protecting ccs_accepted_socket_list . */ | |
591 | +static DEFINE_SPINLOCK(ccs_accepted_socket_list_lock); | |
592 | + | |
593 | +/** | |
594 | + * ccs_update_socket_tag - Update tag associated with accept()ed sockets. | |
595 | + * | |
596 | + * @inode: Pointer to "struct inode". | |
597 | + * @status: New status. | |
598 | + * | |
599 | + * Returns nothing. | |
600 | + * | |
601 | + * If @status == 0, memory for that socket will be released after RCU grace | |
602 | + * period. | |
603 | + */ | |
604 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
605 | +{ | |
606 | + struct ccs_socket_tag *ptr; | |
607 | + /* | |
608 | + * Protect whole section because multiple threads may call this | |
609 | + * function with same "sock" via ccs_validate_socket(). | |
610 | + */ | |
611 | + spin_lock(&ccs_accepted_socket_list_lock); | |
612 | + rcu_read_lock(); | |
613 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
614 | + if (ptr->inode != inode) | |
615 | + continue; | |
616 | + ptr->status = status; | |
617 | + if (status) | |
618 | + break; | |
619 | + list_del_rcu(&ptr->list); | |
620 | + kfree_rcu(ptr, rcu); | |
621 | + break; | |
622 | + } | |
623 | + rcu_read_unlock(); | |
624 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
625 | +} | |
626 | + | |
627 | +/** | |
628 | + * ccs_validate_socket - Check post accept() permission if needed. | |
629 | + * | |
630 | + * @sock: Pointer to "struct socket". | |
631 | + * | |
632 | + * Returns 0 on success, negative value otherwise. | |
633 | + */ | |
634 | +static int ccs_validate_socket(struct socket *sock) | |
635 | +{ | |
636 | + struct inode *inode = SOCK_INODE(sock); | |
637 | + struct ccs_socket_tag *ptr; | |
638 | + int ret = 0; | |
639 | + rcu_read_lock(); | |
640 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
641 | + if (ptr->inode != inode) | |
642 | + continue; | |
643 | + ret = ptr->status; | |
644 | + break; | |
645 | + } | |
646 | + rcu_read_unlock(); | |
647 | + if (ret <= 0) | |
648 | + /* | |
649 | + * This socket is not an accept()ed socket or this socket is | |
650 | + * an accept()ed socket and post accept() permission is done. | |
651 | + */ | |
652 | + return ret; | |
653 | + /* | |
654 | + * Check post accept() permission now. | |
655 | + * | |
656 | + * Strictly speaking, we need to pass both listen()ing socket and | |
657 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
658 | + * But since socket's family and type are same for both sockets, | |
659 | + * passing the accept()ed socket in place for the listen()ing socket | |
660 | + * will work. | |
661 | + */ | |
662 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
663 | + /* | |
664 | + * If permission was granted, we forget that this is an accept()ed | |
665 | + * socket. Otherwise, we remember that this socket needs to return | |
666 | + * error for subsequent socketcalls. | |
667 | + */ | |
668 | + ccs_update_socket_tag(inode, ret); | |
669 | + return ret; | |
670 | +} | |
671 | + | |
672 | +/** | |
673 | + * ccs_socket_accept - Check permission for accept(). | |
674 | + * | |
675 | + * @sock: Pointer to "struct socket". | |
676 | + * @newsock: Pointer to "struct socket". | |
677 | + * | |
678 | + * Returns 0 on success, negative value otherwise. | |
679 | + * | |
680 | + * This hook is used for setting up environment for doing post accept() | |
681 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
682 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
683 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
684 | + * in order to do post accept() permission check before returning to userspace. | |
685 | + * If we make the copy in security_socket_post_create(), it would be possible | |
686 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
687 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
688 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
689 | + * rather than between sock->ops->accept() and returning to userspace. | |
690 | + * This means that if a socket was close()d before calling some socket | |
691 | + * syscalls, post accept() permission check will not be done. | |
692 | + */ | |
693 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
694 | +{ | |
695 | + struct ccs_socket_tag *ptr; | |
696 | + const int rc = ccs_validate_socket(sock); | |
697 | + if (rc < 0) | |
698 | + return rc; | |
699 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
700 | + if (!ptr) | |
701 | + return -ENOMEM; | |
702 | + /* | |
703 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
704 | + * "newsock" as "an accept()ed socket but post accept() permission | |
705 | + * check is not done yet" by allocating memory using inode of the | |
706 | + * "newsock" as a search key. | |
707 | + */ | |
708 | + ptr->inode = SOCK_INODE(newsock); | |
709 | + ptr->status = 1; /* Check post accept() permission later. */ | |
710 | + spin_lock(&ccs_accepted_socket_list_lock); | |
711 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
712 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
713 | + return 0; | |
714 | +} | |
715 | + | |
716 | +/** | |
717 | + * ccs_socket_listen - Check permission for listen(). | |
718 | + * | |
719 | + * @sock: Pointer to "struct socket". | |
720 | + * @backlog: Backlog parameter. | |
721 | + * | |
722 | + * Returns 0 on success, negative value otherwise. | |
723 | + */ | |
724 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
725 | +{ | |
726 | + const int rc = ccs_validate_socket(sock); | |
727 | + if (rc < 0) | |
728 | + return rc; | |
729 | + return ccs_socket_listen_permission(sock); | |
730 | +} | |
731 | + | |
732 | +/** | |
733 | + * ccs_socket_connect - Check permission for connect(). | |
734 | + * | |
735 | + * @sock: Pointer to "struct socket". | |
736 | + * @addr: Pointer to "struct sockaddr". | |
737 | + * @addr_len: Size of @addr. | |
738 | + * | |
739 | + * Returns 0 on success, negative value otherwise. | |
740 | + */ | |
741 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
742 | + int addr_len) | |
743 | +{ | |
744 | + const int rc = ccs_validate_socket(sock); | |
745 | + if (rc < 0) | |
746 | + return rc; | |
747 | + return ccs_socket_connect_permission(sock, addr, addr_len); | |
748 | +} | |
749 | + | |
750 | +/** | |
751 | + * ccs_socket_bind - Check permission for bind(). | |
752 | + * | |
753 | + * @sock: Pointer to "struct socket". | |
754 | + * @addr: Pointer to "struct sockaddr". | |
755 | + * @addr_len: Size of @addr. | |
756 | + * | |
757 | + * Returns 0 on success, negative value otherwise. | |
758 | + */ | |
759 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
760 | + int addr_len) | |
761 | +{ | |
762 | + const int rc = ccs_validate_socket(sock); | |
763 | + if (rc < 0) | |
764 | + return rc; | |
765 | + return ccs_socket_bind_permission(sock, addr, addr_len); | |
766 | +} | |
767 | + | |
768 | +/** | |
769 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
770 | + * | |
771 | + * @sock: Pointer to "struct socket". | |
772 | + * @msg: Pointer to "struct msghdr". | |
773 | + * @size: Size of message. | |
774 | + * | |
775 | + * Returns 0 on success, negative value otherwise. | |
776 | + */ | |
777 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
778 | + int size) | |
779 | +{ | |
780 | + const int rc = ccs_validate_socket(sock); | |
781 | + if (rc < 0) | |
782 | + return rc; | |
783 | + return ccs_socket_sendmsg_permission(sock, msg, size); | |
784 | +} | |
785 | + | |
786 | +/** | |
787 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
788 | + * | |
789 | + * @sock: Pointer to "struct socket". | |
790 | + * @msg: Pointer to "struct msghdr". | |
791 | + * @size: Size of message. | |
792 | + * @flags: Flags. | |
793 | + * | |
794 | + * Returns 0 on success, negative value otherwise. | |
795 | + */ | |
796 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
797 | + int size, int flags) | |
798 | +{ | |
799 | + return ccs_validate_socket(sock); | |
800 | +} | |
801 | + | |
802 | +/** | |
803 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
804 | + * | |
805 | + * @sock: Pointer to "struct socket". | |
806 | + * | |
807 | + * Returns 0 on success, negative value otherwise. | |
808 | + */ | |
809 | +static int ccs_socket_getsockname(struct socket *sock) | |
810 | +{ | |
811 | + return ccs_validate_socket(sock); | |
812 | +} | |
813 | + | |
814 | +/** | |
815 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
816 | + * | |
817 | + * @sock: Pointer to "struct socket". | |
818 | + * | |
819 | + * Returns 0 on success, negative value otherwise. | |
820 | + */ | |
821 | +static int ccs_socket_getpeername(struct socket *sock) | |
822 | +{ | |
823 | + return ccs_validate_socket(sock); | |
824 | +} | |
825 | + | |
826 | +/** | |
827 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
828 | + * | |
829 | + * @sock: Pointer to "struct socket". | |
830 | + * @level: Level. | |
831 | + * @optname: Option's name, | |
832 | + * | |
833 | + * Returns 0 on success, negative value otherwise. | |
834 | + */ | |
835 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
836 | +{ | |
837 | + return ccs_validate_socket(sock); | |
838 | +} | |
839 | + | |
840 | +/** | |
841 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
842 | + * | |
843 | + * @sock: Pointer to "struct socket". | |
844 | + * @level: Level. | |
845 | + * @optname: Option's name, | |
846 | + * | |
847 | + * Returns 0 on success, negative value otherwise. | |
848 | + */ | |
849 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
850 | +{ | |
851 | + return ccs_validate_socket(sock); | |
852 | +} | |
853 | + | |
854 | +/** | |
855 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
856 | + * | |
857 | + * @sock: Pointer to "struct socket". | |
858 | + * @how: Shutdown mode. | |
859 | + * | |
860 | + * Returns 0 on success, negative value otherwise. | |
861 | + */ | |
862 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
863 | +{ | |
864 | + return ccs_validate_socket(sock); | |
865 | +} | |
866 | + | |
867 | +#define SOCKFS_MAGIC 0x534F434B | |
868 | + | |
869 | +/** | |
870 | + * ccs_inode_free_security - Release memory associated with an inode. | |
871 | + * | |
872 | + * @inode: Pointer to "struct inode". | |
873 | + * | |
874 | + * Returns nothing. | |
875 | + * | |
876 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
877 | + */ | |
878 | +static void ccs_inode_free_security(struct inode *inode) | |
879 | +{ | |
880 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
881 | + ccs_update_socket_tag(inode, 0); | |
882 | +} | |
883 | + | |
884 | +#endif | |
885 | + | |
886 | +/** | |
887 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
888 | + * | |
889 | + * @old_path: Pointer to "struct path". | |
890 | + * @new_path: Pointer to "struct path". | |
891 | + * | |
892 | + * Returns 0 on success, negative value otherwise. | |
893 | + */ | |
894 | +static int ccs_sb_pivotroot(const struct path *old_path, | |
895 | + const struct path *new_path) | |
896 | +{ | |
897 | + return ccs_pivot_root_permission(old_path, new_path); | |
898 | +} | |
899 | + | |
900 | +/** | |
901 | + * ccs_sb_mount - Check permission for mount(). | |
902 | + * | |
903 | + * @dev_name: Name of device file. | |
904 | + * @path: Pointer to "struct path". | |
905 | + * @type: Name of filesystem type. Maybe NULL. | |
906 | + * @flags: Mount options. | |
907 | + * @data_page: Optional data. Maybe NULL. | |
908 | + * | |
909 | + * Returns 0 on success, negative value otherwise. | |
910 | + */ | |
911 | +static int ccs_sb_mount(const char *dev_name, const struct path *path, | |
912 | + const char *type, unsigned long flags, void *data_page) | |
913 | +{ | |
914 | + return ccs_mount_permission(dev_name, path, type, flags, data_page); | |
915 | +} | |
916 | + | |
917 | +/** | |
918 | + * ccs_sb_umount - Check permission for umount(). | |
919 | + * | |
920 | + * @mnt: Pointer to "struct vfsmount". | |
921 | + * @flags: Unmount flags. | |
922 | + * | |
923 | + * Returns 0 on success, negative value otherwise. | |
924 | + */ | |
925 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
926 | +{ | |
927 | + return ccs_umount_permission(mnt, flags); | |
928 | +} | |
929 | + | |
930 | +/** | |
931 | + * ccs_file_fcntl - Check permission for fcntl(). | |
932 | + * | |
933 | + * @file: Pointer to "struct file". | |
934 | + * @cmd: Command number. | |
935 | + * @arg: Value for @cmd. | |
936 | + * | |
937 | + * Returns 0 on success, negative value otherwise. | |
938 | + */ | |
939 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
940 | + unsigned long arg) | |
941 | +{ | |
942 | + return ccs_fcntl_permission(file, cmd, arg); | |
943 | +} | |
944 | + | |
945 | +/** | |
946 | + * ccs_file_ioctl - Check permission for ioctl(). | |
947 | + * | |
948 | + * @filp: Pointer to "struct file". | |
949 | + * @cmd: Command number. | |
950 | + * @arg: Value for @cmd. | |
951 | + * | |
952 | + * Returns 0 on success, negative value otherwise. | |
953 | + */ | |
954 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
955 | + unsigned long arg) | |
956 | +{ | |
957 | + return ccs_ioctl_permission(filp, cmd, arg); | |
958 | +} | |
959 | + | |
960 | +#define MY_HOOK_INIT(HEAD, HOOK) \ | |
961 | + { .head = &probe_dummy_security_hook_heads.HEAD, \ | |
962 | + .hook = { .HEAD = HOOK } } | |
963 | + | |
964 | +static struct security_hook_list akari_hooks[] = { | |
965 | + /* Security context allocator. */ | |
966 | + MY_HOOK_INIT(task_free, ccs_task_free_security), | |
967 | + MY_HOOK_INIT(cred_prepare, ccs_cred_prepare), | |
968 | + MY_HOOK_INIT(task_alloc, ccs_task_alloc_security), | |
969 | + /* Security context updater for successful execve(). */ | |
970 | + MY_HOOK_INIT(bprm_check_security, ccs_bprm_check_security), | |
971 | + MY_HOOK_INIT(bprm_committing_creds, ccs_bprm_committing_creds), | |
972 | + /* Various permission checker. */ | |
973 | + MY_HOOK_INIT(file_open, ccs_file_open), | |
974 | + MY_HOOK_INIT(file_fcntl, ccs_file_fcntl), | |
975 | + MY_HOOK_INIT(file_ioctl, ccs_file_ioctl), | |
976 | + MY_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), | |
977 | + MY_HOOK_INIT(sb_mount, ccs_sb_mount), | |
978 | + MY_HOOK_INIT(sb_umount, ccs_sb_umount), | |
979 | +#ifdef CONFIG_SECURITY_PATH | |
980 | + MY_HOOK_INIT(path_mknod, ccs_path_mknod), | |
981 | + MY_HOOK_INIT(path_mkdir, ccs_path_mkdir), | |
982 | + MY_HOOK_INIT(path_rmdir, ccs_path_rmdir), | |
983 | + MY_HOOK_INIT(path_unlink, ccs_path_unlink), | |
984 | + MY_HOOK_INIT(path_symlink, ccs_path_symlink), | |
985 | + MY_HOOK_INIT(path_rename, ccs_path_rename), | |
986 | + MY_HOOK_INIT(path_link, ccs_path_link), | |
987 | + MY_HOOK_INIT(path_truncate, ccs_path_truncate), | |
988 | + MY_HOOK_INIT(path_chmod, ccs_path_chmod), | |
989 | + MY_HOOK_INIT(path_chown, ccs_path_chown), | |
990 | + MY_HOOK_INIT(path_chroot, ccs_path_chroot), | |
991 | +#else | |
992 | + MY_HOOK_INIT(inode_mknod, ccs_inode_mknod), | |
993 | + MY_HOOK_INIT(inode_mkdir, ccs_inode_mkdir), | |
994 | + MY_HOOK_INIT(inode_rmdir, ccs_inode_rmdir), | |
995 | + MY_HOOK_INIT(inode_unlink, ccs_inode_unlink), | |
996 | + MY_HOOK_INIT(inode_symlink, ccs_inode_symlink), | |
997 | + MY_HOOK_INIT(inode_rename, ccs_inode_rename), | |
998 | + MY_HOOK_INIT(inode_link, ccs_inode_link), | |
999 | + MY_HOOK_INIT(inode_create, ccs_inode_create), | |
1000 | + MY_HOOK_INIT(inode_setattr, ccs_inode_setattr), | |
1001 | +#endif | |
1002 | + MY_HOOK_INIT(inode_getattr, ccs_inode_getattr), | |
1003 | +#ifdef CONFIG_SECURITY_NETWORK | |
1004 | + MY_HOOK_INIT(socket_bind, ccs_socket_bind), | |
1005 | + MY_HOOK_INIT(socket_connect, ccs_socket_connect), | |
1006 | + MY_HOOK_INIT(socket_listen, ccs_socket_listen), | |
1007 | + MY_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), | |
1008 | + MY_HOOK_INIT(socket_recvmsg, ccs_socket_recvmsg), | |
1009 | + MY_HOOK_INIT(socket_getsockname, ccs_socket_getsockname), | |
1010 | + MY_HOOK_INIT(socket_getpeername, ccs_socket_getpeername), | |
1011 | + MY_HOOK_INIT(socket_getsockopt, ccs_socket_getsockopt), | |
1012 | + MY_HOOK_INIT(socket_setsockopt, ccs_socket_setsockopt), | |
1013 | + MY_HOOK_INIT(socket_shutdown, ccs_socket_shutdown), | |
1014 | + MY_HOOK_INIT(socket_accept, ccs_socket_accept), | |
1015 | + MY_HOOK_INIT(inode_free_security, ccs_inode_free_security), | |
1016 | +#endif | |
1017 | +}; | |
1018 | + | |
1019 | +static inline void add_hook(struct security_hook_list *hook) | |
1020 | +{ | |
1021 | + list_add_tail_rcu(&hook->list, hook->head); | |
1022 | +} | |
1023 | + | |
1024 | +static void __init swap_hook(struct security_hook_list *hook, | |
1025 | + union security_list_options *original) | |
1026 | +{ | |
1027 | + struct list_head *list = hook->head; | |
1028 | + if (list_empty(list)) { | |
1029 | + add_hook(hook); | |
1030 | + } else { | |
1031 | + struct security_hook_list *shp = | |
1032 | + list_last_entry(list, struct security_hook_list, list); | |
1033 | + *original = shp->hook; | |
1034 | + smp_wmb(); | |
1035 | + shp->hook = hook->hook; | |
1036 | + } | |
1037 | +} | |
1038 | + | |
1039 | +#if defined(CONFIG_STRICT_KERNEL_RWX) && !defined(SECURITY_WRITABLE_HOOKS) | |
1040 | +#include <linux/uaccess.h> /* probe_kernel_write() */ | |
1041 | +#define NEED_TO_CHECK_HOOKS_ARE_WRITABLE | |
1042 | + | |
1043 | +#if defined(CONFIG_X86) | |
1044 | +#define MAX_RO_PAGES 1024 | |
1045 | +static struct page *ro_pages[MAX_RO_PAGES] __initdata; | |
1046 | +static unsigned int ro_pages_len __initdata; | |
1047 | + | |
1048 | +static bool __init lsm_test_page_ro(void *addr) | |
1049 | +{ | |
1050 | + unsigned int i; | |
1051 | + int unused; | |
1052 | + struct page *page; | |
1053 | + | |
1054 | + page = (struct page *) lookup_address((unsigned long) addr, &unused); | |
1055 | + if (!page) | |
1056 | + return false; | |
1057 | + if (test_bit(_PAGE_BIT_RW, &(page->flags))) | |
1058 | + return true; | |
1059 | + for (i = 0; i < ro_pages_len; i++) | |
1060 | + if (page == ro_pages[i]) | |
1061 | + return true; | |
1062 | + if (ro_pages_len == MAX_RO_PAGES) | |
1063 | + return false; | |
1064 | + ro_pages[ro_pages_len++] = page; | |
1065 | + return true; | |
1066 | +} | |
1067 | + | |
1068 | +static bool __init check_ro_pages(struct security_hook_heads *hooks) | |
1069 | +{ | |
1070 | + int i; | |
1071 | + struct list_head *list = (struct list_head *) hooks; | |
1072 | + | |
1073 | + if (!probe_kernel_write(&list->next, list->next, sizeof(void *))) | |
1074 | + return true; | |
1075 | + for (i = 0; i < ARRAY_SIZE(akari_hooks); i++) { | |
1076 | + const unsigned int idx = | |
1077 | + ((unsigned long) akari_hooks[i].head | |
1078 | + - (unsigned long) hooks) | |
1079 | + / sizeof(struct list_head); | |
1080 | + struct list_head *self = &list[idx]; | |
1081 | + struct list_head *prev = self->prev; | |
1082 | + | |
1083 | + if (!lsm_test_page_ro(&prev->next) || | |
1084 | + !lsm_test_page_ro(&self->prev)) | |
1085 | + return false; | |
1086 | + if (!list_empty(self) && | |
1087 | + !lsm_test_page_ro(&list_last_entry | |
1088 | + (self, struct security_hook_list, | |
1089 | + list)->hook)) | |
1090 | + return false; | |
1091 | + } | |
1092 | + return true; | |
1093 | +} | |
1094 | +#else | |
1095 | +static bool __init check_ro_pages(struct security_hook_heads *hooks) | |
1096 | +{ | |
1097 | + return !probe_kernel_write(&((struct list_head *) hooks)->next, | |
1098 | + ((struct list_head *) hooks)->next, | |
1099 | + sizeof(void *)); | |
1100 | +} | |
1101 | +#endif | |
1102 | +#endif | |
1103 | + | |
1104 | +/** | |
1105 | + * ccs_init - Initialize this module. | |
1106 | + * | |
1107 | + * Returns 0 on success, negative value otherwise. | |
1108 | + */ | |
1109 | +static int __init ccs_init(void) | |
1110 | +{ | |
1111 | + int idx; | |
1112 | + struct security_hook_heads *hooks = probe_security_hook_heads(); | |
1113 | + if (!hooks) | |
1114 | + goto out; | |
1115 | + for (idx = 0; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1116 | + akari_hooks[idx].head = ((void *) hooks) | |
1117 | + + ((unsigned long) akari_hooks[idx].head) | |
1118 | + - ((unsigned long) &probe_dummy_security_hook_heads); | |
1119 | +#if defined(NEED_TO_CHECK_HOOKS_ARE_WRITABLE) | |
1120 | + if (!check_ro_pages(hooks)) { | |
1121 | + printk(KERN_INFO "Can't update security_hook_heads due to write protected. Retry with rodata=0 kernel command line option added.\n"); | |
1122 | + return -EINVAL; | |
1123 | + } | |
1124 | +#endif | |
1125 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1126 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1127 | + goto out; | |
1128 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1129 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1130 | + goto out; | |
1131 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1132 | + if (!ccsecurity_exports.d_absolute_path) | |
1133 | + goto out; | |
1134 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) | |
1135 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1136 | + ccs_main_init(); | |
1137 | +#if defined(NEED_TO_CHECK_HOOKS_ARE_WRITABLE) && defined(CONFIG_X86) | |
1138 | + for (idx = 0; idx < ro_pages_len; idx++) | |
1139 | + set_bit(_PAGE_BIT_RW, &(ro_pages[idx]->flags)); | |
1140 | +#endif | |
1141 | + swap_hook(&akari_hooks[0], &original_task_free); | |
1142 | + swap_hook(&akari_hooks[1], &original_cred_prepare); | |
1143 | + swap_hook(&akari_hooks[2], &original_task_alloc); | |
1144 | + for (idx = 3; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1145 | + add_hook(&akari_hooks[idx]); | |
1146 | +#if defined(NEED_TO_CHECK_HOOKS_ARE_WRITABLE) && defined(CONFIG_X86) | |
1147 | + for (idx = 0; idx < ro_pages_len; idx++) | |
1148 | + clear_bit(_PAGE_BIT_RW, &(ro_pages[idx]->flags)); | |
1149 | +#endif | |
1150 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1151 | + printk(KERN_INFO | |
1152 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1153 | + return 0; | |
1154 | +out: | |
1155 | + return -EINVAL; | |
1156 | +} | |
1157 | + | |
1158 | +module_init(ccs_init); | |
1159 | +MODULE_LICENSE("GPL"); | |
1160 | + | |
1161 | +/** | |
1162 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1163 | + * | |
1164 | + * @domain: Pointer to "struct ccs_domain_info". | |
1165 | + * | |
1166 | + * Returns true if @domain is in use, false otherwise. | |
1167 | + * | |
1168 | + * Caller holds rcu_read_lock(). | |
1169 | + */ | |
1170 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1171 | +{ | |
1172 | + return false; | |
1173 | +} | |
1174 | + | |
1175 | +/** | |
1176 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1177 | + * | |
1178 | + * @ptr: Pointer to "struct ccs_security". | |
1179 | + * @list: Pointer to "struct list_head". | |
1180 | + * | |
1181 | + * Returns nothing. | |
1182 | + */ | |
1183 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1184 | + struct list_head *list) | |
1185 | +{ | |
1186 | + unsigned long flags; | |
1187 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1188 | + list_add_rcu(&ptr->list, list); | |
1189 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1190 | +} | |
1191 | + | |
1192 | +/** | |
1193 | + * __ccs_alloc_task_security - Allocate memory for new tasks. | |
1194 | + * | |
1195 | + * @task: Pointer to "struct task_struct". | |
1196 | + * | |
1197 | + * Returns 0 on success, negative value otherwise. | |
1198 | + */ | |
1199 | +static int __ccs_alloc_task_security(const struct task_struct *task) | |
1200 | +{ | |
1201 | + struct ccs_security *old_security = ccs_current_security(); | |
1202 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
1203 | + GFP_KERNEL); | |
1204 | + struct list_head *list = &ccs_task_security_list | |
1205 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1206 | + if (!new_security) | |
1207 | + return -ENOMEM; | |
1208 | + new_security->task = task; | |
1209 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
1210 | + new_security->ccs_flags = old_security->ccs_flags; | |
1211 | + ccs_add_task_security(new_security, list); | |
1212 | + return 0; | |
1213 | +} | |
1214 | + | |
1215 | +/** | |
1216 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1217 | + * | |
1218 | + * @task: Pointer to "struct task_struct". | |
1219 | + * | |
1220 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1221 | + * out of memory, &ccs_default_security otherwise. | |
1222 | + * | |
1223 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1224 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1225 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1226 | + * won't work. | |
1227 | + */ | |
1228 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1229 | +{ | |
1230 | + struct ccs_security *ptr; | |
1231 | + struct list_head *list = &ccs_task_security_list | |
1232 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1233 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1234 | + while (!list->next); | |
1235 | + rcu_read_lock(); | |
1236 | + list_for_each_entry_rcu(ptr, list, list) { | |
1237 | + if (ptr->task != task) | |
1238 | + continue; | |
1239 | + rcu_read_unlock(); | |
1240 | + /* | |
1241 | + * Current thread needs to transit from old domain to new | |
1242 | + * domain before do_execve() succeeds in order to check | |
1243 | + * permission for interpreters and environment variables using | |
1244 | + * new domain's ACL rules. The domain transition has to be | |
1245 | + * visible from other CPU in order to allow interactive | |
1246 | + * enforcing mode. Also, the domain transition has to be | |
1247 | + * reverted if do_execve() failed. However, an LSM hook for | |
1248 | + * reverting domain transition is missing. | |
1249 | + * | |
1250 | + * security_prepare_creds() is called from prepare_creds() from | |
1251 | + * prepare_bprm_creds() from do_execve() before setting | |
1252 | + * current->in_execve flag, and current->in_execve flag is | |
1253 | + * cleared by the time next do_execve() request starts. | |
1254 | + * This means that we can emulate the missing LSM hook for | |
1255 | + * reverting domain transition, by calling this function from | |
1256 | + * security_prepare_creds(). | |
1257 | + * | |
1258 | + * If current->in_execve is not set but ptr->ccs_flags has | |
1259 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
1260 | + * has failed and reverting domain transition is needed. | |
1261 | + */ | |
1262 | + if (task == current && | |
1263 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
1264 | + !current->in_execve) { | |
1265 | + ccs_debug_trace("1"); | |
1266 | + ccs_clear_execve(-1, ptr); | |
1267 | + } | |
1268 | + return ptr; | |
1269 | + } | |
1270 | + rcu_read_unlock(); | |
1271 | + if (task != current) | |
1272 | + return &ccs_default_security; | |
1273 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1274 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1275 | + if (!ptr) { | |
1276 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1277 | + task->pid); | |
1278 | + send_sig(SIGKILL, current, 0); | |
1279 | + return &ccs_oom_security; | |
1280 | + } | |
1281 | + *ptr = ccs_default_security; | |
1282 | + ptr->task = task; | |
1283 | + ccs_add_task_security(ptr, list); | |
1284 | + return ptr; | |
1285 | +} | |
1286 | + | |
1287 | +/** | |
1288 | + * __ccs_free_task_security - Release memory associated with "struct task_struct". | |
1289 | + * | |
1290 | + * @task: Pointer to "struct task_struct". | |
1291 | + * | |
1292 | + * Returns nothing. | |
1293 | + */ | |
1294 | +static void __ccs_free_task_security(const struct task_struct *task) | |
1295 | +{ | |
1296 | + unsigned long flags; | |
1297 | + struct ccs_security *ptr = ccs_find_task_security(task); | |
1298 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
1299 | + return; | |
1300 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1301 | + list_del_rcu(&ptr->list); | |
1302 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1303 | + kfree_rcu(ptr, rcu); | |
1304 | +} |
@@ -0,0 +1,1478 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
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_update_socket_tag - Update tag associated with accept()ed sockets. | |
763 | + * | |
764 | + * @inode: Pointer to "struct inode". | |
765 | + * @status: New status. | |
766 | + * | |
767 | + * Returns nothing. | |
768 | + * | |
769 | + * If @status == 0, memory for that socket will be released after RCU grace | |
770 | + * period. | |
771 | + */ | |
772 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
773 | +{ | |
774 | + struct ccs_socket_tag *ptr; | |
775 | + /* | |
776 | + * Protect whole section because multiple threads may call this | |
777 | + * function with same "sock" via ccs_validate_socket(). | |
778 | + */ | |
779 | + spin_lock(&ccs_accepted_socket_list_lock); | |
780 | + rcu_read_lock(); | |
781 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
782 | + if (ptr->inode != inode) | |
783 | + continue; | |
784 | + ptr->status = status; | |
785 | + if (status) | |
786 | + break; | |
787 | + list_del_rcu(&ptr->list); | |
788 | + kfree_rcu(ptr, rcu); | |
789 | + break; | |
790 | + } | |
791 | + rcu_read_unlock(); | |
792 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
793 | +} | |
794 | + | |
795 | +/** | |
796 | + * ccs_validate_socket - Check post accept() permission if needed. | |
797 | + * | |
798 | + * @sock: Pointer to "struct socket". | |
799 | + * | |
800 | + * Returns 0 on success, negative value otherwise. | |
801 | + */ | |
802 | +static int ccs_validate_socket(struct socket *sock) | |
803 | +{ | |
804 | + struct inode *inode = SOCK_INODE(sock); | |
805 | + struct ccs_socket_tag *ptr; | |
806 | + int ret = 0; | |
807 | + rcu_read_lock(); | |
808 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
809 | + if (ptr->inode != inode) | |
810 | + continue; | |
811 | + ret = ptr->status; | |
812 | + break; | |
813 | + } | |
814 | + rcu_read_unlock(); | |
815 | + if (ret <= 0) | |
816 | + /* | |
817 | + * This socket is not an accept()ed socket or this socket is | |
818 | + * an accept()ed socket and post accept() permission is done. | |
819 | + */ | |
820 | + return ret; | |
821 | + /* | |
822 | + * Check post accept() permission now. | |
823 | + * | |
824 | + * Strictly speaking, we need to pass both listen()ing socket and | |
825 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
826 | + * But since socket's family and type are same for both sockets, | |
827 | + * passing the accept()ed socket in place for the listen()ing socket | |
828 | + * will work. | |
829 | + */ | |
830 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
831 | + /* | |
832 | + * If permission was granted, we forget that this is an accept()ed | |
833 | + * socket. Otherwise, we remember that this socket needs to return | |
834 | + * error for subsequent socketcalls. | |
835 | + */ | |
836 | + ccs_update_socket_tag(inode, ret); | |
837 | + return ret; | |
838 | +} | |
839 | + | |
840 | +/** | |
841 | + * ccs_socket_accept - Check permission for accept(). | |
842 | + * | |
843 | + * @sock: Pointer to "struct socket". | |
844 | + * @newsock: Pointer to "struct socket". | |
845 | + * | |
846 | + * Returns 0 on success, negative value otherwise. | |
847 | + * | |
848 | + * This hook is used for setting up environment for doing post accept() | |
849 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
850 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
851 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
852 | + * in order to do post accept() permission check before returning to userspace. | |
853 | + * If we make the copy in security_socket_post_create(), it would be possible | |
854 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
855 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
856 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
857 | + * rather than between sock->ops->accept() and returning to userspace. | |
858 | + * This means that if a socket was close()d before calling some socket | |
859 | + * syscalls, post accept() permission check will not be done. | |
860 | + */ | |
861 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
862 | +{ | |
863 | + struct ccs_socket_tag *ptr; | |
864 | + const int rc = ccs_validate_socket(sock); | |
865 | + if (rc < 0) | |
866 | + return rc; | |
867 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
868 | + if (!ptr) | |
869 | + return -ENOMEM; | |
870 | + /* | |
871 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
872 | + * "newsock" as "an accept()ed socket but post accept() permission | |
873 | + * check is not done yet" by allocating memory using inode of the | |
874 | + * "newsock" as a search key. | |
875 | + */ | |
876 | + ptr->inode = SOCK_INODE(newsock); | |
877 | + ptr->status = 1; /* Check post accept() permission later. */ | |
878 | + spin_lock(&ccs_accepted_socket_list_lock); | |
879 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
880 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
881 | + return 0; | |
882 | +} | |
883 | + | |
884 | +/** | |
885 | + * ccs_socket_listen - Check permission for listen(). | |
886 | + * | |
887 | + * @sock: Pointer to "struct socket". | |
888 | + * @backlog: Backlog parameter. | |
889 | + * | |
890 | + * Returns 0 on success, negative value otherwise. | |
891 | + */ | |
892 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
893 | +{ | |
894 | + const int rc = ccs_validate_socket(sock); | |
895 | + if (rc < 0) | |
896 | + return rc; | |
897 | + return ccs_socket_listen_permission(sock); | |
898 | +} | |
899 | + | |
900 | +/** | |
901 | + * ccs_socket_connect - Check permission for connect(). | |
902 | + * | |
903 | + * @sock: Pointer to "struct socket". | |
904 | + * @addr: Pointer to "struct sockaddr". | |
905 | + * @addr_len: Size of @addr. | |
906 | + * | |
907 | + * Returns 0 on success, negative value otherwise. | |
908 | + */ | |
909 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
910 | + int addr_len) | |
911 | +{ | |
912 | + const int rc = ccs_validate_socket(sock); | |
913 | + if (rc < 0) | |
914 | + return rc; | |
915 | + return ccs_socket_connect_permission(sock, addr, addr_len); | |
916 | +} | |
917 | + | |
918 | +/** | |
919 | + * ccs_socket_bind - Check permission for bind(). | |
920 | + * | |
921 | + * @sock: Pointer to "struct socket". | |
922 | + * @addr: Pointer to "struct sockaddr". | |
923 | + * @addr_len: Size of @addr. | |
924 | + * | |
925 | + * Returns 0 on success, negative value otherwise. | |
926 | + */ | |
927 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
928 | + int addr_len) | |
929 | +{ | |
930 | + const int rc = ccs_validate_socket(sock); | |
931 | + if (rc < 0) | |
932 | + return rc; | |
933 | + return ccs_socket_bind_permission(sock, addr, addr_len); | |
934 | +} | |
935 | + | |
936 | +/** | |
937 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
938 | + * | |
939 | + * @sock: Pointer to "struct socket". | |
940 | + * @msg: Pointer to "struct msghdr". | |
941 | + * @size: Size of message. | |
942 | + * | |
943 | + * Returns 0 on success, negative value otherwise. | |
944 | + */ | |
945 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
946 | + int size) | |
947 | +{ | |
948 | + const int rc = ccs_validate_socket(sock); | |
949 | + if (rc < 0) | |
950 | + return rc; | |
951 | + return ccs_socket_sendmsg_permission(sock, msg, size); | |
952 | +} | |
953 | + | |
954 | +/** | |
955 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
956 | + * | |
957 | + * @sock: Pointer to "struct socket". | |
958 | + * @msg: Pointer to "struct msghdr". | |
959 | + * @size: Size of message. | |
960 | + * @flags: Flags. | |
961 | + * | |
962 | + * Returns 0 on success, negative value otherwise. | |
963 | + */ | |
964 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
965 | + int size, int flags) | |
966 | +{ | |
967 | + return ccs_validate_socket(sock); | |
968 | +} | |
969 | + | |
970 | +/** | |
971 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
972 | + * | |
973 | + * @sock: Pointer to "struct socket". | |
974 | + * | |
975 | + * Returns 0 on success, negative value otherwise. | |
976 | + */ | |
977 | +static int ccs_socket_getsockname(struct socket *sock) | |
978 | +{ | |
979 | + return ccs_validate_socket(sock); | |
980 | +} | |
981 | + | |
982 | +/** | |
983 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
984 | + * | |
985 | + * @sock: Pointer to "struct socket". | |
986 | + * | |
987 | + * Returns 0 on success, negative value otherwise. | |
988 | + */ | |
989 | +static int ccs_socket_getpeername(struct socket *sock) | |
990 | +{ | |
991 | + return ccs_validate_socket(sock); | |
992 | +} | |
993 | + | |
994 | +/** | |
995 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
996 | + * | |
997 | + * @sock: Pointer to "struct socket". | |
998 | + * @level: Level. | |
999 | + * @optname: Option's name, | |
1000 | + * | |
1001 | + * Returns 0 on success, negative value otherwise. | |
1002 | + */ | |
1003 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1004 | +{ | |
1005 | + return ccs_validate_socket(sock); | |
1006 | +} | |
1007 | + | |
1008 | +/** | |
1009 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1010 | + * | |
1011 | + * @sock: Pointer to "struct socket". | |
1012 | + * @level: Level. | |
1013 | + * @optname: Option's name, | |
1014 | + * | |
1015 | + * Returns 0 on success, negative value otherwise. | |
1016 | + */ | |
1017 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1018 | +{ | |
1019 | + return ccs_validate_socket(sock); | |
1020 | +} | |
1021 | + | |
1022 | +/** | |
1023 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1024 | + * | |
1025 | + * @sock: Pointer to "struct socket". | |
1026 | + * @how: Shutdown mode. | |
1027 | + * | |
1028 | + * Returns 0 on success, negative value otherwise. | |
1029 | + */ | |
1030 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1031 | +{ | |
1032 | + return ccs_validate_socket(sock); | |
1033 | +} | |
1034 | + | |
1035 | +#define SOCKFS_MAGIC 0x534F434B | |
1036 | + | |
1037 | +/** | |
1038 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1039 | + * | |
1040 | + * @inode: Pointer to "struct inode". | |
1041 | + * | |
1042 | + * Returns nothing. | |
1043 | + * | |
1044 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1045 | + */ | |
1046 | +static void ccs_inode_free_security(struct inode *inode) | |
1047 | +{ | |
1048 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1049 | + ccs_update_socket_tag(inode, 0); | |
1050 | +} | |
1051 | + | |
1052 | +#endif | |
1053 | + | |
1054 | +/** | |
1055 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1056 | + * | |
1057 | + * @old_path: Pointer to "struct path". | |
1058 | + * @new_path: Pointer to "struct path". | |
1059 | + * | |
1060 | + * Returns 0 on success, negative value otherwise. | |
1061 | + */ | |
1062 | +static int ccs_sb_pivotroot(struct path *old_path, struct path *new_path) | |
1063 | +{ | |
1064 | + return ccs_pivot_root_permission(old_path, new_path); | |
1065 | +} | |
1066 | + | |
1067 | +/** | |
1068 | + * ccs_sb_mount - Check permission for mount(). | |
1069 | + * | |
1070 | + * @dev_name: Name of device file. | |
1071 | + * @path: Pointer to "struct path". | |
1072 | + * @type: Name of filesystem type. Maybe NULL. | |
1073 | + * @flags: Mount options. | |
1074 | + * @data_page: Optional data. Maybe NULL. | |
1075 | + * | |
1076 | + * Returns 0 on success, negative value otherwise. | |
1077 | + */ | |
1078 | +static int ccs_sb_mount(const char *dev_name, struct path *path, | |
1079 | + const char *type, unsigned long flags, void *data_page) | |
1080 | +{ | |
1081 | + return ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1082 | +} | |
1083 | + | |
1084 | +/** | |
1085 | + * ccs_sb_umount - Check permission for umount(). | |
1086 | + * | |
1087 | + * @mnt: Pointer to "struct vfsmount". | |
1088 | + * @flags: Unmount flags. | |
1089 | + * | |
1090 | + * Returns 0 on success, negative value otherwise. | |
1091 | + */ | |
1092 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1093 | +{ | |
1094 | + return ccs_umount_permission(mnt, flags); | |
1095 | +} | |
1096 | + | |
1097 | +/** | |
1098 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1099 | + * | |
1100 | + * @file: Pointer to "struct file". | |
1101 | + * @cmd: Command number. | |
1102 | + * @arg: Value for @cmd. | |
1103 | + * | |
1104 | + * Returns 0 on success, negative value otherwise. | |
1105 | + */ | |
1106 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1107 | + unsigned long arg) | |
1108 | +{ | |
1109 | + return ccs_fcntl_permission(file, cmd, arg); | |
1110 | +} | |
1111 | + | |
1112 | +/** | |
1113 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1114 | + * | |
1115 | + * @filp: Pointer to "struct file". | |
1116 | + * @cmd: Command number. | |
1117 | + * @arg: Value for @cmd. | |
1118 | + * | |
1119 | + * Returns 0 on success, negative value otherwise. | |
1120 | + */ | |
1121 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1122 | + unsigned long arg) | |
1123 | +{ | |
1124 | + return ccs_ioctl_permission(filp, cmd, arg); | |
1125 | +} | |
1126 | + | |
1127 | +#define MY_HOOK_INIT(HEAD, HOOK) \ | |
1128 | + { .head = &probe_dummy_security_hook_heads.HEAD, \ | |
1129 | + .hook = { .HEAD = HOOK } } | |
1130 | + | |
1131 | +static struct security_hook_list akari_hooks[] = { | |
1132 | + /* Security context allocator. */ | |
1133 | + MY_HOOK_INIT(cred_free, ccs_cred_free), | |
1134 | + MY_HOOK_INIT(cred_prepare, ccs_cred_prepare), | |
1135 | + MY_HOOK_INIT(cred_alloc_blank, ccs_cred_alloc_blank), | |
1136 | + MY_HOOK_INIT(cred_transfer, ccs_cred_transfer), | |
1137 | + MY_HOOK_INIT(task_create, ccs_task_create), | |
1138 | + /* Security context updater for successful execve(). */ | |
1139 | + MY_HOOK_INIT(bprm_check_security, ccs_bprm_check_security), | |
1140 | + MY_HOOK_INIT(bprm_committing_creds, ccs_bprm_committing_creds), | |
1141 | + /* Various permission checker. */ | |
1142 | + MY_HOOK_INIT(file_open, ccs_file_open), | |
1143 | + MY_HOOK_INIT(file_fcntl, ccs_file_fcntl), | |
1144 | + MY_HOOK_INIT(file_ioctl, ccs_file_ioctl), | |
1145 | + MY_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), | |
1146 | + MY_HOOK_INIT(sb_mount, ccs_sb_mount), | |
1147 | + MY_HOOK_INIT(sb_umount, ccs_sb_umount), | |
1148 | +#ifdef CONFIG_SECURITY_PATH | |
1149 | + MY_HOOK_INIT(path_mknod, ccs_path_mknod), | |
1150 | + MY_HOOK_INIT(path_mkdir, ccs_path_mkdir), | |
1151 | + MY_HOOK_INIT(path_rmdir, ccs_path_rmdir), | |
1152 | + MY_HOOK_INIT(path_unlink, ccs_path_unlink), | |
1153 | + MY_HOOK_INIT(path_symlink, ccs_path_symlink), | |
1154 | + MY_HOOK_INIT(path_rename, ccs_path_rename), | |
1155 | + MY_HOOK_INIT(path_link, ccs_path_link), | |
1156 | + MY_HOOK_INIT(path_truncate, ccs_path_truncate), | |
1157 | + MY_HOOK_INIT(path_chmod, ccs_path_chmod), | |
1158 | + MY_HOOK_INIT(path_chown, ccs_path_chown), | |
1159 | + MY_HOOK_INIT(path_chroot, ccs_path_chroot), | |
1160 | +#else | |
1161 | + MY_HOOK_INIT(inode_mknod, ccs_inode_mknod), | |
1162 | + MY_HOOK_INIT(inode_mkdir, ccs_inode_mkdir), | |
1163 | + MY_HOOK_INIT(inode_rmdir, ccs_inode_rmdir), | |
1164 | + MY_HOOK_INIT(inode_unlink, ccs_inode_unlink), | |
1165 | + MY_HOOK_INIT(inode_symlink, ccs_inode_symlink), | |
1166 | + MY_HOOK_INIT(inode_rename, ccs_inode_rename), | |
1167 | + MY_HOOK_INIT(inode_link, ccs_inode_link), | |
1168 | + MY_HOOK_INIT(inode_create, ccs_inode_create), | |
1169 | + MY_HOOK_INIT(inode_setattr, ccs_inode_setattr), | |
1170 | +#endif | |
1171 | + MY_HOOK_INIT(inode_getattr, ccs_inode_getattr), | |
1172 | +#ifdef CONFIG_SECURITY_NETWORK | |
1173 | + MY_HOOK_INIT(socket_bind, ccs_socket_bind), | |
1174 | + MY_HOOK_INIT(socket_connect, ccs_socket_connect), | |
1175 | + MY_HOOK_INIT(socket_listen, ccs_socket_listen), | |
1176 | + MY_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), | |
1177 | + MY_HOOK_INIT(socket_recvmsg, ccs_socket_recvmsg), | |
1178 | + MY_HOOK_INIT(socket_getsockname, ccs_socket_getsockname), | |
1179 | + MY_HOOK_INIT(socket_getpeername, ccs_socket_getpeername), | |
1180 | + MY_HOOK_INIT(socket_getsockopt, ccs_socket_getsockopt), | |
1181 | + MY_HOOK_INIT(socket_setsockopt, ccs_socket_setsockopt), | |
1182 | + MY_HOOK_INIT(socket_shutdown, ccs_socket_shutdown), | |
1183 | + MY_HOOK_INIT(socket_accept, ccs_socket_accept), | |
1184 | + MY_HOOK_INIT(inode_free_security, ccs_inode_free_security), | |
1185 | +#endif | |
1186 | +}; | |
1187 | + | |
1188 | +static inline void add_hook(struct security_hook_list *hook) | |
1189 | +{ | |
1190 | + list_add_tail_rcu(&hook->list, hook->head); | |
1191 | +} | |
1192 | + | |
1193 | +static void __init swap_hook(struct security_hook_list *hook, | |
1194 | + union security_list_options *original) | |
1195 | +{ | |
1196 | + struct list_head *list = hook->head; | |
1197 | + if (list_empty(list)) { | |
1198 | + add_hook(hook); | |
1199 | + } else { | |
1200 | + struct security_hook_list *shp = | |
1201 | + list_last_entry(list, struct security_hook_list, list); | |
1202 | + *original = shp->hook; | |
1203 | + smp_wmb(); | |
1204 | + shp->hook = hook->hook; | |
1205 | + } | |
1206 | +} | |
1207 | + | |
1208 | +/** | |
1209 | + * ccs_init - Initialize this module. | |
1210 | + * | |
1211 | + * Returns 0 on success, negative value otherwise. | |
1212 | + */ | |
1213 | +static int __init ccs_init(void) | |
1214 | +{ | |
1215 | + int idx; | |
1216 | + struct security_hook_heads *hooks = probe_security_hook_heads(); | |
1217 | + if (!hooks) | |
1218 | + goto out; | |
1219 | + for (idx = 0; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1220 | + akari_hooks[idx].head = ((void *) hooks) | |
1221 | + + ((unsigned long) akari_hooks[idx].head) | |
1222 | + - ((unsigned long) &probe_dummy_security_hook_heads); | |
1223 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1224 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1225 | + goto out; | |
1226 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1227 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1228 | + goto out; | |
1229 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1230 | + if (!ccsecurity_exports.d_absolute_path) | |
1231 | + goto out; | |
1232 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1233 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1234 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1235 | + } | |
1236 | + ccs_main_init(); | |
1237 | + swap_hook(&akari_hooks[0], &original_cred_free); | |
1238 | + swap_hook(&akari_hooks[1], &original_cred_prepare); | |
1239 | + swap_hook(&akari_hooks[2], &original_cred_alloc_blank); | |
1240 | + for (idx = 3; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1241 | + add_hook(&akari_hooks[idx]); | |
1242 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1243 | + printk(KERN_INFO | |
1244 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1245 | + return 0; | |
1246 | +out: | |
1247 | + return -EINVAL; | |
1248 | +} | |
1249 | + | |
1250 | +module_init(ccs_init); | |
1251 | +MODULE_LICENSE("GPL"); | |
1252 | + | |
1253 | +/** | |
1254 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1255 | + * | |
1256 | + * @domain: Pointer to "struct ccs_domain_info". | |
1257 | + * | |
1258 | + * Returns true if @domain is in use, false otherwise. | |
1259 | + * | |
1260 | + * Caller holds rcu_read_lock(). | |
1261 | + */ | |
1262 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1263 | +{ | |
1264 | + int idx; | |
1265 | + struct ccs_security *ptr; | |
1266 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1267 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1268 | + list_for_each_entry_rcu(ptr, list, list) { | |
1269 | + struct ccs_execve *ee = ptr->ee; | |
1270 | + if (ptr->ccs_domain_info == domain || | |
1271 | + (ee && ee->previous_domain == domain)) { | |
1272 | + return true; | |
1273 | + } | |
1274 | + } | |
1275 | + } | |
1276 | + return false; | |
1277 | +} | |
1278 | + | |
1279 | +/** | |
1280 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1281 | + * | |
1282 | + * @ptr: Pointer to "struct ccs_security". | |
1283 | + * @list: Pointer to "struct list_head". | |
1284 | + * | |
1285 | + * Returns nothing. | |
1286 | + */ | |
1287 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1288 | + struct list_head *list) | |
1289 | +{ | |
1290 | + unsigned long flags; | |
1291 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1292 | + list_add_rcu(&ptr->list, list); | |
1293 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1294 | +} | |
1295 | + | |
1296 | +/** | |
1297 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1298 | + * | |
1299 | + * @task: Pointer to "struct task_struct". | |
1300 | + * | |
1301 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1302 | + * out of memory, &ccs_default_security otherwise. | |
1303 | + * | |
1304 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1305 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1306 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1307 | + * won't work. | |
1308 | + */ | |
1309 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1310 | +{ | |
1311 | + struct ccs_security *ptr; | |
1312 | + struct list_head *list = &ccs_task_security_list | |
1313 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1314 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1315 | + while (!list->next); | |
1316 | + rcu_read_lock(); | |
1317 | + list_for_each_entry_rcu(ptr, list, list) { | |
1318 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
1319 | + continue; | |
1320 | + rcu_read_unlock(); | |
1321 | + /* | |
1322 | + * Current thread needs to transit from old domain to new | |
1323 | + * domain before do_execve() succeeds in order to check | |
1324 | + * permission for interpreters and environment variables using | |
1325 | + * new domain's ACL rules. The domain transition has to be | |
1326 | + * visible from other CPU in order to allow interactive | |
1327 | + * enforcing mode. Also, the domain transition has to be | |
1328 | + * reverted if do_execve() failed. However, an LSM hook for | |
1329 | + * reverting domain transition is missing. | |
1330 | + * | |
1331 | + * security_prepare_creds() is called from prepare_creds() from | |
1332 | + * prepare_bprm_creds() from do_execve() before setting | |
1333 | + * current->in_execve flag, and current->in_execve flag is | |
1334 | + * cleared by the time next do_execve() request starts. | |
1335 | + * This means that we can emulate the missing LSM hook for | |
1336 | + * reverting domain transition, by calling this function from | |
1337 | + * security_prepare_creds(). | |
1338 | + * | |
1339 | + * If current->in_execve is not set but ptr->ccs_flags has | |
1340 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
1341 | + * has failed and reverting domain transition is needed. | |
1342 | + */ | |
1343 | + if (task == current && | |
1344 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
1345 | + !current->in_execve) { | |
1346 | + ccs_debug_trace("4"); | |
1347 | + ccs_clear_execve(-1, ptr); | |
1348 | + } | |
1349 | + return ptr; | |
1350 | + } | |
1351 | + rcu_read_unlock(); | |
1352 | + if (task != current) { | |
1353 | + /* | |
1354 | + * If a thread does nothing after fork(), caller will reach | |
1355 | + * here because "struct ccs_security" for that thread is not | |
1356 | + * yet allocated. But that thread is keeping a snapshot of | |
1357 | + * "struct ccs_security" taken as of ccs_task_create() | |
1358 | + * associated with that thread's "struct cred". | |
1359 | + * | |
1360 | + * Since that snapshot will be used as initial data when that | |
1361 | + * thread allocates "struct ccs_security" for that thread, we | |
1362 | + * can return that snapshot rather than &ccs_default_security. | |
1363 | + * | |
1364 | + * Since this function is called by only ccs_select_one() and | |
1365 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
1366 | + * it is guaranteed that caller has called rcu_read_lock() | |
1367 | + * (via ccs_tasklist_lock()) before finding this thread and | |
1368 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
1369 | + * like get_robust_list() does. | |
1370 | + */ | |
1371 | + return ccs_find_cred_security(__task_cred(task)); | |
1372 | + } | |
1373 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1374 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1375 | + if (!ptr) { | |
1376 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1377 | + task->pid); | |
1378 | + send_sig(SIGKILL, current, 0); | |
1379 | + return &ccs_oom_security; | |
1380 | + } | |
1381 | + *ptr = *ccs_find_cred_security(task->cred); | |
1382 | + /* We can shortcut because task == current. */ | |
1383 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
1384 | + pids[PIDTYPE_PID].pid); | |
1385 | + ptr->cred = NULL; | |
1386 | + ccs_add_task_security(ptr, list); | |
1387 | + return ptr; | |
1388 | +} | |
1389 | + | |
1390 | +/** | |
1391 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
1392 | + * | |
1393 | + * @new: Pointer to "struct cred". | |
1394 | + * @old: Pointer to "struct cred". | |
1395 | + * @gfp: Memory allocation flags. | |
1396 | + * | |
1397 | + * Returns 0 on success, negative value otherwise. | |
1398 | + */ | |
1399 | +static int ccs_copy_cred_security(const struct cred *new, | |
1400 | + const struct cred *old, gfp_t gfp) | |
1401 | +{ | |
1402 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
1403 | + struct ccs_security *new_security = | |
1404 | + kzalloc(sizeof(*new_security), gfp); | |
1405 | + if (!new_security) | |
1406 | + return -ENOMEM; | |
1407 | + *new_security = *old_security; | |
1408 | + new_security->cred = new; | |
1409 | + ccs_add_cred_security(new_security); | |
1410 | + return 0; | |
1411 | +} | |
1412 | + | |
1413 | +/** | |
1414 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
1415 | + * | |
1416 | + * @cred: Pointer to "struct cred". | |
1417 | + * | |
1418 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
1419 | + * otherwise. | |
1420 | + */ | |
1421 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
1422 | +{ | |
1423 | + struct ccs_security *ptr; | |
1424 | + struct list_head *list = &ccs_cred_security_list | |
1425 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
1426 | + rcu_read_lock(); | |
1427 | + list_for_each_entry_rcu(ptr, list, list) { | |
1428 | + if (ptr->cred != cred) | |
1429 | + continue; | |
1430 | + rcu_read_unlock(); | |
1431 | + return ptr; | |
1432 | + } | |
1433 | + rcu_read_unlock(); | |
1434 | + return &ccs_default_security; | |
1435 | +} | |
1436 | + | |
1437 | +/** | |
1438 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
1439 | + * | |
1440 | + * Returns nothing. | |
1441 | + * | |
1442 | + * Since security_task_free() is missing, I can't release memory associated | |
1443 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
1444 | + * "struct pid" and runs garbage collection when associated | |
1445 | + * "struct task_struct" has gone. | |
1446 | + */ | |
1447 | +static void ccs_task_security_gc(void) | |
1448 | +{ | |
1449 | + static DEFINE_SPINLOCK(lock); | |
1450 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
1451 | + unsigned int idx; | |
1452 | + /* | |
1453 | + * If some process is doing execve(), try to garbage collection now. | |
1454 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
1455 | + * as soon as execve() has completed in order to compensate for lack of | |
1456 | + * security_bprm_free() and security_task_free() hooks. | |
1457 | + * | |
1458 | + * Otherwise, reduce frequency for performance reason. | |
1459 | + */ | |
1460 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
1461 | + atomic_inc_return(&gc_counter) < 1024) | |
1462 | + return; | |
1463 | + if (!spin_trylock(&lock)) | |
1464 | + return; | |
1465 | + atomic_set(&gc_counter, 0); | |
1466 | + rcu_read_lock(); | |
1467 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1468 | + struct ccs_security *ptr; | |
1469 | + struct list_head *list = &ccs_task_security_list[idx]; | |
1470 | + list_for_each_entry_rcu(ptr, list, list) { | |
1471 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
1472 | + continue; | |
1473 | + ccs_del_security(ptr); | |
1474 | + } | |
1475 | + } | |
1476 | + rcu_read_unlock(); | |
1477 | + spin_unlock(&lock); | |
1478 | +} |
@@ -0,0 +1,1481 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
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_update_socket_tag - Update tag associated with accept()ed sockets. | |
765 | + * | |
766 | + * @inode: Pointer to "struct inode". | |
767 | + * @status: New status. | |
768 | + * | |
769 | + * Returns nothing. | |
770 | + * | |
771 | + * If @status == 0, memory for that socket will be released after RCU grace | |
772 | + * period. | |
773 | + */ | |
774 | +static void ccs_update_socket_tag(struct inode *inode, int status) | |
775 | +{ | |
776 | + struct ccs_socket_tag *ptr; | |
777 | + /* | |
778 | + * Protect whole section because multiple threads may call this | |
779 | + * function with same "sock" via ccs_validate_socket(). | |
780 | + */ | |
781 | + spin_lock(&ccs_accepted_socket_list_lock); | |
782 | + rcu_read_lock(); | |
783 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
784 | + if (ptr->inode != inode) | |
785 | + continue; | |
786 | + ptr->status = status; | |
787 | + if (status) | |
788 | + break; | |
789 | + list_del_rcu(&ptr->list); | |
790 | + kfree_rcu(ptr, rcu); | |
791 | + break; | |
792 | + } | |
793 | + rcu_read_unlock(); | |
794 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
795 | +} | |
796 | + | |
797 | +/** | |
798 | + * ccs_validate_socket - Check post accept() permission if needed. | |
799 | + * | |
800 | + * @sock: Pointer to "struct socket". | |
801 | + * | |
802 | + * Returns 0 on success, negative value otherwise. | |
803 | + */ | |
804 | +static int ccs_validate_socket(struct socket *sock) | |
805 | +{ | |
806 | + struct inode *inode = SOCK_INODE(sock); | |
807 | + struct ccs_socket_tag *ptr; | |
808 | + int ret = 0; | |
809 | + rcu_read_lock(); | |
810 | + list_for_each_entry_rcu(ptr, &ccs_accepted_socket_list, list) { | |
811 | + if (ptr->inode != inode) | |
812 | + continue; | |
813 | + ret = ptr->status; | |
814 | + break; | |
815 | + } | |
816 | + rcu_read_unlock(); | |
817 | + if (ret <= 0) | |
818 | + /* | |
819 | + * This socket is not an accept()ed socket or this socket is | |
820 | + * an accept()ed socket and post accept() permission is done. | |
821 | + */ | |
822 | + return ret; | |
823 | + /* | |
824 | + * Check post accept() permission now. | |
825 | + * | |
826 | + * Strictly speaking, we need to pass both listen()ing socket and | |
827 | + * accept()ed socket to __ccs_socket_post_accept_permission(). | |
828 | + * But since socket's family and type are same for both sockets, | |
829 | + * passing the accept()ed socket in place for the listen()ing socket | |
830 | + * will work. | |
831 | + */ | |
832 | + ret = ccs_socket_post_accept_permission(sock, sock); | |
833 | + /* | |
834 | + * If permission was granted, we forget that this is an accept()ed | |
835 | + * socket. Otherwise, we remember that this socket needs to return | |
836 | + * error for subsequent socketcalls. | |
837 | + */ | |
838 | + ccs_update_socket_tag(inode, ret); | |
839 | + return ret; | |
840 | +} | |
841 | + | |
842 | +/** | |
843 | + * ccs_socket_accept - Check permission for accept(). | |
844 | + * | |
845 | + * @sock: Pointer to "struct socket". | |
846 | + * @newsock: Pointer to "struct socket". | |
847 | + * | |
848 | + * Returns 0 on success, negative value otherwise. | |
849 | + * | |
850 | + * This hook is used for setting up environment for doing post accept() | |
851 | + * permission check. If dereferencing sock->ops->something() were ordered by | |
852 | + * rcu_dereference(), we could replace sock->ops with "a copy of original | |
853 | + * sock->ops with modified sock->ops->accept()" using rcu_assign_pointer() | |
854 | + * in order to do post accept() permission check before returning to userspace. | |
855 | + * If we make the copy in security_socket_post_create(), it would be possible | |
856 | + * to safely replace sock->ops here, but we don't do so because we don't want | |
857 | + * to allocate memory for sockets which do not call sock->ops->accept(). | |
858 | + * Therefore, we do post accept() permission check upon next socket syscalls | |
859 | + * rather than between sock->ops->accept() and returning to userspace. | |
860 | + * This means that if a socket was close()d before calling some socket | |
861 | + * syscalls, post accept() permission check will not be done. | |
862 | + */ | |
863 | +static int ccs_socket_accept(struct socket *sock, struct socket *newsock) | |
864 | +{ | |
865 | + struct ccs_socket_tag *ptr; | |
866 | + const int rc = ccs_validate_socket(sock); | |
867 | + if (rc < 0) | |
868 | + return rc; | |
869 | + ptr = kzalloc(sizeof(*ptr), GFP_KERNEL); | |
870 | + if (!ptr) | |
871 | + return -ENOMEM; | |
872 | + /* | |
873 | + * Subsequent LSM hooks will receive "newsock". Therefore, I mark | |
874 | + * "newsock" as "an accept()ed socket but post accept() permission | |
875 | + * check is not done yet" by allocating memory using inode of the | |
876 | + * "newsock" as a search key. | |
877 | + */ | |
878 | + ptr->inode = SOCK_INODE(newsock); | |
879 | + ptr->status = 1; /* Check post accept() permission later. */ | |
880 | + spin_lock(&ccs_accepted_socket_list_lock); | |
881 | + list_add_tail_rcu(&ptr->list, &ccs_accepted_socket_list); | |
882 | + spin_unlock(&ccs_accepted_socket_list_lock); | |
883 | + return 0; | |
884 | +} | |
885 | + | |
886 | +/** | |
887 | + * ccs_socket_listen - Check permission for listen(). | |
888 | + * | |
889 | + * @sock: Pointer to "struct socket". | |
890 | + * @backlog: Backlog parameter. | |
891 | + * | |
892 | + * Returns 0 on success, negative value otherwise. | |
893 | + */ | |
894 | +static int ccs_socket_listen(struct socket *sock, int backlog) | |
895 | +{ | |
896 | + const int rc = ccs_validate_socket(sock); | |
897 | + if (rc < 0) | |
898 | + return rc; | |
899 | + return ccs_socket_listen_permission(sock); | |
900 | +} | |
901 | + | |
902 | +/** | |
903 | + * ccs_socket_connect - Check permission for connect(). | |
904 | + * | |
905 | + * @sock: Pointer to "struct socket". | |
906 | + * @addr: Pointer to "struct sockaddr". | |
907 | + * @addr_len: Size of @addr. | |
908 | + * | |
909 | + * Returns 0 on success, negative value otherwise. | |
910 | + */ | |
911 | +static int ccs_socket_connect(struct socket *sock, struct sockaddr *addr, | |
912 | + int addr_len) | |
913 | +{ | |
914 | + const int rc = ccs_validate_socket(sock); | |
915 | + if (rc < 0) | |
916 | + return rc; | |
917 | + return ccs_socket_connect_permission(sock, addr, addr_len); | |
918 | +} | |
919 | + | |
920 | +/** | |
921 | + * ccs_socket_bind - Check permission for bind(). | |
922 | + * | |
923 | + * @sock: Pointer to "struct socket". | |
924 | + * @addr: Pointer to "struct sockaddr". | |
925 | + * @addr_len: Size of @addr. | |
926 | + * | |
927 | + * Returns 0 on success, negative value otherwise. | |
928 | + */ | |
929 | +static int ccs_socket_bind(struct socket *sock, struct sockaddr *addr, | |
930 | + int addr_len) | |
931 | +{ | |
932 | + const int rc = ccs_validate_socket(sock); | |
933 | + if (rc < 0) | |
934 | + return rc; | |
935 | + return ccs_socket_bind_permission(sock, addr, addr_len); | |
936 | +} | |
937 | + | |
938 | +/** | |
939 | + * ccs_socket_sendmsg - Check permission for sendmsg(). | |
940 | + * | |
941 | + * @sock: Pointer to "struct socket". | |
942 | + * @msg: Pointer to "struct msghdr". | |
943 | + * @size: Size of message. | |
944 | + * | |
945 | + * Returns 0 on success, negative value otherwise. | |
946 | + */ | |
947 | +static int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
948 | + int size) | |
949 | +{ | |
950 | + const int rc = ccs_validate_socket(sock); | |
951 | + if (rc < 0) | |
952 | + return rc; | |
953 | + return ccs_socket_sendmsg_permission(sock, msg, size); | |
954 | +} | |
955 | + | |
956 | +/** | |
957 | + * ccs_socket_recvmsg - Check permission for recvmsg(). | |
958 | + * | |
959 | + * @sock: Pointer to "struct socket". | |
960 | + * @msg: Pointer to "struct msghdr". | |
961 | + * @size: Size of message. | |
962 | + * @flags: Flags. | |
963 | + * | |
964 | + * Returns 0 on success, negative value otherwise. | |
965 | + */ | |
966 | +static int ccs_socket_recvmsg(struct socket *sock, struct msghdr *msg, | |
967 | + int size, int flags) | |
968 | +{ | |
969 | + return ccs_validate_socket(sock); | |
970 | +} | |
971 | + | |
972 | +/** | |
973 | + * ccs_socket_getsockname - Check permission for getsockname(). | |
974 | + * | |
975 | + * @sock: Pointer to "struct socket". | |
976 | + * | |
977 | + * Returns 0 on success, negative value otherwise. | |
978 | + */ | |
979 | +static int ccs_socket_getsockname(struct socket *sock) | |
980 | +{ | |
981 | + return ccs_validate_socket(sock); | |
982 | +} | |
983 | + | |
984 | +/** | |
985 | + * ccs_socket_getpeername - Check permission for getpeername(). | |
986 | + * | |
987 | + * @sock: Pointer to "struct socket". | |
988 | + * | |
989 | + * Returns 0 on success, negative value otherwise. | |
990 | + */ | |
991 | +static int ccs_socket_getpeername(struct socket *sock) | |
992 | +{ | |
993 | + return ccs_validate_socket(sock); | |
994 | +} | |
995 | + | |
996 | +/** | |
997 | + * ccs_socket_getsockopt - Check permission for getsockopt(). | |
998 | + * | |
999 | + * @sock: Pointer to "struct socket". | |
1000 | + * @level: Level. | |
1001 | + * @optname: Option's name, | |
1002 | + * | |
1003 | + * Returns 0 on success, negative value otherwise. | |
1004 | + */ | |
1005 | +static int ccs_socket_getsockopt(struct socket *sock, int level, int optname) | |
1006 | +{ | |
1007 | + return ccs_validate_socket(sock); | |
1008 | +} | |
1009 | + | |
1010 | +/** | |
1011 | + * ccs_socket_setsockopt - Check permission for setsockopt(). | |
1012 | + * | |
1013 | + * @sock: Pointer to "struct socket". | |
1014 | + * @level: Level. | |
1015 | + * @optname: Option's name, | |
1016 | + * | |
1017 | + * Returns 0 on success, negative value otherwise. | |
1018 | + */ | |
1019 | +static int ccs_socket_setsockopt(struct socket *sock, int level, int optname) | |
1020 | +{ | |
1021 | + return ccs_validate_socket(sock); | |
1022 | +} | |
1023 | + | |
1024 | +/** | |
1025 | + * ccs_socket_shutdown - Check permission for shutdown(). | |
1026 | + * | |
1027 | + * @sock: Pointer to "struct socket". | |
1028 | + * @how: Shutdown mode. | |
1029 | + * | |
1030 | + * Returns 0 on success, negative value otherwise. | |
1031 | + */ | |
1032 | +static int ccs_socket_shutdown(struct socket *sock, int how) | |
1033 | +{ | |
1034 | + return ccs_validate_socket(sock); | |
1035 | +} | |
1036 | + | |
1037 | +#define SOCKFS_MAGIC 0x534F434B | |
1038 | + | |
1039 | +/** | |
1040 | + * ccs_inode_free_security - Release memory associated with an inode. | |
1041 | + * | |
1042 | + * @inode: Pointer to "struct inode". | |
1043 | + * | |
1044 | + * Returns nothing. | |
1045 | + * | |
1046 | + * We use this hook for releasing memory associated with an accept()ed socket. | |
1047 | + */ | |
1048 | +static void ccs_inode_free_security(struct inode *inode) | |
1049 | +{ | |
1050 | + if (inode->i_sb && inode->i_sb->s_magic == SOCKFS_MAGIC) | |
1051 | + ccs_update_socket_tag(inode, 0); | |
1052 | +} | |
1053 | + | |
1054 | +#endif | |
1055 | + | |
1056 | +/** | |
1057 | + * ccs_sb_pivotroot - Check permission for pivot_root(). | |
1058 | + * | |
1059 | + * @old_path: Pointer to "struct path". | |
1060 | + * @new_path: Pointer to "struct path". | |
1061 | + * | |
1062 | + * Returns 0 on success, negative value otherwise. | |
1063 | + */ | |
1064 | +static int ccs_sb_pivotroot(const struct path *old_path, | |
1065 | + const struct path *new_path) | |
1066 | +{ | |
1067 | + return ccs_pivot_root_permission(old_path, new_path); | |
1068 | +} | |
1069 | + | |
1070 | +/** | |
1071 | + * ccs_sb_mount - Check permission for mount(). | |
1072 | + * | |
1073 | + * @dev_name: Name of device file. | |
1074 | + * @path: Pointer to "struct path". | |
1075 | + * @type: Name of filesystem type. Maybe NULL. | |
1076 | + * @flags: Mount options. | |
1077 | + * @data_page: Optional data. Maybe NULL. | |
1078 | + * | |
1079 | + * Returns 0 on success, negative value otherwise. | |
1080 | + */ | |
1081 | +static int ccs_sb_mount(const char *dev_name, const struct path *path, | |
1082 | + const char *type, unsigned long flags, void *data_page) | |
1083 | +{ | |
1084 | + return ccs_mount_permission(dev_name, path, type, flags, data_page); | |
1085 | +} | |
1086 | + | |
1087 | +/** | |
1088 | + * ccs_sb_umount - Check permission for umount(). | |
1089 | + * | |
1090 | + * @mnt: Pointer to "struct vfsmount". | |
1091 | + * @flags: Unmount flags. | |
1092 | + * | |
1093 | + * Returns 0 on success, negative value otherwise. | |
1094 | + */ | |
1095 | +static int ccs_sb_umount(struct vfsmount *mnt, int flags) | |
1096 | +{ | |
1097 | + return ccs_umount_permission(mnt, flags); | |
1098 | +} | |
1099 | + | |
1100 | +/** | |
1101 | + * ccs_file_fcntl - Check permission for fcntl(). | |
1102 | + * | |
1103 | + * @file: Pointer to "struct file". | |
1104 | + * @cmd: Command number. | |
1105 | + * @arg: Value for @cmd. | |
1106 | + * | |
1107 | + * Returns 0 on success, negative value otherwise. | |
1108 | + */ | |
1109 | +static int ccs_file_fcntl(struct file *file, unsigned int cmd, | |
1110 | + unsigned long arg) | |
1111 | +{ | |
1112 | + return ccs_fcntl_permission(file, cmd, arg); | |
1113 | +} | |
1114 | + | |
1115 | +/** | |
1116 | + * ccs_file_ioctl - Check permission for ioctl(). | |
1117 | + * | |
1118 | + * @filp: Pointer to "struct file". | |
1119 | + * @cmd: Command number. | |
1120 | + * @arg: Value for @cmd. | |
1121 | + * | |
1122 | + * Returns 0 on success, negative value otherwise. | |
1123 | + */ | |
1124 | +static int ccs_file_ioctl(struct file *filp, unsigned int cmd, | |
1125 | + unsigned long arg) | |
1126 | +{ | |
1127 | + return ccs_ioctl_permission(filp, cmd, arg); | |
1128 | +} | |
1129 | + | |
1130 | +#define MY_HOOK_INIT(HEAD, HOOK) \ | |
1131 | + { .head = &probe_dummy_security_hook_heads.HEAD, \ | |
1132 | + .hook = { .HEAD = HOOK } } | |
1133 | + | |
1134 | +static struct security_hook_list akari_hooks[] = { | |
1135 | + /* Security context allocator. */ | |
1136 | + MY_HOOK_INIT(cred_free, ccs_cred_free), | |
1137 | + MY_HOOK_INIT(cred_prepare, ccs_cred_prepare), | |
1138 | + MY_HOOK_INIT(cred_alloc_blank, ccs_cred_alloc_blank), | |
1139 | + MY_HOOK_INIT(cred_transfer, ccs_cred_transfer), | |
1140 | + MY_HOOK_INIT(task_create, ccs_task_create), | |
1141 | + /* Security context updater for successful execve(). */ | |
1142 | + MY_HOOK_INIT(bprm_check_security, ccs_bprm_check_security), | |
1143 | + MY_HOOK_INIT(bprm_committing_creds, ccs_bprm_committing_creds), | |
1144 | + /* Various permission checker. */ | |
1145 | + MY_HOOK_INIT(file_open, ccs_file_open), | |
1146 | + MY_HOOK_INIT(file_fcntl, ccs_file_fcntl), | |
1147 | + MY_HOOK_INIT(file_ioctl, ccs_file_ioctl), | |
1148 | + MY_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot), | |
1149 | + MY_HOOK_INIT(sb_mount, ccs_sb_mount), | |
1150 | + MY_HOOK_INIT(sb_umount, ccs_sb_umount), | |
1151 | +#ifdef CONFIG_SECURITY_PATH | |
1152 | + MY_HOOK_INIT(path_mknod, ccs_path_mknod), | |
1153 | + MY_HOOK_INIT(path_mkdir, ccs_path_mkdir), | |
1154 | + MY_HOOK_INIT(path_rmdir, ccs_path_rmdir), | |
1155 | + MY_HOOK_INIT(path_unlink, ccs_path_unlink), | |
1156 | + MY_HOOK_INIT(path_symlink, ccs_path_symlink), | |
1157 | + MY_HOOK_INIT(path_rename, ccs_path_rename), | |
1158 | + MY_HOOK_INIT(path_link, ccs_path_link), | |
1159 | + MY_HOOK_INIT(path_truncate, ccs_path_truncate), | |
1160 | + MY_HOOK_INIT(path_chmod, ccs_path_chmod), | |
1161 | + MY_HOOK_INIT(path_chown, ccs_path_chown), | |
1162 | + MY_HOOK_INIT(path_chroot, ccs_path_chroot), | |
1163 | +#else | |
1164 | + MY_HOOK_INIT(inode_mknod, ccs_inode_mknod), | |
1165 | + MY_HOOK_INIT(inode_mkdir, ccs_inode_mkdir), | |
1166 | + MY_HOOK_INIT(inode_rmdir, ccs_inode_rmdir), | |
1167 | + MY_HOOK_INIT(inode_unlink, ccs_inode_unlink), | |
1168 | + MY_HOOK_INIT(inode_symlink, ccs_inode_symlink), | |
1169 | + MY_HOOK_INIT(inode_rename, ccs_inode_rename), | |
1170 | + MY_HOOK_INIT(inode_link, ccs_inode_link), | |
1171 | + MY_HOOK_INIT(inode_create, ccs_inode_create), | |
1172 | + MY_HOOK_INIT(inode_setattr, ccs_inode_setattr), | |
1173 | +#endif | |
1174 | + MY_HOOK_INIT(inode_getattr, ccs_inode_getattr), | |
1175 | +#ifdef CONFIG_SECURITY_NETWORK | |
1176 | + MY_HOOK_INIT(socket_bind, ccs_socket_bind), | |
1177 | + MY_HOOK_INIT(socket_connect, ccs_socket_connect), | |
1178 | + MY_HOOK_INIT(socket_listen, ccs_socket_listen), | |
1179 | + MY_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg), | |
1180 | + MY_HOOK_INIT(socket_recvmsg, ccs_socket_recvmsg), | |
1181 | + MY_HOOK_INIT(socket_getsockname, ccs_socket_getsockname), | |
1182 | + MY_HOOK_INIT(socket_getpeername, ccs_socket_getpeername), | |
1183 | + MY_HOOK_INIT(socket_getsockopt, ccs_socket_getsockopt), | |
1184 | + MY_HOOK_INIT(socket_setsockopt, ccs_socket_setsockopt), | |
1185 | + MY_HOOK_INIT(socket_shutdown, ccs_socket_shutdown), | |
1186 | + MY_HOOK_INIT(socket_accept, ccs_socket_accept), | |
1187 | + MY_HOOK_INIT(inode_free_security, ccs_inode_free_security), | |
1188 | +#endif | |
1189 | +}; | |
1190 | + | |
1191 | +static inline void add_hook(struct security_hook_list *hook) | |
1192 | +{ | |
1193 | + list_add_tail_rcu(&hook->list, hook->head); | |
1194 | +} | |
1195 | + | |
1196 | +static void __init swap_hook(struct security_hook_list *hook, | |
1197 | + union security_list_options *original) | |
1198 | +{ | |
1199 | + struct list_head *list = hook->head; | |
1200 | + if (list_empty(list)) { | |
1201 | + add_hook(hook); | |
1202 | + } else { | |
1203 | + struct security_hook_list *shp = | |
1204 | + list_last_entry(list, struct security_hook_list, list); | |
1205 | + *original = shp->hook; | |
1206 | + smp_wmb(); | |
1207 | + shp->hook = hook->hook; | |
1208 | + } | |
1209 | +} | |
1210 | + | |
1211 | +/** | |
1212 | + * ccs_init - Initialize this module. | |
1213 | + * | |
1214 | + * Returns 0 on success, negative value otherwise. | |
1215 | + */ | |
1216 | +static int __init ccs_init(void) | |
1217 | +{ | |
1218 | + int idx; | |
1219 | + struct security_hook_heads *hooks = probe_security_hook_heads(); | |
1220 | + if (!hooks) | |
1221 | + goto out; | |
1222 | + for (idx = 0; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1223 | + akari_hooks[idx].head = ((void *) hooks) | |
1224 | + + ((unsigned long) akari_hooks[idx].head) | |
1225 | + - ((unsigned long) &probe_dummy_security_hook_heads); | |
1226 | + ccsecurity_exports.find_task_by_vpid = probe_find_task_by_vpid(); | |
1227 | + if (!ccsecurity_exports.find_task_by_vpid) | |
1228 | + goto out; | |
1229 | + ccsecurity_exports.find_task_by_pid_ns = probe_find_task_by_pid_ns(); | |
1230 | + if (!ccsecurity_exports.find_task_by_pid_ns) | |
1231 | + goto out; | |
1232 | + ccsecurity_exports.d_absolute_path = probe_d_absolute_path(); | |
1233 | + if (!ccsecurity_exports.d_absolute_path) | |
1234 | + goto out; | |
1235 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1236 | + INIT_LIST_HEAD(&ccs_cred_security_list[idx]); | |
1237 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
1238 | + } | |
1239 | + ccs_main_init(); | |
1240 | + swap_hook(&akari_hooks[0], &original_cred_free); | |
1241 | + swap_hook(&akari_hooks[1], &original_cred_prepare); | |
1242 | + swap_hook(&akari_hooks[2], &original_cred_alloc_blank); | |
1243 | + for (idx = 3; idx < ARRAY_SIZE(akari_hooks); idx++) | |
1244 | + add_hook(&akari_hooks[idx]); | |
1245 | + printk(KERN_INFO "AKARI: 1.0.37 2017/09/17\n"); | |
1246 | + printk(KERN_INFO | |
1247 | + "Access Keeping And Regulating Instrument registered.\n"); | |
1248 | + return 0; | |
1249 | +out: | |
1250 | + return -EINVAL; | |
1251 | +} | |
1252 | + | |
1253 | +module_init(ccs_init); | |
1254 | +MODULE_LICENSE("GPL"); | |
1255 | + | |
1256 | +/** | |
1257 | + * ccs_used_by_cred - Check whether the given domain is in use or not. | |
1258 | + * | |
1259 | + * @domain: Pointer to "struct ccs_domain_info". | |
1260 | + * | |
1261 | + * Returns true if @domain is in use, false otherwise. | |
1262 | + * | |
1263 | + * Caller holds rcu_read_lock(). | |
1264 | + */ | |
1265 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain) | |
1266 | +{ | |
1267 | + int idx; | |
1268 | + struct ccs_security *ptr; | |
1269 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1270 | + struct list_head *list = &ccs_cred_security_list[idx]; | |
1271 | + list_for_each_entry_rcu(ptr, list, list) { | |
1272 | + struct ccs_execve *ee = ptr->ee; | |
1273 | + if (ptr->ccs_domain_info == domain || | |
1274 | + (ee && ee->previous_domain == domain)) { | |
1275 | + return true; | |
1276 | + } | |
1277 | + } | |
1278 | + } | |
1279 | + return false; | |
1280 | +} | |
1281 | + | |
1282 | +/** | |
1283 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
1284 | + * | |
1285 | + * @ptr: Pointer to "struct ccs_security". | |
1286 | + * @list: Pointer to "struct list_head". | |
1287 | + * | |
1288 | + * Returns nothing. | |
1289 | + */ | |
1290 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
1291 | + struct list_head *list) | |
1292 | +{ | |
1293 | + unsigned long flags; | |
1294 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
1295 | + list_add_rcu(&ptr->list, list); | |
1296 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
1297 | +} | |
1298 | + | |
1299 | +/** | |
1300 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
1301 | + * | |
1302 | + * @task: Pointer to "struct task_struct". | |
1303 | + * | |
1304 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
1305 | + * out of memory, &ccs_default_security otherwise. | |
1306 | + * | |
1307 | + * If @task is current thread and "struct ccs_security" for current thread was | |
1308 | + * not found, I try to allocate it. But if allocation failed, current thread | |
1309 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
1310 | + * won't work. | |
1311 | + */ | |
1312 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
1313 | +{ | |
1314 | + struct ccs_security *ptr; | |
1315 | + struct list_head *list = &ccs_task_security_list | |
1316 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
1317 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
1318 | + while (!list->next); | |
1319 | + rcu_read_lock(); | |
1320 | + list_for_each_entry_rcu(ptr, list, list) { | |
1321 | + if (ptr->pid != task->pids[PIDTYPE_PID].pid) | |
1322 | + continue; | |
1323 | + rcu_read_unlock(); | |
1324 | + /* | |
1325 | + * Current thread needs to transit from old domain to new | |
1326 | + * domain before do_execve() succeeds in order to check | |
1327 | + * permission for interpreters and environment variables using | |
1328 | + * new domain's ACL rules. The domain transition has to be | |
1329 | + * visible from other CPU in order to allow interactive | |
1330 | + * enforcing mode. Also, the domain transition has to be | |
1331 | + * reverted if do_execve() failed. However, an LSM hook for | |
1332 | + * reverting domain transition is missing. | |
1333 | + * | |
1334 | + * security_prepare_creds() is called from prepare_creds() from | |
1335 | + * prepare_bprm_creds() from do_execve() before setting | |
1336 | + * current->in_execve flag, and current->in_execve flag is | |
1337 | + * cleared by the time next do_execve() request starts. | |
1338 | + * This means that we can emulate the missing LSM hook for | |
1339 | + * reverting domain transition, by calling this function from | |
1340 | + * security_prepare_creds(). | |
1341 | + * | |
1342 | + * If current->in_execve is not set but ptr->ccs_flags has | |
1343 | + * CCS_TASK_IS_IN_EXECVE set, it indicates that do_execve() | |
1344 | + * has failed and reverting domain transition is needed. | |
1345 | + */ | |
1346 | + if (task == current && | |
1347 | + (ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE) && | |
1348 | + !current->in_execve) { | |
1349 | + ccs_debug_trace("4"); | |
1350 | + ccs_clear_execve(-1, ptr); | |
1351 | + } | |
1352 | + return ptr; | |
1353 | + } | |
1354 | + rcu_read_unlock(); | |
1355 | + if (task != current) { | |
1356 | + /* | |
1357 | + * If a thread does nothing after fork(), caller will reach | |
1358 | + * here because "struct ccs_security" for that thread is not | |
1359 | + * yet allocated. But that thread is keeping a snapshot of | |
1360 | + * "struct ccs_security" taken as of ccs_task_create() | |
1361 | + * associated with that thread's "struct cred". | |
1362 | + * | |
1363 | + * Since that snapshot will be used as initial data when that | |
1364 | + * thread allocates "struct ccs_security" for that thread, we | |
1365 | + * can return that snapshot rather than &ccs_default_security. | |
1366 | + * | |
1367 | + * Since this function is called by only ccs_select_one() and | |
1368 | + * ccs_read_pid() (via ccs_task_domain() and ccs_task_flags()), | |
1369 | + * it is guaranteed that caller has called rcu_read_lock() | |
1370 | + * (via ccs_tasklist_lock()) before finding this thread and | |
1371 | + * this thread is valid. Therefore, we can do __task_cred(task) | |
1372 | + * like get_robust_list() does. | |
1373 | + */ | |
1374 | + return ccs_find_cred_security(__task_cred(task)); | |
1375 | + } | |
1376 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
1377 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
1378 | + if (!ptr) { | |
1379 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
1380 | + task->pid); | |
1381 | + send_sig(SIGKILL, current, 0); | |
1382 | + return &ccs_oom_security; | |
1383 | + } | |
1384 | + *ptr = *ccs_find_cred_security(task->cred); | |
1385 | + /* We can shortcut because task == current. */ | |
1386 | + ptr->pid = get_pid(((struct task_struct *) task)-> | |
1387 | + pids[PIDTYPE_PID].pid); | |
1388 | + ptr->cred = NULL; | |
1389 | + ccs_add_task_security(ptr, list); | |
1390 | + return ptr; | |
1391 | +} | |
1392 | + | |
1393 | +/** | |
1394 | + * ccs_copy_cred_security - Allocate memory for new credentials. | |
1395 | + * | |
1396 | + * @new: Pointer to "struct cred". | |
1397 | + * @old: Pointer to "struct cred". | |
1398 | + * @gfp: Memory allocation flags. | |
1399 | + * | |
1400 | + * Returns 0 on success, negative value otherwise. | |
1401 | + */ | |
1402 | +static int ccs_copy_cred_security(const struct cred *new, | |
1403 | + const struct cred *old, gfp_t gfp) | |
1404 | +{ | |
1405 | + struct ccs_security *old_security = ccs_find_cred_security(old); | |
1406 | + struct ccs_security *new_security = | |
1407 | + kzalloc(sizeof(*new_security), gfp); | |
1408 | + if (!new_security) | |
1409 | + return -ENOMEM; | |
1410 | + *new_security = *old_security; | |
1411 | + new_security->cred = new; | |
1412 | + ccs_add_cred_security(new_security); | |
1413 | + return 0; | |
1414 | +} | |
1415 | + | |
1416 | +/** | |
1417 | + * ccs_find_cred_security - Find "struct ccs_security" for given credential. | |
1418 | + * | |
1419 | + * @cred: Pointer to "struct cred". | |
1420 | + * | |
1421 | + * Returns pointer to "struct ccs_security" on success, &ccs_default_security | |
1422 | + * otherwise. | |
1423 | + */ | |
1424 | +static struct ccs_security *ccs_find_cred_security(const struct cred *cred) | |
1425 | +{ | |
1426 | + struct ccs_security *ptr; | |
1427 | + struct list_head *list = &ccs_cred_security_list | |
1428 | + [hash_ptr((void *) cred, CCS_TASK_SECURITY_HASH_BITS)]; | |
1429 | + rcu_read_lock(); | |
1430 | + list_for_each_entry_rcu(ptr, list, list) { | |
1431 | + if (ptr->cred != cred) | |
1432 | + continue; | |
1433 | + rcu_read_unlock(); | |
1434 | + return ptr; | |
1435 | + } | |
1436 | + rcu_read_unlock(); | |
1437 | + return &ccs_default_security; | |
1438 | +} | |
1439 | + | |
1440 | +/** | |
1441 | + * ccs_task_security_gc - Do garbage collection for "struct task_struct". | |
1442 | + * | |
1443 | + * Returns nothing. | |
1444 | + * | |
1445 | + * Since security_task_free() is missing, I can't release memory associated | |
1446 | + * with "struct task_struct" when a task dies. Therefore, I hold a reference on | |
1447 | + * "struct pid" and runs garbage collection when associated | |
1448 | + * "struct task_struct" has gone. | |
1449 | + */ | |
1450 | +static void ccs_task_security_gc(void) | |
1451 | +{ | |
1452 | + static DEFINE_SPINLOCK(lock); | |
1453 | + static atomic_t gc_counter = ATOMIC_INIT(0); | |
1454 | + unsigned int idx; | |
1455 | + /* | |
1456 | + * If some process is doing execve(), try to garbage collection now. | |
1457 | + * We should kfree() memory associated with "struct ccs_security"->ee | |
1458 | + * as soon as execve() has completed in order to compensate for lack of | |
1459 | + * security_bprm_free() and security_task_free() hooks. | |
1460 | + * | |
1461 | + * Otherwise, reduce frequency for performance reason. | |
1462 | + */ | |
1463 | + if (!atomic_read(&ccs_in_execve_tasks) && | |
1464 | + atomic_inc_return(&gc_counter) < 1024) | |
1465 | + return; | |
1466 | + if (!spin_trylock(&lock)) | |
1467 | + return; | |
1468 | + atomic_set(&gc_counter, 0); | |
1469 | + rcu_read_lock(); | |
1470 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) { | |
1471 | + struct ccs_security *ptr; | |
1472 | + struct list_head *list = &ccs_task_security_list[idx]; | |
1473 | + list_for_each_entry_rcu(ptr, list, list) { | |
1474 | + if (pid_task(ptr->pid, PIDTYPE_PID)) | |
1475 | + continue; | |
1476 | + ccs_del_security(ptr); | |
1477 | + } | |
1478 | + } | |
1479 | + rcu_read_unlock(); | |
1480 | + spin_unlock(&lock); | |
1481 | +} |
@@ -0,0 +1,34 @@ | ||
1 | +/* | |
2 | + * lsm.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2015 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Version: 1.0.37 2017/09/17 | |
7 | + */ | |
8 | + | |
9 | +#include <linux/version.h> | |
10 | +#include <linux/security.h> | |
11 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) | |
12 | +#include "lsm-4.12.c" | |
13 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) | |
14 | +#include "lsm-4.7.c" | |
15 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) | |
16 | +#include "lsm-4.2.c" | |
17 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) | |
18 | +#include "lsm-2.6.29.c" | |
19 | +/* | |
20 | + * AppArmor patch added "struct vfsmount *" to security_inode_\*() hooks. | |
21 | + * Detect it by checking whether D_PATH_DISCONNECT is defined or not. | |
22 | + * Also, there may be other kernels with "struct vfsmount *" added. | |
23 | + * If you got build failure, check security_inode_\*() hooks in | |
24 | + * include/linux/security.h. | |
25 | + */ | |
26 | +#elif defined(D_PATH_DISCONNECT) | |
27 | +#include "lsm-2.6.0-vfs.c" | |
28 | +#elif defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 25) | |
29 | +#include "lsm-2.6.0-vfs.c" | |
30 | +#elif defined(CONFIG_SECURITY_APPARMOR) && LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 24) | |
31 | +#include "lsm-2.6.0-vfs.c" | |
32 | +#else | |
33 | +#include "lsm-2.6.0.c" | |
34 | +#endif |
@@ -0,0 +1,60 @@ | ||
1 | +/* | |
2 | + * test.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2013 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + */ | |
6 | +#include "probe.h" | |
7 | + | |
8 | +/** | |
9 | + * ccs_init - Initialize this module. | |
10 | + * | |
11 | + * Returns 0 on success, -EINVAL otherwise. | |
12 | + */ | |
13 | +static int __init ccs_init(void) | |
14 | +{ | |
15 | +#if defined(LSM_HOOK_INIT) | |
16 | + if (!probe_security_hook_heads()) | |
17 | + goto out; | |
18 | +#else | |
19 | + if (!probe_security_ops()) | |
20 | + goto out; | |
21 | +#endif | |
22 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
23 | + if (!probe_find_task_by_vpid()) | |
24 | + goto out; | |
25 | + if (!probe_find_task_by_pid_ns()) | |
26 | + goto out; | |
27 | +#endif | |
28 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
29 | + if (!probe_vfsmount_lock()) | |
30 | + goto out; | |
31 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0) | |
32 | + if (!probe___d_path()) | |
33 | + goto out; | |
34 | +#else | |
35 | + if (!probe_d_absolute_path()) | |
36 | + goto out; | |
37 | +#endif | |
38 | + printk(KERN_INFO "All dependent symbols have been guessed.\n"); | |
39 | + printk(KERN_INFO "Please verify these addresses using System.map for this kernel (e.g. /boot/System.map-`uname -r` ).\n"); | |
40 | + printk(KERN_INFO "If these addresses are correct, you can try loading AKARI module on this kernel.\n"); | |
41 | + return 0; | |
42 | +out: | |
43 | + printk(KERN_INFO "Sorry, I couldn't guess dependent symbols.\n"); | |
44 | + printk(KERN_INFO "I need some changes for supporting your environment.\n"); | |
45 | + printk(KERN_INFO "Please contact the author.\n"); | |
46 | + return -EINVAL; | |
47 | +} | |
48 | + | |
49 | +/** | |
50 | + * ccs_exit - Exit this module. | |
51 | + * | |
52 | + * Returns nothing. | |
53 | + */ | |
54 | +static void ccs_exit(void) | |
55 | +{ | |
56 | +} | |
57 | + | |
58 | +module_init(ccs_init); | |
59 | +module_exit(ccs_exit); | |
60 | +MODULE_LICENSE("GPL"); |
@@ -0,0 +1,721 @@ | ||
1 | +/* | |
2 | + * probe.c | |
3 | + * | |
4 | + * Copyright (C) 2010-2013 Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> | |
5 | + * | |
6 | + * Functions in this file are doing runtime address resolution based on byte | |
7 | + * code comparison in order to allow LKM-based LSM modules to access built-in | |
8 | + * functions and variables which are not exported to LKMs. | |
9 | + * Since functions in this file are assuming that using identical source code, | |
10 | + * identical kernel config and identical compiler generates identical byte code | |
11 | + * output, functions in this file may not work on some architectures and/or | |
12 | + * environments. | |
13 | + * | |
14 | + * This file is used by AKARI and CaitSith. This file will become unnecessary | |
15 | + * when LKM-based LSM module comes back and TOMOYO 2.x becomes a LKM-based LSM | |
16 | + * module. | |
17 | + */ | |
18 | + | |
19 | +#include "probe.h" | |
20 | + | |
21 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) || LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 3) | |
22 | + | |
23 | +/** | |
24 | + * probe_kernel_read - Wrapper for kernel_read(). | |
25 | + * | |
26 | + * @file: Pointer to "struct file". | |
27 | + * @offset: Starting position. | |
28 | + * @addr: Buffer. | |
29 | + * @count: Size of @addr. | |
30 | + * | |
31 | + * Returns return value from kernel_read(). | |
32 | + */ | |
33 | +static int __init probe_kernel_read(struct file *file, unsigned long offset, | |
34 | + char *addr, unsigned long count) | |
35 | +{ | |
36 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8) | |
37 | + /* | |
38 | + * I can't use kernel_read() because seq_read() returns -EPIPE | |
39 | + * if &pos != &file->f_pos . | |
40 | + */ | |
41 | + mm_segment_t old_fs; | |
42 | + unsigned long pos = file->f_pos; | |
43 | + int result; | |
44 | + file->f_pos = offset; | |
45 | + old_fs = get_fs(); | |
46 | + set_fs(get_ds()); | |
47 | + result = vfs_read(file, (void __user *)addr, count, &file->f_pos); | |
48 | + set_fs(old_fs); | |
49 | + file->f_pos = pos; | |
50 | + return result; | |
51 | +#else | |
52 | + return kernel_read(file, offset, addr, count); | |
53 | +#endif | |
54 | +} | |
55 | + | |
56 | +/** | |
57 | + * probe_find_symbol - Find function's address from /proc/kallsyms . | |
58 | + * | |
59 | + * @keyline: Function to find. | |
60 | + * | |
61 | + * Returns address of specified function on success, NULL otherwise. | |
62 | + */ | |
63 | +static void *__init probe_find_symbol(const char *keyline) | |
64 | +{ | |
65 | + struct file *file = NULL; | |
66 | + char *buf; | |
67 | + unsigned long entry = 0; | |
68 | + { | |
69 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) | |
70 | + struct file_system_type *fstype = get_fs_type("proc"); | |
71 | + struct vfsmount *mnt = vfs_kern_mount(fstype, 0, "proc", NULL); | |
72 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
73 | + struct file_system_type *fstype = NULL; | |
74 | + struct vfsmount *mnt = do_kern_mount("proc", 0, "proc", NULL); | |
75 | +#else | |
76 | + struct file_system_type *fstype = get_fs_type("proc"); | |
77 | + struct vfsmount *mnt = kern_mount(fstype); | |
78 | +#endif | |
79 | + struct dentry *root; | |
80 | + struct dentry *dentry; | |
81 | + /* | |
82 | + * We embed put_filesystem() here because it is not exported. | |
83 | + */ | |
84 | + if (fstype) | |
85 | + module_put(fstype->owner); | |
86 | + if (IS_ERR(mnt)) | |
87 | + goto out; | |
88 | + root = dget(mnt->mnt_root); | |
89 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) | |
90 | + inode_lock(root->d_inode); | |
91 | + dentry = lookup_one_len("kallsyms", root, 8); | |
92 | + inode_unlock(root->d_inode); | |
93 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16) | |
94 | + mutex_lock(&root->d_inode->i_mutex); | |
95 | + dentry = lookup_one_len("kallsyms", root, 8); | |
96 | + mutex_unlock(&root->d_inode->i_mutex); | |
97 | +#else | |
98 | + down(&root->d_inode->i_sem); | |
99 | + dentry = lookup_one_len("kallsyms", root, 8); | |
100 | + up(&root->d_inode->i_sem); | |
101 | +#endif | |
102 | + dput(root); | |
103 | + if (IS_ERR(dentry)) | |
104 | + mntput(mnt); | |
105 | + else { | |
106 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0) | |
107 | + struct path path = { .mnt = mnt, .dentry = dentry }; | |
108 | + file = dentry_open(&path, O_RDONLY, current_cred()); | |
109 | +#else | |
110 | + file = dentry_open(dentry, mnt, O_RDONLY | |
111 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29) | |
112 | + , current_cred() | |
113 | +#endif | |
114 | + ); | |
115 | +#endif | |
116 | + } | |
117 | + } | |
118 | + if (IS_ERR(file) || !file) | |
119 | + goto out; | |
120 | + buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
121 | + if (buf) { | |
122 | + int len; | |
123 | + int offset = 0; | |
124 | + while ((len = probe_kernel_read(file, offset, buf, | |
125 | + PAGE_SIZE - 1)) > 0) { | |
126 | + char *cp; | |
127 | + buf[len] = '\0'; | |
128 | + cp = strrchr(buf, '\n'); | |
129 | + if (!cp) | |
130 | + break; | |
131 | + *(cp + 1) = '\0'; | |
132 | + offset += strlen(buf); | |
133 | + cp = strstr(buf, keyline); | |
134 | + if (!cp) | |
135 | + continue; | |
136 | + *cp = '\0'; | |
137 | + while (cp > buf && *(cp - 1) != '\n') | |
138 | + cp--; | |
139 | + entry = simple_strtoul(cp, NULL, 16); | |
140 | + break; | |
141 | + } | |
142 | + kfree(buf); | |
143 | + } | |
144 | + filp_close(file, NULL); | |
145 | +out: | |
146 | + return (void *) entry; | |
147 | +} | |
148 | + | |
149 | +#endif | |
150 | + | |
151 | +#if defined(LSM_HOOK_INIT) | |
152 | + | |
153 | +/* | |
154 | + * Dummy variable for finding location of | |
155 | + * "struct security_hook_heads security_hook_heads". | |
156 | + */ | |
157 | +struct security_hook_heads probe_dummy_security_hook_heads; | |
158 | + | |
159 | +/** | |
160 | + * probe_security_bprm_committed_creds - Dummy function which does identical to security_bprm_committed_creds() in security/security.c. | |
161 | + * | |
162 | + * @bprm: Pointer to "struct linux_binprm". | |
163 | + * | |
164 | + * Returns nothing. | |
165 | + */ | |
166 | +void probe_security_bprm_committed_creds(struct linux_binprm *bprm) | |
167 | +{ | |
168 | + do { | |
169 | + struct security_hook_list *p; | |
170 | + list_for_each_entry(p, &probe_dummy_security_hook_heads. | |
171 | + bprm_committed_creds, list) | |
172 | + p->hook.bprm_committed_creds(bprm); | |
173 | + } while (0); | |
174 | +} | |
175 | + | |
176 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
177 | + | |
178 | +/* | |
179 | + * Dummy variable for finding address of | |
180 | + * "struct security_operations *security_ops". | |
181 | + */ | |
182 | +static struct security_operations *probe_dummy_security_ops; | |
183 | + | |
184 | +/** | |
185 | + * probe_security_file_alloc - Dummy function which does identical to security_file_alloc() in security/security.c. | |
186 | + * | |
187 | + * @file: Pointer to "struct file". | |
188 | + * | |
189 | + * Returns return value from security_file_alloc(). | |
190 | + */ | |
191 | +static int probe_security_file_alloc(struct file *file) | |
192 | +{ | |
193 | + return probe_dummy_security_ops->file_alloc_security(file); | |
194 | +} | |
195 | + | |
196 | +#if defined(CONFIG_ARM) | |
197 | + | |
198 | +/** | |
199 | + * probe_security_ops_on_arm - Find security_ops on ARM. | |
200 | + * | |
201 | + * @base: Address of security_file_alloc(). | |
202 | + * | |
203 | + * Returns address of security_ops on success, NULL otherwise. | |
204 | + */ | |
205 | +static void * __init probe_security_ops_on_arm(unsigned int *base) | |
206 | +{ | |
207 | + static unsigned int *ip4ret; | |
208 | + int i; | |
209 | + const unsigned long addr = (unsigned long) &probe_dummy_security_ops; | |
210 | + unsigned int *ip = (unsigned int *) probe_security_file_alloc; | |
211 | + for (i = 0; i < 32; ip++, i++) { | |
212 | + if (*(ip + 2 + ((*ip & 0xFFF) >> 2)) != addr) | |
213 | + continue; | |
214 | + ip = base + i; | |
215 | + ip4ret = (unsigned int *) (*(ip + 2 + ((*ip & 0xFFF) >> 2))); | |
216 | + return &ip4ret; | |
217 | + } | |
218 | + ip = (unsigned int *) probe_security_file_alloc; | |
219 | + for (i = 0; i < 32; ip++, i++) { | |
220 | + /* | |
221 | + * Find | |
222 | + * ldr r3, [pc, #offset1] | |
223 | + * ldr r3, [r3, #offset2] | |
224 | + * sequence. | |
225 | + */ | |
226 | + if ((*ip & 0xFFFFF000) != 0xE59F3000 || | |
227 | + (*(ip + 1) & 0xFFFFF000) != 0xE5933000) | |
228 | + continue; | |
229 | + ip4ret = (unsigned int *) (*(ip + 2 + ((*ip & 0xFFF) >> 2))); | |
230 | + ip4ret += (*(ip + 1) & 0xFFF) >> 2; | |
231 | + if ((unsigned long) ip4ret != addr) | |
232 | + continue; | |
233 | + ip = base + i; | |
234 | + ip4ret = (unsigned int *) (*(ip + 2 + ((*ip & 0xFFF) >> 2))); | |
235 | + ip4ret += (*(ip + 1) & 0xFFF) >> 2; | |
236 | + return &ip4ret; | |
237 | + } | |
238 | + return NULL; | |
239 | +} | |
240 | + | |
241 | +#endif | |
242 | + | |
243 | +#endif | |
244 | + | |
245 | +#if defined(CONFIG_ARM) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
246 | +/** | |
247 | + * probe_find_vfsmount_lock_on_arm - Find vfsmount_lock spinlock on ARM. | |
248 | + * | |
249 | + * @ip: Address of dummy function's entry point. | |
250 | + * @addr: Address of the variable which is used within @function. | |
251 | + * @base: Address of function's entry point. | |
252 | + * | |
253 | + * Returns address of vfsmount_lock on success, NULL otherwise. | |
254 | + */ | |
255 | +static void * __init probe_find_vfsmount_lock_on_arm(unsigned int *ip, | |
256 | + unsigned long addr, | |
257 | + unsigned int *base) | |
258 | +{ | |
259 | + int i; | |
260 | + for (i = 0; i < 32; ip++, i++) { | |
261 | + static unsigned int *ip4ret; | |
262 | + if (*(ip + 2 + ((*ip & 0xFFF) >> 2)) != addr) | |
263 | + continue; | |
264 | + ip = base + i; | |
265 | + ip4ret = (unsigned int *) (*(ip + 2 + ((*ip & 0xFFF) >> 2))); | |
266 | + return &ip4ret; | |
267 | + } | |
268 | + return NULL; | |
269 | +} | |
270 | +#endif | |
271 | + | |
272 | +/** | |
273 | + * probe_find_variable - Find variable's address using dummy. | |
274 | + * | |
275 | + * @function: Pointer to dummy function's entry point. | |
276 | + * @addr: Address of the variable which is used within @function. | |
277 | + * @symbol: Name of symbol to resolve. | |
278 | + * | |
279 | + * This trick depends on below assumptions. | |
280 | + * | |
281 | + * (1) @addr is found within 128 bytes from @function, even if additional | |
282 | + * code (e.g. debug symbols) is added. | |
283 | + * (2) It is safe to read 128 bytes from @function. | |
284 | + * (3) @addr != Byte code except @addr. | |
285 | + */ | |
286 | +static void * __init probe_find_variable(void *function, unsigned long addr, | |
287 | + const char *symbol) | |
288 | +{ | |
289 | + int i; | |
290 | + u8 *base; | |
291 | + u8 *cp = function; | |
292 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) || LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 3) | |
293 | + if (*symbol == ' ') | |
294 | + base = probe_find_symbol(symbol); | |
295 | + else | |
296 | +#endif | |
297 | + base = __symbol_get(symbol); | |
298 | + if (!base) | |
299 | + return NULL; | |
300 | +#if defined(CONFIG_ARM) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) && !defined(LSM_HOOK_INIT) | |
301 | + if (function == probe_security_file_alloc) | |
302 | + return probe_security_ops_on_arm((unsigned int *) base); | |
303 | +#endif | |
304 | +#if defined(CONFIG_ARM) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
305 | + return probe_find_vfsmount_lock_on_arm(function, addr, | |
306 | + (unsigned int *) base); | |
307 | +#endif | |
308 | + /* First, assume absolute adressing mode is used. */ | |
309 | + for (i = 0; i < 128; i++) { | |
310 | + if (*(unsigned long *) cp == addr) | |
311 | + return base + i; | |
312 | + cp++; | |
313 | + } | |
314 | + /* Next, assume PC-relative addressing mode is used. */ | |
315 | +#if defined(CONFIG_S390) | |
316 | + cp = function; | |
317 | + for (i = 0; i < 128; i++) { | |
318 | + if ((unsigned long) (cp + (*(int *) cp) * 2 - 2) == addr) { | |
319 | + static void *cp4ret; | |
320 | + cp = base + i; | |
321 | + cp += (*(int *) cp) * 2 - 2; | |
322 | + cp4ret = cp; | |
323 | + return &cp4ret; | |
324 | + } | |
325 | + cp++; | |
326 | + } | |
327 | +#endif | |
328 | + cp = function; | |
329 | + for (i = 0; i < 128; i++) { | |
330 | + if ((unsigned long) (cp + sizeof(int) + *(int *) cp) == addr) { | |
331 | + static void *cp4ret; | |
332 | + cp = base + i; | |
333 | + cp += sizeof(int) + *(int *) cp; | |
334 | + cp4ret = cp; | |
335 | + return &cp4ret; | |
336 | + } | |
337 | + cp++; | |
338 | + } | |
339 | + cp = function; | |
340 | + for (i = 0; i < 128; i++) { | |
341 | + if ((unsigned long) (long) (*(int *) cp) == addr) { | |
342 | + static void *cp4ret; | |
343 | + cp = base + i; | |
344 | + cp = (void *) (long) (*(int *) cp); | |
345 | + cp4ret = cp; | |
346 | + return &cp4ret; | |
347 | + } | |
348 | + cp++; | |
349 | + } | |
350 | + return NULL; | |
351 | +} | |
352 | + | |
353 | +#if defined(LSM_HOOK_INIT) | |
354 | + | |
355 | +/** | |
356 | + * probe_security_hook_heads - Find address of "struct security_hook_heads security_hook_heads". | |
357 | + * | |
358 | + * Returns pointer to "struct security_hook_heads" on success, NULL otherwise. | |
359 | + */ | |
360 | +struct security_hook_heads * __init probe_security_hook_heads(void) | |
361 | +{ | |
362 | + const unsigned int offset = offsetof(struct security_hook_heads, | |
363 | + bprm_committed_creds); | |
364 | + void *cp; | |
365 | + /* Guess "struct security_hook_heads security_hook_heads;". */ | |
366 | + cp = probe_find_variable(probe_security_bprm_committed_creds, | |
367 | + ((unsigned long) | |
368 | + &probe_dummy_security_hook_heads) + offset, | |
369 | + " security_bprm_committed_creds\n"); | |
370 | + if (!cp) { | |
371 | + printk(KERN_ERR | |
372 | + "Can't resolve security_bprm_committed_creds().\n"); | |
373 | + return NULL; | |
374 | + } | |
375 | + /* This should be "struct security_hook_heads security_hook_heads;". */ | |
376 | + cp = ((void *) (*(unsigned long *) cp)) - offset; | |
377 | + printk(KERN_INFO "security_hook_heads=%p\n", cp); | |
378 | + return cp; | |
379 | +} | |
380 | + | |
381 | +#else | |
382 | + | |
383 | +/** | |
384 | + * probe_security_ops - Find address of "struct security_operations *security_ops". | |
385 | + * | |
386 | + * Returns pointer to "struct security_operations" on success, NULL otherwise. | |
387 | + */ | |
388 | +struct security_operations * __init probe_security_ops(void) | |
389 | +{ | |
390 | + struct security_operations **ptr; | |
391 | + struct security_operations *ops; | |
392 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
393 | + void *cp; | |
394 | + /* Guess "struct security_operations *security_ops;". */ | |
395 | + cp = probe_find_variable(probe_security_file_alloc, (unsigned long) | |
396 | + &probe_dummy_security_ops, | |
397 | + " security_file_alloc\n"); | |
398 | + if (!cp) { | |
399 | + printk(KERN_ERR "Can't resolve security_file_alloc().\n"); | |
400 | + return NULL; | |
401 | + } | |
402 | + /* This should be "struct security_operations *security_ops;". */ | |
403 | + ptr = *(struct security_operations ***) cp; | |
404 | +#else | |
405 | + /* This is "struct security_operations *security_ops;". */ | |
406 | + ptr = (struct security_operations **) __symbol_get("security_ops"); | |
407 | +#endif | |
408 | + if (!ptr) { | |
409 | + printk(KERN_ERR "Can't resolve security_ops structure.\n"); | |
410 | + return NULL; | |
411 | + } | |
412 | + printk(KERN_INFO "security_ops=%p\n", ptr); | |
413 | + ops = *ptr; | |
414 | + if (!ops) { | |
415 | + printk(KERN_ERR "No security_operations registered.\n"); | |
416 | + return NULL; | |
417 | + } | |
418 | + return ops; | |
419 | +} | |
420 | + | |
421 | +#endif | |
422 | + | |
423 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
424 | + | |
425 | +/** | |
426 | + * probe_find_task_by_vpid - Find address of find_task_by_vpid(). | |
427 | + * | |
428 | + * Returns address of find_task_by_vpid() on success, NULL otherwise. | |
429 | + */ | |
430 | +void * __init probe_find_task_by_vpid(void) | |
431 | +{ | |
432 | + void *ptr; | |
433 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
434 | + ptr = probe_find_symbol(" find_task_by_vpid\n"); | |
435 | +#else | |
436 | + ptr = __symbol_get("find_task_by_vpid"); | |
437 | +#endif | |
438 | + if (!ptr) { | |
439 | + printk(KERN_ERR "Can't resolve find_task_by_vpid().\n"); | |
440 | + return NULL; | |
441 | + } | |
442 | + printk(KERN_INFO "find_task_by_vpid=%p\n", ptr); | |
443 | + return ptr; | |
444 | +} | |
445 | + | |
446 | +/** | |
447 | + * probe_find_task_by_pid_ns - Find address of find_task_by_pid(). | |
448 | + * | |
449 | + * Returns address of find_task_by_pid_ns() on success, NULL otherwise. | |
450 | + */ | |
451 | +void * __init probe_find_task_by_pid_ns(void) | |
452 | +{ | |
453 | + void *ptr; | |
454 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) | |
455 | + ptr = probe_find_symbol(" find_task_by_pid_ns\n"); | |
456 | +#else | |
457 | + ptr = __symbol_get("find_task_by_pid_ns"); | |
458 | +#endif | |
459 | + if (!ptr) { | |
460 | + printk(KERN_ERR "Can't resolve find_task_by_pid_ns().\n"); | |
461 | + return NULL; | |
462 | + } | |
463 | + printk(KERN_INFO "find_task_by_pid_ns=%p\n", ptr); | |
464 | + return ptr; | |
465 | +} | |
466 | + | |
467 | +#endif | |
468 | + | |
469 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
470 | + | |
471 | +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) | |
472 | + | |
473 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 3) | |
474 | + | |
475 | +/* Dummy variable for finding address of "spinlock_t vfsmount_lock". */ | |
476 | +static spinlock_t probe_dummy_vfsmount_lock __cacheline_aligned_in_smp = | |
477 | + SPIN_LOCK_UNLOCKED; | |
478 | + | |
479 | +static struct list_head *probe_mount_hashtable; | |
480 | +static int probe_hash_mask, probe_hash_bits; | |
481 | + | |
482 | +/** | |
483 | + * hash - Copy of hash() in fs/namespace.c. | |
484 | + * | |
485 | + * @mnt: Pointer to "struct vfsmount". | |
486 | + * @dentry: Pointer to "struct dentry". | |
487 | + * | |
488 | + * Returns hash value. | |
489 | + */ | |
490 | +static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) | |
491 | +{ | |
492 | + unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES); | |
493 | + tmp += ((unsigned long) dentry / L1_CACHE_BYTES); | |
494 | + tmp = tmp + (tmp >> probe_hash_bits); | |
495 | + return tmp & probe_hash_mask; | |
496 | +} | |
497 | + | |
498 | +/** | |
499 | + * probe_lookup_mnt - Dummy function which does identical to lookup_mnt() in fs/namespace.c. | |
500 | + * | |
501 | + * @mnt: Pointer to "struct vfsmount". | |
502 | + * @dentry: Pointer to "struct dentry". | |
503 | + * | |
504 | + * Returns pointer to "struct vfsmount". | |
505 | + */ | |
506 | +static struct vfsmount *probe_lookup_mnt(struct vfsmount *mnt, | |
507 | + struct dentry *dentry) | |
508 | +{ | |
509 | + struct list_head *head = probe_mount_hashtable + hash(mnt, dentry); | |
510 | + struct list_head *tmp = head; | |
511 | + struct vfsmount *p, *found = NULL; | |
512 | + | |
513 | + spin_lock(&probe_dummy_vfsmount_lock); | |
514 | + for (;;) { | |
515 | + tmp = tmp->next; | |
516 | + p = NULL; | |
517 | + if (tmp == head) | |
518 | + break; | |
519 | + p = list_entry(tmp, struct vfsmount, mnt_hash); | |
520 | + if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) { | |
521 | + found = mntget(p); | |
522 | + break; | |
523 | + } | |
524 | + } | |
525 | + spin_unlock(&probe_dummy_vfsmount_lock); | |
526 | + return found; | |
527 | +} | |
528 | + | |
529 | +/** | |
530 | + * probe_vfsmount_lock - Find address of "spinlock_t vfsmount_lock". | |
531 | + * | |
532 | + * Returns address of vfsmount_lock on success, NULL otherwise. | |
533 | + */ | |
534 | +void * __init probe_vfsmount_lock(void) | |
535 | +{ | |
536 | + void *cp; | |
537 | + spinlock_t *ptr; | |
538 | + /* Guess "spinlock_t vfsmount_lock;". */ | |
539 | + cp = probe_find_variable(probe_lookup_mnt, (unsigned long) | |
540 | + &probe_dummy_vfsmount_lock, " lookup_mnt\n"); | |
541 | + if (!cp) { | |
542 | + printk(KERN_ERR "Can't resolve lookup_mnt().\n"); | |
543 | + return NULL; | |
544 | + } | |
545 | + /* This should be "spinlock_t *vfsmount_lock;". */ | |
546 | + ptr = *(spinlock_t **) cp; | |
547 | + if (!ptr) { | |
548 | + printk(KERN_ERR "Can't resolve vfsmount_lock .\n"); | |
549 | + return NULL; | |
550 | + } | |
551 | + printk(KERN_INFO "vfsmount_lock=%p\n", ptr); | |
552 | + return ptr; | |
553 | +} | |
554 | + | |
555 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15) | |
556 | + | |
557 | +/* Dummy variable for finding address of "spinlock_t vfsmount_lock". */ | |
558 | +static spinlock_t probe_dummy_vfsmount_lock; | |
559 | + | |
560 | +/** | |
561 | + * probe_follow_up - Dummy function which does identical to follow_up() in fs/namei.c. | |
562 | + * | |
563 | + * @mnt: Pointer to "struct vfsmount *". | |
564 | + * @dentry: Pointer to "struct dentry *". | |
565 | + * | |
566 | + * Returns 1 if followed up, 0 otehrwise. | |
567 | + */ | |
568 | +static int probe_follow_up(struct vfsmount **mnt, struct dentry **dentry) | |
569 | +{ | |
570 | + struct vfsmount *parent; | |
571 | + struct dentry *mountpoint; | |
572 | + spin_lock(&probe_dummy_vfsmount_lock); | |
573 | + parent = (*mnt)->mnt_parent; | |
574 | + if (parent == *mnt) { | |
575 | + spin_unlock(&probe_dummy_vfsmount_lock); | |
576 | + return 0; | |
577 | + } | |
578 | + mntget(parent); | |
579 | + mountpoint = dget((*mnt)->mnt_mountpoint); | |
580 | + spin_unlock(&probe_dummy_vfsmount_lock); | |
581 | + dput(*dentry); | |
582 | + *dentry = mountpoint; | |
583 | + mntput(*mnt); | |
584 | + *mnt = parent; | |
585 | + return 1; | |
586 | +} | |
587 | + | |
588 | +/** | |
589 | + * probe_vfsmount_lock - Find address of "spinlock_t vfsmount_lock". | |
590 | + * | |
591 | + * Returns address of vfsmount_lock on success, NULL otherwise. | |
592 | + */ | |
593 | +void * __init probe_vfsmount_lock(void) | |
594 | +{ | |
595 | + void *cp; | |
596 | + spinlock_t *ptr; | |
597 | + /* Guess "spinlock_t vfsmount_lock;". */ | |
598 | + cp = probe_find_variable(probe_follow_up, (unsigned long) | |
599 | + &probe_dummy_vfsmount_lock, "follow_up"); | |
600 | + if (!cp) { | |
601 | + printk(KERN_ERR "Can't resolve follow_up().\n"); | |
602 | + return NULL; | |
603 | + } | |
604 | + /* This should be "spinlock_t *vfsmount_lock;". */ | |
605 | + ptr = *(spinlock_t **) cp; | |
606 | + if (!ptr) { | |
607 | + printk(KERN_ERR "Can't resolve vfsmount_lock .\n"); | |
608 | + return NULL; | |
609 | + } | |
610 | + printk(KERN_INFO "vfsmount_lock=%p\n", ptr); | |
611 | + return ptr; | |
612 | +} | |
613 | + | |
614 | +#else | |
615 | + | |
616 | +/* Dummy variable for finding address of "spinlock_t vfsmount_lock". */ | |
617 | +static spinlock_t probe_dummy_vfsmount_lock; | |
618 | + | |
619 | +/** | |
620 | + * probe_mnt_pin - Dummy function which does identical to mnt_pin() in fs/namespace.c. | |
621 | + * | |
622 | + * @mnt: Pointer to "struct vfsmount". | |
623 | + * | |
624 | + * Returns nothing. | |
625 | + */ | |
626 | +static void probe_mnt_pin(struct vfsmount *mnt) | |
627 | +{ | |
628 | + spin_lock(&probe_dummy_vfsmount_lock); | |
629 | + mnt->mnt_pinned++; | |
630 | + spin_unlock(&probe_dummy_vfsmount_lock); | |
631 | +} | |
632 | + | |
633 | +/** | |
634 | + * probe_vfsmount_lock - Find address of "spinlock_t vfsmount_lock". | |
635 | + * | |
636 | + * Returns address of vfsmount_lock on success, NULL otherwise. | |
637 | + */ | |
638 | +void * __init probe_vfsmount_lock(void) | |
639 | +{ | |
640 | + void *cp; | |
641 | + spinlock_t *ptr; | |
642 | + /* Guess "spinlock_t vfsmount_lock;". */ | |
643 | + cp = probe_find_variable(probe_mnt_pin, (unsigned long) | |
644 | + &probe_dummy_vfsmount_lock, "mnt_pin"); | |
645 | + if (!cp) { | |
646 | + printk(KERN_ERR "Can't resolve mnt_pin().\n"); | |
647 | + return NULL; | |
648 | + } | |
649 | + /* This should be "spinlock_t *vfsmount_lock;". */ | |
650 | + ptr = *(spinlock_t **) cp; | |
651 | + if (!ptr) { | |
652 | + printk(KERN_ERR "Can't resolve vfsmount_lock .\n"); | |
653 | + return NULL; | |
654 | + } | |
655 | + printk(KERN_INFO "vfsmount_lock=%p\n", ptr); | |
656 | + return ptr; | |
657 | +} | |
658 | + | |
659 | +#endif | |
660 | + | |
661 | +#else | |
662 | + | |
663 | +/* | |
664 | + * Never mark this variable as __initdata , for this variable might be accessed | |
665 | + * by caller of probe_find_vfsmount_lock(). | |
666 | + */ | |
667 | +static spinlock_t probe_dummy_vfsmount_lock; | |
668 | + | |
669 | +/** | |
670 | + * probe_vfsmount_lock - Find address of "spinlock_t vfsmount_lock". | |
671 | + * | |
672 | + * Returns address of vfsmount_lock. | |
673 | + */ | |
674 | +void * __init probe_vfsmount_lock(void) | |
675 | +{ | |
676 | + return &probe_dummy_vfsmount_lock; | |
677 | +} | |
678 | + | |
679 | +#endif | |
680 | + | |
681 | +#endif | |
682 | + | |
683 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0) | |
684 | + | |
685 | +/** | |
686 | + * probe___d_path - Find address of "__d_path()". | |
687 | + * | |
688 | + * Returns address of __d_path() on success, NULL otherwise. | |
689 | + */ | |
690 | +void * __init probe___d_path(void) | |
691 | +{ | |
692 | + void *ptr = probe_find_symbol(" __d_path\n"); | |
693 | + if (!ptr) { | |
694 | + printk(KERN_ERR "Can't resolve __d_path().\n"); | |
695 | + return NULL; | |
696 | + } | |
697 | + printk(KERN_INFO "__d_path=%p\n", ptr); | |
698 | + return ptr; | |
699 | +} | |
700 | + | |
701 | +#endif | |
702 | + | |
703 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0) | |
704 | + | |
705 | +/** | |
706 | + * probe_d_absolute_path - Find address of "d_absolute_path()". | |
707 | + * | |
708 | + * Returns address of d_absolute_path() on success, NULL otherwise. | |
709 | + */ | |
710 | +void * __init probe_d_absolute_path(void) | |
711 | +{ | |
712 | + void *ptr = probe_find_symbol(" d_absolute_path\n"); | |
713 | + if (!ptr) { | |
714 | + printk(KERN_ERR "Can't resolve d_absolute_path().\n"); | |
715 | + return NULL; | |
716 | + } | |
717 | + printk(KERN_INFO "d_absolute_path=%p\n", ptr); | |
718 | + return ptr; | |
719 | +} | |
720 | + | |
721 | +#endif |
@@ -0,0 +1,2165 @@ | ||
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 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) | |
69 | +#include <linux/sched/signal.h> | |
70 | +#endif | |
71 | +#include <net/sock.h> | |
72 | +#include <net/af_unix.h> | |
73 | +#include <net/ip.h> | |
74 | +#include <net/ipv6.h> | |
75 | +#include <net/udp.h> | |
76 | + | |
77 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
78 | +#define sk_family family | |
79 | +#define sk_protocol protocol | |
80 | +#define sk_type type | |
81 | +#endif | |
82 | + | |
83 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) | |
84 | + | |
85 | +/* Structure for holding "struct vfsmount *" and "struct dentry *". */ | |
86 | +struct path { | |
87 | + struct vfsmount *mnt; | |
88 | + struct dentry *dentry; | |
89 | +}; | |
90 | + | |
91 | +#endif | |
92 | + | |
93 | +#ifndef __printf | |
94 | +#define __printf(a,b) __attribute__((format(printf,a,b))) | |
95 | +#endif | |
96 | +#ifndef __packed | |
97 | +#define __packed __attribute__((__packed__)) | |
98 | +#endif | |
99 | +#ifndef bool | |
100 | +#define bool _Bool | |
101 | +#endif | |
102 | +#ifndef false | |
103 | +#define false 0 | |
104 | +#endif | |
105 | +#ifndef true | |
106 | +#define true 1 | |
107 | +#endif | |
108 | + | |
109 | +#ifndef __user | |
110 | +#define __user | |
111 | +#endif | |
112 | + | |
113 | +#ifndef current_uid | |
114 | +#define current_uid() (current->uid) | |
115 | +#endif | |
116 | +#ifndef current_gid | |
117 | +#define current_gid() (current->gid) | |
118 | +#endif | |
119 | +#ifndef current_euid | |
120 | +#define current_euid() (current->euid) | |
121 | +#endif | |
122 | +#ifndef current_egid | |
123 | +#define current_egid() (current->egid) | |
124 | +#endif | |
125 | +#ifndef current_suid | |
126 | +#define current_suid() (current->suid) | |
127 | +#endif | |
128 | +#ifndef current_sgid | |
129 | +#define current_sgid() (current->sgid) | |
130 | +#endif | |
131 | +#ifndef current_fsuid | |
132 | +#define current_fsuid() (current->fsuid) | |
133 | +#endif | |
134 | +#ifndef current_fsgid | |
135 | +#define current_fsgid() (current->fsgid) | |
136 | +#endif | |
137 | + | |
138 | +#ifndef DEFINE_SPINLOCK | |
139 | +#define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED | |
140 | +#endif | |
141 | + | |
142 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16) | |
143 | +#define mutex semaphore | |
144 | +#define mutex_init(mutex) init_MUTEX(mutex) | |
145 | +#define mutex_unlock(mutex) up(mutex) | |
146 | +#define mutex_lock(mutex) down(mutex) | |
147 | +#define mutex_lock_interruptible(mutex) down_interruptible(mutex) | |
148 | +#define mutex_trylock(mutex) (!down_trylock(mutex)) | |
149 | +#define DEFINE_MUTEX(mutexname) DECLARE_MUTEX(mutexname) | |
150 | +#endif | |
151 | + | |
152 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15) | |
153 | +#define MS_UNBINDABLE (1<<17) /* change to unbindable */ | |
154 | +#define MS_PRIVATE (1<<18) /* change to private */ | |
155 | +#define MS_SLAVE (1<<19) /* change to slave */ | |
156 | +#define MS_SHARED (1<<20) /* change to shared */ | |
157 | +#endif | |
158 | + | |
159 | +#ifndef container_of | |
160 | +#define container_of(ptr, type, member) ({ \ | |
161 | + const typeof(((type *)0)->member) *__mptr = (ptr); \ | |
162 | + (type *)((char *)__mptr - offsetof(type, member)); }) | |
163 | +#endif | |
164 | + | |
165 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
166 | +#define smp_read_barrier_depends smp_rmb | |
167 | +#endif | |
168 | + | |
169 | +#ifndef ACCESS_ONCE | |
170 | +#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) | |
171 | +#endif | |
172 | + | |
173 | +#ifndef rcu_dereference | |
174 | +#define rcu_dereference(p) ({ \ | |
175 | + typeof(p) _________p1 = ACCESS_ONCE(p); \ | |
176 | + smp_read_barrier_depends(); /* see RCU */ \ | |
177 | + (_________p1); \ | |
178 | + }) | |
179 | +#endif | |
180 | + | |
181 | +#ifndef rcu_assign_pointer | |
182 | +#define rcu_assign_pointer(p, v) \ | |
183 | + ({ \ | |
184 | + if (!__builtin_constant_p(v) || \ | |
185 | + ((v) != NULL)) \ | |
186 | + smp_wmb(); /* see RCU */ \ | |
187 | + (p) = (v); \ | |
188 | + }) | |
189 | +#endif | |
190 | + | |
191 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0) | |
192 | +#define f_vfsmnt f_path.mnt | |
193 | +#endif | |
194 | + | |
195 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14) | |
196 | + | |
197 | +/** | |
198 | + * kzalloc() - Allocate memory. The memory is set to zero. | |
199 | + * | |
200 | + * @size: Size to allocate. | |
201 | + * @flags: GFP flags. | |
202 | + * | |
203 | + * Returns pointer to allocated memory on success, NULL otherwise. | |
204 | + * | |
205 | + * This is for compatibility with older kernels. | |
206 | + * | |
207 | + * Since several distributions backported kzalloc(), I define it as a macro | |
208 | + * rather than an inlined function in order to avoid multiple definition error. | |
209 | + */ | |
210 | +#define kzalloc(size, flags) ({ \ | |
211 | + void *ret = kmalloc((size), (flags)); \ | |
212 | + if (ret) \ | |
213 | + memset(ret, 0, (size)); \ | |
214 | + ret; }) | |
215 | + | |
216 | +#endif | |
217 | + | |
218 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) | |
219 | + | |
220 | +/** | |
221 | + * path_put - Drop reference on "struct path". | |
222 | + * | |
223 | + * @path: Pointer to "struct path". | |
224 | + * | |
225 | + * Returns nothing. | |
226 | + * | |
227 | + * This is for compatibility with older kernels. | |
228 | + */ | |
229 | +static inline void path_put(struct path *path) | |
230 | +{ | |
231 | + dput(path->dentry); | |
232 | + mntput(path->mnt); | |
233 | +} | |
234 | + | |
235 | +#endif | |
236 | + | |
237 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) | |
238 | + | |
239 | +/** | |
240 | + * __list_add_rcu - Insert a new entry between two known consecutive entries. | |
241 | + * | |
242 | + * @new: Pointer to "struct list_head". | |
243 | + * @prev: Pointer to "struct list_head". | |
244 | + * @next: Pointer to "struct list_head". | |
245 | + * | |
246 | + * Returns nothing. | |
247 | + * | |
248 | + * This is for compatibility with older kernels. | |
249 | + */ | |
250 | +static inline void __list_add_rcu(struct list_head *new, | |
251 | + struct list_head *prev, | |
252 | + struct list_head *next) | |
253 | +{ | |
254 | + new->next = next; | |
255 | + new->prev = prev; | |
256 | + rcu_assign_pointer(prev->next, new); | |
257 | + next->prev = new; | |
258 | +} | |
259 | + | |
260 | +/** | |
261 | + * list_add_tail_rcu - Add a new entry to rcu-protected list. | |
262 | + * | |
263 | + * @new: Pointer to "struct list_head". | |
264 | + * @head: Pointer to "struct list_head". | |
265 | + * | |
266 | + * Returns nothing. | |
267 | + * | |
268 | + * This is for compatibility with older kernels. | |
269 | + */ | |
270 | +static inline void list_add_tail_rcu(struct list_head *new, | |
271 | + struct list_head *head) | |
272 | +{ | |
273 | + __list_add_rcu(new, head->prev, head); | |
274 | +} | |
275 | + | |
276 | +/** | |
277 | + * list_add_rcu - Add a new entry to rcu-protected list. | |
278 | + * | |
279 | + * @new: Pointer to "struct list_head". | |
280 | + * @head: Pointer to "struct list_head". | |
281 | + * | |
282 | + * Returns nothing. | |
283 | + * | |
284 | + * This is for compatibility with older kernels. | |
285 | + */ | |
286 | +static inline void list_add_rcu(struct list_head *new, struct list_head *head) | |
287 | +{ | |
288 | + __list_add_rcu(new, head, head->next); | |
289 | +} | |
290 | + | |
291 | +#endif | |
292 | + | |
293 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38) | |
294 | + | |
295 | +/** | |
296 | + * __list_del_entry - Deletes entry from list without re-initialization. | |
297 | + * | |
298 | + * @entry: Pointer to "struct list_head". | |
299 | + * | |
300 | + * Returns nothing. | |
301 | + * | |
302 | + * This is for compatibility with older kernels. | |
303 | + */ | |
304 | +static inline void __list_del_entry(struct list_head *entry) | |
305 | +{ | |
306 | + __list_del(entry->prev, entry->next); | |
307 | +} | |
308 | + | |
309 | +#endif | |
310 | + | |
311 | +#ifndef list_for_each_entry_safe | |
312 | + | |
313 | +/** | |
314 | + * list_for_each_entry_safe - Iterate over list of given type safe against removal of list entry. | |
315 | + * | |
316 | + * @pos: The "type *" to use as a loop cursor. | |
317 | + * @n: Another "type *" to use as temporary storage. | |
318 | + * @head: Pointer to "struct list_head". | |
319 | + * @member: The name of the list_struct within the struct. | |
320 | + * | |
321 | + * This is for compatibility with older kernels. | |
322 | + */ | |
323 | +#define list_for_each_entry_safe(pos, n, head, member) \ | |
324 | + for (pos = list_entry((head)->next, typeof(*pos), member), \ | |
325 | + n = list_entry(pos->member.next, typeof(*pos), member); \ | |
326 | + &pos->member != (head); \ | |
327 | + pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
328 | + | |
329 | +#endif | |
330 | + | |
331 | +#ifndef srcu_dereference | |
332 | + | |
333 | +/** | |
334 | + * srcu_dereference - Fetch SRCU-protected pointer with checking. | |
335 | + * | |
336 | + * @p: The pointer to read, prior to dereferencing. | |
337 | + * @ss: Pointer to "struct srcu_struct". | |
338 | + * | |
339 | + * Returns @p. | |
340 | + * | |
341 | + * This is for compatibility with older kernels. | |
342 | + */ | |
343 | +#define srcu_dereference(p, ss) rcu_dereference(p) | |
344 | + | |
345 | +#endif | |
346 | + | |
347 | +#ifndef list_for_each_entry_srcu | |
348 | + | |
349 | +/** | |
350 | + * list_for_each_entry_srcu - Iterate over rcu list of given type. | |
351 | + * | |
352 | + * @pos: The type * to use as a loop cursor. | |
353 | + * @head: The head for your list. | |
354 | + * @member: The name of the list_struct within the struct. | |
355 | + * @ss: Pointer to "struct srcu_struct". | |
356 | + * | |
357 | + * As of 2.6.36, this macro is not provided because only TOMOYO wants it. | |
358 | + */ | |
359 | +#define list_for_each_entry_srcu(pos, head, member, ss) \ | |
360 | + for (pos = list_entry(srcu_dereference((head)->next, ss), \ | |
361 | + typeof(*pos), member); \ | |
362 | + prefetch(pos->member.next), &pos->member != (head); \ | |
363 | + pos = list_entry(srcu_dereference(pos->member.next, ss), \ | |
364 | + typeof(*pos), member)) | |
365 | + | |
366 | +#endif | |
367 | + | |
368 | +#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)) | |
369 | + | |
370 | +#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 4, 21) | |
371 | +#undef ssleep | |
372 | +#endif | |
373 | + | |
374 | +#ifndef ssleep | |
375 | + | |
376 | +/** | |
377 | + * ssleep - Sleep for specified seconds. | |
378 | + * | |
379 | + * @secs: Seconds to sleep. | |
380 | + * | |
381 | + * Returns nothing. | |
382 | + * | |
383 | + * This is for compatibility with older kernels. | |
384 | + * | |
385 | + * Since several distributions backported ssleep(), I define it as a macro | |
386 | + * rather than an inlined function in order to avoid multiple definition error. | |
387 | + */ | |
388 | +#define ssleep(secs) { \ | |
389 | + set_current_state(TASK_UNINTERRUPTIBLE); \ | |
390 | + schedule_timeout((HZ * secs) + 1); \ | |
391 | + } | |
392 | + | |
393 | +#endif | |
394 | + | |
395 | +#endif | |
396 | + | |
397 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) | |
398 | + | |
399 | +/** | |
400 | + * from_kuid - Convert kuid_t to uid_t. | |
401 | + * | |
402 | + * @ns: Unused. | |
403 | + * @uid: kuid_t value. | |
404 | + * | |
405 | + * Returns uid seen from init's user namespace. | |
406 | + */ | |
407 | +#define from_kuid(ns, uid) (uid) | |
408 | + | |
409 | +/** | |
410 | + * from_kgid - Convert kgid_t to gid_t. | |
411 | + * | |
412 | + * @ns: Unused. | |
413 | + * @gid: kgid_t value. | |
414 | + * | |
415 | + * Returns gid seen from init's user namespace. | |
416 | + */ | |
417 | +#define from_kgid(ns, gid) (gid) | |
418 | + | |
419 | +/** | |
420 | + * uid_eq - Check whether the uids are equals or not. | |
421 | + * | |
422 | + * @left: Uid seen from current user namespace. | |
423 | + * @right: Uid seen from current user namespace. | |
424 | + * | |
425 | + * Returns true if uid is root in init's user namespace, false otherwise. | |
426 | + */ | |
427 | +#define uid_eq(left, right) ((left) == (right)) | |
428 | +#define GLOBAL_ROOT_UID 0 | |
429 | + | |
430 | +#endif | |
431 | + | |
432 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) | |
433 | +#define d_backing_inode(upper) (upper)->d_inode | |
434 | +#endif | |
435 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) | |
436 | +#define d_is_dir(dentry) ({ struct inode *inode = d_backing_inode(dentry); \ | |
437 | + inode && S_ISDIR(inode->i_mode); }) | |
438 | +#endif | |
439 | + | |
440 | +/* | |
441 | + * TOMOYO specific part start. | |
442 | + */ | |
443 | + | |
444 | +/* Clear TOMOYO 1.8 config. */ | |
445 | +#undef CONFIG_CCSECURITY | |
446 | +#undef CONFIG_CCSECURITY_LKM | |
447 | +#undef CONFIG_CCSECURITY_DISABLE_BY_DEFAULT | |
448 | +#undef CONFIG_CCSECURITY_MAX_AUDIT_LOG | |
449 | +#undef CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY | |
450 | +#undef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER | |
451 | +#undef CONFIG_CCSECURITY_POLICY_LOADER | |
452 | +#undef CONFIG_CCSECURITY_ACTIVATION_TRIGGER | |
453 | +#undef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
454 | +#undef CONFIG_CCSECURITY_FILE_READDIR | |
455 | +#undef CONFIG_CCSECURITY_FILE_GETATTR | |
456 | +#undef CONFIG_CCSECURITY_NETWORK | |
457 | +#undef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
458 | +#undef CONFIG_CCSECURITY_CAPABILITY | |
459 | +#undef CONFIG_CCSECURITY_IPC | |
460 | +#undef CONFIG_CCSECURITY_MISC | |
461 | +#undef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
462 | +#undef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
463 | +#undef CONFIG_CCSECURITY_PORTRESERVE | |
464 | +/* Define AKARI 1.0 config. */ | |
465 | +#include "config.h" | |
466 | +#ifndef CONFIG_CCSECURITY | |
467 | +#define CONFIG_CCSECURITY | |
468 | +#endif | |
469 | +#ifndef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
470 | +#define CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
471 | +#endif | |
472 | +#ifndef CONFIG_CCSECURITY_MAX_AUDIT_LOG | |
473 | +#define CONFIG_CCSECURITY_MAX_AUDIT_LOG 1024 | |
474 | +#endif | |
475 | +#ifndef CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY | |
476 | +#define CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY 2048 | |
477 | +#endif | |
478 | +#ifndef CONFIG_CCSECURITY_POLICY_LOADER | |
479 | +#define CONFIG_CCSECURITY_POLICY_LOADER "/sbin/ccs-init" | |
480 | +#endif | |
481 | +#ifndef CONFIG_CCSECURITY_ACTIVATION_TRIGGER | |
482 | +#define CONFIG_CCSECURITY_ACTIVATION_TRIGGER "/sbin/init" | |
483 | +#endif | |
484 | +#include "ccsecurity.h" | |
485 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
486 | +#error This module supports only 2.6.0 and later kernels. | |
487 | +#endif | |
488 | +#ifndef CONFIG_SECURITY | |
489 | +#error You must choose CONFIG_SECURITY=y for building this module. | |
490 | +#endif | |
491 | +#ifndef CONFIG_KALLSYMS | |
492 | +#error You must choose CONFIG_KALLSYMS=y for building this module. | |
493 | +#endif | |
494 | +#ifndef CONFIG_PROC_FS | |
495 | +#error You must choose CONFIG_PROC_FS=y for building this module. | |
496 | +#endif | |
497 | +#ifndef CONFIG_MODULES | |
498 | +#error You must choose CONFIG_MODULES=y for building this module. | |
499 | +#endif | |
500 | + | |
501 | +/* Enumeration definition for internal use. */ | |
502 | + | |
503 | +/* Index numbers for Access Controls. */ | |
504 | +enum ccs_acl_entry_type_index { | |
505 | + CCS_TYPE_PATH_ACL, | |
506 | + CCS_TYPE_PATH2_ACL, | |
507 | + CCS_TYPE_PATH_NUMBER_ACL, | |
508 | + CCS_TYPE_MKDEV_ACL, | |
509 | + CCS_TYPE_MOUNT_ACL, | |
510 | +#ifdef CONFIG_CCSECURITY_MISC | |
511 | + CCS_TYPE_ENV_ACL, | |
512 | +#endif | |
513 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
514 | + CCS_TYPE_CAPABILITY_ACL, | |
515 | +#endif | |
516 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
517 | + CCS_TYPE_INET_ACL, | |
518 | + CCS_TYPE_UNIX_ACL, | |
519 | +#endif | |
520 | +#ifdef CONFIG_CCSECURITY_IPC | |
521 | + CCS_TYPE_SIGNAL_ACL, | |
522 | +#endif | |
523 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
524 | + CCS_TYPE_AUTO_EXECUTE_HANDLER, | |
525 | + CCS_TYPE_DENIED_EXECUTE_HANDLER, | |
526 | +#endif | |
527 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
528 | + CCS_TYPE_AUTO_TASK_ACL, | |
529 | + CCS_TYPE_MANUAL_TASK_ACL, | |
530 | +#endif | |
531 | +}; | |
532 | + | |
533 | +/* Index numbers for "struct ccs_condition". */ | |
534 | +enum ccs_conditions_index { | |
535 | + CCS_TASK_UID, /* current_uid() */ | |
536 | + CCS_TASK_EUID, /* current_euid() */ | |
537 | + CCS_TASK_SUID, /* current_suid() */ | |
538 | + CCS_TASK_FSUID, /* current_fsuid() */ | |
539 | + CCS_TASK_GID, /* current_gid() */ | |
540 | + CCS_TASK_EGID, /* current_egid() */ | |
541 | + CCS_TASK_SGID, /* current_sgid() */ | |
542 | + CCS_TASK_FSGID, /* current_fsgid() */ | |
543 | + CCS_TASK_PID, /* sys_getpid() */ | |
544 | + CCS_TASK_PPID, /* sys_getppid() */ | |
545 | + CCS_EXEC_ARGC, /* "struct linux_binprm *"->argc */ | |
546 | + CCS_EXEC_ENVC, /* "struct linux_binprm *"->envc */ | |
547 | + CCS_TYPE_IS_SOCKET, /* S_IFSOCK */ | |
548 | + CCS_TYPE_IS_SYMLINK, /* S_IFLNK */ | |
549 | + CCS_TYPE_IS_FILE, /* S_IFREG */ | |
550 | + CCS_TYPE_IS_BLOCK_DEV, /* S_IFBLK */ | |
551 | + CCS_TYPE_IS_DIRECTORY, /* S_IFDIR */ | |
552 | + CCS_TYPE_IS_CHAR_DEV, /* S_IFCHR */ | |
553 | + CCS_TYPE_IS_FIFO, /* S_IFIFO */ | |
554 | + CCS_MODE_SETUID, /* S_ISUID */ | |
555 | + CCS_MODE_SETGID, /* S_ISGID */ | |
556 | + CCS_MODE_STICKY, /* S_ISVTX */ | |
557 | + CCS_MODE_OWNER_READ, /* S_IRUSR */ | |
558 | + CCS_MODE_OWNER_WRITE, /* S_IWUSR */ | |
559 | + CCS_MODE_OWNER_EXECUTE, /* S_IXUSR */ | |
560 | + CCS_MODE_GROUP_READ, /* S_IRGRP */ | |
561 | + CCS_MODE_GROUP_WRITE, /* S_IWGRP */ | |
562 | + CCS_MODE_GROUP_EXECUTE, /* S_IXGRP */ | |
563 | + CCS_MODE_OTHERS_READ, /* S_IROTH */ | |
564 | + CCS_MODE_OTHERS_WRITE, /* S_IWOTH */ | |
565 | + CCS_MODE_OTHERS_EXECUTE, /* S_IXOTH */ | |
566 | + CCS_TASK_TYPE, /* ((u8) task->ccs_flags) & | |
567 | + CCS_TASK_IS_EXECUTE_HANDLER */ | |
568 | + CCS_TASK_EXECUTE_HANDLER, /* CCS_TASK_IS_EXECUTE_HANDLER */ | |
569 | + CCS_EXEC_REALPATH, | |
570 | + CCS_SYMLINK_TARGET, | |
571 | + CCS_PATH1_UID, | |
572 | + CCS_PATH1_GID, | |
573 | + CCS_PATH1_INO, | |
574 | + CCS_PATH1_MAJOR, | |
575 | + CCS_PATH1_MINOR, | |
576 | + CCS_PATH1_PERM, | |
577 | + CCS_PATH1_TYPE, | |
578 | + CCS_PATH1_DEV_MAJOR, | |
579 | + CCS_PATH1_DEV_MINOR, | |
580 | + CCS_PATH2_UID, | |
581 | + CCS_PATH2_GID, | |
582 | + CCS_PATH2_INO, | |
583 | + CCS_PATH2_MAJOR, | |
584 | + CCS_PATH2_MINOR, | |
585 | + CCS_PATH2_PERM, | |
586 | + CCS_PATH2_TYPE, | |
587 | + CCS_PATH2_DEV_MAJOR, | |
588 | + CCS_PATH2_DEV_MINOR, | |
589 | + CCS_PATH1_PARENT_UID, | |
590 | + CCS_PATH1_PARENT_GID, | |
591 | + CCS_PATH1_PARENT_INO, | |
592 | + CCS_PATH1_PARENT_PERM, | |
593 | + CCS_PATH2_PARENT_UID, | |
594 | + CCS_PATH2_PARENT_GID, | |
595 | + CCS_PATH2_PARENT_INO, | |
596 | + CCS_PATH2_PARENT_PERM, | |
597 | + CCS_MAX_CONDITION_KEYWORD, | |
598 | + CCS_NUMBER_UNION, | |
599 | + CCS_NAME_UNION, | |
600 | + CCS_ARGV_ENTRY, | |
601 | + CCS_ENVP_ENTRY, | |
602 | +}; | |
603 | + | |
604 | +/* Index numbers for domain's attributes. */ | |
605 | +enum ccs_domain_info_flags_index { | |
606 | + /* Quota warnning flag. */ | |
607 | + CCS_DIF_QUOTA_WARNED, | |
608 | + /* | |
609 | + * This domain was unable to create a new domain at | |
610 | + * ccs_find_next_domain() because the name of the domain to be created | |
611 | + * was too long or it could not allocate memory. | |
612 | + * More than one process continued execve() without domain transition. | |
613 | + */ | |
614 | + CCS_DIF_TRANSITION_FAILED, | |
615 | + CCS_MAX_DOMAIN_INFO_FLAGS | |
616 | +}; | |
617 | + | |
618 | +/* Index numbers for audit type. */ | |
619 | +enum ccs_grant_log { | |
620 | + /* Follow profile's configuration. */ | |
621 | + CCS_GRANTLOG_AUTO, | |
622 | + /* Do not generate grant log. */ | |
623 | + CCS_GRANTLOG_NO, | |
624 | + /* Generate grant_log. */ | |
625 | + CCS_GRANTLOG_YES, | |
626 | +}; | |
627 | + | |
628 | +/* Index numbers for group entries. */ | |
629 | +enum ccs_group_id { | |
630 | + CCS_PATH_GROUP, | |
631 | + CCS_NUMBER_GROUP, | |
632 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
633 | + CCS_ADDRESS_GROUP, | |
634 | +#endif | |
635 | + CCS_MAX_GROUP | |
636 | +}; | |
637 | + | |
638 | +/* Index numbers for category of functionality. */ | |
639 | +enum ccs_mac_category_index { | |
640 | + CCS_MAC_CATEGORY_FILE, | |
641 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
642 | + CCS_MAC_CATEGORY_NETWORK, | |
643 | +#endif | |
644 | +#ifdef CONFIG_CCSECURITY_MISC | |
645 | + CCS_MAC_CATEGORY_MISC, | |
646 | +#endif | |
647 | +#ifdef CONFIG_CCSECURITY_IPC | |
648 | + CCS_MAC_CATEGORY_IPC, | |
649 | +#endif | |
650 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
651 | + CCS_MAC_CATEGORY_CAPABILITY, | |
652 | +#endif | |
653 | + CCS_MAX_MAC_CATEGORY_INDEX | |
654 | +}; | |
655 | + | |
656 | +/* Index numbers for functionality. */ | |
657 | +enum ccs_mac_index { | |
658 | + CCS_MAC_FILE_EXECUTE, | |
659 | + CCS_MAC_FILE_OPEN, | |
660 | + CCS_MAC_FILE_CREATE, | |
661 | + CCS_MAC_FILE_UNLINK, | |
662 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
663 | + CCS_MAC_FILE_GETATTR, | |
664 | +#endif | |
665 | + CCS_MAC_FILE_MKDIR, | |
666 | + CCS_MAC_FILE_RMDIR, | |
667 | + CCS_MAC_FILE_MKFIFO, | |
668 | + CCS_MAC_FILE_MKSOCK, | |
669 | + CCS_MAC_FILE_TRUNCATE, | |
670 | + CCS_MAC_FILE_SYMLINK, | |
671 | + CCS_MAC_FILE_MKBLOCK, | |
672 | + CCS_MAC_FILE_MKCHAR, | |
673 | + CCS_MAC_FILE_LINK, | |
674 | + CCS_MAC_FILE_RENAME, | |
675 | + CCS_MAC_FILE_CHMOD, | |
676 | + CCS_MAC_FILE_CHOWN, | |
677 | + CCS_MAC_FILE_CHGRP, | |
678 | + CCS_MAC_FILE_IOCTL, | |
679 | + CCS_MAC_FILE_CHROOT, | |
680 | + CCS_MAC_FILE_MOUNT, | |
681 | + CCS_MAC_FILE_UMOUNT, | |
682 | + CCS_MAC_FILE_PIVOT_ROOT, | |
683 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
684 | + CCS_MAC_NETWORK_INET_STREAM_BIND, | |
685 | + CCS_MAC_NETWORK_INET_STREAM_LISTEN, | |
686 | + CCS_MAC_NETWORK_INET_STREAM_CONNECT, | |
687 | + CCS_MAC_NETWORK_INET_STREAM_ACCEPT, | |
688 | + CCS_MAC_NETWORK_INET_DGRAM_BIND, | |
689 | + CCS_MAC_NETWORK_INET_DGRAM_SEND, | |
690 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
691 | + CCS_MAC_NETWORK_INET_DGRAM_RECV, | |
692 | +#endif | |
693 | + CCS_MAC_NETWORK_INET_RAW_BIND, | |
694 | + CCS_MAC_NETWORK_INET_RAW_SEND, | |
695 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
696 | + CCS_MAC_NETWORK_INET_RAW_RECV, | |
697 | +#endif | |
698 | + CCS_MAC_NETWORK_UNIX_STREAM_BIND, | |
699 | + CCS_MAC_NETWORK_UNIX_STREAM_LISTEN, | |
700 | + CCS_MAC_NETWORK_UNIX_STREAM_CONNECT, | |
701 | + CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT, | |
702 | + CCS_MAC_NETWORK_UNIX_DGRAM_BIND, | |
703 | + CCS_MAC_NETWORK_UNIX_DGRAM_SEND, | |
704 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
705 | + CCS_MAC_NETWORK_UNIX_DGRAM_RECV, | |
706 | +#endif | |
707 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND, | |
708 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN, | |
709 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT, | |
710 | + CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT, | |
711 | +#endif | |
712 | +#ifdef CONFIG_CCSECURITY_MISC | |
713 | + CCS_MAC_ENVIRON, | |
714 | +#endif | |
715 | +#ifdef CONFIG_CCSECURITY_IPC | |
716 | + CCS_MAC_SIGNAL, | |
717 | +#endif | |
718 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
719 | + CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET, | |
720 | + CCS_MAC_CAPABILITY_USE_PACKET_SOCKET, | |
721 | + CCS_MAC_CAPABILITY_SYS_REBOOT, | |
722 | + CCS_MAC_CAPABILITY_SYS_VHANGUP, | |
723 | + CCS_MAC_CAPABILITY_SYS_SETTIME, | |
724 | + CCS_MAC_CAPABILITY_SYS_NICE, | |
725 | + CCS_MAC_CAPABILITY_SYS_SETHOSTNAME, | |
726 | + CCS_MAC_CAPABILITY_USE_KERNEL_MODULE, | |
727 | + CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD, | |
728 | + CCS_MAC_CAPABILITY_SYS_PTRACE, | |
729 | +#endif | |
730 | + CCS_MAX_MAC_INDEX | |
731 | +}; | |
732 | + | |
733 | +/* Index numbers for /proc/ccs/stat interface. */ | |
734 | +enum ccs_memory_stat_type { | |
735 | + CCS_MEMORY_POLICY, | |
736 | + CCS_MEMORY_AUDIT, | |
737 | + CCS_MEMORY_QUERY, | |
738 | + CCS_MAX_MEMORY_STAT | |
739 | +}; | |
740 | + | |
741 | +/* Index numbers for access controls with one pathname and three numbers. */ | |
742 | +enum ccs_mkdev_acl_index { | |
743 | + CCS_TYPE_MKBLOCK, | |
744 | + CCS_TYPE_MKCHAR, | |
745 | + CCS_MAX_MKDEV_OPERATION | |
746 | +}; | |
747 | + | |
748 | +/* Index numbers for operation mode. */ | |
749 | +enum ccs_mode_value { | |
750 | + CCS_CONFIG_DISABLED, | |
751 | + CCS_CONFIG_LEARNING, | |
752 | + CCS_CONFIG_PERMISSIVE, | |
753 | + CCS_CONFIG_ENFORCING, | |
754 | + CCS_CONFIG_MAX_MODE, | |
755 | + CCS_CONFIG_WANT_REJECT_LOG = 64, | |
756 | + CCS_CONFIG_WANT_GRANT_LOG = 128, | |
757 | + CCS_CONFIG_USE_DEFAULT = 255, | |
758 | +}; | |
759 | + | |
760 | +/* Index numbers for socket operations. */ | |
761 | +enum ccs_network_acl_index { | |
762 | + CCS_NETWORK_BIND, /* bind() operation. */ | |
763 | + CCS_NETWORK_LISTEN, /* listen() operation. */ | |
764 | + CCS_NETWORK_CONNECT, /* connect() operation. */ | |
765 | + CCS_NETWORK_ACCEPT, /* accept() operation. */ | |
766 | + CCS_NETWORK_SEND, /* send() operation. */ | |
767 | +#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG | |
768 | + CCS_NETWORK_RECV, /* recv() operation. */ | |
769 | +#endif | |
770 | + CCS_MAX_NETWORK_OPERATION | |
771 | +}; | |
772 | + | |
773 | +/* Index numbers for access controls with two pathnames. */ | |
774 | +enum ccs_path2_acl_index { | |
775 | + CCS_TYPE_LINK, | |
776 | + CCS_TYPE_RENAME, | |
777 | + CCS_TYPE_PIVOT_ROOT, | |
778 | + CCS_MAX_PATH2_OPERATION | |
779 | +}; | |
780 | + | |
781 | +/* Index numbers for access controls with one pathname. */ | |
782 | +enum ccs_path_acl_index { | |
783 | + CCS_TYPE_EXECUTE, | |
784 | + CCS_TYPE_READ, | |
785 | + CCS_TYPE_WRITE, | |
786 | + CCS_TYPE_APPEND, | |
787 | + CCS_TYPE_UNLINK, | |
788 | +#ifdef CONFIG_CCSECURITY_FILE_GETATTR | |
789 | + CCS_TYPE_GETATTR, | |
790 | +#endif | |
791 | + CCS_TYPE_RMDIR, | |
792 | + CCS_TYPE_TRUNCATE, | |
793 | + CCS_TYPE_SYMLINK, | |
794 | + CCS_TYPE_CHROOT, | |
795 | + CCS_TYPE_UMOUNT, | |
796 | + CCS_MAX_PATH_OPERATION | |
797 | +}; | |
798 | + | |
799 | +/* Index numbers for access controls with one pathname and one number. */ | |
800 | +enum ccs_path_number_acl_index { | |
801 | + CCS_TYPE_CREATE, | |
802 | + CCS_TYPE_MKDIR, | |
803 | + CCS_TYPE_MKFIFO, | |
804 | + CCS_TYPE_MKSOCK, | |
805 | + CCS_TYPE_IOCTL, | |
806 | + CCS_TYPE_CHMOD, | |
807 | + CCS_TYPE_CHOWN, | |
808 | + CCS_TYPE_CHGRP, | |
809 | + CCS_MAX_PATH_NUMBER_OPERATION | |
810 | +}; | |
811 | + | |
812 | +/* Index numbers for stat(). */ | |
813 | +enum ccs_path_stat_index { | |
814 | + /* Do not change this order. */ | |
815 | + CCS_PATH1, | |
816 | + CCS_PATH1_PARENT, | |
817 | + CCS_PATH2, | |
818 | + CCS_PATH2_PARENT, | |
819 | + CCS_MAX_PATH_STAT | |
820 | +}; | |
821 | + | |
822 | +/* Index numbers for entry type. */ | |
823 | +enum ccs_policy_id { | |
824 | +#ifdef CONFIG_CCSECURITY_PORTRESERVE | |
825 | + CCS_ID_RESERVEDPORT, | |
826 | +#endif | |
827 | + CCS_ID_GROUP, | |
828 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
829 | + CCS_ID_ADDRESS_GROUP, | |
830 | +#endif | |
831 | + CCS_ID_PATH_GROUP, | |
832 | + CCS_ID_NUMBER_GROUP, | |
833 | + CCS_ID_AGGREGATOR, | |
834 | + CCS_ID_TRANSITION_CONTROL, | |
835 | + CCS_ID_MANAGER, | |
836 | + CCS_ID_CONDITION, | |
837 | + CCS_ID_NAME, | |
838 | + CCS_ID_ACL, | |
839 | + CCS_ID_DOMAIN, | |
840 | + CCS_MAX_POLICY | |
841 | +}; | |
842 | + | |
843 | +/* Index numbers for /proc/ccs/stat interface. */ | |
844 | +enum ccs_policy_stat_type { | |
845 | + /* Do not change this order. */ | |
846 | + CCS_STAT_POLICY_UPDATES, | |
847 | + CCS_STAT_POLICY_LEARNING, /* == CCS_CONFIG_LEARNING */ | |
848 | + CCS_STAT_POLICY_PERMISSIVE, /* == CCS_CONFIG_PERMISSIVE */ | |
849 | + CCS_STAT_POLICY_ENFORCING, /* == CCS_CONFIG_ENFORCING */ | |
850 | + CCS_MAX_POLICY_STAT | |
851 | +}; | |
852 | + | |
853 | +/* Index numbers for profile's PREFERENCE values. */ | |
854 | +enum ccs_pref_index { | |
855 | + CCS_PREF_MAX_AUDIT_LOG, | |
856 | + CCS_PREF_MAX_LEARNING_ENTRY, | |
857 | + CCS_PREF_ENFORCING_PENALTY, | |
858 | + CCS_MAX_PREF | |
859 | +}; | |
860 | + | |
861 | +/* Index numbers for /proc/ccs/ interfaces. */ | |
862 | +enum ccs_proc_interface_index { | |
863 | + CCS_DOMAIN_POLICY, | |
864 | + CCS_EXCEPTION_POLICY, | |
865 | + CCS_PROCESS_STATUS, | |
866 | + CCS_STAT, | |
867 | + CCS_AUDIT, | |
868 | + CCS_VERSION, | |
869 | + CCS_PROFILE, | |
870 | + CCS_QUERY, | |
871 | + CCS_MANAGER, | |
872 | +#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER | |
873 | + CCS_EXECUTE_HANDLER, | |
874 | +#endif | |
875 | +}; | |
876 | + | |
877 | +/* Index numbers for special mount operations. */ | |
878 | +enum ccs_special_mount { | |
879 | + CCS_MOUNT_BIND, /* mount --bind /source /dest */ | |
880 | + CCS_MOUNT_MOVE, /* mount --move /old /new */ | |
881 | + CCS_MOUNT_REMOUNT, /* mount -o remount /dir */ | |
882 | + CCS_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */ | |
883 | + CCS_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */ | |
884 | + CCS_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */ | |
885 | + CCS_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */ | |
886 | + CCS_MAX_SPECIAL_MOUNT | |
887 | +}; | |
888 | + | |
889 | +/* Index numbers for domain transition control keywords. */ | |
890 | +enum ccs_transition_type { | |
891 | + /* Do not change this order, */ | |
892 | + CCS_TRANSITION_CONTROL_NO_RESET, | |
893 | + CCS_TRANSITION_CONTROL_RESET, | |
894 | + CCS_TRANSITION_CONTROL_NO_INITIALIZE, | |
895 | + CCS_TRANSITION_CONTROL_INITIALIZE, | |
896 | + CCS_TRANSITION_CONTROL_NO_KEEP, | |
897 | + CCS_TRANSITION_CONTROL_KEEP, | |
898 | + CCS_MAX_TRANSITION_TYPE | |
899 | +}; | |
900 | + | |
901 | +/* Index numbers for type of numeric values. */ | |
902 | +enum ccs_value_type { | |
903 | + CCS_VALUE_TYPE_INVALID, | |
904 | + CCS_VALUE_TYPE_DECIMAL, | |
905 | + CCS_VALUE_TYPE_OCTAL, | |
906 | + CCS_VALUE_TYPE_HEXADECIMAL, | |
907 | +}; | |
908 | + | |
909 | +/* Constants definition for internal use. */ | |
910 | + | |
911 | +/* | |
912 | + * TOMOYO uses this hash only when appending a string into the string table. | |
913 | + * Frequency of appending strings is very low. So we don't need large (e.g. | |
914 | + * 64k) hash size. 256 will be sufficient. | |
915 | + */ | |
916 | +#define CCS_HASH_BITS 8 | |
917 | +#define CCS_MAX_HASH (1u << CCS_HASH_BITS) | |
918 | + | |
919 | +/* | |
920 | + * TOMOYO checks only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET. | |
921 | + * Therefore, we don't need SOCK_MAX. | |
922 | + */ | |
923 | +#define CCS_SOCK_MAX 6 | |
924 | + | |
925 | +/* Size of temporary buffer for execve() operation. */ | |
926 | +#define CCS_EXEC_TMPSIZE 4096 | |
927 | + | |
928 | +/* Garbage collector is trying to kfree() this element. */ | |
929 | +#define CCS_GC_IN_PROGRESS -1 | |
930 | + | |
931 | +/* Profile number is an integer between 0 and 255. */ | |
932 | +#define CCS_MAX_PROFILES 256 | |
933 | + | |
934 | +/* Group number is an integer between 0 and 255. */ | |
935 | +#define CCS_MAX_ACL_GROUPS 256 | |
936 | + | |
937 | +/* Current thread is doing open(O_RDONLY | O_TRUNC) ? */ | |
938 | +#define CCS_OPEN_FOR_READ_TRUNCATE 1 | |
939 | +/* Current thread is doing open(3) ? */ | |
940 | +#define CCS_OPEN_FOR_IOCTL_ONLY 2 | |
941 | +/* Current thread is doing do_execve() ? */ | |
942 | +#define CCS_TASK_IS_IN_EXECVE 4 | |
943 | +/* Current thread is running as an execute handler program? */ | |
944 | +#define CCS_TASK_IS_EXECUTE_HANDLER 8 | |
945 | +/* Current thread is allowed to modify policy via /proc/ccs/ interface? */ | |
946 | +#define CCS_TASK_IS_MANAGER 16 | |
947 | + | |
948 | +/* | |
949 | + * Retry this request. Returned by ccs_supervisor() if policy violation has | |
950 | + * occurred in enforcing mode and the userspace daemon decided to retry. | |
951 | + * | |
952 | + * We must choose a positive value in order to distinguish "granted" (which is | |
953 | + * 0) and "rejected" (which is a negative value) and "retry". | |
954 | + */ | |
955 | +#define CCS_RETRY_REQUEST 1 | |
956 | + | |
957 | +/* The gfp flags used by TOMOYO. */ | |
958 | +#define CCS_GFP_FLAGS GFP_NOFS | |
959 | + | |
960 | +/* Size of read buffer for /proc/ccs/ interface. */ | |
961 | +#define CCS_MAX_IO_READ_QUEUE 64 | |
962 | + | |
963 | +/* Structure definition for internal use. */ | |
964 | + | |
965 | +/* Common header for holding ACL entries. */ | |
966 | +struct ccs_acl_head { | |
967 | + struct list_head list; | |
968 | + s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */ | |
969 | +} __packed; | |
970 | + | |
971 | +/* Common header for shared entries. */ | |
972 | +struct ccs_shared_acl_head { | |
973 | + struct list_head list; | |
974 | + atomic_t users; | |
975 | +} __packed; | |
976 | + | |
977 | +/* Common header for individual entries. */ | |
978 | +struct ccs_acl_info { | |
979 | + struct list_head list; | |
980 | + struct ccs_condition *cond; /* Maybe NULL. */ | |
981 | + s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */ | |
982 | + u8 type; /* One of values in "enum ccs_acl_entry_type_index". */ | |
983 | + u16 perm; | |
984 | +} __packed; | |
985 | + | |
986 | +/* Structure for holding a word. */ | |
987 | +struct ccs_name_union { | |
988 | + /* Either @filename or @group is NULL. */ | |
989 | + const struct ccs_path_info *filename; | |
990 | + struct ccs_group *group; | |
991 | +}; | |
992 | + | |
993 | +/* Structure for holding a number. */ | |
994 | +struct ccs_number_union { | |
995 | + unsigned long values[2]; | |
996 | + struct ccs_group *group; /* Maybe NULL. */ | |
997 | + /* One of values in "enum ccs_value_type". */ | |
998 | + u8 value_type[2]; | |
999 | +}; | |
1000 | + | |
1001 | +/* Structure for holding an IP address. */ | |
1002 | +struct ccs_ipaddr_union { | |
1003 | + struct in6_addr ip[2]; /* Big endian. */ | |
1004 | + struct ccs_group *group; /* Pointer to address group. */ | |
1005 | + bool is_ipv6; /* Valid only if @group == NULL. */ | |
1006 | +}; | |
1007 | + | |
1008 | +/* Structure for "path_group"/"number_group"/"address_group" directive. */ | |
1009 | +struct ccs_group { | |
1010 | + struct ccs_shared_acl_head head; | |
1011 | + /* Name of group (without leading '@'). */ | |
1012 | + const struct ccs_path_info *group_name; | |
1013 | + /* | |
1014 | + * List of "struct ccs_path_group" or "struct ccs_number_group" or | |
1015 | + * "struct ccs_address_group". | |
1016 | + */ | |
1017 | + struct list_head member_list; | |
1018 | +}; | |
1019 | + | |
1020 | +/* Structure for "path_group" directive. */ | |
1021 | +struct ccs_path_group { | |
1022 | + struct ccs_acl_head head; | |
1023 | + const struct ccs_path_info *member_name; | |
1024 | +}; | |
1025 | + | |
1026 | +/* Structure for "number_group" directive. */ | |
1027 | +struct ccs_number_group { | |
1028 | + struct ccs_acl_head head; | |
1029 | + struct ccs_number_union number; | |
1030 | +}; | |
1031 | + | |
1032 | +/* Structure for "address_group" directive. */ | |
1033 | +struct ccs_address_group { | |
1034 | + struct ccs_acl_head head; | |
1035 | + /* Structure for holding an IP address. */ | |
1036 | + struct ccs_ipaddr_union address; | |
1037 | +}; | |
1038 | + | |
1039 | +/* Subset of "struct stat". Used by conditional ACL and audit logs. */ | |
1040 | +struct ccs_mini_stat { | |
1041 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) | |
1042 | + kuid_t uid; | |
1043 | + kgid_t gid; | |
1044 | +#else | |
1045 | + uid_t uid; | |
1046 | + gid_t gid; | |
1047 | +#endif | |
1048 | + ino_t ino; | |
1049 | + umode_t mode; | |
1050 | + dev_t dev; | |
1051 | + dev_t rdev; | |
1052 | +}; | |
1053 | + | |
1054 | +/* Structure for dumping argv[] and envp[] of "struct linux_binprm". */ | |
1055 | +struct ccs_page_dump { | |
1056 | + struct page *page; /* Previously dumped page. */ | |
1057 | + char *data; /* Contents of "page". Size is PAGE_SIZE. */ | |
1058 | +}; | |
1059 | + | |
1060 | +/* Structure for attribute checks in addition to pathname checks. */ | |
1061 | +struct ccs_obj_info { | |
1062 | + /* True if ccs_get_attributes() was already called, false otherwise. */ | |
1063 | + bool validate_done; | |
1064 | + /* True if @stat[] is valid. */ | |
1065 | + bool stat_valid[CCS_MAX_PATH_STAT]; | |
1066 | + /* First pathname. Initialized with { NULL, NULL } if no path. */ | |
1067 | + struct path path1; | |
1068 | + /* Second pathname. Initialized with { NULL, NULL } if no path. */ | |
1069 | + struct path path2; | |
1070 | + /* | |
1071 | + * Information on @path1, @path1's parent directory, @path2, @path2's | |
1072 | + * parent directory. | |
1073 | + */ | |
1074 | + struct ccs_mini_stat stat[CCS_MAX_PATH_STAT]; | |
1075 | + /* | |
1076 | + * Content of symbolic link to be created. NULL for operations other | |
1077 | + * than symlink(). | |
1078 | + */ | |
1079 | + struct ccs_path_info *symlink_target; | |
1080 | +}; | |
1081 | + | |
1082 | +/* Structure for entries which follows "struct ccs_condition". */ | |
1083 | +struct ccs_condition_element { | |
1084 | + /* | |
1085 | + * Left hand operand. A "struct ccs_argv" for CCS_ARGV_ENTRY, a | |
1086 | + * "struct ccs_envp" for CCS_ENVP_ENTRY is attached to the tail | |
1087 | + * of the array of this struct. | |
1088 | + */ | |
1089 | + u8 left; | |
1090 | + /* | |
1091 | + * Right hand operand. A "struct ccs_number_union" for | |
1092 | + * CCS_NUMBER_UNION, a "struct ccs_name_union" for CCS_NAME_UNION is | |
1093 | + * attached to the tail of the array of this struct. | |
1094 | + */ | |
1095 | + u8 right; | |
1096 | + /* Equation operator. True if equals or overlaps, false otherwise. */ | |
1097 | + bool equals; | |
1098 | +}; | |
1099 | + | |
1100 | +/* Structure for optional arguments. */ | |
1101 | +struct ccs_condition { | |
1102 | + struct ccs_shared_acl_head head; | |
1103 | + u32 size; /* Memory size allocated for this entry. */ | |
1104 | + u16 condc; /* Number of conditions in this struct. */ | |
1105 | + u16 numbers_count; /* Number of "struct ccs_number_union values". */ | |
1106 | + u16 names_count; /* Number of "struct ccs_name_union names". */ | |
1107 | + u16 argc; /* Number of "struct ccs_argv". */ | |
1108 | + u16 envc; /* Number of "struct ccs_envp". */ | |
1109 | + u8 grant_log; /* One of values in "enum ccs_grant_log". */ | |
1110 | + bool exec_transit; /* True if transit is for "file execute". */ | |
1111 | + const struct ccs_path_info *transit; /* Maybe NULL. */ | |
1112 | + /* | |
1113 | + * struct ccs_condition_element condition[condc]; | |
1114 | + * struct ccs_number_union values[numbers_count]; | |
1115 | + * struct ccs_name_union names[names_count]; | |
1116 | + * struct ccs_argv argv[argc]; | |
1117 | + * struct ccs_envp envp[envc]; | |
1118 | + */ | |
1119 | +}; | |
1120 | + | |
1121 | +struct ccs_execve; | |
1122 | +struct ccs_policy_namespace; | |
1123 | + | |
1124 | +/* Structure for request info. */ | |
1125 | +struct ccs_request_info { | |
1126 | + /* | |
1127 | + * For holding parameters specific to operations which deal files. | |
1128 | + * NULL if not dealing files. | |
1129 | + */ | |
1130 | + struct ccs_obj_info *obj; | |
1131 | + /* | |
1132 | + * For holding parameters specific to execve() request. | |
1133 | + * NULL if not dealing do_execve(). | |
1134 | + */ | |
1135 | + struct ccs_execve *ee; | |
1136 | + /* | |
1137 | + * For holding parameters. | |
1138 | + * Pointers in this union are not NULL except path->matched_path. | |
1139 | + */ | |
1140 | + union { | |
1141 | + struct { | |
1142 | + const struct ccs_path_info *filename; | |
1143 | + /* | |
1144 | + * For using wildcards at ccs_find_next_domain(). | |
1145 | + * | |
1146 | + * The matched_acl cannot be used because it may refer | |
1147 | + * a "struct ccs_path_acl" with ->is_group == true. | |
1148 | + * We want to use exact "struct ccs_path_info" rather | |
1149 | + * than "struct ccs_path_acl". | |
1150 | + */ | |
1151 | + const struct ccs_path_info *matched_path; | |
1152 | + /* One of values in "enum ccs_path_acl_index". */ | |
1153 | + u8 operation; | |
1154 | + } path; | |
1155 | + struct { | |
1156 | + const struct ccs_path_info *filename1; | |
1157 | + const struct ccs_path_info *filename2; | |
1158 | + /* One of values in "enum ccs_path2_acl_index". */ | |
1159 | + u8 operation; | |
1160 | + } path2; | |
1161 | + struct { | |
1162 | + const struct ccs_path_info *filename; | |
1163 | + unsigned int mode; | |
1164 | + unsigned int major; | |
1165 | + unsigned int minor; | |
1166 | + /* One of values in "enum ccs_mkdev_acl_index". */ | |
1167 | + u8 operation; | |
1168 | + } mkdev; | |
1169 | + struct { | |
1170 | + const struct ccs_path_info *filename; | |
1171 | + unsigned long number; | |
1172 | + /* | |
1173 | + * One of values in "enum ccs_path_number_acl_index". | |
1174 | + */ | |
1175 | + u8 operation; | |
1176 | + } path_number; | |
1177 | +#ifdef CONFIG_CCSECURITY_NETWORK | |
1178 | + struct { | |
1179 | + const u32 *address; /* Big endian. */ | |
1180 | + u16 port; /* Host endian. */ | |
1181 | + /* One of values smaller than CCS_SOCK_MAX. */ | |
1182 | + u8 protocol; | |
1183 | + /* One of values in "enum ccs_network_acl_index". */ | |
1184 | + u8 operation; | |
1185 | + bool is_ipv6; | |
1186 | + } inet_network; | |
1187 | + struct { | |
1188 | + const struct ccs_path_info *address; | |
1189 | + /* One of values smaller than CCS_SOCK_MAX. */ | |
1190 | + u8 protocol; | |
1191 | + /* One of values in "enum ccs_network_acl_index". */ | |
1192 | + u8 operation; | |
1193 | + } unix_network; | |
1194 | +#endif | |
1195 | +#ifdef CONFIG_CCSECURITY_MISC | |
1196 | + struct { | |
1197 | + const struct ccs_path_info *name; | |
1198 | + } environ; | |
1199 | +#endif | |
1200 | +#ifdef CONFIG_CCSECURITY_CAPABILITY | |
1201 | + struct { | |
1202 | + /* One of values in "enum ccs_capability_acl_index". */ | |
1203 | + u8 operation; | |
1204 | + } capability; | |
1205 | +#endif | |
1206 | +#ifdef CONFIG_CCSECURITY_IPC | |
1207 | + struct { | |
1208 | + const char *dest_pattern; | |
1209 | + int sig; | |
1210 | + } signal; | |
1211 | +#endif | |
1212 | + struct { | |
1213 | + const struct ccs_path_info *type; | |
1214 | + const struct ccs_path_info *dir; | |
1215 | + const struct ccs_path_info *dev; | |
1216 | + unsigned long flags; | |
1217 | + int need_dev; | |
1218 | + } mount; | |
1219 | +#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION | |
1220 | + struct { | |
1221 | + const struct ccs_path_info *domainname; | |
1222 | + } task; | |
1223 | +#endif | |
1224 | + } param; | |
1225 | + /* | |
1226 | + * For updating current->ccs_domain_info at ccs_update_task_domain(). | |
1227 | + * Initialized to NULL at ccs_init_request_info(). | |
1228 | + * Matching "struct ccs_acl_info" is copied if access request was | |
1229 | + * granted. Re-initialized to NULL at ccs_update_task_domain(). | |
1230 | + */ | |
1231 | + struct ccs_acl_info *matched_acl; | |
1232 | + u8 param_type; /* One of values in "enum ccs_acl_entry_type_index". */ | |
1233 | + bool granted; /* True if granted, false otherwise. */ | |
1234 | + /* True if current thread should not be carried sleep penalty. */ | |
1235 | + bool dont_sleep_on_enforce_error; | |
1236 | + /* | |
1237 | + * For counting number of retries made for this request. | |
1238 | + * This counter is incremented whenever ccs_supervisor() returned | |
1239 | + * CCS_RETRY_REQUEST. | |
1240 | + */ | |
1241 | + u8 retry; | |
1242 | + /* | |
1243 | + * For holding profile number used for this request. | |
1244 | + * One of values between 0 and CCS_MAX_PROFILES - 1. | |
1245 | + */ | |
1246 | + u8 profile; | |
1247 | + /* | |
1248 | + * For holding operation mode used for this request. | |
1249 | + * One of CCS_CONFIG_DISABLED, CCS_CONFIG_LEARNING, | |
1250 | + * CCS_CONFIG_PERMISSIVE, CCS_CONFIG_ENFORCING. | |
1251 | + */ | |
1252 | + u8 mode; | |
1253 | + /* | |
1254 | + * For holding operation index used for this request. | |
1255 | + * Used by ccs_init_request_info() / ccs_get_mode() / | |
1256 | + * ccs_write_log(). One of values in "enum ccs_mac_index". | |
1257 | + */ | |
1258 | + u8 type; | |
1259 | +}; | |
1260 | + | |
1261 | +/* Structure for holding a token. */ | |
1262 | +struct ccs_path_info { | |
1263 | + const char *name; | |
1264 | + u32 hash; /* = full_name_hash(name, strlen(name)) */ | |
1265 | + u16 total_len; /* = strlen(name) */ | |
1266 | + u16 const_len; /* = ccs_const_part_length(name) */ | |
1267 | + bool is_dir; /* = ccs_strendswith(name, "/") */ | |
1268 | + bool is_patterned; /* = const_len < total_len */ | |
1269 | +}; | |
1270 | + | |
1271 | +/* Structure for execve() operation. */ | |
1272 | +struct ccs_execve { | |
1273 | + struct ccs_request_info r; | |
1274 | + struct ccs_obj_info obj; | |
1275 | + struct linux_binprm *bprm; | |
1276 | + struct ccs_domain_info *previous_domain; | |
1277 | + const struct ccs_path_info *transition; | |
1278 | + /* For execute_handler */ | |
1279 | + const struct ccs_path_info *handler; | |
1280 | + char *handler_path; /* = kstrdup(handler->name, CCS_GFP_FLAGS) */ | |
1281 | + /* For dumping argv[] and envp[]. */ | |
1282 | + struct ccs_page_dump dump; | |
1283 | + /* For temporary use. */ | |
1284 | + char *tmp; /* Size is CCS_EXEC_TMPSIZE bytes */ | |
1285 | +}; | |
1286 | + | |
1287 | +/* Structure for domain information. */ | |
1288 | +struct ccs_domain_info { | |
1289 | + struct list_head list; | |
1290 | + struct list_head acl_info_list; | |
1291 | + /* Name of this domain. Never NULL. */ | |
1292 | + const struct ccs_path_info *domainname; | |
1293 | + /* Namespace for this domain. Never NULL. */ | |
1294 | + struct ccs_policy_namespace *ns; | |
1295 | + /* Group numbers to use. */ | |
1296 | + unsigned long group[CCS_MAX_ACL_GROUPS / BITS_PER_LONG]; | |
1297 | + u8 profile; /* Profile number to use. */ | |
1298 | + bool is_deleted; /* Delete flag. */ | |
1299 | + bool flags[CCS_MAX_DOMAIN_INFO_FLAGS]; | |
1300 | +}; | |
1301 | + | |
1302 | +/* | |
1303 | + * Structure for "reset_domain"/"no_reset_domain"/"initialize_domain"/ | |
1304 | + * "no_initialize_domain"/"keep_domain"/"no_keep_domain" keyword. | |
1305 | + */ | |
1306 | +struct ccs_transition_control { | |
1307 | + struct ccs_acl_head head; | |
1308 | + u8 type; /* One of values in "enum ccs_transition_type" */ | |
1309 | + bool is_last_name; /* True if the domainname is ccs_last_word(). */ | |
1310 | + const struct ccs_path_info *domainname; /* Maybe NULL */ | |
1311 | + const struct ccs_path_info *program; /* Maybe NULL */ | |
1312 | +}; | |
1313 | + | |
1314 | +/* Structure for "aggregator" keyword. */ | |
1315 | +struct ccs_aggregator { | |
1316 | + struct ccs_acl_head head; | |
1317 | + const struct ccs_path_info *original_name; | |
1318 | + const struct ccs_path_info *aggregated_name; | |
1319 | +}; | |
1320 | + | |
1321 | +/* Structure for "deny_autobind" keyword. */ | |
1322 | +struct ccs_reserved { | |
1323 | + struct ccs_acl_head head; | |
1324 | + struct ccs_number_union port; | |
1325 | +}; | |
1326 | + | |
1327 | +/* Structure for policy manager. */ | |
1328 | +struct ccs_manager { | |
1329 | + struct ccs_acl_head head; | |
1330 | + /* A path to program or a domainname. */ | |
1331 | + const struct ccs_path_info *manager; | |
1332 | +}; | |
1333 | + | |
1334 | +/* Structure for argv[]. */ | |
1335 | +struct ccs_argv { | |
1336 | + unsigned long index; | |
1337 | + const struct ccs_path_info *value; | |
1338 | + bool is_not; | |
1339 | +}; | |
1340 | + | |
1341 | +/* Structure for envp[]. */ | |
1342 | +struct ccs_envp { | |
1343 | + const struct ccs_path_info *name; | |
1344 | + const struct ccs_path_info *value; | |
1345 | + bool is_not; | |
1346 | +}; | |
1347 | + | |
1348 | +/* | |
1349 | + * Structure for "task auto_execute_handler" and "task denied_execute_handler" | |
1350 | + * directive. | |
1351 | + * | |
1352 | + * If "task auto_execute_handler" directive exists and the current process is | |
1353 | + * not an execute handler, all execve() requests are replaced by execve() | |
1354 | + * requests of a program specified by "task auto_execute_handler" directive. | |
1355 | + * If the current process is an execute handler, "task auto_execute_handler" | |
1356 | + * and "task denied_execute_handler" directives are ignored. | |
1357 | + * The program specified by "task execute_handler" validates execve() | |
1358 | + * parameters and executes the original execve() requests if appropriate. | |
1359 | + * | |
1360 | + * "task denied_execute_handler" directive is used only when execve() request | |
1361 | + * was rejected in enforcing mode (i.e. CONFIG::file::execute={ mode=enforcing | |
1362 | + * }). The program specified by "task denied_execute_handler" does whatever it | |
1363 | + * wants to do (e.g. silently terminate, change firewall settings, redirect the | |
1364 | + * user to honey pot etc.). | |
1365 | + */ | |
1366 | +struct ccs_handler_acl { | |
1367 | + struct ccs_acl_info head; /* type = CCS_TYPE_*_EXECUTE_HANDLER */ | |
1368 | + const struct ccs_path_info *handler; /* Pointer to single pathname. */ | |
1369 | +}; | |
1370 | + | |
1371 | +/* | |
1372 | + * Structure for "task auto_domain_transition" and | |
1373 | + * "task manual_domain_transition" directive. | |
1374 | + */ | |
1375 | +struct ccs_task_acl { | |
1376 | + struct ccs_acl_info head; /* type = CCS_TYPE_*_TASK_ACL */ | |
1377 | + /* Pointer to domainname. */ | |
1378 | + const struct ccs_path_info *domainname; | |
1379 | +}; | |
1380 | + | |
1381 | +/* | |
1382 | + * Structure for "file execute", "file read", "file write", "file append", | |
1383 | + * "file unlink", "file getattr", "file rmdir", "file truncate", | |
1384 | + * "file symlink", "file chroot" and "file unmount" directive. | |
1385 | + */ | |
1386 | +struct ccs_path_acl { | |
1387 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH_ACL */ | |
1388 | + struct ccs_name_union name; | |
1389 | +}; | |
1390 | + | |
1391 | +/* | |
1392 | + * Structure for "file rename", "file link" and "file pivot_root" directive. | |
1393 | + */ | |
1394 | +struct ccs_path2_acl { | |
1395 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH2_ACL */ | |
1396 | + struct ccs_name_union name1; | |
1397 | + struct ccs_name_union name2; | |
1398 | +}; | |
1399 | + | |
1400 | +/* | |
1401 | + * Structure for "file create", "file mkdir", "file mkfifo", "file mksock", | |
1402 | + * "file ioctl", "file chmod", "file chown" and "file chgrp" directive. | |
1403 | + */ | |
1404 | +struct ccs_path_number_acl { | |
1405 | + struct ccs_acl_info head; /* type = CCS_TYPE_PATH_NUMBER_ACL */ | |
1406 | + struct ccs_name_union name; | |
1407 | + struct ccs_number_union number; | |
1408 | +}; | |
1409 | + | |
1410 | +/* Structure for "file mkblock" and "file mkchar" directive. */ | |
1411 | +struct ccs_mkdev_acl { | |
1412 | + struct ccs_acl_info head; /* type = CCS_TYPE_MKDEV_ACL */ | |
1413 | + struct ccs_name_union name; | |
1414 | + struct ccs_number_union mode; | |
1415 | + struct ccs_number_union major; | |
1416 | + struct ccs_number_union minor; | |
1417 | +}; | |
1418 | + | |
1419 | +/* Structure for "file mount" directive. */ | |
1420 | +struct ccs_mount_acl { | |
1421 | + struct ccs_acl_info head; /* type = CCS_TYPE_MOUNT_ACL */ | |
1422 | + struct ccs_name_union dev_name; | |
1423 | + struct ccs_name_union dir_name; | |
1424 | + struct ccs_name_union fs_type; | |
1425 | + struct ccs_number_union flags; | |
1426 | +}; | |
1427 | + | |
1428 | +/* Structure for "misc env" directive in domain policy. */ | |
1429 | +struct ccs_env_acl { | |
1430 | + struct ccs_acl_info head; /* type = CCS_TYPE_ENV_ACL */ | |
1431 | + const struct ccs_path_info *env; /* environment variable */ | |
1432 | +}; | |
1433 | + | |
1434 | +/* Structure for "capability" directive. */ | |
1435 | +struct ccs_capability_acl { | |
1436 | + struct ccs_acl_info head; /* type = CCS_TYPE_CAPABILITY_ACL */ | |
1437 | + u8 operation; /* One of values in "enum ccs_capability_acl_index". */ | |
1438 | +}; | |
1439 | + | |
1440 | +/* Structure for "ipc signal" directive. */ | |
1441 | +struct ccs_signal_acl { | |
1442 | + struct ccs_acl_info head; /* type = CCS_TYPE_SIGNAL_ACL */ | |
1443 | + struct ccs_number_union sig; | |
1444 | + /* Pointer to destination pattern. */ | |
1445 | + const struct ccs_path_info *domainname; | |
1446 | +}; | |
1447 | + | |
1448 | +/* Structure for "network inet" directive. */ | |
1449 | +struct ccs_inet_acl { | |
1450 | + struct ccs_acl_info head; /* type = CCS_TYPE_INET_ACL */ | |
1451 | + u8 protocol; | |
1452 | + struct ccs_ipaddr_union address; | |
1453 | + struct ccs_number_union port; | |
1454 | +}; | |
1455 | + | |
1456 | +/* Structure for "network unix" directive. */ | |
1457 | +struct ccs_unix_acl { | |
1458 | + struct ccs_acl_info head; /* type = CCS_TYPE_UNIX_ACL */ | |
1459 | + u8 protocol; | |
1460 | + struct ccs_name_union name; | |
1461 | +}; | |
1462 | + | |
1463 | +/* Structure for holding string data. */ | |
1464 | +struct ccs_name { | |
1465 | + struct ccs_shared_acl_head head; | |
1466 | + int size; /* Memory size allocated for this entry. */ | |
1467 | + struct ccs_path_info entry; | |
1468 | +}; | |
1469 | + | |
1470 | +/* Structure for holding a line from /proc/ccs/ interface. */ | |
1471 | +struct ccs_acl_param { | |
1472 | + char *data; /* Unprocessed data. */ | |
1473 | + struct list_head *list; /* List to add or remove. */ | |
1474 | + struct ccs_policy_namespace *ns; /* Namespace to use. */ | |
1475 | + bool is_delete; /* True if it is a delete request. */ | |
1476 | + union ccs_acl_union { | |
1477 | + struct ccs_acl_info acl_info; | |
1478 | + struct ccs_handler_acl handler_acl; | |
1479 | + struct ccs_task_acl task_acl; | |
1480 | + struct ccs_path_acl path_acl; | |
1481 | + struct ccs_path2_acl path2_acl; | |
1482 | + struct ccs_path_number_acl path_number_acl; | |
1483 | + struct ccs_mkdev_acl mkdev_acl; | |
1484 | + struct ccs_mount_acl mount_acl; | |
1485 | + struct ccs_env_acl env_acl; | |
1486 | + struct ccs_capability_acl capability_acl; | |
1487 | + struct ccs_signal_acl signal_acl; | |
1488 | + struct ccs_inet_acl inet_acl; | |
1489 | + struct ccs_unix_acl unix_acl; | |
1490 | + /**/ | |
1491 | + struct ccs_acl_head acl_head; | |
1492 | + struct ccs_transition_control transition_control; | |
1493 | + struct ccs_aggregator aggregator; | |
1494 | + struct ccs_reserved reserved; | |
1495 | + struct ccs_manager manager; | |
1496 | + struct ccs_path_group path_group; | |
1497 | + struct ccs_number_group number_group; | |
1498 | + struct ccs_address_group address_group; | |
1499 | + } e; | |
1500 | +}; | |
1501 | + | |
1502 | +/* Structure for reading/writing policy via /proc/ccs/ interfaces. */ | |
1503 | +struct ccs_io_buffer { | |
1504 | + /* Exclusive lock for this structure. */ | |
1505 | + struct mutex io_sem; | |
1506 | + char __user *read_user_buf; | |
1507 | + size_t read_user_buf_avail; | |
1508 | + struct { | |
1509 | + struct list_head *ns; | |
1510 | + struct list_head *domain; | |
1511 | + struct list_head *group; | |
1512 | + struct list_head *acl; | |
1513 | + size_t avail; | |
1514 | + unsigned int step; | |
1515 | + unsigned int query_index; | |
1516 | + u16 index; | |
1517 | + u16 cond_index; | |
1518 | + u8 acl_group_index; | |
1519 | + u8 cond_step; | |
1520 | + u8 bit; | |
1521 | + u8 w_pos; | |
1522 | + bool eof; | |
1523 | + bool print_this_domain_only; | |
1524 | + bool print_transition_related_only; | |
1525 | + bool print_cond_part; | |
1526 | + const char *w[CCS_MAX_IO_READ_QUEUE]; | |
1527 | + } r; | |
1528 | + struct { | |
1529 | + struct ccs_policy_namespace *ns; | |
1530 | + struct ccs_domain_info *domain; | |
1531 | + size_t avail; | |
1532 | + bool is_delete; | |
1533 | + } w; | |
1534 | + /* Buffer for reading. */ | |
1535 | + char *read_buf; | |
1536 | + /* Size of read buffer. */ | |
1537 | + size_t readbuf_size; | |
1538 | + /* Buffer for writing. */ | |
1539 | + char *write_buf; | |
1540 | + /* Size of write buffer. */ | |
1541 | + size_t writebuf_size; | |
1542 | + /* Type of interface. */ | |
1543 | + enum ccs_proc_interface_index type; | |
1544 | + /* Users counter protected by ccs_io_buffer_list_lock. */ | |
1545 | + u8 users; | |
1546 | + /* List for telling GC not to kfree() elements. */ | |
1547 | + struct list_head list; | |
1548 | +}; | |
1549 | + | |
1550 | +/* Structure for /proc/ccs/profile interface. */ | |
1551 | +struct ccs_profile { | |
1552 | + const struct ccs_path_info *comment; | |
1553 | + u8 default_config; | |
1554 | + u8 config[CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX]; | |
1555 | + unsigned int pref[CCS_MAX_PREF]; | |
1556 | +}; | |
1557 | + | |
1558 | +/* Structure for representing YYYY/MM/DD hh/mm/ss. */ | |
1559 | +struct ccs_time { | |
1560 | + u16 year; | |
1561 | + u8 month; | |
1562 | + u8 day; | |
1563 | + u8 hour; | |
1564 | + u8 min; | |
1565 | + u8 sec; | |
1566 | +}; | |
1567 | + | |
1568 | +/* Structure for policy namespace. */ | |
1569 | +struct ccs_policy_namespace { | |
1570 | + /* Profile table. Memory is allocated as needed. */ | |
1571 | + struct ccs_profile *profile_ptr[CCS_MAX_PROFILES]; | |
1572 | + /* List of "struct ccs_group". */ | |
1573 | + struct list_head group_list[CCS_MAX_GROUP]; | |
1574 | + /* List of policy. */ | |
1575 | + struct list_head policy_list[CCS_MAX_POLICY]; | |
1576 | + /* The global ACL referred by "use_group" keyword. */ | |
1577 | + struct list_head acl_group[CCS_MAX_ACL_GROUPS]; | |
1578 | + /* List for connecting to ccs_namespace_list list. */ | |
1579 | + struct list_head namespace_list; | |
1580 | + /* Profile version. Currently only 20150505 is supported. */ | |
1581 | + unsigned int profile_version; | |
1582 | + /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */ | |
1583 | + const char *name; | |
1584 | +}; | |
1585 | + | |
1586 | +/* Prototype definition for "struct ccsecurity_operations". */ | |
1587 | + | |
1588 | +void __init ccs_permission_init(void); | |
1589 | +void __init ccs_mm_init(void); | |
1590 | + | |
1591 | +/* Prototype definition for internal use. */ | |
1592 | + | |
1593 | +bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos, | |
1594 | + struct ccs_page_dump *dump); | |
1595 | +bool ccs_memory_ok(const void *ptr, const unsigned int size); | |
1596 | +char *ccs_encode(const char *str); | |
1597 | +char *ccs_encode2(const char *str, int str_len); | |
1598 | +char *ccs_realpath(const struct path *path); | |
1599 | +const char *ccs_get_exe(void); | |
1600 | +const struct ccs_path_info *ccs_get_name(const char *name); | |
1601 | +int ccs_audit_log(struct ccs_request_info *r); | |
1602 | +int ccs_check_acl(struct ccs_request_info *r); | |
1603 | +int ccs_init_request_info(struct ccs_request_info *r, const u8 index); | |
1604 | +struct ccs_domain_info *ccs_assign_domain(const char *domainname, | |
1605 | + const bool transit); | |
1606 | +u8 ccs_get_config(const u8 profile, const u8 index); | |
1607 | +void *ccs_commit_ok(void *data, const unsigned int size); | |
1608 | +void ccs_del_acl(struct list_head *element); | |
1609 | +void ccs_del_condition(struct list_head *element); | |
1610 | +void ccs_fill_path_info(struct ccs_path_info *ptr); | |
1611 | +void ccs_get_attributes(struct ccs_obj_info *obj); | |
1612 | +void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register); | |
1613 | +void ccs_transition_failed(const char *domainname); | |
1614 | +void ccs_warn_oom(const char *function); | |
1615 | +void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...) | |
1616 | + __printf(2, 3); | |
1617 | + | |
1618 | +/* Variable definition for internal use. */ | |
1619 | + | |
1620 | +extern bool ccs_policy_loaded; | |
1621 | +extern const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS]; | |
1622 | +extern const u8 ccs_c2mac[CCS_MAX_CAPABILITY_INDEX]; | |
1623 | +extern const u8 ccs_pn2mac[CCS_MAX_PATH_NUMBER_OPERATION]; | |
1624 | +extern const u8 ccs_pnnn2mac[CCS_MAX_MKDEV_OPERATION]; | |
1625 | +extern const u8 ccs_pp2mac[CCS_MAX_PATH2_OPERATION]; | |
1626 | +extern struct ccs_domain_info ccs_kernel_domain; | |
1627 | +extern struct list_head ccs_condition_list; | |
1628 | +extern struct list_head ccs_domain_list; | |
1629 | +extern struct list_head ccs_name_list[CCS_MAX_HASH]; | |
1630 | +extern struct list_head ccs_namespace_list; | |
1631 | +extern struct mutex ccs_policy_lock; | |
1632 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
1633 | +extern struct srcu_struct ccs_ss; | |
1634 | +#endif | |
1635 | +extern unsigned int ccs_memory_quota[CCS_MAX_MEMORY_STAT]; | |
1636 | +extern unsigned int ccs_memory_used[CCS_MAX_MEMORY_STAT]; | |
1637 | + | |
1638 | +/* Inlined functions for internal use. */ | |
1639 | + | |
1640 | +/** | |
1641 | + * ccs_pathcmp - strcmp() for "struct ccs_path_info" structure. | |
1642 | + * | |
1643 | + * @a: Pointer to "struct ccs_path_info". | |
1644 | + * @b: Pointer to "struct ccs_path_info". | |
1645 | + * | |
1646 | + * Returns true if @a != @b, false otherwise. | |
1647 | + */ | |
1648 | +static inline bool ccs_pathcmp(const struct ccs_path_info *a, | |
1649 | + const struct ccs_path_info *b) | |
1650 | +{ | |
1651 | + return a->hash != b->hash || strcmp(a->name, b->name); | |
1652 | +} | |
1653 | + | |
1654 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) | |
1655 | + | |
1656 | +/** | |
1657 | + * ccs_read_lock - Take lock for protecting policy. | |
1658 | + * | |
1659 | + * Returns index number for ccs_read_unlock(). | |
1660 | + */ | |
1661 | +static inline int ccs_read_lock(void) | |
1662 | +{ | |
1663 | + return srcu_read_lock(&ccs_ss); | |
1664 | +} | |
1665 | + | |
1666 | +/** | |
1667 | + * ccs_read_unlock - Release lock for protecting policy. | |
1668 | + * | |
1669 | + * @idx: Index number returned by ccs_read_lock(). | |
1670 | + * | |
1671 | + * Returns nothing. | |
1672 | + */ | |
1673 | +static inline void ccs_read_unlock(const int idx) | |
1674 | +{ | |
1675 | + srcu_read_unlock(&ccs_ss, idx); | |
1676 | +} | |
1677 | + | |
1678 | +#else | |
1679 | + | |
1680 | +int ccs_lock(void); | |
1681 | +void ccs_unlock(const int idx); | |
1682 | + | |
1683 | +/** | |
1684 | + * ccs_read_lock - Take lock for protecting policy. | |
1685 | + * | |
1686 | + * Returns index number for ccs_read_unlock(). | |
1687 | + */ | |
1688 | +static inline int ccs_read_lock(void) | |
1689 | +{ | |
1690 | + return ccs_lock(); | |
1691 | +} | |
1692 | + | |
1693 | +/** | |
1694 | + * ccs_read_unlock - Release lock for protecting policy. | |
1695 | + * | |
1696 | + * @idx: Index number returned by ccs_read_lock(). | |
1697 | + * | |
1698 | + * Returns nothing. | |
1699 | + */ | |
1700 | +static inline void ccs_read_unlock(const int idx) | |
1701 | +{ | |
1702 | + ccs_unlock(idx); | |
1703 | +} | |
1704 | + | |
1705 | +#endif | |
1706 | + | |
1707 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18) | |
1708 | + | |
1709 | +/** | |
1710 | + * ccs_tasklist_lock - Take lock for reading list of "struct task_struct". | |
1711 | + * | |
1712 | + * Returns nothing. | |
1713 | + */ | |
1714 | +static inline void ccs_tasklist_lock(void) | |
1715 | +{ | |
1716 | + rcu_read_lock(); | |
1717 | +} | |
1718 | + | |
1719 | +/** | |
1720 | + * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct". | |
1721 | + * | |
1722 | + * Returns nothing. | |
1723 | + */ | |
1724 | +static inline void ccs_tasklist_unlock(void) | |
1725 | +{ | |
1726 | + rcu_read_unlock(); | |
1727 | +} | |
1728 | + | |
1729 | +#else | |
1730 | + | |
1731 | +/** | |
1732 | + * ccs_tasklist_lock - Take lock for reading list of "struct task_struct". | |
1733 | + * | |
1734 | + * Returns nothing. | |
1735 | + */ | |
1736 | +static inline void ccs_tasklist_lock(void) | |
1737 | +{ | |
1738 | + read_lock(&tasklist_lock); | |
1739 | +} | |
1740 | + | |
1741 | +/** | |
1742 | + * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct". | |
1743 | + * | |
1744 | + * Returns nothing. | |
1745 | + */ | |
1746 | +static inline void ccs_tasklist_unlock(void) | |
1747 | +{ | |
1748 | + read_unlock(&tasklist_lock); | |
1749 | +} | |
1750 | + | |
1751 | +#endif | |
1752 | + | |
1753 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1754 | + | |
1755 | +/** | |
1756 | + * ccs_sys_getppid - Copy of getppid(). | |
1757 | + * | |
1758 | + * Returns parent process's PID. | |
1759 | + * | |
1760 | + * Alpha does not have getppid() defined. To be able to build this module on | |
1761 | + * Alpha, I have to copy getppid() from kernel/timer.c. | |
1762 | + */ | |
1763 | +static inline pid_t ccs_sys_getppid(void) | |
1764 | +{ | |
1765 | + pid_t pid; | |
1766 | + rcu_read_lock(); | |
1767 | + pid = task_tgid_vnr(rcu_dereference(current->real_parent)); | |
1768 | + rcu_read_unlock(); | |
1769 | + return pid; | |
1770 | +} | |
1771 | + | |
1772 | +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) | |
1773 | + | |
1774 | +/** | |
1775 | + * ccs_sys_getppid - Copy of getppid(). | |
1776 | + * | |
1777 | + * Returns parent process's PID. | |
1778 | + * | |
1779 | + * This function was rewritten to use RCU in 2.6.16.34. However, distributors | |
1780 | + * which use earlier kernels (e.g. 2.6.8/2.6.9) did not backport the bugfix. | |
1781 | + * Therefore, I'm using code for 2.6.16.34 for earlier kernels. | |
1782 | + */ | |
1783 | +static inline pid_t ccs_sys_getppid(void) | |
1784 | +{ | |
1785 | + pid_t pid; | |
1786 | + rcu_read_lock(); | |
1787 | +#if (defined(RHEL_MAJOR) && RHEL_MAJOR == 5) || (defined(AX_MAJOR) && AX_MAJOR == 3) | |
1788 | + pid = rcu_dereference(current->parent)->tgid; | |
1789 | +#elif defined(CONFIG_UTRACE) | |
1790 | + /* | |
1791 | + * RHEL 5.0 kernel does not have RHEL_MAJOR/RHEL_MINOR defined. | |
1792 | + * Assume RHEL 5.0 if CONFIG_UTRACE is defined. | |
1793 | + */ | |
1794 | + pid = rcu_dereference(current->parent)->tgid; | |
1795 | +#else | |
1796 | + pid = rcu_dereference(current->real_parent)->tgid; | |
1797 | +#endif | |
1798 | + rcu_read_unlock(); | |
1799 | + return pid; | |
1800 | +} | |
1801 | + | |
1802 | +#else | |
1803 | + | |
1804 | +/** | |
1805 | + * ccs_sys_getppid - Copy of getppid(). | |
1806 | + * | |
1807 | + * Returns parent process's PID. | |
1808 | + * | |
1809 | + * I can't use code for 2.6.16.34 for 2.4 kernels because 2.4 kernels does not | |
1810 | + * have RCU. Therefore, I'm using pessimistic lock (i.e. tasklist_lock | |
1811 | + * spinlock). | |
1812 | + */ | |
1813 | +static inline pid_t ccs_sys_getppid(void) | |
1814 | +{ | |
1815 | + pid_t pid; | |
1816 | + read_lock(&tasklist_lock); | |
1817 | +#ifdef TASK_DEAD | |
1818 | + pid = current->group_leader->real_parent->tgid; | |
1819 | +#else | |
1820 | + pid = current->p_opptr->pid; | |
1821 | +#endif | |
1822 | + read_unlock(&tasklist_lock); | |
1823 | + return pid; | |
1824 | +} | |
1825 | + | |
1826 | +#endif | |
1827 | + | |
1828 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
1829 | + | |
1830 | +/** | |
1831 | + * ccs_sys_getpid - Copy of getpid(). | |
1832 | + * | |
1833 | + * Returns current thread's PID. | |
1834 | + * | |
1835 | + * Alpha does not have getpid() defined. To be able to build this module on | |
1836 | + * Alpha, I have to copy getpid() from kernel/timer.c. | |
1837 | + */ | |
1838 | +static inline pid_t ccs_sys_getpid(void) | |
1839 | +{ | |
1840 | + return task_tgid_vnr(current); | |
1841 | +} | |
1842 | + | |
1843 | +#else | |
1844 | + | |
1845 | +/** | |
1846 | + * ccs_sys_getpid - Copy of getpid(). | |
1847 | + * | |
1848 | + * Returns current thread's PID. | |
1849 | + */ | |
1850 | +static inline pid_t ccs_sys_getpid(void) | |
1851 | +{ | |
1852 | + return current->tgid; | |
1853 | +} | |
1854 | + | |
1855 | +#endif | |
1856 | + | |
1857 | +/** | |
1858 | + * ccs_get_mode - Get mode for specified functionality. | |
1859 | + * | |
1860 | + * @profile: Profile number. | |
1861 | + * @index: Functionality number. | |
1862 | + * | |
1863 | + * Returns mode. | |
1864 | + */ | |
1865 | +static inline u8 ccs_get_mode(const u8 profile, const u8 index) | |
1866 | +{ | |
1867 | + return ccs_get_config(profile, index) & (CCS_CONFIG_MAX_MODE - 1); | |
1868 | +} | |
1869 | + | |
1870 | +#if defined(CONFIG_SLOB) | |
1871 | + | |
1872 | +/** | |
1873 | + * ccs_round2 - Round up to power of 2 for calculating memory usage. | |
1874 | + * | |
1875 | + * @size: Size to be rounded up. | |
1876 | + * | |
1877 | + * Returns @size. | |
1878 | + * | |
1879 | + * Since SLOB does not round up, this function simply returns @size. | |
1880 | + */ | |
1881 | +static inline int ccs_round2(size_t size) | |
1882 | +{ | |
1883 | + return size; | |
1884 | +} | |
1885 | + | |
1886 | +#else | |
1887 | + | |
1888 | +/** | |
1889 | + * ccs_round2 - Round up to power of 2 for calculating memory usage. | |
1890 | + * | |
1891 | + * @size: Size to be rounded up. | |
1892 | + * | |
1893 | + * Returns rounded size. | |
1894 | + * | |
1895 | + * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of | |
1896 | + * (e.g.) 128 bytes. | |
1897 | + */ | |
1898 | +static inline int ccs_round2(size_t size) | |
1899 | +{ | |
1900 | +#if PAGE_SIZE == 4096 | |
1901 | + size_t bsize = 32; | |
1902 | +#else | |
1903 | + size_t bsize = 64; | |
1904 | +#endif | |
1905 | + if (!size) | |
1906 | + return 0; | |
1907 | + while (size > bsize) | |
1908 | + bsize <<= 1; | |
1909 | + return bsize; | |
1910 | +} | |
1911 | + | |
1912 | +#endif | |
1913 | + | |
1914 | +/** | |
1915 | + * ccs_put_condition - Drop reference on "struct ccs_condition". | |
1916 | + * | |
1917 | + * @cond: Pointer to "struct ccs_condition". Maybe NULL. | |
1918 | + * | |
1919 | + * Returns nothing. | |
1920 | + */ | |
1921 | +static inline void ccs_put_condition(struct ccs_condition *cond) | |
1922 | +{ | |
1923 | + if (cond) | |
1924 | + atomic_dec(&cond->head.users); | |
1925 | +} | |
1926 | + | |
1927 | +/** | |
1928 | + * ccs_put_group - Drop reference on "struct ccs_group". | |
1929 | + * | |
1930 | + * @group: Pointer to "struct ccs_group". Maybe NULL. | |
1931 | + * | |
1932 | + * Returns nothing. | |
1933 | + */ | |
1934 | +static inline void ccs_put_group(struct ccs_group *group) | |
1935 | +{ | |
1936 | + if (group) | |
1937 | + atomic_dec(&group->head.users); | |
1938 | +} | |
1939 | + | |
1940 | +/** | |
1941 | + * ccs_put_name - Drop reference on "struct ccs_name". | |
1942 | + * | |
1943 | + * @name: Pointer to "struct ccs_path_info". Maybe NULL. | |
1944 | + * | |
1945 | + * Returns nothing. | |
1946 | + */ | |
1947 | +static inline void ccs_put_name(const struct ccs_path_info *name) | |
1948 | +{ | |
1949 | + if (name) | |
1950 | + atomic_dec(&container_of(name, struct ccs_name, entry)-> | |
1951 | + head.users); | |
1952 | +} | |
1953 | + | |
1954 | +/* For importing variables and functions. */ | |
1955 | +extern struct ccsecurity_exports ccsecurity_exports; | |
1956 | + | |
1957 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
1958 | + | |
1959 | +/* | |
1960 | + * Structure for holding "struct ccs_domain_info *" and "struct ccs_execve *" | |
1961 | + * and "u32 ccs_flags" for each "struct task_struct". | |
1962 | + * | |
1963 | + * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct" | |
1964 | + * are maintained outside that "struct task_struct". Therefore, ccs_security | |
1965 | + * != task_struct . This keeps KABI for distributor's prebuilt kernels but | |
1966 | + * entails slow access. | |
1967 | + * | |
1968 | + * Memory for this structure is allocated when current thread tries to access | |
1969 | + * it. Therefore, if memory allocation failed, current thread will be killed by | |
1970 | + * SIGKILL. Note that if current->pid == 1, sending SIGKILL won't work. | |
1971 | + */ | |
1972 | +struct ccs_security { | |
1973 | + struct list_head list; | |
1974 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) || LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) | |
1975 | + const struct task_struct *task; | |
1976 | +#else | |
1977 | + struct pid *pid; /* Maybe NULL. */ | |
1978 | + const struct cred *cred; /* Maybe NULL. */ | |
1979 | +#endif | |
1980 | + struct ccs_domain_info *ccs_domain_info; | |
1981 | + u32 ccs_flags; | |
1982 | + struct ccs_execve *ee; /* Maybe NULL. */ | |
1983 | + struct rcu_head rcu; | |
1984 | +}; | |
1985 | + | |
1986 | +void __init ccs_main_init(void); | |
1987 | +bool ccs_used_by_cred(const struct ccs_domain_info *domain); | |
1988 | +int ccs_start_execve(struct linux_binprm *bprm, struct ccs_execve **eep); | |
1989 | +void ccs_finish_execve(int retval, struct ccs_execve *ee); | |
1990 | +void ccs_load_policy(const char *filename); | |
1991 | +#ifndef CONFIG_AKARI_TRACE_EXECVE_COUNT | |
1992 | +#define ccs_audit_alloc_execve(ee) do { } while (0) | |
1993 | +#define ccs_audit_free_execve(ee, is_current) do { } while (0) | |
1994 | +#else | |
1995 | +void ccs_audit_alloc_execve(const struct ccs_execve * const ee); | |
1996 | +void ccs_audit_free_execve(const struct ccs_execve * const ee, | |
1997 | + const bool is_current); | |
1998 | +#endif | |
1999 | + | |
2000 | +#define CCS_TASK_SECURITY_HASH_BITS 12 | |
2001 | +#define CCS_MAX_TASK_SECURITY_HASH (1u << CCS_TASK_SECURITY_HASH_BITS) | |
2002 | +extern struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
2003 | + | |
2004 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task); | |
2005 | + | |
2006 | +/** | |
2007 | + * ccs_current_security - Get "struct ccs_security" for current thread. | |
2008 | + * | |
2009 | + * Returns pointer to "struct ccs_security" for current thread. | |
2010 | + */ | |
2011 | +static inline struct ccs_security *ccs_current_security(void) | |
2012 | +{ | |
2013 | + return ccs_find_task_security(current); | |
2014 | +} | |
2015 | + | |
2016 | +/** | |
2017 | + * ccs_task_domain - Get "struct ccs_domain_info" for specified thread. | |
2018 | + * | |
2019 | + * @task: Pointer to "struct task_struct". | |
2020 | + * | |
2021 | + * Returns pointer to "struct ccs_security" for specified thread. | |
2022 | + */ | |
2023 | +static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task) | |
2024 | +{ | |
2025 | + struct ccs_domain_info *domain; | |
2026 | + rcu_read_lock(); | |
2027 | + domain = ccs_find_task_security(task)->ccs_domain_info; | |
2028 | + rcu_read_unlock(); | |
2029 | + return domain; | |
2030 | +} | |
2031 | + | |
2032 | +/** | |
2033 | + * ccs_current_domain - Get "struct ccs_domain_info" for current thread. | |
2034 | + * | |
2035 | + * Returns pointer to "struct ccs_domain_info" for current thread. | |
2036 | + */ | |
2037 | +static inline struct ccs_domain_info *ccs_current_domain(void) | |
2038 | +{ | |
2039 | + return ccs_find_task_security(current)->ccs_domain_info; | |
2040 | +} | |
2041 | + | |
2042 | +/** | |
2043 | + * ccs_task_flags - Get flags for specified thread. | |
2044 | + * | |
2045 | + * @task: Pointer to "struct task_struct". | |
2046 | + * | |
2047 | + * Returns flags for specified thread. | |
2048 | + */ | |
2049 | +static inline u32 ccs_task_flags(struct task_struct *task) | |
2050 | +{ | |
2051 | + u32 ccs_flags; | |
2052 | + rcu_read_lock(); | |
2053 | + ccs_flags = ccs_find_task_security(task)->ccs_flags; | |
2054 | + rcu_read_unlock(); | |
2055 | + return ccs_flags; | |
2056 | +} | |
2057 | + | |
2058 | +/** | |
2059 | + * ccs_current_flags - Get flags for current thread. | |
2060 | + * | |
2061 | + * Returns flags for current thread. | |
2062 | + */ | |
2063 | +static inline u32 ccs_current_flags(void) | |
2064 | +{ | |
2065 | + return ccs_find_task_security(current)->ccs_flags; | |
2066 | +} | |
2067 | + | |
2068 | +#else | |
2069 | + | |
2070 | +/* | |
2071 | + * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct" | |
2072 | + * are maintained inside that "struct task_struct". Therefore, ccs_security == | |
2073 | + * task_struct . This allows fast access but breaks KABI checks for | |
2074 | + * distributor's prebuilt kernels due to changes in "struct task_struct". | |
2075 | + */ | |
2076 | +#define ccs_security task_struct | |
2077 | + | |
2078 | +/** | |
2079 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
2080 | + * | |
2081 | + * @task: Pointer to "struct task_struct". | |
2082 | + * | |
2083 | + * Returns pointer to "struct ccs_security". | |
2084 | + */ | |
2085 | +static inline struct ccs_security *ccs_find_task_security(struct task_struct * | |
2086 | + task) | |
2087 | +{ | |
2088 | + return task; | |
2089 | +} | |
2090 | + | |
2091 | +/** | |
2092 | + * ccs_current_security - Get "struct ccs_security" for current thread. | |
2093 | + * | |
2094 | + * Returns pointer to "struct ccs_security" for current thread. | |
2095 | + */ | |
2096 | +static inline struct ccs_security *ccs_current_security(void) | |
2097 | +{ | |
2098 | + return ccs_find_task_security(current); | |
2099 | +} | |
2100 | + | |
2101 | +/** | |
2102 | + * ccs_task_domain - Get "struct ccs_domain_info" for specified thread. | |
2103 | + * | |
2104 | + * @task: Pointer to "struct task_struct". | |
2105 | + * | |
2106 | + * Returns pointer to "struct ccs_security" for specified thread. | |
2107 | + */ | |
2108 | +static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task) | |
2109 | +{ | |
2110 | + struct ccs_domain_info *domain = task->ccs_domain_info; | |
2111 | + return domain ? domain : &ccs_kernel_domain; | |
2112 | +} | |
2113 | + | |
2114 | +/** | |
2115 | + * ccs_current_domain - Get "struct ccs_domain_info" for current thread. | |
2116 | + * | |
2117 | + * Returns pointer to "struct ccs_domain_info" for current thread. | |
2118 | + * | |
2119 | + * If current thread does not belong to a domain (which is true for initial | |
2120 | + * init_task in order to hide ccs_kernel_domain from this module), | |
2121 | + * current thread enters into ccs_kernel_domain. | |
2122 | + */ | |
2123 | +static inline struct ccs_domain_info *ccs_current_domain(void) | |
2124 | +{ | |
2125 | + struct task_struct *task = current; | |
2126 | + if (!task->ccs_domain_info) | |
2127 | + task->ccs_domain_info = &ccs_kernel_domain; | |
2128 | + return task->ccs_domain_info; | |
2129 | +} | |
2130 | + | |
2131 | +/** | |
2132 | + * ccs_task_flags - Get flags for specified thread. | |
2133 | + * | |
2134 | + * @task: Pointer to "struct task_struct". | |
2135 | + * | |
2136 | + * Returns flags for specified thread. | |
2137 | + */ | |
2138 | +static inline u32 ccs_task_flags(struct task_struct *task) | |
2139 | +{ | |
2140 | + return ccs_find_task_security(task)->ccs_flags; | |
2141 | +} | |
2142 | + | |
2143 | +/** | |
2144 | + * ccs_current_flags - Get flags for current thread. | |
2145 | + * | |
2146 | + * Returns flags for current thread. | |
2147 | + */ | |
2148 | +static inline u32 ccs_current_flags(void) | |
2149 | +{ | |
2150 | + return ccs_find_task_security(current)->ccs_flags; | |
2151 | +} | |
2152 | + | |
2153 | +#endif | |
2154 | + | |
2155 | +/** | |
2156 | + * ccs_current_namespace - Get "struct ccs_policy_namespace" for current thread. | |
2157 | + * | |
2158 | + * Returns pointer to "struct ccs_policy_namespace" for current thread. | |
2159 | + */ | |
2160 | +static inline struct ccs_policy_namespace *ccs_current_namespace(void) | |
2161 | +{ | |
2162 | + return ccs_current_domain()->ns; | |
2163 | +} | |
2164 | + | |
2165 | +#endif |
@@ -0,0 +1,363 @@ | ||
1 | +/* | |
2 | + * security/ccsecurity/memory.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 | +/* Use functions in lsm.c */ | |
12 | +#undef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
13 | + | |
14 | +/***** SECTION1: Constants definition *****/ | |
15 | + | |
16 | +/***** SECTION2: Structure definition *****/ | |
17 | + | |
18 | +/***** SECTION3: Prototype definition section *****/ | |
19 | + | |
20 | +bool ccs_memory_ok(const void *ptr, const unsigned int size); | |
21 | +const struct ccs_path_info *ccs_get_name(const char *name); | |
22 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
23 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task); | |
24 | +#endif | |
25 | +void *ccs_commit_ok(void *data, const unsigned int size); | |
26 | +void __init ccs_mm_init(void); | |
27 | +void ccs_warn_oom(const char *function); | |
28 | + | |
29 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
30 | +static int __ccs_alloc_task_security(const struct task_struct *task); | |
31 | +static void __ccs_free_task_security(const struct task_struct *task); | |
32 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
33 | + struct list_head *list); | |
34 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
35 | +static void ccs_rcu_free(struct rcu_head *rcu); | |
36 | +#else | |
37 | +static void ccs_rcu_free(void *arg); | |
38 | +#endif | |
39 | +#endif | |
40 | + | |
41 | +/***** SECTION4: Standalone functions section *****/ | |
42 | + | |
43 | +/***** SECTION5: Variables definition section *****/ | |
44 | + | |
45 | +/* Memoy currently used by policy/audit log/query. */ | |
46 | +unsigned int ccs_memory_used[CCS_MAX_MEMORY_STAT]; | |
47 | + | |
48 | +/* Memory quota for "policy"/"audit log"/"query". */ | |
49 | +unsigned int ccs_memory_quota[CCS_MAX_MEMORY_STAT]; | |
50 | + | |
51 | +/* The list for "struct ccs_name". */ | |
52 | +struct list_head ccs_name_list[CCS_MAX_HASH]; | |
53 | + | |
54 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
55 | + | |
56 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
57 | +static struct ccs_security ccs_oom_security = { | |
58 | + .ccs_domain_info = &ccs_kernel_domain | |
59 | +}; | |
60 | + | |
61 | +/* Dummy security context for avoiding NULL pointer dereference. */ | |
62 | +static struct ccs_security ccs_default_security = { | |
63 | + .ccs_domain_info = &ccs_kernel_domain | |
64 | +}; | |
65 | + | |
66 | +/* List of "struct ccs_security". */ | |
67 | +struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH]; | |
68 | +/* Lock for protecting ccs_task_security_list[]. */ | |
69 | +static DEFINE_SPINLOCK(ccs_task_security_list_lock); | |
70 | + | |
71 | +#endif | |
72 | + | |
73 | +/***** SECTION6: Dependent functions section *****/ | |
74 | + | |
75 | +/** | |
76 | + * ccs_warn_oom - Print out of memory warning message. | |
77 | + * | |
78 | + * @function: Function's name. | |
79 | + * | |
80 | + * Returns nothing. | |
81 | + */ | |
82 | +void ccs_warn_oom(const char *function) | |
83 | +{ | |
84 | + /* Reduce error messages. */ | |
85 | + static pid_t ccs_last_pid; | |
86 | + const pid_t pid = current->pid; | |
87 | + if (ccs_last_pid != pid) { | |
88 | + printk(KERN_WARNING "ERROR: Out of memory at %s.\n", | |
89 | + function); | |
90 | + ccs_last_pid = pid; | |
91 | + } | |
92 | + if (!ccs_policy_loaded) | |
93 | + panic("MAC Initialization failed.\n"); | |
94 | +} | |
95 | + | |
96 | +/** | |
97 | + * ccs_memory_ok - Check memory quota. | |
98 | + * | |
99 | + * @ptr: Pointer to allocated memory. Maybe NULL. | |
100 | + * @size: Size in byte. Not used if @ptr is NULL. | |
101 | + * | |
102 | + * Returns true if @ptr is not NULL and quota not exceeded, false otherwise. | |
103 | + * | |
104 | + * Caller holds ccs_policy_lock mutex. | |
105 | + */ | |
106 | +bool ccs_memory_ok(const void *ptr, const unsigned int size) | |
107 | +{ | |
108 | + if (ptr) { | |
109 | + const size_t s = ccs_round2(size); | |
110 | + ccs_memory_used[CCS_MEMORY_POLICY] += s; | |
111 | + if (!ccs_memory_quota[CCS_MEMORY_POLICY] || | |
112 | + ccs_memory_used[CCS_MEMORY_POLICY] <= | |
113 | + ccs_memory_quota[CCS_MEMORY_POLICY]) | |
114 | + return true; | |
115 | + ccs_memory_used[CCS_MEMORY_POLICY] -= s; | |
116 | + } | |
117 | + ccs_warn_oom(__func__); | |
118 | + return false; | |
119 | +} | |
120 | + | |
121 | +/** | |
122 | + * ccs_commit_ok - Allocate memory and check memory quota. | |
123 | + * | |
124 | + * @data: Data to copy from. | |
125 | + * @size: Size in byte. | |
126 | + * | |
127 | + * Returns pointer to allocated memory on success, NULL otherwise. | |
128 | + * @data is zero-cleared on success. | |
129 | + * | |
130 | + * Caller holds ccs_policy_lock mutex. | |
131 | + */ | |
132 | +void *ccs_commit_ok(void *data, const unsigned int size) | |
133 | +{ | |
134 | + void *ptr = kmalloc(size, CCS_GFP_FLAGS); | |
135 | + if (ccs_memory_ok(ptr, size)) { | |
136 | + memmove(ptr, data, size); | |
137 | + memset(data, 0, size); | |
138 | + return ptr; | |
139 | + } | |
140 | + kfree(ptr); | |
141 | + return NULL; | |
142 | +} | |
143 | + | |
144 | +/** | |
145 | + * ccs_get_name - Allocate memory for string data. | |
146 | + * | |
147 | + * @name: The string to store into the permernent memory. | |
148 | + * | |
149 | + * Returns pointer to "struct ccs_path_info" on success, NULL otherwise. | |
150 | + */ | |
151 | +const struct ccs_path_info *ccs_get_name(const char *name) | |
152 | +{ | |
153 | + struct ccs_name *ptr; | |
154 | + unsigned int hash; | |
155 | + int len; | |
156 | + int allocated_len; | |
157 | + struct list_head *head; | |
158 | + | |
159 | + if (!name) | |
160 | + return NULL; | |
161 | + len = strlen(name) + 1; | |
162 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0) | |
163 | + hash = full_name_hash(NULL, name, len - 1); | |
164 | +#else | |
165 | + hash = full_name_hash((const unsigned char *) name, len - 1); | |
166 | +#endif | |
167 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(RHEL_MAJOR) | |
168 | + head = &ccs_name_list[hash_long(hash, CCS_HASH_BITS)]; | |
169 | +#else | |
170 | + head = &ccs_name_list[hash % CCS_MAX_HASH]; | |
171 | +#endif | |
172 | + if (mutex_lock_interruptible(&ccs_policy_lock)) | |
173 | + return NULL; | |
174 | + list_for_each_entry(ptr, head, head.list) { | |
175 | + if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name) || | |
176 | + atomic_read(&ptr->head.users) == CCS_GC_IN_PROGRESS) | |
177 | + continue; | |
178 | + atomic_inc(&ptr->head.users); | |
179 | + goto out; | |
180 | + } | |
181 | + allocated_len = sizeof(*ptr) + len; | |
182 | + ptr = kzalloc(allocated_len, CCS_GFP_FLAGS); | |
183 | + if (ccs_memory_ok(ptr, allocated_len)) { | |
184 | + ptr->entry.name = ((char *) ptr) + sizeof(*ptr); | |
185 | + memmove((char *) ptr->entry.name, name, len); | |
186 | + atomic_set(&ptr->head.users, 1); | |
187 | + ccs_fill_path_info(&ptr->entry); | |
188 | + ptr->size = allocated_len; | |
189 | + list_add_tail(&ptr->head.list, head); | |
190 | + } else { | |
191 | + kfree(ptr); | |
192 | + ptr = NULL; | |
193 | + } | |
194 | +out: | |
195 | + mutex_unlock(&ccs_policy_lock); | |
196 | + return ptr ? &ptr->entry : NULL; | |
197 | +} | |
198 | + | |
199 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
200 | + | |
201 | +/** | |
202 | + * ccs_add_task_security - Add "struct ccs_security" to list. | |
203 | + * | |
204 | + * @ptr: Pointer to "struct ccs_security". | |
205 | + * @list: Pointer to "struct list_head". | |
206 | + * | |
207 | + * Returns nothing. | |
208 | + */ | |
209 | +static void ccs_add_task_security(struct ccs_security *ptr, | |
210 | + struct list_head *list) | |
211 | +{ | |
212 | + unsigned long flags; | |
213 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
214 | + list_add_rcu(&ptr->list, list); | |
215 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
216 | +} | |
217 | + | |
218 | +/** | |
219 | + * __ccs_alloc_task_security - Allocate memory for new tasks. | |
220 | + * | |
221 | + * @task: Pointer to "struct task_struct". | |
222 | + * | |
223 | + * Returns 0 on success, negative value otherwise. | |
224 | + */ | |
225 | +static int __ccs_alloc_task_security(const struct task_struct *task) | |
226 | +{ | |
227 | + struct ccs_security *old_security = ccs_current_security(); | |
228 | + struct ccs_security *new_security = kzalloc(sizeof(*new_security), | |
229 | + GFP_KERNEL); | |
230 | + struct list_head *list = &ccs_task_security_list | |
231 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
232 | + if (!new_security) | |
233 | + return -ENOMEM; | |
234 | + new_security->task = task; | |
235 | + new_security->ccs_domain_info = old_security->ccs_domain_info; | |
236 | + new_security->ccs_flags = old_security->ccs_flags; | |
237 | + ccs_add_task_security(new_security, list); | |
238 | + return 0; | |
239 | +} | |
240 | + | |
241 | +/** | |
242 | + * ccs_find_task_security - Find "struct ccs_security" for given task. | |
243 | + * | |
244 | + * @task: Pointer to "struct task_struct". | |
245 | + * | |
246 | + * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on | |
247 | + * out of memory, &ccs_default_security otherwise. | |
248 | + * | |
249 | + * If @task is current thread and "struct ccs_security" for current thread was | |
250 | + * not found, I try to allocate it. But if allocation failed, current thread | |
251 | + * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL | |
252 | + * won't work. | |
253 | + */ | |
254 | +struct ccs_security *ccs_find_task_security(const struct task_struct *task) | |
255 | +{ | |
256 | + struct ccs_security *ptr; | |
257 | + struct list_head *list = &ccs_task_security_list | |
258 | + [hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)]; | |
259 | + /* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */ | |
260 | + while (!list->next); | |
261 | + rcu_read_lock(); | |
262 | + list_for_each_entry_rcu(ptr, list, list) { | |
263 | + if (ptr->task != task) | |
264 | + continue; | |
265 | + rcu_read_unlock(); | |
266 | + return ptr; | |
267 | + } | |
268 | + rcu_read_unlock(); | |
269 | + if (task != current) | |
270 | + return &ccs_default_security; | |
271 | + /* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */ | |
272 | + ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC); | |
273 | + if (!ptr) { | |
274 | + printk(KERN_WARNING "Unable to allocate memory for pid=%u\n", | |
275 | + task->pid); | |
276 | + send_sig(SIGKILL, current, 0); | |
277 | + return &ccs_oom_security; | |
278 | + } | |
279 | + *ptr = ccs_default_security; | |
280 | + ptr->task = task; | |
281 | + ccs_add_task_security(ptr, list); | |
282 | + return ptr; | |
283 | +} | |
284 | + | |
285 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
286 | + | |
287 | +/** | |
288 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
289 | + * | |
290 | + * @rcu: Pointer to "struct rcu_head". | |
291 | + * | |
292 | + * Returns nothing. | |
293 | + */ | |
294 | +static void ccs_rcu_free(struct rcu_head *rcu) | |
295 | +{ | |
296 | + struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu); | |
297 | + kfree(ptr); | |
298 | +} | |
299 | + | |
300 | +#else | |
301 | + | |
302 | +/** | |
303 | + * ccs_rcu_free - RCU callback for releasing "struct ccs_security". | |
304 | + * | |
305 | + * @arg: Pointer to "void". | |
306 | + * | |
307 | + * Returns nothing. | |
308 | + */ | |
309 | +static void ccs_rcu_free(void *arg) | |
310 | +{ | |
311 | + struct ccs_security *ptr = arg; | |
312 | + kfree(ptr); | |
313 | +} | |
314 | + | |
315 | +#endif | |
316 | + | |
317 | +/** | |
318 | + * __ccs_free_task_security - Release memory associated with "struct task_struct". | |
319 | + * | |
320 | + * @task: Pointer to "struct task_struct". | |
321 | + * | |
322 | + * Returns nothing. | |
323 | + */ | |
324 | +static void __ccs_free_task_security(const struct task_struct *task) | |
325 | +{ | |
326 | + unsigned long flags; | |
327 | + struct ccs_security *ptr = ccs_find_task_security(task); | |
328 | + if (ptr == &ccs_default_security || ptr == &ccs_oom_security) | |
329 | + return; | |
330 | + spin_lock_irqsave(&ccs_task_security_list_lock, flags); | |
331 | + list_del_rcu(&ptr->list); | |
332 | + spin_unlock_irqrestore(&ccs_task_security_list_lock, flags); | |
333 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8) | |
334 | + call_rcu(&ptr->rcu, ccs_rcu_free); | |
335 | +#else | |
336 | + call_rcu(&ptr->rcu, ccs_rcu_free, ptr); | |
337 | +#endif | |
338 | +} | |
339 | + | |
340 | +#endif | |
341 | + | |
342 | +/** | |
343 | + * ccs_mm_init - Initialize mm related code. | |
344 | + * | |
345 | + * Returns nothing. | |
346 | + */ | |
347 | +void __init ccs_mm_init(void) | |
348 | +{ | |
349 | + int idx; | |
350 | + for (idx = 0; idx < CCS_MAX_HASH; idx++) | |
351 | + INIT_LIST_HEAD(&ccs_name_list[idx]); | |
352 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
353 | + for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) | |
354 | + INIT_LIST_HEAD(&ccs_task_security_list[idx]); | |
355 | +#endif | |
356 | + smp_wmb(); /* Avoid out of order execution. */ | |
357 | +#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY | |
358 | + ccsecurity_ops.alloc_task_security = __ccs_alloc_task_security; | |
359 | + ccsecurity_ops.free_task_security = __ccs_free_task_security; | |
360 | +#endif | |
361 | + ccs_kernel_domain.domainname = ccs_get_name("<kernel>"); | |
362 | + list_add_tail_rcu(&ccs_kernel_domain.list, &ccs_domain_list); | |
363 | +} |
@@ -0,0 +1,52 @@ | ||
1 | +#include <linux/version.h> | |
2 | +#include <linux/module.h> | |
3 | +#include <linux/sched.h> | |
4 | +#include <linux/dcache.h> | |
5 | +#include <linux/mount.h> | |
6 | +#include <linux/namei.h> | |
7 | +#include <linux/fs.h> | |
8 | +#include <linux/security.h> | |
9 | +#ifndef __init | |
10 | +#include <linux/init.h> | |
11 | +#endif | |
12 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) | |
13 | +#include <linux/cred.h> | |
14 | +#endif | |
15 | + | |
16 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) | |
17 | +#error This module supports only 2.6.0 and later kernels. | |
18 | +#endif | |
19 | +#ifndef CONFIG_SECURITY | |
20 | +#error You must choose CONFIG_SECURITY=y for building this module. | |
21 | +#endif | |
22 | +#ifndef CONFIG_KALLSYMS | |
23 | +#error You must choose CONFIG_KALLSYMS=y for building this module. | |
24 | +#endif | |
25 | +#ifndef CONFIG_PROC_FS | |
26 | +#error You must choose CONFIG_PROC_FS=y for building this module. | |
27 | +#endif | |
28 | +#ifndef CONFIG_MODULES | |
29 | +#error You must choose CONFIG_MODULES=y for building this module. | |
30 | +#endif | |
31 | + | |
32 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) && !defined(SECURITY_NAME_MAX) | |
33 | +#include <linux/lsm_hooks.h> | |
34 | +extern struct security_hook_heads probe_dummy_security_hook_heads; | |
35 | +struct security_hook_heads * __init probe_security_hook_heads(void); | |
36 | +#else | |
37 | +struct security_operations; | |
38 | +struct security_operations * __init probe_security_ops(void); | |
39 | +#endif | |
40 | + | |
41 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) | |
42 | +void * __init probe_find_task_by_vpid(void); | |
43 | +void * __init probe_find_task_by_pid_ns(void); | |
44 | +#endif | |
45 | + | |
46 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) | |
47 | +void * __init probe_vfsmount_lock(void); | |
48 | +#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0) | |
49 | +void * __init probe___d_path(void); | |
50 | +#else | |
51 | +void * __init probe_d_absolute_path(void); | |
52 | +#endif |
@@ -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,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 |
Part of diff was cut off due to size limit. Use your local client to view the full diff.