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