blob: 55345f84f17d31fbafae6cf048d1123e5bbfb22f [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 Molnarbb0030792006-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",
Stephen Smalleyda69a532017-01-09 10:07:30 -050048 "extended_socket_class",
Chris PeBenito2be4d742013-05-03 09:05:39 -040049 "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 Molnarbb0030792006-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. PeBenitoe47c8fc52007-05-23 09:12:09 -040071/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040072static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc52007-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 -070080enum sel_inos {
81 SEL_ROOT_INO = 2,
82 SEL_LOAD, /* load policy */
83 SEL_ENFORCE, /* get or set enforcing status */
84 SEL_CONTEXT, /* validate context */
85 SEL_ACCESS, /* compute access decision */
86 SEL_CREATE, /* compute create labeling decision */
87 SEL_RELABEL, /* compute relabeling decision */
88 SEL_USER, /* compute reachable user contexts */
89 SEL_POLICYVERS, /* return policy version for this kernel */
90 SEL_COMMIT_BOOLS, /* commit new boolean values */
91 SEL_MLS, /* return if MLS policy is enabled */
92 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 SEL_MEMBER, /* compute polyinstantiation membership decision */
94 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070095 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040096 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
97 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090098 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040099 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500100 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -0400101 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
James Carter6174eaf2007-04-04 16:18:39 -0400104static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
105
Paul Moore3bb56b22008-01-29 08:38:19 -0500106#define SEL_INITCON_INO_OFFSET 0x01000000
107#define SEL_BOOL_INO_OFFSET 0x02000000
108#define SEL_CLASS_INO_OFFSET 0x04000000
109#define SEL_POLICYCAP_INO_OFFSET 0x08000000
110#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define TMPBUFLEN 12
113static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
114 size_t count, loff_t *ppos)
115{
116 char tmpbuf[TMPBUFLEN];
117 ssize_t length;
118
119 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
120 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
121}
122
123#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400124static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 size_t count, loff_t *ppos)
126
127{
Eric Parisb77a4932010-11-23 11:40:08 -0500128 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 ssize_t length;
130 int new_value;
131
Davi Arnautbfd51622005-10-30 14:59:24 -0800132 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500133 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500134
135 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500136 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500137 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500138
Al Viro8365a712015-12-24 00:08:06 -0500139 page = memdup_user_nul(buf, count);
140 if (IS_ERR(page))
141 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 length = -EINVAL;
144 if (sscanf(page, "%d", &new_value) != 1)
145 goto out;
146
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500147 new_value = !!new_value;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (new_value != selinux_enforcing) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500150 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
151 SECCLASS_SECURITY, SECURITY__SETENFORCE,
152 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (length)
154 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000155 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500156 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
157 new_value, selinux_enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700158 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500159 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 selinux_enforcing = new_value;
161 if (selinux_enforcing)
162 avc_ss_reset(0);
163 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900164 selinux_status_update_setenforce(selinux_enforcing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 length = count;
167out:
Al Viro8365a712015-12-24 00:08:06 -0500168 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return length;
170}
171#else
172#define sel_write_enforce NULL
173#endif
174
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800175static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .read = sel_read_enforce,
177 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200178 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
Eric Paris3f120702007-09-21 14:37:10 -0400181static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
182 size_t count, loff_t *ppos)
183{
184 char tmpbuf[TMPBUFLEN];
185 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500186 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400187 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
188 security_get_reject_unknown() : !security_get_allow_unknown();
189
190 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
191 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
192}
193
194static const struct file_operations sel_handle_unknown_ops = {
195 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200196 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400197};
198
KaiGai Kohei11904162010-09-14 18:28:39 +0900199static int sel_open_handle_status(struct inode *inode, struct file *filp)
200{
201 struct page *status = selinux_kernel_status_page();
202
203 if (!status)
204 return -ENOMEM;
205
206 filp->private_data = status;
207
208 return 0;
209}
210
211static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
212 size_t count, loff_t *ppos)
213{
214 struct page *status = filp->private_data;
215
216 BUG_ON(!status);
217
218 return simple_read_from_buffer(buf, count, ppos,
219 page_address(status),
220 sizeof(struct selinux_kernel_status));
221}
222
223static int sel_mmap_handle_status(struct file *filp,
224 struct vm_area_struct *vma)
225{
226 struct page *status = filp->private_data;
227 unsigned long size = vma->vm_end - vma->vm_start;
228
229 BUG_ON(!status);
230
231 /* only allows one page from the head */
232 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
233 return -EIO;
234 /* disallow writable mapping */
235 if (vma->vm_flags & VM_WRITE)
236 return -EPERM;
237 /* disallow mprotect() turns it into writable */
238 vma->vm_flags &= ~VM_MAYWRITE;
239
240 return remap_pfn_range(vma, vma->vm_start,
241 page_to_pfn(status),
242 size, vma->vm_page_prot);
243}
244
245static const struct file_operations sel_handle_status_ops = {
246 .open = sel_open_handle_status,
247 .read = sel_read_handle_status,
248 .mmap = sel_mmap_handle_status,
249 .llseek = generic_file_llseek,
250};
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400253static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 size_t count, loff_t *ppos)
255
256{
Al Viro8365a712015-12-24 00:08:06 -0500257 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ssize_t length;
259 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Davi Arnautbfd51622005-10-30 14:59:24 -0800261 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500262 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500263
264 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500265 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500266 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500267
Al Viro8365a712015-12-24 00:08:06 -0500268 page = memdup_user_nul(buf, count);
269 if (IS_ERR(page))
270 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 length = -EINVAL;
273 if (sscanf(page, "%d", &new_value) != 1)
274 goto out;
275
276 if (new_value) {
277 length = selinux_disable();
Eric Parisb77a4932010-11-23 11:40:08 -0500278 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000280 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500281 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700282 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500283 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 length = count;
287out:
Al Viro8365a712015-12-24 00:08:06 -0500288 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return length;
290}
291#else
292#define sel_write_disable NULL
293#endif
294
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800295static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200297 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};
299
300static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400301 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 char tmpbuf[TMPBUFLEN];
304 ssize_t length;
305
306 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
307 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
308}
309
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800310static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200312 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
315/* declaration for sel_write_load */
316static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400317static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500318static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400319
320/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400321static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400322 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324static ssize_t sel_read_mls(struct file *filp, char __user *buf,
325 size_t count, loff_t *ppos)
326{
327 char tmpbuf[TMPBUFLEN];
328 ssize_t length;
329
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100330 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
331 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
333}
334
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800335static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200337 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338};
339
Eric Pariscee74f42010-10-13 17:50:25 -0400340struct policy_load_memory {
341 size_t len;
342 void *data;
343};
344
345static int sel_open_policy(struct inode *inode, struct file *filp)
346{
347 struct policy_load_memory *plm = NULL;
348 int rc;
349
350 BUG_ON(filp->private_data);
351
352 mutex_lock(&sel_mutex);
353
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500354 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
355 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400356 if (rc)
357 goto err;
358
359 rc = -EBUSY;
360 if (policy_opened)
361 goto err;
362
363 rc = -ENOMEM;
364 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
365 if (!plm)
366 goto err;
367
368 if (i_size_read(inode) != security_policydb_len()) {
Al Viro59551022016-01-22 15:40:57 -0500369 inode_lock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400370 i_size_write(inode, security_policydb_len());
Al Viro59551022016-01-22 15:40:57 -0500371 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400372 }
373
374 rc = security_read_policy(&plm->data, &plm->len);
375 if (rc)
376 goto err;
377
378 policy_opened = 1;
379
380 filp->private_data = plm;
381
382 mutex_unlock(&sel_mutex);
383
384 return 0;
385err:
386 mutex_unlock(&sel_mutex);
387
388 if (plm)
389 vfree(plm->data);
390 kfree(plm);
391 return rc;
392}
393
394static int sel_release_policy(struct inode *inode, struct file *filp)
395{
396 struct policy_load_memory *plm = filp->private_data;
397
398 BUG_ON(!plm);
399
400 policy_opened = 0;
401
402 vfree(plm->data);
403 kfree(plm);
404
405 return 0;
406}
407
408static ssize_t sel_read_policy(struct file *filp, char __user *buf,
409 size_t count, loff_t *ppos)
410{
411 struct policy_load_memory *plm = filp->private_data;
412 int ret;
413
414 mutex_lock(&sel_mutex);
415
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500416 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
417 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400418 if (ret)
419 goto out;
420
421 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
422out:
423 mutex_unlock(&sel_mutex);
424 return ret;
425}
426
Eric Paris845ca302010-10-13 17:50:31 -0400427static int sel_mmap_policy_fault(struct vm_area_struct *vma,
428 struct vm_fault *vmf)
429{
430 struct policy_load_memory *plm = vma->vm_file->private_data;
431 unsigned long offset;
432 struct page *page;
433
434 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
435 return VM_FAULT_SIGBUS;
436
437 offset = vmf->pgoff << PAGE_SHIFT;
438 if (offset >= roundup(plm->len, PAGE_SIZE))
439 return VM_FAULT_SIGBUS;
440
441 page = vmalloc_to_page(plm->data + offset);
442 get_page(page);
443
444 vmf->page = page;
445
446 return 0;
447}
448
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700449static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400450 .fault = sel_mmap_policy_fault,
451 .page_mkwrite = sel_mmap_policy_fault,
452};
453
James Morrisad3fa082011-08-30 10:50:12 +1000454static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400455{
456 if (vma->vm_flags & VM_SHARED) {
457 /* do not allow mprotect to make mapping writable */
458 vma->vm_flags &= ~VM_MAYWRITE;
459
460 if (vma->vm_flags & VM_WRITE)
461 return -EACCES;
462 }
463
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700464 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400465 vma->vm_ops = &sel_mmap_policy_ops;
466
467 return 0;
468}
469
Eric Pariscee74f42010-10-13 17:50:25 -0400470static const struct file_operations sel_policy_ops = {
471 .open = sel_open_policy,
472 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400473 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400474 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500475 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400476};
477
Eric Paris18729812008-04-17 14:15:45 -0400478static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 size_t count, loff_t *ppos)
480
481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ssize_t length;
483 void *data = NULL;
484
Ingo Molnarbb0030792006-03-22 00:09:14 -0800485 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500487 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
488 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (length)
490 goto out;
491
Eric Parisb77a4932010-11-23 11:40:08 -0500492 /* No partial writes. */
493 length = -EINVAL;
494 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Eric Parisb77a4932010-11-23 11:40:08 -0500497 length = -EFBIG;
498 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500500
501 length = -ENOMEM;
502 data = vmalloc(count);
503 if (!data)
504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 length = -EFAULT;
507 if (copy_from_user(data, buf, count) != 0)
508 goto out;
509
510 length = security_load_policy(data, count);
511 if (length)
512 goto out;
513
Eric Parisb77a4932010-11-23 11:40:08 -0500514 length = sel_make_bools();
515 if (length)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400516 goto out1;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400517
Eric Parisb77a4932010-11-23 11:40:08 -0500518 length = sel_make_classes();
519 if (length)
Paul Moore3bb56b22008-01-29 08:38:19 -0500520 goto out1;
Paul Moore3bb56b22008-01-29 08:38:19 -0500521
Eric Parisb77a4932010-11-23 11:40:08 -0500522 length = sel_make_policycap();
523 if (length)
524 goto out1;
525
526 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400527
528out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000529 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500530 "policy loaded auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700531 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500532 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800534 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 vfree(data);
536 return length;
537}
538
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800539static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200541 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
Eric Paris18729812008-04-17 14:15:45 -0400544static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Eric Parisb77a4932010-11-23 11:40:08 -0500546 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800547 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ssize_t length;
549
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500550 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
551 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +0100555 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500556 if (length)
557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800559 length = security_sid_to_context(sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500560 if (length)
561 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800562
Eric Parisb77a4932010-11-23 11:40:08 -0500563 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800564 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400565 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
566 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800567 goto out;
568 }
569
570 memcpy(buf, canon, len);
571 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800573 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return length;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
578 size_t count, loff_t *ppos)
579{
580 char tmpbuf[TMPBUFLEN];
581 ssize_t length;
582
583 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
584 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
585}
586
Eric Paris18729812008-04-17 14:15:45 -0400587static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 size_t count, loff_t *ppos)
589{
Al Viro8365a712015-12-24 00:08:06 -0500590 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ssize_t length;
592 unsigned int new_value;
593
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500594 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
595 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
596 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500598 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Davi Arnautbfd51622005-10-30 14:59:24 -0800600 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500601 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500602
603 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500604 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500605 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500606
Al Viro8365a712015-12-24 00:08:06 -0500607 page = memdup_user_nul(buf, count);
608 if (IS_ERR(page))
609 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 length = -EINVAL;
612 if (sscanf(page, "%u", &new_value) != 1)
613 goto out;
614
615 selinux_checkreqprot = new_value ? 1 : 0;
616 length = count;
617out:
Al Viro8365a712015-12-24 00:08:06 -0500618 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return length;
620}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800621static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .read = sel_read_checkreqprot,
623 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200624 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625};
626
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500627static ssize_t sel_write_validatetrans(struct file *file,
628 const char __user *buf,
629 size_t count, loff_t *ppos)
630{
631 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
632 char *req = NULL;
633 u32 osid, nsid, tsid;
634 u16 tclass;
635 int rc;
636
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500637 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
638 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500639 if (rc)
640 goto out;
641
642 rc = -ENOMEM;
643 if (count >= PAGE_SIZE)
644 goto out;
645
646 /* No partial writes. */
647 rc = -EINVAL;
648 if (*ppos != 0)
649 goto out;
650
651 rc = -ENOMEM;
652 req = kzalloc(count + 1, GFP_KERNEL);
653 if (!req)
654 goto out;
655
656 rc = -EFAULT;
657 if (copy_from_user(req, buf, count))
658 goto out;
659
660 rc = -ENOMEM;
661 oldcon = kzalloc(count + 1, GFP_KERNEL);
662 if (!oldcon)
663 goto out;
664
665 newcon = kzalloc(count + 1, GFP_KERNEL);
666 if (!newcon)
667 goto out;
668
669 taskcon = kzalloc(count + 1, GFP_KERNEL);
670 if (!taskcon)
671 goto out;
672
673 rc = -EINVAL;
674 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
675 goto out;
676
677 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
678 if (rc)
679 goto out;
680
681 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
682 if (rc)
683 goto out;
684
685 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
686 if (rc)
687 goto out;
688
689 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
690 if (!rc)
691 rc = count;
692out:
693 kfree(req);
694 kfree(oldcon);
695 kfree(newcon);
696 kfree(taskcon);
697 return rc;
698}
699
700static const struct file_operations sel_transition_ops = {
701 .write = sel_write_validatetrans,
702 .llseek = generic_file_llseek,
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
707 */
Eric Paris18729812008-04-17 14:15:45 -0400708static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
709static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
710static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
711static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
712static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714static ssize_t (*write_op[])(struct file *, char *, size_t) = {
715 [SEL_ACCESS] = sel_write_access,
716 [SEL_CREATE] = sel_write_create,
717 [SEL_RELABEL] = sel_write_relabel,
718 [SEL_USER] = sel_write_user,
719 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800720 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721};
722
723static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
724{
Al Viro496ad9a2013-01-23 17:07:38 -0500725 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char *data;
727 ssize_t rv;
728
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800729 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EINVAL;
731
732 data = simple_transaction_get(file, buf, size);
733 if (IS_ERR(data))
734 return PTR_ERR(data);
735
Eric Paris18729812008-04-17 14:15:45 -0400736 rv = write_op[ino](file, data, size);
737 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 simple_transaction_set(file, rv);
739 rv = size;
740 }
741 return rv;
742}
743
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800744static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .write = selinux_transaction_write,
746 .read = simple_transaction_read,
747 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200748 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
750
751/*
752 * payload - write methods
753 * If the method has a response, the response should be put in buf,
754 * and the length returned. Otherwise return 0 or and -error.
755 */
756
Eric Paris18729812008-04-17 14:15:45 -0400757static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Eric Parisb77a4932010-11-23 11:40:08 -0500759 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 u32 ssid, tsid;
761 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct av_decision avd;
763 ssize_t length;
764
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500765 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
766 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800771 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Eric Parisb77a4932010-11-23 11:40:08 -0500775 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800776 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!tcon)
778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500781 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400784 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500785 if (length)
786 goto out;
787
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400788 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500789 if (length)
790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Stephen Smalley19439d02010-01-14 17:28:10 -0500792 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900795 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500796 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900798 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799out:
Eric Parisb77a4932010-11-23 11:40:08 -0500800 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 kfree(scon);
802 return length;
803}
804
Eric Paris18729812008-04-17 14:15:45 -0400805static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Eric Parisb77a4932010-11-23 11:40:08 -0500807 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100808 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 u32 ssid, tsid, newsid;
810 u16 tclass;
811 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500812 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100814 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500816 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
817 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
818 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500820 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800823 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Eric Parisb77a4932010-11-23 11:40:08 -0500827 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800828 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!tcon)
830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100832 length = -ENOMEM;
833 namebuf = kzalloc(size + 1, GFP_KERNEL);
834 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100837 length = -EINVAL;
838 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
839 if (nargs < 3 || nargs > 4)
840 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400841 if (nargs == 4) {
842 /*
843 * If and when the name of new object to be queried contains
844 * either whitespace or multibyte characters, they shall be
845 * encoded based on the percentage-encoding rule.
846 * If not encoded, the sscanf logic picks up only left-half
847 * of the supplied name; splitted by a whitespace unexpectedly.
848 */
849 char *r, *w;
850 int c1, c2;
851
852 r = w = namebuf;
853 do {
854 c1 = *r++;
855 if (c1 == '+')
856 c1 = ' ';
857 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800858 c1 = hex_to_bin(*r++);
859 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400860 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800861 c2 = hex_to_bin(*r++);
862 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400863 goto out;
864 c1 = (c1 << 4) | c2;
865 }
866 *w++ = c1;
867 } while (c1 != '\0');
868
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100869 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400870 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100871
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400872 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500873 if (length)
874 goto out;
875
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400876 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500877 if (length)
878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100880 length = security_transition_sid_user(ssid, tsid, tclass,
881 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500882 if (length)
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500886 if (length)
887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric Parisb77a4932010-11-23 11:40:08 -0500889 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400891 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
892 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 memcpy(buf, newcon, len);
897 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898out:
Eric Parisb77a4932010-11-23 11:40:08 -0500899 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100900 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500901 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 kfree(scon);
903 return length;
904}
905
Eric Paris18729812008-04-17 14:15:45 -0400906static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Eric Parisb77a4932010-11-23 11:40:08 -0500908 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 u32 ssid, tsid, newsid;
910 u16 tclass;
911 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500912 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 u32 len;
914
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500915 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
916 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
917 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800922 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500924 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Eric Parisb77a4932010-11-23 11:40:08 -0500926 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800927 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!tcon)
929 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 length = -EINVAL;
932 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400935 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500936 if (length)
937 goto out;
938
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400939 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500940 if (length)
941 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 length = security_change_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500944 if (length)
945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500948 if (length)
949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Eric Parisb77a4932010-11-23 11:40:08 -0500951 length = -ERANGE;
952 if (len > SIMPLE_TRANSACTION_LIMIT)
953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 memcpy(buf, newcon, len);
956 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957out:
Eric Parisb77a4932010-11-23 11:40:08 -0500958 kfree(newcon);
959 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 kfree(scon);
961 return length;
962}
963
Eric Paris18729812008-04-17 14:15:45 -0400964static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Eric Parisb77a4932010-11-23 11:40:08 -0500966 char *con = NULL, *user = NULL, *ptr;
967 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ssize_t length;
969 char *newcon;
970 int i, rc;
971 u32 len, nsids;
972
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500973 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
974 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
975 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800980 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Eric Parisb77a4932010-11-23 11:40:08 -0500984 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800985 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!user)
987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 length = -EINVAL;
990 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -0500991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400993 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500994 if (length)
995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 length = security_get_user_sids(sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -0500998 if (length)
999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 length = sprintf(buf, "%u", nsids) + 1;
1002 ptr = buf + length;
1003 for (i = 0; i < nsids; i++) {
1004 rc = security_sid_to_context(sids[i], &newcon, &len);
1005 if (rc) {
1006 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1010 kfree(newcon);
1011 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 memcpy(ptr, newcon, len);
1015 kfree(newcon);
1016 ptr += len;
1017 length += len;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019out:
Eric Parisb77a4932010-11-23 11:40:08 -05001020 kfree(sids);
1021 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 kfree(con);
1023 return length;
1024}
1025
Eric Paris18729812008-04-17 14:15:45 -04001026static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Eric Parisb77a4932010-11-23 11:40:08 -05001028 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 u32 ssid, tsid, newsid;
1030 u16 tclass;
1031 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001032 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 u32 len;
1034
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001035 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1036 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1037 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001042 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Eric Parisb77a4932010-11-23 11:40:08 -05001046 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001047 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!tcon)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 length = -EINVAL;
1052 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001055 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001056 if (length)
1057 goto out;
1058
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001059 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001060 if (length)
1061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 length = security_member_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001064 if (length)
1065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001068 if (length)
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Eric Parisb77a4932010-11-23 11:40:08 -05001071 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001073 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1074 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 memcpy(buf, newcon, len);
1079 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080out:
Eric Parisb77a4932010-11-23 11:40:08 -05001081 kfree(newcon);
1082 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 kfree(scon);
1084 return length;
1085}
1086
1087static struct inode *sel_make_inode(struct super_block *sb, int mode)
1088{
1089 struct inode *ret = new_inode(sb);
1090
1091 if (ret) {
1092 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001093 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 return ret;
1096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1099 size_t count, loff_t *ppos)
1100{
1101 char *page = NULL;
1102 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ssize_t ret;
1104 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001105 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001106 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Ingo Molnarbb0030792006-03-22 00:09:14 -08001108 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Eric Parisb77a4932010-11-23 11:40:08 -05001110 ret = -EINVAL;
1111 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Eric Parisb77a4932010-11-23 11:40:08 -05001114 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001115 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001116 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001119 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (cur_enforcing < 0) {
1121 ret = cur_enforcing;
1122 goto out;
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001125 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001126 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001128 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001129 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return ret;
1131}
1132
1133static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1134 size_t count, loff_t *ppos)
1135{
1136 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001137 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001139 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001140 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Ingo Molnarbb0030792006-03-22 00:09:14 -08001142 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001144 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1145 SECCLASS_SECURITY, SECURITY__SETBOOL,
1146 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (length)
1148 goto out;
1149
Eric Parisb77a4932010-11-23 11:40:08 -05001150 length = -EINVAL;
1151 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001152 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001153
Eric Parisb77a4932010-11-23 11:40:08 -05001154 length = -ENOMEM;
1155 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001157
Eric Parisb77a4932010-11-23 11:40:08 -05001158 /* No partial writes. */
1159 length = -EINVAL;
1160 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001162
Al Viro8365a712015-12-24 00:08:06 -05001163 page = memdup_user_nul(buf, count);
1164 if (IS_ERR(page)) {
1165 length = PTR_ERR(page);
1166 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 length = -EINVAL;
1171 if (sscanf(page, "%d", &new_value) != 1)
1172 goto out;
1173
1174 if (new_value)
1175 new_value = 1;
1176
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001177 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 length = count;
1179
1180out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001181 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001182 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return length;
1184}
1185
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001186static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001187 .read = sel_read_bool,
1188 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001189 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190};
1191
1192static ssize_t sel_commit_bools_write(struct file *filep,
1193 const char __user *buf,
1194 size_t count, loff_t *ppos)
1195{
1196 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001197 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int new_value;
1199
Ingo Molnarbb0030792006-03-22 00:09:14 -08001200 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001202 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1203 SECCLASS_SECURITY, SECURITY__SETBOOL,
1204 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (length)
1206 goto out;
1207
Eric Parisb77a4932010-11-23 11:40:08 -05001208 length = -ENOMEM;
1209 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001211
1212 /* No partial writes. */
1213 length = -EINVAL;
1214 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001216
Al Viro8365a712015-12-24 00:08:06 -05001217 page = memdup_user_nul(buf, count);
1218 if (IS_ERR(page)) {
1219 length = PTR_ERR(page);
1220 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 length = -EINVAL;
1225 if (sscanf(page, "%d", &new_value) != 1)
1226 goto out;
1227
Eric Parisb77a4932010-11-23 11:40:08 -05001228 length = 0;
Eric Paris18729812008-04-17 14:15:45 -04001229 if (new_value && bool_pending_values)
Eric Parisb77a4932010-11-23 11:40:08 -05001230 length = security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric Parisb77a4932010-11-23 11:40:08 -05001232 if (!length)
1233 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001236 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001237 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return length;
1239}
1240
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001241static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001242 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001243 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244};
1245
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001246static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Al Viroad521842014-12-24 14:56:48 -05001248 d_genocide(de);
1249 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252#define BOOL_DIR_NAME "booleans"
1253
1254static int sel_make_bools(void)
1255{
Eric Parisb77a4932010-11-23 11:40:08 -05001256 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ssize_t len;
1258 struct dentry *dentry = NULL;
1259 struct dentry *dir = bool_dir;
1260 struct inode *inode = NULL;
1261 struct inode_security_struct *isec;
1262 char **names = NULL, *page;
1263 int num;
1264 int *values = NULL;
1265 u32 sid;
1266
1267 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001268 for (i = 0; i < bool_num; i++)
1269 kfree(bool_pending_names[i]);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001270 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001271 kfree(bool_pending_values);
Eric Paris154c50c2012-04-04 13:47:11 -04001272 bool_num = 0;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001273 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001274 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001276 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Eric Parisb77a4932010-11-23 11:40:08 -05001278 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001279 page = (char *)get_zeroed_page(GFP_KERNEL);
1280 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 ret = security_get_bools(&num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001284 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 goto out;
1286
1287 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001288 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001290 if (!dentry)
1291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Parisb77a4932010-11-23 11:40:08 -05001293 ret = -ENOMEM;
1294 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1295 if (!inode)
1296 goto out;
1297
Eric Parisb77a4932010-11-23 11:40:08 -05001298 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001299 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001300 if (len >= PAGE_SIZE)
1301 goto out;
1302
Eric Paris18729812008-04-17 14:15:45 -04001303 isec = (struct inode_security_struct *)inode->i_security;
1304 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1305 if (ret)
Eric Parisb77a4932010-11-23 11:40:08 -05001306 goto out;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001309 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001311 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 d_add(dentry, inode);
1313 }
1314 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001315 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001317
1318 free_page((unsigned long)page);
1319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320out:
1321 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001324 for (i = 0; i < num; i++)
1325 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 kfree(names);
1327 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001328 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001329 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001330
1331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334#define NULL_FILE_NAME "null"
1335
Al Viro765927b2012-06-26 21:58:53 +04001336struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1339 size_t count, loff_t *ppos)
1340{
1341 char tmpbuf[TMPBUFLEN];
1342 ssize_t length;
1343
1344 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1345 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1346}
1347
Eric Paris18729812008-04-17 14:15:45 -04001348static ssize_t sel_write_avc_cache_threshold(struct file *file,
1349 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 size_t count, loff_t *ppos)
1351
1352{
Al Viro8365a712015-12-24 00:08:06 -05001353 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001355 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001357 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1358 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1359 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001360 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Eric Parisb77a4932010-11-23 11:40:08 -05001363 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001364 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric Parisb77a4932010-11-23 11:40:08 -05001366 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001367 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001368 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001369
Al Viro8365a712015-12-24 00:08:06 -05001370 page = memdup_user_nul(buf, count);
1371 if (IS_ERR(page))
1372 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric Parisb77a4932010-11-23 11:40:08 -05001374 ret = -EINVAL;
1375 if (sscanf(page, "%u", &new_value) != 1)
1376 goto out;
1377
1378 avc_cache_threshold = new_value;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381out:
Al Viro8365a712015-12-24 00:08:06 -05001382 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return ret;
1384}
1385
1386static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1387 size_t count, loff_t *ppos)
1388{
1389 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001390 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001393 if (!page)
1394 return -ENOMEM;
1395
1396 length = avc_get_hash_stats(page);
1397 if (length >= 0)
1398 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001400
1401 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001404static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 .read = sel_read_avc_cache_threshold,
1406 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001407 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408};
1409
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001410static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001412 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413};
1414
1415#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1416static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1417{
1418 int cpu;
1419
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301420 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!cpu_possible(cpu))
1422 continue;
1423 *idx = cpu + 1;
1424 return &per_cpu(avc_cache_stats, cpu);
1425 }
1426 return NULL;
1427}
1428
1429static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1430{
1431 loff_t n = *pos - 1;
1432
1433 if (*pos == 0)
1434 return SEQ_START_TOKEN;
1435
1436 return sel_avc_get_stat_idx(&n);
1437}
1438
1439static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1440{
1441 return sel_avc_get_stat_idx(pos);
1442}
1443
1444static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1445{
1446 struct avc_cache_stats *st = v;
1447
1448 if (v == SEQ_START_TOKEN)
1449 seq_printf(seq, "lookups hits misses allocations reclaims "
1450 "frees\n");
Linus Torvalds257313b2011-05-19 21:22:53 -07001451 else {
1452 unsigned int lookups = st->lookups;
1453 unsigned int misses = st->misses;
1454 unsigned int hits = lookups - misses;
1455 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1456 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return 0;
1460}
1461
1462static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1463{ }
1464
Jan Engelhardt1996a102008-01-23 00:02:58 +01001465static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 .start = sel_avc_stats_seq_start,
1467 .next = sel_avc_stats_seq_next,
1468 .show = sel_avc_stats_seq_show,
1469 .stop = sel_avc_stats_seq_stop,
1470};
1471
1472static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1473{
1474 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1475}
1476
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001477static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 .open = sel_open_avc_cache_stats,
1479 .read = seq_read,
1480 .llseek = seq_lseek,
1481 .release = seq_release,
1482};
1483#endif
1484
1485static int sel_make_avc_files(struct dentry *dir)
1486{
Eric Parisb77a4932010-11-23 11:40:08 -05001487 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 static struct tree_descr files[] = {
1489 { "cache_threshold",
1490 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1491 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1492#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1493 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1494#endif
1495 };
1496
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001497 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct inode *inode;
1499 struct dentry *dentry;
1500
1501 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001502 if (!dentry)
1503 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001506 if (!inode)
1507 return -ENOMEM;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001510 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 d_add(dentry, inode);
1512 }
Eric Parisb77a4932010-11-23 11:40:08 -05001513
1514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Eric Paris18729812008-04-17 14:15:45 -04001517static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001518 size_t count, loff_t *ppos)
1519{
James Carterf0ee2e42007-04-04 10:11:29 -04001520 char *con;
1521 u32 sid, len;
1522 ssize_t ret;
1523
Al Viro496ad9a2013-01-23 17:07:38 -05001524 sid = file_inode(file)->i_ino&SEL_INO_MASK;
James Carterf0ee2e42007-04-04 10:11:29 -04001525 ret = security_sid_to_context(sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001526 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001527 return ret;
1528
1529 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1530 kfree(con);
1531 return ret;
1532}
1533
1534static const struct file_operations sel_initcon_ops = {
1535 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001536 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001537};
1538
1539static int sel_make_initcon_files(struct dentry *dir)
1540{
Eric Parisb77a4932010-11-23 11:40:08 -05001541 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001542
1543 for (i = 1; i <= SECINITSID_NUM; i++) {
1544 struct inode *inode;
1545 struct dentry *dentry;
1546 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001547 if (!dentry)
1548 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001549
1550 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001551 if (!inode)
1552 return -ENOMEM;
1553
James Carterf0ee2e42007-04-04 10:11:29 -04001554 inode->i_fop = &sel_initcon_ops;
1555 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1556 d_add(dentry, inode);
1557 }
Eric Parisb77a4932010-11-23 11:40:08 -05001558
1559 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001560}
1561
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001562static inline unsigned long sel_class_to_ino(u16 class)
1563{
1564 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1565}
1566
1567static inline u16 sel_ino_to_class(unsigned long ino)
1568{
Eric Paris92ae9e82012-04-04 13:46:46 -04001569 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001570}
1571
1572static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1573{
1574 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1575}
1576
1577static inline u32 sel_ino_to_perm(unsigned long ino)
1578{
1579 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1580}
1581
Eric Paris18729812008-04-17 14:15:45 -04001582static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001583 size_t count, loff_t *ppos)
1584{
Al Viro496ad9a2013-01-23 17:07:38 -05001585 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001586 char res[TMPBUFLEN];
1587 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1588 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001589}
1590
1591static const struct file_operations sel_class_ops = {
1592 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001593 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001594};
1595
Eric Paris18729812008-04-17 14:15:45 -04001596static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001597 size_t count, loff_t *ppos)
1598{
Al Viro496ad9a2013-01-23 17:07:38 -05001599 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001600 char res[TMPBUFLEN];
1601 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1602 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001603}
1604
1605static const struct file_operations sel_perm_ops = {
1606 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001607 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001608};
1609
Paul Moore3bb56b22008-01-29 08:38:19 -05001610static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1611 size_t count, loff_t *ppos)
1612{
1613 int value;
1614 char tmpbuf[TMPBUFLEN];
1615 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001616 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001617
1618 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1619 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1620
1621 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1622}
1623
1624static const struct file_operations sel_policycap_ops = {
1625 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001626 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001627};
1628
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001629static int sel_make_perm_files(char *objclass, int classvalue,
1630 struct dentry *dir)
1631{
Eric Parisb77a4932010-11-23 11:40:08 -05001632 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001633 char **perms;
1634
1635 rc = security_get_permissions(objclass, &perms, &nperms);
1636 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001637 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001638
1639 for (i = 0; i < nperms; i++) {
1640 struct inode *inode;
1641 struct dentry *dentry;
1642
Eric Parisb77a4932010-11-23 11:40:08 -05001643 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001644 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001645 if (!dentry)
1646 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001647
Eric Parisb77a4932010-11-23 11:40:08 -05001648 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001649 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001650 if (!inode)
1651 goto out;
1652
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001653 inode->i_fop = &sel_perm_ops;
1654 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001655 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001656 d_add(dentry, inode);
1657 }
Eric Parisb77a4932010-11-23 11:40:08 -05001658 rc = 0;
1659out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001660 for (i = 0; i < nperms; i++)
1661 kfree(perms[i]);
1662 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001663 return rc;
1664}
1665
1666static int sel_make_class_dir_entries(char *classname, int index,
1667 struct dentry *dir)
1668{
1669 struct dentry *dentry = NULL;
1670 struct inode *inode = NULL;
1671 int rc;
1672
1673 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001674 if (!dentry)
1675 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001676
1677 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001678 if (!inode)
1679 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001680
1681 inode->i_fop = &sel_class_ops;
1682 inode->i_ino = sel_class_to_ino(index);
1683 d_add(dentry, inode);
1684
Al Viroa1c2aa12012-03-18 20:36:59 -04001685 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1686 if (IS_ERR(dentry))
1687 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001688
1689 rc = sel_make_perm_files(classname, index, dentry);
1690
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001691 return rc;
1692}
1693
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001694static int sel_make_classes(void)
1695{
Eric Parisb77a4932010-11-23 11:40:08 -05001696 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001697 char **classes;
1698
1699 /* delete any existing entries */
Al Viroad521842014-12-24 14:56:48 -05001700 sel_remove_entries(class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001701
1702 rc = security_get_classes(&classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001703 if (rc)
1704 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001705
1706 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001707 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001708
1709 for (i = 0; i < nclasses; i++) {
1710 struct dentry *class_name_dir;
1711
Al Viroa1c2aa12012-03-18 20:36:59 -04001712 class_name_dir = sel_make_dir(class_dir, classes[i],
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001713 &last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001714 if (IS_ERR(class_name_dir)) {
1715 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001716 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001717 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001718
1719 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001720 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001721 class_name_dir);
1722 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001723 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001724 }
Eric Parisb77a4932010-11-23 11:40:08 -05001725 rc = 0;
1726out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001727 for (i = 0; i < nclasses; i++)
1728 kfree(classes[i]);
1729 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001730 return rc;
1731}
1732
Paul Moore3bb56b22008-01-29 08:38:19 -05001733static int sel_make_policycap(void)
1734{
1735 unsigned int iter;
1736 struct dentry *dentry = NULL;
1737 struct inode *inode = NULL;
1738
1739 sel_remove_entries(policycap_dir);
1740
1741 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1742 if (iter < ARRAY_SIZE(policycap_names))
1743 dentry = d_alloc_name(policycap_dir,
1744 policycap_names[iter]);
1745 else
1746 dentry = d_alloc_name(policycap_dir, "unknown");
1747
1748 if (dentry == NULL)
1749 return -ENOMEM;
1750
1751 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1752 if (inode == NULL)
1753 return -ENOMEM;
1754
1755 inode->i_fop = &sel_policycap_ops;
1756 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1757 d_add(dentry, inode);
1758 }
1759
1760 return 0;
1761}
1762
Al Viroa1c2aa12012-03-18 20:36:59 -04001763static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001764 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Al Viroa1c2aa12012-03-18 20:36:59 -04001766 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct inode *inode;
1768
Al Viroa1c2aa12012-03-18 20:36:59 -04001769 if (!dentry)
1770 return ERR_PTR(-ENOMEM);
1771
1772 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1773 if (!inode) {
1774 dput(dentry);
1775 return ERR_PTR(-ENOMEM);
1776 }
Eric Parisb77a4932010-11-23 11:40:08 -05001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 inode->i_op = &simple_dir_inode_operations;
1779 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001780 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001781 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001782 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001784 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001785 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001786
Al Viroa1c2aa12012-03-18 20:36:59 -04001787 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Eric Paris18729812008-04-17 14:15:45 -04001790static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int ret;
1793 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001794 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct inode_security_struct *isec;
1796
1797 static struct tree_descr selinux_files[] = {
1798 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1799 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001800 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1802 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1803 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1804 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1805 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1806 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1807 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1808 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1809 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1810 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001811 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1812 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001813 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001814 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001815 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1816 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* last one */ {""}
1818 };
1819 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1820 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001821 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Al Viroa1c2aa12012-03-18 20:36:59 -04001823 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1824 if (IS_ERR(bool_dir)) {
1825 ret = PTR_ERR(bool_dir);
1826 bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001827 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Eric Parisb77a4932010-11-23 11:40:08 -05001830 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001832 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001833 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Eric Parisb77a4932010-11-23 11:40:08 -05001835 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001837 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001838 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001839
James Carter6174eaf2007-04-04 16:18:39 -04001840 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001841 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 isec->sid = SECINITSID_DEVNULL;
1843 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001844 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1847 d_add(dentry, inode);
Al Viro765927b2012-06-26 21:58:53 +04001848 selinux_null.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Al Viroa1c2aa12012-03-18 20:36:59 -04001850 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1851 if (IS_ERR(dentry)) {
1852 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001853 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 ret = sel_make_avc_files(dentry);
1857 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001858 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001859
Al Viroa1c2aa12012-03-18 20:36:59 -04001860 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1861 if (IS_ERR(dentry)) {
1862 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001863 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001864 }
James Carterf0ee2e42007-04-04 10:11:29 -04001865
1866 ret = sel_make_initcon_files(dentry);
1867 if (ret)
1868 goto err;
1869
Al Viroa1c2aa12012-03-18 20:36:59 -04001870 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1871 if (IS_ERR(class_dir)) {
1872 ret = PTR_ERR(class_dir);
1873 class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001874 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001875 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001876
Al Viroa1c2aa12012-03-18 20:36:59 -04001877 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1878 if (IS_ERR(policycap_dir)) {
1879 ret = PTR_ERR(policycap_dir);
1880 policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001881 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001882 }
Eric Parisb77a4932010-11-23 11:40:08 -05001883 return 0;
James Morris161ce452006-03-22 00:09:17 -08001884err:
Eric Paris744ba352008-04-17 11:52:44 -04001885 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1886 __func__);
Eric Parisb77a4932010-11-23 11:40:08 -05001887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Al Virofc14f2f2010-07-25 01:48:30 +04001890static struct dentry *sel_mount(struct file_system_type *fs_type,
1891 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Al Virofc14f2f2010-07-25 01:48:30 +04001893 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896static struct file_system_type sel_fs_type = {
1897 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001898 .mount = sel_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 .kill_sb = kill_litter_super,
1900};
1901
1902struct vfsmount *selinuxfs_mount;
1903
1904static int __init init_sel_fs(void)
1905{
1906 int err;
1907
1908 if (!selinux_enabled)
1909 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001910
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001911 err = sysfs_create_mount_point(fs_kobj, "selinux");
1912 if (err)
1913 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001916 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001917 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05001918 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001919 }
Eric Parisb77a4932010-11-23 11:40:08 -05001920
Al Viro765927b2012-06-26 21:58:53 +04001921 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05001922 if (IS_ERR(selinuxfs_mount)) {
1923 printk(KERN_ERR "selinuxfs: could not mount!\n");
1924 err = PTR_ERR(selinuxfs_mount);
1925 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Eric Parisb77a4932010-11-23 11:40:08 -05001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return err;
1929}
1930
1931__initcall(init_sel_fs);
1932
1933#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1934void exit_sel_fs(void)
1935{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001936 sysfs_remove_mount_point(fs_kobj, "selinux");
Tim Chen423e0ab2011-07-19 09:32:38 -07001937 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 unregister_filesystem(&sel_fs_type);
1939}
1940#endif