blob: 0dc407dac3b9791c96d7c4b1ef4f7c57eedc7870 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
Eric Paris18729812008-04-17 14:15:45 -04003 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Paul Moore82c21bf2011-08-01 11:10:33 +00005 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05006 *
Eric Paris18729812008-04-17 14:15:45 -04007 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05008 *
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Eric Paris18729812008-04-17 14:15:45 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * the Free Software Foundation, version 2.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Steve Grubbaf601e42006-01-04 14:08:39 +000029#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040030#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070031#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040032#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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
Paul Moore3bb56b22008-01-29 08:38:19 -050044/* Policy capability filenames */
45static char *policycap_names[] = {
Eric Parisb0c636b2008-02-28 12:58:40 -050046 "network_peer_controls",
Chris PeBenito2be4d742013-05-03 09:05:39 -040047 "open_perms",
48 "redhat1",
49 "always_check_network"
Paul Moore3bb56b22008-01-29 08:38:19 -050050};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
53
54static int __init checkreqprot_setup(char *str)
55{
Eric Parisf5269712008-05-14 11:27:45 -040056 unsigned long checkreqprot;
Jingoo Han29707b22014-02-05 15:13:14 +090057 if (!kstrtoul(str, 0, &checkreqprot))
Eric Parisf5269712008-05-14 11:27:45 -040058 selinux_checkreqprot = checkreqprot ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 1;
60}
61__setup("checkreqprot=", checkreqprot_setup);
62
Ingo Molnarbb003072006-03-22 00:09:14 -080063static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* global data for booleans */
Eric Paris18729812008-04-17 14:15:45 -040066static struct dentry *bool_dir;
67static int bool_num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -050068static char **bool_pending_names;
Eric Paris18729812008-04-17 14:15:45 -040069static int *bool_pending_values;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040071/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040072static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040073static unsigned long last_class_ino;
74
Eric Pariscee74f42010-10-13 17:50:25 -040075static char policy_opened;
76
Paul Moore3bb56b22008-01-29 08:38:19 -050077/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040078static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Check whether a task is allowed to use a security operation. */
81static int task_has_security(struct task_struct *tsk,
82 u32 perms)
83{
David Howellsc69e8d92008-11-14 10:39:19 +110084 const struct task_security_struct *tsec;
85 u32 sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
David Howellsc69e8d92008-11-14 10:39:19 +110087 rcu_read_lock();
88 tsec = __task_cred(tsk)->security;
89 if (tsec)
90 sid = tsec->sid;
91 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!tsec)
93 return -EACCES;
94
David Howellsc69e8d92008-11-14 10:39:19 +110095 return avc_has_perm(sid, SECINITSID_SECURITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 SECCLASS_SECURITY, perms, NULL);
97}
98
99enum 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 SEL_MEMBER, /* compute polyinstantiation membership decision */
113 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700114 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400115 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
116 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +0900117 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -0400118 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500119 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -0400120 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
James Carter6174eaf2007-04-04 16:18:39 -0400123static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
124
Paul Moore3bb56b22008-01-29 08:38:19 -0500125#define SEL_INITCON_INO_OFFSET 0x01000000
126#define SEL_BOOL_INO_OFFSET 0x02000000
127#define SEL_CLASS_INO_OFFSET 0x04000000
128#define SEL_POLICYCAP_INO_OFFSET 0x08000000
129#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TMPBUFLEN 12
132static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
133 size_t count, loff_t *ppos)
134{
135 char tmpbuf[TMPBUFLEN];
136 ssize_t length;
137
138 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
139 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
140}
141
142#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400143static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 size_t count, loff_t *ppos)
145
146{
Eric Parisb77a4932010-11-23 11:40:08 -0500147 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ssize_t length;
149 int new_value;
150
Eric Parisb77a4932010-11-23 11:40:08 -0500151 length = -ENOMEM;
Davi Arnautbfd51622005-10-30 14:59:24 -0800152 if (count >= PAGE_SIZE)
Eric Parisb77a4932010-11-23 11:40:08 -0500153 goto out;
154
155 /* No partial writes. */
Joe Perches6436a122015-03-23 18:01:35 -0700156 length = -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500157 if (*ppos != 0)
158 goto out;
159
160 length = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -0400161 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -0500163 goto out;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 length = -EFAULT;
166 if (copy_from_user(page, buf, count))
167 goto out;
168
169 length = -EINVAL;
170 if (sscanf(page, "%d", &new_value) != 1)
171 goto out;
172
173 if (new_value != selinux_enforcing) {
174 length = task_has_security(current, SECURITY__SETENFORCE);
175 if (length)
176 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000177 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500178 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
179 new_value, selinux_enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700180 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500181 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 selinux_enforcing = new_value;
183 if (selinux_enforcing)
184 avc_ss_reset(0);
185 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900186 selinux_status_update_setenforce(selinux_enforcing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 length = count;
189out:
190 free_page((unsigned long) page);
191 return length;
192}
193#else
194#define sel_write_enforce NULL
195#endif
196
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800197static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .read = sel_read_enforce,
199 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200200 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Eric Paris3f120702007-09-21 14:37:10 -0400203static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
204 size_t count, loff_t *ppos)
205{
206 char tmpbuf[TMPBUFLEN];
207 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500208 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400209 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
210 security_get_reject_unknown() : !security_get_allow_unknown();
211
212 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
213 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
214}
215
216static const struct file_operations sel_handle_unknown_ops = {
217 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200218 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400219};
220
KaiGai Kohei11904162010-09-14 18:28:39 +0900221static int sel_open_handle_status(struct inode *inode, struct file *filp)
222{
223 struct page *status = selinux_kernel_status_page();
224
225 if (!status)
226 return -ENOMEM;
227
228 filp->private_data = status;
229
230 return 0;
231}
232
233static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
234 size_t count, loff_t *ppos)
235{
236 struct page *status = filp->private_data;
237
238 BUG_ON(!status);
239
240 return simple_read_from_buffer(buf, count, ppos,
241 page_address(status),
242 sizeof(struct selinux_kernel_status));
243}
244
245static int sel_mmap_handle_status(struct file *filp,
246 struct vm_area_struct *vma)
247{
248 struct page *status = filp->private_data;
249 unsigned long size = vma->vm_end - vma->vm_start;
250
251 BUG_ON(!status);
252
253 /* only allows one page from the head */
254 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
255 return -EIO;
256 /* disallow writable mapping */
257 if (vma->vm_flags & VM_WRITE)
258 return -EPERM;
259 /* disallow mprotect() turns it into writable */
260 vma->vm_flags &= ~VM_MAYWRITE;
261
262 return remap_pfn_range(vma, vma->vm_start,
263 page_to_pfn(status),
264 size, vma->vm_page_prot);
265}
266
267static const struct file_operations sel_handle_status_ops = {
268 .open = sel_open_handle_status,
269 .read = sel_read_handle_status,
270 .mmap = sel_mmap_handle_status,
271 .llseek = generic_file_llseek,
272};
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400275static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 size_t count, loff_t *ppos)
277
278{
Eric Parisb77a4932010-11-23 11:40:08 -0500279 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ssize_t length;
281 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Eric Parisb77a4932010-11-23 11:40:08 -0500283 length = -ENOMEM;
Davi Arnautbfd51622005-10-30 14:59:24 -0800284 if (count >= PAGE_SIZE)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700285 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500286
287 /* No partial writes. */
288 length = -EINVAL;
289 if (*ppos != 0)
290 goto out;
291
292 length = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -0400293 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -0500295 goto out;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 length = -EFAULT;
298 if (copy_from_user(page, buf, count))
299 goto out;
300
301 length = -EINVAL;
302 if (sscanf(page, "%d", &new_value) != 1)
303 goto out;
304
305 if (new_value) {
306 length = selinux_disable();
Eric Parisb77a4932010-11-23 11:40:08 -0500307 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000309 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500310 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700311 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500312 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 length = count;
316out:
317 free_page((unsigned long) page);
318 return length;
319}
320#else
321#define sel_write_disable NULL
322#endif
323
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800324static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200326 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400330 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 char tmpbuf[TMPBUFLEN];
333 ssize_t length;
334
335 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
336 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
337}
338
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800339static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200341 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344/* declaration for sel_write_load */
345static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400346static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500347static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400348
349/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400350static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400351 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353static ssize_t sel_read_mls(struct file *filp, char __user *buf,
354 size_t count, loff_t *ppos)
355{
356 char tmpbuf[TMPBUFLEN];
357 ssize_t length;
358
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100359 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
360 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
362}
363
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800364static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200366 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
Eric Pariscee74f42010-10-13 17:50:25 -0400369struct policy_load_memory {
370 size_t len;
371 void *data;
372};
373
374static int sel_open_policy(struct inode *inode, struct file *filp)
375{
376 struct policy_load_memory *plm = NULL;
377 int rc;
378
379 BUG_ON(filp->private_data);
380
381 mutex_lock(&sel_mutex);
382
383 rc = task_has_security(current, SECURITY__READ_POLICY);
384 if (rc)
385 goto err;
386
387 rc = -EBUSY;
388 if (policy_opened)
389 goto err;
390
391 rc = -ENOMEM;
392 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
393 if (!plm)
394 goto err;
395
396 if (i_size_read(inode) != security_policydb_len()) {
397 mutex_lock(&inode->i_mutex);
398 i_size_write(inode, security_policydb_len());
399 mutex_unlock(&inode->i_mutex);
400 }
401
402 rc = security_read_policy(&plm->data, &plm->len);
403 if (rc)
404 goto err;
405
406 policy_opened = 1;
407
408 filp->private_data = plm;
409
410 mutex_unlock(&sel_mutex);
411
412 return 0;
413err:
414 mutex_unlock(&sel_mutex);
415
416 if (plm)
417 vfree(plm->data);
418 kfree(plm);
419 return rc;
420}
421
422static int sel_release_policy(struct inode *inode, struct file *filp)
423{
424 struct policy_load_memory *plm = filp->private_data;
425
426 BUG_ON(!plm);
427
428 policy_opened = 0;
429
430 vfree(plm->data);
431 kfree(plm);
432
433 return 0;
434}
435
436static ssize_t sel_read_policy(struct file *filp, char __user *buf,
437 size_t count, loff_t *ppos)
438{
439 struct policy_load_memory *plm = filp->private_data;
440 int ret;
441
442 mutex_lock(&sel_mutex);
443
444 ret = task_has_security(current, SECURITY__READ_POLICY);
445 if (ret)
446 goto out;
447
448 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
449out:
450 mutex_unlock(&sel_mutex);
451 return ret;
452}
453
Eric Paris845ca302010-10-13 17:50:31 -0400454static int sel_mmap_policy_fault(struct vm_area_struct *vma,
455 struct vm_fault *vmf)
456{
457 struct policy_load_memory *plm = vma->vm_file->private_data;
458 unsigned long offset;
459 struct page *page;
460
461 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
462 return VM_FAULT_SIGBUS;
463
464 offset = vmf->pgoff << PAGE_SHIFT;
465 if (offset >= roundup(plm->len, PAGE_SIZE))
466 return VM_FAULT_SIGBUS;
467
468 page = vmalloc_to_page(plm->data + offset);
469 get_page(page);
470
471 vmf->page = page;
472
473 return 0;
474}
475
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700476static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400477 .fault = sel_mmap_policy_fault,
478 .page_mkwrite = sel_mmap_policy_fault,
479};
480
James Morrisad3fa082011-08-30 10:50:12 +1000481static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400482{
483 if (vma->vm_flags & VM_SHARED) {
484 /* do not allow mprotect to make mapping writable */
485 vma->vm_flags &= ~VM_MAYWRITE;
486
487 if (vma->vm_flags & VM_WRITE)
488 return -EACCES;
489 }
490
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700491 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400492 vma->vm_ops = &sel_mmap_policy_ops;
493
494 return 0;
495}
496
Eric Pariscee74f42010-10-13 17:50:25 -0400497static const struct file_operations sel_policy_ops = {
498 .open = sel_open_policy,
499 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400500 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400501 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500502 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400503};
504
Eric Paris18729812008-04-17 14:15:45 -0400505static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 size_t count, loff_t *ppos)
507
508{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ssize_t length;
510 void *data = NULL;
511
Ingo Molnarbb003072006-03-22 00:09:14 -0800512 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 length = task_has_security(current, SECURITY__LOAD_POLICY);
515 if (length)
516 goto out;
517
Eric Parisb77a4932010-11-23 11:40:08 -0500518 /* No partial writes. */
519 length = -EINVAL;
520 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Parisb77a4932010-11-23 11:40:08 -0500523 length = -EFBIG;
524 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500526
527 length = -ENOMEM;
528 data = vmalloc(count);
529 if (!data)
530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 length = -EFAULT;
533 if (copy_from_user(data, buf, count) != 0)
534 goto out;
535
536 length = security_load_policy(data, count);
537 if (length)
538 goto out;
539
Eric Parisb77a4932010-11-23 11:40:08 -0500540 length = sel_make_bools();
541 if (length)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400542 goto out1;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400543
Eric Parisb77a4932010-11-23 11:40:08 -0500544 length = sel_make_classes();
545 if (length)
Paul Moore3bb56b22008-01-29 08:38:19 -0500546 goto out1;
Paul Moore3bb56b22008-01-29 08:38:19 -0500547
Eric Parisb77a4932010-11-23 11:40:08 -0500548 length = sel_make_policycap();
549 if (length)
550 goto out1;
551
552 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400553
554out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000555 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500556 "policy loaded auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700557 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500558 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800560 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 vfree(data);
562 return length;
563}
564
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800565static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200567 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568};
569
Eric Paris18729812008-04-17 14:15:45 -0400570static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Eric Parisb77a4932010-11-23 11:40:08 -0500572 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800573 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ssize_t length;
575
576 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
577 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +0100580 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500581 if (length)
582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800584 length = security_sid_to_context(sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500585 if (length)
586 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800587
Eric Parisb77a4932010-11-23 11:40:08 -0500588 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800589 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400590 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
591 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800592 goto out;
593 }
594
595 memcpy(buf, canon, len);
596 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800598 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return length;
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
603 size_t count, loff_t *ppos)
604{
605 char tmpbuf[TMPBUFLEN];
606 ssize_t length;
607
608 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
609 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
610}
611
Eric Paris18729812008-04-17 14:15:45 -0400612static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 size_t count, loff_t *ppos)
614{
Eric Parisb77a4932010-11-23 11:40:08 -0500615 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ssize_t length;
617 unsigned int new_value;
618
619 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
620 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric Parisb77a4932010-11-23 11:40:08 -0500623 length = -ENOMEM;
Davi Arnautbfd51622005-10-30 14:59:24 -0800624 if (count >= PAGE_SIZE)
Eric Parisb77a4932010-11-23 11:40:08 -0500625 goto out;
626
627 /* No partial writes. */
628 length = -EINVAL;
629 if (*ppos != 0)
630 goto out;
631
632 length = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -0400633 page = (char *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -0500635 goto out;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 length = -EFAULT;
638 if (copy_from_user(page, buf, count))
639 goto out;
640
641 length = -EINVAL;
642 if (sscanf(page, "%u", &new_value) != 1)
643 goto out;
644
645 selinux_checkreqprot = new_value ? 1 : 0;
646 length = count;
647out:
648 free_page((unsigned long) page);
649 return length;
650}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800651static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 .read = sel_read_checkreqprot,
653 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200654 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500657static ssize_t sel_write_validatetrans(struct file *file,
658 const char __user *buf,
659 size_t count, loff_t *ppos)
660{
661 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
662 char *req = NULL;
663 u32 osid, nsid, tsid;
664 u16 tclass;
665 int rc;
666
667 rc = task_has_security(current, SECURITY__VALIDATE_TRANS);
668 if (rc)
669 goto out;
670
671 rc = -ENOMEM;
672 if (count >= PAGE_SIZE)
673 goto out;
674
675 /* No partial writes. */
676 rc = -EINVAL;
677 if (*ppos != 0)
678 goto out;
679
680 rc = -ENOMEM;
681 req = kzalloc(count + 1, GFP_KERNEL);
682 if (!req)
683 goto out;
684
685 rc = -EFAULT;
686 if (copy_from_user(req, buf, count))
687 goto out;
688
689 rc = -ENOMEM;
690 oldcon = kzalloc(count + 1, GFP_KERNEL);
691 if (!oldcon)
692 goto out;
693
694 newcon = kzalloc(count + 1, GFP_KERNEL);
695 if (!newcon)
696 goto out;
697
698 taskcon = kzalloc(count + 1, GFP_KERNEL);
699 if (!taskcon)
700 goto out;
701
702 rc = -EINVAL;
703 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
704 goto out;
705
706 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
707 if (rc)
708 goto out;
709
710 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
711 if (rc)
712 goto out;
713
714 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
715 if (rc)
716 goto out;
717
718 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
719 if (!rc)
720 rc = count;
721out:
722 kfree(req);
723 kfree(oldcon);
724 kfree(newcon);
725 kfree(taskcon);
726 return rc;
727}
728
729static const struct file_operations sel_transition_ops = {
730 .write = sel_write_validatetrans,
731 .llseek = generic_file_llseek,
732};
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
736 */
Eric Paris18729812008-04-17 14:15:45 -0400737static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
738static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
739static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
740static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
741static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743static ssize_t (*write_op[])(struct file *, char *, size_t) = {
744 [SEL_ACCESS] = sel_write_access,
745 [SEL_CREATE] = sel_write_create,
746 [SEL_RELABEL] = sel_write_relabel,
747 [SEL_USER] = sel_write_user,
748 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800749 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};
751
752static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
753{
Al Viro496ad9a2013-01-23 17:07:38 -0500754 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 char *data;
756 ssize_t rv;
757
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800758 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EINVAL;
760
761 data = simple_transaction_get(file, buf, size);
762 if (IS_ERR(data))
763 return PTR_ERR(data);
764
Eric Paris18729812008-04-17 14:15:45 -0400765 rv = write_op[ino](file, data, size);
766 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 simple_transaction_set(file, rv);
768 rv = size;
769 }
770 return rv;
771}
772
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800773static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .write = selinux_transaction_write,
775 .read = simple_transaction_read,
776 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200777 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780/*
781 * payload - write methods
782 * If the method has a response, the response should be put in buf,
783 * and the length returned. Otherwise return 0 or and -error.
784 */
785
Eric Paris18729812008-04-17 14:15:45 -0400786static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Eric Parisb77a4932010-11-23 11:40:08 -0500788 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 u32 ssid, tsid;
790 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct av_decision avd;
792 ssize_t length;
793
794 length = task_has_security(current, SECURITY__COMPUTE_AV);
795 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800799 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Eric Parisb77a4932010-11-23 11:40:08 -0500803 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800804 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!tcon)
806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500809 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400812 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500813 if (length)
814 goto out;
815
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400816 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500817 if (length)
818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Stephen Smalley19439d02010-01-14 17:28:10 -0500820 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900823 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500824 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900826 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827out:
Eric Parisb77a4932010-11-23 11:40:08 -0500828 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 kfree(scon);
830 return length;
831}
832
Eric Paris18729812008-04-17 14:15:45 -0400833static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Eric Parisb77a4932010-11-23 11:40:08 -0500835 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100836 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 u32 ssid, tsid, newsid;
838 u16 tclass;
839 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500840 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100842 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
845 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500846 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800849 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Eric Parisb77a4932010-11-23 11:40:08 -0500853 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800854 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!tcon)
856 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100858 length = -ENOMEM;
859 namebuf = kzalloc(size + 1, GFP_KERNEL);
860 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100863 length = -EINVAL;
864 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
865 if (nargs < 3 || nargs > 4)
866 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400867 if (nargs == 4) {
868 /*
869 * If and when the name of new object to be queried contains
870 * either whitespace or multibyte characters, they shall be
871 * encoded based on the percentage-encoding rule.
872 * If not encoded, the sscanf logic picks up only left-half
873 * of the supplied name; splitted by a whitespace unexpectedly.
874 */
875 char *r, *w;
876 int c1, c2;
877
878 r = w = namebuf;
879 do {
880 c1 = *r++;
881 if (c1 == '+')
882 c1 = ' ';
883 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800884 c1 = hex_to_bin(*r++);
885 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400886 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800887 c2 = hex_to_bin(*r++);
888 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400889 goto out;
890 c1 = (c1 << 4) | c2;
891 }
892 *w++ = c1;
893 } while (c1 != '\0');
894
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100895 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400896 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100897
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400898 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500899 if (length)
900 goto out;
901
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400902 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500903 if (length)
904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100906 length = security_transition_sid_user(ssid, tsid, tclass,
907 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500908 if (length)
909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500912 if (length)
913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric Parisb77a4932010-11-23 11:40:08 -0500915 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400917 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
918 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 memcpy(buf, newcon, len);
923 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924out:
Eric Parisb77a4932010-11-23 11:40:08 -0500925 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100926 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500927 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 kfree(scon);
929 return length;
930}
931
Eric Paris18729812008-04-17 14:15:45 -0400932static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Eric Parisb77a4932010-11-23 11:40:08 -0500934 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 u32 ssid, tsid, newsid;
936 u16 tclass;
937 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500938 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 u32 len;
940
941 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
942 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800946 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500948 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Parisb77a4932010-11-23 11:40:08 -0500950 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800951 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!tcon)
953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 length = -EINVAL;
956 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400959 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500960 if (length)
961 goto out;
962
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400963 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500964 if (length)
965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 length = security_change_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500968 if (length)
969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500972 if (length)
973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Eric Parisb77a4932010-11-23 11:40:08 -0500975 length = -ERANGE;
976 if (len > SIMPLE_TRANSACTION_LIMIT)
977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 memcpy(buf, newcon, len);
980 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981out:
Eric Parisb77a4932010-11-23 11:40:08 -0500982 kfree(newcon);
983 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 kfree(scon);
985 return length;
986}
987
Eric Paris18729812008-04-17 14:15:45 -0400988static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Eric Parisb77a4932010-11-23 11:40:08 -0500990 char *con = NULL, *user = NULL, *ptr;
991 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ssize_t length;
993 char *newcon;
994 int i, rc;
995 u32 len, nsids;
996
997 length = task_has_security(current, SECURITY__COMPUTE_USER);
998 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001002 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Parisb77a4932010-11-23 11:40:08 -05001006 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001007 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!user)
1009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 length = -EINVAL;
1012 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001015 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001016 if (length)
1017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 length = security_get_user_sids(sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001020 if (length)
1021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 length = sprintf(buf, "%u", nsids) + 1;
1024 ptr = buf + length;
1025 for (i = 0; i < nsids; i++) {
1026 rc = security_sid_to_context(sids[i], &newcon, &len);
1027 if (rc) {
1028 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001029 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1032 kfree(newcon);
1033 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 memcpy(ptr, newcon, len);
1037 kfree(newcon);
1038 ptr += len;
1039 length += len;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041out:
Eric Parisb77a4932010-11-23 11:40:08 -05001042 kfree(sids);
1043 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 kfree(con);
1045 return length;
1046}
1047
Eric Paris18729812008-04-17 14:15:45 -04001048static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Eric Parisb77a4932010-11-23 11:40:08 -05001050 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 u32 ssid, tsid, newsid;
1052 u16 tclass;
1053 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001054 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 u32 len;
1056
1057 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
1058 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001062 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Eric Parisb77a4932010-11-23 11:40:08 -05001066 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001067 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!tcon)
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 length = -EINVAL;
1072 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001075 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001076 if (length)
1077 goto out;
1078
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001079 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001080 if (length)
1081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 length = security_member_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001084 if (length)
1085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001088 if (length)
1089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Eric Parisb77a4932010-11-23 11:40:08 -05001091 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001093 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1094 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001095 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 memcpy(buf, newcon, len);
1099 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100out:
Eric Parisb77a4932010-11-23 11:40:08 -05001101 kfree(newcon);
1102 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 kfree(scon);
1104 return length;
1105}
1106
1107static struct inode *sel_make_inode(struct super_block *sb, int mode)
1108{
1109 struct inode *ret = new_inode(sb);
1110
1111 if (ret) {
1112 ret->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
1114 }
1115 return ret;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1119 size_t count, loff_t *ppos)
1120{
1121 char *page = NULL;
1122 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ssize_t ret;
1124 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001125 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001126 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Ingo Molnarbb003072006-03-22 00:09:14 -08001128 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Eric Parisb77a4932010-11-23 11:40:08 -05001130 ret = -EINVAL;
1131 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric Parisb77a4932010-11-23 11:40:08 -05001134 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001135 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001136 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001139 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (cur_enforcing < 0) {
1141 ret = cur_enforcing;
1142 goto out;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001145 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001146 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001148 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001149 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return ret;
1151}
1152
1153static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1154 size_t count, loff_t *ppos)
1155{
1156 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001157 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001159 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001160 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Ingo Molnarbb003072006-03-22 00:09:14 -08001162 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 length = task_has_security(current, SECURITY__SETBOOL);
1165 if (length)
1166 goto out;
1167
Eric Parisb77a4932010-11-23 11:40:08 -05001168 length = -EINVAL;
1169 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001170 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001171
Eric Parisb77a4932010-11-23 11:40:08 -05001172 length = -ENOMEM;
1173 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001175
Eric Parisb77a4932010-11-23 11:40:08 -05001176 /* No partial writes. */
1177 length = -EINVAL;
1178 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001180
1181 length = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001182 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001183 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001186 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (copy_from_user(page, buf, count))
1188 goto out;
1189
1190 length = -EINVAL;
1191 if (sscanf(page, "%d", &new_value) != 1)
1192 goto out;
1193
1194 if (new_value)
1195 new_value = 1;
1196
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001197 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 length = count;
1199
1200out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001201 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001202 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return length;
1204}
1205
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001206static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001207 .read = sel_read_bool,
1208 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001209 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210};
1211
1212static ssize_t sel_commit_bools_write(struct file *filep,
1213 const char __user *buf,
1214 size_t count, loff_t *ppos)
1215{
1216 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001217 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int new_value;
1219
Ingo Molnarbb003072006-03-22 00:09:14 -08001220 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 length = task_has_security(current, SECURITY__SETBOOL);
1223 if (length)
1224 goto out;
1225
Eric Parisb77a4932010-11-23 11:40:08 -05001226 length = -ENOMEM;
1227 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001229
1230 /* No partial writes. */
1231 length = -EINVAL;
1232 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001234
1235 length = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001236 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001237 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001240 length = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (copy_from_user(page, buf, count))
1242 goto out;
1243
1244 length = -EINVAL;
1245 if (sscanf(page, "%d", &new_value) != 1)
1246 goto out;
1247
Eric Parisb77a4932010-11-23 11:40:08 -05001248 length = 0;
Eric Paris18729812008-04-17 14:15:45 -04001249 if (new_value && bool_pending_values)
Eric Parisb77a4932010-11-23 11:40:08 -05001250 length = security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Eric Parisb77a4932010-11-23 11:40:08 -05001252 if (!length)
1253 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255out:
Ingo Molnarbb003072006-03-22 00:09:14 -08001256 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001257 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return length;
1259}
1260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001261static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001262 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001263 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264};
1265
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001266static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Al Viroad521842014-12-24 14:56:48 -05001268 d_genocide(de);
1269 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272#define BOOL_DIR_NAME "booleans"
1273
1274static int sel_make_bools(void)
1275{
Eric Parisb77a4932010-11-23 11:40:08 -05001276 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 ssize_t len;
1278 struct dentry *dentry = NULL;
1279 struct dentry *dir = bool_dir;
1280 struct inode *inode = NULL;
1281 struct inode_security_struct *isec;
1282 char **names = NULL, *page;
1283 int num;
1284 int *values = NULL;
1285 u32 sid;
1286
1287 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001288 for (i = 0; i < bool_num; i++)
1289 kfree(bool_pending_names[i]);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001290 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001291 kfree(bool_pending_values);
Eric Paris154c50c2012-04-04 13:47:11 -04001292 bool_num = 0;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001293 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001294 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001296 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Eric Parisb77a4932010-11-23 11:40:08 -05001298 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001299 page = (char *)get_zeroed_page(GFP_KERNEL);
1300 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 ret = security_get_bools(&num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001304 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto out;
1306
1307 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001308 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001310 if (!dentry)
1311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Eric Parisb77a4932010-11-23 11:40:08 -05001313 ret = -ENOMEM;
1314 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1315 if (!inode)
1316 goto out;
1317
Eric Parisb77a4932010-11-23 11:40:08 -05001318 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001319 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001320 if (len >= PAGE_SIZE)
1321 goto out;
1322
Eric Paris18729812008-04-17 14:15:45 -04001323 isec = (struct inode_security_struct *)inode->i_security;
1324 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1325 if (ret)
Eric Parisb77a4932010-11-23 11:40:08 -05001326 goto out;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 isec->sid = sid;
1329 isec->initialized = 1;
1330 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001331 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 d_add(dentry, inode);
1333 }
1334 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001335 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001337
1338 free_page((unsigned long)page);
1339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340out:
1341 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001344 for (i = 0; i < num; i++)
1345 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 kfree(names);
1347 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001348 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001349 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001350
1351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354#define NULL_FILE_NAME "null"
1355
Al Viro765927b2012-06-26 21:58:53 +04001356struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1359 size_t count, loff_t *ppos)
1360{
1361 char tmpbuf[TMPBUFLEN];
1362 ssize_t length;
1363
1364 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1365 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1366}
1367
Eric Paris18729812008-04-17 14:15:45 -04001368static ssize_t sel_write_avc_cache_threshold(struct file *file,
1369 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 size_t count, loff_t *ppos)
1371
1372{
Eric Parisb77a4932010-11-23 11:40:08 -05001373 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ssize_t ret;
1375 int new_value;
1376
Eric Parisb77a4932010-11-23 11:40:08 -05001377 ret = task_has_security(current, SECURITY__SETSECPARAM);
1378 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Eric Parisb77a4932010-11-23 11:40:08 -05001381 ret = -ENOMEM;
1382 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Parisb77a4932010-11-23 11:40:08 -05001385 /* No partial writes. */
1386 ret = -EINVAL;
1387 if (*ppos != 0)
1388 goto out;
1389
1390 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001391 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001392 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Eric Parisb77a4932010-11-23 11:40:08 -05001395 ret = -EFAULT;
1396 if (copy_from_user(page, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Eric Parisb77a4932010-11-23 11:40:08 -05001399 ret = -EINVAL;
1400 if (sscanf(page, "%u", &new_value) != 1)
1401 goto out;
1402
1403 avc_cache_threshold = new_value;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406out:
Eric Parisb77a4932010-11-23 11:40:08 -05001407 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return ret;
1409}
1410
1411static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1412 size_t count, loff_t *ppos)
1413{
1414 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001415 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001418 if (!page)
1419 return -ENOMEM;
1420
1421 length = avc_get_hash_stats(page);
1422 if (length >= 0)
1423 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001425
1426 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001429static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 .read = sel_read_avc_cache_threshold,
1431 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001432 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433};
1434
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001435static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001437 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438};
1439
1440#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1441static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1442{
1443 int cpu;
1444
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301445 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (!cpu_possible(cpu))
1447 continue;
1448 *idx = cpu + 1;
1449 return &per_cpu(avc_cache_stats, cpu);
1450 }
1451 return NULL;
1452}
1453
1454static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1455{
1456 loff_t n = *pos - 1;
1457
1458 if (*pos == 0)
1459 return SEQ_START_TOKEN;
1460
1461 return sel_avc_get_stat_idx(&n);
1462}
1463
1464static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1465{
1466 return sel_avc_get_stat_idx(pos);
1467}
1468
1469static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1470{
1471 struct avc_cache_stats *st = v;
1472
1473 if (v == SEQ_START_TOKEN)
1474 seq_printf(seq, "lookups hits misses allocations reclaims "
1475 "frees\n");
Linus Torvalds257313b2011-05-19 21:22:53 -07001476 else {
1477 unsigned int lookups = st->lookups;
1478 unsigned int misses = st->misses;
1479 unsigned int hits = lookups - misses;
1480 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1481 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
1485}
1486
1487static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1488{ }
1489
Jan Engelhardt1996a102008-01-23 00:02:58 +01001490static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .start = sel_avc_stats_seq_start,
1492 .next = sel_avc_stats_seq_next,
1493 .show = sel_avc_stats_seq_show,
1494 .stop = sel_avc_stats_seq_stop,
1495};
1496
1497static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1498{
1499 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1500}
1501
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001502static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 .open = sel_open_avc_cache_stats,
1504 .read = seq_read,
1505 .llseek = seq_lseek,
1506 .release = seq_release,
1507};
1508#endif
1509
1510static int sel_make_avc_files(struct dentry *dir)
1511{
Eric Parisb77a4932010-11-23 11:40:08 -05001512 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 static struct tree_descr files[] = {
1514 { "cache_threshold",
1515 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1516 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1517#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1518 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1519#endif
1520 };
1521
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001522 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct inode *inode;
1524 struct dentry *dentry;
1525
1526 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001527 if (!dentry)
1528 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001531 if (!inode)
1532 return -ENOMEM;
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001535 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 d_add(dentry, inode);
1537 }
Eric Parisb77a4932010-11-23 11:40:08 -05001538
1539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Eric Paris18729812008-04-17 14:15:45 -04001542static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001543 size_t count, loff_t *ppos)
1544{
James Carterf0ee2e42007-04-04 10:11:29 -04001545 char *con;
1546 u32 sid, len;
1547 ssize_t ret;
1548
Al Viro496ad9a2013-01-23 17:07:38 -05001549 sid = file_inode(file)->i_ino&SEL_INO_MASK;
James Carterf0ee2e42007-04-04 10:11:29 -04001550 ret = security_sid_to_context(sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001551 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001552 return ret;
1553
1554 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1555 kfree(con);
1556 return ret;
1557}
1558
1559static const struct file_operations sel_initcon_ops = {
1560 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001561 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001562};
1563
1564static int sel_make_initcon_files(struct dentry *dir)
1565{
Eric Parisb77a4932010-11-23 11:40:08 -05001566 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001567
1568 for (i = 1; i <= SECINITSID_NUM; i++) {
1569 struct inode *inode;
1570 struct dentry *dentry;
1571 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001572 if (!dentry)
1573 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001574
1575 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001576 if (!inode)
1577 return -ENOMEM;
1578
James Carterf0ee2e42007-04-04 10:11:29 -04001579 inode->i_fop = &sel_initcon_ops;
1580 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1581 d_add(dentry, inode);
1582 }
Eric Parisb77a4932010-11-23 11:40:08 -05001583
1584 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001585}
1586
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001587static inline unsigned long sel_class_to_ino(u16 class)
1588{
1589 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1590}
1591
1592static inline u16 sel_ino_to_class(unsigned long ino)
1593{
Eric Paris92ae9e82012-04-04 13:46:46 -04001594 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001595}
1596
1597static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1598{
1599 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1600}
1601
1602static inline u32 sel_ino_to_perm(unsigned long ino)
1603{
1604 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1605}
1606
Eric Paris18729812008-04-17 14:15:45 -04001607static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001608 size_t count, loff_t *ppos)
1609{
Al Viro496ad9a2013-01-23 17:07:38 -05001610 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001611 char res[TMPBUFLEN];
1612 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1613 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001614}
1615
1616static const struct file_operations sel_class_ops = {
1617 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001618 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001619};
1620
Eric Paris18729812008-04-17 14:15:45 -04001621static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001622 size_t count, loff_t *ppos)
1623{
Al Viro496ad9a2013-01-23 17:07:38 -05001624 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001625 char res[TMPBUFLEN];
1626 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1627 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001628}
1629
1630static const struct file_operations sel_perm_ops = {
1631 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001632 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001633};
1634
Paul Moore3bb56b22008-01-29 08:38:19 -05001635static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1636 size_t count, loff_t *ppos)
1637{
1638 int value;
1639 char tmpbuf[TMPBUFLEN];
1640 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001641 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001642
1643 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1644 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1645
1646 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1647}
1648
1649static const struct file_operations sel_policycap_ops = {
1650 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001651 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001652};
1653
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001654static int sel_make_perm_files(char *objclass, int classvalue,
1655 struct dentry *dir)
1656{
Eric Parisb77a4932010-11-23 11:40:08 -05001657 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001658 char **perms;
1659
1660 rc = security_get_permissions(objclass, &perms, &nperms);
1661 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001662 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001663
1664 for (i = 0; i < nperms; i++) {
1665 struct inode *inode;
1666 struct dentry *dentry;
1667
Eric Parisb77a4932010-11-23 11:40:08 -05001668 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001669 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001670 if (!dentry)
1671 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001672
Eric Parisb77a4932010-11-23 11:40:08 -05001673 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001674 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001675 if (!inode)
1676 goto out;
1677
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001678 inode->i_fop = &sel_perm_ops;
1679 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001680 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001681 d_add(dentry, inode);
1682 }
Eric Parisb77a4932010-11-23 11:40:08 -05001683 rc = 0;
1684out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001685 for (i = 0; i < nperms; i++)
1686 kfree(perms[i]);
1687 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001688 return rc;
1689}
1690
1691static int sel_make_class_dir_entries(char *classname, int index,
1692 struct dentry *dir)
1693{
1694 struct dentry *dentry = NULL;
1695 struct inode *inode = NULL;
1696 int rc;
1697
1698 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001699 if (!dentry)
1700 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001701
1702 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001703 if (!inode)
1704 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001705
1706 inode->i_fop = &sel_class_ops;
1707 inode->i_ino = sel_class_to_ino(index);
1708 d_add(dentry, inode);
1709
Al Viroa1c2aa12012-03-18 20:36:59 -04001710 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1711 if (IS_ERR(dentry))
1712 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001713
1714 rc = sel_make_perm_files(classname, index, dentry);
1715
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001716 return rc;
1717}
1718
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001719static int sel_make_classes(void)
1720{
Eric Parisb77a4932010-11-23 11:40:08 -05001721 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001722 char **classes;
1723
1724 /* delete any existing entries */
Al Viroad521842014-12-24 14:56:48 -05001725 sel_remove_entries(class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001726
1727 rc = security_get_classes(&classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001728 if (rc)
1729 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001730
1731 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001732 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001733
1734 for (i = 0; i < nclasses; i++) {
1735 struct dentry *class_name_dir;
1736
Al Viroa1c2aa12012-03-18 20:36:59 -04001737 class_name_dir = sel_make_dir(class_dir, classes[i],
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001738 &last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001739 if (IS_ERR(class_name_dir)) {
1740 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001741 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001742 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001743
1744 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001745 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001746 class_name_dir);
1747 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001748 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001749 }
Eric Parisb77a4932010-11-23 11:40:08 -05001750 rc = 0;
1751out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001752 for (i = 0; i < nclasses; i++)
1753 kfree(classes[i]);
1754 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001755 return rc;
1756}
1757
Paul Moore3bb56b22008-01-29 08:38:19 -05001758static int sel_make_policycap(void)
1759{
1760 unsigned int iter;
1761 struct dentry *dentry = NULL;
1762 struct inode *inode = NULL;
1763
1764 sel_remove_entries(policycap_dir);
1765
1766 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1767 if (iter < ARRAY_SIZE(policycap_names))
1768 dentry = d_alloc_name(policycap_dir,
1769 policycap_names[iter]);
1770 else
1771 dentry = d_alloc_name(policycap_dir, "unknown");
1772
1773 if (dentry == NULL)
1774 return -ENOMEM;
1775
1776 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1777 if (inode == NULL)
1778 return -ENOMEM;
1779
1780 inode->i_fop = &sel_policycap_ops;
1781 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1782 d_add(dentry, inode);
1783 }
1784
1785 return 0;
1786}
1787
Al Viroa1c2aa12012-03-18 20:36:59 -04001788static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001789 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Al Viroa1c2aa12012-03-18 20:36:59 -04001791 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct inode *inode;
1793
Al Viroa1c2aa12012-03-18 20:36:59 -04001794 if (!dentry)
1795 return ERR_PTR(-ENOMEM);
1796
1797 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1798 if (!inode) {
1799 dput(dentry);
1800 return ERR_PTR(-ENOMEM);
1801 }
Eric Parisb77a4932010-11-23 11:40:08 -05001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 inode->i_op = &simple_dir_inode_operations;
1804 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001805 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001806 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001807 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001809 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001810 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001811
Al Viroa1c2aa12012-03-18 20:36:59 -04001812 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Eric Paris18729812008-04-17 14:15:45 -04001815static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 int ret;
1818 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001819 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct inode_security_struct *isec;
1821
1822 static struct tree_descr selinux_files[] = {
1823 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1824 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001825 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1827 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1828 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1829 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1830 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1831 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1832 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1833 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1834 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1835 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001836 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1837 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001838 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001839 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001840 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1841 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* last one */ {""}
1843 };
1844 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1845 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001846 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Al Viroa1c2aa12012-03-18 20:36:59 -04001848 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1849 if (IS_ERR(bool_dir)) {
1850 ret = PTR_ERR(bool_dir);
1851 bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001852 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Eric Parisb77a4932010-11-23 11:40:08 -05001855 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001857 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001858 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Eric Parisb77a4932010-11-23 11:40:08 -05001860 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001862 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001863 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001864
James Carter6174eaf2007-04-04 16:18:39 -04001865 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001866 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 isec->sid = SECINITSID_DEVNULL;
1868 isec->sclass = SECCLASS_CHR_FILE;
1869 isec->initialized = 1;
1870
1871 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1872 d_add(dentry, inode);
Al Viro765927b2012-06-26 21:58:53 +04001873 selinux_null.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Al Viroa1c2aa12012-03-18 20:36:59 -04001875 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1876 if (IS_ERR(dentry)) {
1877 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001878 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 ret = sel_make_avc_files(dentry);
1882 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001883 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001884
Al Viroa1c2aa12012-03-18 20:36:59 -04001885 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1886 if (IS_ERR(dentry)) {
1887 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001888 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001889 }
James Carterf0ee2e42007-04-04 10:11:29 -04001890
1891 ret = sel_make_initcon_files(dentry);
1892 if (ret)
1893 goto err;
1894
Al Viroa1c2aa12012-03-18 20:36:59 -04001895 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1896 if (IS_ERR(class_dir)) {
1897 ret = PTR_ERR(class_dir);
1898 class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001899 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001900 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001901
Al Viroa1c2aa12012-03-18 20:36:59 -04001902 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1903 if (IS_ERR(policycap_dir)) {
1904 ret = PTR_ERR(policycap_dir);
1905 policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001906 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001907 }
Eric Parisb77a4932010-11-23 11:40:08 -05001908 return 0;
James Morris161ce452006-03-22 00:09:17 -08001909err:
Eric Paris744ba352008-04-17 11:52:44 -04001910 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1911 __func__);
Eric Parisb77a4932010-11-23 11:40:08 -05001912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Al Virofc14f2f2010-07-25 01:48:30 +04001915static struct dentry *sel_mount(struct file_system_type *fs_type,
1916 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Al Virofc14f2f2010-07-25 01:48:30 +04001918 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921static struct file_system_type sel_fs_type = {
1922 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001923 .mount = sel_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 .kill_sb = kill_litter_super,
1925};
1926
1927struct vfsmount *selinuxfs_mount;
1928
1929static int __init init_sel_fs(void)
1930{
1931 int err;
1932
1933 if (!selinux_enabled)
1934 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001935
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001936 err = sysfs_create_mount_point(fs_kobj, "selinux");
1937 if (err)
1938 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001941 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001942 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05001943 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001944 }
Eric Parisb77a4932010-11-23 11:40:08 -05001945
Al Viro765927b2012-06-26 21:58:53 +04001946 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05001947 if (IS_ERR(selinuxfs_mount)) {
1948 printk(KERN_ERR "selinuxfs: could not mount!\n");
1949 err = PTR_ERR(selinuxfs_mount);
1950 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Eric Parisb77a4932010-11-23 11:40:08 -05001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return err;
1954}
1955
1956__initcall(init_sel_fs);
1957
1958#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1959void exit_sel_fs(void)
1960{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001961 sysfs_remove_mount_point(fs_kobj, "selinux");
Tim Chen423e0ab2011-07-19 09:32:38 -07001962 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 unregister_filesystem(&sel_fs_type);
1964}
1965#endif