blob: aca099aa2ed3d743e65d38a4c77da690e4609e2f [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 Molnarbb0030792006-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 Molnarbb0030792006-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
70extern void selnl_notify_setenforce(int val);
71
72/* Check whether a task is allowed to use a security operation. */
73static int task_has_security(struct task_struct *tsk,
74 u32 perms)
75{
76 struct task_security_struct *tsec;
77
78 tsec = tsk->security;
79 if (!tsec)
80 return -EACCES;
81
82 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
83 SECCLASS_SECURITY, perms, NULL);
84}
85
86enum sel_inos {
87 SEL_ROOT_INO = 2,
88 SEL_LOAD, /* load policy */
89 SEL_ENFORCE, /* get or set enforcing status */
90 SEL_CONTEXT, /* validate context */
91 SEL_ACCESS, /* compute access decision */
92 SEL_CREATE, /* compute create labeling decision */
93 SEL_RELABEL, /* compute relabeling decision */
94 SEL_USER, /* compute reachable user contexts */
95 SEL_POLICYVERS, /* return policy version for this kernel */
96 SEL_COMMIT_BOOLS, /* commit new boolean values */
97 SEL_MLS, /* return if MLS policy is enabled */
98 SEL_DISABLE, /* disable SELinux until next reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 SEL_MEMBER, /* compute polyinstantiation membership decision */
100 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
James Morris4e5ab4c2006-06-09 00:33:33 -0700101 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
James Carter6174eaf2007-04-04 16:18:39 -0400102 SEL_INO_NEXT, /* The next inode number to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
James Carter6174eaf2007-04-04 16:18:39 -0400105static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
106
James Carterf0ee2e42007-04-04 10:11:29 -0400107#define SEL_INITCON_INO_OFFSET 0x01000000
James Carterbce34bc2007-04-04 16:18:50 -0400108#define SEL_BOOL_INO_OFFSET 0x02000000
James Carterf0ee2e42007-04-04 10:11:29 -0400109#define SEL_INO_MASK 0x00ffffff
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define TMPBUFLEN 12
112static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
113 size_t count, loff_t *ppos)
114{
115 char tmpbuf[TMPBUFLEN];
116 ssize_t length;
117
118 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
119 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
120}
121
122#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
123static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
124 size_t count, loff_t *ppos)
125
126{
127 char *page;
128 ssize_t length;
129 int new_value;
130
Davi Arnautbfd51622005-10-30 14:59:24 -0800131 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -ENOMEM;
133 if (*ppos != 0) {
134 /* No partial writes. */
135 return -EINVAL;
136 }
137 page = (char*)get_zeroed_page(GFP_KERNEL);
138 if (!page)
139 return -ENOMEM;
140 length = -EFAULT;
141 if (copy_from_user(page, buf, count))
142 goto out;
143
144 length = -EINVAL;
145 if (sscanf(page, "%d", &new_value) != 1)
146 goto out;
147
148 if (new_value != selinux_enforcing) {
149 length = task_has_security(current, SECURITY__SETENFORCE);
150 if (length)
151 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000152 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
153 "enforcing=%d old_enforcing=%d auid=%u", new_value,
154 selinux_enforcing,
155 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 selinux_enforcing = new_value;
157 if (selinux_enforcing)
158 avc_ss_reset(0);
159 selnl_notify_setenforce(selinux_enforcing);
160 }
161 length = count;
162out:
163 free_page((unsigned long) page);
164 return length;
165}
166#else
167#define sel_write_enforce NULL
168#endif
169
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800170static const struct file_operations sel_enforce_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .read = sel_read_enforce,
172 .write = sel_write_enforce,
173};
174
175#ifdef CONFIG_SECURITY_SELINUX_DISABLE
176static ssize_t sel_write_disable(struct file * file, const char __user * buf,
177 size_t count, loff_t *ppos)
178
179{
180 char *page;
181 ssize_t length;
182 int new_value;
183 extern int selinux_disable(void);
184
Davi Arnautbfd51622005-10-30 14:59:24 -0800185 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENOMEM;
187 if (*ppos != 0) {
188 /* No partial writes. */
189 return -EINVAL;
190 }
191 page = (char*)get_zeroed_page(GFP_KERNEL);
192 if (!page)
193 return -ENOMEM;
194 length = -EFAULT;
195 if (copy_from_user(page, buf, count))
196 goto out;
197
198 length = -EINVAL;
199 if (sscanf(page, "%d", &new_value) != 1)
200 goto out;
201
202 if (new_value) {
203 length = selinux_disable();
204 if (length < 0)
205 goto out;
Steve Grubbaf601e42006-01-04 14:08:39 +0000206 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
207 "selinux=0 auid=%u",
208 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 length = count;
212out:
213 free_page((unsigned long) page);
214 return length;
215}
216#else
217#define sel_write_disable NULL
218#endif
219
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800220static const struct file_operations sel_disable_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .write = sel_write_disable,
222};
223
224static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
225 size_t count, loff_t *ppos)
226{
227 char tmpbuf[TMPBUFLEN];
228 ssize_t length;
229
230 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
231 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
232}
233
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800234static const struct file_operations sel_policyvers_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .read = sel_read_policyvers,
236};
237
238/* declaration for sel_write_load */
239static int sel_make_bools(void);
240
241static ssize_t sel_read_mls(struct file *filp, char __user *buf,
242 size_t count, loff_t *ppos)
243{
244 char tmpbuf[TMPBUFLEN];
245 ssize_t length;
246
247 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
248 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
249}
250
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800251static const struct file_operations sel_mls_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .read = sel_read_mls,
253};
254
255static ssize_t sel_write_load(struct file * file, const char __user * buf,
256 size_t count, loff_t *ppos)
257
258{
259 int ret;
260 ssize_t length;
261 void *data = NULL;
262
Ingo Molnarbb0030792006-03-22 00:09:14 -0800263 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 length = task_has_security(current, SECURITY__LOAD_POLICY);
266 if (length)
267 goto out;
268
269 if (*ppos != 0) {
270 /* No partial writes. */
271 length = -EINVAL;
272 goto out;
273 }
274
Davi Arnautbfd51622005-10-30 14:59:24 -0800275 if ((count > 64 * 1024 * 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 || (data = vmalloc(count)) == NULL) {
277 length = -ENOMEM;
278 goto out;
279 }
280
281 length = -EFAULT;
282 if (copy_from_user(data, buf, count) != 0)
283 goto out;
284
285 length = security_load_policy(data, count);
286 if (length)
287 goto out;
288
289 ret = sel_make_bools();
290 if (ret)
291 length = ret;
292 else
293 length = count;
Steve Grubbaf601e42006-01-04 14:08:39 +0000294 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
295 "policy loaded auid=%u",
296 audit_get_loginuid(current->audit_context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800298 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 vfree(data);
300 return length;
301}
302
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800303static const struct file_operations sel_load_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .write = sel_write_load,
305};
306
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800307static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800309 char *canon;
310 u32 sid, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ssize_t length;
312
313 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
314 if (length)
315 return length;
316
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800317 length = security_context_to_sid(buf, size, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (length < 0)
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800319 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800321 length = security_sid_to_context(sid, &canon, &len);
322 if (length < 0)
323 return length;
324
325 if (len > SIMPLE_TRANSACTION_LIMIT) {
326 printk(KERN_ERR "%s: context size (%u) exceeds payload "
327 "max\n", __FUNCTION__, len);
328 length = -ERANGE;
329 goto out;
330 }
331
332 memcpy(buf, canon, len);
333 length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334out:
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800335 kfree(canon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return length;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
340 size_t count, loff_t *ppos)
341{
342 char tmpbuf[TMPBUFLEN];
343 ssize_t length;
344
345 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
346 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
347}
348
349static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
350 size_t count, loff_t *ppos)
351{
352 char *page;
353 ssize_t length;
354 unsigned int new_value;
355
356 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
357 if (length)
358 return length;
359
Davi Arnautbfd51622005-10-30 14:59:24 -0800360 if (count >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -ENOMEM;
362 if (*ppos != 0) {
363 /* No partial writes. */
364 return -EINVAL;
365 }
366 page = (char*)get_zeroed_page(GFP_KERNEL);
367 if (!page)
368 return -ENOMEM;
369 length = -EFAULT;
370 if (copy_from_user(page, buf, count))
371 goto out;
372
373 length = -EINVAL;
374 if (sscanf(page, "%u", &new_value) != 1)
375 goto out;
376
377 selinux_checkreqprot = new_value ? 1 : 0;
378 length = count;
379out:
380 free_page((unsigned long) page);
381 return length;
382}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800383static const struct file_operations sel_checkreqprot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .read = sel_read_checkreqprot,
385 .write = sel_write_checkreqprot,
386};
387
James Morris4e5ab4c2006-06-09 00:33:33 -0700388static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
389 size_t count, loff_t *ppos)
390{
391 char tmpbuf[TMPBUFLEN];
392 ssize_t length;
393
394 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
395 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
396}
397
398static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
399 size_t count, loff_t *ppos)
400{
401 char *page;
402 ssize_t length;
403 int new_value;
404
405 length = task_has_security(current, SECURITY__LOAD_POLICY);
406 if (length)
407 return length;
408
409 if (count >= PAGE_SIZE)
410 return -ENOMEM;
411 if (*ppos != 0) {
412 /* No partial writes. */
413 return -EINVAL;
414 }
415 page = (char*)get_zeroed_page(GFP_KERNEL);
416 if (!page)
417 return -ENOMEM;
418 length = -EFAULT;
419 if (copy_from_user(page, buf, count))
420 goto out;
421
422 length = -EINVAL;
423 if (sscanf(page, "%d", &new_value) != 1)
424 goto out;
425
426 selinux_compat_net = new_value ? 1 : 0;
427 length = count;
428out:
429 free_page((unsigned long) page);
430 return length;
431}
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800432static const struct file_operations sel_compat_net_ops = {
James Morris4e5ab4c2006-06-09 00:33:33 -0700433 .read = sel_read_compat_net,
434 .write = sel_write_compat_net,
435};
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/*
438 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
439 */
440static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
441static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
442static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
443static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
444static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
445
446static ssize_t (*write_op[])(struct file *, char *, size_t) = {
447 [SEL_ACCESS] = sel_write_access,
448 [SEL_CREATE] = sel_write_create,
449 [SEL_RELABEL] = sel_write_relabel,
450 [SEL_USER] = sel_write_user,
451 [SEL_MEMBER] = sel_write_member,
Stephen Smalleyce9982d2005-11-08 21:34:33 -0800452 [SEL_CONTEXT] = sel_write_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453};
454
455static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
456{
Josef Sipek3d5ff522006-12-08 02:37:38 -0800457 ino_t ino = file->f_path.dentry->d_inode->i_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 char *data;
459 ssize_t rv;
460
Nicolas Kaiser6e20a642006-01-06 00:11:22 -0800461 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -EINVAL;
463
464 data = simple_transaction_get(file, buf, size);
465 if (IS_ERR(data))
466 return PTR_ERR(data);
467
468 rv = write_op[ino](file, data, size);
469 if (rv>0) {
470 simple_transaction_set(file, rv);
471 rv = size;
472 }
473 return rv;
474}
475
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800476static const struct file_operations transaction_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 .write = selinux_transaction_write,
478 .read = simple_transaction_read,
479 .release = simple_transaction_release,
480};
481
482/*
483 * payload - write methods
484 * If the method has a response, the response should be put in buf,
485 * and the length returned. Otherwise return 0 or and -error.
486 */
487
488static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
489{
490 char *scon, *tcon;
491 u32 ssid, tsid;
492 u16 tclass;
493 u32 req;
494 struct av_decision avd;
495 ssize_t length;
496
497 length = task_has_security(current, SECURITY__COMPUTE_AV);
498 if (length)
499 return length;
500
501 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800502 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!scon)
504 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
James Morris89d155e2005-10-30 14:59:21 -0800506 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!tcon)
508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 length = -EINVAL;
511 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
512 goto out2;
513
514 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
515 if (length < 0)
516 goto out2;
517 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
518 if (length < 0)
519 goto out2;
520
521 length = security_compute_av(ssid, tsid, tclass, req, &avd);
522 if (length < 0)
523 goto out2;
524
525 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
526 "%x %x %x %x %u",
527 avd.allowed, avd.decided,
528 avd.auditallow, avd.auditdeny,
529 avd.seqno);
530out2:
531 kfree(tcon);
532out:
533 kfree(scon);
534 return length;
535}
536
537static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
538{
539 char *scon, *tcon;
540 u32 ssid, tsid, newsid;
541 u16 tclass;
542 ssize_t length;
543 char *newcon;
544 u32 len;
545
546 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
547 if (length)
548 return length;
549
550 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800551 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!scon)
553 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
James Morris89d155e2005-10-30 14:59:21 -0800555 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!tcon)
557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 length = -EINVAL;
560 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
561 goto out2;
562
563 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
564 if (length < 0)
565 goto out2;
566 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
567 if (length < 0)
568 goto out2;
569
570 length = security_transition_sid(ssid, tsid, tclass, &newsid);
571 if (length < 0)
572 goto out2;
573
574 length = security_sid_to_context(newsid, &newcon, &len);
575 if (length < 0)
576 goto out2;
577
578 if (len > SIMPLE_TRANSACTION_LIMIT) {
579 printk(KERN_ERR "%s: context size (%u) exceeds payload "
580 "max\n", __FUNCTION__, len);
581 length = -ERANGE;
582 goto out3;
583 }
584
585 memcpy(buf, newcon, len);
586 length = len;
587out3:
588 kfree(newcon);
589out2:
590 kfree(tcon);
591out:
592 kfree(scon);
593 return length;
594}
595
596static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
597{
598 char *scon, *tcon;
599 u32 ssid, tsid, newsid;
600 u16 tclass;
601 ssize_t length;
602 char *newcon;
603 u32 len;
604
605 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
606 if (length)
607 return length;
608
609 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800610 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!scon)
612 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
James Morris89d155e2005-10-30 14:59:21 -0800614 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!tcon)
616 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 length = -EINVAL;
619 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
620 goto out2;
621
622 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
623 if (length < 0)
624 goto out2;
625 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
626 if (length < 0)
627 goto out2;
628
629 length = security_change_sid(ssid, tsid, tclass, &newsid);
630 if (length < 0)
631 goto out2;
632
633 length = security_sid_to_context(newsid, &newcon, &len);
634 if (length < 0)
635 goto out2;
636
637 if (len > SIMPLE_TRANSACTION_LIMIT) {
638 length = -ERANGE;
639 goto out3;
640 }
641
642 memcpy(buf, newcon, len);
643 length = len;
644out3:
645 kfree(newcon);
646out2:
647 kfree(tcon);
648out:
649 kfree(scon);
650 return length;
651}
652
653static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
654{
655 char *con, *user, *ptr;
656 u32 sid, *sids;
657 ssize_t length;
658 char *newcon;
659 int i, rc;
660 u32 len, nsids;
661
662 length = task_has_security(current, SECURITY__COMPUTE_USER);
663 if (length)
664 return length;
665
666 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800667 con = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!con)
669 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
James Morris89d155e2005-10-30 14:59:21 -0800671 user = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!user)
673 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 length = -EINVAL;
676 if (sscanf(buf, "%s %s", con, user) != 2)
677 goto out2;
678
679 length = security_context_to_sid(con, strlen(con)+1, &sid);
680 if (length < 0)
681 goto out2;
682
683 length = security_get_user_sids(sid, user, &sids, &nsids);
684 if (length < 0)
685 goto out2;
686
687 length = sprintf(buf, "%u", nsids) + 1;
688 ptr = buf + length;
689 for (i = 0; i < nsids; i++) {
690 rc = security_sid_to_context(sids[i], &newcon, &len);
691 if (rc) {
692 length = rc;
693 goto out3;
694 }
695 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
696 kfree(newcon);
697 length = -ERANGE;
698 goto out3;
699 }
700 memcpy(ptr, newcon, len);
701 kfree(newcon);
702 ptr += len;
703 length += len;
704 }
705out3:
706 kfree(sids);
707out2:
708 kfree(user);
709out:
710 kfree(con);
711 return length;
712}
713
714static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
715{
716 char *scon, *tcon;
717 u32 ssid, tsid, newsid;
718 u16 tclass;
719 ssize_t length;
720 char *newcon;
721 u32 len;
722
723 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
724 if (length)
725 return length;
726
727 length = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -0800728 scon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!scon)
730 return length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
James Morris89d155e2005-10-30 14:59:21 -0800732 tcon = kzalloc(size+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!tcon)
734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 length = -EINVAL;
737 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
738 goto out2;
739
740 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
741 if (length < 0)
742 goto out2;
743 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
744 if (length < 0)
745 goto out2;
746
747 length = security_member_sid(ssid, tsid, tclass, &newsid);
748 if (length < 0)
749 goto out2;
750
751 length = security_sid_to_context(newsid, &newcon, &len);
752 if (length < 0)
753 goto out2;
754
755 if (len > SIMPLE_TRANSACTION_LIMIT) {
756 printk(KERN_ERR "%s: context size (%u) exceeds payload "
757 "max\n", __FUNCTION__, len);
758 length = -ERANGE;
759 goto out3;
760 }
761
762 memcpy(buf, newcon, len);
763 length = len;
764out3:
765 kfree(newcon);
766out2:
767 kfree(tcon);
768out:
769 kfree(scon);
770 return length;
771}
772
773static struct inode *sel_make_inode(struct super_block *sb, int mode)
774{
775 struct inode *ret = new_inode(sb);
776
777 if (ret) {
778 ret->i_mode = mode;
779 ret->i_uid = ret->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ret->i_blocks = 0;
781 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
782 }
783 return ret;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static ssize_t sel_read_bool(struct file *filep, char __user *buf,
787 size_t count, loff_t *ppos)
788{
789 char *page = NULL;
790 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ssize_t ret;
792 int cur_enforcing;
793 struct inode *inode;
794
Ingo Molnarbb0030792006-03-22 00:09:14 -0800795 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 ret = -EFAULT;
798
799 /* check to see if this file has been deleted */
800 if (!filep->f_op)
801 goto out;
802
Davi Arnautbfd51622005-10-30 14:59:24 -0800803 if (count > PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ret = -EINVAL;
805 goto out;
806 }
807 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
808 ret = -ENOMEM;
809 goto out;
810 }
811
Josef Sipek3d5ff522006-12-08 02:37:38 -0800812 inode = filep->f_path.dentry->d_inode;
James Carterbce34bc2007-04-04 16:18:50 -0400813 cur_enforcing = security_get_bool_value(inode->i_ino&SEL_INO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (cur_enforcing < 0) {
815 ret = cur_enforcing;
816 goto out;
817 }
818
819 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
James Carterbce34bc2007-04-04 16:18:50 -0400820 bool_pending_values[inode->i_ino&SEL_INO_MASK]);
Stephen Smalley68bdcf22006-03-22 00:09:15 -0800821 ret = simple_read_from_buffer(buf, count, ppos, page, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800823 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (page)
825 free_page((unsigned long)page);
826 return ret;
827}
828
829static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
830 size_t count, loff_t *ppos)
831{
832 char *page = NULL;
833 ssize_t length = -EFAULT;
834 int new_value;
835 struct inode *inode;
836
Ingo Molnarbb0030792006-03-22 00:09:14 -0800837 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 length = task_has_security(current, SECURITY__SETBOOL);
840 if (length)
841 goto out;
842
843 /* check to see if this file has been deleted */
844 if (!filep->f_op)
845 goto out;
846
Davi Arnautbfd51622005-10-30 14:59:24 -0800847 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 length = -ENOMEM;
849 goto out;
850 }
851 if (*ppos != 0) {
852 /* No partial writes. */
853 goto out;
854 }
855 page = (char*)get_zeroed_page(GFP_KERNEL);
856 if (!page) {
857 length = -ENOMEM;
858 goto out;
859 }
860
861 if (copy_from_user(page, buf, count))
862 goto out;
863
864 length = -EINVAL;
865 if (sscanf(page, "%d", &new_value) != 1)
866 goto out;
867
868 if (new_value)
869 new_value = 1;
870
Josef Sipek3d5ff522006-12-08 02:37:38 -0800871 inode = filep->f_path.dentry->d_inode;
James Carterbce34bc2007-04-04 16:18:50 -0400872 bool_pending_values[inode->i_ino&SEL_INO_MASK] = new_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 length = count;
874
875out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800876 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (page)
878 free_page((unsigned long) page);
879 return length;
880}
881
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800882static const struct file_operations sel_bool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .read = sel_read_bool,
884 .write = sel_write_bool,
885};
886
887static ssize_t sel_commit_bools_write(struct file *filep,
888 const char __user *buf,
889 size_t count, loff_t *ppos)
890{
891 char *page = NULL;
892 ssize_t length = -EFAULT;
893 int new_value;
894
Ingo Molnarbb0030792006-03-22 00:09:14 -0800895 mutex_lock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 length = task_has_security(current, SECURITY__SETBOOL);
898 if (length)
899 goto out;
900
901 /* check to see if this file has been deleted */
902 if (!filep->f_op)
903 goto out;
904
Davi Arnautbfd51622005-10-30 14:59:24 -0800905 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 length = -ENOMEM;
907 goto out;
908 }
909 if (*ppos != 0) {
910 /* No partial writes. */
911 goto out;
912 }
913 page = (char*)get_zeroed_page(GFP_KERNEL);
914 if (!page) {
915 length = -ENOMEM;
916 goto out;
917 }
918
919 if (copy_from_user(page, buf, count))
920 goto out;
921
922 length = -EINVAL;
923 if (sscanf(page, "%d", &new_value) != 1)
924 goto out;
925
Davi Arnaut20c19e42005-10-23 12:57:16 -0700926 if (new_value && bool_pending_values) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 security_set_bools(bool_num, bool_pending_values);
928 }
929
930 length = count;
931
932out:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800933 mutex_unlock(&sel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (page)
935 free_page((unsigned long) page);
936 return length;
937}
938
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800939static const struct file_operations sel_commit_bools_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .write = sel_commit_bools_write,
941};
942
943/* delete booleans - partial revoke() from
944 * fs/proc/generic.c proc_kill_inodes */
945static void sel_remove_bools(struct dentry *de)
946{
947 struct list_head *p, *node;
948 struct super_block *sb = de->d_sb;
949
950 spin_lock(&dcache_lock);
951 node = de->d_subdirs.next;
952 while (node != &de->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800953 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 list_del_init(node);
955
956 if (d->d_inode) {
957 d = dget_locked(d);
958 spin_unlock(&dcache_lock);
959 d_delete(d);
960 simple_unlink(de->d_inode, d);
961 dput(d);
962 spin_lock(&dcache_lock);
963 }
964 node = de->d_subdirs.next;
965 }
966
967 spin_unlock(&dcache_lock);
968
969 file_list_lock();
970 list_for_each(p, &sb->s_files) {
Eric Dumazet2f512012005-10-30 15:02:16 -0800971 struct file * filp = list_entry(p, struct file, f_u.fu_list);
Josef Sipek3d5ff522006-12-08 02:37:38 -0800972 struct dentry * dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (dentry->d_parent != de) {
975 continue;
976 }
977 filp->f_op = NULL;
978 }
979 file_list_unlock();
980}
981
982#define BOOL_DIR_NAME "booleans"
983
984static int sel_make_bools(void)
985{
986 int i, ret = 0;
987 ssize_t len;
988 struct dentry *dentry = NULL;
989 struct dentry *dir = bool_dir;
990 struct inode *inode = NULL;
991 struct inode_security_struct *isec;
992 char **names = NULL, *page;
993 int num;
994 int *values = NULL;
995 u32 sid;
996
997 /* remove any existing files */
Jesper Juhl9a5f04b2005-06-25 14:58:51 -0700998 kfree(bool_pending_values);
Davi Arnaut20c19e42005-10-23 12:57:16 -0700999 bool_pending_values = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 sel_remove_bools(dir);
1002
1003 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1004 return -ENOMEM;
1005
1006 ret = security_get_bools(&num, &names, &values);
1007 if (ret != 0)
1008 goto out;
1009
1010 for (i = 0; i < num; i++) {
1011 dentry = d_alloc_name(dir, names[i]);
1012 if (!dentry) {
1013 ret = -ENOMEM;
1014 goto err;
1015 }
1016 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1017 if (!inode) {
1018 ret = -ENOMEM;
1019 goto err;
1020 }
1021
1022 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1023 if (len < 0) {
1024 ret = -EINVAL;
1025 goto err;
1026 } else if (len >= PAGE_SIZE) {
1027 ret = -ENAMETOOLONG;
1028 goto err;
1029 }
1030 isec = (struct inode_security_struct*)inode->i_security;
1031 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1032 goto err;
1033 isec->sid = sid;
1034 isec->initialized = 1;
1035 inode->i_fop = &sel_bool_ops;
James Carterbce34bc2007-04-04 16:18:50 -04001036 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 d_add(dentry, inode);
1038 }
1039 bool_num = num;
1040 bool_pending_values = values;
1041out:
1042 free_page((unsigned long)page);
1043 if (names) {
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001044 for (i = 0; i < num; i++)
1045 kfree(names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree(names);
1047 }
1048 return ret;
1049err:
Davi Arnaut20c19e42005-10-23 12:57:16 -07001050 kfree(values);
James Morris253a8b12006-03-22 00:09:18 -08001051 sel_remove_bools(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ret = -ENOMEM;
1053 goto out;
1054}
1055
1056#define NULL_FILE_NAME "null"
1057
1058struct dentry *selinux_null = NULL;
1059
1060static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1061 size_t count, loff_t *ppos)
1062{
1063 char tmpbuf[TMPBUFLEN];
1064 ssize_t length;
1065
1066 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1067 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1068}
1069
1070static ssize_t sel_write_avc_cache_threshold(struct file * file,
1071 const char __user * buf,
1072 size_t count, loff_t *ppos)
1073
1074{
1075 char *page;
1076 ssize_t ret;
1077 int new_value;
1078
Davi Arnautbfd51622005-10-30 14:59:24 -08001079 if (count >= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ret = -ENOMEM;
1081 goto out;
1082 }
1083
1084 if (*ppos != 0) {
1085 /* No partial writes. */
1086 ret = -EINVAL;
1087 goto out;
1088 }
1089
1090 page = (char*)get_zeroed_page(GFP_KERNEL);
1091 if (!page) {
1092 ret = -ENOMEM;
1093 goto out;
1094 }
1095
1096 if (copy_from_user(page, buf, count)) {
1097 ret = -EFAULT;
1098 goto out_free;
1099 }
1100
1101 if (sscanf(page, "%u", &new_value) != 1) {
1102 ret = -EINVAL;
1103 goto out;
1104 }
1105
1106 if (new_value != avc_cache_threshold) {
1107 ret = task_has_security(current, SECURITY__SETSECPARAM);
1108 if (ret)
1109 goto out_free;
1110 avc_cache_threshold = new_value;
1111 }
1112 ret = count;
1113out_free:
1114 free_page((unsigned long)page);
1115out:
1116 return ret;
1117}
1118
1119static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1120 size_t count, loff_t *ppos)
1121{
1122 char *page;
1123 ssize_t ret = 0;
1124
1125 page = (char *)__get_free_page(GFP_KERNEL);
1126 if (!page) {
1127 ret = -ENOMEM;
1128 goto out;
1129 }
1130 ret = avc_get_hash_stats(page);
1131 if (ret >= 0)
1132 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1133 free_page((unsigned long)page);
1134out:
1135 return ret;
1136}
1137
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001138static const struct file_operations sel_avc_cache_threshold_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 .read = sel_read_avc_cache_threshold,
1140 .write = sel_write_avc_cache_threshold,
1141};
1142
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001143static const struct file_operations sel_avc_hash_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 .read = sel_read_avc_hash_stats,
1145};
1146
1147#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1148static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1149{
1150 int cpu;
1151
1152 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1153 if (!cpu_possible(cpu))
1154 continue;
1155 *idx = cpu + 1;
1156 return &per_cpu(avc_cache_stats, cpu);
1157 }
1158 return NULL;
1159}
1160
1161static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1162{
1163 loff_t n = *pos - 1;
1164
1165 if (*pos == 0)
1166 return SEQ_START_TOKEN;
1167
1168 return sel_avc_get_stat_idx(&n);
1169}
1170
1171static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1172{
1173 return sel_avc_get_stat_idx(pos);
1174}
1175
1176static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1177{
1178 struct avc_cache_stats *st = v;
1179
1180 if (v == SEQ_START_TOKEN)
1181 seq_printf(seq, "lookups hits misses allocations reclaims "
1182 "frees\n");
1183 else
1184 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1185 st->hits, st->misses, st->allocations,
1186 st->reclaims, st->frees);
1187 return 0;
1188}
1189
1190static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1191{ }
1192
1193static struct seq_operations sel_avc_cache_stats_seq_ops = {
1194 .start = sel_avc_stats_seq_start,
1195 .next = sel_avc_stats_seq_next,
1196 .show = sel_avc_stats_seq_show,
1197 .stop = sel_avc_stats_seq_stop,
1198};
1199
1200static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1201{
1202 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1203}
1204
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001205static const struct file_operations sel_avc_cache_stats_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .open = sel_open_avc_cache_stats,
1207 .read = seq_read,
1208 .llseek = seq_lseek,
1209 .release = seq_release,
1210};
1211#endif
1212
1213static int sel_make_avc_files(struct dentry *dir)
1214{
1215 int i, ret = 0;
1216 static struct tree_descr files[] = {
1217 { "cache_threshold",
1218 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1219 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1220#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1221 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1222#endif
1223 };
1224
Nicolas Kaiser6e20a642006-01-06 00:11:22 -08001225 for (i = 0; i < ARRAY_SIZE(files); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct inode *inode;
1227 struct dentry *dentry;
1228
1229 dentry = d_alloc_name(dir, files[i].name);
1230 if (!dentry) {
1231 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1236 if (!inode) {
1237 ret = -ENOMEM;
James Morrisd6aafa62006-03-22 00:09:19 -08001238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 inode->i_fop = files[i].ops;
James Carter6174eaf2007-04-04 16:18:39 -04001241 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 d_add(dentry, inode);
1243 }
1244out:
1245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
James Carterf0ee2e42007-04-04 10:11:29 -04001248static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1249 size_t count, loff_t *ppos)
1250{
1251 struct inode *inode;
1252 char *con;
1253 u32 sid, len;
1254 ssize_t ret;
1255
1256 inode = file->f_path.dentry->d_inode;
1257 sid = inode->i_ino&SEL_INO_MASK;
1258 ret = security_sid_to_context(sid, &con, &len);
1259 if (ret < 0)
1260 return ret;
1261
1262 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1263 kfree(con);
1264 return ret;
1265}
1266
1267static const struct file_operations sel_initcon_ops = {
1268 .read = sel_read_initcon,
1269};
1270
1271static int sel_make_initcon_files(struct dentry *dir)
1272{
1273 int i, ret = 0;
1274
1275 for (i = 1; i <= SECINITSID_NUM; i++) {
1276 struct inode *inode;
1277 struct dentry *dentry;
1278 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1279 if (!dentry) {
1280 ret = -ENOMEM;
1281 goto out;
1282 }
1283
1284 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1285 if (!inode) {
1286 ret = -ENOMEM;
1287 goto out;
1288 }
1289 inode->i_fop = &sel_initcon_ops;
1290 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1291 d_add(dentry, inode);
1292 }
1293out:
1294 return ret;
1295}
1296
James Morrisedb20fb2006-03-22 00:09:20 -08001297static int sel_make_dir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 int ret = 0;
1300 struct inode *inode;
1301
James Morrisedb20fb2006-03-22 00:09:20 -08001302 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!inode) {
1304 ret = -ENOMEM;
1305 goto out;
1306 }
1307 inode->i_op = &simple_dir_inode_operations;
1308 inode->i_fop = &simple_dir_operations;
James Carter6174eaf2007-04-04 16:18:39 -04001309 inode->i_ino = ++sel_last_ino;
James Morris40e906f2006-03-22 00:09:16 -08001310 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001311 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 d_add(dentry, inode);
James Morrisedb20fb2006-03-22 00:09:20 -08001313 /* bump link count on parent directory, too */
Dave Hansend8c76e62006-09-30 23:29:04 -07001314 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315out:
1316 return ret;
1317}
1318
1319static int sel_fill_super(struct super_block * sb, void * data, int silent)
1320{
1321 int ret;
1322 struct dentry *dentry;
James Morrisedb20fb2006-03-22 00:09:20 -08001323 struct inode *inode, *root_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct inode_security_struct *isec;
1325
1326 static struct tree_descr selinux_files[] = {
1327 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1328 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
Stephen Smalleyce9982d2005-11-08 21:34:33 -08001329 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1331 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1332 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1333 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1334 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1335 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1336 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1337 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1338 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1339 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
James Morris4e5ab4c2006-06-09 00:33:33 -07001340 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* last one */ {""}
1342 };
1343 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1344 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001345 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
James Morrisedb20fb2006-03-22 00:09:20 -08001347 root_inode = sb->s_root->d_inode;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
James Morris161ce452006-03-22 00:09:17 -08001350 if (!dentry) {
1351 ret = -ENOMEM;
1352 goto err;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
James Morrisedb20fb2006-03-22 00:09:20 -08001355 ret = sel_make_dir(root_inode, dentry);
James Morriscde174a2006-03-22 00:09:17 -08001356 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001357 goto err;
James Morriscde174a2006-03-22 00:09:17 -08001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 bool_dir = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
James Morris161ce452006-03-22 00:09:17 -08001362 if (!dentry) {
1363 ret = -ENOMEM;
1364 goto err;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
James Morris161ce452006-03-22 00:09:17 -08001368 if (!inode) {
1369 ret = -ENOMEM;
1370 goto err;
1371 }
James Carter6174eaf2007-04-04 16:18:39 -04001372 inode->i_ino = ++sel_last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 isec = (struct inode_security_struct*)inode->i_security;
1374 isec->sid = SECINITSID_DEVNULL;
1375 isec->sclass = SECCLASS_CHR_FILE;
1376 isec->initialized = 1;
1377
1378 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1379 d_add(dentry, inode);
1380 selinux_null = dentry;
1381
1382 dentry = d_alloc_name(sb->s_root, "avc");
James Morris161ce452006-03-22 00:09:17 -08001383 if (!dentry) {
1384 ret = -ENOMEM;
1385 goto err;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
James Morrisedb20fb2006-03-22 00:09:20 -08001388 ret = sel_make_dir(root_inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001390 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 ret = sel_make_avc_files(dentry);
1393 if (ret)
James Morris161ce452006-03-22 00:09:17 -08001394 goto err;
James Carterf0ee2e42007-04-04 10:11:29 -04001395
1396 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1397 if (!dentry) {
1398 ret = -ENOMEM;
1399 goto err;
1400 }
1401
1402 ret = sel_make_dir(root_inode, dentry);
1403 if (ret)
1404 goto err;
1405
1406 ret = sel_make_initcon_files(dentry);
1407 if (ret)
1408 goto err;
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410out:
James Morris161ce452006-03-22 00:09:17 -08001411 return ret;
1412err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
James Morris161ce452006-03-22 00:09:17 -08001414 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
David Howells454e2392006-06-23 02:02:57 -07001417static int sel_get_sb(struct file_system_type *fs_type,
1418 int flags, const char *dev_name, void *data,
1419 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
David Howells454e2392006-06-23 02:02:57 -07001421 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static struct file_system_type sel_fs_type = {
1425 .name = "selinuxfs",
1426 .get_sb = sel_get_sb,
1427 .kill_sb = kill_litter_super,
1428};
1429
1430struct vfsmount *selinuxfs_mount;
1431
1432static int __init init_sel_fs(void)
1433{
1434 int err;
1435
1436 if (!selinux_enabled)
1437 return 0;
1438 err = register_filesystem(&sel_fs_type);
1439 if (!err) {
1440 selinuxfs_mount = kern_mount(&sel_fs_type);
1441 if (IS_ERR(selinuxfs_mount)) {
1442 printk(KERN_ERR "selinuxfs: could not mount!\n");
1443 err = PTR_ERR(selinuxfs_mount);
1444 selinuxfs_mount = NULL;
1445 }
1446 }
1447 return err;
1448}
1449
1450__initcall(init_sel_fs);
1451
1452#ifdef CONFIG_SECURITY_SELINUX_DISABLE
1453void exit_sel_fs(void)
1454{
1455 unregister_filesystem(&sel_fs_type);
1456}
1457#endif