blob: 19e35dd695d7a4efda74b270a00ceb1e074ab7cc [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. PeBenitoe47c8fc52007-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. PeBenitoe47c8fc52007-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{
444 struct policy_load_memory *plm = filp->private_data;
445 int ret;
446
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500447 ret = avc_has_perm(&selinux_state,
448 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500449 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400450 if (ret)
Jann Horn0da74122018-06-28 20:39:54 -0400451 return ret;
Eric Pariscee74f42010-10-13 17:50:25 -0400452
Jann Horn0da74122018-06-28 20:39:54 -0400453 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400454}
455
Souptick Joarderac9a1f62018-04-14 21:02:41 +0530456static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
Eric Paris845ca302010-10-13 17:50:31 -0400457{
Dave Jiang11bac802017-02-24 14:56:41 -0800458 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
Eric Paris845ca302010-10-13 17:50:31 -0400459 unsigned long offset;
460 struct page *page;
461
462 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
463 return VM_FAULT_SIGBUS;
464
465 offset = vmf->pgoff << PAGE_SHIFT;
466 if (offset >= roundup(plm->len, PAGE_SIZE))
467 return VM_FAULT_SIGBUS;
468
469 page = vmalloc_to_page(plm->data + offset);
470 get_page(page);
471
472 vmf->page = page;
473
474 return 0;
475}
476
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700477static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400478 .fault = sel_mmap_policy_fault,
479 .page_mkwrite = sel_mmap_policy_fault,
480};
481
James Morrisad3fa082011-08-30 10:50:12 +1000482static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400483{
484 if (vma->vm_flags & VM_SHARED) {
485 /* do not allow mprotect to make mapping writable */
486 vma->vm_flags &= ~VM_MAYWRITE;
487
488 if (vma->vm_flags & VM_WRITE)
489 return -EACCES;
490 }
491
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700492 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400493 vma->vm_ops = &sel_mmap_policy_ops;
494
495 return 0;
496}
497
Eric Pariscee74f42010-10-13 17:50:25 -0400498static const struct file_operations sel_policy_ops = {
499 .open = sel_open_policy,
500 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400501 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400502 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500503 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400504};
505
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400506static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
507{
508 int ret;
509
510 ret = sel_make_bools(fsi);
511 if (ret) {
512 pr_err("SELinux: failed to load policy booleans\n");
513 return ret;
514 }
515
516 ret = sel_make_classes(fsi);
517 if (ret) {
518 pr_err("SELinux: failed to load policy classes\n");
519 return ret;
520 }
521
522 ret = sel_make_policycap(fsi);
523 if (ret) {
524 pr_err("SELinux: failed to load policy capabilities\n");
525 return ret;
526 }
527
528 return 0;
529}
530
Eric Paris18729812008-04-17 14:15:45 -0400531static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 size_t count, loff_t *ppos)
533
534{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400535 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ssize_t length;
537 void *data = NULL;
538
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400539 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500541 length = avc_has_perm(&selinux_state,
542 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500543 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (length)
545 goto out;
546
Eric Parisb77a4932010-11-23 11:40:08 -0500547 /* No partial writes. */
548 length = -EINVAL;
549 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Eric Parisb77a4932010-11-23 11:40:08 -0500552 length = -EFBIG;
553 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500555
556 length = -ENOMEM;
557 data = vmalloc(count);
558 if (!data)
559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 length = -EFAULT;
562 if (copy_from_user(data, buf, count) != 0)
563 goto out;
564
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400565 length = security_load_policy(fsi->state, data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500566 if (length) {
567 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400571 length = sel_make_policy_nodes(fsi);
572 if (length)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400573 goto out1;
Eric Parisb77a4932010-11-23 11:40:08 -0500574
575 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400576
577out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000578 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Richard Guy Briggsd1411362018-04-09 19:36:31 -0400579 "auid=%u ses=%u lsm=selinux res=1",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700580 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500581 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400583 mutex_unlock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 vfree(data);
585 return length;
586}
587
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800588static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200590 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
592
Eric Paris18729812008-04-17 14:15:45 -0400593static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400595 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
596 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500597 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800598 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ssize_t length;
600
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500601 length = avc_has_perm(&selinux_state,
602 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500603 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400607 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500608 if (length)
609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400611 length = security_sid_to_context(state, sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500612 if (length)
613 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800614
Eric Parisb77a4932010-11-23 11:40:08 -0500615 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800616 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400617 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
618 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800619 goto out;
620 }
621
622 memcpy(buf, canon, len);
623 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800625 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return length;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
630 size_t count, loff_t *ppos)
631{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400632 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 char tmpbuf[TMPBUFLEN];
634 ssize_t length;
635
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400636 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
638}
639
Eric Paris18729812008-04-17 14:15:45 -0400640static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 size_t count, loff_t *ppos)
642{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400643 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500644 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ssize_t length;
646 unsigned int new_value;
647
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500648 length = avc_has_perm(&selinux_state,
649 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500650 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
651 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500653 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Davi Arnautbfd51622005-10-30 14:59:24 -0800655 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500656 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500657
658 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500659 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500660 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500661
Al Viro8365a712015-12-24 00:08:06 -0500662 page = memdup_user_nul(buf, count);
663 if (IS_ERR(page))
664 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 length = -EINVAL;
667 if (sscanf(page, "%u", &new_value) != 1)
668 goto out;
669
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400670 fsi->state->checkreqprot = new_value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 length = count;
672out:
Al Viro8365a712015-12-24 00:08:06 -0500673 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return length;
675}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800676static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .read = sel_read_checkreqprot,
678 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200679 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680};
681
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500682static ssize_t sel_write_validatetrans(struct file *file,
683 const char __user *buf,
684 size_t count, loff_t *ppos)
685{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400686 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
687 struct selinux_state *state = fsi->state;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500688 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
689 char *req = NULL;
690 u32 osid, nsid, tsid;
691 u16 tclass;
692 int rc;
693
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500694 rc = avc_has_perm(&selinux_state,
695 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500696 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500697 if (rc)
698 goto out;
699
700 rc = -ENOMEM;
701 if (count >= PAGE_SIZE)
702 goto out;
703
704 /* No partial writes. */
705 rc = -EINVAL;
706 if (*ppos != 0)
707 goto out;
708
Al Viro0b884d22017-05-13 18:12:07 -0400709 req = memdup_user_nul(buf, count);
710 if (IS_ERR(req)) {
711 rc = PTR_ERR(req);
712 req = NULL;
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500713 goto out;
Al Viro0b884d22017-05-13 18:12:07 -0400714 }
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500715
716 rc = -ENOMEM;
717 oldcon = kzalloc(count + 1, GFP_KERNEL);
718 if (!oldcon)
719 goto out;
720
721 newcon = kzalloc(count + 1, GFP_KERNEL);
722 if (!newcon)
723 goto out;
724
725 taskcon = kzalloc(count + 1, GFP_KERNEL);
726 if (!taskcon)
727 goto out;
728
729 rc = -EINVAL;
730 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
731 goto out;
732
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400733 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500734 if (rc)
735 goto out;
736
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400737 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500738 if (rc)
739 goto out;
740
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400741 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500742 if (rc)
743 goto out;
744
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400745 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500746 if (!rc)
747 rc = count;
748out:
749 kfree(req);
750 kfree(oldcon);
751 kfree(newcon);
752 kfree(taskcon);
753 return rc;
754}
755
756static const struct file_operations sel_transition_ops = {
757 .write = sel_write_validatetrans,
758 .llseek = generic_file_llseek,
759};
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/*
762 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
763 */
Eric Paris18729812008-04-17 14:15:45 -0400764static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
765static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
766static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
767static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
768static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static ssize_t (*write_op[])(struct file *, char *, size_t) = {
771 [SEL_ACCESS] = sel_write_access,
772 [SEL_CREATE] = sel_write_create,
773 [SEL_RELABEL] = sel_write_relabel,
774 [SEL_USER] = sel_write_user,
775 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800776 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777};
778
779static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
780{
Al Viro496ad9a2013-01-23 17:07:38 -0500781 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 char *data;
783 ssize_t rv;
784
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800785 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EINVAL;
787
788 data = simple_transaction_get(file, buf, size);
789 if (IS_ERR(data))
790 return PTR_ERR(data);
791
Eric Paris18729812008-04-17 14:15:45 -0400792 rv = write_op[ino](file, data, size);
793 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 simple_transaction_set(file, rv);
795 rv = size;
796 }
797 return rv;
798}
799
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800800static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .write = selinux_transaction_write,
802 .read = simple_transaction_read,
803 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200804 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805};
806
807/*
808 * payload - write methods
809 * If the method has a response, the response should be put in buf,
810 * and the length returned. Otherwise return 0 or and -error.
811 */
812
Eric Paris18729812008-04-17 14:15:45 -0400813static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400815 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
816 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500817 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 u32 ssid, tsid;
819 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct av_decision avd;
821 ssize_t length;
822
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500823 length = avc_has_perm(&selinux_state,
824 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500825 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800830 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Eric Parisb77a4932010-11-23 11:40:08 -0500834 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800835 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!tcon)
837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500840 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400843 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500844 if (length)
845 goto out;
846
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400847 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500848 if (length)
849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400851 security_compute_av_user(state, ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900854 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500855 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900857 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out:
Eric Parisb77a4932010-11-23 11:40:08 -0500859 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 kfree(scon);
861 return length;
862}
863
Eric Paris18729812008-04-17 14:15:45 -0400864static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400866 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
867 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500868 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100869 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 u32 ssid, tsid, newsid;
871 u16 tclass;
872 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500873 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100875 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500877 length = avc_has_perm(&selinux_state,
878 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500879 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
880 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800885 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Eric Parisb77a4932010-11-23 11:40:08 -0500889 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800890 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!tcon)
892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100894 length = -ENOMEM;
895 namebuf = kzalloc(size + 1, GFP_KERNEL);
896 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500897 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100899 length = -EINVAL;
900 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
901 if (nargs < 3 || nargs > 4)
902 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400903 if (nargs == 4) {
904 /*
905 * If and when the name of new object to be queried contains
906 * either whitespace or multibyte characters, they shall be
907 * encoded based on the percentage-encoding rule.
908 * If not encoded, the sscanf logic picks up only left-half
909 * of the supplied name; splitted by a whitespace unexpectedly.
910 */
911 char *r, *w;
912 int c1, c2;
913
914 r = w = namebuf;
915 do {
916 c1 = *r++;
917 if (c1 == '+')
918 c1 = ' ';
919 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800920 c1 = hex_to_bin(*r++);
921 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400922 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800923 c2 = hex_to_bin(*r++);
924 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400925 goto out;
926 c1 = (c1 << 4) | c2;
927 }
928 *w++ = c1;
929 } while (c1 != '\0');
930
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100931 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400932 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100933
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400934 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500935 if (length)
936 goto out;
937
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400938 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500939 if (length)
940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400942 length = security_transition_sid_user(state, ssid, tsid, tclass,
943 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500944 if (length)
945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400947 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500948 if (length)
949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Eric Parisb77a4932010-11-23 11:40:08 -0500951 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400953 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
954 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
958 memcpy(buf, newcon, len);
959 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960out:
Eric Parisb77a4932010-11-23 11:40:08 -0500961 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100962 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500963 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 kfree(scon);
965 return length;
966}
967
Eric Paris18729812008-04-17 14:15:45 -0400968static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400970 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
971 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500972 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 u32 ssid, tsid, newsid;
974 u16 tclass;
975 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500976 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 u32 len;
978
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500979 length = avc_has_perm(&selinux_state,
980 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500981 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
982 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800987 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Eric Parisb77a4932010-11-23 11:40:08 -0500991 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800992 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!tcon)
994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 length = -EINVAL;
997 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001000 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001001 if (length)
1002 goto out;
1003
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001004 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001005 if (length)
1006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001008 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001009 if (length)
1010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001012 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001013 if (length)
1014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Eric Parisb77a4932010-11-23 11:40:08 -05001016 length = -ERANGE;
1017 if (len > SIMPLE_TRANSACTION_LIMIT)
1018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 memcpy(buf, newcon, len);
1021 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022out:
Eric Parisb77a4932010-11-23 11:40:08 -05001023 kfree(newcon);
1024 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 kfree(scon);
1026 return length;
1027}
1028
Eric Paris18729812008-04-17 14:15:45 -04001029static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001031 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1032 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001033 char *con = NULL, *user = NULL, *ptr;
1034 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 ssize_t length;
1036 char *newcon;
1037 int i, rc;
1038 u32 len, nsids;
1039
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001040 length = avc_has_perm(&selinux_state,
1041 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001042 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1043 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001048 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Eric Parisb77a4932010-11-23 11:40:08 -05001052 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001053 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!user)
1055 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 length = -EINVAL;
1058 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -05001059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001061 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001062 if (length)
1063 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001065 length = security_get_user_sids(state, sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001066 if (length)
1067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 length = sprintf(buf, "%u", nsids) + 1;
1070 ptr = buf + length;
1071 for (i = 0; i < nsids; i++) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001072 rc = security_sid_to_context(state, sids[i], &newcon, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (rc) {
1074 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1078 kfree(newcon);
1079 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001080 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 memcpy(ptr, newcon, len);
1083 kfree(newcon);
1084 ptr += len;
1085 length += len;
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087out:
Eric Parisb77a4932010-11-23 11:40:08 -05001088 kfree(sids);
1089 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 kfree(con);
1091 return length;
1092}
1093
Eric Paris18729812008-04-17 14:15:45 -04001094static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001096 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1097 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -05001098 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 u32 ssid, tsid, newsid;
1100 u16 tclass;
1101 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001102 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 u32 len;
1104
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001105 length = avc_has_perm(&selinux_state,
1106 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001107 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1108 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001113 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Parisb77a4932010-11-23 11:40:08 -05001117 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001118 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!tcon)
1120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 length = -EINVAL;
1123 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001126 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001127 if (length)
1128 goto out;
1129
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001130 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001131 if (length)
1132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001134 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001135 if (length)
1136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001138 length = security_sid_to_context(state, newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001139 if (length)
1140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Eric Parisb77a4932010-11-23 11:40:08 -05001142 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001144 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1145 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149 memcpy(buf, newcon, len);
1150 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151out:
Eric Parisb77a4932010-11-23 11:40:08 -05001152 kfree(newcon);
1153 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 kfree(scon);
1155 return length;
1156}
1157
1158static struct inode *sel_make_inode(struct super_block *sb, int mode)
1159{
1160 struct inode *ret = new_inode(sb);
1161
1162 if (ret) {
1163 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001164 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 return ret;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1170 size_t count, loff_t *ppos)
1171{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001172 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 char *page = NULL;
1174 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ssize_t ret;
1176 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001177 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001178 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001180 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Eric Parisb77a4932010-11-23 11:40:08 -05001182 ret = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001183 if (index >= fsi->bool_num || strcmp(name,
1184 fsi->bool_pending_names[index]))
Jann Horn0da74122018-06-28 20:39:54 -04001185 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Eric Parisb77a4932010-11-23 11:40:08 -05001187 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001188 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001189 if (!page)
Jann Horn0da74122018-06-28 20:39:54 -04001190 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001192 cur_enforcing = security_get_bool_value(fsi->state, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (cur_enforcing < 0) {
1194 ret = cur_enforcing;
Jann Horn0da74122018-06-28 20:39:54 -04001195 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001198 fsi->bool_pending_values[index]);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001199 mutex_unlock(&fsi->mutex);
Jann Horn0da74122018-06-28 20:39:54 -04001200 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1201out_free:
Eric Parisb77a4932010-11-23 11:40:08 -05001202 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return ret;
Jann Horn0da74122018-06-28 20:39:54 -04001204
1205out_unlock:
1206 mutex_unlock(&fsi->mutex);
1207 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1211 size_t count, loff_t *ppos)
1212{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001213 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001215 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001217 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001218 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jann Horn0da74122018-06-28 20:39:54 -04001220 if (count >= PAGE_SIZE)
1221 return -ENOMEM;
1222
1223 /* No partial writes. */
1224 if (*ppos != 0)
1225 return -EINVAL;
1226
1227 page = memdup_user_nul(buf, count);
1228 if (IS_ERR(page))
1229 return PTR_ERR(page);
1230
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001231 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001233 length = avc_has_perm(&selinux_state,
1234 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001235 SECCLASS_SECURITY, SECURITY__SETBOOL,
1236 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (length)
1238 goto out;
1239
Eric Parisb77a4932010-11-23 11:40:08 -05001240 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001241 if (index >= fsi->bool_num || strcmp(name,
1242 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001243 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 length = -EINVAL;
1246 if (sscanf(page, "%d", &new_value) != 1)
1247 goto out;
1248
1249 if (new_value)
1250 new_value = 1;
1251
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001252 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 length = count;
1254
1255out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001256 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001257 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return length;
1259}
1260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001261static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001262 .read = sel_read_bool,
1263 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001264 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265};
1266
1267static ssize_t sel_commit_bools_write(struct file *filep,
1268 const char __user *buf,
1269 size_t count, loff_t *ppos)
1270{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001271 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001273 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int new_value;
1275
Jann Horn0da74122018-06-28 20:39:54 -04001276 if (count >= PAGE_SIZE)
1277 return -ENOMEM;
1278
1279 /* No partial writes. */
1280 if (*ppos != 0)
1281 return -EINVAL;
1282
1283 page = memdup_user_nul(buf, count);
1284 if (IS_ERR(page))
1285 return PTR_ERR(page);
1286
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001287 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001289 length = avc_has_perm(&selinux_state,
1290 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001291 SECCLASS_SECURITY, SECURITY__SETBOOL,
1292 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (length)
1294 goto out;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 length = -EINVAL;
1297 if (sscanf(page, "%d", &new_value) != 1)
1298 goto out;
1299
Eric Parisb77a4932010-11-23 11:40:08 -05001300 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001301 if (new_value && fsi->bool_pending_values)
1302 length = security_set_bools(fsi->state, fsi->bool_num,
1303 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Eric Parisb77a4932010-11-23 11:40:08 -05001305 if (!length)
1306 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001309 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001310 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return length;
1312}
1313
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001314static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001315 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001316 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317};
1318
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001319static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Al Viroad521842014-12-24 14:56:48 -05001321 d_genocide(de);
1322 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325#define BOOL_DIR_NAME "booleans"
1326
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001327static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Eric Parisb77a4932010-11-23 11:40:08 -05001329 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ssize_t len;
1331 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001332 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 struct inode *inode = NULL;
1334 struct inode_security_struct *isec;
1335 char **names = NULL, *page;
1336 int num;
1337 int *values = NULL;
1338 u32 sid;
1339
1340 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001341 for (i = 0; i < fsi->bool_num; i++)
1342 kfree(fsi->bool_pending_names[i]);
1343 kfree(fsi->bool_pending_names);
1344 kfree(fsi->bool_pending_values);
1345 fsi->bool_num = 0;
1346 fsi->bool_pending_names = NULL;
1347 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001349 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Eric Parisb77a4932010-11-23 11:40:08 -05001351 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001352 page = (char *)get_zeroed_page(GFP_KERNEL);
1353 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001356 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001357 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 goto out;
1359
1360 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001361 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001363 if (!dentry)
1364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric Parisb77a4932010-11-23 11:40:08 -05001366 ret = -ENOMEM;
1367 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1368 if (!inode)
1369 goto out;
1370
Eric Parisb77a4932010-11-23 11:40:08 -05001371 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001372 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001373 if (len >= PAGE_SIZE)
1374 goto out;
1375
Eric Paris18729812008-04-17 14:15:45 -04001376 isec = (struct inode_security_struct *)inode->i_security;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001377 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001378 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001379 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001380 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1381 page);
1382 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001386 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001388 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 d_add(dentry, inode);
1390 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001391 fsi->bool_num = num;
1392 fsi->bool_pending_names = names;
1393 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001394
1395 free_page((unsigned long)page);
1396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397out:
1398 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001401 for (i = 0; i < num; i++)
1402 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 kfree(names);
1404 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001405 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001406 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001407
1408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1412 size_t count, loff_t *ppos)
1413{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001414 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1415 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 char tmpbuf[TMPBUFLEN];
1417 ssize_t length;
1418
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001419 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1420 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1422}
1423
Eric Paris18729812008-04-17 14:15:45 -04001424static ssize_t sel_write_avc_cache_threshold(struct file *file,
1425 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 size_t count, loff_t *ppos)
1427
1428{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001429 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1430 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001431 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001433 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001435 ret = avc_has_perm(&selinux_state,
1436 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001437 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1438 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001439 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Eric Parisb77a4932010-11-23 11:40:08 -05001442 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001443 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Eric Parisb77a4932010-11-23 11:40:08 -05001445 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001446 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001447 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001448
Al Viro8365a712015-12-24 00:08:06 -05001449 page = memdup_user_nul(buf, count);
1450 if (IS_ERR(page))
1451 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Parisb77a4932010-11-23 11:40:08 -05001453 ret = -EINVAL;
1454 if (sscanf(page, "%u", &new_value) != 1)
1455 goto out;
1456
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001457 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460out:
Al Viro8365a712015-12-24 00:08:06 -05001461 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return ret;
1463}
1464
1465static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1466 size_t count, loff_t *ppos)
1467{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001468 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1469 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001471 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001474 if (!page)
1475 return -ENOMEM;
1476
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001477 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001478 if (length >= 0)
1479 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001481
1482 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001485static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 .read = sel_read_avc_cache_threshold,
1487 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001488 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489};
1490
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001491static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001493 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494};
1495
1496#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1497static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1498{
1499 int cpu;
1500
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301501 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!cpu_possible(cpu))
1503 continue;
1504 *idx = cpu + 1;
1505 return &per_cpu(avc_cache_stats, cpu);
1506 }
1507 return NULL;
1508}
1509
1510static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1511{
1512 loff_t n = *pos - 1;
1513
1514 if (*pos == 0)
1515 return SEQ_START_TOKEN;
1516
1517 return sel_avc_get_stat_idx(&n);
1518}
1519
1520static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1521{
1522 return sel_avc_get_stat_idx(pos);
1523}
1524
1525static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1526{
1527 struct avc_cache_stats *st = v;
1528
Markus Elfring710a0642017-01-15 14:04:53 +01001529 if (v == SEQ_START_TOKEN) {
1530 seq_puts(seq,
1531 "lookups hits misses allocations reclaims frees\n");
1532 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001533 unsigned int lookups = st->lookups;
1534 unsigned int misses = st->misses;
1535 unsigned int hits = lookups - misses;
1536 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1537 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 0;
1541}
1542
1543static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1544{ }
1545
Jan Engelhardt1996a102008-01-23 00:02:58 +01001546static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 .start = sel_avc_stats_seq_start,
1548 .next = sel_avc_stats_seq_next,
1549 .show = sel_avc_stats_seq_show,
1550 .stop = sel_avc_stats_seq_stop,
1551};
1552
1553static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1554{
1555 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1556}
1557
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001558static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .open = sel_open_avc_cache_stats,
1560 .read = seq_read,
1561 .llseek = seq_lseek,
1562 .release = seq_release,
1563};
1564#endif
1565
1566static int sel_make_avc_files(struct dentry *dir)
1567{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001568 struct super_block *sb = dir->d_sb;
1569 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001570 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001571 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 { "cache_threshold",
1573 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1574 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1575#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1576 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1577#endif
1578 };
1579
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001580 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct inode *inode;
1582 struct dentry *dentry;
1583
1584 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001585 if (!dentry)
1586 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001589 if (!inode)
1590 return -ENOMEM;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001593 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 d_add(dentry, inode);
1595 }
Eric Parisb77a4932010-11-23 11:40:08 -05001596
1597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Eric Paris18729812008-04-17 14:15:45 -04001600static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001601 size_t count, loff_t *ppos)
1602{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001603 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001604 char *con;
1605 u32 sid, len;
1606 ssize_t ret;
1607
Al Viro496ad9a2013-01-23 17:07:38 -05001608 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001609 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001610 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001611 return ret;
1612
1613 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1614 kfree(con);
1615 return ret;
1616}
1617
1618static const struct file_operations sel_initcon_ops = {
1619 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001620 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001621};
1622
1623static int sel_make_initcon_files(struct dentry *dir)
1624{
Eric Parisb77a4932010-11-23 11:40:08 -05001625 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001626
1627 for (i = 1; i <= SECINITSID_NUM; i++) {
1628 struct inode *inode;
1629 struct dentry *dentry;
1630 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001631 if (!dentry)
1632 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001633
1634 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001635 if (!inode)
1636 return -ENOMEM;
1637
James Carterf0ee2e42007-04-04 10:11:29 -04001638 inode->i_fop = &sel_initcon_ops;
1639 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1640 d_add(dentry, inode);
1641 }
Eric Parisb77a4932010-11-23 11:40:08 -05001642
1643 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001644}
1645
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001646static inline unsigned long sel_class_to_ino(u16 class)
1647{
1648 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1649}
1650
1651static inline u16 sel_ino_to_class(unsigned long ino)
1652{
Eric Paris92ae9e82012-04-04 13:46:46 -04001653 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001654}
1655
1656static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1657{
1658 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1659}
1660
1661static inline u32 sel_ino_to_perm(unsigned long ino)
1662{
1663 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1664}
1665
Eric Paris18729812008-04-17 14:15:45 -04001666static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001667 size_t count, loff_t *ppos)
1668{
Al Viro496ad9a2013-01-23 17:07:38 -05001669 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001670 char res[TMPBUFLEN];
1671 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1672 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001673}
1674
1675static const struct file_operations sel_class_ops = {
1676 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001677 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001678};
1679
Eric Paris18729812008-04-17 14:15:45 -04001680static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001681 size_t count, loff_t *ppos)
1682{
Al Viro496ad9a2013-01-23 17:07:38 -05001683 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001684 char res[TMPBUFLEN];
1685 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1686 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001687}
1688
1689static const struct file_operations sel_perm_ops = {
1690 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001691 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001692};
1693
Paul Moore3bb56b22008-01-29 08:38:19 -05001694static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1695 size_t count, loff_t *ppos)
1696{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001697 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001698 int value;
1699 char tmpbuf[TMPBUFLEN];
1700 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001701 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001702
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001703 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001704 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1705
1706 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1707}
1708
1709static const struct file_operations sel_policycap_ops = {
1710 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001711 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001712};
1713
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001714static int sel_make_perm_files(char *objclass, int classvalue,
1715 struct dentry *dir)
1716{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001717 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001718 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001719 char **perms;
1720
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001721 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001722 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001723 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001724
1725 for (i = 0; i < nperms; i++) {
1726 struct inode *inode;
1727 struct dentry *dentry;
1728
Eric Parisb77a4932010-11-23 11:40:08 -05001729 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001730 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001731 if (!dentry)
1732 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001733
Eric Parisb77a4932010-11-23 11:40:08 -05001734 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001735 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001736 if (!inode)
1737 goto out;
1738
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001739 inode->i_fop = &sel_perm_ops;
1740 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001741 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001742 d_add(dentry, inode);
1743 }
Eric Parisb77a4932010-11-23 11:40:08 -05001744 rc = 0;
1745out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001746 for (i = 0; i < nperms; i++)
1747 kfree(perms[i]);
1748 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001749 return rc;
1750}
1751
1752static int sel_make_class_dir_entries(char *classname, int index,
1753 struct dentry *dir)
1754{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001755 struct super_block *sb = dir->d_sb;
1756 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001757 struct dentry *dentry = NULL;
1758 struct inode *inode = NULL;
1759 int rc;
1760
1761 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001762 if (!dentry)
1763 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001764
1765 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001766 if (!inode)
1767 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001768
1769 inode->i_fop = &sel_class_ops;
1770 inode->i_ino = sel_class_to_ino(index);
1771 d_add(dentry, inode);
1772
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001773 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001774 if (IS_ERR(dentry))
1775 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001776
1777 rc = sel_make_perm_files(classname, index, dentry);
1778
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001779 return rc;
1780}
1781
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001782static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001783{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001784
Eric Parisb77a4932010-11-23 11:40:08 -05001785 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001786 char **classes;
1787
1788 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001789 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001790
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001791 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001792 if (rc)
1793 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001794
1795 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001796 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001797
1798 for (i = 0; i < nclasses; i++) {
1799 struct dentry *class_name_dir;
1800
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001801 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1802 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001803 if (IS_ERR(class_name_dir)) {
1804 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001805 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001806 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001807
1808 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001809 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001810 class_name_dir);
1811 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001812 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001813 }
Eric Parisb77a4932010-11-23 11:40:08 -05001814 rc = 0;
1815out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001816 for (i = 0; i < nclasses; i++)
1817 kfree(classes[i]);
1818 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001819 return rc;
1820}
1821
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001822static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001823{
1824 unsigned int iter;
1825 struct dentry *dentry = NULL;
1826 struct inode *inode = NULL;
1827
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001828 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001829
1830 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001831 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001832 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001833 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001834 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001835 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001836
1837 if (dentry == NULL)
1838 return -ENOMEM;
1839
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001840 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
Paul Moore3bb56b22008-01-29 08:38:19 -05001841 if (inode == NULL)
1842 return -ENOMEM;
1843
1844 inode->i_fop = &sel_policycap_ops;
1845 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1846 d_add(dentry, inode);
1847 }
1848
1849 return 0;
1850}
1851
Al Viroa1c2aa12012-03-18 20:36:59 -04001852static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001853 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Al Viroa1c2aa12012-03-18 20:36:59 -04001855 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct inode *inode;
1857
Al Viroa1c2aa12012-03-18 20:36:59 -04001858 if (!dentry)
1859 return ERR_PTR(-ENOMEM);
1860
1861 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1862 if (!inode) {
1863 dput(dentry);
1864 return ERR_PTR(-ENOMEM);
1865 }
Eric Parisb77a4932010-11-23 11:40:08 -05001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 inode->i_op = &simple_dir_inode_operations;
1868 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001869 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001870 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001871 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001873 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001874 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001875
Al Viroa1c2aa12012-03-18 20:36:59 -04001876 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001879#define NULL_FILE_NAME "null"
1880
Eric Paris18729812008-04-17 14:15:45 -04001881static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001883 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 int ret;
1885 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001886 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct inode_security_struct *isec;
1888
Eric Biggerscda37122017-03-25 21:15:37 -07001889 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1891 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001892 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1894 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1895 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1896 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1897 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1898 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1899 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1900 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1901 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1902 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001903 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1904 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001905 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001906 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001907 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1908 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* last one */ {""}
1910 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001911
1912 ret = selinux_fs_info_create(sb);
1913 if (ret)
1914 goto err;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1917 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001918 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001920 fsi = sb->s_fs_info;
1921 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1922 if (IS_ERR(fsi->bool_dir)) {
1923 ret = PTR_ERR(fsi->bool_dir);
1924 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001925 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Eric Parisb77a4932010-11-23 11:40:08 -05001928 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001930 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001931 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Eric Parisb77a4932010-11-23 11:40:08 -05001933 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001935 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001936 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001937
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001938 inode->i_ino = ++fsi->last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001939 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 isec->sid = SECINITSID_DEVNULL;
1941 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001942 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1945 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001947 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001948 if (IS_ERR(dentry)) {
1949 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001950 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 ret = sel_make_avc_files(dentry);
1954 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001955 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001956
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001957 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001958 if (IS_ERR(dentry)) {
1959 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001960 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001961 }
James Carterf0ee2e42007-04-04 10:11:29 -04001962
1963 ret = sel_make_initcon_files(dentry);
1964 if (ret)
1965 goto err;
1966
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001967 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1968 if (IS_ERR(fsi->class_dir)) {
1969 ret = PTR_ERR(fsi->class_dir);
1970 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001971 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001972 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001973
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001974 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1975 &fsi->last_ino);
1976 if (IS_ERR(fsi->policycap_dir)) {
1977 ret = PTR_ERR(fsi->policycap_dir);
1978 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001979 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001980 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001981
1982 ret = sel_make_policy_nodes(fsi);
1983 if (ret)
1984 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001985 return 0;
James Morris161ce452006-03-22 00:09:17 -08001986err:
Eric Paris744ba352008-04-17 11:52:44 -04001987 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1988 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001989
1990 selinux_fs_info_free(sb);
1991
Eric Parisb77a4932010-11-23 11:40:08 -05001992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Al Virofc14f2f2010-07-25 01:48:30 +04001995static struct dentry *sel_mount(struct file_system_type *fs_type,
1996 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Al Virofc14f2f2010-07-25 01:48:30 +04001998 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002001static void sel_kill_sb(struct super_block *sb)
2002{
2003 selinux_fs_info_free(sb);
2004 kill_litter_super(sb);
2005}
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007static struct file_system_type sel_fs_type = {
2008 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002009 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002010 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011};
2012
2013struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002014struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016static int __init init_sel_fs(void)
2017{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002018 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2019 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int err;
2021
2022 if (!selinux_enabled)
2023 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002024
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002025 err = sysfs_create_mount_point(fs_kobj, "selinux");
2026 if (err)
2027 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002030 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002031 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002032 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002033 }
Eric Parisb77a4932010-11-23 11:40:08 -05002034
Al Viro765927b2012-06-26 21:58:53 +04002035 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002036 if (IS_ERR(selinuxfs_mount)) {
2037 printk(KERN_ERR "selinuxfs: could not mount!\n");
2038 err = PTR_ERR(selinuxfs_mount);
2039 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002041 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2042 &null_name);
2043 if (IS_ERR(selinux_null.dentry)) {
2044 pr_err("selinuxfs: could not lookup null!\n");
2045 err = PTR_ERR(selinux_null.dentry);
2046 selinux_null.dentry = NULL;
2047 }
Eric Parisb77a4932010-11-23 11:40:08 -05002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return err;
2050}
2051
2052__initcall(init_sel_fs);
2053
2054#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2055void exit_sel_fs(void)
2056{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002057 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002058 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002059 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 unregister_filesystem(&sel_fs_type);
2061}
2062#endif