]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NSA Security-Enhanced Linux (SELinux) security module | |
3 | * | |
4 | * This file contains the SELinux hook function implementations. | |
5 | * | |
6 | * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> | |
828dfe1d EP |
7 | * Chris Vance, <cvance@nai.com> |
8 | * Wayne Salamon, <wsalamon@nai.com> | |
9 | * James Morris <jmorris@redhat.com> | |
1da177e4 LT |
10 | * |
11 | * Copyright (C) 2001,2002 Networks Associates Technology, Inc. | |
2069f457 EP |
12 | * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
13 | * Eric Paris <eparis@redhat.com> | |
1da177e4 | 14 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
828dfe1d | 15 | * <dgoeddel@trustedcs.com> |
ed6d76e4 | 16 | * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. |
82c21bfa | 17 | * Paul Moore <paul@paul-moore.com> |
788e7dd4 | 18 | * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. |
828dfe1d | 19 | * Yuichi Nakamura <ynakam@hitachisoft.jp> |
1da177e4 LT |
20 | * |
21 | * This program is free software; you can redistribute it and/or modify | |
22 | * it under the terms of the GNU General Public License version 2, | |
828dfe1d | 23 | * as published by the Free Software Foundation. |
1da177e4 LT |
24 | */ |
25 | ||
1da177e4 | 26 | #include <linux/init.h> |
0b24dcb7 | 27 | #include <linux/kd.h> |
1da177e4 | 28 | #include <linux/kernel.h> |
0d094efe | 29 | #include <linux/tracehook.h> |
1da177e4 LT |
30 | #include <linux/errno.h> |
31 | #include <linux/sched.h> | |
32 | #include <linux/security.h> | |
33 | #include <linux/xattr.h> | |
34 | #include <linux/capability.h> | |
35 | #include <linux/unistd.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/mman.h> | |
38 | #include <linux/slab.h> | |
39 | #include <linux/pagemap.h> | |
0b24dcb7 | 40 | #include <linux/proc_fs.h> |
1da177e4 | 41 | #include <linux/swap.h> |
1da177e4 LT |
42 | #include <linux/spinlock.h> |
43 | #include <linux/syscalls.h> | |
2a7dba39 | 44 | #include <linux/dcache.h> |
1da177e4 | 45 | #include <linux/file.h> |
9f3acc31 | 46 | #include <linux/fdtable.h> |
1da177e4 LT |
47 | #include <linux/namei.h> |
48 | #include <linux/mount.h> | |
1da177e4 LT |
49 | #include <linux/netfilter_ipv4.h> |
50 | #include <linux/netfilter_ipv6.h> | |
51 | #include <linux/tty.h> | |
52 | #include <net/icmp.h> | |
227b60f5 | 53 | #include <net/ip.h> /* for local_port_range[] */ |
ca10b9e9 | 54 | #include <net/sock.h> |
1da177e4 | 55 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
47180068 | 56 | #include <net/inet_connection_sock.h> |
220deb96 | 57 | #include <net/net_namespace.h> |
d621d35e | 58 | #include <net/netlabel.h> |
f5269710 | 59 | #include <linux/uaccess.h> |
1da177e4 | 60 | #include <asm/ioctls.h> |
60063497 | 61 | #include <linux/atomic.h> |
1da177e4 LT |
62 | #include <linux/bitops.h> |
63 | #include <linux/interrupt.h> | |
64 | #include <linux/netdevice.h> /* for network interface checks */ | |
77954983 | 65 | #include <net/netlink.h> |
1da177e4 LT |
66 | #include <linux/tcp.h> |
67 | #include <linux/udp.h> | |
2ee92d46 | 68 | #include <linux/dccp.h> |
1da177e4 LT |
69 | #include <linux/quota.h> |
70 | #include <linux/un.h> /* for Unix socket types */ | |
71 | #include <net/af_unix.h> /* for Unix socket types */ | |
72 | #include <linux/parser.h> | |
73 | #include <linux/nfs_mount.h> | |
74 | #include <net/ipv6.h> | |
75 | #include <linux/hugetlb.h> | |
76 | #include <linux/personality.h> | |
1da177e4 | 77 | #include <linux/audit.h> |
6931dfc9 | 78 | #include <linux/string.h> |
877ce7c1 | 79 | #include <linux/selinux.h> |
23970741 | 80 | #include <linux/mutex.h> |
f06febc9 | 81 | #include <linux/posix-timers.h> |
00234592 | 82 | #include <linux/syslog.h> |
3486740a | 83 | #include <linux/user_namespace.h> |
44fc7ea0 | 84 | #include <linux/export.h> |
40401530 AV |
85 | #include <linux/msg.h> |
86 | #include <linux/shm.h> | |
1da177e4 LT |
87 | |
88 | #include "avc.h" | |
89 | #include "objsec.h" | |
90 | #include "netif.h" | |
224dfbd8 | 91 | #include "netnode.h" |
3e112172 | 92 | #include "netport.h" |
d28d1e08 | 93 | #include "xfrm.h" |
c60475bf | 94 | #include "netlabel.h" |
9d57a7f9 | 95 | #include "audit.h" |
7b98a585 | 96 | #include "avc_ss.h" |
1da177e4 | 97 | |
20510f2f | 98 | extern struct security_operations *security_ops; |
1da177e4 | 99 | |
d621d35e | 100 | /* SECMARK reference count */ |
56a4ca99 | 101 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
d621d35e | 102 | |
1da177e4 | 103 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
828dfe1d | 104 | int selinux_enforcing; |
1da177e4 LT |
105 | |
106 | static int __init enforcing_setup(char *str) | |
107 | { | |
f5269710 | 108 | unsigned long enforcing; |
29707b20 | 109 | if (!kstrtoul(str, 0, &enforcing)) |
f5269710 | 110 | selinux_enforcing = enforcing ? 1 : 0; |
1da177e4 LT |
111 | return 1; |
112 | } | |
113 | __setup("enforcing=", enforcing_setup); | |
114 | #endif | |
115 | ||
116 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM | |
117 | int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; | |
118 | ||
119 | static int __init selinux_enabled_setup(char *str) | |
120 | { | |
f5269710 | 121 | unsigned long enabled; |
29707b20 | 122 | if (!kstrtoul(str, 0, &enabled)) |
f5269710 | 123 | selinux_enabled = enabled ? 1 : 0; |
1da177e4 LT |
124 | return 1; |
125 | } | |
126 | __setup("selinux=", selinux_enabled_setup); | |
30d55280 SS |
127 | #else |
128 | int selinux_enabled = 1; | |
1da177e4 LT |
129 | #endif |
130 | ||
e18b890b | 131 | static struct kmem_cache *sel_inode_cache; |
7cae7e26 | 132 | |
d621d35e PM |
133 | /** |
134 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled | |
135 | * | |
136 | * Description: | |
137 | * This function checks the SECMARK reference counter to see if any SECMARK | |
138 | * targets are currently configured, if the reference counter is greater than | |
139 | * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is | |
2be4d74f CP |
140 | * enabled, false (0) if SECMARK is disabled. If the always_check_network |
141 | * policy capability is enabled, SECMARK is always considered enabled. | |
d621d35e PM |
142 | * |
143 | */ | |
144 | static int selinux_secmark_enabled(void) | |
145 | { | |
2be4d74f CP |
146 | return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount)); |
147 | } | |
148 | ||
149 | /** | |
150 | * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled | |
151 | * | |
152 | * Description: | |
153 | * This function checks if NetLabel or labeled IPSEC is enabled. Returns true | |
154 | * (1) if any are enabled or false (0) if neither are enabled. If the | |
155 | * always_check_network policy capability is enabled, peer labeling | |
156 | * is always considered enabled. | |
157 | * | |
158 | */ | |
159 | static int selinux_peerlbl_enabled(void) | |
160 | { | |
161 | return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); | |
d621d35e PM |
162 | } |
163 | ||
615e51fd PM |
164 | static int selinux_netcache_avc_callback(u32 event) |
165 | { | |
166 | if (event == AVC_CALLBACK_RESET) { | |
167 | sel_netif_flush(); | |
168 | sel_netnode_flush(); | |
169 | sel_netport_flush(); | |
170 | synchronize_net(); | |
171 | } | |
172 | return 0; | |
173 | } | |
174 | ||
d84f4f99 DH |
175 | /* |
176 | * initialise the security for the init task | |
177 | */ | |
178 | static void cred_init_security(void) | |
1da177e4 | 179 | { |
3b11a1de | 180 | struct cred *cred = (struct cred *) current->real_cred; |
1da177e4 LT |
181 | struct task_security_struct *tsec; |
182 | ||
89d155ef | 183 | tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); |
1da177e4 | 184 | if (!tsec) |
d84f4f99 | 185 | panic("SELinux: Failed to initialize initial task.\n"); |
1da177e4 | 186 | |
d84f4f99 | 187 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
f1752eec | 188 | cred->security = tsec; |
1da177e4 LT |
189 | } |
190 | ||
88e67f3b DH |
191 | /* |
192 | * get the security ID of a set of credentials | |
193 | */ | |
194 | static inline u32 cred_sid(const struct cred *cred) | |
195 | { | |
196 | const struct task_security_struct *tsec; | |
197 | ||
198 | tsec = cred->security; | |
199 | return tsec->sid; | |
200 | } | |
201 | ||
275bb41e | 202 | /* |
3b11a1de | 203 | * get the objective security ID of a task |
275bb41e DH |
204 | */ |
205 | static inline u32 task_sid(const struct task_struct *task) | |
206 | { | |
275bb41e DH |
207 | u32 sid; |
208 | ||
209 | rcu_read_lock(); | |
88e67f3b | 210 | sid = cred_sid(__task_cred(task)); |
275bb41e DH |
211 | rcu_read_unlock(); |
212 | return sid; | |
213 | } | |
214 | ||
215 | /* | |
3b11a1de | 216 | * get the subjective security ID of the current task |
275bb41e DH |
217 | */ |
218 | static inline u32 current_sid(void) | |
219 | { | |
5fb49870 | 220 | const struct task_security_struct *tsec = current_security(); |
275bb41e DH |
221 | |
222 | return tsec->sid; | |
223 | } | |
224 | ||
88e67f3b DH |
225 | /* Allocate and free functions for each kind of security blob. */ |
226 | ||
1da177e4 LT |
227 | static int inode_alloc_security(struct inode *inode) |
228 | { | |
1da177e4 | 229 | struct inode_security_struct *isec; |
275bb41e | 230 | u32 sid = current_sid(); |
1da177e4 | 231 | |
a02fe132 | 232 | isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); |
1da177e4 LT |
233 | if (!isec) |
234 | return -ENOMEM; | |
235 | ||
23970741 | 236 | mutex_init(&isec->lock); |
1da177e4 | 237 | INIT_LIST_HEAD(&isec->list); |
1da177e4 LT |
238 | isec->inode = inode; |
239 | isec->sid = SECINITSID_UNLABELED; | |
240 | isec->sclass = SECCLASS_FILE; | |
275bb41e | 241 | isec->task_sid = sid; |
1da177e4 LT |
242 | inode->i_security = isec; |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
3dc91d43 SR |
247 | static void inode_free_rcu(struct rcu_head *head) |
248 | { | |
249 | struct inode_security_struct *isec; | |
250 | ||
251 | isec = container_of(head, struct inode_security_struct, rcu); | |
252 | kmem_cache_free(sel_inode_cache, isec); | |
253 | } | |
254 | ||
1da177e4 LT |
255 | static void inode_free_security(struct inode *inode) |
256 | { | |
257 | struct inode_security_struct *isec = inode->i_security; | |
258 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; | |
259 | ||
1da177e4 LT |
260 | spin_lock(&sbsec->isec_lock); |
261 | if (!list_empty(&isec->list)) | |
262 | list_del_init(&isec->list); | |
263 | spin_unlock(&sbsec->isec_lock); | |
264 | ||
3dc91d43 SR |
265 | /* |
266 | * The inode may still be referenced in a path walk and | |
267 | * a call to selinux_inode_permission() can be made | |
268 | * after inode_free_security() is called. Ideally, the VFS | |
269 | * wouldn't do this, but fixing that is a much harder | |
270 | * job. For now, simply free the i_security via RCU, and | |
271 | * leave the current inode->i_security pointer intact. | |
272 | * The inode will be freed after the RCU grace period too. | |
273 | */ | |
274 | call_rcu(&isec->rcu, inode_free_rcu); | |
1da177e4 LT |
275 | } |
276 | ||
277 | static int file_alloc_security(struct file *file) | |
278 | { | |
1da177e4 | 279 | struct file_security_struct *fsec; |
275bb41e | 280 | u32 sid = current_sid(); |
1da177e4 | 281 | |
26d2a4be | 282 | fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); |
1da177e4 LT |
283 | if (!fsec) |
284 | return -ENOMEM; | |
285 | ||
275bb41e DH |
286 | fsec->sid = sid; |
287 | fsec->fown_sid = sid; | |
1da177e4 LT |
288 | file->f_security = fsec; |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
293 | static void file_free_security(struct file *file) | |
294 | { | |
295 | struct file_security_struct *fsec = file->f_security; | |
1da177e4 LT |
296 | file->f_security = NULL; |
297 | kfree(fsec); | |
298 | } | |
299 | ||
300 | static int superblock_alloc_security(struct super_block *sb) | |
301 | { | |
302 | struct superblock_security_struct *sbsec; | |
303 | ||
89d155ef | 304 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
1da177e4 LT |
305 | if (!sbsec) |
306 | return -ENOMEM; | |
307 | ||
bc7e982b | 308 | mutex_init(&sbsec->lock); |
1da177e4 LT |
309 | INIT_LIST_HEAD(&sbsec->isec_head); |
310 | spin_lock_init(&sbsec->isec_lock); | |
1da177e4 LT |
311 | sbsec->sb = sb; |
312 | sbsec->sid = SECINITSID_UNLABELED; | |
313 | sbsec->def_sid = SECINITSID_FILE; | |
c312feb2 | 314 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
1da177e4 LT |
315 | sb->s_security = sbsec; |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | static void superblock_free_security(struct super_block *sb) | |
321 | { | |
322 | struct superblock_security_struct *sbsec = sb->s_security; | |
1da177e4 LT |
323 | sb->s_security = NULL; |
324 | kfree(sbsec); | |
325 | } | |
326 | ||
1da177e4 LT |
327 | /* The file system's label must be initialized prior to use. */ |
328 | ||
eb9ae686 | 329 | static const char *labeling_behaviors[7] = { |
1da177e4 LT |
330 | "uses xattr", |
331 | "uses transition SIDs", | |
332 | "uses task SIDs", | |
333 | "uses genfs_contexts", | |
334 | "not configured for labeling", | |
335 | "uses mountpoint labeling", | |
eb9ae686 | 336 | "uses native labeling", |
1da177e4 LT |
337 | }; |
338 | ||
339 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); | |
340 | ||
341 | static inline int inode_doinit(struct inode *inode) | |
342 | { | |
343 | return inode_doinit_with_dentry(inode, NULL); | |
344 | } | |
345 | ||
346 | enum { | |
31e87930 | 347 | Opt_error = -1, |
1da177e4 LT |
348 | Opt_context = 1, |
349 | Opt_fscontext = 2, | |
c9180a57 EP |
350 | Opt_defcontext = 3, |
351 | Opt_rootcontext = 4, | |
11689d47 | 352 | Opt_labelsupport = 5, |
d355987f | 353 | Opt_nextmntopt = 6, |
1da177e4 LT |
354 | }; |
355 | ||
d355987f EP |
356 | #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1) |
357 | ||
a447c093 | 358 | static const match_table_t tokens = { |
832cbd9a EP |
359 | {Opt_context, CONTEXT_STR "%s"}, |
360 | {Opt_fscontext, FSCONTEXT_STR "%s"}, | |
361 | {Opt_defcontext, DEFCONTEXT_STR "%s"}, | |
362 | {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, | |
11689d47 | 363 | {Opt_labelsupport, LABELSUPP_STR}, |
31e87930 | 364 | {Opt_error, NULL}, |
1da177e4 LT |
365 | }; |
366 | ||
367 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" | |
368 | ||
c312feb2 EP |
369 | static int may_context_mount_sb_relabel(u32 sid, |
370 | struct superblock_security_struct *sbsec, | |
275bb41e | 371 | const struct cred *cred) |
c312feb2 | 372 | { |
275bb41e | 373 | const struct task_security_struct *tsec = cred->security; |
c312feb2 EP |
374 | int rc; |
375 | ||
376 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, | |
377 | FILESYSTEM__RELABELFROM, NULL); | |
378 | if (rc) | |
379 | return rc; | |
380 | ||
381 | rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, | |
382 | FILESYSTEM__RELABELTO, NULL); | |
383 | return rc; | |
384 | } | |
385 | ||
0808925e EP |
386 | static int may_context_mount_inode_relabel(u32 sid, |
387 | struct superblock_security_struct *sbsec, | |
275bb41e | 388 | const struct cred *cred) |
0808925e | 389 | { |
275bb41e | 390 | const struct task_security_struct *tsec = cred->security; |
0808925e EP |
391 | int rc; |
392 | rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, | |
393 | FILESYSTEM__RELABELFROM, NULL); | |
394 | if (rc) | |
395 | return rc; | |
396 | ||
397 | rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, | |
398 | FILESYSTEM__ASSOCIATE, NULL); | |
399 | return rc; | |
400 | } | |
401 | ||
b43e725d EP |
402 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
403 | { | |
404 | struct superblock_security_struct *sbsec = sb->s_security; | |
405 | ||
406 | if (sbsec->behavior == SECURITY_FS_USE_XATTR || | |
407 | sbsec->behavior == SECURITY_FS_USE_TRANS || | |
408 | sbsec->behavior == SECURITY_FS_USE_TASK) | |
409 | return 1; | |
410 | ||
411 | /* Special handling for sysfs. Is genfs but also has setxattr handler*/ | |
412 | if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0) | |
413 | return 1; | |
414 | ||
415 | /* | |
416 | * Special handling for rootfs. Is genfs but supports | |
417 | * setting SELinux context on in-core inodes. | |
418 | */ | |
419 | if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0) | |
420 | return 1; | |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
c9180a57 | 425 | static int sb_finish_set_opts(struct super_block *sb) |
1da177e4 | 426 | { |
1da177e4 | 427 | struct superblock_security_struct *sbsec = sb->s_security; |
c9180a57 EP |
428 | struct dentry *root = sb->s_root; |
429 | struct inode *root_inode = root->d_inode; | |
430 | int rc = 0; | |
1da177e4 | 431 | |
c9180a57 EP |
432 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
433 | /* Make sure that the xattr handler exists and that no | |
434 | error other than -ENODATA is returned by getxattr on | |
435 | the root directory. -ENODATA is ok, as this may be | |
436 | the first boot of the SELinux kernel before we have | |
437 | assigned xattr values to the filesystem. */ | |
438 | if (!root_inode->i_op->getxattr) { | |
29b1deb2 LT |
439 | printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " |
440 | "xattr support\n", sb->s_id, sb->s_type->name); | |
c9180a57 EP |
441 | rc = -EOPNOTSUPP; |
442 | goto out; | |
443 | } | |
444 | rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); | |
445 | if (rc < 0 && rc != -ENODATA) { | |
446 | if (rc == -EOPNOTSUPP) | |
447 | printk(KERN_WARNING "SELinux: (dev %s, type " | |
29b1deb2 LT |
448 | "%s) has no security xattr handler\n", |
449 | sb->s_id, sb->s_type->name); | |
c9180a57 EP |
450 | else |
451 | printk(KERN_WARNING "SELinux: (dev %s, type " | |
29b1deb2 LT |
452 | "%s) getxattr errno %d\n", sb->s_id, |
453 | sb->s_type->name, -rc); | |
c9180a57 EP |
454 | goto out; |
455 | } | |
456 | } | |
1da177e4 | 457 | |
c9180a57 | 458 | if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) |
29b1deb2 LT |
459 | printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", |
460 | sb->s_id, sb->s_type->name); | |
c9180a57 | 461 | else |
29b1deb2 LT |
462 | printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", |
463 | sb->s_id, sb->s_type->name, | |
c9180a57 | 464 | labeling_behaviors[sbsec->behavior-1]); |
1da177e4 | 465 | |
eadcabc6 | 466 | sbsec->flags |= SE_SBINITIALIZED; |
b43e725d | 467 | if (selinux_is_sblabel_mnt(sb)) |
12f348b9 | 468 | sbsec->flags |= SBLABEL_MNT; |
ddd29ec6 | 469 | |
c9180a57 EP |
470 | /* Initialize the root inode. */ |
471 | rc = inode_doinit_with_dentry(root_inode, root); | |
1da177e4 | 472 | |
c9180a57 EP |
473 | /* Initialize any other inodes associated with the superblock, e.g. |
474 | inodes created prior to initial policy load or inodes created | |
475 | during get_sb by a pseudo filesystem that directly | |
476 | populates itself. */ | |
477 | spin_lock(&sbsec->isec_lock); | |
478 | next_inode: | |
479 | if (!list_empty(&sbsec->isec_head)) { | |
480 | struct inode_security_struct *isec = | |
481 | list_entry(sbsec->isec_head.next, | |
482 | struct inode_security_struct, list); | |
483 | struct inode *inode = isec->inode; | |
484 | spin_unlock(&sbsec->isec_lock); | |
485 | inode = igrab(inode); | |
486 | if (inode) { | |
487 | if (!IS_PRIVATE(inode)) | |
488 | inode_doinit(inode); | |
489 | iput(inode); | |
490 | } | |
491 | spin_lock(&sbsec->isec_lock); | |
492 | list_del_init(&isec->list); | |
493 | goto next_inode; | |
494 | } | |
495 | spin_unlock(&sbsec->isec_lock); | |
496 | out: | |
497 | return rc; | |
498 | } | |
1da177e4 | 499 | |
c9180a57 EP |
500 | /* |
501 | * This function should allow an FS to ask what it's mount security | |
502 | * options were so it can use those later for submounts, displaying | |
503 | * mount options, or whatever. | |
504 | */ | |
505 | static int selinux_get_mnt_opts(const struct super_block *sb, | |
e0007529 | 506 | struct security_mnt_opts *opts) |
c9180a57 EP |
507 | { |
508 | int rc = 0, i; | |
509 | struct superblock_security_struct *sbsec = sb->s_security; | |
510 | char *context = NULL; | |
511 | u32 len; | |
512 | char tmp; | |
1da177e4 | 513 | |
e0007529 | 514 | security_init_mnt_opts(opts); |
1da177e4 | 515 | |
0d90a7ec | 516 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
c9180a57 | 517 | return -EINVAL; |
1da177e4 | 518 | |
c9180a57 EP |
519 | if (!ss_initialized) |
520 | return -EINVAL; | |
1da177e4 | 521 | |
af8e50cc EP |
522 | /* make sure we always check enough bits to cover the mask */ |
523 | BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS)); | |
524 | ||
0d90a7ec | 525 | tmp = sbsec->flags & SE_MNTMASK; |
c9180a57 | 526 | /* count the number of mount options for this sb */ |
af8e50cc | 527 | for (i = 0; i < NUM_SEL_MNT_OPTS; i++) { |
c9180a57 | 528 | if (tmp & 0x01) |
e0007529 | 529 | opts->num_mnt_opts++; |
c9180a57 EP |
530 | tmp >>= 1; |
531 | } | |
11689d47 | 532 | /* Check if the Label support flag is set */ |
0b4bdb35 | 533 | if (sbsec->flags & SBLABEL_MNT) |
11689d47 | 534 | opts->num_mnt_opts++; |
1da177e4 | 535 | |
e0007529 EP |
536 | opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); |
537 | if (!opts->mnt_opts) { | |
c9180a57 EP |
538 | rc = -ENOMEM; |
539 | goto out_free; | |
540 | } | |
1da177e4 | 541 | |
e0007529 EP |
542 | opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); |
543 | if (!opts->mnt_opts_flags) { | |
c9180a57 EP |
544 | rc = -ENOMEM; |
545 | goto out_free; | |
546 | } | |
1da177e4 | 547 | |
c9180a57 EP |
548 | i = 0; |
549 | if (sbsec->flags & FSCONTEXT_MNT) { | |
550 | rc = security_sid_to_context(sbsec->sid, &context, &len); | |
551 | if (rc) | |
552 | goto out_free; | |
e0007529 EP |
553 | opts->mnt_opts[i] = context; |
554 | opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; | |
c9180a57 EP |
555 | } |
556 | if (sbsec->flags & CONTEXT_MNT) { | |
557 | rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); | |
558 | if (rc) | |
559 | goto out_free; | |
e0007529 EP |
560 | opts->mnt_opts[i] = context; |
561 | opts->mnt_opts_flags[i++] = CONTEXT_MNT; | |
c9180a57 EP |
562 | } |
563 | if (sbsec->flags & DEFCONTEXT_MNT) { | |
564 | rc = security_sid_to_context(sbsec->def_sid, &context, &len); | |
565 | if (rc) | |
566 | goto out_free; | |
e0007529 EP |
567 | opts->mnt_opts[i] = context; |
568 | opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; | |
c9180a57 EP |
569 | } |
570 | if (sbsec->flags & ROOTCONTEXT_MNT) { | |
571 | struct inode *root = sbsec->sb->s_root->d_inode; | |
572 | struct inode_security_struct *isec = root->i_security; | |
0808925e | 573 | |
c9180a57 EP |
574 | rc = security_sid_to_context(isec->sid, &context, &len); |
575 | if (rc) | |
576 | goto out_free; | |
e0007529 EP |
577 | opts->mnt_opts[i] = context; |
578 | opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; | |
c9180a57 | 579 | } |
12f348b9 | 580 | if (sbsec->flags & SBLABEL_MNT) { |
11689d47 | 581 | opts->mnt_opts[i] = NULL; |
12f348b9 | 582 | opts->mnt_opts_flags[i++] = SBLABEL_MNT; |
11689d47 | 583 | } |
1da177e4 | 584 | |
e0007529 | 585 | BUG_ON(i != opts->num_mnt_opts); |
1da177e4 | 586 | |
c9180a57 EP |
587 | return 0; |
588 | ||
589 | out_free: | |
e0007529 | 590 | security_free_mnt_opts(opts); |
c9180a57 EP |
591 | return rc; |
592 | } | |
1da177e4 | 593 | |
c9180a57 EP |
594 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
595 | u32 old_sid, u32 new_sid) | |
596 | { | |
0d90a7ec DQ |
597 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
598 | ||
c9180a57 | 599 | /* check if the old mount command had the same options */ |
0d90a7ec | 600 | if (sbsec->flags & SE_SBINITIALIZED) |
c9180a57 EP |
601 | if (!(sbsec->flags & flag) || |
602 | (old_sid != new_sid)) | |
603 | return 1; | |
604 | ||
605 | /* check if we were passed the same options twice, | |
606 | * aka someone passed context=a,context=b | |
607 | */ | |
0d90a7ec DQ |
608 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
609 | if (mnt_flags & flag) | |
c9180a57 EP |
610 | return 1; |
611 | return 0; | |
612 | } | |
e0007529 | 613 | |
c9180a57 EP |
614 | /* |
615 | * Allow filesystems with binary mount data to explicitly set mount point | |
616 | * labeling information. | |
617 | */ | |
e0007529 | 618 | static int selinux_set_mnt_opts(struct super_block *sb, |
649f6e77 DQ |
619 | struct security_mnt_opts *opts, |
620 | unsigned long kern_flags, | |
621 | unsigned long *set_kern_flags) | |
c9180a57 | 622 | { |
275bb41e | 623 | const struct cred *cred = current_cred(); |
c9180a57 | 624 | int rc = 0, i; |
c9180a57 | 625 | struct superblock_security_struct *sbsec = sb->s_security; |
29b1deb2 | 626 | const char *name = sb->s_type->name; |
089be43e JM |
627 | struct inode *inode = sbsec->sb->s_root->d_inode; |
628 | struct inode_security_struct *root_isec = inode->i_security; | |
c9180a57 EP |
629 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
630 | u32 defcontext_sid = 0; | |
e0007529 EP |
631 | char **mount_options = opts->mnt_opts; |
632 | int *flags = opts->mnt_opts_flags; | |
633 | int num_opts = opts->num_mnt_opts; | |
c9180a57 EP |
634 | |
635 | mutex_lock(&sbsec->lock); | |
636 | ||
637 | if (!ss_initialized) { | |
638 | if (!num_opts) { | |
639 | /* Defer initialization until selinux_complete_init, | |
640 | after the initial policy is loaded and the security | |
641 | server is ready to handle calls. */ | |
c9180a57 EP |
642 | goto out; |
643 | } | |
644 | rc = -EINVAL; | |
744ba35e EP |
645 | printk(KERN_WARNING "SELinux: Unable to set superblock options " |
646 | "before the security server is initialized\n"); | |
1da177e4 | 647 | goto out; |
c9180a57 | 648 | } |
649f6e77 DQ |
649 | if (kern_flags && !set_kern_flags) { |
650 | /* Specifying internal flags without providing a place to | |
651 | * place the results is not allowed */ | |
652 | rc = -EINVAL; | |
653 | goto out; | |
654 | } | |
1da177e4 | 655 | |
e0007529 EP |
656 | /* |
657 | * Binary mount data FS will come through this function twice. Once | |
658 | * from an explicit call and once from the generic calls from the vfs. | |
659 | * Since the generic VFS calls will not contain any security mount data | |
660 | * we need to skip the double mount verification. | |
661 | * | |
662 | * This does open a hole in which we will not notice if the first | |
663 | * mount using this sb set explict options and a second mount using | |
664 | * this sb does not set any security options. (The first options | |
665 | * will be used for both mounts) | |
666 | */ | |
0d90a7ec | 667 | if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
e0007529 | 668 | && (num_opts == 0)) |
f5269710 | 669 | goto out; |
e0007529 | 670 | |
c9180a57 EP |
671 | /* |
672 | * parse the mount options, check if they are valid sids. | |
673 | * also check if someone is trying to mount the same sb more | |
674 | * than once with different security options. | |
675 | */ | |
676 | for (i = 0; i < num_opts; i++) { | |
677 | u32 sid; | |
11689d47 | 678 | |
12f348b9 | 679 | if (flags[i] == SBLABEL_MNT) |
11689d47 | 680 | continue; |
c9180a57 | 681 | rc = security_context_to_sid(mount_options[i], |
52a4c640 | 682 | strlen(mount_options[i]), &sid, GFP_KERNEL); |
1da177e4 LT |
683 | if (rc) { |
684 | printk(KERN_WARNING "SELinux: security_context_to_sid" | |
29b1deb2 LT |
685 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
686 | mount_options[i], sb->s_id, name, rc); | |
c9180a57 EP |
687 | goto out; |
688 | } | |
689 | switch (flags[i]) { | |
690 | case FSCONTEXT_MNT: | |
691 | fscontext_sid = sid; | |
692 | ||
693 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, | |
694 | fscontext_sid)) | |
695 | goto out_double_mount; | |
696 | ||
697 | sbsec->flags |= FSCONTEXT_MNT; | |
698 | break; | |
699 | case CONTEXT_MNT: | |
700 | context_sid = sid; | |
701 | ||
702 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, | |
703 | context_sid)) | |
704 | goto out_double_mount; | |
705 | ||
706 | sbsec->flags |= CONTEXT_MNT; | |
707 | break; | |
708 | case ROOTCONTEXT_MNT: | |
709 | rootcontext_sid = sid; | |
710 | ||
711 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, | |
712 | rootcontext_sid)) | |
713 | goto out_double_mount; | |
714 | ||
715 | sbsec->flags |= ROOTCONTEXT_MNT; | |
716 | ||
717 | break; | |
718 | case DEFCONTEXT_MNT: | |
719 | defcontext_sid = sid; | |
720 | ||
721 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, | |
722 | defcontext_sid)) | |
723 | goto out_double_mount; | |
724 | ||
725 | sbsec->flags |= DEFCONTEXT_MNT; | |
726 | ||
727 | break; | |
728 | default: | |
729 | rc = -EINVAL; | |
730 | goto out; | |
1da177e4 | 731 | } |
c9180a57 EP |
732 | } |
733 | ||
0d90a7ec | 734 | if (sbsec->flags & SE_SBINITIALIZED) { |
c9180a57 | 735 | /* previously mounted with options, but not on this attempt? */ |
0d90a7ec | 736 | if ((sbsec->flags & SE_MNTMASK) && !num_opts) |
c9180a57 EP |
737 | goto out_double_mount; |
738 | rc = 0; | |
739 | goto out; | |
740 | } | |
741 | ||
089be43e | 742 | if (strcmp(sb->s_type->name, "proc") == 0) |
0d90a7ec | 743 | sbsec->flags |= SE_SBPROC; |
c9180a57 | 744 | |
eb9ae686 DQ |
745 | if (!sbsec->behavior) { |
746 | /* | |
747 | * Determine the labeling behavior to use for this | |
748 | * filesystem type. | |
749 | */ | |
98f700f3 | 750 | rc = security_fs_use(sb); |
eb9ae686 DQ |
751 | if (rc) { |
752 | printk(KERN_WARNING | |
753 | "%s: security_fs_use(%s) returned %d\n", | |
754 | __func__, sb->s_type->name, rc); | |
755 | goto out; | |
756 | } | |
c9180a57 | 757 | } |
c9180a57 EP |
758 | /* sets the context of the superblock for the fs being mounted. */ |
759 | if (fscontext_sid) { | |
275bb41e | 760 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
1da177e4 | 761 | if (rc) |
c9180a57 | 762 | goto out; |
1da177e4 | 763 | |
c9180a57 | 764 | sbsec->sid = fscontext_sid; |
c312feb2 EP |
765 | } |
766 | ||
767 | /* | |
768 | * Switch to using mount point labeling behavior. | |
769 | * sets the label used on all file below the mountpoint, and will set | |
770 | * the superblock context if not already set. | |
771 | */ | |
eb9ae686 DQ |
772 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { |
773 | sbsec->behavior = SECURITY_FS_USE_NATIVE; | |
774 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; | |
775 | } | |
776 | ||
c9180a57 EP |
777 | if (context_sid) { |
778 | if (!fscontext_sid) { | |
275bb41e DH |
779 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
780 | cred); | |
b04ea3ce | 781 | if (rc) |
c9180a57 EP |
782 | goto out; |
783 | sbsec->sid = context_sid; | |
b04ea3ce | 784 | } else { |
275bb41e DH |
785 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
786 | cred); | |
b04ea3ce | 787 | if (rc) |
c9180a57 | 788 | goto out; |
b04ea3ce | 789 | } |
c9180a57 EP |
790 | if (!rootcontext_sid) |
791 | rootcontext_sid = context_sid; | |
1da177e4 | 792 | |
c9180a57 | 793 | sbsec->mntpoint_sid = context_sid; |
c312feb2 | 794 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
1da177e4 LT |
795 | } |
796 | ||
c9180a57 | 797 | if (rootcontext_sid) { |
275bb41e DH |
798 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
799 | cred); | |
0808925e | 800 | if (rc) |
c9180a57 | 801 | goto out; |
0808925e | 802 | |
c9180a57 EP |
803 | root_isec->sid = rootcontext_sid; |
804 | root_isec->initialized = 1; | |
0808925e EP |
805 | } |
806 | ||
c9180a57 | 807 | if (defcontext_sid) { |
eb9ae686 DQ |
808 | if (sbsec->behavior != SECURITY_FS_USE_XATTR && |
809 | sbsec->behavior != SECURITY_FS_USE_NATIVE) { | |
c9180a57 EP |
810 | rc = -EINVAL; |
811 | printk(KERN_WARNING "SELinux: defcontext option is " | |
812 | "invalid for this filesystem type\n"); | |
813 | goto out; | |
1da177e4 LT |
814 | } |
815 | ||
c9180a57 EP |
816 | if (defcontext_sid != sbsec->def_sid) { |
817 | rc = may_context_mount_inode_relabel(defcontext_sid, | |
275bb41e | 818 | sbsec, cred); |
c9180a57 EP |
819 | if (rc) |
820 | goto out; | |
821 | } | |
1da177e4 | 822 | |
c9180a57 | 823 | sbsec->def_sid = defcontext_sid; |
1da177e4 LT |
824 | } |
825 | ||
c9180a57 | 826 | rc = sb_finish_set_opts(sb); |
1da177e4 | 827 | out: |
c9180a57 | 828 | mutex_unlock(&sbsec->lock); |
1da177e4 | 829 | return rc; |
c9180a57 EP |
830 | out_double_mount: |
831 | rc = -EINVAL; | |
832 | printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " | |
29b1deb2 | 833 | "security settings for (dev %s, type %s)\n", sb->s_id, name); |
c9180a57 | 834 | goto out; |
1da177e4 LT |
835 | } |
836 | ||
094f7b69 JL |
837 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
838 | const struct super_block *newsb) | |
839 | { | |
840 | struct superblock_security_struct *old = oldsb->s_security; | |
841 | struct superblock_security_struct *new = newsb->s_security; | |
842 | char oldflags = old->flags & SE_MNTMASK; | |
843 | char newflags = new->flags & SE_MNTMASK; | |
844 | ||
845 | if (oldflags != newflags) | |
846 | goto mismatch; | |
847 | if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) | |
848 | goto mismatch; | |
849 | if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) | |
850 | goto mismatch; | |
851 | if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) | |
852 | goto mismatch; | |
853 | if (oldflags & ROOTCONTEXT_MNT) { | |
854 | struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security; | |
855 | struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security; | |
856 | if (oldroot->sid != newroot->sid) | |
857 | goto mismatch; | |
858 | } | |
859 | return 0; | |
860 | mismatch: | |
861 | printk(KERN_WARNING "SELinux: mount invalid. Same superblock, " | |
862 | "different security settings for (dev %s, " | |
863 | "type %s)\n", newsb->s_id, newsb->s_type->name); | |
864 | return -EBUSY; | |
865 | } | |
866 | ||
867 | static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, | |
c9180a57 | 868 | struct super_block *newsb) |
1da177e4 | 869 | { |
c9180a57 EP |
870 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
871 | struct superblock_security_struct *newsbsec = newsb->s_security; | |
1da177e4 | 872 | |
c9180a57 EP |
873 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
874 | int set_context = (oldsbsec->flags & CONTEXT_MNT); | |
875 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); | |
1da177e4 | 876 | |
0f5e6420 EP |
877 | /* |
878 | * if the parent was able to be mounted it clearly had no special lsm | |
e8c26255 | 879 | * mount options. thus we can safely deal with this superblock later |
0f5e6420 | 880 | */ |
e8c26255 | 881 | if (!ss_initialized) |
094f7b69 | 882 | return 0; |
c9180a57 | 883 | |
c9180a57 | 884 | /* how can we clone if the old one wasn't set up?? */ |
0d90a7ec | 885 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
c9180a57 | 886 | |
094f7b69 | 887 | /* if fs is reusing a sb, make sure that the contexts match */ |
0d90a7ec | 888 | if (newsbsec->flags & SE_SBINITIALIZED) |
094f7b69 | 889 | return selinux_cmp_sb_context(oldsb, newsb); |
5a552617 | 890 | |
c9180a57 EP |
891 | mutex_lock(&newsbsec->lock); |
892 | ||
893 | newsbsec->flags = oldsbsec->flags; | |
894 | ||
895 | newsbsec->sid = oldsbsec->sid; | |
896 | newsbsec->def_sid = oldsbsec->def_sid; | |
897 | newsbsec->behavior = oldsbsec->behavior; | |
898 | ||
899 | if (set_context) { | |
900 | u32 sid = oldsbsec->mntpoint_sid; | |
901 | ||
902 | if (!set_fscontext) | |
903 | newsbsec->sid = sid; | |
904 | if (!set_rootcontext) { | |
905 | struct inode *newinode = newsb->s_root->d_inode; | |
906 | struct inode_security_struct *newisec = newinode->i_security; | |
907 | newisec->sid = sid; | |
908 | } | |
909 | newsbsec->mntpoint_sid = sid; | |
1da177e4 | 910 | } |
c9180a57 EP |
911 | if (set_rootcontext) { |
912 | const struct inode *oldinode = oldsb->s_root->d_inode; | |
913 | const struct inode_security_struct *oldisec = oldinode->i_security; | |
914 | struct inode *newinode = newsb->s_root->d_inode; | |
915 | struct inode_security_struct *newisec = newinode->i_security; | |
1da177e4 | 916 | |
c9180a57 | 917 | newisec->sid = oldisec->sid; |
1da177e4 LT |
918 | } |
919 | ||
c9180a57 EP |
920 | sb_finish_set_opts(newsb); |
921 | mutex_unlock(&newsbsec->lock); | |
094f7b69 | 922 | return 0; |
c9180a57 EP |
923 | } |
924 | ||
2e1479d9 AB |
925 | static int selinux_parse_opts_str(char *options, |
926 | struct security_mnt_opts *opts) | |
c9180a57 | 927 | { |
e0007529 | 928 | char *p; |
c9180a57 EP |
929 | char *context = NULL, *defcontext = NULL; |
930 | char *fscontext = NULL, *rootcontext = NULL; | |
e0007529 | 931 | int rc, num_mnt_opts = 0; |
1da177e4 | 932 | |
e0007529 | 933 | opts->num_mnt_opts = 0; |
1da177e4 | 934 | |
c9180a57 EP |
935 | /* Standard string-based options. */ |
936 | while ((p = strsep(&options, "|")) != NULL) { | |
937 | int token; | |
938 | substring_t args[MAX_OPT_ARGS]; | |
1da177e4 | 939 | |
c9180a57 EP |
940 | if (!*p) |
941 | continue; | |
1da177e4 | 942 | |
c9180a57 | 943 | token = match_token(p, tokens, args); |
1da177e4 | 944 | |
c9180a57 EP |
945 | switch (token) { |
946 | case Opt_context: | |
947 | if (context || defcontext) { | |
948 | rc = -EINVAL; | |
949 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); | |
950 | goto out_err; | |
951 | } | |
952 | context = match_strdup(&args[0]); | |
953 | if (!context) { | |
954 | rc = -ENOMEM; | |
955 | goto out_err; | |
956 | } | |
957 | break; | |
958 | ||
959 | case Opt_fscontext: | |
960 | if (fscontext) { | |
961 | rc = -EINVAL; | |
962 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); | |
963 | goto out_err; | |
964 | } | |
965 | fscontext = match_strdup(&args[0]); | |
966 | if (!fscontext) { | |
967 | rc = -ENOMEM; | |
968 | goto out_err; | |
969 | } | |
970 | break; | |
971 | ||
972 | case Opt_rootcontext: | |
973 | if (rootcontext) { | |
974 | rc = -EINVAL; | |
975 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); | |
976 | goto out_err; | |
977 | } | |
978 | rootcontext = match_strdup(&args[0]); | |
979 | if (!rootcontext) { | |
980 | rc = -ENOMEM; | |
981 | goto out_err; | |
982 | } | |
983 | break; | |
984 | ||
985 | case Opt_defcontext: | |
986 | if (context || defcontext) { | |
987 | rc = -EINVAL; | |
988 | printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); | |
989 | goto out_err; | |
990 | } | |
991 | defcontext = match_strdup(&args[0]); | |
992 | if (!defcontext) { | |
993 | rc = -ENOMEM; | |
994 | goto out_err; | |
995 | } | |
996 | break; | |
11689d47 DQ |
997 | case Opt_labelsupport: |
998 | break; | |
c9180a57 EP |
999 | default: |
1000 | rc = -EINVAL; | |
1001 | printk(KERN_WARNING "SELinux: unknown mount option\n"); | |
1002 | goto out_err; | |
1da177e4 | 1003 | |
1da177e4 | 1004 | } |
1da177e4 | 1005 | } |
c9180a57 | 1006 | |
e0007529 EP |
1007 | rc = -ENOMEM; |
1008 | opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); | |
1009 | if (!opts->mnt_opts) | |
1010 | goto out_err; | |
1011 | ||
1012 | opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); | |
1013 | if (!opts->mnt_opts_flags) { | |
1014 | kfree(opts->mnt_opts); | |
1015 | goto out_err; | |
1016 | } | |
1017 | ||
c9180a57 | 1018 | if (fscontext) { |
e0007529 EP |
1019 | opts->mnt_opts[num_mnt_opts] = fscontext; |
1020 | opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; | |
c9180a57 EP |
1021 | } |
1022 | if (context) { | |
e0007529 EP |
1023 | opts->mnt_opts[num_mnt_opts] = context; |
1024 | opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; | |
c9180a57 EP |
1025 | } |
1026 | if (rootcontext) { | |
e0007529 EP |
1027 | opts->mnt_opts[num_mnt_opts] = rootcontext; |
1028 | opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; | |
c9180a57 EP |
1029 | } |
1030 | if (defcontext) { | |
e0007529 EP |
1031 | opts->mnt_opts[num_mnt_opts] = defcontext; |
1032 | opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; | |
c9180a57 EP |
1033 | } |
1034 | ||
e0007529 EP |
1035 | opts->num_mnt_opts = num_mnt_opts; |
1036 | return 0; | |
1037 | ||
c9180a57 EP |
1038 | out_err: |
1039 | kfree(context); | |
1040 | kfree(defcontext); | |
1041 | kfree(fscontext); | |
1042 | kfree(rootcontext); | |
1da177e4 LT |
1043 | return rc; |
1044 | } | |
e0007529 EP |
1045 | /* |
1046 | * string mount options parsing and call set the sbsec | |
1047 | */ | |
1048 | static int superblock_doinit(struct super_block *sb, void *data) | |
1049 | { | |
1050 | int rc = 0; | |
1051 | char *options = data; | |
1052 | struct security_mnt_opts opts; | |
1053 | ||
1054 | security_init_mnt_opts(&opts); | |
1055 | ||
1056 | if (!data) | |
1057 | goto out; | |
1058 | ||
1059 | BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); | |
1060 | ||
1061 | rc = selinux_parse_opts_str(options, &opts); | |
1062 | if (rc) | |
1063 | goto out_err; | |
1064 | ||
1065 | out: | |
649f6e77 | 1066 | rc = selinux_set_mnt_opts(sb, &opts, 0, NULL); |
e0007529 EP |
1067 | |
1068 | out_err: | |
1069 | security_free_mnt_opts(&opts); | |
1070 | return rc; | |
1071 | } | |
1da177e4 | 1072 | |
3583a711 AB |
1073 | static void selinux_write_opts(struct seq_file *m, |
1074 | struct security_mnt_opts *opts) | |
2069f457 EP |
1075 | { |
1076 | int i; | |
1077 | char *prefix; | |
1078 | ||
1079 | for (i = 0; i < opts->num_mnt_opts; i++) { | |
11689d47 DQ |
1080 | char *has_comma; |
1081 | ||
1082 | if (opts->mnt_opts[i]) | |
1083 | has_comma = strchr(opts->mnt_opts[i], ','); | |
1084 | else | |
1085 | has_comma = NULL; | |
2069f457 EP |
1086 | |
1087 | switch (opts->mnt_opts_flags[i]) { | |
1088 | case CONTEXT_MNT: | |
1089 | prefix = CONTEXT_STR; | |
1090 | break; | |
1091 | case FSCONTEXT_MNT: | |
1092 | prefix = FSCONTEXT_STR; | |
1093 | break; | |
1094 | case ROOTCONTEXT_MNT: | |
1095 | prefix = ROOTCONTEXT_STR; | |
1096 | break; | |
1097 | case DEFCONTEXT_MNT: | |
1098 | prefix = DEFCONTEXT_STR; | |
1099 | break; | |
12f348b9 | 1100 | case SBLABEL_MNT: |
11689d47 DQ |
1101 | seq_putc(m, ','); |
1102 | seq_puts(m, LABELSUPP_STR); | |
1103 | continue; | |
2069f457 EP |
1104 | default: |
1105 | BUG(); | |
a35c6c83 | 1106 | return; |
2069f457 EP |
1107 | }; |
1108 | /* we need a comma before each option */ | |
1109 | seq_putc(m, ','); | |
1110 | seq_puts(m, prefix); | |
1111 | if (has_comma) | |
1112 | seq_putc(m, '\"'); | |
1113 | seq_puts(m, opts->mnt_opts[i]); | |
1114 | if (has_comma) | |
1115 | seq_putc(m, '\"'); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) | |
1120 | { | |
1121 | struct security_mnt_opts opts; | |
1122 | int rc; | |
1123 | ||
1124 | rc = selinux_get_mnt_opts(sb, &opts); | |
383795c2 EP |
1125 | if (rc) { |
1126 | /* before policy load we may get EINVAL, don't show anything */ | |
1127 | if (rc == -EINVAL) | |
1128 | rc = 0; | |
2069f457 | 1129 | return rc; |
383795c2 | 1130 | } |
2069f457 EP |
1131 | |
1132 | selinux_write_opts(m, &opts); | |
1133 | ||
1134 | security_free_mnt_opts(&opts); | |
1135 | ||
1136 | return rc; | |
1137 | } | |
1138 | ||
1da177e4 LT |
1139 | static inline u16 inode_mode_to_security_class(umode_t mode) |
1140 | { | |
1141 | switch (mode & S_IFMT) { | |
1142 | case S_IFSOCK: | |
1143 | return SECCLASS_SOCK_FILE; | |
1144 | case S_IFLNK: | |
1145 | return SECCLASS_LNK_FILE; | |
1146 | case S_IFREG: | |
1147 | return SECCLASS_FILE; | |
1148 | case S_IFBLK: | |
1149 | return SECCLASS_BLK_FILE; | |
1150 | case S_IFDIR: | |
1151 | return SECCLASS_DIR; | |
1152 | case S_IFCHR: | |
1153 | return SECCLASS_CHR_FILE; | |
1154 | case S_IFIFO: | |
1155 | return SECCLASS_FIFO_FILE; | |
1156 | ||
1157 | } | |
1158 | ||
1159 | return SECCLASS_FILE; | |
1160 | } | |
1161 | ||
13402580 JM |
1162 | static inline int default_protocol_stream(int protocol) |
1163 | { | |
1164 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); | |
1165 | } | |
1166 | ||
1167 | static inline int default_protocol_dgram(int protocol) | |
1168 | { | |
1169 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); | |
1170 | } | |
1171 | ||
1da177e4 LT |
1172 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
1173 | { | |
1174 | switch (family) { | |
1175 | case PF_UNIX: | |
1176 | switch (type) { | |
1177 | case SOCK_STREAM: | |
1178 | case SOCK_SEQPACKET: | |
1179 | return SECCLASS_UNIX_STREAM_SOCKET; | |
1180 | case SOCK_DGRAM: | |
1181 | return SECCLASS_UNIX_DGRAM_SOCKET; | |
1182 | } | |
1183 | break; | |
1184 | case PF_INET: | |
1185 | case PF_INET6: | |
1186 | switch (type) { | |
1187 | case SOCK_STREAM: | |
13402580 JM |
1188 | if (default_protocol_stream(protocol)) |
1189 | return SECCLASS_TCP_SOCKET; | |
1190 | else | |
1191 | return SECCLASS_RAWIP_SOCKET; | |
1da177e4 | 1192 | case SOCK_DGRAM: |
13402580 JM |
1193 | if (default_protocol_dgram(protocol)) |
1194 | return SECCLASS_UDP_SOCKET; | |
1195 | else | |
1196 | return SECCLASS_RAWIP_SOCKET; | |
2ee92d46 JM |
1197 | case SOCK_DCCP: |
1198 | return SECCLASS_DCCP_SOCKET; | |
13402580 | 1199 | default: |
1da177e4 LT |
1200 | return SECCLASS_RAWIP_SOCKET; |
1201 | } | |
1202 | break; | |
1203 | case PF_NETLINK: | |
1204 | switch (protocol) { | |
1205 | case NETLINK_ROUTE: | |
1206 | return SECCLASS_NETLINK_ROUTE_SOCKET; | |
1207 | case NETLINK_FIREWALL: | |
1208 | return SECCLASS_NETLINK_FIREWALL_SOCKET; | |
7f1fb60c | 1209 | case NETLINK_SOCK_DIAG: |
1da177e4 LT |
1210 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
1211 | case NETLINK_NFLOG: | |
1212 | return SECCLASS_NETLINK_NFLOG_SOCKET; | |
1213 | case NETLINK_XFRM: | |
1214 | return SECCLASS_NETLINK_XFRM_SOCKET; | |
1215 | case NETLINK_SELINUX: | |
1216 | return SECCLASS_NETLINK_SELINUX_SOCKET; | |
1217 | case NETLINK_AUDIT: | |
1218 | return SECCLASS_NETLINK_AUDIT_SOCKET; | |
1219 | case NETLINK_IP6_FW: | |
1220 | return SECCLASS_NETLINK_IP6FW_SOCKET; | |
1221 | case NETLINK_DNRTMSG: | |
1222 | return SECCLASS_NETLINK_DNRT_SOCKET; | |
0c9b7942 JM |
1223 | case NETLINK_KOBJECT_UEVENT: |
1224 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; | |
1da177e4 LT |
1225 | default: |
1226 | return SECCLASS_NETLINK_SOCKET; | |
1227 | } | |
1228 | case PF_PACKET: | |
1229 | return SECCLASS_PACKET_SOCKET; | |
1230 | case PF_KEY: | |
1231 | return SECCLASS_KEY_SOCKET; | |
3e3ff15e CP |
1232 | case PF_APPLETALK: |
1233 | return SECCLASS_APPLETALK_SOCKET; | |
1da177e4 LT |
1234 | } |
1235 | ||
1236 | return SECCLASS_SOCKET; | |
1237 | } | |
1238 | ||
1239 | #ifdef CONFIG_PROC_FS | |
8e6c9693 | 1240 | static int selinux_proc_get_sid(struct dentry *dentry, |
1da177e4 LT |
1241 | u16 tclass, |
1242 | u32 *sid) | |
1243 | { | |
8e6c9693 LAG |
1244 | int rc; |
1245 | char *buffer, *path; | |
1da177e4 | 1246 | |
828dfe1d | 1247 | buffer = (char *)__get_free_page(GFP_KERNEL); |
1da177e4 LT |
1248 | if (!buffer) |
1249 | return -ENOMEM; | |
1250 | ||
8e6c9693 LAG |
1251 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
1252 | if (IS_ERR(path)) | |
1253 | rc = PTR_ERR(path); | |
1254 | else { | |
1255 | /* each process gets a /proc/PID/ entry. Strip off the | |
1256 | * PID part to get a valid selinux labeling. | |
1257 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ | |
1258 | while (path[1] >= '0' && path[1] <= '9') { | |
1259 | path[1] = '/'; | |
1260 | path++; | |
1261 | } | |
1262 | rc = security_genfs_sid("proc", path, tclass, sid); | |
1da177e4 | 1263 | } |
1da177e4 LT |
1264 | free_page((unsigned long)buffer); |
1265 | return rc; | |
1266 | } | |
1267 | #else | |
8e6c9693 | 1268 | static int selinux_proc_get_sid(struct dentry *dentry, |
1da177e4 LT |
1269 | u16 tclass, |
1270 | u32 *sid) | |
1271 | { | |
1272 | return -EINVAL; | |
1273 | } | |
1274 | #endif | |
1275 | ||
1276 | /* The inode's security attributes must be initialized before first use. */ | |
1277 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) | |
1278 | { | |
1279 | struct superblock_security_struct *sbsec = NULL; | |
1280 | struct inode_security_struct *isec = inode->i_security; | |
1281 | u32 sid; | |
1282 | struct dentry *dentry; | |
1283 | #define INITCONTEXTLEN 255 | |
1284 | char *context = NULL; | |
1285 | unsigned len = 0; | |
1286 | int rc = 0; | |
1da177e4 LT |
1287 | |
1288 | if (isec->initialized) | |
1289 | goto out; | |
1290 | ||
23970741 | 1291 | mutex_lock(&isec->lock); |
1da177e4 | 1292 | if (isec->initialized) |
23970741 | 1293 | goto out_unlock; |
1da177e4 LT |
1294 | |
1295 | sbsec = inode->i_sb->s_security; | |
0d90a7ec | 1296 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
1da177e4 LT |
1297 | /* Defer initialization until selinux_complete_init, |
1298 | after the initial policy is loaded and the security | |
1299 | server is ready to handle calls. */ | |
1300 | spin_lock(&sbsec->isec_lock); | |
1301 | if (list_empty(&isec->list)) | |
1302 | list_add(&isec->list, &sbsec->isec_head); | |
1303 | spin_unlock(&sbsec->isec_lock); | |
23970741 | 1304 | goto out_unlock; |
1da177e4 LT |
1305 | } |
1306 | ||
1307 | switch (sbsec->behavior) { | |
eb9ae686 DQ |
1308 | case SECURITY_FS_USE_NATIVE: |
1309 | break; | |
1da177e4 LT |
1310 | case SECURITY_FS_USE_XATTR: |
1311 | if (!inode->i_op->getxattr) { | |
1312 | isec->sid = sbsec->def_sid; | |
1313 | break; | |
1314 | } | |
1315 | ||
1316 | /* Need a dentry, since the xattr API requires one. | |
1317 | Life would be simpler if we could just pass the inode. */ | |
1318 | if (opt_dentry) { | |
1319 | /* Called from d_instantiate or d_splice_alias. */ | |
1320 | dentry = dget(opt_dentry); | |
1321 | } else { | |
1322 | /* Called from selinux_complete_init, try to find a dentry. */ | |
1323 | dentry = d_find_alias(inode); | |
1324 | } | |
1325 | if (!dentry) { | |
df7f54c0 EP |
1326 | /* |
1327 | * this is can be hit on boot when a file is accessed | |
1328 | * before the policy is loaded. When we load policy we | |
1329 | * may find inodes that have no dentry on the | |
1330 | * sbsec->isec_head list. No reason to complain as these | |
1331 | * will get fixed up the next time we go through | |
1332 | * inode_doinit with a dentry, before these inodes could | |
1333 | * be used again by userspace. | |
1334 | */ | |
23970741 | 1335 | goto out_unlock; |
1da177e4 LT |
1336 | } |
1337 | ||
1338 | len = INITCONTEXTLEN; | |
4cb912f1 | 1339 | context = kmalloc(len+1, GFP_NOFS); |
1da177e4 LT |
1340 | if (!context) { |
1341 | rc = -ENOMEM; | |
1342 | dput(dentry); | |
23970741 | 1343 | goto out_unlock; |
1da177e4 | 1344 | } |
4cb912f1 | 1345 | context[len] = '\0'; |
1da177e4 LT |
1346 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, |
1347 | context, len); | |
1348 | if (rc == -ERANGE) { | |
314dabb8 JM |
1349 | kfree(context); |
1350 | ||
1da177e4 LT |
1351 | /* Need a larger buffer. Query for the right size. */ |
1352 | rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, | |
1353 | NULL, 0); | |
1354 | if (rc < 0) { | |
1355 | dput(dentry); | |
23970741 | 1356 | goto out_unlock; |
1da177e4 | 1357 | } |
1da177e4 | 1358 | len = rc; |
4cb912f1 | 1359 | context = kmalloc(len+1, GFP_NOFS); |
1da177e4 LT |
1360 | if (!context) { |
1361 | rc = -ENOMEM; | |
1362 | dput(dentry); | |
23970741 | 1363 | goto out_unlock; |
1da177e4 | 1364 | } |
4cb912f1 | 1365 | context[len] = '\0'; |
1da177e4 LT |
1366 | rc = inode->i_op->getxattr(dentry, |
1367 | XATTR_NAME_SELINUX, | |
1368 | context, len); | |
1369 | } | |
1370 | dput(dentry); | |
1371 | if (rc < 0) { | |
1372 | if (rc != -ENODATA) { | |
744ba35e | 1373 | printk(KERN_WARNING "SELinux: %s: getxattr returned " |
dd6f953a | 1374 | "%d for dev=%s ino=%ld\n", __func__, |
1da177e4 LT |
1375 | -rc, inode->i_sb->s_id, inode->i_ino); |
1376 | kfree(context); | |
23970741 | 1377 | goto out_unlock; |
1da177e4 LT |
1378 | } |
1379 | /* Map ENODATA to the default file SID */ | |
1380 | sid = sbsec->def_sid; | |
1381 | rc = 0; | |
1382 | } else { | |
f5c1d5b2 | 1383 | rc = security_context_to_sid_default(context, rc, &sid, |
869ab514 SS |
1384 | sbsec->def_sid, |
1385 | GFP_NOFS); | |
1da177e4 | 1386 | if (rc) { |
4ba0a8ad EP |
1387 | char *dev = inode->i_sb->s_id; |
1388 | unsigned long ino = inode->i_ino; | |
1389 | ||
1390 | if (rc == -EINVAL) { | |
1391 | if (printk_ratelimit()) | |
1392 | printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " | |
1393 | "context=%s. This indicates you may need to relabel the inode or the " | |
1394 | "filesystem in question.\n", ino, dev, context); | |
1395 | } else { | |
1396 | printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " | |
1397 | "returned %d for dev=%s ino=%ld\n", | |
1398 | __func__, context, -rc, dev, ino); | |
1399 | } | |
1da177e4 LT |
1400 | kfree(context); |
1401 | /* Leave with the unlabeled SID */ | |
1402 | rc = 0; | |
1403 | break; | |
1404 | } | |
1405 | } | |
1406 | kfree(context); | |
1407 | isec->sid = sid; | |
1408 | break; | |
1409 | case SECURITY_FS_USE_TASK: | |
1410 | isec->sid = isec->task_sid; | |
1411 | break; | |
1412 | case SECURITY_FS_USE_TRANS: | |
1413 | /* Default to the fs SID. */ | |
1414 | isec->sid = sbsec->sid; | |
1415 | ||
1416 | /* Try to obtain a transition SID. */ | |
1417 | isec->sclass = inode_mode_to_security_class(inode->i_mode); | |
652bb9b0 EP |
1418 | rc = security_transition_sid(isec->task_sid, sbsec->sid, |
1419 | isec->sclass, NULL, &sid); | |
1da177e4 | 1420 | if (rc) |
23970741 | 1421 | goto out_unlock; |
1da177e4 LT |
1422 | isec->sid = sid; |
1423 | break; | |
c312feb2 EP |
1424 | case SECURITY_FS_USE_MNTPOINT: |
1425 | isec->sid = sbsec->mntpoint_sid; | |
1426 | break; | |
1da177e4 | 1427 | default: |
c312feb2 | 1428 | /* Default to the fs superblock SID. */ |
1da177e4 LT |
1429 | isec->sid = sbsec->sid; |
1430 | ||
0d90a7ec | 1431 | if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { |
f64410ec PM |
1432 | /* We must have a dentry to determine the label on |
1433 | * procfs inodes */ | |
1434 | if (opt_dentry) | |
1435 | /* Called from d_instantiate or | |
1436 | * d_splice_alias. */ | |
1437 | dentry = dget(opt_dentry); | |
1438 | else | |
1439 | /* Called from selinux_complete_init, try to | |
1440 | * find a dentry. */ | |
1441 | dentry = d_find_alias(inode); | |
1442 | /* | |
1443 | * This can be hit on boot when a file is accessed | |
1444 | * before the policy is loaded. When we load policy we | |
1445 | * may find inodes that have no dentry on the | |
1446 | * sbsec->isec_head list. No reason to complain as | |
1447 | * these will get fixed up the next time we go through | |
1448 | * inode_doinit() with a dentry, before these inodes | |
1449 | * could be used again by userspace. | |
1450 | */ | |
1451 | if (!dentry) | |
1452 | goto out_unlock; | |
1453 | isec->sclass = inode_mode_to_security_class(inode->i_mode); | |
1454 | rc = selinux_proc_get_sid(dentry, isec->sclass, &sid); | |
1455 | dput(dentry); | |
1456 | if (rc) | |
1457 | goto out_unlock; | |
1458 | isec->sid = sid; | |
1da177e4 LT |
1459 | } |
1460 | break; | |
1461 | } | |
1462 | ||
1463 | isec->initialized = 1; | |
1464 | ||
23970741 EP |
1465 | out_unlock: |
1466 | mutex_unlock(&isec->lock); | |
1da177e4 LT |
1467 | out: |
1468 | if (isec->sclass == SECCLASS_FILE) | |
1469 | isec->sclass = inode_mode_to_security_class(inode->i_mode); | |
1da177e4 LT |
1470 | return rc; |
1471 | } | |
1472 | ||
1473 | /* Convert a Linux signal to an access vector. */ | |
1474 | static inline u32 signal_to_av(int sig) | |
1475 | { | |
1476 | u32 perm = 0; | |
1477 | ||
1478 | switch (sig) { | |
1479 | case SIGCHLD: | |
1480 | /* Commonly granted from child to parent. */ | |
1481 | perm = PROCESS__SIGCHLD; | |
1482 | break; | |
1483 | case SIGKILL: | |
1484 | /* Cannot be caught or ignored */ | |
1485 | perm = PROCESS__SIGKILL; | |
1486 | break; | |
1487 | case SIGSTOP: | |
1488 | /* Cannot be caught or ignored */ | |
1489 | perm = PROCESS__SIGSTOP; | |
1490 | break; | |
1491 | default: | |
1492 | /* All other signals. */ | |
1493 | perm = PROCESS__SIGNAL; | |
1494 | break; | |
1495 | } | |
1496 | ||
1497 | return perm; | |
1498 | } | |
1499 | ||
d84f4f99 DH |
1500 | /* |
1501 | * Check permission between a pair of credentials | |
1502 | * fork check, ptrace check, etc. | |
1503 | */ | |
1504 | static int cred_has_perm(const struct cred *actor, | |
1505 | const struct cred *target, | |
1506 | u32 perms) | |
1507 | { | |
1508 | u32 asid = cred_sid(actor), tsid = cred_sid(target); | |
1509 | ||
1510 | return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); | |
1511 | } | |
1512 | ||
275bb41e | 1513 | /* |
88e67f3b | 1514 | * Check permission between a pair of tasks, e.g. signal checks, |
275bb41e DH |
1515 | * fork check, ptrace check, etc. |
1516 | * tsk1 is the actor and tsk2 is the target | |
3b11a1de | 1517 | * - this uses the default subjective creds of tsk1 |
275bb41e DH |
1518 | */ |
1519 | static int task_has_perm(const struct task_struct *tsk1, | |
1520 | const struct task_struct *tsk2, | |
1da177e4 LT |
1521 | u32 perms) |
1522 | { | |
275bb41e DH |
1523 | const struct task_security_struct *__tsec1, *__tsec2; |
1524 | u32 sid1, sid2; | |
1da177e4 | 1525 | |
275bb41e DH |
1526 | rcu_read_lock(); |
1527 | __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; | |
1528 | __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; | |
1529 | rcu_read_unlock(); | |
1530 | return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); | |
1da177e4 LT |
1531 | } |
1532 | ||
3b11a1de DH |
1533 | /* |
1534 | * Check permission between current and another task, e.g. signal checks, | |
1535 | * fork check, ptrace check, etc. | |
1536 | * current is the actor and tsk2 is the target | |
1537 | * - this uses current's subjective creds | |
1538 | */ | |
1539 | static int current_has_perm(const struct task_struct *tsk, | |
1540 | u32 perms) | |
1541 | { | |
1542 | u32 sid, tsid; | |
1543 | ||
1544 | sid = current_sid(); | |
1545 | tsid = task_sid(tsk); | |
1546 | return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); | |
1547 | } | |
1548 | ||
b68e418c SS |
1549 | #if CAP_LAST_CAP > 63 |
1550 | #error Fix SELinux to handle capabilities > 63. | |
1551 | #endif | |
1552 | ||
1da177e4 | 1553 | /* Check whether a task is allowed to use a capability. */ |
6a9de491 | 1554 | static int cred_has_capability(const struct cred *cred, |
06112163 | 1555 | int cap, int audit) |
1da177e4 | 1556 | { |
2bf49690 | 1557 | struct common_audit_data ad; |
06112163 | 1558 | struct av_decision avd; |
b68e418c | 1559 | u16 sclass; |
3699c53c | 1560 | u32 sid = cred_sid(cred); |
b68e418c | 1561 | u32 av = CAP_TO_MASK(cap); |
06112163 | 1562 | int rc; |
1da177e4 | 1563 | |
50c205f5 | 1564 | ad.type = LSM_AUDIT_DATA_CAP; |
1da177e4 LT |
1565 | ad.u.cap = cap; |
1566 | ||
b68e418c SS |
1567 | switch (CAP_TO_INDEX(cap)) { |
1568 | case 0: | |
1569 | sclass = SECCLASS_CAPABILITY; | |
1570 | break; | |
1571 | case 1: | |
1572 | sclass = SECCLASS_CAPABILITY2; | |
1573 | break; | |
1574 | default: | |
1575 | printk(KERN_ERR | |
1576 | "SELinux: out of range capability %d\n", cap); | |
1577 | BUG(); | |
a35c6c83 | 1578 | return -EINVAL; |
b68e418c | 1579 | } |
06112163 | 1580 | |
275bb41e | 1581 | rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); |
9ade0cf4 | 1582 | if (audit == SECURITY_CAP_AUDIT) { |
ab354062 | 1583 | int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad); |
9ade0cf4 EP |
1584 | if (rc2) |
1585 | return rc2; | |
1586 | } | |
06112163 | 1587 | return rc; |
1da177e4 LT |
1588 | } |
1589 | ||
1590 | /* Check whether a task is allowed to use a system operation. */ | |
1591 | static int task_has_system(struct task_struct *tsk, | |
1592 | u32 perms) | |
1593 | { | |
275bb41e | 1594 | u32 sid = task_sid(tsk); |
1da177e4 | 1595 | |
275bb41e | 1596 | return avc_has_perm(sid, SECINITSID_KERNEL, |
1da177e4 LT |
1597 | SECCLASS_SYSTEM, perms, NULL); |
1598 | } | |
1599 | ||
1600 | /* Check whether a task has a particular permission to an inode. | |
1601 | The 'adp' parameter is optional and allows other audit | |
1602 | data to be passed (e.g. the dentry). */ | |
88e67f3b | 1603 | static int inode_has_perm(const struct cred *cred, |
1da177e4 LT |
1604 | struct inode *inode, |
1605 | u32 perms, | |
19e49834 | 1606 | struct common_audit_data *adp) |
1da177e4 | 1607 | { |
1da177e4 | 1608 | struct inode_security_struct *isec; |
275bb41e | 1609 | u32 sid; |
1da177e4 | 1610 | |
e0e81739 DH |
1611 | validate_creds(cred); |
1612 | ||
828dfe1d | 1613 | if (unlikely(IS_PRIVATE(inode))) |
bbaca6c2 SS |
1614 | return 0; |
1615 | ||
88e67f3b | 1616 | sid = cred_sid(cred); |
1da177e4 LT |
1617 | isec = inode->i_security; |
1618 | ||
19e49834 | 1619 | return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); |
1da177e4 LT |
1620 | } |
1621 | ||
1622 | /* Same as inode_has_perm, but pass explicit audit data containing | |
1623 | the dentry to help the auditing code to more easily generate the | |
1624 | pathname if needed. */ | |
88e67f3b | 1625 | static inline int dentry_has_perm(const struct cred *cred, |
1da177e4 LT |
1626 | struct dentry *dentry, |
1627 | u32 av) | |
1628 | { | |
1629 | struct inode *inode = dentry->d_inode; | |
2bf49690 | 1630 | struct common_audit_data ad; |
88e67f3b | 1631 | |
50c205f5 | 1632 | ad.type = LSM_AUDIT_DATA_DENTRY; |
2875fa00 | 1633 | ad.u.dentry = dentry; |
19e49834 | 1634 | return inode_has_perm(cred, inode, av, &ad); |
2875fa00 EP |
1635 | } |
1636 | ||
1637 | /* Same as inode_has_perm, but pass explicit audit data containing | |
1638 | the path to help the auditing code to more easily generate the | |
1639 | pathname if needed. */ | |
1640 | static inline int path_has_perm(const struct cred *cred, | |
1641 | struct path *path, | |
1642 | u32 av) | |
1643 | { | |
1644 | struct inode *inode = path->dentry->d_inode; | |
1645 | struct common_audit_data ad; | |
1646 | ||
50c205f5 | 1647 | ad.type = LSM_AUDIT_DATA_PATH; |
2875fa00 | 1648 | ad.u.path = *path; |
19e49834 | 1649 | return inode_has_perm(cred, inode, av, &ad); |
1da177e4 LT |
1650 | } |
1651 | ||
13f8e981 DH |
1652 | /* Same as path_has_perm, but uses the inode from the file struct. */ |
1653 | static inline int file_path_has_perm(const struct cred *cred, | |
1654 | struct file *file, | |
1655 | u32 av) | |
1656 | { | |
1657 | struct common_audit_data ad; | |
1658 | ||
1659 | ad.type = LSM_AUDIT_DATA_PATH; | |
1660 | ad.u.path = file->f_path; | |
19e49834 | 1661 | return inode_has_perm(cred, file_inode(file), av, &ad); |
13f8e981 DH |
1662 | } |
1663 | ||
1da177e4 LT |
1664 | /* Check whether a task can use an open file descriptor to |
1665 | access an inode in a given way. Check access to the | |
1666 | descriptor itself, and then use dentry_has_perm to | |
1667 | check a particular permission to the file. | |
1668 | Access to the descriptor is implicitly granted if it | |
1669 | has the same SID as the process. If av is zero, then | |
1670 | access to the file is not checked, e.g. for cases | |
1671 | where only the descriptor is affected like seek. */ | |
88e67f3b DH |
1672 | static int file_has_perm(const struct cred *cred, |
1673 | struct file *file, | |
1674 | u32 av) | |
1da177e4 | 1675 | { |
1da177e4 | 1676 | struct file_security_struct *fsec = file->f_security; |
496ad9aa | 1677 | struct inode *inode = file_inode(file); |
2bf49690 | 1678 | struct common_audit_data ad; |
88e67f3b | 1679 | u32 sid = cred_sid(cred); |
1da177e4 LT |
1680 | int rc; |
1681 | ||
50c205f5 | 1682 | ad.type = LSM_AUDIT_DATA_PATH; |
f48b7399 | 1683 | ad.u.path = file->f_path; |
1da177e4 | 1684 | |
275bb41e DH |
1685 | if (sid != fsec->sid) { |
1686 | rc = avc_has_perm(sid, fsec->sid, | |
1da177e4 LT |
1687 | SECCLASS_FD, |
1688 | FD__USE, | |
1689 | &ad); | |
1690 | if (rc) | |
88e67f3b | 1691 | goto out; |
1da177e4 LT |
1692 | } |
1693 | ||
1694 | /* av is zero if only checking access to the descriptor. */ | |
88e67f3b | 1695 | rc = 0; |
1da177e4 | 1696 | if (av) |
19e49834 | 1697 | rc = inode_has_perm(cred, inode, av, &ad); |
1da177e4 | 1698 | |
88e67f3b DH |
1699 | out: |
1700 | return rc; | |
1da177e4 LT |
1701 | } |
1702 | ||
1703 | /* Check whether a task can create a file. */ | |
1704 | static int may_create(struct inode *dir, | |
1705 | struct dentry *dentry, | |
1706 | u16 tclass) | |
1707 | { | |
5fb49870 | 1708 | const struct task_security_struct *tsec = current_security(); |
1da177e4 LT |
1709 | struct inode_security_struct *dsec; |
1710 | struct superblock_security_struct *sbsec; | |
275bb41e | 1711 | u32 sid, newsid; |
2bf49690 | 1712 | struct common_audit_data ad; |
1da177e4 LT |
1713 | int rc; |
1714 | ||
1da177e4 LT |
1715 | dsec = dir->i_security; |
1716 | sbsec = dir->i_sb->s_security; | |
1717 | ||
275bb41e DH |
1718 | sid = tsec->sid; |
1719 | newsid = tsec->create_sid; | |
1720 | ||
50c205f5 | 1721 | ad.type = LSM_AUDIT_DATA_DENTRY; |
a269434d | 1722 | ad.u.dentry = dentry; |
1da177e4 | 1723 | |
275bb41e | 1724 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, |
1da177e4 LT |
1725 | DIR__ADD_NAME | DIR__SEARCH, |
1726 | &ad); | |
1727 | if (rc) | |
1728 | return rc; | |
1729 | ||
12f348b9 | 1730 | if (!newsid || !(sbsec->flags & SBLABEL_MNT)) { |
cb1e922f EP |
1731 | rc = security_transition_sid(sid, dsec->sid, tclass, |
1732 | &dentry->d_name, &newsid); | |
1da177e4 LT |
1733 | if (rc) |
1734 | return rc; | |
1735 | } | |
1736 | ||
275bb41e | 1737 | rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); |
1da177e4 LT |
1738 | if (rc) |
1739 | return rc; | |
1740 | ||
1741 | return avc_has_perm(newsid, sbsec->sid, | |
1742 | SECCLASS_FILESYSTEM, | |
1743 | FILESYSTEM__ASSOCIATE, &ad); | |
1744 | } | |
1745 | ||
4eb582cf ML |
1746 | /* Check whether a task can create a key. */ |
1747 | static int may_create_key(u32 ksid, | |
1748 | struct task_struct *ctx) | |
1749 | { | |
275bb41e | 1750 | u32 sid = task_sid(ctx); |
4eb582cf | 1751 | |
275bb41e | 1752 | return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); |
4eb582cf ML |
1753 | } |
1754 | ||
828dfe1d EP |
1755 | #define MAY_LINK 0 |
1756 | #define MAY_UNLINK 1 | |
1757 | #define MAY_RMDIR 2 | |
1da177e4 LT |
1758 | |
1759 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ | |
1760 | static int may_link(struct inode *dir, | |
1761 | struct dentry *dentry, | |
1762 | int kind) | |
1763 | ||
1764 | { | |
1da177e4 | 1765 | struct inode_security_struct *dsec, *isec; |
2bf49690 | 1766 | struct common_audit_data ad; |
275bb41e | 1767 | u32 sid = current_sid(); |
1da177e4 LT |
1768 | u32 av; |
1769 | int rc; | |
1770 | ||
1da177e4 LT |
1771 | dsec = dir->i_security; |
1772 | isec = dentry->d_inode->i_security; | |
1773 | ||
50c205f5 | 1774 | ad.type = LSM_AUDIT_DATA_DENTRY; |
a269434d | 1775 | ad.u.dentry = dentry; |
1da177e4 LT |
1776 | |
1777 | av = DIR__SEARCH; | |
1778 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); | |
275bb41e | 1779 | rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); |
1da177e4 LT |
1780 | if (rc) |
1781 | return rc; | |
1782 | ||
1783 | switch (kind) { | |
1784 | case MAY_LINK: | |
1785 | av = FILE__LINK; | |
1786 | break; | |
1787 | case MAY_UNLINK: | |
1788 | av = FILE__UNLINK; | |
1789 | break; | |
1790 | case MAY_RMDIR: | |
1791 | av = DIR__RMDIR; | |
1792 | break; | |
1793 | default: | |
744ba35e EP |
1794 | printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", |
1795 | __func__, kind); | |
1da177e4 LT |
1796 | return 0; |
1797 | } | |
1798 | ||
275bb41e | 1799 | rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); |
1da177e4 LT |
1800 | return rc; |
1801 | } | |
1802 | ||
1803 | static inline int may_rename(struct inode *old_dir, | |
1804 | struct dentry *old_dentry, | |
1805 | struct inode *new_dir, | |
1806 | struct dentry *new_dentry) | |
1807 | { | |
1da177e4 | 1808 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
2bf49690 | 1809 | struct common_audit_data ad; |
275bb41e | 1810 | u32 sid = current_sid(); |
1da177e4 LT |
1811 | u32 av; |
1812 | int old_is_dir, new_is_dir; | |
1813 | int rc; | |
1814 | ||
1da177e4 LT |
1815 | old_dsec = old_dir->i_security; |
1816 | old_isec = old_dentry->d_inode->i_security; | |
1817 | old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); | |
1818 | new_dsec = new_dir->i_security; | |
1819 | ||
50c205f5 | 1820 | ad.type = LSM_AUDIT_DATA_DENTRY; |
1da177e4 | 1821 | |
a269434d | 1822 | ad.u.dentry = old_dentry; |
275bb41e | 1823 | rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, |
1da177e4 LT |
1824 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
1825 | if (rc) | |
1826 | return rc; | |
275bb41e | 1827 | rc = avc_has_perm(sid, old_isec->sid, |
1da177e4 LT |
1828 | old_isec->sclass, FILE__RENAME, &ad); |
1829 | if (rc) | |
1830 | return rc; | |
1831 | if (old_is_dir && new_dir != old_dir) { | |
275bb41e | 1832 | rc = avc_has_perm(sid, old_isec->sid, |
1da177e4 LT |
1833 | old_isec->sclass, DIR__REPARENT, &ad); |
1834 | if (rc) | |
1835 | return rc; | |
1836 | } | |
1837 | ||
a269434d | 1838 | ad.u.dentry = new_dentry; |
1da177e4 LT |
1839 | av = DIR__ADD_NAME | DIR__SEARCH; |
1840 | if (new_dentry->d_inode) | |
1841 | av |= DIR__REMOVE_NAME; | |
275bb41e | 1842 | rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
1da177e4 LT |
1843 | if (rc) |
1844 | return rc; | |
1845 | if (new_dentry->d_inode) { | |
1846 | new_isec = new_dentry->d_inode->i_security; | |
1847 | new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); | |
275bb41e | 1848 | rc = avc_has_perm(sid, new_isec->sid, |
1da177e4 LT |
1849 | new_isec->sclass, |
1850 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); | |
1851 | if (rc) | |
1852 | return rc; | |
1853 | } | |
1854 | ||
1855 | return 0; | |
1856 | } | |
1857 | ||
1858 | /* Check whether a task can perform a filesystem operation. */ | |
88e67f3b | 1859 | static int superblock_has_perm(const struct cred *cred, |
1da177e4 LT |
1860 | struct super_block *sb, |
1861 | u32 perms, | |
2bf49690 | 1862 | struct common_audit_data *ad) |
1da177e4 | 1863 | { |
1da177e4 | 1864 | struct superblock_security_struct *sbsec; |
88e67f3b | 1865 | u32 sid = cred_sid(cred); |
1da177e4 | 1866 | |
1da177e4 | 1867 | sbsec = sb->s_security; |
275bb41e | 1868 | return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
1da177e4 LT |
1869 | } |
1870 | ||
1871 | /* Convert a Linux mode and permission mask to an access vector. */ | |
1872 | static inline u32 file_mask_to_av(int mode, int mask) | |
1873 | { | |
1874 | u32 av = 0; | |
1875 | ||
dba19c60 | 1876 | if (!S_ISDIR(mode)) { |
1da177e4 LT |
1877 | if (mask & MAY_EXEC) |
1878 | av |= FILE__EXECUTE; | |
1879 | if (mask & MAY_READ) | |
1880 | av |= FILE__READ; | |
1881 | ||
1882 | if (mask & MAY_APPEND) | |
1883 | av |= FILE__APPEND; | |
1884 | else if (mask & MAY_WRITE) | |
1885 | av |= FILE__WRITE; | |
1886 | ||
1887 | } else { | |
1888 | if (mask & MAY_EXEC) | |
1889 | av |= DIR__SEARCH; | |
1890 | if (mask & MAY_WRITE) | |
1891 | av |= DIR__WRITE; | |
1892 | if (mask & MAY_READ) | |
1893 | av |= DIR__READ; | |
1894 | } | |
1895 | ||
1896 | return av; | |
1897 | } | |
1898 | ||
8b6a5a37 EP |
1899 | /* Convert a Linux file to an access vector. */ |
1900 | static inline u32 file_to_av(struct file *file) | |
1901 | { | |
1902 | u32 av = 0; | |
1903 | ||
1904 | if (file->f_mode & FMODE_READ) | |
1905 | av |= FILE__READ; | |
1906 | if (file->f_mode & FMODE_WRITE) { | |
1907 | if (file->f_flags & O_APPEND) | |
1908 | av |= FILE__APPEND; | |
1909 | else | |
1910 | av |= FILE__WRITE; | |
1911 | } | |
1912 | if (!av) { | |
1913 | /* | |
1914 | * Special file opened with flags 3 for ioctl-only use. | |
1915 | */ | |
1916 | av = FILE__IOCTL; | |
1917 | } | |
1918 | ||
1919 | return av; | |
1920 | } | |
1921 | ||
b0c636b9 | 1922 | /* |
8b6a5a37 | 1923 | * Convert a file to an access vector and include the correct open |
b0c636b9 EP |
1924 | * open permission. |
1925 | */ | |
8b6a5a37 | 1926 | static inline u32 open_file_to_av(struct file *file) |
b0c636b9 | 1927 | { |
8b6a5a37 | 1928 | u32 av = file_to_av(file); |
b0c636b9 | 1929 | |
49b7b8de EP |
1930 | if (selinux_policycap_openperm) |
1931 | av |= FILE__OPEN; | |
1932 | ||
b0c636b9 EP |
1933 | return av; |
1934 | } | |
1935 | ||
1da177e4 LT |
1936 | /* Hook functions begin here. */ |
1937 | ||
9e48858f | 1938 | static int selinux_ptrace_access_check(struct task_struct *child, |
5cd9c58f | 1939 | unsigned int mode) |
1da177e4 | 1940 | { |
1da177e4 LT |
1941 | int rc; |
1942 | ||
9e48858f | 1943 | rc = cap_ptrace_access_check(child, mode); |
1da177e4 LT |
1944 | if (rc) |
1945 | return rc; | |
1946 | ||
69f594a3 | 1947 | if (mode & PTRACE_MODE_READ) { |
275bb41e DH |
1948 | u32 sid = current_sid(); |
1949 | u32 csid = task_sid(child); | |
1950 | return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); | |
006ebb40 SS |
1951 | } |
1952 | ||
3b11a1de | 1953 | return current_has_perm(child, PROCESS__PTRACE); |
5cd9c58f DH |
1954 | } |
1955 | ||
1956 | static int selinux_ptrace_traceme(struct task_struct *parent) | |
1957 | { | |
1958 | int rc; | |
1959 | ||
200ac532 | 1960 | rc = cap_ptrace_traceme(parent); |
5cd9c58f DH |
1961 | if (rc) |
1962 | return rc; | |
1963 | ||
1964 | return task_has_perm(parent, current, PROCESS__PTRACE); | |
1da177e4 LT |
1965 | } |
1966 | ||
1967 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, | |
828dfe1d | 1968 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
1da177e4 LT |
1969 | { |
1970 | int error; | |
1971 | ||
3b11a1de | 1972 | error = current_has_perm(target, PROCESS__GETCAP); |
1da177e4 LT |
1973 | if (error) |
1974 | return error; | |
1975 | ||
200ac532 | 1976 | return cap_capget(target, effective, inheritable, permitted); |
1da177e4 LT |
1977 | } |
1978 | ||
d84f4f99 DH |
1979 | static int selinux_capset(struct cred *new, const struct cred *old, |
1980 | const kernel_cap_t *effective, | |
1981 | const kernel_cap_t *inheritable, | |
1982 | const kernel_cap_t *permitted) | |
1da177e4 LT |
1983 | { |
1984 | int error; | |
1985 | ||
200ac532 | 1986 | error = cap_capset(new, old, |
d84f4f99 | 1987 | effective, inheritable, permitted); |
1da177e4 LT |
1988 | if (error) |
1989 | return error; | |
1990 | ||
d84f4f99 | 1991 | return cred_has_perm(old, new, PROCESS__SETCAP); |
1da177e4 LT |
1992 | } |
1993 | ||
5626d3e8 JM |
1994 | /* |
1995 | * (This comment used to live with the selinux_task_setuid hook, | |
1996 | * which was removed). | |
1997 | * | |
1998 | * Since setuid only affects the current process, and since the SELinux | |
1999 | * controls are not based on the Linux identity attributes, SELinux does not | |
2000 | * need to control this operation. However, SELinux does control the use of | |
2001 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. | |
2002 | */ | |
2003 | ||
6a9de491 EP |
2004 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
2005 | int cap, int audit) | |
1da177e4 LT |
2006 | { |
2007 | int rc; | |
2008 | ||
6a9de491 | 2009 | rc = cap_capable(cred, ns, cap, audit); |
1da177e4 LT |
2010 | if (rc) |
2011 | return rc; | |
2012 | ||
6a9de491 | 2013 | return cred_has_capability(cred, cap, audit); |
1da177e4 LT |
2014 | } |
2015 | ||
1da177e4 LT |
2016 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
2017 | { | |
88e67f3b | 2018 | const struct cred *cred = current_cred(); |
1da177e4 LT |
2019 | int rc = 0; |
2020 | ||
2021 | if (!sb) | |
2022 | return 0; | |
2023 | ||
2024 | switch (cmds) { | |
828dfe1d EP |
2025 | case Q_SYNC: |
2026 | case Q_QUOTAON: | |
2027 | case Q_QUOTAOFF: | |
2028 | case Q_SETINFO: | |
2029 | case Q_SETQUOTA: | |
88e67f3b | 2030 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
828dfe1d EP |
2031 | break; |
2032 | case Q_GETFMT: | |
2033 | case Q_GETINFO: | |
2034 | case Q_GETQUOTA: | |
88e67f3b | 2035 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
828dfe1d EP |
2036 | break; |
2037 | default: | |
2038 | rc = 0; /* let the kernel handle invalid cmds */ | |
2039 | break; | |
1da177e4 LT |
2040 | } |
2041 | return rc; | |
2042 | } | |
2043 | ||
2044 | static int selinux_quota_on(struct dentry *dentry) | |
2045 | { | |
88e67f3b DH |
2046 | const struct cred *cred = current_cred(); |
2047 | ||
2875fa00 | 2048 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
1da177e4 LT |
2049 | } |
2050 | ||
12b3052c | 2051 | static int selinux_syslog(int type) |
1da177e4 LT |
2052 | { |
2053 | int rc; | |
2054 | ||
1da177e4 | 2055 | switch (type) { |
d78ca3cd KC |
2056 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
2057 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ | |
828dfe1d EP |
2058 | rc = task_has_system(current, SYSTEM__SYSLOG_READ); |
2059 | break; | |
d78ca3cd KC |
2060 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
2061 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ | |
2062 | /* Set level of messages printed to console */ | |
2063 | case SYSLOG_ACTION_CONSOLE_LEVEL: | |
828dfe1d EP |
2064 | rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); |
2065 | break; | |
d78ca3cd KC |
2066 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
2067 | case SYSLOG_ACTION_OPEN: /* Open log */ | |
2068 | case SYSLOG_ACTION_READ: /* Read from log */ | |
2069 | case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ | |
2070 | case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ | |
828dfe1d EP |
2071 | default: |
2072 | rc = task_has_system(current, SYSTEM__SYSLOG_MOD); | |
2073 | break; | |
1da177e4 LT |
2074 | } |
2075 | return rc; | |
2076 | } | |
2077 | ||
2078 | /* | |
2079 | * Check that a process has enough memory to allocate a new virtual | |
2080 | * mapping. 0 means there is enough memory for the allocation to | |
2081 | * succeed and -ENOMEM implies there is not. | |
2082 | * | |
1da177e4 LT |
2083 | * Do not audit the selinux permission check, as this is applied to all |
2084 | * processes that allocate mappings. | |
2085 | */ | |
34b4e4aa | 2086 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
1da177e4 LT |
2087 | { |
2088 | int rc, cap_sys_admin = 0; | |
1da177e4 | 2089 | |
6a9de491 | 2090 | rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, |
3699c53c | 2091 | SECURITY_CAP_NOAUDIT); |
1da177e4 LT |
2092 | if (rc == 0) |
2093 | cap_sys_admin = 1; | |
2094 | ||
34b4e4aa | 2095 | return __vm_enough_memory(mm, pages, cap_sys_admin); |
1da177e4 LT |
2096 | } |
2097 | ||
2098 | /* binprm security operations */ | |
2099 | ||
a6f76f23 | 2100 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
1da177e4 | 2101 | { |
a6f76f23 DH |
2102 | const struct task_security_struct *old_tsec; |
2103 | struct task_security_struct *new_tsec; | |
1da177e4 | 2104 | struct inode_security_struct *isec; |
2bf49690 | 2105 | struct common_audit_data ad; |
496ad9aa | 2106 | struct inode *inode = file_inode(bprm->file); |
1da177e4 LT |
2107 | int rc; |
2108 | ||
200ac532 | 2109 | rc = cap_bprm_set_creds(bprm); |
1da177e4 LT |
2110 | if (rc) |
2111 | return rc; | |
2112 | ||
a6f76f23 DH |
2113 | /* SELinux context only depends on initial program or script and not |
2114 | * the script interpreter */ | |
2115 | if (bprm->cred_prepared) | |
1da177e4 LT |
2116 | return 0; |
2117 | ||
a6f76f23 DH |
2118 | old_tsec = current_security(); |
2119 | new_tsec = bprm->cred->security; | |
1da177e4 LT |
2120 | isec = inode->i_security; |
2121 | ||
2122 | /* Default to the current task SID. */ | |
a6f76f23 DH |
2123 | new_tsec->sid = old_tsec->sid; |
2124 | new_tsec->osid = old_tsec->sid; | |
1da177e4 | 2125 | |
28eba5bf | 2126 | /* Reset fs, key, and sock SIDs on execve. */ |
a6f76f23 DH |
2127 | new_tsec->create_sid = 0; |
2128 | new_tsec->keycreate_sid = 0; | |
2129 | new_tsec->sockcreate_sid = 0; | |
1da177e4 | 2130 | |
a6f76f23 DH |
2131 | if (old_tsec->exec_sid) { |
2132 | new_tsec->sid = old_tsec->exec_sid; | |
1da177e4 | 2133 | /* Reset exec SID on execve. */ |
a6f76f23 | 2134 | new_tsec->exec_sid = 0; |
259e5e6c AL |
2135 | |
2136 | /* | |
4f189988 PM |
2137 | * Minimize confusion: if no_new_privs or nosuid and a |
2138 | * transition is explicitly requested, then fail the exec. | |
259e5e6c AL |
2139 | */ |
2140 | if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) | |
2141 | return -EPERM; | |
4f189988 PM |
2142 | if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) |
2143 | return -EACCES; | |
1da177e4 LT |
2144 | } else { |
2145 | /* Check for a default transition on this program. */ | |
a6f76f23 | 2146 | rc = security_transition_sid(old_tsec->sid, isec->sid, |
652bb9b0 EP |
2147 | SECCLASS_PROCESS, NULL, |
2148 | &new_tsec->sid); | |
1da177e4 LT |
2149 | if (rc) |
2150 | return rc; | |
2151 | } | |
2152 | ||
50c205f5 | 2153 | ad.type = LSM_AUDIT_DATA_PATH; |
f48b7399 | 2154 | ad.u.path = bprm->file->f_path; |
1da177e4 | 2155 | |
259e5e6c AL |
2156 | if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) || |
2157 | (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) | |
a6f76f23 | 2158 | new_tsec->sid = old_tsec->sid; |
1da177e4 | 2159 | |
a6f76f23 DH |
2160 | if (new_tsec->sid == old_tsec->sid) { |
2161 | rc = avc_has_perm(old_tsec->sid, isec->sid, | |
1da177e4 LT |
2162 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
2163 | if (rc) | |
2164 | return rc; | |
2165 | } else { | |
2166 | /* Check permissions for the transition. */ | |
a6f76f23 | 2167 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, |
1da177e4 LT |
2168 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
2169 | if (rc) | |
2170 | return rc; | |
2171 | ||
a6f76f23 | 2172 | rc = avc_has_perm(new_tsec->sid, isec->sid, |
1da177e4 LT |
2173 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
2174 | if (rc) | |
2175 | return rc; | |
2176 | ||
a6f76f23 DH |
2177 | /* Check for shared state */ |
2178 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { | |
2179 | rc = avc_has_perm(old_tsec->sid, new_tsec->sid, | |
2180 | SECCLASS_PROCESS, PROCESS__SHARE, | |
2181 | NULL); | |
2182 | if (rc) | |
2183 | return -EPERM; | |
2184 | } | |
2185 | ||
2186 | /* Make sure that anyone attempting to ptrace over a task that | |
2187 | * changes its SID has the appropriate permit */ | |
2188 | if (bprm->unsafe & | |
2189 | (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { | |
2190 | struct task_struct *tracer; | |
2191 | struct task_security_struct *sec; | |
2192 | u32 ptsid = 0; | |
2193 | ||
2194 | rcu_read_lock(); | |
06d98473 | 2195 | tracer = ptrace_parent(current); |
a6f76f23 DH |
2196 | if (likely(tracer != NULL)) { |
2197 | sec = __task_cred(tracer)->security; | |
2198 | ptsid = sec->sid; | |
2199 | } | |
2200 | rcu_read_unlock(); | |
2201 | ||
2202 | if (ptsid != 0) { | |
2203 | rc = avc_has_perm(ptsid, new_tsec->sid, | |
2204 | SECCLASS_PROCESS, | |
2205 | PROCESS__PTRACE, NULL); | |
2206 | if (rc) | |
2207 | return -EPERM; | |
2208 | } | |
2209 | } | |
1da177e4 | 2210 | |
a6f76f23 DH |
2211 | /* Clear any possibly unsafe personality bits on exec: */ |
2212 | bprm->per_clear |= PER_CLEAR_ON_SETID; | |
1da177e4 LT |
2213 | } |
2214 | ||
1da177e4 LT |
2215 | return 0; |
2216 | } | |
2217 | ||
828dfe1d | 2218 | static int selinux_bprm_secureexec(struct linux_binprm *bprm) |
1da177e4 | 2219 | { |
5fb49870 | 2220 | const struct task_security_struct *tsec = current_security(); |
275bb41e | 2221 | u32 sid, osid; |
1da177e4 LT |
2222 | int atsecure = 0; |
2223 | ||
275bb41e DH |
2224 | sid = tsec->sid; |
2225 | osid = tsec->osid; | |
2226 | ||
2227 | if (osid != sid) { | |
1da177e4 LT |
2228 | /* Enable secure mode for SIDs transitions unless |
2229 | the noatsecure permission is granted between | |
2230 | the two SIDs, i.e. ahp returns 0. */ | |
275bb41e | 2231 | atsecure = avc_has_perm(osid, sid, |
a6f76f23 DH |
2232 | SECCLASS_PROCESS, |
2233 | PROCESS__NOATSECURE, NULL); | |
1da177e4 LT |
2234 | } |
2235 | ||
200ac532 | 2236 | return (atsecure || cap_bprm_secureexec(bprm)); |
1da177e4 LT |
2237 | } |
2238 | ||
c3c073f8 AV |
2239 | static int match_file(const void *p, struct file *file, unsigned fd) |
2240 | { | |
2241 | return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; | |
2242 | } | |
2243 | ||
1da177e4 | 2244 | /* Derived from fs/exec.c:flush_old_files. */ |
745ca247 DH |
2245 | static inline void flush_unauthorized_files(const struct cred *cred, |
2246 | struct files_struct *files) | |
1da177e4 | 2247 | { |
1da177e4 | 2248 | struct file *file, *devnull = NULL; |
b20c8122 | 2249 | struct tty_struct *tty; |
24ec839c | 2250 | int drop_tty = 0; |
c3c073f8 | 2251 | unsigned n; |
1da177e4 | 2252 | |
24ec839c | 2253 | tty = get_current_tty(); |
1da177e4 | 2254 | if (tty) { |
ee2ffa0d | 2255 | spin_lock(&tty_files_lock); |
37dd0bd0 | 2256 | if (!list_empty(&tty->tty_files)) { |
d996b62a | 2257 | struct tty_file_private *file_priv; |
37dd0bd0 | 2258 | |
1da177e4 | 2259 | /* Revalidate access to controlling tty. |
13f8e981 DH |
2260 | Use file_path_has_perm on the tty path directly |
2261 | rather than using file_has_perm, as this particular | |
2262 | open file may belong to another process and we are | |
2263 | only interested in the inode-based check here. */ | |
d996b62a NP |
2264 | file_priv = list_first_entry(&tty->tty_files, |
2265 | struct tty_file_private, list); | |
2266 | file = file_priv->file; | |
13f8e981 | 2267 | if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) |
24ec839c | 2268 | drop_tty = 1; |
1da177e4 | 2269 | } |
ee2ffa0d | 2270 | spin_unlock(&tty_files_lock); |
452a00d2 | 2271 | tty_kref_put(tty); |
1da177e4 | 2272 | } |
98a27ba4 EB |
2273 | /* Reset controlling tty. */ |
2274 | if (drop_tty) | |
2275 | no_tty(); | |
1da177e4 LT |
2276 | |
2277 | /* Revalidate access to inherited open files. */ | |
c3c073f8 AV |
2278 | n = iterate_fd(files, 0, match_file, cred); |
2279 | if (!n) /* none found? */ | |
2280 | return; | |
1da177e4 | 2281 | |
c3c073f8 | 2282 | devnull = dentry_open(&selinux_null, O_RDWR, cred); |
45525b26 AV |
2283 | if (IS_ERR(devnull)) |
2284 | devnull = NULL; | |
2285 | /* replace all the matching ones with this */ | |
2286 | do { | |
2287 | replace_fd(n - 1, devnull, 0); | |
2288 | } while ((n = iterate_fd(files, n, match_file, cred)) != 0); | |
2289 | if (devnull) | |
c3c073f8 | 2290 | fput(devnull); |
1da177e4 LT |
2291 | } |
2292 | ||
a6f76f23 DH |
2293 | /* |
2294 | * Prepare a process for imminent new credential changes due to exec | |
2295 | */ | |
2296 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) | |
1da177e4 | 2297 | { |
a6f76f23 DH |
2298 | struct task_security_struct *new_tsec; |
2299 | struct rlimit *rlim, *initrlim; | |
2300 | int rc, i; | |
d84f4f99 | 2301 | |
a6f76f23 DH |
2302 | new_tsec = bprm->cred->security; |
2303 | if (new_tsec->sid == new_tsec->osid) | |
2304 | return; | |
1da177e4 | 2305 | |
a6f76f23 DH |
2306 | /* Close files for which the new task SID is not authorized. */ |
2307 | flush_unauthorized_files(bprm->cred, current->files); | |
0356357c | 2308 | |
a6f76f23 DH |
2309 | /* Always clear parent death signal on SID transitions. */ |
2310 | current->pdeath_signal = 0; | |
0356357c | 2311 | |
a6f76f23 DH |
2312 | /* Check whether the new SID can inherit resource limits from the old |
2313 | * SID. If not, reset all soft limits to the lower of the current | |
2314 | * task's hard limit and the init task's soft limit. | |
2315 | * | |
2316 | * Note that the setting of hard limits (even to lower them) can be | |
2317 | * controlled by the setrlimit check. The inclusion of the init task's | |
2318 | * soft limit into the computation is to avoid resetting soft limits | |
2319 | * higher than the default soft limit for cases where the default is | |
2320 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. | |
2321 | */ | |
2322 | rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, | |
2323 | PROCESS__RLIMITINH, NULL); | |
2324 | if (rc) { | |
eb2d55a3 ON |
2325 | /* protect against do_prlimit() */ |
2326 | task_lock(current); | |
a6f76f23 DH |
2327 | for (i = 0; i < RLIM_NLIMITS; i++) { |
2328 | rlim = current->signal->rlim + i; | |
2329 | initrlim = init_task.signal->rlim + i; | |
2330 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); | |
1da177e4 | 2331 | } |
eb2d55a3 ON |
2332 | task_unlock(current); |
2333 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); | |
1da177e4 LT |
2334 | } |
2335 | } | |
2336 | ||
2337 | /* | |
a6f76f23 DH |
2338 | * Clean up the process immediately after the installation of new credentials |
2339 | * due to exec | |
1da177e4 | 2340 | */ |
a6f76f23 | 2341 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
1da177e4 | 2342 | { |
a6f76f23 | 2343 | const struct task_security_struct *tsec = current_security(); |
1da177e4 | 2344 | struct itimerval itimer; |
a6f76f23 | 2345 | u32 osid, sid; |
1da177e4 LT |
2346 | int rc, i; |
2347 | ||
a6f76f23 DH |
2348 | osid = tsec->osid; |
2349 | sid = tsec->sid; | |
2350 | ||
2351 | if (sid == osid) | |
1da177e4 LT |
2352 | return; |
2353 | ||
a6f76f23 DH |
2354 | /* Check whether the new SID can inherit signal state from the old SID. |
2355 | * If not, clear itimers to avoid subsequent signal generation and | |
2356 | * flush and unblock signals. | |
2357 | * | |
2358 | * This must occur _after_ the task SID has been updated so that any | |
2359 | * kill done after the flush will be checked against the new SID. | |
2360 | */ | |
2361 | rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); | |
1da177e4 LT |
2362 | if (rc) { |
2363 | memset(&itimer, 0, sizeof itimer); | |
2364 | for (i = 0; i < 3; i++) | |
2365 | do_setitimer(i, &itimer, NULL); | |
1da177e4 | 2366 | spin_lock_irq(¤t->sighand->siglock); |
3bcac026 DH |
2367 | if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { |
2368 | __flush_signals(current); | |
2369 | flush_signal_handlers(current, 1); | |
2370 | sigemptyset(¤t->blocked); | |
2371 | } | |
1da177e4 LT |
2372 | spin_unlock_irq(¤t->sighand->siglock); |
2373 | } | |
2374 | ||
a6f76f23 DH |
2375 | /* Wake up the parent if it is waiting so that it can recheck |
2376 | * wait permission to the new task SID. */ | |
ecd6de3c | 2377 | read_lock(&tasklist_lock); |
0b7570e7 | 2378 | __wake_up_parent(current, current->real_parent); |
ecd6de3c | 2379 | read_unlock(&tasklist_lock); |
1da177e4 LT |
2380 | } |
2381 | ||
2382 | /* superblock security operations */ | |
2383 | ||
2384 | static int selinux_sb_alloc_security(struct super_block *sb) | |
2385 | { | |
2386 | return superblock_alloc_security(sb); | |
2387 | } | |
2388 | ||
2389 | static void selinux_sb_free_security(struct super_block *sb) | |
2390 | { | |
2391 | superblock_free_security(sb); | |
2392 | } | |
2393 | ||
2394 | static inline int match_prefix(char *prefix, int plen, char *option, int olen) | |
2395 | { | |
2396 | if (plen > olen) | |
2397 | return 0; | |
2398 | ||
2399 | return !memcmp(prefix, option, plen); | |
2400 | } | |
2401 | ||
2402 | static inline int selinux_option(char *option, int len) | |
2403 | { | |
832cbd9a EP |
2404 | return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || |
2405 | match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || | |
2406 | match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || | |
11689d47 DQ |
2407 | match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || |
2408 | match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); | |
1da177e4 LT |
2409 | } |
2410 | ||
2411 | static inline void take_option(char **to, char *from, int *first, int len) | |
2412 | { | |
2413 | if (!*first) { | |
2414 | **to = ','; | |
2415 | *to += 1; | |
3528a953 | 2416 | } else |
1da177e4 LT |
2417 | *first = 0; |
2418 | memcpy(*to, from, len); | |
2419 | *to += len; | |
2420 | } | |
2421 | ||
828dfe1d EP |
2422 | static inline void take_selinux_option(char **to, char *from, int *first, |
2423 | int len) | |
3528a953 CO |
2424 | { |
2425 | int current_size = 0; | |
2426 | ||
2427 | if (!*first) { | |
2428 | **to = '|'; | |
2429 | *to += 1; | |
828dfe1d | 2430 | } else |
3528a953 CO |
2431 | *first = 0; |
2432 | ||
2433 | while (current_size < len) { | |
2434 | if (*from != '"') { | |
2435 | **to = *from; | |
2436 | *to += 1; | |
2437 | } | |
2438 | from += 1; | |
2439 | current_size += 1; | |
2440 | } | |
2441 | } | |
2442 | ||
e0007529 | 2443 | static int selinux_sb_copy_data(char *orig, char *copy) |
1da177e4 LT |
2444 | { |
2445 | int fnosec, fsec, rc = 0; | |
2446 | char *in_save, *in_curr, *in_end; | |
2447 | char *sec_curr, *nosec_save, *nosec; | |
3528a953 | 2448 | int open_quote = 0; |
1da177e4 LT |
2449 | |
2450 | in_curr = orig; | |
2451 | sec_curr = copy; | |
2452 | ||
1da177e4 LT |
2453 | nosec = (char *)get_zeroed_page(GFP_KERNEL); |
2454 | if (!nosec) { | |
2455 | rc = -ENOMEM; | |
2456 | goto out; | |
2457 | } | |
2458 | ||
2459 | nosec_save = nosec; | |
2460 | fnosec = fsec = 1; | |
2461 | in_save = in_end = orig; | |
2462 | ||
2463 | do { | |
3528a953 CO |
2464 | if (*in_end == '"') |
2465 | open_quote = !open_quote; | |
2466 | if ((*in_end == ',' && open_quote == 0) || | |
2467 | *in_end == '\0') { | |
1da177e4 LT |
2468 | int len = in_end - in_curr; |
2469 | ||
2470 | if (selinux_option(in_curr, len)) | |
3528a953 | 2471 | take_selinux_option(&sec_curr, in_curr, &fsec, len); |
1da177e4 LT |
2472 | else |
2473 | take_option(&nosec, in_curr, &fnosec, len); | |
2474 | ||
2475 | in_curr = in_end + 1; | |
2476 | } | |
2477 | } while (*in_end++); | |
2478 | ||
6931dfc9 | 2479 | strcpy(in_save, nosec_save); |
da3caa20 | 2480 | free_page((unsigned long)nosec_save); |
1da177e4 LT |
2481 | out: |
2482 | return rc; | |
2483 | } | |
2484 | ||
026eb167 EP |
2485 | static int selinux_sb_remount(struct super_block *sb, void *data) |
2486 | { | |
2487 | int rc, i, *flags; | |
2488 | struct security_mnt_opts opts; | |
2489 | char *secdata, **mount_options; | |
2490 | struct superblock_security_struct *sbsec = sb->s_security; | |
2491 | ||
2492 | if (!(sbsec->flags & SE_SBINITIALIZED)) | |
2493 | return 0; | |
2494 | ||
2495 | if (!data) | |
2496 | return 0; | |
2497 | ||
2498 | if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) | |
2499 | return 0; | |
2500 | ||
2501 | security_init_mnt_opts(&opts); | |
2502 | secdata = alloc_secdata(); | |
2503 | if (!secdata) | |
2504 | return -ENOMEM; | |
2505 | rc = selinux_sb_copy_data(data, secdata); | |
2506 | if (rc) | |
2507 | goto out_free_secdata; | |
2508 | ||
2509 | rc = selinux_parse_opts_str(secdata, &opts); | |
2510 | if (rc) | |
2511 | goto out_free_secdata; | |
2512 | ||
2513 | mount_options = opts.mnt_opts; | |
2514 | flags = opts.mnt_opts_flags; | |
2515 | ||
2516 | for (i = 0; i < opts.num_mnt_opts; i++) { | |
2517 | u32 sid; | |
2518 | size_t len; | |
2519 | ||
12f348b9 | 2520 | if (flags[i] == SBLABEL_MNT) |
026eb167 EP |
2521 | continue; |
2522 | len = strlen(mount_options[i]); | |
52a4c640 NA |
2523 | rc = security_context_to_sid(mount_options[i], len, &sid, |
2524 | GFP_KERNEL); | |
026eb167 EP |
2525 | if (rc) { |
2526 | printk(KERN_WARNING "SELinux: security_context_to_sid" | |
29b1deb2 LT |
2527 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
2528 | mount_options[i], sb->s_id, sb->s_type->name, rc); | |
026eb167 EP |
2529 | goto out_free_opts; |
2530 | } | |
2531 | rc = -EINVAL; | |
2532 | switch (flags[i]) { | |
2533 | case FSCONTEXT_MNT: | |
2534 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) | |
2535 | goto out_bad_option; | |
2536 | break; | |
2537 | case CONTEXT_MNT: | |
2538 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) | |
2539 | goto out_bad_option; | |
2540 | break; | |
2541 | case ROOTCONTEXT_MNT: { | |
2542 | struct inode_security_struct *root_isec; | |
2543 | root_isec = sb->s_root->d_inode->i_security; | |
2544 | ||
2545 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) | |
2546 | goto out_bad_option; | |
2547 | break; | |
2548 | } | |
2549 | case DEFCONTEXT_MNT: | |
2550 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) | |
2551 | goto out_bad_option; | |
2552 | break; | |
2553 | default: | |
2554 | goto out_free_opts; | |
2555 | } | |
2556 | } | |
2557 | ||
2558 | rc = 0; | |
2559 | out_free_opts: | |
2560 | security_free_mnt_opts(&opts); | |
2561 | out_free_secdata: | |
2562 | free_secdata(secdata); | |
2563 | return rc; | |
2564 | out_bad_option: | |
2565 | printk(KERN_WARNING "SELinux: unable to change security options " | |
29b1deb2 LT |
2566 | "during remount (dev %s, type=%s)\n", sb->s_id, |
2567 | sb->s_type->name); | |
026eb167 EP |
2568 | goto out_free_opts; |
2569 | } | |
2570 | ||
12204e24 | 2571 | static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) |
1da177e4 | 2572 | { |
88e67f3b | 2573 | const struct cred *cred = current_cred(); |
2bf49690 | 2574 | struct common_audit_data ad; |
1da177e4 LT |
2575 | int rc; |
2576 | ||
2577 | rc = superblock_doinit(sb, data); | |
2578 | if (rc) | |
2579 | return rc; | |
2580 | ||
74192246 JM |
2581 | /* Allow all mounts performed by the kernel */ |
2582 | if (flags & MS_KERNMOUNT) | |
2583 | return 0; | |
2584 | ||
50c205f5 | 2585 | ad.type = LSM_AUDIT_DATA_DENTRY; |
a269434d | 2586 | ad.u.dentry = sb->s_root; |
88e67f3b | 2587 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
1da177e4 LT |
2588 | } |
2589 | ||
726c3342 | 2590 | static int selinux_sb_statfs(struct dentry *dentry) |
1da177e4 | 2591 | { |
88e67f3b | 2592 | const struct cred *cred = current_cred(); |
2bf49690 | 2593 | struct common_audit_data ad; |
1da177e4 | 2594 | |
50c205f5 | 2595 | ad.type = LSM_AUDIT_DATA_DENTRY; |
a269434d | 2596 | ad.u.dentry = dentry->d_sb->s_root; |
88e67f3b | 2597 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
1da177e4 LT |
2598 | } |
2599 | ||
808d4e3c | 2600 | static int selinux_mount(const char *dev_name, |
b5266eb4 | 2601 | struct path *path, |
808d4e3c | 2602 | const char *type, |
828dfe1d EP |
2603 | unsigned long flags, |
2604 | void *data) | |
1da177e4 | 2605 | { |
88e67f3b | 2606 | const struct cred *cred = current_cred(); |
1da177e4 LT |
2607 | |
2608 | if (flags & MS_REMOUNT) | |
d8c9584e | 2609 | return superblock_has_perm(cred, path->dentry->d_sb, |
828dfe1d | 2610 | FILESYSTEM__REMOUNT, NULL); |
1da177e4 | 2611 | else |
2875fa00 | 2612 | return path_has_perm(cred, path, FILE__MOUNTON); |
1da177e4 LT |
2613 | } |
2614 | ||
2615 | static int selinux_umount(struct vfsmount *mnt, int flags) | |
2616 | { | |
88e67f3b | 2617 | const struct cred *cred = current_cred(); |
1da177e4 | 2618 | |
88e67f3b | 2619 | return superblock_has_perm(cred, mnt->mnt_sb, |
828dfe1d | 2620 | FILESYSTEM__UNMOUNT, NULL); |
1da177e4 LT |
2621 | } |
2622 | ||
2623 | /* inode security operations */ | |
2624 | ||
2625 | static int selinux_inode_alloc_security(struct inode *inode) | |
2626 | { | |
2627 | return inode_alloc_security(inode); | |
2628 | } | |
2629 | ||
2630 | static void selinux_inode_free_security(struct inode *inode) | |
2631 | { | |
2632 | inode_free_security(inode); | |
2633 | } | |
2634 | ||
d47be3df DQ |
2635 | static int selinux_dentry_init_security(struct dentry *dentry, int mode, |
2636 | struct qstr *name, void **ctx, | |
2637 | u32 *ctxlen) | |
2638 | { | |
2639 | const struct cred *cred = current_cred(); | |
2640 | struct task_security_struct *tsec; | |
2641 | struct inode_security_struct *dsec; | |
2642 | struct superblock_security_struct *sbsec; | |
2643 | struct inode *dir = dentry->d_parent->d_inode; | |
2644 | u32 newsid; | |
2645 | int rc; | |
2646 | ||
2647 | tsec = cred->security; | |
2648 | dsec = dir->i_security; | |
2649 | sbsec = dir->i_sb->s_security; | |
2650 | ||
2651 | if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { | |
2652 | newsid = tsec->create_sid; | |
2653 | } else { | |
2654 | rc = security_transition_sid(tsec->sid, dsec->sid, | |
2655 | inode_mode_to_security_class(mode), | |
2656 | name, | |
2657 | &newsid); | |
2658 | if (rc) { | |
2659 | printk(KERN_WARNING | |
2660 | "%s: security_transition_sid failed, rc=%d\n", | |
2661 | __func__, -rc); | |
2662 | return rc; | |
2663 | } | |
2664 | } | |
2665 | ||
2666 | return security_sid_to_context(newsid, (char **)ctx, ctxlen); | |
2667 | } | |
2668 | ||
5e41ff9e | 2669 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
9548906b TH |
2670 | const struct qstr *qstr, |
2671 | const char **name, | |
2a7dba39 | 2672 | void **value, size_t *len) |
5e41ff9e | 2673 | { |
5fb49870 | 2674 | const struct task_security_struct *tsec = current_security(); |
5e41ff9e SS |
2675 | struct inode_security_struct *dsec; |
2676 | struct superblock_security_struct *sbsec; | |
275bb41e | 2677 | u32 sid, newsid, clen; |
5e41ff9e | 2678 | int rc; |
9548906b | 2679 | char *context; |
5e41ff9e | 2680 | |
5e41ff9e SS |
2681 | dsec = dir->i_security; |
2682 | sbsec = dir->i_sb->s_security; | |
5e41ff9e | 2683 | |
275bb41e DH |
2684 | sid = tsec->sid; |
2685 | newsid = tsec->create_sid; | |
2686 | ||
415103f9 EP |
2687 | if ((sbsec->flags & SE_SBINITIALIZED) && |
2688 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) | |
2689 | newsid = sbsec->mntpoint_sid; | |
12f348b9 | 2690 | else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) { |
275bb41e | 2691 | rc = security_transition_sid(sid, dsec->sid, |
5e41ff9e | 2692 | inode_mode_to_security_class(inode->i_mode), |
652bb9b0 | 2693 | qstr, &newsid); |
5e41ff9e SS |
2694 | if (rc) { |
2695 | printk(KERN_WARNING "%s: " | |
2696 | "security_transition_sid failed, rc=%d (dev=%s " | |
2697 | "ino=%ld)\n", | |
dd6f953a | 2698 | __func__, |
5e41ff9e SS |
2699 | -rc, inode->i_sb->s_id, inode->i_ino); |
2700 | return rc; | |
2701 | } | |
2702 | } | |
2703 | ||
296fddf7 | 2704 | /* Possibly defer initialization to selinux_complete_init. */ |
0d90a7ec | 2705 | if (sbsec->flags & SE_SBINITIALIZED) { |
296fddf7 EP |
2706 | struct inode_security_struct *isec = inode->i_security; |
2707 | isec->sclass = inode_mode_to_security_class(inode->i_mode); | |
2708 | isec->sid = newsid; | |
2709 | isec->initialized = 1; | |
2710 | } | |
5e41ff9e | 2711 | |
12f348b9 | 2712 | if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT)) |
25a74f3b SS |
2713 | return -EOPNOTSUPP; |
2714 | ||
9548906b TH |
2715 | if (name) |
2716 | *name = XATTR_SELINUX_SUFFIX; | |
5e41ff9e | 2717 | |
570bc1c2 | 2718 | if (value && len) { |
12b29f34 | 2719 | rc = security_sid_to_context_force(newsid, &context, &clen); |
9548906b | 2720 | if (rc) |
570bc1c2 | 2721 | return rc; |
570bc1c2 SS |
2722 | *value = context; |
2723 | *len = clen; | |
5e41ff9e | 2724 | } |
5e41ff9e | 2725 | |
5e41ff9e SS |
2726 | return 0; |
2727 | } | |
2728 | ||
4acdaf27 | 2729 | static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
2730 | { |
2731 | return may_create(dir, dentry, SECCLASS_FILE); | |
2732 | } | |
2733 | ||
1da177e4 LT |
2734 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
2735 | { | |
1da177e4 LT |
2736 | return may_link(dir, old_dentry, MAY_LINK); |
2737 | } | |
2738 | ||
1da177e4 LT |
2739 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
2740 | { | |
1da177e4 LT |
2741 | return may_link(dir, dentry, MAY_UNLINK); |
2742 | } | |
2743 | ||
2744 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) | |
2745 | { | |
2746 | return may_create(dir, dentry, SECCLASS_LNK_FILE); | |
2747 | } | |
2748 | ||
18bb1db3 | 2749 | static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) |
1da177e4 LT |
2750 | { |
2751 | return may_create(dir, dentry, SECCLASS_DIR); | |
2752 | } | |
2753 | ||
1da177e4 LT |
2754 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
2755 | { | |
2756 | return may_link(dir, dentry, MAY_RMDIR); | |
2757 | } | |
2758 | ||
1a67aafb | 2759 | static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 2760 | { |
1da177e4 LT |
2761 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
2762 | } | |
2763 | ||
1da177e4 | 2764 | static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, |
828dfe1d | 2765 | struct inode *new_inode, struct dentry *new_dentry) |
1da177e4 LT |
2766 | { |
2767 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); | |
2768 | } | |
2769 | ||
1da177e4 LT |
2770 | static int selinux_inode_readlink(struct dentry *dentry) |
2771 | { | |
88e67f3b DH |
2772 | const struct cred *cred = current_cred(); |
2773 | ||
2875fa00 | 2774 | return dentry_has_perm(cred, dentry, FILE__READ); |
1da177e4 LT |
2775 | } |
2776 | ||
2777 | static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) | |
2778 | { | |
88e67f3b | 2779 | const struct cred *cred = current_cred(); |
1da177e4 | 2780 | |
2875fa00 | 2781 | return dentry_has_perm(cred, dentry, FILE__READ); |
1da177e4 LT |
2782 | } |
2783 | ||
d4cf970d EP |
2784 | static noinline int audit_inode_permission(struct inode *inode, |
2785 | u32 perms, u32 audited, u32 denied, | |
626b9740 | 2786 | int result, |
d4cf970d | 2787 | unsigned flags) |
1da177e4 | 2788 | { |
b782e0a6 | 2789 | struct common_audit_data ad; |
d4cf970d EP |
2790 | struct inode_security_struct *isec = inode->i_security; |
2791 | int rc; | |
2792 | ||
50c205f5 | 2793 | ad.type = LSM_AUDIT_DATA_INODE; |
d4cf970d EP |
2794 | ad.u.inode = inode; |
2795 | ||
2796 | rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, | |
626b9740 | 2797 | audited, denied, result, &ad, flags); |
d4cf970d EP |
2798 | if (rc) |
2799 | return rc; | |
2800 | return 0; | |
2801 | } | |
2802 | ||
e74f71eb | 2803 | static int selinux_inode_permission(struct inode *inode, int mask) |
1da177e4 | 2804 | { |
88e67f3b | 2805 | const struct cred *cred = current_cred(); |
b782e0a6 EP |
2806 | u32 perms; |
2807 | bool from_access; | |
cf1dd1da | 2808 | unsigned flags = mask & MAY_NOT_BLOCK; |
2e334057 EP |
2809 | struct inode_security_struct *isec; |
2810 | u32 sid; | |
2811 | struct av_decision avd; | |
2812 | int rc, rc2; | |
2813 | u32 audited, denied; | |
1da177e4 | 2814 | |
b782e0a6 | 2815 | from_access = mask & MAY_ACCESS; |
d09ca739 EP |
2816 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
2817 | ||
b782e0a6 EP |
2818 | /* No permission to check. Existence test. */ |
2819 | if (!mask) | |
1da177e4 | 2820 | return 0; |
1da177e4 | 2821 | |
2e334057 | 2822 | validate_creds(cred); |
b782e0a6 | 2823 | |
2e334057 EP |
2824 | if (unlikely(IS_PRIVATE(inode))) |
2825 | return 0; | |
b782e0a6 EP |
2826 | |
2827 | perms = file_mask_to_av(inode->i_mode, mask); | |
2828 | ||
2e334057 EP |
2829 | sid = cred_sid(cred); |
2830 | isec = inode->i_security; | |
2831 | ||
2832 | rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd); | |
2833 | audited = avc_audit_required(perms, &avd, rc, | |
2834 | from_access ? FILE__AUDIT_ACCESS : 0, | |
2835 | &denied); | |
2836 | if (likely(!audited)) | |
2837 | return rc; | |
2838 | ||
626b9740 | 2839 | rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); |
2e334057 EP |
2840 | if (rc2) |
2841 | return rc2; | |
2842 | return rc; | |
1da177e4 LT |
2843 | } |
2844 | ||
2845 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) | |
2846 | { | |
88e67f3b | 2847 | const struct cred *cred = current_cred(); |
bc6a6008 | 2848 | unsigned int ia_valid = iattr->ia_valid; |
95dbf739 | 2849 | __u32 av = FILE__WRITE; |
1da177e4 | 2850 | |
bc6a6008 AW |
2851 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
2852 | if (ia_valid & ATTR_FORCE) { | |
2853 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | | |
2854 | ATTR_FORCE); | |
2855 | if (!ia_valid) | |
2856 | return 0; | |
2857 | } | |
1da177e4 | 2858 | |
bc6a6008 AW |
2859 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
2860 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) | |
2875fa00 | 2861 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
1da177e4 | 2862 | |
3d2195c3 | 2863 | if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE)) |
95dbf739 EP |
2864 | av |= FILE__OPEN; |
2865 | ||
2866 | return dentry_has_perm(cred, dentry, av); | |
1da177e4 LT |
2867 | } |
2868 | ||
2869 | static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) | |
2870 | { | |
88e67f3b | 2871 | const struct cred *cred = current_cred(); |
2875fa00 EP |
2872 | struct path path; |
2873 | ||
2874 | path.dentry = dentry; | |
2875 | path.mnt = mnt; | |
88e67f3b | 2876 | |
2875fa00 | 2877 | return path_has_perm(cred, &path, FILE__GETATTR); |
1da177e4 LT |
2878 | } |
2879 | ||
8f0cfa52 | 2880 | static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) |
b5376771 | 2881 | { |
88e67f3b DH |
2882 | const struct cred *cred = current_cred(); |
2883 | ||
b5376771 SH |
2884 | if (!strncmp(name, XATTR_SECURITY_PREFIX, |
2885 | sizeof XATTR_SECURITY_PREFIX - 1)) { | |
2886 | if (!strcmp(name, XATTR_NAME_CAPS)) { | |
2887 | if (!capable(CAP_SETFCAP)) | |
2888 | return -EPERM; | |
2889 | } else if (!capable(CAP_SYS_ADMIN)) { | |
2890 | /* A different attribute in the security namespace. | |
2891 | Restrict to administrator. */ | |
2892 | return -EPERM; | |
2893 | } | |
2894 | } | |
2895 | ||
2896 | /* Not an attribute we recognize, so just check the | |
2897 | ordinary setattr permission. */ | |
2875fa00 | 2898 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
b5376771 SH |
2899 | } |
2900 | ||
8f0cfa52 DH |
2901 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
2902 | const void *value, size_t size, int flags) | |
1da177e4 | 2903 | { |
1da177e4 LT |
2904 | struct inode *inode = dentry->d_inode; |
2905 | struct inode_security_struct *isec = inode->i_security; | |
2906 | struct superblock_security_struct *sbsec; | |
2bf49690 | 2907 | struct common_audit_data ad; |
275bb41e | 2908 | u32 newsid, sid = current_sid(); |
1da177e4 LT |
2909 | int rc = 0; |
2910 | ||
b5376771 SH |
2911 | if (strcmp(name, XATTR_NAME_SELINUX)) |
2912 | return selinux_inode_setotherxattr(dentry, name); | |
1da177e4 LT |
2913 | |
2914 | sbsec = inode->i_sb->s_security; | |
12f348b9 | 2915 | if (!(sbsec->flags & SBLABEL_MNT)) |
1da177e4 LT |
2916 | return -EOPNOTSUPP; |
2917 | ||
2e149670 | 2918 | if (!inode_owner_or_capable(inode)) |
1da177e4 LT |
2919 | return -EPERM; |
2920 | ||
50c205f5 | 2921 | ad.type = LSM_AUDIT_DATA_DENTRY; |
a269434d | 2922 | ad.u.dentry = dentry; |
1da177e4 | 2923 | |
275bb41e | 2924 | rc = avc_has_perm(sid, isec->sid, isec->sclass, |
1da177e4 LT |
2925 | FILE__RELABELFROM, &ad); |
2926 | if (rc) | |
2927 | return rc; | |
2928 | ||
52a4c640 | 2929 | rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); |
12b29f34 | 2930 | if (rc == -EINVAL) { |
d6ea83ec EP |
2931 | if (!capable(CAP_MAC_ADMIN)) { |
2932 | struct audit_buffer *ab; | |
2933 | size_t audit_size; | |
2934 | const char *str; | |
2935 | ||
2936 | /* We strip a nul only if it is at the end, otherwise the | |
2937 | * context contains a nul and we should audit that */ | |
e3fea3f7 AV |
2938 | if (value) { |
2939 | str = value; | |
2940 | if (str[size - 1] == '\0') | |
2941 | audit_size = size - 1; | |
2942 | else | |
2943 | audit_size = size; | |
2944 | } else { | |
2945 | str = ""; | |
2946 | audit_size = 0; | |
2947 | } | |
d6ea83ec EP |
2948 | ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); |
2949 | audit_log_format(ab, "op=setxattr invalid_context="); | |
2950 | audit_log_n_untrustedstring(ab, value, audit_size); | |
2951 | audit_log_end(ab); | |
2952 | ||
12b29f34 | 2953 | return rc; |
d6ea83ec | 2954 | } |
12b29f34 SS |
2955 | rc = security_context_to_sid_force(value, size, &newsid); |
2956 | } | |
1da177e4 LT |
2957 | if (rc) |
2958 | return rc; | |
2959 | ||
275bb41e | 2960 | rc = avc_has_perm(sid, newsid, isec->sclass, |
1da177e4 LT |
2961 | FILE__RELABELTO, &ad); |
2962 | if (rc) | |
2963 | return rc; | |
2964 | ||
275bb41e | 2965 | rc = security_validate_transition(isec->sid, newsid, sid, |
828dfe1d | 2966 | isec->sclass); |
1da177e4 LT |
2967 | if (rc) |
2968 | return rc; | |
2969 | ||
2970 | return avc_has_perm(newsid, | |
2971 | sbsec->sid, | |
2972 | SECCLASS_FILESYSTEM, | |
2973 | FILESYSTEM__ASSOCIATE, | |
2974 | &ad); | |
2975 | } | |
2976 | ||
8f0cfa52 | 2977 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
f5269710 | 2978 | const void *value, size_t size, |
8f0cfa52 | 2979 | int flags) |
1da177e4 LT |
2980 | { |
2981 | struct inode *inode = dentry->d_inode; | |
2982 | struct inode_security_struct *isec = inode->i_security; | |
2983 | u32 newsid; | |
2984 | int rc; | |
2985 | ||
2986 | if (strcmp(name, XATTR_NAME_SELINUX)) { | |
2987 | /* Not an attribute we recognize, so nothing to do. */ | |
2988 | return; | |
2989 | } | |
2990 | ||
12b29f34 | 2991 | rc = security_context_to_sid_force(value, size, &newsid); |
1da177e4 | 2992 | if (rc) { |
12b29f34 SS |
2993 | printk(KERN_ERR "SELinux: unable to map context to SID" |
2994 | "for (%s, %lu), rc=%d\n", | |
2995 | inode->i_sb->s_id, inode->i_ino, -rc); | |
1da177e4 LT |
2996 | return; |
2997 | } | |
2998 | ||
aa9c2669 | 2999 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
1da177e4 | 3000 | isec->sid = newsid; |
aa9c2669 DQ |
3001 | isec->initialized = 1; |
3002 | ||
1da177e4 LT |
3003 | return; |
3004 | } | |
3005 | ||
8f0cfa52 | 3006 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
1da177e4 | 3007 | { |
88e67f3b DH |
3008 | const struct cred *cred = current_cred(); |
3009 | ||
2875fa00 | 3010 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
1da177e4 LT |
3011 | } |
3012 | ||
828dfe1d | 3013 | static int selinux_inode_listxattr(struct dentry *dentry) |
1da177e4 | 3014 | { |
88e67f3b DH |
3015 | const struct cred *cred = current_cred(); |
3016 | ||
2875fa00 | 3017 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
1da177e4 LT |
3018 | } |
3019 | ||
8f0cfa52 | 3020 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
1da177e4 | 3021 | { |
b5376771 SH |
3022 | if (strcmp(name, XATTR_NAME_SELINUX)) |
3023 | return selinux_inode_setotherxattr(dentry, name); | |
1da177e4 LT |
3024 | |
3025 | /* No one is allowed to remove a SELinux security label. | |
3026 | You can change the label, but all data must be labeled. */ | |
3027 | return -EACCES; | |
3028 | } | |
3029 | ||
d381d8a9 | 3030 | /* |
abc69bb6 | 3031 | * Copy the inode security context value to the user. |
d381d8a9 JM |
3032 | * |
3033 | * Permission check is handled by selinux_inode_getxattr hook. | |
3034 | */ | |
42492594 | 3035 | static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) |
1da177e4 | 3036 | { |
42492594 DQ |
3037 | u32 size; |
3038 | int error; | |
3039 | char *context = NULL; | |
1da177e4 | 3040 | struct inode_security_struct *isec = inode->i_security; |
d381d8a9 | 3041 | |
8c8570fb DK |
3042 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
3043 | return -EOPNOTSUPP; | |
d381d8a9 | 3044 | |
abc69bb6 SS |
3045 | /* |
3046 | * If the caller has CAP_MAC_ADMIN, then get the raw context | |
3047 | * value even if it is not defined by current policy; otherwise, | |
3048 | * use the in-core value under current policy. | |
3049 | * Use the non-auditing forms of the permission checks since | |
3050 | * getxattr may be called by unprivileged processes commonly | |
3051 | * and lack of permission just means that we fall back to the | |
3052 | * in-core context value, not a denial. | |
3053 | */ | |
6a9de491 | 3054 | error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN, |
3699c53c | 3055 | SECURITY_CAP_NOAUDIT); |
abc69bb6 SS |
3056 | if (!error) |
3057 | error = security_sid_to_context_force(isec->sid, &context, | |
3058 | &size); | |
3059 | else | |
3060 | error = security_sid_to_context(isec->sid, &context, &size); | |
42492594 DQ |
3061 | if (error) |
3062 | return error; | |
3063 | error = size; | |
3064 | if (alloc) { | |
3065 | *buffer = context; | |
3066 | goto out_nofree; | |
3067 | } | |
3068 | kfree(context); | |
3069 | out_nofree: | |
3070 | return error; | |
1da177e4 LT |
3071 | } |
3072 | ||
3073 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, | |
828dfe1d | 3074 | const void *value, size_t size, int flags) |
1da177e4 LT |
3075 | { |
3076 | struct inode_security_struct *isec = inode->i_security; | |
3077 | u32 newsid; | |
3078 | int rc; | |
3079 | ||
3080 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) | |
3081 | return -EOPNOTSUPP; | |
3082 | ||
3083 | if (!value || !size) | |
3084 | return -EACCES; | |
3085 | ||
52a4c640 | 3086 | rc = security_context_to_sid((void *)value, size, &newsid, GFP_KERNEL); |
1da177e4 LT |
3087 | if (rc) |
3088 | return rc; | |
3089 | ||
aa9c2669 | 3090 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
1da177e4 | 3091 | isec->sid = newsid; |
ddd29ec6 | 3092 | isec->initialized = 1; |
1da177e4 LT |
3093 | return 0; |
3094 | } | |
3095 | ||
3096 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) | |
3097 | { | |
3098 | const int len = sizeof(XATTR_NAME_SELINUX); | |
3099 | if (buffer && len <= buffer_size) | |
3100 | memcpy(buffer, XATTR_NAME_SELINUX, len); | |
3101 | return len; | |
3102 | } | |
3103 | ||
713a04ae AD |
3104 | static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) |
3105 | { | |
3106 | struct inode_security_struct *isec = inode->i_security; | |
3107 | *secid = isec->sid; | |
3108 | } | |
3109 | ||
1da177e4 LT |
3110 | /* file security operations */ |
3111 | ||
788e7dd4 | 3112 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
1da177e4 | 3113 | { |
88e67f3b | 3114 | const struct cred *cred = current_cred(); |
496ad9aa | 3115 | struct inode *inode = file_inode(file); |
1da177e4 | 3116 | |
1da177e4 LT |
3117 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
3118 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) | |
3119 | mask |= MAY_APPEND; | |
3120 | ||
389fb800 PM |
3121 | return file_has_perm(cred, file, |
3122 | file_mask_to_av(inode->i_mode, mask)); | |
1da177e4 LT |
3123 | } |
3124 | ||
788e7dd4 YN |
3125 | static int selinux_file_permission(struct file *file, int mask) |
3126 | { | |
496ad9aa | 3127 | struct inode *inode = file_inode(file); |
20dda18b SS |
3128 | struct file_security_struct *fsec = file->f_security; |
3129 | struct inode_security_struct *isec = inode->i_security; | |
3130 | u32 sid = current_sid(); | |
3131 | ||
389fb800 | 3132 | if (!mask) |
788e7dd4 YN |
3133 | /* No permission to check. Existence test. */ |
3134 | return 0; | |
788e7dd4 | 3135 | |
20dda18b SS |
3136 | if (sid == fsec->sid && fsec->isid == isec->sid && |
3137 | fsec->pseqno == avc_policy_seqno()) | |
83d49856 | 3138 | /* No change since file_open check. */ |
20dda18b SS |
3139 | return 0; |
3140 | ||
788e7dd4 YN |
3141 | return selinux_revalidate_file_permission(file, mask); |
3142 | } | |
3143 | ||
1da177e4 LT |
3144 | static int selinux_file_alloc_security(struct file *file) |
3145 | { | |
3146 | return file_alloc_security(file); | |
3147 | } | |
3148 | ||
3149 | static void selinux_file_free_security(struct file *file) | |
3150 | { | |
3151 | file_free_security(file); | |
3152 | } | |
3153 | ||
3154 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, | |
3155 | unsigned long arg) | |
3156 | { | |
88e67f3b | 3157 | const struct cred *cred = current_cred(); |
0b24dcb7 | 3158 | int error = 0; |
1da177e4 | 3159 | |
0b24dcb7 EP |
3160 | switch (cmd) { |
3161 | case FIONREAD: | |
3162 | /* fall through */ | |
3163 | case FIBMAP: | |
3164 | /* fall through */ | |
3165 | case FIGETBSZ: | |
3166 | /* fall through */ | |
2f99c369 | 3167 | case FS_IOC_GETFLAGS: |
0b24dcb7 | 3168 | /* fall through */ |
2f99c369 | 3169 | case FS_IOC_GETVERSION: |
0b24dcb7 EP |
3170 | error = file_has_perm(cred, file, FILE__GETATTR); |
3171 | break; | |
1da177e4 | 3172 | |
2f99c369 | 3173 | case FS_IOC_SETFLAGS: |
0b24dcb7 | 3174 | /* fall through */ |
2f99c369 | 3175 | case FS_IOC_SETVERSION: |
0b24dcb7 EP |
3176 | error = file_has_perm(cred, file, FILE__SETATTR); |
3177 | break; | |
3178 | ||
3179 | /* sys_ioctl() checks */ | |
3180 | case FIONBIO: | |
3181 | /* fall through */ | |
3182 | case FIOASYNC: | |
3183 | error = file_has_perm(cred, file, 0); | |
3184 | break; | |
1da177e4 | 3185 | |
0b24dcb7 EP |
3186 | case KDSKBENT: |
3187 | case KDSKBSENT: | |
6a9de491 EP |
3188 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
3189 | SECURITY_CAP_AUDIT); | |
0b24dcb7 EP |
3190 | break; |
3191 | ||
3192 | /* default case assumes that the command will go | |
3193 | * to the file's ioctl() function. | |
3194 | */ | |
3195 | default: | |
3196 | error = file_has_perm(cred, file, FILE__IOCTL); | |
3197 | } | |
3198 | return error; | |
1da177e4 LT |
3199 | } |
3200 | ||
fcaaade1 SS |
3201 | static int default_noexec; |
3202 | ||
1da177e4 LT |
3203 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
3204 | { | |
88e67f3b | 3205 | const struct cred *cred = current_cred(); |
d84f4f99 | 3206 | int rc = 0; |
88e67f3b | 3207 | |
fcaaade1 SS |
3208 | if (default_noexec && |
3209 | (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { | |
1da177e4 LT |
3210 | /* |
3211 | * We are making executable an anonymous mapping or a | |
3212 | * private file mapping that will also be writable. | |
3213 | * This has an additional check. | |
3214 | */ | |
d84f4f99 | 3215 | rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); |
1da177e4 | 3216 | if (rc) |
d84f4f99 | 3217 | goto error; |
1da177e4 | 3218 | } |
1da177e4 LT |
3219 | |
3220 | if (file) { | |
3221 | /* read access is always possible with a mapping */ | |
3222 | u32 av = FILE__READ; | |
3223 | ||
3224 | /* write access only matters if the mapping is shared */ | |
3225 | if (shared && (prot & PROT_WRITE)) | |
3226 | av |= FILE__WRITE; | |
3227 | ||
3228 | if (prot & PROT_EXEC) | |
3229 | av |= FILE__EXECUTE; | |
3230 | ||
88e67f3b | 3231 | return file_has_perm(cred, file, av); |
1da177e4 | 3232 | } |
d84f4f99 DH |
3233 | |
3234 | error: | |
3235 | return rc; | |
1da177e4 LT |
3236 | } |
3237 | ||
e5467859 | 3238 | static int selinux_mmap_addr(unsigned long addr) |
1da177e4 | 3239 | { |
98883bfd PM |
3240 | int rc; |
3241 | ||
3242 | /* do DAC check on address space usage */ | |
3243 | rc = cap_mmap_addr(addr); | |
3244 | if (rc) | |
3245 | return rc; | |
1da177e4 | 3246 | |
a2551df7 | 3247 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
98883bfd | 3248 | u32 sid = current_sid(); |
ed032189 EP |
3249 | rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, |
3250 | MEMPROTECT__MMAP_ZERO, NULL); | |
84336d1a EP |
3251 | } |
3252 | ||
98883bfd | 3253 | return rc; |
e5467859 | 3254 | } |
1da177e4 | 3255 | |
e5467859 AV |
3256 | static int selinux_mmap_file(struct file *file, unsigned long reqprot, |
3257 | unsigned long prot, unsigned long flags) | |
3258 | { | |
1da177e4 LT |
3259 | if (selinux_checkreqprot) |
3260 | prot = reqprot; | |
3261 | ||
3262 | return file_map_prot_check(file, prot, | |
3263 | (flags & MAP_TYPE) == MAP_SHARED); | |
3264 | } | |
3265 | ||
3266 | static int selinux_file_mprotect(struct vm_area_struct *vma, | |
3267 | unsigned long reqprot, | |
3268 | unsigned long prot) | |
3269 | { | |
88e67f3b | 3270 | const struct cred *cred = current_cred(); |
1da177e4 LT |
3271 | |
3272 | if (selinux_checkreqprot) | |
3273 | prot = reqprot; | |
3274 | ||
fcaaade1 SS |
3275 | if (default_noexec && |
3276 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { | |
d541bbee | 3277 | int rc = 0; |
db4c9641 SS |
3278 | if (vma->vm_start >= vma->vm_mm->start_brk && |
3279 | vma->vm_end <= vma->vm_mm->brk) { | |
d84f4f99 | 3280 | rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); |
db4c9641 SS |
3281 | } else if (!vma->vm_file && |
3282 | vma->vm_start <= vma->vm_mm->start_stack && | |
3283 | vma->vm_end >= vma->vm_mm->start_stack) { | |
3b11a1de | 3284 | rc = current_has_perm(current, PROCESS__EXECSTACK); |
db4c9641 SS |
3285 | } else if (vma->vm_file && vma->anon_vma) { |
3286 | /* | |
3287 | * We are making executable a file mapping that has | |
3288 | * had some COW done. Since pages might have been | |
3289 | * written, check ability to execute the possibly | |
3290 | * modified content. This typically should only | |
3291 | * occur for text relocations. | |
3292 | */ | |
d84f4f99 | 3293 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
db4c9641 | 3294 | } |