blob: 0b3155d827a19a7bceb560435ab4a6686b795d72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
Eric Paris18729812008-04-17 14:15:45 -04003 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Paul Moore82c21bf2011-08-01 11:10:33 +00005 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05006 *
Eric Paris18729812008-04-17 14:15:45 -04007 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05008 *
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
Eric Paris18729812008-04-17 14:15:45 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * the Free Software Foundation, version 2.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
David Howells920f50b2019-03-25 16:38:30 +000022#include <linux/fs_context.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040023#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/security.h>
28#include <linux/major.h>
29#include <linux/seq_file.h>
30#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000031#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040032#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070033#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040034#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* selinuxfs pseudo filesystem for exporting the security policy API.
37 Based on the proc code and the fs/nfsd/nfsctl.c code. */
38
39#include "flask.h"
40#include "avc.h"
41#include "avc_ss.h"
42#include "security.h"
43#include "objsec.h"
44#include "conditional.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046enum sel_inos {
47 SEL_ROOT_INO = 2,
48 SEL_LOAD, /* load policy */
49 SEL_ENFORCE, /* get or set enforcing status */
50 SEL_CONTEXT, /* validate context */
51 SEL_ACCESS, /* compute access decision */
52 SEL_CREATE, /* compute create labeling decision */
53 SEL_RELABEL, /* compute relabeling decision */
54 SEL_USER, /* compute reachable user contexts */
55 SEL_POLICYVERS, /* return policy version for this kernel */
56 SEL_COMMIT_BOOLS, /* commit new boolean values */
57 SEL_MLS, /* return if MLS policy is enabled */
58 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 SEL_MEMBER, /* compute polyinstantiation membership decision */
60 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070061 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040062 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
63 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090064 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040065 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050066 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040067 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Stephen Smalley0619f0f2018-03-20 11:59:11 -040070struct selinux_fs_info {
71 struct dentry *bool_dir;
72 unsigned int bool_num;
73 char **bool_pending_names;
74 unsigned int *bool_pending_values;
75 struct dentry *class_dir;
76 unsigned long last_class_ino;
77 bool policy_opened;
78 struct dentry *policycap_dir;
79 struct mutex mutex;
80 unsigned long last_ino;
81 struct selinux_state *state;
82 struct super_block *sb;
83};
84
85static int selinux_fs_info_create(struct super_block *sb)
86{
87 struct selinux_fs_info *fsi;
88
89 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
90 if (!fsi)
91 return -ENOMEM;
92
93 mutex_init(&fsi->mutex);
94 fsi->last_ino = SEL_INO_NEXT - 1;
95 fsi->state = &selinux_state;
96 fsi->sb = sb;
97 sb->s_fs_info = fsi;
98 return 0;
99}
100
101static void selinux_fs_info_free(struct super_block *sb)
102{
103 struct selinux_fs_info *fsi = sb->s_fs_info;
104 int i;
105
106 if (fsi) {
107 for (i = 0; i < fsi->bool_num; i++)
108 kfree(fsi->bool_pending_names[i]);
109 kfree(fsi->bool_pending_names);
110 kfree(fsi->bool_pending_values);
111 }
112 kfree(sb->s_fs_info);
113 sb->s_fs_info = NULL;
114}
James Carter6174eaf2007-04-04 16:18:39 -0400115
Paul Moore3bb56b22008-01-29 08:38:19 -0500116#define SEL_INITCON_INO_OFFSET 0x01000000
117#define SEL_BOOL_INO_OFFSET 0x02000000
118#define SEL_CLASS_INO_OFFSET 0x04000000
119#define SEL_POLICYCAP_INO_OFFSET 0x08000000
120#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define TMPBUFLEN 12
123static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
124 size_t count, loff_t *ppos)
125{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400126 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 char tmpbuf[TMPBUFLEN];
128 ssize_t length;
129
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500130 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400131 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
133}
134
135#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400136static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 size_t count, loff_t *ppos)
138
139{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400140 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
141 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500142 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500144 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Davi Arnautbfd51622005-10-30 14:59:24 -0800146 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500147 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500148
149 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500150 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500151 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500152
Al Viro8365a712015-12-24 00:08:06 -0500153 page = memdup_user_nul(buf, count);
154 if (IS_ERR(page))
155 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 length = -EINVAL;
158 if (sscanf(page, "%d", &new_value) != 1)
159 goto out;
160
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500161 new_value = !!new_value;
162
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400163 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500164 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500165 length = avc_has_perm(&selinux_state,
166 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500167 SECCLASS_SECURITY, SECURITY__SETENFORCE,
168 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (length)
170 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400171 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400172 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
173 " enabled=%d old-enabled=%d lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500174 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700175 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400176 audit_get_sessionid(current),
177 selinux_enabled, selinux_enabled);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400178 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500179 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500180 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500181 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400182 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500183 if (!new_value)
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300184 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 length = count;
187out:
Al Viro8365a712015-12-24 00:08:06 -0500188 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return length;
190}
191#else
192#define sel_write_enforce NULL
193#endif
194
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800195static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .read = sel_read_enforce,
197 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200198 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Eric Paris3f120702007-09-21 14:37:10 -0400201static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
202 size_t count, loff_t *ppos)
203{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400204 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
205 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400206 char tmpbuf[TMPBUFLEN];
207 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500208 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400209 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400210 security_get_reject_unknown(state) :
211 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400212
213 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
214 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
215}
216
217static const struct file_operations sel_handle_unknown_ops = {
218 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200219 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400220};
221
KaiGai Kohei11904162010-09-14 18:28:39 +0900222static int sel_open_handle_status(struct inode *inode, struct file *filp)
223{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400224 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
225 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900226
227 if (!status)
228 return -ENOMEM;
229
230 filp->private_data = status;
231
232 return 0;
233}
234
235static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
236 size_t count, loff_t *ppos)
237{
238 struct page *status = filp->private_data;
239
240 BUG_ON(!status);
241
242 return simple_read_from_buffer(buf, count, ppos,
243 page_address(status),
244 sizeof(struct selinux_kernel_status));
245}
246
247static int sel_mmap_handle_status(struct file *filp,
248 struct vm_area_struct *vma)
249{
250 struct page *status = filp->private_data;
251 unsigned long size = vma->vm_end - vma->vm_start;
252
253 BUG_ON(!status);
254
255 /* only allows one page from the head */
256 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
257 return -EIO;
258 /* disallow writable mapping */
259 if (vma->vm_flags & VM_WRITE)
260 return -EPERM;
261 /* disallow mprotect() turns it into writable */
262 vma->vm_flags &= ~VM_MAYWRITE;
263
264 return remap_pfn_range(vma, vma->vm_start,
265 page_to_pfn(status),
266 size, vma->vm_page_prot);
267}
268
269static const struct file_operations sel_handle_status_ops = {
270 .open = sel_open_handle_status,
271 .read = sel_read_handle_status,
272 .mmap = sel_mmap_handle_status,
273 .llseek = generic_file_llseek,
274};
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400277static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 size_t count, loff_t *ppos)
279
280{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400281 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500282 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ssize_t length;
284 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400285 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Davi Arnautbfd51622005-10-30 14:59:24 -0800287 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500288 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500289
290 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500291 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500292 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500293
Al Viro8365a712015-12-24 00:08:06 -0500294 page = memdup_user_nul(buf, count);
295 if (IS_ERR(page))
296 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 length = -EINVAL;
299 if (sscanf(page, "%d", &new_value) != 1)
300 goto out;
301
302 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400303 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400304 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500305 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400307 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400308 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
309 " enabled=%d old-enabled=%d lsm=selinux res=1",
310 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700311 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400312 audit_get_sessionid(current), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 length = count;
316out:
Al Viro8365a712015-12-24 00:08:06 -0500317 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return length;
319}
320#else
321#define sel_write_disable NULL
322#endif
323
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800324static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200326 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327};
328
329static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400330 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 char tmpbuf[TMPBUFLEN];
333 ssize_t length;
334
335 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
336 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
337}
338
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800339static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200341 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400345static int sel_make_bools(struct selinux_fs_info *fsi);
346static int sel_make_classes(struct selinux_fs_info *fsi);
347static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400348
349/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400350static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400351 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353static ssize_t sel_read_mls(struct file *filp, char __user *buf,
354 size_t count, loff_t *ppos)
355{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400356 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 char tmpbuf[TMPBUFLEN];
358 ssize_t length;
359
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100360 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400361 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
363}
364
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800365static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200367 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Eric Pariscee74f42010-10-13 17:50:25 -0400370struct policy_load_memory {
371 size_t len;
372 void *data;
373};
374
375static int sel_open_policy(struct inode *inode, struct file *filp)
376{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400377 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
378 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400379 struct policy_load_memory *plm = NULL;
380 int rc;
381
382 BUG_ON(filp->private_data);
383
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400384 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400385
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500386 rc = avc_has_perm(&selinux_state,
387 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500388 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400389 if (rc)
390 goto err;
391
392 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400393 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400394 goto err;
395
396 rc = -ENOMEM;
397 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
398 if (!plm)
399 goto err;
400
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400401 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500402 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400403 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500404 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400405 }
406
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400407 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400408 if (rc)
409 goto err;
410
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400411 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400412
413 filp->private_data = plm;
414
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400415 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400416
417 return 0;
418err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400419 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400420
421 if (plm)
422 vfree(plm->data);
423 kfree(plm);
424 return rc;
425}
426
427static int sel_release_policy(struct inode *inode, struct file *filp)
428{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400429 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400430 struct policy_load_memory *plm = filp->private_data;
431
432 BUG_ON(!plm);
433
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400434 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400435
436 vfree(plm->data);
437 kfree(plm);
438
439 return 0;
440}
441
442static ssize_t sel_read_policy(struct file *filp, char __user *buf,
443 size_t count, loff_t *ppos)
444{
445 struct policy_load_memory *plm = filp->private_data;
446 int ret;
447
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500448 ret = avc_has_perm(&selinux_state,
449 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500450 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400451 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400452 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400453
Jann Horn0da74122018-06-28 20:39:54 -0400454 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400455}
456
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530457static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400458{
Dave Jiang11bac802017-02-24 14:56:41 -0800459 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400460 unsigned long offset;
461 struct page *page;
462
463 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
464 return VM_FAULT_SIGBUS;
465
466 offset = vmf->pgoff << PAGE_SHIFT;
467 if (offset >= roundup(plm->len, PAGE_SIZE))
468 return VM_FAULT_SIGBUS;
469
470 page = vmalloc_to_page(plm->data + offset);
471 get_page(page);
472
473 vmf->page = page;
474
475 return 0;
476}
477
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700478static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400479 .fault = sel_mmap_policy_fault,
480 .page_mkwrite = sel_mmap_policy_fault,
481};
482
James Morrisad3fa082011-08-30 10:50:12 +1000483static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400484{
485 if (vma->vm_flags & VM_SHARED) {
486 /* do not allow mprotect to make mapping writable */
487 vma->vm_flags &= ~VM_MAYWRITE;
488
489 if (vma->vm_flags & VM_WRITE)
490 return -EACCES;
491 }
492
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700493 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400494 vma->vm_ops = &sel_mmap_policy_ops;
495
496 return 0;
497}
498
Eric Pariscee74f42010-10-13 17:50:25 -0400499static const struct file_operations sel_policy_ops = {
500 .open = sel_open_policy,
501 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400502 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400503 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500504 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400505};
506
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400507static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
508{
509 int ret;
510
511 ret = sel_make_bools(fsi);
512 if (ret) {
513 pr_err("SELinux: failed to load policy booleans\n");
514 return ret;
515 }
516
517 ret = sel_make_classes(fsi);
518 if (ret) {
519 pr_err("SELinux: failed to load policy classes\n");
520 return ret;
521 }
522
523 ret = sel_make_policycap(fsi);
524 if (ret) {
525 pr_err("SELinux: failed to load policy capabilities\n");
526 return ret;
527 }
528
529 return 0;
530}
531
Eric Paris18729812008-04-17 14:15:45 -0400532static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 size_t count, loff_t *ppos)
534
535{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400536 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ssize_t length;
538 void *data = NULL;
539
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400540 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500542 length = avc_has_perm(&selinux_state,
543 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500544 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (length)
546 goto out;
547
Eric Parisb77a4932010-11-23 11:40:08 -0500548 /* No partial writes. */
549 length = -EINVAL;
550 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Eric Parisb77a4932010-11-23 11:40:08 -0500553 length = -EFBIG;
554 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500556
557 length = -ENOMEM;
558 data = vmalloc(count);
559 if (!data)
560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 length = -EFAULT;
563 if (copy_from_user(data, buf, count) != 0)
564 goto out;
565
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400566 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500567 if (length) {
568 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400572 length = sel_make_policy_nodes(fsi);
573 if (length)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400574 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500575
576 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400577
578out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400579 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400580 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700581 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500582 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400584 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 vfree(data);
586 return length;
587}
588
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800589static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200591 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592};
593
Eric Paris18729812008-04-17 14:15:45 -0400594static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400596 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
597 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500598 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800599 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 ssize_t length;
601
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500602 length = avc_has_perm(&selinux_state,
603 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500604 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400608 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500609 if (length)
610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400612 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500613 if (length)
614 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800615
Eric Parisb77a4932010-11-23 11:40:08 -0500616 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800617 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200618 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400619 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800620 goto out;
621 }
622
623 memcpy(buf, canon, len);
624 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800626 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return length;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
631 size_t count, loff_t *ppos)
632{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400633 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 char tmpbuf[TMPBUFLEN];
635 ssize_t length;
636
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400637 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
639}
640
Eric Paris18729812008-04-17 14:15:45 -0400641static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 size_t count, loff_t *ppos)
643{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400644 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500645 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ssize_t length;
647 unsigned int new_value;
648
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500649 length = avc_has_perm(&selinux_state,
650 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500651 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
652 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500654 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Davi Arnautbfd51622005-10-30 14:59:24 -0800656 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500657 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500658
659 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500660 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500661 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500662
Al Viro8365a712015-12-24 00:08:06 -0500663 page = memdup_user_nul(buf, count);
664 if (IS_ERR(page))
665 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 length = -EINVAL;
668 if (sscanf(page, "%u", &new_value) != 1)
669 goto out;
670
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400671 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 length = count;
673out:
Al Viro8365a712015-12-24 00:08:06 -0500674 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return length;
676}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800677static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .read = sel_read_checkreqprot,
679 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200680 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500683static ssize_t sel_write_validatetrans(struct file *file,
684 const char __user *buf,
685 size_t count, loff_t *ppos)
686{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400687 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
688 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500689 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
690 char *req = NULL;
691 u32 osid, nsid, tsid;
692 u16 tclass;
693 int rc;
694
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500695 rc = avc_has_perm(&selinux_state,
696 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500697 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500698 if (rc)
699 goto out;
700
701 rc = -ENOMEM;
702 if (count >= PAGE_SIZE)
703 goto out;
704
705 /* No partial writes. */
706 rc = -EINVAL;
707 if (*ppos != 0)
708 goto out;
709
Al Viro0b884d22017-05-13 18:12:07 -0400710 req = memdup_user_nul(buf, count);
711 if (IS_ERR(req)) {
712 rc = PTR_ERR(req);
713 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500714 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400715 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500716
717 rc = -ENOMEM;
718 oldcon = kzalloc(count + 1, GFP_KERNEL);
719 if (!oldcon)
720 goto out;
721
722 newcon = kzalloc(count + 1, GFP_KERNEL);
723 if (!newcon)
724 goto out;
725
726 taskcon = kzalloc(count + 1, GFP_KERNEL);
727 if (!taskcon)
728 goto out;
729
730 rc = -EINVAL;
731 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
732 goto out;
733
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400734 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500735 if (rc)
736 goto out;
737
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400738 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500739 if (rc)
740 goto out;
741
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400742 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500743 if (rc)
744 goto out;
745
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400746 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500747 if (!rc)
748 rc = count;
749out:
750 kfree(req);
751 kfree(oldcon);
752 kfree(newcon);
753 kfree(taskcon);
754 return rc;
755}
756
757static const struct file_operations sel_transition_ops = {
758 .write = sel_write_validatetrans,
759 .llseek = generic_file_llseek,
760};
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
764 */
Eric Paris18729812008-04-17 14:15:45 -0400765static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
766static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
767static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
768static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
769static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Eric Biggers631d2b42018-07-17 10:43:56 -0700771static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 [SEL_ACCESS] = sel_write_access,
773 [SEL_CREATE] = sel_write_create,
774 [SEL_RELABEL] = sel_write_relabel,
775 [SEL_USER] = sel_write_user,
776 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800777 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
781{
Al Viro496ad9a2013-01-23 17:07:38 -0500782 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 char *data;
784 ssize_t rv;
785
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800786 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EINVAL;
788
789 data = simple_transaction_get(file, buf, size);
790 if (IS_ERR(data))
791 return PTR_ERR(data);
792
Eric Paris18729812008-04-17 14:15:45 -0400793 rv = write_op[ino](file, data, size);
794 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 simple_transaction_set(file, rv);
796 rv = size;
797 }
798 return rv;
799}
800
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800801static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .write = selinux_transaction_write,
803 .read = simple_transaction_read,
804 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200805 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
808/*
809 * payload - write methods
810 * If the method has a response, the response should be put in buf,
811 * and the length returned. Otherwise return 0 or and -error.
812 */
813
Eric Paris18729812008-04-17 14:15:45 -0400814static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400816 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
817 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500818 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 u32 ssid, tsid;
820 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct av_decision avd;
822 ssize_t length;
823
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500824 length = avc_has_perm(&selinux_state,
825 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500826 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800831 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric Parisb77a4932010-11-23 11:40:08 -0500835 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800836 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!tcon)
838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500841 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400844 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500845 if (length)
846 goto out;
847
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400848 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500849 if (length)
850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400852 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900855 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500856 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900858 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859out:
Eric Parisb77a4932010-11-23 11:40:08 -0500860 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 kfree(scon);
862 return length;
863}
864
Eric Paris18729812008-04-17 14:15:45 -0400865static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400867 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
868 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500869 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100870 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 u32 ssid, tsid, newsid;
872 u16 tclass;
873 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500874 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100876 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500878 length = avc_has_perm(&selinux_state,
879 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500880 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
881 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800886 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Eric Parisb77a4932010-11-23 11:40:08 -0500890 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800891 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!tcon)
893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100895 length = -ENOMEM;
896 namebuf = kzalloc(size + 1, GFP_KERNEL);
897 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100900 length = -EINVAL;
901 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
902 if (nargs < 3 || nargs > 4)
903 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400904 if (nargs == 4) {
905 /*
906 * If and when the name of new object to be queried contains
907 * either whitespace or multibyte characters, they shall be
908 * encoded based on the percentage-encoding rule.
909 * If not encoded, the sscanf logic picks up only left-half
910 * of the supplied name; splitted by a whitespace unexpectedly.
911 */
912 char *r, *w;
913 int c1, c2;
914
915 r = w = namebuf;
916 do {
917 c1 = *r++;
918 if (c1 == '+')
919 c1 = ' ';
920 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800921 c1 = hex_to_bin(*r++);
922 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400923 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800924 c2 = hex_to_bin(*r++);
925 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400926 goto out;
927 c1 = (c1 << 4) | c2;
928 }
929 *w++ = c1;
930 } while (c1 != '\0');
931
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100932 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400933 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100934
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400935 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500936 if (length)
937 goto out;
938
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400939 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500940 if (length)
941 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400943 length = security_transition_sid_user(state, ssid, tsid, tclass,
944 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500945 if (length)
946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400948 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500949 if (length)
950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Eric Parisb77a4932010-11-23 11:40:08 -0500952 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200954 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400955 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500956 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 memcpy(buf, newcon, len);
960 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961out:
Eric Parisb77a4932010-11-23 11:40:08 -0500962 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100963 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500964 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 kfree(scon);
966 return length;
967}
968
Eric Paris18729812008-04-17 14:15:45 -0400969static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400971 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
972 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500973 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 u32 ssid, tsid, newsid;
975 u16 tclass;
976 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500977 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 u32 len;
979
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500980 length = avc_has_perm(&selinux_state,
981 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500982 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
983 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800988 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Eric Parisb77a4932010-11-23 11:40:08 -0500992 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800993 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!tcon)
995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 length = -EINVAL;
998 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001001 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001002 if (length)
1003 goto out;
1004
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001005 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001006 if (length)
1007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001009 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001010 if (length)
1011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001013 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001014 if (length)
1015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Parisb77a4932010-11-23 11:40:08 -05001017 length = -ERANGE;
1018 if (len > SIMPLE_TRANSACTION_LIMIT)
1019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 memcpy(buf, newcon, len);
1022 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023out:
Eric Parisb77a4932010-11-23 11:40:08 -05001024 kfree(newcon);
1025 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 kfree(scon);
1027 return length;
1028}
1029
Eric Paris18729812008-04-17 14:15:45 -04001030static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001032 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1033 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001034 char *con = NULL, *user = NULL, *ptr;
1035 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ssize_t length;
1037 char *newcon;
1038 int i, rc;
1039 u32 len, nsids;
1040
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001041 length = avc_has_perm(&selinux_state,
1042 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001043 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1044 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001049 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Eric Parisb77a4932010-11-23 11:40:08 -05001053 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001054 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!user)
1056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 length = -EINVAL;
1059 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001062 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001063 if (length)
1064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001066 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001067 if (length)
1068 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 length = sprintf(buf, "%u", nsids) + 1;
1071 ptr = buf + length;
1072 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001073 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (rc) {
1075 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1079 kfree(newcon);
1080 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 memcpy(ptr, newcon, len);
1084 kfree(newcon);
1085 ptr += len;
1086 length += len;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088out:
Eric Parisb77a4932010-11-23 11:40:08 -05001089 kfree(sids);
1090 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kfree(con);
1092 return length;
1093}
1094
Eric Paris18729812008-04-17 14:15:45 -04001095static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001097 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1098 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001099 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 u32 ssid, tsid, newsid;
1101 u16 tclass;
1102 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001103 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 u32 len;
1105
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001106 length = avc_has_perm(&selinux_state,
1107 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001108 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1109 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001114 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Eric Parisb77a4932010-11-23 11:40:08 -05001118 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001119 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!tcon)
1121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 length = -EINVAL;
1124 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001127 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001128 if (length)
1129 goto out;
1130
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001131 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001132 if (length)
1133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001135 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001136 if (length)
1137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001139 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001140 if (length)
1141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Eric Parisb77a4932010-11-23 11:40:08 -05001143 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001145 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001146 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 memcpy(buf, newcon, len);
1151 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152out:
Eric Parisb77a4932010-11-23 11:40:08 -05001153 kfree(newcon);
1154 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 kfree(scon);
1156 return length;
1157}
1158
1159static struct inode *sel_make_inode(struct super_block *sb, int mode)
1160{
1161 struct inode *ret = new_inode(sb);
1162
1163 if (ret) {
1164 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001165 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 return ret;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1171 size_t count, loff_t *ppos)
1172{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001173 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 char *page = NULL;
1175 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ssize_t ret;
1177 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001178 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001179 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001181 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Eric Parisb77a4932010-11-23 11:40:08 -05001183 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001184 if (index >= fsi->bool_num || strcmp(name,
1185 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001186 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Eric Parisb77a4932010-11-23 11:40:08 -05001188 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001189 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001190 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001191 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001193 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (cur_enforcing < 0) {
1195 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001196 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001199 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001200 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001201 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1202out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001203 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001205
1206out_unlock:
1207 mutex_unlock(&fsi->mutex);
1208 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1212 size_t count, loff_t *ppos)
1213{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001214 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001216 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001218 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001219 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jann Horn0da74122018-06-28 20:39:54 -04001221 if (count >= PAGE_SIZE)
1222 return -ENOMEM;
1223
1224 /* No partial writes. */
1225 if (*ppos != 0)
1226 return -EINVAL;
1227
1228 page = memdup_user_nul(buf, count);
1229 if (IS_ERR(page))
1230 return PTR_ERR(page);
1231
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001232 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001234 length = avc_has_perm(&selinux_state,
1235 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001236 SECCLASS_SECURITY, SECURITY__SETBOOL,
1237 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (length)
1239 goto out;
1240
Eric Parisb77a4932010-11-23 11:40:08 -05001241 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001242 if (index >= fsi->bool_num || strcmp(name,
1243 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001244 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 length = -EINVAL;
1247 if (sscanf(page, "%d", &new_value) != 1)
1248 goto out;
1249
1250 if (new_value)
1251 new_value = 1;
1252
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001253 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 length = count;
1255
1256out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001257 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001258 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return length;
1260}
1261
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001262static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001263 .read = sel_read_bool,
1264 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001265 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266};
1267
1268static ssize_t sel_commit_bools_write(struct file *filep,
1269 const char __user *buf,
1270 size_t count, loff_t *ppos)
1271{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001272 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001274 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int new_value;
1276
Jann Horn0da74122018-06-28 20:39:54 -04001277 if (count >= PAGE_SIZE)
1278 return -ENOMEM;
1279
1280 /* No partial writes. */
1281 if (*ppos != 0)
1282 return -EINVAL;
1283
1284 page = memdup_user_nul(buf, count);
1285 if (IS_ERR(page))
1286 return PTR_ERR(page);
1287
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001288 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001290 length = avc_has_perm(&selinux_state,
1291 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001292 SECCLASS_SECURITY, SECURITY__SETBOOL,
1293 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (length)
1295 goto out;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 length = -EINVAL;
1298 if (sscanf(page, "%d", &new_value) != 1)
1299 goto out;
1300
Eric Parisb77a4932010-11-23 11:40:08 -05001301 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001302 if (new_value && fsi->bool_pending_values)
1303 length = security_set_bools(fsi->state, fsi->bool_num,
1304 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Eric Parisb77a4932010-11-23 11:40:08 -05001306 if (!length)
1307 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001310 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001311 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return length;
1313}
1314
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001315static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001316 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001317 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318};
1319
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001320static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Al Viroad521842014-12-24 14:56:48 -05001322 d_genocide(de);
1323 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326#define BOOL_DIR_NAME "booleans"
1327
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001328static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Eric Parisb77a4932010-11-23 11:40:08 -05001330 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ssize_t len;
1332 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001333 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct inode *inode = NULL;
1335 struct inode_security_struct *isec;
1336 char **names = NULL, *page;
1337 int num;
1338 int *values = NULL;
1339 u32 sid;
1340
1341 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001342 for (i = 0; i < fsi->bool_num; i++)
1343 kfree(fsi->bool_pending_names[i]);
1344 kfree(fsi->bool_pending_names);
1345 kfree(fsi->bool_pending_values);
1346 fsi->bool_num = 0;
1347 fsi->bool_pending_names = NULL;
1348 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001350 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Eric Parisb77a4932010-11-23 11:40:08 -05001352 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001353 page = (char *)get_zeroed_page(GFP_KERNEL);
1354 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001357 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001358 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto out;
1360
1361 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001362 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001364 if (!dentry)
1365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Eric Parisb77a4932010-11-23 11:40:08 -05001367 ret = -ENOMEM;
1368 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001369 if (!inode) {
1370 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001371 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001372 }
Eric Parisb77a4932010-11-23 11:40:08 -05001373
Eric Parisb77a4932010-11-23 11:40:08 -05001374 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001375 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001376 if (len >= PAGE_SIZE) {
1377 dput(dentry);
1378 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001379 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001380 }
Eric Parisb77a4932010-11-23 11:40:08 -05001381
Casey Schaufler80788c22018-09-21 17:19:11 -07001382 isec = selinux_inode(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001383 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001384 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001385 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001386 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1387 page);
1388 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001392 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001394 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 d_add(dentry, inode);
1396 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001397 fsi->bool_num = num;
1398 fsi->bool_pending_names = names;
1399 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001400
1401 free_page((unsigned long)page);
1402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403out:
1404 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001407 for (i = 0; i < num; i++)
1408 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 kfree(names);
1410 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001411 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001412 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001413
1414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1418 size_t count, loff_t *ppos)
1419{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001420 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1421 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 char tmpbuf[TMPBUFLEN];
1423 ssize_t length;
1424
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001425 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1426 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1428}
1429
Eric Paris18729812008-04-17 14:15:45 -04001430static ssize_t sel_write_avc_cache_threshold(struct file *file,
1431 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 size_t count, loff_t *ppos)
1433
1434{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001435 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1436 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001437 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001439 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001441 ret = avc_has_perm(&selinux_state,
1442 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001443 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1444 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001445 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Eric Parisb77a4932010-11-23 11:40:08 -05001448 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001449 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Eric Parisb77a4932010-11-23 11:40:08 -05001451 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001452 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001453 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001454
Al Viro8365a712015-12-24 00:08:06 -05001455 page = memdup_user_nul(buf, count);
1456 if (IS_ERR(page))
1457 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Parisb77a4932010-11-23 11:40:08 -05001459 ret = -EINVAL;
1460 if (sscanf(page, "%u", &new_value) != 1)
1461 goto out;
1462
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001463 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
Al Viro8365a712015-12-24 00:08:06 -05001467 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
1471static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1472 size_t count, loff_t *ppos)
1473{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001474 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1475 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001477 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001480 if (!page)
1481 return -ENOMEM;
1482
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001483 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001484 if (length >= 0)
1485 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001487
1488 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001491static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .read = sel_read_avc_cache_threshold,
1493 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001494 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495};
1496
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001497static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001499 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500};
1501
1502#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1503static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1504{
1505 int cpu;
1506
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301507 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (!cpu_possible(cpu))
1509 continue;
1510 *idx = cpu + 1;
1511 return &per_cpu(avc_cache_stats, cpu);
1512 }
1513 return NULL;
1514}
1515
1516static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1517{
1518 loff_t n = *pos - 1;
1519
1520 if (*pos == 0)
1521 return SEQ_START_TOKEN;
1522
1523 return sel_avc_get_stat_idx(&n);
1524}
1525
1526static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1527{
1528 return sel_avc_get_stat_idx(pos);
1529}
1530
1531static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1532{
1533 struct avc_cache_stats *st = v;
1534
Markus Elfring710a0642017-01-15 14:04:53 +01001535 if (v == SEQ_START_TOKEN) {
1536 seq_puts(seq,
1537 "lookups hits misses allocations reclaims frees\n");
1538 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001539 unsigned int lookups = st->lookups;
1540 unsigned int misses = st->misses;
1541 unsigned int hits = lookups - misses;
1542 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1543 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
1548
1549static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1550{ }
1551
Jan Engelhardt1996a102008-01-23 00:02:58 +01001552static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 .start = sel_avc_stats_seq_start,
1554 .next = sel_avc_stats_seq_next,
1555 .show = sel_avc_stats_seq_show,
1556 .stop = sel_avc_stats_seq_stop,
1557};
1558
1559static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1560{
1561 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1562}
1563
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001564static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .open = sel_open_avc_cache_stats,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = seq_release,
1569};
1570#endif
1571
1572static int sel_make_avc_files(struct dentry *dir)
1573{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001574 struct super_block *sb = dir->d_sb;
1575 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001576 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001577 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 { "cache_threshold",
1579 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1580 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1581#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1582 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1583#endif
1584 };
1585
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001586 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct inode *inode;
1588 struct dentry *dentry;
1589
1590 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001591 if (!dentry)
1592 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001595 if (!inode) {
1596 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001597 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001598 }
Eric Parisb77a4932010-11-23 11:40:08 -05001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001601 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 d_add(dentry, inode);
1603 }
Eric Parisb77a4932010-11-23 11:40:08 -05001604
1605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Eric Paris18729812008-04-17 14:15:45 -04001608static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001609 size_t count, loff_t *ppos)
1610{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001611 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001612 char *con;
1613 u32 sid, len;
1614 ssize_t ret;
1615
Al Viro496ad9a2013-01-23 17:07:38 -05001616 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001617 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001618 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001619 return ret;
1620
1621 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1622 kfree(con);
1623 return ret;
1624}
1625
1626static const struct file_operations sel_initcon_ops = {
1627 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001628 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001629};
1630
1631static int sel_make_initcon_files(struct dentry *dir)
1632{
Eric Parisb77a4932010-11-23 11:40:08 -05001633 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001634
1635 for (i = 1; i <= SECINITSID_NUM; i++) {
1636 struct inode *inode;
1637 struct dentry *dentry;
1638 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001639 if (!dentry)
1640 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001641
1642 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001643 if (!inode) {
1644 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001645 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001646 }
Eric Parisb77a4932010-11-23 11:40:08 -05001647
James Carterf0ee2e42007-04-04 10:11:29 -04001648 inode->i_fop = &sel_initcon_ops;
1649 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1650 d_add(dentry, inode);
1651 }
Eric Parisb77a4932010-11-23 11:40:08 -05001652
1653 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001654}
1655
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001656static inline unsigned long sel_class_to_ino(u16 class)
1657{
1658 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1659}
1660
1661static inline u16 sel_ino_to_class(unsigned long ino)
1662{
Eric Paris92ae9e82012-04-04 13:46:46 -04001663 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001664}
1665
1666static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1667{
1668 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1669}
1670
1671static inline u32 sel_ino_to_perm(unsigned long ino)
1672{
1673 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1674}
1675
Eric Paris18729812008-04-17 14:15:45 -04001676static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001677 size_t count, loff_t *ppos)
1678{
Al Viro496ad9a2013-01-23 17:07:38 -05001679 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001680 char res[TMPBUFLEN];
1681 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1682 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001683}
1684
1685static const struct file_operations sel_class_ops = {
1686 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001687 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001688};
1689
Eric Paris18729812008-04-17 14:15:45 -04001690static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001691 size_t count, loff_t *ppos)
1692{
Al Viro496ad9a2013-01-23 17:07:38 -05001693 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001694 char res[TMPBUFLEN];
1695 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1696 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001697}
1698
1699static const struct file_operations sel_perm_ops = {
1700 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001701 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001702};
1703
Paul Moore3bb56b22008-01-29 08:38:19 -05001704static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1705 size_t count, loff_t *ppos)
1706{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001707 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001708 int value;
1709 char tmpbuf[TMPBUFLEN];
1710 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001711 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001712
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001713 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001714 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1715
1716 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1717}
1718
1719static const struct file_operations sel_policycap_ops = {
1720 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001721 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001722};
1723
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001724static int sel_make_perm_files(char *objclass, int classvalue,
1725 struct dentry *dir)
1726{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001727 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001728 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001729 char **perms;
1730
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001731 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001732 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001733 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001734
1735 for (i = 0; i < nperms; i++) {
1736 struct inode *inode;
1737 struct dentry *dentry;
1738
Eric Parisb77a4932010-11-23 11:40:08 -05001739 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001740 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001741 if (!dentry)
1742 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001743
Eric Parisb77a4932010-11-23 11:40:08 -05001744 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001745 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001746 if (!inode) {
1747 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001748 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001749 }
Eric Parisb77a4932010-11-23 11:40:08 -05001750
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001751 inode->i_fop = &sel_perm_ops;
1752 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001753 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001754 d_add(dentry, inode);
1755 }
Eric Parisb77a4932010-11-23 11:40:08 -05001756 rc = 0;
1757out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001758 for (i = 0; i < nperms; i++)
1759 kfree(perms[i]);
1760 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001761 return rc;
1762}
1763
1764static int sel_make_class_dir_entries(char *classname, int index,
1765 struct dentry *dir)
1766{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001767 struct super_block *sb = dir->d_sb;
1768 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001769 struct dentry *dentry = NULL;
1770 struct inode *inode = NULL;
1771 int rc;
1772
1773 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001774 if (!dentry)
1775 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001776
1777 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001778 if (!inode) {
1779 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001780 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001781 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001782
1783 inode->i_fop = &sel_class_ops;
1784 inode->i_ino = sel_class_to_ino(index);
1785 d_add(dentry, inode);
1786
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001787 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001788 if (IS_ERR(dentry))
1789 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001790
1791 rc = sel_make_perm_files(classname, index, dentry);
1792
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001793 return rc;
1794}
1795
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001796static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001797{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001798
Eric Parisb77a4932010-11-23 11:40:08 -05001799 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001800 char **classes;
1801
1802 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001803 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001804
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001805 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001806 if (rc)
1807 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001808
1809 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001810 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001811
1812 for (i = 0; i < nclasses; i++) {
1813 struct dentry *class_name_dir;
1814
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001815 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1816 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001817 if (IS_ERR(class_name_dir)) {
1818 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001819 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001820 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001821
1822 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001823 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001824 class_name_dir);
1825 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001826 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001827 }
Eric Parisb77a4932010-11-23 11:40:08 -05001828 rc = 0;
1829out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001830 for (i = 0; i < nclasses; i++)
1831 kfree(classes[i]);
1832 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001833 return rc;
1834}
1835
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001836static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001837{
1838 unsigned int iter;
1839 struct dentry *dentry = NULL;
1840 struct inode *inode = NULL;
1841
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001842 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001843
1844 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001845 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001846 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001847 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001848 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001849 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001850
1851 if (dentry == NULL)
1852 return -ENOMEM;
1853
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001854 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001855 if (inode == NULL) {
1856 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001857 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001858 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001859
1860 inode->i_fop = &sel_policycap_ops;
1861 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1862 d_add(dentry, inode);
1863 }
1864
1865 return 0;
1866}
1867
Al Viroa1c2aa12012-03-18 20:36:59 -04001868static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001869 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Al Viroa1c2aa12012-03-18 20:36:59 -04001871 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct inode *inode;
1873
Al Viroa1c2aa12012-03-18 20:36:59 -04001874 if (!dentry)
1875 return ERR_PTR(-ENOMEM);
1876
1877 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1878 if (!inode) {
1879 dput(dentry);
1880 return ERR_PTR(-ENOMEM);
1881 }
Eric Parisb77a4932010-11-23 11:40:08 -05001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 inode->i_op = &simple_dir_inode_operations;
1884 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001885 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001886 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001887 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001889 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001890 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001891
Al Viroa1c2aa12012-03-18 20:36:59 -04001892 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001895#define NULL_FILE_NAME "null"
1896
David Howells920f50b2019-03-25 16:38:30 +00001897static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001899 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int ret;
1901 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001902 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct inode_security_struct *isec;
1904
Eric Biggerscda37122017-03-25 21:15:37 -07001905 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1907 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001908 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1910 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1911 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1912 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1913 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1914 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1915 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1916 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1917 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1918 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001919 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1920 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001921 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001922 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001923 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1924 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* last one */ {""}
1926 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001927
1928 ret = selinux_fs_info_create(sb);
1929 if (ret)
1930 goto err;
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1933 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001934 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001936 fsi = sb->s_fs_info;
1937 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1938 if (IS_ERR(fsi->bool_dir)) {
1939 ret = PTR_ERR(fsi->bool_dir);
1940 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001941 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Eric Parisb77a4932010-11-23 11:40:08 -05001944 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001946 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001947 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Eric Parisb77a4932010-11-23 11:40:08 -05001949 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001951 if (!inode) {
1952 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08001953 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08001954 }
Eric Parisb77a4932010-11-23 11:40:08 -05001955
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001956 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07001957 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 isec->sid = SECINITSID_DEVNULL;
1959 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001960 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1963 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001965 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001966 if (IS_ERR(dentry)) {
1967 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001968 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 ret = sel_make_avc_files(dentry);
1972 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001973 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001974
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001975 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001976 if (IS_ERR(dentry)) {
1977 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001978 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001979 }
James Carterf0ee2e42007-04-04 10:11:29 -04001980
1981 ret = sel_make_initcon_files(dentry);
1982 if (ret)
1983 goto err;
1984
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001985 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1986 if (IS_ERR(fsi->class_dir)) {
1987 ret = PTR_ERR(fsi->class_dir);
1988 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001989 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001990 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001991
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001992 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1993 &fsi->last_ino);
1994 if (IS_ERR(fsi->policycap_dir)) {
1995 ret = PTR_ERR(fsi->policycap_dir);
1996 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001997 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001998 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001999
2000 ret = sel_make_policy_nodes(fsi);
2001 if (ret)
2002 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05002003 return 0;
James Morris161ce452006-03-22 00:09:17 -08002004err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002005 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002006 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002007
2008 selinux_fs_info_free(sb);
2009
Eric Parisb77a4932010-11-23 11:40:08 -05002010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
David Howells920f50b2019-03-25 16:38:30 +00002013static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
David Howells920f50b2019-03-25 16:38:30 +00002015 return get_tree_single(fc, sel_fill_super);
2016}
2017
2018static const struct fs_context_operations sel_context_ops = {
2019 .get_tree = sel_get_tree,
2020};
2021
2022static int sel_init_fs_context(struct fs_context *fc)
2023{
2024 fc->ops = &sel_context_ops;
2025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002028static void sel_kill_sb(struct super_block *sb)
2029{
2030 selinux_fs_info_free(sb);
2031 kill_litter_super(sb);
2032}
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034static struct file_system_type sel_fs_type = {
2035 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002036 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002037 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038};
2039
2040struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002041struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043static int __init init_sel_fs(void)
2044{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002045 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2046 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int err;
2048
2049 if (!selinux_enabled)
2050 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002051
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002052 err = sysfs_create_mount_point(fs_kobj, "selinux");
2053 if (err)
2054 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002057 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002058 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002059 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002060 }
Eric Parisb77a4932010-11-23 11:40:08 -05002061
Al Viro765927b2012-06-26 21:58:53 +04002062 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002063 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002064 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002065 err = PTR_ERR(selinuxfs_mount);
2066 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002068 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2069 &null_name);
2070 if (IS_ERR(selinux_null.dentry)) {
2071 pr_err("selinuxfs: could not lookup null!\n");
2072 err = PTR_ERR(selinux_null.dentry);
2073 selinux_null.dentry = NULL;
2074 }
Eric Parisb77a4932010-11-23 11:40:08 -05002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return err;
2077}
2078
2079__initcall(init_sel_fs);
2080
2081#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2082void exit_sel_fs(void)
2083{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002084 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002085 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002086 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 unregister_filesystem(&sel_fs_type);
2088}
2089#endif