blob: 41099cc3d5e205747c33b324288e03fa91259f24 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
Eric Paris18729812008-04-17 14:15:45 -04003 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Paul Moore82c21bf2011-08-01 11:10:33 +00005 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05006 *
Eric Paris18729812008-04-17 14:15:45 -04007 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05008 *
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
Eric Paris18729812008-04-17 14:15:45 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * the Free Software Foundation, version 2.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040022#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/security.h>
27#include <linux/major.h>
28#include <linux/seq_file.h>
29#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000030#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040031#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070032#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040033#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* selinuxfs pseudo filesystem for exporting the security policy API.
36 Based on the proc code and the fs/nfsd/nfsctl.c code. */
37
38#include "flask.h"
39#include "avc.h"
40#include "avc_ss.h"
41#include "security.h"
42#include "objsec.h"
43#include "conditional.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045enum sel_inos {
46 SEL_ROOT_INO = 2,
47 SEL_LOAD, /* load policy */
48 SEL_ENFORCE, /* get or set enforcing status */
49 SEL_CONTEXT, /* validate context */
50 SEL_ACCESS, /* compute access decision */
51 SEL_CREATE, /* compute create labeling decision */
52 SEL_RELABEL, /* compute relabeling decision */
53 SEL_USER, /* compute reachable user contexts */
54 SEL_POLICYVERS, /* return policy version for this kernel */
55 SEL_COMMIT_BOOLS, /* commit new boolean values */
56 SEL_MLS, /* return if MLS policy is enabled */
57 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 SEL_MEMBER, /* compute polyinstantiation membership decision */
59 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070060 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040061 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
62 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090063 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040064 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050065 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040066 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Stephen Smalley0619f0f2018-03-20 11:59:11 -040069struct selinux_fs_info {
70 struct dentry *bool_dir;
71 unsigned int bool_num;
72 char **bool_pending_names;
73 unsigned int *bool_pending_values;
74 struct dentry *class_dir;
75 unsigned long last_class_ino;
76 bool policy_opened;
77 struct dentry *policycap_dir;
78 struct mutex mutex;
79 unsigned long last_ino;
80 struct selinux_state *state;
81 struct super_block *sb;
82};
83
84static int selinux_fs_info_create(struct super_block *sb)
85{
86 struct selinux_fs_info *fsi;
87
88 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
89 if (!fsi)
90 return -ENOMEM;
91
92 mutex_init(&fsi->mutex);
93 fsi->last_ino = SEL_INO_NEXT - 1;
94 fsi->state = &selinux_state;
95 fsi->sb = sb;
96 sb->s_fs_info = fsi;
97 return 0;
98}
99
100static void selinux_fs_info_free(struct super_block *sb)
101{
102 struct selinux_fs_info *fsi = sb->s_fs_info;
103 int i;
104
105 if (fsi) {
106 for (i = 0; i < fsi->bool_num; i++)
107 kfree(fsi->bool_pending_names[i]);
108 kfree(fsi->bool_pending_names);
109 kfree(fsi->bool_pending_values);
110 }
111 kfree(sb->s_fs_info);
112 sb->s_fs_info = NULL;
113}
James Carter6174eaf2007-04-04 16:18:39 -0400114
Paul Moore3bb56b22008-01-29 08:38:19 -0500115#define SEL_INITCON_INO_OFFSET 0x01000000
116#define SEL_BOOL_INO_OFFSET 0x02000000
117#define SEL_CLASS_INO_OFFSET 0x04000000
118#define SEL_POLICYCAP_INO_OFFSET 0x08000000
119#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define TMPBUFLEN 12
122static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
123 size_t count, loff_t *ppos)
124{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400125 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char tmpbuf[TMPBUFLEN];
127 ssize_t length;
128
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500129 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400130 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
132}
133
134#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400135static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 size_t count, loff_t *ppos)
137
138{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400139 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
140 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500141 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500143 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Davi Arnautbfd51622005-10-30 14:59:24 -0800145 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500146 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500147
148 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500149 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500150 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500151
Al Viro8365a712015-12-24 00:08:06 -0500152 page = memdup_user_nul(buf, count);
153 if (IS_ERR(page))
154 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 length = -EINVAL;
157 if (sscanf(page, "%d", &new_value) != 1)
158 goto out;
159
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500160 new_value = !!new_value;
161
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400162 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500163 if (new_value != old_value) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500164 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
165 SECCLASS_SECURITY, SECURITY__SETENFORCE,
166 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (length)
168 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000169 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500170 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500171 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700172 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500173 audit_get_sessionid(current));
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400174 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500175 if (new_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 avc_ss_reset(0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500177 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400178 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500179 if (!new_value)
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300180 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 length = count;
183out:
Al Viro8365a712015-12-24 00:08:06 -0500184 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return length;
186}
187#else
188#define sel_write_enforce NULL
189#endif
190
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800191static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .read = sel_read_enforce,
193 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200194 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Eric Paris3f120702007-09-21 14:37:10 -0400197static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
198 size_t count, loff_t *ppos)
199{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400200 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
201 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400202 char tmpbuf[TMPBUFLEN];
203 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500204 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400205 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400206 security_get_reject_unknown(state) :
207 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400208
209 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
210 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
211}
212
213static const struct file_operations sel_handle_unknown_ops = {
214 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200215 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400216};
217
KaiGai Kohei11904162010-09-14 18:28:39 +0900218static int sel_open_handle_status(struct inode *inode, struct file *filp)
219{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400220 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
221 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900222
223 if (!status)
224 return -ENOMEM;
225
226 filp->private_data = status;
227
228 return 0;
229}
230
231static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
232 size_t count, loff_t *ppos)
233{
234 struct page *status = filp->private_data;
235
236 BUG_ON(!status);
237
238 return simple_read_from_buffer(buf, count, ppos,
239 page_address(status),
240 sizeof(struct selinux_kernel_status));
241}
242
243static int sel_mmap_handle_status(struct file *filp,
244 struct vm_area_struct *vma)
245{
246 struct page *status = filp->private_data;
247 unsigned long size = vma->vm_end - vma->vm_start;
248
249 BUG_ON(!status);
250
251 /* only allows one page from the head */
252 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
253 return -EIO;
254 /* disallow writable mapping */
255 if (vma->vm_flags & VM_WRITE)
256 return -EPERM;
257 /* disallow mprotect() turns it into writable */
258 vma->vm_flags &= ~VM_MAYWRITE;
259
260 return remap_pfn_range(vma, vma->vm_start,
261 page_to_pfn(status),
262 size, vma->vm_page_prot);
263}
264
265static const struct file_operations sel_handle_status_ops = {
266 .open = sel_open_handle_status,
267 .read = sel_read_handle_status,
268 .mmap = sel_mmap_handle_status,
269 .llseek = generic_file_llseek,
270};
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400273static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 size_t count, loff_t *ppos)
275
276{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400277 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500278 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ssize_t length;
280 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Davi Arnautbfd51622005-10-30 14:59:24 -0800282 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500283 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500284
285 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500286 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500287 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500288
Al Viro8365a712015-12-24 00:08:06 -0500289 page = memdup_user_nul(buf, count);
290 if (IS_ERR(page))
291 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 length = -EINVAL;
294 if (sscanf(page, "%d", &new_value) != 1)
295 goto out;
296
297 if (new_value) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400298 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500299 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000301 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500302 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700303 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500304 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 length = count;
308out:
Al Viro8365a712015-12-24 00:08:06 -0500309 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return length;
311}
312#else
313#define sel_write_disable NULL
314#endif
315
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800316static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200318 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400322 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 char tmpbuf[TMPBUFLEN];
325 ssize_t length;
326
327 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
328 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
329}
330
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800331static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200333 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400337static int sel_make_bools(struct selinux_fs_info *fsi);
338static int sel_make_classes(struct selinux_fs_info *fsi);
339static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400340
341/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400342static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400343 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345static ssize_t sel_read_mls(struct file *filp, char __user *buf,
346 size_t count, loff_t *ppos)
347{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400348 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 char tmpbuf[TMPBUFLEN];
350 ssize_t length;
351
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100352 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400353 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
355}
356
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800357static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200359 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
Eric Pariscee74f42010-10-13 17:50:25 -0400362struct policy_load_memory {
363 size_t len;
364 void *data;
365};
366
367static int sel_open_policy(struct inode *inode, struct file *filp)
368{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400369 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
370 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400371 struct policy_load_memory *plm = NULL;
372 int rc;
373
374 BUG_ON(filp->private_data);
375
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400376 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400377
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500378 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
379 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400380 if (rc)
381 goto err;
382
383 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400384 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400385 goto err;
386
387 rc = -ENOMEM;
388 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
389 if (!plm)
390 goto err;
391
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400392 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500393 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400394 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500395 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400396 }
397
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400398 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400399 if (rc)
400 goto err;
401
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400402 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400403
404 filp->private_data = plm;
405
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400406 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400407
408 return 0;
409err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400410 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400411
412 if (plm)
413 vfree(plm->data);
414 kfree(plm);
415 return rc;
416}
417
418static int sel_release_policy(struct inode *inode, struct file *filp)
419{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400420 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400421 struct policy_load_memory *plm = filp->private_data;
422
423 BUG_ON(!plm);
424
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400425 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400426
427 vfree(plm->data);
428 kfree(plm);
429
430 return 0;
431}
432
433static ssize_t sel_read_policy(struct file *filp, char __user *buf,
434 size_t count, loff_t *ppos)
435{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400436 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400437 struct policy_load_memory *plm = filp->private_data;
438 int ret;
439
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400440 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400441
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500442 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
443 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400444 if (ret)
445 goto out;
446
447 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
448out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400449 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400450 return ret;
451}
452
Dave Jiang11bac802017-02-24 14:56:41 -0800453static int sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400454{
Dave Jiang11bac802017-02-24 14:56:41 -0800455 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400456 unsigned long offset;
457 struct page *page;
458
459 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
460 return VM_FAULT_SIGBUS;
461
462 offset = vmf->pgoff << PAGE_SHIFT;
463 if (offset >= roundup(plm->len, PAGE_SIZE))
464 return VM_FAULT_SIGBUS;
465
466 page = vmalloc_to_page(plm->data + offset);
467 get_page(page);
468
469 vmf->page = page;
470
471 return 0;
472}
473
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700474static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400475 .fault = sel_mmap_policy_fault,
476 .page_mkwrite = sel_mmap_policy_fault,
477};
478
James Morrisad3fa082011-08-30 10:50:12 +1000479static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400480{
481 if (vma->vm_flags & VM_SHARED) {
482 /* do not allow mprotect to make mapping writable */
483 vma->vm_flags &= ~VM_MAYWRITE;
484
485 if (vma->vm_flags & VM_WRITE)
486 return -EACCES;
487 }
488
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700489 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400490 vma->vm_ops = &sel_mmap_policy_ops;
491
492 return 0;
493}
494
Eric Pariscee74f42010-10-13 17:50:25 -0400495static const struct file_operations sel_policy_ops = {
496 .open = sel_open_policy,
497 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400498 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400499 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500500 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400501};
502
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400503static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
504{
505 int ret;
506
507 ret = sel_make_bools(fsi);
508 if (ret) {
509 pr_err("SELinux: failed to load policy booleans\n");
510 return ret;
511 }
512
513 ret = sel_make_classes(fsi);
514 if (ret) {
515 pr_err("SELinux: failed to load policy classes\n");
516 return ret;
517 }
518
519 ret = sel_make_policycap(fsi);
520 if (ret) {
521 pr_err("SELinux: failed to load policy capabilities\n");
522 return ret;
523 }
524
525 return 0;
526}
527
Eric Paris18729812008-04-17 14:15:45 -0400528static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 size_t count, loff_t *ppos)
530
531{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400532 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ssize_t length;
534 void *data = NULL;
535
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400536 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500538 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
539 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (length)
541 goto out;
542
Eric Parisb77a4932010-11-23 11:40:08 -0500543 /* No partial writes. */
544 length = -EINVAL;
545 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Eric Parisb77a4932010-11-23 11:40:08 -0500548 length = -EFBIG;
549 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500551
552 length = -ENOMEM;
553 data = vmalloc(count);
554 if (!data)
555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 length = -EFAULT;
558 if (copy_from_user(data, buf, count) != 0)
559 goto out;
560
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400561 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500562 if (length) {
563 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400567 length = sel_make_policy_nodes(fsi);
568 if (length)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400569 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500570
571 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400572
573out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000574 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500575 "policy loaded auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700576 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500577 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400579 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 vfree(data);
581 return length;
582}
583
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800584static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200586 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
Eric Paris18729812008-04-17 14:15:45 -0400589static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400591 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
592 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500593 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800594 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ssize_t length;
596
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500597 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
598 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400602 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500603 if (length)
604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400606 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500607 if (length)
608 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800609
Eric Parisb77a4932010-11-23 11:40:08 -0500610 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800611 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400612 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
613 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800614 goto out;
615 }
616
617 memcpy(buf, canon, len);
618 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800620 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return length;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
625 size_t count, loff_t *ppos)
626{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400627 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 char tmpbuf[TMPBUFLEN];
629 ssize_t length;
630
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400631 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
633}
634
Eric Paris18729812008-04-17 14:15:45 -0400635static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 size_t count, loff_t *ppos)
637{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400638 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500639 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ssize_t length;
641 unsigned int new_value;
642
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500643 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
644 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
645 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500647 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Davi Arnautbfd51622005-10-30 14:59:24 -0800649 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500650 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500651
652 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500653 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500654 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500655
Al Viro8365a712015-12-24 00:08:06 -0500656 page = memdup_user_nul(buf, count);
657 if (IS_ERR(page))
658 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 length = -EINVAL;
661 if (sscanf(page, "%u", &new_value) != 1)
662 goto out;
663
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400664 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 length = count;
666out:
Al Viro8365a712015-12-24 00:08:06 -0500667 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return length;
669}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800670static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 .read = sel_read_checkreqprot,
672 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200673 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500676static ssize_t sel_write_validatetrans(struct file *file,
677 const char __user *buf,
678 size_t count, loff_t *ppos)
679{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400680 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
681 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500682 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
683 char *req = NULL;
684 u32 osid, nsid, tsid;
685 u16 tclass;
686 int rc;
687
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500688 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
689 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500690 if (rc)
691 goto out;
692
693 rc = -ENOMEM;
694 if (count >= PAGE_SIZE)
695 goto out;
696
697 /* No partial writes. */
698 rc = -EINVAL;
699 if (*ppos != 0)
700 goto out;
701
Al Viro0b884d22017-05-13 18:12:07 -0400702 req = memdup_user_nul(buf, count);
703 if (IS_ERR(req)) {
704 rc = PTR_ERR(req);
705 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500706 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400707 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500708
709 rc = -ENOMEM;
710 oldcon = kzalloc(count + 1, GFP_KERNEL);
711 if (!oldcon)
712 goto out;
713
714 newcon = kzalloc(count + 1, GFP_KERNEL);
715 if (!newcon)
716 goto out;
717
718 taskcon = kzalloc(count + 1, GFP_KERNEL);
719 if (!taskcon)
720 goto out;
721
722 rc = -EINVAL;
723 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
724 goto out;
725
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400726 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500727 if (rc)
728 goto out;
729
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400730 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500731 if (rc)
732 goto out;
733
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400734 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500735 if (rc)
736 goto out;
737
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400738 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500739 if (!rc)
740 rc = count;
741out:
742 kfree(req);
743 kfree(oldcon);
744 kfree(newcon);
745 kfree(taskcon);
746 return rc;
747}
748
749static const struct file_operations sel_transition_ops = {
750 .write = sel_write_validatetrans,
751 .llseek = generic_file_llseek,
752};
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/*
755 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
756 */
Eric Paris18729812008-04-17 14:15:45 -0400757static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
758static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
759static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
760static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
761static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763static ssize_t (*write_op[])(struct file *, char *, size_t) = {
764 [SEL_ACCESS] = sel_write_access,
765 [SEL_CREATE] = sel_write_create,
766 [SEL_RELABEL] = sel_write_relabel,
767 [SEL_USER] = sel_write_user,
768 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800769 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770};
771
772static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
773{
Al Viro496ad9a2013-01-23 17:07:38 -0500774 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 char *data;
776 ssize_t rv;
777
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800778 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EINVAL;
780
781 data = simple_transaction_get(file, buf, size);
782 if (IS_ERR(data))
783 return PTR_ERR(data);
784
Eric Paris18729812008-04-17 14:15:45 -0400785 rv = write_op[ino](file, data, size);
786 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 simple_transaction_set(file, rv);
788 rv = size;
789 }
790 return rv;
791}
792
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800793static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .write = selinux_transaction_write,
795 .read = simple_transaction_read,
796 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200797 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
800/*
801 * payload - write methods
802 * If the method has a response, the response should be put in buf,
803 * and the length returned. Otherwise return 0 or and -error.
804 */
805
Eric Paris18729812008-04-17 14:15:45 -0400806static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400808 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
809 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500810 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 u32 ssid, tsid;
812 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct av_decision avd;
814 ssize_t length;
815
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500816 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
817 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800822 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Eric Parisb77a4932010-11-23 11:40:08 -0500826 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800827 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!tcon)
829 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500832 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400835 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500836 if (length)
837 goto out;
838
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400839 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500840 if (length)
841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400843 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900846 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500847 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900849 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850out:
Eric Parisb77a4932010-11-23 11:40:08 -0500851 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 kfree(scon);
853 return length;
854}
855
Eric Paris18729812008-04-17 14:15:45 -0400856static ssize_t sel_write_create(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;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100861 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 u32 ssid, tsid, newsid;
863 u16 tclass;
864 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500865 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100867 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500869 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
870 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
871 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800876 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Eric Parisb77a4932010-11-23 11:40:08 -0500880 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800881 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!tcon)
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100885 length = -ENOMEM;
886 namebuf = kzalloc(size + 1, GFP_KERNEL);
887 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100890 length = -EINVAL;
891 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
892 if (nargs < 3 || nargs > 4)
893 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400894 if (nargs == 4) {
895 /*
896 * If and when the name of new object to be queried contains
897 * either whitespace or multibyte characters, they shall be
898 * encoded based on the percentage-encoding rule.
899 * If not encoded, the sscanf logic picks up only left-half
900 * of the supplied name; splitted by a whitespace unexpectedly.
901 */
902 char *r, *w;
903 int c1, c2;
904
905 r = w = namebuf;
906 do {
907 c1 = *r++;
908 if (c1 == '+')
909 c1 = ' ';
910 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800911 c1 = hex_to_bin(*r++);
912 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400913 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800914 c2 = hex_to_bin(*r++);
915 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400916 goto out;
917 c1 = (c1 << 4) | c2;
918 }
919 *w++ = c1;
920 } while (c1 != '\0');
921
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100922 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400923 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100924
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400925 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500926 if (length)
927 goto out;
928
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400929 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500930 if (length)
931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400933 length = security_transition_sid_user(state, ssid, tsid, tclass,
934 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500935 if (length)
936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400938 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500939 if (length)
940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric Parisb77a4932010-11-23 11:40:08 -0500942 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400944 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
945 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 memcpy(buf, newcon, len);
950 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951out:
Eric Parisb77a4932010-11-23 11:40:08 -0500952 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100953 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500954 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 kfree(scon);
956 return length;
957}
958
Eric Paris18729812008-04-17 14:15:45 -0400959static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400961 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
962 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500963 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 u32 ssid, tsid, newsid;
965 u16 tclass;
966 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500967 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 u32 len;
969
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500970 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
971 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
972 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800977 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Eric Parisb77a4932010-11-23 11:40:08 -0500981 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800982 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (!tcon)
984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 length = -EINVAL;
987 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400990 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500991 if (length)
992 goto out;
993
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400994 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500995 if (length)
996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400998 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500999 if (length)
1000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001002 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001003 if (length)
1004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Parisb77a4932010-11-23 11:40:08 -05001006 length = -ERANGE;
1007 if (len > SIMPLE_TRANSACTION_LIMIT)
1008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 memcpy(buf, newcon, len);
1011 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012out:
Eric Parisb77a4932010-11-23 11:40:08 -05001013 kfree(newcon);
1014 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 kfree(scon);
1016 return length;
1017}
1018
Eric Paris18729812008-04-17 14:15:45 -04001019static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001021 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1022 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001023 char *con = NULL, *user = NULL, *ptr;
1024 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ssize_t length;
1026 char *newcon;
1027 int i, rc;
1028 u32 len, nsids;
1029
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001030 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1031 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1032 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001037 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Eric Parisb77a4932010-11-23 11:40:08 -05001041 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001042 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!user)
1044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 length = -EINVAL;
1047 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001050 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001051 if (length)
1052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001054 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001055 if (length)
1056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 length = sprintf(buf, "%u", nsids) + 1;
1059 ptr = buf + length;
1060 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001061 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (rc) {
1063 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1067 kfree(newcon);
1068 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 memcpy(ptr, newcon, len);
1072 kfree(newcon);
1073 ptr += len;
1074 length += len;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076out:
Eric Parisb77a4932010-11-23 11:40:08 -05001077 kfree(sids);
1078 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 kfree(con);
1080 return length;
1081}
1082
Eric Paris18729812008-04-17 14:15:45 -04001083static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001085 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1086 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001087 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 u32 ssid, tsid, newsid;
1089 u16 tclass;
1090 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001091 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 u32 len;
1093
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001094 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1095 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1096 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001101 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Eric Parisb77a4932010-11-23 11:40:08 -05001105 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001106 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!tcon)
1108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 length = -EINVAL;
1111 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001114 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001115 if (length)
1116 goto out;
1117
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001118 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001119 if (length)
1120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001122 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001123 if (length)
1124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001126 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001127 if (length)
1128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Eric Parisb77a4932010-11-23 11:40:08 -05001130 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001132 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1133 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
1137 memcpy(buf, newcon, len);
1138 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139out:
Eric Parisb77a4932010-11-23 11:40:08 -05001140 kfree(newcon);
1141 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 kfree(scon);
1143 return length;
1144}
1145
1146static struct inode *sel_make_inode(struct super_block *sb, int mode)
1147{
1148 struct inode *ret = new_inode(sb);
1149
1150 if (ret) {
1151 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001152 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 return ret;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1158 size_t count, loff_t *ppos)
1159{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001160 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 char *page = NULL;
1162 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ssize_t ret;
1164 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001165 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001166 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001168 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric Parisb77a4932010-11-23 11:40:08 -05001170 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001171 if (index >= fsi->bool_num || strcmp(name,
1172 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Eric Parisb77a4932010-11-23 11:40:08 -05001175 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001176 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001177 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001180 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (cur_enforcing < 0) {
1182 ret = cur_enforcing;
1183 goto out;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001186 fsi->bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001187 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001189 mutex_unlock(&fsi->mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001190 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return ret;
1192}
1193
1194static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1195 size_t count, loff_t *ppos)
1196{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001197 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001199 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001201 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001202 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001204 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001206 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1207 SECCLASS_SECURITY, SECURITY__SETBOOL,
1208 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (length)
1210 goto out;
1211
Eric Parisb77a4932010-11-23 11:40:08 -05001212 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001213 if (index >= fsi->bool_num || strcmp(name,
1214 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001215 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001216
Eric Parisb77a4932010-11-23 11:40:08 -05001217 length = -ENOMEM;
1218 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001220
Eric Parisb77a4932010-11-23 11:40:08 -05001221 /* No partial writes. */
1222 length = -EINVAL;
1223 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001225
Al Viro8365a712015-12-24 00:08:06 -05001226 page = memdup_user_nul(buf, count);
1227 if (IS_ERR(page)) {
1228 length = PTR_ERR(page);
1229 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 length = -EINVAL;
1234 if (sscanf(page, "%d", &new_value) != 1)
1235 goto out;
1236
1237 if (new_value)
1238 new_value = 1;
1239
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001240 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 length = count;
1242
1243out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001244 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001245 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return length;
1247}
1248
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001249static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001250 .read = sel_read_bool,
1251 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001252 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253};
1254
1255static ssize_t sel_commit_bools_write(struct file *filep,
1256 const char __user *buf,
1257 size_t count, loff_t *ppos)
1258{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001259 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001261 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int new_value;
1263
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001264 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001266 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1267 SECCLASS_SECURITY, SECURITY__SETBOOL,
1268 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (length)
1270 goto out;
1271
Eric Parisb77a4932010-11-23 11:40:08 -05001272 length = -ENOMEM;
1273 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001275
1276 /* No partial writes. */
1277 length = -EINVAL;
1278 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001280
Al Viro8365a712015-12-24 00:08:06 -05001281 page = memdup_user_nul(buf, count);
1282 if (IS_ERR(page)) {
1283 length = PTR_ERR(page);
1284 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 length = -EINVAL;
1289 if (sscanf(page, "%d", &new_value) != 1)
1290 goto out;
1291
Eric Parisb77a4932010-11-23 11:40:08 -05001292 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001293 if (new_value && fsi->bool_pending_values)
1294 length = security_set_bools(fsi->state, fsi->bool_num,
1295 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Eric Parisb77a4932010-11-23 11:40:08 -05001297 if (!length)
1298 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001301 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001302 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return length;
1304}
1305
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001306static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001307 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001308 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309};
1310
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001311static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Al Viroad521842014-12-24 14:56:48 -05001313 d_genocide(de);
1314 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317#define BOOL_DIR_NAME "booleans"
1318
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001319static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Eric Parisb77a4932010-11-23 11:40:08 -05001321 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 ssize_t len;
1323 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001324 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct inode *inode = NULL;
1326 struct inode_security_struct *isec;
1327 char **names = NULL, *page;
1328 int num;
1329 int *values = NULL;
1330 u32 sid;
1331
1332 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001333 for (i = 0; i < fsi->bool_num; i++)
1334 kfree(fsi->bool_pending_names[i]);
1335 kfree(fsi->bool_pending_names);
1336 kfree(fsi->bool_pending_values);
1337 fsi->bool_num = 0;
1338 fsi->bool_pending_names = NULL;
1339 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001341 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Eric Parisb77a4932010-11-23 11:40:08 -05001343 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001344 page = (char *)get_zeroed_page(GFP_KERNEL);
1345 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001348 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001349 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto out;
1351
1352 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001353 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001355 if (!dentry)
1356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Eric Parisb77a4932010-11-23 11:40:08 -05001358 ret = -ENOMEM;
1359 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1360 if (!inode)
1361 goto out;
1362
Eric Parisb77a4932010-11-23 11:40:08 -05001363 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001364 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001365 if (len >= PAGE_SIZE)
1366 goto out;
1367
Eric Paris18729812008-04-17 14:15:45 -04001368 isec = (struct inode_security_struct *)inode->i_security;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001369 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001370 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001371 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001372 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1373 page);
1374 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001375 }
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001378 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001380 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 d_add(dentry, inode);
1382 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001383 fsi->bool_num = num;
1384 fsi->bool_pending_names = names;
1385 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001386
1387 free_page((unsigned long)page);
1388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389out:
1390 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001393 for (i = 0; i < num; i++)
1394 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 kfree(names);
1396 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001397 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001398 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001399
1400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1404 size_t count, loff_t *ppos)
1405{
1406 char tmpbuf[TMPBUFLEN];
1407 ssize_t length;
1408
1409 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1410 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1411}
1412
Eric Paris18729812008-04-17 14:15:45 -04001413static ssize_t sel_write_avc_cache_threshold(struct file *file,
1414 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 size_t count, loff_t *ppos)
1416
1417{
Al Viro8365a712015-12-24 00:08:06 -05001418 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001420 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001422 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1423 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1424 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001425 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Eric Parisb77a4932010-11-23 11:40:08 -05001428 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001429 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Eric Parisb77a4932010-11-23 11:40:08 -05001431 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001432 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001433 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001434
Al Viro8365a712015-12-24 00:08:06 -05001435 page = memdup_user_nul(buf, count);
1436 if (IS_ERR(page))
1437 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Eric Parisb77a4932010-11-23 11:40:08 -05001439 ret = -EINVAL;
1440 if (sscanf(page, "%u", &new_value) != 1)
1441 goto out;
1442
1443 avc_cache_threshold = new_value;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446out:
Al Viro8365a712015-12-24 00:08:06 -05001447 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return ret;
1449}
1450
1451static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1452 size_t count, loff_t *ppos)
1453{
1454 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001455 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001458 if (!page)
1459 return -ENOMEM;
1460
1461 length = avc_get_hash_stats(page);
1462 if (length >= 0)
1463 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001465
1466 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001469static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .read = sel_read_avc_cache_threshold,
1471 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001472 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473};
1474
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001475static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001477 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478};
1479
1480#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1481static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1482{
1483 int cpu;
1484
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301485 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (!cpu_possible(cpu))
1487 continue;
1488 *idx = cpu + 1;
1489 return &per_cpu(avc_cache_stats, cpu);
1490 }
1491 return NULL;
1492}
1493
1494static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1495{
1496 loff_t n = *pos - 1;
1497
1498 if (*pos == 0)
1499 return SEQ_START_TOKEN;
1500
1501 return sel_avc_get_stat_idx(&n);
1502}
1503
1504static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1505{
1506 return sel_avc_get_stat_idx(pos);
1507}
1508
1509static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1510{
1511 struct avc_cache_stats *st = v;
1512
Markus Elfring710a0642017-01-15 14:04:53 +01001513 if (v == SEQ_START_TOKEN) {
1514 seq_puts(seq,
1515 "lookups hits misses allocations reclaims frees\n");
1516 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001517 unsigned int lookups = st->lookups;
1518 unsigned int misses = st->misses;
1519 unsigned int hits = lookups - misses;
1520 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1521 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525}
1526
1527static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1528{ }
1529
Jan Engelhardt1996a102008-01-23 00:02:58 +01001530static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .start = sel_avc_stats_seq_start,
1532 .next = sel_avc_stats_seq_next,
1533 .show = sel_avc_stats_seq_show,
1534 .stop = sel_avc_stats_seq_stop,
1535};
1536
1537static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1538{
1539 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1540}
1541
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001542static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 .open = sel_open_avc_cache_stats,
1544 .read = seq_read,
1545 .llseek = seq_lseek,
1546 .release = seq_release,
1547};
1548#endif
1549
1550static int sel_make_avc_files(struct dentry *dir)
1551{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001552 struct super_block *sb = dir->d_sb;
1553 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001554 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001555 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 { "cache_threshold",
1557 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1558 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1559#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1560 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1561#endif
1562 };
1563
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001564 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct inode *inode;
1566 struct dentry *dentry;
1567
1568 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001569 if (!dentry)
1570 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001573 if (!inode)
1574 return -ENOMEM;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001577 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 d_add(dentry, inode);
1579 }
Eric Parisb77a4932010-11-23 11:40:08 -05001580
1581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Eric Paris18729812008-04-17 14:15:45 -04001584static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001585 size_t count, loff_t *ppos)
1586{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001587 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001588 char *con;
1589 u32 sid, len;
1590 ssize_t ret;
1591
Al Viro496ad9a2013-01-23 17:07:38 -05001592 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001593 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001594 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001595 return ret;
1596
1597 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1598 kfree(con);
1599 return ret;
1600}
1601
1602static const struct file_operations sel_initcon_ops = {
1603 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001604 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001605};
1606
1607static int sel_make_initcon_files(struct dentry *dir)
1608{
Eric Parisb77a4932010-11-23 11:40:08 -05001609 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001610
1611 for (i = 1; i <= SECINITSID_NUM; i++) {
1612 struct inode *inode;
1613 struct dentry *dentry;
1614 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001615 if (!dentry)
1616 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001617
1618 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001619 if (!inode)
1620 return -ENOMEM;
1621
James Carterf0ee2e42007-04-04 10:11:29 -04001622 inode->i_fop = &sel_initcon_ops;
1623 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1624 d_add(dentry, inode);
1625 }
Eric Parisb77a4932010-11-23 11:40:08 -05001626
1627 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001628}
1629
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001630static inline unsigned long sel_class_to_ino(u16 class)
1631{
1632 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1633}
1634
1635static inline u16 sel_ino_to_class(unsigned long ino)
1636{
Eric Paris92ae9e82012-04-04 13:46:46 -04001637 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001638}
1639
1640static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1641{
1642 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1643}
1644
1645static inline u32 sel_ino_to_perm(unsigned long ino)
1646{
1647 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1648}
1649
Eric Paris18729812008-04-17 14:15:45 -04001650static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001651 size_t count, loff_t *ppos)
1652{
Al Viro496ad9a2013-01-23 17:07:38 -05001653 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001654 char res[TMPBUFLEN];
1655 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1656 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001657}
1658
1659static const struct file_operations sel_class_ops = {
1660 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001661 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001662};
1663
Eric Paris18729812008-04-17 14:15:45 -04001664static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001665 size_t count, loff_t *ppos)
1666{
Al Viro496ad9a2013-01-23 17:07:38 -05001667 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001668 char res[TMPBUFLEN];
1669 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1670 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001671}
1672
1673static const struct file_operations sel_perm_ops = {
1674 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001675 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001676};
1677
Paul Moore3bb56b22008-01-29 08:38:19 -05001678static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1679 size_t count, loff_t *ppos)
1680{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001681 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001682 int value;
1683 char tmpbuf[TMPBUFLEN];
1684 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001685 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001686
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001687 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001688 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1689
1690 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1691}
1692
1693static const struct file_operations sel_policycap_ops = {
1694 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001695 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001696};
1697
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001698static int sel_make_perm_files(char *objclass, int classvalue,
1699 struct dentry *dir)
1700{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001701 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001702 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001703 char **perms;
1704
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001705 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001706 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001707 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001708
1709 for (i = 0; i < nperms; i++) {
1710 struct inode *inode;
1711 struct dentry *dentry;
1712
Eric Parisb77a4932010-11-23 11:40:08 -05001713 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001714 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001715 if (!dentry)
1716 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001717
Eric Parisb77a4932010-11-23 11:40:08 -05001718 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001719 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001720 if (!inode)
1721 goto out;
1722
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001723 inode->i_fop = &sel_perm_ops;
1724 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001725 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001726 d_add(dentry, inode);
1727 }
Eric Parisb77a4932010-11-23 11:40:08 -05001728 rc = 0;
1729out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001730 for (i = 0; i < nperms; i++)
1731 kfree(perms[i]);
1732 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001733 return rc;
1734}
1735
1736static int sel_make_class_dir_entries(char *classname, int index,
1737 struct dentry *dir)
1738{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001739 struct super_block *sb = dir->d_sb;
1740 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001741 struct dentry *dentry = NULL;
1742 struct inode *inode = NULL;
1743 int rc;
1744
1745 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001746 if (!dentry)
1747 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001748
1749 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001750 if (!inode)
1751 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001752
1753 inode->i_fop = &sel_class_ops;
1754 inode->i_ino = sel_class_to_ino(index);
1755 d_add(dentry, inode);
1756
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001757 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001758 if (IS_ERR(dentry))
1759 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001760
1761 rc = sel_make_perm_files(classname, index, dentry);
1762
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001763 return rc;
1764}
1765
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001766static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001767{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001768
Eric Parisb77a4932010-11-23 11:40:08 -05001769 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001770 char **classes;
1771
1772 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001773 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001774
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001775 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001776 if (rc)
1777 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001778
1779 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001780 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001781
1782 for (i = 0; i < nclasses; i++) {
1783 struct dentry *class_name_dir;
1784
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001785 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1786 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001787 if (IS_ERR(class_name_dir)) {
1788 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001789 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001790 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001791
1792 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001793 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001794 class_name_dir);
1795 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001796 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001797 }
Eric Parisb77a4932010-11-23 11:40:08 -05001798 rc = 0;
1799out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001800 for (i = 0; i < nclasses; i++)
1801 kfree(classes[i]);
1802 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001803 return rc;
1804}
1805
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001806static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001807{
1808 unsigned int iter;
1809 struct dentry *dentry = NULL;
1810 struct inode *inode = NULL;
1811
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001812 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001813
1814 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001815 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001816 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001817 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001818 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001819 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001820
1821 if (dentry == NULL)
1822 return -ENOMEM;
1823
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001824 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
Paul Moore3bb56b22008-01-29 08:38:19 -05001825 if (inode == NULL)
1826 return -ENOMEM;
1827
1828 inode->i_fop = &sel_policycap_ops;
1829 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1830 d_add(dentry, inode);
1831 }
1832
1833 return 0;
1834}
1835
Al Viroa1c2aa12012-03-18 20:36:59 -04001836static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001837 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Al Viroa1c2aa12012-03-18 20:36:59 -04001839 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct inode *inode;
1841
Al Viroa1c2aa12012-03-18 20:36:59 -04001842 if (!dentry)
1843 return ERR_PTR(-ENOMEM);
1844
1845 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1846 if (!inode) {
1847 dput(dentry);
1848 return ERR_PTR(-ENOMEM);
1849 }
Eric Parisb77a4932010-11-23 11:40:08 -05001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 inode->i_op = &simple_dir_inode_operations;
1852 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001853 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001854 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001855 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001857 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001858 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001859
Al Viroa1c2aa12012-03-18 20:36:59 -04001860 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001863#define NULL_FILE_NAME "null"
1864
Eric Paris18729812008-04-17 14:15:45 -04001865static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001867 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int ret;
1869 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001870 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct inode_security_struct *isec;
1872
Eric Biggerscda37122017-03-25 21:15:37 -07001873 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1875 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001876 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1878 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1879 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1880 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1881 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1882 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1883 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1884 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1885 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1886 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001887 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1888 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001889 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001890 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001891 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1892 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* last one */ {""}
1894 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001895
1896 ret = selinux_fs_info_create(sb);
1897 if (ret)
1898 goto err;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1901 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001902 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001904 fsi = sb->s_fs_info;
1905 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1906 if (IS_ERR(fsi->bool_dir)) {
1907 ret = PTR_ERR(fsi->bool_dir);
1908 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001909 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric Parisb77a4932010-11-23 11:40:08 -05001912 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001914 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001915 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Eric Parisb77a4932010-11-23 11:40:08 -05001917 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001919 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001920 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001921
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001922 inode->i_ino = ++fsi->last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001923 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 isec->sid = SECINITSID_DEVNULL;
1925 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001926 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1929 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001931 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001932 if (IS_ERR(dentry)) {
1933 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001934 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 ret = sel_make_avc_files(dentry);
1938 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001939 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001940
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001941 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001942 if (IS_ERR(dentry)) {
1943 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001944 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001945 }
James Carterf0ee2e42007-04-04 10:11:29 -04001946
1947 ret = sel_make_initcon_files(dentry);
1948 if (ret)
1949 goto err;
1950
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001951 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1952 if (IS_ERR(fsi->class_dir)) {
1953 ret = PTR_ERR(fsi->class_dir);
1954 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001955 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001956 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001957
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001958 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1959 &fsi->last_ino);
1960 if (IS_ERR(fsi->policycap_dir)) {
1961 ret = PTR_ERR(fsi->policycap_dir);
1962 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001963 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001964 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001965
1966 ret = sel_make_policy_nodes(fsi);
1967 if (ret)
1968 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001969 return 0;
James Morris161ce452006-03-22 00:09:17 -08001970err:
Eric Paris744ba352008-04-17 11:52:44 -04001971 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1972 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001973
1974 selinux_fs_info_free(sb);
1975
Eric Parisb77a4932010-11-23 11:40:08 -05001976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Al Virofc14f2f2010-07-25 01:48:30 +04001979static struct dentry *sel_mount(struct file_system_type *fs_type,
1980 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Al Virofc14f2f2010-07-25 01:48:30 +04001982 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001985static void sel_kill_sb(struct super_block *sb)
1986{
1987 selinux_fs_info_free(sb);
1988 kill_litter_super(sb);
1989}
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991static struct file_system_type sel_fs_type = {
1992 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001993 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001994 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995};
1996
1997struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001998struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000static int __init init_sel_fs(void)
2001{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002002 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2003 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 int err;
2005
2006 if (!selinux_enabled)
2007 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002008
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002009 err = sysfs_create_mount_point(fs_kobj, "selinux");
2010 if (err)
2011 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002014 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002015 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002016 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002017 }
Eric Parisb77a4932010-11-23 11:40:08 -05002018
Al Viro765927b2012-06-26 21:58:53 +04002019 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002020 if (IS_ERR(selinuxfs_mount)) {
2021 printk(KERN_ERR "selinuxfs: could not mount!\n");
2022 err = PTR_ERR(selinuxfs_mount);
2023 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002025 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2026 &null_name);
2027 if (IS_ERR(selinux_null.dentry)) {
2028 pr_err("selinuxfs: could not lookup null!\n");
2029 err = PTR_ERR(selinux_null.dentry);
2030 selinux_null.dentry = NULL;
2031 }
Eric Parisb77a4932010-11-23 11:40:08 -05002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return err;
2034}
2035
2036__initcall(init_sel_fs);
2037
2038#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2039void exit_sel_fs(void)
2040{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002041 sysfs_remove_mount_point(fs_kobj, "selinux");
Tim Chen423e0ab2011-07-19 09:32:38 -07002042 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 unregister_filesystem(&sel_fs_type);
2044}
2045#endif