blob: 35fd77737c599416278a2b05ede272c2bdc832ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
Eric Paris18729812008-04-17 14:15:45 -04003 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Paul Moore82c21bf2011-08-01 11:10:33 +00005 * Updated: Hewlett-Packard <paul@paul-moore.com>
Paul Moore3bb56b22008-01-29 08:38:19 -05006 *
Eric Paris18729812008-04-17 14:15:45 -04007 * Added support for the policy capability bitmap
Paul Moore3bb56b22008-01-29 08:38:19 -05008 *
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
Eric Paris18729812008-04-17 14:15:45 -040013 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * the Free Software Foundation, version 2.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
21#include <linux/fs.h>
Stephen Smalley0619f0f2018-03-20 11:59:11 -040022#include <linux/mount.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/security.h>
27#include <linux/major.h>
28#include <linux/seq_file.h>
29#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000030#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040031#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070032#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040033#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* selinuxfs pseudo filesystem for exporting the security policy API.
36 Based on the proc code and the fs/nfsd/nfsctl.c code. */
37
38#include "flask.h"
39#include "avc.h"
40#include "avc_ss.h"
41#include "security.h"
42#include "objsec.h"
43#include "conditional.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045enum sel_inos {
46 SEL_ROOT_INO = 2,
47 SEL_LOAD, /* load policy */
48 SEL_ENFORCE, /* get or set enforcing status */
49 SEL_CONTEXT, /* validate context */
50 SEL_ACCESS, /* compute access decision */
51 SEL_CREATE, /* compute create labeling decision */
52 SEL_RELABEL, /* compute relabeling decision */
53 SEL_USER, /* compute reachable user contexts */
54 SEL_POLICYVERS, /* return policy version for this kernel */
55 SEL_COMMIT_BOOLS, /* commit new boolean values */
56 SEL_MLS, /* return if MLS policy is enabled */
57 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 SEL_MEMBER, /* compute polyinstantiation membership decision */
59 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070060 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040061 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
62 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090063 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040064 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -050065 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -040066 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Stephen Smalley0619f0f2018-03-20 11:59:11 -040069struct selinux_fs_info {
70 struct dentry *bool_dir;
71 unsigned int bool_num;
72 char **bool_pending_names;
73 unsigned int *bool_pending_values;
74 struct dentry *class_dir;
75 unsigned long last_class_ino;
76 bool policy_opened;
77 struct dentry *policycap_dir;
78 struct mutex mutex;
79 unsigned long last_ino;
80 struct selinux_state *state;
81 struct super_block *sb;
82};
83
84static int selinux_fs_info_create(struct super_block *sb)
85{
86 struct selinux_fs_info *fsi;
87
88 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
89 if (!fsi)
90 return -ENOMEM;
91
92 mutex_init(&fsi->mutex);
93 fsi->last_ino = SEL_INO_NEXT - 1;
94 fsi->state = &selinux_state;
95 fsi->sb = sb;
96 sb->s_fs_info = fsi;
97 return 0;
98}
99
100static void selinux_fs_info_free(struct super_block *sb)
101{
102 struct selinux_fs_info *fsi = sb->s_fs_info;
103 int i;
104
105 if (fsi) {
106 for (i = 0; i < fsi->bool_num; i++)
107 kfree(fsi->bool_pending_names[i]);
108 kfree(fsi->bool_pending_names);
109 kfree(fsi->bool_pending_values);
110 }
111 kfree(sb->s_fs_info);
112 sb->s_fs_info = NULL;
113}
James Carter6174eaf2007-04-04 16:18:39 -0400114
Paul Moore3bb56b22008-01-29 08:38:19 -0500115#define SEL_INITCON_INO_OFFSET 0x01000000
116#define SEL_BOOL_INO_OFFSET 0x02000000
117#define SEL_CLASS_INO_OFFSET 0x04000000
118#define SEL_POLICYCAP_INO_OFFSET 0x08000000
119#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define TMPBUFLEN 12
122static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
123 size_t count, loff_t *ppos)
124{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400125 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char tmpbuf[TMPBUFLEN];
127 ssize_t length;
128
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500129 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400130 enforcing_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
132}
133
134#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400135static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 size_t count, loff_t *ppos)
137
138{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400139 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
140 struct selinux_state *state = fsi->state;
Eric Parisb77a4932010-11-23 11:40:08 -0500141 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ssize_t length;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500143 int old_value, new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Davi Arnautbfd51622005-10-30 14:59:24 -0800145 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500146 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500147
148 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500149 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500150 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500151
Al Viro8365a712015-12-24 00:08:06 -0500152 page = memdup_user_nul(buf, count);
153 if (IS_ERR(page))
154 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 length = -EINVAL;
157 if (sscanf(page, "%d", &new_value) != 1)
158 goto out;
159
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500160 new_value = !!new_value;
161
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400162 old_value = enforcing_enabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500163 if (new_value != old_value) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500164 length = avc_has_perm(&selinux_state,
165 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500166 SECCLASS_SECURITY, SECURITY__SETENFORCE,
167 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (length)
169 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400170 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500171 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500172 new_value, old_value,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700173 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500174 audit_get_sessionid(current));
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400175 enforcing_set(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500176 if (new_value)
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500177 avc_ss_reset(state->avc, 0);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500178 selnl_notify_setenforce(new_value);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400179 selinux_status_update_setenforce(state, new_value);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 if (!new_value)
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300181 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 length = count;
184out:
Al Viro8365a712015-12-24 00:08:06 -0500185 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return length;
187}
188#else
189#define sel_write_enforce NULL
190#endif
191
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800192static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200195 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Eric Paris3f120702007-09-21 14:37:10 -0400198static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
199 size_t count, loff_t *ppos)
200{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400201 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
202 struct selinux_state *state = fsi->state;
Eric Paris3f120702007-09-21 14:37:10 -0400203 char tmpbuf[TMPBUFLEN];
204 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500205 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400206 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400207 security_get_reject_unknown(state) :
208 !security_get_allow_unknown(state);
Eric Paris3f120702007-09-21 14:37:10 -0400209
210 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
211 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
212}
213
214static const struct file_operations sel_handle_unknown_ops = {
215 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200216 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400217};
218
KaiGai Kohei11904162010-09-14 18:28:39 +0900219static int sel_open_handle_status(struct inode *inode, struct file *filp)
220{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400221 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
222 struct page *status = selinux_kernel_status_page(fsi->state);
KaiGai Kohei11904162010-09-14 18:28:39 +0900223
224 if (!status)
225 return -ENOMEM;
226
227 filp->private_data = status;
228
229 return 0;
230}
231
232static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
233 size_t count, loff_t *ppos)
234{
235 struct page *status = filp->private_data;
236
237 BUG_ON(!status);
238
239 return simple_read_from_buffer(buf, count, ppos,
240 page_address(status),
241 sizeof(struct selinux_kernel_status));
242}
243
244static int sel_mmap_handle_status(struct file *filp,
245 struct vm_area_struct *vma)
246{
247 struct page *status = filp->private_data;
248 unsigned long size = vma->vm_end - vma->vm_start;
249
250 BUG_ON(!status);
251
252 /* only allows one page from the head */
253 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
254 return -EIO;
255 /* disallow writable mapping */
256 if (vma->vm_flags & VM_WRITE)
257 return -EPERM;
258 /* disallow mprotect() turns it into writable */
259 vma->vm_flags &= ~VM_MAYWRITE;
260
261 return remap_pfn_range(vma, vma->vm_start,
262 page_to_pfn(status),
263 size, vma->vm_page_prot);
264}
265
266static const struct file_operations sel_handle_status_ops = {
267 .open = sel_open_handle_status,
268 .read = sel_read_handle_status,
269 .mmap = sel_mmap_handle_status,
270 .llseek = generic_file_llseek,
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400274static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 size_t count, loff_t *ppos)
276
277{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400278 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Al Viro8365a712015-12-24 00:08:06 -0500279 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ssize_t length;
281 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Davi Arnautbfd51622005-10-30 14:59:24 -0800283 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500284 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500285
286 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500287 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500288 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500289
Al Viro8365a712015-12-24 00:08:06 -0500290 page = memdup_user_nul(buf, count);
291 if (IS_ERR(page))
292 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 length = -EINVAL;
295 if (sscanf(page, "%d", &new_value) != 1)
296 goto out;
297
298 if (new_value) {
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400299 length = selinux_disable(fsi->state);
Eric Parisb77a4932010-11-23 11:40:08 -0500300 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400302 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500303 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700304 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500305 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 length = count;
309out:
Al Viro8365a712015-12-24 00:08:06 -0500310 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return length;
312}
313#else
314#define sel_write_disable NULL
315#endif
316
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800317static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200319 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400323 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 char tmpbuf[TMPBUFLEN];
326 ssize_t length;
327
328 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
329 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
330}
331
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800332static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200334 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337/* declaration for sel_write_load */
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400338static int sel_make_bools(struct selinux_fs_info *fsi);
339static int sel_make_classes(struct selinux_fs_info *fsi);
340static int sel_make_policycap(struct selinux_fs_info *fsi);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400341
342/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400343static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400344 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346static ssize_t sel_read_mls(struct file *filp, char __user *buf,
347 size_t count, loff_t *ppos)
348{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400349 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 char tmpbuf[TMPBUFLEN];
351 ssize_t length;
352
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100353 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400354 security_mls_enabled(fsi->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
356}
357
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800358static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200360 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
Eric Pariscee74f42010-10-13 17:50:25 -0400363struct policy_load_memory {
364 size_t len;
365 void *data;
366};
367
368static int sel_open_policy(struct inode *inode, struct file *filp)
369{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400370 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
371 struct selinux_state *state = fsi->state;
Eric Pariscee74f42010-10-13 17:50:25 -0400372 struct policy_load_memory *plm = NULL;
373 int rc;
374
375 BUG_ON(filp->private_data);
376
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400377 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400378
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500379 rc = avc_has_perm(&selinux_state,
380 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500381 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400382 if (rc)
383 goto err;
384
385 rc = -EBUSY;
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400386 if (fsi->policy_opened)
Eric Pariscee74f42010-10-13 17:50:25 -0400387 goto err;
388
389 rc = -ENOMEM;
390 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
391 if (!plm)
392 goto err;
393
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400394 if (i_size_read(inode) != security_policydb_len(state)) {
Al Viro59551022016-01-22 15:40:57 -0500395 inode_lock(inode);
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400396 i_size_write(inode, security_policydb_len(state));
Al Viro59551022016-01-22 15:40:57 -0500397 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400398 }
399
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400400 rc = security_read_policy(state, &plm->data, &plm->len);
Eric Pariscee74f42010-10-13 17:50:25 -0400401 if (rc)
402 goto err;
403
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400404 fsi->policy_opened = 1;
Eric Pariscee74f42010-10-13 17:50:25 -0400405
406 filp->private_data = plm;
407
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400408 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400409
410 return 0;
411err:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400412 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400413
414 if (plm)
415 vfree(plm->data);
416 kfree(plm);
417 return rc;
418}
419
420static int sel_release_policy(struct inode *inode, struct file *filp)
421{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400422 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400423 struct policy_load_memory *plm = filp->private_data;
424
425 BUG_ON(!plm);
426
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400427 fsi->policy_opened = 0;
Eric Pariscee74f42010-10-13 17:50:25 -0400428
429 vfree(plm->data);
430 kfree(plm);
431
432 return 0;
433}
434
435static ssize_t sel_read_policy(struct file *filp, char __user *buf,
436 size_t count, loff_t *ppos)
437{
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400438 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
Eric Pariscee74f42010-10-13 17:50:25 -0400439 struct policy_load_memory *plm = filp->private_data;
440 int ret;
441
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400442 mutex_lock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400443
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500444 ret = avc_has_perm(&selinux_state,
445 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500446 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400447 if (ret)
448 goto out;
449
450 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
451out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -0400452 mutex_unlock(&fsi->mutex);
Eric Pariscee74f42010-10-13 17:50:25 -0400453 return ret;
454}
455
Dave Jiang11bac802017-02-24 14:56:41 -0800456static int 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:
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400578 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500579 "policy loaded auid=%u ses=%u",
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]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001185 goto out;
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto out;
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;
1195 goto out;
1196 }
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 Smalley68bdcf22006-03-22 00:09:15 -08001199 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001201 mutex_unlock(&fsi->mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001202 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return ret;
1204}
1205
1206static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1207 size_t count, loff_t *ppos)
1208{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001209 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001211 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001213 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001214 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001216 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001218 length = avc_has_perm(&selinux_state,
1219 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001220 SECCLASS_SECURITY, SECURITY__SETBOOL,
1221 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (length)
1223 goto out;
1224
Eric Parisb77a4932010-11-23 11:40:08 -05001225 length = -EINVAL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001226 if (index >= fsi->bool_num || strcmp(name,
1227 fsi->bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001228 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001229
Eric Parisb77a4932010-11-23 11:40:08 -05001230 length = -ENOMEM;
1231 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001233
Eric Parisb77a4932010-11-23 11:40:08 -05001234 /* No partial writes. */
1235 length = -EINVAL;
1236 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001238
Al Viro8365a712015-12-24 00:08:06 -05001239 page = memdup_user_nul(buf, count);
1240 if (IS_ERR(page)) {
1241 length = PTR_ERR(page);
1242 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 length = -EINVAL;
1247 if (sscanf(page, "%d", &new_value) != 1)
1248 goto out;
1249
1250 if (new_value)
1251 new_value = 1;
1252
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001253 fsi->bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 length = count;
1255
1256out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001257 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001258 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return length;
1260}
1261
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001262static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001263 .read = sel_read_bool,
1264 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001265 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266};
1267
1268static ssize_t sel_commit_bools_write(struct file *filep,
1269 const char __user *buf,
1270 size_t count, loff_t *ppos)
1271{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001272 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001274 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int new_value;
1276
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001277 mutex_lock(&fsi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001279 length = avc_has_perm(&selinux_state,
1280 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001281 SECCLASS_SECURITY, SECURITY__SETBOOL,
1282 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (length)
1284 goto out;
1285
Eric Parisb77a4932010-11-23 11:40:08 -05001286 length = -ENOMEM;
1287 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001289
1290 /* No partial writes. */
1291 length = -EINVAL;
1292 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001294
Al Viro8365a712015-12-24 00:08:06 -05001295 page = memdup_user_nul(buf, count);
1296 if (IS_ERR(page)) {
1297 length = PTR_ERR(page);
1298 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 length = -EINVAL;
1303 if (sscanf(page, "%d", &new_value) != 1)
1304 goto out;
1305
Eric Parisb77a4932010-11-23 11:40:08 -05001306 length = 0;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001307 if (new_value && fsi->bool_pending_values)
1308 length = security_set_bools(fsi->state, fsi->bool_num,
1309 fsi->bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Eric Parisb77a4932010-11-23 11:40:08 -05001311 if (!length)
1312 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314out:
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001315 mutex_unlock(&fsi->mutex);
Al Viro8365a712015-12-24 00:08:06 -05001316 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return length;
1318}
1319
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001320static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001321 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001322 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323};
1324
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001325static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Al Viroad521842014-12-24 14:56:48 -05001327 d_genocide(de);
1328 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331#define BOOL_DIR_NAME "booleans"
1332
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001333static int sel_make_bools(struct selinux_fs_info *fsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Eric Parisb77a4932010-11-23 11:40:08 -05001335 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 ssize_t len;
1337 struct dentry *dentry = NULL;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001338 struct dentry *dir = fsi->bool_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct inode *inode = NULL;
1340 struct inode_security_struct *isec;
1341 char **names = NULL, *page;
1342 int num;
1343 int *values = NULL;
1344 u32 sid;
1345
1346 /* remove any existing files */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001347 for (i = 0; i < fsi->bool_num; i++)
1348 kfree(fsi->bool_pending_names[i]);
1349 kfree(fsi->bool_pending_names);
1350 kfree(fsi->bool_pending_values);
1351 fsi->bool_num = 0;
1352 fsi->bool_pending_names = NULL;
1353 fsi->bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001355 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Eric Parisb77a4932010-11-23 11:40:08 -05001357 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001358 page = (char *)get_zeroed_page(GFP_KERNEL);
1359 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001362 ret = security_get_bools(fsi->state, &num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001363 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto out;
1365
1366 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001367 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001369 if (!dentry)
1370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Eric Parisb77a4932010-11-23 11:40:08 -05001372 ret = -ENOMEM;
1373 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1374 if (!inode)
1375 goto out;
1376
Eric Parisb77a4932010-11-23 11:40:08 -05001377 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001378 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001379 if (len >= PAGE_SIZE)
1380 goto out;
1381
Eric Paris18729812008-04-17 14:15:45 -04001382 isec = (struct inode_security_struct *)inode->i_security;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001383 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001384 SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001385 if (ret) {
Gary Tierney900fde02017-01-09 10:07:32 -05001386 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1387 page);
1388 sid = SECINITSID_SECURITY;
Gary Tierney4262fb52017-01-09 10:07:31 -05001389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001392 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001394 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 d_add(dentry, inode);
1396 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001397 fsi->bool_num = num;
1398 fsi->bool_pending_names = names;
1399 fsi->bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001400
1401 free_page((unsigned long)page);
1402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403out:
1404 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001407 for (i = 0; i < num; i++)
1408 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 kfree(names);
1410 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001411 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001412 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001413
1414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1418 size_t count, loff_t *ppos)
1419{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001420 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1421 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 char tmpbuf[TMPBUFLEN];
1423 ssize_t length;
1424
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001425 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1426 avc_get_cache_threshold(state->avc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1428}
1429
Eric Paris18729812008-04-17 14:15:45 -04001430static ssize_t sel_write_avc_cache_threshold(struct file *file,
1431 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 size_t count, loff_t *ppos)
1433
1434{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001435 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1436 struct selinux_state *state = fsi->state;
Al Viro8365a712015-12-24 00:08:06 -05001437 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001439 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001441 ret = avc_has_perm(&selinux_state,
1442 current_sid(), SECINITSID_SECURITY,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001443 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1444 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001445 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Eric Parisb77a4932010-11-23 11:40:08 -05001448 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001449 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Eric Parisb77a4932010-11-23 11:40:08 -05001451 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001452 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001453 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001454
Al Viro8365a712015-12-24 00:08:06 -05001455 page = memdup_user_nul(buf, count);
1456 if (IS_ERR(page))
1457 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Parisb77a4932010-11-23 11:40:08 -05001459 ret = -EINVAL;
1460 if (sscanf(page, "%u", &new_value) != 1)
1461 goto out;
1462
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001463 avc_set_cache_threshold(state->avc, new_value);
Eric Parisb77a4932010-11-23 11:40:08 -05001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
Al Viro8365a712015-12-24 00:08:06 -05001467 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
1471static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1472 size_t count, loff_t *ppos)
1473{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001474 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1475 struct selinux_state *state = fsi->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001477 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001480 if (!page)
1481 return -ENOMEM;
1482
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001483 length = avc_get_hash_stats(state->avc, page);
Eric Parisb77a4932010-11-23 11:40:08 -05001484 if (length >= 0)
1485 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001487
1488 return length;
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_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .read = sel_read_avc_cache_threshold,
1493 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001494 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495};
1496
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001497static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001499 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500};
1501
1502#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1503static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1504{
1505 int cpu;
1506
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301507 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (!cpu_possible(cpu))
1509 continue;
1510 *idx = cpu + 1;
1511 return &per_cpu(avc_cache_stats, cpu);
1512 }
1513 return NULL;
1514}
1515
1516static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1517{
1518 loff_t n = *pos - 1;
1519
1520 if (*pos == 0)
1521 return SEQ_START_TOKEN;
1522
1523 return sel_avc_get_stat_idx(&n);
1524}
1525
1526static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1527{
1528 return sel_avc_get_stat_idx(pos);
1529}
1530
1531static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1532{
1533 struct avc_cache_stats *st = v;
1534
Markus Elfring710a0642017-01-15 14:04:53 +01001535 if (v == SEQ_START_TOKEN) {
1536 seq_puts(seq,
1537 "lookups hits misses allocations reclaims frees\n");
1538 } else {
Linus Torvalds257313b2011-05-19 21:22:53 -07001539 unsigned int lookups = st->lookups;
1540 unsigned int misses = st->misses;
1541 unsigned int hits = lookups - misses;
1542 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1543 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
1548
1549static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1550{ }
1551
Jan Engelhardt1996a102008-01-23 00:02:58 +01001552static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 .start = sel_avc_stats_seq_start,
1554 .next = sel_avc_stats_seq_next,
1555 .show = sel_avc_stats_seq_show,
1556 .stop = sel_avc_stats_seq_stop,
1557};
1558
1559static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1560{
1561 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1562}
1563
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001564static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .open = sel_open_avc_cache_stats,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = seq_release,
1569};
1570#endif
1571
1572static int sel_make_avc_files(struct dentry *dir)
1573{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001574 struct super_block *sb = dir->d_sb;
1575 struct selinux_fs_info *fsi = sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001576 int i;
Eric Biggerscda37122017-03-25 21:15:37 -07001577 static const struct tree_descr files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 { "cache_threshold",
1579 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1580 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1581#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1582 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1583#endif
1584 };
1585
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001586 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct inode *inode;
1588 struct dentry *dentry;
1589
1590 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001591 if (!dentry)
1592 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001595 if (!inode)
1596 return -ENOMEM;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 inode->i_fop = files[i].ops;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001599 inode->i_ino = ++fsi->last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 d_add(dentry, inode);
1601 }
Eric Parisb77a4932010-11-23 11:40:08 -05001602
1603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Eric Paris18729812008-04-17 14:15:45 -04001606static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001607 size_t count, loff_t *ppos)
1608{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001609 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
James Carterf0ee2e42007-04-04 10:11:29 -04001610 char *con;
1611 u32 sid, len;
1612 ssize_t ret;
1613
Al Viro496ad9a2013-01-23 17:07:38 -05001614 sid = file_inode(file)->i_ino&SEL_INO_MASK;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001615 ret = security_sid_to_context(fsi->state, sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001616 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001617 return ret;
1618
1619 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1620 kfree(con);
1621 return ret;
1622}
1623
1624static const struct file_operations sel_initcon_ops = {
1625 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001626 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001627};
1628
1629static int sel_make_initcon_files(struct dentry *dir)
1630{
Eric Parisb77a4932010-11-23 11:40:08 -05001631 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001632
1633 for (i = 1; i <= SECINITSID_NUM; i++) {
1634 struct inode *inode;
1635 struct dentry *dentry;
1636 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001637 if (!dentry)
1638 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001639
1640 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001641 if (!inode)
1642 return -ENOMEM;
1643
James Carterf0ee2e42007-04-04 10:11:29 -04001644 inode->i_fop = &sel_initcon_ops;
1645 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1646 d_add(dentry, inode);
1647 }
Eric Parisb77a4932010-11-23 11:40:08 -05001648
1649 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001650}
1651
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001652static inline unsigned long sel_class_to_ino(u16 class)
1653{
1654 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1655}
1656
1657static inline u16 sel_ino_to_class(unsigned long ino)
1658{
Eric Paris92ae9e82012-04-04 13:46:46 -04001659 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001660}
1661
1662static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1663{
1664 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1665}
1666
1667static inline u32 sel_ino_to_perm(unsigned long ino)
1668{
1669 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1670}
1671
Eric Paris18729812008-04-17 14:15:45 -04001672static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001673 size_t count, loff_t *ppos)
1674{
Al Viro496ad9a2013-01-23 17:07:38 -05001675 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001676 char res[TMPBUFLEN];
1677 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1678 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001679}
1680
1681static const struct file_operations sel_class_ops = {
1682 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001683 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001684};
1685
Eric Paris18729812008-04-17 14:15:45 -04001686static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001687 size_t count, loff_t *ppos)
1688{
Al Viro496ad9a2013-01-23 17:07:38 -05001689 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001690 char res[TMPBUFLEN];
1691 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1692 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001693}
1694
1695static const struct file_operations sel_perm_ops = {
1696 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001697 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001698};
1699
Paul Moore3bb56b22008-01-29 08:38:19 -05001700static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1701 size_t count, loff_t *ppos)
1702{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001703 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
Paul Moore3bb56b22008-01-29 08:38:19 -05001704 int value;
1705 char tmpbuf[TMPBUFLEN];
1706 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001707 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001708
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001709 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
Paul Moore3bb56b22008-01-29 08:38:19 -05001710 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1711
1712 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1713}
1714
1715static const struct file_operations sel_policycap_ops = {
1716 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001717 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001718};
1719
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001720static int sel_make_perm_files(char *objclass, int classvalue,
1721 struct dentry *dir)
1722{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001723 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
Eric Parisb77a4932010-11-23 11:40:08 -05001724 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001725 char **perms;
1726
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001727 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001728 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001729 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001730
1731 for (i = 0; i < nperms; i++) {
1732 struct inode *inode;
1733 struct dentry *dentry;
1734
Eric Parisb77a4932010-11-23 11:40:08 -05001735 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001736 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001737 if (!dentry)
1738 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001739
Eric Parisb77a4932010-11-23 11:40:08 -05001740 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001741 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001742 if (!inode)
1743 goto out;
1744
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001745 inode->i_fop = &sel_perm_ops;
1746 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001747 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001748 d_add(dentry, inode);
1749 }
Eric Parisb77a4932010-11-23 11:40:08 -05001750 rc = 0;
1751out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001752 for (i = 0; i < nperms; i++)
1753 kfree(perms[i]);
1754 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001755 return rc;
1756}
1757
1758static int sel_make_class_dir_entries(char *classname, int index,
1759 struct dentry *dir)
1760{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001761 struct super_block *sb = dir->d_sb;
1762 struct selinux_fs_info *fsi = sb->s_fs_info;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001763 struct dentry *dentry = NULL;
1764 struct inode *inode = NULL;
1765 int rc;
1766
1767 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001768 if (!dentry)
1769 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001770
1771 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001772 if (!inode)
1773 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001774
1775 inode->i_fop = &sel_class_ops;
1776 inode->i_ino = sel_class_to_ino(index);
1777 d_add(dentry, inode);
1778
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001779 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001780 if (IS_ERR(dentry))
1781 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001782
1783 rc = sel_make_perm_files(classname, index, dentry);
1784
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001785 return rc;
1786}
1787
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001788static int sel_make_classes(struct selinux_fs_info *fsi)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001789{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001790
Eric Parisb77a4932010-11-23 11:40:08 -05001791 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001792 char **classes;
1793
1794 /* delete any existing entries */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001795 sel_remove_entries(fsi->class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001796
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001797 rc = security_get_classes(fsi->state, &classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001798 if (rc)
1799 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001800
1801 /* +2 since classes are 1-indexed */
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001802 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001803
1804 for (i = 0; i < nclasses; i++) {
1805 struct dentry *class_name_dir;
1806
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001807 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1808 &fsi->last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001809 if (IS_ERR(class_name_dir)) {
1810 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001811 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001812 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001813
1814 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001815 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001816 class_name_dir);
1817 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001818 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001819 }
Eric Parisb77a4932010-11-23 11:40:08 -05001820 rc = 0;
1821out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001822 for (i = 0; i < nclasses; i++)
1823 kfree(classes[i]);
1824 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001825 return rc;
1826}
1827
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001828static int sel_make_policycap(struct selinux_fs_info *fsi)
Paul Moore3bb56b22008-01-29 08:38:19 -05001829{
1830 unsigned int iter;
1831 struct dentry *dentry = NULL;
1832 struct inode *inode = NULL;
1833
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001834 sel_remove_entries(fsi->policycap_dir);
Paul Moore3bb56b22008-01-29 08:38:19 -05001835
1836 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001837 if (iter < ARRAY_SIZE(selinux_policycap_names))
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001838 dentry = d_alloc_name(fsi->policycap_dir,
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001839 selinux_policycap_names[iter]);
Paul Moore3bb56b22008-01-29 08:38:19 -05001840 else
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001841 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
Paul Moore3bb56b22008-01-29 08:38:19 -05001842
1843 if (dentry == NULL)
1844 return -ENOMEM;
1845
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001846 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
Paul Moore3bb56b22008-01-29 08:38:19 -05001847 if (inode == NULL)
1848 return -ENOMEM;
1849
1850 inode->i_fop = &sel_policycap_ops;
1851 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1852 d_add(dentry, inode);
1853 }
1854
1855 return 0;
1856}
1857
Al Viroa1c2aa12012-03-18 20:36:59 -04001858static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001859 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Al Viroa1c2aa12012-03-18 20:36:59 -04001861 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct inode *inode;
1863
Al Viroa1c2aa12012-03-18 20:36:59 -04001864 if (!dentry)
1865 return ERR_PTR(-ENOMEM);
1866
1867 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1868 if (!inode) {
1869 dput(dentry);
1870 return ERR_PTR(-ENOMEM);
1871 }
Eric Parisb77a4932010-11-23 11:40:08 -05001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 inode->i_op = &simple_dir_inode_operations;
1874 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001875 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001876 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001877 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001879 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001880 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001881
Al Viroa1c2aa12012-03-18 20:36:59 -04001882 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001885#define NULL_FILE_NAME "null"
1886
Eric Paris18729812008-04-17 14:15:45 -04001887static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001889 struct selinux_fs_info *fsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int ret;
1891 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001892 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct inode_security_struct *isec;
1894
Eric Biggerscda37122017-03-25 21:15:37 -07001895 static const struct tree_descr selinux_files[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1897 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001898 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1900 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1901 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1902 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1903 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1904 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1905 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1906 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1907 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001909 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1910 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001911 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001912 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001913 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1914 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /* last one */ {""}
1916 };
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001917
1918 ret = selinux_fs_info_create(sb);
1919 if (ret)
1920 goto err;
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1923 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001924 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001926 fsi = sb->s_fs_info;
1927 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1928 if (IS_ERR(fsi->bool_dir)) {
1929 ret = PTR_ERR(fsi->bool_dir);
1930 fsi->bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001931 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Eric Parisb77a4932010-11-23 11:40:08 -05001934 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001936 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001937 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Eric Parisb77a4932010-11-23 11:40:08 -05001939 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001941 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001942 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001943
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001944 inode->i_ino = ++fsi->last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001945 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 isec->sid = SECINITSID_DEVNULL;
1947 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001948 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1951 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001953 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001954 if (IS_ERR(dentry)) {
1955 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001956 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 ret = sel_make_avc_files(dentry);
1960 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001961 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001962
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001963 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001964 if (IS_ERR(dentry)) {
1965 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001966 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001967 }
James Carterf0ee2e42007-04-04 10:11:29 -04001968
1969 ret = sel_make_initcon_files(dentry);
1970 if (ret)
1971 goto err;
1972
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001973 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1974 if (IS_ERR(fsi->class_dir)) {
1975 ret = PTR_ERR(fsi->class_dir);
1976 fsi->class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001977 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001978 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001979
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001980 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1981 &fsi->last_ino);
1982 if (IS_ERR(fsi->policycap_dir)) {
1983 ret = PTR_ERR(fsi->policycap_dir);
1984 fsi->policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001985 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001986 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001987
1988 ret = sel_make_policy_nodes(fsi);
1989 if (ret)
1990 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001991 return 0;
James Morris161ce452006-03-22 00:09:17 -08001992err:
Eric Paris744ba352008-04-17 11:52:44 -04001993 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1994 __func__);
Stephen Smalley0619f0f2018-03-20 11:59:11 -04001995
1996 selinux_fs_info_free(sb);
1997
Eric Parisb77a4932010-11-23 11:40:08 -05001998 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Al Virofc14f2f2010-07-25 01:48:30 +04002001static struct dentry *sel_mount(struct file_system_type *fs_type,
2002 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Al Virofc14f2f2010-07-25 01:48:30 +04002004 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002007static void sel_kill_sb(struct super_block *sb)
2008{
2009 selinux_fs_info_free(sb);
2010 kill_litter_super(sb);
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static struct file_system_type sel_fs_type = {
2014 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002015 .mount = sel_mount,
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002016 .kill_sb = sel_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017};
2018
2019struct vfsmount *selinuxfs_mount;
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002020struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022static int __init init_sel_fs(void)
2023{
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002024 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2025 sizeof(NULL_FILE_NAME)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int err;
2027
2028 if (!selinux_enabled)
2029 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002030
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002031 err = sysfs_create_mount_point(fs_kobj, "selinux");
2032 if (err)
2033 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002036 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002037 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05002038 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07002039 }
Eric Parisb77a4932010-11-23 11:40:08 -05002040
Al Viro765927b2012-06-26 21:58:53 +04002041 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05002042 if (IS_ERR(selinuxfs_mount)) {
2043 printk(KERN_ERR "selinuxfs: could not mount!\n");
2044 err = PTR_ERR(selinuxfs_mount);
2045 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Stephen Smalley0619f0f2018-03-20 11:59:11 -04002047 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2048 &null_name);
2049 if (IS_ERR(selinux_null.dentry)) {
2050 pr_err("selinuxfs: could not lookup null!\n");
2051 err = PTR_ERR(selinux_null.dentry);
2052 selinux_null.dentry = NULL;
2053 }
Eric Parisb77a4932010-11-23 11:40:08 -05002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return err;
2056}
2057
2058__initcall(init_sel_fs);
2059
2060#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2061void exit_sel_fs(void)
2062{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05002063 sysfs_remove_mount_point(fs_kobj, "selinux");
Stephen Smalleyfd40ffc2018-04-09 14:36:05 -04002064 dput(selinux_null.dentry);
Tim Chen423e0ab2011-07-19 09:32:38 -07002065 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 unregister_filesystem(&sel_fs_type);
2067}
2068#endif