blob: 19670e9bcd72e5d8c1c4ea2cc473928c16a01367 [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 */
Stephen Smalley02a52c52020-08-07 09:29:34 -0400349static int sel_make_bools(struct selinux_fs_info *fsi,
350 struct selinux_policy *newpolicy);
351static int sel_make_classes(struct selinux_fs_info *fsi,
352 struct selinux_policy *newpolicy);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400353
354/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400355static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400356 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400358/* declaration for sel_remove_old_policy_nodes */
359static void sel_remove_entries(struct dentry *de);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static ssize_t sel_read_mls(struct file *filp, char __user *buf,
362 size_t count, loff_t *ppos)
363{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400364 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 char tmpbuf[TMPBUFLEN];
366 ssize_t length;
367
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100368 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400369 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
371}
372
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800373static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200375 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
Eric Pariscee74f42010-10-13 17:50:25 -0400378struct policy_load_memory {
379 size_t len;
380 void *data;
381};
382
383static int sel_open_policy(struct inode *inode, struct file *filp)
384{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400385 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
386 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400387 struct policy_load_memory *plm = NULL;
388 int rc;
389
390 BUG_ON(filp->private_data);
391
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400392 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400393
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500394 rc = avc_has_perm(&selinux_state,
395 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500396 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400397 if (rc)
398 goto err;
399
400 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400401 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400402 goto err;
403
404 rc = -ENOMEM;
405 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
406 if (!plm)
407 goto err;
408
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400409 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500410 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400411 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500412 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400413 }
414
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400415 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400416 if (rc)
417 goto err;
418
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400419 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400420
421 filp->private_data = plm;
422
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400423 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400424
425 return 0;
426err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400427 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400428
429 if (plm)
430 vfree(plm->data);
431 kfree(plm);
432 return rc;
433}
434
435static int sel_release_policy(struct inode *inode, struct file *filp)
436{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400437 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400438 struct policy_load_memory *plm = filp->private_data;
439
440 BUG_ON(!plm);
441
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400442 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400443
444 vfree(plm->data);
445 kfree(plm);
446
447 return 0;
448}
449
450static ssize_t sel_read_policy(struct file *filp, char __user *buf,
451 size_t count, loff_t *ppos)
452{
453 struct policy_load_memory *plm = filp->private_data;
454 int ret;
455
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500456 ret = avc_has_perm(&selinux_state,
457 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500458 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400459 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400460 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400461
Jann Horn0da74122018-06-28 20:39:54 -0400462 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400463}
464
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530465static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400466{
Dave Jiang11bac802017-02-24 14:56:41 -0800467 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400468 unsigned long offset;
469 struct page *page;
470
471 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
472 return VM_FAULT_SIGBUS;
473
474 offset = vmf->pgoff << PAGE_SHIFT;
475 if (offset >= roundup(plm->len, PAGE_SIZE))
476 return VM_FAULT_SIGBUS;
477
478 page = vmalloc_to_page(plm->data + offset);
479 get_page(page);
480
481 vmf->page = page;
482
483 return 0;
484}
485
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700486static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400487 .fault = sel_mmap_policy_fault,
488 .page_mkwrite = sel_mmap_policy_fault,
489};
490
James Morrisad3fa082011-08-30 10:50:12 +1000491static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400492{
493 if (vma->vm_flags & VM_SHARED) {
494 /* do not allow mprotect to make mapping writable */
495 vma->vm_flags &= ~VM_MAYWRITE;
496
497 if (vma->vm_flags & VM_WRITE)
498 return -EACCES;
499 }
500
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700501 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400502 vma->vm_ops = &sel_mmap_policy_ops;
503
504 return 0;
505}
506
Eric Pariscee74f42010-10-13 17:50:25 -0400507static const struct file_operations sel_policy_ops = {
508 .open = sel_open_policy,
509 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400510 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400511 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500512 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400513};
514
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400515static void sel_remove_old_policy_nodes(struct selinux_fs_info *fsi)
516{
517 u32 i;
518
519 /* bool_dir cleanup */
520 for (i = 0; i < fsi->bool_num; i++)
521 kfree(fsi->bool_pending_names[i]);
522 kfree(fsi->bool_pending_names);
523 kfree(fsi->bool_pending_values);
524 fsi->bool_num = 0;
525 fsi->bool_pending_names = NULL;
526 fsi->bool_pending_values = NULL;
527
528 sel_remove_entries(fsi->bool_dir);
529
530 /* class_dir cleanup */
531 sel_remove_entries(fsi->class_dir);
532
533}
534
Stephen Smalley02a52c52020-08-07 09:29:34 -0400535static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
536 struct selinux_policy *newpolicy)
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400537{
538 int ret;
539
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400540 sel_remove_old_policy_nodes(fsi);
541
Stephen Smalley02a52c52020-08-07 09:29:34 -0400542 ret = sel_make_bools(fsi, newpolicy);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400543 if (ret) {
544 pr_err("SELinux: failed to load policy booleans\n");
545 return ret;
546 }
547
Stephen Smalley02a52c52020-08-07 09:29:34 -0400548 ret = sel_make_classes(fsi, newpolicy);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400549 if (ret) {
550 pr_err("SELinux: failed to load policy classes\n");
551 return ret;
552 }
553
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400554 return 0;
555}
556
Eric Paris18729812008-04-17 14:15:45 -0400557static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 size_t count, loff_t *ppos)
559
560{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400561 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400562 struct selinux_policy *newpolicy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ssize_t length;
564 void *data = NULL;
565
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400566 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500568 length = avc_has_perm(&selinux_state,
569 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500570 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (length)
572 goto out;
573
Eric Parisb77a4932010-11-23 11:40:08 -0500574 /* No partial writes. */
575 length = -EINVAL;
576 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Parisb77a4932010-11-23 11:40:08 -0500579 length = -ENOMEM;
580 data = vmalloc(count);
581 if (!data)
582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 length = -EFAULT;
585 if (copy_from_user(data, buf, count) != 0)
586 goto out;
587
Stephen Smalley02a52c52020-08-07 09:29:34 -0400588 length = security_load_policy(fsi->state, data, count, &newpolicy);
Gary Tierney4262fb52017-01-09 10:07:31 -0500589 if (length) {
590 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Stephen Smalley02a52c52020-08-07 09:29:34 -0400594 length = sel_make_policy_nodes(fsi, newpolicy);
595 if (length) {
596 selinux_policy_cancel(fsi->state, newpolicy);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400597 goto out1;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400598 }
599
600 selinux_policy_commit(fsi->state, newpolicy);
Eric Parisb77a4932010-11-23 11:40:08 -0500601
602 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400603
604out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400605 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400606 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700607 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500608 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400610 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 vfree(data);
612 return length;
613}
614
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800615static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200617 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618};
619
Eric Paris18729812008-04-17 14:15:45 -0400620static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400622 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
623 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500624 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800625 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ssize_t length;
627
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500628 length = avc_has_perm(&selinux_state,
629 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500630 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400634 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500635 if (length)
636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400638 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500639 if (length)
640 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800641
Eric Parisb77a4932010-11-23 11:40:08 -0500642 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800643 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200644 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400645 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800646 goto out;
647 }
648
649 memcpy(buf, canon, len);
650 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800652 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return length;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
657 size_t count, loff_t *ppos)
658{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400659 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 char tmpbuf[TMPBUFLEN];
661 ssize_t length;
662
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400663 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
665}
666
Eric Paris18729812008-04-17 14:15:45 -0400667static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 size_t count, loff_t *ppos)
669{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400670 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500671 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ssize_t length;
673 unsigned int new_value;
674
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500675 length = avc_has_perm(&selinux_state,
676 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500677 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
678 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500680 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Davi Arnautbfd51622005-10-30 14:59:24 -0800682 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500683 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500684
685 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500686 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500687 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500688
Al Viro8365a712015-12-24 00:08:06 -0500689 page = memdup_user_nul(buf, count);
690 if (IS_ERR(page))
691 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 length = -EINVAL;
694 if (sscanf(page, "%u", &new_value) != 1)
695 goto out;
696
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500697 if (new_value) {
698 char comm[sizeof(current->comm)];
699
700 memcpy(comm, current->comm, sizeof(comm));
701 pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
702 comm, current->pid);
703 }
704
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400705 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 length = count;
707out:
Al Viro8365a712015-12-24 00:08:06 -0500708 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return length;
710}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800711static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 .read = sel_read_checkreqprot,
713 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200714 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
716
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500717static ssize_t sel_write_validatetrans(struct file *file,
718 const char __user *buf,
719 size_t count, loff_t *ppos)
720{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400721 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
722 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500723 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
724 char *req = NULL;
725 u32 osid, nsid, tsid;
726 u16 tclass;
727 int rc;
728
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500729 rc = avc_has_perm(&selinux_state,
730 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500731 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500732 if (rc)
733 goto out;
734
735 rc = -ENOMEM;
736 if (count >= PAGE_SIZE)
737 goto out;
738
739 /* No partial writes. */
740 rc = -EINVAL;
741 if (*ppos != 0)
742 goto out;
743
Al Viro0b884d22017-05-13 18:12:07 -0400744 req = memdup_user_nul(buf, count);
745 if (IS_ERR(req)) {
746 rc = PTR_ERR(req);
747 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500748 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400749 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500750
751 rc = -ENOMEM;
752 oldcon = kzalloc(count + 1, GFP_KERNEL);
753 if (!oldcon)
754 goto out;
755
756 newcon = kzalloc(count + 1, GFP_KERNEL);
757 if (!newcon)
758 goto out;
759
760 taskcon = kzalloc(count + 1, GFP_KERNEL);
761 if (!taskcon)
762 goto out;
763
764 rc = -EINVAL;
765 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
766 goto out;
767
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400768 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500769 if (rc)
770 goto out;
771
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400772 rc = security_context_str_to_sid(state, newcon, &nsid, 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, taskcon, &tsid, 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_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500781 if (!rc)
782 rc = count;
783out:
784 kfree(req);
785 kfree(oldcon);
786 kfree(newcon);
787 kfree(taskcon);
788 return rc;
789}
790
791static const struct file_operations sel_transition_ops = {
792 .write = sel_write_validatetrans,
793 .llseek = generic_file_llseek,
794};
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/*
797 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
798 */
Eric Paris18729812008-04-17 14:15:45 -0400799static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
800static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
801static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
802static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
803static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Eric Biggers631d2b42018-07-17 10:43:56 -0700805static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 [SEL_ACCESS] = sel_write_access,
807 [SEL_CREATE] = sel_write_create,
808 [SEL_RELABEL] = sel_write_relabel,
809 [SEL_USER] = sel_write_user,
810 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800811 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812};
813
814static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
815{
Al Viro496ad9a2013-01-23 17:07:38 -0500816 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 char *data;
818 ssize_t rv;
819
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800820 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -EINVAL;
822
823 data = simple_transaction_get(file, buf, size);
824 if (IS_ERR(data))
825 return PTR_ERR(data);
826
Eric Paris18729812008-04-17 14:15:45 -0400827 rv = write_op[ino](file, data, size);
828 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 simple_transaction_set(file, rv);
830 rv = size;
831 }
832 return rv;
833}
834
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800835static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 .write = selinux_transaction_write,
837 .read = simple_transaction_read,
838 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200839 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840};
841
842/*
843 * payload - write methods
844 * If the method has a response, the response should be put in buf,
845 * and the length returned. Otherwise return 0 or and -error.
846 */
847
Eric Paris18729812008-04-17 14:15:45 -0400848static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400850 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
851 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500852 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 u32 ssid, tsid;
854 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct av_decision avd;
856 ssize_t length;
857
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500858 length = avc_has_perm(&selinux_state,
859 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500860 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800865 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Eric Parisb77a4932010-11-23 11:40:08 -0500869 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800870 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!tcon)
872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500875 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400878 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500879 if (length)
880 goto out;
881
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400882 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500883 if (length)
884 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400886 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900889 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500890 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900892 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893out:
Eric Parisb77a4932010-11-23 11:40:08 -0500894 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 kfree(scon);
896 return length;
897}
898
Eric Paris18729812008-04-17 14:15:45 -0400899static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400901 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
902 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500903 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100904 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 u32 ssid, tsid, newsid;
906 u16 tclass;
907 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500908 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100910 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500912 length = avc_has_perm(&selinux_state,
913 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500914 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
915 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800920 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Eric Parisb77a4932010-11-23 11:40:08 -0500924 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800925 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!tcon)
927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100929 length = -ENOMEM;
930 namebuf = kzalloc(size + 1, GFP_KERNEL);
931 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100934 length = -EINVAL;
935 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
936 if (nargs < 3 || nargs > 4)
937 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400938 if (nargs == 4) {
939 /*
940 * If and when the name of new object to be queried contains
941 * either whitespace or multibyte characters, they shall be
942 * encoded based on the percentage-encoding rule.
943 * If not encoded, the sscanf logic picks up only left-half
944 * of the supplied name; splitted by a whitespace unexpectedly.
945 */
946 char *r, *w;
947 int c1, c2;
948
949 r = w = namebuf;
950 do {
951 c1 = *r++;
952 if (c1 == '+')
953 c1 = ' ';
954 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800955 c1 = hex_to_bin(*r++);
956 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400957 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800958 c2 = hex_to_bin(*r++);
959 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400960 goto out;
961 c1 = (c1 << 4) | c2;
962 }
963 *w++ = c1;
964 } while (c1 != '\0');
965
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100966 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400967 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100968
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400969 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500970 if (length)
971 goto out;
972
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400973 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500974 if (length)
975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400977 length = security_transition_sid_user(state, ssid, tsid, tclass,
978 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500979 if (length)
980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400982 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500983 if (length)
984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Eric Parisb77a4932010-11-23 11:40:08 -0500986 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200988 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400989 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 memcpy(buf, newcon, len);
994 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995out:
Eric Parisb77a4932010-11-23 11:40:08 -0500996 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100997 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500998 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 kfree(scon);
1000 return length;
1001}
1002
Eric Paris18729812008-04-17 14:15:45 -04001003static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001005 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1006 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001007 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 u32 ssid, tsid, newsid;
1009 u16 tclass;
1010 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001011 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 u32 len;
1013
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001014 length = avc_has_perm(&selinux_state,
1015 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001016 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1017 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001022 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -05001024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Eric Parisb77a4932010-11-23 11:40:08 -05001026 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001027 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!tcon)
1029 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 length = -EINVAL;
1032 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001035 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001036 if (length)
1037 goto out;
1038
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001039 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001040 if (length)
1041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001043 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
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_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001048 if (length)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Eric Parisb77a4932010-11-23 11:40:08 -05001051 length = -ERANGE;
1052 if (len > SIMPLE_TRANSACTION_LIMIT)
1053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 memcpy(buf, newcon, len);
1056 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057out:
Eric Parisb77a4932010-11-23 11:40:08 -05001058 kfree(newcon);
1059 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 kfree(scon);
1061 return length;
1062}
1063
Eric Paris18729812008-04-17 14:15:45 -04001064static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001066 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1067 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001068 char *con = NULL, *user = NULL, *ptr;
1069 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ssize_t length;
1071 char *newcon;
1072 int i, rc;
1073 u32 len, nsids;
1074
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001075 length = avc_has_perm(&selinux_state,
1076 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001077 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1078 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001080 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001083 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Eric Parisb77a4932010-11-23 11:40:08 -05001087 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001088 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!user)
1090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 length = -EINVAL;
1093 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001096 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001097 if (length)
1098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001100 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001101 if (length)
1102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 length = sprintf(buf, "%u", nsids) + 1;
1105 ptr = buf + length;
1106 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001107 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (rc) {
1109 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1113 kfree(newcon);
1114 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117 memcpy(ptr, newcon, len);
1118 kfree(newcon);
1119 ptr += len;
1120 length += len;
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122out:
Eric Parisb77a4932010-11-23 11:40:08 -05001123 kfree(sids);
1124 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 kfree(con);
1126 return length;
1127}
1128
Eric Paris18729812008-04-17 14:15:45 -04001129static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001131 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1132 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001133 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 u32 ssid, tsid, newsid;
1135 u16 tclass;
1136 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001137 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 u32 len;
1139
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001140 length = avc_has_perm(&selinux_state,
1141 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001142 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1143 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001148 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Eric Parisb77a4932010-11-23 11:40:08 -05001152 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001153 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!tcon)
1155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 length = -EINVAL;
1158 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001161 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001162 if (length)
1163 goto out;
1164
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001165 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001166 if (length)
1167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001169 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
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_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001174 if (length)
1175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Eric Parisb77a4932010-11-23 11:40:08 -05001177 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001179 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001180 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 memcpy(buf, newcon, len);
1185 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186out:
Eric Parisb77a4932010-11-23 11:40:08 -05001187 kfree(newcon);
1188 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 kfree(scon);
1190 return length;
1191}
1192
1193static struct inode *sel_make_inode(struct super_block *sb, int mode)
1194{
1195 struct inode *ret = new_inode(sb);
1196
1197 if (ret) {
1198 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001199 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 return ret;
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1205 size_t count, loff_t *ppos)
1206{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001207 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 char *page = NULL;
1209 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ssize_t ret;
1211 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001212 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001213 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001215 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Eric Parisb77a4932010-11-23 11:40:08 -05001217 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001218 if (index >= fsi->bool_num || strcmp(name,
1219 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001220 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Eric Parisb77a4932010-11-23 11:40:08 -05001222 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001223 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001224 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001225 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001227 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (cur_enforcing < 0) {
1229 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001230 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001233 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001234 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001235 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1236out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001237 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001239
1240out_unlock:
1241 mutex_unlock(&fsi->mutex);
1242 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1246 size_t count, loff_t *ppos)
1247{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001248 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001250 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001252 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001253 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jann Horn0da74122018-06-28 20:39:54 -04001255 if (count >= PAGE_SIZE)
1256 return -ENOMEM;
1257
1258 /* No partial writes. */
1259 if (*ppos != 0)
1260 return -EINVAL;
1261
1262 page = memdup_user_nul(buf, count);
1263 if (IS_ERR(page))
1264 return PTR_ERR(page);
1265
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001266 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001268 length = avc_has_perm(&selinux_state,
1269 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001270 SECCLASS_SECURITY, SECURITY__SETBOOL,
1271 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (length)
1273 goto out;
1274
Eric Parisb77a4932010-11-23 11:40:08 -05001275 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001276 if (index >= fsi->bool_num || strcmp(name,
1277 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001278 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 length = -EINVAL;
1281 if (sscanf(page, "%d", &new_value) != 1)
1282 goto out;
1283
1284 if (new_value)
1285 new_value = 1;
1286
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001287 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 length = count;
1289
1290out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001291 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001292 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return length;
1294}
1295
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001296static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001297 .read = sel_read_bool,
1298 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001299 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300};
1301
1302static ssize_t sel_commit_bools_write(struct file *filep,
1303 const char __user *buf,
1304 size_t count, loff_t *ppos)
1305{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001306 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001308 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int new_value;
1310
Jann Horn0da74122018-06-28 20:39:54 -04001311 if (count >= PAGE_SIZE)
1312 return -ENOMEM;
1313
1314 /* No partial writes. */
1315 if (*ppos != 0)
1316 return -EINVAL;
1317
1318 page = memdup_user_nul(buf, count);
1319 if (IS_ERR(page))
1320 return PTR_ERR(page);
1321
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001322 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001324 length = avc_has_perm(&selinux_state,
1325 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001326 SECCLASS_SECURITY, SECURITY__SETBOOL,
1327 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (length)
1329 goto out;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 length = -EINVAL;
1332 if (sscanf(page, "%d", &new_value) != 1)
1333 goto out;
1334
Eric Parisb77a4932010-11-23 11:40:08 -05001335 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001336 if (new_value && fsi->bool_pending_values)
1337 length = security_set_bools(fsi->state, fsi->bool_num,
1338 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Eric Parisb77a4932010-11-23 11:40:08 -05001340 if (!length)
1341 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001344 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001345 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return length;
1347}
1348
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001349static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001350 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001351 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352};
1353
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001354static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Al Viroad521842014-12-24 14:56:48 -05001356 d_genocide(de);
1357 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360#define BOOL_DIR_NAME "booleans"
1361
Stephen Smalley02a52c52020-08-07 09:29:34 -04001362static int sel_make_bools(struct selinux_fs_info *fsi,
1363 struct selinux_policy *newpolicy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001365 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 ssize_t len;
1367 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001368 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct inode *inode = NULL;
1370 struct inode_security_struct *isec;
1371 char **names = NULL, *page;
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001372 u32 i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int *values = NULL;
1374 u32 sid;
1375
Eric Parisb77a4932010-11-23 11:40:08 -05001376 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001377 page = (char *)get_zeroed_page(GFP_KERNEL);
1378 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Stephen Smalley02a52c52020-08-07 09:29:34 -04001381 ret = security_get_bools(newpolicy, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001382 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 goto out;
1384
1385 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001386 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001388 if (!dentry)
1389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Eric Parisb77a4932010-11-23 11:40:08 -05001391 ret = -ENOMEM;
1392 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001393 if (!inode) {
1394 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001395 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001396 }
Eric Parisb77a4932010-11-23 11:40:08 -05001397
Eric Parisb77a4932010-11-23 11:40:08 -05001398 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001399 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001400 if (len >= PAGE_SIZE) {
1401 dput(dentry);
1402 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001403 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001404 }
Eric Parisb77a4932010-11-23 11:40:08 -05001405
Casey Schaufler80788c22018-09-21 17:19:11 -07001406 isec = selinux_inode(inode);
Stephen Smalley02a52c52020-08-07 09:29:34 -04001407 ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001408 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001409 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001410 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1411 page);
1412 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001416 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001418 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 d_add(dentry, inode);
1420 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001421 fsi->bool_num = num;
1422 fsi->bool_pending_names = names;
1423 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001424
1425 free_page((unsigned long)page);
1426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427out:
1428 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001431 for (i = 0; i < num; i++)
1432 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 kfree(names);
1434 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001435 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001436 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001437
1438 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1442 size_t count, loff_t *ppos)
1443{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001444 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1445 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 char tmpbuf[TMPBUFLEN];
1447 ssize_t length;
1448
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001449 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1450 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1452}
1453
Eric Paris18729812008-04-17 14:15:45 -04001454static ssize_t sel_write_avc_cache_threshold(struct file *file,
1455 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 size_t count, loff_t *ppos)
1457
1458{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001459 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1460 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001461 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001463 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001465 ret = avc_has_perm(&selinux_state,
1466 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001467 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1468 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001469 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Eric Parisb77a4932010-11-23 11:40:08 -05001472 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001473 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Eric Parisb77a4932010-11-23 11:40:08 -05001475 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001476 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001477 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001478
Al Viro8365a712015-12-24 00:08:06 -05001479 page = memdup_user_nul(buf, count);
1480 if (IS_ERR(page))
1481 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Eric Parisb77a4932010-11-23 11:40:08 -05001483 ret = -EINVAL;
1484 if (sscanf(page, "%u", &new_value) != 1)
1485 goto out;
1486
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001487 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490out:
Al Viro8365a712015-12-24 00:08:06 -05001491 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return ret;
1493}
1494
1495static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1496 size_t count, loff_t *ppos)
1497{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001498 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1499 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001501 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001504 if (!page)
1505 return -ENOMEM;
1506
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001507 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001508 if (length >= 0)
1509 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001511
1512 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001515static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1516 size_t count, loff_t *ppos)
1517{
1518 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1519 struct selinux_state *state = fsi->state;
1520 char *page;
1521 ssize_t length;
1522
1523 page = (char *)__get_free_page(GFP_KERNEL);
1524 if (!page)
1525 return -ENOMEM;
1526
1527 length = security_sidtab_hash_stats(state, page);
1528 if (length >= 0)
1529 length = simple_read_from_buffer(buf, count, ppos, page,
1530 length);
1531 free_page((unsigned long)page);
1532
1533 return length;
1534}
1535
1536static const struct file_operations sel_sidtab_hash_stats_ops = {
1537 .read = sel_read_sidtab_hash_stats,
1538 .llseek = generic_file_llseek,
1539};
1540
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001541static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 .read = sel_read_avc_cache_threshold,
1543 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001544 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545};
1546
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001547static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001549 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550};
1551
1552#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1553static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1554{
1555 int cpu;
1556
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301557 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (!cpu_possible(cpu))
1559 continue;
1560 *idx = cpu + 1;
1561 return &per_cpu(avc_cache_stats, cpu);
1562 }
Vasily Averin8d269a82020-02-01 10:47:47 +03001563 (*idx)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return NULL;
1565}
1566
1567static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1568{
1569 loff_t n = *pos - 1;
1570
1571 if (*pos == 0)
1572 return SEQ_START_TOKEN;
1573
1574 return sel_avc_get_stat_idx(&n);
1575}
1576
1577static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1578{
1579 return sel_avc_get_stat_idx(pos);
1580}
1581
1582static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1583{
1584 struct avc_cache_stats *st = v;
1585
Markus Elfring710a0642017-01-15 14:04:53 +01001586 if (v == SEQ_START_TOKEN) {
1587 seq_puts(seq,
1588 "lookups hits misses allocations reclaims frees\n");
1589 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001590 unsigned int lookups = st->lookups;
1591 unsigned int misses = st->misses;
1592 unsigned int hits = lookups - misses;
1593 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1594 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return 0;
1598}
1599
1600static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1601{ }
1602
Jan Engelhardt1996a102008-01-23 00:02:58 +01001603static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 .start = sel_avc_stats_seq_start,
1605 .next = sel_avc_stats_seq_next,
1606 .show = sel_avc_stats_seq_show,
1607 .stop = sel_avc_stats_seq_stop,
1608};
1609
1610static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1611{
1612 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1613}
1614
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001615static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 .open = sel_open_avc_cache_stats,
1617 .read = seq_read,
1618 .llseek = seq_lseek,
1619 .release = seq_release,
1620};
1621#endif
1622
1623static int sel_make_avc_files(struct dentry *dir)
1624{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001625 struct super_block *sb = dir->d_sb;
1626 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001627 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001628 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 { "cache_threshold",
1630 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1631 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1632#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1633 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1634#endif
1635 };
1636
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001637 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct inode *inode;
1639 struct dentry *dentry;
1640
1641 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001642 if (!dentry)
1643 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001646 if (!inode) {
1647 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001648 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001649 }
Eric Parisb77a4932010-11-23 11:40:08 -05001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001652 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 d_add(dentry, inode);
1654 }
Eric Parisb77a4932010-11-23 11:40:08 -05001655
1656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001659static int sel_make_ss_files(struct dentry *dir)
1660{
1661 struct super_block *sb = dir->d_sb;
1662 struct selinux_fs_info *fsi = sb->s_fs_info;
1663 int i;
1664 static struct tree_descr files[] = {
1665 { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1666 };
1667
1668 for (i = 0; i < ARRAY_SIZE(files); i++) {
1669 struct inode *inode;
1670 struct dentry *dentry;
1671
1672 dentry = d_alloc_name(dir, files[i].name);
1673 if (!dentry)
1674 return -ENOMEM;
1675
1676 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1677 if (!inode) {
1678 dput(dentry);
1679 return -ENOMEM;
1680 }
1681
1682 inode->i_fop = files[i].ops;
1683 inode->i_ino = ++fsi->last_ino;
1684 d_add(dentry, inode);
1685 }
1686
1687 return 0;
1688}
1689
Eric Paris18729812008-04-17 14:15:45 -04001690static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001691 size_t count, loff_t *ppos)
1692{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001693 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001694 char *con;
1695 u32 sid, len;
1696 ssize_t ret;
1697
Al Viro496ad9a2013-01-23 17:07:38 -05001698 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001699 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001700 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001701 return ret;
1702
1703 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1704 kfree(con);
1705 return ret;
1706}
1707
1708static const struct file_operations sel_initcon_ops = {
1709 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001710 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001711};
1712
1713static int sel_make_initcon_files(struct dentry *dir)
1714{
Eric Parisb77a4932010-11-23 11:40:08 -05001715 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001716
1717 for (i = 1; i <= SECINITSID_NUM; i++) {
1718 struct inode *inode;
1719 struct dentry *dentry;
Stephen Smalleye3e0b582020-02-24 11:10:23 -05001720 const char *s = security_get_initial_sid_context(i);
1721
1722 if (!s)
1723 continue;
1724 dentry = d_alloc_name(dir, s);
Eric Parisb77a4932010-11-23 11:40:08 -05001725 if (!dentry)
1726 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001727
1728 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001729 if (!inode) {
1730 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001731 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001732 }
Eric Parisb77a4932010-11-23 11:40:08 -05001733
James Carterf0ee2e42007-04-04 10:11:29 -04001734 inode->i_fop = &sel_initcon_ops;
1735 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1736 d_add(dentry, inode);
1737 }
Eric Parisb77a4932010-11-23 11:40:08 -05001738
1739 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001740}
1741
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001742static inline unsigned long sel_class_to_ino(u16 class)
1743{
1744 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1745}
1746
1747static inline u16 sel_ino_to_class(unsigned long ino)
1748{
Eric Paris92ae9e82012-04-04 13:46:46 -04001749 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001750}
1751
1752static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1753{
1754 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1755}
1756
1757static inline u32 sel_ino_to_perm(unsigned long ino)
1758{
1759 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1760}
1761
Eric Paris18729812008-04-17 14:15:45 -04001762static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001763 size_t count, loff_t *ppos)
1764{
Al Viro496ad9a2013-01-23 17:07:38 -05001765 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001766 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001767 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001768 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001769}
1770
1771static const struct file_operations sel_class_ops = {
1772 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001773 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001774};
1775
Eric Paris18729812008-04-17 14:15:45 -04001776static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001777 size_t count, loff_t *ppos)
1778{
Al Viro496ad9a2013-01-23 17:07:38 -05001779 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001780 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001781 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001782 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001783}
1784
1785static const struct file_operations sel_perm_ops = {
1786 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001787 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001788};
1789
Paul Moore3bb56b22008-01-29 08:38:19 -05001790static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1791 size_t count, loff_t *ppos)
1792{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001793 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001794 int value;
1795 char tmpbuf[TMPBUFLEN];
1796 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001797 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001798
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001799 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001800 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1801
1802 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1803}
1804
1805static const struct file_operations sel_policycap_ops = {
1806 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001807 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001808};
1809
Stephen Smalley02a52c52020-08-07 09:29:34 -04001810static int sel_make_perm_files(struct selinux_policy *newpolicy,
1811 char *objclass, int classvalue,
1812 struct dentry *dir)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001813{
Eric Parisb77a4932010-11-23 11:40:08 -05001814 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001815 char **perms;
1816
Stephen Smalley02a52c52020-08-07 09:29:34 -04001817 rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001818 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001819 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001820
1821 for (i = 0; i < nperms; i++) {
1822 struct inode *inode;
1823 struct dentry *dentry;
1824
Eric Parisb77a4932010-11-23 11:40:08 -05001825 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001826 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001827 if (!dentry)
1828 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001829
Eric Parisb77a4932010-11-23 11:40:08 -05001830 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001831 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001832 if (!inode) {
1833 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001834 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001835 }
Eric Parisb77a4932010-11-23 11:40:08 -05001836
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001837 inode->i_fop = &sel_perm_ops;
1838 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001839 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001840 d_add(dentry, inode);
1841 }
Eric Parisb77a4932010-11-23 11:40:08 -05001842 rc = 0;
1843out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001844 for (i = 0; i < nperms; i++)
1845 kfree(perms[i]);
1846 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001847 return rc;
1848}
1849
Stephen Smalley02a52c52020-08-07 09:29:34 -04001850static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
1851 char *classname, int index,
1852 struct dentry *dir)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001853{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001854 struct super_block *sb = dir->d_sb;
1855 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001856 struct dentry *dentry = NULL;
1857 struct inode *inode = NULL;
1858 int rc;
1859
1860 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001861 if (!dentry)
1862 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001863
1864 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001865 if (!inode) {
1866 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001867 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001868 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001869
1870 inode->i_fop = &sel_class_ops;
1871 inode->i_ino = sel_class_to_ino(index);
1872 d_add(dentry, inode);
1873
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001874 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001875 if (IS_ERR(dentry))
1876 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001877
Stephen Smalley02a52c52020-08-07 09:29:34 -04001878 rc = sel_make_perm_files(newpolicy, classname, index, dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001879
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001880 return rc;
1881}
1882
Stephen Smalley02a52c52020-08-07 09:29:34 -04001883static int sel_make_classes(struct selinux_fs_info *fsi,
1884 struct selinux_policy *newpolicy)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001885{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001886
Eric Parisb77a4932010-11-23 11:40:08 -05001887 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001888 char **classes;
1889
Stephen Smalley02a52c52020-08-07 09:29:34 -04001890 rc = security_get_classes(newpolicy, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001891 if (rc)
1892 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001893
1894 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001895 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001896
1897 for (i = 0; i < nclasses; i++) {
1898 struct dentry *class_name_dir;
1899
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001900 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1901 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001902 if (IS_ERR(class_name_dir)) {
1903 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001904 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001905 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001906
1907 /* i+1 since class values are 1-indexed */
Stephen Smalley02a52c52020-08-07 09:29:34 -04001908 rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001909 class_name_dir);
1910 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001911 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001912 }
Eric Parisb77a4932010-11-23 11:40:08 -05001913 rc = 0;
1914out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001915 for (i = 0; i < nclasses; i++)
1916 kfree(classes[i]);
1917 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001918 return rc;
1919}
1920
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001921static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001922{
1923 unsigned int iter;
1924 struct dentry *dentry = NULL;
1925 struct inode *inode = NULL;
1926
Paul Moore3bb56b22008-01-29 08:38:19 -05001927 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001928 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001929 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001930 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001931 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001932 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001933
1934 if (dentry == NULL)
1935 return -ENOMEM;
1936
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001937 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001938 if (inode == NULL) {
1939 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001940 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001941 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001942
1943 inode->i_fop = &sel_policycap_ops;
1944 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1945 d_add(dentry, inode);
1946 }
1947
1948 return 0;
1949}
1950
Al Viroa1c2aa12012-03-18 20:36:59 -04001951static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001952 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Al Viroa1c2aa12012-03-18 20:36:59 -04001954 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct inode *inode;
1956
Al Viroa1c2aa12012-03-18 20:36:59 -04001957 if (!dentry)
1958 return ERR_PTR(-ENOMEM);
1959
1960 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1961 if (!inode) {
1962 dput(dentry);
1963 return ERR_PTR(-ENOMEM);
1964 }
Eric Parisb77a4932010-11-23 11:40:08 -05001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 inode->i_op = &simple_dir_inode_operations;
1967 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001968 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001969 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001970 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001972 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001973 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001974
Al Viroa1c2aa12012-03-18 20:36:59 -04001975 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001978#define NULL_FILE_NAME "null"
1979
David Howells920f50b2019-03-25 16:38:30 +00001980static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001982 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int ret;
1984 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001985 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct inode_security_struct *isec;
1987
Eric Biggerscda37122017-03-25 21:15:37 -07001988 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1990 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001991 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1993 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1994 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1995 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1996 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1997 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1998 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1999 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
2000 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
2001 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04002002 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
2003 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09002004 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05002005 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05002006 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
2007 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* last one */ {""}
2009 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002010
2011 ret = selinux_fs_info_create(sb);
2012 if (ret)
2013 goto err;
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
2016 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002017 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002019 fsi = sb->s_fs_info;
2020 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
2021 if (IS_ERR(fsi->bool_dir)) {
2022 ret = PTR_ERR(fsi->bool_dir);
2023 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08002024 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Eric Parisb77a4932010-11-23 11:40:08 -05002027 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05002029 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08002030 goto err;
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 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08002034 if (!inode) {
2035 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08002036 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08002037 }
Eric Parisb77a4932010-11-23 11:40:08 -05002038
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002039 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07002040 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 isec->sid = SECINITSID_DEVNULL;
2042 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01002043 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2046 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002048 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002049 if (IS_ERR(dentry)) {
2050 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08002051 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 ret = sel_make_avc_files(dentry);
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01002055
2056 dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2057 if (IS_ERR(dentry)) {
2058 ret = PTR_ERR(dentry);
2059 goto err;
2060 }
2061
2062 ret = sel_make_ss_files(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002064 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04002065
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002066 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002067 if (IS_ERR(dentry)) {
2068 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04002069 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002070 }
James Carterf0ee2e42007-04-04 10:11:29 -04002071
2072 ret = sel_make_initcon_files(dentry);
2073 if (ret)
2074 goto err;
2075
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002076 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
2077 if (IS_ERR(fsi->class_dir)) {
2078 ret = PTR_ERR(fsi->class_dir);
2079 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002080 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002081 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002082
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002083 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
2084 &fsi->last_ino);
2085 if (IS_ERR(fsi->policycap_dir)) {
2086 ret = PTR_ERR(fsi->policycap_dir);
2087 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002088 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002089 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002090
Stephen Smalley02a52c52020-08-07 09:29:34 -04002091 ret = sel_make_policycap(fsi);
2092 if (ret) {
2093 pr_err("SELinux: failed to load policy capabilities\n");
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002094 goto err;
Stephen Smalley02a52c52020-08-07 09:29:34 -04002095 }
2096
Eric Parisb77a4932010-11-23 11:40:08 -05002097 return 0;
James Morris161ce452006-03-22 00:09:17 -08002098err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002099 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002100 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002101
2102 selinux_fs_info_free(sb);
2103
Eric Parisb77a4932010-11-23 11:40:08 -05002104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
David Howells920f50b2019-03-25 16:38:30 +00002107static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
David Howells920f50b2019-03-25 16:38:30 +00002109 return get_tree_single(fc, sel_fill_super);
2110}
2111
2112static const struct fs_context_operations sel_context_ops = {
2113 .get_tree = sel_get_tree,
2114};
2115
2116static int sel_init_fs_context(struct fs_context *fc)
2117{
2118 fc->ops = &sel_context_ops;
2119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002122static void sel_kill_sb(struct super_block *sb)
2123{
2124 selinux_fs_info_free(sb);
2125 kill_litter_super(sb);
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static struct file_system_type sel_fs_type = {
2129 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002130 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002131 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132};
2133
2134struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002135struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137static int __init init_sel_fs(void)
2138{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002139 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2140 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 int err;
2142
Stephen Smalley6c5a6822019-12-17 09:15:10 -05002143 if (!selinux_enabled_boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002145
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002146 err = sysfs_create_mount_point(fs_kobj, "selinux");
2147 if (err)
2148 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002151 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002152 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002153 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002154 }
Eric Parisb77a4932010-11-23 11:40:08 -05002155
Al Viro765927b2012-06-26 21:58:53 +04002156 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002157 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002158 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002159 err = PTR_ERR(selinuxfs_mount);
2160 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002162 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2163 &null_name);
2164 if (IS_ERR(selinux_null.dentry)) {
2165 pr_err("selinuxfs: could not lookup null!\n");
2166 err = PTR_ERR(selinux_null.dentry);
2167 selinux_null.dentry = NULL;
2168 }
Eric Parisb77a4932010-11-23 11:40:08 -05002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return err;
2171}
2172
2173__initcall(init_sel_fs);
2174
2175#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2176void exit_sel_fs(void)
2177{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002178 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002179 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002180 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 unregister_filesystem(&sel_fs_type);
2182}
2183#endif