blob: c0cadbc5f85cd23fbb37ee20f2d88dba3e97f779 [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;
Steve Grubbaf601e42006-01-04 14:08:39 +0000170 audit_log(current->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;
Steve Grubbaf601e42006-01-04 14:08:39 +0000306 audit_log(current->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:
Steve Grubbaf601e42006-01-04 14:08:39 +0000584 audit_log(current->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) {
Eric Paris744ba352008-04-17 11:52:44 -0400623 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
624 "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
776static ssize_t (*write_op[])(struct file *, char *, size_t) = {
777 [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) {
Eric Paris744ba352008-04-17 11:52:44 -0400959 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
960 "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) {
Eric Paris744ba352008-04-17 11:52:44 -04001150 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1151 "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);
1380 if (!inode)
1381 goto out;
1382
Eric Parisb77a4932010-11-23 11:40:08 -05001383 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001384 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001385 if (len >= PAGE_SIZE)
1386 goto out;
1387
Eric Paris18729812008-04-17 14:15:45 -04001388 isec = (struct inode_security_struct *)inode->i_security;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001389 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001390 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001391 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001392 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1393 page);
1394 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001395 }
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001398 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001400 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 d_add(dentry, inode);
1402 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001403 fsi->bool_num = num;
1404 fsi->bool_pending_names = names;
1405 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001406
1407 free_page((unsigned long)page);
1408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409out:
1410 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001413 for (i = 0; i < num; i++)
1414 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 kfree(names);
1416 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001417 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001418 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001419
1420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1424 size_t count, loff_t *ppos)
1425{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001426 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1427 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 char tmpbuf[TMPBUFLEN];
1429 ssize_t length;
1430
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001431 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1432 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1434}
1435
Eric Paris18729812008-04-17 14:15:45 -04001436static ssize_t sel_write_avc_cache_threshold(struct file *file,
1437 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 size_t count, loff_t *ppos)
1439
1440{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001441 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1442 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001443 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001445 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001447 ret = avc_has_perm(&selinux_state,
1448 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001449 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1450 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001451 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Eric Parisb77a4932010-11-23 11:40:08 -05001454 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Eric Parisb77a4932010-11-23 11:40:08 -05001457 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001458 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001459 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001460
Al Viro8365a712015-12-24 00:08:06 -05001461 page = memdup_user_nul(buf, count);
1462 if (IS_ERR(page))
1463 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Eric Parisb77a4932010-11-23 11:40:08 -05001465 ret = -EINVAL;
1466 if (sscanf(page, "%u", &new_value) != 1)
1467 goto out;
1468
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001469 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472out:
Al Viro8365a712015-12-24 00:08:06 -05001473 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return ret;
1475}
1476
1477static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1478 size_t count, loff_t *ppos)
1479{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001480 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1481 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001483 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001486 if (!page)
1487 return -ENOMEM;
1488
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001489 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001490 if (length >= 0)
1491 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001493
1494 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001497static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 .read = sel_read_avc_cache_threshold,
1499 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001500 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501};
1502
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001503static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001505 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506};
1507
1508#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1509static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1510{
1511 int cpu;
1512
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301513 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!cpu_possible(cpu))
1515 continue;
1516 *idx = cpu + 1;
1517 return &per_cpu(avc_cache_stats, cpu);
1518 }
1519 return NULL;
1520}
1521
1522static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1523{
1524 loff_t n = *pos - 1;
1525
1526 if (*pos == 0)
1527 return SEQ_START_TOKEN;
1528
1529 return sel_avc_get_stat_idx(&n);
1530}
1531
1532static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1533{
1534 return sel_avc_get_stat_idx(pos);
1535}
1536
1537static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1538{
1539 struct avc_cache_stats *st = v;
1540
Markus Elfring710a0642017-01-15 14:04:53 +01001541 if (v == SEQ_START_TOKEN) {
1542 seq_puts(seq,
1543 "lookups hits misses allocations reclaims frees\n");
1544 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001545 unsigned int lookups = st->lookups;
1546 unsigned int misses = st->misses;
1547 unsigned int hits = lookups - misses;
1548 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1549 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 0;
1553}
1554
1555static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1556{ }
1557
Jan Engelhardt1996a102008-01-23 00:02:58 +01001558static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .start = sel_avc_stats_seq_start,
1560 .next = sel_avc_stats_seq_next,
1561 .show = sel_avc_stats_seq_show,
1562 .stop = sel_avc_stats_seq_stop,
1563};
1564
1565static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1566{
1567 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1568}
1569
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001570static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .open = sel_open_avc_cache_stats,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = seq_release,
1575};
1576#endif
1577
1578static int sel_make_avc_files(struct dentry *dir)
1579{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001580 struct super_block *sb = dir->d_sb;
1581 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001582 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001583 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 { "cache_threshold",
1585 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1586 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1587#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1588 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1589#endif
1590 };
1591
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001592 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct inode *inode;
1594 struct dentry *dentry;
1595
1596 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001597 if (!dentry)
1598 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001601 if (!inode)
1602 return -ENOMEM;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001605 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 d_add(dentry, inode);
1607 }
Eric Parisb77a4932010-11-23 11:40:08 -05001608
1609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Eric Paris18729812008-04-17 14:15:45 -04001612static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001613 size_t count, loff_t *ppos)
1614{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001615 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001616 char *con;
1617 u32 sid, len;
1618 ssize_t ret;
1619
Al Viro496ad9a2013-01-23 17:07:38 -05001620 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001621 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001622 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001623 return ret;
1624
1625 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1626 kfree(con);
1627 return ret;
1628}
1629
1630static const struct file_operations sel_initcon_ops = {
1631 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001632 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001633};
1634
1635static int sel_make_initcon_files(struct dentry *dir)
1636{
Eric Parisb77a4932010-11-23 11:40:08 -05001637 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001638
1639 for (i = 1; i <= SECINITSID_NUM; i++) {
1640 struct inode *inode;
1641 struct dentry *dentry;
1642 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001643 if (!dentry)
1644 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001645
1646 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001647 if (!inode)
1648 return -ENOMEM;
1649
James Carterf0ee2e42007-04-04 10:11:29 -04001650 inode->i_fop = &sel_initcon_ops;
1651 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1652 d_add(dentry, inode);
1653 }
Eric Parisb77a4932010-11-23 11:40:08 -05001654
1655 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001656}
1657
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001658static inline unsigned long sel_class_to_ino(u16 class)
1659{
1660 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1661}
1662
1663static inline u16 sel_ino_to_class(unsigned long ino)
1664{
Eric Paris92ae9e82012-04-04 13:46:46 -04001665 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001666}
1667
1668static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1669{
1670 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1671}
1672
1673static inline u32 sel_ino_to_perm(unsigned long ino)
1674{
1675 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1676}
1677
Eric Paris18729812008-04-17 14:15:45 -04001678static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001679 size_t count, loff_t *ppos)
1680{
Al Viro496ad9a2013-01-23 17:07:38 -05001681 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001682 char res[TMPBUFLEN];
1683 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1684 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001685}
1686
1687static const struct file_operations sel_class_ops = {
1688 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001689 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001690};
1691
Eric Paris18729812008-04-17 14:15:45 -04001692static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001693 size_t count, loff_t *ppos)
1694{
Al Viro496ad9a2013-01-23 17:07:38 -05001695 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001696 char res[TMPBUFLEN];
1697 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1698 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001699}
1700
1701static const struct file_operations sel_perm_ops = {
1702 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001703 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001704};
1705
Paul Moore3bb56b22008-01-29 08:38:19 -05001706static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1707 size_t count, loff_t *ppos)
1708{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001709 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001710 int value;
1711 char tmpbuf[TMPBUFLEN];
1712 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001713 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001714
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001715 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001716 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1717
1718 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1719}
1720
1721static const struct file_operations sel_policycap_ops = {
1722 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001723 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001724};
1725
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001726static int sel_make_perm_files(char *objclass, int classvalue,
1727 struct dentry *dir)
1728{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001729 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001730 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001731 char **perms;
1732
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001733 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001734 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001735 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001736
1737 for (i = 0; i < nperms; i++) {
1738 struct inode *inode;
1739 struct dentry *dentry;
1740
Eric Parisb77a4932010-11-23 11:40:08 -05001741 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001742 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001743 if (!dentry)
1744 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001745
Eric Parisb77a4932010-11-23 11:40:08 -05001746 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001747 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001748 if (!inode)
1749 goto out;
1750
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001751 inode->i_fop = &sel_perm_ops;
1752 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001753 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001754 d_add(dentry, inode);
1755 }
Eric Parisb77a4932010-11-23 11:40:08 -05001756 rc = 0;
1757out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001758 for (i = 0; i < nperms; i++)
1759 kfree(perms[i]);
1760 kfree(perms);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001761 return rc;
1762}
1763
1764static int sel_make_class_dir_entries(char *classname, int index,
1765 struct dentry *dir)
1766{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001767 struct super_block *sb = dir->d_sb;
1768 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001769 struct dentry *dentry = NULL;
1770 struct inode *inode = NULL;
1771 int rc;
1772
1773 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001774 if (!dentry)
1775 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001776
1777 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001778 if (!inode)
1779 return -ENOMEM;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001780
1781 inode->i_fop = &sel_class_ops;
1782 inode->i_ino = sel_class_to_ino(index);
1783 d_add(dentry, inode);
1784
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001785 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001786 if (IS_ERR(dentry))
1787 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001788
1789 rc = sel_make_perm_files(classname, index, dentry);
1790
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001791 return rc;
1792}
1793
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001794static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001795{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001796
Eric Parisb77a4932010-11-23 11:40:08 -05001797 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001798 char **classes;
1799
1800 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001801 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001802
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001803 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001804 if (rc)
1805 return rc;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001806
1807 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001808 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001809
1810 for (i = 0; i < nclasses; i++) {
1811 struct dentry *class_name_dir;
1812
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001813 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1814 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001815 if (IS_ERR(class_name_dir)) {
1816 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001817 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001818 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001819
1820 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001821 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001822 class_name_dir);
1823 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001824 goto out;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001825 }
Eric Parisb77a4932010-11-23 11:40:08 -05001826 rc = 0;
1827out:
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001828 for (i = 0; i < nclasses; i++)
1829 kfree(classes[i]);
1830 kfree(classes);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001831 return rc;
1832}
1833
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001834static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001835{
1836 unsigned int iter;
1837 struct dentry *dentry = NULL;
1838 struct inode *inode = NULL;
1839
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001840 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001841
1842 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001843 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001844 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001845 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001846 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001847 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001848
1849 if (dentry == NULL)
1850 return -ENOMEM;
1851
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001852 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
Paul Moore3bb56b22008-01-29 08:38:19 -05001853 if (inode == NULL)
1854 return -ENOMEM;
1855
1856 inode->i_fop = &sel_policycap_ops;
1857 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1858 d_add(dentry, inode);
1859 }
1860
1861 return 0;
1862}
1863
Al Viroa1c2aa12012-03-18 20:36:59 -04001864static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001865 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Al Viroa1c2aa12012-03-18 20:36:59 -04001867 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct inode *inode;
1869
Al Viroa1c2aa12012-03-18 20:36:59 -04001870 if (!dentry)
1871 return ERR_PTR(-ENOMEM);
1872
1873 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1874 if (!inode) {
1875 dput(dentry);
1876 return ERR_PTR(-ENOMEM);
1877 }
Eric Parisb77a4932010-11-23 11:40:08 -05001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 inode->i_op = &simple_dir_inode_operations;
1880 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001881 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001882 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001883 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001885 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001886 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001887
Al Viroa1c2aa12012-03-18 20:36:59 -04001888 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001891#define NULL_FILE_NAME "null"
1892
Eric Paris18729812008-04-17 14:15:45 -04001893static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001895 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 int ret;
1897 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001898 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct inode_security_struct *isec;
1900
Eric Biggerscda37122017-03-25 21:15:37 -07001901 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1903 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001904 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1906 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1907 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1909 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1910 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1911 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1912 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1913 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1914 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001915 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1916 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001917 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001918 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001919 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1920 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* last one */ {""}
1922 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001923
1924 ret = selinux_fs_info_create(sb);
1925 if (ret)
1926 goto err;
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1929 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001930 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001932 fsi = sb->s_fs_info;
1933 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1934 if (IS_ERR(fsi->bool_dir)) {
1935 ret = PTR_ERR(fsi->bool_dir);
1936 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001937 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Eric Parisb77a4932010-11-23 11:40:08 -05001940 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001942 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001943 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Eric Parisb77a4932010-11-23 11:40:08 -05001945 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001947 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001948 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001949
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001950 inode->i_ino = ++fsi->last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001951 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 isec->sid = SECINITSID_DEVNULL;
1953 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001954 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1957 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001959 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001960 if (IS_ERR(dentry)) {
1961 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001962 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 ret = sel_make_avc_files(dentry);
1966 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001967 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001968
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001969 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001970 if (IS_ERR(dentry)) {
1971 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001972 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001973 }
James Carterf0ee2e42007-04-04 10:11:29 -04001974
1975 ret = sel_make_initcon_files(dentry);
1976 if (ret)
1977 goto err;
1978
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001979 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1980 if (IS_ERR(fsi->class_dir)) {
1981 ret = PTR_ERR(fsi->class_dir);
1982 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001983 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001984 }
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001985
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001986 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1987 &fsi->last_ino);
1988 if (IS_ERR(fsi->policycap_dir)) {
1989 ret = PTR_ERR(fsi->policycap_dir);
1990 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001991 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001992 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001993
1994 ret = sel_make_policy_nodes(fsi);
1995 if (ret)
1996 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001997 return 0;
James Morris161ce452006-03-22 00:09:17 -08001998err:
Eric Paris744ba352008-04-17 11:52:44 -04001999 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
2000 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002001
2002 selinux_fs_info_free(sb);
2003
Eric Parisb77a4932010-11-23 11:40:08 -05002004 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Al Virofc14f2f2010-07-25 01:48:30 +04002007static struct dentry *sel_mount(struct file_system_type *fs_type,
2008 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Al Virofc14f2f2010-07-25 01:48:30 +04002010 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002013static void sel_kill_sb(struct super_block *sb)
2014{
2015 selinux_fs_info_free(sb);
2016 kill_litter_super(sb);
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static struct file_system_type sel_fs_type = {
2020 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002021 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002022 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023};
2024
2025struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002026struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028static int __init init_sel_fs(void)
2029{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002030 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2031 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int err;
2033
2034 if (!selinux_enabled)
2035 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002036
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002037 err = sysfs_create_mount_point(fs_kobj, "selinux");
2038 if (err)
2039 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002042 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002043 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002044 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002045 }
Eric Parisb77a4932010-11-23 11:40:08 -05002046
Al Viro765927b2012-06-26 21:58:53 +04002047 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002048 if (IS_ERR(selinuxfs_mount)) {
2049 printk(KERN_ERR "selinuxfs: could not mount!\n");
2050 err = PTR_ERR(selinuxfs_mount);
2051 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002053 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2054 &null_name);
2055 if (IS_ERR(selinux_null.dentry)) {
2056 pr_err("selinuxfs: could not lookup null!\n");
2057 err = PTR_ERR(selinux_null.dentry);
2058 selinux_null.dentry = NULL;
2059 }
Eric Parisb77a4932010-11-23 11:40:08 -05002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return err;
2062}
2063
2064__initcall(init_sel_fs);
2065
2066#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2067void exit_sel_fs(void)
2068{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002069 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002070 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002071 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 unregister_filesystem(&sel_fs_type);
2073}
2074#endif