1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3  *
4  *	Added conditional policy language extensions
5  *
6  *  Updated: Hewlett-Packard <paul@paul-moore.com>
7  *
8  *	Added support for the policy capability bitmap
9  *
10  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
11  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/pagemap.h>
17 #include <linux/page_size_compat.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/fs.h>
21 #include <linux/fs_context.h>
22 #include <linux/mount.h>
23 #include <linux/mutex.h>
24 #include <linux/namei.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/security.h>
28 #include <linux/major.h>
29 #include <linux/seq_file.h>
30 #include <linux/percpu.h>
31 #include <linux/audit.h>
32 #include <linux/uaccess.h>
33 #include <linux/kobject.h>
34 #include <linux/ctype.h>
35 
36 /* selinuxfs pseudo filesystem for exporting the security policy API.
37    Based on the proc code and the fs/nfsd/nfsctl.c code. */
38 
39 #include "flask.h"
40 #include "avc.h"
41 #include "avc_ss.h"
42 #include "security.h"
43 #include "objsec.h"
44 #include "conditional.h"
45 #include "ima.h"
46 
47 enum sel_inos {
48 	SEL_ROOT_INO = 2,
49 	SEL_LOAD,	/* load policy */
50 	SEL_ENFORCE,	/* get or set enforcing status */
51 	SEL_CONTEXT,	/* validate context */
52 	SEL_ACCESS,	/* compute access decision */
53 	SEL_CREATE,	/* compute create labeling decision */
54 	SEL_RELABEL,	/* compute relabeling decision */
55 	SEL_USER,	/* compute reachable user contexts */
56 	SEL_POLICYVERS,	/* return policy version for this kernel */
57 	SEL_COMMIT_BOOLS, /* commit new boolean values */
58 	SEL_MLS,	/* return if MLS policy is enabled */
59 	SEL_DISABLE,	/* disable SELinux until next reboot */
60 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
61 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
62 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
63 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
64 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
65 	SEL_STATUS,	/* export current status using mmap() */
66 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
67 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
68 	SEL_INO_NEXT,	/* The next inode number to use */
69 };
70 
71 struct selinux_fs_info {
72 	struct dentry *bool_dir;
73 	unsigned int bool_num;
74 	char **bool_pending_names;
75 	int *bool_pending_values;
76 	struct dentry *class_dir;
77 	unsigned long last_class_ino;
78 	bool policy_opened;
79 	struct dentry *policycap_dir;
80 	unsigned long last_ino;
81 	struct super_block *sb;
82 };
83 
selinux_fs_info_create(struct super_block * sb)84 static int selinux_fs_info_create(struct super_block *sb)
85 {
86 	struct selinux_fs_info *fsi;
87 
88 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
89 	if (!fsi)
90 		return -ENOMEM;
91 
92 	fsi->last_ino = SEL_INO_NEXT - 1;
93 	fsi->sb = sb;
94 	sb->s_fs_info = fsi;
95 	return 0;
96 }
97 
selinux_fs_info_free(struct super_block * sb)98 static void selinux_fs_info_free(struct super_block *sb)
99 {
100 	struct selinux_fs_info *fsi = sb->s_fs_info;
101 	unsigned int i;
102 
103 	if (fsi) {
104 		for (i = 0; i < fsi->bool_num; i++)
105 			kfree(fsi->bool_pending_names[i]);
106 		kfree(fsi->bool_pending_names);
107 		kfree(fsi->bool_pending_values);
108 	}
109 	kfree(sb->s_fs_info);
110 	sb->s_fs_info = NULL;
111 }
112 
113 #define SEL_INITCON_INO_OFFSET		0x01000000
114 #define SEL_BOOL_INO_OFFSET		0x02000000
115 #define SEL_CLASS_INO_OFFSET		0x04000000
116 #define SEL_POLICYCAP_INO_OFFSET	0x08000000
117 #define SEL_INO_MASK			0x00ffffff
118 
119 #define BOOL_DIR_NAME "booleans"
120 #define CLASS_DIR_NAME "class"
121 #define POLICYCAP_DIR_NAME "policy_capabilities"
122 
123 #define TMPBUFLEN	12
sel_read_enforce(struct file * filp,char __user * buf,size_t count,loff_t * ppos)124 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
125 				size_t count, loff_t *ppos)
126 {
127 	char tmpbuf[TMPBUFLEN];
128 	ssize_t length;
129 
130 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
131 			   enforcing_enabled());
132 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
133 }
134 
135 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
sel_write_enforce(struct file * file,const char __user * buf,size_t count,loff_t * ppos)136 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
137 				 size_t count, loff_t *ppos)
138 
139 {
140 	char *page = NULL;
141 	ssize_t length;
142 	int scan_value;
143 	bool old_value, new_value;
144 
145 	if (count >= PAGE_SIZE)
146 		return -ENOMEM;
147 
148 	/* No partial writes. */
149 	if (*ppos != 0)
150 		return -EINVAL;
151 
152 	page = memdup_user_nul(buf, count);
153 	if (IS_ERR(page))
154 		return PTR_ERR(page);
155 
156 	length = -EINVAL;
157 	if (sscanf(page, "%d", &scan_value) != 1)
158 		goto out;
159 
160 	new_value = !!scan_value;
161 
162 	old_value = enforcing_enabled();
163 	if (new_value != old_value) {
164 		length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
165 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
166 				      NULL);
167 		if (length)
168 			goto out;
169 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
170 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
171 			" enabled=1 old-enabled=1 lsm=selinux res=1",
172 			new_value, old_value,
173 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
174 			audit_get_sessionid(current));
175 		enforcing_set(new_value);
176 		if (new_value)
177 			avc_ss_reset(0);
178 		selnl_notify_setenforce(new_value);
179 		selinux_status_update_setenforce(new_value);
180 		if (!new_value)
181 			call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
182 
183 		selinux_ima_measure_state();
184 	}
185 	length = count;
186 out:
187 	kfree(page);
188 	return length;
189 }
190 #else
191 #define sel_write_enforce NULL
192 #endif
193 
194 static const struct file_operations sel_enforce_ops = {
195 	.read		= sel_read_enforce,
196 	.write		= sel_write_enforce,
197 	.llseek		= generic_file_llseek,
198 };
199 
sel_read_handle_unknown(struct file * filp,char __user * buf,size_t count,loff_t * ppos)200 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
201 					size_t count, loff_t *ppos)
202 {
203 	char tmpbuf[TMPBUFLEN];
204 	ssize_t length;
205 	ino_t ino = file_inode(filp)->i_ino;
206 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
207 		security_get_reject_unknown() :
208 		!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,
216 	.llseek		= generic_file_llseek,
217 };
218 
sel_open_handle_status(struct inode * inode,struct file * filp)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 
sel_read_handle_status(struct file * filp,char __user * buf,size_t count,loff_t * ppos)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 
sel_mmap_handle_status(struct file * filp,struct vm_area_struct * vma)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 	vm_flags_clear(vma, 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 
sel_write_disable(struct file * file,const char __user * buf,size_t count,loff_t * ppos)272 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
273 				 size_t count, loff_t *ppos)
274 
275 {
276 	char *page;
277 	ssize_t length;
278 	int new_value;
279 
280 	if (count >= PAGE_SIZE)
281 		return -ENOMEM;
282 
283 	/* No partial writes. */
284 	if (*ppos != 0)
285 		return -EINVAL;
286 
287 	page = memdup_user_nul(buf, count);
288 	if (IS_ERR(page))
289 		return PTR_ERR(page);
290 
291 	if (sscanf(page, "%d", &new_value) != 1) {
292 		length = -EINVAL;
293 		goto out;
294 	}
295 	length = count;
296 
297 	if (new_value) {
298 		pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n");
299 		pr_err("SELinux: Runtime disable is not supported, use selinux=0 on the kernel cmdline.\n");
300 	}
301 
302 out:
303 	kfree(page);
304 	return length;
305 }
306 
307 static const struct file_operations sel_disable_ops = {
308 	.write		= sel_write_disable,
309 	.llseek		= generic_file_llseek,
310 };
311 
sel_read_policyvers(struct file * filp,char __user * buf,size_t count,loff_t * ppos)312 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
313 				   size_t count, loff_t *ppos)
314 {
315 	char tmpbuf[TMPBUFLEN];
316 	ssize_t length;
317 
318 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
319 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
320 }
321 
322 static const struct file_operations sel_policyvers_ops = {
323 	.read		= sel_read_policyvers,
324 	.llseek		= generic_file_llseek,
325 };
326 
327 /* declaration for sel_write_load */
328 static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
329 			  unsigned int *bool_num, char ***bool_pending_names,
330 			  int **bool_pending_values);
331 static int sel_make_classes(struct selinux_policy *newpolicy,
332 			    struct dentry *class_dir,
333 			    unsigned long *last_class_ino);
334 
335 /* declaration for sel_make_class_dirs */
336 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
337 			unsigned long *ino);
338 
339 /* declaration for sel_make_policy_nodes */
340 static struct dentry *sel_make_swapover_dir(struct super_block *sb,
341 						unsigned long *ino);
342 
sel_read_mls(struct file * filp,char __user * buf,size_t count,loff_t * ppos)343 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
344 				size_t count, loff_t *ppos)
345 {
346 	char tmpbuf[TMPBUFLEN];
347 	ssize_t length;
348 
349 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
350 			   security_mls_enabled());
351 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
352 }
353 
354 static const struct file_operations sel_mls_ops = {
355 	.read		= sel_read_mls,
356 	.llseek		= generic_file_llseek,
357 };
358 
359 struct policy_load_memory {
360 	size_t len;
361 	void *data;
362 };
363 
sel_open_policy(struct inode * inode,struct file * filp)364 static int sel_open_policy(struct inode *inode, struct file *filp)
365 {
366 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
367 	struct policy_load_memory *plm = NULL;
368 	int rc;
369 
370 	BUG_ON(filp->private_data);
371 
372 	mutex_lock(&selinux_state.policy_mutex);
373 
374 	rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
375 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
376 	if (rc)
377 		goto err;
378 
379 	rc = -EBUSY;
380 	if (fsi->policy_opened)
381 		goto err;
382 
383 	rc = -ENOMEM;
384 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
385 	if (!plm)
386 		goto err;
387 
388 	rc = security_read_policy(&plm->data, &plm->len);
389 	if (rc)
390 		goto err;
391 
392 	if ((size_t)i_size_read(inode) != plm->len) {
393 		inode_lock(inode);
394 		i_size_write(inode, plm->len);
395 		inode_unlock(inode);
396 	}
397 
398 	fsi->policy_opened = 1;
399 
400 	filp->private_data = plm;
401 
402 	mutex_unlock(&selinux_state.policy_mutex);
403 
404 	return 0;
405 err:
406 	mutex_unlock(&selinux_state.policy_mutex);
407 
408 	if (plm)
409 		vfree(plm->data);
410 	kfree(plm);
411 	return rc;
412 }
413 
sel_release_policy(struct inode * inode,struct file * filp)414 static int sel_release_policy(struct inode *inode, struct file *filp)
415 {
416 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
417 	struct policy_load_memory *plm = filp->private_data;
418 
419 	BUG_ON(!plm);
420 
421 	fsi->policy_opened = 0;
422 
423 	vfree(plm->data);
424 	kfree(plm);
425 
426 	return 0;
427 }
428 
sel_read_policy(struct file * filp,char __user * buf,size_t count,loff_t * ppos)429 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
430 			       size_t count, loff_t *ppos)
431 {
432 	struct policy_load_memory *plm = filp->private_data;
433 	int ret;
434 
435 	ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
436 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
437 	if (ret)
438 		return ret;
439 
440 	return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
441 }
442 
sel_mmap_policy_fault(struct vm_fault * vmf)443 static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
444 {
445 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
446 	unsigned long offset;
447 	struct page *page;
448 
449 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
450 		return VM_FAULT_SIGBUS;
451 
452 	offset = vmf->pgoff << PAGE_SHIFT;
453 	if (offset >= roundup(plm->len, PAGE_SIZE))
454 		return VM_FAULT_SIGBUS;
455 
456 	page = vmalloc_to_page(plm->data + offset);
457 	get_page(page);
458 
459 	vmf->page = page;
460 
461 	return 0;
462 }
463 
464 static const struct vm_operations_struct sel_mmap_policy_ops = {
465 	.fault = sel_mmap_policy_fault,
466 	.page_mkwrite = sel_mmap_policy_fault,
467 };
468 
sel_mmap_policy(struct file * filp,struct vm_area_struct * vma)469 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
470 {
471 	if (vma->vm_flags & VM_SHARED) {
472 		/* do not allow mprotect to make mapping writable */
473 		vm_flags_clear(vma, VM_MAYWRITE);
474 
475 		if (vma->vm_flags & VM_WRITE)
476 			return -EACCES;
477 	}
478 
479 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
480 	vma->vm_ops = &sel_mmap_policy_ops;
481 
482 	return 0;
483 }
484 
485 static const struct file_operations sel_policy_ops = {
486 	.open		= sel_open_policy,
487 	.read		= sel_read_policy,
488 	.mmap		= sel_mmap_policy,
489 	.release	= sel_release_policy,
490 	.llseek		= generic_file_llseek,
491 };
492 
sel_remove_old_bool_data(unsigned int bool_num,char ** bool_names,int * bool_values)493 static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names,
494 				     int *bool_values)
495 {
496 	u32 i;
497 
498 	/* bool_dir cleanup */
499 	for (i = 0; i < bool_num; i++)
500 		kfree(bool_names[i]);
501 	kfree(bool_names);
502 	kfree(bool_values);
503 }
504 
sel_make_policy_nodes(struct selinux_fs_info * fsi,struct selinux_policy * newpolicy)505 static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
506 				struct selinux_policy *newpolicy)
507 {
508 	int ret = 0;
509 	struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir;
510 	unsigned int bool_num = 0;
511 	char **bool_names = NULL;
512 	int *bool_values = NULL;
513 	unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */
514 
515 	tmp_parent = sel_make_swapover_dir(fsi->sb, &tmp_ino);
516 	if (IS_ERR(tmp_parent))
517 		return PTR_ERR(tmp_parent);
518 
519 	tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
520 	tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino);
521 	if (IS_ERR(tmp_bool_dir)) {
522 		ret = PTR_ERR(tmp_bool_dir);
523 		goto out;
524 	}
525 
526 	tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
527 	tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino);
528 	if (IS_ERR(tmp_class_dir)) {
529 		ret = PTR_ERR(tmp_class_dir);
530 		goto out;
531 	}
532 
533 	ret = sel_make_bools(newpolicy, tmp_bool_dir, &bool_num,
534 			     &bool_names, &bool_values);
535 	if (ret)
536 		goto out;
537 
538 	ret = sel_make_classes(newpolicy, tmp_class_dir,
539 			       &fsi->last_class_ino);
540 	if (ret)
541 		goto out;
542 
543 	lock_rename(tmp_parent, fsi->sb->s_root);
544 
545 	/* booleans */
546 	d_exchange(tmp_bool_dir, fsi->bool_dir);
547 
548 	swap(fsi->bool_num, bool_num);
549 	swap(fsi->bool_pending_names, bool_names);
550 	swap(fsi->bool_pending_values, bool_values);
551 
552 	fsi->bool_dir = tmp_bool_dir;
553 
554 	/* classes */
555 	d_exchange(tmp_class_dir, fsi->class_dir);
556 	fsi->class_dir = tmp_class_dir;
557 
558 	unlock_rename(tmp_parent, fsi->sb->s_root);
559 
560 out:
561 	sel_remove_old_bool_data(bool_num, bool_names, bool_values);
562 	/* Since the other temporary dirs are children of tmp_parent
563 	 * this will handle all the cleanup in the case of a failure before
564 	 * the swapover
565 	 */
566 	simple_recursive_removal(tmp_parent, NULL);
567 
568 	return ret;
569 }
570 
sel_write_load(struct file * file,const char __user * buf,size_t count,loff_t * ppos)571 static ssize_t sel_write_load(struct file *file, const char __user *buf,
572 			      size_t count, loff_t *ppos)
573 
574 {
575 	struct selinux_fs_info *fsi;
576 	struct selinux_load_state load_state;
577 	ssize_t length;
578 	void *data = NULL;
579 
580 	/* no partial writes */
581 	if (*ppos)
582 		return -EINVAL;
583 	/* no empty policies */
584 	if (!count)
585 		return -EINVAL;
586 
587 	mutex_lock(&selinux_state.policy_mutex);
588 
589 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
590 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
591 	if (length)
592 		goto out;
593 
594 	data = vmalloc(count);
595 	if (!data) {
596 		length = -ENOMEM;
597 		goto out;
598 	}
599 	if (copy_from_user(data, buf, count) != 0) {
600 		length = -EFAULT;
601 		goto out;
602 	}
603 
604 	length = security_load_policy(data, count, &load_state);
605 	if (length) {
606 		pr_warn_ratelimited("SELinux: failed to load policy\n");
607 		goto out;
608 	}
609 	fsi = file_inode(file)->i_sb->s_fs_info;
610 	length = sel_make_policy_nodes(fsi, load_state.policy);
611 	if (length) {
612 		pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
613 		selinux_policy_cancel(&load_state);
614 		goto out;
615 	}
616 
617 	selinux_policy_commit(&load_state);
618 	length = count;
619 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
620 		"auid=%u ses=%u lsm=selinux res=1",
621 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
622 		audit_get_sessionid(current));
623 
624 out:
625 	mutex_unlock(&selinux_state.policy_mutex);
626 	vfree(data);
627 	return length;
628 }
629 
630 static const struct file_operations sel_load_ops = {
631 	.write		= sel_write_load,
632 	.llseek		= generic_file_llseek,
633 };
634 
sel_write_context(struct file * file,char * buf,size_t size)635 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
636 {
637 	char *canon = NULL;
638 	u32 sid, len;
639 	ssize_t length;
640 
641 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
642 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
643 	if (length)
644 		goto out;
645 
646 	length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
647 	if (length)
648 		goto out;
649 
650 	length = security_sid_to_context(sid, &canon, &len);
651 	if (length)
652 		goto out;
653 
654 	length = -ERANGE;
655 	if (len > SIMPLE_TRANSACTION_LIMIT) {
656 		pr_err("SELinux: %s:  context size (%u) exceeds "
657 			"payload max\n", __func__, len);
658 		goto out;
659 	}
660 
661 	memcpy(buf, canon, len);
662 	length = len;
663 out:
664 	kfree(canon);
665 	return length;
666 }
667 
sel_read_checkreqprot(struct file * filp,char __user * buf,size_t count,loff_t * ppos)668 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
669 				     size_t count, loff_t *ppos)
670 {
671 	char tmpbuf[TMPBUFLEN];
672 	ssize_t length;
673 
674 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
675 			   checkreqprot_get());
676 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
677 }
678 
sel_write_checkreqprot(struct file * file,const char __user * buf,size_t count,loff_t * ppos)679 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
680 				      size_t count, loff_t *ppos)
681 {
682 	char *page;
683 	ssize_t length;
684 	unsigned int new_value;
685 
686 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
687 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
688 			      NULL);
689 	if (length)
690 		return length;
691 
692 	if (count >= PAGE_SIZE)
693 		return -ENOMEM;
694 
695 	/* No partial writes. */
696 	if (*ppos != 0)
697 		return -EINVAL;
698 
699 	page = memdup_user_nul(buf, count);
700 	if (IS_ERR(page))
701 		return PTR_ERR(page);
702 
703 	if (sscanf(page, "%u", &new_value) != 1) {
704 		length = -EINVAL;
705 		goto out;
706 	}
707 	length = count;
708 
709 	if (new_value) {
710 		char comm[sizeof(current->comm)];
711 
712 		memcpy(comm, current->comm, sizeof(comm));
713 		pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n",
714 		       comm, current->pid);
715 	}
716 
717 	selinux_ima_measure_state();
718 
719 out:
720 	kfree(page);
721 	return length;
722 }
723 static const struct file_operations sel_checkreqprot_ops = {
724 	.read		= sel_read_checkreqprot,
725 	.write		= sel_write_checkreqprot,
726 	.llseek		= generic_file_llseek,
727 };
728 
sel_write_validatetrans(struct file * file,const char __user * buf,size_t count,loff_t * ppos)729 static ssize_t sel_write_validatetrans(struct file *file,
730 					const char __user *buf,
731 					size_t count, loff_t *ppos)
732 {
733 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
734 	char *req = NULL;
735 	u32 osid, nsid, tsid;
736 	u16 tclass;
737 	int rc;
738 
739 	rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
740 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
741 	if (rc)
742 		goto out;
743 
744 	rc = -ENOMEM;
745 	if (count >= PAGE_SIZE)
746 		goto out;
747 
748 	/* No partial writes. */
749 	rc = -EINVAL;
750 	if (*ppos != 0)
751 		goto out;
752 
753 	req = memdup_user_nul(buf, count);
754 	if (IS_ERR(req)) {
755 		rc = PTR_ERR(req);
756 		req = NULL;
757 		goto out;
758 	}
759 
760 	rc = -ENOMEM;
761 	oldcon = kzalloc(count + 1, GFP_KERNEL);
762 	if (!oldcon)
763 		goto out;
764 
765 	newcon = kzalloc(count + 1, GFP_KERNEL);
766 	if (!newcon)
767 		goto out;
768 
769 	taskcon = kzalloc(count + 1, GFP_KERNEL);
770 	if (!taskcon)
771 		goto out;
772 
773 	rc = -EINVAL;
774 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
775 		goto out;
776 
777 	rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
778 	if (rc)
779 		goto out;
780 
781 	rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
782 	if (rc)
783 		goto out;
784 
785 	rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
786 	if (rc)
787 		goto out;
788 
789 	rc = security_validate_transition_user(osid, nsid, tsid, tclass);
790 	if (!rc)
791 		rc = count;
792 out:
793 	kfree(req);
794 	kfree(oldcon);
795 	kfree(newcon);
796 	kfree(taskcon);
797 	return rc;
798 }
799 
800 static const struct file_operations sel_transition_ops = {
801 	.write		= sel_write_validatetrans,
802 	.llseek		= generic_file_llseek,
803 };
804 
805 /*
806  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
807  */
808 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
809 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
810 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
811 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
812 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
813 
814 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
815 	[SEL_ACCESS] = sel_write_access,
816 	[SEL_CREATE] = sel_write_create,
817 	[SEL_RELABEL] = sel_write_relabel,
818 	[SEL_USER] = sel_write_user,
819 	[SEL_MEMBER] = sel_write_member,
820 	[SEL_CONTEXT] = sel_write_context,
821 };
822 
selinux_transaction_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)823 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
824 {
825 	ino_t ino = file_inode(file)->i_ino;
826 	char *data;
827 	ssize_t rv;
828 
829 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
830 		return -EINVAL;
831 
832 	data = simple_transaction_get(file, buf, size);
833 	if (IS_ERR(data))
834 		return PTR_ERR(data);
835 
836 	rv = write_op[ino](file, data, size);
837 	if (rv > 0) {
838 		simple_transaction_set(file, rv);
839 		rv = size;
840 	}
841 	return rv;
842 }
843 
844 static const struct file_operations transaction_ops = {
845 	.write		= selinux_transaction_write,
846 	.read		= simple_transaction_read,
847 	.release	= simple_transaction_release,
848 	.llseek		= generic_file_llseek,
849 };
850 
851 /*
852  * payload - write methods
853  * If the method has a response, the response should be put in buf,
854  * and the length returned.  Otherwise return 0 or and -error.
855  */
856 
sel_write_access(struct file * file,char * buf,size_t size)857 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
858 {
859 	char *scon = NULL, *tcon = NULL;
860 	u32 ssid, tsid;
861 	u16 tclass;
862 	struct av_decision avd;
863 	ssize_t length;
864 
865 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
866 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
867 	if (length)
868 		goto out;
869 
870 	length = -ENOMEM;
871 	scon = kzalloc(size + 1, GFP_KERNEL);
872 	if (!scon)
873 		goto out;
874 
875 	length = -ENOMEM;
876 	tcon = kzalloc(size + 1, GFP_KERNEL);
877 	if (!tcon)
878 		goto out;
879 
880 	length = -EINVAL;
881 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
882 		goto out;
883 
884 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
885 	if (length)
886 		goto out;
887 
888 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
889 	if (length)
890 		goto out;
891 
892 	security_compute_av_user(ssid, tsid, tclass, &avd);
893 
894 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
895 			  "%x %x %x %x %u %x",
896 			  avd.allowed, 0xffffffff,
897 			  avd.auditallow, avd.auditdeny,
898 			  avd.seqno, avd.flags);
899 out:
900 	kfree(tcon);
901 	kfree(scon);
902 	return length;
903 }
904 
sel_write_create(struct file * file,char * buf,size_t size)905 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
906 {
907 	char *scon = NULL, *tcon = NULL;
908 	char *namebuf = NULL, *objname = NULL;
909 	u32 ssid, tsid, newsid;
910 	u16 tclass;
911 	ssize_t length;
912 	char *newcon = NULL;
913 	u32 len;
914 	int nargs;
915 
916 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
917 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
918 			      NULL);
919 	if (length)
920 		goto out;
921 
922 	length = -ENOMEM;
923 	scon = kzalloc(size + 1, GFP_KERNEL);
924 	if (!scon)
925 		goto out;
926 
927 	length = -ENOMEM;
928 	tcon = kzalloc(size + 1, GFP_KERNEL);
929 	if (!tcon)
930 		goto out;
931 
932 	length = -ENOMEM;
933 	namebuf = kzalloc(size + 1, GFP_KERNEL);
934 	if (!namebuf)
935 		goto out;
936 
937 	length = -EINVAL;
938 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
939 	if (nargs < 3 || nargs > 4)
940 		goto out;
941 	if (nargs == 4) {
942 		/*
943 		 * If and when the name of new object to be queried contains
944 		 * either whitespace or multibyte characters, they shall be
945 		 * encoded based on the percentage-encoding rule.
946 		 * If not encoded, the sscanf logic picks up only left-half
947 		 * of the supplied name; split by a whitespace unexpectedly.
948 		 */
949 		char   *r, *w;
950 		int     c1, c2;
951 
952 		r = w = namebuf;
953 		do {
954 			c1 = *r++;
955 			if (c1 == '+')
956 				c1 = ' ';
957 			else if (c1 == '%') {
958 				c1 = hex_to_bin(*r++);
959 				if (c1 < 0)
960 					goto out;
961 				c2 = hex_to_bin(*r++);
962 				if (c2 < 0)
963 					goto out;
964 				c1 = (c1 << 4) | c2;
965 			}
966 			*w++ = c1;
967 		} while (c1 != '\0');
968 
969 		objname = namebuf;
970 	}
971 
972 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
973 	if (length)
974 		goto out;
975 
976 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
977 	if (length)
978 		goto out;
979 
980 	length = security_transition_sid_user(ssid, tsid, tclass,
981 					      objname, &newsid);
982 	if (length)
983 		goto out;
984 
985 	length = security_sid_to_context(newsid, &newcon, &len);
986 	if (length)
987 		goto out;
988 
989 	length = -ERANGE;
990 	if (len > SIMPLE_TRANSACTION_LIMIT) {
991 		pr_err("SELinux: %s:  context size (%u) exceeds "
992 			"payload max\n", __func__, len);
993 		goto out;
994 	}
995 
996 	memcpy(buf, newcon, len);
997 	length = len;
998 out:
999 	kfree(newcon);
1000 	kfree(namebuf);
1001 	kfree(tcon);
1002 	kfree(scon);
1003 	return length;
1004 }
1005 
sel_write_relabel(struct file * file,char * buf,size_t size)1006 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
1007 {
1008 	char *scon = NULL, *tcon = NULL;
1009 	u32 ssid, tsid, newsid;
1010 	u16 tclass;
1011 	ssize_t length;
1012 	char *newcon = NULL;
1013 	u32 len;
1014 
1015 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1016 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1017 			      NULL);
1018 	if (length)
1019 		goto out;
1020 
1021 	length = -ENOMEM;
1022 	scon = kzalloc(size + 1, GFP_KERNEL);
1023 	if (!scon)
1024 		goto out;
1025 
1026 	length = -ENOMEM;
1027 	tcon = kzalloc(size + 1, GFP_KERNEL);
1028 	if (!tcon)
1029 		goto out;
1030 
1031 	length = -EINVAL;
1032 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1033 		goto out;
1034 
1035 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1036 	if (length)
1037 		goto out;
1038 
1039 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1040 	if (length)
1041 		goto out;
1042 
1043 	length = security_change_sid(ssid, tsid, tclass, &newsid);
1044 	if (length)
1045 		goto out;
1046 
1047 	length = security_sid_to_context(newsid, &newcon, &len);
1048 	if (length)
1049 		goto out;
1050 
1051 	length = -ERANGE;
1052 	if (len > SIMPLE_TRANSACTION_LIMIT)
1053 		goto out;
1054 
1055 	memcpy(buf, newcon, len);
1056 	length = len;
1057 out:
1058 	kfree(newcon);
1059 	kfree(tcon);
1060 	kfree(scon);
1061 	return length;
1062 }
1063 
sel_write_user(struct file * file,char * buf,size_t size)1064 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
1065 {
1066 	char *con = NULL, *user = NULL, *ptr;
1067 	u32 sid, *sids = NULL;
1068 	ssize_t length;
1069 	char *newcon;
1070 	int rc;
1071 	u32 i, len, nsids;
1072 
1073 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1074 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1075 			      NULL);
1076 	if (length)
1077 		goto out;
1078 
1079 	length = -ENOMEM;
1080 	con = kzalloc(size + 1, GFP_KERNEL);
1081 	if (!con)
1082 		goto out;
1083 
1084 	length = -ENOMEM;
1085 	user = kzalloc(size + 1, GFP_KERNEL);
1086 	if (!user)
1087 		goto out;
1088 
1089 	length = -EINVAL;
1090 	if (sscanf(buf, "%s %s", con, user) != 2)
1091 		goto out;
1092 
1093 	length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
1094 	if (length)
1095 		goto out;
1096 
1097 	length = security_get_user_sids(sid, user, &sids, &nsids);
1098 	if (length)
1099 		goto out;
1100 
1101 	length = sprintf(buf, "%u", nsids) + 1;
1102 	ptr = buf + length;
1103 	for (i = 0; i < nsids; i++) {
1104 		rc = security_sid_to_context(sids[i], &newcon, &len);
1105 		if (rc) {
1106 			length = rc;
1107 			goto out;
1108 		}
1109 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1110 			kfree(newcon);
1111 			length = -ERANGE;
1112 			goto out;
1113 		}
1114 		memcpy(ptr, newcon, len);
1115 		kfree(newcon);
1116 		ptr += len;
1117 		length += len;
1118 	}
1119 out:
1120 	kfree(sids);
1121 	kfree(user);
1122 	kfree(con);
1123 	return length;
1124 }
1125 
sel_write_member(struct file * file,char * buf,size_t size)1126 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1127 {
1128 	char *scon = NULL, *tcon = NULL;
1129 	u32 ssid, tsid, newsid;
1130 	u16 tclass;
1131 	ssize_t length;
1132 	char *newcon = NULL;
1133 	u32 len;
1134 
1135 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1136 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1137 			      NULL);
1138 	if (length)
1139 		goto out;
1140 
1141 	length = -ENOMEM;
1142 	scon = kzalloc(size + 1, GFP_KERNEL);
1143 	if (!scon)
1144 		goto out;
1145 
1146 	length = -ENOMEM;
1147 	tcon = kzalloc(size + 1, GFP_KERNEL);
1148 	if (!tcon)
1149 		goto out;
1150 
1151 	length = -EINVAL;
1152 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1153 		goto out;
1154 
1155 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1156 	if (length)
1157 		goto out;
1158 
1159 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1160 	if (length)
1161 		goto out;
1162 
1163 	length = security_member_sid(ssid, tsid, tclass, &newsid);
1164 	if (length)
1165 		goto out;
1166 
1167 	length = security_sid_to_context(newsid, &newcon, &len);
1168 	if (length)
1169 		goto out;
1170 
1171 	length = -ERANGE;
1172 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1173 		pr_err("SELinux: %s:  context size (%u) exceeds "
1174 			"payload max\n", __func__, len);
1175 		goto out;
1176 	}
1177 
1178 	memcpy(buf, newcon, len);
1179 	length = len;
1180 out:
1181 	kfree(newcon);
1182 	kfree(tcon);
1183 	kfree(scon);
1184 	return length;
1185 }
1186 
sel_make_inode(struct super_block * sb,umode_t mode)1187 static struct inode *sel_make_inode(struct super_block *sb, umode_t mode)
1188 {
1189 	struct inode *ret = new_inode(sb);
1190 
1191 	if (ret) {
1192 		ret->i_mode = mode;
1193 		simple_inode_init_ts(ret);
1194 	}
1195 	return ret;
1196 }
1197 
sel_read_bool(struct file * filep,char __user * buf,size_t count,loff_t * ppos)1198 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1199 			     size_t count, loff_t *ppos)
1200 {
1201 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1202 	char *page = NULL;
1203 	ssize_t length;
1204 	ssize_t ret;
1205 	int cur_enforcing;
1206 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1207 	const char *name = filep->f_path.dentry->d_name.name;
1208 
1209 	mutex_lock(&selinux_state.policy_mutex);
1210 
1211 	ret = -EINVAL;
1212 	if (index >= fsi->bool_num || strcmp(name,
1213 					     fsi->bool_pending_names[index]))
1214 		goto out_unlock;
1215 
1216 	ret = -ENOMEM;
1217 	page = (char *)get_zeroed_page(GFP_KERNEL);
1218 	if (!page)
1219 		goto out_unlock;
1220 
1221 	cur_enforcing = security_get_bool_value(index);
1222 	if (cur_enforcing < 0) {
1223 		ret = cur_enforcing;
1224 		goto out_unlock;
1225 	}
1226 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1227 			  fsi->bool_pending_values[index]);
1228 	mutex_unlock(&selinux_state.policy_mutex);
1229 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
1230 out_free:
1231 	free_page((unsigned long)page);
1232 	return ret;
1233 
1234 out_unlock:
1235 	mutex_unlock(&selinux_state.policy_mutex);
1236 	goto out_free;
1237 }
1238 
sel_write_bool(struct file * filep,const char __user * buf,size_t count,loff_t * ppos)1239 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1240 			      size_t count, loff_t *ppos)
1241 {
1242 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1243 	char *page = NULL;
1244 	ssize_t length;
1245 	int new_value;
1246 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1247 	const char *name = filep->f_path.dentry->d_name.name;
1248 
1249 	if (count >= PAGE_SIZE)
1250 		return -ENOMEM;
1251 
1252 	/* No partial writes. */
1253 	if (*ppos != 0)
1254 		return -EINVAL;
1255 
1256 	page = memdup_user_nul(buf, count);
1257 	if (IS_ERR(page))
1258 		return PTR_ERR(page);
1259 
1260 	mutex_lock(&selinux_state.policy_mutex);
1261 
1262 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1263 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1264 			      NULL);
1265 	if (length)
1266 		goto out;
1267 
1268 	length = -EINVAL;
1269 	if (index >= fsi->bool_num || strcmp(name,
1270 					     fsi->bool_pending_names[index]))
1271 		goto out;
1272 
1273 	length = -EINVAL;
1274 	if (sscanf(page, "%d", &new_value) != 1)
1275 		goto out;
1276 
1277 	if (new_value)
1278 		new_value = 1;
1279 
1280 	fsi->bool_pending_values[index] = new_value;
1281 	length = count;
1282 
1283 out:
1284 	mutex_unlock(&selinux_state.policy_mutex);
1285 	kfree(page);
1286 	return length;
1287 }
1288 
1289 static const struct file_operations sel_bool_ops = {
1290 	.read		= sel_read_bool,
1291 	.write		= sel_write_bool,
1292 	.llseek		= generic_file_llseek,
1293 };
1294 
sel_commit_bools_write(struct file * filep,const char __user * buf,size_t count,loff_t * ppos)1295 static ssize_t sel_commit_bools_write(struct file *filep,
1296 				      const char __user *buf,
1297 				      size_t count, loff_t *ppos)
1298 {
1299 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1300 	char *page = NULL;
1301 	ssize_t length;
1302 	int new_value;
1303 
1304 	if (count >= PAGE_SIZE)
1305 		return -ENOMEM;
1306 
1307 	/* No partial writes. */
1308 	if (*ppos != 0)
1309 		return -EINVAL;
1310 
1311 	page = memdup_user_nul(buf, count);
1312 	if (IS_ERR(page))
1313 		return PTR_ERR(page);
1314 
1315 	mutex_lock(&selinux_state.policy_mutex);
1316 
1317 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1318 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1319 			      NULL);
1320 	if (length)
1321 		goto out;
1322 
1323 	length = -EINVAL;
1324 	if (sscanf(page, "%d", &new_value) != 1)
1325 		goto out;
1326 
1327 	length = 0;
1328 	if (new_value && fsi->bool_pending_values)
1329 		length = security_set_bools(fsi->bool_num,
1330 					    fsi->bool_pending_values);
1331 
1332 	if (!length)
1333 		length = count;
1334 
1335 out:
1336 	mutex_unlock(&selinux_state.policy_mutex);
1337 	kfree(page);
1338 	return length;
1339 }
1340 
1341 static const struct file_operations sel_commit_bools_ops = {
1342 	.write		= sel_commit_bools_write,
1343 	.llseek		= generic_file_llseek,
1344 };
1345 
sel_make_bools(struct selinux_policy * newpolicy,struct dentry * bool_dir,unsigned int * bool_num,char *** bool_pending_names,int ** bool_pending_values)1346 static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
1347 			  unsigned int *bool_num, char ***bool_pending_names,
1348 			  int **bool_pending_values)
1349 {
1350 	int ret;
1351 	char **names, *page;
1352 	u32 i, num;
1353 
1354 	page = (char *)get_zeroed_page(GFP_KERNEL);
1355 	if (!page)
1356 		return -ENOMEM;
1357 
1358 	ret = security_get_bools(newpolicy, &num, &names, bool_pending_values);
1359 	if (ret)
1360 		goto out;
1361 
1362 	*bool_num = num;
1363 	*bool_pending_names = names;
1364 
1365 	for (i = 0; i < num; i++) {
1366 		struct dentry *dentry;
1367 		struct inode *inode;
1368 		struct inode_security_struct *isec;
1369 		ssize_t len;
1370 		u32 sid;
1371 
1372 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1373 		if (len >= PAGE_SIZE) {
1374 			ret = -ENAMETOOLONG;
1375 			break;
1376 		}
1377 		dentry = d_alloc_name(bool_dir, names[i]);
1378 		if (!dentry) {
1379 			ret = -ENOMEM;
1380 			break;
1381 		}
1382 
1383 		inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1384 		if (!inode) {
1385 			dput(dentry);
1386 			ret = -ENOMEM;
1387 			break;
1388 		}
1389 
1390 		isec = selinux_inode(inode);
1391 		ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
1392 					 SECCLASS_FILE, &sid);
1393 		if (ret) {
1394 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1395 					   page);
1396 			sid = SECINITSID_SECURITY;
1397 		}
1398 
1399 		isec->sid = sid;
1400 		isec->initialized = LABEL_INITIALIZED;
1401 		inode->i_fop = &sel_bool_ops;
1402 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1403 		d_add(dentry, inode);
1404 	}
1405 out:
1406 	free_page((unsigned long)page);
1407 	return ret;
1408 }
1409 
sel_read_avc_cache_threshold(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1410 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1411 					    size_t count, loff_t *ppos)
1412 {
1413 	char tmpbuf[TMPBUFLEN];
1414 	ssize_t length;
1415 
1416 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1417 			   avc_get_cache_threshold());
1418 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1419 }
1420 
sel_write_avc_cache_threshold(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1421 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1422 					     const char __user *buf,
1423 					     size_t count, loff_t *ppos)
1424 
1425 {
1426 	char *page;
1427 	ssize_t ret;
1428 	unsigned int new_value;
1429 
1430 	ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1431 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1432 			   NULL);
1433 	if (ret)
1434 		return ret;
1435 
1436 	if (count >= PAGE_SIZE)
1437 		return -ENOMEM;
1438 
1439 	/* No partial writes. */
1440 	if (*ppos != 0)
1441 		return -EINVAL;
1442 
1443 	page = memdup_user_nul(buf, count);
1444 	if (IS_ERR(page))
1445 		return PTR_ERR(page);
1446 
1447 	ret = -EINVAL;
1448 	if (sscanf(page, "%u", &new_value) != 1)
1449 		goto out;
1450 
1451 	avc_set_cache_threshold(new_value);
1452 
1453 	ret = count;
1454 out:
1455 	kfree(page);
1456 	return ret;
1457 }
1458 
sel_read_avc_hash_stats(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1459 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1460 				       size_t count, loff_t *ppos)
1461 {
1462 	char *page;
1463 	ssize_t length;
1464 
1465 	page = (char *)__get_free_page(GFP_KERNEL);
1466 	if (!page)
1467 		return -ENOMEM;
1468 
1469 	length = avc_get_hash_stats(page);
1470 	if (length >= 0)
1471 		length = simple_read_from_buffer(buf, count, ppos, page, length);
1472 	free_page((unsigned long)page);
1473 
1474 	return length;
1475 }
1476 
sel_read_sidtab_hash_stats(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1477 static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1478 					size_t count, loff_t *ppos)
1479 {
1480 	char *page;
1481 	ssize_t length;
1482 
1483 	page = (char *)__get_free_page(GFP_KERNEL);
1484 	if (!page)
1485 		return -ENOMEM;
1486 
1487 	length = security_sidtab_hash_stats(page);
1488 	if (length >= 0)
1489 		length = simple_read_from_buffer(buf, count, ppos, page,
1490 						length);
1491 	free_page((unsigned long)page);
1492 
1493 	return length;
1494 }
1495 
1496 static const struct file_operations sel_sidtab_hash_stats_ops = {
1497 	.read		= sel_read_sidtab_hash_stats,
1498 	.llseek		= generic_file_llseek,
1499 };
1500 
1501 static const struct file_operations sel_avc_cache_threshold_ops = {
1502 	.read		= sel_read_avc_cache_threshold,
1503 	.write		= sel_write_avc_cache_threshold,
1504 	.llseek		= generic_file_llseek,
1505 };
1506 
1507 static const struct file_operations sel_avc_hash_stats_ops = {
1508 	.read		= sel_read_avc_hash_stats,
1509 	.llseek		= generic_file_llseek,
1510 };
1511 
1512 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
sel_avc_get_stat_idx(loff_t * idx)1513 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1514 {
1515 	int cpu;
1516 
1517 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1518 		if (!cpu_possible(cpu))
1519 			continue;
1520 		*idx = cpu + 1;
1521 		return &per_cpu(avc_cache_stats, cpu);
1522 	}
1523 	(*idx)++;
1524 	return NULL;
1525 }
1526 
sel_avc_stats_seq_start(struct seq_file * seq,loff_t * pos)1527 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1528 {
1529 	loff_t n = *pos - 1;
1530 
1531 	if (*pos == 0)
1532 		return SEQ_START_TOKEN;
1533 
1534 	return sel_avc_get_stat_idx(&n);
1535 }
1536 
sel_avc_stats_seq_next(struct seq_file * seq,void * v,loff_t * pos)1537 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1538 {
1539 	return sel_avc_get_stat_idx(pos);
1540 }
1541 
sel_avc_stats_seq_show(struct seq_file * seq,void * v)1542 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1543 {
1544 	struct avc_cache_stats *st = v;
1545 
1546 	if (v == SEQ_START_TOKEN) {
1547 		seq_puts(seq,
1548 			 "lookups hits misses allocations reclaims frees\n");
1549 	} else {
1550 		unsigned int lookups = st->lookups;
1551 		unsigned int misses = st->misses;
1552 		unsigned int hits = lookups - misses;
1553 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1554 			   hits, misses, st->allocations,
1555 			   st->reclaims, st->frees);
1556 	}
1557 	return 0;
1558 }
1559 
sel_avc_stats_seq_stop(struct seq_file * seq,void * v)1560 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1561 { }
1562 
1563 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1564 	.start		= sel_avc_stats_seq_start,
1565 	.next		= sel_avc_stats_seq_next,
1566 	.show		= sel_avc_stats_seq_show,
1567 	.stop		= sel_avc_stats_seq_stop,
1568 };
1569 
sel_open_avc_cache_stats(struct inode * inode,struct file * file)1570 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1571 {
1572 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
1573 }
1574 
1575 static const struct file_operations sel_avc_cache_stats_ops = {
1576 	.open		= sel_open_avc_cache_stats,
1577 	.read		= seq_read,
1578 	.llseek		= seq_lseek,
1579 	.release	= seq_release,
1580 };
1581 #endif
1582 
sel_make_avc_files(struct dentry * dir)1583 static int sel_make_avc_files(struct dentry *dir)
1584 {
1585 	struct super_block *sb = dir->d_sb;
1586 	struct selinux_fs_info *fsi = sb->s_fs_info;
1587 	unsigned int i;
1588 	static const struct tree_descr files[] = {
1589 		{ "cache_threshold",
1590 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1591 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1592 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1593 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1594 #endif
1595 	};
1596 
1597 	for (i = 0; i < ARRAY_SIZE(files); i++) {
1598 		struct inode *inode;
1599 		struct dentry *dentry;
1600 
1601 		dentry = d_alloc_name(dir, files[i].name);
1602 		if (!dentry)
1603 			return -ENOMEM;
1604 
1605 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1606 		if (!inode) {
1607 			dput(dentry);
1608 			return -ENOMEM;
1609 		}
1610 
1611 		inode->i_fop = files[i].ops;
1612 		inode->i_ino = ++fsi->last_ino;
1613 		d_add(dentry, inode);
1614 	}
1615 
1616 	return 0;
1617 }
1618 
sel_make_ss_files(struct dentry * dir)1619 static int sel_make_ss_files(struct dentry *dir)
1620 {
1621 	struct super_block *sb = dir->d_sb;
1622 	struct selinux_fs_info *fsi = sb->s_fs_info;
1623 	unsigned int i;
1624 	static const struct tree_descr files[] = {
1625 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1626 	};
1627 
1628 	for (i = 0; i < ARRAY_SIZE(files); i++) {
1629 		struct inode *inode;
1630 		struct dentry *dentry;
1631 
1632 		dentry = d_alloc_name(dir, files[i].name);
1633 		if (!dentry)
1634 			return -ENOMEM;
1635 
1636 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1637 		if (!inode) {
1638 			dput(dentry);
1639 			return -ENOMEM;
1640 		}
1641 
1642 		inode->i_fop = files[i].ops;
1643 		inode->i_ino = ++fsi->last_ino;
1644 		d_add(dentry, inode);
1645 	}
1646 
1647 	return 0;
1648 }
1649 
sel_read_initcon(struct file * file,char __user * buf,size_t count,loff_t * ppos)1650 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1651 				size_t count, loff_t *ppos)
1652 {
1653 	char *con;
1654 	u32 sid, len;
1655 	ssize_t ret;
1656 
1657 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
1658 	ret = security_sid_to_context(sid, &con, &len);
1659 	if (ret)
1660 		return ret;
1661 
1662 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1663 	kfree(con);
1664 	return ret;
1665 }
1666 
1667 static const struct file_operations sel_initcon_ops = {
1668 	.read		= sel_read_initcon,
1669 	.llseek		= generic_file_llseek,
1670 };
1671 
sel_make_initcon_files(struct dentry * dir)1672 static int sel_make_initcon_files(struct dentry *dir)
1673 {
1674 	unsigned int i;
1675 
1676 	for (i = 1; i <= SECINITSID_NUM; i++) {
1677 		struct inode *inode;
1678 		struct dentry *dentry;
1679 		const char *s = security_get_initial_sid_context(i);
1680 
1681 		if (!s)
1682 			continue;
1683 		dentry = d_alloc_name(dir, s);
1684 		if (!dentry)
1685 			return -ENOMEM;
1686 
1687 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1688 		if (!inode) {
1689 			dput(dentry);
1690 			return -ENOMEM;
1691 		}
1692 
1693 		inode->i_fop = &sel_initcon_ops;
1694 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1695 		d_add(dentry, inode);
1696 	}
1697 
1698 	return 0;
1699 }
1700 
sel_class_to_ino(u16 class)1701 static inline unsigned long sel_class_to_ino(u16 class)
1702 {
1703 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1704 }
1705 
sel_ino_to_class(unsigned long ino)1706 static inline u16 sel_ino_to_class(unsigned long ino)
1707 {
1708 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1709 }
1710 
sel_perm_to_ino(u16 class,u32 perm)1711 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1712 {
1713 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1714 }
1715 
sel_ino_to_perm(unsigned long ino)1716 static inline u32 sel_ino_to_perm(unsigned long ino)
1717 {
1718 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1719 }
1720 
sel_read_class(struct file * file,char __user * buf,size_t count,loff_t * ppos)1721 static ssize_t sel_read_class(struct file *file, char __user *buf,
1722 				size_t count, loff_t *ppos)
1723 {
1724 	unsigned long ino = file_inode(file)->i_ino;
1725 	char res[TMPBUFLEN];
1726 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1727 	return simple_read_from_buffer(buf, count, ppos, res, len);
1728 }
1729 
1730 static const struct file_operations sel_class_ops = {
1731 	.read		= sel_read_class,
1732 	.llseek		= generic_file_llseek,
1733 };
1734 
sel_read_perm(struct file * file,char __user * buf,size_t count,loff_t * ppos)1735 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1736 				size_t count, loff_t *ppos)
1737 {
1738 	unsigned long ino = file_inode(file)->i_ino;
1739 	char res[TMPBUFLEN];
1740 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1741 	return simple_read_from_buffer(buf, count, ppos, res, len);
1742 }
1743 
1744 static const struct file_operations sel_perm_ops = {
1745 	.read		= sel_read_perm,
1746 	.llseek		= generic_file_llseek,
1747 };
1748 
sel_read_policycap(struct file * file,char __user * buf,size_t count,loff_t * ppos)1749 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1750 				  size_t count, loff_t *ppos)
1751 {
1752 	int value;
1753 	char tmpbuf[TMPBUFLEN];
1754 	ssize_t length;
1755 	unsigned long i_ino = file_inode(file)->i_ino;
1756 
1757 	value = security_policycap_supported(i_ino & SEL_INO_MASK);
1758 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1759 
1760 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1761 }
1762 
1763 static const struct file_operations sel_policycap_ops = {
1764 	.read		= sel_read_policycap,
1765 	.llseek		= generic_file_llseek,
1766 };
1767 
sel_make_perm_files(struct selinux_policy * newpolicy,char * objclass,int classvalue,struct dentry * dir)1768 static int sel_make_perm_files(struct selinux_policy *newpolicy,
1769 			char *objclass, int classvalue,
1770 			struct dentry *dir)
1771 {
1772 	u32 i, nperms;
1773 	int rc;
1774 	char **perms;
1775 
1776 	rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
1777 	if (rc)
1778 		return rc;
1779 
1780 	for (i = 0; i < nperms; i++) {
1781 		struct inode *inode;
1782 		struct dentry *dentry;
1783 
1784 		rc = -ENOMEM;
1785 		dentry = d_alloc_name(dir, perms[i]);
1786 		if (!dentry)
1787 			goto out;
1788 
1789 		rc = -ENOMEM;
1790 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1791 		if (!inode) {
1792 			dput(dentry);
1793 			goto out;
1794 		}
1795 
1796 		inode->i_fop = &sel_perm_ops;
1797 		/* i+1 since perm values are 1-indexed */
1798 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1799 		d_add(dentry, inode);
1800 	}
1801 	rc = 0;
1802 out:
1803 	for (i = 0; i < nperms; i++)
1804 		kfree(perms[i]);
1805 	kfree(perms);
1806 	return rc;
1807 }
1808 
sel_make_class_dir_entries(struct selinux_policy * newpolicy,char * classname,int index,struct dentry * dir)1809 static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
1810 				char *classname, int index,
1811 				struct dentry *dir)
1812 {
1813 	struct super_block *sb = dir->d_sb;
1814 	struct selinux_fs_info *fsi = sb->s_fs_info;
1815 	struct dentry *dentry = NULL;
1816 	struct inode *inode = NULL;
1817 
1818 	dentry = d_alloc_name(dir, "index");
1819 	if (!dentry)
1820 		return -ENOMEM;
1821 
1822 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1823 	if (!inode) {
1824 		dput(dentry);
1825 		return -ENOMEM;
1826 	}
1827 
1828 	inode->i_fop = &sel_class_ops;
1829 	inode->i_ino = sel_class_to_ino(index);
1830 	d_add(dentry, inode);
1831 
1832 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1833 	if (IS_ERR(dentry))
1834 		return PTR_ERR(dentry);
1835 
1836 	return sel_make_perm_files(newpolicy, classname, index, dentry);
1837 }
1838 
sel_make_classes(struct selinux_policy * newpolicy,struct dentry * class_dir,unsigned long * last_class_ino)1839 static int sel_make_classes(struct selinux_policy *newpolicy,
1840 			    struct dentry *class_dir,
1841 			    unsigned long *last_class_ino)
1842 {
1843 	u32 i, nclasses;
1844 	int rc;
1845 	char **classes;
1846 
1847 	rc = security_get_classes(newpolicy, &classes, &nclasses);
1848 	if (rc)
1849 		return rc;
1850 
1851 	/* +2 since classes are 1-indexed */
1852 	*last_class_ino = sel_class_to_ino(nclasses + 2);
1853 
1854 	for (i = 0; i < nclasses; i++) {
1855 		struct dentry *class_name_dir;
1856 
1857 		class_name_dir = sel_make_dir(class_dir, classes[i],
1858 					      last_class_ino);
1859 		if (IS_ERR(class_name_dir)) {
1860 			rc = PTR_ERR(class_name_dir);
1861 			goto out;
1862 		}
1863 
1864 		/* i+1 since class values are 1-indexed */
1865 		rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
1866 				class_name_dir);
1867 		if (rc)
1868 			goto out;
1869 	}
1870 	rc = 0;
1871 out:
1872 	for (i = 0; i < nclasses; i++)
1873 		kfree(classes[i]);
1874 	kfree(classes);
1875 	return rc;
1876 }
1877 
sel_make_policycap(struct selinux_fs_info * fsi)1878 static int sel_make_policycap(struct selinux_fs_info *fsi)
1879 {
1880 	unsigned int iter;
1881 	struct dentry *dentry = NULL;
1882 	struct inode *inode = NULL;
1883 
1884 	for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) {
1885 		if (iter < ARRAY_SIZE(selinux_policycap_names))
1886 			dentry = d_alloc_name(fsi->policycap_dir,
1887 					      selinux_policycap_names[iter]);
1888 		else
1889 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
1890 
1891 		if (dentry == NULL)
1892 			return -ENOMEM;
1893 
1894 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
1895 		if (inode == NULL) {
1896 			dput(dentry);
1897 			return -ENOMEM;
1898 		}
1899 
1900 		inode->i_fop = &sel_policycap_ops;
1901 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1902 		d_add(dentry, inode);
1903 	}
1904 
1905 	return 0;
1906 }
1907 
sel_make_dir(struct dentry * dir,const char * name,unsigned long * ino)1908 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1909 			unsigned long *ino)
1910 {
1911 	struct dentry *dentry = d_alloc_name(dir, name);
1912 	struct inode *inode;
1913 
1914 	if (!dentry)
1915 		return ERR_PTR(-ENOMEM);
1916 
1917 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1918 	if (!inode) {
1919 		dput(dentry);
1920 		return ERR_PTR(-ENOMEM);
1921 	}
1922 
1923 	inode->i_op = &simple_dir_inode_operations;
1924 	inode->i_fop = &simple_dir_operations;
1925 	inode->i_ino = ++(*ino);
1926 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1927 	inc_nlink(inode);
1928 	d_add(dentry, inode);
1929 	/* bump link count on parent directory, too */
1930 	inc_nlink(d_inode(dir));
1931 
1932 	return dentry;
1933 }
1934 
reject_all(struct mnt_idmap * idmap,struct inode * inode,int mask)1935 static int reject_all(struct mnt_idmap *idmap, struct inode *inode, int mask)
1936 {
1937 	return -EPERM;	// no access for anyone, root or no root.
1938 }
1939 
1940 static const struct inode_operations swapover_dir_inode_operations = {
1941 	.lookup		= simple_lookup,
1942 	.permission	= reject_all,
1943 };
1944 
sel_make_swapover_dir(struct super_block * sb,unsigned long * ino)1945 static struct dentry *sel_make_swapover_dir(struct super_block *sb,
1946 						unsigned long *ino)
1947 {
1948 	struct dentry *dentry = d_alloc_name(sb->s_root, ".swapover");
1949 	struct inode *inode;
1950 
1951 	if (!dentry)
1952 		return ERR_PTR(-ENOMEM);
1953 
1954 	inode = sel_make_inode(sb, S_IFDIR);
1955 	if (!inode) {
1956 		dput(dentry);
1957 		return ERR_PTR(-ENOMEM);
1958 	}
1959 
1960 	inode->i_op = &swapover_dir_inode_operations;
1961 	inode->i_ino = ++(*ino);
1962 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1963 	inc_nlink(inode);
1964 	inode_lock(sb->s_root->d_inode);
1965 	d_add(dentry, inode);
1966 	inc_nlink(sb->s_root->d_inode);
1967 	inode_unlock(sb->s_root->d_inode);
1968 	return dentry;
1969 }
1970 
1971 #define NULL_FILE_NAME "null"
1972 
sel_fill_super(struct super_block * sb,struct fs_context * fc)1973 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
1974 {
1975 	struct selinux_fs_info *fsi;
1976 	int ret;
1977 	struct dentry *dentry;
1978 	struct inode *inode;
1979 	struct inode_security_struct *isec;
1980 
1981 	static const struct tree_descr selinux_files[] = {
1982 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1983 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1984 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1985 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1986 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1987 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1988 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1989 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1990 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1991 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1992 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1993 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1994 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1995 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1996 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1997 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1998 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1999 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
2000 					S_IWUGO},
2001 		/* last one */ {""}
2002 	};
2003 
2004 	ret = selinux_fs_info_create(sb);
2005 	if (ret)
2006 		goto err;
2007 
2008 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
2009 	if (ret)
2010 		goto err;
2011 
2012 	fsi = sb->s_fs_info;
2013 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
2014 	if (IS_ERR(fsi->bool_dir)) {
2015 		ret = PTR_ERR(fsi->bool_dir);
2016 		fsi->bool_dir = NULL;
2017 		goto err;
2018 	}
2019 
2020 	ret = -ENOMEM;
2021 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2022 	if (!dentry)
2023 		goto err;
2024 
2025 	ret = -ENOMEM;
2026 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
2027 	if (!inode) {
2028 		dput(dentry);
2029 		goto err;
2030 	}
2031 
2032 	inode->i_ino = ++fsi->last_ino;
2033 	isec = selinux_inode(inode);
2034 	isec->sid = SECINITSID_DEVNULL;
2035 	isec->sclass = SECCLASS_CHR_FILE;
2036 	isec->initialized = LABEL_INITIALIZED;
2037 
2038 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2039 	d_add(dentry, inode);
2040 
2041 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2042 	if (IS_ERR(dentry)) {
2043 		ret = PTR_ERR(dentry);
2044 		goto err;
2045 	}
2046 
2047 	ret = sel_make_avc_files(dentry);
2048 	if (ret)
2049 		goto err;
2050 
2051 	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2052 	if (IS_ERR(dentry)) {
2053 		ret = PTR_ERR(dentry);
2054 		goto err;
2055 	}
2056 
2057 	ret = sel_make_ss_files(dentry);
2058 	if (ret)
2059 		goto err;
2060 
2061 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2062 	if (IS_ERR(dentry)) {
2063 		ret = PTR_ERR(dentry);
2064 		goto err;
2065 	}
2066 
2067 	ret = sel_make_initcon_files(dentry);
2068 	if (ret)
2069 		goto err;
2070 
2071 	fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino);
2072 	if (IS_ERR(fsi->class_dir)) {
2073 		ret = PTR_ERR(fsi->class_dir);
2074 		fsi->class_dir = NULL;
2075 		goto err;
2076 	}
2077 
2078 	fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME,
2079 					  &fsi->last_ino);
2080 	if (IS_ERR(fsi->policycap_dir)) {
2081 		ret = PTR_ERR(fsi->policycap_dir);
2082 		fsi->policycap_dir = NULL;
2083 		goto err;
2084 	}
2085 
2086 	ret = sel_make_policycap(fsi);
2087 	if (ret) {
2088 		pr_err("SELinux: failed to load policy capabilities\n");
2089 		goto err;
2090 	}
2091 
2092 	return 0;
2093 err:
2094 	pr_err("SELinux: %s:  failed while creating inodes\n",
2095 		__func__);
2096 
2097 	selinux_fs_info_free(sb);
2098 
2099 	return ret;
2100 }
2101 
sel_get_tree(struct fs_context * fc)2102 static int sel_get_tree(struct fs_context *fc)
2103 {
2104 	return get_tree_single(fc, sel_fill_super);
2105 }
2106 
2107 static const struct fs_context_operations sel_context_ops = {
2108 	.get_tree	= sel_get_tree,
2109 };
2110 
sel_init_fs_context(struct fs_context * fc)2111 static int sel_init_fs_context(struct fs_context *fc)
2112 {
2113 	fc->ops = &sel_context_ops;
2114 	return 0;
2115 }
2116 
sel_kill_sb(struct super_block * sb)2117 static void sel_kill_sb(struct super_block *sb)
2118 {
2119 	selinux_fs_info_free(sb);
2120 	kill_litter_super(sb);
2121 }
2122 
2123 static struct file_system_type sel_fs_type = {
2124 	.name		= "selinuxfs",
2125 	.init_fs_context = sel_init_fs_context,
2126 	.kill_sb	= sel_kill_sb,
2127 };
2128 
2129 struct path selinux_null __ro_after_init;
2130 
init_sel_fs(void)2131 static int __init init_sel_fs(void)
2132 {
2133 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2134 					  sizeof(NULL_FILE_NAME)-1);
2135 	int err;
2136 
2137 	if (!selinux_enabled_boot)
2138 		return 0;
2139 
2140 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2141 	if (err)
2142 		return err;
2143 
2144 	err = register_filesystem(&sel_fs_type);
2145 	if (err) {
2146 		sysfs_remove_mount_point(fs_kobj, "selinux");
2147 		return err;
2148 	}
2149 
2150 	selinux_null.mnt = kern_mount(&sel_fs_type);
2151 	if (IS_ERR(selinux_null.mnt)) {
2152 		pr_err("selinuxfs:  could not mount!\n");
2153 		err = PTR_ERR(selinux_null.mnt);
2154 		selinux_null.mnt = NULL;
2155 		return err;
2156 	}
2157 
2158 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2159 						&null_name);
2160 	if (IS_ERR(selinux_null.dentry)) {
2161 		pr_err("selinuxfs:  could not lookup null!\n");
2162 		err = PTR_ERR(selinux_null.dentry);
2163 		selinux_null.dentry = NULL;
2164 		return err;
2165 	}
2166 
2167 	/*
2168 	 * Try to pre-allocate the status page, so the sequence number of the
2169 	 * initial policy load can be stored.
2170 	 */
2171 	(void) selinux_kernel_status_page();
2172 
2173 	return err;
2174 }
2175 
2176 __initcall(init_sel_fs);
2177