blob: c9e92daedee2c72c0860d8c0ff2cfcfc7b16a38d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Updated: Karl MacMillan <kmacmillan@tresys.com>
2 *
3 * Added conditional policy language extensions
4 *
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/pagemap.h>
14#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/fs.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/string.h>
20#include <linux/security.h>
21#include <linux/major.h>
22#include <linux/seq_file.h>
23#include <linux/percpu.h>
Steve Grubbaf601e42006-01-04 14:08:39 +000024#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/semaphore.h>
27
28/* selinuxfs pseudo filesystem for exporting the security policy API.
29 Based on the proc code and the fs/nfsd/nfsctl.c code. */
30
31#include "flask.h"
32#include "avc.h"
33#include "avc_ss.h"
34#include "security.h"
35#include "objsec.h"
36#include "conditional.h"
37
38unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
39
James Morris4e5ab4c2006-06-09 00:33:33 -070040#ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
41#define SELINUX_COMPAT_NET_VALUE 0
42#else
43#define SELINUX_COMPAT_NET_VALUE 1
44#endif
45
46int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int __init checkreqprot_setup(char *str)
49{
50 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
51 return 1;
52}
53__setup("checkreqprot=", checkreqprot_setup);
54
James Morris4e5ab4c2006-06-09 00:33:33 -070055static int __init selinux_compat_net_setup(char *str)
56{
57 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
58 return 1;
59}
60__setup("selinux_compat_net=", selinux_compat_net_setup);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnarbb003072006-03-22 00:09:14 -080063static DEFINE_MUTEX(sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* global data for booleans */
66static struct dentry *bool_dir = NULL;
67static int bool_num = 0;
68static int *bool_pending_values = NULL;
69
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -040070/* global data for classes */
71static struct dentry *class_dir = NULL;
72static unsigned long last_class_ino;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074extern void selnl_notify_setenforce(int val);
75
76/* Check whether a task is allowed to use a security operation. */
77static int task_has_security(struct task_struct *tsk,
78 u32 perms)
79{
80 struct task_security_struct *tsec;
81
82 tsec = tsk->security;
83 if (!tsec)
84 return -EACCES;
85
86 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
87 SECCLASS_SECURITY, perms, NULL);
88}
89
90enum sel_inos {
91 SEL_ROOT_INO = 2,
92 SEL_LOAD, /* load policy */
93 SEL_ENFORCE, /* get or set enforcing status */
94 SEL_CONTEXT, /* validate context */
95 SEL_ACCESS, /* compute access decision */
96 SEL_CREATE, /* compute create labeling decision */
97 SEL_RELABEL, /* compute relabeling decision */
98 SEL_USER, /* compute reachable user contexts */
99 SEL_POLICYVERS, /* return policy version for this kernel */
100 SEL_COMMIT_BOOLS, /* commit new boolean values */
101 SEL_MLS, /* return if MLS policy is enabled */
102 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 SEL_MEMBER, /* compute polyinstantiation membership decision */
104 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700105 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
James Carter6174eaf2007-04-04 16:18:39 -0400106 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
James Carter6174eaf2007-04-04 16:18:39 -0400109static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
110
James Carterf0ee2e42007-04-04 10:11:29 -0400111#define SEL_INITCON_INO_OFFSET 0x01000000
James Carterbce34bc2007-04-04 16:18:50 -0400112#define SEL_BOOL_INO_OFFSET 0x02000000
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400113#define SEL_CLASS_INO_OFFSET 0x04000000
James Carterf0ee2e42007-04-04 10:11:29 -0400114#define SEL_INO_MASK 0x00ffffff
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define TMPBUFLEN 12
117static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
118 size_t count, loff_t *ppos)
119{
120 char tmpbuf[TMPBUFLEN];
121 ssize_t length;
122
123 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
124 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
125}
126
127#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
128static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
129 size_t count, loff_t *ppos)
130
131{
132 char *page;
133 ssize_t length;
134 int new_value;
135
Davi Arnautbfd51622005-10-30 14:59:24 -0800136 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -ENOMEM;
138 if (*ppos != 0) {
139 /* No partial writes. */
140 return -EINVAL;
141 }
142 page = (char*)get_zeroed_page(GFP_KERNEL);
143 if (!page)
144 return -ENOMEM;
145 length = -EFAULT;
146 if (copy_from_user(page, buf, count))
147 goto out;
148
149 length = -EINVAL;
150 if (sscanf(page, "%d", &new_value) != 1)
151 goto out;
152
153 if (new_value != selinux_enforcing) {
154 length = task_has_security(current, SECURITY__SETENFORCE);
155 if (length)
156 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000157 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
158 "enforcing=%d old_enforcing=%d auid=%u", new_value,
159 selinux_enforcing,
160 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 selinux_enforcing = new_value;
162 if (selinux_enforcing)
163 avc_ss_reset(0);
164 selnl_notify_setenforce(selinux_enforcing);
165 }
166 length = count;
167out:
168 free_page((unsigned long) page);
169 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,
178};
179
180#ifdef CONFIG_SECURITY_SELINUX_DISABLE
181static ssize_t sel_write_disable(struct file * file, const char __user * buf,
182 size_t count, loff_t *ppos)
183
184{
185 char *page;
186 ssize_t length;
187 int new_value;
188 extern int selinux_disable(void);
189
Davi Arnautbfd51622005-10-30 14:59:24 -0800190 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -ENOMEM;
192 if (*ppos != 0) {
193 /* No partial writes. */
194 return -EINVAL;
195 }
196 page = (char*)get_zeroed_page(GFP_KERNEL);
197 if (!page)
198 return -ENOMEM;
199 length = -EFAULT;
200 if (copy_from_user(page, buf, count))
201 goto out;
202
203 length = -EINVAL;
204 if (sscanf(page, "%d", &new_value) != 1)
205 goto out;
206
207 if (new_value) {
208 length = selinux_disable();
209 if (length < 0)
210 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000211 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
212 "selinux=0 auid=%u",
213 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 length = count;
217out:
218 free_page((unsigned long) page);
219 return length;
220}
221#else
222#define sel_write_disable NULL
223#endif
224
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800225static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 .write = sel_write_disable,
227};
228
229static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
230 size_t count, loff_t *ppos)
231{
232 char tmpbuf[TMPBUFLEN];
233 ssize_t length;
234
235 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
236 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
237}
238
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800239static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .read = sel_read_policyvers,
241};
242
243/* declaration for sel_write_load */
244static int sel_make_bools(void);
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400245static int sel_make_classes(void);
246
247/* declaration for sel_make_class_dirs */
248static int sel_make_dir(struct inode *dir, struct dentry *dentry,
249 unsigned long *ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251static ssize_t sel_read_mls(struct file *filp, char __user *buf,
252 size_t count, loff_t *ppos)
253{
254 char tmpbuf[TMPBUFLEN];
255 ssize_t length;
256
257 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
258 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
259}
260
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800261static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .read = sel_read_mls,
263};
264
265static ssize_t sel_write_load(struct file * file, const char __user * buf,
266 size_t count, loff_t *ppos)
267
268{
269 int ret;
270 ssize_t length;
271 void *data = NULL;
272
Ingo Molnarbb003072006-03-22 00:09:14 -0800273 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 length = task_has_security(current, SECURITY__LOAD_POLICY);
276 if (length)
277 goto out;
278
279 if (*ppos != 0) {
280 /* No partial writes. */
281 length = -EINVAL;
282 goto out;
283 }
284
Davi Arnautbfd51622005-10-30 14:59:24 -0800285 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 || (data = vmalloc(count)) == NULL) {
287 length = -ENOMEM;
288 goto out;
289 }
290
291 length = -EFAULT;
292 if (copy_from_user(data, buf, count) != 0)
293 goto out;
294
295 length = security_load_policy(data, count);
296 if (length)
297 goto out;
298
299 ret = sel_make_bools();
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400300 if (ret) {
301 length = ret;
302 goto out1;
303 }
304
305 ret = sel_make_classes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (ret)
307 length = ret;
308 else
309 length = count;
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -0400310
311out1:
Steve Grubbaf601e42006-01-04 14:08:39 +0000312 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
313 "policy loaded auid=%u",
314 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800316 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 vfree(data);
318 return length;
319}
320
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800321static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 .write = sel_write_load,
323};
324
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800325static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800327 char *canon;
328 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ssize_t length;
330
331 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
332 if (length)
333 return length;
334
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800335 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800337 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800339 length = security_sid_to_context(sid, &canon, &len);
340 if (length < 0)
341 return length;
342
343 if (len > SIMPLE_TRANSACTION_LIMIT) {
344 printk(KERN_ERR "%s: context size (%u) exceeds payload "
345 "max\n", __FUNCTION__, len);
346 length = -ERANGE;
347 goto out;
348 }
349
350 memcpy(buf, canon, len);
351 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800353 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return length;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
358 size_t count, loff_t *ppos)
359{
360 char tmpbuf[TMPBUFLEN];
361 ssize_t length;
362
363 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
364 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
365}
366
367static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
368 size_t count, loff_t *ppos)
369{
370 char *page;
371 ssize_t length;
372 unsigned int new_value;
373
374 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
375 if (length)
376 return length;
377
Davi Arnautbfd51622005-10-30 14:59:24 -0800378 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOMEM;
380 if (*ppos != 0) {
381 /* No partial writes. */
382 return -EINVAL;
383 }
384 page = (char*)get_zeroed_page(GFP_KERNEL);
385 if (!page)
386 return -ENOMEM;
387 length = -EFAULT;
388 if (copy_from_user(page, buf, count))
389 goto out;
390
391 length = -EINVAL;
392 if (sscanf(page, "%u", &new_value) != 1)
393 goto out;
394
395 selinux_checkreqprot = new_value ? 1 : 0;
396 length = count;
397out:
398 free_page((unsigned long) page);
399 return length;
400}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800401static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .read = sel_read_checkreqprot,
403 .write = sel_write_checkreqprot,
404};
405
James Morris4e5ab4c2006-06-09 00:33:33 -0700406static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
407 size_t count, loff_t *ppos)
408{
409 char tmpbuf[TMPBUFLEN];
410 ssize_t length;
411
412 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
413 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
414}
415
416static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
417 size_t count, loff_t *ppos)
418{
419 char *page;
420 ssize_t length;
421 int new_value;
422
423 length = task_has_security(current, SECURITY__LOAD_POLICY);
424 if (length)
425 return length;
426
427 if (count >= PAGE_SIZE)
428 return -ENOMEM;
429 if (*ppos != 0) {
430 /* No partial writes. */
431 return -EINVAL;
432 }
433 page = (char*)get_zeroed_page(GFP_KERNEL);
434 if (!page)
435 return -ENOMEM;
436 length = -EFAULT;
437 if (copy_from_user(page, buf, count))
438 goto out;
439
440 length = -EINVAL;
441 if (sscanf(page, "%d", &new_value) != 1)
442 goto out;
443
444 selinux_compat_net = new_value ? 1 : 0;
445 length = count;
446out:
447 free_page((unsigned long) page);
448 return length;
449}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800450static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700451 .read = sel_read_compat_net,
452 .write = sel_write_compat_net,
453};
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
457 */
458static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
459static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
460static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
461static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
462static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
463
464static ssize_t (*write_op[])(struct file *, char *, size_t) = {
465 [SEL_ACCESS] = sel_write_access,
466 [SEL_CREATE] = sel_write_create,
467 [SEL_RELABEL] = sel_write_relabel,
468 [SEL_USER] = sel_write_user,
469 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800470 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
473static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
474{
Josef Sipek3d5ff522006-12-08 02:37:38 -0800475 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 char *data;
477 ssize_t rv;
478
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800479 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EINVAL;
481
482 data = simple_transaction_get(file, buf, size);
483 if (IS_ERR(data))
484 return PTR_ERR(data);
485
486 rv = write_op[ino](file, data, size);
487 if (rv>0) {
488 simple_transaction_set(file, rv);
489 rv = size;
490 }
491 return rv;
492}
493
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800494static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .write = selinux_transaction_write,
496 .read = simple_transaction_read,
497 .release = simple_transaction_release,
498};
499
500/*
501 * payload - write methods
502 * If the method has a response, the response should be put in buf,
503 * and the length returned. Otherwise return 0 or and -error.
504 */
505
506static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
507{
508 char *scon, *tcon;
509 u32 ssid, tsid;
510 u16 tclass;
511 u32 req;
512 struct av_decision avd;
513 ssize_t length;
514
515 length = task_has_security(current, SECURITY__COMPUTE_AV);
516 if (length)
517 return length;
518
519 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800520 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!scon)
522 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
James Morris89d155e2005-10-30 14:59:21 -0800524 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!tcon)
526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 length = -EINVAL;
529 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
530 goto out2;
531
532 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
533 if (length < 0)
534 goto out2;
535 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
536 if (length < 0)
537 goto out2;
538
539 length = security_compute_av(ssid, tsid, tclass, req, &avd);
540 if (length < 0)
541 goto out2;
542
543 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
544 "%x %x %x %x %u",
545 avd.allowed, avd.decided,
546 avd.auditallow, avd.auditdeny,
547 avd.seqno);
548out2:
549 kfree(tcon);
550out:
551 kfree(scon);
552 return length;
553}
554
555static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
556{
557 char *scon, *tcon;
558 u32 ssid, tsid, newsid;
559 u16 tclass;
560 ssize_t length;
561 char *newcon;
562 u32 len;
563
564 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
565 if (length)
566 return length;
567
568 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800569 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!scon)
571 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
James Morris89d155e2005-10-30 14:59:21 -0800573 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!tcon)
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 length = -EINVAL;
578 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
579 goto out2;
580
581 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
582 if (length < 0)
583 goto out2;
584 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
585 if (length < 0)
586 goto out2;
587
588 length = security_transition_sid(ssid, tsid, tclass, &newsid);
589 if (length < 0)
590 goto out2;
591
592 length = security_sid_to_context(newsid, &newcon, &len);
593 if (length < 0)
594 goto out2;
595
596 if (len > SIMPLE_TRANSACTION_LIMIT) {
597 printk(KERN_ERR "%s: context size (%u) exceeds payload "
598 "max\n", __FUNCTION__, len);
599 length = -ERANGE;
600 goto out3;
601 }
602
603 memcpy(buf, newcon, len);
604 length = len;
605out3:
606 kfree(newcon);
607out2:
608 kfree(tcon);
609out:
610 kfree(scon);
611 return length;
612}
613
614static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
615{
616 char *scon, *tcon;
617 u32 ssid, tsid, newsid;
618 u16 tclass;
619 ssize_t length;
620 char *newcon;
621 u32 len;
622
623 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
624 if (length)
625 return length;
626
627 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800628 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!scon)
630 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
James Morris89d155e2005-10-30 14:59:21 -0800632 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!tcon)
634 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 length = -EINVAL;
637 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
638 goto out2;
639
640 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
641 if (length < 0)
642 goto out2;
643 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
644 if (length < 0)
645 goto out2;
646
647 length = security_change_sid(ssid, tsid, tclass, &newsid);
648 if (length < 0)
649 goto out2;
650
651 length = security_sid_to_context(newsid, &newcon, &len);
652 if (length < 0)
653 goto out2;
654
655 if (len > SIMPLE_TRANSACTION_LIMIT) {
656 length = -ERANGE;
657 goto out3;
658 }
659
660 memcpy(buf, newcon, len);
661 length = len;
662out3:
663 kfree(newcon);
664out2:
665 kfree(tcon);
666out:
667 kfree(scon);
668 return length;
669}
670
671static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
672{
673 char *con, *user, *ptr;
674 u32 sid, *sids;
675 ssize_t length;
676 char *newcon;
677 int i, rc;
678 u32 len, nsids;
679
680 length = task_has_security(current, SECURITY__COMPUTE_USER);
681 if (length)
682 return length;
683
684 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800685 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!con)
687 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
James Morris89d155e2005-10-30 14:59:21 -0800689 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!user)
691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 length = -EINVAL;
694 if (sscanf(buf, "%s %s", con, user) != 2)
695 goto out2;
696
697 length = security_context_to_sid(con, strlen(con)+1, &sid);
698 if (length < 0)
699 goto out2;
700
701 length = security_get_user_sids(sid, user, &sids, &nsids);
702 if (length < 0)
703 goto out2;
704
705 length = sprintf(buf, "%u", nsids) + 1;
706 ptr = buf + length;
707 for (i = 0; i < nsids; i++) {
708 rc = security_sid_to_context(sids[i], &newcon, &len);
709 if (rc) {
710 length = rc;
711 goto out3;
712 }
713 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
714 kfree(newcon);
715 length = -ERANGE;
716 goto out3;
717 }
718 memcpy(ptr, newcon, len);
719 kfree(newcon);
720 ptr += len;
721 length += len;
722 }
723out3:
724 kfree(sids);
725out2:
726 kfree(user);
727out:
728 kfree(con);
729 return length;
730}
731
732static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
733{
734 char *scon, *tcon;
735 u32 ssid, tsid, newsid;
736 u16 tclass;
737 ssize_t length;
738 char *newcon;
739 u32 len;
740
741 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
742 if (length)
743 return length;
744
745 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800746 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!scon)
748 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
James Morris89d155e2005-10-30 14:59:21 -0800750 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!tcon)
752 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 length = -EINVAL;
755 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
756 goto out2;
757
758 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
759 if (length < 0)
760 goto out2;
761 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
762 if (length < 0)
763 goto out2;
764
765 length = security_member_sid(ssid, tsid, tclass, &newsid);
766 if (length < 0)
767 goto out2;
768
769 length = security_sid_to_context(newsid, &newcon, &len);
770 if (length < 0)
771 goto out2;
772
773 if (len > SIMPLE_TRANSACTION_LIMIT) {
774 printk(KERN_ERR "%s: context size (%u) exceeds payload "
775 "max\n", __FUNCTION__, len);
776 length = -ERANGE;
777 goto out3;
778 }
779
780 memcpy(buf, newcon, len);
781 length = len;
782out3:
783 kfree(newcon);
784out2:
785 kfree(tcon);
786out:
787 kfree(scon);
788 return length;
789}
790
791static struct inode *sel_make_inode(struct super_block *sb, int mode)
792{
793 struct inode *ret = new_inode(sb);
794
795 if (ret) {
796 ret->i_mode = mode;
797 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ret->i_blocks = 0;
799 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
800 }
801 return ret;
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static ssize_t sel_read_bool(struct file *filep, char __user *buf,
805 size_t count, loff_t *ppos)
806{
807 char *page = NULL;
808 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 ssize_t ret;
810 int cur_enforcing;
811 struct inode *inode;
812
Ingo Molnarbb003072006-03-22 00:09:14 -0800813 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 ret = -EFAULT;
816
817 /* check to see if this file has been deleted */
818 if (!filep->f_op)
819 goto out;
820
Davi Arnautbfd51622005-10-30 14:59:24 -0800821 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ret = -EINVAL;
823 goto out;
824 }
825 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
826 ret = -ENOMEM;
827 goto out;
828 }
829
Josef Sipek3d5ff522006-12-08 02:37:38 -0800830 inode = filep->f_path.dentry->d_inode;
James Carterbce34bc2007-04-04 16:18:50 -0400831 cur_enforcing = security_get_bool_value(inode->i_ino&SEL_INO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (cur_enforcing < 0) {
833 ret = cur_enforcing;
834 goto out;
835 }
836
837 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
James Carterbce34bc2007-04-04 16:18:50 -0400838 bool_pending_values[inode->i_ino&SEL_INO_MASK]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800839 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800841 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (page)
843 free_page((unsigned long)page);
844 return ret;
845}
846
847static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
848 size_t count, loff_t *ppos)
849{
850 char *page = NULL;
851 ssize_t length = -EFAULT;
852 int new_value;
853 struct inode *inode;
854
Ingo Molnarbb003072006-03-22 00:09:14 -0800855 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 length = task_has_security(current, SECURITY__SETBOOL);
858 if (length)
859 goto out;
860
861 /* check to see if this file has been deleted */
862 if (!filep->f_op)
863 goto out;
864
Davi Arnautbfd51622005-10-30 14:59:24 -0800865 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 length = -ENOMEM;
867 goto out;
868 }
869 if (*ppos != 0) {
870 /* No partial writes. */
871 goto out;
872 }
873 page = (char*)get_zeroed_page(GFP_KERNEL);
874 if (!page) {
875 length = -ENOMEM;
876 goto out;
877 }
878
879 if (copy_from_user(page, buf, count))
880 goto out;
881
882 length = -EINVAL;
883 if (sscanf(page, "%d", &new_value) != 1)
884 goto out;
885
886 if (new_value)
887 new_value = 1;
888
Josef Sipek3d5ff522006-12-08 02:37:38 -0800889 inode = filep->f_path.dentry->d_inode;
James Carterbce34bc2007-04-04 16:18:50 -0400890 bool_pending_values[inode->i_ino&SEL_INO_MASK] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 length = count;
892
893out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800894 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (page)
896 free_page((unsigned long) page);
897 return length;
898}
899
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800900static const struct file_operations sel_bool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .read = sel_read_bool,
902 .write = sel_write_bool,
903};
904
905static ssize_t sel_commit_bools_write(struct file *filep,
906 const char __user *buf,
907 size_t count, loff_t *ppos)
908{
909 char *page = NULL;
910 ssize_t length = -EFAULT;
911 int new_value;
912
Ingo Molnarbb003072006-03-22 00:09:14 -0800913 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 length = task_has_security(current, SECURITY__SETBOOL);
916 if (length)
917 goto out;
918
919 /* check to see if this file has been deleted */
920 if (!filep->f_op)
921 goto out;
922
Davi Arnautbfd51622005-10-30 14:59:24 -0800923 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 length = -ENOMEM;
925 goto out;
926 }
927 if (*ppos != 0) {
928 /* No partial writes. */
929 goto out;
930 }
931 page = (char*)get_zeroed_page(GFP_KERNEL);
932 if (!page) {
933 length = -ENOMEM;
934 goto out;
935 }
936
937 if (copy_from_user(page, buf, count))
938 goto out;
939
940 length = -EINVAL;
941 if (sscanf(page, "%d", &new_value) != 1)
942 goto out;
943
Davi Arnaut20c19e42005-10-23 12:57:16 -0700944 if (new_value && bool_pending_values) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 security_set_bools(bool_num, bool_pending_values);
946 }
947
948 length = count;
949
950out:
Ingo Molnarbb003072006-03-22 00:09:14 -0800951 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (page)
953 free_page((unsigned long) page);
954 return length;
955}
956
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800957static const struct file_operations sel_commit_bools_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .write = sel_commit_bools_write,
959};
960
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -0400961/* partial revoke() from fs/proc/generic.c proc_kill_inodes */
962static void sel_remove_entries(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 struct list_head *p, *node;
965 struct super_block *sb = de->d_sb;
966
967 spin_lock(&dcache_lock);
968 node = de->d_subdirs.next;
969 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800970 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 list_del_init(node);
972
973 if (d->d_inode) {
974 d = dget_locked(d);
975 spin_unlock(&dcache_lock);
976 d_delete(d);
977 simple_unlink(de->d_inode, d);
978 dput(d);
979 spin_lock(&dcache_lock);
980 }
981 node = de->d_subdirs.next;
982 }
983
984 spin_unlock(&dcache_lock);
985
986 file_list_lock();
987 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800988 struct file * filp = list_entry(p, struct file, f_u.fu_list);
Josef Sipek3d5ff522006-12-08 02:37:38 -0800989 struct dentry * dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (dentry->d_parent != de) {
992 continue;
993 }
994 filp->f_op = NULL;
995 }
996 file_list_unlock();
997}
998
999#define BOOL_DIR_NAME "booleans"
1000
1001static int sel_make_bools(void)
1002{
1003 int i, ret = 0;
1004 ssize_t len;
1005 struct dentry *dentry = NULL;
1006 struct dentry *dir = bool_dir;
1007 struct inode *inode = NULL;
1008 struct inode_security_struct *isec;
1009 char **names = NULL, *page;
1010 int num;
1011 int *values = NULL;
1012 u32 sid;
1013
1014 /* remove any existing files */
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001015 kfree(bool_pending_values);
Davi Arnaut20c19e42005-10-23 12:57:16 -07001016 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001018 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1021 return -ENOMEM;
1022
1023 ret = security_get_bools(&num, &names, &values);
1024 if (ret != 0)
1025 goto out;
1026
1027 for (i = 0; i < num; i++) {
1028 dentry = d_alloc_name(dir, names[i]);
1029 if (!dentry) {
1030 ret = -ENOMEM;
1031 goto err;
1032 }
1033 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1034 if (!inode) {
1035 ret = -ENOMEM;
1036 goto err;
1037 }
1038
1039 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1040 if (len < 0) {
1041 ret = -EINVAL;
1042 goto err;
1043 } else if (len >= PAGE_SIZE) {
1044 ret = -ENAMETOOLONG;
1045 goto err;
1046 }
1047 isec = (struct inode_security_struct*)inode->i_security;
1048 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1049 goto err;
1050 isec->sid = sid;
1051 isec->initialized = 1;
1052 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001053 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 d_add(dentry, inode);
1055 }
1056 bool_num = num;
1057 bool_pending_values = values;
1058out:
1059 free_page((unsigned long)page);
1060 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001061 for (i = 0; i < num; i++)
1062 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 kfree(names);
1064 }
1065 return ret;
1066err:
Davi Arnaut20c19e42005-10-23 12:57:16 -07001067 kfree(values);
Christopher J. PeBenito0c92d7c2007-05-23 09:12:07 -04001068 sel_remove_entries(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ret = -ENOMEM;
1070 goto out;
1071}
1072
1073#define NULL_FILE_NAME "null"
1074
1075struct dentry *selinux_null = NULL;
1076
1077static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1078 size_t count, loff_t *ppos)
1079{
1080 char tmpbuf[TMPBUFLEN];
1081 ssize_t length;
1082
1083 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1084 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1085}
1086
1087static ssize_t sel_write_avc_cache_threshold(struct file * file,
1088 const char __user * buf,
1089 size_t count, loff_t *ppos)
1090
1091{
1092 char *page;
1093 ssize_t ret;
1094 int new_value;
1095
Davi Arnautbfd51622005-10-30 14:59:24 -08001096 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 ret = -ENOMEM;
1098 goto out;
1099 }
1100
1101 if (*ppos != 0) {
1102 /* No partial writes. */
1103 ret = -EINVAL;
1104 goto out;
1105 }
1106
1107 page = (char*)get_zeroed_page(GFP_KERNEL);
1108 if (!page) {
1109 ret = -ENOMEM;
1110 goto out;
1111 }
1112
1113 if (copy_from_user(page, buf, count)) {
1114 ret = -EFAULT;
1115 goto out_free;
1116 }
1117
1118 if (sscanf(page, "%u", &new_value) != 1) {
1119 ret = -EINVAL;
1120 goto out;
1121 }
1122
1123 if (new_value != avc_cache_threshold) {
1124 ret = task_has_security(current, SECURITY__SETSECPARAM);
1125 if (ret)
1126 goto out_free;
1127 avc_cache_threshold = new_value;
1128 }
1129 ret = count;
1130out_free:
1131 free_page((unsigned long)page);
1132out:
1133 return ret;
1134}
1135
1136static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1137 size_t count, loff_t *ppos)
1138{
1139 char *page;
1140 ssize_t ret = 0;
1141
1142 page = (char *)__get_free_page(GFP_KERNEL);
1143 if (!page) {
1144 ret = -ENOMEM;
1145 goto out;
1146 }
1147 ret = avc_get_hash_stats(page);
1148 if (ret >= 0)
1149 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1150 free_page((unsigned long)page);
1151out:
1152 return ret;
1153}
1154
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001155static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 .read = sel_read_avc_cache_threshold,
1157 .write = sel_write_avc_cache_threshold,
1158};
1159
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001160static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 .read = sel_read_avc_hash_stats,
1162};
1163
1164#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1165static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1166{
1167 int cpu;
1168
1169 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1170 if (!cpu_possible(cpu))
1171 continue;
1172 *idx = cpu + 1;
1173 return &per_cpu(avc_cache_stats, cpu);
1174 }
1175 return NULL;
1176}
1177
1178static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1179{
1180 loff_t n = *pos - 1;
1181
1182 if (*pos == 0)
1183 return SEQ_START_TOKEN;
1184
1185 return sel_avc_get_stat_idx(&n);
1186}
1187
1188static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1189{
1190 return sel_avc_get_stat_idx(pos);
1191}
1192
1193static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1194{
1195 struct avc_cache_stats *st = v;
1196
1197 if (v == SEQ_START_TOKEN)
1198 seq_printf(seq, "lookups hits misses allocations reclaims "
1199 "frees\n");
1200 else
1201 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1202 st->hits, st->misses, st->allocations,
1203 st->reclaims, st->frees);
1204 return 0;
1205}
1206
1207static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1208{ }
1209
1210static struct seq_operations sel_avc_cache_stats_seq_ops = {
1211 .start = sel_avc_stats_seq_start,
1212 .next = sel_avc_stats_seq_next,
1213 .show = sel_avc_stats_seq_show,
1214 .stop = sel_avc_stats_seq_stop,
1215};
1216
1217static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1218{
1219 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1220}
1221
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001222static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .open = sel_open_avc_cache_stats,
1224 .read = seq_read,
1225 .llseek = seq_lseek,
1226 .release = seq_release,
1227};
1228#endif
1229
1230static int sel_make_avc_files(struct dentry *dir)
1231{
1232 int i, ret = 0;
1233 static struct tree_descr files[] = {
1234 { "cache_threshold",
1235 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1236 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1237#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1238 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1239#endif
1240 };
1241
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001242 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct inode *inode;
1244 struct dentry *dentry;
1245
1246 dentry = d_alloc_name(dir, files[i].name);
1247 if (!dentry) {
1248 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
1252 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1253 if (!inode) {
1254 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001255 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001258 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 d_add(dentry, inode);
1260 }
1261out:
1262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
James Carterf0ee2e42007-04-04 10:11:29 -04001265static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1266 size_t count, loff_t *ppos)
1267{
1268 struct inode *inode;
1269 char *con;
1270 u32 sid, len;
1271 ssize_t ret;
1272
1273 inode = file->f_path.dentry->d_inode;
1274 sid = inode->i_ino&SEL_INO_MASK;
1275 ret = security_sid_to_context(sid, &con, &len);
1276 if (ret < 0)
1277 return ret;
1278
1279 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1280 kfree(con);
1281 return ret;
1282}
1283
1284static const struct file_operations sel_initcon_ops = {
1285 .read = sel_read_initcon,
1286};
1287
1288static int sel_make_initcon_files(struct dentry *dir)
1289{
1290 int i, ret = 0;
1291
1292 for (i = 1; i <= SECINITSID_NUM; i++) {
1293 struct inode *inode;
1294 struct dentry *dentry;
1295 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1296 if (!dentry) {
1297 ret = -ENOMEM;
1298 goto out;
1299 }
1300
1301 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1302 if (!inode) {
1303 ret = -ENOMEM;
1304 goto out;
1305 }
1306 inode->i_fop = &sel_initcon_ops;
1307 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1308 d_add(dentry, inode);
1309 }
1310out:
1311 return ret;
1312}
1313
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001314static inline unsigned int sel_div(unsigned long a, unsigned long b)
1315{
1316 return a / b - (a % b < 0);
1317}
1318
1319static inline unsigned long sel_class_to_ino(u16 class)
1320{
1321 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1322}
1323
1324static inline u16 sel_ino_to_class(unsigned long ino)
1325{
1326 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1327}
1328
1329static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1330{
1331 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1332}
1333
1334static inline u32 sel_ino_to_perm(unsigned long ino)
1335{
1336 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1337}
1338
1339static ssize_t sel_read_class(struct file * file, char __user *buf,
1340 size_t count, loff_t *ppos)
1341{
1342 ssize_t rc, len;
1343 char *page;
1344 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1345
1346 page = (char *)__get_free_page(GFP_KERNEL);
1347 if (!page) {
1348 rc = -ENOMEM;
1349 goto out;
1350 }
1351
1352 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1353 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1354 free_page((unsigned long)page);
1355out:
1356 return rc;
1357}
1358
1359static const struct file_operations sel_class_ops = {
1360 .read = sel_read_class,
1361};
1362
1363static ssize_t sel_read_perm(struct file * file, char __user *buf,
1364 size_t count, loff_t *ppos)
1365{
1366 ssize_t rc, len;
1367 char *page;
1368 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1369
1370 page = (char *)__get_free_page(GFP_KERNEL);
1371 if (!page) {
1372 rc = -ENOMEM;
1373 goto out;
1374 }
1375
1376 len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
1377 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1378 free_page((unsigned long)page);
1379out:
1380 return rc;
1381}
1382
1383static const struct file_operations sel_perm_ops = {
1384 .read = sel_read_perm,
1385};
1386
1387static int sel_make_perm_files(char *objclass, int classvalue,
1388 struct dentry *dir)
1389{
1390 int i, rc = 0, nperms;
1391 char **perms;
1392
1393 rc = security_get_permissions(objclass, &perms, &nperms);
1394 if (rc)
1395 goto out;
1396
1397 for (i = 0; i < nperms; i++) {
1398 struct inode *inode;
1399 struct dentry *dentry;
1400
1401 dentry = d_alloc_name(dir, perms[i]);
1402 if (!dentry) {
1403 rc = -ENOMEM;
1404 goto out1;
1405 }
1406
1407 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1408 if (!inode) {
1409 rc = -ENOMEM;
1410 goto out1;
1411 }
1412 inode->i_fop = &sel_perm_ops;
1413 /* i+1 since perm values are 1-indexed */
1414 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1415 d_add(dentry, inode);
1416 }
1417
1418out1:
1419 for (i = 0; i < nperms; i++)
1420 kfree(perms[i]);
1421 kfree(perms);
1422out:
1423 return rc;
1424}
1425
1426static int sel_make_class_dir_entries(char *classname, int index,
1427 struct dentry *dir)
1428{
1429 struct dentry *dentry = NULL;
1430 struct inode *inode = NULL;
1431 int rc;
1432
1433 dentry = d_alloc_name(dir, "index");
1434 if (!dentry) {
1435 rc = -ENOMEM;
1436 goto out;
1437 }
1438
1439 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1440 if (!inode) {
1441 rc = -ENOMEM;
1442 goto out;
1443 }
1444
1445 inode->i_fop = &sel_class_ops;
1446 inode->i_ino = sel_class_to_ino(index);
1447 d_add(dentry, inode);
1448
1449 dentry = d_alloc_name(dir, "perms");
1450 if (!dentry) {
1451 rc = -ENOMEM;
1452 goto out;
1453 }
1454
1455 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1456 if (rc)
1457 goto out;
1458
1459 rc = sel_make_perm_files(classname, index, dentry);
1460
1461out:
1462 return rc;
1463}
1464
1465static void sel_remove_classes(void)
1466{
1467 struct list_head *class_node;
1468
1469 list_for_each(class_node, &class_dir->d_subdirs) {
1470 struct dentry *class_subdir = list_entry(class_node,
1471 struct dentry, d_u.d_child);
1472 struct list_head *class_subdir_node;
1473
1474 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1475 struct dentry *d = list_entry(class_subdir_node,
1476 struct dentry, d_u.d_child);
1477
1478 if (d->d_inode)
1479 if (d->d_inode->i_mode & S_IFDIR)
1480 sel_remove_entries(d);
1481 }
1482
1483 sel_remove_entries(class_subdir);
1484 }
1485
1486 sel_remove_entries(class_dir);
1487}
1488
1489static int sel_make_classes(void)
1490{
1491 int rc = 0, nclasses, i;
1492 char **classes;
1493
1494 /* delete any existing entries */
1495 sel_remove_classes();
1496
1497 rc = security_get_classes(&classes, &nclasses);
1498 if (rc < 0)
1499 goto out;
1500
1501 /* +2 since classes are 1-indexed */
1502 last_class_ino = sel_class_to_ino(nclasses+2);
1503
1504 for (i = 0; i < nclasses; i++) {
1505 struct dentry *class_name_dir;
1506
1507 class_name_dir = d_alloc_name(class_dir, classes[i]);
1508 if (!class_name_dir) {
1509 rc = -ENOMEM;
1510 goto out1;
1511 }
1512
1513 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1514 &last_class_ino);
1515 if (rc)
1516 goto out1;
1517
1518 /* i+1 since class values are 1-indexed */
1519 rc = sel_make_class_dir_entries(classes[i], i+1,
1520 class_name_dir);
1521 if (rc)
1522 goto out1;
1523 }
1524
1525out1:
1526 for (i = 0; i < nclasses; i++)
1527 kfree(classes[i]);
1528 kfree(classes);
1529out:
1530 return rc;
1531}
1532
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001533static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1534 unsigned long *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 int ret = 0;
1537 struct inode *inode;
1538
James Morrisedb20fb2006-03-22 00:09:20 -08001539 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (!inode) {
1541 ret = -ENOMEM;
1542 goto out;
1543 }
1544 inode->i_op = &simple_dir_inode_operations;
1545 inode->i_fop = &simple_dir_operations;
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001546 inode->i_ino = ++(*ino);
James Morris40e906f2006-03-22 00:09:16 -08001547 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001548 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001550 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001551 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552out:
1553 return ret;
1554}
1555
1556static int sel_fill_super(struct super_block * sb, void * data, int silent)
1557{
1558 int ret;
1559 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001560 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 struct inode_security_struct *isec;
1562
1563 static struct tree_descr selinux_files[] = {
1564 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1565 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001566 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1568 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1569 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1570 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1571 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1572 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1573 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1574 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1575 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1576 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001577 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* last one */ {""}
1579 };
1580 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1581 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001582 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
James Morrisedb20fb2006-03-22 00:09:20 -08001584 root_inode = sb->s_root->d_inode;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001587 if (!dentry) {
1588 ret = -ENOMEM;
1589 goto err;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001592 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Morriscde174a2006-03-22 00:09:17 -08001593 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001594 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001599 if (!dentry) {
1600 ret = -ENOMEM;
1601 goto err;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001605 if (!inode) {
1606 ret = -ENOMEM;
1607 goto err;
1608 }
James Carter6174eaf2007-04-04 16:18:39 -04001609 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 isec = (struct inode_security_struct*)inode->i_security;
1611 isec->sid = SECINITSID_DEVNULL;
1612 isec->sclass = SECCLASS_CHR_FILE;
1613 isec->initialized = 1;
1614
1615 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1616 d_add(dentry, inode);
1617 selinux_null = dentry;
1618
1619 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001620 if (!dentry) {
1621 ret = -ENOMEM;
1622 goto err;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001625 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001627 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 ret = sel_make_avc_files(dentry);
1630 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001631 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001632
1633 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1634 if (!dentry) {
1635 ret = -ENOMEM;
1636 goto err;
1637 }
1638
Christopher J. PeBenito0dd4ae52007-05-23 09:12:08 -04001639 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
James Carterf0ee2e42007-04-04 10:11:29 -04001640 if (ret)
1641 goto err;
1642
1643 ret = sel_make_initcon_files(dentry);
1644 if (ret)
1645 goto err;
1646
Christopher J. PeBenitoe47c8fc2007-05-23 09:12:09 -04001647 dentry = d_alloc_name(sb->s_root, "class");
1648 if (!dentry) {
1649 ret = -ENOMEM;
1650 goto err;
1651 }
1652
1653 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1654 if (ret)
1655 goto err;
1656
1657 class_dir = dentry;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659out:
James Morris161ce452006-03-22 00:09:17 -08001660 return ret;
1661err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
James Morris161ce452006-03-22 00:09:17 -08001663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
David Howells454e2392006-06-23 02:02:57 -07001666static int sel_get_sb(struct file_system_type *fs_type,
1667 int flags, const char *dev_name, void *data,
1668 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
David Howells454e2392006-06-23 02:02:57 -07001670 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
1673static struct file_system_type sel_fs_type = {
1674 .name = "selinuxfs",
1675 .get_sb = sel_get_sb,
1676 .kill_sb = kill_litter_super,
1677};
1678
1679struct vfsmount *selinuxfs_mount;
1680
1681static int __init init_sel_fs(void)
1682{
1683 int err;
1684
1685 if (!selinux_enabled)
1686 return 0;
1687 err = register_filesystem(&sel_fs_type);
1688 if (!err) {
1689 selinuxfs_mount = kern_mount(&sel_fs_type);
1690 if (IS_ERR(selinuxfs_mount)) {
1691 printk(KERN_ERR "selinuxfs: could not mount!\n");
1692 err = PTR_ERR(selinuxfs_mount);
1693 selinuxfs_mount = NULL;
1694 }
1695 }
1696 return err;
1697}
1698
1699__initcall(init_sel_fs);
1700
1701#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1702void exit_sel_fs(void)
1703{
1704 unregister_filesystem(&sel_fs_type);
1705}
1706#endif