]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Updated: Karl MacMillan <kmacmillan@tresys.com> |
2 | * | |
3 | * Added conditional policy language extensions | |
4 | * | |
5 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC | |
6 | * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, version 2. | |
10 | */ | |
11 | ||
1da177e4 LT |
12 | #include <linux/kernel.h> |
13 | #include <linux/pagemap.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/vmalloc.h> | |
16 | #include <linux/fs.h> | |
bb003079 | 17 | #include <linux/mutex.h> |
1da177e4 LT |
18 | #include <linux/init.h> |
19 | #include <linux/string.h> | |
20 | #include <linux/security.h> | |
21 | #include <linux/major.h> | |
22 | #include <linux/seq_file.h> | |
23 | #include <linux/percpu.h> | |
af601e46 | 24 | #include <linux/audit.h> |
1da177e4 LT |
25 | #include <asm/uaccess.h> |
26 | #include <asm/semaphore.h> | |
27 | ||
28 | /* selinuxfs pseudo filesystem for exporting the security policy API. | |
29 | Based on the proc code and the fs/nfsd/nfsctl.c code. */ | |
30 | ||
31 | #include "flask.h" | |
32 | #include "avc.h" | |
33 | #include "avc_ss.h" | |
34 | #include "security.h" | |
35 | #include "objsec.h" | |
36 | #include "conditional.h" | |
37 | ||
38 | unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; | |
39 | ||
4e5ab4cb JM |
40 | #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT |
41 | #define SELINUX_COMPAT_NET_VALUE 0 | |
42 | #else | |
43 | #define SELINUX_COMPAT_NET_VALUE 1 | |
44 | #endif | |
45 | ||
46 | int selinux_compat_net = SELINUX_COMPAT_NET_VALUE; | |
47 | ||
1da177e4 LT |
48 | static int __init checkreqprot_setup(char *str) |
49 | { | |
50 | selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0; | |
51 | return 1; | |
52 | } | |
53 | __setup("checkreqprot=", checkreqprot_setup); | |
54 | ||
4e5ab4cb JM |
55 | static int __init selinux_compat_net_setup(char *str) |
56 | { | |
57 | selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0; | |
58 | return 1; | |
59 | } | |
60 | __setup("selinux_compat_net=", selinux_compat_net_setup); | |
61 | ||
1da177e4 | 62 | |
bb003079 | 63 | static DEFINE_MUTEX(sel_mutex); |
1da177e4 LT |
64 | |
65 | /* global data for booleans */ | |
66 | static struct dentry *bool_dir = NULL; | |
67 | static int bool_num = 0; | |
68 | static int *bool_pending_values = NULL; | |
69 | ||
e47c8fc5 CP |
70 | /* global data for classes */ |
71 | static struct dentry *class_dir = NULL; | |
72 | static unsigned long last_class_ino; | |
73 | ||
1da177e4 LT |
74 | extern void selnl_notify_setenforce(int val); |
75 | ||
76 | /* Check whether a task is allowed to use a security operation. */ | |
77 | static int task_has_security(struct task_struct *tsk, | |
78 | u32 perms) | |
79 | { | |
80 | struct task_security_struct *tsec; | |
81 | ||
82 | tsec = tsk->security; | |
83 | if (!tsec) | |
84 | return -EACCES; | |
85 | ||
86 | return avc_has_perm(tsec->sid, SECINITSID_SECURITY, | |
87 | SECCLASS_SECURITY, perms, NULL); | |
88 | } | |
89 | ||
90 | enum sel_inos { | |
91 | SEL_ROOT_INO = 2, | |
92 | SEL_LOAD, /* load policy */ | |
93 | SEL_ENFORCE, /* get or set enforcing status */ | |
94 | SEL_CONTEXT, /* validate context */ | |
95 | SEL_ACCESS, /* compute access decision */ | |
96 | SEL_CREATE, /* compute create labeling decision */ | |
97 | SEL_RELABEL, /* compute relabeling decision */ | |
98 | SEL_USER, /* compute reachable user contexts */ | |
99 | SEL_POLICYVERS, /* return policy version for this kernel */ | |
100 | SEL_COMMIT_BOOLS, /* commit new boolean values */ | |
101 | SEL_MLS, /* return if MLS policy is enabled */ | |
102 | SEL_DISABLE, /* disable SELinux until next reboot */ | |
1da177e4 LT |
103 | SEL_MEMBER, /* compute polyinstantiation membership decision */ |
104 | SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ | |
4e5ab4cb | 105 | SEL_COMPAT_NET, /* whether to use old compat network packet controls */ |
6174eafc | 106 | SEL_INO_NEXT, /* The next inode number to use */ |
1da177e4 LT |
107 | }; |
108 | ||
6174eafc JC |
109 | static unsigned long sel_last_ino = SEL_INO_NEXT - 1; |
110 | ||
f0ee2e46 | 111 | #define SEL_INITCON_INO_OFFSET 0x01000000 |
bce34bc0 | 112 | #define SEL_BOOL_INO_OFFSET 0x02000000 |
e47c8fc5 | 113 | #define SEL_CLASS_INO_OFFSET 0x04000000 |
f0ee2e46 JC |
114 | #define SEL_INO_MASK 0x00ffffff |
115 | ||
1da177e4 LT |
116 | #define TMPBUFLEN 12 |
117 | static ssize_t sel_read_enforce(struct file *filp, char __user *buf, | |
118 | size_t count, loff_t *ppos) | |
119 | { | |
120 | char tmpbuf[TMPBUFLEN]; | |
121 | ssize_t length; | |
122 | ||
123 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); | |
124 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
125 | } | |
126 | ||
127 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP | |
128 | static ssize_t sel_write_enforce(struct file * file, const char __user * buf, | |
129 | size_t count, loff_t *ppos) | |
130 | ||
131 | { | |
132 | char *page; | |
133 | ssize_t length; | |
134 | int new_value; | |
135 | ||
bfd51626 | 136 | if (count >= PAGE_SIZE) |
1da177e4 LT |
137 | return -ENOMEM; |
138 | if (*ppos != 0) { | |
139 | /* No partial writes. */ | |
140 | return -EINVAL; | |
141 | } | |
142 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
143 | if (!page) | |
144 | return -ENOMEM; | |
145 | length = -EFAULT; | |
146 | if (copy_from_user(page, buf, count)) | |
147 | goto out; | |
148 | ||
149 | length = -EINVAL; | |
150 | if (sscanf(page, "%d", &new_value) != 1) | |
151 | goto out; | |
152 | ||
153 | if (new_value != selinux_enforcing) { | |
154 | length = task_has_security(current, SECURITY__SETENFORCE); | |
155 | if (length) | |
156 | goto out; | |
af601e46 SG |
157 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
158 | "enforcing=%d old_enforcing=%d auid=%u", new_value, | |
159 | selinux_enforcing, | |
160 | audit_get_loginuid(current->audit_context)); | |
1da177e4 LT |
161 | selinux_enforcing = new_value; |
162 | if (selinux_enforcing) | |
163 | avc_ss_reset(0); | |
164 | selnl_notify_setenforce(selinux_enforcing); | |
165 | } | |
166 | length = count; | |
167 | out: | |
168 | free_page((unsigned long) page); | |
169 | return length; | |
170 | } | |
171 | #else | |
172 | #define sel_write_enforce NULL | |
173 | #endif | |
174 | ||
9c2e08c5 | 175 | static const struct file_operations sel_enforce_ops = { |
1da177e4 LT |
176 | .read = sel_read_enforce, |
177 | .write = sel_write_enforce, | |
178 | }; | |
179 | ||
180 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE | |
181 | static ssize_t sel_write_disable(struct file * file, const char __user * buf, | |
182 | size_t count, loff_t *ppos) | |
183 | ||
184 | { | |
185 | char *page; | |
186 | ssize_t length; | |
187 | int new_value; | |
188 | extern int selinux_disable(void); | |
189 | ||
bfd51626 | 190 | if (count >= PAGE_SIZE) |
1da177e4 LT |
191 | return -ENOMEM; |
192 | if (*ppos != 0) { | |
193 | /* No partial writes. */ | |
194 | return -EINVAL; | |
195 | } | |
196 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
197 | if (!page) | |
198 | return -ENOMEM; | |
199 | length = -EFAULT; | |
200 | if (copy_from_user(page, buf, count)) | |
201 | goto out; | |
202 | ||
203 | length = -EINVAL; | |
204 | if (sscanf(page, "%d", &new_value) != 1) | |
205 | goto out; | |
206 | ||
207 | if (new_value) { | |
208 | length = selinux_disable(); | |
209 | if (length < 0) | |
210 | goto out; | |
af601e46 SG |
211 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
212 | "selinux=0 auid=%u", | |
213 | audit_get_loginuid(current->audit_context)); | |
1da177e4 LT |
214 | } |
215 | ||
216 | length = count; | |
217 | out: | |
218 | free_page((unsigned long) page); | |
219 | return length; | |
220 | } | |
221 | #else | |
222 | #define sel_write_disable NULL | |
223 | #endif | |
224 | ||
9c2e08c5 | 225 | static const struct file_operations sel_disable_ops = { |
1da177e4 LT |
226 | .write = sel_write_disable, |
227 | }; | |
228 | ||
229 | static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, | |
230 | size_t count, loff_t *ppos) | |
231 | { | |
232 | char tmpbuf[TMPBUFLEN]; | |
233 | ssize_t length; | |
234 | ||
235 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); | |
236 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
237 | } | |
238 | ||
9c2e08c5 | 239 | static const struct file_operations sel_policyvers_ops = { |
1da177e4 LT |
240 | .read = sel_read_policyvers, |
241 | }; | |
242 | ||
243 | /* declaration for sel_write_load */ | |
244 | static int sel_make_bools(void); | |
e47c8fc5 CP |
245 | static int sel_make_classes(void); |
246 | ||
247 | /* declaration for sel_make_class_dirs */ | |
248 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, | |
249 | unsigned long *ino); | |
1da177e4 LT |
250 | |
251 | static ssize_t sel_read_mls(struct file *filp, char __user *buf, | |
252 | size_t count, loff_t *ppos) | |
253 | { | |
254 | char tmpbuf[TMPBUFLEN]; | |
255 | ssize_t length; | |
256 | ||
257 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled); | |
258 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
259 | } | |
260 | ||
9c2e08c5 | 261 | static const struct file_operations sel_mls_ops = { |
1da177e4 LT |
262 | .read = sel_read_mls, |
263 | }; | |
264 | ||
265 | static ssize_t sel_write_load(struct file * file, const char __user * buf, | |
266 | size_t count, loff_t *ppos) | |
267 | ||
268 | { | |
269 | int ret; | |
270 | ssize_t length; | |
271 | void *data = NULL; | |
272 | ||
bb003079 | 273 | mutex_lock(&sel_mutex); |
1da177e4 LT |
274 | |
275 | length = task_has_security(current, SECURITY__LOAD_POLICY); | |
276 | if (length) | |
277 | goto out; | |
278 | ||
279 | if (*ppos != 0) { | |
280 | /* No partial writes. */ | |
281 | length = -EINVAL; | |
282 | goto out; | |
283 | } | |
284 | ||
bfd51626 | 285 | if ((count > 64 * 1024 * 1024) |
1da177e4 LT |
286 | || (data = vmalloc(count)) == NULL) { |
287 | length = -ENOMEM; | |
288 | goto out; | |
289 | } | |
290 | ||
291 | length = -EFAULT; | |
292 | if (copy_from_user(data, buf, count) != 0) | |
293 | goto out; | |
294 | ||
295 | length = security_load_policy(data, count); | |
296 | if (length) | |
297 | goto out; | |
298 | ||
299 | ret = sel_make_bools(); | |
e47c8fc5 CP |
300 | if (ret) { |
301 | length = ret; | |
302 | goto out1; | |
303 | } | |
304 | ||
305 | ret = sel_make_classes(); | |
1da177e4 LT |
306 | if (ret) |
307 | length = ret; | |
308 | else | |
309 | length = count; | |
e47c8fc5 CP |
310 | |
311 | out1: | |
af601e46 SG |
312 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
313 | "policy loaded auid=%u", | |
314 | audit_get_loginuid(current->audit_context)); | |
1da177e4 | 315 | out: |
bb003079 | 316 | mutex_unlock(&sel_mutex); |
1da177e4 LT |
317 | vfree(data); |
318 | return length; | |
319 | } | |
320 | ||
9c2e08c5 | 321 | static const struct file_operations sel_load_ops = { |
1da177e4 LT |
322 | .write = sel_write_load, |
323 | }; | |
324 | ||
ce9982d0 | 325 | static ssize_t sel_write_context(struct file * file, char *buf, size_t size) |
1da177e4 | 326 | { |
ce9982d0 SS |
327 | char *canon; |
328 | u32 sid, len; | |
1da177e4 LT |
329 | ssize_t length; |
330 | ||
331 | length = task_has_security(current, SECURITY__CHECK_CONTEXT); | |
332 | if (length) | |
333 | return length; | |
334 | ||
ce9982d0 SS |
335 | length = security_context_to_sid(buf, size, &sid); |
336 | if (length < 0) | |
337 | return length; | |
1da177e4 | 338 | |
ce9982d0 | 339 | length = security_sid_to_context(sid, &canon, &len); |
1da177e4 | 340 | if (length < 0) |
ce9982d0 SS |
341 | return length; |
342 | ||
343 | if (len > SIMPLE_TRANSACTION_LIMIT) { | |
344 | printk(KERN_ERR "%s: context size (%u) exceeds payload " | |
345 | "max\n", __FUNCTION__, len); | |
346 | length = -ERANGE; | |
1da177e4 | 347 | goto out; |
ce9982d0 | 348 | } |
1da177e4 | 349 | |
ce9982d0 SS |
350 | memcpy(buf, canon, len); |
351 | length = len; | |
1da177e4 | 352 | out: |
ce9982d0 | 353 | kfree(canon); |
1da177e4 LT |
354 | return length; |
355 | } | |
356 | ||
1da177e4 LT |
357 | static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, |
358 | size_t count, loff_t *ppos) | |
359 | { | |
360 | char tmpbuf[TMPBUFLEN]; | |
361 | ssize_t length; | |
362 | ||
363 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); | |
364 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
365 | } | |
366 | ||
367 | static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf, | |
368 | size_t count, loff_t *ppos) | |
369 | { | |
370 | char *page; | |
371 | ssize_t length; | |
372 | unsigned int new_value; | |
373 | ||
374 | length = task_has_security(current, SECURITY__SETCHECKREQPROT); | |
375 | if (length) | |
376 | return length; | |
377 | ||
bfd51626 | 378 | if (count >= PAGE_SIZE) |
1da177e4 LT |
379 | return -ENOMEM; |
380 | if (*ppos != 0) { | |
381 | /* No partial writes. */ | |
382 | return -EINVAL; | |
383 | } | |
384 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
385 | if (!page) | |
386 | return -ENOMEM; | |
387 | length = -EFAULT; | |
388 | if (copy_from_user(page, buf, count)) | |
389 | goto out; | |
390 | ||
391 | length = -EINVAL; | |
392 | if (sscanf(page, "%u", &new_value) != 1) | |
393 | goto out; | |
394 | ||
395 | selinux_checkreqprot = new_value ? 1 : 0; | |
396 | length = count; | |
397 | out: | |
398 | free_page((unsigned long) page); | |
399 | return length; | |
400 | } | |
9c2e08c5 | 401 | static const struct file_operations sel_checkreqprot_ops = { |
1da177e4 LT |
402 | .read = sel_read_checkreqprot, |
403 | .write = sel_write_checkreqprot, | |
404 | }; | |
405 | ||
4e5ab4cb JM |
406 | static ssize_t sel_read_compat_net(struct file *filp, char __user *buf, |
407 | size_t count, loff_t *ppos) | |
408 | { | |
409 | char tmpbuf[TMPBUFLEN]; | |
410 | ssize_t length; | |
411 | ||
412 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net); | |
413 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
414 | } | |
415 | ||
416 | static ssize_t sel_write_compat_net(struct file * file, const char __user * buf, | |
417 | size_t count, loff_t *ppos) | |
418 | { | |
419 | char *page; | |
420 | ssize_t length; | |
421 | int new_value; | |
422 | ||
423 | length = task_has_security(current, SECURITY__LOAD_POLICY); | |
424 | if (length) | |
425 | return length; | |
426 | ||
427 | if (count >= PAGE_SIZE) | |
428 | return -ENOMEM; | |
429 | if (*ppos != 0) { | |
430 | /* No partial writes. */ | |
431 | return -EINVAL; | |
432 | } | |
433 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
434 | if (!page) | |
435 | return -ENOMEM; | |
436 | length = -EFAULT; | |
437 | if (copy_from_user(page, buf, count)) | |
438 | goto out; | |
439 | ||
440 | length = -EINVAL; | |
441 | if (sscanf(page, "%d", &new_value) != 1) | |
442 | goto out; | |
443 | ||
444 | selinux_compat_net = new_value ? 1 : 0; | |
445 | length = count; | |
446 | out: | |
447 | free_page((unsigned long) page); | |
448 | return length; | |
449 | } | |
9c2e08c5 | 450 | static const struct file_operations sel_compat_net_ops = { |
4e5ab4cb JM |
451 | .read = sel_read_compat_net, |
452 | .write = sel_write_compat_net, | |
453 | }; | |
454 | ||
1da177e4 LT |
455 | /* |
456 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c | |
457 | */ | |
458 | static ssize_t sel_write_access(struct file * file, char *buf, size_t size); | |
459 | static ssize_t sel_write_create(struct file * file, char *buf, size_t size); | |
460 | static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size); | |
461 | static ssize_t sel_write_user(struct file * file, char *buf, size_t size); | |
462 | static ssize_t sel_write_member(struct file * file, char *buf, size_t size); | |
463 | ||
464 | static ssize_t (*write_op[])(struct file *, char *, size_t) = { | |
465 | [SEL_ACCESS] = sel_write_access, | |
466 | [SEL_CREATE] = sel_write_create, | |
467 | [SEL_RELABEL] = sel_write_relabel, | |
468 | [SEL_USER] = sel_write_user, | |
469 | [SEL_MEMBER] = sel_write_member, | |
ce9982d0 | 470 | [SEL_CONTEXT] = sel_write_context, |
1da177e4 LT |
471 | }; |
472 | ||
473 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) | |
474 | { | |
3d5ff529 | 475 | ino_t ino = file->f_path.dentry->d_inode->i_ino; |
1da177e4 LT |
476 | char *data; |
477 | ssize_t rv; | |
478 | ||
6e20a64a | 479 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
1da177e4 LT |
480 | return -EINVAL; |
481 | ||
482 | data = simple_transaction_get(file, buf, size); | |
483 | if (IS_ERR(data)) | |
484 | return PTR_ERR(data); | |
485 | ||
486 | rv = write_op[ino](file, data, size); | |
487 | if (rv>0) { | |
488 | simple_transaction_set(file, rv); | |
489 | rv = size; | |
490 | } | |
491 | return rv; | |
492 | } | |
493 | ||
9c2e08c5 | 494 | static const struct file_operations transaction_ops = { |
1da177e4 LT |
495 | .write = selinux_transaction_write, |
496 | .read = simple_transaction_read, | |
497 | .release = simple_transaction_release, | |
498 | }; | |
499 | ||
500 | /* | |
501 | * payload - write methods | |
502 | * If the method has a response, the response should be put in buf, | |
503 | * and the length returned. Otherwise return 0 or and -error. | |
504 | */ | |
505 | ||
506 | static ssize_t sel_write_access(struct file * file, char *buf, size_t size) | |
507 | { | |
508 | char *scon, *tcon; | |
509 | u32 ssid, tsid; | |
510 | u16 tclass; | |
511 | u32 req; | |
512 | struct av_decision avd; | |
513 | ssize_t length; | |
514 | ||
515 | length = task_has_security(current, SECURITY__COMPUTE_AV); | |
516 | if (length) | |
517 | return length; | |
518 | ||
519 | length = -ENOMEM; | |
89d155ef | 520 | scon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
521 | if (!scon) |
522 | return length; | |
1da177e4 | 523 | |
89d155ef | 524 | tcon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
525 | if (!tcon) |
526 | goto out; | |
1da177e4 LT |
527 | |
528 | length = -EINVAL; | |
529 | if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4) | |
530 | goto out2; | |
531 | ||
532 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); | |
533 | if (length < 0) | |
534 | goto out2; | |
535 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); | |
536 | if (length < 0) | |
537 | goto out2; | |
538 | ||
539 | length = security_compute_av(ssid, tsid, tclass, req, &avd); | |
540 | if (length < 0) | |
541 | goto out2; | |
542 | ||
543 | length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, | |
544 | "%x %x %x %x %u", | |
545 | avd.allowed, avd.decided, | |
546 | avd.auditallow, avd.auditdeny, | |
547 | avd.seqno); | |
548 | out2: | |
549 | kfree(tcon); | |
550 | out: | |
551 | kfree(scon); | |
552 | return length; | |
553 | } | |
554 | ||
555 | static ssize_t sel_write_create(struct file * file, char *buf, size_t size) | |
556 | { | |
557 | char *scon, *tcon; | |
558 | u32 ssid, tsid, newsid; | |
559 | u16 tclass; | |
560 | ssize_t length; | |
561 | char *newcon; | |
562 | u32 len; | |
563 | ||
564 | length = task_has_security(current, SECURITY__COMPUTE_CREATE); | |
565 | if (length) | |
566 | return length; | |
567 | ||
568 | length = -ENOMEM; | |
89d155ef | 569 | scon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
570 | if (!scon) |
571 | return length; | |
1da177e4 | 572 | |
89d155ef | 573 | tcon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
574 | if (!tcon) |
575 | goto out; | |
1da177e4 LT |
576 | |
577 | length = -EINVAL; | |
578 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) | |
579 | goto out2; | |
580 | ||
581 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); | |
582 | if (length < 0) | |
583 | goto out2; | |
584 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); | |
585 | if (length < 0) | |
586 | goto out2; | |
587 | ||
588 | length = security_transition_sid(ssid, tsid, tclass, &newsid); | |
589 | if (length < 0) | |
590 | goto out2; | |
591 | ||
592 | length = security_sid_to_context(newsid, &newcon, &len); | |
593 | if (length < 0) | |
594 | goto out2; | |
595 | ||
596 | if (len > SIMPLE_TRANSACTION_LIMIT) { | |
597 | printk(KERN_ERR "%s: context size (%u) exceeds payload " | |
598 | "max\n", __FUNCTION__, len); | |
599 | length = -ERANGE; | |
600 | goto out3; | |
601 | } | |
602 | ||
603 | memcpy(buf, newcon, len); | |
604 | length = len; | |
605 | out3: | |
606 | kfree(newcon); | |
607 | out2: | |
608 | kfree(tcon); | |
609 | out: | |
610 | kfree(scon); | |
611 | return length; | |
612 | } | |
613 | ||
614 | static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size) | |
615 | { | |
616 | char *scon, *tcon; | |
617 | u32 ssid, tsid, newsid; | |
618 | u16 tclass; | |
619 | ssize_t length; | |
620 | char *newcon; | |
621 | u32 len; | |
622 | ||
623 | length = task_has_security(current, SECURITY__COMPUTE_RELABEL); | |
624 | if (length) | |
625 | return length; | |
626 | ||
627 | length = -ENOMEM; | |
89d155ef | 628 | scon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
629 | if (!scon) |
630 | return length; | |
1da177e4 | 631 | |
89d155ef | 632 | tcon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
633 | if (!tcon) |
634 | goto out; | |
1da177e4 LT |
635 | |
636 | length = -EINVAL; | |
637 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) | |
638 | goto out2; | |
639 | ||
640 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); | |
641 | if (length < 0) | |
642 | goto out2; | |
643 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); | |
644 | if (length < 0) | |
645 | goto out2; | |
646 | ||
647 | length = security_change_sid(ssid, tsid, tclass, &newsid); | |
648 | if (length < 0) | |
649 | goto out2; | |
650 | ||
651 | length = security_sid_to_context(newsid, &newcon, &len); | |
652 | if (length < 0) | |
653 | goto out2; | |
654 | ||
655 | if (len > SIMPLE_TRANSACTION_LIMIT) { | |
656 | length = -ERANGE; | |
657 | goto out3; | |
658 | } | |
659 | ||
660 | memcpy(buf, newcon, len); | |
661 | length = len; | |
662 | out3: | |
663 | kfree(newcon); | |
664 | out2: | |
665 | kfree(tcon); | |
666 | out: | |
667 | kfree(scon); | |
668 | return length; | |
669 | } | |
670 | ||
671 | static ssize_t sel_write_user(struct file * file, char *buf, size_t size) | |
672 | { | |
673 | char *con, *user, *ptr; | |
674 | u32 sid, *sids; | |
675 | ssize_t length; | |
676 | char *newcon; | |
677 | int i, rc; | |
678 | u32 len, nsids; | |
679 | ||
680 | length = task_has_security(current, SECURITY__COMPUTE_USER); | |
681 | if (length) | |
682 | return length; | |
683 | ||
684 | length = -ENOMEM; | |
89d155ef | 685 | con = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
686 | if (!con) |
687 | return length; | |
1da177e4 | 688 | |
89d155ef | 689 | user = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
690 | if (!user) |
691 | goto out; | |
1da177e4 LT |
692 | |
693 | length = -EINVAL; | |
694 | if (sscanf(buf, "%s %s", con, user) != 2) | |
695 | goto out2; | |
696 | ||
697 | length = security_context_to_sid(con, strlen(con)+1, &sid); | |
698 | if (length < 0) | |
699 | goto out2; | |
700 | ||
701 | length = security_get_user_sids(sid, user, &sids, &nsids); | |
702 | if (length < 0) | |
703 | goto out2; | |
704 | ||
705 | length = sprintf(buf, "%u", nsids) + 1; | |
706 | ptr = buf + length; | |
707 | for (i = 0; i < nsids; i++) { | |
708 | rc = security_sid_to_context(sids[i], &newcon, &len); | |
709 | if (rc) { | |
710 | length = rc; | |
711 | goto out3; | |
712 | } | |
713 | if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { | |
714 | kfree(newcon); | |
715 | length = -ERANGE; | |
716 | goto out3; | |
717 | } | |
718 | memcpy(ptr, newcon, len); | |
719 | kfree(newcon); | |
720 | ptr += len; | |
721 | length += len; | |
722 | } | |
723 | out3: | |
724 | kfree(sids); | |
725 | out2: | |
726 | kfree(user); | |
727 | out: | |
728 | kfree(con); | |
729 | return length; | |
730 | } | |
731 | ||
732 | static ssize_t sel_write_member(struct file * file, char *buf, size_t size) | |
733 | { | |
734 | char *scon, *tcon; | |
735 | u32 ssid, tsid, newsid; | |
736 | u16 tclass; | |
737 | ssize_t length; | |
738 | char *newcon; | |
739 | u32 len; | |
740 | ||
741 | length = task_has_security(current, SECURITY__COMPUTE_MEMBER); | |
742 | if (length) | |
743 | return length; | |
744 | ||
745 | length = -ENOMEM; | |
89d155ef | 746 | scon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
747 | if (!scon) |
748 | return length; | |
1da177e4 | 749 | |
89d155ef | 750 | tcon = kzalloc(size+1, GFP_KERNEL); |
1da177e4 LT |
751 | if (!tcon) |
752 | goto out; | |
1da177e4 LT |
753 | |
754 | length = -EINVAL; | |
755 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) | |
756 | goto out2; | |
757 | ||
758 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); | |
759 | if (length < 0) | |
760 | goto out2; | |
761 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); | |
762 | if (length < 0) | |
763 | goto out2; | |
764 | ||
765 | length = security_member_sid(ssid, tsid, tclass, &newsid); | |
766 | if (length < 0) | |
767 | goto out2; | |
768 | ||
769 | length = security_sid_to_context(newsid, &newcon, &len); | |
770 | if (length < 0) | |
771 | goto out2; | |
772 | ||
773 | if (len > SIMPLE_TRANSACTION_LIMIT) { | |
774 | printk(KERN_ERR "%s: context size (%u) exceeds payload " | |
775 | "max\n", __FUNCTION__, len); | |
776 | length = -ERANGE; | |
777 | goto out3; | |
778 | } | |
779 | ||
780 | memcpy(buf, newcon, len); | |
781 | length = len; | |
782 | out3: | |
783 | kfree(newcon); | |
784 | out2: | |
785 | kfree(tcon); | |
786 | out: | |
787 | kfree(scon); | |
788 | return length; | |
789 | } | |
790 | ||
791 | static struct inode *sel_make_inode(struct super_block *sb, int mode) | |
792 | { | |
793 | struct inode *ret = new_inode(sb); | |
794 | ||
795 | if (ret) { | |
796 | ret->i_mode = mode; | |
797 | ret->i_uid = ret->i_gid = 0; | |
1da177e4 LT |
798 | ret->i_blocks = 0; |
799 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; | |
800 | } | |
801 | return ret; | |
802 | } | |
803 | ||
1da177e4 LT |
804 | static ssize_t sel_read_bool(struct file *filep, char __user *buf, |
805 | size_t count, loff_t *ppos) | |
806 | { | |
807 | char *page = NULL; | |
808 | ssize_t length; | |
1da177e4 LT |
809 | ssize_t ret; |
810 | int cur_enforcing; | |
811 | struct inode *inode; | |
812 | ||
bb003079 | 813 | mutex_lock(&sel_mutex); |
1da177e4 LT |
814 | |
815 | ret = -EFAULT; | |
816 | ||
817 | /* check to see if this file has been deleted */ | |
818 | if (!filep->f_op) | |
819 | goto out; | |
820 | ||
bfd51626 | 821 | if (count > PAGE_SIZE) { |
1da177e4 LT |
822 | ret = -EINVAL; |
823 | goto out; | |
824 | } | |
825 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) { | |
826 | ret = -ENOMEM; | |
827 | goto out; | |
828 | } | |
829 | ||
3d5ff529 | 830 | inode = filep->f_path.dentry->d_inode; |
bce34bc0 | 831 | cur_enforcing = security_get_bool_value(inode->i_ino&SEL_INO_MASK); |
1da177e4 LT |
832 | if (cur_enforcing < 0) { |
833 | ret = cur_enforcing; | |
834 | goto out; | |
835 | } | |
836 | ||
837 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, | |
bce34bc0 | 838 | bool_pending_values[inode->i_ino&SEL_INO_MASK]); |
68bdcf28 | 839 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
1da177e4 | 840 | out: |
bb003079 | 841 | mutex_unlock(&sel_mutex); |
1da177e4 LT |
842 | if (page) |
843 | free_page((unsigned long)page); | |
844 | return ret; | |
845 | } | |
846 | ||
847 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, | |
848 | size_t count, loff_t *ppos) | |
849 | { | |
850 | char *page = NULL; | |
851 | ssize_t length = -EFAULT; | |
852 | int new_value; | |
853 | struct inode *inode; | |
854 | ||
bb003079 | 855 | mutex_lock(&sel_mutex); |
1da177e4 LT |
856 | |
857 | length = task_has_security(current, SECURITY__SETBOOL); | |
858 | if (length) | |
859 | goto out; | |
860 | ||
861 | /* check to see if this file has been deleted */ | |
862 | if (!filep->f_op) | |
863 | goto out; | |
864 | ||
bfd51626 | 865 | if (count >= PAGE_SIZE) { |
1da177e4 LT |
866 | length = -ENOMEM; |
867 | goto out; | |
868 | } | |
869 | if (*ppos != 0) { | |
870 | /* No partial writes. */ | |
871 | goto out; | |
872 | } | |
873 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
874 | if (!page) { | |
875 | length = -ENOMEM; | |
876 | goto out; | |
877 | } | |
878 | ||
879 | if (copy_from_user(page, buf, count)) | |
880 | goto out; | |
881 | ||
882 | length = -EINVAL; | |
883 | if (sscanf(page, "%d", &new_value) != 1) | |
884 | goto out; | |
885 | ||
886 | if (new_value) | |
887 | new_value = 1; | |
888 | ||
3d5ff529 | 889 | inode = filep->f_path.dentry->d_inode; |
bce34bc0 | 890 | bool_pending_values[inode->i_ino&SEL_INO_MASK] = new_value; |
1da177e4 LT |
891 | length = count; |
892 | ||
893 | out: | |
bb003079 | 894 | mutex_unlock(&sel_mutex); |
1da177e4 LT |
895 | if (page) |
896 | free_page((unsigned long) page); | |
897 | return length; | |
898 | } | |
899 | ||
9c2e08c5 | 900 | static const struct file_operations sel_bool_ops = { |
1da177e4 LT |
901 | .read = sel_read_bool, |
902 | .write = sel_write_bool, | |
903 | }; | |
904 | ||
905 | static ssize_t sel_commit_bools_write(struct file *filep, | |
906 | const char __user *buf, | |
907 | size_t count, loff_t *ppos) | |
908 | { | |
909 | char *page = NULL; | |
910 | ssize_t length = -EFAULT; | |
911 | int new_value; | |
912 | ||
bb003079 | 913 | mutex_lock(&sel_mutex); |
1da177e4 LT |
914 | |
915 | length = task_has_security(current, SECURITY__SETBOOL); | |
916 | if (length) | |
917 | goto out; | |
918 | ||
919 | /* check to see if this file has been deleted */ | |
920 | if (!filep->f_op) | |
921 | goto out; | |
922 | ||
bfd51626 | 923 | if (count >= PAGE_SIZE) { |
1da177e4 LT |
924 | length = -ENOMEM; |
925 | goto out; | |
926 | } | |
927 | if (*ppos != 0) { | |
928 | /* No partial writes. */ | |
929 | goto out; | |
930 | } | |
931 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
932 | if (!page) { | |
933 | length = -ENOMEM; | |
934 | goto out; | |
935 | } | |
936 | ||
937 | if (copy_from_user(page, buf, count)) | |
938 | goto out; | |
939 | ||
940 | length = -EINVAL; | |
941 | if (sscanf(page, "%d", &new_value) != 1) | |
942 | goto out; | |
943 | ||
20c19e41 | 944 | if (new_value && bool_pending_values) { |
1da177e4 LT |
945 | security_set_bools(bool_num, bool_pending_values); |
946 | } | |
947 | ||
948 | length = count; | |
949 | ||
950 | out: | |
bb003079 | 951 | mutex_unlock(&sel_mutex); |
1da177e4 LT |
952 | if (page) |
953 | free_page((unsigned long) page); | |
954 | return length; | |
955 | } | |
956 | ||
9c2e08c5 | 957 | static const struct file_operations sel_commit_bools_ops = { |
1da177e4 LT |
958 | .write = sel_commit_bools_write, |
959 | }; | |
960 | ||
0c92d7c7 CP |
961 | /* partial revoke() from fs/proc/generic.c proc_kill_inodes */ |
962 | static void sel_remove_entries(struct dentry *de) | |
1da177e4 LT |
963 | { |
964 | struct list_head *p, *node; | |
965 | struct super_block *sb = de->d_sb; | |
966 | ||
967 | spin_lock(&dcache_lock); | |
968 | node = de->d_subdirs.next; | |
969 | while (node != &de->d_subdirs) { | |
5160ee6f | 970 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
1da177e4 LT |
971 | list_del_init(node); |
972 | ||
973 | if (d->d_inode) { | |
974 | d = dget_locked(d); | |
975 | spin_unlock(&dcache_lock); | |
976 | d_delete(d); | |
977 | simple_unlink(de->d_inode, d); | |
978 | dput(d); | |
979 | spin_lock(&dcache_lock); | |
980 | } | |
981 | node = de->d_subdirs.next; | |
982 | } | |
983 | ||
984 | spin_unlock(&dcache_lock); | |
985 | ||
986 | file_list_lock(); | |
987 | list_for_each(p, &sb->s_files) { | |
2f512016 | 988 | struct file * filp = list_entry(p, struct file, f_u.fu_list); |
3d5ff529 | 989 | struct dentry * dentry = filp->f_path.dentry; |
1da177e4 LT |
990 | |
991 | if (dentry->d_parent != de) { | |
992 | continue; | |
993 | } | |
994 | filp->f_op = NULL; | |
995 | } | |
996 | file_list_unlock(); | |
997 | } | |
998 | ||
999 | #define BOOL_DIR_NAME "booleans" | |
1000 | ||
1001 | static int sel_make_bools(void) | |
1002 | { | |
1003 | int i, ret = 0; | |
1004 | ssize_t len; | |
1005 | struct dentry *dentry = NULL; | |
1006 | struct dentry *dir = bool_dir; | |
1007 | struct inode *inode = NULL; | |
1008 | struct inode_security_struct *isec; | |
1009 | char **names = NULL, *page; | |
1010 | int num; | |
1011 | int *values = NULL; | |
1012 | u32 sid; | |
1013 | ||
1014 | /* remove any existing files */ | |
9a5f04bf | 1015 | kfree(bool_pending_values); |
20c19e41 | 1016 | bool_pending_values = NULL; |
1da177e4 | 1017 | |
0c92d7c7 | 1018 | sel_remove_entries(dir); |
1da177e4 LT |
1019 | |
1020 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) | |
1021 | return -ENOMEM; | |
1022 | ||
1023 | ret = security_get_bools(&num, &names, &values); | |
1024 | if (ret != 0) | |
1025 | goto out; | |
1026 | ||
1027 | for (i = 0; i < num; i++) { | |
1028 | dentry = d_alloc_name(dir, names[i]); | |
1029 | if (!dentry) { | |
1030 | ret = -ENOMEM; | |
1031 | goto err; | |
1032 | } | |
1033 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); | |
1034 | if (!inode) { | |
1035 | ret = -ENOMEM; | |
1036 | goto err; | |
1037 | } | |
1038 | ||
1039 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); | |
1040 | if (len < 0) { | |
1041 | ret = -EINVAL; | |
1042 | goto err; | |
1043 | } else if (len >= PAGE_SIZE) { | |
1044 | ret = -ENAMETOOLONG; | |
1045 | goto err; | |
1046 | } | |
1047 | isec = (struct inode_security_struct*)inode->i_security; | |
1048 | if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid))) | |
1049 | goto err; | |
1050 | isec->sid = sid; | |
1051 | isec->initialized = 1; | |
1052 | inode->i_fop = &sel_bool_ops; | |
bce34bc0 | 1053 | inode->i_ino = i|SEL_BOOL_INO_OFFSET; |
1da177e4 LT |
1054 | d_add(dentry, inode); |
1055 | } | |
1056 | bool_num = num; | |
1057 | bool_pending_values = values; | |
1058 | out: | |
1059 | free_page((unsigned long)page); | |
1060 | if (names) { | |
9a5f04bf JJ |
1061 | for (i = 0; i < num; i++) |
1062 | kfree(names[i]); | |
1da177e4 LT |
1063 | kfree(names); |
1064 | } | |
1065 | return ret; | |
1066 | err: | |
20c19e41 | 1067 | kfree(values); |
0c92d7c7 | 1068 | sel_remove_entries(dir); |
1da177e4 LT |
1069 | ret = -ENOMEM; |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | #define NULL_FILE_NAME "null" | |
1074 | ||
1075 | struct dentry *selinux_null = NULL; | |
1076 | ||
1077 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, | |
1078 | size_t count, loff_t *ppos) | |
1079 | { | |
1080 | char tmpbuf[TMPBUFLEN]; | |
1081 | ssize_t length; | |
1082 | ||
1083 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); | |
1084 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
1085 | } | |
1086 | ||
1087 | static ssize_t sel_write_avc_cache_threshold(struct file * file, | |
1088 | const char __user * buf, | |
1089 | size_t count, loff_t *ppos) | |
1090 | ||
1091 | { | |
1092 | char *page; | |
1093 | ssize_t ret; | |
1094 | int new_value; | |
1095 | ||
bfd51626 | 1096 | if (count >= PAGE_SIZE) { |
1da177e4 LT |
1097 | ret = -ENOMEM; |
1098 | goto out; | |
1099 | } | |
1100 | ||
1101 | if (*ppos != 0) { | |
1102 | /* No partial writes. */ | |
1103 | ret = -EINVAL; | |
1104 | goto out; | |
1105 | } | |
1106 | ||
1107 | page = (char*)get_zeroed_page(GFP_KERNEL); | |
1108 | if (!page) { | |
1109 | ret = -ENOMEM; | |
1110 | goto out; | |
1111 | } | |
1112 | ||
1113 | if (copy_from_user(page, buf, count)) { | |
1114 | ret = -EFAULT; | |
1115 | goto out_free; | |
1116 | } | |
1117 | ||
1118 | if (sscanf(page, "%u", &new_value) != 1) { | |
1119 | ret = -EINVAL; | |
1120 | goto out; | |
1121 | } | |
1122 | ||
1123 | if (new_value != avc_cache_threshold) { | |
1124 | ret = task_has_security(current, SECURITY__SETSECPARAM); | |
1125 | if (ret) | |
1126 | goto out_free; | |
1127 | avc_cache_threshold = new_value; | |
1128 | } | |
1129 | ret = count; | |
1130 | out_free: | |
1131 | free_page((unsigned long)page); | |
1132 | out: | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, | |
1137 | size_t count, loff_t *ppos) | |
1138 | { | |
1139 | char *page; | |
1140 | ssize_t ret = 0; | |
1141 | ||
1142 | page = (char *)__get_free_page(GFP_KERNEL); | |
1143 | if (!page) { | |
1144 | ret = -ENOMEM; | |
1145 | goto out; | |
1146 | } | |
1147 | ret = avc_get_hash_stats(page); | |
1148 | if (ret >= 0) | |
1149 | ret = simple_read_from_buffer(buf, count, ppos, page, ret); | |
1150 | free_page((unsigned long)page); | |
1151 | out: | |
1152 | return ret; | |
1153 | } | |
1154 | ||
9c2e08c5 | 1155 | static const struct file_operations sel_avc_cache_threshold_ops = { |
1da177e4 LT |
1156 | .read = sel_read_avc_cache_threshold, |
1157 | .write = sel_write_avc_cache_threshold, | |
1158 | }; | |
1159 | ||
9c2e08c5 | 1160 | static const struct file_operations sel_avc_hash_stats_ops = { |
1da177e4 LT |
1161 | .read = sel_read_avc_hash_stats, |
1162 | }; | |
1163 | ||
1164 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
1165 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) | |
1166 | { | |
1167 | int cpu; | |
1168 | ||
1169 | for (cpu = *idx; cpu < NR_CPUS; ++cpu) { | |
1170 | if (!cpu_possible(cpu)) | |
1171 | continue; | |
1172 | *idx = cpu + 1; | |
1173 | return &per_cpu(avc_cache_stats, cpu); | |
1174 | } | |
1175 | return NULL; | |
1176 | } | |
1177 | ||
1178 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) | |
1179 | { | |
1180 | loff_t n = *pos - 1; | |
1181 | ||
1182 | if (*pos == 0) | |
1183 | return SEQ_START_TOKEN; | |
1184 | ||
1185 | return sel_avc_get_stat_idx(&n); | |
1186 | } | |
1187 | ||
1188 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1189 | { | |
1190 | return sel_avc_get_stat_idx(pos); | |
1191 | } | |
1192 | ||
1193 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) | |
1194 | { | |
1195 | struct avc_cache_stats *st = v; | |
1196 | ||
1197 | if (v == SEQ_START_TOKEN) | |
1198 | seq_printf(seq, "lookups hits misses allocations reclaims " | |
1199 | "frees\n"); | |
1200 | else | |
1201 | seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, | |
1202 | st->hits, st->misses, st->allocations, | |
1203 | st->reclaims, st->frees); | |
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) | |
1208 | { } | |
1209 | ||
1210 | static struct seq_operations sel_avc_cache_stats_seq_ops = { | |
1211 | .start = sel_avc_stats_seq_start, | |
1212 | .next = sel_avc_stats_seq_next, | |
1213 | .show = sel_avc_stats_seq_show, | |
1214 | .stop = sel_avc_stats_seq_stop, | |
1215 | }; | |
1216 | ||
1217 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) | |
1218 | { | |
1219 | return seq_open(file, &sel_avc_cache_stats_seq_ops); | |
1220 | } | |
1221 | ||
9c2e08c5 | 1222 | static const struct file_operations sel_avc_cache_stats_ops = { |
1da177e4 LT |
1223 | .open = sel_open_avc_cache_stats, |
1224 | .read = seq_read, | |
1225 | .llseek = seq_lseek, | |
1226 | .release = seq_release, | |
1227 | }; | |
1228 | #endif | |
1229 | ||
1230 | static int sel_make_avc_files(struct dentry *dir) | |
1231 | { | |
1232 | int i, ret = 0; | |
1233 | static struct tree_descr files[] = { | |
1234 | { "cache_threshold", | |
1235 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, | |
1236 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, | |
1237 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS | |
1238 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, | |
1239 | #endif | |
1240 | }; | |
1241 | ||
6e20a64a | 1242 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
1da177e4 LT |
1243 | struct inode *inode; |
1244 | struct dentry *dentry; | |
1245 | ||
1246 | dentry = d_alloc_name(dir, files[i].name); | |
1247 | if (!dentry) { | |
1248 | ret = -ENOMEM; | |
d6aafa65 | 1249 | goto out; |
1da177e4 LT |
1250 | } |
1251 | ||
1252 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); | |
1253 | if (!inode) { | |
1254 | ret = -ENOMEM; | |
d6aafa65 | 1255 | goto out; |
1da177e4 LT |
1256 | } |
1257 | inode->i_fop = files[i].ops; | |
6174eafc | 1258 | inode->i_ino = ++sel_last_ino; |
1da177e4 LT |
1259 | d_add(dentry, inode); |
1260 | } | |
1261 | out: | |
1262 | return ret; | |
1da177e4 LT |
1263 | } |
1264 | ||
f0ee2e46 JC |
1265 | static ssize_t sel_read_initcon(struct file * file, char __user *buf, |
1266 | size_t count, loff_t *ppos) | |
1267 | { | |
1268 | struct inode *inode; | |
1269 | char *con; | |
1270 | u32 sid, len; | |
1271 | ssize_t ret; | |
1272 | ||
1273 | inode = file->f_path.dentry->d_inode; | |
1274 | sid = inode->i_ino&SEL_INO_MASK; | |
1275 | ret = security_sid_to_context(sid, &con, &len); | |
1276 | if (ret < 0) | |
1277 | return ret; | |
1278 | ||
1279 | ret = simple_read_from_buffer(buf, count, ppos, con, len); | |
1280 | kfree(con); | |
1281 | return ret; | |
1282 | } | |
1283 | ||
1284 | static const struct file_operations sel_initcon_ops = { | |
1285 | .read = sel_read_initcon, | |
1286 | }; | |
1287 | ||
1288 | static int sel_make_initcon_files(struct dentry *dir) | |
1289 | { | |
1290 | int i, ret = 0; | |
1291 | ||
1292 | for (i = 1; i <= SECINITSID_NUM; i++) { | |
1293 | struct inode *inode; | |
1294 | struct dentry *dentry; | |
1295 | dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); | |
1296 | if (!dentry) { | |
1297 | ret = -ENOMEM; | |
1298 | goto out; | |
1299 | } | |
1300 | ||
1301 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); | |
1302 | if (!inode) { | |
1303 | ret = -ENOMEM; | |
1304 | goto out; | |
1305 | } | |
1306 | inode->i_fop = &sel_initcon_ops; | |
1307 | inode->i_ino = i|SEL_INITCON_INO_OFFSET; | |
1308 | d_add(dentry, inode); | |
1309 | } | |
1310 | out: | |
1311 | return ret; | |
1312 | } | |
1313 | ||
e47c8fc5 CP |
1314 | static inline unsigned int sel_div(unsigned long a, unsigned long b) |
1315 | { | |
1316 | return a / b - (a % b < 0); | |
1317 | } | |
1318 | ||
1319 | static inline unsigned long sel_class_to_ino(u16 class) | |
1320 | { | |
1321 | return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; | |
1322 | } | |
1323 | ||
1324 | static inline u16 sel_ino_to_class(unsigned long ino) | |
1325 | { | |
1326 | return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1); | |
1327 | } | |
1328 | ||
1329 | static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) | |
1330 | { | |
1331 | return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; | |
1332 | } | |
1333 | ||
1334 | static inline u32 sel_ino_to_perm(unsigned long ino) | |
1335 | { | |
1336 | return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); | |
1337 | } | |
1338 | ||
1339 | static ssize_t sel_read_class(struct file * file, char __user *buf, | |
1340 | size_t count, loff_t *ppos) | |
1341 | { | |
1342 | ssize_t rc, len; | |
1343 | char *page; | |
1344 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; | |
1345 | ||
1346 | page = (char *)__get_free_page(GFP_KERNEL); | |
1347 | if (!page) { | |
1348 | rc = -ENOMEM; | |
1349 | goto out; | |
1350 | } | |
1351 | ||
1352 | len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino)); | |
1353 | rc = simple_read_from_buffer(buf, count, ppos, page, len); | |
1354 | free_page((unsigned long)page); | |
1355 | out: | |
1356 | return rc; | |
1357 | } | |
1358 | ||
1359 | static const struct file_operations sel_class_ops = { | |
1360 | .read = sel_read_class, | |
1361 | }; | |
1362 | ||
1363 | static ssize_t sel_read_perm(struct file * file, char __user *buf, | |
1364 | size_t count, loff_t *ppos) | |
1365 | { | |
1366 | ssize_t rc, len; | |
1367 | char *page; | |
1368 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; | |
1369 | ||
1370 | page = (char *)__get_free_page(GFP_KERNEL); | |
1371 | if (!page) { | |
1372 | rc = -ENOMEM; | |
1373 | goto out; | |
1374 | } | |
1375 | ||
1376 | len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino)); | |
1377 | rc = simple_read_from_buffer(buf, count, ppos, page, len); | |
1378 | free_page((unsigned long)page); | |
1379 | out: | |
1380 | return rc; | |
1381 | } | |
1382 | ||
1383 | static const struct file_operations sel_perm_ops = { | |
1384 | .read = sel_read_perm, | |
1385 | }; | |
1386 | ||
1387 | static int sel_make_perm_files(char *objclass, int classvalue, | |
1388 | struct dentry *dir) | |
1389 | { | |
1390 | int i, rc = 0, nperms; | |
1391 | char **perms; | |
1392 | ||
1393 | rc = security_get_permissions(objclass, &perms, &nperms); | |
1394 | if (rc) | |
1395 | goto out; | |
1396 | ||
1397 | for (i = 0; i < nperms; i++) { | |
1398 | struct inode *inode; | |
1399 | struct dentry *dentry; | |
1400 | ||
1401 | dentry = d_alloc_name(dir, perms[i]); | |
1402 | if (!dentry) { | |
1403 | rc = -ENOMEM; | |
1404 | goto out1; | |
1405 | } | |
1406 | ||
1407 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); | |
1408 | if (!inode) { | |
1409 | rc = -ENOMEM; | |
1410 | goto out1; | |
1411 | } | |
1412 | inode->i_fop = &sel_perm_ops; | |
1413 | /* i+1 since perm values are 1-indexed */ | |
1414 | inode->i_ino = sel_perm_to_ino(classvalue, i+1); | |
1415 | d_add(dentry, inode); | |
1416 | } | |
1417 | ||
1418 | out1: | |
1419 | for (i = 0; i < nperms; i++) | |
1420 | kfree(perms[i]); | |
1421 | kfree(perms); | |
1422 | out: | |
1423 | return rc; | |
1424 | } | |
1425 | ||
1426 | static int sel_make_class_dir_entries(char *classname, int index, | |
1427 | struct dentry *dir) | |
1428 | { | |
1429 | struct dentry *dentry = NULL; | |
1430 | struct inode *inode = NULL; | |
1431 | int rc; | |
1432 | ||
1433 | dentry = d_alloc_name(dir, "index"); | |
1434 | if (!dentry) { | |
1435 | rc = -ENOMEM; | |
1436 | goto out; | |
1437 | } | |
1438 | ||
1439 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); | |
1440 | if (!inode) { | |
1441 | rc = -ENOMEM; | |
1442 | goto out; | |
1443 | } | |
1444 | ||
1445 | inode->i_fop = &sel_class_ops; | |
1446 | inode->i_ino = sel_class_to_ino(index); | |
1447 | d_add(dentry, inode); | |
1448 | ||
1449 | dentry = d_alloc_name(dir, "perms"); | |
1450 | if (!dentry) { | |
1451 | rc = -ENOMEM; | |
1452 | goto out; | |
1453 | } | |
1454 | ||
1455 | rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino); | |
1456 | if (rc) | |
1457 | goto out; | |
1458 | ||
1459 | rc = sel_make_perm_files(classname, index, dentry); | |
1460 | ||
1461 | out: | |
1462 | return rc; | |
1463 | } | |
1464 | ||
1465 | static void sel_remove_classes(void) | |
1466 | { | |
1467 | struct list_head *class_node; | |
1468 | ||
1469 | list_for_each(class_node, &class_dir->d_subdirs) { | |
1470 | struct dentry *class_subdir = list_entry(class_node, | |
1471 | struct dentry, d_u.d_child); | |
1472 | struct list_head *class_subdir_node; | |
1473 | ||
1474 | list_for_each(class_subdir_node, &class_subdir->d_subdirs) { | |
1475 | struct dentry *d = list_entry(class_subdir_node, | |
1476 | struct dentry, d_u.d_child); | |
1477 | ||
1478 | if (d->d_inode) | |
1479 | if (d->d_inode->i_mode & S_IFDIR) | |
1480 | sel_remove_entries(d); | |
1481 | } | |
1482 | ||
1483 | sel_remove_entries(class_subdir); | |
1484 | } | |
1485 | ||
1486 | sel_remove_entries(class_dir); | |
1487 | } | |
1488 | ||
1489 | static int sel_make_classes(void) | |
1490 | { | |
1491 | int rc = 0, nclasses, i; | |
1492 | char **classes; | |
1493 | ||
1494 | /* delete any existing entries */ | |
1495 | sel_remove_classes(); | |
1496 | ||
1497 | rc = security_get_classes(&classes, &nclasses); | |
1498 | if (rc < 0) | |
1499 | goto out; | |
1500 | ||
1501 | /* +2 since classes are 1-indexed */ | |
1502 | last_class_ino = sel_class_to_ino(nclasses+2); | |
1503 | ||
1504 | for (i = 0; i < nclasses; i++) { | |
1505 | struct dentry *class_name_dir; | |
1506 | ||
1507 | class_name_dir = d_alloc_name(class_dir, classes[i]); | |
1508 | if (!class_name_dir) { | |
1509 | rc = -ENOMEM; | |
1510 | goto out1; | |
1511 | } | |
1512 | ||
1513 | rc = sel_make_dir(class_dir->d_inode, class_name_dir, | |
1514 | &last_class_ino); | |
1515 | if (rc) | |
1516 | goto out1; | |
1517 | ||
1518 | /* i+1 since class values are 1-indexed */ | |
1519 | rc = sel_make_class_dir_entries(classes[i], i+1, | |
1520 | class_name_dir); | |
1521 | if (rc) | |
1522 | goto out1; | |
1523 | } | |
1524 | ||
1525 | out1: | |
1526 | for (i = 0; i < nclasses; i++) | |
1527 | kfree(classes[i]); | |
1528 | kfree(classes); | |
1529 | out: | |
1530 | return rc; | |
1531 | } | |
1532 | ||
0dd4ae51 CP |
1533 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, |
1534 | unsigned long *ino) | |
1da177e4 LT |
1535 | { |
1536 | int ret = 0; | |
1537 | struct inode *inode; | |
1538 | ||
edb20fb5 | 1539 | inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO); |
1da177e4 LT |
1540 | if (!inode) { |
1541 | ret = -ENOMEM; | |
1542 | goto out; | |
1543 | } | |
1544 | inode->i_op = &simple_dir_inode_operations; | |
1545 | inode->i_fop = &simple_dir_operations; | |
0dd4ae51 | 1546 | inode->i_ino = ++(*ino); |
40e906f8 | 1547 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
d8c76e6f | 1548 | inc_nlink(inode); |
1da177e4 | 1549 | d_add(dentry, inode); |
edb20fb5 | 1550 | /* bump link count on parent directory, too */ |
d8c76e6f | 1551 | inc_nlink(dir); |
1da177e4 LT |
1552 | out: |
1553 | return ret; | |
1554 | } | |
1555 | ||
1556 | static int sel_fill_super(struct super_block * sb, void * data, int silent) | |
1557 | { | |
1558 | int ret; | |
1559 | struct dentry *dentry; | |
edb20fb5 | 1560 | struct inode *inode, *root_inode; |
1da177e4 LT |
1561 | struct inode_security_struct *isec; |
1562 | ||
1563 | static struct tree_descr selinux_files[] = { | |
1564 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, | |
1565 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, | |
ce9982d0 | 1566 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
1da177e4 LT |
1567 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
1568 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1569 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1570 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1571 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, | |
1572 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, | |
1573 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, | |
1574 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, | |
1575 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, | |
1576 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, | |
4e5ab4cb | 1577 | [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR}, |
1da177e4 LT |
1578 | /* last one */ {""} |
1579 | }; | |
1580 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); | |
1581 | if (ret) | |
161ce45a | 1582 | goto err; |
1da177e4 | 1583 | |
edb20fb5 JM |
1584 | root_inode = sb->s_root->d_inode; |
1585 | ||
1da177e4 | 1586 | dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); |
161ce45a JM |
1587 | if (!dentry) { |
1588 | ret = -ENOMEM; | |
1589 | goto err; | |
1590 | } | |
1da177e4 | 1591 | |
0dd4ae51 | 1592 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
cde174a8 | 1593 | if (ret) |
161ce45a | 1594 | goto err; |
cde174a8 | 1595 | |
1da177e4 | 1596 | bool_dir = dentry; |
1da177e4 LT |
1597 | |
1598 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); | |
161ce45a JM |
1599 | if (!dentry) { |
1600 | ret = -ENOMEM; | |
1601 | goto err; | |
1602 | } | |
1da177e4 LT |
1603 | |
1604 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); | |
161ce45a JM |
1605 | if (!inode) { |
1606 | ret = -ENOMEM; | |
1607 | goto err; | |
1608 | } | |
6174eafc | 1609 | inode->i_ino = ++sel_last_ino; |
1da177e4 LT |
1610 | isec = (struct inode_security_struct*)inode->i_security; |
1611 | isec->sid = SECINITSID_DEVNULL; | |
1612 | isec->sclass = SECCLASS_CHR_FILE; | |
1613 | isec->initialized = 1; | |
1614 | ||
1615 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); | |
1616 | d_add(dentry, inode); | |
1617 | selinux_null = dentry; | |
1618 | ||
1619 | dentry = d_alloc_name(sb->s_root, "avc"); | |
161ce45a JM |
1620 | if (!dentry) { |
1621 | ret = -ENOMEM; | |
1622 | goto err; | |
1623 | } | |
1da177e4 | 1624 | |
0dd4ae51 | 1625 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
1da177e4 | 1626 | if (ret) |
161ce45a | 1627 | goto err; |
1da177e4 LT |
1628 | |
1629 | ret = sel_make_avc_files(dentry); | |
1630 | if (ret) | |
161ce45a | 1631 | goto err; |
f0ee2e46 JC |
1632 | |
1633 | dentry = d_alloc_name(sb->s_root, "initial_contexts"); | |
1634 | if (!dentry) { | |
1635 | ret = -ENOMEM; | |
1636 | goto err; | |
1637 | } | |
1638 | ||
0dd4ae51 | 1639 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
f0ee2e46 JC |
1640 | if (ret) |
1641 | goto err; | |
1642 | ||
1643 | ret = sel_make_initcon_files(dentry); | |
1644 | if (ret) | |
1645 | goto err; | |
1646 | ||
e47c8fc5 CP |
1647 | dentry = d_alloc_name(sb->s_root, "class"); |
1648 | if (!dentry) { | |
1649 | ret = -ENOMEM; | |
1650 | goto err; | |
1651 | } | |
1652 | ||
1653 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); | |
1654 | if (ret) | |
1655 | goto err; | |
1656 | ||
1657 | class_dir = dentry; | |
1658 | ||
1da177e4 | 1659 | out: |
161ce45a JM |
1660 | return ret; |
1661 | err: | |
1da177e4 | 1662 | printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__); |
161ce45a | 1663 | goto out; |
1da177e4 LT |
1664 | } |
1665 | ||
454e2398 DH |
1666 | static int sel_get_sb(struct file_system_type *fs_type, |
1667 | int flags, const char *dev_name, void *data, | |
1668 | struct vfsmount *mnt) | |
1da177e4 | 1669 | { |
454e2398 | 1670 | return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); |
1da177e4 LT |
1671 | } |
1672 | ||
1673 | static struct file_system_type sel_fs_type = { | |
1674 | .name = "selinuxfs", | |
1675 | .get_sb = sel_get_sb, | |
1676 | .kill_sb = kill_litter_super, | |
1677 | }; | |
1678 | ||
1679 | struct vfsmount *selinuxfs_mount; | |
1680 | ||
1681 | static int __init init_sel_fs(void) | |
1682 | { | |
1683 | int err; | |
1684 | ||
1685 | if (!selinux_enabled) | |
1686 | return 0; | |
1687 | err = register_filesystem(&sel_fs_type); | |
1688 | if (!err) { | |
1689 | selinuxfs_mount = kern_mount(&sel_fs_type); | |
1690 | if (IS_ERR(selinuxfs_mount)) { | |
1691 | printk(KERN_ERR "selinuxfs: could not mount!\n"); | |
1692 | err = PTR_ERR(selinuxfs_mount); | |
1693 | selinuxfs_mount = NULL; | |
1694 | } | |
1695 | } | |
1696 | return err; | |
1697 | } | |
1698 | ||
1699 | __initcall(init_sel_fs); | |
1700 | ||
1701 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE | |
1702 | void exit_sel_fs(void) | |
1703 | { | |
1704 | unregister_filesystem(&sel_fs_type); | |
1705 | } | |
1706 | #endif |