blob: 7672b61d6673d2e70cff7f90ac5e94b045f8cb21 [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>
Ingo Molnarbb0030792006-03-22 00:09:14 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/string.h>
25#include <linux/security.h>
26#include <linux/major.h>
27#include <linux/seq_file.h>
28#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000029#include <linux/audit.h>
Eric Parisf5269712008-05-14 11:27:45 -040030#include <linux/uaccess.h>
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -070031#include <linux/kobject.h>
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -040032#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36
37#include "flask.h"
38#include "avc.h"
39#include "avc_ss.h"
40#include "security.h"
41#include "objsec.h"
42#include "conditional.h"
43
Paul Moore3bb56b22008-01-29 08:38:19 -050044/* Policy capability filenames */
45static char *policycap_names[] = {
Eric Parisb0c636b2008-02-28 12:58:40 -050046 "network_peer_controls",
Chris PeBenito2be4d742013-05-03 09:05:39 -040047 "open_perms",
Stephen Smalleyda69a532017-01-09 10:07:30 -050048 "extended_socket_class",
Chris PeBenito2be4d742013-05-03 09:05:39 -040049 "always_check_network"
Paul Moore3bb56b22008-01-29 08:38:19 -050050};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
53
54static int __init checkreqprot_setup(char *str)
55{
Eric Parisf5269712008-05-14 11:27:45 -040056 unsigned long checkreqprot;
Jingoo Han29707b22014-02-05 15:13:14 +090057 if (!kstrtoul(str, 0, &checkreqprot))
Eric Parisf5269712008-05-14 11:27:45 -040058 selinux_checkreqprot = checkreqprot ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 1;
60}
61__setup("checkreqprot=", checkreqprot_setup);
62
Ingo Molnarbb0030792006-03-22 00:09:14 -080063static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* global data for booleans */
Eric Paris18729812008-04-17 14:15:45 -040066static struct dentry *bool_dir;
67static int bool_num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -050068static char **bool_pending_names;
Eric Paris18729812008-04-17 14:15:45 -040069static int *bool_pending_values;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -040071/* global data for classes */
Eric Paris18729812008-04-17 14:15:45 -040072static struct dentry *class_dir;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -040073static unsigned long last_class_ino;
74
Eric Pariscee74f42010-10-13 17:50:25 -040075static char policy_opened;
76
Paul Moore3bb56b22008-01-29 08:38:19 -050077/* global data for policy capabilities */
Eric Paris18729812008-04-17 14:15:45 -040078static struct dentry *policycap_dir;
Paul Moore3bb56b22008-01-29 08:38:19 -050079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080enum sel_inos {
81 SEL_ROOT_INO = 2,
82 SEL_LOAD, /* load policy */
83 SEL_ENFORCE, /* get or set enforcing status */
84 SEL_CONTEXT, /* validate context */
85 SEL_ACCESS, /* compute access decision */
86 SEL_CREATE, /* compute create labeling decision */
87 SEL_RELABEL, /* compute relabeling decision */
88 SEL_USER, /* compute reachable user contexts */
89 SEL_POLICYVERS, /* return policy version for this kernel */
90 SEL_COMMIT_BOOLS, /* commit new boolean values */
91 SEL_MLS, /* return if MLS policy is enabled */
92 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 SEL_MEMBER, /* compute polyinstantiation membership decision */
94 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -070095 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -040096 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
97 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +090098 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -040099 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500100 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -0400101 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
James Carter6174eaf2007-04-04 16:18:39 -0400104static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
105
Paul Moore3bb56b22008-01-29 08:38:19 -0500106#define SEL_INITCON_INO_OFFSET 0x01000000
107#define SEL_BOOL_INO_OFFSET 0x02000000
108#define SEL_CLASS_INO_OFFSET 0x04000000
109#define SEL_POLICYCAP_INO_OFFSET 0x08000000
110#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define TMPBUFLEN 12
113static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
114 size_t count, loff_t *ppos)
115{
116 char tmpbuf[TMPBUFLEN];
117 ssize_t length;
118
119 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
120 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
121}
122
123#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400124static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 size_t count, loff_t *ppos)
126
127{
Eric Parisb77a4932010-11-23 11:40:08 -0500128 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 ssize_t length;
130 int new_value;
131
Davi Arnautbfd51622005-10-30 14:59:24 -0800132 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500133 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500134
135 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500136 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500137 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500138
Al Viro8365a712015-12-24 00:08:06 -0500139 page = memdup_user_nul(buf, count);
140 if (IS_ERR(page))
141 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 length = -EINVAL;
144 if (sscanf(page, "%d", &new_value) != 1)
145 goto out;
146
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500147 new_value = !!new_value;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (new_value != selinux_enforcing) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500150 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
151 SECCLASS_SECURITY, SECURITY__SETENFORCE,
152 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (length)
154 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000155 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500156 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
157 new_value, selinux_enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700158 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500159 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 selinux_enforcing = new_value;
161 if (selinux_enforcing)
162 avc_ss_reset(0);
163 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900164 selinux_status_update_setenforce(selinux_enforcing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 length = count;
167out:
Al Viro8365a712015-12-24 00:08:06 -0500168 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return length;
170}
171#else
172#define sel_write_enforce NULL
173#endif
174
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800175static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .read = sel_read_enforce,
177 .write = sel_write_enforce,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200178 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
Eric Paris3f120702007-09-21 14:37:10 -0400181static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
182 size_t count, loff_t *ppos)
183{
184 char tmpbuf[TMPBUFLEN];
185 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500186 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400187 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
188 security_get_reject_unknown() : !security_get_allow_unknown();
189
190 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
191 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
192}
193
194static const struct file_operations sel_handle_unknown_ops = {
195 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200196 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400197};
198
KaiGai Kohei11904162010-09-14 18:28:39 +0900199static int sel_open_handle_status(struct inode *inode, struct file *filp)
200{
201 struct page *status = selinux_kernel_status_page();
202
203 if (!status)
204 return -ENOMEM;
205
206 filp->private_data = status;
207
208 return 0;
209}
210
211static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
212 size_t count, loff_t *ppos)
213{
214 struct page *status = filp->private_data;
215
216 BUG_ON(!status);
217
218 return simple_read_from_buffer(buf, count, ppos,
219 page_address(status),
220 sizeof(struct selinux_kernel_status));
221}
222
223static int sel_mmap_handle_status(struct file *filp,
224 struct vm_area_struct *vma)
225{
226 struct page *status = filp->private_data;
227 unsigned long size = vma->vm_end - vma->vm_start;
228
229 BUG_ON(!status);
230
231 /* only allows one page from the head */
232 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
233 return -EIO;
234 /* disallow writable mapping */
235 if (vma->vm_flags & VM_WRITE)
236 return -EPERM;
237 /* disallow mprotect() turns it into writable */
238 vma->vm_flags &= ~VM_MAYWRITE;
239
240 return remap_pfn_range(vma, vma->vm_start,
241 page_to_pfn(status),
242 size, vma->vm_page_prot);
243}
244
245static const struct file_operations sel_handle_status_ops = {
246 .open = sel_open_handle_status,
247 .read = sel_read_handle_status,
248 .mmap = sel_mmap_handle_status,
249 .llseek = generic_file_llseek,
250};
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400253static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 size_t count, loff_t *ppos)
255
256{
Al Viro8365a712015-12-24 00:08:06 -0500257 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ssize_t length;
259 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Davi Arnautbfd51622005-10-30 14:59:24 -0800261 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500262 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500263
264 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500265 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500266 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500267
Al Viro8365a712015-12-24 00:08:06 -0500268 page = memdup_user_nul(buf, count);
269 if (IS_ERR(page))
270 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 length = -EINVAL;
273 if (sscanf(page, "%d", &new_value) != 1)
274 goto out;
275
276 if (new_value) {
277 length = selinux_disable();
Eric Parisb77a4932010-11-23 11:40:08 -0500278 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000280 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500281 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700282 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500283 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 length = count;
287out:
Al Viro8365a712015-12-24 00:08:06 -0500288 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return length;
290}
291#else
292#define sel_write_disable NULL
293#endif
294
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800295static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200297 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};
299
300static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400301 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 char tmpbuf[TMPBUFLEN];
304 ssize_t length;
305
306 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
307 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
308}
309
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800310static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200312 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
315/* declaration for sel_write_load */
316static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400317static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500318static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400319
320/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400321static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400322 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324static ssize_t sel_read_mls(struct file *filp, char __user *buf,
325 size_t count, loff_t *ppos)
326{
327 char tmpbuf[TMPBUFLEN];
328 ssize_t length;
329
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100330 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
331 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
333}
334
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800335static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200337 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338};
339
Eric Pariscee74f42010-10-13 17:50:25 -0400340struct policy_load_memory {
341 size_t len;
342 void *data;
343};
344
345static int sel_open_policy(struct inode *inode, struct file *filp)
346{
347 struct policy_load_memory *plm = NULL;
348 int rc;
349
350 BUG_ON(filp->private_data);
351
352 mutex_lock(&sel_mutex);
353
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500354 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
355 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400356 if (rc)
357 goto err;
358
359 rc = -EBUSY;
360 if (policy_opened)
361 goto err;
362
363 rc = -ENOMEM;
364 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
365 if (!plm)
366 goto err;
367
368 if (i_size_read(inode) != security_policydb_len()) {
Al Viro59551022016-01-22 15:40:57 -0500369 inode_lock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400370 i_size_write(inode, security_policydb_len());
Al Viro59551022016-01-22 15:40:57 -0500371 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400372 }
373
374 rc = security_read_policy(&plm->data, &plm->len);
375 if (rc)
376 goto err;
377
378 policy_opened = 1;
379
380 filp->private_data = plm;
381
382 mutex_unlock(&sel_mutex);
383
384 return 0;
385err:
386 mutex_unlock(&sel_mutex);
387
388 if (plm)
389 vfree(plm->data);
390 kfree(plm);
391 return rc;
392}
393
394static int sel_release_policy(struct inode *inode, struct file *filp)
395{
396 struct policy_load_memory *plm = filp->private_data;
397
398 BUG_ON(!plm);
399
400 policy_opened = 0;
401
402 vfree(plm->data);
403 kfree(plm);
404
405 return 0;
406}
407
408static ssize_t sel_read_policy(struct file *filp, char __user *buf,
409 size_t count, loff_t *ppos)
410{
411 struct policy_load_memory *plm = filp->private_data;
412 int ret;
413
414 mutex_lock(&sel_mutex);
415
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500416 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
417 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
Eric Pariscee74f42010-10-13 17:50:25 -0400418 if (ret)
419 goto out;
420
421 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
422out:
423 mutex_unlock(&sel_mutex);
424 return ret;
425}
426
Eric Paris845ca302010-10-13 17:50:31 -0400427static int sel_mmap_policy_fault(struct vm_area_struct *vma,
428 struct vm_fault *vmf)
429{
430 struct policy_load_memory *plm = vma->vm_file->private_data;
431 unsigned long offset;
432 struct page *page;
433
434 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
435 return VM_FAULT_SIGBUS;
436
437 offset = vmf->pgoff << PAGE_SHIFT;
438 if (offset >= roundup(plm->len, PAGE_SIZE))
439 return VM_FAULT_SIGBUS;
440
441 page = vmalloc_to_page(plm->data + offset);
442 get_page(page);
443
444 vmf->page = page;
445
446 return 0;
447}
448
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700449static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400450 .fault = sel_mmap_policy_fault,
451 .page_mkwrite = sel_mmap_policy_fault,
452};
453
James Morrisad3fa082011-08-30 10:50:12 +1000454static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400455{
456 if (vma->vm_flags & VM_SHARED) {
457 /* do not allow mprotect to make mapping writable */
458 vma->vm_flags &= ~VM_MAYWRITE;
459
460 if (vma->vm_flags & VM_WRITE)
461 return -EACCES;
462 }
463
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700464 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400465 vma->vm_ops = &sel_mmap_policy_ops;
466
467 return 0;
468}
469
Eric Pariscee74f42010-10-13 17:50:25 -0400470static const struct file_operations sel_policy_ops = {
471 .open = sel_open_policy,
472 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400473 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400474 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500475 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400476};
477
Eric Paris18729812008-04-17 14:15:45 -0400478static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 size_t count, loff_t *ppos)
480
481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ssize_t length;
483 void *data = NULL;
484
Ingo Molnarbb0030792006-03-22 00:09:14 -0800485 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500487 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
488 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (length)
490 goto out;
491
Eric Parisb77a4932010-11-23 11:40:08 -0500492 /* No partial writes. */
493 length = -EINVAL;
494 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Eric Parisb77a4932010-11-23 11:40:08 -0500497 length = -EFBIG;
498 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500500
501 length = -ENOMEM;
502 data = vmalloc(count);
503 if (!data)
504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 length = -EFAULT;
507 if (copy_from_user(data, buf, count) != 0)
508 goto out;
509
510 length = security_load_policy(data, count);
Gary Tierney4262fb52017-01-09 10:07:31 -0500511 if (length) {
512 pr_warn_ratelimited("SELinux: failed to load policy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
Gary Tierney4262fb52017-01-09 10:07:31 -0500514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eric Parisb77a4932010-11-23 11:40:08 -0500516 length = sel_make_bools();
Gary Tierney4262fb52017-01-09 10:07:31 -0500517 if (length) {
518 pr_err("SELinux: failed to load policy booleans\n");
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400519 goto out1;
Gary Tierney4262fb52017-01-09 10:07:31 -0500520 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400521
Eric Parisb77a4932010-11-23 11:40:08 -0500522 length = sel_make_classes();
Gary Tierney4262fb52017-01-09 10:07:31 -0500523 if (length) {
524 pr_err("SELinux: failed to load policy classes\n");
Paul Moore3bb56b22008-01-29 08:38:19 -0500525 goto out1;
Gary Tierney4262fb52017-01-09 10:07:31 -0500526 }
Paul Moore3bb56b22008-01-29 08:38:19 -0500527
Eric Parisb77a4932010-11-23 11:40:08 -0500528 length = sel_make_policycap();
Gary Tierney4262fb52017-01-09 10:07:31 -0500529 if (length) {
530 pr_err("SELinux: failed to load policy capabilities\n");
Eric Parisb77a4932010-11-23 11:40:08 -0500531 goto out1;
Gary Tierney4262fb52017-01-09 10:07:31 -0500532 }
Eric Parisb77a4932010-11-23 11:40:08 -0500533
534 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400535
536out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000537 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500538 "policy loaded auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700539 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500540 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800542 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 vfree(data);
544 return length;
545}
546
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800547static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200549 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550};
551
Eric Paris18729812008-04-17 14:15:45 -0400552static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Eric Parisb77a4932010-11-23 11:40:08 -0500554 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800555 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ssize_t length;
557
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500558 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
559 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +0100563 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500564 if (length)
565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800567 length = security_sid_to_context(sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500568 if (length)
569 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800570
Eric Parisb77a4932010-11-23 11:40:08 -0500571 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800572 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400573 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
574 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800575 goto out;
576 }
577
578 memcpy(buf, canon, len);
579 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800581 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return length;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
586 size_t count, loff_t *ppos)
587{
588 char tmpbuf[TMPBUFLEN];
589 ssize_t length;
590
591 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
592 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
593}
594
Eric Paris18729812008-04-17 14:15:45 -0400595static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 size_t count, loff_t *ppos)
597{
Al Viro8365a712015-12-24 00:08:06 -0500598 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ssize_t length;
600 unsigned int new_value;
601
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500602 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
603 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
604 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500606 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Davi Arnautbfd51622005-10-30 14:59:24 -0800608 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500609 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500610
611 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500612 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500613 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500614
Al Viro8365a712015-12-24 00:08:06 -0500615 page = memdup_user_nul(buf, count);
616 if (IS_ERR(page))
617 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 length = -EINVAL;
620 if (sscanf(page, "%u", &new_value) != 1)
621 goto out;
622
623 selinux_checkreqprot = new_value ? 1 : 0;
624 length = count;
625out:
Al Viro8365a712015-12-24 00:08:06 -0500626 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return length;
628}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800629static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .read = sel_read_checkreqprot,
631 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200632 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633};
634
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500635static ssize_t sel_write_validatetrans(struct file *file,
636 const char __user *buf,
637 size_t count, loff_t *ppos)
638{
639 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
640 char *req = NULL;
641 u32 osid, nsid, tsid;
642 u16 tclass;
643 int rc;
644
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500645 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
646 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500647 if (rc)
648 goto out;
649
650 rc = -ENOMEM;
651 if (count >= PAGE_SIZE)
652 goto out;
653
654 /* No partial writes. */
655 rc = -EINVAL;
656 if (*ppos != 0)
657 goto out;
658
659 rc = -ENOMEM;
660 req = kzalloc(count + 1, GFP_KERNEL);
661 if (!req)
662 goto out;
663
664 rc = -EFAULT;
665 if (copy_from_user(req, buf, count))
666 goto out;
667
668 rc = -ENOMEM;
669 oldcon = kzalloc(count + 1, GFP_KERNEL);
670 if (!oldcon)
671 goto out;
672
673 newcon = kzalloc(count + 1, GFP_KERNEL);
674 if (!newcon)
675 goto out;
676
677 taskcon = kzalloc(count + 1, GFP_KERNEL);
678 if (!taskcon)
679 goto out;
680
681 rc = -EINVAL;
682 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
683 goto out;
684
685 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
686 if (rc)
687 goto out;
688
689 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
690 if (rc)
691 goto out;
692
693 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
694 if (rc)
695 goto out;
696
697 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
698 if (!rc)
699 rc = count;
700out:
701 kfree(req);
702 kfree(oldcon);
703 kfree(newcon);
704 kfree(taskcon);
705 return rc;
706}
707
708static const struct file_operations sel_transition_ops = {
709 .write = sel_write_validatetrans,
710 .llseek = generic_file_llseek,
711};
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
715 */
Eric Paris18729812008-04-17 14:15:45 -0400716static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
717static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
718static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
719static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
720static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722static ssize_t (*write_op[])(struct file *, char *, size_t) = {
723 [SEL_ACCESS] = sel_write_access,
724 [SEL_CREATE] = sel_write_create,
725 [SEL_RELABEL] = sel_write_relabel,
726 [SEL_USER] = sel_write_user,
727 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800728 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729};
730
731static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
732{
Al Viro496ad9a2013-01-23 17:07:38 -0500733 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 char *data;
735 ssize_t rv;
736
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800737 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
739
740 data = simple_transaction_get(file, buf, size);
741 if (IS_ERR(data))
742 return PTR_ERR(data);
743
Eric Paris18729812008-04-17 14:15:45 -0400744 rv = write_op[ino](file, data, size);
745 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 simple_transaction_set(file, rv);
747 rv = size;
748 }
749 return rv;
750}
751
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800752static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 .write = selinux_transaction_write,
754 .read = simple_transaction_read,
755 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200756 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757};
758
759/*
760 * payload - write methods
761 * If the method has a response, the response should be put in buf,
762 * and the length returned. Otherwise return 0 or and -error.
763 */
764
Eric Paris18729812008-04-17 14:15:45 -0400765static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Eric Parisb77a4932010-11-23 11:40:08 -0500767 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 u32 ssid, tsid;
769 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct av_decision avd;
771 ssize_t length;
772
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500773 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
774 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800779 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Eric Parisb77a4932010-11-23 11:40:08 -0500783 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800784 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!tcon)
786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500789 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400792 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500793 if (length)
794 goto out;
795
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400796 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500797 if (length)
798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Stephen Smalley19439d02010-01-14 17:28:10 -0500800 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900803 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500804 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900806 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807out:
Eric Parisb77a4932010-11-23 11:40:08 -0500808 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 kfree(scon);
810 return length;
811}
812
Eric Paris18729812008-04-17 14:15:45 -0400813static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Eric Parisb77a4932010-11-23 11:40:08 -0500815 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100816 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 u32 ssid, tsid, newsid;
818 u16 tclass;
819 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500820 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100822 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500824 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
825 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
826 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500828 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800831 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500833 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Eric Parisb77a4932010-11-23 11:40:08 -0500835 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800836 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!tcon)
838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100840 length = -ENOMEM;
841 namebuf = kzalloc(size + 1, GFP_KERNEL);
842 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100845 length = -EINVAL;
846 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
847 if (nargs < 3 || nargs > 4)
848 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400849 if (nargs == 4) {
850 /*
851 * If and when the name of new object to be queried contains
852 * either whitespace or multibyte characters, they shall be
853 * encoded based on the percentage-encoding rule.
854 * If not encoded, the sscanf logic picks up only left-half
855 * of the supplied name; splitted by a whitespace unexpectedly.
856 */
857 char *r, *w;
858 int c1, c2;
859
860 r = w = namebuf;
861 do {
862 c1 = *r++;
863 if (c1 == '+')
864 c1 = ' ';
865 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800866 c1 = hex_to_bin(*r++);
867 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400868 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800869 c2 = hex_to_bin(*r++);
870 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400871 goto out;
872 c1 = (c1 << 4) | c2;
873 }
874 *w++ = c1;
875 } while (c1 != '\0');
876
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100877 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400878 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100879
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400880 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500881 if (length)
882 goto out;
883
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400884 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500885 if (length)
886 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100888 length = security_transition_sid_user(ssid, tsid, tclass,
889 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500890 if (length)
891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500894 if (length)
895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric Parisb77a4932010-11-23 11:40:08 -0500897 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400899 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
900 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 memcpy(buf, newcon, len);
905 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906out:
Eric Parisb77a4932010-11-23 11:40:08 -0500907 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100908 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500909 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 kfree(scon);
911 return length;
912}
913
Eric Paris18729812008-04-17 14:15:45 -0400914static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Eric Parisb77a4932010-11-23 11:40:08 -0500916 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 u32 ssid, tsid, newsid;
918 u16 tclass;
919 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500920 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 u32 len;
922
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500923 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
924 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
925 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800930 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Parisb77a4932010-11-23 11:40:08 -0500934 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800935 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!tcon)
937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 length = -EINVAL;
940 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500941 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400943 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500944 if (length)
945 goto out;
946
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400947 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500948 if (length)
949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 length = security_change_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500952 if (length)
953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500956 if (length)
957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Parisb77a4932010-11-23 11:40:08 -0500959 length = -ERANGE;
960 if (len > SIMPLE_TRANSACTION_LIMIT)
961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 memcpy(buf, newcon, len);
964 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965out:
Eric Parisb77a4932010-11-23 11:40:08 -0500966 kfree(newcon);
967 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 kfree(scon);
969 return length;
970}
971
Eric Paris18729812008-04-17 14:15:45 -0400972static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Eric Parisb77a4932010-11-23 11:40:08 -0500974 char *con = NULL, *user = NULL, *ptr;
975 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 ssize_t length;
977 char *newcon;
978 int i, rc;
979 u32 len, nsids;
980
Stephen Smalleybe0554c2017-01-09 10:07:31 -0500981 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
982 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
983 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800988 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Eric Parisb77a4932010-11-23 11:40:08 -0500992 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800993 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!user)
995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 length = -EINVAL;
998 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -0500999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001001 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001002 if (length)
1003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 length = security_get_user_sids(sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001006 if (length)
1007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 length = sprintf(buf, "%u", nsids) + 1;
1010 ptr = buf + length;
1011 for (i = 0; i < nsids; i++) {
1012 rc = security_sid_to_context(sids[i], &newcon, &len);
1013 if (rc) {
1014 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1018 kfree(newcon);
1019 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 memcpy(ptr, newcon, len);
1023 kfree(newcon);
1024 ptr += len;
1025 length += len;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027out:
Eric Parisb77a4932010-11-23 11:40:08 -05001028 kfree(sids);
1029 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree(con);
1031 return length;
1032}
1033
Eric Paris18729812008-04-17 14:15:45 -04001034static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Eric Parisb77a4932010-11-23 11:40:08 -05001036 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 u32 ssid, tsid, newsid;
1038 u16 tclass;
1039 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001040 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 u32 len;
1042
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001043 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1044 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1045 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001050 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric Parisb77a4932010-11-23 11:40:08 -05001054 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001055 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!tcon)
1057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 length = -EINVAL;
1060 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001063 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001064 if (length)
1065 goto out;
1066
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001067 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001068 if (length)
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 length = security_member_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001072 if (length)
1073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001076 if (length)
1077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Eric Parisb77a4932010-11-23 11:40:08 -05001079 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001081 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1082 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 memcpy(buf, newcon, len);
1087 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088out:
Eric Parisb77a4932010-11-23 11:40:08 -05001089 kfree(newcon);
1090 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kfree(scon);
1092 return length;
1093}
1094
1095static struct inode *sel_make_inode(struct super_block *sb, int mode)
1096{
1097 struct inode *ret = new_inode(sb);
1098
1099 if (ret) {
1100 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001101 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 return ret;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1107 size_t count, loff_t *ppos)
1108{
1109 char *page = NULL;
1110 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ssize_t ret;
1112 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001113 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001114 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Ingo Molnarbb0030792006-03-22 00:09:14 -08001116 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Eric Parisb77a4932010-11-23 11:40:08 -05001118 ret = -EINVAL;
1119 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Eric Parisb77a4932010-11-23 11:40:08 -05001122 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001123 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001124 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001127 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (cur_enforcing < 0) {
1129 ret = cur_enforcing;
1130 goto out;
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001133 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001134 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001136 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001137 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return ret;
1139}
1140
1141static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1142 size_t count, loff_t *ppos)
1143{
1144 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001145 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001147 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001148 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Ingo Molnarbb0030792006-03-22 00:09:14 -08001150 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001152 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1153 SECCLASS_SECURITY, SECURITY__SETBOOL,
1154 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (length)
1156 goto out;
1157
Eric Parisb77a4932010-11-23 11:40:08 -05001158 length = -EINVAL;
1159 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001160 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001161
Eric Parisb77a4932010-11-23 11:40:08 -05001162 length = -ENOMEM;
1163 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001165
Eric Parisb77a4932010-11-23 11:40:08 -05001166 /* No partial writes. */
1167 length = -EINVAL;
1168 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001170
Al Viro8365a712015-12-24 00:08:06 -05001171 page = memdup_user_nul(buf, count);
1172 if (IS_ERR(page)) {
1173 length = PTR_ERR(page);
1174 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 length = -EINVAL;
1179 if (sscanf(page, "%d", &new_value) != 1)
1180 goto out;
1181
1182 if (new_value)
1183 new_value = 1;
1184
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001185 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 length = count;
1187
1188out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001189 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001190 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return length;
1192}
1193
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001194static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001195 .read = sel_read_bool,
1196 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001197 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198};
1199
1200static ssize_t sel_commit_bools_write(struct file *filep,
1201 const char __user *buf,
1202 size_t count, loff_t *ppos)
1203{
1204 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001205 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int new_value;
1207
Ingo Molnarbb0030792006-03-22 00:09:14 -08001208 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001210 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1211 SECCLASS_SECURITY, SECURITY__SETBOOL,
1212 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (length)
1214 goto out;
1215
Eric Parisb77a4932010-11-23 11:40:08 -05001216 length = -ENOMEM;
1217 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001219
1220 /* No partial writes. */
1221 length = -EINVAL;
1222 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001224
Al Viro8365a712015-12-24 00:08:06 -05001225 page = memdup_user_nul(buf, count);
1226 if (IS_ERR(page)) {
1227 length = PTR_ERR(page);
1228 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 length = -EINVAL;
1233 if (sscanf(page, "%d", &new_value) != 1)
1234 goto out;
1235
Eric Parisb77a4932010-11-23 11:40:08 -05001236 length = 0;
Eric Paris18729812008-04-17 14:15:45 -04001237 if (new_value && bool_pending_values)
Eric Parisb77a4932010-11-23 11:40:08 -05001238 length = security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Eric Parisb77a4932010-11-23 11:40:08 -05001240 if (!length)
1241 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001244 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001245 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return length;
1247}
1248
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001249static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001250 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001251 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252};
1253
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001254static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Al Viroad521842014-12-24 14:56:48 -05001256 d_genocide(de);
1257 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260#define BOOL_DIR_NAME "booleans"
1261
1262static int sel_make_bools(void)
1263{
Eric Parisb77a4932010-11-23 11:40:08 -05001264 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ssize_t len;
1266 struct dentry *dentry = NULL;
1267 struct dentry *dir = bool_dir;
1268 struct inode *inode = NULL;
1269 struct inode_security_struct *isec;
1270 char **names = NULL, *page;
1271 int num;
1272 int *values = NULL;
1273 u32 sid;
1274
1275 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001276 for (i = 0; i < bool_num; i++)
1277 kfree(bool_pending_names[i]);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001278 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001279 kfree(bool_pending_values);
Eric Paris154c50c2012-04-04 13:47:11 -04001280 bool_num = 0;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001281 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001282 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001284 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Eric Parisb77a4932010-11-23 11:40:08 -05001286 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001287 page = (char *)get_zeroed_page(GFP_KERNEL);
1288 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 ret = security_get_bools(&num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001292 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto out;
1294
1295 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001296 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001298 if (!dentry)
1299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Eric Parisb77a4932010-11-23 11:40:08 -05001301 ret = -ENOMEM;
1302 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1303 if (!inode)
1304 goto out;
1305
Eric Parisb77a4932010-11-23 11:40:08 -05001306 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001307 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001308 if (len >= PAGE_SIZE)
1309 goto out;
1310
Eric Paris18729812008-04-17 14:15:45 -04001311 isec = (struct inode_security_struct *)inode->i_security;
1312 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
Gary Tierney4262fb52017-01-09 10:07:31 -05001313 if (ret) {
1314 pr_err("SELinux: failed to lookup sid for %s\n", page);
Eric Parisb77a4932010-11-23 11:40:08 -05001315 goto out;
1316
Gary Tierney4262fb52017-01-09 10:07:31 -05001317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001320 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001322 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 d_add(dentry, inode);
1324 }
1325 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001326 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001328
1329 free_page((unsigned long)page);
1330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331out:
1332 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001335 for (i = 0; i < num; i++)
1336 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 kfree(names);
1338 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001339 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001340 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001341
1342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345#define NULL_FILE_NAME "null"
1346
Al Viro765927b2012-06-26 21:58:53 +04001347struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1350 size_t count, loff_t *ppos)
1351{
1352 char tmpbuf[TMPBUFLEN];
1353 ssize_t length;
1354
1355 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1356 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1357}
1358
Eric Paris18729812008-04-17 14:15:45 -04001359static ssize_t sel_write_avc_cache_threshold(struct file *file,
1360 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 size_t count, loff_t *ppos)
1362
1363{
Al Viro8365a712015-12-24 00:08:06 -05001364 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001366 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Stephen Smalleybe0554c2017-01-09 10:07:31 -05001368 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1369 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1370 NULL);
Eric Parisb77a4932010-11-23 11:40:08 -05001371 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric Parisb77a4932010-11-23 11:40:08 -05001374 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Eric Parisb77a4932010-11-23 11:40:08 -05001377 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001378 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001379 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001380
Al Viro8365a712015-12-24 00:08:06 -05001381 page = memdup_user_nul(buf, count);
1382 if (IS_ERR(page))
1383 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Parisb77a4932010-11-23 11:40:08 -05001385 ret = -EINVAL;
1386 if (sscanf(page, "%u", &new_value) != 1)
1387 goto out;
1388
1389 avc_cache_threshold = new_value;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392out:
Al Viro8365a712015-12-24 00:08:06 -05001393 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return ret;
1395}
1396
1397static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1398 size_t count, loff_t *ppos)
1399{
1400 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001401 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001404 if (!page)
1405 return -ENOMEM;
1406
1407 length = avc_get_hash_stats(page);
1408 if (length >= 0)
1409 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001411
1412 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001415static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 .read = sel_read_avc_cache_threshold,
1417 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001418 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419};
1420
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001421static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001423 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424};
1425
1426#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1427static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1428{
1429 int cpu;
1430
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301431 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!cpu_possible(cpu))
1433 continue;
1434 *idx = cpu + 1;
1435 return &per_cpu(avc_cache_stats, cpu);
1436 }
1437 return NULL;
1438}
1439
1440static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1441{
1442 loff_t n = *pos - 1;
1443
1444 if (*pos == 0)
1445 return SEQ_START_TOKEN;
1446
1447 return sel_avc_get_stat_idx(&n);
1448}
1449
1450static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1451{
1452 return sel_avc_get_stat_idx(pos);
1453}
1454
1455static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1456{
1457 struct avc_cache_stats *st = v;
1458
1459 if (v == SEQ_START_TOKEN)
1460 seq_printf(seq, "lookups hits misses allocations reclaims "
1461 "frees\n");
Linus Torvalds257313b2011-05-19 21:22:53 -07001462 else {
1463 unsigned int lookups = st->lookups;
1464 unsigned int misses = st->misses;
1465 unsigned int hits = lookups - misses;
1466 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1467 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1474{ }
1475
Jan Engelhardt1996a102008-01-23 00:02:58 +01001476static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 .start = sel_avc_stats_seq_start,
1478 .next = sel_avc_stats_seq_next,
1479 .show = sel_avc_stats_seq_show,
1480 .stop = sel_avc_stats_seq_stop,
1481};
1482
1483static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1484{
1485 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1486}
1487
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001488static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 .open = sel_open_avc_cache_stats,
1490 .read = seq_read,
1491 .llseek = seq_lseek,
1492 .release = seq_release,
1493};
1494#endif
1495
1496static int sel_make_avc_files(struct dentry *dir)
1497{
Eric Parisb77a4932010-11-23 11:40:08 -05001498 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 static struct tree_descr files[] = {
1500 { "cache_threshold",
1501 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1502 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1503#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1504 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1505#endif
1506 };
1507
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001508 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 struct inode *inode;
1510 struct dentry *dentry;
1511
1512 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001513 if (!dentry)
1514 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001517 if (!inode)
1518 return -ENOMEM;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001521 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 d_add(dentry, inode);
1523 }
Eric Parisb77a4932010-11-23 11:40:08 -05001524
1525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Eric Paris18729812008-04-17 14:15:45 -04001528static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001529 size_t count, loff_t *ppos)
1530{
James Carterf0ee2e42007-04-04 10:11:29 -04001531 char *con;
1532 u32 sid, len;
1533 ssize_t ret;
1534
Al Viro496ad9a2013-01-23 17:07:38 -05001535 sid = file_inode(file)->i_ino&SEL_INO_MASK;
James Carterf0ee2e42007-04-04 10:11:29 -04001536 ret = security_sid_to_context(sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001537 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001538 return ret;
1539
1540 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1541 kfree(con);
1542 return ret;
1543}
1544
1545static const struct file_operations sel_initcon_ops = {
1546 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001547 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001548};
1549
1550static int sel_make_initcon_files(struct dentry *dir)
1551{
Eric Parisb77a4932010-11-23 11:40:08 -05001552 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001553
1554 for (i = 1; i <= SECINITSID_NUM; i++) {
1555 struct inode *inode;
1556 struct dentry *dentry;
1557 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001558 if (!dentry)
1559 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001560
1561 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001562 if (!inode)
1563 return -ENOMEM;
1564
James Carterf0ee2e42007-04-04 10:11:29 -04001565 inode->i_fop = &sel_initcon_ops;
1566 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1567 d_add(dentry, inode);
1568 }
Eric Parisb77a4932010-11-23 11:40:08 -05001569
1570 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001571}
1572
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001573static inline unsigned long sel_class_to_ino(u16 class)
1574{
1575 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1576}
1577
1578static inline u16 sel_ino_to_class(unsigned long ino)
1579{
Eric Paris92ae9e82012-04-04 13:46:46 -04001580 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001581}
1582
1583static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1584{
1585 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1586}
1587
1588static inline u32 sel_ino_to_perm(unsigned long ino)
1589{
1590 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1591}
1592
Eric Paris18729812008-04-17 14:15:45 -04001593static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001594 size_t count, loff_t *ppos)
1595{
Al Viro496ad9a2013-01-23 17:07:38 -05001596 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001597 char res[TMPBUFLEN];
1598 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1599 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001600}
1601
1602static const struct file_operations sel_class_ops = {
1603 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001604 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001605};
1606
Eric Paris18729812008-04-17 14:15:45 -04001607static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001608 size_t count, loff_t *ppos)
1609{
Al Viro496ad9a2013-01-23 17:07:38 -05001610 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001611 char res[TMPBUFLEN];
1612 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1613 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001614}
1615
1616static const struct file_operations sel_perm_ops = {
1617 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001618 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001619};
1620
Paul Moore3bb56b22008-01-29 08:38:19 -05001621static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1622 size_t count, loff_t *ppos)
1623{
1624 int value;
1625 char tmpbuf[TMPBUFLEN];
1626 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001627 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001628
1629 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1630 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1631
1632 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1633}
1634
1635static const struct file_operations sel_policycap_ops = {
1636 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001637 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001638};
1639
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001640static int sel_make_perm_files(char *objclass, int classvalue,
1641 struct dentry *dir)
1642{
Eric Parisb77a4932010-11-23 11:40:08 -05001643 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001644 char **perms;
1645
1646 rc = security_get_permissions(objclass, &perms, &nperms);
1647 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001648 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001649
1650 for (i = 0; i < nperms; i++) {
1651 struct inode *inode;
1652 struct dentry *dentry;
1653
Eric Parisb77a4932010-11-23 11:40:08 -05001654 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001655 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001656 if (!dentry)
1657 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001658
Eric Parisb77a4932010-11-23 11:40:08 -05001659 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001660 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001661 if (!inode)
1662 goto out;
1663
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001664 inode->i_fop = &sel_perm_ops;
1665 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001666 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001667 d_add(dentry, inode);
1668 }
Eric Parisb77a4932010-11-23 11:40:08 -05001669 rc = 0;
1670out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001671 for (i = 0; i < nperms; i++)
1672 kfree(perms[i]);
1673 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001674 return rc;
1675}
1676
1677static int sel_make_class_dir_entries(char *classname, int index,
1678 struct dentry *dir)
1679{
1680 struct dentry *dentry = NULL;
1681 struct inode *inode = NULL;
1682 int rc;
1683
1684 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001685 if (!dentry)
1686 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001687
1688 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001689 if (!inode)
1690 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001691
1692 inode->i_fop = &sel_class_ops;
1693 inode->i_ino = sel_class_to_ino(index);
1694 d_add(dentry, inode);
1695
Al Viroa1c2aa12012-03-18 20:36:59 -04001696 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1697 if (IS_ERR(dentry))
1698 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001699
1700 rc = sel_make_perm_files(classname, index, dentry);
1701
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001702 return rc;
1703}
1704
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001705static int sel_make_classes(void)
1706{
Eric Parisb77a4932010-11-23 11:40:08 -05001707 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001708 char **classes;
1709
1710 /* delete any existing entries */
Al Viroad521842014-12-24 14:56:48 -05001711 sel_remove_entries(class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001712
1713 rc = security_get_classes(&classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001714 if (rc)
1715 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001716
1717 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001718 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001719
1720 for (i = 0; i < nclasses; i++) {
1721 struct dentry *class_name_dir;
1722
Al Viroa1c2aa12012-03-18 20:36:59 -04001723 class_name_dir = sel_make_dir(class_dir, classes[i],
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001724 &last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001725 if (IS_ERR(class_name_dir)) {
1726 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001727 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001728 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001729
1730 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001731 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001732 class_name_dir);
1733 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001734 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001735 }
Eric Parisb77a4932010-11-23 11:40:08 -05001736 rc = 0;
1737out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001738 for (i = 0; i < nclasses; i++)
1739 kfree(classes[i]);
1740 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001741 return rc;
1742}
1743
Paul Moore3bb56b22008-01-29 08:38:19 -05001744static int sel_make_policycap(void)
1745{
1746 unsigned int iter;
1747 struct dentry *dentry = NULL;
1748 struct inode *inode = NULL;
1749
1750 sel_remove_entries(policycap_dir);
1751
1752 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1753 if (iter < ARRAY_SIZE(policycap_names))
1754 dentry = d_alloc_name(policycap_dir,
1755 policycap_names[iter]);
1756 else
1757 dentry = d_alloc_name(policycap_dir, "unknown");
1758
1759 if (dentry == NULL)
1760 return -ENOMEM;
1761
1762 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1763 if (inode == NULL)
1764 return -ENOMEM;
1765
1766 inode->i_fop = &sel_policycap_ops;
1767 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1768 d_add(dentry, inode);
1769 }
1770
1771 return 0;
1772}
1773
Al Viroa1c2aa12012-03-18 20:36:59 -04001774static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001775 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Al Viroa1c2aa12012-03-18 20:36:59 -04001777 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct inode *inode;
1779
Al Viroa1c2aa12012-03-18 20:36:59 -04001780 if (!dentry)
1781 return ERR_PTR(-ENOMEM);
1782
1783 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1784 if (!inode) {
1785 dput(dentry);
1786 return ERR_PTR(-ENOMEM);
1787 }
Eric Parisb77a4932010-11-23 11:40:08 -05001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 inode->i_op = &simple_dir_inode_operations;
1790 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001791 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001792 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001793 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001795 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001796 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001797
Al Viroa1c2aa12012-03-18 20:36:59 -04001798 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Eric Paris18729812008-04-17 14:15:45 -04001801static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 int ret;
1804 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001805 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct inode_security_struct *isec;
1807
1808 static struct tree_descr selinux_files[] = {
1809 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1810 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001811 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1813 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1814 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1815 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1816 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1817 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1818 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1819 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1820 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1821 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001822 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1823 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001824 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001825 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001826 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1827 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* last one */ {""}
1829 };
1830 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1831 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001832 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Al Viroa1c2aa12012-03-18 20:36:59 -04001834 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1835 if (IS_ERR(bool_dir)) {
1836 ret = PTR_ERR(bool_dir);
1837 bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001838 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Eric Parisb77a4932010-11-23 11:40:08 -05001841 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001843 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001844 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Eric Parisb77a4932010-11-23 11:40:08 -05001846 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001848 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001849 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001850
James Carter6174eaf2007-04-04 16:18:39 -04001851 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001852 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 isec->sid = SECINITSID_DEVNULL;
1854 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001855 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1858 d_add(dentry, inode);
Al Viro765927b2012-06-26 21:58:53 +04001859 selinux_null.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Al Viroa1c2aa12012-03-18 20:36:59 -04001861 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1862 if (IS_ERR(dentry)) {
1863 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001864 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 ret = sel_make_avc_files(dentry);
1868 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001869 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001870
Al Viroa1c2aa12012-03-18 20:36:59 -04001871 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1872 if (IS_ERR(dentry)) {
1873 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001874 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001875 }
James Carterf0ee2e42007-04-04 10:11:29 -04001876
1877 ret = sel_make_initcon_files(dentry);
1878 if (ret)
1879 goto err;
1880
Al Viroa1c2aa12012-03-18 20:36:59 -04001881 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1882 if (IS_ERR(class_dir)) {
1883 ret = PTR_ERR(class_dir);
1884 class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001885 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001886 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001887
Al Viroa1c2aa12012-03-18 20:36:59 -04001888 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1889 if (IS_ERR(policycap_dir)) {
1890 ret = PTR_ERR(policycap_dir);
1891 policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001892 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001893 }
Eric Parisb77a4932010-11-23 11:40:08 -05001894 return 0;
James Morris161ce452006-03-22 00:09:17 -08001895err:
Eric Paris744ba352008-04-17 11:52:44 -04001896 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1897 __func__);
Eric Parisb77a4932010-11-23 11:40:08 -05001898 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Al Virofc14f2f2010-07-25 01:48:30 +04001901static struct dentry *sel_mount(struct file_system_type *fs_type,
1902 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Al Virofc14f2f2010-07-25 01:48:30 +04001904 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
1907static struct file_system_type sel_fs_type = {
1908 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001909 .mount = sel_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 .kill_sb = kill_litter_super,
1911};
1912
1913struct vfsmount *selinuxfs_mount;
1914
1915static int __init init_sel_fs(void)
1916{
1917 int err;
1918
1919 if (!selinux_enabled)
1920 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001921
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001922 err = sysfs_create_mount_point(fs_kobj, "selinux");
1923 if (err)
1924 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001927 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001928 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05001929 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001930 }
Eric Parisb77a4932010-11-23 11:40:08 -05001931
Al Viro765927b2012-06-26 21:58:53 +04001932 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05001933 if (IS_ERR(selinuxfs_mount)) {
1934 printk(KERN_ERR "selinuxfs: could not mount!\n");
1935 err = PTR_ERR(selinuxfs_mount);
1936 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Eric Parisb77a4932010-11-23 11:40:08 -05001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return err;
1940}
1941
1942__initcall(init_sel_fs);
1943
1944#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1945void exit_sel_fs(void)
1946{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001947 sysfs_remove_mount_point(fs_kobj, "selinux");
Tim Chen423e0ab2011-07-19 09:32:38 -07001948 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 unregister_filesystem(&sel_fs_type);
1950}
1951#endif