blob: 1884f34bb9832eff210065e9043207755f3f98d8 [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 *
Eric Paris18729812008-04-17 14:15:45 -04004 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Paul Moore82c21bf2011-08-01 11:10:33 +00006 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05007 *
Eric Paris18729812008-04-17 14:15:45 -04008 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05009 *
10 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/pagemap.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/fs.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040020#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/string.h>
24#include <linux/security.h>
25#include <linux/major.h>
26#include <linux/seq_file.h>
27#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000028#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040029#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070030#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040031#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
35
36#include "flask.h"
37#include "avc.h"
38#include "avc_ss.h"
39#include "security.h"
40#include "objsec.h"
41#include "conditional.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043enum sel_inos {
44 SEL_ROOT_INO = 2,
45 SEL_LOAD, /* load policy */
46 SEL_ENFORCE, /* get or set enforcing status */
47 SEL_CONTEXT, /* validate context */
48 SEL_ACCESS, /* compute access decision */
49 SEL_CREATE, /* compute create labeling decision */
50 SEL_RELABEL, /* compute relabeling decision */
51 SEL_USER, /* compute reachable user contexts */
52 SEL_POLICYVERS, /* return policy version for this kernel */
53 SEL_COMMIT_BOOLS, /* commit new boolean values */
54 SEL_MLS, /* return if MLS policy is enabled */
55 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 SEL_MEMBER, /* compute polyinstantiation membership decision */
57 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070058 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040059 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
60 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090061 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040062 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050063 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040064 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Stephen Smalley0619f0f2018-03-20 11:59:11 -040067struct selinux_fs_info {
68 struct dentry *bool_dir;
69 unsigned int bool_num;
70 char **bool_pending_names;
71 unsigned int *bool_pending_values;
72 struct dentry *class_dir;
73 unsigned long last_class_ino;
74 bool policy_opened;
75 struct dentry *policycap_dir;
76 struct mutex mutex;
77 unsigned long last_ino;
78 struct selinux_state *state;
79 struct super_block *sb;
80};
81
82static int selinux_fs_info_create(struct super_block *sb)
83{
84 struct selinux_fs_info *fsi;
85
86 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
87 if (!fsi)
88 return -ENOMEM;
89
90 mutex_init(&fsi->mutex);
91 fsi->last_ino = SEL_INO_NEXT - 1;
92 fsi->state = &selinux_state;
93 fsi->sb = sb;
94 sb->s_fs_info = fsi;
95 return 0;
96}
97
98static void selinux_fs_info_free(struct super_block *sb)
99{
100 struct selinux_fs_info *fsi = sb->s_fs_info;
101 int i;
102
103 if (fsi) {
104 for (i = 0; i < fsi->bool_num; i++)
105 kfree(fsi->bool_pending_names[i]);
106 kfree(fsi->bool_pending_names);
107 kfree(fsi->bool_pending_values);
108 }
109 kfree(sb->s_fs_info);
110 sb->s_fs_info = NULL;
111}
James Carter6174eaf2007-04-04 16:18:39 -0400112
Paul Moore3bb56b22008-01-29 08:38:19 -0500113#define SEL_INITCON_INO_OFFSET 0x01000000
114#define SEL_BOOL_INO_OFFSET 0x02000000
115#define SEL_CLASS_INO_OFFSET 0x04000000
116#define SEL_POLICYCAP_INO_OFFSET 0x08000000
117#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TMPBUFLEN 12
120static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
121 size_t count, loff_t *ppos)
122{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400123 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 char tmpbuf[TMPBUFLEN];
125 ssize_t length;
126
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500127 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400128 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
130}
131
132#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400133static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 size_t count, loff_t *ppos)
135
136{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400137 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
138 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500139 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500141 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Davi Arnautbfd51622005-10-30 14:59:24 -0800143 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500144 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500145
146 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500147 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500148 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500149
Al Viro8365a712015-12-24 00:08:06 -0500150 page = memdup_user_nul(buf, count);
151 if (IS_ERR(page))
152 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 length = -EINVAL;
155 if (sscanf(page, "%d", &new_value) != 1)
156 goto out;
157
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500158 new_value = !!new_value;
159
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400160 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500161 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500162 length = avc_has_perm(&selinux_state,
163 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500164 SECCLASS_SECURITY, SECURITY__SETENFORCE,
165 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (length)
167 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400168 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400169 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
170 " enabled=%d old-enabled=%d lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500171 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700172 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400173 audit_get_sessionid(current),
174 selinux_enabled, selinux_enabled);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400175 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500176 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500177 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500178 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400179 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 if (!new_value)
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300181 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 length = count;
184out:
Al Viro8365a712015-12-24 00:08:06 -0500185 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return length;
187}
188#else
189#define sel_write_enforce NULL
190#endif
191
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800192static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200195 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Eric Paris3f120702007-09-21 14:37:10 -0400198static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
199 size_t count, loff_t *ppos)
200{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400201 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
202 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400203 char tmpbuf[TMPBUFLEN];
204 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500205 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400206 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400207 security_get_reject_unknown(state) :
208 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400209
210 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
211 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
212}
213
214static const struct file_operations sel_handle_unknown_ops = {
215 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200216 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400217};
218
KaiGai Kohei11904162010-09-14 18:28:39 +0900219static int sel_open_handle_status(struct inode *inode, struct file *filp)
220{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400221 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
222 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900223
224 if (!status)
225 return -ENOMEM;
226
227 filp->private_data = status;
228
229 return 0;
230}
231
232static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
233 size_t count, loff_t *ppos)
234{
235 struct page *status = filp->private_data;
236
237 BUG_ON(!status);
238
239 return simple_read_from_buffer(buf, count, ppos,
240 page_address(status),
241 sizeof(struct selinux_kernel_status));
242}
243
244static int sel_mmap_handle_status(struct file *filp,
245 struct vm_area_struct *vma)
246{
247 struct page *status = filp->private_data;
248 unsigned long size = vma->vm_end - vma->vm_start;
249
250 BUG_ON(!status);
251
252 /* only allows one page from the head */
253 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
254 return -EIO;
255 /* disallow writable mapping */
256 if (vma->vm_flags & VM_WRITE)
257 return -EPERM;
258 /* disallow mprotect() turns it into writable */
259 vma->vm_flags &= ~VM_MAYWRITE;
260
261 return remap_pfn_range(vma, vma->vm_start,
262 page_to_pfn(status),
263 size, vma->vm_page_prot);
264}
265
266static const struct file_operations sel_handle_status_ops = {
267 .open = sel_open_handle_status,
268 .read = sel_read_handle_status,
269 .mmap = sel_mmap_handle_status,
270 .llseek = generic_file_llseek,
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400274static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 size_t count, loff_t *ppos)
276
277{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400278 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500279 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ssize_t length;
281 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400282 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Davi Arnautbfd51622005-10-30 14:59:24 -0800284 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500285 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500286
287 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500288 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500289 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500290
Al Viro8365a712015-12-24 00:08:06 -0500291 page = memdup_user_nul(buf, count);
292 if (IS_ERR(page))
293 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 length = -EINVAL;
296 if (sscanf(page, "%d", &new_value) != 1)
297 goto out;
298
299 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400300 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400301 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500302 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400304 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400305 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
306 " enabled=%d old-enabled=%d lsm=selinux res=1",
307 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700308 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400309 audit_get_sessionid(current), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
312 length = count;
313out:
Al Viro8365a712015-12-24 00:08:06 -0500314 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return length;
316}
317#else
318#define sel_write_disable NULL
319#endif
320
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800321static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200323 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324};
325
326static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400327 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 char tmpbuf[TMPBUFLEN];
330 ssize_t length;
331
332 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
333 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
334}
335
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800336static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200338 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
340
341/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400342static int sel_make_bools(struct selinux_fs_info *fsi);
343static int sel_make_classes(struct selinux_fs_info *fsi);
344static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400345
346/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400347static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400348 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350static ssize_t sel_read_mls(struct file *filp, char __user *buf,
351 size_t count, loff_t *ppos)
352{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400353 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 char tmpbuf[TMPBUFLEN];
355 ssize_t length;
356
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100357 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400358 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
360}
361
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800362static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200364 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365};
366
Eric Pariscee74f42010-10-13 17:50:25 -0400367struct policy_load_memory {
368 size_t len;
369 void *data;
370};
371
372static int sel_open_policy(struct inode *inode, struct file *filp)
373{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400374 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
375 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400376 struct policy_load_memory *plm = NULL;
377 int rc;
378
379 BUG_ON(filp->private_data);
380
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400381 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400382
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500383 rc = avc_has_perm(&selinux_state,
384 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500385 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400386 if (rc)
387 goto err;
388
389 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400390 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400391 goto err;
392
393 rc = -ENOMEM;
394 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
395 if (!plm)
396 goto err;
397
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400398 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500399 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400400 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500401 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400402 }
403
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400404 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400405 if (rc)
406 goto err;
407
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400408 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400409
410 filp->private_data = plm;
411
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400412 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400413
414 return 0;
415err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400416 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400417
418 if (plm)
419 vfree(plm->data);
420 kfree(plm);
421 return rc;
422}
423
424static int sel_release_policy(struct inode *inode, struct file *filp)
425{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400426 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400427 struct policy_load_memory *plm = filp->private_data;
428
429 BUG_ON(!plm);
430
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400431 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400432
433 vfree(plm->data);
434 kfree(plm);
435
436 return 0;
437}
438
439static ssize_t sel_read_policy(struct file *filp, char __user *buf,
440 size_t count, loff_t *ppos)
441{
442 struct policy_load_memory *plm = filp->private_data;
443 int ret;
444
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500445 ret = avc_has_perm(&selinux_state,
446 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500447 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400448 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400449 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400450
Jann Horn0da74122018-06-28 20:39:54 -0400451 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400452}
453
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530454static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400455{
Dave Jiang11bac802017-02-24 14:56:41 -0800456 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400457 unsigned long offset;
458 struct page *page;
459
460 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
461 return VM_FAULT_SIGBUS;
462
463 offset = vmf->pgoff << PAGE_SHIFT;
464 if (offset >= roundup(plm->len, PAGE_SIZE))
465 return VM_FAULT_SIGBUS;
466
467 page = vmalloc_to_page(plm->data + offset);
468 get_page(page);
469
470 vmf->page = page;
471
472 return 0;
473}
474
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700475static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400476 .fault = sel_mmap_policy_fault,
477 .page_mkwrite = sel_mmap_policy_fault,
478};
479
James Morrisad3fa082011-08-30 10:50:12 +1000480static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400481{
482 if (vma->vm_flags & VM_SHARED) {
483 /* do not allow mprotect to make mapping writable */
484 vma->vm_flags &= ~VM_MAYWRITE;
485
486 if (vma->vm_flags & VM_WRITE)
487 return -EACCES;
488 }
489
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700490 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400491 vma->vm_ops = &sel_mmap_policy_ops;
492
493 return 0;
494}
495
Eric Pariscee74f42010-10-13 17:50:25 -0400496static const struct file_operations sel_policy_ops = {
497 .open = sel_open_policy,
498 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400499 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400500 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500501 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400502};
503
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400504static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
505{
506 int ret;
507
508 ret = sel_make_bools(fsi);
509 if (ret) {
510 pr_err("SELinux: failed to load policy booleans\n");
511 return ret;
512 }
513
514 ret = sel_make_classes(fsi);
515 if (ret) {
516 pr_err("SELinux: failed to load policy classes\n");
517 return ret;
518 }
519
520 ret = sel_make_policycap(fsi);
521 if (ret) {
522 pr_err("SELinux: failed to load policy capabilities\n");
523 return ret;
524 }
525
526 return 0;
527}
528
Eric Paris18729812008-04-17 14:15:45 -0400529static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 size_t count, loff_t *ppos)
531
532{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400533 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ssize_t length;
535 void *data = NULL;
536
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400537 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500539 length = avc_has_perm(&selinux_state,
540 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500541 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (length)
543 goto out;
544
Eric Parisb77a4932010-11-23 11:40:08 -0500545 /* No partial writes. */
546 length = -EINVAL;
547 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Eric Parisb77a4932010-11-23 11:40:08 -0500550 length = -EFBIG;
551 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500553
554 length = -ENOMEM;
555 data = vmalloc(count);
556 if (!data)
557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 length = -EFAULT;
560 if (copy_from_user(data, buf, count) != 0)
561 goto out;
562
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400563 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500564 if (length) {
565 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400569 length = sel_make_policy_nodes(fsi);
570 if (length)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400571 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500572
573 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400574
575out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400576 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400577 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700578 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500579 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400581 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 vfree(data);
583 return length;
584}
585
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800586static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200588 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589};
590
Eric Paris18729812008-04-17 14:15:45 -0400591static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400593 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
594 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500595 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800596 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ssize_t length;
598
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500599 length = avc_has_perm(&selinux_state,
600 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500601 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400605 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500606 if (length)
607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400609 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500610 if (length)
611 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800612
Eric Parisb77a4932010-11-23 11:40:08 -0500613 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800614 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200615 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400616 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800617 goto out;
618 }
619
620 memcpy(buf, canon, len);
621 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800623 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return length;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
628 size_t count, loff_t *ppos)
629{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400630 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 char tmpbuf[TMPBUFLEN];
632 ssize_t length;
633
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400634 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
636}
637
Eric Paris18729812008-04-17 14:15:45 -0400638static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 size_t count, loff_t *ppos)
640{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400641 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500642 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ssize_t length;
644 unsigned int new_value;
645
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500646 length = avc_has_perm(&selinux_state,
647 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500648 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
649 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500651 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Davi Arnautbfd51622005-10-30 14:59:24 -0800653 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500654 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500655
656 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500657 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500658 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500659
Al Viro8365a712015-12-24 00:08:06 -0500660 page = memdup_user_nul(buf, count);
661 if (IS_ERR(page))
662 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 length = -EINVAL;
665 if (sscanf(page, "%u", &new_value) != 1)
666 goto out;
667
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400668 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 length = count;
670out:
Al Viro8365a712015-12-24 00:08:06 -0500671 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return length;
673}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800674static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 .read = sel_read_checkreqprot,
676 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200677 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678};
679
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500680static ssize_t sel_write_validatetrans(struct file *file,
681 const char __user *buf,
682 size_t count, loff_t *ppos)
683{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400684 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
685 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500686 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
687 char *req = NULL;
688 u32 osid, nsid, tsid;
689 u16 tclass;
690 int rc;
691
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500692 rc = avc_has_perm(&selinux_state,
693 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500694 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500695 if (rc)
696 goto out;
697
698 rc = -ENOMEM;
699 if (count >= PAGE_SIZE)
700 goto out;
701
702 /* No partial writes. */
703 rc = -EINVAL;
704 if (*ppos != 0)
705 goto out;
706
Al Viro0b884d22017-05-13 18:12:07 -0400707 req = memdup_user_nul(buf, count);
708 if (IS_ERR(req)) {
709 rc = PTR_ERR(req);
710 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500711 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400712 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500713
714 rc = -ENOMEM;
715 oldcon = kzalloc(count + 1, GFP_KERNEL);
716 if (!oldcon)
717 goto out;
718
719 newcon = kzalloc(count + 1, GFP_KERNEL);
720 if (!newcon)
721 goto out;
722
723 taskcon = kzalloc(count + 1, GFP_KERNEL);
724 if (!taskcon)
725 goto out;
726
727 rc = -EINVAL;
728 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
729 goto out;
730
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400731 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500732 if (rc)
733 goto out;
734
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400735 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500736 if (rc)
737 goto out;
738
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400739 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500740 if (rc)
741 goto out;
742
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400743 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500744 if (!rc)
745 rc = count;
746out:
747 kfree(req);
748 kfree(oldcon);
749 kfree(newcon);
750 kfree(taskcon);
751 return rc;
752}
753
754static const struct file_operations sel_transition_ops = {
755 .write = sel_write_validatetrans,
756 .llseek = generic_file_llseek,
757};
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
761 */
Eric Paris18729812008-04-17 14:15:45 -0400762static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
763static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
764static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
765static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
766static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Eric Biggers631d2b42018-07-17 10:43:56 -0700768static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 [SEL_ACCESS] = sel_write_access,
770 [SEL_CREATE] = sel_write_create,
771 [SEL_RELABEL] = sel_write_relabel,
772 [SEL_USER] = sel_write_user,
773 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800774 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775};
776
777static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
778{
Al Viro496ad9a2013-01-23 17:07:38 -0500779 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 char *data;
781 ssize_t rv;
782
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800783 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EINVAL;
785
786 data = simple_transaction_get(file, buf, size);
787 if (IS_ERR(data))
788 return PTR_ERR(data);
789
Eric Paris18729812008-04-17 14:15:45 -0400790 rv = write_op[ino](file, data, size);
791 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 simple_transaction_set(file, rv);
793 rv = size;
794 }
795 return rv;
796}
797
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800798static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .write = selinux_transaction_write,
800 .read = simple_transaction_read,
801 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200802 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
805/*
806 * payload - write methods
807 * If the method has a response, the response should be put in buf,
808 * and the length returned. Otherwise return 0 or and -error.
809 */
810
Eric Paris18729812008-04-17 14:15:45 -0400811static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400813 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
814 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500815 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 u32 ssid, tsid;
817 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct av_decision avd;
819 ssize_t length;
820
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500821 length = avc_has_perm(&selinux_state,
822 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500823 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800828 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Eric Parisb77a4932010-11-23 11:40:08 -0500832 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800833 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!tcon)
835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500838 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400841 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500842 if (length)
843 goto out;
844
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400845 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500846 if (length)
847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400849 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900852 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500853 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900855 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856out:
Eric Parisb77a4932010-11-23 11:40:08 -0500857 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 kfree(scon);
859 return length;
860}
861
Eric Paris18729812008-04-17 14:15:45 -0400862static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400864 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
865 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500866 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100867 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 u32 ssid, tsid, newsid;
869 u16 tclass;
870 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500871 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100873 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500875 length = avc_has_perm(&selinux_state,
876 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500877 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
878 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800883 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Eric Parisb77a4932010-11-23 11:40:08 -0500887 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800888 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!tcon)
890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100892 length = -ENOMEM;
893 namebuf = kzalloc(size + 1, GFP_KERNEL);
894 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100897 length = -EINVAL;
898 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
899 if (nargs < 3 || nargs > 4)
900 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400901 if (nargs == 4) {
902 /*
903 * If and when the name of new object to be queried contains
904 * either whitespace or multibyte characters, they shall be
905 * encoded based on the percentage-encoding rule.
906 * If not encoded, the sscanf logic picks up only left-half
907 * of the supplied name; splitted by a whitespace unexpectedly.
908 */
909 char *r, *w;
910 int c1, c2;
911
912 r = w = namebuf;
913 do {
914 c1 = *r++;
915 if (c1 == '+')
916 c1 = ' ';
917 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800918 c1 = hex_to_bin(*r++);
919 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400920 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800921 c2 = hex_to_bin(*r++);
922 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400923 goto out;
924 c1 = (c1 << 4) | c2;
925 }
926 *w++ = c1;
927 } while (c1 != '\0');
928
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100929 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400930 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100931
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400932 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500933 if (length)
934 goto out;
935
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400936 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500937 if (length)
938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400940 length = security_transition_sid_user(state, ssid, tsid, tclass,
941 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500942 if (length)
943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400945 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500946 if (length)
947 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Parisb77a4932010-11-23 11:40:08 -0500949 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200951 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400952 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 memcpy(buf, newcon, len);
957 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958out:
Eric Parisb77a4932010-11-23 11:40:08 -0500959 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100960 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500961 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 kfree(scon);
963 return length;
964}
965
Eric Paris18729812008-04-17 14:15:45 -0400966static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400968 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
969 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500970 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 u32 ssid, tsid, newsid;
972 u16 tclass;
973 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500974 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 u32 len;
976
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500977 length = avc_has_perm(&selinux_state,
978 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500979 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
980 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800985 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Eric Parisb77a4932010-11-23 11:40:08 -0500989 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800990 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!tcon)
992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 length = -EINVAL;
995 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400998 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500999 if (length)
1000 goto out;
1001
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001002 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001003 if (length)
1004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001006 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001007 if (length)
1008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001010 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001011 if (length)
1012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Parisb77a4932010-11-23 11:40:08 -05001014 length = -ERANGE;
1015 if (len > SIMPLE_TRANSACTION_LIMIT)
1016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 memcpy(buf, newcon, len);
1019 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020out:
Eric Parisb77a4932010-11-23 11:40:08 -05001021 kfree(newcon);
1022 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 kfree(scon);
1024 return length;
1025}
1026
Eric Paris18729812008-04-17 14:15:45 -04001027static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001029 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1030 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001031 char *con = NULL, *user = NULL, *ptr;
1032 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 ssize_t length;
1034 char *newcon;
1035 int i, rc;
1036 u32 len, nsids;
1037
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001038 length = avc_has_perm(&selinux_state,
1039 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001040 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1041 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001046 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Eric Parisb77a4932010-11-23 11:40:08 -05001050 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001051 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!user)
1053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 length = -EINVAL;
1056 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001059 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001060 if (length)
1061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001063 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001064 if (length)
1065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 length = sprintf(buf, "%u", nsids) + 1;
1068 ptr = buf + length;
1069 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001070 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (rc) {
1072 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1076 kfree(newcon);
1077 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 memcpy(ptr, newcon, len);
1081 kfree(newcon);
1082 ptr += len;
1083 length += len;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085out:
Eric Parisb77a4932010-11-23 11:40:08 -05001086 kfree(sids);
1087 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 kfree(con);
1089 return length;
1090}
1091
Eric Paris18729812008-04-17 14:15:45 -04001092static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001094 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1095 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001096 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 u32 ssid, tsid, newsid;
1098 u16 tclass;
1099 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001100 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 u32 len;
1102
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001103 length = avc_has_perm(&selinux_state,
1104 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001105 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1106 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001111 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001113 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Eric Parisb77a4932010-11-23 11:40:08 -05001115 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001116 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!tcon)
1118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 length = -EINVAL;
1121 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001124 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001125 if (length)
1126 goto out;
1127
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001128 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001129 if (length)
1130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001132 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001133 if (length)
1134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001136 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001137 if (length)
1138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Eric Parisb77a4932010-11-23 11:40:08 -05001140 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001142 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001143 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 memcpy(buf, newcon, len);
1148 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149out:
Eric Parisb77a4932010-11-23 11:40:08 -05001150 kfree(newcon);
1151 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 kfree(scon);
1153 return length;
1154}
1155
1156static struct inode *sel_make_inode(struct super_block *sb, int mode)
1157{
1158 struct inode *ret = new_inode(sb);
1159
1160 if (ret) {
1161 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001162 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 return ret;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1168 size_t count, loff_t *ppos)
1169{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001170 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 char *page = NULL;
1172 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ssize_t ret;
1174 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001175 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001176 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001178 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Eric Parisb77a4932010-11-23 11:40:08 -05001180 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001181 if (index >= fsi->bool_num || strcmp(name,
1182 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001183 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Eric Parisb77a4932010-11-23 11:40:08 -05001185 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001186 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001187 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001188 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001190 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (cur_enforcing < 0) {
1192 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001193 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001196 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001197 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001198 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1199out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001200 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001202
1203out_unlock:
1204 mutex_unlock(&fsi->mutex);
1205 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1209 size_t count, loff_t *ppos)
1210{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001211 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001213 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001215 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001216 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jann Horn0da74122018-06-28 20:39:54 -04001218 if (count >= PAGE_SIZE)
1219 return -ENOMEM;
1220
1221 /* No partial writes. */
1222 if (*ppos != 0)
1223 return -EINVAL;
1224
1225 page = memdup_user_nul(buf, count);
1226 if (IS_ERR(page))
1227 return PTR_ERR(page);
1228
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001229 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001231 length = avc_has_perm(&selinux_state,
1232 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001233 SECCLASS_SECURITY, SECURITY__SETBOOL,
1234 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (length)
1236 goto out;
1237
Eric Parisb77a4932010-11-23 11:40:08 -05001238 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001239 if (index >= fsi->bool_num || strcmp(name,
1240 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001241 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 length = -EINVAL;
1244 if (sscanf(page, "%d", &new_value) != 1)
1245 goto out;
1246
1247 if (new_value)
1248 new_value = 1;
1249
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001250 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 length = count;
1252
1253out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001254 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001255 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return length;
1257}
1258
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001259static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001260 .read = sel_read_bool,
1261 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001262 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263};
1264
1265static ssize_t sel_commit_bools_write(struct file *filep,
1266 const char __user *buf,
1267 size_t count, loff_t *ppos)
1268{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001269 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001271 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int new_value;
1273
Jann Horn0da74122018-06-28 20:39:54 -04001274 if (count >= PAGE_SIZE)
1275 return -ENOMEM;
1276
1277 /* No partial writes. */
1278 if (*ppos != 0)
1279 return -EINVAL;
1280
1281 page = memdup_user_nul(buf, count);
1282 if (IS_ERR(page))
1283 return PTR_ERR(page);
1284
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001285 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001287 length = avc_has_perm(&selinux_state,
1288 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001289 SECCLASS_SECURITY, SECURITY__SETBOOL,
1290 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (length)
1292 goto out;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 length = -EINVAL;
1295 if (sscanf(page, "%d", &new_value) != 1)
1296 goto out;
1297
Eric Parisb77a4932010-11-23 11:40:08 -05001298 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001299 if (new_value && fsi->bool_pending_values)
1300 length = security_set_bools(fsi->state, fsi->bool_num,
1301 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Eric Parisb77a4932010-11-23 11:40:08 -05001303 if (!length)
1304 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001307 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001308 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return length;
1310}
1311
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001312static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001313 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001314 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315};
1316
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001317static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Al Viroad521842014-12-24 14:56:48 -05001319 d_genocide(de);
1320 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323#define BOOL_DIR_NAME "booleans"
1324
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001325static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Eric Parisb77a4932010-11-23 11:40:08 -05001327 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 ssize_t len;
1329 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001330 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct inode *inode = NULL;
1332 struct inode_security_struct *isec;
1333 char **names = NULL, *page;
1334 int num;
1335 int *values = NULL;
1336 u32 sid;
1337
1338 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001339 for (i = 0; i < fsi->bool_num; i++)
1340 kfree(fsi->bool_pending_names[i]);
1341 kfree(fsi->bool_pending_names);
1342 kfree(fsi->bool_pending_values);
1343 fsi->bool_num = 0;
1344 fsi->bool_pending_names = NULL;
1345 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001347 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Eric Parisb77a4932010-11-23 11:40:08 -05001349 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001350 page = (char *)get_zeroed_page(GFP_KERNEL);
1351 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001354 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001355 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto out;
1357
1358 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001359 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001361 if (!dentry)
1362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Eric Parisb77a4932010-11-23 11:40:08 -05001364 ret = -ENOMEM;
1365 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001366 if (!inode) {
1367 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001368 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001369 }
Eric Parisb77a4932010-11-23 11:40:08 -05001370
Eric Parisb77a4932010-11-23 11:40:08 -05001371 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001372 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001373 if (len >= PAGE_SIZE) {
1374 dput(dentry);
1375 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001376 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001377 }
Eric Parisb77a4932010-11-23 11:40:08 -05001378
Casey Schaufler80788c22018-09-21 17:19:11 -07001379 isec = selinux_inode(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001380 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001381 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001382 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001383 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1384 page);
1385 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001389 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001391 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 d_add(dentry, inode);
1393 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001394 fsi->bool_num = num;
1395 fsi->bool_pending_names = names;
1396 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001397
1398 free_page((unsigned long)page);
1399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400out:
1401 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001404 for (i = 0; i < num; i++)
1405 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 kfree(names);
1407 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001408 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001409 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001410
1411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1415 size_t count, loff_t *ppos)
1416{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001417 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1418 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 char tmpbuf[TMPBUFLEN];
1420 ssize_t length;
1421
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001422 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1423 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1425}
1426
Eric Paris18729812008-04-17 14:15:45 -04001427static ssize_t sel_write_avc_cache_threshold(struct file *file,
1428 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 size_t count, loff_t *ppos)
1430
1431{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001432 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1433 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001434 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001436 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001438 ret = avc_has_perm(&selinux_state,
1439 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001440 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1441 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001442 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Eric Parisb77a4932010-11-23 11:40:08 -05001445 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Eric Parisb77a4932010-11-23 11:40:08 -05001448 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001449 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001450 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001451
Al Viro8365a712015-12-24 00:08:06 -05001452 page = memdup_user_nul(buf, count);
1453 if (IS_ERR(page))
1454 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Eric Parisb77a4932010-11-23 11:40:08 -05001456 ret = -EINVAL;
1457 if (sscanf(page, "%u", &new_value) != 1)
1458 goto out;
1459
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001460 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463out:
Al Viro8365a712015-12-24 00:08:06 -05001464 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return ret;
1466}
1467
1468static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1469 size_t count, loff_t *ppos)
1470{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001471 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1472 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001474 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001477 if (!page)
1478 return -ENOMEM;
1479
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001480 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001481 if (length >= 0)
1482 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001484
1485 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001488static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 .read = sel_read_avc_cache_threshold,
1490 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001491 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492};
1493
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001494static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001496 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497};
1498
1499#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1500static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1501{
1502 int cpu;
1503
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301504 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (!cpu_possible(cpu))
1506 continue;
1507 *idx = cpu + 1;
1508 return &per_cpu(avc_cache_stats, cpu);
1509 }
1510 return NULL;
1511}
1512
1513static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1514{
1515 loff_t n = *pos - 1;
1516
1517 if (*pos == 0)
1518 return SEQ_START_TOKEN;
1519
1520 return sel_avc_get_stat_idx(&n);
1521}
1522
1523static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1524{
1525 return sel_avc_get_stat_idx(pos);
1526}
1527
1528static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1529{
1530 struct avc_cache_stats *st = v;
1531
Markus Elfring710a0642017-01-15 14:04:53 +01001532 if (v == SEQ_START_TOKEN) {
1533 seq_puts(seq,
1534 "lookups hits misses allocations reclaims frees\n");
1535 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001536 unsigned int lookups = st->lookups;
1537 unsigned int misses = st->misses;
1538 unsigned int hits = lookups - misses;
1539 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1540 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544}
1545
1546static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1547{ }
1548
Jan Engelhardt1996a102008-01-23 00:02:58 +01001549static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .start = sel_avc_stats_seq_start,
1551 .next = sel_avc_stats_seq_next,
1552 .show = sel_avc_stats_seq_show,
1553 .stop = sel_avc_stats_seq_stop,
1554};
1555
1556static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1557{
1558 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1559}
1560
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001561static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .open = sel_open_avc_cache_stats,
1563 .read = seq_read,
1564 .llseek = seq_lseek,
1565 .release = seq_release,
1566};
1567#endif
1568
1569static int sel_make_avc_files(struct dentry *dir)
1570{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001571 struct super_block *sb = dir->d_sb;
1572 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001573 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001574 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 { "cache_threshold",
1576 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1577 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1578#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1579 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1580#endif
1581 };
1582
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001583 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct inode *inode;
1585 struct dentry *dentry;
1586
1587 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001588 if (!dentry)
1589 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001592 if (!inode) {
1593 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001594 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001595 }
Eric Parisb77a4932010-11-23 11:40:08 -05001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001598 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 d_add(dentry, inode);
1600 }
Eric Parisb77a4932010-11-23 11:40:08 -05001601
1602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Eric Paris18729812008-04-17 14:15:45 -04001605static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001606 size_t count, loff_t *ppos)
1607{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001608 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001609 char *con;
1610 u32 sid, len;
1611 ssize_t ret;
1612
Al Viro496ad9a2013-01-23 17:07:38 -05001613 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001614 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001615 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001616 return ret;
1617
1618 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1619 kfree(con);
1620 return ret;
1621}
1622
1623static const struct file_operations sel_initcon_ops = {
1624 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001625 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001626};
1627
1628static int sel_make_initcon_files(struct dentry *dir)
1629{
Eric Parisb77a4932010-11-23 11:40:08 -05001630 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001631
1632 for (i = 1; i <= SECINITSID_NUM; i++) {
1633 struct inode *inode;
1634 struct dentry *dentry;
1635 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001636 if (!dentry)
1637 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001638
1639 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001640 if (!inode) {
1641 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001642 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001643 }
Eric Parisb77a4932010-11-23 11:40:08 -05001644
James Carterf0ee2e42007-04-04 10:11:29 -04001645 inode->i_fop = &sel_initcon_ops;
1646 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1647 d_add(dentry, inode);
1648 }
Eric Parisb77a4932010-11-23 11:40:08 -05001649
1650 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001651}
1652
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001653static inline unsigned long sel_class_to_ino(u16 class)
1654{
1655 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1656}
1657
1658static inline u16 sel_ino_to_class(unsigned long ino)
1659{
Eric Paris92ae9e82012-04-04 13:46:46 -04001660 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001661}
1662
1663static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1664{
1665 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1666}
1667
1668static inline u32 sel_ino_to_perm(unsigned long ino)
1669{
1670 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1671}
1672
Eric Paris18729812008-04-17 14:15:45 -04001673static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001674 size_t count, loff_t *ppos)
1675{
Al Viro496ad9a2013-01-23 17:07:38 -05001676 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001677 char res[TMPBUFLEN];
1678 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1679 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001680}
1681
1682static const struct file_operations sel_class_ops = {
1683 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001684 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001685};
1686
Eric Paris18729812008-04-17 14:15:45 -04001687static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001688 size_t count, loff_t *ppos)
1689{
Al Viro496ad9a2013-01-23 17:07:38 -05001690 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001691 char res[TMPBUFLEN];
1692 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1693 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001694}
1695
1696static const struct file_operations sel_perm_ops = {
1697 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001698 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001699};
1700
Paul Moore3bb56b22008-01-29 08:38:19 -05001701static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1702 size_t count, loff_t *ppos)
1703{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001704 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001705 int value;
1706 char tmpbuf[TMPBUFLEN];
1707 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001708 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001709
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001710 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001711 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1712
1713 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1714}
1715
1716static const struct file_operations sel_policycap_ops = {
1717 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001718 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001719};
1720
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001721static int sel_make_perm_files(char *objclass, int classvalue,
1722 struct dentry *dir)
1723{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001724 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001725 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001726 char **perms;
1727
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001728 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001729 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001730 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001731
1732 for (i = 0; i < nperms; i++) {
1733 struct inode *inode;
1734 struct dentry *dentry;
1735
Eric Parisb77a4932010-11-23 11:40:08 -05001736 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001737 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001738 if (!dentry)
1739 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001740
Eric Parisb77a4932010-11-23 11:40:08 -05001741 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001742 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001743 if (!inode) {
1744 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001745 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001746 }
Eric Parisb77a4932010-11-23 11:40:08 -05001747
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001748 inode->i_fop = &sel_perm_ops;
1749 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001750 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001751 d_add(dentry, inode);
1752 }
Eric Parisb77a4932010-11-23 11:40:08 -05001753 rc = 0;
1754out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001755 for (i = 0; i < nperms; i++)
1756 kfree(perms[i]);
1757 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001758 return rc;
1759}
1760
1761static int sel_make_class_dir_entries(char *classname, int index,
1762 struct dentry *dir)
1763{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001764 struct super_block *sb = dir->d_sb;
1765 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001766 struct dentry *dentry = NULL;
1767 struct inode *inode = NULL;
1768 int rc;
1769
1770 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001771 if (!dentry)
1772 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001773
1774 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001775 if (!inode) {
1776 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001777 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001778 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001779
1780 inode->i_fop = &sel_class_ops;
1781 inode->i_ino = sel_class_to_ino(index);
1782 d_add(dentry, inode);
1783
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001784 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001785 if (IS_ERR(dentry))
1786 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001787
1788 rc = sel_make_perm_files(classname, index, dentry);
1789
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001790 return rc;
1791}
1792
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001793static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001794{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001795
Eric Parisb77a4932010-11-23 11:40:08 -05001796 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001797 char **classes;
1798
1799 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001800 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001801
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001802 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001803 if (rc)
1804 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001805
1806 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001807 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001808
1809 for (i = 0; i < nclasses; i++) {
1810 struct dentry *class_name_dir;
1811
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001812 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1813 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001814 if (IS_ERR(class_name_dir)) {
1815 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001816 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001817 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001818
1819 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001820 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001821 class_name_dir);
1822 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001823 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001824 }
Eric Parisb77a4932010-11-23 11:40:08 -05001825 rc = 0;
1826out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001827 for (i = 0; i < nclasses; i++)
1828 kfree(classes[i]);
1829 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001830 return rc;
1831}
1832
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001833static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001834{
1835 unsigned int iter;
1836 struct dentry *dentry = NULL;
1837 struct inode *inode = NULL;
1838
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001839 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001840
1841 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001842 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001843 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001844 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001845 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001846 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001847
1848 if (dentry == NULL)
1849 return -ENOMEM;
1850
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001851 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001852 if (inode == NULL) {
1853 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001854 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001855 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001856
1857 inode->i_fop = &sel_policycap_ops;
1858 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1859 d_add(dentry, inode);
1860 }
1861
1862 return 0;
1863}
1864
Al Viroa1c2aa12012-03-18 20:36:59 -04001865static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001866 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Al Viroa1c2aa12012-03-18 20:36:59 -04001868 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct inode *inode;
1870
Al Viroa1c2aa12012-03-18 20:36:59 -04001871 if (!dentry)
1872 return ERR_PTR(-ENOMEM);
1873
1874 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1875 if (!inode) {
1876 dput(dentry);
1877 return ERR_PTR(-ENOMEM);
1878 }
Eric Parisb77a4932010-11-23 11:40:08 -05001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 inode->i_op = &simple_dir_inode_operations;
1881 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001882 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001883 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001884 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001886 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001887 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001888
Al Viroa1c2aa12012-03-18 20:36:59 -04001889 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001892#define NULL_FILE_NAME "null"
1893
Eric Paris18729812008-04-17 14:15:45 -04001894static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001896 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int ret;
1898 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001899 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct inode_security_struct *isec;
1901
Eric Biggerscda37122017-03-25 21:15:37 -07001902 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1904 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001905 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1907 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1909 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1910 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1911 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1912 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1913 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1914 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1915 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001916 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1917 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001918 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001919 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001920 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1921 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /* last one */ {""}
1923 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001924
1925 ret = selinux_fs_info_create(sb);
1926 if (ret)
1927 goto err;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1930 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001931 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001933 fsi = sb->s_fs_info;
1934 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1935 if (IS_ERR(fsi->bool_dir)) {
1936 ret = PTR_ERR(fsi->bool_dir);
1937 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001938 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Eric Parisb77a4932010-11-23 11:40:08 -05001941 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001943 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001944 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Eric Parisb77a4932010-11-23 11:40:08 -05001946 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001948 if (!inode) {
1949 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08001950 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08001951 }
Eric Parisb77a4932010-11-23 11:40:08 -05001952
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001953 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07001954 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 isec->sid = SECINITSID_DEVNULL;
1956 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001957 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1960 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001962 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001963 if (IS_ERR(dentry)) {
1964 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001965 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 ret = sel_make_avc_files(dentry);
1969 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001970 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001971
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001972 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001973 if (IS_ERR(dentry)) {
1974 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001975 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001976 }
James Carterf0ee2e42007-04-04 10:11:29 -04001977
1978 ret = sel_make_initcon_files(dentry);
1979 if (ret)
1980 goto err;
1981
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001982 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1983 if (IS_ERR(fsi->class_dir)) {
1984 ret = PTR_ERR(fsi->class_dir);
1985 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001986 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001987 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001988
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001989 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1990 &fsi->last_ino);
1991 if (IS_ERR(fsi->policycap_dir)) {
1992 ret = PTR_ERR(fsi->policycap_dir);
1993 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001994 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001995 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001996
1997 ret = sel_make_policy_nodes(fsi);
1998 if (ret)
1999 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05002000 return 0;
James Morris161ce452006-03-22 00:09:17 -08002001err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002002 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002003 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002004
2005 selinux_fs_info_free(sb);
2006
Eric Parisb77a4932010-11-23 11:40:08 -05002007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Al Virofc14f2f2010-07-25 01:48:30 +04002010static struct dentry *sel_mount(struct file_system_type *fs_type,
2011 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Al Virofc14f2f2010-07-25 01:48:30 +04002013 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002016static void sel_kill_sb(struct super_block *sb)
2017{
2018 selinux_fs_info_free(sb);
2019 kill_litter_super(sb);
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static struct file_system_type sel_fs_type = {
2023 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002024 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002025 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026};
2027
2028struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002029struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031static int __init init_sel_fs(void)
2032{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002033 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2034 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int err;
2036
2037 if (!selinux_enabled)
2038 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002039
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002040 err = sysfs_create_mount_point(fs_kobj, "selinux");
2041 if (err)
2042 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002045 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002046 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002047 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002048 }
Eric Parisb77a4932010-11-23 11:40:08 -05002049
Al Viro765927b2012-06-26 21:58:53 +04002050 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002051 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002052 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002053 err = PTR_ERR(selinuxfs_mount);
2054 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002056 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2057 &null_name);
2058 if (IS_ERR(selinux_null.dentry)) {
2059 pr_err("selinuxfs: could not lookup null!\n");
2060 err = PTR_ERR(selinux_null.dentry);
2061 selinux_null.dentry = NULL;
2062 }
Eric Parisb77a4932010-11-23 11:40:08 -05002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return err;
2065}
2066
2067__initcall(init_sel_fs);
2068
2069#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2070void exit_sel_fs(void)
2071{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002072 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002073 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002074 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 unregister_filesystem(&sel_fs_type);
2076}
2077#endif