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