blob: e6c7643c3fc08219e07b3fd8894abb3a8df4ff1d [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"
171 " enabled=%d old-enabled=%d 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)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400174 audit_get_sessionid(current),
175 selinux_enabled, selinux_enabled);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400176 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500177 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500178 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500179 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400180 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500181 if (!new_value)
Janne Karhunen42df7442019-06-14 15:20:14 +0300182 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 length = count;
185out:
Al Viro8365a712015-12-24 00:08:06 -0500186 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return length;
188}
189#else
190#define sel_write_enforce NULL
191#endif
192
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800193static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .read = sel_read_enforce,
195 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200196 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Eric Paris3f120702007-09-21 14:37:10 -0400199static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
200 size_t count, loff_t *ppos)
201{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400202 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
203 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400204 char tmpbuf[TMPBUFLEN];
205 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500206 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400207 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400208 security_get_reject_unknown(state) :
209 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400210
211 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
212 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
213}
214
215static const struct file_operations sel_handle_unknown_ops = {
216 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200217 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400218};
219
KaiGai Kohei11904162010-09-14 18:28:39 +0900220static int sel_open_handle_status(struct inode *inode, struct file *filp)
221{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400222 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
223 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900224
225 if (!status)
226 return -ENOMEM;
227
228 filp->private_data = status;
229
230 return 0;
231}
232
233static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
234 size_t count, loff_t *ppos)
235{
236 struct page *status = filp->private_data;
237
238 BUG_ON(!status);
239
240 return simple_read_from_buffer(buf, count, ppos,
241 page_address(status),
242 sizeof(struct selinux_kernel_status));
243}
244
245static int sel_mmap_handle_status(struct file *filp,
246 struct vm_area_struct *vma)
247{
248 struct page *status = filp->private_data;
249 unsigned long size = vma->vm_end - vma->vm_start;
250
251 BUG_ON(!status);
252
253 /* only allows one page from the head */
254 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
255 return -EIO;
256 /* disallow writable mapping */
257 if (vma->vm_flags & VM_WRITE)
258 return -EPERM;
259 /* disallow mprotect() turns it into writable */
260 vma->vm_flags &= ~VM_MAYWRITE;
261
262 return remap_pfn_range(vma, vma->vm_start,
263 page_to_pfn(status),
264 size, vma->vm_page_prot);
265}
266
267static const struct file_operations sel_handle_status_ops = {
268 .open = sel_open_handle_status,
269 .read = sel_read_handle_status,
270 .mmap = sel_mmap_handle_status,
271 .llseek = generic_file_llseek,
272};
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400275static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 size_t count, loff_t *ppos)
277
278{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400279 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500280 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 ssize_t length;
282 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400283 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Davi Arnautbfd51622005-10-30 14:59:24 -0800285 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500286 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500287
288 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500289 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500290 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500291
Al Viro8365a712015-12-24 00:08:06 -0500292 page = memdup_user_nul(buf, count);
293 if (IS_ERR(page))
294 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 length = -EINVAL;
297 if (sscanf(page, "%d", &new_value) != 1)
298 goto out;
299
300 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400301 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400302 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500303 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400305 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400306 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
307 " enabled=%d old-enabled=%d lsm=selinux res=1",
308 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700309 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400310 audit_get_sessionid(current), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 length = count;
314out:
Al Viro8365a712015-12-24 00:08:06 -0500315 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return length;
317}
318#else
319#define sel_write_disable NULL
320#endif
321
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800322static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200324 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326
327static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400328 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 char tmpbuf[TMPBUFLEN];
331 ssize_t length;
332
333 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
334 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
335}
336
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800337static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200339 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
342/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400343static int sel_make_bools(struct selinux_fs_info *fsi);
344static int sel_make_classes(struct selinux_fs_info *fsi);
345static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400346
347/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400348static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400349 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351static ssize_t sel_read_mls(struct file *filp, char __user *buf,
352 size_t count, loff_t *ppos)
353{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400354 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 char tmpbuf[TMPBUFLEN];
356 ssize_t length;
357
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100358 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400359 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
361}
362
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800363static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200365 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366};
367
Eric Pariscee74f42010-10-13 17:50:25 -0400368struct policy_load_memory {
369 size_t len;
370 void *data;
371};
372
373static int sel_open_policy(struct inode *inode, struct file *filp)
374{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400375 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
376 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400377 struct policy_load_memory *plm = NULL;
378 int rc;
379
380 BUG_ON(filp->private_data);
381
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400382 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400383
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500384 rc = avc_has_perm(&selinux_state,
385 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500386 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400387 if (rc)
388 goto err;
389
390 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400391 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400392 goto err;
393
394 rc = -ENOMEM;
395 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
396 if (!plm)
397 goto err;
398
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400399 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500400 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400401 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500402 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400403 }
404
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400405 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400406 if (rc)
407 goto err;
408
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400409 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400410
411 filp->private_data = plm;
412
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400413 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400414
415 return 0;
416err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400417 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400418
419 if (plm)
420 vfree(plm->data);
421 kfree(plm);
422 return rc;
423}
424
425static int sel_release_policy(struct inode *inode, struct file *filp)
426{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400427 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400428 struct policy_load_memory *plm = filp->private_data;
429
430 BUG_ON(!plm);
431
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400432 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400433
434 vfree(plm->data);
435 kfree(plm);
436
437 return 0;
438}
439
440static ssize_t sel_read_policy(struct file *filp, char __user *buf,
441 size_t count, loff_t *ppos)
442{
443 struct policy_load_memory *plm = filp->private_data;
444 int ret;
445
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500446 ret = avc_has_perm(&selinux_state,
447 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500448 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400449 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400450 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400451
Jann Horn0da74122018-06-28 20:39:54 -0400452 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400453}
454
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530455static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400456{
Dave Jiang11bac802017-02-24 14:56:41 -0800457 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400458 unsigned long offset;
459 struct page *page;
460
461 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
462 return VM_FAULT_SIGBUS;
463
464 offset = vmf->pgoff << PAGE_SHIFT;
465 if (offset >= roundup(plm->len, PAGE_SIZE))
466 return VM_FAULT_SIGBUS;
467
468 page = vmalloc_to_page(plm->data + offset);
469 get_page(page);
470
471 vmf->page = page;
472
473 return 0;
474}
475
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700476static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400477 .fault = sel_mmap_policy_fault,
478 .page_mkwrite = sel_mmap_policy_fault,
479};
480
James Morrisad3fa082011-08-30 10:50:12 +1000481static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400482{
483 if (vma->vm_flags & VM_SHARED) {
484 /* do not allow mprotect to make mapping writable */
485 vma->vm_flags &= ~VM_MAYWRITE;
486
487 if (vma->vm_flags & VM_WRITE)
488 return -EACCES;
489 }
490
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700491 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400492 vma->vm_ops = &sel_mmap_policy_ops;
493
494 return 0;
495}
496
Eric Pariscee74f42010-10-13 17:50:25 -0400497static const struct file_operations sel_policy_ops = {
498 .open = sel_open_policy,
499 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400500 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400501 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500502 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400503};
504
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400505static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
506{
507 int ret;
508
509 ret = sel_make_bools(fsi);
510 if (ret) {
511 pr_err("SELinux: failed to load policy booleans\n");
512 return ret;
513 }
514
515 ret = sel_make_classes(fsi);
516 if (ret) {
517 pr_err("SELinux: failed to load policy classes\n");
518 return ret;
519 }
520
521 ret = sel_make_policycap(fsi);
522 if (ret) {
523 pr_err("SELinux: failed to load policy capabilities\n");
524 return ret;
525 }
526
527 return 0;
528}
529
Eric Paris18729812008-04-17 14:15:45 -0400530static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 size_t count, loff_t *ppos)
532
533{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400534 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ssize_t length;
536 void *data = NULL;
537
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400538 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500540 length = avc_has_perm(&selinux_state,
541 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500542 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (length)
544 goto out;
545
Eric Parisb77a4932010-11-23 11:40:08 -0500546 /* No partial writes. */
547 length = -EINVAL;
548 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Eric Parisb77a4932010-11-23 11:40:08 -0500551 length = -EFBIG;
552 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500554
555 length = -ENOMEM;
556 data = vmalloc(count);
557 if (!data)
558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 length = -EFAULT;
561 if (copy_from_user(data, buf, count) != 0)
562 goto out;
563
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400564 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500565 if (length) {
566 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400570 length = sel_make_policy_nodes(fsi);
571 if (length)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400572 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500573
574 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400575
576out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400577 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400578 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700579 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500580 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400582 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 vfree(data);
584 return length;
585}
586
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800587static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200589 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
Eric Paris18729812008-04-17 14:15:45 -0400592static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400594 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
595 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500596 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800597 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ssize_t length;
599
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500600 length = avc_has_perm(&selinux_state,
601 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500602 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400606 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500607 if (length)
608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400610 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500611 if (length)
612 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800613
Eric Parisb77a4932010-11-23 11:40:08 -0500614 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800615 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200616 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400617 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800618 goto out;
619 }
620
621 memcpy(buf, canon, len);
622 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800624 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return length;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
629 size_t count, loff_t *ppos)
630{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400631 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 char tmpbuf[TMPBUFLEN];
633 ssize_t length;
634
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400635 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
637}
638
Eric Paris18729812008-04-17 14:15:45 -0400639static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 size_t count, loff_t *ppos)
641{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400642 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500643 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ssize_t length;
645 unsigned int new_value;
646
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500647 length = avc_has_perm(&selinux_state,
648 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500649 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
650 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500652 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Davi Arnautbfd51622005-10-30 14:59:24 -0800654 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500655 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500656
657 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500658 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500659 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500660
Al Viro8365a712015-12-24 00:08:06 -0500661 page = memdup_user_nul(buf, count);
662 if (IS_ERR(page))
663 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 length = -EINVAL;
666 if (sscanf(page, "%u", &new_value) != 1)
667 goto out;
668
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400669 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 length = count;
671out:
Al Viro8365a712015-12-24 00:08:06 -0500672 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return length;
674}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800675static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .read = sel_read_checkreqprot,
677 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200678 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679};
680
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500681static ssize_t sel_write_validatetrans(struct file *file,
682 const char __user *buf,
683 size_t count, loff_t *ppos)
684{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400685 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
686 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500687 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
688 char *req = NULL;
689 u32 osid, nsid, tsid;
690 u16 tclass;
691 int rc;
692
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500693 rc = avc_has_perm(&selinux_state,
694 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500695 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500696 if (rc)
697 goto out;
698
699 rc = -ENOMEM;
700 if (count >= PAGE_SIZE)
701 goto out;
702
703 /* No partial writes. */
704 rc = -EINVAL;
705 if (*ppos != 0)
706 goto out;
707
Al Viro0b884d22017-05-13 18:12:07 -0400708 req = memdup_user_nul(buf, count);
709 if (IS_ERR(req)) {
710 rc = PTR_ERR(req);
711 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500712 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400713 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500714
715 rc = -ENOMEM;
716 oldcon = kzalloc(count + 1, GFP_KERNEL);
717 if (!oldcon)
718 goto out;
719
720 newcon = kzalloc(count + 1, GFP_KERNEL);
721 if (!newcon)
722 goto out;
723
724 taskcon = kzalloc(count + 1, GFP_KERNEL);
725 if (!taskcon)
726 goto out;
727
728 rc = -EINVAL;
729 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
730 goto out;
731
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400732 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500733 if (rc)
734 goto out;
735
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400736 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500737 if (rc)
738 goto out;
739
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400740 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500741 if (rc)
742 goto out;
743
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400744 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500745 if (!rc)
746 rc = count;
747out:
748 kfree(req);
749 kfree(oldcon);
750 kfree(newcon);
751 kfree(taskcon);
752 return rc;
753}
754
755static const struct file_operations sel_transition_ops = {
756 .write = sel_write_validatetrans,
757 .llseek = generic_file_llseek,
758};
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
761 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
762 */
Eric Paris18729812008-04-17 14:15:45 -0400763static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
764static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
765static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
766static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
767static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Eric Biggers631d2b42018-07-17 10:43:56 -0700769static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 [SEL_ACCESS] = sel_write_access,
771 [SEL_CREATE] = sel_write_create,
772 [SEL_RELABEL] = sel_write_relabel,
773 [SEL_USER] = sel_write_user,
774 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800775 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
777
778static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
779{
Al Viro496ad9a2013-01-23 17:07:38 -0500780 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 char *data;
782 ssize_t rv;
783
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800784 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EINVAL;
786
787 data = simple_transaction_get(file, buf, size);
788 if (IS_ERR(data))
789 return PTR_ERR(data);
790
Eric Paris18729812008-04-17 14:15:45 -0400791 rv = write_op[ino](file, data, size);
792 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 simple_transaction_set(file, rv);
794 rv = size;
795 }
796 return rv;
797}
798
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800799static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .write = selinux_transaction_write,
801 .read = simple_transaction_read,
802 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200803 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
806/*
807 * payload - write methods
808 * If the method has a response, the response should be put in buf,
809 * and the length returned. Otherwise return 0 or and -error.
810 */
811
Eric Paris18729812008-04-17 14:15:45 -0400812static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400814 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
815 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500816 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 u32 ssid, tsid;
818 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct av_decision avd;
820 ssize_t length;
821
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500822 length = avc_has_perm(&selinux_state,
823 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500824 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800829 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Eric Parisb77a4932010-11-23 11:40:08 -0500833 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800834 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!tcon)
836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500839 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500840 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400842 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500843 if (length)
844 goto out;
845
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400846 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500847 if (length)
848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400850 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900853 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500854 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900856 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857out:
Eric Parisb77a4932010-11-23 11:40:08 -0500858 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 kfree(scon);
860 return length;
861}
862
Eric Paris18729812008-04-17 14:15:45 -0400863static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400865 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
866 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500867 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100868 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 u32 ssid, tsid, newsid;
870 u16 tclass;
871 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500872 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100874 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500876 length = avc_has_perm(&selinux_state,
877 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500878 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
879 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800884 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500886 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Eric Parisb77a4932010-11-23 11:40:08 -0500888 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800889 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!tcon)
891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100893 length = -ENOMEM;
894 namebuf = kzalloc(size + 1, GFP_KERNEL);
895 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100898 length = -EINVAL;
899 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
900 if (nargs < 3 || nargs > 4)
901 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400902 if (nargs == 4) {
903 /*
904 * If and when the name of new object to be queried contains
905 * either whitespace or multibyte characters, they shall be
906 * encoded based on the percentage-encoding rule.
907 * If not encoded, the sscanf logic picks up only left-half
908 * of the supplied name; splitted by a whitespace unexpectedly.
909 */
910 char *r, *w;
911 int c1, c2;
912
913 r = w = namebuf;
914 do {
915 c1 = *r++;
916 if (c1 == '+')
917 c1 = ' ';
918 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800919 c1 = hex_to_bin(*r++);
920 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400921 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800922 c2 = hex_to_bin(*r++);
923 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400924 goto out;
925 c1 = (c1 << 4) | c2;
926 }
927 *w++ = c1;
928 } while (c1 != '\0');
929
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100930 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400931 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100932
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400933 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500934 if (length)
935 goto out;
936
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400937 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
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_transition_sid_user(state, ssid, tsid, tclass,
942 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500943 if (length)
944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400946 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500947 if (length)
948 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Parisb77a4932010-11-23 11:40:08 -0500950 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200952 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400953 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500954 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957 memcpy(buf, newcon, len);
958 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959out:
Eric Parisb77a4932010-11-23 11:40:08 -0500960 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100961 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500962 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 kfree(scon);
964 return length;
965}
966
Eric Paris18729812008-04-17 14:15:45 -0400967static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400969 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
970 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500971 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 u32 ssid, tsid, newsid;
973 u16 tclass;
974 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500975 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 u32 len;
977
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500978 length = avc_has_perm(&selinux_state,
979 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500980 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
981 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800986 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Eric Parisb77a4932010-11-23 11:40:08 -0500990 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800991 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (!tcon)
993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 length = -EINVAL;
996 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400999 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001000 if (length)
1001 goto out;
1002
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001003 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001004 if (length)
1005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001007 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001008 if (length)
1009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001011 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001012 if (length)
1013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Parisb77a4932010-11-23 11:40:08 -05001015 length = -ERANGE;
1016 if (len > SIMPLE_TRANSACTION_LIMIT)
1017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 memcpy(buf, newcon, len);
1020 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021out:
Eric Parisb77a4932010-11-23 11:40:08 -05001022 kfree(newcon);
1023 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 kfree(scon);
1025 return length;
1026}
1027
Eric Paris18729812008-04-17 14:15:45 -04001028static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001030 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1031 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001032 char *con = NULL, *user = NULL, *ptr;
1033 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 ssize_t length;
1035 char *newcon;
1036 int i, rc;
1037 u32 len, nsids;
1038
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001039 length = avc_has_perm(&selinux_state,
1040 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001041 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1042 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001047 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Eric Parisb77a4932010-11-23 11:40:08 -05001051 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001052 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!user)
1054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 length = -EINVAL;
1057 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001058 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001060 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001061 if (length)
1062 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001064 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001065 if (length)
1066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 length = sprintf(buf, "%u", nsids) + 1;
1069 ptr = buf + length;
1070 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001071 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (rc) {
1073 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1077 kfree(newcon);
1078 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001079 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 memcpy(ptr, newcon, len);
1082 kfree(newcon);
1083 ptr += len;
1084 length += len;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086out:
Eric Parisb77a4932010-11-23 11:40:08 -05001087 kfree(sids);
1088 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 kfree(con);
1090 return length;
1091}
1092
Eric Paris18729812008-04-17 14:15:45 -04001093static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001095 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1096 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001097 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 u32 ssid, tsid, newsid;
1099 u16 tclass;
1100 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001101 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 u32 len;
1103
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001104 length = avc_has_perm(&selinux_state,
1105 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001106 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1107 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001112 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Parisb77a4932010-11-23 11:40:08 -05001116 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001117 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!tcon)
1119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 length = -EINVAL;
1122 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001125 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001126 if (length)
1127 goto out;
1128
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001129 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001130 if (length)
1131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001133 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001134 if (length)
1135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001137 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001138 if (length)
1139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Eric Parisb77a4932010-11-23 11:40:08 -05001141 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001143 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001144 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 memcpy(buf, newcon, len);
1149 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150out:
Eric Parisb77a4932010-11-23 11:40:08 -05001151 kfree(newcon);
1152 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 kfree(scon);
1154 return length;
1155}
1156
1157static struct inode *sel_make_inode(struct super_block *sb, int mode)
1158{
1159 struct inode *ret = new_inode(sb);
1160
1161 if (ret) {
1162 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001163 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return ret;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1169 size_t count, loff_t *ppos)
1170{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001171 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 char *page = NULL;
1173 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 ssize_t ret;
1175 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001176 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001177 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001179 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Eric Parisb77a4932010-11-23 11:40:08 -05001181 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001182 if (index >= fsi->bool_num || strcmp(name,
1183 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001184 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Eric Parisb77a4932010-11-23 11:40:08 -05001186 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001187 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001188 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001189 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001191 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (cur_enforcing < 0) {
1193 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001194 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001197 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001198 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001199 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1200out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001201 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001203
1204out_unlock:
1205 mutex_unlock(&fsi->mutex);
1206 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1210 size_t count, loff_t *ppos)
1211{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001212 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001214 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001216 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001217 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jann Horn0da74122018-06-28 20:39:54 -04001219 if (count >= PAGE_SIZE)
1220 return -ENOMEM;
1221
1222 /* No partial writes. */
1223 if (*ppos != 0)
1224 return -EINVAL;
1225
1226 page = memdup_user_nul(buf, count);
1227 if (IS_ERR(page))
1228 return PTR_ERR(page);
1229
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001230 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001232 length = avc_has_perm(&selinux_state,
1233 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001234 SECCLASS_SECURITY, SECURITY__SETBOOL,
1235 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (length)
1237 goto out;
1238
Eric Parisb77a4932010-11-23 11:40:08 -05001239 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001240 if (index >= fsi->bool_num || strcmp(name,
1241 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001242 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 length = -EINVAL;
1245 if (sscanf(page, "%d", &new_value) != 1)
1246 goto out;
1247
1248 if (new_value)
1249 new_value = 1;
1250
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001251 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 length = count;
1253
1254out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001255 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001256 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return length;
1258}
1259
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001260static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001261 .read = sel_read_bool,
1262 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001263 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264};
1265
1266static ssize_t sel_commit_bools_write(struct file *filep,
1267 const char __user *buf,
1268 size_t count, loff_t *ppos)
1269{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001270 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001272 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int new_value;
1274
Jann Horn0da74122018-06-28 20:39:54 -04001275 if (count >= PAGE_SIZE)
1276 return -ENOMEM;
1277
1278 /* No partial writes. */
1279 if (*ppos != 0)
1280 return -EINVAL;
1281
1282 page = memdup_user_nul(buf, count);
1283 if (IS_ERR(page))
1284 return PTR_ERR(page);
1285
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001286 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001288 length = avc_has_perm(&selinux_state,
1289 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001290 SECCLASS_SECURITY, SECURITY__SETBOOL,
1291 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (length)
1293 goto out;
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 length = -EINVAL;
1296 if (sscanf(page, "%d", &new_value) != 1)
1297 goto out;
1298
Eric Parisb77a4932010-11-23 11:40:08 -05001299 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001300 if (new_value && fsi->bool_pending_values)
1301 length = security_set_bools(fsi->state, fsi->bool_num,
1302 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Eric Parisb77a4932010-11-23 11:40:08 -05001304 if (!length)
1305 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001308 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001309 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return length;
1311}
1312
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001313static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001314 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001315 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316};
1317
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001318static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Al Viroad521842014-12-24 14:56:48 -05001320 d_genocide(de);
1321 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324#define BOOL_DIR_NAME "booleans"
1325
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001326static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Eric Parisb77a4932010-11-23 11:40:08 -05001328 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ssize_t len;
1330 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001331 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct inode *inode = NULL;
1333 struct inode_security_struct *isec;
1334 char **names = NULL, *page;
1335 int num;
1336 int *values = NULL;
1337 u32 sid;
1338
1339 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001340 for (i = 0; i < fsi->bool_num; i++)
1341 kfree(fsi->bool_pending_names[i]);
1342 kfree(fsi->bool_pending_names);
1343 kfree(fsi->bool_pending_values);
1344 fsi->bool_num = 0;
1345 fsi->bool_pending_names = NULL;
1346 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001348 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Eric Parisb77a4932010-11-23 11:40:08 -05001350 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001351 page = (char *)get_zeroed_page(GFP_KERNEL);
1352 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001355 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001356 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto out;
1358
1359 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001360 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001362 if (!dentry)
1363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Eric Parisb77a4932010-11-23 11:40:08 -05001365 ret = -ENOMEM;
1366 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001367 if (!inode) {
1368 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001369 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001370 }
Eric Parisb77a4932010-11-23 11:40:08 -05001371
Eric Parisb77a4932010-11-23 11:40:08 -05001372 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001373 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001374 if (len >= PAGE_SIZE) {
1375 dput(dentry);
1376 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001377 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001378 }
Eric Parisb77a4932010-11-23 11:40:08 -05001379
Casey Schaufler80788c22018-09-21 17:19:11 -07001380 isec = selinux_inode(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001381 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001382 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001383 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001384 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1385 page);
1386 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001387 }
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001390 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001392 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 d_add(dentry, inode);
1394 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001395 fsi->bool_num = num;
1396 fsi->bool_pending_names = names;
1397 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001398
1399 free_page((unsigned long)page);
1400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401out:
1402 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001405 for (i = 0; i < num; i++)
1406 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 kfree(names);
1408 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001409 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001410 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001411
1412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1416 size_t count, loff_t *ppos)
1417{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001418 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1419 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 char tmpbuf[TMPBUFLEN];
1421 ssize_t length;
1422
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001423 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1424 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1426}
1427
Eric Paris18729812008-04-17 14:15:45 -04001428static ssize_t sel_write_avc_cache_threshold(struct file *file,
1429 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 size_t count, loff_t *ppos)
1431
1432{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001433 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1434 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001435 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001437 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001439 ret = avc_has_perm(&selinux_state,
1440 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001441 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1442 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001443 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Eric Parisb77a4932010-11-23 11:40:08 -05001446 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001447 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Eric Parisb77a4932010-11-23 11:40:08 -05001449 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001450 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001451 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001452
Al Viro8365a712015-12-24 00:08:06 -05001453 page = memdup_user_nul(buf, count);
1454 if (IS_ERR(page))
1455 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Eric Parisb77a4932010-11-23 11:40:08 -05001457 ret = -EINVAL;
1458 if (sscanf(page, "%u", &new_value) != 1)
1459 goto out;
1460
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001461 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464out:
Al Viro8365a712015-12-24 00:08:06 -05001465 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return ret;
1467}
1468
1469static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1470 size_t count, loff_t *ppos)
1471{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001472 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1473 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001475 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001478 if (!page)
1479 return -ENOMEM;
1480
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001481 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001482 if (length >= 0)
1483 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001485
1486 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001489static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 .read = sel_read_avc_cache_threshold,
1491 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001492 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493};
1494
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001495static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001497 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498};
1499
1500#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1501static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1502{
1503 int cpu;
1504
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301505 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (!cpu_possible(cpu))
1507 continue;
1508 *idx = cpu + 1;
1509 return &per_cpu(avc_cache_stats, cpu);
1510 }
1511 return NULL;
1512}
1513
1514static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1515{
1516 loff_t n = *pos - 1;
1517
1518 if (*pos == 0)
1519 return SEQ_START_TOKEN;
1520
1521 return sel_avc_get_stat_idx(&n);
1522}
1523
1524static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1525{
1526 return sel_avc_get_stat_idx(pos);
1527}
1528
1529static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1530{
1531 struct avc_cache_stats *st = v;
1532
Markus Elfring710a0642017-01-15 14:04:53 +01001533 if (v == SEQ_START_TOKEN) {
1534 seq_puts(seq,
1535 "lookups hits misses allocations reclaims frees\n");
1536 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001537 unsigned int lookups = st->lookups;
1538 unsigned int misses = st->misses;
1539 unsigned int hits = lookups - misses;
1540 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1541 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
1545}
1546
1547static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1548{ }
1549
Jan Engelhardt1996a102008-01-23 00:02:58 +01001550static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 .start = sel_avc_stats_seq_start,
1552 .next = sel_avc_stats_seq_next,
1553 .show = sel_avc_stats_seq_show,
1554 .stop = sel_avc_stats_seq_stop,
1555};
1556
1557static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1558{
1559 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1560}
1561
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001562static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .open = sel_open_avc_cache_stats,
1564 .read = seq_read,
1565 .llseek = seq_lseek,
1566 .release = seq_release,
1567};
1568#endif
1569
1570static int sel_make_avc_files(struct dentry *dir)
1571{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001572 struct super_block *sb = dir->d_sb;
1573 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001574 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001575 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 { "cache_threshold",
1577 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1578 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1579#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1580 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1581#endif
1582 };
1583
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001584 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct inode *inode;
1586 struct dentry *dentry;
1587
1588 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001589 if (!dentry)
1590 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001593 if (!inode) {
1594 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001595 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001596 }
Eric Parisb77a4932010-11-23 11:40:08 -05001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001599 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 d_add(dentry, inode);
1601 }
Eric Parisb77a4932010-11-23 11:40:08 -05001602
1603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Eric Paris18729812008-04-17 14:15:45 -04001606static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001607 size_t count, loff_t *ppos)
1608{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001609 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001610 char *con;
1611 u32 sid, len;
1612 ssize_t ret;
1613
Al Viro496ad9a2013-01-23 17:07:38 -05001614 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001615 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001616 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001617 return ret;
1618
1619 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1620 kfree(con);
1621 return ret;
1622}
1623
1624static const struct file_operations sel_initcon_ops = {
1625 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001626 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001627};
1628
1629static int sel_make_initcon_files(struct dentry *dir)
1630{
Eric Parisb77a4932010-11-23 11:40:08 -05001631 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001632
1633 for (i = 1; i <= SECINITSID_NUM; i++) {
1634 struct inode *inode;
1635 struct dentry *dentry;
1636 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001637 if (!dentry)
1638 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001639
1640 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001641 if (!inode) {
1642 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001643 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001644 }
Eric Parisb77a4932010-11-23 11:40:08 -05001645
James Carterf0ee2e42007-04-04 10:11:29 -04001646 inode->i_fop = &sel_initcon_ops;
1647 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1648 d_add(dentry, inode);
1649 }
Eric Parisb77a4932010-11-23 11:40:08 -05001650
1651 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001652}
1653
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001654static inline unsigned long sel_class_to_ino(u16 class)
1655{
1656 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1657}
1658
1659static inline u16 sel_ino_to_class(unsigned long ino)
1660{
Eric Paris92ae9e82012-04-04 13:46:46 -04001661 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001662}
1663
1664static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1665{
1666 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1667}
1668
1669static inline u32 sel_ino_to_perm(unsigned long ino)
1670{
1671 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1672}
1673
Eric Paris18729812008-04-17 14:15:45 -04001674static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001675 size_t count, loff_t *ppos)
1676{
Al Viro496ad9a2013-01-23 17:07:38 -05001677 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001678 char res[TMPBUFLEN];
1679 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1680 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001681}
1682
1683static const struct file_operations sel_class_ops = {
1684 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001685 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001686};
1687
Eric Paris18729812008-04-17 14:15:45 -04001688static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001689 size_t count, loff_t *ppos)
1690{
Al Viro496ad9a2013-01-23 17:07:38 -05001691 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001692 char res[TMPBUFLEN];
1693 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1694 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001695}
1696
1697static const struct file_operations sel_perm_ops = {
1698 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001699 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001700};
1701
Paul Moore3bb56b22008-01-29 08:38:19 -05001702static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1703 size_t count, loff_t *ppos)
1704{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001705 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001706 int value;
1707 char tmpbuf[TMPBUFLEN];
1708 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001709 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001710
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001711 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001712 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1713
1714 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1715}
1716
1717static const struct file_operations sel_policycap_ops = {
1718 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001719 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001720};
1721
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001722static int sel_make_perm_files(char *objclass, int classvalue,
1723 struct dentry *dir)
1724{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001725 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001726 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001727 char **perms;
1728
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001729 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001730 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001731 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001732
1733 for (i = 0; i < nperms; i++) {
1734 struct inode *inode;
1735 struct dentry *dentry;
1736
Eric Parisb77a4932010-11-23 11:40:08 -05001737 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001738 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001739 if (!dentry)
1740 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001741
Eric Parisb77a4932010-11-23 11:40:08 -05001742 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001743 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001744 if (!inode) {
1745 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001746 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001747 }
Eric Parisb77a4932010-11-23 11:40:08 -05001748
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001749 inode->i_fop = &sel_perm_ops;
1750 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001751 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001752 d_add(dentry, inode);
1753 }
Eric Parisb77a4932010-11-23 11:40:08 -05001754 rc = 0;
1755out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001756 for (i = 0; i < nperms; i++)
1757 kfree(perms[i]);
1758 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001759 return rc;
1760}
1761
1762static int sel_make_class_dir_entries(char *classname, int index,
1763 struct dentry *dir)
1764{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001765 struct super_block *sb = dir->d_sb;
1766 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001767 struct dentry *dentry = NULL;
1768 struct inode *inode = NULL;
1769 int rc;
1770
1771 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001772 if (!dentry)
1773 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001774
1775 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001776 if (!inode) {
1777 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001778 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001779 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001780
1781 inode->i_fop = &sel_class_ops;
1782 inode->i_ino = sel_class_to_ino(index);
1783 d_add(dentry, inode);
1784
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001785 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001786 if (IS_ERR(dentry))
1787 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001788
1789 rc = sel_make_perm_files(classname, index, dentry);
1790
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001791 return rc;
1792}
1793
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001794static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001795{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001796
Eric Parisb77a4932010-11-23 11:40:08 -05001797 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001798 char **classes;
1799
1800 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001801 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001802
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001803 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001804 if (rc)
1805 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001806
1807 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001808 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001809
1810 for (i = 0; i < nclasses; i++) {
1811 struct dentry *class_name_dir;
1812
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001813 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1814 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001815 if (IS_ERR(class_name_dir)) {
1816 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001817 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001818 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001819
1820 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001821 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001822 class_name_dir);
1823 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001824 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001825 }
Eric Parisb77a4932010-11-23 11:40:08 -05001826 rc = 0;
1827out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001828 for (i = 0; i < nclasses; i++)
1829 kfree(classes[i]);
1830 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001831 return rc;
1832}
1833
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001834static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001835{
1836 unsigned int iter;
1837 struct dentry *dentry = NULL;
1838 struct inode *inode = NULL;
1839
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001840 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001841
1842 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001843 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001844 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001845 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001846 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001847 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001848
1849 if (dentry == NULL)
1850 return -ENOMEM;
1851
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001852 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001853 if (inode == NULL) {
1854 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001855 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001856 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001857
1858 inode->i_fop = &sel_policycap_ops;
1859 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1860 d_add(dentry, inode);
1861 }
1862
1863 return 0;
1864}
1865
Al Viroa1c2aa12012-03-18 20:36:59 -04001866static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001867 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Al Viroa1c2aa12012-03-18 20:36:59 -04001869 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct inode *inode;
1871
Al Viroa1c2aa12012-03-18 20:36:59 -04001872 if (!dentry)
1873 return ERR_PTR(-ENOMEM);
1874
1875 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1876 if (!inode) {
1877 dput(dentry);
1878 return ERR_PTR(-ENOMEM);
1879 }
Eric Parisb77a4932010-11-23 11:40:08 -05001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 inode->i_op = &simple_dir_inode_operations;
1882 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001883 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001884 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001885 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001887 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001888 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001889
Al Viroa1c2aa12012-03-18 20:36:59 -04001890 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001893#define NULL_FILE_NAME "null"
1894
David Howells920f50b2019-03-25 16:38:30 +00001895static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001897 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int ret;
1899 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001900 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct inode_security_struct *isec;
1902
Eric Biggerscda37122017-03-25 21:15:37 -07001903 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1905 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001906 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1909 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1910 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1911 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1912 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1913 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1914 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1915 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1916 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001917 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1918 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001919 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001920 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001921 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1922 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /* last one */ {""}
1924 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001925
1926 ret = selinux_fs_info_create(sb);
1927 if (ret)
1928 goto err;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1931 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001932 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001934 fsi = sb->s_fs_info;
1935 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1936 if (IS_ERR(fsi->bool_dir)) {
1937 ret = PTR_ERR(fsi->bool_dir);
1938 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001939 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Eric Parisb77a4932010-11-23 11:40:08 -05001942 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001944 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001945 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Eric Parisb77a4932010-11-23 11:40:08 -05001947 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001949 if (!inode) {
1950 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08001951 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08001952 }
Eric Parisb77a4932010-11-23 11:40:08 -05001953
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001954 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07001955 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 isec->sid = SECINITSID_DEVNULL;
1957 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001958 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1961 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001963 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001964 if (IS_ERR(dentry)) {
1965 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001966 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 ret = sel_make_avc_files(dentry);
1970 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001971 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001972
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001973 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001974 if (IS_ERR(dentry)) {
1975 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001976 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001977 }
James Carterf0ee2e42007-04-04 10:11:29 -04001978
1979 ret = sel_make_initcon_files(dentry);
1980 if (ret)
1981 goto err;
1982
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001983 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1984 if (IS_ERR(fsi->class_dir)) {
1985 ret = PTR_ERR(fsi->class_dir);
1986 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001987 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001988 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001989
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001990 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1991 &fsi->last_ino);
1992 if (IS_ERR(fsi->policycap_dir)) {
1993 ret = PTR_ERR(fsi->policycap_dir);
1994 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001995 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001996 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001997
1998 ret = sel_make_policy_nodes(fsi);
1999 if (ret)
2000 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05002001 return 0;
James Morris161ce452006-03-22 00:09:17 -08002002err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002003 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002004 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002005
2006 selinux_fs_info_free(sb);
2007
Eric Parisb77a4932010-11-23 11:40:08 -05002008 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
David Howells920f50b2019-03-25 16:38:30 +00002011static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
David Howells920f50b2019-03-25 16:38:30 +00002013 return get_tree_single(fc, sel_fill_super);
2014}
2015
2016static const struct fs_context_operations sel_context_ops = {
2017 .get_tree = sel_get_tree,
2018};
2019
2020static int sel_init_fs_context(struct fs_context *fc)
2021{
2022 fc->ops = &sel_context_ops;
2023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002026static void sel_kill_sb(struct super_block *sb)
2027{
2028 selinux_fs_info_free(sb);
2029 kill_litter_super(sb);
2030}
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032static struct file_system_type sel_fs_type = {
2033 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002034 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002035 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036};
2037
2038struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002039struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041static int __init init_sel_fs(void)
2042{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002043 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2044 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int err;
2046
2047 if (!selinux_enabled)
2048 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002049
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002050 err = sysfs_create_mount_point(fs_kobj, "selinux");
2051 if (err)
2052 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002055 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002056 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002057 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002058 }
Eric Parisb77a4932010-11-23 11:40:08 -05002059
Al Viro765927b2012-06-26 21:58:53 +04002060 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002061 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002062 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002063 err = PTR_ERR(selinuxfs_mount);
2064 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002066 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2067 &null_name);
2068 if (IS_ERR(selinux_null.dentry)) {
2069 pr_err("selinuxfs: could not lookup null!\n");
2070 err = PTR_ERR(selinux_null.dentry);
2071 selinux_null.dentry = NULL;
2072 }
Eric Parisb77a4932010-11-23 11:40:08 -05002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return err;
2075}
2076
2077__initcall(init_sel_fs);
2078
2079#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2080void exit_sel_fs(void)
2081{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002082 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002083 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002084 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 unregister_filesystem(&sel_fs_type);
2086}
2087#endif