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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | #define TMPBUFLEN 12 |
| 106 | static ssize_t sel_read_enforce(struct file *filp, char __user *buf, |
| 107 | size_t count, loff_t *ppos) |
| 108 | { |
| 109 | char tmpbuf[TMPBUFLEN]; |
| 110 | ssize_t length; |
| 111 | |
| 112 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); |
| 113 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 114 | } |
| 115 | |
| 116 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
| 117 | static ssize_t sel_write_enforce(struct file * file, const char __user * buf, |
| 118 | size_t count, loff_t *ppos) |
| 119 | |
| 120 | { |
| 121 | char *page; |
| 122 | ssize_t length; |
| 123 | int new_value; |
| 124 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 125 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return -ENOMEM; |
| 127 | if (*ppos != 0) { |
| 128 | /* No partial writes. */ |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 132 | if (!page) |
| 133 | return -ENOMEM; |
| 134 | length = -EFAULT; |
| 135 | if (copy_from_user(page, buf, count)) |
| 136 | goto out; |
| 137 | |
| 138 | length = -EINVAL; |
| 139 | if (sscanf(page, "%d", &new_value) != 1) |
| 140 | goto out; |
| 141 | |
| 142 | if (new_value != selinux_enforcing) { |
| 143 | length = task_has_security(current, SECURITY__SETENFORCE); |
| 144 | if (length) |
| 145 | goto out; |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 146 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
| 147 | "enforcing=%d old_enforcing=%d auid=%u", new_value, |
| 148 | selinux_enforcing, |
| 149 | audit_get_loginuid(current->audit_context)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | selinux_enforcing = new_value; |
| 151 | if (selinux_enforcing) |
| 152 | avc_ss_reset(0); |
| 153 | selnl_notify_setenforce(selinux_enforcing); |
| 154 | } |
| 155 | length = count; |
| 156 | out: |
| 157 | free_page((unsigned long) page); |
| 158 | return length; |
| 159 | } |
| 160 | #else |
| 161 | #define sel_write_enforce NULL |
| 162 | #endif |
| 163 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 164 | static const struct file_operations sel_enforce_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .read = sel_read_enforce, |
| 166 | .write = sel_write_enforce, |
| 167 | }; |
| 168 | |
| 169 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 170 | static ssize_t sel_write_disable(struct file * file, const char __user * buf, |
| 171 | size_t count, loff_t *ppos) |
| 172 | |
| 173 | { |
| 174 | char *page; |
| 175 | ssize_t length; |
| 176 | int new_value; |
| 177 | extern int selinux_disable(void); |
| 178 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 179 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return -ENOMEM; |
| 181 | if (*ppos != 0) { |
| 182 | /* No partial writes. */ |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 186 | if (!page) |
| 187 | return -ENOMEM; |
| 188 | length = -EFAULT; |
| 189 | if (copy_from_user(page, buf, count)) |
| 190 | goto out; |
| 191 | |
| 192 | length = -EINVAL; |
| 193 | if (sscanf(page, "%d", &new_value) != 1) |
| 194 | goto out; |
| 195 | |
| 196 | if (new_value) { |
| 197 | length = selinux_disable(); |
| 198 | if (length < 0) |
| 199 | goto out; |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 200 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
| 201 | "selinux=0 auid=%u", |
| 202 | audit_get_loginuid(current->audit_context)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | length = count; |
| 206 | out: |
| 207 | free_page((unsigned long) page); |
| 208 | return length; |
| 209 | } |
| 210 | #else |
| 211 | #define sel_write_disable NULL |
| 212 | #endif |
| 213 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 214 | static const struct file_operations sel_disable_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | .write = sel_write_disable, |
| 216 | }; |
| 217 | |
| 218 | static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, |
| 219 | size_t count, loff_t *ppos) |
| 220 | { |
| 221 | char tmpbuf[TMPBUFLEN]; |
| 222 | ssize_t length; |
| 223 | |
| 224 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); |
| 225 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 226 | } |
| 227 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 228 | static const struct file_operations sel_policyvers_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | .read = sel_read_policyvers, |
| 230 | }; |
| 231 | |
| 232 | /* declaration for sel_write_load */ |
| 233 | static int sel_make_bools(void); |
| 234 | |
| 235 | static ssize_t sel_read_mls(struct file *filp, char __user *buf, |
| 236 | size_t count, loff_t *ppos) |
| 237 | { |
| 238 | char tmpbuf[TMPBUFLEN]; |
| 239 | ssize_t length; |
| 240 | |
| 241 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled); |
| 242 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 243 | } |
| 244 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 245 | static const struct file_operations sel_mls_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | .read = sel_read_mls, |
| 247 | }; |
| 248 | |
| 249 | static ssize_t sel_write_load(struct file * file, const char __user * buf, |
| 250 | size_t count, loff_t *ppos) |
| 251 | |
| 252 | { |
| 253 | int ret; |
| 254 | ssize_t length; |
| 255 | void *data = NULL; |
| 256 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 257 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | length = task_has_security(current, SECURITY__LOAD_POLICY); |
| 260 | if (length) |
| 261 | goto out; |
| 262 | |
| 263 | if (*ppos != 0) { |
| 264 | /* No partial writes. */ |
| 265 | length = -EINVAL; |
| 266 | goto out; |
| 267 | } |
| 268 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 269 | if ((count > 64 * 1024 * 1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | || (data = vmalloc(count)) == NULL) { |
| 271 | length = -ENOMEM; |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | length = -EFAULT; |
| 276 | if (copy_from_user(data, buf, count) != 0) |
| 277 | goto out; |
| 278 | |
| 279 | length = security_load_policy(data, count); |
| 280 | if (length) |
| 281 | goto out; |
| 282 | |
| 283 | ret = sel_make_bools(); |
| 284 | if (ret) |
| 285 | length = ret; |
| 286 | else |
| 287 | length = count; |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 288 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
| 289 | "policy loaded auid=%u", |
| 290 | audit_get_loginuid(current->audit_context)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 292 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | vfree(data); |
| 294 | return length; |
| 295 | } |
| 296 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 297 | static const struct file_operations sel_load_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | .write = sel_write_load, |
| 299 | }; |
| 300 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 301 | 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] | 302 | { |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 303 | char *canon; |
| 304 | u32 sid, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | ssize_t length; |
| 306 | |
| 307 | length = task_has_security(current, SECURITY__CHECK_CONTEXT); |
| 308 | if (length) |
| 309 | return length; |
| 310 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 311 | length = security_context_to_sid(buf, size, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (length < 0) |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 313 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 315 | length = security_sid_to_context(sid, &canon, &len); |
| 316 | if (length < 0) |
| 317 | return length; |
| 318 | |
| 319 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 320 | printk(KERN_ERR "%s: context size (%u) exceeds payload " |
| 321 | "max\n", __FUNCTION__, len); |
| 322 | length = -ERANGE; |
| 323 | goto out; |
| 324 | } |
| 325 | |
| 326 | memcpy(buf, canon, len); |
| 327 | length = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | out: |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 329 | kfree(canon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return length; |
| 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, |
| 334 | size_t count, loff_t *ppos) |
| 335 | { |
| 336 | char tmpbuf[TMPBUFLEN]; |
| 337 | ssize_t length; |
| 338 | |
| 339 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); |
| 340 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 341 | } |
| 342 | |
| 343 | static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf, |
| 344 | size_t count, loff_t *ppos) |
| 345 | { |
| 346 | char *page; |
| 347 | ssize_t length; |
| 348 | unsigned int new_value; |
| 349 | |
| 350 | length = task_has_security(current, SECURITY__SETCHECKREQPROT); |
| 351 | if (length) |
| 352 | return length; |
| 353 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 354 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return -ENOMEM; |
| 356 | if (*ppos != 0) { |
| 357 | /* No partial writes. */ |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 361 | if (!page) |
| 362 | return -ENOMEM; |
| 363 | length = -EFAULT; |
| 364 | if (copy_from_user(page, buf, count)) |
| 365 | goto out; |
| 366 | |
| 367 | length = -EINVAL; |
| 368 | if (sscanf(page, "%u", &new_value) != 1) |
| 369 | goto out; |
| 370 | |
| 371 | selinux_checkreqprot = new_value ? 1 : 0; |
| 372 | length = count; |
| 373 | out: |
| 374 | free_page((unsigned long) page); |
| 375 | return length; |
| 376 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 377 | static const struct file_operations sel_checkreqprot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | .read = sel_read_checkreqprot, |
| 379 | .write = sel_write_checkreqprot, |
| 380 | }; |
| 381 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 382 | static ssize_t sel_read_compat_net(struct file *filp, char __user *buf, |
| 383 | size_t count, loff_t *ppos) |
| 384 | { |
| 385 | char tmpbuf[TMPBUFLEN]; |
| 386 | ssize_t length; |
| 387 | |
| 388 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net); |
| 389 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 390 | } |
| 391 | |
| 392 | static ssize_t sel_write_compat_net(struct file * file, const char __user * buf, |
| 393 | size_t count, loff_t *ppos) |
| 394 | { |
| 395 | char *page; |
| 396 | ssize_t length; |
| 397 | int new_value; |
| 398 | |
| 399 | length = task_has_security(current, SECURITY__LOAD_POLICY); |
| 400 | if (length) |
| 401 | return length; |
| 402 | |
| 403 | if (count >= PAGE_SIZE) |
| 404 | return -ENOMEM; |
| 405 | if (*ppos != 0) { |
| 406 | /* No partial writes. */ |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 410 | if (!page) |
| 411 | return -ENOMEM; |
| 412 | length = -EFAULT; |
| 413 | if (copy_from_user(page, buf, count)) |
| 414 | goto out; |
| 415 | |
| 416 | length = -EINVAL; |
| 417 | if (sscanf(page, "%d", &new_value) != 1) |
| 418 | goto out; |
| 419 | |
| 420 | selinux_compat_net = new_value ? 1 : 0; |
| 421 | length = count; |
| 422 | out: |
| 423 | free_page((unsigned long) page); |
| 424 | return length; |
| 425 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 426 | static const struct file_operations sel_compat_net_ops = { |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 427 | .read = sel_read_compat_net, |
| 428 | .write = sel_write_compat_net, |
| 429 | }; |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* |
| 432 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c |
| 433 | */ |
| 434 | static ssize_t sel_write_access(struct file * file, char *buf, size_t size); |
| 435 | static ssize_t sel_write_create(struct file * file, char *buf, size_t size); |
| 436 | static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size); |
| 437 | static ssize_t sel_write_user(struct file * file, char *buf, size_t size); |
| 438 | static ssize_t sel_write_member(struct file * file, char *buf, size_t size); |
| 439 | |
| 440 | static ssize_t (*write_op[])(struct file *, char *, size_t) = { |
| 441 | [SEL_ACCESS] = sel_write_access, |
| 442 | [SEL_CREATE] = sel_write_create, |
| 443 | [SEL_RELABEL] = sel_write_relabel, |
| 444 | [SEL_USER] = sel_write_user, |
| 445 | [SEL_MEMBER] = sel_write_member, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 446 | [SEL_CONTEXT] = sel_write_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) |
| 450 | { |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 451 | ino_t ino = file->f_path.dentry->d_inode->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | char *data; |
| 453 | ssize_t rv; |
| 454 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 455 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | |
| 458 | data = simple_transaction_get(file, buf, size); |
| 459 | if (IS_ERR(data)) |
| 460 | return PTR_ERR(data); |
| 461 | |
| 462 | rv = write_op[ino](file, data, size); |
| 463 | if (rv>0) { |
| 464 | simple_transaction_set(file, rv); |
| 465 | rv = size; |
| 466 | } |
| 467 | return rv; |
| 468 | } |
| 469 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 470 | static const struct file_operations transaction_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | .write = selinux_transaction_write, |
| 472 | .read = simple_transaction_read, |
| 473 | .release = simple_transaction_release, |
| 474 | }; |
| 475 | |
| 476 | /* |
| 477 | * payload - write methods |
| 478 | * If the method has a response, the response should be put in buf, |
| 479 | * and the length returned. Otherwise return 0 or and -error. |
| 480 | */ |
| 481 | |
| 482 | static ssize_t sel_write_access(struct file * file, char *buf, size_t size) |
| 483 | { |
| 484 | char *scon, *tcon; |
| 485 | u32 ssid, tsid; |
| 486 | u16 tclass; |
| 487 | u32 req; |
| 488 | struct av_decision avd; |
| 489 | ssize_t length; |
| 490 | |
| 491 | length = task_has_security(current, SECURITY__COMPUTE_AV); |
| 492 | if (length) |
| 493 | return length; |
| 494 | |
| 495 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 496 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!scon) |
| 498 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 500 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (!tcon) |
| 502 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | length = -EINVAL; |
| 505 | if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4) |
| 506 | goto out2; |
| 507 | |
| 508 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 509 | if (length < 0) |
| 510 | goto out2; |
| 511 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 512 | if (length < 0) |
| 513 | goto out2; |
| 514 | |
| 515 | length = security_compute_av(ssid, tsid, tclass, req, &avd); |
| 516 | if (length < 0) |
| 517 | goto out2; |
| 518 | |
| 519 | length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, |
| 520 | "%x %x %x %x %u", |
| 521 | avd.allowed, avd.decided, |
| 522 | avd.auditallow, avd.auditdeny, |
| 523 | avd.seqno); |
| 524 | out2: |
| 525 | kfree(tcon); |
| 526 | out: |
| 527 | kfree(scon); |
| 528 | return length; |
| 529 | } |
| 530 | |
| 531 | static ssize_t sel_write_create(struct file * file, char *buf, size_t size) |
| 532 | { |
| 533 | char *scon, *tcon; |
| 534 | u32 ssid, tsid, newsid; |
| 535 | u16 tclass; |
| 536 | ssize_t length; |
| 537 | char *newcon; |
| 538 | u32 len; |
| 539 | |
| 540 | length = task_has_security(current, SECURITY__COMPUTE_CREATE); |
| 541 | if (length) |
| 542 | return length; |
| 543 | |
| 544 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 545 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (!scon) |
| 547 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 549 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | if (!tcon) |
| 551 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | length = -EINVAL; |
| 554 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 555 | goto out2; |
| 556 | |
| 557 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 558 | if (length < 0) |
| 559 | goto out2; |
| 560 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 561 | if (length < 0) |
| 562 | goto out2; |
| 563 | |
| 564 | length = security_transition_sid(ssid, tsid, tclass, &newsid); |
| 565 | if (length < 0) |
| 566 | goto out2; |
| 567 | |
| 568 | length = security_sid_to_context(newsid, &newcon, &len); |
| 569 | if (length < 0) |
| 570 | goto out2; |
| 571 | |
| 572 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 573 | printk(KERN_ERR "%s: context size (%u) exceeds payload " |
| 574 | "max\n", __FUNCTION__, len); |
| 575 | length = -ERANGE; |
| 576 | goto out3; |
| 577 | } |
| 578 | |
| 579 | memcpy(buf, newcon, len); |
| 580 | length = len; |
| 581 | out3: |
| 582 | kfree(newcon); |
| 583 | out2: |
| 584 | kfree(tcon); |
| 585 | out: |
| 586 | kfree(scon); |
| 587 | return length; |
| 588 | } |
| 589 | |
| 590 | static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size) |
| 591 | { |
| 592 | char *scon, *tcon; |
| 593 | u32 ssid, tsid, newsid; |
| 594 | u16 tclass; |
| 595 | ssize_t length; |
| 596 | char *newcon; |
| 597 | u32 len; |
| 598 | |
| 599 | length = task_has_security(current, SECURITY__COMPUTE_RELABEL); |
| 600 | if (length) |
| 601 | return length; |
| 602 | |
| 603 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 604 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | if (!scon) |
| 606 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 608 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (!tcon) |
| 610 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | length = -EINVAL; |
| 613 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 614 | goto out2; |
| 615 | |
| 616 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 617 | if (length < 0) |
| 618 | goto out2; |
| 619 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 620 | if (length < 0) |
| 621 | goto out2; |
| 622 | |
| 623 | length = security_change_sid(ssid, tsid, tclass, &newsid); |
| 624 | if (length < 0) |
| 625 | goto out2; |
| 626 | |
| 627 | length = security_sid_to_context(newsid, &newcon, &len); |
| 628 | if (length < 0) |
| 629 | goto out2; |
| 630 | |
| 631 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 632 | length = -ERANGE; |
| 633 | goto out3; |
| 634 | } |
| 635 | |
| 636 | memcpy(buf, newcon, len); |
| 637 | length = len; |
| 638 | out3: |
| 639 | kfree(newcon); |
| 640 | out2: |
| 641 | kfree(tcon); |
| 642 | out: |
| 643 | kfree(scon); |
| 644 | return length; |
| 645 | } |
| 646 | |
| 647 | static ssize_t sel_write_user(struct file * file, char *buf, size_t size) |
| 648 | { |
| 649 | char *con, *user, *ptr; |
| 650 | u32 sid, *sids; |
| 651 | ssize_t length; |
| 652 | char *newcon; |
| 653 | int i, rc; |
| 654 | u32 len, nsids; |
| 655 | |
| 656 | length = task_has_security(current, SECURITY__COMPUTE_USER); |
| 657 | if (length) |
| 658 | return length; |
| 659 | |
| 660 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 661 | con = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (!con) |
| 663 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 665 | user = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (!user) |
| 667 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | length = -EINVAL; |
| 670 | if (sscanf(buf, "%s %s", con, user) != 2) |
| 671 | goto out2; |
| 672 | |
| 673 | length = security_context_to_sid(con, strlen(con)+1, &sid); |
| 674 | if (length < 0) |
| 675 | goto out2; |
| 676 | |
| 677 | length = security_get_user_sids(sid, user, &sids, &nsids); |
| 678 | if (length < 0) |
| 679 | goto out2; |
| 680 | |
| 681 | length = sprintf(buf, "%u", nsids) + 1; |
| 682 | ptr = buf + length; |
| 683 | for (i = 0; i < nsids; i++) { |
| 684 | rc = security_sid_to_context(sids[i], &newcon, &len); |
| 685 | if (rc) { |
| 686 | length = rc; |
| 687 | goto out3; |
| 688 | } |
| 689 | if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { |
| 690 | kfree(newcon); |
| 691 | length = -ERANGE; |
| 692 | goto out3; |
| 693 | } |
| 694 | memcpy(ptr, newcon, len); |
| 695 | kfree(newcon); |
| 696 | ptr += len; |
| 697 | length += len; |
| 698 | } |
| 699 | out3: |
| 700 | kfree(sids); |
| 701 | out2: |
| 702 | kfree(user); |
| 703 | out: |
| 704 | kfree(con); |
| 705 | return length; |
| 706 | } |
| 707 | |
| 708 | static ssize_t sel_write_member(struct file * file, char *buf, size_t size) |
| 709 | { |
| 710 | char *scon, *tcon; |
| 711 | u32 ssid, tsid, newsid; |
| 712 | u16 tclass; |
| 713 | ssize_t length; |
| 714 | char *newcon; |
| 715 | u32 len; |
| 716 | |
| 717 | length = task_has_security(current, SECURITY__COMPUTE_MEMBER); |
| 718 | if (length) |
| 719 | return length; |
| 720 | |
| 721 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 722 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (!scon) |
| 724 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 726 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (!tcon) |
| 728 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
| 730 | length = -EINVAL; |
| 731 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 732 | goto out2; |
| 733 | |
| 734 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 735 | if (length < 0) |
| 736 | goto out2; |
| 737 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 738 | if (length < 0) |
| 739 | goto out2; |
| 740 | |
| 741 | length = security_member_sid(ssid, tsid, tclass, &newsid); |
| 742 | if (length < 0) |
| 743 | goto out2; |
| 744 | |
| 745 | length = security_sid_to_context(newsid, &newcon, &len); |
| 746 | if (length < 0) |
| 747 | goto out2; |
| 748 | |
| 749 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 750 | printk(KERN_ERR "%s: context size (%u) exceeds payload " |
| 751 | "max\n", __FUNCTION__, len); |
| 752 | length = -ERANGE; |
| 753 | goto out3; |
| 754 | } |
| 755 | |
| 756 | memcpy(buf, newcon, len); |
| 757 | length = len; |
| 758 | out3: |
| 759 | kfree(newcon); |
| 760 | out2: |
| 761 | kfree(tcon); |
| 762 | out: |
| 763 | kfree(scon); |
| 764 | return length; |
| 765 | } |
| 766 | |
| 767 | static struct inode *sel_make_inode(struct super_block *sb, int mode) |
| 768 | { |
| 769 | struct inode *ret = new_inode(sb); |
| 770 | |
| 771 | if (ret) { |
| 772 | ret->i_mode = mode; |
| 773 | ret->i_uid = ret->i_gid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | ret->i_blocks = 0; |
| 775 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 776 | } |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | #define BOOL_INO_OFFSET 30 |
| 781 | |
| 782 | static ssize_t sel_read_bool(struct file *filep, char __user *buf, |
| 783 | size_t count, loff_t *ppos) |
| 784 | { |
| 785 | char *page = NULL; |
| 786 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | ssize_t ret; |
| 788 | int cur_enforcing; |
| 789 | struct inode *inode; |
| 790 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 791 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | ret = -EFAULT; |
| 794 | |
| 795 | /* check to see if this file has been deleted */ |
| 796 | if (!filep->f_op) |
| 797 | goto out; |
| 798 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 799 | if (count > PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | ret = -EINVAL; |
| 801 | goto out; |
| 802 | } |
| 803 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) { |
| 804 | ret = -ENOMEM; |
| 805 | goto out; |
| 806 | } |
| 807 | |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 808 | inode = filep->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET); |
| 810 | if (cur_enforcing < 0) { |
| 811 | ret = cur_enforcing; |
| 812 | goto out; |
| 813 | } |
| 814 | |
| 815 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
| 816 | bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]); |
Stephen Smalley | 68bdcf2 | 2006-03-22 00:09:15 -0800 | [diff] [blame] | 817 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 819 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (page) |
| 821 | free_page((unsigned long)page); |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, |
| 826 | size_t count, loff_t *ppos) |
| 827 | { |
| 828 | char *page = NULL; |
| 829 | ssize_t length = -EFAULT; |
| 830 | int new_value; |
| 831 | struct inode *inode; |
| 832 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 833 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
| 835 | length = task_has_security(current, SECURITY__SETBOOL); |
| 836 | if (length) |
| 837 | goto out; |
| 838 | |
| 839 | /* check to see if this file has been deleted */ |
| 840 | if (!filep->f_op) |
| 841 | goto out; |
| 842 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 843 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | length = -ENOMEM; |
| 845 | goto out; |
| 846 | } |
| 847 | if (*ppos != 0) { |
| 848 | /* No partial writes. */ |
| 849 | goto out; |
| 850 | } |
| 851 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 852 | if (!page) { |
| 853 | length = -ENOMEM; |
| 854 | goto out; |
| 855 | } |
| 856 | |
| 857 | if (copy_from_user(page, buf, count)) |
| 858 | goto out; |
| 859 | |
| 860 | length = -EINVAL; |
| 861 | if (sscanf(page, "%d", &new_value) != 1) |
| 862 | goto out; |
| 863 | |
| 864 | if (new_value) |
| 865 | new_value = 1; |
| 866 | |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 867 | inode = filep->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value; |
| 869 | length = count; |
| 870 | |
| 871 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 872 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | if (page) |
| 874 | free_page((unsigned long) page); |
| 875 | return length; |
| 876 | } |
| 877 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 878 | static const struct file_operations sel_bool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | .read = sel_read_bool, |
| 880 | .write = sel_write_bool, |
| 881 | }; |
| 882 | |
| 883 | static ssize_t sel_commit_bools_write(struct file *filep, |
| 884 | const char __user *buf, |
| 885 | size_t count, loff_t *ppos) |
| 886 | { |
| 887 | char *page = NULL; |
| 888 | ssize_t length = -EFAULT; |
| 889 | int new_value; |
| 890 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 891 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | length = task_has_security(current, SECURITY__SETBOOL); |
| 894 | if (length) |
| 895 | goto out; |
| 896 | |
| 897 | /* check to see if this file has been deleted */ |
| 898 | if (!filep->f_op) |
| 899 | goto out; |
| 900 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 901 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | length = -ENOMEM; |
| 903 | goto out; |
| 904 | } |
| 905 | if (*ppos != 0) { |
| 906 | /* No partial writes. */ |
| 907 | goto out; |
| 908 | } |
| 909 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 910 | if (!page) { |
| 911 | length = -ENOMEM; |
| 912 | goto out; |
| 913 | } |
| 914 | |
| 915 | if (copy_from_user(page, buf, count)) |
| 916 | goto out; |
| 917 | |
| 918 | length = -EINVAL; |
| 919 | if (sscanf(page, "%d", &new_value) != 1) |
| 920 | goto out; |
| 921 | |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 922 | if (new_value && bool_pending_values) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | security_set_bools(bool_num, bool_pending_values); |
| 924 | } |
| 925 | |
| 926 | length = count; |
| 927 | |
| 928 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 929 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (page) |
| 931 | free_page((unsigned long) page); |
| 932 | return length; |
| 933 | } |
| 934 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 935 | static const struct file_operations sel_commit_bools_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | .write = sel_commit_bools_write, |
| 937 | }; |
| 938 | |
| 939 | /* delete booleans - partial revoke() from |
| 940 | * fs/proc/generic.c proc_kill_inodes */ |
| 941 | static void sel_remove_bools(struct dentry *de) |
| 942 | { |
| 943 | struct list_head *p, *node; |
| 944 | struct super_block *sb = de->d_sb; |
| 945 | |
| 946 | spin_lock(&dcache_lock); |
| 947 | node = de->d_subdirs.next; |
| 948 | while (node != &de->d_subdirs) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 949 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | list_del_init(node); |
| 951 | |
| 952 | if (d->d_inode) { |
| 953 | d = dget_locked(d); |
| 954 | spin_unlock(&dcache_lock); |
| 955 | d_delete(d); |
| 956 | simple_unlink(de->d_inode, d); |
| 957 | dput(d); |
| 958 | spin_lock(&dcache_lock); |
| 959 | } |
| 960 | node = de->d_subdirs.next; |
| 961 | } |
| 962 | |
| 963 | spin_unlock(&dcache_lock); |
| 964 | |
| 965 | file_list_lock(); |
| 966 | list_for_each(p, &sb->s_files) { |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 967 | struct file * filp = list_entry(p, struct file, f_u.fu_list); |
Josef Sipek | 3d5ff52 | 2006-12-08 02:37:38 -0800 | [diff] [blame] | 968 | struct dentry * dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | if (dentry->d_parent != de) { |
| 971 | continue; |
| 972 | } |
| 973 | filp->f_op = NULL; |
| 974 | } |
| 975 | file_list_unlock(); |
| 976 | } |
| 977 | |
| 978 | #define BOOL_DIR_NAME "booleans" |
| 979 | |
| 980 | static int sel_make_bools(void) |
| 981 | { |
| 982 | int i, ret = 0; |
| 983 | ssize_t len; |
| 984 | struct dentry *dentry = NULL; |
| 985 | struct dentry *dir = bool_dir; |
| 986 | struct inode *inode = NULL; |
| 987 | struct inode_security_struct *isec; |
| 988 | char **names = NULL, *page; |
| 989 | int num; |
| 990 | int *values = NULL; |
| 991 | u32 sid; |
| 992 | |
| 993 | /* remove any existing files */ |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 994 | kfree(bool_pending_values); |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 995 | bool_pending_values = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
| 997 | sel_remove_bools(dir); |
| 998 | |
| 999 | if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) |
| 1000 | return -ENOMEM; |
| 1001 | |
| 1002 | ret = security_get_bools(&num, &names, &values); |
| 1003 | if (ret != 0) |
| 1004 | goto out; |
| 1005 | |
| 1006 | for (i = 0; i < num; i++) { |
| 1007 | dentry = d_alloc_name(dir, names[i]); |
| 1008 | if (!dentry) { |
| 1009 | ret = -ENOMEM; |
| 1010 | goto err; |
| 1011 | } |
| 1012 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
| 1013 | if (!inode) { |
| 1014 | ret = -ENOMEM; |
| 1015 | goto err; |
| 1016 | } |
| 1017 | |
| 1018 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); |
| 1019 | if (len < 0) { |
| 1020 | ret = -EINVAL; |
| 1021 | goto err; |
| 1022 | } else if (len >= PAGE_SIZE) { |
| 1023 | ret = -ENAMETOOLONG; |
| 1024 | goto err; |
| 1025 | } |
| 1026 | isec = (struct inode_security_struct*)inode->i_security; |
| 1027 | if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid))) |
| 1028 | goto err; |
| 1029 | isec->sid = sid; |
| 1030 | isec->initialized = 1; |
| 1031 | inode->i_fop = &sel_bool_ops; |
| 1032 | inode->i_ino = i + BOOL_INO_OFFSET; |
| 1033 | d_add(dentry, inode); |
| 1034 | } |
| 1035 | bool_num = num; |
| 1036 | bool_pending_values = values; |
| 1037 | out: |
| 1038 | free_page((unsigned long)page); |
| 1039 | if (names) { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1040 | for (i = 0; i < num; i++) |
| 1041 | kfree(names[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | kfree(names); |
| 1043 | } |
| 1044 | return ret; |
| 1045 | err: |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1046 | kfree(values); |
James Morris | 253a8b1 | 2006-03-22 00:09:18 -0800 | [diff] [blame] | 1047 | sel_remove_bools(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | ret = -ENOMEM; |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
| 1052 | #define NULL_FILE_NAME "null" |
| 1053 | |
| 1054 | struct dentry *selinux_null = NULL; |
| 1055 | |
| 1056 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, |
| 1057 | size_t count, loff_t *ppos) |
| 1058 | { |
| 1059 | char tmpbuf[TMPBUFLEN]; |
| 1060 | ssize_t length; |
| 1061 | |
| 1062 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); |
| 1063 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1064 | } |
| 1065 | |
| 1066 | static ssize_t sel_write_avc_cache_threshold(struct file * file, |
| 1067 | const char __user * buf, |
| 1068 | size_t count, loff_t *ppos) |
| 1069 | |
| 1070 | { |
| 1071 | char *page; |
| 1072 | ssize_t ret; |
| 1073 | int new_value; |
| 1074 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1075 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | ret = -ENOMEM; |
| 1077 | goto out; |
| 1078 | } |
| 1079 | |
| 1080 | if (*ppos != 0) { |
| 1081 | /* No partial writes. */ |
| 1082 | ret = -EINVAL; |
| 1083 | goto out; |
| 1084 | } |
| 1085 | |
| 1086 | page = (char*)get_zeroed_page(GFP_KERNEL); |
| 1087 | if (!page) { |
| 1088 | ret = -ENOMEM; |
| 1089 | goto out; |
| 1090 | } |
| 1091 | |
| 1092 | if (copy_from_user(page, buf, count)) { |
| 1093 | ret = -EFAULT; |
| 1094 | goto out_free; |
| 1095 | } |
| 1096 | |
| 1097 | if (sscanf(page, "%u", &new_value) != 1) { |
| 1098 | ret = -EINVAL; |
| 1099 | goto out; |
| 1100 | } |
| 1101 | |
| 1102 | if (new_value != avc_cache_threshold) { |
| 1103 | ret = task_has_security(current, SECURITY__SETSECPARAM); |
| 1104 | if (ret) |
| 1105 | goto out_free; |
| 1106 | avc_cache_threshold = new_value; |
| 1107 | } |
| 1108 | ret = count; |
| 1109 | out_free: |
| 1110 | free_page((unsigned long)page); |
| 1111 | out: |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, |
| 1116 | size_t count, loff_t *ppos) |
| 1117 | { |
| 1118 | char *page; |
| 1119 | ssize_t ret = 0; |
| 1120 | |
| 1121 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1122 | if (!page) { |
| 1123 | ret = -ENOMEM; |
| 1124 | goto out; |
| 1125 | } |
| 1126 | ret = avc_get_hash_stats(page); |
| 1127 | if (ret >= 0) |
| 1128 | ret = simple_read_from_buffer(buf, count, ppos, page, ret); |
| 1129 | free_page((unsigned long)page); |
| 1130 | out: |
| 1131 | return ret; |
| 1132 | } |
| 1133 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 1134 | static const struct file_operations sel_avc_cache_threshold_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | .read = sel_read_avc_cache_threshold, |
| 1136 | .write = sel_write_avc_cache_threshold, |
| 1137 | }; |
| 1138 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 1139 | static const struct file_operations sel_avc_hash_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | .read = sel_read_avc_hash_stats, |
| 1141 | }; |
| 1142 | |
| 1143 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1144 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) |
| 1145 | { |
| 1146 | int cpu; |
| 1147 | |
| 1148 | for (cpu = *idx; cpu < NR_CPUS; ++cpu) { |
| 1149 | if (!cpu_possible(cpu)) |
| 1150 | continue; |
| 1151 | *idx = cpu + 1; |
| 1152 | return &per_cpu(avc_cache_stats, cpu); |
| 1153 | } |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
| 1157 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) |
| 1158 | { |
| 1159 | loff_t n = *pos - 1; |
| 1160 | |
| 1161 | if (*pos == 0) |
| 1162 | return SEQ_START_TOKEN; |
| 1163 | |
| 1164 | return sel_avc_get_stat_idx(&n); |
| 1165 | } |
| 1166 | |
| 1167 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1168 | { |
| 1169 | return sel_avc_get_stat_idx(pos); |
| 1170 | } |
| 1171 | |
| 1172 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) |
| 1173 | { |
| 1174 | struct avc_cache_stats *st = v; |
| 1175 | |
| 1176 | if (v == SEQ_START_TOKEN) |
| 1177 | seq_printf(seq, "lookups hits misses allocations reclaims " |
| 1178 | "frees\n"); |
| 1179 | else |
| 1180 | seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, |
| 1181 | st->hits, st->misses, st->allocations, |
| 1182 | st->reclaims, st->frees); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) |
| 1187 | { } |
| 1188 | |
| 1189 | static struct seq_operations sel_avc_cache_stats_seq_ops = { |
| 1190 | .start = sel_avc_stats_seq_start, |
| 1191 | .next = sel_avc_stats_seq_next, |
| 1192 | .show = sel_avc_stats_seq_show, |
| 1193 | .stop = sel_avc_stats_seq_stop, |
| 1194 | }; |
| 1195 | |
| 1196 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) |
| 1197 | { |
| 1198 | return seq_open(file, &sel_avc_cache_stats_seq_ops); |
| 1199 | } |
| 1200 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame^] | 1201 | static const struct file_operations sel_avc_cache_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | .open = sel_open_avc_cache_stats, |
| 1203 | .read = seq_read, |
| 1204 | .llseek = seq_lseek, |
| 1205 | .release = seq_release, |
| 1206 | }; |
| 1207 | #endif |
| 1208 | |
| 1209 | static int sel_make_avc_files(struct dentry *dir) |
| 1210 | { |
| 1211 | int i, ret = 0; |
| 1212 | static struct tree_descr files[] = { |
| 1213 | { "cache_threshold", |
| 1214 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, |
| 1215 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, |
| 1216 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1217 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, |
| 1218 | #endif |
| 1219 | }; |
| 1220 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 1221 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | struct inode *inode; |
| 1223 | struct dentry *dentry; |
| 1224 | |
| 1225 | dentry = d_alloc_name(dir, files[i].name); |
| 1226 | if (!dentry) { |
| 1227 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1228 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); |
| 1232 | if (!inode) { |
| 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 | inode->i_fop = files[i].ops; |
| 1237 | d_add(dentry, inode); |
| 1238 | } |
| 1239 | out: |
| 1240 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1243 | static int sel_make_dir(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | { |
| 1245 | int ret = 0; |
| 1246 | struct inode *inode; |
| 1247 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1248 | 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] | 1249 | if (!inode) { |
| 1250 | ret = -ENOMEM; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | inode->i_op = &simple_dir_inode_operations; |
| 1254 | inode->i_fop = &simple_dir_operations; |
James Morris | 40e906f | 2006-03-22 00:09:16 -0800 | [diff] [blame] | 1255 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1256 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | d_add(dentry, inode); |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1258 | /* bump link count on parent directory, too */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1259 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | out: |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | static int sel_fill_super(struct super_block * sb, void * data, int silent) |
| 1265 | { |
| 1266 | int ret; |
| 1267 | struct dentry *dentry; |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1268 | struct inode *inode, *root_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | struct inode_security_struct *isec; |
| 1270 | |
| 1271 | static struct tree_descr selinux_files[] = { |
| 1272 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, |
| 1273 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 1274 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1276 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1277 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1278 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1279 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, |
| 1280 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, |
| 1281 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, |
| 1282 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, |
| 1283 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1284 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 1285 | [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] | 1286 | /* last one */ {""} |
| 1287 | }; |
| 1288 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); |
| 1289 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1290 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1292 | root_inode = sb->s_root->d_inode; |
| 1293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1295 | if (!dentry) { |
| 1296 | ret = -ENOMEM; |
| 1297 | goto err; |
| 1298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1300 | ret = sel_make_dir(root_inode, dentry); |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1301 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1302 | goto err; |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | bool_dir = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
| 1306 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1307 | if (!dentry) { |
| 1308 | ret = -ENOMEM; |
| 1309 | goto err; |
| 1310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1313 | if (!inode) { |
| 1314 | ret = -ENOMEM; |
| 1315 | goto err; |
| 1316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | isec = (struct inode_security_struct*)inode->i_security; |
| 1318 | isec->sid = SECINITSID_DEVNULL; |
| 1319 | isec->sclass = SECCLASS_CHR_FILE; |
| 1320 | isec->initialized = 1; |
| 1321 | |
| 1322 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); |
| 1323 | d_add(dentry, inode); |
| 1324 | selinux_null = dentry; |
| 1325 | |
| 1326 | dentry = d_alloc_name(sb->s_root, "avc"); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1327 | if (!dentry) { |
| 1328 | ret = -ENOMEM; |
| 1329 | goto err; |
| 1330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1332 | ret = sel_make_dir(root_inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1334 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | ret = sel_make_avc_files(dentry); |
| 1337 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1338 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | out: |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1340 | return ret; |
| 1341 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1343 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1346 | static int sel_get_sb(struct file_system_type *fs_type, |
| 1347 | int flags, const char *dev_name, void *data, |
| 1348 | struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1350 | return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | static struct file_system_type sel_fs_type = { |
| 1354 | .name = "selinuxfs", |
| 1355 | .get_sb = sel_get_sb, |
| 1356 | .kill_sb = kill_litter_super, |
| 1357 | }; |
| 1358 | |
| 1359 | struct vfsmount *selinuxfs_mount; |
| 1360 | |
| 1361 | static int __init init_sel_fs(void) |
| 1362 | { |
| 1363 | int err; |
| 1364 | |
| 1365 | if (!selinux_enabled) |
| 1366 | return 0; |
| 1367 | err = register_filesystem(&sel_fs_type); |
| 1368 | if (!err) { |
| 1369 | selinuxfs_mount = kern_mount(&sel_fs_type); |
| 1370 | if (IS_ERR(selinuxfs_mount)) { |
| 1371 | printk(KERN_ERR "selinuxfs: could not mount!\n"); |
| 1372 | err = PTR_ERR(selinuxfs_mount); |
| 1373 | selinuxfs_mount = NULL; |
| 1374 | } |
| 1375 | } |
| 1376 | return err; |
| 1377 | } |
| 1378 | |
| 1379 | __initcall(init_sel_fs); |
| 1380 | |
| 1381 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 1382 | void exit_sel_fs(void) |
| 1383 | { |
| 1384 | unregister_filesystem(&sel_fs_type); |
| 1385 | } |
| 1386 | #endif |