blob: 5cc9101ab79b705d1060c1ec312269179bcc6d15 [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 Smalley6b6bc622018-03-05 11:47:56 -0500164 length = avc_has_perm(&selinux_state,
165 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500166 SECCLASS_SECURITY, SECURITY__SETENFORCE,
167 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (length)
169 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400170 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400171 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
172 " enabled=%d old-enabled=%d lsm=selinux res=1",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500173 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700174 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400175 audit_get_sessionid(current),
176 selinux_enabled, selinux_enabled);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400177 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500178 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500179 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400181 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500182 if (!new_value)
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300183 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 length = count;
186out:
Al Viro8365a712015-12-24 00:08:06 -0500187 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return length;
189}
190#else
191#define sel_write_enforce NULL
192#endif
193
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800194static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .read = sel_read_enforce,
196 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200197 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Eric Paris3f120702007-09-21 14:37:10 -0400200static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
201 size_t count, loff_t *ppos)
202{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400203 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
204 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400205 char tmpbuf[TMPBUFLEN];
206 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500207 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400208 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400209 security_get_reject_unknown(state) :
210 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400211
212 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
213 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
214}
215
216static const struct file_operations sel_handle_unknown_ops = {
217 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200218 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400219};
220
KaiGai Kohei11904162010-09-14 18:28:39 +0900221static int sel_open_handle_status(struct inode *inode, struct file *filp)
222{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400223 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
224 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900225
226 if (!status)
227 return -ENOMEM;
228
229 filp->private_data = status;
230
231 return 0;
232}
233
234static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
235 size_t count, loff_t *ppos)
236{
237 struct page *status = filp->private_data;
238
239 BUG_ON(!status);
240
241 return simple_read_from_buffer(buf, count, ppos,
242 page_address(status),
243 sizeof(struct selinux_kernel_status));
244}
245
246static int sel_mmap_handle_status(struct file *filp,
247 struct vm_area_struct *vma)
248{
249 struct page *status = filp->private_data;
250 unsigned long size = vma->vm_end - vma->vm_start;
251
252 BUG_ON(!status);
253
254 /* only allows one page from the head */
255 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
256 return -EIO;
257 /* disallow writable mapping */
258 if (vma->vm_flags & VM_WRITE)
259 return -EPERM;
260 /* disallow mprotect() turns it into writable */
261 vma->vm_flags &= ~VM_MAYWRITE;
262
263 return remap_pfn_range(vma, vma->vm_start,
264 page_to_pfn(status),
265 size, vma->vm_page_prot);
266}
267
268static const struct file_operations sel_handle_status_ops = {
269 .open = sel_open_handle_status,
270 .read = sel_read_handle_status,
271 .mmap = sel_mmap_handle_status,
272 .llseek = generic_file_llseek,
273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400276static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 size_t count, loff_t *ppos)
278
279{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400280 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500281 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 ssize_t length;
283 int new_value;
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400284 int enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Davi Arnautbfd51622005-10-30 14:59:24 -0800286 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500287 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500288
289 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500290 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500291 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500292
Al Viro8365a712015-12-24 00:08:06 -0500293 page = memdup_user_nul(buf, count);
294 if (IS_ERR(page))
295 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 length = -EINVAL;
298 if (sscanf(page, "%d", &new_value) != 1)
299 goto out;
300
301 if (new_value) {
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400302 enforcing = enforcing_enabled(fsi->state);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400303 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500304 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400306 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400307 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
308 " enabled=%d old-enabled=%d lsm=selinux res=1",
309 enforcing, enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700310 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Richard Guy Briggs4195ed42018-04-09 19:34:22 -0400311 audit_get_sessionid(current), 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 length = count;
315out:
Al Viro8365a712015-12-24 00:08:06 -0500316 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return length;
318}
319#else
320#define sel_write_disable NULL
321#endif
322
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800323static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200325 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
328static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400329 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 char tmpbuf[TMPBUFLEN];
332 ssize_t length;
333
334 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
335 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
336}
337
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800338static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200340 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
343/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400344static int sel_make_bools(struct selinux_fs_info *fsi);
345static int sel_make_classes(struct selinux_fs_info *fsi);
346static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400347
348/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400349static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400350 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352static ssize_t sel_read_mls(struct file *filp, char __user *buf,
353 size_t count, loff_t *ppos)
354{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400355 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 char tmpbuf[TMPBUFLEN];
357 ssize_t length;
358
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100359 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400360 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
362}
363
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800364static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200366 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
Eric Pariscee74f42010-10-13 17:50:25 -0400369struct policy_load_memory {
370 size_t len;
371 void *data;
372};
373
374static int sel_open_policy(struct inode *inode, struct file *filp)
375{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400376 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
377 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400378 struct policy_load_memory *plm = NULL;
379 int rc;
380
381 BUG_ON(filp->private_data);
382
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400383 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400384
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500385 rc = avc_has_perm(&selinux_state,
386 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500387 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400388 if (rc)
389 goto err;
390
391 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400392 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400393 goto err;
394
395 rc = -ENOMEM;
396 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
397 if (!plm)
398 goto err;
399
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400400 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500401 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400402 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500403 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400404 }
405
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400406 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400407 if (rc)
408 goto err;
409
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400410 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400411
412 filp->private_data = plm;
413
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400414 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400415
416 return 0;
417err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400418 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400419
420 if (plm)
421 vfree(plm->data);
422 kfree(plm);
423 return rc;
424}
425
426static int sel_release_policy(struct inode *inode, struct file *filp)
427{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400428 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400429 struct policy_load_memory *plm = filp->private_data;
430
431 BUG_ON(!plm);
432
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400433 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400434
435 vfree(plm->data);
436 kfree(plm);
437
438 return 0;
439}
440
441static ssize_t sel_read_policy(struct file *filp, char __user *buf,
442 size_t count, loff_t *ppos)
443{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400444 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400445 struct policy_load_memory *plm = filp->private_data;
446 int ret;
447
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400448 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400449
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500450 ret = avc_has_perm(&selinux_state,
451 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500452 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400453 if (ret)
454 goto out;
455
456 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
457out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400458 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400459 return ret;
460}
461
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530462static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400463{
Dave Jiang11bac802017-02-24 14:56:41 -0800464 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400465 unsigned long offset;
466 struct page *page;
467
468 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
469 return VM_FAULT_SIGBUS;
470
471 offset = vmf->pgoff << PAGE_SHIFT;
472 if (offset >= roundup(plm->len, PAGE_SIZE))
473 return VM_FAULT_SIGBUS;
474
475 page = vmalloc_to_page(plm->data + offset);
476 get_page(page);
477
478 vmf->page = page;
479
480 return 0;
481}
482
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700483static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400484 .fault = sel_mmap_policy_fault,
485 .page_mkwrite = sel_mmap_policy_fault,
486};
487
James Morrisad3fa082011-08-30 10:50:12 +1000488static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400489{
490 if (vma->vm_flags & VM_SHARED) {
491 /* do not allow mprotect to make mapping writable */
492 vma->vm_flags &= ~VM_MAYWRITE;
493
494 if (vma->vm_flags & VM_WRITE)
495 return -EACCES;
496 }
497
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700498 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400499 vma->vm_ops = &sel_mmap_policy_ops;
500
501 return 0;
502}
503
Eric Pariscee74f42010-10-13 17:50:25 -0400504static const struct file_operations sel_policy_ops = {
505 .open = sel_open_policy,
506 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400507 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400508 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500509 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400510};
511
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400512static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
513{
514 int ret;
515
516 ret = sel_make_bools(fsi);
517 if (ret) {
518 pr_err("SELinux: failed to load policy booleans\n");
519 return ret;
520 }
521
522 ret = sel_make_classes(fsi);
523 if (ret) {
524 pr_err("SELinux: failed to load policy classes\n");
525 return ret;
526 }
527
528 ret = sel_make_policycap(fsi);
529 if (ret) {
530 pr_err("SELinux: failed to load policy capabilities\n");
531 return ret;
532 }
533
534 return 0;
535}
536
Eric Paris18729812008-04-17 14:15:45 -0400537static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 size_t count, loff_t *ppos)
539
540{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400541 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ssize_t length;
543 void *data = NULL;
544
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400545 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500547 length = avc_has_perm(&selinux_state,
548 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500549 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (length)
551 goto out;
552
Eric Parisb77a4932010-11-23 11:40:08 -0500553 /* No partial writes. */
554 length = -EINVAL;
555 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Eric Parisb77a4932010-11-23 11:40:08 -0500558 length = -EFBIG;
559 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500561
562 length = -ENOMEM;
563 data = vmalloc(count);
564 if (!data)
565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 length = -EFAULT;
568 if (copy_from_user(data, buf, count) != 0)
569 goto out;
570
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400571 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500572 if (length) {
573 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400577 length = sel_make_policy_nodes(fsi);
578 if (length)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400579 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500580
581 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400582
583out1:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400584 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400585 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700586 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500587 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400589 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 vfree(data);
591 return length;
592}
593
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800594static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200596 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597};
598
Eric Paris18729812008-04-17 14:15:45 -0400599static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400601 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
602 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500603 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800604 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ssize_t length;
606
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500607 length = avc_has_perm(&selinux_state,
608 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500609 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400613 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500614 if (length)
615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400617 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500618 if (length)
619 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800620
Eric Parisb77a4932010-11-23 11:40:08 -0500621 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800622 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200623 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400624 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800625 goto out;
626 }
627
628 memcpy(buf, canon, len);
629 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800631 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return length;
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
636 size_t count, loff_t *ppos)
637{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400638 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 char tmpbuf[TMPBUFLEN];
640 ssize_t length;
641
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400642 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
644}
645
Eric Paris18729812008-04-17 14:15:45 -0400646static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 size_t count, loff_t *ppos)
648{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400649 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500650 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ssize_t length;
652 unsigned int new_value;
653
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500654 length = avc_has_perm(&selinux_state,
655 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500656 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
657 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500659 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Davi Arnautbfd51622005-10-30 14:59:24 -0800661 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500662 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500663
664 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500665 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500666 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500667
Al Viro8365a712015-12-24 00:08:06 -0500668 page = memdup_user_nul(buf, count);
669 if (IS_ERR(page))
670 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 length = -EINVAL;
673 if (sscanf(page, "%u", &new_value) != 1)
674 goto out;
675
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400676 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 length = count;
678out:
Al Viro8365a712015-12-24 00:08:06 -0500679 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return length;
681}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800682static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .read = sel_read_checkreqprot,
684 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200685 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686};
687
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500688static ssize_t sel_write_validatetrans(struct file *file,
689 const char __user *buf,
690 size_t count, loff_t *ppos)
691{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400692 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
693 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500694 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
695 char *req = NULL;
696 u32 osid, nsid, tsid;
697 u16 tclass;
698 int rc;
699
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500700 rc = avc_has_perm(&selinux_state,
701 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500702 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500703 if (rc)
704 goto out;
705
706 rc = -ENOMEM;
707 if (count >= PAGE_SIZE)
708 goto out;
709
710 /* No partial writes. */
711 rc = -EINVAL;
712 if (*ppos != 0)
713 goto out;
714
Al Viro0b884d22017-05-13 18:12:07 -0400715 req = memdup_user_nul(buf, count);
716 if (IS_ERR(req)) {
717 rc = PTR_ERR(req);
718 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500719 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400720 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500721
722 rc = -ENOMEM;
723 oldcon = kzalloc(count + 1, GFP_KERNEL);
724 if (!oldcon)
725 goto out;
726
727 newcon = kzalloc(count + 1, GFP_KERNEL);
728 if (!newcon)
729 goto out;
730
731 taskcon = kzalloc(count + 1, GFP_KERNEL);
732 if (!taskcon)
733 goto out;
734
735 rc = -EINVAL;
736 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
737 goto out;
738
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400739 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500740 if (rc)
741 goto out;
742
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400743 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500744 if (rc)
745 goto out;
746
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400747 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500748 if (rc)
749 goto out;
750
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400751 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500752 if (!rc)
753 rc = count;
754out:
755 kfree(req);
756 kfree(oldcon);
757 kfree(newcon);
758 kfree(taskcon);
759 return rc;
760}
761
762static const struct file_operations sel_transition_ops = {
763 .write = sel_write_validatetrans,
764 .llseek = generic_file_llseek,
765};
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
769 */
Eric Paris18729812008-04-17 14:15:45 -0400770static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
771static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
772static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
773static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
774static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Eric Biggers631d2b42018-07-17 10:43:56 -0700776static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 [SEL_ACCESS] = sel_write_access,
778 [SEL_CREATE] = sel_write_create,
779 [SEL_RELABEL] = sel_write_relabel,
780 [SEL_USER] = sel_write_user,
781 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800782 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783};
784
785static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
786{
Al Viro496ad9a2013-01-23 17:07:38 -0500787 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 char *data;
789 ssize_t rv;
790
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800791 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EINVAL;
793
794 data = simple_transaction_get(file, buf, size);
795 if (IS_ERR(data))
796 return PTR_ERR(data);
797
Eric Paris18729812008-04-17 14:15:45 -0400798 rv = write_op[ino](file, data, size);
799 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 simple_transaction_set(file, rv);
801 rv = size;
802 }
803 return rv;
804}
805
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800806static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .write = selinux_transaction_write,
808 .read = simple_transaction_read,
809 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200810 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811};
812
813/*
814 * payload - write methods
815 * If the method has a response, the response should be put in buf,
816 * and the length returned. Otherwise return 0 or and -error.
817 */
818
Eric Paris18729812008-04-17 14:15:45 -0400819static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400821 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
822 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500823 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 u32 ssid, tsid;
825 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct av_decision avd;
827 ssize_t length;
828
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500829 length = avc_has_perm(&selinux_state,
830 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500831 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800836 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Eric Parisb77a4932010-11-23 11:40:08 -0500840 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800841 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!tcon)
843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500846 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400849 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500850 if (length)
851 goto out;
852
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400853 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500854 if (length)
855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400857 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900860 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500861 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900863 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864out:
Eric Parisb77a4932010-11-23 11:40:08 -0500865 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 kfree(scon);
867 return length;
868}
869
Eric Paris18729812008-04-17 14:15:45 -0400870static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400872 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
873 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500874 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100875 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 u32 ssid, tsid, newsid;
877 u16 tclass;
878 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500879 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100881 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500883 length = avc_has_perm(&selinux_state,
884 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500885 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
886 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800891 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Eric Parisb77a4932010-11-23 11:40:08 -0500895 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800896 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (!tcon)
898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100900 length = -ENOMEM;
901 namebuf = kzalloc(size + 1, GFP_KERNEL);
902 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100905 length = -EINVAL;
906 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
907 if (nargs < 3 || nargs > 4)
908 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400909 if (nargs == 4) {
910 /*
911 * If and when the name of new object to be queried contains
912 * either whitespace or multibyte characters, they shall be
913 * encoded based on the percentage-encoding rule.
914 * If not encoded, the sscanf logic picks up only left-half
915 * of the supplied name; splitted by a whitespace unexpectedly.
916 */
917 char *r, *w;
918 int c1, c2;
919
920 r = w = namebuf;
921 do {
922 c1 = *r++;
923 if (c1 == '+')
924 c1 = ' ';
925 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800926 c1 = hex_to_bin(*r++);
927 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400928 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800929 c2 = hex_to_bin(*r++);
930 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400931 goto out;
932 c1 = (c1 << 4) | c2;
933 }
934 *w++ = c1;
935 } while (c1 != '\0');
936
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100937 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400938 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100939
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400940 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500941 if (length)
942 goto out;
943
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400944 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500945 if (length)
946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400948 length = security_transition_sid_user(state, ssid, tsid, tclass,
949 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500950 if (length)
951 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400953 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500954 if (length)
955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Eric Parisb77a4932010-11-23 11:40:08 -0500957 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +0200959 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -0400960 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
964 memcpy(buf, newcon, len);
965 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966out:
Eric Parisb77a4932010-11-23 11:40:08 -0500967 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100968 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500969 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 kfree(scon);
971 return length;
972}
973
Eric Paris18729812008-04-17 14:15:45 -0400974static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400976 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
977 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500978 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 u32 ssid, tsid, newsid;
980 u16 tclass;
981 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500982 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 u32 len;
984
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500985 length = avc_has_perm(&selinux_state,
986 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500987 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
988 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800993 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric Parisb77a4932010-11-23 11:40:08 -0500997 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800998 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!tcon)
1000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 length = -EINVAL;
1003 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001006 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001007 if (length)
1008 goto out;
1009
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001010 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001011 if (length)
1012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001014 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001015 if (length)
1016 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001018 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001019 if (length)
1020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Eric Parisb77a4932010-11-23 11:40:08 -05001022 length = -ERANGE;
1023 if (len > SIMPLE_TRANSACTION_LIMIT)
1024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 memcpy(buf, newcon, len);
1027 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028out:
Eric Parisb77a4932010-11-23 11:40:08 -05001029 kfree(newcon);
1030 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 kfree(scon);
1032 return length;
1033}
1034
Eric Paris18729812008-04-17 14:15:45 -04001035static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001037 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1038 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001039 char *con = NULL, *user = NULL, *ptr;
1040 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ssize_t length;
1042 char *newcon;
1043 int i, rc;
1044 u32 len, nsids;
1045
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001046 length = avc_has_perm(&selinux_state,
1047 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001048 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1049 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001054 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Eric Parisb77a4932010-11-23 11:40:08 -05001058 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001059 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!user)
1061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 length = -EINVAL;
1064 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001067 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001068 if (length)
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001071 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001072 if (length)
1073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 length = sprintf(buf, "%u", nsids) + 1;
1076 ptr = buf + length;
1077 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001078 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (rc) {
1080 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1084 kfree(newcon);
1085 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088 memcpy(ptr, newcon, len);
1089 kfree(newcon);
1090 ptr += len;
1091 length += len;
1092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093out:
Eric Parisb77a4932010-11-23 11:40:08 -05001094 kfree(sids);
1095 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(con);
1097 return length;
1098}
1099
Eric Paris18729812008-04-17 14:15:45 -04001100static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001102 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1103 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001104 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 u32 ssid, tsid, newsid;
1106 u16 tclass;
1107 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001108 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 u32 len;
1110
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001111 length = avc_has_perm(&selinux_state,
1112 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001113 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1114 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001119 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Eric Parisb77a4932010-11-23 11:40:08 -05001123 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001124 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (!tcon)
1126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 length = -EINVAL;
1129 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001132 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001133 if (length)
1134 goto out;
1135
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001136 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001137 if (length)
1138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001140 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001141 if (length)
1142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001144 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001145 if (length)
1146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Eric Parisb77a4932010-11-23 11:40:08 -05001148 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (len > SIMPLE_TRANSACTION_LIMIT) {
peter enderborgf8b69a52018-06-12 10:09:06 +02001150 pr_err("SELinux: %s: context size (%u) exceeds "
Eric Paris744ba352008-04-17 11:52:44 -04001151 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 memcpy(buf, newcon, len);
1156 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157out:
Eric Parisb77a4932010-11-23 11:40:08 -05001158 kfree(newcon);
1159 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 kfree(scon);
1161 return length;
1162}
1163
1164static struct inode *sel_make_inode(struct super_block *sb, int mode)
1165{
1166 struct inode *ret = new_inode(sb);
1167
1168 if (ret) {
1169 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001170 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 return ret;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1176 size_t count, loff_t *ppos)
1177{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001178 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 char *page = NULL;
1180 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 ssize_t ret;
1182 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001183 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001184 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001186 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Eric Parisb77a4932010-11-23 11:40:08 -05001188 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001189 if (index >= fsi->bool_num || strcmp(name,
1190 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Eric Parisb77a4932010-11-23 11:40:08 -05001193 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001194 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001195 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001198 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (cur_enforcing < 0) {
1200 ret = cur_enforcing;
1201 goto out;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001204 fsi->bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001205 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001207 mutex_unlock(&fsi->mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001208 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return ret;
1210}
1211
1212static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1213 size_t count, loff_t *ppos)
1214{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001215 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001217 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001219 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001220 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001222 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001224 length = avc_has_perm(&selinux_state,
1225 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001226 SECCLASS_SECURITY, SECURITY__SETBOOL,
1227 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (length)
1229 goto out;
1230
Eric Parisb77a4932010-11-23 11:40:08 -05001231 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001232 if (index >= fsi->bool_num || strcmp(name,
1233 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001234 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001235
Eric Parisb77a4932010-11-23 11:40:08 -05001236 length = -ENOMEM;
1237 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001239
Eric Parisb77a4932010-11-23 11:40:08 -05001240 /* No partial writes. */
1241 length = -EINVAL;
1242 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001244
Al Viro8365a712015-12-24 00:08:06 -05001245 page = memdup_user_nul(buf, count);
1246 if (IS_ERR(page)) {
1247 length = PTR_ERR(page);
1248 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 length = -EINVAL;
1253 if (sscanf(page, "%d", &new_value) != 1)
1254 goto out;
1255
1256 if (new_value)
1257 new_value = 1;
1258
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001259 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 length = count;
1261
1262out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001263 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001264 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return length;
1266}
1267
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001268static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001269 .read = sel_read_bool,
1270 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001271 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272};
1273
1274static ssize_t sel_commit_bools_write(struct file *filep,
1275 const char __user *buf,
1276 size_t count, loff_t *ppos)
1277{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001278 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001280 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int new_value;
1282
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001283 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001285 length = avc_has_perm(&selinux_state,
1286 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001287 SECCLASS_SECURITY, SECURITY__SETBOOL,
1288 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (length)
1290 goto out;
1291
Eric Parisb77a4932010-11-23 11:40:08 -05001292 length = -ENOMEM;
1293 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001295
1296 /* No partial writes. */
1297 length = -EINVAL;
1298 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001300
Al Viro8365a712015-12-24 00:08:06 -05001301 page = memdup_user_nul(buf, count);
1302 if (IS_ERR(page)) {
1303 length = PTR_ERR(page);
1304 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 length = -EINVAL;
1309 if (sscanf(page, "%d", &new_value) != 1)
1310 goto out;
1311
Eric Parisb77a4932010-11-23 11:40:08 -05001312 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001313 if (new_value && fsi->bool_pending_values)
1314 length = security_set_bools(fsi->state, fsi->bool_num,
1315 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Eric Parisb77a4932010-11-23 11:40:08 -05001317 if (!length)
1318 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001321 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001322 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return length;
1324}
1325
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001326static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001327 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001328 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329};
1330
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001331static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Al Viroad521842014-12-24 14:56:48 -05001333 d_genocide(de);
1334 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337#define BOOL_DIR_NAME "booleans"
1338
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001339static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Eric Parisb77a4932010-11-23 11:40:08 -05001341 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 ssize_t len;
1343 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001344 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 struct inode *inode = NULL;
1346 struct inode_security_struct *isec;
1347 char **names = NULL, *page;
1348 int num;
1349 int *values = NULL;
1350 u32 sid;
1351
1352 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001353 for (i = 0; i < fsi->bool_num; i++)
1354 kfree(fsi->bool_pending_names[i]);
1355 kfree(fsi->bool_pending_names);
1356 kfree(fsi->bool_pending_values);
1357 fsi->bool_num = 0;
1358 fsi->bool_pending_names = NULL;
1359 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001361 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Eric Parisb77a4932010-11-23 11:40:08 -05001363 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001364 page = (char *)get_zeroed_page(GFP_KERNEL);
1365 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001368 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001369 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto out;
1371
1372 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001373 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001375 if (!dentry)
1376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Eric Parisb77a4932010-11-23 11:40:08 -05001378 ret = -ENOMEM;
1379 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
nixiaoming7e4237f2018-08-05 17:10:36 +08001380 if (!inode) {
1381 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001382 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001383 }
Eric Parisb77a4932010-11-23 11:40:08 -05001384
Eric Parisb77a4932010-11-23 11:40:08 -05001385 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001386 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
nixiaoming7e4237f2018-08-05 17:10:36 +08001387 if (len >= PAGE_SIZE) {
1388 dput(dentry);
1389 iput(inode);
Eric Parisb77a4932010-11-23 11:40:08 -05001390 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001391 }
Eric Parisb77a4932010-11-23 11:40:08 -05001392
Eric Paris18729812008-04-17 14:15:45 -04001393 isec = (struct inode_security_struct *)inode->i_security;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001394 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001395 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001396 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001397 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1398 page);
1399 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001400 }
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001403 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001405 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 d_add(dentry, inode);
1407 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001408 fsi->bool_num = num;
1409 fsi->bool_pending_names = names;
1410 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001411
1412 free_page((unsigned long)page);
1413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414out:
1415 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001418 for (i = 0; i < num; i++)
1419 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 kfree(names);
1421 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001422 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001423 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001424
1425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1429 size_t count, loff_t *ppos)
1430{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001431 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1432 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 char tmpbuf[TMPBUFLEN];
1434 ssize_t length;
1435
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001436 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1437 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1439}
1440
Eric Paris18729812008-04-17 14:15:45 -04001441static ssize_t sel_write_avc_cache_threshold(struct file *file,
1442 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 size_t count, loff_t *ppos)
1444
1445{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001446 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1447 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001448 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001450 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001452 ret = avc_has_perm(&selinux_state,
1453 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001454 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1455 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001456 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Parisb77a4932010-11-23 11:40:08 -05001459 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001460 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Eric Parisb77a4932010-11-23 11:40:08 -05001462 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001463 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001464 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001465
Al Viro8365a712015-12-24 00:08:06 -05001466 page = memdup_user_nul(buf, count);
1467 if (IS_ERR(page))
1468 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Eric Parisb77a4932010-11-23 11:40:08 -05001470 ret = -EINVAL;
1471 if (sscanf(page, "%u", &new_value) != 1)
1472 goto out;
1473
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001474 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477out:
Al Viro8365a712015-12-24 00:08:06 -05001478 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return ret;
1480}
1481
1482static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1483 size_t count, loff_t *ppos)
1484{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001485 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1486 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001488 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001491 if (!page)
1492 return -ENOMEM;
1493
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001494 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001495 if (length >= 0)
1496 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001498
1499 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001502static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 .read = sel_read_avc_cache_threshold,
1504 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001505 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506};
1507
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001508static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001510 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511};
1512
1513#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1514static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1515{
1516 int cpu;
1517
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301518 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (!cpu_possible(cpu))
1520 continue;
1521 *idx = cpu + 1;
1522 return &per_cpu(avc_cache_stats, cpu);
1523 }
1524 return NULL;
1525}
1526
1527static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1528{
1529 loff_t n = *pos - 1;
1530
1531 if (*pos == 0)
1532 return SEQ_START_TOKEN;
1533
1534 return sel_avc_get_stat_idx(&n);
1535}
1536
1537static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1538{
1539 return sel_avc_get_stat_idx(pos);
1540}
1541
1542static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1543{
1544 struct avc_cache_stats *st = v;
1545
Markus Elfring710a0642017-01-15 14:04:53 +01001546 if (v == SEQ_START_TOKEN) {
1547 seq_puts(seq,
1548 "lookups hits misses allocations reclaims frees\n");
1549 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001550 unsigned int lookups = st->lookups;
1551 unsigned int misses = st->misses;
1552 unsigned int hits = lookups - misses;
1553 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1554 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
1558}
1559
1560static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1561{ }
1562
Jan Engelhardt1996a102008-01-23 00:02:58 +01001563static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 .start = sel_avc_stats_seq_start,
1565 .next = sel_avc_stats_seq_next,
1566 .show = sel_avc_stats_seq_show,
1567 .stop = sel_avc_stats_seq_stop,
1568};
1569
1570static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1571{
1572 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1573}
1574
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001575static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 .open = sel_open_avc_cache_stats,
1577 .read = seq_read,
1578 .llseek = seq_lseek,
1579 .release = seq_release,
1580};
1581#endif
1582
1583static int sel_make_avc_files(struct dentry *dir)
1584{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001585 struct super_block *sb = dir->d_sb;
1586 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001587 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001588 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 { "cache_threshold",
1590 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1591 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1592#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1593 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1594#endif
1595 };
1596
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001597 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct inode *inode;
1599 struct dentry *dentry;
1600
1601 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001602 if (!dentry)
1603 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
nixiaoming7e4237f2018-08-05 17:10:36 +08001606 if (!inode) {
1607 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001608 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001609 }
Eric Parisb77a4932010-11-23 11:40:08 -05001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001612 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 d_add(dentry, inode);
1614 }
Eric Parisb77a4932010-11-23 11:40:08 -05001615
1616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Eric Paris18729812008-04-17 14:15:45 -04001619static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001620 size_t count, loff_t *ppos)
1621{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001622 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001623 char *con;
1624 u32 sid, len;
1625 ssize_t ret;
1626
Al Viro496ad9a2013-01-23 17:07:38 -05001627 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001628 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001629 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001630 return ret;
1631
1632 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1633 kfree(con);
1634 return ret;
1635}
1636
1637static const struct file_operations sel_initcon_ops = {
1638 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001639 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001640};
1641
1642static int sel_make_initcon_files(struct dentry *dir)
1643{
Eric Parisb77a4932010-11-23 11:40:08 -05001644 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001645
1646 for (i = 1; i <= SECINITSID_NUM; i++) {
1647 struct inode *inode;
1648 struct dentry *dentry;
1649 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001650 if (!dentry)
1651 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001652
1653 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001654 if (!inode) {
1655 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001656 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001657 }
Eric Parisb77a4932010-11-23 11:40:08 -05001658
James Carterf0ee2e42007-04-04 10:11:29 -04001659 inode->i_fop = &sel_initcon_ops;
1660 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1661 d_add(dentry, inode);
1662 }
Eric Parisb77a4932010-11-23 11:40:08 -05001663
1664 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001665}
1666
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001667static inline unsigned long sel_class_to_ino(u16 class)
1668{
1669 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1670}
1671
1672static inline u16 sel_ino_to_class(unsigned long ino)
1673{
Eric Paris92ae9e82012-04-04 13:46:46 -04001674 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001675}
1676
1677static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1678{
1679 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1680}
1681
1682static inline u32 sel_ino_to_perm(unsigned long ino)
1683{
1684 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1685}
1686
Eric Paris18729812008-04-17 14:15:45 -04001687static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001688 size_t count, loff_t *ppos)
1689{
Al Viro496ad9a2013-01-23 17:07:38 -05001690 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001691 char res[TMPBUFLEN];
1692 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1693 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001694}
1695
1696static const struct file_operations sel_class_ops = {
1697 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001698 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001699};
1700
Eric Paris18729812008-04-17 14:15:45 -04001701static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001702 size_t count, loff_t *ppos)
1703{
Al Viro496ad9a2013-01-23 17:07:38 -05001704 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001705 char res[TMPBUFLEN];
1706 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1707 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001708}
1709
1710static const struct file_operations sel_perm_ops = {
1711 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001712 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001713};
1714
Paul Moore3bb56b22008-01-29 08:38:19 -05001715static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1716 size_t count, loff_t *ppos)
1717{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001718 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001719 int value;
1720 char tmpbuf[TMPBUFLEN];
1721 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001722 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001723
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001724 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001725 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1726
1727 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1728}
1729
1730static const struct file_operations sel_policycap_ops = {
1731 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001732 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001733};
1734
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001735static int sel_make_perm_files(char *objclass, int classvalue,
1736 struct dentry *dir)
1737{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001738 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001739 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001740 char **perms;
1741
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001742 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001743 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001744 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001745
1746 for (i = 0; i < nperms; i++) {
1747 struct inode *inode;
1748 struct dentry *dentry;
1749
Eric Parisb77a4932010-11-23 11:40:08 -05001750 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001751 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001752 if (!dentry)
1753 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001754
Eric Parisb77a4932010-11-23 11:40:08 -05001755 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001756 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001757 if (!inode) {
1758 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001759 goto out;
nixiaoming7e4237f2018-08-05 17:10:36 +08001760 }
Eric Parisb77a4932010-11-23 11:40:08 -05001761
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001762 inode->i_fop = &sel_perm_ops;
1763 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001764 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001765 d_add(dentry, inode);
1766 }
Eric Parisb77a4932010-11-23 11:40:08 -05001767 rc = 0;
1768out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001769 for (i = 0; i < nperms; i++)
1770 kfree(perms[i]);
1771 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001772 return rc;
1773}
1774
1775static int sel_make_class_dir_entries(char *classname, int index,
1776 struct dentry *dir)
1777{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001778 struct super_block *sb = dir->d_sb;
1779 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001780 struct dentry *dentry = NULL;
1781 struct inode *inode = NULL;
1782 int rc;
1783
1784 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001785 if (!dentry)
1786 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001787
1788 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001789 if (!inode) {
1790 dput(dentry);
Eric Parisb77a4932010-11-23 11:40:08 -05001791 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001792 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001793
1794 inode->i_fop = &sel_class_ops;
1795 inode->i_ino = sel_class_to_ino(index);
1796 d_add(dentry, inode);
1797
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001798 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001799 if (IS_ERR(dentry))
1800 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001801
1802 rc = sel_make_perm_files(classname, index, dentry);
1803
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001804 return rc;
1805}
1806
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001807static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001808{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001809
Eric Parisb77a4932010-11-23 11:40:08 -05001810 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001811 char **classes;
1812
1813 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001814 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001815
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001816 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001817 if (rc)
1818 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001819
1820 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001821 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001822
1823 for (i = 0; i < nclasses; i++) {
1824 struct dentry *class_name_dir;
1825
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001826 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1827 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001828 if (IS_ERR(class_name_dir)) {
1829 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001830 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001831 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001832
1833 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001834 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001835 class_name_dir);
1836 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001837 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001838 }
Eric Parisb77a4932010-11-23 11:40:08 -05001839 rc = 0;
1840out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001841 for (i = 0; i < nclasses; i++)
1842 kfree(classes[i]);
1843 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001844 return rc;
1845}
1846
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001847static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001848{
1849 unsigned int iter;
1850 struct dentry *dentry = NULL;
1851 struct inode *inode = NULL;
1852
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001853 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001854
1855 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001856 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001857 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001858 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001859 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001860 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001861
1862 if (dentry == NULL)
1863 return -ENOMEM;
1864
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001865 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
nixiaoming7e4237f2018-08-05 17:10:36 +08001866 if (inode == NULL) {
1867 dput(dentry);
Paul Moore3bb56b22008-01-29 08:38:19 -05001868 return -ENOMEM;
nixiaoming7e4237f2018-08-05 17:10:36 +08001869 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001870
1871 inode->i_fop = &sel_policycap_ops;
1872 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1873 d_add(dentry, inode);
1874 }
1875
1876 return 0;
1877}
1878
Al Viroa1c2aa12012-03-18 20:36:59 -04001879static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001880 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Al Viroa1c2aa12012-03-18 20:36:59 -04001882 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct inode *inode;
1884
Al Viroa1c2aa12012-03-18 20:36:59 -04001885 if (!dentry)
1886 return ERR_PTR(-ENOMEM);
1887
1888 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1889 if (!inode) {
1890 dput(dentry);
1891 return ERR_PTR(-ENOMEM);
1892 }
Eric Parisb77a4932010-11-23 11:40:08 -05001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 inode->i_op = &simple_dir_inode_operations;
1895 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001896 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001897 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001898 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001900 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001901 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001902
Al Viroa1c2aa12012-03-18 20:36:59 -04001903 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001906#define NULL_FILE_NAME "null"
1907
Eric Paris18729812008-04-17 14:15:45 -04001908static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001910 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int ret;
1912 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001913 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct inode_security_struct *isec;
1915
Eric Biggerscda37122017-03-25 21:15:37 -07001916 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1918 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001919 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1921 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1922 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1923 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1924 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1925 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1926 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1927 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1928 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1929 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001930 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1931 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001932 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001933 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001934 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1935 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /* last one */ {""}
1937 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001938
1939 ret = selinux_fs_info_create(sb);
1940 if (ret)
1941 goto err;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1944 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001945 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001947 fsi = sb->s_fs_info;
1948 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1949 if (IS_ERR(fsi->bool_dir)) {
1950 ret = PTR_ERR(fsi->bool_dir);
1951 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001952 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Eric Parisb77a4932010-11-23 11:40:08 -05001955 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001957 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001958 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Eric Parisb77a4932010-11-23 11:40:08 -05001960 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
nixiaoming7e4237f2018-08-05 17:10:36 +08001962 if (!inode) {
1963 dput(dentry);
James Morris161ce452006-03-22 00:09:17 -08001964 goto err;
nixiaoming7e4237f2018-08-05 17:10:36 +08001965 }
Eric Parisb77a4932010-11-23 11:40:08 -05001966
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001967 inode->i_ino = ++fsi->last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001968 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 isec->sid = SECINITSID_DEVNULL;
1970 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001971 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1974 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001976 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001977 if (IS_ERR(dentry)) {
1978 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001979 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 ret = sel_make_avc_files(dentry);
1983 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001984 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001985
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001986 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001987 if (IS_ERR(dentry)) {
1988 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001989 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001990 }
James Carterf0ee2e42007-04-04 10:11:29 -04001991
1992 ret = sel_make_initcon_files(dentry);
1993 if (ret)
1994 goto err;
1995
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001996 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1997 if (IS_ERR(fsi->class_dir)) {
1998 ret = PTR_ERR(fsi->class_dir);
1999 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002000 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002001 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002002
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002003 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
2004 &fsi->last_ino);
2005 if (IS_ERR(fsi->policycap_dir)) {
2006 ret = PTR_ERR(fsi->policycap_dir);
2007 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04002008 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04002009 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002010
2011 ret = sel_make_policy_nodes(fsi);
2012 if (ret)
2013 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05002014 return 0;
James Morris161ce452006-03-22 00:09:17 -08002015err:
peter enderborgf8b69a52018-06-12 10:09:06 +02002016 pr_err("SELinux: %s: failed while creating inodes\n",
Eric Paris744ba352008-04-17 11:52:44 -04002017 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002018
2019 selinux_fs_info_free(sb);
2020
Eric Parisb77a4932010-11-23 11:40:08 -05002021 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Al Virofc14f2f2010-07-25 01:48:30 +04002024static struct dentry *sel_mount(struct file_system_type *fs_type,
2025 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Al Virofc14f2f2010-07-25 01:48:30 +04002027 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002030static void sel_kill_sb(struct super_block *sb)
2031{
2032 selinux_fs_info_free(sb);
2033 kill_litter_super(sb);
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036static struct file_system_type sel_fs_type = {
2037 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002038 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002039 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040};
2041
2042struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002043struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045static int __init init_sel_fs(void)
2046{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002047 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2048 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int err;
2050
2051 if (!selinux_enabled)
2052 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002053
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002054 err = sysfs_create_mount_point(fs_kobj, "selinux");
2055 if (err)
2056 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002059 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002060 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002061 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002062 }
Eric Parisb77a4932010-11-23 11:40:08 -05002063
Al Viro765927b2012-06-26 21:58:53 +04002064 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002065 if (IS_ERR(selinuxfs_mount)) {
peter enderborgf8b69a52018-06-12 10:09:06 +02002066 pr_err("selinuxfs: could not mount!\n");
Eric Parisb77a4932010-11-23 11:40:08 -05002067 err = PTR_ERR(selinuxfs_mount);
2068 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002070 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2071 &null_name);
2072 if (IS_ERR(selinux_null.dentry)) {
2073 pr_err("selinuxfs: could not lookup null!\n");
2074 err = PTR_ERR(selinux_null.dentry);
2075 selinux_null.dentry = NULL;
2076 }
Eric Parisb77a4932010-11-23 11:40:08 -05002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return err;
2079}
2080
2081__initcall(init_sel_fs);
2082
2083#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2084void exit_sel_fs(void)
2085{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002086 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002087 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002088 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unregister_filesystem(&sel_fs_type);
2090}
2091#endif