blob: cac585ce576b98a697d3b7e0d9ba87ae49e971a0 [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 *
Eric Paris18729812008-04-17 14:15:45 -04004 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Paul Moore82c21bf2011-08-01 11:10:33 +00006 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05007 *
Eric Paris18729812008-04-17 14:15:45 -04008 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05009 *
10 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/pagemap.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/fs.h>
David Howells920f50b2019-03-25 16:38:30 +000020#include <linux/fs_context.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040021#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/string.h>
25#include <linux/security.h>
26#include <linux/major.h>
27#include <linux/seq_file.h>
28#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000029#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040030#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070031#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040032#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36
37#include "flask.h"
38#include "avc.h"
39#include "avc_ss.h"
40#include "security.h"
41#include "objsec.h"
42#include "conditional.h"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044enum sel_inos {
45 SEL_ROOT_INO = 2,
46 SEL_LOAD, /* load policy */
47 SEL_ENFORCE, /* get or set enforcing status */
48 SEL_CONTEXT, /* validate context */
49 SEL_ACCESS, /* compute access decision */
50 SEL_CREATE, /* compute create labeling decision */
51 SEL_RELABEL, /* compute relabeling decision */
52 SEL_USER, /* compute reachable user contexts */
53 SEL_POLICYVERS, /* return policy version for this kernel */
54 SEL_COMMIT_BOOLS, /* commit new boolean values */
55 SEL_MLS, /* return if MLS policy is enabled */
56 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 SEL_MEMBER, /* compute polyinstantiation membership decision */
58 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070059 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040060 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
61 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090062 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040063 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050064 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040065 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Stephen Smalley0619f0f2018-03-20 11:59:11 -040068struct selinux_fs_info {
69 struct dentry *bool_dir;
70 unsigned int bool_num;
71 char **bool_pending_names;
72 unsigned int *bool_pending_values;
73 struct dentry *class_dir;
74 unsigned long last_class_ino;
75 bool policy_opened;
76 struct dentry *policycap_dir;
77 struct mutex mutex;
78 unsigned long last_ino;
79 struct selinux_state *state;
80 struct super_block *sb;
81};
82
83static int selinux_fs_info_create(struct super_block *sb)
84{
85 struct selinux_fs_info *fsi;
86
87 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
88 if (!fsi)
89 return -ENOMEM;
90
91 mutex_init(&fsi->mutex);
92 fsi->last_ino = SEL_INO_NEXT - 1;
93 fsi->state = &selinux_state;
94 fsi->sb = sb;
95 sb->s_fs_info = fsi;
96 return 0;
97}
98
99static void selinux_fs_info_free(struct super_block *sb)
100{
101 struct selinux_fs_info *fsi = sb->s_fs_info;
102 int i;
103
104 if (fsi) {
105 for (i = 0; i < fsi->bool_num; i++)
106 kfree(fsi->bool_pending_names[i]);
107 kfree(fsi->bool_pending_names);
108 kfree(fsi->bool_pending_values);
109 }
110 kfree(sb->s_fs_info);
111 sb->s_fs_info = NULL;
112}
James Carter6174eaf2007-04-04 16:18:39 -0400113
Paul Moore3bb56b22008-01-29 08:38:19 -0500114#define SEL_INITCON_INO_OFFSET 0x01000000
115#define SEL_BOOL_INO_OFFSET 0x02000000
116#define SEL_CLASS_INO_OFFSET 0x04000000
117#define SEL_POLICYCAP_INO_OFFSET 0x08000000
118#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define TMPBUFLEN 12
121static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
122 size_t count, loff_t *ppos)
123{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400124 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 char tmpbuf[TMPBUFLEN];
126 ssize_t length;
127
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500128 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400129 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
131}
132
133#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400134static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 size_t count, loff_t *ppos)
136
137{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400138 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
139 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500140 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500142 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Davi Arnautbfd51622005-10-30 14:59:24 -0800144 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500145 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500146
147 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500148 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500149 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500150
Al Viro8365a712015-12-24 00:08:06 -0500151 page = memdup_user_nul(buf, count);
152 if (IS_ERR(page))
153 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 length = -EINVAL;
156 if (sscanf(page, "%d", &new_value) != 1)
157 goto out;
158
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500159 new_value = !!new_value;
160
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400161 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500162 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500163 length = avc_has_perm(&selinux_state,
164 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500165 SECCLASS_SECURITY, SECURITY__SETENFORCE,
166 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (length)
168 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400169 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400170 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500171 " enabled=1 old-enabled=1 lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500172 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700173 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500174 audit_get_sessionid(current));
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400175 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500176 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500177 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500178 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400179 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 if (!new_value)
Janne Karhunen42df7442019-06-14 15:20:14 +0300181 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 length = count;
184out:
Al Viro8365a712015-12-24 00:08:06 -0500185 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return length;
187}
188#else
189#define sel_write_enforce NULL
190#endif
191
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800192static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200195 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Eric Paris3f120702007-09-21 14:37:10 -0400198static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
199 size_t count, loff_t *ppos)
200{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400201 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
202 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400203 char tmpbuf[TMPBUFLEN];
204 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500205 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400206 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400207 security_get_reject_unknown(state) :
208 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400209
210 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
211 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
212}
213
214static const struct file_operations sel_handle_unknown_ops = {
215 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200216 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400217};
218
KaiGai Kohei11904162010-09-14 18:28:39 +0900219static int sel_open_handle_status(struct inode *inode, struct file *filp)
220{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400221 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
222 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900223
224 if (!status)
225 return -ENOMEM;
226
227 filp->private_data = status;
228
229 return 0;
230}
231
232static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
233 size_t count, loff_t *ppos)
234{
235 struct page *status = filp->private_data;
236
237 BUG_ON(!status);
238
239 return simple_read_from_buffer(buf, count, ppos,
240 page_address(status),
241 sizeof(struct selinux_kernel_status));
242}
243
244static int sel_mmap_handle_status(struct file *filp,
245 struct vm_area_struct *vma)
246{
247 struct page *status = filp->private_data;
248 unsigned long size = vma->vm_end - vma->vm_start;
249
250 BUG_ON(!status);
251
252 /* only allows one page from the head */
253 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
254 return -EIO;
255 /* disallow writable mapping */
256 if (vma->vm_flags & VM_WRITE)
257 return -EPERM;
258 /* disallow mprotect() turns it into writable */
259 vma->vm_flags &= ~VM_MAYWRITE;
260
261 return remap_pfn_range(vma, vma->vm_start,
262 page_to_pfn(status),
263 size, vma->vm_page_prot);
264}
265
266static const struct file_operations sel_handle_status_ops = {
267 .open = sel_open_handle_status,
268 .read = sel_read_handle_status,
269 .mmap = sel_mmap_handle_status,
270 .llseek = generic_file_llseek,
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400274static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 size_t count, loff_t *ppos)
276
277{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400278 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500279 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ssize_t length;
281 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400282 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Paul Moore89b223b2019-12-18 21:45:08 -0500284 /* NOTE: we are now officially considering runtime disable as
285 * deprecated, and using it will become increasingly painful
286 * (e.g. sleeping/blocking) as we progress through future
287 * kernel releases until eventually it is removed
288 */
289 pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n");
290
Davi Arnautbfd51622005-10-30 14:59:24 -0800291 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500292 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500293
294 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500295 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500296 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500297
Al Viro8365a712015-12-24 00:08:06 -0500298 page = memdup_user_nul(buf, count);
299 if (IS_ERR(page))
300 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 length = -EINVAL;
303 if (sscanf(page, "%d", &new_value) != 1)
304 goto out;
305
306 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400307 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400308 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500309 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400311 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400312 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500313 " enabled=0 old-enabled=1 lsm=selinux res=1",
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400314 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700315 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500316 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 length = count;
320out:
Al Viro8365a712015-12-24 00:08:06 -0500321 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return length;
323}
324#else
325#define sel_write_disable NULL
326#endif
327
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800328static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200330 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
333static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400334 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 char tmpbuf[TMPBUFLEN];
337 ssize_t length;
338
339 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
340 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
341}
342
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800343static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200345 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348/* declaration for sel_write_load */
Daniel Burgener66ec3842020-08-19 15:59:33 -0400349static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
350 unsigned int *bool_num, char ***bool_pending_names,
351 unsigned int **bool_pending_values);
352static int sel_make_classes(struct selinux_policy *newpolicy,
353 struct dentry *class_dir,
354 unsigned long *last_class_ino);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400355
356/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400357static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400358 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400360/* declaration for sel_remove_old_policy_nodes */
361static void sel_remove_entries(struct dentry *de);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static ssize_t sel_read_mls(struct file *filp, char __user *buf,
364 size_t count, loff_t *ppos)
365{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400366 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 char tmpbuf[TMPBUFLEN];
368 ssize_t length;
369
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100370 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400371 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
373}
374
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800375static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200377 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
Eric Pariscee74f42010-10-13 17:50:25 -0400380struct policy_load_memory {
381 size_t len;
382 void *data;
383};
384
385static int sel_open_policy(struct inode *inode, struct file *filp)
386{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400387 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
388 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400389 struct policy_load_memory *plm = NULL;
390 int rc;
391
392 BUG_ON(filp->private_data);
393
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400394 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400395
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500396 rc = avc_has_perm(&selinux_state,
397 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500398 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400399 if (rc)
400 goto err;
401
402 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400403 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400404 goto err;
405
406 rc = -ENOMEM;
407 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
408 if (!plm)
409 goto err;
410
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400411 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500412 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400413 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500414 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400415 }
416
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400417 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400418 if (rc)
419 goto err;
420
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400421 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400422
423 filp->private_data = plm;
424
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400425 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400426
427 return 0;
428err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400429 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400430
431 if (plm)
432 vfree(plm->data);
433 kfree(plm);
434 return rc;
435}
436
437static int sel_release_policy(struct inode *inode, struct file *filp)
438{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400439 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400440 struct policy_load_memory *plm = filp->private_data;
441
442 BUG_ON(!plm);
443
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400444 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400445
446 vfree(plm->data);
447 kfree(plm);
448
449 return 0;
450}
451
452static ssize_t sel_read_policy(struct file *filp, char __user *buf,
453 size_t count, loff_t *ppos)
454{
455 struct policy_load_memory *plm = filp->private_data;
456 int ret;
457
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500458 ret = avc_has_perm(&selinux_state,
459 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500460 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400461 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400462 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400463
Jann Horn0da74122018-06-28 20:39:54 -0400464 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400465}
466
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530467static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400468{
Dave Jiang11bac802017-02-24 14:56:41 -0800469 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400470 unsigned long offset;
471 struct page *page;
472
473 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
474 return VM_FAULT_SIGBUS;
475
476 offset = vmf->pgoff << PAGE_SHIFT;
477 if (offset >= roundup(plm->len, PAGE_SIZE))
478 return VM_FAULT_SIGBUS;
479
480 page = vmalloc_to_page(plm->data + offset);
481 get_page(page);
482
483 vmf->page = page;
484
485 return 0;
486}
487
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700488static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400489 .fault = sel_mmap_policy_fault,
490 .page_mkwrite = sel_mmap_policy_fault,
491};
492
James Morrisad3fa082011-08-30 10:50:12 +1000493static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400494{
495 if (vma->vm_flags & VM_SHARED) {
496 /* do not allow mprotect to make mapping writable */
497 vma->vm_flags &= ~VM_MAYWRITE;
498
499 if (vma->vm_flags & VM_WRITE)
500 return -EACCES;
501 }
502
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700503 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400504 vma->vm_ops = &sel_mmap_policy_ops;
505
506 return 0;
507}
508
Eric Pariscee74f42010-10-13 17:50:25 -0400509static const struct file_operations sel_policy_ops = {
510 .open = sel_open_policy,
511 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400512 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400513 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500514 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400515};
516
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400517static void sel_remove_old_policy_nodes(struct selinux_fs_info *fsi)
518{
519 u32 i;
520
521 /* bool_dir cleanup */
522 for (i = 0; i < fsi->bool_num; i++)
523 kfree(fsi->bool_pending_names[i]);
524 kfree(fsi->bool_pending_names);
525 kfree(fsi->bool_pending_values);
526 fsi->bool_num = 0;
527 fsi->bool_pending_names = NULL;
528 fsi->bool_pending_values = NULL;
529
530 sel_remove_entries(fsi->bool_dir);
531
532 /* class_dir cleanup */
533 sel_remove_entries(fsi->class_dir);
534
535}
536
Stephen Smalley02a52c52020-08-07 09:29:34 -0400537static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
538 struct selinux_policy *newpolicy)
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400539{
540 int ret;
541
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400542 sel_remove_old_policy_nodes(fsi);
543
Daniel Burgener66ec3842020-08-19 15:59:33 -0400544 ret = sel_make_bools(newpolicy, fsi->bool_dir, &fsi->bool_num,
545 &fsi->bool_pending_names, &fsi->bool_pending_values);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400546 if (ret) {
547 pr_err("SELinux: failed to load policy booleans\n");
548 return ret;
549 }
550
Daniel Burgener66ec3842020-08-19 15:59:33 -0400551 ret = sel_make_classes(newpolicy, fsi->class_dir,
552 &fsi->last_class_ino);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400553 if (ret) {
554 pr_err("SELinux: failed to load policy classes\n");
555 return ret;
556 }
557
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400558 return 0;
559}
560
Eric Paris18729812008-04-17 14:15:45 -0400561static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 size_t count, loff_t *ppos)
563
564{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400565 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400566 struct selinux_policy *newpolicy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ssize_t length;
568 void *data = NULL;
569
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400570 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500572 length = avc_has_perm(&selinux_state,
573 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500574 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (length)
576 goto out;
577
Eric Parisb77a4932010-11-23 11:40:08 -0500578 /* No partial writes. */
579 length = -EINVAL;
580 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric Parisb77a4932010-11-23 11:40:08 -0500583 length = -ENOMEM;
584 data = vmalloc(count);
585 if (!data)
586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 length = -EFAULT;
589 if (copy_from_user(data, buf, count) != 0)
590 goto out;
591
Stephen Smalley02a52c52020-08-07 09:29:34 -0400592 length = security_load_policy(fsi->state, data, count, &newpolicy);
Gary Tierney4262fb52017-01-09 10:07:31 -0500593 if (length) {
594 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Stephen Smalley02a52c52020-08-07 09:29:34 -0400598 length = sel_make_policy_nodes(fsi, newpolicy);
599 if (length) {
600 selinux_policy_cancel(fsi->state, newpolicy);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400601 goto out1;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400602 }
603
604 selinux_policy_commit(fsi->state, newpolicy);
Eric Parisb77a4932010-11-23 11:40:08 -0500605
606 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400607
608out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400609 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400610 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700611 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500612 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400614 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 vfree(data);
616 return length;
617}
618
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800619static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200621 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622};
623
Eric Paris18729812008-04-17 14:15:45 -0400624static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400626 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
627 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500628 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800629 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ssize_t length;
631
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500632 length = avc_has_perm(&selinux_state,
633 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500634 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400638 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500639 if (length)
640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400642 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500643 if (length)
644 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800645
Eric Parisb77a4932010-11-23 11:40:08 -0500646 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800647 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200648 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400649 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800650 goto out;
651 }
652
653 memcpy(buf, canon, len);
654 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800656 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return length;
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
661 size_t count, loff_t *ppos)
662{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400663 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 char tmpbuf[TMPBUFLEN];
665 ssize_t length;
666
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400667 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
669}
670
Eric Paris18729812008-04-17 14:15:45 -0400671static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 size_t count, loff_t *ppos)
673{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400674 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500675 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ssize_t length;
677 unsigned int new_value;
678
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500679 length = avc_has_perm(&selinux_state,
680 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500681 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
682 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500684 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Davi Arnautbfd51622005-10-30 14:59:24 -0800686 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500687 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500688
689 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500690 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500691 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500692
Al Viro8365a712015-12-24 00:08:06 -0500693 page = memdup_user_nul(buf, count);
694 if (IS_ERR(page))
695 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 length = -EINVAL;
698 if (sscanf(page, "%u", &new_value) != 1)
699 goto out;
700
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500701 if (new_value) {
702 char comm[sizeof(current->comm)];
703
704 memcpy(comm, current->comm, sizeof(comm));
705 pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
706 comm, current->pid);
707 }
708
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400709 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 length = count;
711out:
Al Viro8365a712015-12-24 00:08:06 -0500712 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return length;
714}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800715static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .read = sel_read_checkreqprot,
717 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200718 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719};
720
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500721static ssize_t sel_write_validatetrans(struct file *file,
722 const char __user *buf,
723 size_t count, loff_t *ppos)
724{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400725 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
726 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500727 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
728 char *req = NULL;
729 u32 osid, nsid, tsid;
730 u16 tclass;
731 int rc;
732
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500733 rc = avc_has_perm(&selinux_state,
734 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500735 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500736 if (rc)
737 goto out;
738
739 rc = -ENOMEM;
740 if (count >= PAGE_SIZE)
741 goto out;
742
743 /* No partial writes. */
744 rc = -EINVAL;
745 if (*ppos != 0)
746 goto out;
747
Al Viro0b884d22017-05-13 18:12:07 -0400748 req = memdup_user_nul(buf, count);
749 if (IS_ERR(req)) {
750 rc = PTR_ERR(req);
751 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500752 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400753 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500754
755 rc = -ENOMEM;
756 oldcon = kzalloc(count + 1, GFP_KERNEL);
757 if (!oldcon)
758 goto out;
759
760 newcon = kzalloc(count + 1, GFP_KERNEL);
761 if (!newcon)
762 goto out;
763
764 taskcon = kzalloc(count + 1, GFP_KERNEL);
765 if (!taskcon)
766 goto out;
767
768 rc = -EINVAL;
769 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
770 goto out;
771
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400772 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500773 if (rc)
774 goto out;
775
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400776 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500777 if (rc)
778 goto out;
779
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400780 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500781 if (rc)
782 goto out;
783
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400784 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500785 if (!rc)
786 rc = count;
787out:
788 kfree(req);
789 kfree(oldcon);
790 kfree(newcon);
791 kfree(taskcon);
792 return rc;
793}
794
795static const struct file_operations sel_transition_ops = {
796 .write = sel_write_validatetrans,
797 .llseek = generic_file_llseek,
798};
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
802 */
Eric Paris18729812008-04-17 14:15:45 -0400803static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
804static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
805static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
806static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
807static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Eric Biggers631d2b42018-07-17 10:43:56 -0700809static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 [SEL_ACCESS] = sel_write_access,
811 [SEL_CREATE] = sel_write_create,
812 [SEL_RELABEL] = sel_write_relabel,
813 [SEL_USER] = sel_write_user,
814 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800815 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816};
817
818static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
819{
Al Viro496ad9a2013-01-23 17:07:38 -0500820 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 char *data;
822 ssize_t rv;
823
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800824 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
826
827 data = simple_transaction_get(file, buf, size);
828 if (IS_ERR(data))
829 return PTR_ERR(data);
830
Eric Paris18729812008-04-17 14:15:45 -0400831 rv = write_op[ino](file, data, size);
832 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 simple_transaction_set(file, rv);
834 rv = size;
835 }
836 return rv;
837}
838
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800839static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 .write = selinux_transaction_write,
841 .read = simple_transaction_read,
842 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200843 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844};
845
846/*
847 * payload - write methods
848 * If the method has a response, the response should be put in buf,
849 * and the length returned. Otherwise return 0 or and -error.
850 */
851
Eric Paris18729812008-04-17 14:15:45 -0400852static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400854 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
855 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500856 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 u32 ssid, tsid;
858 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct av_decision avd;
860 ssize_t length;
861
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500862 length = avc_has_perm(&selinux_state,
863 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500864 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800869 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Eric Parisb77a4932010-11-23 11:40:08 -0500873 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800874 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!tcon)
876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500879 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400882 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500883 if (length)
884 goto out;
885
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400886 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500887 if (length)
888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400890 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900893 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500894 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900896 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897out:
Eric Parisb77a4932010-11-23 11:40:08 -0500898 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 kfree(scon);
900 return length;
901}
902
Eric Paris18729812008-04-17 14:15:45 -0400903static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400905 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
906 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500907 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100908 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 u32 ssid, tsid, newsid;
910 u16 tclass;
911 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500912 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100914 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500916 length = avc_has_perm(&selinux_state,
917 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500918 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
919 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800924 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Eric Parisb77a4932010-11-23 11:40:08 -0500928 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800929 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!tcon)
931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100933 length = -ENOMEM;
934 namebuf = kzalloc(size + 1, GFP_KERNEL);
935 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100938 length = -EINVAL;
939 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
940 if (nargs < 3 || nargs > 4)
941 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400942 if (nargs == 4) {
943 /*
944 * If and when the name of new object to be queried contains
945 * either whitespace or multibyte characters, they shall be
946 * encoded based on the percentage-encoding rule.
947 * If not encoded, the sscanf logic picks up only left-half
948 * of the supplied name; splitted by a whitespace unexpectedly.
949 */
950 char *r, *w;
951 int c1, c2;
952
953 r = w = namebuf;
954 do {
955 c1 = *r++;
956 if (c1 == '+')
957 c1 = ' ';
958 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800959 c1 = hex_to_bin(*r++);
960 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400961 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800962 c2 = hex_to_bin(*r++);
963 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400964 goto out;
965 c1 = (c1 << 4) | c2;
966 }
967 *w++ = c1;
968 } while (c1 != '\0');
969
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100970 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400971 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100972
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400973 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500974 if (length)
975 goto out;
976
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400977 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500978 if (length)
979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400981 length = security_transition_sid_user(state, ssid, tsid, tclass,
982 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500983 if (length)
984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400986 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500987 if (length)
988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Eric Parisb77a4932010-11-23 11:40:08 -0500990 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200992 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400993 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 memcpy(buf, newcon, len);
998 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999out:
Eric Parisb77a4932010-11-23 11:40:08 -05001000 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001001 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -05001002 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree(scon);
1004 return length;
1005}
1006
Eric Paris18729812008-04-17 14:15:45 -04001007static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001009 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1010 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001011 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 u32 ssid, tsid, newsid;
1013 u16 tclass;
1014 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001015 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 u32 len;
1017
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001018 length = avc_has_perm(&selinux_state,
1019 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001020 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1021 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001023 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001026 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -05001028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric Parisb77a4932010-11-23 11:40:08 -05001030 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001031 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!tcon)
1033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 length = -EINVAL;
1036 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001039 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001040 if (length)
1041 goto out;
1042
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001043 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001044 if (length)
1045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001047 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001048 if (length)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001051 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001052 if (length)
1053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Eric Parisb77a4932010-11-23 11:40:08 -05001055 length = -ERANGE;
1056 if (len > SIMPLE_TRANSACTION_LIMIT)
1057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 memcpy(buf, newcon, len);
1060 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061out:
Eric Parisb77a4932010-11-23 11:40:08 -05001062 kfree(newcon);
1063 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 kfree(scon);
1065 return length;
1066}
1067
Eric Paris18729812008-04-17 14:15:45 -04001068static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001070 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1071 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001072 char *con = NULL, *user = NULL, *ptr;
1073 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ssize_t length;
1075 char *newcon;
1076 int i, rc;
1077 u32 len, nsids;
1078
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001079 length = avc_has_perm(&selinux_state,
1080 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001081 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1082 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001087 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Eric Parisb77a4932010-11-23 11:40:08 -05001091 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001092 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!user)
1094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 length = -EINVAL;
1097 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001100 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001101 if (length)
1102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001104 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001105 if (length)
1106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 length = sprintf(buf, "%u", nsids) + 1;
1109 ptr = buf + length;
1110 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001111 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (rc) {
1113 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1117 kfree(newcon);
1118 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 memcpy(ptr, newcon, len);
1122 kfree(newcon);
1123 ptr += len;
1124 length += len;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126out:
Eric Parisb77a4932010-11-23 11:40:08 -05001127 kfree(sids);
1128 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 kfree(con);
1130 return length;
1131}
1132
Eric Paris18729812008-04-17 14:15:45 -04001133static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001135 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1136 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001137 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 u32 ssid, tsid, newsid;
1139 u16 tclass;
1140 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001141 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 u32 len;
1143
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001144 length = avc_has_perm(&selinux_state,
1145 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001146 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1147 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001152 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001154 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Parisb77a4932010-11-23 11:40:08 -05001156 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001157 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!tcon)
1159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 length = -EINVAL;
1162 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001165 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001166 if (length)
1167 goto out;
1168
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001169 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001170 if (length)
1171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001173 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001174 if (length)
1175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001177 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001178 if (length)
1179 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Eric Parisb77a4932010-11-23 11:40:08 -05001181 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001183 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001184 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 memcpy(buf, newcon, len);
1189 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190out:
Eric Parisb77a4932010-11-23 11:40:08 -05001191 kfree(newcon);
1192 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 kfree(scon);
1194 return length;
1195}
1196
1197static struct inode *sel_make_inode(struct super_block *sb, int mode)
1198{
1199 struct inode *ret = new_inode(sb);
1200
1201 if (ret) {
1202 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001203 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 return ret;
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1209 size_t count, loff_t *ppos)
1210{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001211 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 char *page = NULL;
1213 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ssize_t ret;
1215 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001216 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001217 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001219 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Eric Parisb77a4932010-11-23 11:40:08 -05001221 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001222 if (index >= fsi->bool_num || strcmp(name,
1223 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001224 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric Parisb77a4932010-11-23 11:40:08 -05001226 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001227 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001228 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001229 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001231 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (cur_enforcing < 0) {
1233 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001234 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001237 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001238 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001239 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1240out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001241 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001243
1244out_unlock:
1245 mutex_unlock(&fsi->mutex);
1246 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1250 size_t count, loff_t *ppos)
1251{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001252 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001254 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001256 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001257 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jann Horn0da74122018-06-28 20:39:54 -04001259 if (count >= PAGE_SIZE)
1260 return -ENOMEM;
1261
1262 /* No partial writes. */
1263 if (*ppos != 0)
1264 return -EINVAL;
1265
1266 page = memdup_user_nul(buf, count);
1267 if (IS_ERR(page))
1268 return PTR_ERR(page);
1269
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001270 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001272 length = avc_has_perm(&selinux_state,
1273 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001274 SECCLASS_SECURITY, SECURITY__SETBOOL,
1275 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (length)
1277 goto out;
1278
Eric Parisb77a4932010-11-23 11:40:08 -05001279 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001280 if (index >= fsi->bool_num || strcmp(name,
1281 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001282 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 length = -EINVAL;
1285 if (sscanf(page, "%d", &new_value) != 1)
1286 goto out;
1287
1288 if (new_value)
1289 new_value = 1;
1290
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001291 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 length = count;
1293
1294out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001295 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001296 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return length;
1298}
1299
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001300static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001301 .read = sel_read_bool,
1302 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001303 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304};
1305
1306static ssize_t sel_commit_bools_write(struct file *filep,
1307 const char __user *buf,
1308 size_t count, loff_t *ppos)
1309{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001310 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001312 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int new_value;
1314
Jann Horn0da74122018-06-28 20:39:54 -04001315 if (count >= PAGE_SIZE)
1316 return -ENOMEM;
1317
1318 /* No partial writes. */
1319 if (*ppos != 0)
1320 return -EINVAL;
1321
1322 page = memdup_user_nul(buf, count);
1323 if (IS_ERR(page))
1324 return PTR_ERR(page);
1325
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001326 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001328 length = avc_has_perm(&selinux_state,
1329 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001330 SECCLASS_SECURITY, SECURITY__SETBOOL,
1331 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (length)
1333 goto out;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 length = -EINVAL;
1336 if (sscanf(page, "%d", &new_value) != 1)
1337 goto out;
1338
Eric Parisb77a4932010-11-23 11:40:08 -05001339 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001340 if (new_value && fsi->bool_pending_values)
1341 length = security_set_bools(fsi->state, fsi->bool_num,
1342 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Parisb77a4932010-11-23 11:40:08 -05001344 if (!length)
1345 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001348 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001349 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return length;
1351}
1352
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001353static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001354 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001355 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356};
1357
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001358static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Al Viroad521842014-12-24 14:56:48 -05001360 d_genocide(de);
1361 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364#define BOOL_DIR_NAME "booleans"
1365
Daniel Burgener66ec3842020-08-19 15:59:33 -04001366static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
1367 unsigned int *bool_num, char ***bool_pending_names,
1368 unsigned int **bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001370 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ssize_t len;
1372 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct inode *inode = NULL;
1374 struct inode_security_struct *isec;
1375 char **names = NULL, *page;
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001376 u32 i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int *values = NULL;
1378 u32 sid;
1379
Eric Parisb77a4932010-11-23 11:40:08 -05001380 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001381 page = (char *)get_zeroed_page(GFP_KERNEL);
1382 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Stephen Smalley02a52c52020-08-07 09:29:34 -04001385 ret = security_get_bools(newpolicy, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001386 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto out;
1388
1389 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001390 ret = -ENOMEM;
Daniel Burgener66ec3842020-08-19 15:59:33 -04001391 dentry = d_alloc_name(bool_dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001392 if (!dentry)
1393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Eric Parisb77a4932010-11-23 11:40:08 -05001395 ret = -ENOMEM;
Daniel Burgener66ec3842020-08-19 15:59:33 -04001396 inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001397 if (!inode) {
1398 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001399 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001400 }
Eric Parisb77a4932010-11-23 11:40:08 -05001401
Eric Parisb77a4932010-11-23 11:40:08 -05001402 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001403 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001404 if (len >= PAGE_SIZE) {
1405 dput(dentry);
1406 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001407 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001408 }
Eric Parisb77a4932010-11-23 11:40:08 -05001409
Casey Schaufler80788c22018-09-21 17:19:11 -07001410 isec = selinux_inode(inode);
Stephen Smalley02a52c52020-08-07 09:29:34 -04001411 ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001412 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001413 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001414 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1415 page);
1416 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001417 }
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001420 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001422 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 d_add(dentry, inode);
1424 }
Daniel Burgener66ec3842020-08-19 15:59:33 -04001425 *bool_num = num;
1426 *bool_pending_names = names;
1427 *bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001428
1429 free_page((unsigned long)page);
1430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431out:
1432 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001435 for (i = 0; i < num; i++)
1436 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 kfree(names);
1438 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001439 kfree(values);
Daniel Burgener66ec3842020-08-19 15:59:33 -04001440 sel_remove_entries(bool_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001441
1442 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1446 size_t count, loff_t *ppos)
1447{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001448 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1449 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 char tmpbuf[TMPBUFLEN];
1451 ssize_t length;
1452
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001453 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1454 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1456}
1457
Eric Paris18729812008-04-17 14:15:45 -04001458static ssize_t sel_write_avc_cache_threshold(struct file *file,
1459 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 size_t count, loff_t *ppos)
1461
1462{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001463 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1464 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001465 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001467 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001469 ret = avc_has_perm(&selinux_state,
1470 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001471 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1472 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001473 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Eric Parisb77a4932010-11-23 11:40:08 -05001476 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001477 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric Parisb77a4932010-11-23 11:40:08 -05001479 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001480 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001481 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001482
Al Viro8365a712015-12-24 00:08:06 -05001483 page = memdup_user_nul(buf, count);
1484 if (IS_ERR(page))
1485 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Eric Parisb77a4932010-11-23 11:40:08 -05001487 ret = -EINVAL;
1488 if (sscanf(page, "%u", &new_value) != 1)
1489 goto out;
1490
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001491 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494out:
Al Viro8365a712015-12-24 00:08:06 -05001495 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return ret;
1497}
1498
1499static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1500 size_t count, loff_t *ppos)
1501{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001502 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1503 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001505 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001508 if (!page)
1509 return -ENOMEM;
1510
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001511 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001512 if (length >= 0)
1513 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001515
1516 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001519static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1520 size_t count, loff_t *ppos)
1521{
1522 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1523 struct selinux_state *state = fsi->state;
1524 char *page;
1525 ssize_t length;
1526
1527 page = (char *)__get_free_page(GFP_KERNEL);
1528 if (!page)
1529 return -ENOMEM;
1530
1531 length = security_sidtab_hash_stats(state, page);
1532 if (length >= 0)
1533 length = simple_read_from_buffer(buf, count, ppos, page,
1534 length);
1535 free_page((unsigned long)page);
1536
1537 return length;
1538}
1539
1540static const struct file_operations sel_sidtab_hash_stats_ops = {
1541 .read = sel_read_sidtab_hash_stats,
1542 .llseek = generic_file_llseek,
1543};
1544
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001545static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 .read = sel_read_avc_cache_threshold,
1547 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001548 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549};
1550
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001551static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001553 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554};
1555
1556#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1557static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1558{
1559 int cpu;
1560
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301561 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (!cpu_possible(cpu))
1563 continue;
1564 *idx = cpu + 1;
1565 return &per_cpu(avc_cache_stats, cpu);
1566 }
Vasily Averin8d269a82020-02-01 10:47:47 +03001567 (*idx)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return NULL;
1569}
1570
1571static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1572{
1573 loff_t n = *pos - 1;
1574
1575 if (*pos == 0)
1576 return SEQ_START_TOKEN;
1577
1578 return sel_avc_get_stat_idx(&n);
1579}
1580
1581static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1582{
1583 return sel_avc_get_stat_idx(pos);
1584}
1585
1586static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1587{
1588 struct avc_cache_stats *st = v;
1589
Markus Elfring710a0642017-01-15 14:04:53 +01001590 if (v == SEQ_START_TOKEN) {
1591 seq_puts(seq,
1592 "lookups hits misses allocations reclaims frees\n");
1593 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001594 unsigned int lookups = st->lookups;
1595 unsigned int misses = st->misses;
1596 unsigned int hits = lookups - misses;
1597 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1598 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return 0;
1602}
1603
1604static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1605{ }
1606
Jan Engelhardt1996a102008-01-23 00:02:58 +01001607static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .start = sel_avc_stats_seq_start,
1609 .next = sel_avc_stats_seq_next,
1610 .show = sel_avc_stats_seq_show,
1611 .stop = sel_avc_stats_seq_stop,
1612};
1613
1614static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1615{
1616 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1617}
1618
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001619static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 .open = sel_open_avc_cache_stats,
1621 .read = seq_read,
1622 .llseek = seq_lseek,
1623 .release = seq_release,
1624};
1625#endif
1626
1627static int sel_make_avc_files(struct dentry *dir)
1628{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001629 struct super_block *sb = dir->d_sb;
1630 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001631 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001632 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 { "cache_threshold",
1634 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1635 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1636#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1637 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1638#endif
1639 };
1640
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001641 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct inode *inode;
1643 struct dentry *dentry;
1644
1645 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001646 if (!dentry)
1647 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001650 if (!inode) {
1651 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001652 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001653 }
Eric Parisb77a4932010-11-23 11:40:08 -05001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001656 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 d_add(dentry, inode);
1658 }
Eric Parisb77a4932010-11-23 11:40:08 -05001659
1660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001663static int sel_make_ss_files(struct dentry *dir)
1664{
1665 struct super_block *sb = dir->d_sb;
1666 struct selinux_fs_info *fsi = sb->s_fs_info;
1667 int i;
1668 static struct tree_descr files[] = {
1669 { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1670 };
1671
1672 for (i = 0; i < ARRAY_SIZE(files); i++) {
1673 struct inode *inode;
1674 struct dentry *dentry;
1675
1676 dentry = d_alloc_name(dir, files[i].name);
1677 if (!dentry)
1678 return -ENOMEM;
1679
1680 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1681 if (!inode) {
1682 dput(dentry);
1683 return -ENOMEM;
1684 }
1685
1686 inode->i_fop = files[i].ops;
1687 inode->i_ino = ++fsi->last_ino;
1688 d_add(dentry, inode);
1689 }
1690
1691 return 0;
1692}
1693
Eric Paris18729812008-04-17 14:15:45 -04001694static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001695 size_t count, loff_t *ppos)
1696{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001697 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001698 char *con;
1699 u32 sid, len;
1700 ssize_t ret;
1701
Al Viro496ad9a2013-01-23 17:07:38 -05001702 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001703 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001704 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001705 return ret;
1706
1707 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1708 kfree(con);
1709 return ret;
1710}
1711
1712static const struct file_operations sel_initcon_ops = {
1713 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001714 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001715};
1716
1717static int sel_make_initcon_files(struct dentry *dir)
1718{
Eric Parisb77a4932010-11-23 11:40:08 -05001719 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001720
1721 for (i = 1; i <= SECINITSID_NUM; i++) {
1722 struct inode *inode;
1723 struct dentry *dentry;
Stephen Smalleye3e0b582020-02-24 11:10:23 -05001724 const char *s = security_get_initial_sid_context(i);
1725
1726 if (!s)
1727 continue;
1728 dentry = d_alloc_name(dir, s);
Eric Parisb77a4932010-11-23 11:40:08 -05001729 if (!dentry)
1730 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001731
1732 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001733 if (!inode) {
1734 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001735 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001736 }
Eric Parisb77a4932010-11-23 11:40:08 -05001737
James Carterf0ee2e42007-04-04 10:11:29 -04001738 inode->i_fop = &sel_initcon_ops;
1739 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1740 d_add(dentry, inode);
1741 }
Eric Parisb77a4932010-11-23 11:40:08 -05001742
1743 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001744}
1745
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001746static inline unsigned long sel_class_to_ino(u16 class)
1747{
1748 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1749}
1750
1751static inline u16 sel_ino_to_class(unsigned long ino)
1752{
Eric Paris92ae9e82012-04-04 13:46:46 -04001753 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001754}
1755
1756static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1757{
1758 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1759}
1760
1761static inline u32 sel_ino_to_perm(unsigned long ino)
1762{
1763 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1764}
1765
Eric Paris18729812008-04-17 14:15:45 -04001766static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001767 size_t count, loff_t *ppos)
1768{
Al Viro496ad9a2013-01-23 17:07:38 -05001769 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001770 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001771 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001772 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001773}
1774
1775static const struct file_operations sel_class_ops = {
1776 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001777 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001778};
1779
Eric Paris18729812008-04-17 14:15:45 -04001780static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001781 size_t count, loff_t *ppos)
1782{
Al Viro496ad9a2013-01-23 17:07:38 -05001783 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001784 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001785 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001786 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001787}
1788
1789static const struct file_operations sel_perm_ops = {
1790 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001791 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001792};
1793
Paul Moore3bb56b22008-01-29 08:38:19 -05001794static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1795 size_t count, loff_t *ppos)
1796{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001797 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001798 int value;
1799 char tmpbuf[TMPBUFLEN];
1800 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001801 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001802
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001803 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001804 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1805
1806 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1807}
1808
1809static const struct file_operations sel_policycap_ops = {
1810 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001811 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001812};
1813
Stephen Smalley02a52c52020-08-07 09:29:34 -04001814static int sel_make_perm_files(struct selinux_policy *newpolicy,
1815 char *objclass, int classvalue,
1816 struct dentry *dir)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001817{
Eric Parisb77a4932010-11-23 11:40:08 -05001818 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001819 char **perms;
1820
Stephen Smalley02a52c52020-08-07 09:29:34 -04001821 rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001822 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001823 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001824
1825 for (i = 0; i < nperms; i++) {
1826 struct inode *inode;
1827 struct dentry *dentry;
1828
Eric Parisb77a4932010-11-23 11:40:08 -05001829 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001830 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001831 if (!dentry)
1832 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001833
Eric Parisb77a4932010-11-23 11:40:08 -05001834 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001835 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001836 if (!inode) {
1837 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001838 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001839 }
Eric Parisb77a4932010-11-23 11:40:08 -05001840
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001841 inode->i_fop = &sel_perm_ops;
1842 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001843 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001844 d_add(dentry, inode);
1845 }
Eric Parisb77a4932010-11-23 11:40:08 -05001846 rc = 0;
1847out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001848 for (i = 0; i < nperms; i++)
1849 kfree(perms[i]);
1850 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001851 return rc;
1852}
1853
Stephen Smalley02a52c52020-08-07 09:29:34 -04001854static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
1855 char *classname, int index,
1856 struct dentry *dir)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001857{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001858 struct super_block *sb = dir->d_sb;
1859 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001860 struct dentry *dentry = NULL;
1861 struct inode *inode = NULL;
1862 int rc;
1863
1864 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001865 if (!dentry)
1866 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001867
1868 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001869 if (!inode) {
1870 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001871 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001872 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001873
1874 inode->i_fop = &sel_class_ops;
1875 inode->i_ino = sel_class_to_ino(index);
1876 d_add(dentry, inode);
1877
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001878 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001879 if (IS_ERR(dentry))
1880 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001881
Stephen Smalley02a52c52020-08-07 09:29:34 -04001882 rc = sel_make_perm_files(newpolicy, classname, index, dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001883
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001884 return rc;
1885}
1886
Daniel Burgener66ec3842020-08-19 15:59:33 -04001887static int sel_make_classes(struct selinux_policy *newpolicy,
1888 struct dentry *class_dir,
1889 unsigned long *last_class_ino)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001890{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001891
Eric Parisb77a4932010-11-23 11:40:08 -05001892 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001893 char **classes;
1894
Stephen Smalley02a52c52020-08-07 09:29:34 -04001895 rc = security_get_classes(newpolicy, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001896 if (rc)
1897 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001898
1899 /* +2 since classes are 1-indexed */
Daniel Burgener66ec3842020-08-19 15:59:33 -04001900 *last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001901
1902 for (i = 0; i < nclasses; i++) {
1903 struct dentry *class_name_dir;
1904
Daniel Burgener66ec3842020-08-19 15:59:33 -04001905 class_name_dir = sel_make_dir(class_dir, classes[i],
1906 last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001907 if (IS_ERR(class_name_dir)) {
1908 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001909 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001910 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001911
1912 /* i+1 since class values are 1-indexed */
Stephen Smalley02a52c52020-08-07 09:29:34 -04001913 rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001914 class_name_dir);
1915 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001916 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001917 }
Eric Parisb77a4932010-11-23 11:40:08 -05001918 rc = 0;
1919out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001920 for (i = 0; i < nclasses; i++)
1921 kfree(classes[i]);
1922 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001923 return rc;
1924}
1925
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001926static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001927{
1928 unsigned int iter;
1929 struct dentry *dentry = NULL;
1930 struct inode *inode = NULL;
1931
Paul Moore3bb56b22008-01-29 08:38:19 -05001932 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001933 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001934 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001935 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001936 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001937 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001938
1939 if (dentry == NULL)
1940 return -ENOMEM;
1941
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001942 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001943 if (inode == NULL) {
1944 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001945 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001946 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001947
1948 inode->i_fop = &sel_policycap_ops;
1949 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1950 d_add(dentry, inode);
1951 }
1952
1953 return 0;
1954}
1955
Al Viroa1c2aa12012-03-18 20:36:59 -04001956static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001957 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Al Viroa1c2aa12012-03-18 20:36:59 -04001959 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 struct inode *inode;
1961
Al Viroa1c2aa12012-03-18 20:36:59 -04001962 if (!dentry)
1963 return ERR_PTR(-ENOMEM);
1964
1965 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1966 if (!inode) {
1967 dput(dentry);
1968 return ERR_PTR(-ENOMEM);
1969 }
Eric Parisb77a4932010-11-23 11:40:08 -05001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 inode->i_op = &simple_dir_inode_operations;
1972 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001973 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001974 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001975 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001977 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001978 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001979
Al Viroa1c2aa12012-03-18 20:36:59 -04001980 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001983#define NULL_FILE_NAME "null"
1984
David Howells920f50b2019-03-25 16:38:30 +00001985static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001987 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 int ret;
1989 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001990 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct inode_security_struct *isec;
1992
Eric Biggerscda37122017-03-25 21:15:37 -07001993 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1995 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001996 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1998 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1999 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
2000 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
2001 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
2002 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
2003 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
2004 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
2005 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
2006 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04002007 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
2008 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09002009 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05002010 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05002011 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
2012 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* last one */ {""}
2014 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002015
2016 ret = selinux_fs_info_create(sb);
2017 if (ret)
2018 goto err;
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
2021 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002022 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002024 fsi = sb->s_fs_info;
2025 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
2026 if (IS_ERR(fsi->bool_dir)) {
2027 ret = PTR_ERR(fsi->bool_dir);
2028 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08002029 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Eric Parisb77a4932010-11-23 11:40:08 -05002032 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05002034 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08002035 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Eric Parisb77a4932010-11-23 11:40:08 -05002037 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08002039 if (!inode) {
2040 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08002041 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08002042 }
Eric Parisb77a4932010-11-23 11:40:08 -05002043
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002044 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07002045 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 isec->sid = SECINITSID_DEVNULL;
2047 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01002048 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2051 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002053 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002054 if (IS_ERR(dentry)) {
2055 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08002056 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 ret = sel_make_avc_files(dentry);
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01002060
2061 dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2062 if (IS_ERR(dentry)) {
2063 ret = PTR_ERR(dentry);
2064 goto err;
2065 }
2066
2067 ret = sel_make_ss_files(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002069 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04002070
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002071 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002072 if (IS_ERR(dentry)) {
2073 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04002074 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002075 }
James Carterf0ee2e42007-04-04 10:11:29 -04002076
2077 ret = sel_make_initcon_files(dentry);
2078 if (ret)
2079 goto err;
2080
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002081 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
2082 if (IS_ERR(fsi->class_dir)) {
2083 ret = PTR_ERR(fsi->class_dir);
2084 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002085 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002086 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002087
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002088 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
2089 &fsi->last_ino);
2090 if (IS_ERR(fsi->policycap_dir)) {
2091 ret = PTR_ERR(fsi->policycap_dir);
2092 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002093 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002094 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002095
Stephen Smalley02a52c52020-08-07 09:29:34 -04002096 ret = sel_make_policycap(fsi);
2097 if (ret) {
2098 pr_err("SELinux: failed to load policy capabilities\n");
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002099 goto err;
Stephen Smalley02a52c52020-08-07 09:29:34 -04002100 }
2101
Eric Parisb77a4932010-11-23 11:40:08 -05002102 return 0;
James Morris161ce452006-03-22 00:09:17 -08002103err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002104 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002105 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002106
2107 selinux_fs_info_free(sb);
2108
Eric Parisb77a4932010-11-23 11:40:08 -05002109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
David Howells920f50b2019-03-25 16:38:30 +00002112static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
David Howells920f50b2019-03-25 16:38:30 +00002114 return get_tree_single(fc, sel_fill_super);
2115}
2116
2117static const struct fs_context_operations sel_context_ops = {
2118 .get_tree = sel_get_tree,
2119};
2120
2121static int sel_init_fs_context(struct fs_context *fc)
2122{
2123 fc->ops = &sel_context_ops;
2124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002127static void sel_kill_sb(struct super_block *sb)
2128{
2129 selinux_fs_info_free(sb);
2130 kill_litter_super(sb);
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static struct file_system_type sel_fs_type = {
2134 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002135 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002136 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137};
2138
2139struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002140struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142static int __init init_sel_fs(void)
2143{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002144 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2145 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 int err;
2147
Stephen Smalley6c5a6822019-12-17 09:15:10 -05002148 if (!selinux_enabled_boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002150
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002151 err = sysfs_create_mount_point(fs_kobj, "selinux");
2152 if (err)
2153 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002156 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002157 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002158 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002159 }
Eric Parisb77a4932010-11-23 11:40:08 -05002160
Al Viro765927b2012-06-26 21:58:53 +04002161 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002162 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002163 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002164 err = PTR_ERR(selinuxfs_mount);
2165 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002167 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2168 &null_name);
2169 if (IS_ERR(selinux_null.dentry)) {
2170 pr_err("selinuxfs: could not lookup null!\n");
2171 err = PTR_ERR(selinux_null.dentry);
2172 selinux_null.dentry = NULL;
2173 }
Eric Parisb77a4932010-11-23 11:40:08 -05002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176}
2177
2178__initcall(init_sel_fs);
2179
2180#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2181void exit_sel_fs(void)
2182{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002183 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002184 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002185 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 unregister_filesystem(&sel_fs_type);
2187}
2188#endif