Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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 Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 24 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #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 | |
| 38 | unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 39 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 40 | #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 | |
| 46 | int selinux_compat_net = SELINUX_COMPAT_NET_VALUE; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static 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 Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 55 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 63 | static DEFINE_MUTEX(sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* global data for booleans */ |
| 66 | static struct dentry *bool_dir = NULL; |
| 67 | static int bool_num = 0; |
| 68 | static int *bool_pending_values = NULL; |
| 69 | |
| 70 | extern void selnl_notify_setenforce(int val); |
| 71 | |
| 72 | /* Check whether a task is allowed to use a security operation. */ |
| 73 | static 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 | |
| 86 | enum 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 */ |
| 99 | SEL_AVC, /* AVC management directory */ |
| 100 | SEL_MEMBER, /* compute polyinstantiation membership decision */ |
| 101 | SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 102 | SEL_COMPAT_NET, /* whether to use old compat network packet controls */ |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame^] | 103 | SEL_INO_NEXT, /* The next inode number to use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame^] | 106 | static unsigned long sel_last_ino = SEL_INO_NEXT - 1; |
| 107 | |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 108 | #define SEL_INITCON_INO_OFFSET 0x01000000 |
| 109 | #define SEL_INO_MASK 0x00ffffff |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define TMPBUFLEN 12 |
| 112 | static 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 |
| 123 | static 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 Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 131 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 152 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | selinux_enforcing = new_value; |
| 157 | if (selinux_enforcing) |
| 158 | avc_ss_reset(0); |
| 159 | selnl_notify_setenforce(selinux_enforcing); |
| 160 | } |
| 161 | length = count; |
| 162 | out: |
| 163 | free_page((unsigned long) page); |
| 164 | return length; |
| 165 | } |
| 166 | #else |
| 167 | #define sel_write_enforce NULL |
| 168 | #endif |
| 169 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 170 | static const struct file_operations sel_enforce_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | .read = sel_read_enforce, |
| 172 | .write = sel_write_enforce, |
| 173 | }; |
| 174 | |
| 175 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 176 | static 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 Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 185 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | 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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 206 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
| 207 | "selinux=0 auid=%u", |
| 208 | audit_get_loginuid(current->audit_context)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | length = count; |
| 212 | out: |
| 213 | free_page((unsigned long) page); |
| 214 | return length; |
| 215 | } |
| 216 | #else |
| 217 | #define sel_write_disable NULL |
| 218 | #endif |
| 219 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 220 | static const struct file_operations sel_disable_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | .write = sel_write_disable, |
| 222 | }; |
| 223 | |
| 224 | static 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 Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 234 | static const struct file_operations sel_policyvers_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | .read = sel_read_policyvers, |
| 236 | }; |
| 237 | |
| 238 | /* declaration for sel_write_load */ |
| 239 | static int sel_make_bools(void); |
| 240 | |
| 241 | static 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 Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 251 | static const struct file_operations sel_mls_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | .read = sel_read_mls, |
| 253 | }; |
| 254 | |
| 255 | static 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 Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 263 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 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 Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 275 | if ((count > 64 * 1024 * 1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | || (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 Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 294 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
| 295 | "policy loaded auid=%u", |
| 296 | audit_get_loginuid(current->audit_context)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 298 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | vfree(data); |
| 300 | return length; |
| 301 | } |
| 302 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 303 | static const struct file_operations sel_load_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | .write = sel_write_load, |
| 305 | }; |
| 306 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 307 | static ssize_t sel_write_context(struct file * file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 309 | char *canon; |
| 310 | u32 sid, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | ssize_t length; |
| 312 | |
| 313 | length = task_has_security(current, SECURITY__CHECK_CONTEXT); |
| 314 | if (length) |
| 315 | return length; |
| 316 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 317 | length = security_context_to_sid(buf, size, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (length < 0) |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 319 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 321 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | out: |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 335 | kfree(canon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return length; |
| 337 | } |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | static 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 | |
| 349 | static 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 Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 360 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | 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; |
| 379 | out: |
| 380 | free_page((unsigned long) page); |
| 381 | return length; |
| 382 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 383 | static const struct file_operations sel_checkreqprot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | .read = sel_read_checkreqprot, |
| 385 | .write = sel_write_checkreqprot, |
| 386 | }; |
| 387 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 388 | static 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 | |
| 398 | static 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; |
| 428 | out: |
| 429 | free_page((unsigned long) page); |
| 430 | return length; |
| 431 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 432 | static const struct file_operations sel_compat_net_ops = { |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 433 | .read = sel_read_compat_net, |
| 434 | .write = sel_write_compat_net, |
| 435 | }; |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | /* |
| 438 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c |
| 439 | */ |
| 440 | static ssize_t sel_write_access(struct file * file, char *buf, size_t size); |
| 441 | static ssize_t sel_write_create(struct file * file, char *buf, size_t size); |
| 442 | static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size); |
| 443 | static ssize_t sel_write_user(struct file * file, char *buf, size_t size); |
| 444 | static ssize_t sel_write_member(struct file * file, char *buf, size_t size); |
| 445 | |
| 446 | static 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 Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 452 | [SEL_CONTEXT] = sel_write_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) |
| 456 | { |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 457 | ino_t ino = file->f_path.dentry->d_inode->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | char *data; |
| 459 | ssize_t rv; |
| 460 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 461 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | 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 Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 476 | static const struct file_operations transaction_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | .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 | |
| 488 | static 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 Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 502 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (!scon) |
| 504 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 506 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (!tcon) |
| 508 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 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); |
| 530 | out2: |
| 531 | kfree(tcon); |
| 532 | out: |
| 533 | kfree(scon); |
| 534 | return length; |
| 535 | } |
| 536 | |
| 537 | static 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 Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 551 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (!scon) |
| 553 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 555 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (!tcon) |
| 557 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 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; |
| 587 | out3: |
| 588 | kfree(newcon); |
| 589 | out2: |
| 590 | kfree(tcon); |
| 591 | out: |
| 592 | kfree(scon); |
| 593 | return length; |
| 594 | } |
| 595 | |
| 596 | static 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 Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 610 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | if (!scon) |
| 612 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 614 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (!tcon) |
| 616 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 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; |
| 644 | out3: |
| 645 | kfree(newcon); |
| 646 | out2: |
| 647 | kfree(tcon); |
| 648 | out: |
| 649 | kfree(scon); |
| 650 | return length; |
| 651 | } |
| 652 | |
| 653 | static 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 Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 667 | con = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (!con) |
| 669 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 671 | user = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (!user) |
| 673 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 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 | } |
| 705 | out3: |
| 706 | kfree(sids); |
| 707 | out2: |
| 708 | kfree(user); |
| 709 | out: |
| 710 | kfree(con); |
| 711 | return length; |
| 712 | } |
| 713 | |
| 714 | static 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 Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 728 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if (!scon) |
| 730 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 732 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | if (!tcon) |
| 734 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 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; |
| 764 | out3: |
| 765 | kfree(newcon); |
| 766 | out2: |
| 767 | kfree(tcon); |
| 768 | out: |
| 769 | kfree(scon); |
| 770 | return length; |
| 771 | } |
| 772 | |
| 773 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | ret->i_blocks = 0; |
| 781 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 782 | } |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | #define BOOL_INO_OFFSET 30 |
| 787 | |
| 788 | static ssize_t sel_read_bool(struct file *filep, char __user *buf, |
| 789 | size_t count, loff_t *ppos) |
| 790 | { |
| 791 | char *page = NULL; |
| 792 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | ssize_t ret; |
| 794 | int cur_enforcing; |
| 795 | struct inode *inode; |
| 796 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 797 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | ret = -EFAULT; |
| 800 | |
| 801 | /* check to see if this file has been deleted */ |
| 802 | if (!filep->f_op) |
| 803 | goto out; |
| 804 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 805 | if (count > PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | ret = -EINVAL; |
| 807 | goto out; |
| 808 | } |
| 809 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) { |
| 810 | ret = -ENOMEM; |
| 811 | goto out; |
| 812 | } |
| 813 | |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 814 | inode = filep->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET); |
| 816 | if (cur_enforcing < 0) { |
| 817 | ret = cur_enforcing; |
| 818 | goto out; |
| 819 | } |
| 820 | |
| 821 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
| 822 | bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]); |
Stephen Smalley | 68bdcf2 | 2006-03-22 00:09:15 -0800 | [diff] [blame] | 823 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 825 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (page) |
| 827 | free_page((unsigned long)page); |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, |
| 832 | size_t count, loff_t *ppos) |
| 833 | { |
| 834 | char *page = NULL; |
| 835 | ssize_t length = -EFAULT; |
| 836 | int new_value; |
| 837 | struct inode *inode; |
| 838 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 839 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | length = task_has_security(current, SECURITY__SETBOOL); |
| 842 | if (length) |
| 843 | goto out; |
| 844 | |
| 845 | /* check to see if this file has been deleted */ |
| 846 | if (!filep->f_op) |
| 847 | goto out; |
| 848 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 849 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | length = -ENOMEM; |
| 851 | goto out; |
| 852 | } |
| 853 | if (*ppos != 0) { |
| 854 | /* No partial writes. */ |
| 855 | goto out; |
| 856 | } |
| 857 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 858 | if (!page) { |
| 859 | length = -ENOMEM; |
| 860 | goto out; |
| 861 | } |
| 862 | |
| 863 | if (copy_from_user(page, buf, count)) |
| 864 | goto out; |
| 865 | |
| 866 | length = -EINVAL; |
| 867 | if (sscanf(page, "%d", &new_value) != 1) |
| 868 | goto out; |
| 869 | |
| 870 | if (new_value) |
| 871 | new_value = 1; |
| 872 | |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 873 | inode = filep->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value; |
| 875 | length = count; |
| 876 | |
| 877 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 878 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | if (page) |
| 880 | free_page((unsigned long) page); |
| 881 | return length; |
| 882 | } |
| 883 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 884 | static const struct file_operations sel_bool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | .read = sel_read_bool, |
| 886 | .write = sel_write_bool, |
| 887 | }; |
| 888 | |
| 889 | static ssize_t sel_commit_bools_write(struct file *filep, |
| 890 | const char __user *buf, |
| 891 | size_t count, loff_t *ppos) |
| 892 | { |
| 893 | char *page = NULL; |
| 894 | ssize_t length = -EFAULT; |
| 895 | int new_value; |
| 896 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 897 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | length = task_has_security(current, SECURITY__SETBOOL); |
| 900 | if (length) |
| 901 | goto out; |
| 902 | |
| 903 | /* check to see if this file has been deleted */ |
| 904 | if (!filep->f_op) |
| 905 | goto out; |
| 906 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 907 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | length = -ENOMEM; |
| 909 | goto out; |
| 910 | } |
| 911 | if (*ppos != 0) { |
| 912 | /* No partial writes. */ |
| 913 | goto out; |
| 914 | } |
| 915 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 916 | if (!page) { |
| 917 | length = -ENOMEM; |
| 918 | goto out; |
| 919 | } |
| 920 | |
| 921 | if (copy_from_user(page, buf, count)) |
| 922 | goto out; |
| 923 | |
| 924 | length = -EINVAL; |
| 925 | if (sscanf(page, "%d", &new_value) != 1) |
| 926 | goto out; |
| 927 | |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 928 | if (new_value && bool_pending_values) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | security_set_bools(bool_num, bool_pending_values); |
| 930 | } |
| 931 | |
| 932 | length = count; |
| 933 | |
| 934 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 935 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | if (page) |
| 937 | free_page((unsigned long) page); |
| 938 | return length; |
| 939 | } |
| 940 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 941 | static const struct file_operations sel_commit_bools_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | .write = sel_commit_bools_write, |
| 943 | }; |
| 944 | |
| 945 | /* delete booleans - partial revoke() from |
| 946 | * fs/proc/generic.c proc_kill_inodes */ |
| 947 | static void sel_remove_bools(struct dentry *de) |
| 948 | { |
| 949 | struct list_head *p, *node; |
| 950 | struct super_block *sb = de->d_sb; |
| 951 | |
| 952 | spin_lock(&dcache_lock); |
| 953 | node = de->d_subdirs.next; |
| 954 | while (node != &de->d_subdirs) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 955 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | list_del_init(node); |
| 957 | |
| 958 | if (d->d_inode) { |
| 959 | d = dget_locked(d); |
| 960 | spin_unlock(&dcache_lock); |
| 961 | d_delete(d); |
| 962 | simple_unlink(de->d_inode, d); |
| 963 | dput(d); |
| 964 | spin_lock(&dcache_lock); |
| 965 | } |
| 966 | node = de->d_subdirs.next; |
| 967 | } |
| 968 | |
| 969 | spin_unlock(&dcache_lock); |
| 970 | |
| 971 | file_list_lock(); |
| 972 | list_for_each(p, &sb->s_files) { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 973 | struct file * filp = list_entry(p, struct file, f_u.fu_list); |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 974 | struct dentry * dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
| 976 | if (dentry->d_parent != de) { |
| 977 | continue; |
| 978 | } |
| 979 | filp->f_op = NULL; |
| 980 | } |
| 981 | file_list_unlock(); |
| 982 | } |
| 983 | |
| 984 | #define BOOL_DIR_NAME "booleans" |
| 985 | |
| 986 | static int sel_make_bools(void) |
| 987 | { |
| 988 | int i, ret = 0; |
| 989 | ssize_t len; |
| 990 | struct dentry *dentry = NULL; |
| 991 | struct dentry *dir = bool_dir; |
| 992 | struct inode *inode = NULL; |
| 993 | struct inode_security_struct *isec; |
| 994 | char **names = NULL, *page; |
| 995 | int num; |
| 996 | int *values = NULL; |
| 997 | u32 sid; |
| 998 | |
| 999 | /* remove any existing files */ |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1000 | kfree(bool_pending_values); |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1001 | bool_pending_values = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | sel_remove_bools(dir); |
| 1004 | |
| 1005 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) |
| 1006 | return -ENOMEM; |
| 1007 | |
| 1008 | ret = security_get_bools(&num, &names, &values); |
| 1009 | if (ret != 0) |
| 1010 | goto out; |
| 1011 | |
| 1012 | for (i = 0; i < num; i++) { |
| 1013 | dentry = d_alloc_name(dir, names[i]); |
| 1014 | if (!dentry) { |
| 1015 | ret = -ENOMEM; |
| 1016 | goto err; |
| 1017 | } |
| 1018 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
| 1019 | if (!inode) { |
| 1020 | ret = -ENOMEM; |
| 1021 | goto err; |
| 1022 | } |
| 1023 | |
| 1024 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); |
| 1025 | if (len < 0) { |
| 1026 | ret = -EINVAL; |
| 1027 | goto err; |
| 1028 | } else if (len >= PAGE_SIZE) { |
| 1029 | ret = -ENAMETOOLONG; |
| 1030 | goto err; |
| 1031 | } |
| 1032 | isec = (struct inode_security_struct*)inode->i_security; |
| 1033 | if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid))) |
| 1034 | goto err; |
| 1035 | isec->sid = sid; |
| 1036 | isec->initialized = 1; |
| 1037 | inode->i_fop = &sel_bool_ops; |
| 1038 | inode->i_ino = i + BOOL_INO_OFFSET; |
| 1039 | d_add(dentry, inode); |
| 1040 | } |
| 1041 | bool_num = num; |
| 1042 | bool_pending_values = values; |
| 1043 | out: |
| 1044 | free_page((unsigned long)page); |
| 1045 | if (names) { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1046 | for (i = 0; i < num; i++) |
| 1047 | kfree(names[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | kfree(names); |
| 1049 | } |
| 1050 | return ret; |
| 1051 | err: |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1052 | kfree(values); |
James Morris | 253a8b1 | 2006-03-22 00:09:18 -0800 | [diff] [blame] | 1053 | sel_remove_bools(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | ret = -ENOMEM; |
| 1055 | goto out; |
| 1056 | } |
| 1057 | |
| 1058 | #define NULL_FILE_NAME "null" |
| 1059 | |
| 1060 | struct dentry *selinux_null = NULL; |
| 1061 | |
| 1062 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, |
| 1063 | size_t count, loff_t *ppos) |
| 1064 | { |
| 1065 | char tmpbuf[TMPBUFLEN]; |
| 1066 | ssize_t length; |
| 1067 | |
| 1068 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); |
| 1069 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1070 | } |
| 1071 | |
| 1072 | static ssize_t sel_write_avc_cache_threshold(struct file * file, |
| 1073 | const char __user * buf, |
| 1074 | size_t count, loff_t *ppos) |
| 1075 | |
| 1076 | { |
| 1077 | char *page; |
| 1078 | ssize_t ret; |
| 1079 | int new_value; |
| 1080 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1081 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | ret = -ENOMEM; |
| 1083 | goto out; |
| 1084 | } |
| 1085 | |
| 1086 | if (*ppos != 0) { |
| 1087 | /* No partial writes. */ |
| 1088 | ret = -EINVAL; |
| 1089 | goto out; |
| 1090 | } |
| 1091 | |
| 1092 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 1093 | if (!page) { |
| 1094 | ret = -ENOMEM; |
| 1095 | goto out; |
| 1096 | } |
| 1097 | |
| 1098 | if (copy_from_user(page, buf, count)) { |
| 1099 | ret = -EFAULT; |
| 1100 | goto out_free; |
| 1101 | } |
| 1102 | |
| 1103 | if (sscanf(page, "%u", &new_value) != 1) { |
| 1104 | ret = -EINVAL; |
| 1105 | goto out; |
| 1106 | } |
| 1107 | |
| 1108 | if (new_value != avc_cache_threshold) { |
| 1109 | ret = task_has_security(current, SECURITY__SETSECPARAM); |
| 1110 | if (ret) |
| 1111 | goto out_free; |
| 1112 | avc_cache_threshold = new_value; |
| 1113 | } |
| 1114 | ret = count; |
| 1115 | out_free: |
| 1116 | free_page((unsigned long)page); |
| 1117 | out: |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, |
| 1122 | size_t count, loff_t *ppos) |
| 1123 | { |
| 1124 | char *page; |
| 1125 | ssize_t ret = 0; |
| 1126 | |
| 1127 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1128 | if (!page) { |
| 1129 | ret = -ENOMEM; |
| 1130 | goto out; |
| 1131 | } |
| 1132 | ret = avc_get_hash_stats(page); |
| 1133 | if (ret >= 0) |
| 1134 | ret = simple_read_from_buffer(buf, count, ppos, page, ret); |
| 1135 | free_page((unsigned long)page); |
| 1136 | out: |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1140 | static const struct file_operations sel_avc_cache_threshold_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | .read = sel_read_avc_cache_threshold, |
| 1142 | .write = sel_write_avc_cache_threshold, |
| 1143 | }; |
| 1144 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1145 | static const struct file_operations sel_avc_hash_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | .read = sel_read_avc_hash_stats, |
| 1147 | }; |
| 1148 | |
| 1149 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1150 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) |
| 1151 | { |
| 1152 | int cpu; |
| 1153 | |
| 1154 | for (cpu = *idx; cpu < NR_CPUS; ++cpu) { |
| 1155 | if (!cpu_possible(cpu)) |
| 1156 | continue; |
| 1157 | *idx = cpu + 1; |
| 1158 | return &per_cpu(avc_cache_stats, cpu); |
| 1159 | } |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | |
| 1163 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) |
| 1164 | { |
| 1165 | loff_t n = *pos - 1; |
| 1166 | |
| 1167 | if (*pos == 0) |
| 1168 | return SEQ_START_TOKEN; |
| 1169 | |
| 1170 | return sel_avc_get_stat_idx(&n); |
| 1171 | } |
| 1172 | |
| 1173 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1174 | { |
| 1175 | return sel_avc_get_stat_idx(pos); |
| 1176 | } |
| 1177 | |
| 1178 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) |
| 1179 | { |
| 1180 | struct avc_cache_stats *st = v; |
| 1181 | |
| 1182 | if (v == SEQ_START_TOKEN) |
| 1183 | seq_printf(seq, "lookups hits misses allocations reclaims " |
| 1184 | "frees\n"); |
| 1185 | else |
| 1186 | seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, |
| 1187 | st->hits, st->misses, st->allocations, |
| 1188 | st->reclaims, st->frees); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) |
| 1193 | { } |
| 1194 | |
| 1195 | static struct seq_operations sel_avc_cache_stats_seq_ops = { |
| 1196 | .start = sel_avc_stats_seq_start, |
| 1197 | .next = sel_avc_stats_seq_next, |
| 1198 | .show = sel_avc_stats_seq_show, |
| 1199 | .stop = sel_avc_stats_seq_stop, |
| 1200 | }; |
| 1201 | |
| 1202 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) |
| 1203 | { |
| 1204 | return seq_open(file, &sel_avc_cache_stats_seq_ops); |
| 1205 | } |
| 1206 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1207 | static const struct file_operations sel_avc_cache_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | .open = sel_open_avc_cache_stats, |
| 1209 | .read = seq_read, |
| 1210 | .llseek = seq_lseek, |
| 1211 | .release = seq_release, |
| 1212 | }; |
| 1213 | #endif |
| 1214 | |
| 1215 | static int sel_make_avc_files(struct dentry *dir) |
| 1216 | { |
| 1217 | int i, ret = 0; |
| 1218 | static struct tree_descr files[] = { |
| 1219 | { "cache_threshold", |
| 1220 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, |
| 1221 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, |
| 1222 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1223 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, |
| 1224 | #endif |
| 1225 | }; |
| 1226 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 1227 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | struct inode *inode; |
| 1229 | struct dentry *dentry; |
| 1230 | |
| 1231 | dentry = d_alloc_name(dir, files[i].name); |
| 1232 | if (!dentry) { |
| 1233 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1234 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); |
| 1238 | if (!inode) { |
| 1239 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1240 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | inode->i_fop = files[i].ops; |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame^] | 1243 | inode->i_ino = ++sel_last_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | d_add(dentry, inode); |
| 1245 | } |
| 1246 | out: |
| 1247 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1250 | static ssize_t sel_read_initcon(struct file * file, char __user *buf, |
| 1251 | size_t count, loff_t *ppos) |
| 1252 | { |
| 1253 | struct inode *inode; |
| 1254 | char *con; |
| 1255 | u32 sid, len; |
| 1256 | ssize_t ret; |
| 1257 | |
| 1258 | inode = file->f_path.dentry->d_inode; |
| 1259 | sid = inode->i_ino&SEL_INO_MASK; |
| 1260 | ret = security_sid_to_context(sid, &con, &len); |
| 1261 | if (ret < 0) |
| 1262 | return ret; |
| 1263 | |
| 1264 | ret = simple_read_from_buffer(buf, count, ppos, con, len); |
| 1265 | kfree(con); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | static const struct file_operations sel_initcon_ops = { |
| 1270 | .read = sel_read_initcon, |
| 1271 | }; |
| 1272 | |
| 1273 | static int sel_make_initcon_files(struct dentry *dir) |
| 1274 | { |
| 1275 | int i, ret = 0; |
| 1276 | |
| 1277 | for (i = 1; i <= SECINITSID_NUM; i++) { |
| 1278 | struct inode *inode; |
| 1279 | struct dentry *dentry; |
| 1280 | dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); |
| 1281 | if (!dentry) { |
| 1282 | ret = -ENOMEM; |
| 1283 | goto out; |
| 1284 | } |
| 1285 | |
| 1286 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1287 | if (!inode) { |
| 1288 | ret = -ENOMEM; |
| 1289 | goto out; |
| 1290 | } |
| 1291 | inode->i_fop = &sel_initcon_ops; |
| 1292 | inode->i_ino = i|SEL_INITCON_INO_OFFSET; |
| 1293 | d_add(dentry, inode); |
| 1294 | } |
| 1295 | out: |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1299 | static int sel_make_dir(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | { |
| 1301 | int ret = 0; |
| 1302 | struct inode *inode; |
| 1303 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1304 | inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | if (!inode) { |
| 1306 | ret = -ENOMEM; |
| 1307 | goto out; |
| 1308 | } |
| 1309 | inode->i_op = &simple_dir_inode_operations; |
| 1310 | inode->i_fop = &simple_dir_operations; |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame^] | 1311 | inode->i_ino = ++sel_last_ino; |
James Morris | 40e906f | 2006-03-22 00:09:16 -0800 | [diff] [blame] | 1312 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1313 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | d_add(dentry, inode); |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1315 | /* bump link count on parent directory, too */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1316 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | out: |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | static int sel_fill_super(struct super_block * sb, void * data, int silent) |
| 1322 | { |
| 1323 | int ret; |
| 1324 | struct dentry *dentry; |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1325 | struct inode *inode, *root_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | struct inode_security_struct *isec; |
| 1327 | |
| 1328 | static struct tree_descr selinux_files[] = { |
| 1329 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, |
| 1330 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 1331 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1333 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1334 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1335 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1336 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, |
| 1337 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, |
| 1338 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, |
| 1339 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, |
| 1340 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1341 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 1342 | [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | /* last one */ {""} |
| 1344 | }; |
| 1345 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); |
| 1346 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1347 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1349 | root_inode = sb->s_root->d_inode; |
| 1350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1352 | if (!dentry) { |
| 1353 | ret = -ENOMEM; |
| 1354 | goto err; |
| 1355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1357 | ret = sel_make_dir(root_inode, dentry); |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1358 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1359 | goto err; |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | bool_dir = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1364 | if (!dentry) { |
| 1365 | ret = -ENOMEM; |
| 1366 | goto err; |
| 1367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1370 | if (!inode) { |
| 1371 | ret = -ENOMEM; |
| 1372 | goto err; |
| 1373 | } |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame^] | 1374 | inode->i_ino = ++sel_last_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | isec = (struct inode_security_struct*)inode->i_security; |
| 1376 | isec->sid = SECINITSID_DEVNULL; |
| 1377 | isec->sclass = SECCLASS_CHR_FILE; |
| 1378 | isec->initialized = 1; |
| 1379 | |
| 1380 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); |
| 1381 | d_add(dentry, inode); |
| 1382 | selinux_null = dentry; |
| 1383 | |
| 1384 | dentry = d_alloc_name(sb->s_root, "avc"); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1385 | if (!dentry) { |
| 1386 | ret = -ENOMEM; |
| 1387 | goto err; |
| 1388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1390 | ret = sel_make_dir(root_inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1392 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | ret = sel_make_avc_files(dentry); |
| 1395 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1396 | goto err; |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1397 | |
| 1398 | dentry = d_alloc_name(sb->s_root, "initial_contexts"); |
| 1399 | if (!dentry) { |
| 1400 | ret = -ENOMEM; |
| 1401 | goto err; |
| 1402 | } |
| 1403 | |
| 1404 | ret = sel_make_dir(root_inode, dentry); |
| 1405 | if (ret) |
| 1406 | goto err; |
| 1407 | |
| 1408 | ret = sel_make_initcon_files(dentry); |
| 1409 | if (ret) |
| 1410 | goto err; |
| 1411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | out: |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1413 | return ret; |
| 1414 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1416 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1419 | static int sel_get_sb(struct file_system_type *fs_type, |
| 1420 | int flags, const char *dev_name, void *data, |
| 1421 | struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1423 | return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static struct file_system_type sel_fs_type = { |
| 1427 | .name = "selinuxfs", |
| 1428 | .get_sb = sel_get_sb, |
| 1429 | .kill_sb = kill_litter_super, |
| 1430 | }; |
| 1431 | |
| 1432 | struct vfsmount *selinuxfs_mount; |
| 1433 | |
| 1434 | static int __init init_sel_fs(void) |
| 1435 | { |
| 1436 | int err; |
| 1437 | |
| 1438 | if (!selinux_enabled) |
| 1439 | return 0; |
| 1440 | err = register_filesystem(&sel_fs_type); |
| 1441 | if (!err) { |
| 1442 | selinuxfs_mount = kern_mount(&sel_fs_type); |
| 1443 | if (IS_ERR(selinuxfs_mount)) { |
| 1444 | printk(KERN_ERR "selinuxfs: could not mount!\n"); |
| 1445 | err = PTR_ERR(selinuxfs_mount); |
| 1446 | selinuxfs_mount = NULL; |
| 1447 | } |
| 1448 | } |
| 1449 | return err; |
| 1450 | } |
| 1451 | |
| 1452 | __initcall(init_sel_fs); |
| 1453 | |
| 1454 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 1455 | void exit_sel_fs(void) |
| 1456 | { |
| 1457 | unregister_filesystem(&sel_fs_type); |
| 1458 | } |
| 1459 | #endif |