Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Updated: Karl MacMillan <kmacmillan@tresys.com> |
| 2 | * |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 3 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 5 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 6 | * |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 7 | * Added support for the policy capability bitmap |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Copyright (C) 2003 - 2004 Tresys Technology, LLC |
| 11 | * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 12 | * This program is free software; you can redistribute it and/or modify |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * the Free Software Foundation, version 2. |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include <linux/fs.h> |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/security.h> |
| 26 | #include <linux/major.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/percpu.h> |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 29 | #include <linux/audit.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* selinuxfs pseudo filesystem for exporting the security policy API. |
| 33 | Based on the proc code and the fs/nfsd/nfsctl.c code. */ |
| 34 | |
| 35 | #include "flask.h" |
| 36 | #include "avc.h" |
| 37 | #include "avc_ss.h" |
| 38 | #include "security.h" |
| 39 | #include "objsec.h" |
| 40 | #include "conditional.h" |
| 41 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 42 | /* Policy capability filenames */ |
| 43 | static char *policycap_names[] = { |
Eric Paris | b0c636b | 2008-02-28 12:58:40 -0500 | [diff] [blame] | 44 | "network_peer_controls", |
| 45 | "open_perms" |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 49 | |
| 50 | static int __init checkreqprot_setup(char *str) |
| 51 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 52 | unsigned long checkreqprot; |
| 53 | if (!strict_strtoul(str, 0, &checkreqprot)) |
| 54 | selinux_checkreqprot = checkreqprot ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | __setup("checkreqprot=", checkreqprot_setup); |
| 58 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 59 | static DEFINE_MUTEX(sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* global data for booleans */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 62 | static struct dentry *bool_dir; |
| 63 | static int bool_num; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 64 | static char **bool_pending_names; |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 65 | static int *bool_pending_values; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 67 | /* global data for classes */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 68 | static struct dentry *class_dir; |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 69 | static unsigned long last_class_ino; |
| 70 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 71 | /* global data for policy capabilities */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 72 | static struct dentry *policycap_dir; |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | extern void selnl_notify_setenforce(int val); |
| 75 | |
| 76 | /* Check whether a task is allowed to use a security operation. */ |
| 77 | static int task_has_security(struct task_struct *tsk, |
| 78 | u32 perms) |
| 79 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 80 | const struct task_security_struct *tsec; |
| 81 | u32 sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 83 | rcu_read_lock(); |
| 84 | tsec = __task_cred(tsk)->security; |
| 85 | if (tsec) |
| 86 | sid = tsec->sid; |
| 87 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (!tsec) |
| 89 | return -EACCES; |
| 90 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 91 | return avc_has_perm(sid, SECINITSID_SECURITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | SECCLASS_SECURITY, perms, NULL); |
| 93 | } |
| 94 | |
| 95 | enum sel_inos { |
| 96 | SEL_ROOT_INO = 2, |
| 97 | SEL_LOAD, /* load policy */ |
| 98 | SEL_ENFORCE, /* get or set enforcing status */ |
| 99 | SEL_CONTEXT, /* validate context */ |
| 100 | SEL_ACCESS, /* compute access decision */ |
| 101 | SEL_CREATE, /* compute create labeling decision */ |
| 102 | SEL_RELABEL, /* compute relabeling decision */ |
| 103 | SEL_USER, /* compute reachable user contexts */ |
| 104 | SEL_POLICYVERS, /* return policy version for this kernel */ |
| 105 | SEL_COMMIT_BOOLS, /* commit new boolean values */ |
| 106 | SEL_MLS, /* return if MLS policy is enabled */ |
| 107 | SEL_DISABLE, /* disable SELinux until next reboot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | SEL_MEMBER, /* compute polyinstantiation membership decision */ |
| 109 | SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 110 | SEL_COMPAT_NET, /* whether to use old compat network packet controls */ |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 111 | SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ |
| 112 | SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 113 | SEL_INO_NEXT, /* The next inode number to use */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 116 | static unsigned long sel_last_ino = SEL_INO_NEXT - 1; |
| 117 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 118 | #define SEL_INITCON_INO_OFFSET 0x01000000 |
| 119 | #define SEL_BOOL_INO_OFFSET 0x02000000 |
| 120 | #define SEL_CLASS_INO_OFFSET 0x04000000 |
| 121 | #define SEL_POLICYCAP_INO_OFFSET 0x08000000 |
| 122 | #define SEL_INO_MASK 0x00ffffff |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | #define TMPBUFLEN 12 |
| 125 | static ssize_t sel_read_enforce(struct file *filp, char __user *buf, |
| 126 | size_t count, loff_t *ppos) |
| 127 | { |
| 128 | char tmpbuf[TMPBUFLEN]; |
| 129 | ssize_t length; |
| 130 | |
| 131 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); |
| 132 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 133 | } |
| 134 | |
| 135 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 136 | static ssize_t sel_write_enforce(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | size_t count, loff_t *ppos) |
| 138 | |
| 139 | { |
| 140 | char *page; |
| 141 | ssize_t length; |
| 142 | int new_value; |
| 143 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 144 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -ENOMEM; |
| 146 | if (*ppos != 0) { |
| 147 | /* No partial writes. */ |
| 148 | return -EINVAL; |
| 149 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 150 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (!page) |
| 152 | return -ENOMEM; |
| 153 | length = -EFAULT; |
| 154 | if (copy_from_user(page, buf, count)) |
| 155 | goto out; |
| 156 | |
| 157 | length = -EINVAL; |
| 158 | if (sscanf(page, "%d", &new_value) != 1) |
| 159 | goto out; |
| 160 | |
| 161 | if (new_value != selinux_enforcing) { |
| 162 | length = task_has_security(current, SECURITY__SETENFORCE); |
| 163 | if (length) |
| 164 | goto out; |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 165 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 166 | "enforcing=%d old_enforcing=%d auid=%u ses=%u", |
| 167 | new_value, selinux_enforcing, |
| 168 | audit_get_loginuid(current), |
| 169 | audit_get_sessionid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | selinux_enforcing = new_value; |
| 171 | if (selinux_enforcing) |
| 172 | avc_ss_reset(0); |
| 173 | selnl_notify_setenforce(selinux_enforcing); |
| 174 | } |
| 175 | length = count; |
| 176 | out: |
| 177 | free_page((unsigned long) page); |
| 178 | return length; |
| 179 | } |
| 180 | #else |
| 181 | #define sel_write_enforce NULL |
| 182 | #endif |
| 183 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 184 | static const struct file_operations sel_enforce_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | .read = sel_read_enforce, |
| 186 | .write = sel_write_enforce, |
| 187 | }; |
| 188 | |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 189 | static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, |
| 190 | size_t count, loff_t *ppos) |
| 191 | { |
| 192 | char tmpbuf[TMPBUFLEN]; |
| 193 | ssize_t length; |
| 194 | ino_t ino = filp->f_path.dentry->d_inode->i_ino; |
| 195 | int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? |
| 196 | security_get_reject_unknown() : !security_get_allow_unknown(); |
| 197 | |
| 198 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); |
| 199 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 200 | } |
| 201 | |
| 202 | static const struct file_operations sel_handle_unknown_ops = { |
| 203 | .read = sel_read_handle_unknown, |
| 204 | }; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 207 | static ssize_t sel_write_disable(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | size_t count, loff_t *ppos) |
| 209 | |
| 210 | { |
| 211 | char *page; |
| 212 | ssize_t length; |
| 213 | int new_value; |
| 214 | extern int selinux_disable(void); |
| 215 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 216 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -ENOMEM; |
| 218 | if (*ppos != 0) { |
| 219 | /* No partial writes. */ |
| 220 | return -EINVAL; |
| 221 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 222 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (!page) |
| 224 | return -ENOMEM; |
| 225 | length = -EFAULT; |
| 226 | if (copy_from_user(page, buf, count)) |
| 227 | goto out; |
| 228 | |
| 229 | length = -EINVAL; |
| 230 | if (sscanf(page, "%d", &new_value) != 1) |
| 231 | goto out; |
| 232 | |
| 233 | if (new_value) { |
| 234 | length = selinux_disable(); |
| 235 | if (length < 0) |
| 236 | goto out; |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 237 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 238 | "selinux=0 auid=%u ses=%u", |
| 239 | audit_get_loginuid(current), |
| 240 | audit_get_sessionid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | length = count; |
| 244 | out: |
| 245 | free_page((unsigned long) page); |
| 246 | return length; |
| 247 | } |
| 248 | #else |
| 249 | #define sel_write_disable NULL |
| 250 | #endif |
| 251 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 252 | static const struct file_operations sel_disable_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | .write = sel_write_disable, |
| 254 | }; |
| 255 | |
| 256 | static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 257 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | char tmpbuf[TMPBUFLEN]; |
| 260 | ssize_t length; |
| 261 | |
| 262 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); |
| 263 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 264 | } |
| 265 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 266 | static const struct file_operations sel_policyvers_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | .read = sel_read_policyvers, |
| 268 | }; |
| 269 | |
| 270 | /* declaration for sel_write_load */ |
| 271 | static int sel_make_bools(void); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 272 | static int sel_make_classes(void); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 273 | static int sel_make_policycap(void); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 274 | |
| 275 | /* declaration for sel_make_class_dirs */ |
| 276 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, |
| 277 | unsigned long *ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | static ssize_t sel_read_mls(struct file *filp, char __user *buf, |
| 280 | size_t count, loff_t *ppos) |
| 281 | { |
| 282 | char tmpbuf[TMPBUFLEN]; |
| 283 | ssize_t length; |
| 284 | |
| 285 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled); |
| 286 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 287 | } |
| 288 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 289 | static const struct file_operations sel_mls_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | .read = sel_read_mls, |
| 291 | }; |
| 292 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 293 | static ssize_t sel_write_load(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | size_t count, loff_t *ppos) |
| 295 | |
| 296 | { |
| 297 | int ret; |
| 298 | ssize_t length; |
| 299 | void *data = NULL; |
| 300 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 301 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | length = task_has_security(current, SECURITY__LOAD_POLICY); |
| 304 | if (length) |
| 305 | goto out; |
| 306 | |
| 307 | if (*ppos != 0) { |
| 308 | /* No partial writes. */ |
| 309 | length = -EINVAL; |
| 310 | goto out; |
| 311 | } |
| 312 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 313 | if ((count > 64 * 1024 * 1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | || (data = vmalloc(count)) == NULL) { |
| 315 | length = -ENOMEM; |
| 316 | goto out; |
| 317 | } |
| 318 | |
| 319 | length = -EFAULT; |
| 320 | if (copy_from_user(data, buf, count) != 0) |
| 321 | goto out; |
| 322 | |
| 323 | length = security_load_policy(data, count); |
| 324 | if (length) |
| 325 | goto out; |
| 326 | |
| 327 | ret = sel_make_bools(); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 328 | if (ret) { |
| 329 | length = ret; |
| 330 | goto out1; |
| 331 | } |
| 332 | |
| 333 | ret = sel_make_classes(); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 334 | if (ret) { |
| 335 | length = ret; |
| 336 | goto out1; |
| 337 | } |
| 338 | |
| 339 | ret = sel_make_policycap(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | if (ret) |
| 341 | length = ret; |
| 342 | else |
| 343 | length = count; |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 344 | |
| 345 | out1: |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 346 | audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 347 | "policy loaded auid=%u ses=%u", |
| 348 | audit_get_loginuid(current), |
| 349 | audit_get_sessionid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 351 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | vfree(data); |
| 353 | return length; |
| 354 | } |
| 355 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 356 | static const struct file_operations sel_load_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | .write = sel_write_load, |
| 358 | }; |
| 359 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 360 | 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] | 361 | { |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 362 | char *canon; |
| 363 | u32 sid, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | ssize_t length; |
| 365 | |
| 366 | length = task_has_security(current, SECURITY__CHECK_CONTEXT); |
| 367 | if (length) |
| 368 | return length; |
| 369 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 370 | length = security_context_to_sid(buf, size, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (length < 0) |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 372 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 374 | length = security_sid_to_context(sid, &canon, &len); |
| 375 | if (length < 0) |
| 376 | return length; |
| 377 | |
| 378 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 379 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 380 | "payload max\n", __func__, len); |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 381 | length = -ERANGE; |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | memcpy(buf, canon, len); |
| 386 | length = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | out: |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 388 | kfree(canon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return length; |
| 390 | } |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, |
| 393 | size_t count, loff_t *ppos) |
| 394 | { |
| 395 | char tmpbuf[TMPBUFLEN]; |
| 396 | ssize_t length; |
| 397 | |
| 398 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); |
| 399 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 400 | } |
| 401 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 402 | static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | size_t count, loff_t *ppos) |
| 404 | { |
| 405 | char *page; |
| 406 | ssize_t length; |
| 407 | unsigned int new_value; |
| 408 | |
| 409 | length = task_has_security(current, SECURITY__SETCHECKREQPROT); |
| 410 | if (length) |
| 411 | return length; |
| 412 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 413 | if (count >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return -ENOMEM; |
| 415 | if (*ppos != 0) { |
| 416 | /* No partial writes. */ |
| 417 | return -EINVAL; |
| 418 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 419 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!page) |
| 421 | return -ENOMEM; |
| 422 | length = -EFAULT; |
| 423 | if (copy_from_user(page, buf, count)) |
| 424 | goto out; |
| 425 | |
| 426 | length = -EINVAL; |
| 427 | if (sscanf(page, "%u", &new_value) != 1) |
| 428 | goto out; |
| 429 | |
| 430 | selinux_checkreqprot = new_value ? 1 : 0; |
| 431 | length = count; |
| 432 | out: |
| 433 | free_page((unsigned long) page); |
| 434 | return length; |
| 435 | } |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 436 | static const struct file_operations sel_checkreqprot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | .read = sel_read_checkreqprot, |
| 438 | .write = sel_write_checkreqprot, |
| 439 | }; |
| 440 | |
| 441 | /* |
| 442 | * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c |
| 443 | */ |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 444 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size); |
| 445 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size); |
| 446 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); |
| 447 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size); |
| 448 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | static ssize_t (*write_op[])(struct file *, char *, size_t) = { |
| 451 | [SEL_ACCESS] = sel_write_access, |
| 452 | [SEL_CREATE] = sel_write_create, |
| 453 | [SEL_RELABEL] = sel_write_relabel, |
| 454 | [SEL_USER] = sel_write_user, |
| 455 | [SEL_MEMBER] = sel_write_member, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 456 | [SEL_CONTEXT] = sel_write_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) |
| 460 | { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 461 | ino_t ino = file->f_path.dentry->d_inode->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | char *data; |
| 463 | ssize_t rv; |
| 464 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 465 | if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -EINVAL; |
| 467 | |
| 468 | data = simple_transaction_get(file, buf, size); |
| 469 | if (IS_ERR(data)) |
| 470 | return PTR_ERR(data); |
| 471 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 472 | rv = write_op[ino](file, data, size); |
| 473 | if (rv > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | simple_transaction_set(file, rv); |
| 475 | rv = size; |
| 476 | } |
| 477 | return rv; |
| 478 | } |
| 479 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 480 | static const struct file_operations transaction_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | .write = selinux_transaction_write, |
| 482 | .read = simple_transaction_read, |
| 483 | .release = simple_transaction_release, |
| 484 | }; |
| 485 | |
| 486 | /* |
| 487 | * payload - write methods |
| 488 | * If the method has a response, the response should be put in buf, |
| 489 | * and the length returned. Otherwise return 0 or and -error. |
| 490 | */ |
| 491 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 492 | static ssize_t sel_write_access(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | char *scon, *tcon; |
| 495 | u32 ssid, tsid; |
| 496 | u16 tclass; |
| 497 | u32 req; |
| 498 | struct av_decision avd; |
| 499 | ssize_t length; |
| 500 | |
| 501 | length = task_has_security(current, SECURITY__COMPUTE_AV); |
| 502 | if (length) |
| 503 | return length; |
| 504 | |
| 505 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 506 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (!scon) |
| 508 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 510 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | if (!tcon) |
| 512 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | length = -EINVAL; |
| 515 | if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4) |
| 516 | goto out2; |
| 517 | |
| 518 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 519 | if (length < 0) |
| 520 | goto out2; |
| 521 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 522 | if (length < 0) |
| 523 | goto out2; |
| 524 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 525 | length = security_compute_av_user(ssid, tsid, tclass, req, &avd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (length < 0) |
| 527 | goto out2; |
| 528 | |
| 529 | length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, |
KaiGai Kohei | 8a6f83a | 2009-04-01 10:07:57 +0900 | [diff] [blame] | 530 | "%x %x %x %x %u %x", |
Eric Paris | f1c6381 | 2009-02-12 14:50:54 -0500 | [diff] [blame] | 531 | avd.allowed, 0xffffffff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | avd.auditallow, avd.auditdeny, |
KaiGai Kohei | 8a6f83a | 2009-04-01 10:07:57 +0900 | [diff] [blame] | 533 | avd.seqno, avd.flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | out2: |
| 535 | kfree(tcon); |
| 536 | out: |
| 537 | kfree(scon); |
| 538 | return length; |
| 539 | } |
| 540 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 541 | static ssize_t sel_write_create(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | char *scon, *tcon; |
| 544 | u32 ssid, tsid, newsid; |
| 545 | u16 tclass; |
| 546 | ssize_t length; |
| 547 | char *newcon; |
| 548 | u32 len; |
| 549 | |
| 550 | length = task_has_security(current, SECURITY__COMPUTE_CREATE); |
| 551 | if (length) |
| 552 | return length; |
| 553 | |
| 554 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 555 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (!scon) |
| 557 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 559 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (!tcon) |
| 561 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | length = -EINVAL; |
| 564 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 565 | goto out2; |
| 566 | |
| 567 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 568 | if (length < 0) |
| 569 | goto out2; |
| 570 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 571 | if (length < 0) |
| 572 | goto out2; |
| 573 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 574 | length = security_transition_sid_user(ssid, tsid, tclass, &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (length < 0) |
| 576 | goto out2; |
| 577 | |
| 578 | length = security_sid_to_context(newsid, &newcon, &len); |
| 579 | if (length < 0) |
| 580 | goto out2; |
| 581 | |
| 582 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 583 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 584 | "payload max\n", __func__, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | length = -ERANGE; |
| 586 | goto out3; |
| 587 | } |
| 588 | |
| 589 | memcpy(buf, newcon, len); |
| 590 | length = len; |
| 591 | out3: |
| 592 | kfree(newcon); |
| 593 | out2: |
| 594 | kfree(tcon); |
| 595 | out: |
| 596 | kfree(scon); |
| 597 | return length; |
| 598 | } |
| 599 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 600 | static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
| 602 | char *scon, *tcon; |
| 603 | u32 ssid, tsid, newsid; |
| 604 | u16 tclass; |
| 605 | ssize_t length; |
| 606 | char *newcon; |
| 607 | u32 len; |
| 608 | |
| 609 | length = task_has_security(current, SECURITY__COMPUTE_RELABEL); |
| 610 | if (length) |
| 611 | return length; |
| 612 | |
| 613 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 614 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (!scon) |
| 616 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 618 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (!tcon) |
| 620 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | length = -EINVAL; |
| 623 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 624 | goto out2; |
| 625 | |
| 626 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 627 | if (length < 0) |
| 628 | goto out2; |
| 629 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 630 | if (length < 0) |
| 631 | goto out2; |
| 632 | |
| 633 | length = security_change_sid(ssid, tsid, tclass, &newsid); |
| 634 | if (length < 0) |
| 635 | goto out2; |
| 636 | |
| 637 | length = security_sid_to_context(newsid, &newcon, &len); |
| 638 | if (length < 0) |
| 639 | goto out2; |
| 640 | |
| 641 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
| 642 | length = -ERANGE; |
| 643 | goto out3; |
| 644 | } |
| 645 | |
| 646 | memcpy(buf, newcon, len); |
| 647 | length = len; |
| 648 | out3: |
| 649 | kfree(newcon); |
| 650 | out2: |
| 651 | kfree(tcon); |
| 652 | out: |
| 653 | kfree(scon); |
| 654 | return length; |
| 655 | } |
| 656 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 657 | static ssize_t sel_write_user(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
| 659 | char *con, *user, *ptr; |
| 660 | u32 sid, *sids; |
| 661 | ssize_t length; |
| 662 | char *newcon; |
| 663 | int i, rc; |
| 664 | u32 len, nsids; |
| 665 | |
| 666 | length = task_has_security(current, SECURITY__COMPUTE_USER); |
| 667 | if (length) |
| 668 | return length; |
| 669 | |
| 670 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 671 | con = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (!con) |
| 673 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 675 | user = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (!user) |
| 677 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
| 679 | length = -EINVAL; |
| 680 | if (sscanf(buf, "%s %s", con, user) != 2) |
| 681 | goto out2; |
| 682 | |
| 683 | length = security_context_to_sid(con, strlen(con)+1, &sid); |
| 684 | if (length < 0) |
| 685 | goto out2; |
| 686 | |
| 687 | length = security_get_user_sids(sid, user, &sids, &nsids); |
| 688 | if (length < 0) |
| 689 | goto out2; |
| 690 | |
| 691 | length = sprintf(buf, "%u", nsids) + 1; |
| 692 | ptr = buf + length; |
| 693 | for (i = 0; i < nsids; i++) { |
| 694 | rc = security_sid_to_context(sids[i], &newcon, &len); |
| 695 | if (rc) { |
| 696 | length = rc; |
| 697 | goto out3; |
| 698 | } |
| 699 | if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { |
| 700 | kfree(newcon); |
| 701 | length = -ERANGE; |
| 702 | goto out3; |
| 703 | } |
| 704 | memcpy(ptr, newcon, len); |
| 705 | kfree(newcon); |
| 706 | ptr += len; |
| 707 | length += len; |
| 708 | } |
| 709 | out3: |
| 710 | kfree(sids); |
| 711 | out2: |
| 712 | kfree(user); |
| 713 | out: |
| 714 | kfree(con); |
| 715 | return length; |
| 716 | } |
| 717 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 718 | static ssize_t sel_write_member(struct file *file, char *buf, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { |
| 720 | char *scon, *tcon; |
| 721 | u32 ssid, tsid, newsid; |
| 722 | u16 tclass; |
| 723 | ssize_t length; |
| 724 | char *newcon; |
| 725 | u32 len; |
| 726 | |
| 727 | length = task_has_security(current, SECURITY__COMPUTE_MEMBER); |
| 728 | if (length) |
| 729 | return length; |
| 730 | |
| 731 | length = -ENOMEM; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 732 | scon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | if (!scon) |
| 734 | return length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 736 | tcon = kzalloc(size+1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (!tcon) |
| 738 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | length = -EINVAL; |
| 741 | if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) |
| 742 | goto out2; |
| 743 | |
| 744 | length = security_context_to_sid(scon, strlen(scon)+1, &ssid); |
| 745 | if (length < 0) |
| 746 | goto out2; |
| 747 | length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); |
| 748 | if (length < 0) |
| 749 | goto out2; |
| 750 | |
| 751 | length = security_member_sid(ssid, tsid, tclass, &newsid); |
| 752 | if (length < 0) |
| 753 | goto out2; |
| 754 | |
| 755 | length = security_sid_to_context(newsid, &newcon, &len); |
| 756 | if (length < 0) |
| 757 | goto out2; |
| 758 | |
| 759 | if (len > SIMPLE_TRANSACTION_LIMIT) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 760 | printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " |
| 761 | "payload max\n", __func__, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | length = -ERANGE; |
| 763 | goto out3; |
| 764 | } |
| 765 | |
| 766 | memcpy(buf, newcon, len); |
| 767 | length = len; |
| 768 | out3: |
| 769 | kfree(newcon); |
| 770 | out2: |
| 771 | kfree(tcon); |
| 772 | out: |
| 773 | kfree(scon); |
| 774 | return length; |
| 775 | } |
| 776 | |
| 777 | static struct inode *sel_make_inode(struct super_block *sb, int mode) |
| 778 | { |
| 779 | struct inode *ret = new_inode(sb); |
| 780 | |
| 781 | if (ret) { |
| 782 | ret->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 784 | } |
| 785 | return ret; |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 795 | struct inode *inode = filep->f_path.dentry->d_inode; |
| 796 | unsigned index = inode->i_ino & SEL_INO_MASK; |
| 797 | const char *name = filep->f_path.dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 799 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 801 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) { |
| 802 | ret = -EINVAL; |
| 803 | goto out; |
| 804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 806 | page = (char *)get_zeroed_page(GFP_KERNEL); |
| 807 | if (!page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | ret = -ENOMEM; |
| 809 | goto out; |
| 810 | } |
| 811 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 812 | cur_enforcing = security_get_bool_value(index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | if (cur_enforcing < 0) { |
| 814 | ret = cur_enforcing; |
| 815 | goto out; |
| 816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 818 | bool_pending_values[index]); |
Stephen Smalley | 68bdcf2 | 2006-03-22 00:09:15 -0800 | [diff] [blame] | 819 | ret = simple_read_from_buffer(buf, count, ppos, page, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 821 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (page) |
| 823 | free_page((unsigned long)page); |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static ssize_t sel_write_bool(struct file *filep, const char __user *buf, |
| 828 | size_t count, loff_t *ppos) |
| 829 | { |
| 830 | char *page = NULL; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 831 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | int new_value; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 833 | struct inode *inode = filep->f_path.dentry->d_inode; |
| 834 | unsigned index = inode->i_ino & SEL_INO_MASK; |
| 835 | const char *name = filep->f_path.dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 837 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
| 839 | length = task_has_security(current, SECURITY__SETBOOL); |
| 840 | if (length) |
| 841 | goto out; |
| 842 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 843 | if (index >= bool_num || strcmp(name, bool_pending_names[index])) { |
| 844 | length = -EINVAL; |
| 845 | goto out; |
| 846 | } |
| 847 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 848 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | length = -ENOMEM; |
| 850 | goto out; |
| 851 | } |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (*ppos != 0) { |
| 854 | /* No partial writes. */ |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 855 | length = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | goto out; |
| 857 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 858 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (!page) { |
| 860 | length = -ENOMEM; |
| 861 | goto out; |
| 862 | } |
| 863 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 864 | length = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (copy_from_user(page, buf, count)) |
| 866 | goto out; |
| 867 | |
| 868 | length = -EINVAL; |
| 869 | if (sscanf(page, "%d", &new_value) != 1) |
| 870 | goto out; |
| 871 | |
| 872 | if (new_value) |
| 873 | new_value = 1; |
| 874 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 875 | bool_pending_values[index] = new_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | length = count; |
| 877 | |
| 878 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 879 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | if (page) |
| 881 | free_page((unsigned long) page); |
| 882 | return length; |
| 883 | } |
| 884 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 885 | static const struct file_operations sel_bool_ops = { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 886 | .read = sel_read_bool, |
| 887 | .write = sel_write_bool, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | }; |
| 889 | |
| 890 | static ssize_t sel_commit_bools_write(struct file *filep, |
| 891 | const char __user *buf, |
| 892 | size_t count, loff_t *ppos) |
| 893 | { |
| 894 | char *page = NULL; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 895 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | int new_value; |
| 897 | |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 898 | mutex_lock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
| 900 | length = task_has_security(current, SECURITY__SETBOOL); |
| 901 | if (length) |
| 902 | goto out; |
| 903 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 904 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | length = -ENOMEM; |
| 906 | goto out; |
| 907 | } |
| 908 | if (*ppos != 0) { |
| 909 | /* No partial writes. */ |
| 910 | goto out; |
| 911 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 912 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | if (!page) { |
| 914 | length = -ENOMEM; |
| 915 | goto out; |
| 916 | } |
| 917 | |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 918 | length = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (copy_from_user(page, buf, count)) |
| 920 | goto out; |
| 921 | |
| 922 | length = -EINVAL; |
| 923 | if (sscanf(page, "%d", &new_value) != 1) |
| 924 | goto out; |
| 925 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 926 | if (new_value && bool_pending_values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | security_set_bools(bool_num, bool_pending_values); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | length = count; |
| 930 | |
| 931 | out: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 932 | mutex_unlock(&sel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | if (page) |
| 934 | free_page((unsigned long) page); |
| 935 | return length; |
| 936 | } |
| 937 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 938 | static const struct file_operations sel_commit_bools_ops = { |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 939 | .write = sel_commit_bools_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | }; |
| 941 | |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 942 | static void sel_remove_entries(struct dentry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Stephen Smalley | 0955dc0 | 2007-11-21 09:01:36 -0500 | [diff] [blame] | 944 | struct list_head *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | #define BOOL_DIR_NAME "booleans" |
| 967 | |
| 968 | static int sel_make_bools(void) |
| 969 | { |
| 970 | int i, ret = 0; |
| 971 | ssize_t len; |
| 972 | struct dentry *dentry = NULL; |
| 973 | struct dentry *dir = bool_dir; |
| 974 | struct inode *inode = NULL; |
| 975 | struct inode_security_struct *isec; |
| 976 | char **names = NULL, *page; |
| 977 | int num; |
| 978 | int *values = NULL; |
| 979 | u32 sid; |
| 980 | |
| 981 | /* remove any existing files */ |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 982 | kfree(bool_pending_names); |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 983 | kfree(bool_pending_values); |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 984 | bool_pending_names = NULL; |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 985 | bool_pending_values = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 987 | sel_remove_entries(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 989 | page = (char *)get_zeroed_page(GFP_KERNEL); |
| 990 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return -ENOMEM; |
| 992 | |
| 993 | ret = security_get_bools(&num, &names, &values); |
| 994 | if (ret != 0) |
| 995 | goto out; |
| 996 | |
| 997 | for (i = 0; i < num; i++) { |
| 998 | dentry = d_alloc_name(dir, names[i]); |
| 999 | if (!dentry) { |
| 1000 | ret = -ENOMEM; |
| 1001 | goto err; |
| 1002 | } |
| 1003 | inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); |
| 1004 | if (!inode) { |
| 1005 | ret = -ENOMEM; |
| 1006 | goto err; |
| 1007 | } |
| 1008 | |
| 1009 | len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); |
| 1010 | if (len < 0) { |
| 1011 | ret = -EINVAL; |
| 1012 | goto err; |
| 1013 | } else if (len >= PAGE_SIZE) { |
| 1014 | ret = -ENAMETOOLONG; |
| 1015 | goto err; |
| 1016 | } |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1017 | isec = (struct inode_security_struct *)inode->i_security; |
| 1018 | ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid); |
| 1019 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | goto err; |
| 1021 | isec->sid = sid; |
| 1022 | isec->initialized = 1; |
| 1023 | inode->i_fop = &sel_bool_ops; |
James Carter | bce34bc | 2007-04-04 16:18:50 -0400 | [diff] [blame] | 1024 | inode->i_ino = i|SEL_BOOL_INO_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | d_add(dentry, inode); |
| 1026 | } |
| 1027 | bool_num = num; |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1028 | bool_pending_names = names; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | bool_pending_values = values; |
| 1030 | out: |
| 1031 | free_page((unsigned long)page); |
Stephen Smalley | d313f9483 | 2007-11-26 11:12:53 -0500 | [diff] [blame] | 1032 | return ret; |
| 1033 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | if (names) { |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 1035 | for (i = 0; i < num; i++) |
| 1036 | kfree(names[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | kfree(names); |
| 1038 | } |
Davi Arnaut | 20c19e4 | 2005-10-23 12:57:16 -0700 | [diff] [blame] | 1039 | kfree(values); |
Christopher J. PeBenito | 0c92d7c | 2007-05-23 09:12:07 -0400 | [diff] [blame] | 1040 | sel_remove_entries(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | ret = -ENOMEM; |
| 1042 | goto out; |
| 1043 | } |
| 1044 | |
| 1045 | #define NULL_FILE_NAME "null" |
| 1046 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1047 | struct dentry *selinux_null; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, |
| 1050 | size_t count, loff_t *ppos) |
| 1051 | { |
| 1052 | char tmpbuf[TMPBUFLEN]; |
| 1053 | ssize_t length; |
| 1054 | |
| 1055 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); |
| 1056 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1057 | } |
| 1058 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1059 | static ssize_t sel_write_avc_cache_threshold(struct file *file, |
| 1060 | const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | size_t count, loff_t *ppos) |
| 1062 | |
| 1063 | { |
| 1064 | char *page; |
| 1065 | ssize_t ret; |
| 1066 | int new_value; |
| 1067 | |
Davi Arnaut | bfd5162 | 2005-10-30 14:59:24 -0800 | [diff] [blame] | 1068 | if (count >= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | ret = -ENOMEM; |
| 1070 | goto out; |
| 1071 | } |
| 1072 | |
| 1073 | if (*ppos != 0) { |
| 1074 | /* No partial writes. */ |
| 1075 | ret = -EINVAL; |
| 1076 | goto out; |
| 1077 | } |
| 1078 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1079 | page = (char *)get_zeroed_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | if (!page) { |
| 1081 | ret = -ENOMEM; |
| 1082 | goto out; |
| 1083 | } |
| 1084 | |
| 1085 | if (copy_from_user(page, buf, count)) { |
| 1086 | ret = -EFAULT; |
| 1087 | goto out_free; |
| 1088 | } |
| 1089 | |
| 1090 | if (sscanf(page, "%u", &new_value) != 1) { |
| 1091 | ret = -EINVAL; |
| 1092 | goto out; |
| 1093 | } |
| 1094 | |
| 1095 | if (new_value != avc_cache_threshold) { |
| 1096 | ret = task_has_security(current, SECURITY__SETSECPARAM); |
| 1097 | if (ret) |
| 1098 | goto out_free; |
| 1099 | avc_cache_threshold = new_value; |
| 1100 | } |
| 1101 | ret = count; |
| 1102 | out_free: |
| 1103 | free_page((unsigned long)page); |
| 1104 | out: |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, |
| 1109 | size_t count, loff_t *ppos) |
| 1110 | { |
| 1111 | char *page; |
| 1112 | ssize_t ret = 0; |
| 1113 | |
| 1114 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1115 | if (!page) { |
| 1116 | ret = -ENOMEM; |
| 1117 | goto out; |
| 1118 | } |
| 1119 | ret = avc_get_hash_stats(page); |
| 1120 | if (ret >= 0) |
| 1121 | ret = simple_read_from_buffer(buf, count, ppos, page, ret); |
| 1122 | free_page((unsigned long)page); |
| 1123 | out: |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1127 | static const struct file_operations sel_avc_cache_threshold_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | .read = sel_read_avc_cache_threshold, |
| 1129 | .write = sel_write_avc_cache_threshold, |
| 1130 | }; |
| 1131 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1132 | static const struct file_operations sel_avc_hash_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | .read = sel_read_avc_hash_stats, |
| 1134 | }; |
| 1135 | |
| 1136 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1137 | static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) |
| 1138 | { |
| 1139 | int cpu; |
| 1140 | |
Rusty Russell | 4f4b6c1 | 2009-01-01 10:12:15 +1030 | [diff] [blame] | 1141 | for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | if (!cpu_possible(cpu)) |
| 1143 | continue; |
| 1144 | *idx = cpu + 1; |
| 1145 | return &per_cpu(avc_cache_stats, cpu); |
| 1146 | } |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
| 1150 | static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) |
| 1151 | { |
| 1152 | loff_t n = *pos - 1; |
| 1153 | |
| 1154 | if (*pos == 0) |
| 1155 | return SEQ_START_TOKEN; |
| 1156 | |
| 1157 | return sel_avc_get_stat_idx(&n); |
| 1158 | } |
| 1159 | |
| 1160 | static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1161 | { |
| 1162 | return sel_avc_get_stat_idx(pos); |
| 1163 | } |
| 1164 | |
| 1165 | static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) |
| 1166 | { |
| 1167 | struct avc_cache_stats *st = v; |
| 1168 | |
| 1169 | if (v == SEQ_START_TOKEN) |
| 1170 | seq_printf(seq, "lookups hits misses allocations reclaims " |
| 1171 | "frees\n"); |
| 1172 | else |
| 1173 | seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, |
| 1174 | st->hits, st->misses, st->allocations, |
| 1175 | st->reclaims, st->frees); |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) |
| 1180 | { } |
| 1181 | |
Jan Engelhardt | 1996a10 | 2008-01-23 00:02:58 +0100 | [diff] [blame] | 1182 | static const struct seq_operations sel_avc_cache_stats_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | .start = sel_avc_stats_seq_start, |
| 1184 | .next = sel_avc_stats_seq_next, |
| 1185 | .show = sel_avc_stats_seq_show, |
| 1186 | .stop = sel_avc_stats_seq_stop, |
| 1187 | }; |
| 1188 | |
| 1189 | static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) |
| 1190 | { |
| 1191 | return seq_open(file, &sel_avc_cache_stats_seq_ops); |
| 1192 | } |
| 1193 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1194 | static const struct file_operations sel_avc_cache_stats_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | .open = sel_open_avc_cache_stats, |
| 1196 | .read = seq_read, |
| 1197 | .llseek = seq_lseek, |
| 1198 | .release = seq_release, |
| 1199 | }; |
| 1200 | #endif |
| 1201 | |
| 1202 | static int sel_make_avc_files(struct dentry *dir) |
| 1203 | { |
| 1204 | int i, ret = 0; |
| 1205 | static struct tree_descr files[] = { |
| 1206 | { "cache_threshold", |
| 1207 | &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, |
| 1208 | { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, |
| 1209 | #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS |
| 1210 | { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, |
| 1211 | #endif |
| 1212 | }; |
| 1213 | |
Nicolas Kaiser | 6e20a64 | 2006-01-06 00:11:22 -0800 | [diff] [blame] | 1214 | for (i = 0; i < ARRAY_SIZE(files); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | struct inode *inode; |
| 1216 | struct dentry *dentry; |
| 1217 | |
| 1218 | dentry = d_alloc_name(dir, files[i].name); |
| 1219 | if (!dentry) { |
| 1220 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1221 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); |
| 1225 | if (!inode) { |
| 1226 | ret = -ENOMEM; |
James Morris | d6aafa6 | 2006-03-22 00:09:19 -0800 | [diff] [blame] | 1227 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | inode->i_fop = files[i].ops; |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 1230 | inode->i_ino = ++sel_last_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | d_add(dentry, inode); |
| 1232 | } |
| 1233 | out: |
| 1234 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1237 | static ssize_t sel_read_initcon(struct file *file, char __user *buf, |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1238 | size_t count, loff_t *ppos) |
| 1239 | { |
| 1240 | struct inode *inode; |
| 1241 | char *con; |
| 1242 | u32 sid, len; |
| 1243 | ssize_t ret; |
| 1244 | |
| 1245 | inode = file->f_path.dentry->d_inode; |
| 1246 | sid = inode->i_ino&SEL_INO_MASK; |
| 1247 | ret = security_sid_to_context(sid, &con, &len); |
| 1248 | if (ret < 0) |
| 1249 | return ret; |
| 1250 | |
| 1251 | ret = simple_read_from_buffer(buf, count, ppos, con, len); |
| 1252 | kfree(con); |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | static const struct file_operations sel_initcon_ops = { |
| 1257 | .read = sel_read_initcon, |
| 1258 | }; |
| 1259 | |
| 1260 | static int sel_make_initcon_files(struct dentry *dir) |
| 1261 | { |
| 1262 | int i, ret = 0; |
| 1263 | |
| 1264 | for (i = 1; i <= SECINITSID_NUM; i++) { |
| 1265 | struct inode *inode; |
| 1266 | struct dentry *dentry; |
| 1267 | dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); |
| 1268 | if (!dentry) { |
| 1269 | ret = -ENOMEM; |
| 1270 | goto out; |
| 1271 | } |
| 1272 | |
| 1273 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1274 | if (!inode) { |
| 1275 | ret = -ENOMEM; |
| 1276 | goto out; |
| 1277 | } |
| 1278 | inode->i_fop = &sel_initcon_ops; |
| 1279 | inode->i_ino = i|SEL_INITCON_INO_OFFSET; |
| 1280 | d_add(dentry, inode); |
| 1281 | } |
| 1282 | out: |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1286 | static inline unsigned int sel_div(unsigned long a, unsigned long b) |
| 1287 | { |
| 1288 | return a / b - (a % b < 0); |
| 1289 | } |
| 1290 | |
| 1291 | static inline unsigned long sel_class_to_ino(u16 class) |
| 1292 | { |
| 1293 | return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; |
| 1294 | } |
| 1295 | |
| 1296 | static inline u16 sel_ino_to_class(unsigned long ino) |
| 1297 | { |
| 1298 | return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1); |
| 1299 | } |
| 1300 | |
| 1301 | static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) |
| 1302 | { |
| 1303 | return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; |
| 1304 | } |
| 1305 | |
| 1306 | static inline u32 sel_ino_to_perm(unsigned long ino) |
| 1307 | { |
| 1308 | return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); |
| 1309 | } |
| 1310 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1311 | static ssize_t sel_read_class(struct file *file, char __user *buf, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1312 | size_t count, loff_t *ppos) |
| 1313 | { |
| 1314 | ssize_t rc, len; |
| 1315 | char *page; |
| 1316 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; |
| 1317 | |
| 1318 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1319 | if (!page) { |
| 1320 | rc = -ENOMEM; |
| 1321 | goto out; |
| 1322 | } |
| 1323 | |
| 1324 | len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino)); |
| 1325 | rc = simple_read_from_buffer(buf, count, ppos, page, len); |
| 1326 | free_page((unsigned long)page); |
| 1327 | out: |
| 1328 | return rc; |
| 1329 | } |
| 1330 | |
| 1331 | static const struct file_operations sel_class_ops = { |
| 1332 | .read = sel_read_class, |
| 1333 | }; |
| 1334 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1335 | static ssize_t sel_read_perm(struct file *file, char __user *buf, |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1336 | size_t count, loff_t *ppos) |
| 1337 | { |
| 1338 | ssize_t rc, len; |
| 1339 | char *page; |
| 1340 | unsigned long ino = file->f_path.dentry->d_inode->i_ino; |
| 1341 | |
| 1342 | page = (char *)__get_free_page(GFP_KERNEL); |
| 1343 | if (!page) { |
| 1344 | rc = -ENOMEM; |
| 1345 | goto out; |
| 1346 | } |
| 1347 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1348 | len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino)); |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1349 | rc = simple_read_from_buffer(buf, count, ppos, page, len); |
| 1350 | free_page((unsigned long)page); |
| 1351 | out: |
| 1352 | return rc; |
| 1353 | } |
| 1354 | |
| 1355 | static const struct file_operations sel_perm_ops = { |
| 1356 | .read = sel_read_perm, |
| 1357 | }; |
| 1358 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1359 | static ssize_t sel_read_policycap(struct file *file, char __user *buf, |
| 1360 | size_t count, loff_t *ppos) |
| 1361 | { |
| 1362 | int value; |
| 1363 | char tmpbuf[TMPBUFLEN]; |
| 1364 | ssize_t length; |
| 1365 | unsigned long i_ino = file->f_path.dentry->d_inode->i_ino; |
| 1366 | |
| 1367 | value = security_policycap_supported(i_ino & SEL_INO_MASK); |
| 1368 | length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); |
| 1369 | |
| 1370 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1371 | } |
| 1372 | |
| 1373 | static const struct file_operations sel_policycap_ops = { |
| 1374 | .read = sel_read_policycap, |
| 1375 | }; |
| 1376 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1377 | static int sel_make_perm_files(char *objclass, int classvalue, |
| 1378 | struct dentry *dir) |
| 1379 | { |
| 1380 | int i, rc = 0, nperms; |
| 1381 | char **perms; |
| 1382 | |
| 1383 | rc = security_get_permissions(objclass, &perms, &nperms); |
| 1384 | if (rc) |
| 1385 | goto out; |
| 1386 | |
| 1387 | for (i = 0; i < nperms; i++) { |
| 1388 | struct inode *inode; |
| 1389 | struct dentry *dentry; |
| 1390 | |
| 1391 | dentry = d_alloc_name(dir, perms[i]); |
| 1392 | if (!dentry) { |
| 1393 | rc = -ENOMEM; |
| 1394 | goto out1; |
| 1395 | } |
| 1396 | |
| 1397 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1398 | if (!inode) { |
| 1399 | rc = -ENOMEM; |
| 1400 | goto out1; |
| 1401 | } |
| 1402 | inode->i_fop = &sel_perm_ops; |
| 1403 | /* i+1 since perm values are 1-indexed */ |
| 1404 | inode->i_ino = sel_perm_to_ino(classvalue, i+1); |
| 1405 | d_add(dentry, inode); |
| 1406 | } |
| 1407 | |
| 1408 | out1: |
| 1409 | for (i = 0; i < nperms; i++) |
| 1410 | kfree(perms[i]); |
| 1411 | kfree(perms); |
| 1412 | out: |
| 1413 | return rc; |
| 1414 | } |
| 1415 | |
| 1416 | static int sel_make_class_dir_entries(char *classname, int index, |
| 1417 | struct dentry *dir) |
| 1418 | { |
| 1419 | struct dentry *dentry = NULL; |
| 1420 | struct inode *inode = NULL; |
| 1421 | int rc; |
| 1422 | |
| 1423 | dentry = d_alloc_name(dir, "index"); |
| 1424 | if (!dentry) { |
| 1425 | rc = -ENOMEM; |
| 1426 | goto out; |
| 1427 | } |
| 1428 | |
| 1429 | inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); |
| 1430 | if (!inode) { |
| 1431 | rc = -ENOMEM; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | |
| 1435 | inode->i_fop = &sel_class_ops; |
| 1436 | inode->i_ino = sel_class_to_ino(index); |
| 1437 | d_add(dentry, inode); |
| 1438 | |
| 1439 | dentry = d_alloc_name(dir, "perms"); |
| 1440 | if (!dentry) { |
| 1441 | rc = -ENOMEM; |
| 1442 | goto out; |
| 1443 | } |
| 1444 | |
| 1445 | rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino); |
| 1446 | if (rc) |
| 1447 | goto out; |
| 1448 | |
| 1449 | rc = sel_make_perm_files(classname, index, dentry); |
| 1450 | |
| 1451 | out: |
| 1452 | return rc; |
| 1453 | } |
| 1454 | |
| 1455 | static void sel_remove_classes(void) |
| 1456 | { |
| 1457 | struct list_head *class_node; |
| 1458 | |
| 1459 | list_for_each(class_node, &class_dir->d_subdirs) { |
| 1460 | struct dentry *class_subdir = list_entry(class_node, |
| 1461 | struct dentry, d_u.d_child); |
| 1462 | struct list_head *class_subdir_node; |
| 1463 | |
| 1464 | list_for_each(class_subdir_node, &class_subdir->d_subdirs) { |
| 1465 | struct dentry *d = list_entry(class_subdir_node, |
| 1466 | struct dentry, d_u.d_child); |
| 1467 | |
| 1468 | if (d->d_inode) |
| 1469 | if (d->d_inode->i_mode & S_IFDIR) |
| 1470 | sel_remove_entries(d); |
| 1471 | } |
| 1472 | |
| 1473 | sel_remove_entries(class_subdir); |
| 1474 | } |
| 1475 | |
| 1476 | sel_remove_entries(class_dir); |
| 1477 | } |
| 1478 | |
| 1479 | static int sel_make_classes(void) |
| 1480 | { |
| 1481 | int rc = 0, nclasses, i; |
| 1482 | char **classes; |
| 1483 | |
| 1484 | /* delete any existing entries */ |
| 1485 | sel_remove_classes(); |
| 1486 | |
| 1487 | rc = security_get_classes(&classes, &nclasses); |
| 1488 | if (rc < 0) |
| 1489 | goto out; |
| 1490 | |
| 1491 | /* +2 since classes are 1-indexed */ |
| 1492 | last_class_ino = sel_class_to_ino(nclasses+2); |
| 1493 | |
| 1494 | for (i = 0; i < nclasses; i++) { |
| 1495 | struct dentry *class_name_dir; |
| 1496 | |
| 1497 | class_name_dir = d_alloc_name(class_dir, classes[i]); |
| 1498 | if (!class_name_dir) { |
| 1499 | rc = -ENOMEM; |
| 1500 | goto out1; |
| 1501 | } |
| 1502 | |
| 1503 | rc = sel_make_dir(class_dir->d_inode, class_name_dir, |
| 1504 | &last_class_ino); |
| 1505 | if (rc) |
| 1506 | goto out1; |
| 1507 | |
| 1508 | /* i+1 since class values are 1-indexed */ |
| 1509 | rc = sel_make_class_dir_entries(classes[i], i+1, |
| 1510 | class_name_dir); |
| 1511 | if (rc) |
| 1512 | goto out1; |
| 1513 | } |
| 1514 | |
| 1515 | out1: |
| 1516 | for (i = 0; i < nclasses; i++) |
| 1517 | kfree(classes[i]); |
| 1518 | kfree(classes); |
| 1519 | out: |
| 1520 | return rc; |
| 1521 | } |
| 1522 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1523 | static int sel_make_policycap(void) |
| 1524 | { |
| 1525 | unsigned int iter; |
| 1526 | struct dentry *dentry = NULL; |
| 1527 | struct inode *inode = NULL; |
| 1528 | |
| 1529 | sel_remove_entries(policycap_dir); |
| 1530 | |
| 1531 | for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { |
| 1532 | if (iter < ARRAY_SIZE(policycap_names)) |
| 1533 | dentry = d_alloc_name(policycap_dir, |
| 1534 | policycap_names[iter]); |
| 1535 | else |
| 1536 | dentry = d_alloc_name(policycap_dir, "unknown"); |
| 1537 | |
| 1538 | if (dentry == NULL) |
| 1539 | return -ENOMEM; |
| 1540 | |
| 1541 | inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); |
| 1542 | if (inode == NULL) |
| 1543 | return -ENOMEM; |
| 1544 | |
| 1545 | inode->i_fop = &sel_policycap_ops; |
| 1546 | inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; |
| 1547 | d_add(dentry, inode); |
| 1548 | } |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1553 | static int sel_make_dir(struct inode *dir, struct dentry *dentry, |
| 1554 | unsigned long *ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | { |
| 1556 | int ret = 0; |
| 1557 | struct inode *inode; |
| 1558 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1559 | 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] | 1560 | if (!inode) { |
| 1561 | ret = -ENOMEM; |
| 1562 | goto out; |
| 1563 | } |
| 1564 | inode->i_op = &simple_dir_inode_operations; |
| 1565 | inode->i_fop = &simple_dir_operations; |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1566 | inode->i_ino = ++(*ino); |
James Morris | 40e906f | 2006-03-22 00:09:16 -0800 | [diff] [blame] | 1567 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1568 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | d_add(dentry, inode); |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1570 | /* bump link count on parent directory, too */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1571 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | out: |
| 1573 | return ret; |
| 1574 | } |
| 1575 | |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1576 | static int sel_fill_super(struct super_block *sb, void *data, int silent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | { |
| 1578 | int ret; |
| 1579 | struct dentry *dentry; |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1580 | struct inode *inode, *root_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | struct inode_security_struct *isec; |
| 1582 | |
| 1583 | static struct tree_descr selinux_files[] = { |
| 1584 | [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, |
| 1585 | [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, |
Stephen Smalley | ce9982d | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 1586 | [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1588 | [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1589 | [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1590 | [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1591 | [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, |
| 1592 | [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, |
| 1593 | [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, |
| 1594 | [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, |
| 1595 | [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, |
| 1596 | [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 1597 | [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, |
| 1598 | [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | /* last one */ {""} |
| 1600 | }; |
| 1601 | ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); |
| 1602 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1603 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
James Morris | edb20fb | 2006-03-22 00:09:20 -0800 | [diff] [blame] | 1605 | root_inode = sb->s_root->d_inode; |
| 1606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1608 | if (!dentry) { |
| 1609 | ret = -ENOMEM; |
| 1610 | goto err; |
| 1611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1613 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1614 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1615 | goto err; |
James Morris | cde174a | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | bool_dir = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1620 | if (!dentry) { |
| 1621 | ret = -ENOMEM; |
| 1622 | goto err; |
| 1623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
| 1625 | inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1626 | if (!inode) { |
| 1627 | ret = -ENOMEM; |
| 1628 | goto err; |
| 1629 | } |
James Carter | 6174eaf | 2007-04-04 16:18:39 -0400 | [diff] [blame] | 1630 | inode->i_ino = ++sel_last_ino; |
Eric Paris | 1872981 | 2008-04-17 14:15:45 -0400 | [diff] [blame] | 1631 | isec = (struct inode_security_struct *)inode->i_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | isec->sid = SECINITSID_DEVNULL; |
| 1633 | isec->sclass = SECCLASS_CHR_FILE; |
| 1634 | isec->initialized = 1; |
| 1635 | |
| 1636 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); |
| 1637 | d_add(dentry, inode); |
| 1638 | selinux_null = dentry; |
| 1639 | |
| 1640 | dentry = d_alloc_name(sb->s_root, "avc"); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1641 | if (!dentry) { |
| 1642 | ret = -ENOMEM; |
| 1643 | goto err; |
| 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1646 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1648 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
| 1650 | ret = sel_make_avc_files(dentry); |
| 1651 | if (ret) |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1652 | goto err; |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1653 | |
| 1654 | dentry = d_alloc_name(sb->s_root, "initial_contexts"); |
| 1655 | if (!dentry) { |
| 1656 | ret = -ENOMEM; |
| 1657 | goto err; |
| 1658 | } |
| 1659 | |
Christopher J. PeBenito | 0dd4ae5 | 2007-05-23 09:12:08 -0400 | [diff] [blame] | 1660 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1661 | if (ret) |
| 1662 | goto err; |
| 1663 | |
| 1664 | ret = sel_make_initcon_files(dentry); |
| 1665 | if (ret) |
| 1666 | goto err; |
| 1667 | |
Christopher J. PeBenito | e47c8fc | 2007-05-23 09:12:09 -0400 | [diff] [blame] | 1668 | dentry = d_alloc_name(sb->s_root, "class"); |
| 1669 | if (!dentry) { |
| 1670 | ret = -ENOMEM; |
| 1671 | goto err; |
| 1672 | } |
| 1673 | |
| 1674 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
| 1675 | if (ret) |
| 1676 | goto err; |
| 1677 | |
| 1678 | class_dir = dentry; |
| 1679 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1680 | dentry = d_alloc_name(sb->s_root, "policy_capabilities"); |
| 1681 | if (!dentry) { |
| 1682 | ret = -ENOMEM; |
| 1683 | goto err; |
| 1684 | } |
| 1685 | |
| 1686 | ret = sel_make_dir(root_inode, dentry, &sel_last_ino); |
| 1687 | if (ret) |
| 1688 | goto err; |
| 1689 | |
| 1690 | policycap_dir = dentry; |
| 1691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | out: |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1693 | return ret; |
| 1694 | err: |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1695 | printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", |
| 1696 | __func__); |
James Morris | 161ce45 | 2006-03-22 00:09:17 -0800 | [diff] [blame] | 1697 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1700 | static int sel_get_sb(struct file_system_type *fs_type, |
| 1701 | int flags, const char *dev_name, void *data, |
| 1702 | struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 1704 | return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | static struct file_system_type sel_fs_type = { |
| 1708 | .name = "selinuxfs", |
| 1709 | .get_sb = sel_get_sb, |
| 1710 | .kill_sb = kill_litter_super, |
| 1711 | }; |
| 1712 | |
| 1713 | struct vfsmount *selinuxfs_mount; |
| 1714 | |
| 1715 | static int __init init_sel_fs(void) |
| 1716 | { |
| 1717 | int err; |
| 1718 | |
| 1719 | if (!selinux_enabled) |
| 1720 | return 0; |
| 1721 | err = register_filesystem(&sel_fs_type); |
| 1722 | if (!err) { |
| 1723 | selinuxfs_mount = kern_mount(&sel_fs_type); |
| 1724 | if (IS_ERR(selinuxfs_mount)) { |
| 1725 | printk(KERN_ERR "selinuxfs: could not mount!\n"); |
| 1726 | err = PTR_ERR(selinuxfs_mount); |
| 1727 | selinuxfs_mount = NULL; |
| 1728 | } |
| 1729 | } |
| 1730 | return err; |
| 1731 | } |
| 1732 | |
| 1733 | __initcall(init_sel_fs); |
| 1734 | |
| 1735 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 1736 | void exit_sel_fs(void) |
| 1737 | { |
| 1738 | unregister_filesystem(&sel_fs_type); |
| 1739 | } |
| 1740 | #endif |