blob: 0aac402a0f118e034a020bd8862d2ac20c223255 [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 -070080/* Check whether a task is allowed to use a security operation. */
81static int task_has_security(struct task_struct *tsk,
82 u32 perms)
83{
David Howellsc69e8d92008-11-14 10:39:19 +110084 const struct task_security_struct *tsec;
85 u32 sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
David Howellsc69e8d92008-11-14 10:39:19 +110087 rcu_read_lock();
88 tsec = __task_cred(tsk)->security;
89 if (tsec)
90 sid = tsec->sid;
91 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!tsec)
93 return -EACCES;
94
David Howellsc69e8d92008-11-14 10:39:19 +110095 return avc_has_perm(sid, SECINITSID_SECURITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 SECCLASS_SECURITY, perms, NULL);
97}
98
99enum sel_inos {
100 SEL_ROOT_INO = 2,
101 SEL_LOAD, /* load policy */
102 SEL_ENFORCE, /* get or set enforcing status */
103 SEL_CONTEXT, /* validate context */
104 SEL_ACCESS, /* compute access decision */
105 SEL_CREATE, /* compute create labeling decision */
106 SEL_RELABEL, /* compute relabeling decision */
107 SEL_USER, /* compute reachable user contexts */
108 SEL_POLICYVERS, /* return policy version for this kernel */
109 SEL_COMMIT_BOOLS, /* commit new boolean values */
110 SEL_MLS, /* return if MLS policy is enabled */
111 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 SEL_MEMBER, /* compute polyinstantiation membership decision */
113 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700114 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
Eric Paris3f120702007-09-21 14:37:10 -0400115 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
116 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
KaiGai Kohei11904162010-09-14 18:28:39 +0900117 SEL_STATUS, /* export current status using mmap() */
Eric Pariscee74f42010-10-13 17:50:25 -0400118 SEL_POLICY, /* allow userspace to read the in kernel policy */
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500119 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
James Carter6174eaf2007-04-04 16:18:39 -0400120 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
James Carter6174eaf2007-04-04 16:18:39 -0400123static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
124
Paul Moore3bb56b22008-01-29 08:38:19 -0500125#define SEL_INITCON_INO_OFFSET 0x01000000
126#define SEL_BOOL_INO_OFFSET 0x02000000
127#define SEL_CLASS_INO_OFFSET 0x04000000
128#define SEL_POLICYCAP_INO_OFFSET 0x08000000
129#define SEL_INO_MASK 0x00ffffff
James Carterf0ee2e42007-04-04 10:11:29 -0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TMPBUFLEN 12
132static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
133 size_t count, loff_t *ppos)
134{
135 char tmpbuf[TMPBUFLEN];
136 ssize_t length;
137
138 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
139 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
140}
141
142#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris18729812008-04-17 14:15:45 -0400143static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 size_t count, loff_t *ppos)
145
146{
Eric Parisb77a4932010-11-23 11:40:08 -0500147 char *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ssize_t length;
149 int new_value;
150
Davi Arnautbfd51622005-10-30 14:59:24 -0800151 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500152 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500153
154 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500155 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500156 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500157
Al Viro8365a712015-12-24 00:08:06 -0500158 page = memdup_user_nul(buf, count);
159 if (IS_ERR(page))
160 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 length = -EINVAL;
163 if (sscanf(page, "%d", &new_value) != 1)
164 goto out;
165
Stephen Smalleyea49d10ee2016-11-18 09:30:38 -0500166 new_value = !!new_value;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (new_value != selinux_enforcing) {
169 length = task_has_security(current, SECURITY__SETENFORCE);
170 if (length)
171 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000172 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500173 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
174 new_value, selinux_enforcing,
Eric W. Biederman581abc02012-08-20 00:09:36 -0700175 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500176 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 selinux_enforcing = new_value;
178 if (selinux_enforcing)
179 avc_ss_reset(0);
180 selnl_notify_setenforce(selinux_enforcing);
KaiGai Kohei11904162010-09-14 18:28:39 +0900181 selinux_status_update_setenforce(selinux_enforcing);
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{
201 char tmpbuf[TMPBUFLEN];
202 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -0500203 ino_t ino = file_inode(filp)->i_ino;
Eric Paris3f120702007-09-21 14:37:10 -0400204 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
205 security_get_reject_unknown() : !security_get_allow_unknown();
206
207 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
208 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
209}
210
211static const struct file_operations sel_handle_unknown_ops = {
212 .read = sel_read_handle_unknown,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200213 .llseek = generic_file_llseek,
Eric Paris3f120702007-09-21 14:37:10 -0400214};
215
KaiGai Kohei11904162010-09-14 18:28:39 +0900216static int sel_open_handle_status(struct inode *inode, struct file *filp)
217{
218 struct page *status = selinux_kernel_status_page();
219
220 if (!status)
221 return -ENOMEM;
222
223 filp->private_data = status;
224
225 return 0;
226}
227
228static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
229 size_t count, loff_t *ppos)
230{
231 struct page *status = filp->private_data;
232
233 BUG_ON(!status);
234
235 return simple_read_from_buffer(buf, count, ppos,
236 page_address(status),
237 sizeof(struct selinux_kernel_status));
238}
239
240static int sel_mmap_handle_status(struct file *filp,
241 struct vm_area_struct *vma)
242{
243 struct page *status = filp->private_data;
244 unsigned long size = vma->vm_end - vma->vm_start;
245
246 BUG_ON(!status);
247
248 /* only allows one page from the head */
249 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
250 return -EIO;
251 /* disallow writable mapping */
252 if (vma->vm_flags & VM_WRITE)
253 return -EPERM;
254 /* disallow mprotect() turns it into writable */
255 vma->vm_flags &= ~VM_MAYWRITE;
256
257 return remap_pfn_range(vma, vma->vm_start,
258 page_to_pfn(status),
259 size, vma->vm_page_prot);
260}
261
262static const struct file_operations sel_handle_status_ops = {
263 .open = sel_open_handle_status,
264 .read = sel_read_handle_status,
265 .mmap = sel_mmap_handle_status,
266 .llseek = generic_file_llseek,
267};
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris18729812008-04-17 14:15:45 -0400270static ssize_t sel_write_disable(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 size_t count, loff_t *ppos)
272
273{
Al Viro8365a712015-12-24 00:08:06 -0500274 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ssize_t length;
276 int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Davi Arnautbfd51622005-10-30 14:59:24 -0800278 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500279 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500280
281 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500282 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500283 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500284
Al Viro8365a712015-12-24 00:08:06 -0500285 page = memdup_user_nul(buf, count);
286 if (IS_ERR(page))
287 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 length = -EINVAL;
290 if (sscanf(page, "%d", &new_value) != 1)
291 goto out;
292
293 if (new_value) {
294 length = selinux_disable();
Eric Parisb77a4932010-11-23 11:40:08 -0500295 if (length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000297 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
Eric Paris4746ec52008-01-08 10:06:53 -0500298 "selinux=0 auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700299 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500300 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 length = count;
304out:
Al Viro8365a712015-12-24 00:08:06 -0500305 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return length;
307}
308#else
309#define sel_write_disable NULL
310#endif
311
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800312static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .write = sel_write_disable,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200314 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
317static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
Eric Paris18729812008-04-17 14:15:45 -0400318 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 char tmpbuf[TMPBUFLEN];
321 ssize_t length;
322
323 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
324 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
325}
326
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800327static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .read = sel_read_policyvers,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200329 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332/* declaration for sel_write_load */
333static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400334static int sel_make_classes(void);
Paul Moore3bb56b22008-01-29 08:38:19 -0500335static int sel_make_policycap(void);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400336
337/* declaration for sel_make_class_dirs */
Al Viroa1c2aa12012-03-18 20:36:59 -0400338static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400339 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341static ssize_t sel_read_mls(struct file *filp, char __user *buf,
342 size_t count, loff_t *ppos)
343{
344 char tmpbuf[TMPBUFLEN];
345 ssize_t length;
346
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100347 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
348 security_mls_enabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
350}
351
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800352static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .read = sel_read_mls,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200354 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Eric Pariscee74f42010-10-13 17:50:25 -0400357struct policy_load_memory {
358 size_t len;
359 void *data;
360};
361
362static int sel_open_policy(struct inode *inode, struct file *filp)
363{
364 struct policy_load_memory *plm = NULL;
365 int rc;
366
367 BUG_ON(filp->private_data);
368
369 mutex_lock(&sel_mutex);
370
371 rc = task_has_security(current, SECURITY__READ_POLICY);
372 if (rc)
373 goto err;
374
375 rc = -EBUSY;
376 if (policy_opened)
377 goto err;
378
379 rc = -ENOMEM;
380 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
381 if (!plm)
382 goto err;
383
384 if (i_size_read(inode) != security_policydb_len()) {
Al Viro59551022016-01-22 15:40:57 -0500385 inode_lock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400386 i_size_write(inode, security_policydb_len());
Al Viro59551022016-01-22 15:40:57 -0500387 inode_unlock(inode);
Eric Pariscee74f42010-10-13 17:50:25 -0400388 }
389
390 rc = security_read_policy(&plm->data, &plm->len);
391 if (rc)
392 goto err;
393
394 policy_opened = 1;
395
396 filp->private_data = plm;
397
398 mutex_unlock(&sel_mutex);
399
400 return 0;
401err:
402 mutex_unlock(&sel_mutex);
403
404 if (plm)
405 vfree(plm->data);
406 kfree(plm);
407 return rc;
408}
409
410static int sel_release_policy(struct inode *inode, struct file *filp)
411{
412 struct policy_load_memory *plm = filp->private_data;
413
414 BUG_ON(!plm);
415
416 policy_opened = 0;
417
418 vfree(plm->data);
419 kfree(plm);
420
421 return 0;
422}
423
424static ssize_t sel_read_policy(struct file *filp, char __user *buf,
425 size_t count, loff_t *ppos)
426{
427 struct policy_load_memory *plm = filp->private_data;
428 int ret;
429
430 mutex_lock(&sel_mutex);
431
432 ret = task_has_security(current, SECURITY__READ_POLICY);
433 if (ret)
434 goto out;
435
436 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
437out:
438 mutex_unlock(&sel_mutex);
439 return ret;
440}
441
Eric Paris845ca302010-10-13 17:50:31 -0400442static int sel_mmap_policy_fault(struct vm_area_struct *vma,
443 struct vm_fault *vmf)
444{
445 struct policy_load_memory *plm = vma->vm_file->private_data;
446 unsigned long offset;
447 struct page *page;
448
449 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
450 return VM_FAULT_SIGBUS;
451
452 offset = vmf->pgoff << PAGE_SHIFT;
453 if (offset >= roundup(plm->len, PAGE_SIZE))
454 return VM_FAULT_SIGBUS;
455
456 page = vmalloc_to_page(plm->data + offset);
457 get_page(page);
458
459 vmf->page = page;
460
461 return 0;
462}
463
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -0700464static const struct vm_operations_struct sel_mmap_policy_ops = {
Eric Paris845ca302010-10-13 17:50:31 -0400465 .fault = sel_mmap_policy_fault,
466 .page_mkwrite = sel_mmap_policy_fault,
467};
468
James Morrisad3fa082011-08-30 10:50:12 +1000469static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
Eric Paris845ca302010-10-13 17:50:31 -0400470{
471 if (vma->vm_flags & VM_SHARED) {
472 /* do not allow mprotect to make mapping writable */
473 vma->vm_flags &= ~VM_MAYWRITE;
474
475 if (vma->vm_flags & VM_WRITE)
476 return -EACCES;
477 }
478
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700479 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Eric Paris845ca302010-10-13 17:50:31 -0400480 vma->vm_ops = &sel_mmap_policy_ops;
481
482 return 0;
483}
484
Eric Pariscee74f42010-10-13 17:50:25 -0400485static const struct file_operations sel_policy_ops = {
486 .open = sel_open_policy,
487 .read = sel_read_policy,
Eric Paris845ca302010-10-13 17:50:31 -0400488 .mmap = sel_mmap_policy,
Eric Pariscee74f42010-10-13 17:50:25 -0400489 .release = sel_release_policy,
Eric Paris47a93a52012-02-16 15:08:39 -0500490 .llseek = generic_file_llseek,
Eric Pariscee74f42010-10-13 17:50:25 -0400491};
492
Eric Paris18729812008-04-17 14:15:45 -0400493static ssize_t sel_write_load(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 size_t count, loff_t *ppos)
495
496{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ssize_t length;
498 void *data = NULL;
499
Ingo Molnarbb0030792006-03-22 00:09:14 -0800500 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 length = task_has_security(current, SECURITY__LOAD_POLICY);
503 if (length)
504 goto out;
505
Eric Parisb77a4932010-11-23 11:40:08 -0500506 /* No partial writes. */
507 length = -EINVAL;
508 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Eric Parisb77a4932010-11-23 11:40:08 -0500511 length = -EFBIG;
512 if (count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -0500514
515 length = -ENOMEM;
516 data = vmalloc(count);
517 if (!data)
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 length = -EFAULT;
521 if (copy_from_user(data, buf, count) != 0)
522 goto out;
523
524 length = security_load_policy(data, count);
525 if (length)
526 goto out;
527
Eric Parisb77a4932010-11-23 11:40:08 -0500528 length = sel_make_bools();
529 if (length)
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400530 goto out1;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400531
Eric Parisb77a4932010-11-23 11:40:08 -0500532 length = sel_make_classes();
533 if (length)
Paul Moore3bb56b22008-01-29 08:38:19 -0500534 goto out1;
Paul Moore3bb56b22008-01-29 08:38:19 -0500535
Eric Parisb77a4932010-11-23 11:40:08 -0500536 length = sel_make_policycap();
537 if (length)
538 goto out1;
539
540 length = count;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -0400541
542out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000543 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
Eric Paris4746ec52008-01-08 10:06:53 -0500544 "policy loaded auid=%u ses=%u",
Eric W. Biederman581abc02012-08-20 00:09:36 -0700545 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -0500546 audit_get_sessionid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800548 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 vfree(data);
550 return length;
551}
552
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800553static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .write = sel_write_load,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200555 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556};
557
Eric Paris18729812008-04-17 14:15:45 -0400558static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Eric Parisb77a4932010-11-23 11:40:08 -0500560 char *canon = NULL;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800561 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ssize_t length;
563
564 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
565 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +0100568 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500569 if (length)
570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800572 length = security_sid_to_context(sid, &canon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500573 if (length)
574 goto out;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800575
Eric Parisb77a4932010-11-23 11:40:08 -0500576 length = -ERANGE;
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800577 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400578 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
579 "payload max\n", __func__, len);
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800580 goto out;
581 }
582
583 memcpy(buf, canon, len);
584 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800586 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return length;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
591 size_t count, loff_t *ppos)
592{
593 char tmpbuf[TMPBUFLEN];
594 ssize_t length;
595
596 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
597 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
598}
599
Eric Paris18729812008-04-17 14:15:45 -0400600static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 size_t count, loff_t *ppos)
602{
Al Viro8365a712015-12-24 00:08:06 -0500603 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ssize_t length;
605 unsigned int new_value;
606
607 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
608 if (length)
Al Viro8365a712015-12-24 00:08:06 -0500609 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Davi Arnautbfd51622005-10-30 14:59:24 -0800611 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -0500612 return -ENOMEM;
Eric Parisb77a4932010-11-23 11:40:08 -0500613
614 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -0500615 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -0500616 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -0500617
Al Viro8365a712015-12-24 00:08:06 -0500618 page = memdup_user_nul(buf, count);
619 if (IS_ERR(page))
620 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 length = -EINVAL;
623 if (sscanf(page, "%u", &new_value) != 1)
624 goto out;
625
626 selinux_checkreqprot = new_value ? 1 : 0;
627 length = count;
628out:
Al Viro8365a712015-12-24 00:08:06 -0500629 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return length;
631}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800632static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .read = sel_read_checkreqprot,
634 .write = sel_write_checkreqprot,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200635 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636};
637
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500638static ssize_t sel_write_validatetrans(struct file *file,
639 const char __user *buf,
640 size_t count, loff_t *ppos)
641{
642 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
643 char *req = NULL;
644 u32 osid, nsid, tsid;
645 u16 tclass;
646 int rc;
647
648 rc = task_has_security(current, SECURITY__VALIDATE_TRANS);
649 if (rc)
650 goto out;
651
652 rc = -ENOMEM;
653 if (count >= PAGE_SIZE)
654 goto out;
655
656 /* No partial writes. */
657 rc = -EINVAL;
658 if (*ppos != 0)
659 goto out;
660
661 rc = -ENOMEM;
662 req = kzalloc(count + 1, GFP_KERNEL);
663 if (!req)
664 goto out;
665
666 rc = -EFAULT;
667 if (copy_from_user(req, buf, count))
668 goto out;
669
670 rc = -ENOMEM;
671 oldcon = kzalloc(count + 1, GFP_KERNEL);
672 if (!oldcon)
673 goto out;
674
675 newcon = kzalloc(count + 1, GFP_KERNEL);
676 if (!newcon)
677 goto out;
678
679 taskcon = kzalloc(count + 1, GFP_KERNEL);
680 if (!taskcon)
681 goto out;
682
683 rc = -EINVAL;
684 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
685 goto out;
686
687 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
688 if (rc)
689 goto out;
690
691 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
692 if (rc)
693 goto out;
694
695 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
696 if (rc)
697 goto out;
698
699 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
700 if (!rc)
701 rc = count;
702out:
703 kfree(req);
704 kfree(oldcon);
705 kfree(newcon);
706 kfree(taskcon);
707 return rc;
708}
709
710static const struct file_operations sel_transition_ops = {
711 .write = sel_write_validatetrans,
712 .llseek = generic_file_llseek,
713};
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
717 */
Eric Paris18729812008-04-17 14:15:45 -0400718static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
719static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
720static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
721static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
722static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724static ssize_t (*write_op[])(struct file *, char *, size_t) = {
725 [SEL_ACCESS] = sel_write_access,
726 [SEL_CREATE] = sel_write_create,
727 [SEL_RELABEL] = sel_write_relabel,
728 [SEL_USER] = sel_write_user,
729 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800730 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
733static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
734{
Al Viro496ad9a2013-01-23 17:07:38 -0500735 ino_t ino = file_inode(file)->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 char *data;
737 ssize_t rv;
738
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800739 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
741
742 data = simple_transaction_get(file, buf, size);
743 if (IS_ERR(data))
744 return PTR_ERR(data);
745
Eric Paris18729812008-04-17 14:15:45 -0400746 rv = write_op[ino](file, data, size);
747 if (rv > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 simple_transaction_set(file, rv);
749 rv = size;
750 }
751 return rv;
752}
753
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800754static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .write = selinux_transaction_write,
756 .read = simple_transaction_read,
757 .release = simple_transaction_release,
Arnd Bergmann57a62c22010-07-07 23:40:10 +0200758 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759};
760
761/*
762 * payload - write methods
763 * If the method has a response, the response should be put in buf,
764 * and the length returned. Otherwise return 0 or and -error.
765 */
766
Eric Paris18729812008-04-17 14:15:45 -0400767static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Eric Parisb77a4932010-11-23 11:40:08 -0500769 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 u32 ssid, tsid;
771 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct av_decision avd;
773 ssize_t length;
774
775 length = task_has_security(current, SECURITY__COMPUTE_AV);
776 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500777 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800780 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Eric Parisb77a4932010-11-23 11:40:08 -0500784 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800785 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!tcon)
787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 length = -EINVAL;
Stephen Smalley19439d02010-01-14 17:28:10 -0500790 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400793 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500794 if (length)
795 goto out;
796
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400797 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500798 if (length)
799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Stephen Smalley19439d02010-01-14 17:28:10 -0500801 security_compute_av_user(ssid, tsid, tclass, &avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900804 "%x %x %x %x %u %x",
Eric Parisf1c63812009-02-12 14:50:54 -0500805 avd.allowed, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 avd.auditallow, avd.auditdeny,
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900807 avd.seqno, avd.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808out:
Eric Parisb77a4932010-11-23 11:40:08 -0500809 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 kfree(scon);
811 return length;
812}
813
Eric Paris18729812008-04-17 14:15:45 -0400814static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Eric Parisb77a4932010-11-23 11:40:08 -0500816 char *scon = NULL, *tcon = NULL;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100817 char *namebuf = NULL, *objname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 u32 ssid, tsid, newsid;
819 u16 tclass;
820 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500821 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 u32 len;
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100823 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
826 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
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100839 length = -ENOMEM;
840 namebuf = kzalloc(size + 1, GFP_KERNEL);
841 if (!namebuf)
Eric Parisb77a4932010-11-23 11:40:08 -0500842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100844 length = -EINVAL;
845 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
846 if (nargs < 3 || nargs > 4)
847 goto out;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400848 if (nargs == 4) {
849 /*
850 * If and when the name of new object to be queried contains
851 * either whitespace or multibyte characters, they shall be
852 * encoded based on the percentage-encoding rule.
853 * If not encoded, the sscanf logic picks up only left-half
854 * of the supplied name; splitted by a whitespace unexpectedly.
855 */
856 char *r, *w;
857 int c1, c2;
858
859 r = w = namebuf;
860 do {
861 c1 = *r++;
862 if (c1 == '+')
863 c1 = ' ';
864 else if (c1 == '%') {
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800865 c1 = hex_to_bin(*r++);
866 if (c1 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400867 goto out;
Andy Shevchenkoaf7ff2c2011-11-15 15:11:41 -0800868 c2 = hex_to_bin(*r++);
869 if (c2 < 0)
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400870 goto out;
871 c1 = (c1 << 4) | c2;
872 }
873 *w++ = c1;
874 } while (c1 != '\0');
875
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100876 objname = namebuf;
Kohei Kaigai0f7e4c32011-05-26 14:59:25 -0400877 }
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100878
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400879 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500880 if (length)
881 goto out;
882
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400883 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500884 if (length)
885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100887 length = security_transition_sid_user(ssid, tsid, tclass,
888 objname, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500889 if (length)
890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500893 if (length)
894 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Eric Parisb77a4932010-11-23 11:40:08 -0500896 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -0400898 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
899 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -0500900 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 memcpy(buf, newcon, len);
904 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905out:
Eric Parisb77a4932010-11-23 11:40:08 -0500906 kfree(newcon);
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +0100907 kfree(namebuf);
Eric Parisb77a4932010-11-23 11:40:08 -0500908 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 kfree(scon);
910 return length;
911}
912
Eric Paris18729812008-04-17 14:15:45 -0400913static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Eric Parisb77a4932010-11-23 11:40:08 -0500915 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 u32 ssid, tsid, newsid;
917 u16 tclass;
918 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -0500919 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 u32 len;
921
922 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
923 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -0500924 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800927 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!scon)
Eric Parisb77a4932010-11-23 11:40:08 -0500929 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Parisb77a4932010-11-23 11:40:08 -0500931 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800932 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!tcon)
934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 length = -EINVAL;
937 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -0500938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400940 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500941 if (length)
942 goto out;
943
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400944 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500945 if (length)
946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 length = security_change_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -0500949 if (length)
950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -0500953 if (length)
954 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Eric Parisb77a4932010-11-23 11:40:08 -0500956 length = -ERANGE;
957 if (len > SIMPLE_TRANSACTION_LIMIT)
958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 memcpy(buf, newcon, len);
961 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962out:
Eric Parisb77a4932010-11-23 11:40:08 -0500963 kfree(newcon);
964 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 kfree(scon);
966 return length;
967}
968
Eric Paris18729812008-04-17 14:15:45 -0400969static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Eric Parisb77a4932010-11-23 11:40:08 -0500971 char *con = NULL, *user = NULL, *ptr;
972 u32 sid, *sids = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ssize_t length;
974 char *newcon;
975 int i, rc;
976 u32 len, nsids;
977
978 length = task_has_security(current, SECURITY__COMPUTE_USER);
979 if (length)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800983 con = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (!con)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Eric Parisb77a4932010-11-23 11:40:08 -0500987 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800988 user = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!user)
990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 length = -EINVAL;
993 if (sscanf(buf, "%s %s", con, user) != 2)
Eric Parisb77a4932010-11-23 11:40:08 -0500994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400996 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -0500997 if (length)
998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 length = security_get_user_sids(sid, user, &sids, &nsids);
Eric Parisb77a4932010-11-23 11:40:08 -05001001 if (length)
1002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 length = sprintf(buf, "%u", nsids) + 1;
1005 ptr = buf + length;
1006 for (i = 0; i < nsids; i++) {
1007 rc = security_sid_to_context(sids[i], &newcon, &len);
1008 if (rc) {
1009 length = rc;
Eric Parisb77a4932010-11-23 11:40:08 -05001010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1013 kfree(newcon);
1014 length = -ERANGE;
Eric Parisb77a4932010-11-23 11:40:08 -05001015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 memcpy(ptr, newcon, len);
1018 kfree(newcon);
1019 ptr += len;
1020 length += len;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022out:
Eric Parisb77a4932010-11-23 11:40:08 -05001023 kfree(sids);
1024 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 kfree(con);
1026 return length;
1027}
1028
Eric Paris18729812008-04-17 14:15:45 -04001029static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Eric Parisb77a4932010-11-23 11:40:08 -05001031 char *scon = NULL, *tcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 u32 ssid, tsid, newsid;
1033 u16 tclass;
1034 ssize_t length;
Eric Parisb77a4932010-11-23 11:40:08 -05001035 char *newcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 u32 len;
1037
1038 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
1039 if (length)
Eric Parisb77a4932010-11-23 11:40:08 -05001040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001043 scon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!scon)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Eric Parisb77a4932010-11-23 11:40:08 -05001047 length = -ENOMEM;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001048 tcon = kzalloc(size + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!tcon)
1050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 length = -EINVAL;
1053 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
Eric Parisb77a4932010-11-23 11:40:08 -05001054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001056 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001057 if (length)
1058 goto out;
1059
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001060 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001061 if (length)
1062 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 length = security_member_sid(ssid, tsid, tclass, &newsid);
Eric Parisb77a4932010-11-23 11:40:08 -05001065 if (length)
1066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 length = security_sid_to_context(newsid, &newcon, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001069 if (length)
1070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Eric Parisb77a4932010-11-23 11:40:08 -05001072 length = -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (len > SIMPLE_TRANSACTION_LIMIT) {
Eric Paris744ba352008-04-17 11:52:44 -04001074 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1075 "payload max\n", __func__, len);
Eric Parisb77a4932010-11-23 11:40:08 -05001076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
1079 memcpy(buf, newcon, len);
1080 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081out:
Eric Parisb77a4932010-11-23 11:40:08 -05001082 kfree(newcon);
1083 kfree(tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 kfree(scon);
1085 return length;
1086}
1087
1088static struct inode *sel_make_inode(struct super_block *sb, int mode)
1089{
1090 struct inode *ret = new_inode(sb);
1091
1092 if (ret) {
1093 ret->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001094 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 return ret;
1097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1100 size_t count, loff_t *ppos)
1101{
1102 char *page = NULL;
1103 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ssize_t ret;
1105 int cur_enforcing;
Al Viro496ad9a2013-01-23 17:07:38 -05001106 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001107 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Ingo Molnarbb0030792006-03-22 00:09:14 -08001109 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Parisb77a4932010-11-23 11:40:08 -05001111 ret = -EINVAL;
1112 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001113 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Eric Parisb77a4932010-11-23 11:40:08 -05001115 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001116 page = (char *)get_zeroed_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001117 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001120 cur_enforcing = security_get_bool_value(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (cur_enforcing < 0) {
1122 ret = cur_enforcing;
1123 goto out;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001126 bool_pending_values[index]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -08001127 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001129 mutex_unlock(&sel_mutex);
Eric Parisb77a4932010-11-23 11:40:08 -05001130 free_page((unsigned long)page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return ret;
1132}
1133
1134static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1135 size_t count, loff_t *ppos)
1136{
1137 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001138 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int new_value;
Al Viro496ad9a2013-01-23 17:07:38 -05001140 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001141 const char *name = filep->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Ingo Molnarbb0030792006-03-22 00:09:14 -08001143 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 length = task_has_security(current, SECURITY__SETBOOL);
1146 if (length)
1147 goto out;
1148
Eric Parisb77a4932010-11-23 11:40:08 -05001149 length = -EINVAL;
1150 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001151 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001152
Eric Parisb77a4932010-11-23 11:40:08 -05001153 length = -ENOMEM;
1154 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 goto out;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001156
Eric Parisb77a4932010-11-23 11:40:08 -05001157 /* No partial writes. */
1158 length = -EINVAL;
1159 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001161
Al Viro8365a712015-12-24 00:08:06 -05001162 page = memdup_user_nul(buf, count);
1163 if (IS_ERR(page)) {
1164 length = PTR_ERR(page);
1165 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 length = -EINVAL;
1170 if (sscanf(page, "%d", &new_value) != 1)
1171 goto out;
1172
1173 if (new_value)
1174 new_value = 1;
1175
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001176 bool_pending_values[index] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 length = count;
1178
1179out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001180 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001181 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return length;
1183}
1184
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001185static const struct file_operations sel_bool_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001186 .read = sel_read_bool,
1187 .write = sel_write_bool,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001188 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189};
1190
1191static ssize_t sel_commit_bools_write(struct file *filep,
1192 const char __user *buf,
1193 size_t count, loff_t *ppos)
1194{
1195 char *page = NULL;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001196 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int new_value;
1198
Ingo Molnarbb0030792006-03-22 00:09:14 -08001199 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 length = task_has_security(current, SECURITY__SETBOOL);
1202 if (length)
1203 goto out;
1204
Eric Parisb77a4932010-11-23 11:40:08 -05001205 length = -ENOMEM;
1206 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001208
1209 /* No partial writes. */
1210 length = -EINVAL;
1211 if (*ppos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto out;
Eric Parisb77a4932010-11-23 11:40:08 -05001213
Al Viro8365a712015-12-24 00:08:06 -05001214 page = memdup_user_nul(buf, count);
1215 if (IS_ERR(page)) {
1216 length = PTR_ERR(page);
1217 page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto out;
Al Viro8365a712015-12-24 00:08:06 -05001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 length = -EINVAL;
1222 if (sscanf(page, "%d", &new_value) != 1)
1223 goto out;
1224
Eric Parisb77a4932010-11-23 11:40:08 -05001225 length = 0;
Eric Paris18729812008-04-17 14:15:45 -04001226 if (new_value && bool_pending_values)
Eric Parisb77a4932010-11-23 11:40:08 -05001227 length = security_set_bools(bool_num, bool_pending_values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Eric Parisb77a4932010-11-23 11:40:08 -05001229 if (!length)
1230 length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232out:
Ingo Molnarbb0030792006-03-22 00:09:14 -08001233 mutex_unlock(&sel_mutex);
Al Viro8365a712015-12-24 00:08:06 -05001234 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return length;
1236}
1237
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001238static const struct file_operations sel_commit_bools_ops = {
Eric Paris18729812008-04-17 14:15:45 -04001239 .write = sel_commit_bools_write,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001240 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241};
1242
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001243static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Al Viroad521842014-12-24 14:56:48 -05001245 d_genocide(de);
1246 shrink_dcache_parent(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249#define BOOL_DIR_NAME "booleans"
1250
1251static int sel_make_bools(void)
1252{
Eric Parisb77a4932010-11-23 11:40:08 -05001253 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ssize_t len;
1255 struct dentry *dentry = NULL;
1256 struct dentry *dir = bool_dir;
1257 struct inode *inode = NULL;
1258 struct inode_security_struct *isec;
1259 char **names = NULL, *page;
1260 int num;
1261 int *values = NULL;
1262 u32 sid;
1263
1264 /* remove any existing files */
Xiaotian Feng8007f102010-02-09 08:22:24 +11001265 for (i = 0; i < bool_num; i++)
1266 kfree(bool_pending_names[i]);
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001267 kfree(bool_pending_names);
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001268 kfree(bool_pending_values);
Eric Paris154c50c2012-04-04 13:47:11 -04001269 bool_num = 0;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001270 bool_pending_names = NULL;
Davi Arnaut20c19e42005-10-23 12:57:16 -07001271 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001273 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Eric Parisb77a4932010-11-23 11:40:08 -05001275 ret = -ENOMEM;
Eric Paris18729812008-04-17 14:15:45 -04001276 page = (char *)get_zeroed_page(GFP_KERNEL);
1277 if (!page)
Eric Parisb77a4932010-11-23 11:40:08 -05001278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 ret = security_get_bools(&num, &names, &values);
Eric Parisb77a4932010-11-23 11:40:08 -05001281 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out;
1283
1284 for (i = 0; i < num; i++) {
Eric Parisb77a4932010-11-23 11:40:08 -05001285 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 dentry = d_alloc_name(dir, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001287 if (!dentry)
1288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Eric Parisb77a4932010-11-23 11:40:08 -05001290 ret = -ENOMEM;
1291 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1292 if (!inode)
1293 goto out;
1294
Eric Parisb77a4932010-11-23 11:40:08 -05001295 ret = -ENAMETOOLONG;
Al Virocc1dad72012-04-02 19:40:47 -04001296 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001297 if (len >= PAGE_SIZE)
1298 goto out;
1299
Eric Paris18729812008-04-17 14:15:45 -04001300 isec = (struct inode_security_struct *)inode->i_security;
1301 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1302 if (ret)
Eric Parisb77a4932010-11-23 11:40:08 -05001303 goto out;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 isec->sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001306 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001308 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 d_add(dentry, inode);
1310 }
1311 bool_num = num;
Stephen Smalleyd313f94832007-11-26 11:12:53 -05001312 bool_pending_names = names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 bool_pending_values = values;
Eric Parisb77a4932010-11-23 11:40:08 -05001314
1315 free_page((unsigned long)page);
1316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317out:
1318 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001321 for (i = 0; i < num; i++)
1322 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 kfree(names);
1324 }
Davi Arnaut20c19e42005-10-23 12:57:16 -07001325 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001326 sel_remove_entries(dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001327
1328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331#define NULL_FILE_NAME "null"
1332
Al Viro765927b2012-06-26 21:58:53 +04001333struct path selinux_null;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1336 size_t count, loff_t *ppos)
1337{
1338 char tmpbuf[TMPBUFLEN];
1339 ssize_t length;
1340
1341 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1342 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1343}
1344
Eric Paris18729812008-04-17 14:15:45 -04001345static ssize_t sel_write_avc_cache_threshold(struct file *file,
1346 const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 size_t count, loff_t *ppos)
1348
1349{
Al Viro8365a712015-12-24 00:08:06 -05001350 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ssize_t ret;
Heinrich Schuchardt309c5fa2016-06-10 23:14:26 +02001352 unsigned int new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Eric Parisb77a4932010-11-23 11:40:08 -05001354 ret = task_has_security(current, SECURITY__SETSECPARAM);
1355 if (ret)
Al Viro8365a712015-12-24 00:08:06 -05001356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Eric Parisb77a4932010-11-23 11:40:08 -05001358 if (count >= PAGE_SIZE)
Al Viro8365a712015-12-24 00:08:06 -05001359 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Eric Parisb77a4932010-11-23 11:40:08 -05001361 /* No partial writes. */
Eric Parisb77a4932010-11-23 11:40:08 -05001362 if (*ppos != 0)
Al Viro8365a712015-12-24 00:08:06 -05001363 return -EINVAL;
Eric Parisb77a4932010-11-23 11:40:08 -05001364
Al Viro8365a712015-12-24 00:08:06 -05001365 page = memdup_user_nul(buf, count);
1366 if (IS_ERR(page))
1367 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric Parisb77a4932010-11-23 11:40:08 -05001369 ret = -EINVAL;
1370 if (sscanf(page, "%u", &new_value) != 1)
1371 goto out;
1372
1373 avc_cache_threshold = new_value;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ret = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376out:
Al Viro8365a712015-12-24 00:08:06 -05001377 kfree(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return ret;
1379}
1380
1381static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1382 size_t count, loff_t *ppos)
1383{
1384 char *page;
Eric Parisb77a4932010-11-23 11:40:08 -05001385 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 page = (char *)__get_free_page(GFP_KERNEL);
Eric Parisb77a4932010-11-23 11:40:08 -05001388 if (!page)
1389 return -ENOMEM;
1390
1391 length = avc_get_hash_stats(page);
1392 if (length >= 0)
1393 length = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 free_page((unsigned long)page);
Eric Parisb77a4932010-11-23 11:40:08 -05001395
1396 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001399static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 .read = sel_read_avc_cache_threshold,
1401 .write = sel_write_avc_cache_threshold,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001402 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403};
1404
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001405static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 .read = sel_read_avc_hash_stats,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001407 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408};
1409
1410#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1411static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1412{
1413 int cpu;
1414
Rusty Russell4f4b6c12009-01-01 10:12:15 +10301415 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!cpu_possible(cpu))
1417 continue;
1418 *idx = cpu + 1;
1419 return &per_cpu(avc_cache_stats, cpu);
1420 }
1421 return NULL;
1422}
1423
1424static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1425{
1426 loff_t n = *pos - 1;
1427
1428 if (*pos == 0)
1429 return SEQ_START_TOKEN;
1430
1431 return sel_avc_get_stat_idx(&n);
1432}
1433
1434static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1435{
1436 return sel_avc_get_stat_idx(pos);
1437}
1438
1439static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1440{
1441 struct avc_cache_stats *st = v;
1442
1443 if (v == SEQ_START_TOKEN)
1444 seq_printf(seq, "lookups hits misses allocations reclaims "
1445 "frees\n");
Linus Torvalds257313b2011-05-19 21:22:53 -07001446 else {
1447 unsigned int lookups = st->lookups;
1448 unsigned int misses = st->misses;
1449 unsigned int hits = lookups - misses;
1450 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1451 hits, misses, st->allocations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 st->reclaims, st->frees);
Linus Torvalds257313b2011-05-19 21:22:53 -07001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455}
1456
1457static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1458{ }
1459
Jan Engelhardt1996a102008-01-23 00:02:58 +01001460static const struct seq_operations sel_avc_cache_stats_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 .start = sel_avc_stats_seq_start,
1462 .next = sel_avc_stats_seq_next,
1463 .show = sel_avc_stats_seq_show,
1464 .stop = sel_avc_stats_seq_stop,
1465};
1466
1467static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1468{
1469 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1470}
1471
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001472static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 .open = sel_open_avc_cache_stats,
1474 .read = seq_read,
1475 .llseek = seq_lseek,
1476 .release = seq_release,
1477};
1478#endif
1479
1480static int sel_make_avc_files(struct dentry *dir)
1481{
Eric Parisb77a4932010-11-23 11:40:08 -05001482 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 static struct tree_descr files[] = {
1484 { "cache_threshold",
1485 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1486 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1487#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1488 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1489#endif
1490 };
1491
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001492 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct inode *inode;
1494 struct dentry *dentry;
1495
1496 dentry = d_alloc_name(dir, files[i].name);
Eric Parisb77a4932010-11-23 11:40:08 -05001497 if (!dentry)
1498 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
Eric Parisb77a4932010-11-23 11:40:08 -05001501 if (!inode)
1502 return -ENOMEM;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001505 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 d_add(dentry, inode);
1507 }
Eric Parisb77a4932010-11-23 11:40:08 -05001508
1509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Eric Paris18729812008-04-17 14:15:45 -04001512static ssize_t sel_read_initcon(struct file *file, char __user *buf,
James Carterf0ee2e42007-04-04 10:11:29 -04001513 size_t count, loff_t *ppos)
1514{
James Carterf0ee2e42007-04-04 10:11:29 -04001515 char *con;
1516 u32 sid, len;
1517 ssize_t ret;
1518
Al Viro496ad9a2013-01-23 17:07:38 -05001519 sid = file_inode(file)->i_ino&SEL_INO_MASK;
James Carterf0ee2e42007-04-04 10:11:29 -04001520 ret = security_sid_to_context(sid, &con, &len);
Eric Parisb77a4932010-11-23 11:40:08 -05001521 if (ret)
James Carterf0ee2e42007-04-04 10:11:29 -04001522 return ret;
1523
1524 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1525 kfree(con);
1526 return ret;
1527}
1528
1529static const struct file_operations sel_initcon_ops = {
1530 .read = sel_read_initcon,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001531 .llseek = generic_file_llseek,
James Carterf0ee2e42007-04-04 10:11:29 -04001532};
1533
1534static int sel_make_initcon_files(struct dentry *dir)
1535{
Eric Parisb77a4932010-11-23 11:40:08 -05001536 int i;
James Carterf0ee2e42007-04-04 10:11:29 -04001537
1538 for (i = 1; i <= SECINITSID_NUM; i++) {
1539 struct inode *inode;
1540 struct dentry *dentry;
1541 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
Eric Parisb77a4932010-11-23 11:40:08 -05001542 if (!dentry)
1543 return -ENOMEM;
James Carterf0ee2e42007-04-04 10:11:29 -04001544
1545 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001546 if (!inode)
1547 return -ENOMEM;
1548
James Carterf0ee2e42007-04-04 10:11:29 -04001549 inode->i_fop = &sel_initcon_ops;
1550 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1551 d_add(dentry, inode);
1552 }
Eric Parisb77a4932010-11-23 11:40:08 -05001553
1554 return 0;
James Carterf0ee2e42007-04-04 10:11:29 -04001555}
1556
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001557static inline unsigned long sel_class_to_ino(u16 class)
1558{
1559 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1560}
1561
1562static inline u16 sel_ino_to_class(unsigned long ino)
1563{
Eric Paris92ae9e82012-04-04 13:46:46 -04001564 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001565}
1566
1567static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1568{
1569 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1570}
1571
1572static inline u32 sel_ino_to_perm(unsigned long ino)
1573{
1574 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1575}
1576
Eric Paris18729812008-04-17 14:15:45 -04001577static ssize_t sel_read_class(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001578 size_t count, loff_t *ppos)
1579{
Al Viro496ad9a2013-01-23 17:07:38 -05001580 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001581 char res[TMPBUFLEN];
1582 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1583 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001584}
1585
1586static const struct file_operations sel_class_ops = {
1587 .read = sel_read_class,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001588 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001589};
1590
Eric Paris18729812008-04-17 14:15:45 -04001591static ssize_t sel_read_perm(struct file *file, char __user *buf,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001592 size_t count, loff_t *ppos)
1593{
Al Viro496ad9a2013-01-23 17:07:38 -05001594 unsigned long ino = file_inode(file)->i_ino;
Al Virocc1dad72012-04-02 19:40:47 -04001595 char res[TMPBUFLEN];
1596 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1597 return simple_read_from_buffer(buf, count, ppos, res, len);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001598}
1599
1600static const struct file_operations sel_perm_ops = {
1601 .read = sel_read_perm,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001602 .llseek = generic_file_llseek,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001603};
1604
Paul Moore3bb56b22008-01-29 08:38:19 -05001605static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1606 size_t count, loff_t *ppos)
1607{
1608 int value;
1609 char tmpbuf[TMPBUFLEN];
1610 ssize_t length;
Al Viro496ad9a2013-01-23 17:07:38 -05001611 unsigned long i_ino = file_inode(file)->i_ino;
Paul Moore3bb56b22008-01-29 08:38:19 -05001612
1613 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1614 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1615
1616 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1617}
1618
1619static const struct file_operations sel_policycap_ops = {
1620 .read = sel_read_policycap,
Arnd Bergmann57a62c22010-07-07 23:40:10 +02001621 .llseek = generic_file_llseek,
Paul Moore3bb56b22008-01-29 08:38:19 -05001622};
1623
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001624static int sel_make_perm_files(char *objclass, int classvalue,
1625 struct dentry *dir)
1626{
Eric Parisb77a4932010-11-23 11:40:08 -05001627 int i, rc, nperms;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001628 char **perms;
1629
1630 rc = security_get_permissions(objclass, &perms, &nperms);
1631 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001632 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001633
1634 for (i = 0; i < nperms; i++) {
1635 struct inode *inode;
1636 struct dentry *dentry;
1637
Eric Parisb77a4932010-11-23 11:40:08 -05001638 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001639 dentry = d_alloc_name(dir, perms[i]);
Eric Parisb77a4932010-11-23 11:40:08 -05001640 if (!dentry)
1641 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001642
Eric Parisb77a4932010-11-23 11:40:08 -05001643 rc = -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001644 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001645 if (!inode)
1646 goto out;
1647
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001648 inode->i_fop = &sel_perm_ops;
1649 /* i+1 since perm values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001650 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001651 d_add(dentry, inode);
1652 }
Eric Parisb77a4932010-11-23 11:40:08 -05001653 rc = 0;
1654out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001655 for (i = 0; i < nperms; i++)
1656 kfree(perms[i]);
1657 kfree(perms);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001658 return rc;
1659}
1660
1661static int sel_make_class_dir_entries(char *classname, int index,
1662 struct dentry *dir)
1663{
1664 struct dentry *dentry = NULL;
1665 struct inode *inode = NULL;
1666 int rc;
1667
1668 dentry = d_alloc_name(dir, "index");
Eric Parisb77a4932010-11-23 11:40:08 -05001669 if (!dentry)
1670 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001671
1672 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001673 if (!inode)
1674 return -ENOMEM;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001675
1676 inode->i_fop = &sel_class_ops;
1677 inode->i_ino = sel_class_to_ino(index);
1678 d_add(dentry, inode);
1679
Al Viroa1c2aa12012-03-18 20:36:59 -04001680 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1681 if (IS_ERR(dentry))
1682 return PTR_ERR(dentry);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001683
1684 rc = sel_make_perm_files(classname, index, dentry);
1685
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001686 return rc;
1687}
1688
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001689static int sel_make_classes(void)
1690{
Eric Parisb77a4932010-11-23 11:40:08 -05001691 int rc, nclasses, i;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001692 char **classes;
1693
1694 /* delete any existing entries */
Al Viroad521842014-12-24 14:56:48 -05001695 sel_remove_entries(class_dir);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001696
1697 rc = security_get_classes(&classes, &nclasses);
Eric Parisb77a4932010-11-23 11:40:08 -05001698 if (rc)
1699 return rc;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001700
1701 /* +2 since classes are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001702 last_class_ino = sel_class_to_ino(nclasses + 2);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001703
1704 for (i = 0; i < nclasses; i++) {
1705 struct dentry *class_name_dir;
1706
Al Viroa1c2aa12012-03-18 20:36:59 -04001707 class_name_dir = sel_make_dir(class_dir, classes[i],
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001708 &last_class_ino);
Al Viroa1c2aa12012-03-18 20:36:59 -04001709 if (IS_ERR(class_name_dir)) {
1710 rc = PTR_ERR(class_name_dir);
Eric Parisb77a4932010-11-23 11:40:08 -05001711 goto out;
Al Viroa1c2aa12012-03-18 20:36:59 -04001712 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001713
1714 /* i+1 since class values are 1-indexed */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001715 rc = sel_make_class_dir_entries(classes[i], i + 1,
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001716 class_name_dir);
1717 if (rc)
Eric Parisb77a4932010-11-23 11:40:08 -05001718 goto out;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001719 }
Eric Parisb77a4932010-11-23 11:40:08 -05001720 rc = 0;
1721out:
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001722 for (i = 0; i < nclasses; i++)
1723 kfree(classes[i]);
1724 kfree(classes);
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001725 return rc;
1726}
1727
Paul Moore3bb56b22008-01-29 08:38:19 -05001728static int sel_make_policycap(void)
1729{
1730 unsigned int iter;
1731 struct dentry *dentry = NULL;
1732 struct inode *inode = NULL;
1733
1734 sel_remove_entries(policycap_dir);
1735
1736 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1737 if (iter < ARRAY_SIZE(policycap_names))
1738 dentry = d_alloc_name(policycap_dir,
1739 policycap_names[iter]);
1740 else
1741 dentry = d_alloc_name(policycap_dir, "unknown");
1742
1743 if (dentry == NULL)
1744 return -ENOMEM;
1745
1746 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1747 if (inode == NULL)
1748 return -ENOMEM;
1749
1750 inode->i_fop = &sel_policycap_ops;
1751 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1752 d_add(dentry, inode);
1753 }
1754
1755 return 0;
1756}
1757
Al Viroa1c2aa12012-03-18 20:36:59 -04001758static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001759 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Al Viroa1c2aa12012-03-18 20:36:59 -04001761 struct dentry *dentry = d_alloc_name(dir, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct inode *inode;
1763
Al Viroa1c2aa12012-03-18 20:36:59 -04001764 if (!dentry)
1765 return ERR_PTR(-ENOMEM);
1766
1767 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1768 if (!inode) {
1769 dput(dentry);
1770 return ERR_PTR(-ENOMEM);
1771 }
Eric Parisb77a4932010-11-23 11:40:08 -05001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 inode->i_op = &simple_dir_inode_operations;
1774 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001775 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001776 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001777 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001779 /* bump link count on parent directory, too */
David Howellsce0b16d2015-02-19 10:47:02 +00001780 inc_nlink(d_inode(dir));
Eric Parisb77a4932010-11-23 11:40:08 -05001781
Al Viroa1c2aa12012-03-18 20:36:59 -04001782 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Eric Paris18729812008-04-17 14:15:45 -04001785static int sel_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 int ret;
1788 struct dentry *dentry;
Al Viroa1c2aa12012-03-18 20:36:59 -04001789 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct inode_security_struct *isec;
1791
1792 static struct tree_descr selinux_files[] = {
1793 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1794 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001795 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1797 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1798 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1799 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1800 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1801 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1802 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1803 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1804 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1805 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
Eric Paris3f120702007-09-21 14:37:10 -04001806 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1807 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
KaiGai Kohei11904162010-09-14 18:28:39 +09001808 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
Eric Paris72e8c8592012-02-16 15:08:39 -05001809 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
Andrew Perepechkof9df6452015-12-24 11:09:41 -05001810 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1811 S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* last one */ {""}
1813 };
1814 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1815 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001816 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Al Viroa1c2aa12012-03-18 20:36:59 -04001818 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1819 if (IS_ERR(bool_dir)) {
1820 ret = PTR_ERR(bool_dir);
1821 bool_dir = NULL;
James Morris161ce452006-03-22 00:09:17 -08001822 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Eric Parisb77a4932010-11-23 11:40:08 -05001825 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
Eric Parisb77a4932010-11-23 11:40:08 -05001827 if (!dentry)
James Morris161ce452006-03-22 00:09:17 -08001828 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Eric Parisb77a4932010-11-23 11:40:08 -05001830 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
Eric Parisb77a4932010-11-23 11:40:08 -05001832 if (!inode)
James Morris161ce452006-03-22 00:09:17 -08001833 goto err;
Eric Parisb77a4932010-11-23 11:40:08 -05001834
James Carter6174eaf2007-04-04 16:18:39 -04001835 inode->i_ino = ++sel_last_ino;
Eric Paris18729812008-04-17 14:15:45 -04001836 isec = (struct inode_security_struct *)inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 isec->sid = SECINITSID_DEVNULL;
1838 isec->sclass = SECCLASS_CHR_FILE;
Andreas Gruenbacher42059112016-11-10 22:18:27 +01001839 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1842 d_add(dentry, inode);
Al Viro765927b2012-06-26 21:58:53 +04001843 selinux_null.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Al Viroa1c2aa12012-03-18 20:36:59 -04001845 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1846 if (IS_ERR(dentry)) {
1847 ret = PTR_ERR(dentry);
James Morris161ce452006-03-22 00:09:17 -08001848 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 ret = sel_make_avc_files(dentry);
1852 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001853 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001854
Al Viroa1c2aa12012-03-18 20:36:59 -04001855 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1856 if (IS_ERR(dentry)) {
1857 ret = PTR_ERR(dentry);
James Carterf0ee2e42007-04-04 10:11:29 -04001858 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001859 }
James Carterf0ee2e42007-04-04 10:11:29 -04001860
1861 ret = sel_make_initcon_files(dentry);
1862 if (ret)
1863 goto err;
1864
Al Viroa1c2aa12012-03-18 20:36:59 -04001865 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1866 if (IS_ERR(class_dir)) {
1867 ret = PTR_ERR(class_dir);
1868 class_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001869 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001870 }
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001871
Al Viroa1c2aa12012-03-18 20:36:59 -04001872 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1873 if (IS_ERR(policycap_dir)) {
1874 ret = PTR_ERR(policycap_dir);
1875 policycap_dir = NULL;
Christopher J. PeBenitoe47c8fc52007-05-23 09:12:09 -04001876 goto err;
Al Viroa1c2aa12012-03-18 20:36:59 -04001877 }
Eric Parisb77a4932010-11-23 11:40:08 -05001878 return 0;
James Morris161ce452006-03-22 00:09:17 -08001879err:
Eric Paris744ba352008-04-17 11:52:44 -04001880 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1881 __func__);
Eric Parisb77a4932010-11-23 11:40:08 -05001882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Al Virofc14f2f2010-07-25 01:48:30 +04001885static struct dentry *sel_mount(struct file_system_type *fs_type,
1886 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Al Virofc14f2f2010-07-25 01:48:30 +04001888 return mount_single(fs_type, flags, data, sel_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
1891static struct file_system_type sel_fs_type = {
1892 .name = "selinuxfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001893 .mount = sel_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 .kill_sb = kill_litter_super,
1895};
1896
1897struct vfsmount *selinuxfs_mount;
1898
1899static int __init init_sel_fs(void)
1900{
1901 int err;
1902
1903 if (!selinux_enabled)
1904 return 0;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001905
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001906 err = sysfs_create_mount_point(fs_kobj, "selinux");
1907 if (err)
1908 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 err = register_filesystem(&sel_fs_type);
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001911 if (err) {
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001912 sysfs_remove_mount_point(fs_kobj, "selinux");
Eric Parisb77a4932010-11-23 11:40:08 -05001913 return err;
Greg Kroah-Hartman7a627e32011-05-10 15:34:16 -07001914 }
Eric Parisb77a4932010-11-23 11:40:08 -05001915
Al Viro765927b2012-06-26 21:58:53 +04001916 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
Eric Parisb77a4932010-11-23 11:40:08 -05001917 if (IS_ERR(selinuxfs_mount)) {
1918 printk(KERN_ERR "selinuxfs: could not mount!\n");
1919 err = PTR_ERR(selinuxfs_mount);
1920 selinuxfs_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Eric Parisb77a4932010-11-23 11:40:08 -05001922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return err;
1924}
1925
1926__initcall(init_sel_fs);
1927
1928#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1929void exit_sel_fs(void)
1930{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001931 sysfs_remove_mount_point(fs_kobj, "selinux");
Tim Chen423e0ab2011-07-19 09:32:38 -07001932 kern_unmount(selinuxfs_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 unregister_filesystem(&sel_fs_type);
1934}
1935#endif