Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NSA Security-Enhanced Linux (SELinux) security module |
| 3 | * |
| 4 | * This file contains the SELinux hook function implementations. |
| 5 | * |
Stephen Smalley | 7efbb60 | 2017-08-17 13:32:36 -0400 | [diff] [blame] | 6 | * Authors: Stephen Smalley, <sds@tycho.nsa.gov> |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7 | * Chris Vance, <cvance@nai.com> |
| 8 | * Wayne Salamon, <wsalamon@nai.com> |
| 9 | * James Morris <jmorris@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2001,2002 Networks Associates Technology, Inc. |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 12 | * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 13 | * Eric Paris <eparis@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 15 | * <dgoeddel@trustedcs.com> |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 16 | * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 17 | * Paul Moore <paul@paul-moore.com> |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 18 | * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 19 | * Yuichi Nakamura <ynakam@hitachisoft.jp> |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 20 | * Copyright (C) 2016 Mellanox Technologies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * |
| 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License version 2, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 24 | * as published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 28 | #include <linux/kd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 30 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/errno.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 33 | #include <linux/sched/task.h> |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 34 | #include <linux/lsm_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/xattr.h> |
| 36 | #include <linux/capability.h> |
| 37 | #include <linux/unistd.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/mman.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/pagemap.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 42 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
| 45 | #include <linux/syscalls.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 46 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 48 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/namei.h> |
| 50 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/netfilter_ipv4.h> |
| 52 | #include <linux/netfilter_ipv6.h> |
| 53 | #include <linux/tty.h> |
| 54 | #include <net/icmp.h> |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 55 | #include <net/ip.h> /* for local_port_range[] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 57 | #include <net/inet_connection_sock.h> |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 59 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 60 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <asm/ioctls.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 62 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/bitops.h> |
| 64 | #include <linux/interrupt.h> |
| 65 | #include <linux/netdevice.h> /* for network interface checks */ |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 66 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/tcp.h> |
| 68 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 69 | #include <linux/dccp.h> |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 70 | #include <linux/sctp.h> |
| 71 | #include <net/sctp/structs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/quota.h> |
| 73 | #include <linux/un.h> /* for Unix socket types */ |
| 74 | #include <net/af_unix.h> /* for Unix socket types */ |
| 75 | #include <linux/parser.h> |
| 76 | #include <linux/nfs_mount.h> |
| 77 | #include <net/ipv6.h> |
| 78 | #include <linux/hugetlb.h> |
| 79 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 81 | #include <linux/string.h> |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 82 | #include <linux/selinux.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 83 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 84 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 85 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 86 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 87 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 88 | #include <linux/msg.h> |
| 89 | #include <linux/shm.h> |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 90 | #include <linux/bpf.h> |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 91 | #include <uapi/linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | #include "avc.h" |
| 94 | #include "objsec.h" |
| 95 | #include "netif.h" |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 96 | #include "netnode.h" |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 97 | #include "netport.h" |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 98 | #include "ibpkey.h" |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 99 | #include "xfrm.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 100 | #include "netlabel.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 101 | #include "audit.h" |
James Morris | 7b98a58 | 2011-08-30 12:52:32 +1000 | [diff] [blame] | 102 | #include "avc_ss.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 104 | struct selinux_state selinux_state; |
| 105 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 106 | /* SECMARK reference count */ |
James Morris | 56a4ca9 | 2011-08-17 11:08:43 +1000 | [diff] [blame] | 107 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 110 | static int selinux_enforcing_boot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | static int __init enforcing_setup(char *str) |
| 113 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 114 | unsigned long enforcing; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 115 | if (!kstrtoul(str, 0, &enforcing)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 116 | selinux_enforcing_boot = enforcing ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 1; |
| 118 | } |
| 119 | __setup("enforcing=", enforcing_setup); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 120 | #else |
| 121 | #define selinux_enforcing_boot 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
| 125 | int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; |
| 126 | |
| 127 | static int __init selinux_enabled_setup(char *str) |
| 128 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 129 | unsigned long enabled; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 130 | if (!kstrtoul(str, 0, &enabled)) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 131 | selinux_enabled = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | __setup("selinux=", selinux_enabled_setup); |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 135 | #else |
| 136 | int selinux_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #endif |
| 138 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 139 | static unsigned int selinux_checkreqprot_boot = |
| 140 | CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 141 | |
| 142 | static int __init checkreqprot_setup(char *str) |
| 143 | { |
| 144 | unsigned long checkreqprot; |
| 145 | |
| 146 | if (!kstrtoul(str, 0, &checkreqprot)) |
| 147 | selinux_checkreqprot_boot = checkreqprot ? 1 : 0; |
| 148 | return 1; |
| 149 | } |
| 150 | __setup("checkreqprot=", checkreqprot_setup); |
| 151 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 152 | static struct kmem_cache *sel_inode_cache; |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 153 | static struct kmem_cache *file_security_cache; |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 154 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 155 | /** |
| 156 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 157 | * |
| 158 | * Description: |
| 159 | * This function checks the SECMARK reference counter to see if any SECMARK |
| 160 | * targets are currently configured, if the reference counter is greater than |
| 161 | * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 162 | * enabled, false (0) if SECMARK is disabled. If the always_check_network |
| 163 | * policy capability is enabled, SECMARK is always considered enabled. |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 164 | * |
| 165 | */ |
| 166 | static int selinux_secmark_enabled(void) |
| 167 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 168 | return (selinux_policycap_alwaysnetwork() || |
| 169 | atomic_read(&selinux_secmark_refcount)); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled |
| 174 | * |
| 175 | * Description: |
| 176 | * This function checks if NetLabel or labeled IPSEC is enabled. Returns true |
| 177 | * (1) if any are enabled or false (0) if neither are enabled. If the |
| 178 | * always_check_network policy capability is enabled, peer labeling |
| 179 | * is always considered enabled. |
| 180 | * |
| 181 | */ |
| 182 | static int selinux_peerlbl_enabled(void) |
| 183 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 184 | return (selinux_policycap_alwaysnetwork() || |
| 185 | netlbl_enabled() || selinux_xfrm_enabled()); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 188 | static int selinux_netcache_avc_callback(u32 event) |
| 189 | { |
| 190 | if (event == AVC_CALLBACK_RESET) { |
| 191 | sel_netif_flush(); |
| 192 | sel_netnode_flush(); |
| 193 | sel_netport_flush(); |
| 194 | synchronize_net(); |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 199 | static int selinux_lsm_notifier_avc_callback(u32 event) |
| 200 | { |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 201 | if (event == AVC_CALLBACK_RESET) { |
| 202 | sel_ib_pkey_flush(); |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 203 | call_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 204 | } |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 209 | /* |
| 210 | * initialise the security for the init task |
| 211 | */ |
| 212 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 214 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct task_security_struct *tsec; |
| 216 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 217 | tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (!tsec) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 219 | panic("SELinux: Failed to initialize initial task.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 221 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 222 | cred->security = tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 225 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 226 | * get the security ID of a set of credentials |
| 227 | */ |
| 228 | static inline u32 cred_sid(const struct cred *cred) |
| 229 | { |
| 230 | const struct task_security_struct *tsec; |
| 231 | |
| 232 | tsec = cred->security; |
| 233 | return tsec->sid; |
| 234 | } |
| 235 | |
| 236 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 237 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 238 | */ |
| 239 | static inline u32 task_sid(const struct task_struct *task) |
| 240 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 241 | u32 sid; |
| 242 | |
| 243 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 244 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 245 | rcu_read_unlock(); |
| 246 | return sid; |
| 247 | } |
| 248 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 249 | /* Allocate and free functions for each kind of security blob. */ |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static int inode_alloc_security(struct inode *inode) |
| 252 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 254 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 256 | isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (!isec) |
| 258 | return -ENOMEM; |
| 259 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 260 | spin_lock_init(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | INIT_LIST_HEAD(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | isec->inode = inode; |
| 263 | isec->sid = SECINITSID_UNLABELED; |
| 264 | isec->sclass = SECCLASS_FILE; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 265 | isec->task_sid = sid; |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 266 | isec->initialized = LABEL_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | inode->i_security = isec; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 272 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 273 | |
| 274 | /* |
| 275 | * Try reloading inode security labels that have been marked as invalid. The |
| 276 | * @may_sleep parameter indicates when sleeping and thus reloading labels is |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 277 | * allowed; when set to false, returns -ECHILD when the label is |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 278 | * invalid. The @dentry parameter should be set to a dentry of the inode. |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 279 | */ |
| 280 | static int __inode_security_revalidate(struct inode *inode, |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 281 | struct dentry *dentry, |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 282 | bool may_sleep) |
| 283 | { |
| 284 | struct inode_security_struct *isec = inode->i_security; |
| 285 | |
| 286 | might_sleep_if(may_sleep); |
| 287 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 288 | if (selinux_state.initialized && |
| 289 | isec->initialized != LABEL_INITIALIZED) { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 290 | if (!may_sleep) |
| 291 | return -ECHILD; |
| 292 | |
| 293 | /* |
| 294 | * Try reloading the inode security label. This will fail if |
| 295 | * @opt_dentry is NULL and no dentry for this inode can be |
| 296 | * found; in that case, continue using the old label. |
| 297 | */ |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 298 | inode_doinit_with_dentry(inode, dentry); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 303 | static struct inode_security_struct *inode_security_novalidate(struct inode *inode) |
| 304 | { |
| 305 | return inode->i_security; |
| 306 | } |
| 307 | |
| 308 | static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) |
| 309 | { |
| 310 | int error; |
| 311 | |
| 312 | error = __inode_security_revalidate(inode, NULL, !rcu); |
| 313 | if (error) |
| 314 | return ERR_PTR(error); |
| 315 | return inode->i_security; |
| 316 | } |
| 317 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 318 | /* |
| 319 | * Get the security label of an inode. |
| 320 | */ |
| 321 | static struct inode_security_struct *inode_security(struct inode *inode) |
| 322 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 323 | __inode_security_revalidate(inode, NULL, true); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 324 | return inode->i_security; |
| 325 | } |
| 326 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 327 | static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) |
| 328 | { |
| 329 | struct inode *inode = d_backing_inode(dentry); |
| 330 | |
| 331 | return inode->i_security; |
| 332 | } |
| 333 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 334 | /* |
| 335 | * Get the security label of a dentry's backing inode. |
| 336 | */ |
| 337 | static struct inode_security_struct *backing_inode_security(struct dentry *dentry) |
| 338 | { |
| 339 | struct inode *inode = d_backing_inode(dentry); |
| 340 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 341 | __inode_security_revalidate(inode, dentry, true); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 342 | return inode->i_security; |
| 343 | } |
| 344 | |
Steven Rostedt | 3dc91d4 | 2014-01-09 21:46:34 -0500 | [diff] [blame] | 345 | static void inode_free_rcu(struct rcu_head *head) |
| 346 | { |
| 347 | struct inode_security_struct *isec; |
| 348 | |
| 349 | isec = container_of(head, struct inode_security_struct, rcu); |
| 350 | kmem_cache_free(sel_inode_cache, isec); |
| 351 | } |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | static void inode_free_security(struct inode *inode) |
| 354 | { |
| 355 | struct inode_security_struct *isec = inode->i_security; |
| 356 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
| 357 | |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 358 | /* |
| 359 | * As not all inode security structures are in a list, we check for |
| 360 | * empty list outside of the lock to make sure that we won't waste |
| 361 | * time taking a lock doing nothing. |
| 362 | * |
| 363 | * The list_del_init() function can be safely called more than once. |
| 364 | * It should not be possible for this function to be called with |
| 365 | * concurrent list_add(), but for better safety against future changes |
| 366 | * in the code, we use list_empty_careful() here. |
| 367 | */ |
| 368 | if (!list_empty_careful(&isec->list)) { |
| 369 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | list_del_init(&isec->list); |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 371 | spin_unlock(&sbsec->isec_lock); |
| 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Steven Rostedt | 3dc91d4 | 2014-01-09 21:46:34 -0500 | [diff] [blame] | 374 | /* |
| 375 | * The inode may still be referenced in a path walk and |
| 376 | * a call to selinux_inode_permission() can be made |
| 377 | * after inode_free_security() is called. Ideally, the VFS |
| 378 | * wouldn't do this, but fixing that is a much harder |
| 379 | * job. For now, simply free the i_security via RCU, and |
| 380 | * leave the current inode->i_security pointer intact. |
| 381 | * The inode will be freed after the RCU grace period too. |
| 382 | */ |
| 383 | call_rcu(&isec->rcu, inode_free_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static int file_alloc_security(struct file *file) |
| 387 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct file_security_struct *fsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 389 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 391 | fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (!fsec) |
| 393 | return -ENOMEM; |
| 394 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 395 | fsec->sid = sid; |
| 396 | fsec->fown_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | file->f_security = fsec; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void file_free_security(struct file *file) |
| 403 | { |
| 404 | struct file_security_struct *fsec = file->f_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | file->f_security = NULL; |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 406 | kmem_cache_free(file_security_cache, fsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int superblock_alloc_security(struct super_block *sb) |
| 410 | { |
| 411 | struct superblock_security_struct *sbsec; |
| 412 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 413 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (!sbsec) |
| 415 | return -ENOMEM; |
| 416 | |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 417 | mutex_init(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 419 | spin_lock_init(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | sbsec->sb = sb; |
| 421 | sbsec->sid = SECINITSID_UNLABELED; |
| 422 | sbsec->def_sid = SECINITSID_FILE; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 423 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | sb->s_security = sbsec; |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static void superblock_free_security(struct super_block *sb) |
| 430 | { |
| 431 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | sb->s_security = NULL; |
| 433 | kfree(sbsec); |
| 434 | } |
| 435 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 436 | struct selinux_mnt_opts { |
| 437 | const char *fscontext, *context, *rootcontext, *defcontext; |
| 438 | }; |
| 439 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 440 | static void selinux_free_mnt_opts(void *mnt_opts) |
| 441 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 442 | struct selinux_mnt_opts *opts = mnt_opts; |
| 443 | kfree(opts->fscontext); |
| 444 | kfree(opts->context); |
| 445 | kfree(opts->rootcontext); |
| 446 | kfree(opts->defcontext); |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 447 | kfree(opts); |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | static inline int inode_doinit(struct inode *inode) |
| 451 | { |
| 452 | return inode_doinit_with_dentry(inode, NULL); |
| 453 | } |
| 454 | |
| 455 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 456 | Opt_error = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | Opt_context = 1, |
| 458 | Opt_fscontext = 2, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 459 | Opt_defcontext = 3, |
| 460 | Opt_rootcontext = 4, |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 461 | Opt_labelsupport = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 464 | #define A(s, opt, has_arg) {s, sizeof(s) - 1, opt, has_arg} |
| 465 | static struct { |
| 466 | const char *name; |
| 467 | int len; |
| 468 | int opt; |
| 469 | bool has_arg; |
| 470 | } tokens[] = { |
| 471 | A("context", Opt_context, true), |
| 472 | A("fscontext", Opt_fscontext, true), |
| 473 | A("defcontext", Opt_defcontext, true), |
| 474 | A("rootcontext", Opt_rootcontext, true), |
| 475 | A("seclabel", Opt_labelsupport, false), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | }; |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 477 | #undef A |
| 478 | |
| 479 | static int match_opt_prefix(char *s, int l, char **arg) |
| 480 | { |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 484 | size_t len = tokens[i].len; |
| 485 | if (len > l || memcmp(s, tokens[i].name, len)) |
| 486 | continue; |
| 487 | if (tokens[i].has_arg) { |
| 488 | if (len == l || s[len] != '=') |
| 489 | continue; |
| 490 | *arg = s + len + 1; |
| 491 | } else if (len != l) |
| 492 | continue; |
| 493 | return tokens[i].opt; |
| 494 | } |
| 495 | return Opt_error; |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 499 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 500 | static int may_context_mount_sb_relabel(u32 sid, |
| 501 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 502 | const struct cred *cred) |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 503 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 504 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 505 | int rc; |
| 506 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 507 | rc = avc_has_perm(&selinux_state, |
| 508 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 509 | FILESYSTEM__RELABELFROM, NULL); |
| 510 | if (rc) |
| 511 | return rc; |
| 512 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 513 | rc = avc_has_perm(&selinux_state, |
| 514 | tsec->sid, sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 515 | FILESYSTEM__RELABELTO, NULL); |
| 516 | return rc; |
| 517 | } |
| 518 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 519 | static int may_context_mount_inode_relabel(u32 sid, |
| 520 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 521 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 522 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 523 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 524 | int rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 525 | rc = avc_has_perm(&selinux_state, |
| 526 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 527 | FILESYSTEM__RELABELFROM, NULL); |
| 528 | if (rc) |
| 529 | return rc; |
| 530 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 531 | rc = avc_has_perm(&selinux_state, |
| 532 | sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 533 | FILESYSTEM__ASSOCIATE, NULL); |
| 534 | return rc; |
| 535 | } |
| 536 | |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 537 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
| 538 | { |
| 539 | struct superblock_security_struct *sbsec = sb->s_security; |
| 540 | |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 541 | return sbsec->behavior == SECURITY_FS_USE_XATTR || |
| 542 | sbsec->behavior == SECURITY_FS_USE_TRANS || |
| 543 | sbsec->behavior == SECURITY_FS_USE_TASK || |
J. Bruce Fields | 9fc2b4b | 2015-06-04 15:57:25 -0400 | [diff] [blame] | 544 | sbsec->behavior == SECURITY_FS_USE_NATIVE || |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 545 | /* Special handling. Genfs but also in-core setxattr handler */ |
| 546 | !strcmp(sb->s_type->name, "sysfs") || |
| 547 | !strcmp(sb->s_type->name, "pstore") || |
| 548 | !strcmp(sb->s_type->name, "debugfs") || |
Yongqin Liu | a2c7c6f | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 549 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 550 | !strcmp(sb->s_type->name, "rootfs") || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 551 | (selinux_policycap_cgroupseclabel() && |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 552 | (!strcmp(sb->s_type->name, "cgroup") || |
| 553 | !strcmp(sb->s_type->name, "cgroup2"))); |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 554 | } |
| 555 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 556 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | struct superblock_security_struct *sbsec = sb->s_security; |
| 559 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 560 | struct inode *root_inode = d_backing_inode(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | int rc = 0; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 564 | /* Make sure that the xattr handler exists and that no |
| 565 | error other than -ENODATA is returned by getxattr on |
| 566 | the root directory. -ENODATA is ok, as this may be |
| 567 | the first boot of the SELinux kernel before we have |
| 568 | assigned xattr values to the filesystem. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 569 | if (!(root_inode->i_opflags & IOP_XATTR)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 570 | pr_warn("SELinux: (dev %s, type %s) has no " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 571 | "xattr support\n", sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | rc = -EOPNOTSUPP; |
| 573 | goto out; |
| 574 | } |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 575 | |
| 576 | rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (rc < 0 && rc != -ENODATA) { |
| 578 | if (rc == -EOPNOTSUPP) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 579 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 580 | "%s) has no security xattr handler\n", |
| 581 | sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 583 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 584 | "%s) getxattr errno %d\n", sb->s_id, |
| 585 | sb->s_type->name, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | goto out; |
| 587 | } |
| 588 | } |
| 589 | |
Eric Paris | eadcabc | 2012-08-24 15:59:14 -0400 | [diff] [blame] | 590 | sbsec->flags |= SE_SBINITIALIZED; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply |
| 594 | * leave the flag untouched because sb_clone_mnt_opts might be handing |
| 595 | * us a superblock that needs the flag to be cleared. |
| 596 | */ |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 597 | if (selinux_is_sblabel_mnt(sb)) |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 598 | sbsec->flags |= SBLABEL_MNT; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 599 | else |
| 600 | sbsec->flags &= ~SBLABEL_MNT; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 603 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* Initialize any other inodes associated with the superblock, e.g. |
| 606 | inodes created prior to initial policy load or inodes created |
| 607 | during get_sb by a pseudo filesystem that directly |
| 608 | populates itself. */ |
| 609 | spin_lock(&sbsec->isec_lock); |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 610 | while (!list_empty(&sbsec->isec_head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | struct inode_security_struct *isec = |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 612 | list_first_entry(&sbsec->isec_head, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 613 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct inode *inode = isec->inode; |
Stephen Smalley | 923190d | 2014-10-06 16:32:52 -0400 | [diff] [blame] | 615 | list_del_init(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | spin_unlock(&sbsec->isec_lock); |
| 617 | inode = igrab(inode); |
| 618 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 619 | if (!IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | inode_doinit(inode); |
| 621 | iput(inode); |
| 622 | } |
| 623 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | spin_unlock(&sbsec->isec_lock); |
| 626 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 627 | return rc; |
| 628 | } |
| 629 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 630 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 631 | u32 old_sid, u32 new_sid) |
| 632 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 633 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 634 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 635 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 636 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 637 | if (!(sbsec->flags & flag) || |
| 638 | (old_sid != new_sid)) |
| 639 | return 1; |
| 640 | |
| 641 | /* check if we were passed the same options twice, |
| 642 | * aka someone passed context=a,context=b |
| 643 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 644 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 645 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 646 | return 1; |
| 647 | return 0; |
| 648 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 649 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 650 | static int parse_sid(struct super_block *sb, const char *s, u32 *sid) |
| 651 | { |
| 652 | int rc = security_context_str_to_sid(&selinux_state, s, |
| 653 | sid, GFP_KERNEL); |
| 654 | if (rc) |
| 655 | pr_warn("SELinux: security_context_str_to_sid" |
| 656 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 657 | s, sb->s_id, sb->s_type->name, rc); |
| 658 | return rc; |
| 659 | } |
| 660 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 661 | /* |
| 662 | * Allow filesystems with binary mount data to explicitly set mount point |
| 663 | * labeling information. |
| 664 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 665 | static int selinux_set_mnt_opts(struct super_block *sb, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 666 | void *mnt_opts, |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 667 | unsigned long kern_flags, |
| 668 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 669 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 670 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 671 | struct superblock_security_struct *sbsec = sb->s_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 672 | struct dentry *root = sbsec->sb->s_root; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 673 | struct selinux_mnt_opts *opts = mnt_opts; |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 674 | struct inode_security_struct *root_isec; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 675 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 676 | u32 defcontext_sid = 0; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 677 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 678 | |
| 679 | mutex_lock(&sbsec->lock); |
| 680 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 681 | if (!selinux_state.initialized) { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 682 | if (!opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 683 | /* Defer initialization until selinux_complete_init, |
| 684 | after the initial policy is loaded and the security |
| 685 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 686 | goto out; |
| 687 | } |
| 688 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 689 | pr_warn("SELinux: Unable to set superblock options " |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 690 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 691 | goto out; |
| 692 | } |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 693 | if (kern_flags && !set_kern_flags) { |
| 694 | /* Specifying internal flags without providing a place to |
| 695 | * place the results is not allowed */ |
| 696 | rc = -EINVAL; |
| 697 | goto out; |
| 698 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 699 | |
| 700 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 701 | * Binary mount data FS will come through this function twice. Once |
| 702 | * from an explicit call and once from the generic calls from the vfs. |
| 703 | * Since the generic VFS calls will not contain any security mount data |
| 704 | * we need to skip the double mount verification. |
| 705 | * |
| 706 | * This does open a hole in which we will not notice if the first |
| 707 | * mount using this sb set explict options and a second mount using |
| 708 | * this sb does not set any security options. (The first options |
| 709 | * will be used for both mounts) |
| 710 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 711 | if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 712 | && !opts) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 713 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 714 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 715 | root_isec = backing_inode_security_novalidate(root); |
| 716 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 717 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 718 | * parse the mount options, check if they are valid sids. |
| 719 | * also check if someone is trying to mount the same sb more |
| 720 | * than once with different security options. |
| 721 | */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 722 | if (opts) { |
| 723 | if (opts->fscontext) { |
| 724 | rc = parse_sid(sb, opts->fscontext, &fscontext_sid); |
| 725 | if (rc) |
| 726 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 727 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 728 | fscontext_sid)) |
| 729 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 730 | sbsec->flags |= FSCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 731 | } |
| 732 | if (opts->context) { |
| 733 | rc = parse_sid(sb, opts->context, &context_sid); |
| 734 | if (rc) |
| 735 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 736 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 737 | context_sid)) |
| 738 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 739 | sbsec->flags |= CONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 740 | } |
| 741 | if (opts->rootcontext) { |
| 742 | rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); |
| 743 | if (rc) |
| 744 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 745 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 746 | rootcontext_sid)) |
| 747 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 748 | sbsec->flags |= ROOTCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 749 | } |
| 750 | if (opts->defcontext) { |
| 751 | rc = parse_sid(sb, opts->defcontext, &defcontext_sid); |
| 752 | if (rc) |
| 753 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 754 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 755 | defcontext_sid)) |
| 756 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 757 | sbsec->flags |= DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 761 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 762 | /* previously mounted with options, but not on this attempt? */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 763 | if ((sbsec->flags & SE_MNTMASK) && !opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 764 | goto out_double_mount; |
| 765 | rc = 0; |
| 766 | goto out; |
| 767 | } |
| 768 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 769 | if (strcmp(sb->s_type->name, "proc") == 0) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 770 | sbsec->flags |= SE_SBPROC | SE_SBGENFS; |
| 771 | |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 772 | if (!strcmp(sb->s_type->name, "debugfs") || |
Jeff Vander Stoep | 6a39118 | 2017-06-20 09:35:33 -0700 | [diff] [blame] | 773 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 774 | !strcmp(sb->s_type->name, "sysfs") || |
Antonio Murdaca | 901ef84 | 2017-02-09 17:02:42 +0100 | [diff] [blame] | 775 | !strcmp(sb->s_type->name, "pstore") || |
| 776 | !strcmp(sb->s_type->name, "cgroup") || |
| 777 | !strcmp(sb->s_type->name, "cgroup2")) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 778 | sbsec->flags |= SE_SBGENFS; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 779 | |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 780 | if (!sbsec->behavior) { |
| 781 | /* |
| 782 | * Determine the labeling behavior to use for this |
| 783 | * filesystem type. |
| 784 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 785 | rc = security_fs_use(&selinux_state, sb); |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 786 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 787 | pr_warn("%s: security_fs_use(%s) returned %d\n", |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 788 | __func__, sb->s_type->name, rc); |
| 789 | goto out; |
| 790 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 791 | } |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 792 | |
| 793 | /* |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 794 | * If this is a user namespace mount and the filesystem type is not |
| 795 | * explicitly whitelisted, then no contexts are allowed on the command |
| 796 | * line and security labels must be ignored. |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 797 | */ |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 798 | if (sb->s_user_ns != &init_user_ns && |
| 799 | strcmp(sb->s_type->name, "tmpfs") && |
| 800 | strcmp(sb->s_type->name, "ramfs") && |
| 801 | strcmp(sb->s_type->name, "devpts")) { |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 802 | if (context_sid || fscontext_sid || rootcontext_sid || |
| 803 | defcontext_sid) { |
| 804 | rc = -EACCES; |
| 805 | goto out; |
| 806 | } |
| 807 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 808 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 809 | rc = security_transition_sid(&selinux_state, |
| 810 | current_sid(), |
| 811 | current_sid(), |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 812 | SECCLASS_FILE, NULL, |
| 813 | &sbsec->mntpoint_sid); |
| 814 | if (rc) |
| 815 | goto out; |
| 816 | } |
| 817 | goto out_set_opts; |
| 818 | } |
| 819 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 820 | /* sets the context of the superblock for the fs being mounted. */ |
| 821 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 822 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 823 | if (rc) |
| 824 | goto out; |
| 825 | |
| 826 | sbsec->sid = fscontext_sid; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Switch to using mount point labeling behavior. |
| 831 | * sets the label used on all file below the mountpoint, and will set |
| 832 | * the superblock context if not already set. |
| 833 | */ |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 834 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { |
| 835 | sbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 836 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 837 | } |
| 838 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 839 | if (context_sid) { |
| 840 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 841 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 842 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 843 | if (rc) |
| 844 | goto out; |
| 845 | sbsec->sid = context_sid; |
| 846 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 847 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 848 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 849 | if (rc) |
| 850 | goto out; |
| 851 | } |
| 852 | if (!rootcontext_sid) |
| 853 | rootcontext_sid = context_sid; |
| 854 | |
| 855 | sbsec->mntpoint_sid = context_sid; |
| 856 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 857 | } |
| 858 | |
| 859 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 860 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 861 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 862 | if (rc) |
| 863 | goto out; |
| 864 | |
| 865 | root_isec->sid = rootcontext_sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 866 | root_isec->initialized = LABEL_INITIALIZED; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (defcontext_sid) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 870 | if (sbsec->behavior != SECURITY_FS_USE_XATTR && |
| 871 | sbsec->behavior != SECURITY_FS_USE_NATIVE) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 872 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 873 | pr_warn("SELinux: defcontext option is " |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 874 | "invalid for this filesystem type\n"); |
| 875 | goto out; |
| 876 | } |
| 877 | |
| 878 | if (defcontext_sid != sbsec->def_sid) { |
| 879 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 880 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 881 | if (rc) |
| 882 | goto out; |
| 883 | } |
| 884 | |
| 885 | sbsec->def_sid = defcontext_sid; |
| 886 | } |
| 887 | |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 888 | out_set_opts: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 889 | rc = sb_finish_set_opts(sb); |
| 890 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 891 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 893 | out_double_mount: |
| 894 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 895 | pr_warn("SELinux: mount invalid. Same superblock, different " |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 896 | "security settings for (dev %s, type %s)\n", sb->s_id, |
| 897 | sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 898 | goto out; |
| 899 | } |
| 900 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 901 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
| 902 | const struct super_block *newsb) |
| 903 | { |
| 904 | struct superblock_security_struct *old = oldsb->s_security; |
| 905 | struct superblock_security_struct *new = newsb->s_security; |
| 906 | char oldflags = old->flags & SE_MNTMASK; |
| 907 | char newflags = new->flags & SE_MNTMASK; |
| 908 | |
| 909 | if (oldflags != newflags) |
| 910 | goto mismatch; |
| 911 | if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) |
| 912 | goto mismatch; |
| 913 | if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) |
| 914 | goto mismatch; |
| 915 | if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) |
| 916 | goto mismatch; |
| 917 | if (oldflags & ROOTCONTEXT_MNT) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 918 | struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); |
| 919 | struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 920 | if (oldroot->sid != newroot->sid) |
| 921 | goto mismatch; |
| 922 | } |
| 923 | return 0; |
| 924 | mismatch: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 925 | pr_warn("SELinux: mount invalid. Same superblock, " |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 926 | "different security settings for (dev %s, " |
| 927 | "type %s)\n", newsb->s_id, newsb->s_type->name); |
| 928 | return -EBUSY; |
| 929 | } |
| 930 | |
| 931 | static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 932 | struct super_block *newsb, |
| 933 | unsigned long kern_flags, |
| 934 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 935 | { |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 936 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 937 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 938 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 939 | |
| 940 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 941 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 942 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 943 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 944 | /* |
| 945 | * if the parent was able to be mounted it clearly had no special lsm |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 946 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 947 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 948 | if (!selinux_state.initialized) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 949 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 950 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 951 | /* |
| 952 | * Specifying internal flags without providing a place to |
| 953 | * place the results is not allowed. |
| 954 | */ |
| 955 | if (kern_flags && !set_kern_flags) |
| 956 | return -EINVAL; |
| 957 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 958 | /* how can we clone if the old one wasn't set up?? */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 959 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 960 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 961 | /* if fs is reusing a sb, make sure that the contexts match */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 962 | if (newsbsec->flags & SE_SBINITIALIZED) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 963 | return selinux_cmp_sb_context(oldsb, newsb); |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 964 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 965 | mutex_lock(&newsbsec->lock); |
| 966 | |
| 967 | newsbsec->flags = oldsbsec->flags; |
| 968 | |
| 969 | newsbsec->sid = oldsbsec->sid; |
| 970 | newsbsec->def_sid = oldsbsec->def_sid; |
| 971 | newsbsec->behavior = oldsbsec->behavior; |
| 972 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 973 | if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && |
| 974 | !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 975 | rc = security_fs_use(&selinux_state, newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 976 | if (rc) |
| 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { |
| 981 | newsbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 982 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 983 | } |
| 984 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 985 | if (set_context) { |
| 986 | u32 sid = oldsbsec->mntpoint_sid; |
| 987 | |
| 988 | if (!set_fscontext) |
| 989 | newsbsec->sid = sid; |
| 990 | if (!set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 991 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 992 | newisec->sid = sid; |
| 993 | } |
| 994 | newsbsec->mntpoint_sid = sid; |
| 995 | } |
| 996 | if (set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 997 | const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); |
| 998 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 999 | |
| 1000 | newisec->sid = oldisec->sid; |
| 1001 | } |
| 1002 | |
| 1003 | sb_finish_set_opts(newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 1004 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1005 | mutex_unlock(&newsbsec->lock); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 1006 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1007 | } |
| 1008 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1009 | static int selinux_add_opt(int token, const char *s, void **mnt_opts) |
| 1010 | { |
| 1011 | struct selinux_mnt_opts *opts = *mnt_opts; |
| 1012 | |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1013 | if (token == Opt_labelsupport) /* eaten and completely ignored */ |
| 1014 | return 0; |
| 1015 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1016 | if (!opts) { |
| 1017 | opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 1018 | if (!opts) |
| 1019 | return -ENOMEM; |
| 1020 | *mnt_opts = opts; |
| 1021 | } |
| 1022 | if (!s) |
| 1023 | return -ENOMEM; |
| 1024 | switch (token) { |
| 1025 | case Opt_context: |
| 1026 | if (opts->context || opts->defcontext) |
| 1027 | goto Einval; |
| 1028 | opts->context = s; |
| 1029 | break; |
| 1030 | case Opt_fscontext: |
| 1031 | if (opts->fscontext) |
| 1032 | goto Einval; |
| 1033 | opts->fscontext = s; |
| 1034 | break; |
| 1035 | case Opt_rootcontext: |
| 1036 | if (opts->rootcontext) |
| 1037 | goto Einval; |
| 1038 | opts->rootcontext = s; |
| 1039 | break; |
| 1040 | case Opt_defcontext: |
| 1041 | if (opts->context || opts->defcontext) |
| 1042 | goto Einval; |
| 1043 | opts->defcontext = s; |
| 1044 | break; |
| 1045 | } |
| 1046 | return 0; |
| 1047 | Einval: |
| 1048 | pr_warn(SEL_MOUNT_FAIL_MSG); |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1049 | return -EINVAL; |
| 1050 | } |
| 1051 | |
Adrian Bunk | 2e1479d | 2008-03-17 22:29:23 +0200 | [diff] [blame] | 1052 | static int selinux_parse_opts_str(char *options, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 1053 | void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1054 | { |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1055 | char *p = options, *next; |
| 1056 | int rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1057 | |
| 1058 | /* Standard string-based options. */ |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1059 | for (p = options; *p; p = next) { |
| 1060 | int token, len; |
| 1061 | char *arg = NULL; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1062 | |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1063 | next = strchr(p, '|'); |
| 1064 | if (next) { |
| 1065 | len = next++ - p; |
| 1066 | } else { |
| 1067 | len = strlen(p); |
| 1068 | next = p + len; |
| 1069 | } |
| 1070 | |
| 1071 | if (!len) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1072 | continue; |
| 1073 | |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1074 | token = match_opt_prefix(p, len, &arg); |
| 1075 | if (arg) |
| 1076 | arg = kmemdup_nul(arg, p + len - arg, GFP_KERNEL); |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1077 | rc = selinux_add_opt(token, arg, mnt_opts); |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1078 | if (rc) { |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1079 | kfree(arg); |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame^] | 1080 | selinux_free_mnt_opts(*mnt_opts); |
| 1081 | *mnt_opts = NULL; |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1082 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1083 | } |
| 1084 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 1085 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1088 | static int show_sid(struct seq_file *m, u32 sid) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1089 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1090 | char *context = NULL; |
| 1091 | u32 len; |
| 1092 | int rc; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1093 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1094 | rc = security_sid_to_context(&selinux_state, sid, |
| 1095 | &context, &len); |
| 1096 | if (!rc) { |
| 1097 | bool has_comma = context && strchr(context, ','); |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 1098 | |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1099 | if (has_comma) |
| 1100 | seq_putc(m, '\"'); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1101 | seq_escape(m, context, "\"\n\\"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1102 | if (has_comma) |
| 1103 | seq_putc(m, '\"'); |
| 1104 | } |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1105 | kfree(context); |
| 1106 | return rc; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1110 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1111 | struct superblock_security_struct *sbsec = sb->s_security; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1112 | int rc; |
| 1113 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1114 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 1115 | return 0; |
| 1116 | |
| 1117 | if (!selinux_state.initialized) |
| 1118 | return 0; |
| 1119 | |
| 1120 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 1121 | seq_putc(m, ','); |
| 1122 | seq_puts(m, FSCONTEXT_STR); |
| 1123 | rc = show_sid(m, sbsec->sid); |
| 1124 | if (rc) |
| 1125 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1126 | } |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1127 | if (sbsec->flags & CONTEXT_MNT) { |
| 1128 | seq_putc(m, ','); |
| 1129 | seq_puts(m, CONTEXT_STR); |
| 1130 | rc = show_sid(m, sbsec->mntpoint_sid); |
| 1131 | if (rc) |
| 1132 | return rc; |
| 1133 | } |
| 1134 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 1135 | seq_putc(m, ','); |
| 1136 | seq_puts(m, DEFCONTEXT_STR); |
| 1137 | rc = show_sid(m, sbsec->def_sid); |
| 1138 | if (rc) |
| 1139 | return rc; |
| 1140 | } |
| 1141 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
| 1142 | struct dentry *root = sbsec->sb->s_root; |
| 1143 | struct inode_security_struct *isec = backing_inode_security(root); |
| 1144 | seq_putc(m, ','); |
| 1145 | seq_puts(m, ROOTCONTEXT_STR); |
| 1146 | rc = show_sid(m, isec->sid); |
| 1147 | if (rc) |
| 1148 | return rc; |
| 1149 | } |
| 1150 | if (sbsec->flags & SBLABEL_MNT) { |
| 1151 | seq_putc(m, ','); |
| 1152 | seq_puts(m, LABELSUPP_STR); |
| 1153 | } |
| 1154 | return 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1155 | } |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1158 | { |
| 1159 | switch (mode & S_IFMT) { |
| 1160 | case S_IFSOCK: |
| 1161 | return SECCLASS_SOCK_FILE; |
| 1162 | case S_IFLNK: |
| 1163 | return SECCLASS_LNK_FILE; |
| 1164 | case S_IFREG: |
| 1165 | return SECCLASS_FILE; |
| 1166 | case S_IFBLK: |
| 1167 | return SECCLASS_BLK_FILE; |
| 1168 | case S_IFDIR: |
| 1169 | return SECCLASS_DIR; |
| 1170 | case S_IFCHR: |
| 1171 | return SECCLASS_CHR_FILE; |
| 1172 | case S_IFIFO: |
| 1173 | return SECCLASS_FIFO_FILE; |
| 1174 | |
| 1175 | } |
| 1176 | |
| 1177 | return SECCLASS_FILE; |
| 1178 | } |
| 1179 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1180 | static inline int default_protocol_stream(int protocol) |
| 1181 | { |
| 1182 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); |
| 1183 | } |
| 1184 | |
| 1185 | static inline int default_protocol_dgram(int protocol) |
| 1186 | { |
| 1187 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1188 | } |
| 1189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1191 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1192 | int extsockclass = selinux_policycap_extsockclass(); |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | switch (family) { |
| 1195 | case PF_UNIX: |
| 1196 | switch (type) { |
| 1197 | case SOCK_STREAM: |
| 1198 | case SOCK_SEQPACKET: |
| 1199 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1200 | case SOCK_DGRAM: |
Luis Ressel | 2a764b5 | 2017-07-25 15:13:41 -0400 | [diff] [blame] | 1201 | case SOCK_RAW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1203 | } |
| 1204 | break; |
| 1205 | case PF_INET: |
| 1206 | case PF_INET6: |
| 1207 | switch (type) { |
| 1208 | case SOCK_STREAM: |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1209 | case SOCK_SEQPACKET: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1210 | if (default_protocol_stream(protocol)) |
| 1211 | return SECCLASS_TCP_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1212 | else if (extsockclass && protocol == IPPROTO_SCTP) |
| 1213 | return SECCLASS_SCTP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1214 | else |
| 1215 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1217 | if (default_protocol_dgram(protocol)) |
| 1218 | return SECCLASS_UDP_SOCKET; |
Stephen Smalley | ef37979 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 1219 | else if (extsockclass && (protocol == IPPROTO_ICMP || |
| 1220 | protocol == IPPROTO_ICMPV6)) |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1221 | return SECCLASS_ICMP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1222 | else |
| 1223 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1224 | case SOCK_DCCP: |
| 1225 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1226 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | return SECCLASS_RAWIP_SOCKET; |
| 1228 | } |
| 1229 | break; |
| 1230 | case PF_NETLINK: |
| 1231 | switch (protocol) { |
| 1232 | case NETLINK_ROUTE: |
| 1233 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1234 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1236 | case NETLINK_NFLOG: |
| 1237 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1238 | case NETLINK_XFRM: |
| 1239 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1240 | case NETLINK_SELINUX: |
| 1241 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1242 | case NETLINK_ISCSI: |
| 1243 | return SECCLASS_NETLINK_ISCSI_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | case NETLINK_AUDIT: |
| 1245 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1246 | case NETLINK_FIB_LOOKUP: |
| 1247 | return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; |
| 1248 | case NETLINK_CONNECTOR: |
| 1249 | return SECCLASS_NETLINK_CONNECTOR_SOCKET; |
| 1250 | case NETLINK_NETFILTER: |
| 1251 | return SECCLASS_NETLINK_NETFILTER_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | case NETLINK_DNRTMSG: |
| 1253 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1254 | case NETLINK_KOBJECT_UEVENT: |
| 1255 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1256 | case NETLINK_GENERIC: |
| 1257 | return SECCLASS_NETLINK_GENERIC_SOCKET; |
| 1258 | case NETLINK_SCSITRANSPORT: |
| 1259 | return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; |
| 1260 | case NETLINK_RDMA: |
| 1261 | return SECCLASS_NETLINK_RDMA_SOCKET; |
| 1262 | case NETLINK_CRYPTO: |
| 1263 | return SECCLASS_NETLINK_CRYPTO_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | default: |
| 1265 | return SECCLASS_NETLINK_SOCKET; |
| 1266 | } |
| 1267 | case PF_PACKET: |
| 1268 | return SECCLASS_PACKET_SOCKET; |
| 1269 | case PF_KEY: |
| 1270 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1271 | case PF_APPLETALK: |
| 1272 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1275 | if (extsockclass) { |
| 1276 | switch (family) { |
| 1277 | case PF_AX25: |
| 1278 | return SECCLASS_AX25_SOCKET; |
| 1279 | case PF_IPX: |
| 1280 | return SECCLASS_IPX_SOCKET; |
| 1281 | case PF_NETROM: |
| 1282 | return SECCLASS_NETROM_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1283 | case PF_ATMPVC: |
| 1284 | return SECCLASS_ATMPVC_SOCKET; |
| 1285 | case PF_X25: |
| 1286 | return SECCLASS_X25_SOCKET; |
| 1287 | case PF_ROSE: |
| 1288 | return SECCLASS_ROSE_SOCKET; |
| 1289 | case PF_DECnet: |
| 1290 | return SECCLASS_DECNET_SOCKET; |
| 1291 | case PF_ATMSVC: |
| 1292 | return SECCLASS_ATMSVC_SOCKET; |
| 1293 | case PF_RDS: |
| 1294 | return SECCLASS_RDS_SOCKET; |
| 1295 | case PF_IRDA: |
| 1296 | return SECCLASS_IRDA_SOCKET; |
| 1297 | case PF_PPPOX: |
| 1298 | return SECCLASS_PPPOX_SOCKET; |
| 1299 | case PF_LLC: |
| 1300 | return SECCLASS_LLC_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1301 | case PF_CAN: |
| 1302 | return SECCLASS_CAN_SOCKET; |
| 1303 | case PF_TIPC: |
| 1304 | return SECCLASS_TIPC_SOCKET; |
| 1305 | case PF_BLUETOOTH: |
| 1306 | return SECCLASS_BLUETOOTH_SOCKET; |
| 1307 | case PF_IUCV: |
| 1308 | return SECCLASS_IUCV_SOCKET; |
| 1309 | case PF_RXRPC: |
| 1310 | return SECCLASS_RXRPC_SOCKET; |
| 1311 | case PF_ISDN: |
| 1312 | return SECCLASS_ISDN_SOCKET; |
| 1313 | case PF_PHONET: |
| 1314 | return SECCLASS_PHONET_SOCKET; |
| 1315 | case PF_IEEE802154: |
| 1316 | return SECCLASS_IEEE802154_SOCKET; |
| 1317 | case PF_CAIF: |
| 1318 | return SECCLASS_CAIF_SOCKET; |
| 1319 | case PF_ALG: |
| 1320 | return SECCLASS_ALG_SOCKET; |
| 1321 | case PF_NFC: |
| 1322 | return SECCLASS_NFC_SOCKET; |
| 1323 | case PF_VSOCK: |
| 1324 | return SECCLASS_VSOCK_SOCKET; |
| 1325 | case PF_KCM: |
| 1326 | return SECCLASS_KCM_SOCKET; |
| 1327 | case PF_QIPCRTR: |
| 1328 | return SECCLASS_QIPCRTR_SOCKET; |
Linus Torvalds | 3051bf3 | 2017-02-22 10:15:09 -0800 | [diff] [blame] | 1329 | case PF_SMC: |
| 1330 | return SECCLASS_SMC_SOCKET; |
Björn Töpel | 68e8b84 | 2018-05-02 13:01:22 +0200 | [diff] [blame] | 1331 | case PF_XDP: |
| 1332 | return SECCLASS_XDP_SOCKET; |
| 1333 | #if PF_MAX > 45 |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1334 | #error New address family defined, please update this function. |
| 1335 | #endif |
| 1336 | } |
| 1337 | } |
| 1338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | return SECCLASS_SOCKET; |
| 1340 | } |
| 1341 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1342 | static int selinux_genfs_get_sid(struct dentry *dentry, |
| 1343 | u16 tclass, |
| 1344 | u16 flags, |
| 1345 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1347 | int rc; |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1348 | struct super_block *sb = dentry->d_sb; |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1349 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1351 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | if (!buffer) |
| 1353 | return -ENOMEM; |
| 1354 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1355 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1356 | if (IS_ERR(path)) |
| 1357 | rc = PTR_ERR(path); |
| 1358 | else { |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1359 | if (flags & SE_SBPROC) { |
| 1360 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1361 | * PID part to get a valid selinux labeling. |
| 1362 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1363 | while (path[1] >= '0' && path[1] <= '9') { |
| 1364 | path[1] = '/'; |
| 1365 | path++; |
| 1366 | } |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1367 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1368 | rc = security_genfs_sid(&selinux_state, sb->s_type->name, |
| 1369 | path, tclass, sid); |
Stephen Smalley | 7bb185e | 2018-09-04 16:51:36 -0400 | [diff] [blame] | 1370 | if (rc == -ENOENT) { |
| 1371 | /* No match in policy, mark as unlabeled. */ |
| 1372 | *sid = SECINITSID_UNLABELED; |
| 1373 | rc = 0; |
| 1374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | free_page((unsigned long)buffer); |
| 1377 | return rc; |
| 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | /* The inode's security attributes must be initialized before first use. */ |
| 1381 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1382 | { |
| 1383 | struct superblock_security_struct *sbsec = NULL; |
| 1384 | struct inode_security_struct *isec = inode->i_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1385 | u32 task_sid, sid = 0; |
| 1386 | u16 sclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | struct dentry *dentry; |
| 1388 | #define INITCONTEXTLEN 255 |
| 1389 | char *context = NULL; |
| 1390 | unsigned len = 0; |
| 1391 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1393 | if (isec->initialized == LABEL_INITIALIZED) |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1394 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1396 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1397 | if (isec->initialized == LABEL_INITIALIZED) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1398 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1400 | if (isec->sclass == SECCLASS_FILE) |
| 1401 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 1402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1404 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | /* Defer initialization until selinux_complete_init, |
| 1406 | after the initial policy is loaded and the security |
| 1407 | server is ready to handle calls. */ |
| 1408 | spin_lock(&sbsec->isec_lock); |
| 1409 | if (list_empty(&isec->list)) |
| 1410 | list_add(&isec->list, &sbsec->isec_head); |
| 1411 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1412 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1415 | sclass = isec->sclass; |
| 1416 | task_sid = isec->task_sid; |
| 1417 | sid = isec->sid; |
| 1418 | isec->initialized = LABEL_PENDING; |
| 1419 | spin_unlock(&isec->lock); |
| 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | switch (sbsec->behavior) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 1422 | case SECURITY_FS_USE_NATIVE: |
| 1423 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | case SECURITY_FS_USE_XATTR: |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1425 | if (!(inode->i_opflags & IOP_XATTR)) { |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1426 | sid = sbsec->def_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | break; |
| 1428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | /* Need a dentry, since the xattr API requires one. |
| 1430 | Life would be simpler if we could just pass the inode. */ |
| 1431 | if (opt_dentry) { |
| 1432 | /* Called from d_instantiate or d_splice_alias. */ |
| 1433 | dentry = dget(opt_dentry); |
| 1434 | } else { |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1435 | /* |
| 1436 | * Called from selinux_complete_init, try to find a dentry. |
| 1437 | * Some filesystems really want a connected one, so try |
| 1438 | * that first. We could split SECURITY_FS_USE_XATTR in |
| 1439 | * two, depending upon that... |
| 1440 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1442 | if (!dentry) |
| 1443 | dentry = d_find_any_alias(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1446 | /* |
| 1447 | * this is can be hit on boot when a file is accessed |
| 1448 | * before the policy is loaded. When we load policy we |
| 1449 | * may find inodes that have no dentry on the |
| 1450 | * sbsec->isec_head list. No reason to complain as these |
| 1451 | * will get fixed up the next time we go through |
| 1452 | * inode_doinit with a dentry, before these inodes could |
| 1453 | * be used again by userspace. |
| 1454 | */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1455 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | len = INITCONTEXTLEN; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1459 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | if (!context) { |
| 1461 | rc = -ENOMEM; |
| 1462 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1463 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1465 | context[len] = '\0'; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1466 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | if (rc == -ERANGE) { |
James Morris | 314dabb | 2009-08-10 22:00:13 +1000 | [diff] [blame] | 1468 | kfree(context); |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | /* Need a larger buffer. Query for the right size. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1471 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (rc < 0) { |
| 1473 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1474 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | len = rc; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1477 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | if (!context) { |
| 1479 | rc = -ENOMEM; |
| 1480 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1481 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1483 | context[len] = '\0'; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1484 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
| 1486 | dput(dentry); |
| 1487 | if (rc < 0) { |
| 1488 | if (rc != -ENODATA) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1489 | pr_warn("SELinux: %s: getxattr returned " |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 1490 | "%d for dev=%s ino=%ld\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 1492 | kfree(context); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1493 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | } |
| 1495 | /* Map ENODATA to the default file SID */ |
| 1496 | sid = sbsec->def_sid; |
| 1497 | rc = 0; |
| 1498 | } else { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1499 | rc = security_context_to_sid_default(&selinux_state, |
| 1500 | context, rc, &sid, |
Stephen Smalley | 869ab51 | 2008-04-04 08:46:05 -0400 | [diff] [blame] | 1501 | sbsec->def_sid, |
| 1502 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (rc) { |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1504 | char *dev = inode->i_sb->s_id; |
| 1505 | unsigned long ino = inode->i_ino; |
| 1506 | |
| 1507 | if (rc == -EINVAL) { |
| 1508 | if (printk_ratelimit()) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1509 | pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1510 | "context=%s. This indicates you may need to relabel the inode or the " |
| 1511 | "filesystem in question.\n", ino, dev, context); |
| 1512 | } else { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1513 | pr_warn("SELinux: %s: context_to_sid(%s) " |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1514 | "returned %d for dev=%s ino=%ld\n", |
| 1515 | __func__, context, -rc, dev, ino); |
| 1516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | kfree(context); |
| 1518 | /* Leave with the unlabeled SID */ |
| 1519 | rc = 0; |
| 1520 | break; |
| 1521 | } |
| 1522 | } |
| 1523 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | case SECURITY_FS_USE_TASK: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1526 | sid = task_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | break; |
| 1528 | case SECURITY_FS_USE_TRANS: |
| 1529 | /* Default to the fs SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1530 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* Try to obtain a transition SID. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1533 | rc = security_transition_sid(&selinux_state, task_sid, sid, |
| 1534 | sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1536 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1538 | case SECURITY_FS_USE_MNTPOINT: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1539 | sid = sbsec->mntpoint_sid; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1540 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1542 | /* Default to the fs superblock SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1543 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1545 | if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1546 | /* We must have a dentry to determine the label on |
| 1547 | * procfs inodes */ |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1548 | if (opt_dentry) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1549 | /* Called from d_instantiate or |
| 1550 | * d_splice_alias. */ |
| 1551 | dentry = dget(opt_dentry); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1552 | } else { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1553 | /* Called from selinux_complete_init, try to |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1554 | * find a dentry. Some filesystems really want |
| 1555 | * a connected one, so try that first. |
| 1556 | */ |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1557 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1558 | if (!dentry) |
| 1559 | dentry = d_find_any_alias(inode); |
| 1560 | } |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1561 | /* |
| 1562 | * This can be hit on boot when a file is accessed |
| 1563 | * before the policy is loaded. When we load policy we |
| 1564 | * may find inodes that have no dentry on the |
| 1565 | * sbsec->isec_head list. No reason to complain as |
| 1566 | * these will get fixed up the next time we go through |
| 1567 | * inode_doinit() with a dentry, before these inodes |
| 1568 | * could be used again by userspace. |
| 1569 | */ |
| 1570 | if (!dentry) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1571 | goto out; |
| 1572 | rc = selinux_genfs_get_sid(dentry, sclass, |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1573 | sbsec->flags, &sid); |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1574 | dput(dentry); |
| 1575 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1576 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | break; |
| 1579 | } |
| 1580 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1581 | out: |
| 1582 | spin_lock(&isec->lock); |
| 1583 | if (isec->initialized == LABEL_PENDING) { |
| 1584 | if (!sid || rc) { |
| 1585 | isec->initialized = LABEL_INVALID; |
| 1586 | goto out_unlock; |
| 1587 | } |
| 1588 | |
| 1589 | isec->initialized = LABEL_INITIALIZED; |
| 1590 | isec->sid = sid; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1593 | out_unlock: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1594 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | return rc; |
| 1596 | } |
| 1597 | |
| 1598 | /* Convert a Linux signal to an access vector. */ |
| 1599 | static inline u32 signal_to_av(int sig) |
| 1600 | { |
| 1601 | u32 perm = 0; |
| 1602 | |
| 1603 | switch (sig) { |
| 1604 | case SIGCHLD: |
| 1605 | /* Commonly granted from child to parent. */ |
| 1606 | perm = PROCESS__SIGCHLD; |
| 1607 | break; |
| 1608 | case SIGKILL: |
| 1609 | /* Cannot be caught or ignored */ |
| 1610 | perm = PROCESS__SIGKILL; |
| 1611 | break; |
| 1612 | case SIGSTOP: |
| 1613 | /* Cannot be caught or ignored */ |
| 1614 | perm = PROCESS__SIGSTOP; |
| 1615 | break; |
| 1616 | default: |
| 1617 | /* All other signals. */ |
| 1618 | perm = PROCESS__SIGNAL; |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | return perm; |
| 1623 | } |
| 1624 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1625 | #if CAP_LAST_CAP > 63 |
| 1626 | #error Fix SELinux to handle capabilities > 63. |
| 1627 | #endif |
| 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1630 | static int cred_has_capability(const struct cred *cred, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1631 | int cap, int audit, bool initns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1633 | struct common_audit_data ad; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1634 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1635 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1636 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1637 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1638 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1640 | ad.type = LSM_AUDIT_DATA_CAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | ad.u.cap = cap; |
| 1642 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1643 | switch (CAP_TO_INDEX(cap)) { |
| 1644 | case 0: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1645 | sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1646 | break; |
| 1647 | case 1: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1648 | sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1649 | break; |
| 1650 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1651 | pr_err("SELinux: out of range capability %d\n", cap); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1652 | BUG(); |
Eric Paris | a35c6c83 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1653 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1654 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1655 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1656 | rc = avc_has_perm_noaudit(&selinux_state, |
| 1657 | sid, sid, sclass, av, 0, &avd); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1658 | if (audit == SECURITY_CAP_AUDIT) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1659 | int rc2 = avc_audit(&selinux_state, |
| 1660 | sid, sid, sclass, av, &avd, rc, &ad, 0); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1661 | if (rc2) |
| 1662 | return rc2; |
| 1663 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1664 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | /* Check whether a task has a particular permission to an inode. |
| 1668 | The 'adp' parameter is optional and allows other audit |
| 1669 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1670 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | struct inode *inode, |
| 1672 | u32 perms, |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1673 | struct common_audit_data *adp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1676 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1678 | validate_creds(cred); |
| 1679 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1680 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1681 | return 0; |
| 1682 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1683 | sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | isec = inode->i_security; |
| 1685 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1686 | return avc_has_perm(&selinux_state, |
| 1687 | sid, isec->sid, isec->sclass, perms, adp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1691 | the dentry to help the auditing code to more easily generate the |
| 1692 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1693 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | struct dentry *dentry, |
| 1695 | u32 av) |
| 1696 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1697 | struct inode *inode = d_backing_inode(dentry); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1698 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1699 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1700 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1701 | ad.u.dentry = dentry; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1702 | __inode_security_revalidate(inode, dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1703 | return inode_has_perm(cred, inode, av, &ad); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1707 | the path to help the auditing code to more easily generate the |
| 1708 | pathname if needed. */ |
| 1709 | static inline int path_has_perm(const struct cred *cred, |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1710 | const struct path *path, |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1711 | u32 av) |
| 1712 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1713 | struct inode *inode = d_backing_inode(path->dentry); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1714 | struct common_audit_data ad; |
| 1715 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1716 | ad.type = LSM_AUDIT_DATA_PATH; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1717 | ad.u.path = *path; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1718 | __inode_security_revalidate(inode, path->dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1719 | return inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1722 | /* Same as path_has_perm, but uses the inode from the file struct. */ |
| 1723 | static inline int file_path_has_perm(const struct cred *cred, |
| 1724 | struct file *file, |
| 1725 | u32 av) |
| 1726 | { |
| 1727 | struct common_audit_data ad; |
| 1728 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1729 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1730 | ad.u.file = file; |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1731 | return inode_has_perm(cred, file_inode(file), av, &ad); |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1734 | #ifdef CONFIG_BPF_SYSCALL |
| 1735 | static int bpf_fd_pass(struct file *file, u32 sid); |
| 1736 | #endif |
| 1737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | /* Check whether a task can use an open file descriptor to |
| 1739 | access an inode in a given way. Check access to the |
| 1740 | descriptor itself, and then use dentry_has_perm to |
| 1741 | check a particular permission to the file. |
| 1742 | Access to the descriptor is implicitly granted if it |
| 1743 | has the same SID as the process. If av is zero, then |
| 1744 | access to the file is not checked, e.g. for cases |
| 1745 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1746 | static int file_has_perm(const struct cred *cred, |
| 1747 | struct file *file, |
| 1748 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | struct file_security_struct *fsec = file->f_security; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1751 | struct inode *inode = file_inode(file); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1752 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1753 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | int rc; |
| 1755 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1756 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1757 | ad.u.file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1759 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1760 | rc = avc_has_perm(&selinux_state, |
| 1761 | sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | SECCLASS_FD, |
| 1763 | FD__USE, |
| 1764 | &ad); |
| 1765 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1766 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1769 | #ifdef CONFIG_BPF_SYSCALL |
| 1770 | rc = bpf_fd_pass(file, cred_sid(cred)); |
| 1771 | if (rc) |
| 1772 | return rc; |
| 1773 | #endif |
| 1774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1776 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | if (av) |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1778 | rc = inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1780 | out: |
| 1781 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1784 | /* |
| 1785 | * Determine the label for an inode that might be unioned. |
| 1786 | */ |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1787 | static int |
| 1788 | selinux_determine_inode_label(const struct task_security_struct *tsec, |
| 1789 | struct inode *dir, |
| 1790 | const struct qstr *name, u16 tclass, |
| 1791 | u32 *_new_isid) |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1792 | { |
| 1793 | const struct superblock_security_struct *sbsec = dir->i_sb->s_security; |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1794 | |
| 1795 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 1796 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { |
| 1797 | *_new_isid = sbsec->mntpoint_sid; |
| 1798 | } else if ((sbsec->flags & SBLABEL_MNT) && |
| 1799 | tsec->create_sid) { |
| 1800 | *_new_isid = tsec->create_sid; |
| 1801 | } else { |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 1802 | const struct inode_security_struct *dsec = inode_security(dir); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1803 | return security_transition_sid(&selinux_state, tsec->sid, |
| 1804 | dsec->sid, tclass, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1805 | name, _new_isid); |
| 1806 | } |
| 1807 | |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | /* Check whether a task can create a file. */ |
| 1812 | static int may_create(struct inode *dir, |
| 1813 | struct dentry *dentry, |
| 1814 | u16 tclass) |
| 1815 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 1816 | const struct task_security_struct *tsec = current_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | struct inode_security_struct *dsec; |
| 1818 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1819 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1820 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | int rc; |
| 1822 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1823 | dsec = inode_security(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | sbsec = dir->i_sb->s_security; |
| 1825 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1826 | sid = tsec->sid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1827 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1828 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1829 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1831 | rc = avc_has_perm(&selinux_state, |
| 1832 | sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | DIR__ADD_NAME | DIR__SEARCH, |
| 1834 | &ad); |
| 1835 | if (rc) |
| 1836 | return rc; |
| 1837 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1838 | rc = selinux_determine_inode_label(current_security(), dir, |
| 1839 | &dentry->d_name, tclass, &newsid); |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1840 | if (rc) |
| 1841 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1843 | rc = avc_has_perm(&selinux_state, |
| 1844 | sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | if (rc) |
| 1846 | return rc; |
| 1847 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1848 | return avc_has_perm(&selinux_state, |
| 1849 | newsid, sbsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | SECCLASS_FILESYSTEM, |
| 1851 | FILESYSTEM__ASSOCIATE, &ad); |
| 1852 | } |
| 1853 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1854 | #define MAY_LINK 0 |
| 1855 | #define MAY_UNLINK 1 |
| 1856 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1859 | static int may_link(struct inode *dir, |
| 1860 | struct dentry *dentry, |
| 1861 | int kind) |
| 1862 | |
| 1863 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1865 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1866 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | u32 av; |
| 1868 | int rc; |
| 1869 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1870 | dsec = inode_security(dir); |
| 1871 | isec = backing_inode_security(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1873 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1874 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
| 1876 | av = DIR__SEARCH; |
| 1877 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1878 | rc = avc_has_perm(&selinux_state, |
| 1879 | sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | if (rc) |
| 1881 | return rc; |
| 1882 | |
| 1883 | switch (kind) { |
| 1884 | case MAY_LINK: |
| 1885 | av = FILE__LINK; |
| 1886 | break; |
| 1887 | case MAY_UNLINK: |
| 1888 | av = FILE__UNLINK; |
| 1889 | break; |
| 1890 | case MAY_RMDIR: |
| 1891 | av = DIR__RMDIR; |
| 1892 | break; |
| 1893 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1894 | pr_warn("SELinux: %s: unrecognized kind %d\n", |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1895 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | return 0; |
| 1897 | } |
| 1898 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1899 | rc = avc_has_perm(&selinux_state, |
| 1900 | sid, isec->sid, isec->sclass, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | return rc; |
| 1902 | } |
| 1903 | |
| 1904 | static inline int may_rename(struct inode *old_dir, |
| 1905 | struct dentry *old_dentry, |
| 1906 | struct inode *new_dir, |
| 1907 | struct dentry *new_dentry) |
| 1908 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1910 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1911 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | u32 av; |
| 1913 | int old_is_dir, new_is_dir; |
| 1914 | int rc; |
| 1915 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1916 | old_dsec = inode_security(old_dir); |
| 1917 | old_isec = backing_inode_security(old_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1918 | old_is_dir = d_is_dir(old_dentry); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1919 | new_dsec = inode_security(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1921 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1923 | ad.u.dentry = old_dentry; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1924 | rc = avc_has_perm(&selinux_state, |
| 1925 | sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1927 | if (rc) |
| 1928 | return rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1929 | rc = avc_has_perm(&selinux_state, |
| 1930 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | old_isec->sclass, FILE__RENAME, &ad); |
| 1932 | if (rc) |
| 1933 | return rc; |
| 1934 | if (old_is_dir && new_dir != old_dir) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1935 | rc = avc_has_perm(&selinux_state, |
| 1936 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1938 | if (rc) |
| 1939 | return rc; |
| 1940 | } |
| 1941 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1942 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | av = DIR__ADD_NAME | DIR__SEARCH; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1944 | if (d_is_positive(new_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | av |= DIR__REMOVE_NAME; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1946 | rc = avc_has_perm(&selinux_state, |
| 1947 | sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | if (rc) |
| 1949 | return rc; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1950 | if (d_is_positive(new_dentry)) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1951 | new_isec = backing_inode_security(new_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1952 | new_is_dir = d_is_dir(new_dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1953 | rc = avc_has_perm(&selinux_state, |
| 1954 | sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | new_isec->sclass, |
| 1956 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1957 | if (rc) |
| 1958 | return rc; |
| 1959 | } |
| 1960 | |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1965 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | struct super_block *sb, |
| 1967 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1968 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1971 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | sbsec = sb->s_security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1974 | return avc_has_perm(&selinux_state, |
| 1975 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1979 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1980 | { |
| 1981 | u32 av = 0; |
| 1982 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1983 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | if (mask & MAY_EXEC) |
| 1985 | av |= FILE__EXECUTE; |
| 1986 | if (mask & MAY_READ) |
| 1987 | av |= FILE__READ; |
| 1988 | |
| 1989 | if (mask & MAY_APPEND) |
| 1990 | av |= FILE__APPEND; |
| 1991 | else if (mask & MAY_WRITE) |
| 1992 | av |= FILE__WRITE; |
| 1993 | |
| 1994 | } else { |
| 1995 | if (mask & MAY_EXEC) |
| 1996 | av |= DIR__SEARCH; |
| 1997 | if (mask & MAY_WRITE) |
| 1998 | av |= DIR__WRITE; |
| 1999 | if (mask & MAY_READ) |
| 2000 | av |= DIR__READ; |
| 2001 | } |
| 2002 | |
| 2003 | return av; |
| 2004 | } |
| 2005 | |
| 2006 | /* Convert a Linux file to an access vector. */ |
| 2007 | static inline u32 file_to_av(struct file *file) |
| 2008 | { |
| 2009 | u32 av = 0; |
| 2010 | |
| 2011 | if (file->f_mode & FMODE_READ) |
| 2012 | av |= FILE__READ; |
| 2013 | if (file->f_mode & FMODE_WRITE) { |
| 2014 | if (file->f_flags & O_APPEND) |
| 2015 | av |= FILE__APPEND; |
| 2016 | else |
| 2017 | av |= FILE__WRITE; |
| 2018 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 2019 | if (!av) { |
| 2020 | /* |
| 2021 | * Special file opened with flags 3 for ioctl-only use. |
| 2022 | */ |
| 2023 | av = FILE__IOCTL; |
| 2024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
| 2026 | return av; |
| 2027 | } |
| 2028 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2029 | /* |
| 2030 | * Convert a file to an access vector and include the correct open |
| 2031 | * open permission. |
| 2032 | */ |
| 2033 | static inline u32 open_file_to_av(struct file *file) |
| 2034 | { |
| 2035 | u32 av = file_to_av(file); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 2036 | struct inode *inode = file_inode(file); |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2037 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2038 | if (selinux_policycap_openperm() && |
| 2039 | inode->i_sb->s_magic != SOCKFS_MAGIC) |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 2040 | av |= FILE__OPEN; |
| 2041 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2042 | return av; |
| 2043 | } |
| 2044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | /* Hook functions begin here. */ |
| 2046 | |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2047 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 2048 | { |
| 2049 | u32 mysid = current_sid(); |
| 2050 | u32 mgrsid = task_sid(mgr); |
| 2051 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2052 | return avc_has_perm(&selinux_state, |
| 2053 | mysid, mgrsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2054 | BINDER__SET_CONTEXT_MGR, NULL); |
| 2055 | } |
| 2056 | |
| 2057 | static int selinux_binder_transaction(struct task_struct *from, |
| 2058 | struct task_struct *to) |
| 2059 | { |
| 2060 | u32 mysid = current_sid(); |
| 2061 | u32 fromsid = task_sid(from); |
| 2062 | u32 tosid = task_sid(to); |
| 2063 | int rc; |
| 2064 | |
| 2065 | if (mysid != fromsid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2066 | rc = avc_has_perm(&selinux_state, |
| 2067 | mysid, fromsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2068 | BINDER__IMPERSONATE, NULL); |
| 2069 | if (rc) |
| 2070 | return rc; |
| 2071 | } |
| 2072 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2073 | return avc_has_perm(&selinux_state, |
| 2074 | fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2075 | NULL); |
| 2076 | } |
| 2077 | |
| 2078 | static int selinux_binder_transfer_binder(struct task_struct *from, |
| 2079 | struct task_struct *to) |
| 2080 | { |
| 2081 | u32 fromsid = task_sid(from); |
| 2082 | u32 tosid = task_sid(to); |
| 2083 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2084 | return avc_has_perm(&selinux_state, |
| 2085 | fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2086 | NULL); |
| 2087 | } |
| 2088 | |
| 2089 | static int selinux_binder_transfer_file(struct task_struct *from, |
| 2090 | struct task_struct *to, |
| 2091 | struct file *file) |
| 2092 | { |
| 2093 | u32 sid = task_sid(to); |
| 2094 | struct file_security_struct *fsec = file->f_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2095 | struct dentry *dentry = file->f_path.dentry; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2096 | struct inode_security_struct *isec; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2097 | struct common_audit_data ad; |
| 2098 | int rc; |
| 2099 | |
| 2100 | ad.type = LSM_AUDIT_DATA_PATH; |
| 2101 | ad.u.path = file->f_path; |
| 2102 | |
| 2103 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2104 | rc = avc_has_perm(&selinux_state, |
| 2105 | sid, fsec->sid, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2106 | SECCLASS_FD, |
| 2107 | FD__USE, |
| 2108 | &ad); |
| 2109 | if (rc) |
| 2110 | return rc; |
| 2111 | } |
| 2112 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 2113 | #ifdef CONFIG_BPF_SYSCALL |
| 2114 | rc = bpf_fd_pass(file, sid); |
| 2115 | if (rc) |
| 2116 | return rc; |
| 2117 | #endif |
| 2118 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2119 | if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2120 | return 0; |
| 2121 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2122 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2123 | return avc_has_perm(&selinux_state, |
| 2124 | sid, isec->sid, isec->sclass, file_to_av(file), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2125 | &ad); |
| 2126 | } |
| 2127 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 2128 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2129 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2131 | u32 sid = current_sid(); |
| 2132 | u32 csid = task_sid(child); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 2133 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2134 | if (mode & PTRACE_MODE_READ) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2135 | return avc_has_perm(&selinux_state, |
| 2136 | sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2137 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2138 | return avc_has_perm(&selinux_state, |
| 2139 | sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 2143 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2144 | return avc_has_perm(&selinux_state, |
| 2145 | task_sid(parent), current_sid(), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2146 | PROCESS__PTRACE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2150 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2152 | return avc_has_perm(&selinux_state, |
| 2153 | current_sid(), task_sid(target), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2154 | PROCESS__GETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2157 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 2158 | const kernel_cap_t *effective, |
| 2159 | const kernel_cap_t *inheritable, |
| 2160 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2162 | return avc_has_perm(&selinux_state, |
| 2163 | cred_sid(old), cred_sid(new), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2164 | PROCESS__SETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 2167 | /* |
| 2168 | * (This comment used to live with the selinux_task_setuid hook, |
| 2169 | * which was removed). |
| 2170 | * |
| 2171 | * Since setuid only affects the current process, and since the SELinux |
| 2172 | * controls are not based on the Linux identity attributes, SELinux does not |
| 2173 | * need to control this operation. However, SELinux does control the use of |
| 2174 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 2175 | */ |
| 2176 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2177 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
| 2178 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 2180 | return cred_has_capability(cred, cap, audit, ns == &init_user_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 2184 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2185 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | int rc = 0; |
| 2187 | |
| 2188 | if (!sb) |
| 2189 | return 0; |
| 2190 | |
| 2191 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2192 | case Q_SYNC: |
| 2193 | case Q_QUOTAON: |
| 2194 | case Q_QUOTAOFF: |
| 2195 | case Q_SETINFO: |
| 2196 | case Q_SETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2197 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2198 | break; |
| 2199 | case Q_GETFMT: |
| 2200 | case Q_GETINFO: |
| 2201 | case Q_GETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2202 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2203 | break; |
| 2204 | default: |
| 2205 | rc = 0; /* let the kernel handle invalid cmds */ |
| 2206 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | } |
| 2208 | return rc; |
| 2209 | } |
| 2210 | |
| 2211 | static int selinux_quota_on(struct dentry *dentry) |
| 2212 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2213 | const struct cred *cred = current_cred(); |
| 2214 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2215 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2218 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2221 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2222 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2223 | return avc_has_perm(&selinux_state, |
| 2224 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2225 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2226 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2227 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2228 | /* Set level of messages printed to console */ |
| 2229 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2230 | return avc_has_perm(&selinux_state, |
| 2231 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2232 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, |
| 2233 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | } |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2235 | /* All other syslog types */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2236 | return avc_has_perm(&selinux_state, |
| 2237 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2238 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | /* |
| 2242 | * Check that a process has enough memory to allocate a new virtual |
| 2243 | * mapping. 0 means there is enough memory for the allocation to |
| 2244 | * succeed and -ENOMEM implies there is not. |
| 2245 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | * Do not audit the selinux permission check, as this is applied to all |
| 2247 | * processes that allocate mappings. |
| 2248 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2249 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | { |
| 2251 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2253 | rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 2254 | SECURITY_CAP_NOAUDIT, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | if (rc == 0) |
| 2256 | cap_sys_admin = 1; |
| 2257 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2258 | return cap_sys_admin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | /* binprm security operations */ |
| 2262 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2263 | static u32 ptrace_parent_sid(void) |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2264 | { |
| 2265 | u32 sid = 0; |
| 2266 | struct task_struct *tracer; |
| 2267 | |
| 2268 | rcu_read_lock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2269 | tracer = ptrace_parent(current); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2270 | if (tracer) |
| 2271 | sid = task_sid(tracer); |
| 2272 | rcu_read_unlock(); |
| 2273 | |
| 2274 | return sid; |
| 2275 | } |
| 2276 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2277 | static int check_nnp_nosuid(const struct linux_binprm *bprm, |
| 2278 | const struct task_security_struct *old_tsec, |
| 2279 | const struct task_security_struct *new_tsec) |
| 2280 | { |
| 2281 | int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); |
Andy Lutomirski | 380cf5b | 2016-06-23 16:41:05 -0500 | [diff] [blame] | 2282 | int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2283 | int rc; |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2284 | u32 av; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2285 | |
| 2286 | if (!nnp && !nosuid) |
| 2287 | return 0; /* neither NNP nor nosuid */ |
| 2288 | |
| 2289 | if (new_tsec->sid == old_tsec->sid) |
| 2290 | return 0; /* No change in credentials */ |
| 2291 | |
| 2292 | /* |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2293 | * If the policy enables the nnp_nosuid_transition policy capability, |
| 2294 | * then we permit transitions under NNP or nosuid if the |
| 2295 | * policy allows the corresponding permission between |
| 2296 | * the old and new contexts. |
| 2297 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2298 | if (selinux_policycap_nnp_nosuid_transition()) { |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2299 | av = 0; |
| 2300 | if (nnp) |
| 2301 | av |= PROCESS2__NNP_TRANSITION; |
| 2302 | if (nosuid) |
| 2303 | av |= PROCESS2__NOSUID_TRANSITION; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2304 | rc = avc_has_perm(&selinux_state, |
| 2305 | old_tsec->sid, new_tsec->sid, |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2306 | SECCLASS_PROCESS2, av, NULL); |
| 2307 | if (!rc) |
| 2308 | return 0; |
| 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * We also permit NNP or nosuid transitions to bounded SIDs, |
| 2313 | * i.e. SIDs that are guaranteed to only be allowed a subset |
| 2314 | * of the permissions of the current SID. |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2315 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2316 | rc = security_bounded_transition(&selinux_state, old_tsec->sid, |
| 2317 | new_tsec->sid); |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2318 | if (!rc) |
| 2319 | return 0; |
| 2320 | |
| 2321 | /* |
| 2322 | * On failure, preserve the errno values for NNP vs nosuid. |
| 2323 | * NNP: Operation not permitted for caller. |
| 2324 | * nosuid: Permission denied to file. |
| 2325 | */ |
| 2326 | if (nnp) |
| 2327 | return -EPERM; |
| 2328 | return -EACCES; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2329 | } |
| 2330 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2331 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2333 | const struct task_security_struct *old_tsec; |
| 2334 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2336 | struct common_audit_data ad; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2337 | struct inode *inode = file_inode(bprm->file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | int rc; |
| 2339 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2340 | /* SELinux context only depends on initial program or script and not |
| 2341 | * the script interpreter */ |
Kees Cook | ddb4a14 | 2017-07-18 15:25:23 -0700 | [diff] [blame] | 2342 | if (bprm->called_set_creds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | return 0; |
| 2344 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2345 | old_tsec = current_security(); |
| 2346 | new_tsec = bprm->cred->security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2347 | isec = inode_security(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
| 2349 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2350 | new_tsec->sid = old_tsec->sid; |
| 2351 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2353 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2354 | new_tsec->create_sid = 0; |
| 2355 | new_tsec->keycreate_sid = 0; |
| 2356 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2358 | if (old_tsec->exec_sid) { |
| 2359 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2361 | new_tsec->exec_sid = 0; |
Andy Lutomirski | 259e5e6 | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2362 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2363 | /* Fail on NNP or nosuid if not an allowed transition. */ |
| 2364 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2365 | if (rc) |
| 2366 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | } else { |
| 2368 | /* Check for a default transition on this program. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2369 | rc = security_transition_sid(&selinux_state, old_tsec->sid, |
| 2370 | isec->sid, SECCLASS_PROCESS, NULL, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2371 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | if (rc) |
| 2373 | return rc; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2374 | |
| 2375 | /* |
| 2376 | * Fallback to old SID on NNP or nosuid if not an allowed |
| 2377 | * transition. |
| 2378 | */ |
| 2379 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2380 | if (rc) |
| 2381 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 2384 | ad.type = LSM_AUDIT_DATA_FILE; |
| 2385 | ad.u.file = bprm->file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2387 | if (new_tsec->sid == old_tsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2388 | rc = avc_has_perm(&selinux_state, |
| 2389 | old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2391 | if (rc) |
| 2392 | return rc; |
| 2393 | } else { |
| 2394 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2395 | rc = avc_has_perm(&selinux_state, |
| 2396 | old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2398 | if (rc) |
| 2399 | return rc; |
| 2400 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2401 | rc = avc_has_perm(&selinux_state, |
| 2402 | new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2404 | if (rc) |
| 2405 | return rc; |
| 2406 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2407 | /* Check for shared state */ |
| 2408 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2409 | rc = avc_has_perm(&selinux_state, |
| 2410 | old_tsec->sid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2411 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2412 | NULL); |
| 2413 | if (rc) |
| 2414 | return -EPERM; |
| 2415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2417 | /* Make sure that anyone attempting to ptrace over a task that |
| 2418 | * changes its SID has the appropriate permit */ |
Eric W. Biederman | 9227dd2 | 2017-01-23 17:26:31 +1300 | [diff] [blame] | 2419 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2420 | u32 ptsid = ptrace_parent_sid(); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2421 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2422 | rc = avc_has_perm(&selinux_state, |
| 2423 | ptsid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2424 | SECCLASS_PROCESS, |
| 2425 | PROCESS__PTRACE, NULL); |
| 2426 | if (rc) |
| 2427 | return -EPERM; |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2432 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | /* Enable secure mode for SIDs transitions unless |
| 2435 | the noatsecure permission is granted between |
| 2436 | the two SIDs, i.e. ahp returns 0. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2437 | rc = avc_has_perm(&selinux_state, |
| 2438 | old_tsec->sid, new_tsec->sid, |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2439 | SECCLASS_PROCESS, PROCESS__NOATSECURE, |
| 2440 | NULL); |
| 2441 | bprm->secureexec |= !!rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2444 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2447 | static int match_file(const void *p, struct file *file, unsigned fd) |
| 2448 | { |
| 2449 | return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; |
| 2450 | } |
| 2451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2453 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2454 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2457 | struct tty_struct *tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2458 | int drop_tty = 0; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2459 | unsigned n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2461 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | if (tty) { |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2463 | spin_lock(&tty->files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2464 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2465 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | /* Revalidate access to controlling tty. |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2468 | Use file_path_has_perm on the tty path directly |
| 2469 | rather than using file_has_perm, as this particular |
| 2470 | open file may belong to another process and we are |
| 2471 | only interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2472 | file_priv = list_first_entry(&tty->tty_files, |
| 2473 | struct tty_file_private, list); |
| 2474 | file = file_priv->file; |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2475 | if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2476 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2478 | spin_unlock(&tty->files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2479 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2481 | /* Reset controlling tty. */ |
| 2482 | if (drop_tty) |
| 2483 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | |
| 2485 | /* Revalidate access to inherited open files. */ |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2486 | n = iterate_fd(files, 0, match_file, cred); |
| 2487 | if (!n) /* none found? */ |
| 2488 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2490 | devnull = dentry_open(&selinux_null, O_RDWR, cred); |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 2491 | if (IS_ERR(devnull)) |
| 2492 | devnull = NULL; |
| 2493 | /* replace all the matching ones with this */ |
| 2494 | do { |
| 2495 | replace_fd(n - 1, devnull, 0); |
| 2496 | } while ((n = iterate_fd(files, n, match_file, cred)) != 0); |
| 2497 | if (devnull) |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2498 | fput(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2502 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2504 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2506 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | int rc, i; |
| 2509 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2510 | new_tsec = bprm->cred->security; |
| 2511 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | return; |
| 2513 | |
| 2514 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2515 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2517 | /* Always clear parent death signal on SID transitions. */ |
| 2518 | current->pdeath_signal = 0; |
| 2519 | |
| 2520 | /* Check whether the new SID can inherit resource limits from the old |
| 2521 | * SID. If not, reset all soft limits to the lower of the current |
| 2522 | * task's hard limit and the init task's soft limit. |
| 2523 | * |
| 2524 | * Note that the setting of hard limits (even to lower them) can be |
| 2525 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2526 | * soft limit into the computation is to avoid resetting soft limits |
| 2527 | * higher than the default soft limit for cases where the default is |
| 2528 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2529 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2530 | rc = avc_has_perm(&selinux_state, |
| 2531 | new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2532 | PROCESS__RLIMITINH, NULL); |
| 2533 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2534 | /* protect against do_prlimit() */ |
| 2535 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2536 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2537 | rlim = current->signal->rlim + i; |
| 2538 | initrlim = init_task.signal->rlim + i; |
| 2539 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2540 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2541 | task_unlock(current); |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2542 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) |
| 2543 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | /* |
| 2548 | * Clean up the process immediately after the installation of new credentials |
| 2549 | * due to exec |
| 2550 | */ |
| 2551 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2552 | { |
| 2553 | const struct task_security_struct *tsec = current_security(); |
| 2554 | struct itimerval itimer; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2555 | u32 osid, sid; |
| 2556 | int rc, i; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2557 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2558 | osid = tsec->osid; |
| 2559 | sid = tsec->sid; |
| 2560 | |
| 2561 | if (sid == osid) |
| 2562 | return; |
| 2563 | |
| 2564 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2565 | * If not, clear itimers to avoid subsequent signal generation and |
| 2566 | * flush and unblock signals. |
| 2567 | * |
| 2568 | * This must occur _after_ the task SID has been updated so that any |
| 2569 | * kill done after the flush will be checked against the new SID. |
| 2570 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2571 | rc = avc_has_perm(&selinux_state, |
| 2572 | osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | if (rc) { |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2574 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { |
| 2575 | memset(&itimer, 0, sizeof itimer); |
| 2576 | for (i = 0; i < 3; i++) |
| 2577 | do_setitimer(i, &itimer, NULL); |
| 2578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2580 | if (!fatal_signal_pending(current)) { |
| 2581 | flush_sigqueue(¤t->pending); |
| 2582 | flush_sigqueue(¤t->signal->shared_pending); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2583 | flush_signal_handlers(current, 1); |
| 2584 | sigemptyset(¤t->blocked); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2585 | recalc_sigpending(); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | spin_unlock_irq(¤t->sighand->siglock); |
| 2588 | } |
| 2589 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2590 | /* Wake up the parent if it is waiting so that it can recheck |
| 2591 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2592 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2593 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2594 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* superblock security operations */ |
| 2598 | |
| 2599 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2600 | { |
| 2601 | return superblock_alloc_security(sb); |
| 2602 | } |
| 2603 | |
| 2604 | static void selinux_sb_free_security(struct super_block *sb) |
| 2605 | { |
| 2606 | superblock_free_security(sb); |
| 2607 | } |
| 2608 | |
| 2609 | static inline int match_prefix(char *prefix, int plen, char *option, int olen) |
| 2610 | { |
| 2611 | if (plen > olen) |
| 2612 | return 0; |
| 2613 | |
| 2614 | return !memcmp(prefix, option, plen); |
| 2615 | } |
| 2616 | |
| 2617 | static inline int selinux_option(char *option, int len) |
| 2618 | { |
Eric Paris | 832cbd9 | 2008-04-01 13:24:09 -0400 | [diff] [blame] | 2619 | return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || |
| 2620 | match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || |
| 2621 | match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || |
David P. Quigley | 11689d4 | 2009-01-16 09:22:03 -0500 | [diff] [blame] | 2622 | match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || |
| 2623 | match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | static inline void take_option(char **to, char *from, int *first, int len) |
| 2627 | { |
| 2628 | if (!*first) { |
| 2629 | **to = ','; |
| 2630 | *to += 1; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2631 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | *first = 0; |
| 2633 | memcpy(*to, from, len); |
| 2634 | *to += len; |
| 2635 | } |
| 2636 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2637 | static inline void take_selinux_option(char **to, char *from, int *first, |
| 2638 | int len) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2639 | { |
| 2640 | int current_size = 0; |
| 2641 | |
| 2642 | if (!*first) { |
| 2643 | **to = '|'; |
| 2644 | *to += 1; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2645 | } else |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2646 | *first = 0; |
| 2647 | |
| 2648 | while (current_size < len) { |
| 2649 | if (*from != '"') { |
| 2650 | **to = *from; |
| 2651 | *to += 1; |
| 2652 | } |
| 2653 | from += 1; |
| 2654 | current_size += 1; |
| 2655 | } |
| 2656 | } |
| 2657 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 2658 | static int selinux_sb_copy_data(char *orig, char *copy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | { |
| 2660 | int fnosec, fsec, rc = 0; |
| 2661 | char *in_save, *in_curr, *in_end; |
| 2662 | char *sec_curr, *nosec_save, *nosec; |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2663 | int open_quote = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | |
| 2665 | in_curr = orig; |
| 2666 | sec_curr = copy; |
| 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | nosec = (char *)get_zeroed_page(GFP_KERNEL); |
| 2669 | if (!nosec) { |
| 2670 | rc = -ENOMEM; |
| 2671 | goto out; |
| 2672 | } |
| 2673 | |
| 2674 | nosec_save = nosec; |
| 2675 | fnosec = fsec = 1; |
| 2676 | in_save = in_end = orig; |
| 2677 | |
| 2678 | do { |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2679 | if (*in_end == '"') |
| 2680 | open_quote = !open_quote; |
| 2681 | if ((*in_end == ',' && open_quote == 0) || |
| 2682 | *in_end == '\0') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | int len = in_end - in_curr; |
| 2684 | |
| 2685 | if (selinux_option(in_curr, len)) |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2686 | take_selinux_option(&sec_curr, in_curr, &fsec, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | else |
| 2688 | take_option(&nosec, in_curr, &fnosec, len); |
| 2689 | |
| 2690 | in_curr = in_end + 1; |
| 2691 | } |
| 2692 | } while (*in_end++); |
| 2693 | |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 2694 | strcpy(in_save, nosec_save); |
Gerald Schaefer | da3caa2 | 2005-06-21 17:15:18 -0700 | [diff] [blame] | 2695 | free_page((unsigned long)nosec_save); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | out: |
| 2697 | return rc; |
| 2698 | } |
| 2699 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2700 | static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2701 | { |
| 2702 | char *s = (char *)get_zeroed_page(GFP_KERNEL); |
| 2703 | int err; |
| 2704 | |
| 2705 | if (!s) |
| 2706 | return -ENOMEM; |
| 2707 | err = selinux_sb_copy_data(options, s); |
| 2708 | if (!err) |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2709 | err = selinux_parse_opts_str(s, mnt_opts); |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2710 | free_page((unsigned long)s); |
| 2711 | return err; |
| 2712 | } |
| 2713 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2714 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2715 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2716 | struct selinux_mnt_opts *opts = mnt_opts; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2717 | struct superblock_security_struct *sbsec = sb->s_security; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2718 | u32 sid; |
| 2719 | int rc; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2720 | |
| 2721 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2722 | return 0; |
| 2723 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2724 | if (!opts) |
| 2725 | return 0; |
| 2726 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2727 | if (opts->fscontext) { |
| 2728 | rc = parse_sid(sb, opts->fscontext, &sid); |
| 2729 | if (rc) |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2730 | return rc; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2731 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2732 | goto out_bad_option; |
| 2733 | } |
| 2734 | if (opts->context) { |
| 2735 | rc = parse_sid(sb, opts->context, &sid); |
| 2736 | if (rc) |
| 2737 | return rc; |
| 2738 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2739 | goto out_bad_option; |
| 2740 | } |
| 2741 | if (opts->rootcontext) { |
| 2742 | struct inode_security_struct *root_isec; |
| 2743 | root_isec = backing_inode_security(sb->s_root); |
| 2744 | rc = parse_sid(sb, opts->rootcontext, &sid); |
| 2745 | if (rc) |
| 2746 | return rc; |
| 2747 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2748 | goto out_bad_option; |
| 2749 | } |
| 2750 | if (opts->defcontext) { |
| 2751 | rc = parse_sid(sb, opts->defcontext, &sid); |
| 2752 | if (rc) |
| 2753 | return rc; |
| 2754 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2755 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2756 | } |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2757 | return 0; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2758 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2759 | out_bad_option: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 2760 | pr_warn("SELinux: unable to change security options " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 2761 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2762 | sb->s_type->name); |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2763 | return -EINVAL; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2764 | } |
| 2765 | |
Al Viro | a10d7c2 | 2018-12-05 11:58:35 -0500 | [diff] [blame] | 2766 | static int selinux_sb_kern_mount(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2768 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2769 | struct common_audit_data ad; |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2770 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2771 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2772 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2773 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2776 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2778 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2779 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2781 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2782 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2783 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2786 | static int selinux_mount(const char *dev_name, |
Al Viro | 8a04c43 | 2016-03-25 14:52:53 -0400 | [diff] [blame] | 2787 | const struct path *path, |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2788 | const char *type, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2789 | unsigned long flags, |
| 2790 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2792 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
| 2794 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2795 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2796 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2798 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2802 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2803 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2805 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2806 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | /* inode security operations */ |
| 2810 | |
| 2811 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2812 | { |
| 2813 | return inode_alloc_security(inode); |
| 2814 | } |
| 2815 | |
| 2816 | static void selinux_inode_free_security(struct inode *inode) |
| 2817 | { |
| 2818 | inode_free_security(inode); |
| 2819 | } |
| 2820 | |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2821 | static int selinux_dentry_init_security(struct dentry *dentry, int mode, |
Al Viro | 4f3ccd7 | 2016-07-20 16:06:15 -0400 | [diff] [blame] | 2822 | const struct qstr *name, void **ctx, |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2823 | u32 *ctxlen) |
| 2824 | { |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2825 | u32 newsid; |
| 2826 | int rc; |
| 2827 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2828 | rc = selinux_determine_inode_label(current_security(), |
| 2829 | d_inode(dentry->d_parent), name, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2830 | inode_mode_to_security_class(mode), |
| 2831 | &newsid); |
| 2832 | if (rc) |
| 2833 | return rc; |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2834 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2835 | return security_sid_to_context(&selinux_state, newsid, (char **)ctx, |
| 2836 | ctxlen); |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2837 | } |
| 2838 | |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2839 | static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, |
| 2840 | struct qstr *name, |
| 2841 | const struct cred *old, |
| 2842 | struct cred *new) |
| 2843 | { |
| 2844 | u32 newsid; |
| 2845 | int rc; |
| 2846 | struct task_security_struct *tsec; |
| 2847 | |
| 2848 | rc = selinux_determine_inode_label(old->security, |
| 2849 | d_inode(dentry->d_parent), name, |
| 2850 | inode_mode_to_security_class(mode), |
| 2851 | &newsid); |
| 2852 | if (rc) |
| 2853 | return rc; |
| 2854 | |
| 2855 | tsec = new->security; |
| 2856 | tsec->create_sid = newsid; |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2860 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2861 | const struct qstr *qstr, |
| 2862 | const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2863 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2864 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2865 | const struct task_security_struct *tsec = current_security(); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2866 | struct superblock_security_struct *sbsec; |
Corentin LABBE | c0d4f46 | 2017-10-04 20:32:17 +0200 | [diff] [blame] | 2867 | u32 newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2868 | int rc; |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2869 | char *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2870 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2871 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2872 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2873 | newsid = tsec->create_sid; |
| 2874 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2875 | rc = selinux_determine_inode_label(current_security(), |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2876 | dir, qstr, |
| 2877 | inode_mode_to_security_class(inode->i_mode), |
| 2878 | &newsid); |
| 2879 | if (rc) |
| 2880 | return rc; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2881 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2882 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2883 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2884 | struct inode_security_struct *isec = inode->i_security; |
| 2885 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2886 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2887 | isec->initialized = LABEL_INITIALIZED; |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2888 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2889 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2890 | if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2891 | return -EOPNOTSUPP; |
| 2892 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2893 | if (name) |
| 2894 | *name = XATTR_SELINUX_SUFFIX; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2895 | |
| 2896 | if (value && len) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2897 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 2898 | &context, &clen); |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2899 | if (rc) |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2900 | return rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2901 | *value = context; |
| 2902 | *len = clen; |
| 2903 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2904 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2905 | return 0; |
| 2906 | } |
| 2907 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2908 | static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | { |
| 2910 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2911 | } |
| 2912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | return may_link(dir, old_dentry, MAY_LINK); |
| 2916 | } |
| 2917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2919 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | return may_link(dir, dentry, MAY_UNLINK); |
| 2921 | } |
| 2922 | |
| 2923 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2924 | { |
| 2925 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2926 | } |
| 2927 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2928 | static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | { |
| 2930 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2931 | } |
| 2932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2934 | { |
| 2935 | return may_link(dir, dentry, MAY_RMDIR); |
| 2936 | } |
| 2937 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2938 | static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2941 | } |
| 2942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2944 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | { |
| 2946 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2947 | } |
| 2948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2950 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2951 | const struct cred *cred = current_cred(); |
| 2952 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2953 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2956 | static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, |
| 2957 | bool rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2959 | const struct cred *cred = current_cred(); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2960 | struct common_audit_data ad; |
| 2961 | struct inode_security_struct *isec; |
| 2962 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2964 | validate_creds(cred); |
| 2965 | |
| 2966 | ad.type = LSM_AUDIT_DATA_DENTRY; |
| 2967 | ad.u.dentry = dentry; |
| 2968 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2969 | isec = inode_security_rcu(inode, rcu); |
| 2970 | if (IS_ERR(isec)) |
| 2971 | return PTR_ERR(isec); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2972 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2973 | return avc_has_perm_flags(&selinux_state, |
| 2974 | sid, isec->sid, isec->sclass, FILE__READ, &ad, |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2975 | rcu ? MAY_NOT_BLOCK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2978 | static noinline int audit_inode_permission(struct inode *inode, |
| 2979 | u32 perms, u32 audited, u32 denied, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 2980 | int result, |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2981 | unsigned flags) |
| 2982 | { |
| 2983 | struct common_audit_data ad; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2984 | struct inode_security_struct *isec = inode->i_security; |
| 2985 | int rc; |
| 2986 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2987 | ad.type = LSM_AUDIT_DATA_INODE; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2988 | ad.u.inode = inode; |
| 2989 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2990 | rc = slow_avc_audit(&selinux_state, |
| 2991 | current_sid(), isec->sid, isec->sclass, perms, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 2992 | audited, denied, result, &ad, flags); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2993 | if (rc) |
| 2994 | return rc; |
| 2995 | return 0; |
| 2996 | } |
| 2997 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 2998 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3000 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3001 | u32 perms; |
| 3002 | bool from_access; |
Al Viro | cf1dd1d | 2011-06-20 19:44:08 -0400 | [diff] [blame] | 3003 | unsigned flags = mask & MAY_NOT_BLOCK; |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3004 | struct inode_security_struct *isec; |
| 3005 | u32 sid; |
| 3006 | struct av_decision avd; |
| 3007 | int rc, rc2; |
| 3008 | u32 audited, denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3010 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 3011 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 3012 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3013 | /* No permission to check. Existence test. */ |
| 3014 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3017 | validate_creds(cred); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3018 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3019 | if (unlikely(IS_PRIVATE(inode))) |
| 3020 | return 0; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3021 | |
| 3022 | perms = file_mask_to_av(inode->i_mode, mask); |
| 3023 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3024 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3025 | isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); |
| 3026 | if (IS_ERR(isec)) |
| 3027 | return PTR_ERR(isec); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3028 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3029 | rc = avc_has_perm_noaudit(&selinux_state, |
| 3030 | sid, isec->sid, isec->sclass, perms, 0, &avd); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3031 | audited = avc_audit_required(perms, &avd, rc, |
| 3032 | from_access ? FILE__AUDIT_ACCESS : 0, |
| 3033 | &denied); |
| 3034 | if (likely(!audited)) |
| 3035 | return rc; |
| 3036 | |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 3037 | rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3038 | if (rc2) |
| 3039 | return rc2; |
| 3040 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 3044 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3045 | const struct cred *cred = current_cred(); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3046 | struct inode *inode = d_backing_inode(dentry); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3047 | unsigned int ia_valid = iattr->ia_valid; |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3048 | __u32 av = FILE__WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3050 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 3051 | if (ia_valid & ATTR_FORCE) { |
| 3052 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 3053 | ATTR_FORCE); |
| 3054 | if (!ia_valid) |
| 3055 | return 0; |
| 3056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3058 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 3059 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3060 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3062 | if (selinux_policycap_openperm() && |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3063 | inode->i_sb->s_magic != SOCKFS_MAGIC && |
| 3064 | (ia_valid & ATTR_SIZE) && |
| 3065 | !(ia_valid & ATTR_FILE)) |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3066 | av |= FILE__OPEN; |
| 3067 | |
| 3068 | return dentry_has_perm(cred, dentry, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3071 | static int selinux_inode_getattr(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | { |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3073 | return path_has_perm(current_cred(), path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | } |
| 3075 | |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3076 | static bool has_cap_mac_admin(bool audit) |
| 3077 | { |
| 3078 | const struct cred *cred = current_cred(); |
| 3079 | int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; |
| 3080 | |
| 3081 | if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) |
| 3082 | return false; |
| 3083 | if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) |
| 3084 | return false; |
| 3085 | return true; |
| 3086 | } |
| 3087 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3088 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 3089 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3091 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3092 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3094 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3095 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | int rc = 0; |
| 3097 | |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3098 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3099 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 3100 | if (rc) |
| 3101 | return rc; |
| 3102 | |
| 3103 | /* Not an attribute we recognize, so just check the |
| 3104 | ordinary setattr permission. */ |
| 3105 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
| 3108 | sbsec = inode->i_sb->s_security; |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 3109 | if (!(sbsec->flags & SBLABEL_MNT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | return -EOPNOTSUPP; |
| 3111 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 3112 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | return -EPERM; |
| 3114 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3115 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 3116 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3118 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3119 | rc = avc_has_perm(&selinux_state, |
| 3120 | sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | FILE__RELABELFROM, &ad); |
| 3122 | if (rc) |
| 3123 | return rc; |
| 3124 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3125 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3126 | GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3127 | if (rc == -EINVAL) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3128 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3129 | struct audit_buffer *ab; |
| 3130 | size_t audit_size; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3131 | |
| 3132 | /* We strip a nul only if it is at the end, otherwise the |
| 3133 | * context contains a nul and we should audit that */ |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3134 | if (value) { |
Colin Ian King | add2437 | 2017-10-14 13:46:55 +0100 | [diff] [blame] | 3135 | const char *str = value; |
| 3136 | |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3137 | if (str[size - 1] == '\0') |
| 3138 | audit_size = size - 1; |
| 3139 | else |
| 3140 | audit_size = size; |
| 3141 | } else { |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3142 | audit_size = 0; |
| 3143 | } |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 3144 | ab = audit_log_start(audit_context(), |
| 3145 | GFP_ATOMIC, AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3146 | audit_log_format(ab, "op=setxattr invalid_context="); |
| 3147 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 3148 | audit_log_end(ab); |
| 3149 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3150 | return rc; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3151 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3152 | rc = security_context_to_sid_force(&selinux_state, value, |
| 3153 | size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | if (rc) |
| 3156 | return rc; |
| 3157 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3158 | rc = avc_has_perm(&selinux_state, |
| 3159 | sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | FILE__RELABELTO, &ad); |
| 3161 | if (rc) |
| 3162 | return rc; |
| 3163 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3164 | rc = security_validate_transition(&selinux_state, isec->sid, newsid, |
| 3165 | sid, isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | if (rc) |
| 3167 | return rc; |
| 3168 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3169 | return avc_has_perm(&selinux_state, |
| 3170 | newsid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | sbsec->sid, |
| 3172 | SECCLASS_FILESYSTEM, |
| 3173 | FILESYSTEM__ASSOCIATE, |
| 3174 | &ad); |
| 3175 | } |
| 3176 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3177 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 3178 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3179 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3181 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3182 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | u32 newsid; |
| 3184 | int rc; |
| 3185 | |
| 3186 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3187 | /* Not an attribute we recognize, so nothing to do. */ |
| 3188 | return; |
| 3189 | } |
| 3190 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3191 | rc = security_context_to_sid_force(&selinux_state, value, size, |
| 3192 | &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 3194 | pr_err("SELinux: unable to map context to SID" |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3195 | "for (%s, %lu), rc=%d\n", |
| 3196 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | return; |
| 3198 | } |
| 3199 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3200 | isec = backing_inode_security(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3201 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3202 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3204 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3205 | spin_unlock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | return; |
| 3208 | } |
| 3209 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3210 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3212 | const struct cred *cred = current_cred(); |
| 3213 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3214 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3217 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3219 | const struct cred *cred = current_cred(); |
| 3220 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3221 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | } |
| 3223 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3224 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | { |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3226 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3227 | int rc = cap_inode_removexattr(dentry, name); |
| 3228 | if (rc) |
| 3229 | return rc; |
| 3230 | |
| 3231 | /* Not an attribute we recognize, so just check the |
| 3232 | ordinary setattr permission. */ |
| 3233 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | |
| 3236 | /* No one is allowed to remove a SELinux security label. |
| 3237 | You can change the label, but all data must be labeled. */ |
| 3238 | return -EACCES; |
| 3239 | } |
| 3240 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3241 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3242 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3243 | * |
| 3244 | * Permission check is handled by selinux_inode_getxattr hook. |
| 3245 | */ |
Andreas Gruenbacher | ea861df | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3246 | static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3248 | u32 size; |
| 3249 | int error; |
| 3250 | char *context = NULL; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3251 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 3253 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3254 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3256 | /* |
| 3257 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 3258 | * value even if it is not defined by current policy; otherwise, |
| 3259 | * use the in-core value under current policy. |
| 3260 | * Use the non-auditing forms of the permission checks since |
| 3261 | * getxattr may be called by unprivileged processes commonly |
| 3262 | * and lack of permission just means that we fall back to the |
| 3263 | * in-core context value, not a denial. |
| 3264 | */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3265 | isec = inode_security(inode); |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3266 | if (has_cap_mac_admin(false)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3267 | error = security_sid_to_context_force(&selinux_state, |
| 3268 | isec->sid, &context, |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3269 | &size); |
| 3270 | else |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3271 | error = security_sid_to_context(&selinux_state, isec->sid, |
| 3272 | &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3273 | if (error) |
| 3274 | return error; |
| 3275 | error = size; |
| 3276 | if (alloc) { |
| 3277 | *buffer = context; |
| 3278 | goto out_nofree; |
| 3279 | } |
| 3280 | kfree(context); |
| 3281 | out_nofree: |
| 3282 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3286 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | { |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 3288 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | u32 newsid; |
| 3290 | int rc; |
| 3291 | |
| 3292 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3293 | return -EOPNOTSUPP; |
| 3294 | |
| 3295 | if (!value || !size) |
| 3296 | return -EACCES; |
| 3297 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3298 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3299 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | if (rc) |
| 3301 | return rc; |
| 3302 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3303 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3304 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3306 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3307 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | return 0; |
| 3309 | } |
| 3310 | |
| 3311 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3312 | { |
| 3313 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3314 | if (buffer && len <= buffer_size) |
| 3315 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3316 | return len; |
| 3317 | } |
| 3318 | |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3319 | static void selinux_inode_getsecid(struct inode *inode, u32 *secid) |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3320 | { |
Andreas Gruenbacher | e817c2f | 2016-02-18 12:04:08 +0100 | [diff] [blame] | 3321 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3322 | *secid = isec->sid; |
| 3323 | } |
| 3324 | |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3325 | static int selinux_inode_copy_up(struct dentry *src, struct cred **new) |
| 3326 | { |
| 3327 | u32 sid; |
| 3328 | struct task_security_struct *tsec; |
| 3329 | struct cred *new_creds = *new; |
| 3330 | |
| 3331 | if (new_creds == NULL) { |
| 3332 | new_creds = prepare_creds(); |
| 3333 | if (!new_creds) |
| 3334 | return -ENOMEM; |
| 3335 | } |
| 3336 | |
| 3337 | tsec = new_creds->security; |
| 3338 | /* Get label from overlay inode and set it in create_sid */ |
| 3339 | selinux_inode_getsecid(d_inode(src), &sid); |
| 3340 | tsec->create_sid = sid; |
| 3341 | *new = new_creds; |
| 3342 | return 0; |
| 3343 | } |
| 3344 | |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 3345 | static int selinux_inode_copy_up_xattr(const char *name) |
| 3346 | { |
| 3347 | /* The copy_up hook above sets the initial context on an inode, but we |
| 3348 | * don't then want to overwrite it by blindly copying all the lower |
| 3349 | * xattrs up. Instead, we have to filter out SELinux-related xattrs. |
| 3350 | */ |
| 3351 | if (strcmp(name, XATTR_NAME_SELINUX) == 0) |
| 3352 | return 1; /* Discard */ |
| 3353 | /* |
| 3354 | * Any other attribute apart from SELINUX is not claimed, supported |
| 3355 | * by selinux. |
| 3356 | */ |
| 3357 | return -EOPNOTSUPP; |
| 3358 | } |
| 3359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | /* file security operations */ |
| 3361 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3362 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3364 | const struct cred *cred = current_cred(); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3365 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3368 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3369 | mask |= MAY_APPEND; |
| 3370 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3371 | return file_has_perm(cred, file, |
| 3372 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
| 3374 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3375 | static int selinux_file_permission(struct file *file, int mask) |
| 3376 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3377 | struct inode *inode = file_inode(file); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3378 | struct file_security_struct *fsec = file->f_security; |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3379 | struct inode_security_struct *isec; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3380 | u32 sid = current_sid(); |
| 3381 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3382 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3383 | /* No permission to check. Existence test. */ |
| 3384 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3385 | |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3386 | isec = inode_security(inode); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3387 | if (sid == fsec->sid && fsec->isid == isec->sid && |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3388 | fsec->pseqno == avc_policy_seqno(&selinux_state)) |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 3389 | /* No change since file_open check. */ |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3390 | return 0; |
| 3391 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3392 | return selinux_revalidate_file_permission(file, mask); |
| 3393 | } |
| 3394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | static int selinux_file_alloc_security(struct file *file) |
| 3396 | { |
| 3397 | return file_alloc_security(file); |
| 3398 | } |
| 3399 | |
| 3400 | static void selinux_file_free_security(struct file *file) |
| 3401 | { |
| 3402 | file_free_security(file); |
| 3403 | } |
| 3404 | |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3405 | /* |
| 3406 | * Check whether a task has the ioctl permission and cmd |
| 3407 | * operation to an inode. |
| 3408 | */ |
Geliang Tang | 1d2a168 | 2015-10-21 17:44:27 -0400 | [diff] [blame] | 3409 | static int ioctl_has_perm(const struct cred *cred, struct file *file, |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3410 | u32 requested, u16 cmd) |
| 3411 | { |
| 3412 | struct common_audit_data ad; |
| 3413 | struct file_security_struct *fsec = file->f_security; |
| 3414 | struct inode *inode = file_inode(file); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3415 | struct inode_security_struct *isec; |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3416 | struct lsm_ioctlop_audit ioctl; |
| 3417 | u32 ssid = cred_sid(cred); |
| 3418 | int rc; |
| 3419 | u8 driver = cmd >> 8; |
| 3420 | u8 xperm = cmd & 0xff; |
| 3421 | |
| 3422 | ad.type = LSM_AUDIT_DATA_IOCTL_OP; |
| 3423 | ad.u.op = &ioctl; |
| 3424 | ad.u.op->cmd = cmd; |
| 3425 | ad.u.op->path = file->f_path; |
| 3426 | |
| 3427 | if (ssid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3428 | rc = avc_has_perm(&selinux_state, |
| 3429 | ssid, fsec->sid, |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3430 | SECCLASS_FD, |
| 3431 | FD__USE, |
| 3432 | &ad); |
| 3433 | if (rc) |
| 3434 | goto out; |
| 3435 | } |
| 3436 | |
| 3437 | if (unlikely(IS_PRIVATE(inode))) |
| 3438 | return 0; |
| 3439 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3440 | isec = inode_security(inode); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3441 | rc = avc_has_extended_perms(&selinux_state, |
| 3442 | ssid, isec->sid, isec->sclass, |
| 3443 | requested, driver, xperm, &ad); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3444 | out: |
| 3445 | return rc; |
| 3446 | } |
| 3447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3449 | unsigned long arg) |
| 3450 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3451 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3452 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3454 | switch (cmd) { |
| 3455 | case FIONREAD: |
| 3456 | /* fall through */ |
| 3457 | case FIBMAP: |
| 3458 | /* fall through */ |
| 3459 | case FIGETBSZ: |
| 3460 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3461 | case FS_IOC_GETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3462 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3463 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3464 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3465 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3467 | case FS_IOC_SETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3468 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3469 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3470 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3471 | break; |
| 3472 | |
| 3473 | /* sys_ioctl() checks */ |
| 3474 | case FIONBIO: |
| 3475 | /* fall through */ |
| 3476 | case FIOASYNC: |
| 3477 | error = file_has_perm(cred, file, 0); |
| 3478 | break; |
| 3479 | |
| 3480 | case KDSKBENT: |
| 3481 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3482 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 3483 | SECURITY_CAP_AUDIT, true); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3484 | break; |
| 3485 | |
| 3486 | /* default case assumes that the command will go |
| 3487 | * to the file's ioctl() function. |
| 3488 | */ |
| 3489 | default: |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3490 | error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3491 | } |
| 3492 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | } |
| 3494 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3495 | static int default_noexec; |
| 3496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3498 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3499 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3500 | u32 sid = cred_sid(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3501 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3502 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3503 | if (default_noexec && |
Stephen Smalley | 892e8ca | 2015-07-10 09:40:59 -0400 | [diff] [blame] | 3504 | (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || |
| 3505 | (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | /* |
| 3507 | * We are making executable an anonymous mapping or a |
| 3508 | * private file mapping that will also be writable. |
| 3509 | * This has an additional check. |
| 3510 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3511 | rc = avc_has_perm(&selinux_state, |
| 3512 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3513 | PROCESS__EXECMEM, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3515 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | |
| 3518 | if (file) { |
| 3519 | /* read access is always possible with a mapping */ |
| 3520 | u32 av = FILE__READ; |
| 3521 | |
| 3522 | /* write access only matters if the mapping is shared */ |
| 3523 | if (shared && (prot & PROT_WRITE)) |
| 3524 | av |= FILE__WRITE; |
| 3525 | |
| 3526 | if (prot & PROT_EXEC) |
| 3527 | av |= FILE__EXECUTE; |
| 3528 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3529 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3531 | |
| 3532 | error: |
| 3533 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | } |
| 3535 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3536 | static int selinux_mmap_addr(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 3538 | int rc = 0; |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3539 | |
| 3540 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
| 3541 | u32 sid = current_sid(); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3542 | rc = avc_has_perm(&selinux_state, |
| 3543 | sid, sid, SECCLASS_MEMPROTECT, |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3544 | MEMPROTECT__MMAP_ZERO, NULL); |
| 3545 | } |
| 3546 | |
| 3547 | return rc; |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3550 | static int selinux_mmap_file(struct file *file, unsigned long reqprot, |
| 3551 | unsigned long prot, unsigned long flags) |
| 3552 | { |
Stephen Smalley | 3ba4bf5 | 2017-05-05 09:14:48 -0400 | [diff] [blame] | 3553 | struct common_audit_data ad; |
| 3554 | int rc; |
| 3555 | |
| 3556 | if (file) { |
| 3557 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3558 | ad.u.file = file; |
| 3559 | rc = inode_has_perm(current_cred(), file_inode(file), |
| 3560 | FILE__MAP, &ad); |
| 3561 | if (rc) |
| 3562 | return rc; |
| 3563 | } |
| 3564 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3565 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | prot = reqprot; |
| 3567 | |
| 3568 | return file_map_prot_check(file, prot, |
| 3569 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3570 | } |
| 3571 | |
| 3572 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3573 | unsigned long reqprot, |
| 3574 | unsigned long prot) |
| 3575 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3576 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3577 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3579 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | prot = reqprot; |
| 3581 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3582 | if (default_noexec && |
| 3583 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3584 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3585 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3586 | vma->vm_end <= vma->vm_mm->brk) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3587 | rc = avc_has_perm(&selinux_state, |
| 3588 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3589 | PROCESS__EXECHEAP, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3590 | } else if (!vma->vm_file && |
Stephen Smalley | c2316db | 2016-04-08 13:55:03 -0400 | [diff] [blame] | 3591 | ((vma->vm_start <= vma->vm_mm->start_stack && |
| 3592 | vma->vm_end >= vma->vm_mm->start_stack) || |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 3593 | vma_is_stack_for_current(vma))) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3594 | rc = avc_has_perm(&selinux_state, |
| 3595 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3596 | PROCESS__EXECSTACK, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3597 | } else if (vma->vm_file && vma->anon_vma) { |
| 3598 | /* |
| 3599 | * We are making executable a file mapping that has |
| 3600 | * had some COW done. Since pages might have been |
| 3601 | * written, check ability to execute the possibly |
| 3602 | * modified content. This typically should only |
| 3603 | * occur for text relocations. |
| 3604 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3605 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3606 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3607 | if (rc) |
| 3608 | return rc; |
| 3609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | |
| 3611 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3612 | } |
| 3613 | |
| 3614 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3615 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3616 | const struct cred *cred = current_cred(); |
| 3617 | |
| 3618 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3622 | unsigned long arg) |
| 3623 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3624 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | int err = 0; |
| 3626 | |
| 3627 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3628 | case F_SETFL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3629 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3630 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3632 | } |
| 3633 | /* fall through */ |
| 3634 | case F_SETOWN: |
| 3635 | case F_SETSIG: |
| 3636 | case F_GETFL: |
| 3637 | case F_GETOWN: |
| 3638 | case F_GETSIG: |
Cyrill Gorcunov | 1d151c3 | 2012-07-30 14:43:00 -0700 | [diff] [blame] | 3639 | case F_GETOWNER_UIDS: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3640 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3641 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3642 | break; |
| 3643 | case F_GETLK: |
| 3644 | case F_SETLK: |
| 3645 | case F_SETLKW: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 3646 | case F_OFD_GETLK: |
| 3647 | case F_OFD_SETLK: |
| 3648 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3650 | case F_GETLK64: |
| 3651 | case F_SETLK64: |
| 3652 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | #endif |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3654 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3655 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | return err; |
| 3659 | } |
| 3660 | |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 3661 | static void selinux_file_set_fowner(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | struct file_security_struct *fsec; |
| 3664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | fsec = file->f_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3666 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3670 | struct fown_struct *fown, int signum) |
| 3671 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3672 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3673 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | struct file_security_struct *fsec; |
| 3676 | |
| 3677 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3678 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | fsec = file->f_security; |
| 3681 | |
| 3682 | if (!signum) |
| 3683 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3684 | else |
| 3685 | perm = signal_to_av(signum); |
| 3686 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3687 | return avc_has_perm(&selinux_state, |
| 3688 | fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | SECCLASS_PROCESS, perm, NULL); |
| 3690 | } |
| 3691 | |
| 3692 | static int selinux_file_receive(struct file *file) |
| 3693 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3694 | const struct cred *cred = current_cred(); |
| 3695 | |
| 3696 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3699 | static int selinux_file_open(struct file *file) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3700 | { |
| 3701 | struct file_security_struct *fsec; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3702 | struct inode_security_struct *isec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3703 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3704 | fsec = file->f_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3705 | isec = inode_security(file_inode(file)); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3706 | /* |
| 3707 | * Save inode label and policy sequence number |
| 3708 | * at open-time so that selinux_file_permission |
| 3709 | * can determine whether revalidation is necessary. |
| 3710 | * Task label is already saved in the file security |
| 3711 | * struct as its SID. |
| 3712 | */ |
| 3713 | fsec->isid = isec->sid; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3714 | fsec->pseqno = avc_policy_seqno(&selinux_state); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3715 | /* |
| 3716 | * Since the inode label or policy seqno may have changed |
| 3717 | * between the selinux_inode_permission check and the saving |
| 3718 | * of state above, recheck that access is still permitted. |
| 3719 | * Otherwise, access might never be revalidated against the |
| 3720 | * new inode label or new policy. |
| 3721 | * This check is not redundant - do not remove. |
| 3722 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3723 | return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3724 | } |
| 3725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | /* task security operations */ |
| 3727 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 3728 | static int selinux_task_alloc(struct task_struct *task, |
| 3729 | unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3731 | u32 sid = current_sid(); |
| 3732 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3733 | return avc_has_perm(&selinux_state, |
| 3734 | sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | } |
| 3736 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3737 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3738 | * allocate the SELinux part of blank credentials |
| 3739 | */ |
| 3740 | static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 3741 | { |
| 3742 | struct task_security_struct *tsec; |
| 3743 | |
| 3744 | tsec = kzalloc(sizeof(struct task_security_struct), gfp); |
| 3745 | if (!tsec) |
| 3746 | return -ENOMEM; |
| 3747 | |
| 3748 | cred->security = tsec; |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
| 3752 | /* |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3753 | * detach and free the LSM part of a set of credentials |
| 3754 | */ |
| 3755 | static void selinux_cred_free(struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | { |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3757 | struct task_security_struct *tsec = cred->security; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3758 | |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 3759 | /* |
| 3760 | * cred->security == NULL if security_cred_alloc_blank() or |
| 3761 | * security_prepare_creds() returned an error. |
| 3762 | */ |
| 3763 | BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3764 | cred->security = (void *) 0x7UL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3765 | kfree(tsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | } |
| 3767 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3768 | /* |
| 3769 | * prepare a new set of credentials for modification |
| 3770 | */ |
| 3771 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3772 | gfp_t gfp) |
| 3773 | { |
| 3774 | const struct task_security_struct *old_tsec; |
| 3775 | struct task_security_struct *tsec; |
| 3776 | |
| 3777 | old_tsec = old->security; |
| 3778 | |
| 3779 | tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); |
| 3780 | if (!tsec) |
| 3781 | return -ENOMEM; |
| 3782 | |
| 3783 | new->security = tsec; |
| 3784 | return 0; |
| 3785 | } |
| 3786 | |
| 3787 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3788 | * transfer the SELinux data to a blank set of creds |
| 3789 | */ |
| 3790 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3791 | { |
| 3792 | const struct task_security_struct *old_tsec = old->security; |
| 3793 | struct task_security_struct *tsec = new->security; |
| 3794 | |
| 3795 | *tsec = *old_tsec; |
| 3796 | } |
| 3797 | |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 3798 | static void selinux_cred_getsecid(const struct cred *c, u32 *secid) |
| 3799 | { |
| 3800 | *secid = cred_sid(c); |
| 3801 | } |
| 3802 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3803 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3804 | * set the security data for a kernel service |
| 3805 | * - all the creation contexts are set to unlabelled |
| 3806 | */ |
| 3807 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3808 | { |
| 3809 | struct task_security_struct *tsec = new->security; |
| 3810 | u32 sid = current_sid(); |
| 3811 | int ret; |
| 3812 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3813 | ret = avc_has_perm(&selinux_state, |
| 3814 | sid, secid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3815 | SECCLASS_KERNEL_SERVICE, |
| 3816 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3817 | NULL); |
| 3818 | if (ret == 0) { |
| 3819 | tsec->sid = secid; |
| 3820 | tsec->create_sid = 0; |
| 3821 | tsec->keycreate_sid = 0; |
| 3822 | tsec->sockcreate_sid = 0; |
| 3823 | } |
| 3824 | return ret; |
| 3825 | } |
| 3826 | |
| 3827 | /* |
| 3828 | * set the file creation context in a security record to the same as the |
| 3829 | * objective context of the specified inode |
| 3830 | */ |
| 3831 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3832 | { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3833 | struct inode_security_struct *isec = inode_security(inode); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3834 | struct task_security_struct *tsec = new->security; |
| 3835 | u32 sid = current_sid(); |
| 3836 | int ret; |
| 3837 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3838 | ret = avc_has_perm(&selinux_state, |
| 3839 | sid, isec->sid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3840 | SECCLASS_KERNEL_SERVICE, |
| 3841 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3842 | NULL); |
| 3843 | |
| 3844 | if (ret == 0) |
| 3845 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3846 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3847 | } |
| 3848 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3849 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3850 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3851 | struct common_audit_data ad; |
| 3852 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3853 | ad.type = LSM_AUDIT_DATA_KMOD; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3854 | ad.u.kmod_name = kmod_name; |
| 3855 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3856 | return avc_has_perm(&selinux_state, |
| 3857 | current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3858 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3859 | } |
| 3860 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3861 | static int selinux_kernel_module_from_file(struct file *file) |
| 3862 | { |
| 3863 | struct common_audit_data ad; |
| 3864 | struct inode_security_struct *isec; |
| 3865 | struct file_security_struct *fsec; |
| 3866 | u32 sid = current_sid(); |
| 3867 | int rc; |
| 3868 | |
| 3869 | /* init_module */ |
| 3870 | if (file == NULL) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3871 | return avc_has_perm(&selinux_state, |
| 3872 | sid, sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3873 | SYSTEM__MODULE_LOAD, NULL); |
| 3874 | |
| 3875 | /* finit_module */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3876 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 3877 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3878 | ad.u.file = file; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3879 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3880 | fsec = file->f_security; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3881 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3882 | rc = avc_has_perm(&selinux_state, |
| 3883 | sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3884 | if (rc) |
| 3885 | return rc; |
| 3886 | } |
| 3887 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3888 | isec = inode_security(file_inode(file)); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3889 | return avc_has_perm(&selinux_state, |
| 3890 | sid, isec->sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3891 | SYSTEM__MODULE_LOAD, &ad); |
| 3892 | } |
| 3893 | |
| 3894 | static int selinux_kernel_read_file(struct file *file, |
| 3895 | enum kernel_read_file_id id) |
| 3896 | { |
| 3897 | int rc = 0; |
| 3898 | |
| 3899 | switch (id) { |
| 3900 | case READING_MODULE: |
| 3901 | rc = selinux_kernel_module_from_file(file); |
| 3902 | break; |
| 3903 | default: |
| 3904 | break; |
| 3905 | } |
| 3906 | |
| 3907 | return rc; |
| 3908 | } |
| 3909 | |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 3910 | static int selinux_kernel_load_data(enum kernel_load_data_id id) |
| 3911 | { |
| 3912 | int rc = 0; |
| 3913 | |
| 3914 | switch (id) { |
| 3915 | case LOADING_MODULE: |
| 3916 | rc = selinux_kernel_module_from_file(NULL); |
| 3917 | default: |
| 3918 | break; |
| 3919 | } |
| 3920 | |
| 3921 | return rc; |
| 3922 | } |
| 3923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 3925 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3926 | return avc_has_perm(&selinux_state, |
| 3927 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3928 | PROCESS__SETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | static int selinux_task_getpgid(struct task_struct *p) |
| 3932 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3933 | return avc_has_perm(&selinux_state, |
| 3934 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3935 | PROCESS__GETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | static int selinux_task_getsid(struct task_struct *p) |
| 3939 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3940 | return avc_has_perm(&selinux_state, |
| 3941 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3942 | PROCESS__GETSESSION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | } |
| 3944 | |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3945 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 3946 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3947 | *secid = task_sid(p); |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 3951 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3952 | return avc_has_perm(&selinux_state, |
| 3953 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3954 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | } |
| 3956 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3957 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 3958 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3959 | return avc_has_perm(&selinux_state, |
| 3960 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3961 | PROCESS__SETSCHED, NULL); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3962 | } |
| 3963 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3964 | static int selinux_task_getioprio(struct task_struct *p) |
| 3965 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3966 | return avc_has_perm(&selinux_state, |
| 3967 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3968 | PROCESS__GETSCHED, NULL); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3969 | } |
| 3970 | |
Corentin LABBE | 4298555 | 2017-10-04 20:32:18 +0200 | [diff] [blame] | 3971 | static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, |
| 3972 | unsigned int flags) |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3973 | { |
| 3974 | u32 av = 0; |
| 3975 | |
Stephen Smalley | 84e6885 | 2017-02-28 09:35:08 -0500 | [diff] [blame] | 3976 | if (!flags) |
| 3977 | return 0; |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3978 | if (flags & LSM_PRLIMIT_WRITE) |
| 3979 | av |= PROCESS__SETRLIMIT; |
| 3980 | if (flags & LSM_PRLIMIT_READ) |
| 3981 | av |= PROCESS__GETRLIMIT; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3982 | return avc_has_perm(&selinux_state, |
| 3983 | cred_sid(cred), cred_sid(tcred), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3984 | SECCLASS_PROCESS, av, NULL); |
| 3985 | } |
| 3986 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3987 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 3988 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3990 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | |
| 3992 | /* Control the ability to change the hard limit (whether |
| 3993 | lowering or raising it), so that the hard limit can |
| 3994 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3995 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3997 | return avc_has_perm(&selinux_state, |
| 3998 | current_sid(), task_sid(p), |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3999 | SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | |
| 4001 | return 0; |
| 4002 | } |
| 4003 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 4004 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4006 | return avc_has_perm(&selinux_state, |
| 4007 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4008 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | static int selinux_task_getscheduler(struct task_struct *p) |
| 4012 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4013 | return avc_has_perm(&selinux_state, |
| 4014 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4015 | PROCESS__GETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4018 | static int selinux_task_movememory(struct task_struct *p) |
| 4019 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4020 | return avc_has_perm(&selinux_state, |
| 4021 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4022 | PROCESS__SETSCHED, NULL); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 4025 | static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4026 | int sig, const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | { |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4028 | u32 secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | if (!sig) |
| 4032 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 4033 | else |
| 4034 | perm = signal_to_av(sig); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4035 | if (!cred) |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4036 | secid = current_sid(); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4037 | else |
| 4038 | secid = cred_sid(cred); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4039 | return avc_has_perm(&selinux_state, |
| 4040 | secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | static void selinux_task_to_inode(struct task_struct *p, |
| 4044 | struct inode *inode) |
| 4045 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | struct inode_security_struct *isec = inode->i_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4047 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4049 | spin_lock(&isec->lock); |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 4050 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4051 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4052 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4053 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4057 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4058 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | { |
| 4060 | int offset, ihlen, ret = -EINVAL; |
| 4061 | struct iphdr _iph, *ih; |
| 4062 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4063 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 4065 | if (ih == NULL) |
| 4066 | goto out; |
| 4067 | |
| 4068 | ihlen = ih->ihl * 4; |
| 4069 | if (ihlen < sizeof(_iph)) |
| 4070 | goto out; |
| 4071 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4072 | ad->u.net->v4info.saddr = ih->saddr; |
| 4073 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | ret = 0; |
| 4075 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4076 | if (proto) |
| 4077 | *proto = ih->protocol; |
| 4078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4080 | case IPPROTO_TCP: { |
| 4081 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4083 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4084 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | |
| 4086 | offset += ihlen; |
| 4087 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4088 | if (th == NULL) |
| 4089 | break; |
| 4090 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4091 | ad->u.net->sport = th->source; |
| 4092 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4096 | case IPPROTO_UDP: { |
| 4097 | struct udphdr _udph, *uh; |
| 4098 | |
| 4099 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4100 | break; |
| 4101 | |
| 4102 | offset += ihlen; |
| 4103 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4104 | if (uh == NULL) |
| 4105 | break; |
| 4106 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4107 | ad->u.net->sport = uh->source; |
| 4108 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4109 | break; |
| 4110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4112 | case IPPROTO_DCCP: { |
| 4113 | struct dccp_hdr _dccph, *dh; |
| 4114 | |
| 4115 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4116 | break; |
| 4117 | |
| 4118 | offset += ihlen; |
| 4119 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4120 | if (dh == NULL) |
| 4121 | break; |
| 4122 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4123 | ad->u.net->sport = dh->dccph_sport; |
| 4124 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4125 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4126 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4127 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4128 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4129 | case IPPROTO_SCTP: { |
| 4130 | struct sctphdr _sctph, *sh; |
| 4131 | |
| 4132 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4133 | break; |
| 4134 | |
| 4135 | offset += ihlen; |
| 4136 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4137 | if (sh == NULL) |
| 4138 | break; |
| 4139 | |
| 4140 | ad->u.net->sport = sh->source; |
| 4141 | ad->u.net->dport = sh->dest; |
| 4142 | break; |
| 4143 | } |
| 4144 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4145 | default: |
| 4146 | break; |
| 4147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | out: |
| 4149 | return ret; |
| 4150 | } |
| 4151 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4152 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | |
| 4154 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4155 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4156 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | { |
| 4158 | u8 nexthdr; |
| 4159 | int ret = -EINVAL, offset; |
| 4160 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4161 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4163 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 4165 | if (ip6 == NULL) |
| 4166 | goto out; |
| 4167 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4168 | ad->u.net->v6info.saddr = ip6->saddr; |
| 4169 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | ret = 0; |
| 4171 | |
| 4172 | nexthdr = ip6->nexthdr; |
| 4173 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4174 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | if (offset < 0) |
| 4176 | goto out; |
| 4177 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4178 | if (proto) |
| 4179 | *proto = nexthdr; |
| 4180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | switch (nexthdr) { |
| 4182 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4183 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | |
| 4185 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4186 | if (th == NULL) |
| 4187 | break; |
| 4188 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4189 | ad->u.net->sport = th->source; |
| 4190 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | break; |
| 4192 | } |
| 4193 | |
| 4194 | case IPPROTO_UDP: { |
| 4195 | struct udphdr _udph, *uh; |
| 4196 | |
| 4197 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4198 | if (uh == NULL) |
| 4199 | break; |
| 4200 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4201 | ad->u.net->sport = uh->source; |
| 4202 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | break; |
| 4204 | } |
| 4205 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4206 | case IPPROTO_DCCP: { |
| 4207 | struct dccp_hdr _dccph, *dh; |
| 4208 | |
| 4209 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4210 | if (dh == NULL) |
| 4211 | break; |
| 4212 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4213 | ad->u.net->sport = dh->dccph_sport; |
| 4214 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4215 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4216 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4217 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4218 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4219 | case IPPROTO_SCTP: { |
| 4220 | struct sctphdr _sctph, *sh; |
| 4221 | |
| 4222 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4223 | if (sh == NULL) |
| 4224 | break; |
| 4225 | |
| 4226 | ad->u.net->sport = sh->source; |
| 4227 | ad->u.net->dport = sh->dest; |
| 4228 | break; |
| 4229 | } |
| 4230 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | /* includes fragments */ |
| 4232 | default: |
| 4233 | break; |
| 4234 | } |
| 4235 | out: |
| 4236 | return ret; |
| 4237 | } |
| 4238 | |
| 4239 | #endif /* IPV6 */ |
| 4240 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4241 | static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4242 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4244 | char *addrp; |
| 4245 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4247 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4249 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4250 | if (ret) |
| 4251 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4252 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 4253 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4254 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4256 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4258 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4259 | if (ret) |
| 4260 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4261 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 4262 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4263 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | #endif /* IPV6 */ |
| 4265 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4266 | addrp = NULL; |
| 4267 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4270 | parse_error: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4271 | pr_warn( |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4272 | "SELinux: failure in selinux_parse_skb()," |
| 4273 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4275 | |
| 4276 | okay: |
| 4277 | if (_addrp) |
| 4278 | *_addrp = addrp; |
| 4279 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4282 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4283 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4284 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4285 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4286 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4287 | * |
| 4288 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4289 | * Check the various different forms of network peer labeling and determine |
| 4290 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 4291 | * the security server function security_net_peersid_cmp(). The function |
| 4292 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 4293 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 4294 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4295 | * |
| 4296 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4297 | static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4298 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4299 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4300 | u32 xfrm_sid; |
| 4301 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4302 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4303 | |
Paul Moore | 817eff7 | 2013-12-10 14:57:54 -0500 | [diff] [blame] | 4304 | err = selinux_xfrm_skb_sid(skb, &xfrm_sid); |
Paul Moore | bed4d7e | 2013-07-23 17:38:40 -0400 | [diff] [blame] | 4305 | if (unlikely(err)) |
| 4306 | return -EACCES; |
| 4307 | err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
| 4308 | if (unlikely(err)) |
| 4309 | return -EACCES; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4310 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4311 | err = security_net_peersid_resolve(&selinux_state, nlbl_sid, |
| 4312 | nlbl_type, xfrm_sid, sid); |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4313 | if (unlikely(err)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4314 | pr_warn( |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4315 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 4316 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4317 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4318 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4319 | |
| 4320 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4321 | } |
| 4322 | |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4323 | /** |
| 4324 | * selinux_conn_sid - Determine the child socket label for a connection |
| 4325 | * @sk_sid: the parent socket's SID |
| 4326 | * @skb_sid: the packet's SID |
| 4327 | * @conn_sid: the resulting connection SID |
| 4328 | * |
| 4329 | * If @skb_sid is valid then the user:role:type information from @sk_sid is |
| 4330 | * combined with the MLS information from @skb_sid in order to create |
| 4331 | * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy |
| 4332 | * of @sk_sid. Returns zero on success, negative values on failure. |
| 4333 | * |
| 4334 | */ |
| 4335 | static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) |
| 4336 | { |
| 4337 | int err = 0; |
| 4338 | |
| 4339 | if (skb_sid != SECSID_NULL) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4340 | err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, |
| 4341 | conn_sid); |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4342 | else |
| 4343 | *conn_sid = sk_sid; |
| 4344 | |
| 4345 | return err; |
| 4346 | } |
| 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4349 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4350 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 4351 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4352 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4353 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 4354 | *socksid = tsec->sockcreate_sid; |
| 4355 | return 0; |
| 4356 | } |
| 4357 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4358 | return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, |
| 4359 | secclass, NULL, socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4360 | } |
| 4361 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4362 | static int sock_has_perm(struct sock *sk, u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4364 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4365 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4366 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4368 | if (sksec->sid == SECINITSID_KERNEL) |
| 4369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4371 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4372 | ad.u.net = &net; |
| 4373 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4375 | return avc_has_perm(&selinux_state, |
| 4376 | current_sid(), sksec->sid, sksec->sclass, perms, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4377 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | static int selinux_socket_create(int family, int type, |
| 4381 | int protocol, int kern) |
| 4382 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4383 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4384 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4385 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4386 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | |
| 4388 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4389 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4391 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4392 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 4393 | if (rc) |
| 4394 | return rc; |
| 4395 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4396 | return avc_has_perm(&selinux_state, |
| 4397 | tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | } |
| 4399 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4400 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4401 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4403 | const struct task_security_struct *tsec = current_security(); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4404 | struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4405 | struct sk_security_struct *sksec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4406 | u16 sclass = socket_type_to_security_class(family, type, protocol); |
| 4407 | u32 sid = SECINITSID_KERNEL; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4408 | int err = 0; |
| 4409 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4410 | if (!kern) { |
| 4411 | err = socket_sockcreate_sid(tsec, sclass, &sid); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4412 | if (err) |
| 4413 | return err; |
| 4414 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4415 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4416 | isec->sclass = sclass; |
| 4417 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4418 | isec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4420 | if (sock->sk) { |
| 4421 | sksec = sock->sk->sk_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4422 | sksec->sclass = sclass; |
| 4423 | sksec->sid = sid; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4424 | /* Allows detection of the first association on this socket */ |
| 4425 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4426 | sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; |
| 4427 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4428 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4431 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | } |
| 4433 | |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 4434 | static int selinux_socket_socketpair(struct socket *socka, |
| 4435 | struct socket *sockb) |
| 4436 | { |
| 4437 | struct sk_security_struct *sksec_a = socka->sk->sk_security; |
| 4438 | struct sk_security_struct *sksec_b = sockb->sk->sk_security; |
| 4439 | |
| 4440 | sksec_a->peer_sid = sksec_b->sid; |
| 4441 | sksec_b->peer_sid = sksec_a->sid; |
| 4442 | |
| 4443 | return 0; |
| 4444 | } |
| 4445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | /* Range of port numbers used to automatically bind. |
| 4447 | Need to determine whether we should perform a name_bind |
| 4448 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | |
| 4450 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4451 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4452 | struct sock *sk = sock->sk; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4453 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | u16 family; |
| 4455 | int err; |
| 4456 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4457 | err = sock_has_perm(sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | if (err) |
| 4459 | goto out; |
| 4460 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4461 | /* If PF_INET or PF_INET6, check name_bind permission for the port. */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4462 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (family == PF_INET || family == PF_INET6) { |
| 4464 | char *addrp; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4465 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4466 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | struct sockaddr_in *addr4 = NULL; |
| 4468 | struct sockaddr_in6 *addr6 = NULL; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4469 | u16 family_sa = address->sa_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 4471 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4473 | /* |
| 4474 | * sctp_bindx(3) calls via selinux_sctp_bind_connect() |
| 4475 | * that validates multiple binding addresses. Because of this |
| 4476 | * need to check address->sa_family as it is possible to have |
| 4477 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4478 | */ |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4479 | switch (family_sa) { |
| 4480 | case AF_UNSPEC: |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4481 | case AF_INET: |
| 4482 | if (addrlen < sizeof(struct sockaddr_in)) |
| 4483 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | addr4 = (struct sockaddr_in *)address; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4485 | if (family_sa == AF_UNSPEC) { |
| 4486 | /* see __inet_bind(), we only want to allow |
| 4487 | * AF_UNSPEC if the address is INADDR_ANY |
| 4488 | */ |
| 4489 | if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 4490 | goto err_af; |
| 4491 | family_sa = AF_INET; |
| 4492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | addrp = (char *)&addr4->sin_addr.s_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4495 | break; |
| 4496 | case AF_INET6: |
| 4497 | if (addrlen < SIN6_LEN_RFC2133) |
| 4498 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | addr6 = (struct sockaddr_in6 *)address; |
| 4500 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4502 | break; |
| 4503 | default: |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4504 | goto err_af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4507 | ad.type = LSM_AUDIT_DATA_NET; |
| 4508 | ad.u.net = &net; |
| 4509 | ad.u.net->sport = htons(snum); |
| 4510 | ad.u.net->family = family_sa; |
| 4511 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4512 | if (snum) { |
| 4513 | int low, high; |
| 4514 | |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 4515 | inet_get_local_port_range(sock_net(sk), &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4516 | |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 4517 | if (snum < max(inet_prot_sock(sock_net(sk)), low) || |
| 4518 | snum > high) { |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4519 | err = sel_netport_sid(sk->sk_protocol, |
| 4520 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4521 | if (err) |
| 4522 | goto out; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4523 | err = avc_has_perm(&selinux_state, |
| 4524 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4525 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4526 | SOCKET__NAME_BIND, &ad); |
| 4527 | if (err) |
| 4528 | goto out; |
| 4529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4531 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4532 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4533 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4535 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4536 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4537 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4539 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4540 | |
| 4541 | case SECCLASS_DCCP_SOCKET: |
| 4542 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4543 | break; |
| 4544 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4545 | case SECCLASS_SCTP_SOCKET: |
| 4546 | node_perm = SCTP_SOCKET__NODE_BIND; |
| 4547 | break; |
| 4548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | default: |
| 4550 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4551 | break; |
| 4552 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4553 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4554 | err = sel_netnode_sid(addrp, family_sa, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | if (err) |
| 4556 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4557 | |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4558 | if (family_sa == AF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4559 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4561 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4563 | err = avc_has_perm(&selinux_state, |
| 4564 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4565 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | if (err) |
| 4567 | goto out; |
| 4568 | } |
| 4569 | out: |
| 4570 | return err; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4571 | err_af: |
| 4572 | /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ |
| 4573 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4574 | return -EINVAL; |
| 4575 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4578 | /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) |
Mauro Carvalho Chehab | 5fb94e9 | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 4579 | * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4580 | */ |
| 4581 | static int selinux_socket_connect_helper(struct socket *sock, |
| 4582 | struct sockaddr *address, int addrlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4584 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4585 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | int err; |
| 4587 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4588 | err = sock_has_perm(sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | if (err) |
| 4590 | return err; |
| 4591 | |
| 4592 | /* |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4593 | * If a TCP, DCCP or SCTP socket, check name_connect permission |
| 4594 | * for the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4596 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4597 | sksec->sclass == SECCLASS_DCCP_SOCKET || |
| 4598 | sksec->sclass == SECCLASS_SCTP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4599 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4600 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | struct sockaddr_in *addr4 = NULL; |
| 4602 | struct sockaddr_in6 *addr6 = NULL; |
| 4603 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4604 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4606 | /* sctp_connectx(3) calls via selinux_sctp_bind_connect() |
| 4607 | * that validates multiple connect addresses. Because of this |
| 4608 | * need to check address->sa_family as it is possible to have |
| 4609 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4610 | */ |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4611 | switch (address->sa_family) { |
| 4612 | case AF_INET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4614 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | return -EINVAL; |
| 4616 | snum = ntohs(addr4->sin_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4617 | break; |
| 4618 | case AF_INET6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4620 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | return -EINVAL; |
| 4622 | snum = ntohs(addr6->sin6_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4623 | break; |
| 4624 | default: |
| 4625 | /* Note that SCTP services expect -EINVAL, whereas |
| 4626 | * others expect -EAFNOSUPPORT. |
| 4627 | */ |
| 4628 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4629 | return -EINVAL; |
| 4630 | else |
| 4631 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4634 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4636 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4638 | switch (sksec->sclass) { |
| 4639 | case SECCLASS_TCP_SOCKET: |
| 4640 | perm = TCP_SOCKET__NAME_CONNECT; |
| 4641 | break; |
| 4642 | case SECCLASS_DCCP_SOCKET: |
| 4643 | perm = DCCP_SOCKET__NAME_CONNECT; |
| 4644 | break; |
| 4645 | case SECCLASS_SCTP_SOCKET: |
| 4646 | perm = SCTP_SOCKET__NAME_CONNECT; |
| 4647 | break; |
| 4648 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4649 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4650 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4651 | ad.u.net = &net; |
| 4652 | ad.u.net->dport = htons(snum); |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4653 | ad.u.net->family = address->sa_family; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4654 | err = avc_has_perm(&selinux_state, |
| 4655 | sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4657 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | } |
| 4659 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4660 | return 0; |
| 4661 | } |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4662 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4663 | /* Supports connect(2), see comments in selinux_socket_connect_helper() */ |
| 4664 | static int selinux_socket_connect(struct socket *sock, |
| 4665 | struct sockaddr *address, int addrlen) |
| 4666 | { |
| 4667 | int err; |
| 4668 | struct sock *sk = sock->sk; |
| 4669 | |
| 4670 | err = selinux_socket_connect_helper(sock, address, addrlen); |
| 4671 | if (err) |
| 4672 | return err; |
| 4673 | |
| 4674 | return selinux_netlbl_socket_connect(sk, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4678 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4679 | return sock_has_perm(sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4683 | { |
| 4684 | int err; |
| 4685 | struct inode_security_struct *isec; |
| 4686 | struct inode_security_struct *newisec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4687 | u16 sclass; |
| 4688 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4690 | err = sock_has_perm(sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | if (err) |
| 4692 | return err; |
| 4693 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4694 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4695 | spin_lock(&isec->lock); |
| 4696 | sclass = isec->sclass; |
| 4697 | sid = isec->sid; |
| 4698 | spin_unlock(&isec->lock); |
| 4699 | |
| 4700 | newisec = inode_security_novalidate(SOCK_INODE(newsock)); |
| 4701 | newisec->sclass = sclass; |
| 4702 | newisec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4703 | newisec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4709 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4711 | return sock_has_perm(sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4715 | int size, int flags) |
| 4716 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4717 | return sock_has_perm(sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | static int selinux_socket_getsockname(struct socket *sock) |
| 4721 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4722 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | static int selinux_socket_getpeername(struct socket *sock) |
| 4726 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4727 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4730 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4732 | int err; |
| 4733 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4734 | err = sock_has_perm(sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4735 | if (err) |
| 4736 | return err; |
| 4737 | |
| 4738 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4742 | int optname) |
| 4743 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4744 | return sock_has_perm(sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4748 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4749 | return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4752 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4753 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | struct sock *newsk) |
| 4755 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4756 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4757 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4758 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4759 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4760 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | int err; |
| 4762 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4763 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4764 | ad.u.net = &net; |
| 4765 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4767 | err = avc_has_perm(&selinux_state, |
| 4768 | sksec_sock->sid, sksec_other->sid, |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4769 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4771 | if (err) |
| 4772 | return err; |
| 4773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4775 | sksec_new->peer_sid = sksec_sock->sid; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4776 | err = security_sid_mls_copy(&selinux_state, sksec_other->sid, |
| 4777 | sksec_sock->sid, &sksec_new->sid); |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4778 | if (err) |
| 4779 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4780 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4781 | /* connecting socket */ |
| 4782 | sksec_sock->peer_sid = sksec_new->sid; |
| 4783 | |
| 4784 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4788 | struct socket *other) |
| 4789 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4790 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4791 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4792 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4793 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4795 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4796 | ad.u.net = &net; |
| 4797 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4799 | return avc_has_perm(&selinux_state, |
| 4800 | ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4801 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4804 | static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, |
| 4805 | char *addrp, u16 family, u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4806 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4807 | { |
| 4808 | int err; |
| 4809 | u32 if_sid; |
| 4810 | u32 node_sid; |
| 4811 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4812 | err = sel_netif_sid(ns, ifindex, &if_sid); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4813 | if (err) |
| 4814 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4815 | err = avc_has_perm(&selinux_state, |
| 4816 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4817 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4818 | if (err) |
| 4819 | return err; |
| 4820 | |
| 4821 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4822 | if (err) |
| 4823 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4824 | return avc_has_perm(&selinux_state, |
| 4825 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4826 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4827 | } |
| 4828 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4829 | static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4830 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4831 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4832 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4833 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4834 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4835 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4836 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4837 | char *addrp; |
| 4838 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4839 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4840 | ad.u.net = &net; |
| 4841 | ad.u.net->netif = skb->skb_iif; |
| 4842 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4843 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4844 | if (err) |
| 4845 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4846 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4847 | if (selinux_secmark_enabled()) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4848 | err = avc_has_perm(&selinux_state, |
| 4849 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4850 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4851 | if (err) |
| 4852 | return err; |
| 4853 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4854 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4855 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4856 | if (err) |
| 4857 | return err; |
| 4858 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4859 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4860 | return err; |
| 4861 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4862 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4863 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4864 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4865 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4866 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4867 | u16 family = sk->sk_family; |
| 4868 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4869 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4870 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4871 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4872 | u8 secmark_active; |
| 4873 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4874 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4875 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4876 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4877 | |
| 4878 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 4879 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4880 | family = PF_INET; |
| 4881 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4882 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4883 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 4884 | * special handling. We do this in an attempt to keep this function |
| 4885 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4886 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4887 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 4888 | |
| 4889 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 4890 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4891 | if (!secmark_active && !peerlbl_active) |
| 4892 | return 0; |
| 4893 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4894 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4895 | ad.u.net = &net; |
| 4896 | ad.u.net->netif = skb->skb_iif; |
| 4897 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4898 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4899 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4900 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4901 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4902 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4903 | u32 peer_sid; |
| 4904 | |
| 4905 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 4906 | if (err) |
| 4907 | return err; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4908 | err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, |
| 4909 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4910 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4911 | selinux_netlbl_err(skb, family, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4912 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4913 | } |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4914 | err = avc_has_perm(&selinux_state, |
| 4915 | sk_sid, peer_sid, SECCLASS_PEER, |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4916 | PEER__RECV, &ad); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4917 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4918 | selinux_netlbl_err(skb, family, err, 0); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4919 | return err; |
| 4920 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4921 | } |
| 4922 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4923 | if (secmark_active) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4924 | err = avc_has_perm(&selinux_state, |
| 4925 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4926 | PACKET__RECV, &ad); |
| 4927 | if (err) |
| 4928 | return err; |
| 4929 | } |
| 4930 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4931 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | } |
| 4933 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4934 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 4935 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | { |
| 4937 | int err = 0; |
| 4938 | char *scontext; |
| 4939 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4940 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4941 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4943 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4944 | sksec->sclass == SECCLASS_TCP_SOCKET || |
| 4945 | sksec->sclass == SECCLASS_SCTP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4946 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4947 | if (peer_sid == SECSID_NULL) |
| 4948 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4950 | err = security_sid_to_context(&selinux_state, peer_sid, &scontext, |
| 4951 | &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4953 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | |
| 4955 | if (scontext_len > len) { |
| 4956 | err = -ERANGE; |
| 4957 | goto out_len; |
| 4958 | } |
| 4959 | |
| 4960 | if (copy_to_user(optval, scontext, scontext_len)) |
| 4961 | err = -EFAULT; |
| 4962 | |
| 4963 | out_len: |
| 4964 | if (put_user(scontext_len, optlen)) |
| 4965 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | return err; |
| 4968 | } |
| 4969 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4970 | static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4971 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4972 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4973 | u16 family; |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 4974 | struct inode_security_struct *isec; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 4975 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4976 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 4977 | family = PF_INET; |
| 4978 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 4979 | family = PF_INET6; |
| 4980 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4981 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4982 | else |
| 4983 | goto out; |
| 4984 | |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 4985 | if (sock && family == PF_UNIX) { |
| 4986 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
| 4987 | peer_secid = isec->sid; |
| 4988 | } else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4989 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4990 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4991 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4992 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4993 | if (peer_secid == SECSID_NULL) |
| 4994 | return -EINVAL; |
| 4995 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4996 | } |
| 4997 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 4998 | static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5000 | struct sk_security_struct *sksec; |
| 5001 | |
| 5002 | sksec = kzalloc(sizeof(*sksec), priority); |
| 5003 | if (!sksec) |
| 5004 | return -ENOMEM; |
| 5005 | |
| 5006 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 5007 | sksec->sid = SECINITSID_UNLABELED; |
Stephen Smalley | 5dee25d | 2015-07-10 17:19:57 -0400 | [diff] [blame] | 5008 | sksec->sclass = SECCLASS_SOCKET; |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5009 | selinux_netlbl_sk_security_reset(sksec); |
| 5010 | sk->sk_security = sksec; |
| 5011 | |
| 5012 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | static void selinux_sk_free_security(struct sock *sk) |
| 5016 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5017 | struct sk_security_struct *sksec = sk->sk_security; |
| 5018 | |
| 5019 | sk->sk_security = NULL; |
| 5020 | selinux_netlbl_sk_security_free(sksec); |
| 5021 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | } |
| 5023 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5024 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 5025 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5026 | struct sk_security_struct *sksec = sk->sk_security; |
| 5027 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5028 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5029 | newsksec->sid = sksec->sid; |
| 5030 | newsksec->peer_sid = sksec->peer_sid; |
| 5031 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5032 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5033 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5034 | } |
| 5035 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5036 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5037 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5038 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5039 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5040 | else { |
| 5041 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5042 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5043 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5044 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5045 | } |
| 5046 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5047 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5048 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 5049 | struct inode_security_struct *isec = |
| 5050 | inode_security_novalidate(SOCK_INODE(parent)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5051 | struct sk_security_struct *sksec = sk->sk_security; |
| 5052 | |
Paul Moore | 2873ead | 2014-07-28 10:42:48 -0400 | [diff] [blame] | 5053 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 5054 | sk->sk_family == PF_UNIX) |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 5055 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5056 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5057 | } |
| 5058 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5059 | /* Called whenever SCTP receives an INIT chunk. This happens when an incoming |
| 5060 | * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association |
| 5061 | * already present). |
| 5062 | */ |
| 5063 | static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, |
| 5064 | struct sk_buff *skb) |
| 5065 | { |
| 5066 | struct sk_security_struct *sksec = ep->base.sk->sk_security; |
| 5067 | struct common_audit_data ad; |
| 5068 | struct lsm_network_audit net = {0,}; |
| 5069 | u8 peerlbl_active; |
| 5070 | u32 peer_sid = SECINITSID_UNLABELED; |
| 5071 | u32 conn_sid; |
| 5072 | int err = 0; |
| 5073 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5074 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5075 | return 0; |
| 5076 | |
| 5077 | peerlbl_active = selinux_peerlbl_enabled(); |
| 5078 | |
| 5079 | if (peerlbl_active) { |
| 5080 | /* This will return peer_sid = SECSID_NULL if there are |
| 5081 | * no peer labels, see security_net_peersid_resolve(). |
| 5082 | */ |
| 5083 | err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, |
| 5084 | &peer_sid); |
| 5085 | if (err) |
| 5086 | return err; |
| 5087 | |
| 5088 | if (peer_sid == SECSID_NULL) |
| 5089 | peer_sid = SECINITSID_UNLABELED; |
| 5090 | } |
| 5091 | |
| 5092 | if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { |
| 5093 | sksec->sctp_assoc_state = SCTP_ASSOC_SET; |
| 5094 | |
| 5095 | /* Here as first association on socket. As the peer SID |
| 5096 | * was allowed by peer recv (and the netif/node checks), |
| 5097 | * then it is approved by policy and used as the primary |
| 5098 | * peer SID for getpeercon(3). |
| 5099 | */ |
| 5100 | sksec->peer_sid = peer_sid; |
| 5101 | } else if (sksec->peer_sid != peer_sid) { |
| 5102 | /* Other association peer SIDs are checked to enforce |
| 5103 | * consistency among the peer SIDs. |
| 5104 | */ |
| 5105 | ad.type = LSM_AUDIT_DATA_NET; |
| 5106 | ad.u.net = &net; |
| 5107 | ad.u.net->sk = ep->base.sk; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5108 | err = avc_has_perm(&selinux_state, |
| 5109 | sksec->peer_sid, peer_sid, sksec->sclass, |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5110 | SCTP_SOCKET__ASSOCIATION, &ad); |
| 5111 | if (err) |
| 5112 | return err; |
| 5113 | } |
| 5114 | |
| 5115 | /* Compute the MLS component for the connection and store |
| 5116 | * the information in ep. This will be used by SCTP TCP type |
| 5117 | * sockets and peeled off connections as they cause a new |
| 5118 | * socket to be generated. selinux_sctp_sk_clone() will then |
| 5119 | * plug this into the new socket. |
| 5120 | */ |
| 5121 | err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); |
| 5122 | if (err) |
| 5123 | return err; |
| 5124 | |
| 5125 | ep->secid = conn_sid; |
| 5126 | ep->peer_secid = peer_sid; |
| 5127 | |
| 5128 | /* Set any NetLabel labels including CIPSO/CALIPSO options. */ |
| 5129 | return selinux_netlbl_sctp_assoc_request(ep, skb); |
| 5130 | } |
| 5131 | |
| 5132 | /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting |
| 5133 | * based on their @optname. |
| 5134 | */ |
| 5135 | static int selinux_sctp_bind_connect(struct sock *sk, int optname, |
| 5136 | struct sockaddr *address, |
| 5137 | int addrlen) |
| 5138 | { |
| 5139 | int len, err = 0, walk_size = 0; |
| 5140 | void *addr_buf; |
| 5141 | struct sockaddr *addr; |
| 5142 | struct socket *sock; |
| 5143 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5144 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5145 | return 0; |
| 5146 | |
| 5147 | /* Process one or more addresses that may be IPv4 or IPv6 */ |
| 5148 | sock = sk->sk_socket; |
| 5149 | addr_buf = address; |
| 5150 | |
| 5151 | while (walk_size < addrlen) { |
| 5152 | addr = addr_buf; |
| 5153 | switch (addr->sa_family) { |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5154 | case AF_UNSPEC: |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5155 | case AF_INET: |
| 5156 | len = sizeof(struct sockaddr_in); |
| 5157 | break; |
| 5158 | case AF_INET6: |
| 5159 | len = sizeof(struct sockaddr_in6); |
| 5160 | break; |
| 5161 | default: |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5162 | return -EINVAL; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | err = -EINVAL; |
| 5166 | switch (optname) { |
| 5167 | /* Bind checks */ |
| 5168 | case SCTP_PRIMARY_ADDR: |
| 5169 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 5170 | case SCTP_SOCKOPT_BINDX_ADD: |
| 5171 | err = selinux_socket_bind(sock, addr, len); |
| 5172 | break; |
| 5173 | /* Connect checks */ |
| 5174 | case SCTP_SOCKOPT_CONNECTX: |
| 5175 | case SCTP_PARAM_SET_PRIMARY: |
| 5176 | case SCTP_PARAM_ADD_IP: |
| 5177 | case SCTP_SENDMSG_CONNECT: |
| 5178 | err = selinux_socket_connect_helper(sock, addr, len); |
| 5179 | if (err) |
| 5180 | return err; |
| 5181 | |
| 5182 | /* As selinux_sctp_bind_connect() is called by the |
| 5183 | * SCTP protocol layer, the socket is already locked, |
| 5184 | * therefore selinux_netlbl_socket_connect_locked() is |
| 5185 | * is called here. The situations handled are: |
| 5186 | * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), |
| 5187 | * whenever a new IP address is added or when a new |
| 5188 | * primary address is selected. |
| 5189 | * Note that an SCTP connect(2) call happens before |
| 5190 | * the SCTP protocol layer and is handled via |
| 5191 | * selinux_socket_connect(). |
| 5192 | */ |
| 5193 | err = selinux_netlbl_socket_connect_locked(sk, addr); |
| 5194 | break; |
| 5195 | } |
| 5196 | |
| 5197 | if (err) |
| 5198 | return err; |
| 5199 | |
| 5200 | addr_buf += len; |
| 5201 | walk_size += len; |
| 5202 | } |
| 5203 | |
| 5204 | return 0; |
| 5205 | } |
| 5206 | |
| 5207 | /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ |
| 5208 | static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, |
| 5209 | struct sock *newsk) |
| 5210 | { |
| 5211 | struct sk_security_struct *sksec = sk->sk_security; |
| 5212 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5213 | |
| 5214 | /* If policy does not support SECCLASS_SCTP_SOCKET then call |
| 5215 | * the non-sctp clone version. |
| 5216 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5217 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5218 | return selinux_sk_clone_security(sk, newsk); |
| 5219 | |
| 5220 | newsksec->sid = ep->secid; |
| 5221 | newsksec->peer_sid = ep->peer_secid; |
| 5222 | newsksec->sclass = sksec->sclass; |
| 5223 | selinux_netlbl_sctp_sk_clone(sk, newsk); |
| 5224 | } |
| 5225 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5226 | static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 5227 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5228 | { |
| 5229 | struct sk_security_struct *sksec = sk->sk_security; |
| 5230 | int err; |
Paul Moore | 0b1f24e | 2013-12-03 11:39:13 -0500 | [diff] [blame] | 5231 | u16 family = req->rsk_ops->family; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5232 | u32 connsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5233 | u32 peersid; |
| 5234 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5235 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5236 | if (err) |
| 5237 | return err; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5238 | err = selinux_conn_sid(sksec->sid, peersid, &connsid); |
| 5239 | if (err) |
| 5240 | return err; |
| 5241 | req->secid = connsid; |
| 5242 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 5243 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5244 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5247 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 5248 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5249 | { |
| 5250 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5251 | |
| 5252 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5253 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5254 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 5255 | new socket in sync, but we don't have the isec available yet. |
| 5256 | So we will wait until sock_graft to do it, by which |
| 5257 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5258 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 5259 | /* We don't need to take any sort of lock here as we are the only |
| 5260 | * thread with access to newsksec */ |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5261 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 5264 | static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5265 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5266 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5267 | struct sk_security_struct *sksec = sk->sk_security; |
| 5268 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5269 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 5270 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 5271 | family = PF_INET; |
| 5272 | |
| 5273 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5274 | } |
| 5275 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5276 | static int selinux_secmark_relabel_packet(u32 sid) |
| 5277 | { |
| 5278 | const struct task_security_struct *__tsec; |
| 5279 | u32 tsid; |
| 5280 | |
| 5281 | __tsec = current_security(); |
| 5282 | tsid = __tsec->sid; |
| 5283 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5284 | return avc_has_perm(&selinux_state, |
| 5285 | tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, |
| 5286 | NULL); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | static void selinux_secmark_refcount_inc(void) |
| 5290 | { |
| 5291 | atomic_inc(&selinux_secmark_refcount); |
| 5292 | } |
| 5293 | |
| 5294 | static void selinux_secmark_refcount_dec(void) |
| 5295 | { |
| 5296 | atomic_dec(&selinux_secmark_refcount); |
| 5297 | } |
| 5298 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5299 | static void selinux_req_classify_flow(const struct request_sock *req, |
| 5300 | struct flowi *fl) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5301 | { |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 5302 | fl->flowi_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5303 | } |
| 5304 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5305 | static int selinux_tun_dev_alloc_security(void **security) |
| 5306 | { |
| 5307 | struct tun_security_struct *tunsec; |
| 5308 | |
| 5309 | tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); |
| 5310 | if (!tunsec) |
| 5311 | return -ENOMEM; |
| 5312 | tunsec->sid = current_sid(); |
| 5313 | |
| 5314 | *security = tunsec; |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | static void selinux_tun_dev_free_security(void *security) |
| 5319 | { |
| 5320 | kfree(security); |
| 5321 | } |
| 5322 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5323 | static int selinux_tun_dev_create(void) |
| 5324 | { |
| 5325 | u32 sid = current_sid(); |
| 5326 | |
| 5327 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 5328 | * that is being created here is not a socket in the traditional sense, |
| 5329 | * instead it is a private sock, accessible only to the kernel, and |
| 5330 | * representing a wide range of network traffic spanning multiple |
| 5331 | * connections unlike traditional sockets - check the TUN driver to |
| 5332 | * get a better understanding of why this socket is special */ |
| 5333 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5334 | return avc_has_perm(&selinux_state, |
| 5335 | sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5336 | NULL); |
| 5337 | } |
| 5338 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5339 | static int selinux_tun_dev_attach_queue(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5340 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5341 | struct tun_security_struct *tunsec = security; |
| 5342 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5343 | return avc_has_perm(&selinux_state, |
| 5344 | current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5345 | TUN_SOCKET__ATTACH_QUEUE, NULL); |
| 5346 | } |
| 5347 | |
| 5348 | static int selinux_tun_dev_attach(struct sock *sk, void *security) |
| 5349 | { |
| 5350 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5351 | struct sk_security_struct *sksec = sk->sk_security; |
| 5352 | |
| 5353 | /* we don't currently perform any NetLabel based labeling here and it |
| 5354 | * isn't clear that we would want to do so anyway; while we could apply |
| 5355 | * labeling without the support of the TUN user the resulting labeled |
| 5356 | * traffic from the other end of the connection would almost certainly |
| 5357 | * cause confusion to the TUN user that had no idea network labeling |
| 5358 | * protocols were being used */ |
| 5359 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5360 | sksec->sid = tunsec->sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5361 | sksec->sclass = SECCLASS_TUN_SOCKET; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5362 | |
| 5363 | return 0; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5364 | } |
| 5365 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5366 | static int selinux_tun_dev_open(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5367 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5368 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5369 | u32 sid = current_sid(); |
| 5370 | int err; |
| 5371 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5372 | err = avc_has_perm(&selinux_state, |
| 5373 | sid, tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5374 | TUN_SOCKET__RELABELFROM, NULL); |
| 5375 | if (err) |
| 5376 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5377 | err = avc_has_perm(&selinux_state, |
| 5378 | sid, sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5379 | TUN_SOCKET__RELABELTO, NULL); |
| 5380 | if (err) |
| 5381 | return err; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5382 | tunsec->sid = sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5383 | |
| 5384 | return 0; |
| 5385 | } |
| 5386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) |
| 5388 | { |
| 5389 | int err = 0; |
| 5390 | u32 perm; |
| 5391 | struct nlmsghdr *nlh; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5392 | struct sk_security_struct *sksec = sk->sk_security; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5393 | |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 5394 | if (skb->len < NLMSG_HDRLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | err = -EINVAL; |
| 5396 | goto out; |
| 5397 | } |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 5398 | nlh = nlmsg_hdr(skb); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5399 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5400 | err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | if (err) { |
| 5402 | if (err == -EINVAL) { |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5403 | pr_warn_ratelimited("SELinux: unrecognized netlink" |
| 5404 | " message: protocol=%hu nlmsg_type=%hu sclass=%s" |
| 5405 | " pig=%d comm=%s\n", |
Marek Milkovic | cded3ff | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 5406 | sk->sk_protocol, nlh->nlmsg_type, |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5407 | secclass_map[sksec->sclass - 1].name, |
| 5408 | task_pid_nr(current), current->comm); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 5409 | if (!enforcing_enabled(&selinux_state) || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5410 | security_get_allow_unknown(&selinux_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | err = 0; |
| 5412 | } |
| 5413 | |
| 5414 | /* Ignore */ |
| 5415 | if (err == -ENOENT) |
| 5416 | err = 0; |
| 5417 | goto out; |
| 5418 | } |
| 5419 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5420 | err = sock_has_perm(sk, perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | out: |
| 5422 | return err; |
| 5423 | } |
| 5424 | |
| 5425 | #ifdef CONFIG_NETFILTER |
| 5426 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5427 | static unsigned int selinux_ip_forward(struct sk_buff *skb, |
| 5428 | const struct net_device *indev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5429 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5431 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5432 | char *addrp; |
| 5433 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5434 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5435 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5436 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5437 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5438 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5439 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5440 | if (!selinux_policycap_netpeer()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5441 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5442 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5443 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5444 | netlbl_active = netlbl_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5445 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5446 | if (!secmark_active && !peerlbl_active) |
| 5447 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5448 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5449 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 5450 | return NF_DROP; |
| 5451 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5452 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5453 | ad.u.net = &net; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5454 | ad.u.net->netif = indev->ifindex; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5455 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5456 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 5457 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5459 | if (peerlbl_active) { |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5460 | err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, |
| 5461 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5462 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5463 | selinux_netlbl_err(skb, family, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5464 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5465 | } |
| 5466 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5467 | |
| 5468 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5469 | if (avc_has_perm(&selinux_state, |
| 5470 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5471 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 5472 | return NF_DROP; |
| 5473 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5474 | if (netlbl_active) |
| 5475 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 5476 | * path because we want to make sure we apply the necessary |
| 5477 | * labeling before IPsec is applied so we can leverage AH |
| 5478 | * protection */ |
| 5479 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 5480 | return NF_DROP; |
| 5481 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5482 | return NF_ACCEPT; |
| 5483 | } |
| 5484 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5485 | static unsigned int selinux_ipv4_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5486 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5487 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5488 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5489 | return selinux_ip_forward(skb, state->in, PF_INET); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5490 | } |
| 5491 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5492 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5493 | static unsigned int selinux_ipv6_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5494 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5495 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5496 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5497 | return selinux_ip_forward(skb, state->in, PF_INET6); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5498 | } |
| 5499 | #endif /* IPV6 */ |
| 5500 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5501 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 5502 | u16 family) |
| 5503 | { |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5504 | struct sock *sk; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5505 | u32 sid; |
| 5506 | |
| 5507 | if (!netlbl_enabled()) |
| 5508 | return NF_ACCEPT; |
| 5509 | |
| 5510 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 5511 | * because we want to make sure we apply the necessary labeling |
| 5512 | * before IPsec is applied so we can leverage AH protection */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5513 | sk = skb->sk; |
| 5514 | if (sk) { |
| 5515 | struct sk_security_struct *sksec; |
| 5516 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5517 | if (sk_listener(sk)) |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5518 | /* if the socket is the listening state then this |
| 5519 | * packet is a SYN-ACK packet which means it needs to |
| 5520 | * be labeled based on the connection/request_sock and |
| 5521 | * not the parent socket. unfortunately, we can't |
| 5522 | * lookup the request_sock yet as it isn't queued on |
| 5523 | * the parent socket until after the SYN-ACK is sent. |
| 5524 | * the "solution" is to simply pass the packet as-is |
| 5525 | * as any IP option based labeling should be copied |
| 5526 | * from the initial connection request (in the IP |
| 5527 | * layer). it is far from ideal, but until we get a |
| 5528 | * security label in the packet itself this is the |
| 5529 | * best we can do. */ |
| 5530 | return NF_ACCEPT; |
| 5531 | |
| 5532 | /* standard practice, label using the parent socket */ |
| 5533 | sksec = sk->sk_security; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5534 | sid = sksec->sid; |
| 5535 | } else |
| 5536 | sid = SECINITSID_KERNEL; |
| 5537 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 5538 | return NF_DROP; |
| 5539 | |
| 5540 | return NF_ACCEPT; |
| 5541 | } |
| 5542 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5543 | static unsigned int selinux_ipv4_output(void *priv, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5544 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5545 | const struct nf_hook_state *state) |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5546 | { |
| 5547 | return selinux_ip_output(skb, PF_INET); |
| 5548 | } |
| 5549 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5550 | #if IS_ENABLED(CONFIG_IPV6) |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 5551 | static unsigned int selinux_ipv6_output(void *priv, |
| 5552 | struct sk_buff *skb, |
| 5553 | const struct nf_hook_state *state) |
| 5554 | { |
| 5555 | return selinux_ip_output(skb, PF_INET6); |
| 5556 | } |
| 5557 | #endif /* IPV6 */ |
| 5558 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5559 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 5560 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5561 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5562 | { |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5563 | struct sock *sk = skb_to_full_sk(skb); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5564 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5565 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5566 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5567 | char *addrp; |
| 5568 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5569 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5570 | if (sk == NULL) |
| 5571 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5572 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5573 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5574 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5575 | ad.u.net = &net; |
| 5576 | ad.u.net->netif = ifindex; |
| 5577 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5578 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 5579 | return NF_DROP; |
| 5580 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 5581 | if (selinux_secmark_enabled()) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5582 | if (avc_has_perm(&selinux_state, |
| 5583 | sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5584 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 5585 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5586 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 5587 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 5588 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5589 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5590 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } |
| 5592 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5593 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, |
| 5594 | const struct net_device *outdev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5595 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5597 | u32 secmark_perm; |
| 5598 | u32 peer_sid; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5599 | int ifindex = outdev->ifindex; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5600 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5601 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5602 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5603 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5604 | u8 secmark_active; |
| 5605 | u8 peerlbl_active; |
| 5606 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5607 | /* If any sort of compatibility mode is enabled then handoff processing |
| 5608 | * to the selinux_ip_postroute_compat() function to deal with the |
| 5609 | * special handling. We do this in an attempt to keep this function |
| 5610 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5611 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5612 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5613 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5614 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5615 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5616 | if (!secmark_active && !peerlbl_active) |
| 5617 | return NF_ACCEPT; |
| 5618 | |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5619 | sk = skb_to_full_sk(skb); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5620 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5621 | #ifdef CONFIG_XFRM |
| 5622 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 5623 | * packet transformation so allow the packet to pass without any checks |
| 5624 | * since we'll have another chance to perform access control checks |
| 5625 | * when the packet is on it's final way out. |
| 5626 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5627 | * is NULL, in this case go ahead and apply access control. |
| 5628 | * NOTE: if this is a local socket (skb->sk != NULL) that is in the |
| 5629 | * TCP listening state we cannot wait until the XFRM processing |
| 5630 | * is done as we will miss out on the SA label if we do; |
| 5631 | * unfortunately, this means more work, but it is only once per |
| 5632 | * connection. */ |
| 5633 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5634 | !(sk && sk_listener(sk))) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5635 | return NF_ACCEPT; |
| 5636 | #endif |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5637 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5638 | if (sk == NULL) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5639 | /* Without an associated socket the packet is either coming |
| 5640 | * from the kernel or it is being forwarded; check the packet |
| 5641 | * to determine which and if the packet is being forwarded |
| 5642 | * query the packet directly to determine the security label. */ |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5643 | if (skb->skb_iif) { |
| 5644 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5645 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5646 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5647 | } else { |
| 5648 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5649 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5650 | } |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5651 | } else if (sk_listener(sk)) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5652 | /* Locally generated packet but the associated socket is in the |
| 5653 | * listening state which means this is a SYN-ACK packet. In |
| 5654 | * this particular case the correct security label is assigned |
| 5655 | * to the connection/request_sock but unfortunately we can't |
| 5656 | * query the request_sock as it isn't queued on the parent |
| 5657 | * socket until after the SYN-ACK packet is sent; the only |
| 5658 | * viable choice is to regenerate the label like we do in |
| 5659 | * selinux_inet_conn_request(). See also selinux_ip_output() |
| 5660 | * for similar problems. */ |
| 5661 | u32 skb_sid; |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5662 | struct sk_security_struct *sksec; |
| 5663 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5664 | sksec = sk->sk_security; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5665 | if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) |
| 5666 | return NF_DROP; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5667 | /* At this point, if the returned skb peerlbl is SECSID_NULL |
| 5668 | * and the packet has been through at least one XFRM |
| 5669 | * transformation then we must be dealing with the "final" |
| 5670 | * form of labeled IPsec packet; since we've already applied |
| 5671 | * all of our access controls on this packet we can safely |
| 5672 | * pass the packet. */ |
| 5673 | if (skb_sid == SECSID_NULL) { |
| 5674 | switch (family) { |
| 5675 | case PF_INET: |
| 5676 | if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 5677 | return NF_ACCEPT; |
| 5678 | break; |
| 5679 | case PF_INET6: |
| 5680 | if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) |
| 5681 | return NF_ACCEPT; |
Paul Moore | a7a91a1 | 2014-09-03 10:51:59 -0400 | [diff] [blame] | 5682 | break; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5683 | default: |
| 5684 | return NF_DROP_ERR(-ECONNREFUSED); |
| 5685 | } |
| 5686 | } |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5687 | if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) |
| 5688 | return NF_DROP; |
| 5689 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5690 | } else { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5691 | /* Locally generated packet, fetch the security label from the |
| 5692 | * associated socket. */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5693 | struct sk_security_struct *sksec = sk->sk_security; |
| 5694 | peer_sid = sksec->sid; |
| 5695 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5696 | } |
| 5697 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5698 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5699 | ad.u.net = &net; |
| 5700 | ad.u.net->netif = ifindex; |
| 5701 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5702 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5703 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5704 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5705 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5706 | if (avc_has_perm(&selinux_state, |
| 5707 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5708 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5709 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5710 | |
| 5711 | if (peerlbl_active) { |
| 5712 | u32 if_sid; |
| 5713 | u32 node_sid; |
| 5714 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5715 | if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5716 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5717 | if (avc_has_perm(&selinux_state, |
| 5718 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5719 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5720 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5721 | |
| 5722 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5723 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5724 | if (avc_has_perm(&selinux_state, |
| 5725 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5726 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5727 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | return NF_ACCEPT; |
| 5731 | } |
| 5732 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5733 | static unsigned int selinux_ipv4_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5734 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5735 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5736 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5737 | return selinux_ip_postroute(skb, state->out, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | } |
| 5739 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5740 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5741 | static unsigned int selinux_ipv6_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5742 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5743 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5745 | return selinux_ip_postroute(skb, state->out, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | #endif /* IPV6 */ |
| 5748 | |
| 5749 | #endif /* CONFIG_NETFILTER */ |
| 5750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 5752 | { |
Stephen Smalley | 941fc5b | 2009-10-01 14:48:23 -0400 | [diff] [blame] | 5753 | return selinux_nlmsg_perm(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | } |
| 5755 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5756 | static int ipc_alloc_security(struct kern_ipc_perm *perm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | u16 sclass) |
| 5758 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | struct ipc_security_struct *isec; |
| 5760 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5761 | isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | if (!isec) |
| 5763 | return -ENOMEM; |
| 5764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | isec->sclass = sclass; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5766 | isec->sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | perm->security = isec; |
| 5768 | |
| 5769 | return 0; |
| 5770 | } |
| 5771 | |
| 5772 | static void ipc_free_security(struct kern_ipc_perm *perm) |
| 5773 | { |
| 5774 | struct ipc_security_struct *isec = perm->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | perm->security = NULL; |
| 5776 | kfree(isec); |
| 5777 | } |
| 5778 | |
| 5779 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 5780 | { |
| 5781 | struct msg_security_struct *msec; |
| 5782 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5783 | msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | if (!msec) |
| 5785 | return -ENOMEM; |
| 5786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | msec->sid = SECINITSID_UNLABELED; |
| 5788 | msg->security = msec; |
| 5789 | |
| 5790 | return 0; |
| 5791 | } |
| 5792 | |
| 5793 | static void msg_msg_free_security(struct msg_msg *msg) |
| 5794 | { |
| 5795 | struct msg_security_struct *msec = msg->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | |
| 5797 | msg->security = NULL; |
| 5798 | kfree(msec); |
| 5799 | } |
| 5800 | |
| 5801 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5802 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5805 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5806 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | isec = ipc_perms->security; |
| 5809 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5810 | ad.type = LSM_AUDIT_DATA_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | ad.u.ipc_id = ipc_perms->key; |
| 5812 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5813 | return avc_has_perm(&selinux_state, |
| 5814 | sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 5818 | { |
| 5819 | return msg_msg_alloc_security(msg); |
| 5820 | } |
| 5821 | |
| 5822 | static void selinux_msg_msg_free_security(struct msg_msg *msg) |
| 5823 | { |
| 5824 | msg_msg_free_security(msg); |
| 5825 | } |
| 5826 | |
| 5827 | /* message queue security operations */ |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5828 | static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5831 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5832 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | int rc; |
| 5834 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5835 | rc = ipc_alloc_security(msq, SECCLASS_MSGQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | if (rc) |
| 5837 | return rc; |
| 5838 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5839 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5841 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5842 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5844 | rc = avc_has_perm(&selinux_state, |
| 5845 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | MSGQ__CREATE, &ad); |
| 5847 | if (rc) { |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5848 | ipc_free_security(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | return rc; |
| 5850 | } |
| 5851 | return 0; |
| 5852 | } |
| 5853 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5854 | static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | { |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5856 | ipc_free_security(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | } |
| 5858 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5859 | static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5862 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5863 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5865 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5867 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5868 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5870 | return avc_has_perm(&selinux_state, |
| 5871 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | MSGQ__ASSOCIATE, &ad); |
| 5873 | } |
| 5874 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5875 | static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | { |
| 5877 | int err; |
| 5878 | int perms; |
| 5879 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5880 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | case IPC_INFO: |
| 5882 | case MSG_INFO: |
| 5883 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5884 | return avc_has_perm(&selinux_state, |
| 5885 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5886 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | case IPC_STAT: |
| 5888 | case MSG_STAT: |
Davidlohr Bueso | 23c8cec | 2018-04-10 16:35:30 -0700 | [diff] [blame] | 5889 | case MSG_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5891 | break; |
| 5892 | case IPC_SET: |
| 5893 | perms = MSGQ__SETATTR; |
| 5894 | break; |
| 5895 | case IPC_RMID: |
| 5896 | perms = MSGQ__DESTROY; |
| 5897 | break; |
| 5898 | default: |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5902 | err = ipc_has_perm(msq, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | return err; |
| 5904 | } |
| 5905 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5906 | static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | struct ipc_security_struct *isec; |
| 5909 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5910 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5911 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | int rc; |
| 5913 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5914 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | msec = msg->security; |
| 5916 | |
| 5917 | /* |
| 5918 | * First time through, need to assign label to the message |
| 5919 | */ |
| 5920 | if (msec->sid == SECINITSID_UNLABELED) { |
| 5921 | /* |
| 5922 | * Compute new sid based on current process and |
| 5923 | * message queue this message will be stored in |
| 5924 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5925 | rc = security_transition_sid(&selinux_state, sid, isec->sid, |
| 5926 | SECCLASS_MSG, NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | if (rc) |
| 5928 | return rc; |
| 5929 | } |
| 5930 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5931 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5932 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | |
| 5934 | /* Can this process write to the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5935 | rc = avc_has_perm(&selinux_state, |
| 5936 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | MSGQ__WRITE, &ad); |
| 5938 | if (!rc) |
| 5939 | /* Can this process send the message */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5940 | rc = avc_has_perm(&selinux_state, |
| 5941 | sid, msec->sid, SECCLASS_MSG, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5942 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | if (!rc) |
| 5944 | /* Can the message be put in the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5945 | rc = avc_has_perm(&selinux_state, |
| 5946 | msec->sid, isec->sid, SECCLASS_MSGQ, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5947 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | |
| 5949 | return rc; |
| 5950 | } |
| 5951 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5952 | static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | struct task_struct *target, |
| 5954 | long type, int mode) |
| 5955 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | struct ipc_security_struct *isec; |
| 5957 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5958 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5959 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | int rc; |
| 5961 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5962 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | msec = msg->security; |
| 5964 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5965 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5966 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5968 | rc = avc_has_perm(&selinux_state, |
| 5969 | sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 5971 | if (!rc) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5972 | rc = avc_has_perm(&selinux_state, |
| 5973 | sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 5975 | return rc; |
| 5976 | } |
| 5977 | |
| 5978 | /* Shared Memory security operations */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5979 | static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5982 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5983 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | int rc; |
| 5985 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5986 | rc = ipc_alloc_security(shp, SECCLASS_SHM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | if (rc) |
| 5988 | return rc; |
| 5989 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5990 | isec = shp->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5992 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5993 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5995 | rc = avc_has_perm(&selinux_state, |
| 5996 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | SHM__CREATE, &ad); |
| 5998 | if (rc) { |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5999 | ipc_free_security(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | return rc; |
| 6001 | } |
| 6002 | return 0; |
| 6003 | } |
| 6004 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6005 | static void selinux_shm_free_security(struct kern_ipc_perm *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | { |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6007 | ipc_free_security(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | } |
| 6009 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6010 | static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6013 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6014 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6016 | isec = shp->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6018 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6019 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6021 | return avc_has_perm(&selinux_state, |
| 6022 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | SHM__ASSOCIATE, &ad); |
| 6024 | } |
| 6025 | |
| 6026 | /* Note, at this point, shp is locked down */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6027 | static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | { |
| 6029 | int perms; |
| 6030 | int err; |
| 6031 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6032 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | case IPC_INFO: |
| 6034 | case SHM_INFO: |
| 6035 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6036 | return avc_has_perm(&selinux_state, |
| 6037 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6038 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | case IPC_STAT: |
| 6040 | case SHM_STAT: |
Davidlohr Bueso | c21a697 | 2018-04-10 16:35:23 -0700 | [diff] [blame] | 6041 | case SHM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 6043 | break; |
| 6044 | case IPC_SET: |
| 6045 | perms = SHM__SETATTR; |
| 6046 | break; |
| 6047 | case SHM_LOCK: |
| 6048 | case SHM_UNLOCK: |
| 6049 | perms = SHM__LOCK; |
| 6050 | break; |
| 6051 | case IPC_RMID: |
| 6052 | perms = SHM__DESTROY; |
| 6053 | break; |
| 6054 | default: |
| 6055 | return 0; |
| 6056 | } |
| 6057 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6058 | err = ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | return err; |
| 6060 | } |
| 6061 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6062 | static int selinux_shm_shmat(struct kern_ipc_perm *shp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | char __user *shmaddr, int shmflg) |
| 6064 | { |
| 6065 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | |
| 6067 | if (shmflg & SHM_RDONLY) |
| 6068 | perms = SHM__READ; |
| 6069 | else |
| 6070 | perms = SHM__READ | SHM__WRITE; |
| 6071 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6072 | return ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /* Semaphore security operations */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6076 | static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6079 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6080 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | int rc; |
| 6082 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6083 | rc = ipc_alloc_security(sma, SECCLASS_SEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | if (rc) |
| 6085 | return rc; |
| 6086 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6087 | isec = sma->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6089 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6090 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6092 | rc = avc_has_perm(&selinux_state, |
| 6093 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | SEM__CREATE, &ad); |
| 6095 | if (rc) { |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6096 | ipc_free_security(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | return rc; |
| 6098 | } |
| 6099 | return 0; |
| 6100 | } |
| 6101 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6102 | static void selinux_sem_free_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | { |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6104 | ipc_free_security(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | } |
| 6106 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6107 | static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6110 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6111 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6113 | isec = sma->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6115 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6116 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6118 | return avc_has_perm(&selinux_state, |
| 6119 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | SEM__ASSOCIATE, &ad); |
| 6121 | } |
| 6122 | |
| 6123 | /* Note, at this point, sma is locked down */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6124 | static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | { |
| 6126 | int err; |
| 6127 | u32 perms; |
| 6128 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6129 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | case IPC_INFO: |
| 6131 | case SEM_INFO: |
| 6132 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6133 | return avc_has_perm(&selinux_state, |
| 6134 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6135 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | case GETPID: |
| 6137 | case GETNCNT: |
| 6138 | case GETZCNT: |
| 6139 | perms = SEM__GETATTR; |
| 6140 | break; |
| 6141 | case GETVAL: |
| 6142 | case GETALL: |
| 6143 | perms = SEM__READ; |
| 6144 | break; |
| 6145 | case SETVAL: |
| 6146 | case SETALL: |
| 6147 | perms = SEM__WRITE; |
| 6148 | break; |
| 6149 | case IPC_RMID: |
| 6150 | perms = SEM__DESTROY; |
| 6151 | break; |
| 6152 | case IPC_SET: |
| 6153 | perms = SEM__SETATTR; |
| 6154 | break; |
| 6155 | case IPC_STAT: |
| 6156 | case SEM_STAT: |
Davidlohr Bueso | a280d6d | 2018-04-10 16:35:26 -0700 | [diff] [blame] | 6157 | case SEM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 6159 | break; |
| 6160 | default: |
| 6161 | return 0; |
| 6162 | } |
| 6163 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6164 | err = ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | return err; |
| 6166 | } |
| 6167 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6168 | static int selinux_sem_semop(struct kern_ipc_perm *sma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | struct sembuf *sops, unsigned nsops, int alter) |
| 6170 | { |
| 6171 | u32 perms; |
| 6172 | |
| 6173 | if (alter) |
| 6174 | perms = SEM__READ | SEM__WRITE; |
| 6175 | else |
| 6176 | perms = SEM__READ; |
| 6177 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6178 | return ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 6182 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | u32 av = 0; |
| 6184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | av = 0; |
| 6186 | if (flag & S_IRUGO) |
| 6187 | av |= IPC__UNIX_READ; |
| 6188 | if (flag & S_IWUGO) |
| 6189 | av |= IPC__UNIX_WRITE; |
| 6190 | |
| 6191 | if (av == 0) |
| 6192 | return 0; |
| 6193 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 6194 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | } |
| 6196 | |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6197 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 6198 | { |
| 6199 | struct ipc_security_struct *isec = ipcp->security; |
| 6200 | *secid = isec->sid; |
| 6201 | } |
| 6202 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6203 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | { |
| 6205 | if (inode) |
| 6206 | inode_doinit_with_dentry(inode, dentry); |
| 6207 | } |
| 6208 | |
| 6209 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6210 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6212 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 6213 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6215 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6217 | rcu_read_lock(); |
| 6218 | __tsec = __task_cred(p)->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6220 | if (current != p) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6221 | error = avc_has_perm(&selinux_state, |
| 6222 | current_sid(), __tsec->sid, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6223 | SECCLASS_PROCESS, PROCESS__GETATTR, NULL); |
| 6224 | if (error) |
| 6225 | goto bad; |
| 6226 | } |
| 6227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6229 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6231 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6233 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6235 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6236 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6237 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6238 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6239 | sid = __tsec->sockcreate_sid; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6240 | else { |
| 6241 | error = -EINVAL; |
| 6242 | goto bad; |
| 6243 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6244 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | |
| 6246 | if (!sid) |
| 6247 | return 0; |
| 6248 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6249 | error = security_sid_to_context(&selinux_state, sid, value, &len); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6250 | if (error) |
| 6251 | return error; |
| 6252 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6253 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6254 | bad: |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6255 | rcu_read_unlock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6256 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | } |
| 6258 | |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6259 | static int selinux_setprocattr(const char *name, void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | { |
| 6261 | struct task_security_struct *tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6262 | struct cred *new; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6263 | u32 mysid = current_sid(), sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | int error; |
| 6265 | char *str = value; |
| 6266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | /* |
| 6268 | * Basic control over ability to set these attributes at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | */ |
| 6270 | if (!strcmp(name, "exec")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6271 | error = avc_has_perm(&selinux_state, |
| 6272 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6273 | PROCESS__SETEXEC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | else if (!strcmp(name, "fscreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6275 | error = avc_has_perm(&selinux_state, |
| 6276 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6277 | PROCESS__SETFSCREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6278 | else if (!strcmp(name, "keycreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6279 | error = avc_has_perm(&selinux_state, |
| 6280 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6281 | PROCESS__SETKEYCREATE, NULL); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6282 | else if (!strcmp(name, "sockcreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6283 | error = avc_has_perm(&selinux_state, |
| 6284 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6285 | PROCESS__SETSOCKCREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | else if (!strcmp(name, "current")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6287 | error = avc_has_perm(&selinux_state, |
| 6288 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6289 | PROCESS__SETCURRENT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | else |
| 6291 | error = -EINVAL; |
| 6292 | if (error) |
| 6293 | return error; |
| 6294 | |
| 6295 | /* Obtain a SID for the context, if one was specified. */ |
Stephen Smalley | a050a57 | 2017-01-31 11:54:04 -0500 | [diff] [blame] | 6296 | if (size && str[0] && str[0] != '\n') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | if (str[size-1] == '\n') { |
| 6298 | str[size-1] = 0; |
| 6299 | size--; |
| 6300 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6301 | error = security_context_to_sid(&selinux_state, value, size, |
| 6302 | &sid, GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6303 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6304 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6305 | struct audit_buffer *ab; |
| 6306 | size_t audit_size; |
| 6307 | |
| 6308 | /* We strip a nul only if it is at the end, otherwise the |
| 6309 | * context contains a nul and we should audit that */ |
| 6310 | if (str[size - 1] == '\0') |
| 6311 | audit_size = size - 1; |
| 6312 | else |
| 6313 | audit_size = size; |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 6314 | ab = audit_log_start(audit_context(), |
| 6315 | GFP_ATOMIC, |
| 6316 | AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6317 | audit_log_format(ab, "op=fscreate invalid_context="); |
| 6318 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 6319 | audit_log_end(ab); |
| 6320 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6321 | return error; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6322 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6323 | error = security_context_to_sid_force( |
| 6324 | &selinux_state, |
| 6325 | value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | if (error) |
| 6328 | return error; |
| 6329 | } |
| 6330 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6331 | new = prepare_creds(); |
| 6332 | if (!new) |
| 6333 | return -ENOMEM; |
| 6334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | /* Permission checking based on the specified context is |
| 6336 | performed during the actual operation (execve, |
| 6337 | open/mkdir/...), when we know the full context of the |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6338 | operation. See selinux_bprm_set_creds for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | checks and may_create for the file creation checks. The |
| 6340 | operation will then fail if the context is not permitted. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6341 | tsec = new->security; |
| 6342 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6344 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6346 | } else if (!strcmp(name, "keycreate")) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6347 | error = avc_has_perm(&selinux_state, |
| 6348 | mysid, sid, SECCLASS_KEY, KEY__CREATE, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6349 | NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6350 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6351 | goto abort_change; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6352 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6353 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6354 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6355 | } else if (!strcmp(name, "current")) { |
| 6356 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6358 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 6359 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6360 | /* Only allow single threaded processes to change context */ |
| 6361 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 6362 | if (!current_is_single_threaded()) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6363 | error = security_bounded_transition(&selinux_state, |
| 6364 | tsec->sid, sid); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6365 | if (error) |
| 6366 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | |
| 6369 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6370 | error = avc_has_perm(&selinux_state, |
| 6371 | tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6372 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6374 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
| 6376 | /* Check for ptracing, and update the task SID if ok. |
| 6377 | Otherwise, leave SID unchanged and fail. */ |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6378 | ptsid = ptrace_parent_sid(); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 6379 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6380 | error = avc_has_perm(&selinux_state, |
| 6381 | ptsid, sid, SECCLASS_PROCESS, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6382 | PROCESS__PTRACE, NULL); |
| 6383 | if (error) |
| 6384 | goto abort_change; |
| 6385 | } |
| 6386 | |
| 6387 | tsec->sid = sid; |
| 6388 | } else { |
| 6389 | error = -EINVAL; |
| 6390 | goto abort_change; |
| 6391 | } |
| 6392 | |
| 6393 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6395 | |
| 6396 | abort_change: |
| 6397 | abort_creds(new); |
| 6398 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | } |
| 6400 | |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 6401 | static int selinux_ismaclabel(const char *name) |
| 6402 | { |
| 6403 | return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); |
| 6404 | } |
| 6405 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6406 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 6407 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6408 | return security_sid_to_context(&selinux_state, secid, |
| 6409 | secdata, seclen); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6410 | } |
| 6411 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 6412 | static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 6413 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6414 | return security_context_to_sid(&selinux_state, secdata, seclen, |
| 6415 | secid, GFP_KERNEL); |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6418 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 6419 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 6420 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6421 | } |
| 6422 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6423 | static void selinux_inode_invalidate_secctx(struct inode *inode) |
| 6424 | { |
| 6425 | struct inode_security_struct *isec = inode->i_security; |
| 6426 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6427 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6428 | isec->initialized = LABEL_INVALID; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6429 | spin_unlock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6430 | } |
| 6431 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6432 | /* |
| 6433 | * called with inode->i_mutex locked |
| 6434 | */ |
| 6435 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 6436 | { |
| 6437 | return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); |
| 6438 | } |
| 6439 | |
| 6440 | /* |
| 6441 | * called with inode->i_mutex locked |
| 6442 | */ |
| 6443 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 6444 | { |
| 6445 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 6446 | } |
| 6447 | |
| 6448 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 6449 | { |
| 6450 | int len = 0; |
| 6451 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6452 | ctx, true); |
| 6453 | if (len < 0) |
| 6454 | return len; |
| 6455 | *ctxlen = len; |
| 6456 | return 0; |
| 6457 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6458 | #ifdef CONFIG_KEYS |
| 6459 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6460 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 6461 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6462 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6463 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6464 | struct key_security_struct *ksec; |
| 6465 | |
| 6466 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 6467 | if (!ksec) |
| 6468 | return -ENOMEM; |
| 6469 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6470 | tsec = cred->security; |
| 6471 | if (tsec->keycreate_sid) |
| 6472 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6473 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6474 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6475 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6476 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6477 | return 0; |
| 6478 | } |
| 6479 | |
| 6480 | static void selinux_key_free(struct key *k) |
| 6481 | { |
| 6482 | struct key_security_struct *ksec = k->security; |
| 6483 | |
| 6484 | k->security = NULL; |
| 6485 | kfree(ksec); |
| 6486 | } |
| 6487 | |
| 6488 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6489 | const struct cred *cred, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 6490 | unsigned perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6491 | { |
| 6492 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6493 | struct key_security_struct *ksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6494 | u32 sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6495 | |
| 6496 | /* if no specific permissions are requested, we skip the |
| 6497 | permission check. No serious, additional covert channels |
| 6498 | appear to be created. */ |
| 6499 | if (perm == 0) |
| 6500 | return 0; |
| 6501 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6502 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6503 | |
| 6504 | key = key_ref_to_ptr(key_ref); |
| 6505 | ksec = key->security; |
| 6506 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6507 | return avc_has_perm(&selinux_state, |
| 6508 | sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6509 | } |
| 6510 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6511 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 6512 | { |
| 6513 | struct key_security_struct *ksec = key->security; |
| 6514 | char *context = NULL; |
| 6515 | unsigned len; |
| 6516 | int rc; |
| 6517 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6518 | rc = security_sid_to_context(&selinux_state, ksec->sid, |
| 6519 | &context, &len); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6520 | if (!rc) |
| 6521 | rc = len; |
| 6522 | *_buffer = context; |
| 6523 | return rc; |
| 6524 | } |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6525 | #endif |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6526 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6527 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6528 | static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) |
| 6529 | { |
| 6530 | struct common_audit_data ad; |
| 6531 | int err; |
| 6532 | u32 sid = 0; |
| 6533 | struct ib_security_struct *sec = ib_sec; |
| 6534 | struct lsm_ibpkey_audit ibpkey; |
| 6535 | |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 6536 | err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6537 | if (err) |
| 6538 | return err; |
| 6539 | |
| 6540 | ad.type = LSM_AUDIT_DATA_IBPKEY; |
| 6541 | ibpkey.subnet_prefix = subnet_prefix; |
| 6542 | ibpkey.pkey = pkey_val; |
| 6543 | ad.u.ibpkey = &ibpkey; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6544 | return avc_has_perm(&selinux_state, |
| 6545 | sec->sid, sid, |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6546 | SECCLASS_INFINIBAND_PKEY, |
| 6547 | INFINIBAND_PKEY__ACCESS, &ad); |
| 6548 | } |
| 6549 | |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6550 | static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, |
| 6551 | u8 port_num) |
| 6552 | { |
| 6553 | struct common_audit_data ad; |
| 6554 | int err; |
| 6555 | u32 sid = 0; |
| 6556 | struct ib_security_struct *sec = ib_sec; |
| 6557 | struct lsm_ibendport_audit ibendport; |
| 6558 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6559 | err = security_ib_endport_sid(&selinux_state, dev_name, port_num, |
| 6560 | &sid); |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6561 | |
| 6562 | if (err) |
| 6563 | return err; |
| 6564 | |
| 6565 | ad.type = LSM_AUDIT_DATA_IBENDPORT; |
| 6566 | strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); |
| 6567 | ibendport.port = port_num; |
| 6568 | ad.u.ibendport = &ibendport; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6569 | return avc_has_perm(&selinux_state, |
| 6570 | sec->sid, sid, |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6571 | SECCLASS_INFINIBAND_ENDPORT, |
| 6572 | INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); |
| 6573 | } |
| 6574 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6575 | static int selinux_ib_alloc_security(void **ib_sec) |
| 6576 | { |
| 6577 | struct ib_security_struct *sec; |
| 6578 | |
| 6579 | sec = kzalloc(sizeof(*sec), GFP_KERNEL); |
| 6580 | if (!sec) |
| 6581 | return -ENOMEM; |
| 6582 | sec->sid = current_sid(); |
| 6583 | |
| 6584 | *ib_sec = sec; |
| 6585 | return 0; |
| 6586 | } |
| 6587 | |
| 6588 | static void selinux_ib_free_security(void *ib_sec) |
| 6589 | { |
| 6590 | kfree(ib_sec); |
| 6591 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6592 | #endif |
| 6593 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6594 | #ifdef CONFIG_BPF_SYSCALL |
| 6595 | static int selinux_bpf(int cmd, union bpf_attr *attr, |
| 6596 | unsigned int size) |
| 6597 | { |
| 6598 | u32 sid = current_sid(); |
| 6599 | int ret; |
| 6600 | |
| 6601 | switch (cmd) { |
| 6602 | case BPF_MAP_CREATE: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6603 | ret = avc_has_perm(&selinux_state, |
| 6604 | sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6605 | NULL); |
| 6606 | break; |
| 6607 | case BPF_PROG_LOAD: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6608 | ret = avc_has_perm(&selinux_state, |
| 6609 | sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6610 | NULL); |
| 6611 | break; |
| 6612 | default: |
| 6613 | ret = 0; |
| 6614 | break; |
| 6615 | } |
| 6616 | |
| 6617 | return ret; |
| 6618 | } |
| 6619 | |
| 6620 | static u32 bpf_map_fmode_to_av(fmode_t fmode) |
| 6621 | { |
| 6622 | u32 av = 0; |
| 6623 | |
| 6624 | if (fmode & FMODE_READ) |
| 6625 | av |= BPF__MAP_READ; |
| 6626 | if (fmode & FMODE_WRITE) |
| 6627 | av |= BPF__MAP_WRITE; |
| 6628 | return av; |
| 6629 | } |
| 6630 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6631 | /* This function will check the file pass through unix socket or binder to see |
| 6632 | * if it is a bpf related object. And apply correspinding checks on the bpf |
| 6633 | * object based on the type. The bpf maps and programs, not like other files and |
| 6634 | * socket, are using a shared anonymous inode inside the kernel as their inode. |
| 6635 | * So checking that inode cannot identify if the process have privilege to |
| 6636 | * access the bpf object and that's why we have to add this additional check in |
| 6637 | * selinux_file_receive and selinux_binder_transfer_files. |
| 6638 | */ |
| 6639 | static int bpf_fd_pass(struct file *file, u32 sid) |
| 6640 | { |
| 6641 | struct bpf_security_struct *bpfsec; |
| 6642 | struct bpf_prog *prog; |
| 6643 | struct bpf_map *map; |
| 6644 | int ret; |
| 6645 | |
| 6646 | if (file->f_op == &bpf_map_fops) { |
| 6647 | map = file->private_data; |
| 6648 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6649 | ret = avc_has_perm(&selinux_state, |
| 6650 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6651 | bpf_map_fmode_to_av(file->f_mode), NULL); |
| 6652 | if (ret) |
| 6653 | return ret; |
| 6654 | } else if (file->f_op == &bpf_prog_fops) { |
| 6655 | prog = file->private_data; |
| 6656 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6657 | ret = avc_has_perm(&selinux_state, |
| 6658 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6659 | BPF__PROG_RUN, NULL); |
| 6660 | if (ret) |
| 6661 | return ret; |
| 6662 | } |
| 6663 | return 0; |
| 6664 | } |
| 6665 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6666 | static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) |
| 6667 | { |
| 6668 | u32 sid = current_sid(); |
| 6669 | struct bpf_security_struct *bpfsec; |
| 6670 | |
| 6671 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6672 | return avc_has_perm(&selinux_state, |
| 6673 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6674 | bpf_map_fmode_to_av(fmode), NULL); |
| 6675 | } |
| 6676 | |
| 6677 | static int selinux_bpf_prog(struct bpf_prog *prog) |
| 6678 | { |
| 6679 | u32 sid = current_sid(); |
| 6680 | struct bpf_security_struct *bpfsec; |
| 6681 | |
| 6682 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6683 | return avc_has_perm(&selinux_state, |
| 6684 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6685 | BPF__PROG_RUN, NULL); |
| 6686 | } |
| 6687 | |
| 6688 | static int selinux_bpf_map_alloc(struct bpf_map *map) |
| 6689 | { |
| 6690 | struct bpf_security_struct *bpfsec; |
| 6691 | |
| 6692 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6693 | if (!bpfsec) |
| 6694 | return -ENOMEM; |
| 6695 | |
| 6696 | bpfsec->sid = current_sid(); |
| 6697 | map->security = bpfsec; |
| 6698 | |
| 6699 | return 0; |
| 6700 | } |
| 6701 | |
| 6702 | static void selinux_bpf_map_free(struct bpf_map *map) |
| 6703 | { |
| 6704 | struct bpf_security_struct *bpfsec = map->security; |
| 6705 | |
| 6706 | map->security = NULL; |
| 6707 | kfree(bpfsec); |
| 6708 | } |
| 6709 | |
| 6710 | static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) |
| 6711 | { |
| 6712 | struct bpf_security_struct *bpfsec; |
| 6713 | |
| 6714 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6715 | if (!bpfsec) |
| 6716 | return -ENOMEM; |
| 6717 | |
| 6718 | bpfsec->sid = current_sid(); |
| 6719 | aux->security = bpfsec; |
| 6720 | |
| 6721 | return 0; |
| 6722 | } |
| 6723 | |
| 6724 | static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) |
| 6725 | { |
| 6726 | struct bpf_security_struct *bpfsec = aux->security; |
| 6727 | |
| 6728 | aux->security = NULL; |
| 6729 | kfree(bpfsec); |
| 6730 | } |
| 6731 | #endif |
| 6732 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 6733 | static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6734 | LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), |
| 6735 | LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), |
| 6736 | LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), |
| 6737 | LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6738 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6739 | LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), |
| 6740 | LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), |
| 6741 | LSM_HOOK_INIT(capget, selinux_capget), |
| 6742 | LSM_HOOK_INIT(capset, selinux_capset), |
| 6743 | LSM_HOOK_INIT(capable, selinux_capable), |
| 6744 | LSM_HOOK_INIT(quotactl, selinux_quotactl), |
| 6745 | LSM_HOOK_INIT(quota_on, selinux_quota_on), |
| 6746 | LSM_HOOK_INIT(syslog, selinux_syslog), |
| 6747 | LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 6748 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6749 | LSM_HOOK_INIT(netlink_send, selinux_netlink_send), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6751 | LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), |
| 6752 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 6753 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6755 | LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), |
| 6756 | LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 6757 | LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 6758 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6759 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), |
| 6760 | LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), |
| 6761 | LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), |
| 6762 | LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), |
| 6763 | LSM_HOOK_INIT(sb_mount, selinux_mount), |
| 6764 | LSM_HOOK_INIT(sb_umount, selinux_umount), |
| 6765 | LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), |
| 6766 | LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), |
| 6767 | LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6769 | LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 6770 | LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 6771 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6772 | LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), |
| 6773 | LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), |
| 6774 | LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), |
| 6775 | LSM_HOOK_INIT(inode_create, selinux_inode_create), |
| 6776 | LSM_HOOK_INIT(inode_link, selinux_inode_link), |
| 6777 | LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), |
| 6778 | LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), |
| 6779 | LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), |
| 6780 | LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), |
| 6781 | LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), |
| 6782 | LSM_HOOK_INIT(inode_rename, selinux_inode_rename), |
| 6783 | LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), |
| 6784 | LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), |
| 6785 | LSM_HOOK_INIT(inode_permission, selinux_inode_permission), |
| 6786 | LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), |
| 6787 | LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), |
| 6788 | LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), |
| 6789 | LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), |
| 6790 | LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), |
| 6791 | LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), |
| 6792 | LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), |
| 6793 | LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), |
| 6794 | LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), |
| 6795 | LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), |
| 6796 | LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 6797 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 6798 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6800 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
| 6801 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
| 6802 | LSM_HOOK_INIT(file_free_security, selinux_file_free_security), |
| 6803 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |
| 6804 | LSM_HOOK_INIT(mmap_file, selinux_mmap_file), |
| 6805 | LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), |
| 6806 | LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), |
| 6807 | LSM_HOOK_INIT(file_lock, selinux_file_lock), |
| 6808 | LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), |
| 6809 | LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), |
| 6810 | LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), |
| 6811 | LSM_HOOK_INIT(file_receive, selinux_file_receive), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6813 | LSM_HOOK_INIT(file_open, selinux_file_open), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 6815 | LSM_HOOK_INIT(task_alloc, selinux_task_alloc), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6816 | LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), |
| 6817 | LSM_HOOK_INIT(cred_free, selinux_cred_free), |
| 6818 | LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), |
| 6819 | LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 6820 | LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6821 | LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), |
| 6822 | LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), |
| 6823 | LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 6824 | LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 6825 | LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6826 | LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), |
| 6827 | LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), |
| 6828 | LSM_HOOK_INIT(task_getsid, selinux_task_getsid), |
| 6829 | LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), |
| 6830 | LSM_HOOK_INIT(task_setnice, selinux_task_setnice), |
| 6831 | LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), |
| 6832 | LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 6833 | LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6834 | LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), |
| 6835 | LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), |
| 6836 | LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), |
| 6837 | LSM_HOOK_INIT(task_movememory, selinux_task_movememory), |
| 6838 | LSM_HOOK_INIT(task_kill, selinux_task_kill), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6839 | LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 6840 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6841 | LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), |
| 6842 | LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6844 | LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), |
| 6845 | LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6847 | LSM_HOOK_INIT(msg_queue_alloc_security, |
| 6848 | selinux_msg_queue_alloc_security), |
| 6849 | LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), |
| 6850 | LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), |
| 6851 | LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), |
| 6852 | LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), |
| 6853 | LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6855 | LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), |
| 6856 | LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), |
| 6857 | LSM_HOOK_INIT(shm_associate, selinux_shm_associate), |
| 6858 | LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), |
| 6859 | LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6861 | LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), |
| 6862 | LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), |
| 6863 | LSM_HOOK_INIT(sem_associate, selinux_sem_associate), |
| 6864 | LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), |
| 6865 | LSM_HOOK_INIT(sem_semop, selinux_sem_semop), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6867 | LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6869 | LSM_HOOK_INIT(getprocattr, selinux_getprocattr), |
| 6870 | LSM_HOOK_INIT(setprocattr, selinux_setprocattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6872 | LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), |
| 6873 | LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), |
| 6874 | LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), |
| 6875 | LSM_HOOK_INIT(release_secctx, selinux_release_secctx), |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6876 | LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6877 | LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), |
| 6878 | LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), |
| 6879 | LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6881 | LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), |
| 6882 | LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6883 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6884 | LSM_HOOK_INIT(socket_create, selinux_socket_create), |
| 6885 | LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 6886 | LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6887 | LSM_HOOK_INIT(socket_bind, selinux_socket_bind), |
| 6888 | LSM_HOOK_INIT(socket_connect, selinux_socket_connect), |
| 6889 | LSM_HOOK_INIT(socket_listen, selinux_socket_listen), |
| 6890 | LSM_HOOK_INIT(socket_accept, selinux_socket_accept), |
| 6891 | LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), |
| 6892 | LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), |
| 6893 | LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), |
| 6894 | LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), |
| 6895 | LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), |
| 6896 | LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), |
| 6897 | LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), |
| 6898 | LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), |
| 6899 | LSM_HOOK_INIT(socket_getpeersec_stream, |
| 6900 | selinux_socket_getpeersec_stream), |
| 6901 | LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), |
| 6902 | LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), |
| 6903 | LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), |
| 6904 | LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), |
| 6905 | LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), |
| 6906 | LSM_HOOK_INIT(sock_graft, selinux_sock_graft), |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 6907 | LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), |
| 6908 | LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), |
| 6909 | LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6910 | LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), |
| 6911 | LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), |
| 6912 | LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), |
| 6913 | LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), |
| 6914 | LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), |
| 6915 | LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), |
| 6916 | LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), |
| 6917 | LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), |
| 6918 | LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), |
| 6919 | LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), |
| 6920 | LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), |
| 6921 | LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), |
| 6922 | LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6923 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6924 | LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6925 | LSM_HOOK_INIT(ib_endport_manage_subnet, |
| 6926 | selinux_ib_endport_manage_subnet), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6927 | LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), |
| 6928 | LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), |
| 6929 | #endif |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6930 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6931 | LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), |
| 6932 | LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), |
| 6933 | LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), |
| 6934 | LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), |
| 6935 | LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), |
| 6936 | LSM_HOOK_INIT(xfrm_state_alloc_acquire, |
| 6937 | selinux_xfrm_state_alloc_acquire), |
| 6938 | LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), |
| 6939 | LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), |
| 6940 | LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), |
| 6941 | LSM_HOOK_INIT(xfrm_state_pol_flow_match, |
| 6942 | selinux_xfrm_state_pol_flow_match), |
| 6943 | LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6945 | |
| 6946 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6947 | LSM_HOOK_INIT(key_alloc, selinux_key_alloc), |
| 6948 | LSM_HOOK_INIT(key_free, selinux_key_free), |
| 6949 | LSM_HOOK_INIT(key_permission, selinux_key_permission), |
| 6950 | LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6951 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6952 | |
| 6953 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6954 | LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), |
| 6955 | LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), |
| 6956 | LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), |
| 6957 | LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6958 | #endif |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6959 | |
| 6960 | #ifdef CONFIG_BPF_SYSCALL |
| 6961 | LSM_HOOK_INIT(bpf, selinux_bpf), |
| 6962 | LSM_HOOK_INIT(bpf_map, selinux_bpf_map), |
| 6963 | LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), |
| 6964 | LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), |
| 6965 | LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), |
| 6966 | LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), |
| 6967 | LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), |
| 6968 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | }; |
| 6970 | |
| 6971 | static __init int selinux_init(void) |
| 6972 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 6973 | if (!security_module_enable("selinux")) { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6974 | selinux_enabled = 0; |
| 6975 | return 0; |
| 6976 | } |
| 6977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | if (!selinux_enabled) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6979 | pr_info("SELinux: Disabled at boot.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | return 0; |
| 6981 | } |
| 6982 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6983 | pr_info("SELinux: Initializing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6985 | memset(&selinux_state, 0, sizeof(selinux_state)); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 6986 | enforcing_set(&selinux_state, selinux_enforcing_boot); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6987 | selinux_state.checkreqprot = selinux_checkreqprot_boot; |
| 6988 | selinux_ss_init(&selinux_state.ss); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6989 | selinux_avc_init(&selinux_state.avc); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6992 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 6994 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 6995 | |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 6996 | sel_inode_cache = kmem_cache_create("selinux_inode_security", |
| 6997 | sizeof(struct inode_security_struct), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 6998 | 0, SLAB_PANIC, NULL); |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 6999 | file_security_cache = kmem_cache_create("selinux_file_security", |
| 7000 | sizeof(struct file_security_struct), |
| 7001 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | avc_init(); |
| 7003 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7004 | avtab_cache_init(); |
| 7005 | |
| 7006 | ebitmap_cache_init(); |
| 7007 | |
| 7008 | hashtab_cache_init(); |
| 7009 | |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 7010 | security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7011 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 7012 | if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) |
| 7013 | panic("SELinux: Unable to register AVC netcache callback\n"); |
| 7014 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 7015 | if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) |
| 7016 | panic("SELinux: Unable to register AVC LSM notifier callback\n"); |
| 7017 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7018 | if (selinux_enforcing_boot) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7019 | pr_debug("SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7020 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7021 | pr_debug("SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 7022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | return 0; |
| 7024 | } |
| 7025 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7026 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 7027 | { |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 7028 | selinux_set_mnt_opts(sb, NULL, 0, NULL); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7029 | } |
| 7030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | void selinux_complete_init(void) |
| 7032 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7033 | pr_debug("SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | |
| 7035 | /* Set up any superblocks initialized prior to the policy load. */ |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7036 | pr_debug("SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7037 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | } |
| 7039 | |
| 7040 | /* SELinux requires early initialization in order to label |
| 7041 | all processes and objects when they are created. */ |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7042 | DEFINE_LSM(selinux) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 7043 | .name = "selinux", |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7044 | .init = selinux_init, |
| 7045 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7047 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | |
Florian Westphal | 591bb27 | 2017-07-26 11:40:52 +0200 | [diff] [blame] | 7049 | static const struct nf_hook_ops selinux_nf_ops[] = { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7050 | { |
| 7051 | .hook = selinux_ipv4_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7052 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7053 | .hooknum = NF_INET_POST_ROUTING, |
| 7054 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 7055 | }, |
| 7056 | { |
| 7057 | .hook = selinux_ipv4_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7058 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7059 | .hooknum = NF_INET_FORWARD, |
| 7060 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7061 | }, |
| 7062 | { |
| 7063 | .hook = selinux_ipv4_output, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7064 | .pf = NFPROTO_IPV4, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7065 | .hooknum = NF_INET_LOCAL_OUT, |
| 7066 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7067 | }, |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 7068 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7069 | { |
| 7070 | .hook = selinux_ipv6_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7071 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7072 | .hooknum = NF_INET_POST_ROUTING, |
| 7073 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 7074 | }, |
| 7075 | { |
| 7076 | .hook = selinux_ipv6_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7077 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7078 | .hooknum = NF_INET_FORWARD, |
| 7079 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7080 | }, |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 7081 | { |
| 7082 | .hook = selinux_ipv6_output, |
| 7083 | .pf = NFPROTO_IPV6, |
| 7084 | .hooknum = NF_INET_LOCAL_OUT, |
| 7085 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 7086 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | #endif /* IPV6 */ |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7088 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7089 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7090 | static int __net_init selinux_nf_register(struct net *net) |
| 7091 | { |
| 7092 | return nf_register_net_hooks(net, selinux_nf_ops, |
| 7093 | ARRAY_SIZE(selinux_nf_ops)); |
| 7094 | } |
| 7095 | |
| 7096 | static void __net_exit selinux_nf_unregister(struct net *net) |
| 7097 | { |
| 7098 | nf_unregister_net_hooks(net, selinux_nf_ops, |
| 7099 | ARRAY_SIZE(selinux_nf_ops)); |
| 7100 | } |
| 7101 | |
| 7102 | static struct pernet_operations selinux_net_ops = { |
| 7103 | .init = selinux_nf_register, |
| 7104 | .exit = selinux_nf_unregister, |
| 7105 | }; |
| 7106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | static int __init selinux_nf_ip_init(void) |
| 7108 | { |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7109 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | |
| 7111 | if (!selinux_enabled) |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7112 | return 0; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7113 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7114 | pr_debug("SELinux: Registering netfilter hooks\n"); |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7115 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7116 | err = register_pernet_subsys(&selinux_net_ops); |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 7117 | if (err) |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7118 | panic("SELinux: register_pernet_subsys: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | __initcall(selinux_nf_ip_init); |
| 7123 | |
| 7124 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7125 | static void selinux_nf_ip_exit(void) |
| 7126 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7127 | pr_debug("SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7129 | unregister_pernet_subsys(&selinux_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | } |
| 7131 | #endif |
| 7132 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7133 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | |
| 7135 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7136 | #define selinux_nf_ip_exit() |
| 7137 | #endif |
| 7138 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7139 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | |
| 7141 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7142 | int selinux_disable(struct selinux_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7144 | if (state->initialized) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | /* Not permitted after initial policy load. */ |
| 7146 | return -EINVAL; |
| 7147 | } |
| 7148 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7149 | if (state->disabled) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | /* Only do this once. */ |
| 7151 | return -EINVAL; |
| 7152 | } |
| 7153 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7154 | state->disabled = 1; |
| 7155 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7156 | pr_info("SELinux: Disabled at runtime.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7157 | |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 7158 | selinux_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 7160 | security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 7162 | /* Try to destroy the avc node cache */ |
| 7163 | avc_disable(); |
| 7164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | /* Unregister netfilter hooks. */ |
| 7166 | selinux_nf_ip_exit(); |
| 7167 | |
| 7168 | /* Unregister selinuxfs. */ |
| 7169 | exit_sel_fs(); |
| 7170 | |
| 7171 | return 0; |
| 7172 | } |
| 7173 | #endif |