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