blob: 2a0e8b5f19d5b6ab6d6232ca3eef35386da36f86 [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
Daniel Burgener613ba182020-08-19 15:59:34 -0400120#define BOOL_DIR_NAME "booleans"
121#define CLASS_DIR_NAME "class"
122#define POLICYCAP_DIR_NAME "policy_capabilities"
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TMPBUFLEN 12
125static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
126 size_t count, loff_t *ppos)
127{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400128 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char tmpbuf[TMPBUFLEN];
130 ssize_t length;
131
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500132 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400133 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
135}
136
137#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400138static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 size_t count, loff_t *ppos)
140
141{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400142 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
143 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500144 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500146 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Davi Arnautbfd51622005-10-30 14:59:24 -0800148 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500149 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500150
151 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500152 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500153 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500154
Al Viro8365a712015-12-24 00:08:06 -0500155 page = memdup_user_nul(buf, count);
156 if (IS_ERR(page))
157 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 length = -EINVAL;
160 if (sscanf(page, "%d", &new_value) != 1)
161 goto out;
162
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500163 new_value = !!new_value;
164
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400165 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500166 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500167 length = avc_has_perm(&selinux_state,
168 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500169 SECCLASS_SECURITY, SECURITY__SETENFORCE,
170 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (length)
172 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400173 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400174 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500175 " enabled=1 old-enabled=1 lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500176 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700177 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500178 audit_get_sessionid(current));
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400179 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500181 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500182 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400183 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 if (!new_value)
Janne Karhunen42df7442019-06-14 15:20:14 +0300185 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 length = count;
188out:
Al Viro8365a712015-12-24 00:08:06 -0500189 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return length;
191}
192#else
193#define sel_write_enforce NULL
194#endif
195
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800196static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .read = sel_read_enforce,
198 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200199 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Eric Paris3f120702007-09-21 14:37:10 -0400202static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
203 size_t count, loff_t *ppos)
204{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400205 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
206 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400207 char tmpbuf[TMPBUFLEN];
208 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500209 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400210 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400211 security_get_reject_unknown(state) :
212 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400213
214 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
215 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
216}
217
218static const struct file_operations sel_handle_unknown_ops = {
219 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200220 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400221};
222
KaiGai Kohei11904162010-09-14 18:28:39 +0900223static int sel_open_handle_status(struct inode *inode, struct file *filp)
224{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400225 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
226 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900227
228 if (!status)
229 return -ENOMEM;
230
231 filp->private_data = status;
232
233 return 0;
234}
235
236static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
237 size_t count, loff_t *ppos)
238{
239 struct page *status = filp->private_data;
240
241 BUG_ON(!status);
242
243 return simple_read_from_buffer(buf, count, ppos,
244 page_address(status),
245 sizeof(struct selinux_kernel_status));
246}
247
248static int sel_mmap_handle_status(struct file *filp,
249 struct vm_area_struct *vma)
250{
251 struct page *status = filp->private_data;
252 unsigned long size = vma->vm_end - vma->vm_start;
253
254 BUG_ON(!status);
255
256 /* only allows one page from the head */
257 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
258 return -EIO;
259 /* disallow writable mapping */
260 if (vma->vm_flags & VM_WRITE)
261 return -EPERM;
262 /* disallow mprotect() turns it into writable */
263 vma->vm_flags &= ~VM_MAYWRITE;
264
265 return remap_pfn_range(vma, vma->vm_start,
266 page_to_pfn(status),
267 size, vma->vm_page_prot);
268}
269
270static const struct file_operations sel_handle_status_ops = {
271 .open = sel_open_handle_status,
272 .read = sel_read_handle_status,
273 .mmap = sel_mmap_handle_status,
274 .llseek = generic_file_llseek,
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400278static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 size_t count, loff_t *ppos)
280
281{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400282 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500283 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ssize_t length;
285 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400286 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Paul Moore89b223b2019-12-18 21:45:08 -0500288 /* NOTE: we are now officially considering runtime disable as
289 * deprecated, and using it will become increasingly painful
290 * (e.g. sleeping/blocking) as we progress through future
291 * kernel releases until eventually it is removed
292 */
293 pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n");
294
Davi Arnautbfd51622005-10-30 14:59:24 -0800295 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500296 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500297
298 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500299 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500300 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500301
Al Viro8365a712015-12-24 00:08:06 -0500302 page = memdup_user_nul(buf, count);
303 if (IS_ERR(page))
304 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 length = -EINVAL;
307 if (sscanf(page, "%d", &new_value) != 1)
308 goto out;
309
310 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400311 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400312 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500313 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400315 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400316 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500317 " enabled=0 old-enabled=1 lsm=selinux res=1",
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400318 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700319 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500320 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 length = count;
324out:
Al Viro8365a712015-12-24 00:08:06 -0500325 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return length;
327}
328#else
329#define sel_write_disable NULL
330#endif
331
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800332static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200334 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400338 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 char tmpbuf[TMPBUFLEN];
341 ssize_t length;
342
343 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
344 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
345}
346
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800347static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200349 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352/* declaration for sel_write_load */
Daniel Burgener66ec3842020-08-19 15:59:33 -0400353static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
354 unsigned int *bool_num, char ***bool_pending_names,
355 unsigned int **bool_pending_values);
356static int sel_make_classes(struct selinux_policy *newpolicy,
357 struct dentry *class_dir,
358 unsigned long *last_class_ino);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400359
360/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400361static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400362 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400364/* declaration for sel_remove_old_policy_nodes */
365static void sel_remove_entries(struct dentry *de);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static ssize_t sel_read_mls(struct file *filp, char __user *buf,
368 size_t count, loff_t *ppos)
369{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400370 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 char tmpbuf[TMPBUFLEN];
372 ssize_t length;
373
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100374 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400375 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
377}
378
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800379static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200381 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Eric Pariscee74f42010-10-13 17:50:25 -0400384struct policy_load_memory {
385 size_t len;
386 void *data;
387};
388
389static int sel_open_policy(struct inode *inode, struct file *filp)
390{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400391 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
392 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400393 struct policy_load_memory *plm = NULL;
394 int rc;
395
396 BUG_ON(filp->private_data);
397
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400398 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400399
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500400 rc = avc_has_perm(&selinux_state,
401 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500402 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400403 if (rc)
404 goto err;
405
406 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400407 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400408 goto err;
409
410 rc = -ENOMEM;
411 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
412 if (!plm)
413 goto err;
414
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400415 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500416 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400417 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500418 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400419 }
420
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400421 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400422 if (rc)
423 goto err;
424
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400425 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400426
427 filp->private_data = plm;
428
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400429 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400430
431 return 0;
432err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400433 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400434
435 if (plm)
436 vfree(plm->data);
437 kfree(plm);
438 return rc;
439}
440
441static int sel_release_policy(struct inode *inode, struct file *filp)
442{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400443 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400444 struct policy_load_memory *plm = filp->private_data;
445
446 BUG_ON(!plm);
447
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400448 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400449
450 vfree(plm->data);
451 kfree(plm);
452
453 return 0;
454}
455
456static ssize_t sel_read_policy(struct file *filp, char __user *buf,
457 size_t count, loff_t *ppos)
458{
459 struct policy_load_memory *plm = filp->private_data;
460 int ret;
461
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500462 ret = avc_has_perm(&selinux_state,
463 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500464 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400465 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400466 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400467
Jann Horn0da74122018-06-28 20:39:54 -0400468 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400469}
470
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530471static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400472{
Dave Jiang11bac802017-02-24 14:56:41 -0800473 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400474 unsigned long offset;
475 struct page *page;
476
477 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
478 return VM_FAULT_SIGBUS;
479
480 offset = vmf->pgoff << PAGE_SHIFT;
481 if (offset >= roundup(plm->len, PAGE_SIZE))
482 return VM_FAULT_SIGBUS;
483
484 page = vmalloc_to_page(plm->data + offset);
485 get_page(page);
486
487 vmf->page = page;
488
489 return 0;
490}
491
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700492static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400493 .fault = sel_mmap_policy_fault,
494 .page_mkwrite = sel_mmap_policy_fault,
495};
496
James Morrisad3fa082011-08-30 10:50:12 +1000497static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400498{
499 if (vma->vm_flags & VM_SHARED) {
500 /* do not allow mprotect to make mapping writable */
501 vma->vm_flags &= ~VM_MAYWRITE;
502
503 if (vma->vm_flags & VM_WRITE)
504 return -EACCES;
505 }
506
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700507 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400508 vma->vm_ops = &sel_mmap_policy_ops;
509
510 return 0;
511}
512
Eric Pariscee74f42010-10-13 17:50:25 -0400513static const struct file_operations sel_policy_ops = {
514 .open = sel_open_policy,
515 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400516 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400517 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500518 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400519};
520
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400521static void sel_remove_old_policy_nodes(struct selinux_fs_info *fsi)
522{
523 u32 i;
524
525 /* bool_dir cleanup */
526 for (i = 0; i < fsi->bool_num; i++)
527 kfree(fsi->bool_pending_names[i]);
528 kfree(fsi->bool_pending_names);
529 kfree(fsi->bool_pending_values);
530 fsi->bool_num = 0;
531 fsi->bool_pending_names = NULL;
532 fsi->bool_pending_values = NULL;
533
534 sel_remove_entries(fsi->bool_dir);
535
536 /* class_dir cleanup */
537 sel_remove_entries(fsi->class_dir);
538
539}
540
Stephen Smalley02a52c52020-08-07 09:29:34 -0400541static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
542 struct selinux_policy *newpolicy)
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400543{
544 int ret;
545
Daniel Burgeneraeecf4a2020-08-19 15:59:32 -0400546 sel_remove_old_policy_nodes(fsi);
547
Daniel Burgener66ec3842020-08-19 15:59:33 -0400548 ret = sel_make_bools(newpolicy, fsi->bool_dir, &fsi->bool_num,
549 &fsi->bool_pending_names, &fsi->bool_pending_values);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400550 if (ret) {
551 pr_err("SELinux: failed to load policy booleans\n");
552 return ret;
553 }
554
Daniel Burgener66ec3842020-08-19 15:59:33 -0400555 ret = sel_make_classes(newpolicy, fsi->class_dir,
556 &fsi->last_class_ino);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400557 if (ret) {
558 pr_err("SELinux: failed to load policy classes\n");
559 return ret;
560 }
561
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400562 return 0;
563}
564
Eric Paris18729812008-04-17 14:15:45 -0400565static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 size_t count, loff_t *ppos)
567
568{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400569 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400570 struct selinux_policy *newpolicy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ssize_t length;
572 void *data = NULL;
573
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400574 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500576 length = avc_has_perm(&selinux_state,
577 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500578 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (length)
580 goto out;
581
Eric Parisb77a4932010-11-23 11:40:08 -0500582 /* No partial writes. */
583 length = -EINVAL;
584 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric Parisb77a4932010-11-23 11:40:08 -0500587 length = -ENOMEM;
588 data = vmalloc(count);
589 if (!data)
590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 length = -EFAULT;
593 if (copy_from_user(data, buf, count) != 0)
594 goto out;
595
Stephen Smalley02a52c52020-08-07 09:29:34 -0400596 length = security_load_policy(fsi->state, data, count, &newpolicy);
Gary Tierney4262fb52017-01-09 10:07:31 -0500597 if (length) {
598 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Stephen Smalley02a52c52020-08-07 09:29:34 -0400602 length = sel_make_policy_nodes(fsi, newpolicy);
603 if (length) {
604 selinux_policy_cancel(fsi->state, newpolicy);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400605 goto out1;
Stephen Smalley02a52c52020-08-07 09:29:34 -0400606 }
607
608 selinux_policy_commit(fsi->state, newpolicy);
Eric Parisb77a4932010-11-23 11:40:08 -0500609
610 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400611
612out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400613 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400614 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700615 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500616 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400618 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 vfree(data);
620 return length;
621}
622
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800623static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200625 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626};
627
Eric Paris18729812008-04-17 14:15:45 -0400628static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400630 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
631 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500632 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800633 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 ssize_t length;
635
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500636 length = avc_has_perm(&selinux_state,
637 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500638 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400642 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500643 if (length)
644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400646 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500647 if (length)
648 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800649
Eric Parisb77a4932010-11-23 11:40:08 -0500650 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800651 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200652 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400653 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800654 goto out;
655 }
656
657 memcpy(buf, canon, len);
658 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800660 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return length;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
665 size_t count, loff_t *ppos)
666{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400667 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 char tmpbuf[TMPBUFLEN];
669 ssize_t length;
670
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400671 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
673}
674
Eric Paris18729812008-04-17 14:15:45 -0400675static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 size_t count, loff_t *ppos)
677{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400678 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500679 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ssize_t length;
681 unsigned int new_value;
682
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500683 length = avc_has_perm(&selinux_state,
684 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500685 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
686 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500688 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Davi Arnautbfd51622005-10-30 14:59:24 -0800690 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500691 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500692
693 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500694 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500695 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500696
Al Viro8365a712015-12-24 00:08:06 -0500697 page = memdup_user_nul(buf, count);
698 if (IS_ERR(page))
699 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 length = -EINVAL;
702 if (sscanf(page, "%u", &new_value) != 1)
703 goto out;
704
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500705 if (new_value) {
706 char comm[sizeof(current->comm)];
707
708 memcpy(comm, current->comm, sizeof(comm));
709 pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
710 comm, current->pid);
711 }
712
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400713 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 length = count;
715out:
Al Viro8365a712015-12-24 00:08:06 -0500716 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return length;
718}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800719static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .read = sel_read_checkreqprot,
721 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200722 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
724
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500725static ssize_t sel_write_validatetrans(struct file *file,
726 const char __user *buf,
727 size_t count, loff_t *ppos)
728{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400729 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
730 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500731 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
732 char *req = NULL;
733 u32 osid, nsid, tsid;
734 u16 tclass;
735 int rc;
736
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500737 rc = avc_has_perm(&selinux_state,
738 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500739 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500740 if (rc)
741 goto out;
742
743 rc = -ENOMEM;
744 if (count >= PAGE_SIZE)
745 goto out;
746
747 /* No partial writes. */
748 rc = -EINVAL;
749 if (*ppos != 0)
750 goto out;
751
Al Viro0b884d22017-05-13 18:12:07 -0400752 req = memdup_user_nul(buf, count);
753 if (IS_ERR(req)) {
754 rc = PTR_ERR(req);
755 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500756 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400757 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500758
759 rc = -ENOMEM;
760 oldcon = kzalloc(count + 1, GFP_KERNEL);
761 if (!oldcon)
762 goto out;
763
764 newcon = kzalloc(count + 1, GFP_KERNEL);
765 if (!newcon)
766 goto out;
767
768 taskcon = kzalloc(count + 1, GFP_KERNEL);
769 if (!taskcon)
770 goto out;
771
772 rc = -EINVAL;
773 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
774 goto out;
775
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400776 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500777 if (rc)
778 goto out;
779
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400780 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500781 if (rc)
782 goto out;
783
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400784 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500785 if (rc)
786 goto out;
787
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400788 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500789 if (!rc)
790 rc = count;
791out:
792 kfree(req);
793 kfree(oldcon);
794 kfree(newcon);
795 kfree(taskcon);
796 return rc;
797}
798
799static const struct file_operations sel_transition_ops = {
800 .write = sel_write_validatetrans,
801 .llseek = generic_file_llseek,
802};
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/*
805 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
806 */
Eric Paris18729812008-04-17 14:15:45 -0400807static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
808static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
809static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
810static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
811static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Eric Biggers631d2b42018-07-17 10:43:56 -0700813static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 [SEL_ACCESS] = sel_write_access,
815 [SEL_CREATE] = sel_write_create,
816 [SEL_RELABEL] = sel_write_relabel,
817 [SEL_USER] = sel_write_user,
818 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800819 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
822static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
823{
Al Viro496ad9a2013-01-23 17:07:38 -0500824 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 char *data;
826 ssize_t rv;
827
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800828 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EINVAL;
830
831 data = simple_transaction_get(file, buf, size);
832 if (IS_ERR(data))
833 return PTR_ERR(data);
834
Eric Paris18729812008-04-17 14:15:45 -0400835 rv = write_op[ino](file, data, size);
836 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 simple_transaction_set(file, rv);
838 rv = size;
839 }
840 return rv;
841}
842
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800843static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .write = selinux_transaction_write,
845 .read = simple_transaction_read,
846 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200847 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
850/*
851 * payload - write methods
852 * If the method has a response, the response should be put in buf,
853 * and the length returned. Otherwise return 0 or and -error.
854 */
855
Eric Paris18729812008-04-17 14:15:45 -0400856static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400858 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
859 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500860 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 u32 ssid, tsid;
862 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct av_decision avd;
864 ssize_t length;
865
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500866 length = avc_has_perm(&selinux_state,
867 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500868 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800873 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Eric Parisb77a4932010-11-23 11:40:08 -0500877 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800878 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!tcon)
880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500883 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500884 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400886 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500887 if (length)
888 goto out;
889
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400890 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500891 if (length)
892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400894 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900897 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500898 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900900 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901out:
Eric Parisb77a4932010-11-23 11:40:08 -0500902 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 kfree(scon);
904 return length;
905}
906
Eric Paris18729812008-04-17 14:15:45 -0400907static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400909 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
910 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500911 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100912 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 u32 ssid, tsid, newsid;
914 u16 tclass;
915 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500916 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100918 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500920 length = avc_has_perm(&selinux_state,
921 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500922 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
923 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800928 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Parisb77a4932010-11-23 11:40:08 -0500932 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800933 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!tcon)
935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100937 length = -ENOMEM;
938 namebuf = kzalloc(size + 1, GFP_KERNEL);
939 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100942 length = -EINVAL;
943 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
944 if (nargs < 3 || nargs > 4)
945 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400946 if (nargs == 4) {
947 /*
948 * If and when the name of new object to be queried contains
949 * either whitespace or multibyte characters, they shall be
950 * encoded based on the percentage-encoding rule.
951 * If not encoded, the sscanf logic picks up only left-half
952 * of the supplied name; splitted by a whitespace unexpectedly.
953 */
954 char *r, *w;
955 int c1, c2;
956
957 r = w = namebuf;
958 do {
959 c1 = *r++;
960 if (c1 == '+')
961 c1 = ' ';
962 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800963 c1 = hex_to_bin(*r++);
964 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400965 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800966 c2 = hex_to_bin(*r++);
967 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400968 goto out;
969 c1 = (c1 << 4) | c2;
970 }
971 *w++ = c1;
972 } while (c1 != '\0');
973
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100974 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400975 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100976
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400977 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500978 if (length)
979 goto out;
980
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400981 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500982 if (length)
983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400985 length = security_transition_sid_user(state, ssid, tsid, tclass,
986 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500987 if (length)
988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400990 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500991 if (length)
992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Eric Parisb77a4932010-11-23 11:40:08 -0500994 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200996 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400997 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001 memcpy(buf, newcon, len);
1002 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003out:
Eric Parisb77a4932010-11-23 11:40:08 -05001004 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001005 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -05001006 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree(scon);
1008 return length;
1009}
1010
Eric Paris18729812008-04-17 14:15:45 -04001011static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001013 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1014 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001015 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 u32 ssid, tsid, newsid;
1017 u16 tclass;
1018 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001019 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 u32 len;
1021
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001022 length = avc_has_perm(&selinux_state,
1023 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001024 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1025 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001030 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -05001032 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Eric Parisb77a4932010-11-23 11:40:08 -05001034 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001035 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!tcon)
1037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 length = -EINVAL;
1040 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001043 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001044 if (length)
1045 goto out;
1046
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001047 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001048 if (length)
1049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001051 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001052 if (length)
1053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001055 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001056 if (length)
1057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric Parisb77a4932010-11-23 11:40:08 -05001059 length = -ERANGE;
1060 if (len > SIMPLE_TRANSACTION_LIMIT)
1061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 memcpy(buf, newcon, len);
1064 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065out:
Eric Parisb77a4932010-11-23 11:40:08 -05001066 kfree(newcon);
1067 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree(scon);
1069 return length;
1070}
1071
Eric Paris18729812008-04-17 14:15:45 -04001072static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001074 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1075 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001076 char *con = NULL, *user = NULL, *ptr;
1077 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ssize_t length;
1079 char *newcon;
1080 int i, rc;
1081 u32 len, nsids;
1082
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001083 length = avc_has_perm(&selinux_state,
1084 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001085 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1086 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001088 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001091 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Eric Parisb77a4932010-11-23 11:40:08 -05001095 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001096 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!user)
1098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 length = -EINVAL;
1101 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001104 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001105 if (length)
1106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001108 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001109 if (length)
1110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 length = sprintf(buf, "%u", nsids) + 1;
1113 ptr = buf + length;
1114 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001115 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (rc) {
1117 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1121 kfree(newcon);
1122 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 memcpy(ptr, newcon, len);
1126 kfree(newcon);
1127 ptr += len;
1128 length += len;
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130out:
Eric Parisb77a4932010-11-23 11:40:08 -05001131 kfree(sids);
1132 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 kfree(con);
1134 return length;
1135}
1136
Eric Paris18729812008-04-17 14:15:45 -04001137static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001139 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1140 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001141 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 u32 ssid, tsid, newsid;
1143 u16 tclass;
1144 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001145 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 u32 len;
1147
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001148 length = avc_has_perm(&selinux_state,
1149 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001150 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1151 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001156 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Eric Parisb77a4932010-11-23 11:40:08 -05001160 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001161 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!tcon)
1163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 length = -EINVAL;
1166 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001169 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001170 if (length)
1171 goto out;
1172
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001173 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001174 if (length)
1175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001177 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001178 if (length)
1179 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001181 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001182 if (length)
1183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Eric Parisb77a4932010-11-23 11:40:08 -05001185 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001187 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001188 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
1192 memcpy(buf, newcon, len);
1193 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194out:
Eric Parisb77a4932010-11-23 11:40:08 -05001195 kfree(newcon);
1196 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 kfree(scon);
1198 return length;
1199}
1200
1201static struct inode *sel_make_inode(struct super_block *sb, int mode)
1202{
1203 struct inode *ret = new_inode(sb);
1204
1205 if (ret) {
1206 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001207 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 return ret;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1213 size_t count, loff_t *ppos)
1214{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001215 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 char *page = NULL;
1217 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ssize_t ret;
1219 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001220 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001221 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001223 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Eric Parisb77a4932010-11-23 11:40:08 -05001225 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001226 if (index >= fsi->bool_num || strcmp(name,
1227 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001228 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Eric Parisb77a4932010-11-23 11:40:08 -05001230 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001231 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001232 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001233 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001235 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (cur_enforcing < 0) {
1237 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001238 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001241 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001242 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001243 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1244out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001245 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001247
1248out_unlock:
1249 mutex_unlock(&fsi->mutex);
1250 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1254 size_t count, loff_t *ppos)
1255{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001256 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001258 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001260 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001261 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jann Horn0da74122018-06-28 20:39:54 -04001263 if (count >= PAGE_SIZE)
1264 return -ENOMEM;
1265
1266 /* No partial writes. */
1267 if (*ppos != 0)
1268 return -EINVAL;
1269
1270 page = memdup_user_nul(buf, count);
1271 if (IS_ERR(page))
1272 return PTR_ERR(page);
1273
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001274 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001276 length = avc_has_perm(&selinux_state,
1277 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001278 SECCLASS_SECURITY, SECURITY__SETBOOL,
1279 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (length)
1281 goto out;
1282
Eric Parisb77a4932010-11-23 11:40:08 -05001283 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001284 if (index >= fsi->bool_num || strcmp(name,
1285 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001286 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 length = -EINVAL;
1289 if (sscanf(page, "%d", &new_value) != 1)
1290 goto out;
1291
1292 if (new_value)
1293 new_value = 1;
1294
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001295 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 length = count;
1297
1298out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001299 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001300 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return length;
1302}
1303
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001304static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001305 .read = sel_read_bool,
1306 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001307 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308};
1309
1310static ssize_t sel_commit_bools_write(struct file *filep,
1311 const char __user *buf,
1312 size_t count, loff_t *ppos)
1313{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001314 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001316 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int new_value;
1318
Jann Horn0da74122018-06-28 20:39:54 -04001319 if (count >= PAGE_SIZE)
1320 return -ENOMEM;
1321
1322 /* No partial writes. */
1323 if (*ppos != 0)
1324 return -EINVAL;
1325
1326 page = memdup_user_nul(buf, count);
1327 if (IS_ERR(page))
1328 return PTR_ERR(page);
1329
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001330 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001332 length = avc_has_perm(&selinux_state,
1333 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001334 SECCLASS_SECURITY, SECURITY__SETBOOL,
1335 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (length)
1337 goto out;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 length = -EINVAL;
1340 if (sscanf(page, "%d", &new_value) != 1)
1341 goto out;
1342
Eric Parisb77a4932010-11-23 11:40:08 -05001343 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001344 if (new_value && fsi->bool_pending_values)
1345 length = security_set_bools(fsi->state, fsi->bool_num,
1346 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Eric Parisb77a4932010-11-23 11:40:08 -05001348 if (!length)
1349 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001352 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001353 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return length;
1355}
1356
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001357static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001358 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001359 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360};
1361
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001362static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Al Viroad521842014-12-24 14:56:48 -05001364 d_genocide(de);
1365 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Daniel Burgener66ec3842020-08-19 15:59:33 -04001368static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
1369 unsigned int *bool_num, char ***bool_pending_names,
1370 unsigned int **bool_pending_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001372 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 ssize_t len;
1374 struct dentry *dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 struct inode *inode = NULL;
1376 struct inode_security_struct *isec;
1377 char **names = NULL, *page;
Ondrej Mosnacek60abd312020-02-03 12:27:20 +01001378 u32 i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int *values = NULL;
1380 u32 sid;
1381
Eric Parisb77a4932010-11-23 11:40:08 -05001382 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001383 page = (char *)get_zeroed_page(GFP_KERNEL);
1384 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Stephen Smalley02a52c52020-08-07 09:29:34 -04001387 ret = security_get_bools(newpolicy, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001388 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto out;
1390
1391 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001392 ret = -ENOMEM;
Daniel Burgener66ec3842020-08-19 15:59:33 -04001393 dentry = d_alloc_name(bool_dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001394 if (!dentry)
1395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Eric Parisb77a4932010-11-23 11:40:08 -05001397 ret = -ENOMEM;
Daniel Burgener66ec3842020-08-19 15:59:33 -04001398 inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001399 if (!inode) {
1400 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001401 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001402 }
Eric Parisb77a4932010-11-23 11:40:08 -05001403
Eric Parisb77a4932010-11-23 11:40:08 -05001404 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001405 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001406 if (len >= PAGE_SIZE) {
1407 dput(dentry);
1408 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001409 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001410 }
Eric Parisb77a4932010-11-23 11:40:08 -05001411
Casey Schaufler80788c22018-09-21 17:19:11 -07001412 isec = selinux_inode(inode);
Stephen Smalley02a52c52020-08-07 09:29:34 -04001413 ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001414 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001415 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001416 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1417 page);
1418 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001419 }
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001422 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001424 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 d_add(dentry, inode);
1426 }
Daniel Burgener66ec3842020-08-19 15:59:33 -04001427 *bool_num = num;
1428 *bool_pending_names = names;
1429 *bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001430
1431 free_page((unsigned long)page);
1432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433out:
1434 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001437 for (i = 0; i < num; i++)
1438 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(names);
1440 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001441 kfree(values);
Daniel Burgener66ec3842020-08-19 15:59:33 -04001442 sel_remove_entries(bool_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001443
1444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1448 size_t count, loff_t *ppos)
1449{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001450 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1451 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 char tmpbuf[TMPBUFLEN];
1453 ssize_t length;
1454
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001455 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1456 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1458}
1459
Eric Paris18729812008-04-17 14:15:45 -04001460static ssize_t sel_write_avc_cache_threshold(struct file *file,
1461 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 size_t count, loff_t *ppos)
1463
1464{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001465 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1466 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001467 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001469 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001471 ret = avc_has_perm(&selinux_state,
1472 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001473 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1474 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001475 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Eric Parisb77a4932010-11-23 11:40:08 -05001478 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric Parisb77a4932010-11-23 11:40:08 -05001481 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001482 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001483 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001484
Al Viro8365a712015-12-24 00:08:06 -05001485 page = memdup_user_nul(buf, count);
1486 if (IS_ERR(page))
1487 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Eric Parisb77a4932010-11-23 11:40:08 -05001489 ret = -EINVAL;
1490 if (sscanf(page, "%u", &new_value) != 1)
1491 goto out;
1492
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001493 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out:
Al Viro8365a712015-12-24 00:08:06 -05001497 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return ret;
1499}
1500
1501static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1502 size_t count, loff_t *ppos)
1503{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001504 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1505 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001507 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001510 if (!page)
1511 return -ENOMEM;
1512
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001513 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001514 if (length >= 0)
1515 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001517
1518 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001521static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1522 size_t count, loff_t *ppos)
1523{
1524 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1525 struct selinux_state *state = fsi->state;
1526 char *page;
1527 ssize_t length;
1528
1529 page = (char *)__get_free_page(GFP_KERNEL);
1530 if (!page)
1531 return -ENOMEM;
1532
1533 length = security_sidtab_hash_stats(state, page);
1534 if (length >= 0)
1535 length = simple_read_from_buffer(buf, count, ppos, page,
1536 length);
1537 free_page((unsigned long)page);
1538
1539 return length;
1540}
1541
1542static const struct file_operations sel_sidtab_hash_stats_ops = {
1543 .read = sel_read_sidtab_hash_stats,
1544 .llseek = generic_file_llseek,
1545};
1546
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001547static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 .read = sel_read_avc_cache_threshold,
1549 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001550 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551};
1552
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001553static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001555 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556};
1557
1558#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1559static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1560{
1561 int cpu;
1562
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301563 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (!cpu_possible(cpu))
1565 continue;
1566 *idx = cpu + 1;
1567 return &per_cpu(avc_cache_stats, cpu);
1568 }
Vasily Averin8d269a82020-02-01 10:47:47 +03001569 (*idx)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return NULL;
1571}
1572
1573static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1574{
1575 loff_t n = *pos - 1;
1576
1577 if (*pos == 0)
1578 return SEQ_START_TOKEN;
1579
1580 return sel_avc_get_stat_idx(&n);
1581}
1582
1583static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1584{
1585 return sel_avc_get_stat_idx(pos);
1586}
1587
1588static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1589{
1590 struct avc_cache_stats *st = v;
1591
Markus Elfring710a0642017-01-15 14:04:53 +01001592 if (v == SEQ_START_TOKEN) {
1593 seq_puts(seq,
1594 "lookups hits misses allocations reclaims frees\n");
1595 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001596 unsigned int lookups = st->lookups;
1597 unsigned int misses = st->misses;
1598 unsigned int hits = lookups - misses;
1599 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1600 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
1604}
1605
1606static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1607{ }
1608
Jan Engelhardt1996a102008-01-23 00:02:58 +01001609static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 .start = sel_avc_stats_seq_start,
1611 .next = sel_avc_stats_seq_next,
1612 .show = sel_avc_stats_seq_show,
1613 .stop = sel_avc_stats_seq_stop,
1614};
1615
1616static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1617{
1618 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1619}
1620
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001621static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 .open = sel_open_avc_cache_stats,
1623 .read = seq_read,
1624 .llseek = seq_lseek,
1625 .release = seq_release,
1626};
1627#endif
1628
1629static int sel_make_avc_files(struct dentry *dir)
1630{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001631 struct super_block *sb = dir->d_sb;
1632 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001633 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001634 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 { "cache_threshold",
1636 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1637 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1638#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1639 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1640#endif
1641 };
1642
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001643 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct inode *inode;
1645 struct dentry *dentry;
1646
1647 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001648 if (!dentry)
1649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001652 if (!inode) {
1653 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001654 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001655 }
Eric Parisb77a4932010-11-23 11:40:08 -05001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001658 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 d_add(dentry, inode);
1660 }
Eric Parisb77a4932010-11-23 11:40:08 -05001661
1662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01001665static int sel_make_ss_files(struct dentry *dir)
1666{
1667 struct super_block *sb = dir->d_sb;
1668 struct selinux_fs_info *fsi = sb->s_fs_info;
1669 int i;
1670 static struct tree_descr files[] = {
1671 { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1672 };
1673
1674 for (i = 0; i < ARRAY_SIZE(files); i++) {
1675 struct inode *inode;
1676 struct dentry *dentry;
1677
1678 dentry = d_alloc_name(dir, files[i].name);
1679 if (!dentry)
1680 return -ENOMEM;
1681
1682 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1683 if (!inode) {
1684 dput(dentry);
1685 return -ENOMEM;
1686 }
1687
1688 inode->i_fop = files[i].ops;
1689 inode->i_ino = ++fsi->last_ino;
1690 d_add(dentry, inode);
1691 }
1692
1693 return 0;
1694}
1695
Eric Paris18729812008-04-17 14:15:45 -04001696static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001697 size_t count, loff_t *ppos)
1698{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001699 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001700 char *con;
1701 u32 sid, len;
1702 ssize_t ret;
1703
Al Viro496ad9a2013-01-23 17:07:38 -05001704 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001705 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001706 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001707 return ret;
1708
1709 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1710 kfree(con);
1711 return ret;
1712}
1713
1714static const struct file_operations sel_initcon_ops = {
1715 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001716 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001717};
1718
1719static int sel_make_initcon_files(struct dentry *dir)
1720{
Eric Parisb77a4932010-11-23 11:40:08 -05001721 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001722
1723 for (i = 1; i <= SECINITSID_NUM; i++) {
1724 struct inode *inode;
1725 struct dentry *dentry;
Stephen Smalleye3e0b582020-02-24 11:10:23 -05001726 const char *s = security_get_initial_sid_context(i);
1727
1728 if (!s)
1729 continue;
1730 dentry = d_alloc_name(dir, s);
Eric Parisb77a4932010-11-23 11:40:08 -05001731 if (!dentry)
1732 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001733
1734 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001735 if (!inode) {
1736 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001737 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001738 }
Eric Parisb77a4932010-11-23 11:40:08 -05001739
James Carterf0ee2e42007-04-04 10:11:29 -04001740 inode->i_fop = &sel_initcon_ops;
1741 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1742 d_add(dentry, inode);
1743 }
Eric Parisb77a4932010-11-23 11:40:08 -05001744
1745 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001746}
1747
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001748static inline unsigned long sel_class_to_ino(u16 class)
1749{
1750 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1751}
1752
1753static inline u16 sel_ino_to_class(unsigned long ino)
1754{
Eric Paris92ae9e82012-04-04 13:46:46 -04001755 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001756}
1757
1758static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1759{
1760 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1761}
1762
1763static inline u32 sel_ino_to_perm(unsigned long ino)
1764{
1765 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1766}
1767
Eric Paris18729812008-04-17 14:15:45 -04001768static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001769 size_t count, loff_t *ppos)
1770{
Al Viro496ad9a2013-01-23 17:07:38 -05001771 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001772 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001773 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001774 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001775}
1776
1777static const struct file_operations sel_class_ops = {
1778 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001779 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001780};
1781
Eric Paris18729812008-04-17 14:15:45 -04001782static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001783 size_t count, loff_t *ppos)
1784{
Al Viro496ad9a2013-01-23 17:07:38 -05001785 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001786 char res[TMPBUFLEN];
liuyang347e78c872020-01-07 09:39:18 +08001787 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
Al Virocc1dad72012-04-02 19:40:47 -04001788 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001789}
1790
1791static const struct file_operations sel_perm_ops = {
1792 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001793 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001794};
1795
Paul Moore3bb56b22008-01-29 08:38:19 -05001796static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1797 size_t count, loff_t *ppos)
1798{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001799 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001800 int value;
1801 char tmpbuf[TMPBUFLEN];
1802 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001803 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001804
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001805 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001806 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1807
1808 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1809}
1810
1811static const struct file_operations sel_policycap_ops = {
1812 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001813 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001814};
1815
Stephen Smalley02a52c52020-08-07 09:29:34 -04001816static int sel_make_perm_files(struct selinux_policy *newpolicy,
1817 char *objclass, int classvalue,
1818 struct dentry *dir)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001819{
Eric Parisb77a4932010-11-23 11:40:08 -05001820 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001821 char **perms;
1822
Stephen Smalley02a52c52020-08-07 09:29:34 -04001823 rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001824 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001825 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001826
1827 for (i = 0; i < nperms; i++) {
1828 struct inode *inode;
1829 struct dentry *dentry;
1830
Eric Parisb77a4932010-11-23 11:40:08 -05001831 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001832 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001833 if (!dentry)
1834 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001835
Eric Parisb77a4932010-11-23 11:40:08 -05001836 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001837 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001838 if (!inode) {
1839 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001840 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001841 }
Eric Parisb77a4932010-11-23 11:40:08 -05001842
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001843 inode->i_fop = &sel_perm_ops;
1844 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001845 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001846 d_add(dentry, inode);
1847 }
Eric Parisb77a4932010-11-23 11:40:08 -05001848 rc = 0;
1849out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001850 for (i = 0; i < nperms; i++)
1851 kfree(perms[i]);
1852 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001853 return rc;
1854}
1855
Stephen Smalley02a52c52020-08-07 09:29:34 -04001856static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
1857 char *classname, int index,
1858 struct dentry *dir)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001859{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001860 struct super_block *sb = dir->d_sb;
1861 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001862 struct dentry *dentry = NULL;
1863 struct inode *inode = NULL;
1864 int rc;
1865
1866 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001867 if (!dentry)
1868 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001869
1870 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001871 if (!inode) {
1872 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001873 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001874 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001875
1876 inode->i_fop = &sel_class_ops;
1877 inode->i_ino = sel_class_to_ino(index);
1878 d_add(dentry, inode);
1879
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001880 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001881 if (IS_ERR(dentry))
1882 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001883
Stephen Smalley02a52c52020-08-07 09:29:34 -04001884 rc = sel_make_perm_files(newpolicy, classname, index, dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001885
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001886 return rc;
1887}
1888
Daniel Burgener66ec3842020-08-19 15:59:33 -04001889static int sel_make_classes(struct selinux_policy *newpolicy,
1890 struct dentry *class_dir,
1891 unsigned long *last_class_ino)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001892{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001893
Eric Parisb77a4932010-11-23 11:40:08 -05001894 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001895 char **classes;
1896
Stephen Smalley02a52c52020-08-07 09:29:34 -04001897 rc = security_get_classes(newpolicy, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001898 if (rc)
1899 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001900
1901 /* +2 since classes are 1-indexed */
Daniel Burgener66ec3842020-08-19 15:59:33 -04001902 *last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001903
1904 for (i = 0; i < nclasses; i++) {
1905 struct dentry *class_name_dir;
1906
Daniel Burgener66ec3842020-08-19 15:59:33 -04001907 class_name_dir = sel_make_dir(class_dir, classes[i],
1908 last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001909 if (IS_ERR(class_name_dir)) {
1910 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001911 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001912 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001913
1914 /* i+1 since class values are 1-indexed */
Stephen Smalley02a52c52020-08-07 09:29:34 -04001915 rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001916 class_name_dir);
1917 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001918 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001919 }
Eric Parisb77a4932010-11-23 11:40:08 -05001920 rc = 0;
1921out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001922 for (i = 0; i < nclasses; i++)
1923 kfree(classes[i]);
1924 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001925 return rc;
1926}
1927
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001928static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001929{
1930 unsigned int iter;
1931 struct dentry *dentry = NULL;
1932 struct inode *inode = NULL;
1933
Paul Moore3bb56b22008-01-29 08:38:19 -05001934 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001935 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001936 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001937 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001938 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001939 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001940
1941 if (dentry == NULL)
1942 return -ENOMEM;
1943
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001944 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001945 if (inode == NULL) {
1946 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001947 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001948 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001949
1950 inode->i_fop = &sel_policycap_ops;
1951 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1952 d_add(dentry, inode);
1953 }
1954
1955 return 0;
1956}
1957
Al Viroa1c2aa12012-03-18 20:36:59 -04001958static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001959 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Al Viroa1c2aa12012-03-18 20:36:59 -04001961 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct inode *inode;
1963
Al Viroa1c2aa12012-03-18 20:36:59 -04001964 if (!dentry)
1965 return ERR_PTR(-ENOMEM);
1966
1967 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1968 if (!inode) {
1969 dput(dentry);
1970 return ERR_PTR(-ENOMEM);
1971 }
Eric Parisb77a4932010-11-23 11:40:08 -05001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 inode->i_op = &simple_dir_inode_operations;
1974 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001975 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001976 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001977 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001979 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001980 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001981
Al Viroa1c2aa12012-03-18 20:36:59 -04001982 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001985#define NULL_FILE_NAME "null"
1986
David Howells920f50b2019-03-25 16:38:30 +00001987static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001989 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int ret;
1991 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001992 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct inode_security_struct *isec;
1994
Eric Biggerscda37122017-03-25 21:15:37 -07001995 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1997 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001998 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
2000 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
2001 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
2002 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
2003 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
2004 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
2005 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
2006 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
2007 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
2008 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04002009 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
2010 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09002011 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05002012 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05002013 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
2014 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* last one */ {""}
2016 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002017
2018 ret = selinux_fs_info_create(sb);
2019 if (ret)
2020 goto err;
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
2023 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002024 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002026 fsi = sb->s_fs_info;
2027 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
2028 if (IS_ERR(fsi->bool_dir)) {
2029 ret = PTR_ERR(fsi->bool_dir);
2030 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08002031 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Eric Parisb77a4932010-11-23 11:40:08 -05002034 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05002036 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08002037 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Eric Parisb77a4932010-11-23 11:40:08 -05002039 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08002041 if (!inode) {
2042 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08002043 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08002044 }
Eric Parisb77a4932010-11-23 11:40:08 -05002045
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002046 inode->i_ino = ++fsi->last_ino;
Casey Schaufler80788c22018-09-21 17:19:11 -07002047 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 isec->sid = SECINITSID_DEVNULL;
2049 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01002050 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2053 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002055 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002056 if (IS_ERR(dentry)) {
2057 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08002058 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 ret = sel_make_avc_files(dentry);
Jeff Vander Stoep66f8e2f2019-11-22 10:33:06 +01002062
2063 dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2064 if (IS_ERR(dentry)) {
2065 ret = PTR_ERR(dentry);
2066 goto err;
2067 }
2068
2069 ret = sel_make_ss_files(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (ret)
James Morris161ce452006-03-22 00:09:17 -08002071 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04002072
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002073 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04002074 if (IS_ERR(dentry)) {
2075 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04002076 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002077 }
James Carterf0ee2e42007-04-04 10:11:29 -04002078
2079 ret = sel_make_initcon_files(dentry);
2080 if (ret)
2081 goto err;
2082
Daniel Burgener613ba182020-08-19 15:59:34 -04002083 fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002084 if (IS_ERR(fsi->class_dir)) {
2085 ret = PTR_ERR(fsi->class_dir);
2086 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002087 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002088 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002089
Daniel Burgener613ba182020-08-19 15:59:34 -04002090 fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002091 &fsi->last_ino);
2092 if (IS_ERR(fsi->policycap_dir)) {
2093 ret = PTR_ERR(fsi->policycap_dir);
2094 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04002095 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002096 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002097
Stephen Smalley02a52c52020-08-07 09:29:34 -04002098 ret = sel_make_policycap(fsi);
2099 if (ret) {
2100 pr_err("SELinux: failed to load policy capabilities\n");
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002101 goto err;
Stephen Smalley02a52c52020-08-07 09:29:34 -04002102 }
2103
Eric Parisb77a4932010-11-23 11:40:08 -05002104 return 0;
James Morris161ce452006-03-22 00:09:17 -08002105err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002106 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002107 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002108
2109 selinux_fs_info_free(sb);
2110
Eric Parisb77a4932010-11-23 11:40:08 -05002111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
David Howells920f50b2019-03-25 16:38:30 +00002114static int sel_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
David Howells920f50b2019-03-25 16:38:30 +00002116 return get_tree_single(fc, sel_fill_super);
2117}
2118
2119static const struct fs_context_operations sel_context_ops = {
2120 .get_tree = sel_get_tree,
2121};
2122
2123static int sel_init_fs_context(struct fs_context *fc)
2124{
2125 fc->ops = &sel_context_ops;
2126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002129static void sel_kill_sb(struct super_block *sb)
2130{
2131 selinux_fs_info_free(sb);
2132 kill_litter_super(sb);
2133}
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135static struct file_system_type sel_fs_type = {
2136 .name = "selinuxfs",
David Howells920f50b2019-03-25 16:38:30 +00002137 .init_fs_context = sel_init_fs_context,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002138 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139};
2140
2141struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002142struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144static int __init init_sel_fs(void)
2145{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002146 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2147 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 int err;
2149
Stephen Smalley6c5a6822019-12-17 09:15:10 -05002150 if (!selinux_enabled_boot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002152
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002153 err = sysfs_create_mount_point(fs_kobj, "selinux");
2154 if (err)
2155 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002158 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002159 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002160 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002161 }
Eric Parisb77a4932010-11-23 11:40:08 -05002162
Al Viro765927b2012-06-26 21:58:53 +04002163 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002164 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002165 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002166 err = PTR_ERR(selinuxfs_mount);
2167 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002169 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2170 &null_name);
2171 if (IS_ERR(selinux_null.dentry)) {
2172 pr_err("selinuxfs: could not lookup null!\n");
2173 err = PTR_ERR(selinux_null.dentry);
2174 selinux_null.dentry = NULL;
2175 }
Eric Parisb77a4932010-11-23 11:40:08 -05002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return err;
2178}
2179
2180__initcall(init_sel_fs);
2181
2182#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2183void exit_sel_fs(void)
2184{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002185 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002186 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002187 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 unregister_filesystem(&sel_fs_type);
2189}
2190#endif