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