blob: d903574f1f0d54886bf21499f76a0286786ecf12 [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>
David Howells920f50b2019-03-25 16:38:30 +000020#include <linux/fs_context.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040021#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/string.h>
25#include <linux/security.h>
26#include <linux/major.h>
27#include <linux/seq_file.h>
28#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000029#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040030#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070031#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040032#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36
37#include "flask.h"
38#include "avc.h"
39#include "avc_ss.h"
40#include "security.h"
41#include "objsec.h"
42#include "conditional.h"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044enum sel_inos {
45 SEL_ROOT_INO = 2,
46 SEL_LOAD, /* load policy */
47 SEL_ENFORCE, /* get or set enforcing status */
48 SEL_CONTEXT, /* validate context */
49 SEL_ACCESS, /* compute access decision */
50 SEL_CREATE, /* compute create labeling decision */
51 SEL_RELABEL, /* compute relabeling decision */
52 SEL_USER, /* compute reachable user contexts */
53 SEL_POLICYVERS, /* return policy version for this kernel */
54 SEL_COMMIT_BOOLS, /* commit new boolean values */
55 SEL_MLS, /* return if MLS policy is enabled */
56 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 SEL_MEMBER, /* compute polyinstantiation membership decision */
58 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070059 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040060 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
61 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090062 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040063 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050064 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040065 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Stephen Smalley0619f0f2018-03-20 11:59:11 -040068struct selinux_fs_info {
69 struct dentry *bool_dir;
70 unsigned int bool_num;
71 char **bool_pending_names;
72 unsigned int *bool_pending_values;
73 struct dentry *class_dir;
74 unsigned long last_class_ino;
75 bool policy_opened;
76 struct dentry *policycap_dir;
77 struct mutex mutex;
78 unsigned long last_ino;
79 struct selinux_state *state;
80 struct super_block *sb;
81};
82
83static int selinux_fs_info_create(struct super_block *sb)
84{
85 struct selinux_fs_info *fsi;
86
87 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
88 if (!fsi)
89 return -ENOMEM;
90
91 mutex_init(&fsi->mutex);
92 fsi->last_ino = SEL_INO_NEXT - 1;
93 fsi->state = &selinux_state;
94 fsi->sb = sb;
95 sb->s_fs_info = fsi;
96 return 0;
97}
98
99static void selinux_fs_info_free(struct super_block *sb)
100{
101 struct selinux_fs_info *fsi = sb->s_fs_info;
102 int i;
103
104 if (fsi) {
105 for (i = 0; i < fsi->bool_num; i++)
106 kfree(fsi->bool_pending_names[i]);
107 kfree(fsi->bool_pending_names);
108 kfree(fsi->bool_pending_values);
109 }
110 kfree(sb->s_fs_info);
111 sb->s_fs_info = NULL;
112}
James Carter6174eaf2007-04-04 16:18:39 -0400113
Paul Moore3bb56b22008-01-29 08:38:19 -0500114#define SEL_INITCON_INO_OFFSET 0x01000000
115#define SEL_BOOL_INO_OFFSET 0x02000000
116#define SEL_CLASS_INO_OFFSET 0x04000000
117#define SEL_POLICYCAP_INO_OFFSET 0x08000000
118#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define TMPBUFLEN 12
121static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
122 size_t count, loff_t *ppos)
123{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400124 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 char tmpbuf[TMPBUFLEN];
126 ssize_t length;
127
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500128 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400129 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
131}
132
133#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400134static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 size_t count, loff_t *ppos)
136
137{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400138 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
139 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500140 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500142 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Davi Arnautbfd51622005-10-30 14:59:24 -0800144 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500145 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500146
147 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500148 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500149 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500150
Al Viro8365a712015-12-24 00:08:06 -0500151 page = memdup_user_nul(buf, count);
152 if (IS_ERR(page))
153 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 length = -EINVAL;
156 if (sscanf(page, "%d", &new_value) != 1)
157 goto out;
158
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500159 new_value = !!new_value;
160
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400161 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500162 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500163 length = avc_has_perm(&selinux_state,
164 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500165 SECCLASS_SECURITY, SECURITY__SETENFORCE,
166 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (length)
168 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400169 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400170 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500171 " enabled=1 old-enabled=1 lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500172 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700173 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500174 audit_get_sessionid(current));
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)
Janne Karhunen42df7442019-06-14 15:20:14 +0300181 call_blocking_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"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500306 " enabled=0 old-enabled=1 lsm=selinux res=1",
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400307 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700308 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500309 audit_get_sessionid(current));
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. PeBenitoe47c8fc52007-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. PeBenitoe47c8fc52007-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 = -ENOMEM;
551 data = vmalloc(count);
552 if (!data)
553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 length = -EFAULT;
556 if (copy_from_user(data, buf, count) != 0)
557 goto out;
558
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400559 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500560 if (length) {
561 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400565 length = sel_make_policy_nodes(fsi);
566 if (length)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400567 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500568
569 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400570
571out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400572 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400573 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700574 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500575 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400577 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 vfree(data);
579 return length;
580}
581
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800582static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200584 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
586
Eric Paris18729812008-04-17 14:15:45 -0400587static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400589 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
590 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500591 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800592 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ssize_t length;
594
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500595 length = avc_has_perm(&selinux_state,
596 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500597 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500599 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400601 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500602 if (length)
603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400605 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500606 if (length)
607 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800608
Eric Parisb77a4932010-11-23 11:40:08 -0500609 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800610 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200611 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400612 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800613 goto out;
614 }
615
616 memcpy(buf, canon, len);
617 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800619 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return length;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
624 size_t count, loff_t *ppos)
625{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400626 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 char tmpbuf[TMPBUFLEN];
628 ssize_t length;
629
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400630 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
632}
633
Eric Paris18729812008-04-17 14:15:45 -0400634static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 size_t count, loff_t *ppos)
636{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400637 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500638 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ssize_t length;
640 unsigned int new_value;
641
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500642 length = avc_has_perm(&selinux_state,
643 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500644 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
645 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500647 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Davi Arnautbfd51622005-10-30 14:59:24 -0800649 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500650 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500651
652 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500653 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500654 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500655
Al Viro8365a712015-12-24 00:08:06 -0500656 page = memdup_user_nul(buf, count);
657 if (IS_ERR(page))
658 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 length = -EINVAL;
661 if (sscanf(page, "%u", &new_value) != 1)
662 goto out;
663
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400664 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 length = count;
666out:
Al Viro8365a712015-12-24 00:08:06 -0500667 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return length;
669}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800670static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 .read = sel_read_checkreqprot,
672 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200673 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500676static ssize_t sel_write_validatetrans(struct file *file,
677 const char __user *buf,
678 size_t count, loff_t *ppos)
679{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400680 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
681 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500682 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
683 char *req = NULL;
684 u32 osid, nsid, tsid;
685 u16 tclass;
686 int rc;
687
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500688 rc = avc_has_perm(&selinux_state,
689 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500690 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500691 if (rc)
692 goto out;
693
694 rc = -ENOMEM;
695 if (count >= PAGE_SIZE)
696 goto out;
697
698 /* No partial writes. */
699 rc = -EINVAL;
700 if (*ppos != 0)
701 goto out;
702
Al Viro0b884d22017-05-13 18:12:07 -0400703 req = memdup_user_nul(buf, count);
704 if (IS_ERR(req)) {
705 rc = PTR_ERR(req);
706 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500707 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400708 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500709
710 rc = -ENOMEM;
711 oldcon = kzalloc(count + 1, GFP_KERNEL);
712 if (!oldcon)
713 goto out;
714
715 newcon = kzalloc(count + 1, GFP_KERNEL);
716 if (!newcon)
717 goto out;
718
719 taskcon = kzalloc(count + 1, GFP_KERNEL);
720 if (!taskcon)
721 goto out;
722
723 rc = -EINVAL;
724 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
725 goto out;
726
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400727 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500728 if (rc)
729 goto out;
730
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400731 rc = security_context_str_to_sid(state, newcon, &nsid, 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, taskcon, &tsid, 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_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500740 if (!rc)
741 rc = count;
742out:
743 kfree(req);
744 kfree(oldcon);
745 kfree(newcon);
746 kfree(taskcon);
747 return rc;
748}
749
750static const struct file_operations sel_transition_ops = {
751 .write = sel_write_validatetrans,
752 .llseek = generic_file_llseek,
753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
757 */
Eric Paris18729812008-04-17 14:15:45 -0400758static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
759static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
760static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
761static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
762static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Biggers631d2b42018-07-17 10:43:56 -0700764static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 [SEL_ACCESS] = sel_write_access,
766 [SEL_CREATE] = sel_write_create,
767 [SEL_RELABEL] = sel_write_relabel,
768 [SEL_USER] = sel_write_user,
769 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800770 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771};
772
773static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
774{
Al Viro496ad9a2013-01-23 17:07:38 -0500775 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 char *data;
777 ssize_t rv;
778
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800779 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EINVAL;
781
782 data = simple_transaction_get(file, buf, size);
783 if (IS_ERR(data))
784 return PTR_ERR(data);
785
Eric Paris18729812008-04-17 14:15:45 -0400786 rv = write_op[ino](file, data, size);
787 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 simple_transaction_set(file, rv);
789 rv = size;
790 }
791 return rv;
792}
793
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800794static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .write = selinux_transaction_write,
796 .read = simple_transaction_read,
797 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200798 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799};
800
801/*
802 * payload - write methods
803 * If the method has a response, the response should be put in buf,
804 * and the length returned. Otherwise return 0 or and -error.
805 */
806
Eric Paris18729812008-04-17 14:15:45 -0400807static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400809 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
810 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500811 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 u32 ssid, tsid;
813 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct av_decision avd;
815 ssize_t length;
816
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500817 length = avc_has_perm(&selinux_state,
818 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500819 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800824 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Eric Parisb77a4932010-11-23 11:40:08 -0500828 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800829 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!tcon)
831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500834 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400837 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500838 if (length)
839 goto out;
840
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400841 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500842 if (length)
843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400845 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900848 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500849 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900851 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852out:
Eric Parisb77a4932010-11-23 11:40:08 -0500853 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 kfree(scon);
855 return length;
856}
857
Eric Paris18729812008-04-17 14:15:45 -0400858static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400860 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
861 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500862 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100863 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 u32 ssid, tsid, newsid;
865 u16 tclass;
866 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500867 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100869 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500871 length = avc_has_perm(&selinux_state,
872 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500873 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
874 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800879 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Eric Parisb77a4932010-11-23 11:40:08 -0500883 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800884 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!tcon)
886 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100888 length = -ENOMEM;
889 namebuf = kzalloc(size + 1, GFP_KERNEL);
890 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100893 length = -EINVAL;
894 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
895 if (nargs < 3 || nargs > 4)
896 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400897 if (nargs == 4) {
898 /*
899 * If and when the name of new object to be queried contains
900 * either whitespace or multibyte characters, they shall be
901 * encoded based on the percentage-encoding rule.
902 * If not encoded, the sscanf logic picks up only left-half
903 * of the supplied name; splitted by a whitespace unexpectedly.
904 */
905 char *r, *w;
906 int c1, c2;
907
908 r = w = namebuf;
909 do {
910 c1 = *r++;
911 if (c1 == '+')
912 c1 = ' ';
913 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800914 c1 = hex_to_bin(*r++);
915 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400916 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800917 c2 = hex_to_bin(*r++);
918 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400919 goto out;
920 c1 = (c1 << 4) | c2;
921 }
922 *w++ = c1;
923 } while (c1 != '\0');
924
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100925 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400926 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100927
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400928 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500929 if (length)
930 goto out;
931
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400932 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500933 if (length)
934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400936 length = security_transition_sid_user(state, ssid, tsid, tclass,
937 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500938 if (length)
939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400941 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500942 if (length)
943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Eric Parisb77a4932010-11-23 11:40:08 -0500945 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200947 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400948 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
952 memcpy(buf, newcon, len);
953 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954out:
Eric Parisb77a4932010-11-23 11:40:08 -0500955 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100956 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500957 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 kfree(scon);
959 return length;
960}
961
Eric Paris18729812008-04-17 14:15:45 -0400962static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400964 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
965 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500966 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 u32 ssid, tsid, newsid;
968 u16 tclass;
969 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500970 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 u32 len;
972
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500973 length = avc_has_perm(&selinux_state,
974 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500975 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
976 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800981 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Eric Parisb77a4932010-11-23 11:40:08 -0500985 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800986 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!tcon)
988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 length = -EINVAL;
991 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400994 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500995 if (length)
996 goto out;
997
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400998 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500999 if (length)
1000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001002 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
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_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001007 if (length)
1008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric Parisb77a4932010-11-23 11:40:08 -05001010 length = -ERANGE;
1011 if (len > SIMPLE_TRANSACTION_LIMIT)
1012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 memcpy(buf, newcon, len);
1015 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016out:
Eric Parisb77a4932010-11-23 11:40:08 -05001017 kfree(newcon);
1018 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 kfree(scon);
1020 return length;
1021}
1022
Eric Paris18729812008-04-17 14:15:45 -04001023static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001025 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1026 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001027 char *con = NULL, *user = NULL, *ptr;
1028 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ssize_t length;
1030 char *newcon;
1031 int i, rc;
1032 u32 len, nsids;
1033
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001034 length = avc_has_perm(&selinux_state,
1035 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001036 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1037 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001042 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Eric Parisb77a4932010-11-23 11:40:08 -05001046 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001047 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!user)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 length = -EINVAL;
1052 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001055 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001056 if (length)
1057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001059 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001060 if (length)
1061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 length = sprintf(buf, "%u", nsids) + 1;
1064 ptr = buf + length;
1065 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001066 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (rc) {
1068 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1072 kfree(newcon);
1073 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 memcpy(ptr, newcon, len);
1077 kfree(newcon);
1078 ptr += len;
1079 length += len;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081out:
Eric Parisb77a4932010-11-23 11:40:08 -05001082 kfree(sids);
1083 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 kfree(con);
1085 return length;
1086}
1087
Eric Paris18729812008-04-17 14:15:45 -04001088static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001090 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1091 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001092 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 u32 ssid, tsid, newsid;
1094 u16 tclass;
1095 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001096 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 u32 len;
1098
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001099 length = avc_has_perm(&selinux_state,
1100 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001101 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1102 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001107 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Parisb77a4932010-11-23 11:40:08 -05001111 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001112 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!tcon)
1114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 length = -EINVAL;
1117 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001120 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001121 if (length)
1122 goto out;
1123
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001124 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001125 if (length)
1126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001128 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
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_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001133 if (length)
1134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Eric Parisb77a4932010-11-23 11:40:08 -05001136 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001138 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001139 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
1143 memcpy(buf, newcon, len);
1144 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145out:
Eric Parisb77a4932010-11-23 11:40:08 -05001146 kfree(newcon);
1147 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 kfree(scon);
1149 return length;
1150}
1151
1152static struct inode *sel_make_inode(struct super_block *sb, int mode)
1153{
1154 struct inode *ret = new_inode(sb);
1155
1156 if (ret) {
1157 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001158 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 return ret;
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001166 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 char *page = NULL;
1168 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ssize_t ret;
1170 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001171 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001172 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001174 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Eric Parisb77a4932010-11-23 11:40:08 -05001176 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001177 if (index >= fsi->bool_num || strcmp(name,
1178 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001179 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Eric Parisb77a4932010-11-23 11:40:08 -05001181 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001182 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001183 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001184 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001186 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (cur_enforcing < 0) {
1188 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001189 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001192 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001193 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001194 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1195out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001196 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001198
1199out_unlock:
1200 mutex_unlock(&fsi->mutex);
1201 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1205 size_t count, loff_t *ppos)
1206{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001207 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001209 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001211 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001212 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jann Horn0da74122018-06-28 20:39:54 -04001214 if (count >= PAGE_SIZE)
1215 return -ENOMEM;
1216
1217 /* No partial writes. */
1218 if (*ppos != 0)
1219 return -EINVAL;
1220
1221 page = memdup_user_nul(buf, count);
1222 if (IS_ERR(page))
1223 return PTR_ERR(page);
1224
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001225 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001227 length = avc_has_perm(&selinux_state,
1228 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001229 SECCLASS_SECURITY, SECURITY__SETBOOL,
1230 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (length)
1232 goto out;
1233
Eric Parisb77a4932010-11-23 11:40:08 -05001234 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001235 if (index >= fsi->bool_num || strcmp(name,
1236 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001237 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 length = -EINVAL;
1240 if (sscanf(page, "%d", &new_value) != 1)
1241 goto out;
1242
1243 if (new_value)
1244 new_value = 1;
1245
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001246 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 length = count;
1248
1249out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001250 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001251 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return length;
1253}
1254
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001255static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001256 .read = sel_read_bool,
1257 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001258 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
1261static ssize_t sel_commit_bools_write(struct file *filep,
1262 const char __user *buf,
1263 size_t count, loff_t *ppos)
1264{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001265 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001267 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int new_value;
1269
Jann Horn0da74122018-06-28 20:39:54 -04001270 if (count >= PAGE_SIZE)
1271 return -ENOMEM;
1272
1273 /* No partial writes. */
1274 if (*ppos != 0)
1275 return -EINVAL;
1276
1277 page = memdup_user_nul(buf, count);
1278 if (IS_ERR(page))
1279 return PTR_ERR(page);
1280
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001281 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001283 length = avc_has_perm(&selinux_state,
1284 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001285 SECCLASS_SECURITY, SECURITY__SETBOOL,
1286 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (length)
1288 goto out;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 length = -EINVAL;
1291 if (sscanf(page, "%d", &new_value) != 1)
1292 goto out;
1293
Eric Parisb77a4932010-11-23 11:40:08 -05001294 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001295 if (new_value && fsi->bool_pending_values)
1296 length = security_set_bools(fsi->state, fsi->bool_num,
1297 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Eric Parisb77a4932010-11-23 11:40:08 -05001299 if (!length)
1300 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001303 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001304 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return length;
1306}
1307
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001308static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001309 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001310 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311};
1312
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001313static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Al Viroad521842014-12-24 14:56:48 -05001315 d_genocide(de);
1316 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319#define BOOL_DIR_NAME "booleans"
1320
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001321static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Eric Parisb77a4932010-11-23 11:40:08 -05001323 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 ssize_t len;
1325 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001326 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct inode *inode = NULL;
1328 struct inode_security_struct *isec;
1329 char **names = NULL, *page;
1330 int num;
1331 int *values = NULL;
1332 u32 sid;
1333
1334 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001335 for (i = 0; i < fsi->bool_num; i++)
1336 kfree(fsi->bool_pending_names[i]);
1337 kfree(fsi->bool_pending_names);
1338 kfree(fsi->bool_pending_values);
1339 fsi->bool_num = 0;
1340 fsi->bool_pending_names = NULL;
1341 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001343 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Eric Parisb77a4932010-11-23 11:40:08 -05001345 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001346 page = (char *)get_zeroed_page(GFP_KERNEL);
1347 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001350 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001351 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto out;
1353
1354 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001355 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001357 if (!dentry)
1358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Eric Parisb77a4932010-11-23 11:40:08 -05001360 ret = -ENOMEM;
1361 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001362 if (!inode) {
1363 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001364 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001365 }
Eric Parisb77a4932010-11-23 11:40:08 -05001366
Eric Parisb77a4932010-11-23 11:40:08 -05001367 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001368 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001369 if (len >= PAGE_SIZE) {
1370 dput(dentry);
1371 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001372 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001373 }
Eric Parisb77a4932010-11-23 11:40:08 -05001374
Casey Schaufler80788c22018-09-21 17:19:11 -07001375 isec = selinux_inode(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001376 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001377 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001378 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001379 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1380 page);
1381 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001385 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001387 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 d_add(dentry, inode);
1389 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001390 fsi->bool_num = num;
1391 fsi->bool_pending_names = names;
1392 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001393
1394 free_page((unsigned long)page);
1395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396out:
1397 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001400 for (i = 0; i < num; i++)
1401 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 kfree(names);
1403 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001404 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001405 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001406
1407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1411 size_t count, loff_t *ppos)
1412{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001413 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1414 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 char tmpbuf[TMPBUFLEN];
1416 ssize_t length;
1417
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001418 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1419 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1421}
1422
Eric Paris18729812008-04-17 14:15:45 -04001423static ssize_t sel_write_avc_cache_threshold(struct file *file,
1424 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 size_t count, loff_t *ppos)
1426
1427{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001428 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1429 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001430 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001432 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001434 ret = avc_has_perm(&selinux_state,
1435 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001436 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1437 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001438 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001439 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Eric Parisb77a4932010-11-23 11:40:08 -05001441 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001442 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric Parisb77a4932010-11-23 11:40:08 -05001444 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001445 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001446 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001447
Al Viro8365a712015-12-24 00:08:06 -05001448 page = memdup_user_nul(buf, count);
1449 if (IS_ERR(page))
1450 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Eric Parisb77a4932010-11-23 11:40:08 -05001452 ret = -EINVAL;
1453 if (sscanf(page, "%u", &new_value) != 1)
1454 goto out;
1455
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001456 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459out:
Al Viro8365a712015-12-24 00:08:06 -05001460 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return ret;
1462}
1463
1464static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1465 size_t count, loff_t *ppos)
1466{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001467 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1468 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001470 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001473 if (!page)
1474 return -ENOMEM;
1475
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001476 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001477 if (length >= 0)
1478 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001480
1481 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001484static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1485 size_t count, loff_t *ppos)
1486{
1487 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1488 struct selinux_state *state = fsi->state;
1489 char *page;
1490 ssize_t length;
1491
1492 page = (char *)__get_free_page(GFP_KERNEL);
1493 if (!page)
1494 return -ENOMEM;
1495
1496 length = security_sidtab_hash_stats(state, page);
1497 if (length >= 0)
1498 length = simple_read_from_buffer(buf, count, ppos, page,
1499 length);
1500 free_page((unsigned long)page);
1501
1502 return length;
1503}
1504
1505static const struct file_operations sel_sidtab_hash_stats_ops = {
1506 .read = sel_read_sidtab_hash_stats,
1507 .llseek = generic_file_llseek,
1508};
1509
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001510static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .read = sel_read_avc_cache_threshold,
1512 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001513 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514};
1515
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001516static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001518 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519};
1520
1521#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1522static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1523{
1524 int cpu;
1525
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301526 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!cpu_possible(cpu))
1528 continue;
1529 *idx = cpu + 1;
1530 return &per_cpu(avc_cache_stats, cpu);
1531 }
1532 return NULL;
1533}
1534
1535static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1536{
1537 loff_t n = *pos - 1;
1538
1539 if (*pos == 0)
1540 return SEQ_START_TOKEN;
1541
1542 return sel_avc_get_stat_idx(&n);
1543}
1544
1545static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1546{
1547 return sel_avc_get_stat_idx(pos);
1548}
1549
1550static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1551{
1552 struct avc_cache_stats *st = v;
1553
Markus Elfring710a0642017-01-15 14:04:53 +01001554 if (v == SEQ_START_TOKEN) {
1555 seq_puts(seq,
1556 "lookups hits misses allocations reclaims frees\n");
1557 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001558 unsigned int lookups = st->lookups;
1559 unsigned int misses = st->misses;
1560 unsigned int hits = lookups - misses;
1561 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1562 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return 0;
1566}
1567
1568static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1569{ }
1570
Jan Engelhardt1996a102008-01-23 00:02:58 +01001571static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 .start = sel_avc_stats_seq_start,
1573 .next = sel_avc_stats_seq_next,
1574 .show = sel_avc_stats_seq_show,
1575 .stop = sel_avc_stats_seq_stop,
1576};
1577
1578static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1579{
1580 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1581}
1582
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001583static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 .open = sel_open_avc_cache_stats,
1585 .read = seq_read,
1586 .llseek = seq_lseek,
1587 .release = seq_release,
1588};
1589#endif
1590
1591static int sel_make_avc_files(struct dentry *dir)
1592{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001593 struct super_block *sb = dir->d_sb;
1594 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001595 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001596 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 { "cache_threshold",
1598 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1599 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1600#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1601 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1602#endif
1603 };
1604
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001605 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct inode *inode;
1607 struct dentry *dentry;
1608
1609 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001610 if (!dentry)
1611 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001614 if (!inode) {
1615 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001616 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001617 }
Eric Parisb77a4932010-11-23 11:40:08 -05001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001620 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 d_add(dentry, inode);
1622 }
Eric Parisb77a4932010-11-23 11:40:08 -05001623
1624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001627static int sel_make_ss_files(struct dentry *dir)
1628{
1629 struct super_block *sb = dir->d_sb;
1630 struct selinux_fs_info *fsi = sb->s_fs_info;
1631 int i;
1632 static struct tree_descr files[] = {
1633 { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1634 };
1635
1636 for (i = 0; i < ARRAY_SIZE(files); i++) {
1637 struct inode *inode;
1638 struct dentry *dentry;
1639
1640 dentry = d_alloc_name(dir, files[i].name);
1641 if (!dentry)
1642 return -ENOMEM;
1643
1644 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1645 if (!inode) {
1646 dput(dentry);
1647 return -ENOMEM;
1648 }
1649
1650 inode->i_fop = files[i].ops;
1651 inode->i_ino = ++fsi->last_ino;
1652 d_add(dentry, inode);
1653 }
1654
1655 return 0;
1656}
1657
Eric Paris18729812008-04-17 14:15:45 -04001658static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001659 size_t count, loff_t *ppos)
1660{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001661 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001662 char *con;
1663 u32 sid, len;
1664 ssize_t ret;
1665
Al Viro496ad9a2013-01-23 17:07:38 -05001666 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001667 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001668 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001669 return ret;
1670
1671 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1672 kfree(con);
1673 return ret;
1674}
1675
1676static const struct file_operations sel_initcon_ops = {
1677 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001678 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001679};
1680
1681static int sel_make_initcon_files(struct dentry *dir)
1682{
Eric Parisb77a4932010-11-23 11:40:08 -05001683 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001684
1685 for (i = 1; i <= SECINITSID_NUM; i++) {
1686 struct inode *inode;
1687 struct dentry *dentry;
1688 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001689 if (!dentry)
1690 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001691
1692 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001693 if (!inode) {
1694 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001695 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001696 }
Eric Parisb77a4932010-11-23 11:40:08 -05001697
James Carterf0ee2e42007-04-04 10:11:29 -04001698 inode->i_fop = &sel_initcon_ops;
1699 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1700 d_add(dentry, inode);
1701 }
Eric Parisb77a4932010-11-23 11:40:08 -05001702
1703 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001704}
1705
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001706static inline unsigned long sel_class_to_ino(u16 class)
1707{
1708 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1709}
1710
1711static inline u16 sel_ino_to_class(unsigned long ino)
1712{
Eric Paris92ae9e82012-04-04 13:46:46 -04001713 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001714}
1715
1716static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1717{
1718 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1719}
1720
1721static inline u32 sel_ino_to_perm(unsigned long ino)
1722{
1723 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1724}
1725
Eric Paris18729812008-04-17 14:15:45 -04001726static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001727 size_t count, loff_t *ppos)
1728{
Al Viro496ad9a2013-01-23 17:07:38 -05001729 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001730 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001731 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001732 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001733}
1734
1735static const struct file_operations sel_class_ops = {
1736 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001737 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001738};
1739
Eric Paris18729812008-04-17 14:15:45 -04001740static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001741 size_t count, loff_t *ppos)
1742{
Al Viro496ad9a2013-01-23 17:07:38 -05001743 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001744 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001745 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001746 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001747}
1748
1749static const struct file_operations sel_perm_ops = {
1750 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001751 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001752};
1753
Paul Moore3bb56b22008-01-29 08:38:19 -05001754static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1755 size_t count, loff_t *ppos)
1756{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001757 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001758 int value;
1759 char tmpbuf[TMPBUFLEN];
1760 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001761 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001762
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001763 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001764 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1765
1766 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1767}
1768
1769static const struct file_operations sel_policycap_ops = {
1770 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001771 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001772};
1773
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001774static int sel_make_perm_files(char *objclass, int classvalue,
1775 struct dentry *dir)
1776{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001777 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001778 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001779 char **perms;
1780
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001781 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001782 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001783 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001784
1785 for (i = 0; i < nperms; i++) {
1786 struct inode *inode;
1787 struct dentry *dentry;
1788
Eric Parisb77a4932010-11-23 11:40:08 -05001789 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001790 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001791 if (!dentry)
1792 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001793
Eric Parisb77a4932010-11-23 11:40:08 -05001794 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001795 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001796 if (!inode) {
1797 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001798 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001799 }
Eric Parisb77a4932010-11-23 11:40:08 -05001800
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001801 inode->i_fop = &sel_perm_ops;
1802 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001803 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001804 d_add(dentry, inode);
1805 }
Eric Parisb77a4932010-11-23 11:40:08 -05001806 rc = 0;
1807out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001808 for (i = 0; i < nperms; i++)
1809 kfree(perms[i]);
1810 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001811 return rc;
1812}
1813
1814static int sel_make_class_dir_entries(char *classname, int index,
1815 struct dentry *dir)
1816{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001817 struct super_block *sb = dir->d_sb;
1818 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001819 struct dentry *dentry = NULL;
1820 struct inode *inode = NULL;
1821 int rc;
1822
1823 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001824 if (!dentry)
1825 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001826
1827 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001828 if (!inode) {
1829 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001830 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001831 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001832
1833 inode->i_fop = &sel_class_ops;
1834 inode->i_ino = sel_class_to_ino(index);
1835 d_add(dentry, inode);
1836
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001837 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001838 if (IS_ERR(dentry))
1839 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001840
1841 rc = sel_make_perm_files(classname, index, dentry);
1842
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001843 return rc;
1844}
1845
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001846static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001847{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001848
Eric Parisb77a4932010-11-23 11:40:08 -05001849 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001850 char **classes;
1851
1852 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001853 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001854
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001855 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001856 if (rc)
1857 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001858
1859 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001860 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001861
1862 for (i = 0; i < nclasses; i++) {
1863 struct dentry *class_name_dir;
1864
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001865 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1866 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001867 if (IS_ERR(class_name_dir)) {
1868 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001869 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001870 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001871
1872 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001873 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001874 class_name_dir);
1875 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001876 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001877 }
Eric Parisb77a4932010-11-23 11:40:08 -05001878 rc = 0;
1879out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001880 for (i = 0; i < nclasses; i++)
1881 kfree(classes[i]);
1882 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001883 return rc;
1884}
1885
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001886static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001887{
1888 unsigned int iter;
1889 struct dentry *dentry = NULL;
1890 struct inode *inode = NULL;
1891
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001892 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001893
1894 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001895 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001896 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001897 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001898 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001899 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001900
1901 if (dentry == NULL)
1902 return -ENOMEM;
1903
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001904 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001905 if (inode == NULL) {
1906 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001907 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001908 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001909
1910 inode->i_fop = &sel_policycap_ops;
1911 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1912 d_add(dentry, inode);
1913 }
1914
1915 return 0;
1916}
1917
Al Viroa1c2aa12012-03-18 20:36:59 -04001918static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001919 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Al Viroa1c2aa12012-03-18 20:36:59 -04001921 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct inode *inode;
1923
Al Viroa1c2aa12012-03-18 20:36:59 -04001924 if (!dentry)
1925 return ERR_PTR(-ENOMEM);
1926
1927 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1928 if (!inode) {
1929 dput(dentry);
1930 return ERR_PTR(-ENOMEM);
1931 }
Eric Parisb77a4932010-11-23 11:40:08 -05001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 inode->i_op = &simple_dir_inode_operations;
1934 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001935 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001936 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001937 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001939 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001940 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001941
Al Viroa1c2aa12012-03-18 20:36:59 -04001942 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001945#define NULL_FILE_NAME "null"
1946
David Howells920f50b2019-03-25 16:38:30 +00001947static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001949 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int ret;
1951 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001952 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct inode_security_struct *isec;
1954
Eric Biggerscda37122017-03-25 21:15:37 -07001955 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1957 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001958 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1960 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1961 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1962 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1963 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1964 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1965 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1966 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1967 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1968 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001969 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1970 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001971 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001972 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001973 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1974 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* last one */ {""}
1976 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001977
1978 ret = selinux_fs_info_create(sb);
1979 if (ret)
1980 goto err;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1983 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001984 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001986 fsi = sb->s_fs_info;
1987 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1988 if (IS_ERR(fsi->bool_dir)) {
1989 ret = PTR_ERR(fsi->bool_dir);
1990 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001991 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Eric Parisb77a4932010-11-23 11:40:08 -05001994 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001996 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001997 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Eric Parisb77a4932010-11-23 11:40:08 -05001999 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08002001 if (!inode) {
2002 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08002003 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08002004 }
Eric Parisb77a4932010-11-23 11:40:08 -05002005
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002006 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07002007 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 isec->sid = SECINITSID_DEVNULL;
2009 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01002010 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2013 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002015 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002016 if (IS_ERR(dentry)) {
2017 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08002018 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 ret = sel_make_avc_files(dentry);
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01002022
2023 dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2024 if (IS_ERR(dentry)) {
2025 ret = PTR_ERR(dentry);
2026 goto err;
2027 }
2028
2029 ret = sel_make_ss_files(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002031 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04002032
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002033 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002034 if (IS_ERR(dentry)) {
2035 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04002036 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002037 }
James Carterf0ee2e42007-04-04 10:11:29 -04002038
2039 ret = sel_make_initcon_files(dentry);
2040 if (ret)
2041 goto err;
2042
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002043 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
2044 if (IS_ERR(fsi->class_dir)) {
2045 ret = PTR_ERR(fsi->class_dir);
2046 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002047 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002048 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002049
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002050 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
2051 &fsi->last_ino);
2052 if (IS_ERR(fsi->policycap_dir)) {
2053 ret = PTR_ERR(fsi->policycap_dir);
2054 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002055 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002056 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002057
2058 ret = sel_make_policy_nodes(fsi);
2059 if (ret)
2060 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05002061 return 0;
James Morris161ce452006-03-22 00:09:17 -08002062err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002063 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002064 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002065
2066 selinux_fs_info_free(sb);
2067
Eric Parisb77a4932010-11-23 11:40:08 -05002068 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
David Howells920f50b2019-03-25 16:38:30 +00002071static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
David Howells920f50b2019-03-25 16:38:30 +00002073 return get_tree_single(fc, sel_fill_super);
2074}
2075
2076static const struct fs_context_operations sel_context_ops = {
2077 .get_tree = sel_get_tree,
2078};
2079
2080static int sel_init_fs_context(struct fs_context *fc)
2081{
2082 fc->ops = &sel_context_ops;
2083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002086static void sel_kill_sb(struct super_block *sb)
2087{
2088 selinux_fs_info_free(sb);
2089 kill_litter_super(sb);
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static struct file_system_type sel_fs_type = {
2093 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002094 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002095 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096};
2097
2098struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002099struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101static int __init init_sel_fs(void)
2102{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002103 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2104 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int err;
2106
Stephen Smalley6c5a6822019-12-17 09:15:10 -05002107 if (!selinux_enabled_boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002109
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002110 err = sysfs_create_mount_point(fs_kobj, "selinux");
2111 if (err)
2112 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002115 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002116 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002117 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002118 }
Eric Parisb77a4932010-11-23 11:40:08 -05002119
Al Viro765927b2012-06-26 21:58:53 +04002120 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002121 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002122 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002123 err = PTR_ERR(selinuxfs_mount);
2124 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002126 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2127 &null_name);
2128 if (IS_ERR(selinux_null.dentry)) {
2129 pr_err("selinuxfs: could not lookup null!\n");
2130 err = PTR_ERR(selinux_null.dentry);
2131 selinux_null.dentry = NULL;
2132 }
Eric Parisb77a4932010-11-23 11:40:08 -05002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return err;
2135}
2136
2137__initcall(init_sel_fs);
2138
2139#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2140void exit_sel_fs(void)
2141{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002142 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002143 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002144 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 unregister_filesystem(&sel_fs_type);
2146}
2147#endif